https://github.com/lukel97 created https://github.com/llvm/llvm-project/pull/237120
If an i1 blend has false for all but one incoming value, then we can simplify this to the one non-false value. Blends should never have overlapping mask values, so we don't need to worry about the order as the masks can't "shortcut" each other. Needed to avoid regressions in an upcoming PR to keep SSA with early exits. --- <sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub> >From ae63602f6746f9463c64f8f19eb44c773d06beef Mon Sep 17 00:00:00 2001 From: Luke Lau <[email protected]> Date: Fri, 30 Oct 2026 14:08:45 +0800 Subject: [PATCH 1/2] Precommit tests --- .../test/Transforms/LoopVectorize/blend-i1.ll | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/llvm/test/Transforms/LoopVectorize/blend-i1.ll b/llvm/test/Transforms/LoopVectorize/blend-i1.ll index 25bfbb3167fc28..84c8bc2a24426c 100644 --- a/llvm/test/Transforms/LoopVectorize/blend-i1.ll +++ b/llvm/test/Transforms/LoopVectorize/blend-i1.ll @@ -55,3 +55,72 @@ latch: exit: ret void } + +define void @blend_of_i1_false_nested(ptr noalias %a, ptr noalias %dst) { +; CHECK-LABEL: define void @blend_of_i1_false_nested( +; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[DST:%.*]]) { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: br label %[[VECTOR_PH:.*]] +; CHECK: [[VECTOR_PH]]: +; CHECK-NEXT: br label %[[VECTOR_BODY:.*]] +; CHECK: [[VECTOR_BODY]]: +; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ] +; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i32, ptr [[A]], i64 [[INDEX]] +; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i32>, ptr [[TMP0]], align 4 +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[WIDE_LOAD]], zeroinitializer +; CHECK-NEXT: [[TMP2:%.*]] = icmp slt <4 x i32> [[WIDE_LOAD]], splat (i32 100) +; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[TMP1]], <4 x i1> [[TMP2]], <4 x i1> zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = icmp sge <4 x i32> [[WIDE_LOAD]], splat (i32 50) +; CHECK-NEXT: [[TMP5:%.*]] = icmp eq <4 x i32> [[WIDE_LOAD]], splat (i32 42) +; CHECK-NEXT: [[TMP6:%.*]] = xor <4 x i1> [[TMP1]], splat (i1 true) +; CHECK-NEXT: [[TMP7:%.*]] = xor <4 x i1> [[TMP2]], splat (i1 true) +; CHECK-NEXT: [[TMP8:%.*]] = select <4 x i1> [[TMP1]], <4 x i1> [[TMP7]], <4 x i1> zeroinitializer +; CHECK-NEXT: [[TMP9:%.*]] = select <4 x i1> [[TMP3]], <4 x i1> [[TMP4]], <4 x i1> zeroinitializer +; CHECK-NEXT: [[TMP10:%.*]] = or <4 x i1> [[TMP9]], [[TMP8]] +; CHECK-NEXT: [[TMP11:%.*]] = or <4 x i1> [[TMP10]], [[TMP6]] +; CHECK-NEXT: [[PREDPHI:%.*]] = select <4 x i1> [[TMP11]], <4 x i1> zeroinitializer, <4 x i1> [[TMP5]] +; CHECK-NEXT: [[TMP12:%.*]] = zext <4 x i1> [[PREDPHI]] to <4 x i8> +; CHECK-NEXT: [[TMP13:%.*]] = getelementptr i8, ptr [[DST]], i64 [[INDEX]] +; CHECK-NEXT: store <4 x i8> [[TMP12]], ptr [[TMP13]], align 1 +; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4 +; CHECK-NEXT: [[TMP14:%.*]] = icmp eq i64 [[INDEX_NEXT]], 128 +; CHECK-NEXT: br i1 [[TMP14]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]] +; CHECK: [[MIDDLE_BLOCK]]: +; CHECK-NEXT: br label %[[EXIT:.*]] +; CHECK: [[EXIT]]: +; CHECK-NEXT: ret void +; +entry: + br label %loop + +loop: + %iv = phi i64 [ 0, %entry ], [ %iv.next, %latch ] + %gep.a = getelementptr i32, ptr %a, i64 %iv + %x = load i32, ptr %gep.a + %c0 = icmp sgt i32 %x, 0 + br i1 %c0, label %if.0, label %latch + +if.0: + %c1 = icmp slt i32 %x, 100 + br i1 %c1, label %if.1, label %latch + +if.1: + %c2 = icmp slt i32 %x, 50 + br i1 %c2, label %then, label %latch + +then: + %c3 = icmp eq i32 %x, 42 + br label %latch + +latch: + %p = phi i1 [ %c3, %then ], [ false, %loop ], [ false, %if.0 ], [ false, %if.1 ] + %zext = zext i1 %p to i8 + %gep.dst = getelementptr i8, ptr %dst, i64 %iv + store i8 %zext, ptr %gep.dst + %iv.next = add nuw nsw i64 %iv, 1 + %ec = icmp eq i64 %iv.next, 128 + br i1 %ec, label %exit, label %loop + +exit: + ret void +} >From dbfbb2e531a4b06311db811ad3774ceaf8fe0348 Mon Sep 17 00:00:00 2001 From: Luke Lau <[email protected]> Date: Tue, 27 Oct 2026 22:35:39 +0800 Subject: [PATCH 2/2] [VPlan] Simplify BLEND X/M, false, ..., false -> M && X If an i1 blend has false for all but one incoming value, then we can simplify this to the one non-false value. Blends by construction never have any overlapping mask values, so we don't need to worry about the order as the masks can't "shortcut" each other. --- llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp | 13 +++++++++++++ llvm/test/Transforms/LoopVectorize/blend-i1.ll | 11 +++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp index 0de7544c625472..25012815b59fc2 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp @@ -2014,6 +2014,19 @@ static void simplifyBlends(VPlan &Plan) { if (Blend.isNormalized()) continue; + // BLEND X/M, false, ..., false -> M && X + auto NonFalse = make_filter_range( + seq<unsigned>(Blend.getNumIncomingValues()), [&Blend](unsigned I) { + return !match(Blend.getIncomingValue(I), m_False()); + }); + if (hasSingleElement(NonFalse)) { + unsigned I = *NonFalse.begin(); + Blend.replaceAllUsesWith(VPBuilder(&Blend).createLogicalAnd( + Blend.getMask(I), Blend.getIncomingValue(I))); + Blend.eraseFromParent(); + continue; + } + // Normalize the blend so its first incoming value is used as the initial // value with the others blended into it. diff --git a/llvm/test/Transforms/LoopVectorize/blend-i1.ll b/llvm/test/Transforms/LoopVectorize/blend-i1.ll index 84c8bc2a24426c..e728d712911e0b 100644 --- a/llvm/test/Transforms/LoopVectorize/blend-i1.ll +++ b/llvm/test/Transforms/LoopVectorize/blend-i1.ll @@ -70,15 +70,10 @@ define void @blend_of_i1_false_nested(ptr noalias %a, ptr noalias %dst) { ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[WIDE_LOAD]], zeroinitializer ; CHECK-NEXT: [[TMP2:%.*]] = icmp slt <4 x i32> [[WIDE_LOAD]], splat (i32 100) ; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[TMP1]], <4 x i1> [[TMP2]], <4 x i1> zeroinitializer -; CHECK-NEXT: [[TMP4:%.*]] = icmp sge <4 x i32> [[WIDE_LOAD]], splat (i32 50) -; CHECK-NEXT: [[TMP5:%.*]] = icmp eq <4 x i32> [[WIDE_LOAD]], splat (i32 42) -; CHECK-NEXT: [[TMP6:%.*]] = xor <4 x i1> [[TMP1]], splat (i1 true) -; CHECK-NEXT: [[TMP7:%.*]] = xor <4 x i1> [[TMP2]], splat (i1 true) -; CHECK-NEXT: [[TMP8:%.*]] = select <4 x i1> [[TMP1]], <4 x i1> [[TMP7]], <4 x i1> zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = icmp slt <4 x i32> [[WIDE_LOAD]], splat (i32 50) ; CHECK-NEXT: [[TMP9:%.*]] = select <4 x i1> [[TMP3]], <4 x i1> [[TMP4]], <4 x i1> zeroinitializer -; CHECK-NEXT: [[TMP10:%.*]] = or <4 x i1> [[TMP9]], [[TMP8]] -; CHECK-NEXT: [[TMP11:%.*]] = or <4 x i1> [[TMP10]], [[TMP6]] -; CHECK-NEXT: [[PREDPHI:%.*]] = select <4 x i1> [[TMP11]], <4 x i1> zeroinitializer, <4 x i1> [[TMP5]] +; CHECK-NEXT: [[TMP6:%.*]] = icmp eq <4 x i32> [[WIDE_LOAD]], splat (i32 42) +; CHECK-NEXT: [[PREDPHI:%.*]] = select <4 x i1> [[TMP9]], <4 x i1> [[TMP6]], <4 x i1> zeroinitializer ; CHECK-NEXT: [[TMP12:%.*]] = zext <4 x i1> [[PREDPHI]] to <4 x i8> ; CHECK-NEXT: [[TMP13:%.*]] = getelementptr i8, ptr [[DST]], i64 [[INDEX]] ; CHECK-NEXT: store <4 x i8> [[TMP12]], ptr [[TMP13]], align 1 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
