| Issue |
233272
|
| Summary |
[X86] Redundant vpsllw/vpsraw on deep vector mask chains
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
veluca93
|
The following LLVM IR:
```
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define <16 x i16> @mask_recursion_limit(
<16 x i16> %v0, <16 x i16> %v1, <16 x i16> %v2, <16 x i16> %v3,
<16 x i16> %v4, <16 x i16> %v5, <16 x i16> %v6, <16 x i16> %val) #0 {
%c0 = icmp sgt <16 x i16> %v0, %v1
%c1 = icmp sgt <16 x i16> %v1, %v2
%c2 = icmp sle <16 x i16> %v2, %v3
%c3 = icmp sle <16 x i16> %v3, %v4
%c4 = icmp sle <16 x i16> %v4, %v5
%c5 = icmp sle <16 x i16> %v5, %v6
%c6 = icmp sle <16 x i16> %v6, %v0
%m0 = sext <16 x i1> %c0 to <16 x i16>
%m1 = sext <16 x i1> %c1 to <16 x i16>
%m2 = sext <16 x i1> %c2 to <16 x i16>
%m3 = sext <16 x i1> %c3 to <16 x i16>
%m4 = sext <16 x i1> %c4 to <16 x i16>
%m5 = sext <16 x i1> %c5 to <16 x i16>
%m6 = sext <16 x i1> %c6 to <16 x i16>
%op1 = or <16 x i16> %m0, %m1
%op2 = and <16 x i16> %op1, %m2
%op3 = and <16 x i16> %op2, %m3
%op4 = and <16 x i16> %op3, %m4
%op5 = and <16 x i16> %op4, %m5
%op6 = and <16 x i16> %op5, %m6
%res = and <16 x i16> %val, %op6
ret <16 x i16> %res
}
attributes #0 = { nounwind "target-cpu"="x86-64" "target-features"="+avx,+avx2" }
```
compiles (with `clang -O3`) to:
```
mask_recursion_limit:
vpcmpgtw ymm8, ymm0, ymm1
vpcmpgtw ymm1, ymm1, ymm2
vpor ymm1, ymm8, ymm1
vpcmpgtw ymm2, ymm2, ymm3
vpandn ymm1, ymm2, ymm1
vpcmpgtw ymm2, ymm3, ymm4
vpandn ymm1, ymm2, ymm1
vpcmpgtw ymm2, ymm4, ymm5
vpandn ymm1, ymm2, ymm1
vpcmpgtw ymm2, ymm5, ymm6
vpandn ymm1, ymm2, ymm1
vpcmpgtw ymm0, ymm6, ymm0
vpandn ymm0, ymm0, ymm1
vpsllw ymm0, ymm0, 15
vpsraw ymm0, ymm0, 15
vpand ymm0, ymm0, ymm7
ret
```
If compiling directly with LLC, I get the same code, except `vpsllw` and `vpsraw` are missing.
This is what Gemini has to say on the root cause - I do not know enough of the LLVM optimization passes to know if it is correct, but it seems plausible.
> When SelectionDAG legalizes `select <16 x i1> %op6, <16 x i16> %val, zeroinitializer` on AVX2, `v16i1` is promoted to `v16i16`. To convert a boolean mask with potentially unknown upper bits into an all-0 / all-1 lane mask for `vpand` / `vpblendvb`, X86 SelectionDAG emits `vpsllw $15` + `vpsraw $15` (or `vpcmpgtb` against zero for `<32 x i8>`), **unless** it can prove via `SelectionDAG::ComputeNumSignBits` that the mask already consists of all-0 / all-1 elements (`NumSignBits == 16`).
>
> - `SelectionDAG::ComputeNumSignBits` has a hardcoded recursion limit of **`SelectionDAG::MaxRecursionDepth = 6`**.
> - Each bitwise operation (`and`, `or`, `andnp`) in the mask tree increments `Depth` by 1.
> - When a mask is formed by a chain of **6** bitwise operations on comparisons (7 `icmp` leaves), `Depth` reaches `6` when visiting the leaf `SETCC` node. `ComputeNumSignBits` bails out at the recursion limit and returns `1` instead of `16`.
> - As a result, X86 ISel fails to see that the result of `vpcmpgtw` + `vpor` / `vpandn` is already an all-0 / all-1 mask, and emits a redundant `vpsllw $15` + `vpsraw $15` (or `vpxor` + `vpcmpgtb`) before `vpand`. With 5 bitwise operations instead of 6, those instructions disappear.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs