[PATCH] D122152: [InstCombine] Fold two select patterns into and-or

chenglin.bi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 21 08:42:53 PDT 2022


bcl5980 created this revision.
bcl5980 added reviewers: spatel, RKSimon, nikic, craig.topper.
Herald added a subscriber: hiraditya.
Herald added a project: All.
bcl5980 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

select (~a | c), a, b -> and a, (or c, b)   https://alive2.llvm.org/ce/z/bnDobs
select (~c & b), a, b -> and b, (or a, c) https://alive2.llvm.org/ce/z/k2jJHJ


https://reviews.llvm.org/D122152

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/select-and-or.ll


Index: llvm/test/Transforms/InstCombine/select-and-or.ll
===================================================================
--- llvm/test/Transforms/InstCombine/select-and-or.ll
+++ llvm/test/Transforms/InstCombine/select-and-or.ll
@@ -448,3 +448,27 @@
   %C15 = select i1 %not.L, i1 true, i1 xor (i1 and (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1), i1 icmp ne (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1)), i1 true)
   ret i1 %C15
 }
+
+define i1 @and_or1(i1 %a, i1 noundef %b, i1 %c) {
+; CHECK-LABEL: @and_or1(
+; CHECK-NEXT:    [[TMP1:%.*]] = or i1 [[C:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = and i1 [[TMP1]], [[A:%.*]]
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %nota = xor i1 %a, true
+  %cond = or i1 %nota, %c
+  %r = select i1 %cond, i1 %a, i1 %b
+  ret i1 %r
+}
+
+define i1 @and_or2(i1 noundef %a, i1 %b, i1 %c) {
+; CHECK-LABEL: @and_or2(
+; CHECK-NEXT:    [[TMP1:%.*]] = or i1 [[C:%.*]], [[A:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = and i1 [[TMP1]], [[B:%.*]]
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %notc = xor i1 %c, true
+  %cond = and i1 %notc, %b
+  %r = select i1 %cond, i1 %a, i1 %b
+  ret i1 %r
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2570,6 +2570,21 @@
         match(TrueVal, m_Specific(B)) && match(FalseVal, m_Zero()))
       return replaceOperand(SI, 0, A);
 
+    if (isGuaranteedNotToBeUndefOrPoison(FalseVal)) {
+      Value *C;
+      // select (~a | c), a, b -> and a, (or c, b)
+      if (match(CondVal, m_Or(m_Not(m_Value(TrueVal)), m_Value(C))))
+        return BinaryOperator::CreateAnd(TrueVal,
+                                         Builder.CreateOr(C, FalseVal));
+    }
+    if (isGuaranteedNotToBeUndefOrPoison(TrueVal)) {
+      Value *C;
+      // select (~c & b), a, b -> and b, (or a, c)
+      if (match(CondVal, m_And(m_Not(m_Value(C)), m_Value(FalseVal))))
+        return BinaryOperator::CreateAnd(FalseVal,
+                                         Builder.CreateOr(C, TrueVal));
+    }
+
     if (!SelType->isVectorTy()) {
       if (Value *S = simplifyWithOpReplaced(TrueVal, CondVal, One, SQ,
                                             /* AllowRefinement */ true))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122152.416962.patch
Type: text/x-patch
Size: 2377 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220321/229bbb50/attachment.bin>


More information about the llvm-commits mailing list