Issue 233274
Summary [X86] Unnecessary vpacksswb/vpmovsxbw round-trips
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 void @mask_in_loop(
    ptr noalias nocapture noundef nonnull align 32 %xs,
    i64 noundef %n,
    <16 x i16> %a, <16 x i16> %b, <16 x i16> %c) #0 {
entry:
  br label %loop

loop:
  %idx = phi i64 [ 0, %entry ], [ %idx.next, %loop ]
  %ptr = getelementptr inbounds nuw <16 x i16>, ptr %xs, i64 %idx
  %x = load <16 x i16>, ptr %ptr, align 32

  %c0 = icmp sgt <16 x i16> %a, %b
  %m0 = sext <16 x i1> %c0 to <16 x i16>

  %c1 = icmp sgt <16 x i16> %b, %c
  %m1 = sext <16 x i1> %c1 to <16 x i16>

  %c2 = icmp sgt <16 x i16> %x, %a
  %m2 = sext <16 x i1> %c2 to <16 x i16>

  %m01 = and <16 x i16> %m0, %m1
 %mask = and <16 x i16> %m01, %m2

  %res = and <16 x i16> %x, %mask
 store <16 x i16> %res, ptr %ptr, align 32

  %idx.next = add nuw nsw i64 %idx, 1
  %done = icmp eq i64 %idx.next, %n
  br i1 %done, label %exit, label %loop, !llvm.loop !0

exit:
  ret void
}

attributes #0 = { nounwind "target-cpu"="x86-64" "target-features"="+avx,+avx2" }
!0 = distinct !{!0, !1}
!1 = !{!"llvm.loop.unroll.disable"}
```

compiles to (with `clang -O3`):

```
mask_in_loop:
        vpcmpgtw        ymm3, ymm0, ymm1
        vpcmpgtw        ymm1, ymm1, ymm2
        vpand   ymm1, ymm3, ymm1
        vextracti128    xmm2, ymm1, 1
        vpacksswb xmm1, xmm1, xmm2
.LBB1_1:
        vmovdqa ymm2, ymmword ptr [rdi]
 vpcmpgtw        ymm3, ymm2, ymm0
        vextracti128    xmm4, ymm3, 1
 vpacksswb       xmm3, xmm3, xmm4
        vpand   xmm3, xmm1, xmm3
 vpmovsxbw       ymm3, xmm3
        vpand   ymm2, ymm3, ymm2
 vmovdqa ymmword ptr [rdi], ymm2
        add     rdi, 32
        dec rsi
        jne     .LBB1_1
        vzeroupper
        ret
```

If compiling directly with LLC, I get approximately same code, except vextracti128/vpacksswb/vpmovsxbw are missing.

This is what Gemini has to say on the root cause - I do not know enough about LLVM to know if it is correct, but it seems plausible.

1. **Middle-end (`InstCombine` + `LICM`)**: After `InstCombine` narrows the `<16 x i16>` mask operations to `<16 x i1>`, `LICM` hoists loop-invariant `<16 x i1>` mask subexpressions (such as `%m011 = and <16 x i1> %c0, %c1`) into the loop preheader.
2. **`CodeGenPrepare`**: `CodeGenPrepare` sinks single `icmp` instructions across basic blocks to their users, which avoids cross-block `<16 x i1>` values when only one comparison is hoisted. However, it does **not** sink or widen `<16 x i1>` boolean trees (`and` / `or` / `xor` of comparisons).
3. **SelectionDAG cross-BB legalization**: SelectionDAG legalizes one basic block at a time, so any value live across a basic block boundary must be legalized to a fixed target register type (`getRegisterTypeForCallingConv`). On AVX2 (without AVX-512), `v16i1` maps to **`v16i8` (`XMM`)**, regardless of the fact that all producing comparisons and consuming selects operate on **`v16i16` (`YMM`)**.
4. **Resulting codegen**:
  - **In the preheader**: The `<16 x i16>` masks produced by `vpcmpgtw` + `vpand` in `YMM` registers are truncated from `v16i16` to `v16i8` (`XMM`) via `vextracti128 + vpacksswb` just to cross the basic block boundary into the loop.
  - **Inside the loop**: Because the hoisted mask arrives as `v16i8` (`XMM`), the loop-varying `vpcmpgtw` (`v16i16` in `YMM`) is *also* truncated to `v16i8` via `vextracti128 + vpacksswb` on every iteration, combined with `vpand %xmm`, and then widened back from `v16i8` to `v16i16` (`YMM`) via `vpmovsxbw` (or `vpmovzxbw + vpsllw + vpsraw`) before masking `%x`. 


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to