[PATCH] D133919: [InstCombine] Fold ((x?1:4)&(y?1:4))==0 to x^y

Marc Auberer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 3 14:11:13 PDT 2022


marcauberer updated this revision to Diff 464811.
marcauberer added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133919/new/

https://reviews.llvm.org/D133919

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp


Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6472,6 +6472,35 @@
   if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
     return Res;
 
+  {
+    Value *A, *B, *C, *D;
+
+    // Find hidden xor optimization (only if `C` and `D` have no common bits)
+    // (icmp eq (and (select A, C, D), (select B, C, D)), 0) --> (xor A, B)
+    if (I.getPredicate() == ICmpInst::ICMP_EQ &&
+        match(Op0, m_And(m_Select(m_Value(A), m_Value(C), m_Value(D)),
+                         m_Select(m_Value(B), m_Specific(C), m_Specific(D)))) &&
+        match(Op1, m_Zero()) &&
+        isKnownNonZero(C, DL, /*Depth*/ 0, &AC, &I, &DT) &&
+        isKnownNonZero(D, DL, /*Depth*/ 0, &AC, &I, &DT) &&
+        haveNoCommonBitsSet(C, D, DL, &AC, &I, &DT)) {
+      return BinaryOperator::CreateXor(A, B);
+    }
+    // (icmp ne (and (select A, C, D), (select B, C, D)), 0) --> (not (xor A,
+    // B), true)
+    if (I.getPredicate() == ICmpInst::ICMP_NE &&
+        match(Op0, m_OneUse(m_And(
+                       m_Select(m_Value(A), m_Value(C), m_Value(D)),
+                       m_Select(m_Value(B), m_Specific(C), m_Specific(D))))) &&
+        match(Op1, m_Zero()) &&
+        isKnownNonZero(C, DL, /*Depth*/ 0, &AC, &I, &DT) &&
+        isKnownNonZero(D, DL, /*Depth*/ 0, &AC, &I, &DT) &&
+        haveNoCommonBitsSet(C, D, DL, &AC, &I, &DT)) {
+      Value *Xor = Builder.CreateXor(A, B);
+      return BinaryOperator::CreateNeg(Xor);
+    }
+  }
+
   return Changed ? &I : nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133919.464811.patch
Type: text/x-patch
Size: 1703 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221003/30f9f1a6/attachment.bin>


More information about the llvm-commits mailing list