[llvm] [VPlan] Add simplifications to remove header masks in predicated AnyOf select reductions (PR #190196)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 11 08:27:22 PDT 2026


lukel97 wrote:

> > The first transform on its own breaks some vp.merge patterns without the second one
> 
> > Then there's a separate existing combine in simplifyRecipes for  vp.merge true, (or x, y), x, evl -> vp.merge y, true, x, evl:
> 
> Then why do the first transformation with the root on `select` instead of matching larger single `vp.merge`/`select` pattern at once?

Yeah I had the same thought, i.e. do `select headermask, (i1 lhs | rhs), lhs -> vp.merge rhs, true, lhs, evl`. But there are other masks than the header mask that this simplification works on, i.e. with only the one transform this we lose this optimization:

```diff
--- a/llvm/test/Transforms/LoopVectorize/RISCV/select-cmp-reduction.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/select-cmp-reduction.ll
@@ -322,8 +322,9 @@ define i32 @pred_select_const_i32_from_icmp(ptr noalias nocapture readonly %src1
 ; CHECK-NEXT:    [[TMP8:%.*]] = getelementptr i32, ptr [[SRC2]], i64 [[INDEX]]
 ; CHECK-NEXT:    [[WIDE_MASKED_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP8]], <vscale x 4 x i1> [[TMP7]], i32 [[TMP17]])
 ; CHECK-NEXT:    [[TMP9:%.*]] = icmp eq <vscale x 4 x i32> [[WIDE_MASKED_LOAD]], splat (i32 2)
-; CHECK-NEXT:    [[TMP5:%.*]] = select <vscale x 4 x i1> [[TMP7]], <vscale x 4 x i1> [[TMP9]], <vscale x 4 x i1> zeroinitializer
-; CHECK-NEXT:    [[PREDPHI]] = call <vscale x 4 x i1> @llvm.vp.merge.nxv4i1(<vscale x 4 x i1> [[TMP5]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i1> [[VEC_PHI]], i32 [[TMP17]])
+; CHECK-NEXT:    [[TMP5:%.*]] = or <vscale x 4 x i1> [[VEC_PHI]], [[TMP9]]
+; CHECK-NEXT:    [[PREDPHI1:%.*]] = select <vscale x 4 x i1> [[TMP7]], <vscale x 4 x i1> [[TMP5]], <vscale x 4 x i1> [[VEC_PHI]]
+; CHECK-NEXT:    [[PREDPHI]] = call <vscale x 4 x i1> @llvm.vp.merge.nxv4i1(<vscale x 4 x i1> splat (i1 true), <vscale x 4 x i1> [[PREDPHI1]], <vscale x 4 x i1> [[VEC_PHI]], i32 [[TMP17]])
```


https://github.com/llvm/llvm-project/pull/190196


More information about the llvm-commits mailing list