[llvm-branch-commits] [llvm] [VPlan] Simplify BLEND X/M, false, ..., false -> M && X (PR #237120)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Oct 30 00:13:08 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Luke Lau (lukel97)

<details>
<summary>Changes</summary>

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>

---
Full diff: https://github.com/llvm/llvm-project/pull/237120.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+13) 
- (modified) llvm/test/Transforms/LoopVectorize/blend-i1.ll (+64) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 0de7544c62547..25012815b59fc 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 25bfbb3167fc2..e728d712911e0 100644
--- a/llvm/test/Transforms/LoopVectorize/blend-i1.ll
+++ b/llvm/test/Transforms/LoopVectorize/blend-i1.ll
@@ -55,3 +55,67 @@ 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 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:    [[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
+; 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
+}

``````````

</details>


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


More information about the llvm-branch-commits mailing list