[llvm] [InstCombie] Add folds for (icmp eq/ne (and (add/sub/xor A, P2), P2), 0/P2) (PR #67836)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 1 11:21:38 PDT 2023


================
@@ -5453,6 +5453,45 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
                          m_CombineAnd(m_Value(B), m_Unless(m_ImmConstant())))))
     return new ICmpInst(Pred, Builder.CreateXor(A, B), Cst);
 
+  {
+    auto MatchAndP2OfAddSubXor = [&](unsigned Opc) -> std::optional<bool> {
+      // (icmp eq/ne (and (add/sub/xor X, P2), P2), P2)
+      std::optional<bool> Matched = std::nullopt;
+      if (match(&I, m_c_ICmp(
+                        PredUnused,
+                        m_OneUse(m_c_And(m_Value(A), m_c_BinOp(Opc, m_Value(B),
+                                                               m_Deferred(A)))),
+                        m_Deferred(A))))
+        Matched = false;
+      // (icmp eq/ne (and (add/sub/xor X, P2), P2), 0)
+      else if (match(&I, m_ICmp(PredUnused,
+                                m_OneUse(m_c_And(
+                                    m_Value(A),
+                                    m_c_BinOp(Opc, m_Value(B), m_Deferred(A)))),
+                                m_Zero())))
+        Matched = true;
+
+      if (Matched && isKnownToBeAPowerOfTwo(A, /* OrZero */ true, 0, &I))
+        return Matched;
+      return std::nullopt;
+    };
+    std::optional<bool> IsZero = MatchAndP2OfAddSubXor(Instruction::Add);
----------------
goldsteinn wrote:

In favor of your commit but still want this for multiuse case. Although realize there is bug in this code b.c commutative sub.

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


More information about the llvm-commits mailing list