Issue 235818
Summary [InstCombine] Fold zero passthrough into masked gather through freeze
Labels new issue
Assignees
Reporter adamant-pwn
    InstCombine folds a select of zero into a masked gather's passthrough, but an intervening `freeze` prevents the fold. This leaves a redundant mask copy and masked move on x86. This is a missed optimization, not incorrect behavior.

Reproduced with LLVM 23.1.3 (Arch Linux package `llvm 23.1.3-5`):

```llvm
declare <4 x i64> @llvm.masked.gather.v4i64.v4p0(<4 x ptr>, <4 x i1>, <4 x i64>)

define <4 x i64> @read(<4 x ptr> %p, <4 x i1> noundef %mask) {
  %g = call <4 x i64> @llvm.masked.gather.v4i64.v4p0(<4 x ptr> align 8 %p, <4 x i1> %mask, <4 x i64> poison)
  %f = freeze <4 x i64> %g
  %s = select <4 x i1> %mask, <4 x i64> %f, <4 x i64> zeroinitializer
  ret <4 x i64> %s
}
```

```sh
opt -passes=instcombine -S repro.ll -o optimized.ll
llc -mtriple=x86_64 -mcpu=znver4 optimized.ll -o -
```

The select remains. Could its zero fallback become the gather's passthrough, retaining `freeze` and returning `%f` directly? Inactive lanes would already be zero, while active lanes retain their freeze semantics. Removing `freeze` from this example allows the existing fold, but dropping it generally would be incorrect.

Reduced from vectorized packed-integer sums. A local narrow prototype handles single-use gather/freeze pairs; result range annotations need adjusting when the passthrough changes.

Assisted-by: OpenAI Codex.

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

Reply via email to