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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 1 01:56:22 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);
----------------
dtcxzyw wrote:

It is easy to do canonicalization for non-constant.
Alive2: https://alive2.llvm.org/ce/z/LmLn2R
I will submit a patch later.


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


More information about the llvm-commits mailing list