[llvm] [CmpInstAnalysis] Decompose icmp eq (and x, C) C2 (PR #136367)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 23 01:49:28 PDT 2025


================
@@ -3730,31 +3730,29 @@ static Value *foldSelectBitTest(SelectInst &Sel, Value *CondVal, Value *TrueVal,
   Value *CmpLHS, *CmpRHS;
 
   if (match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
-    if (ICmpInst::isEquality(Pred)) {
-      if (!match(CmpRHS, m_Zero()))
-        return nullptr;
+    auto Res = decomposeBitTestICmp(
+        CmpLHS, CmpRHS, Pred, /*LookThroughTrunc=*/true,
+        /*AllowNonZeroC=*/false, /*DecomposeBitMask=*/true);
 
-      V = CmpLHS;
-      const APInt *AndRHS;
-      if (!match(CmpLHS, m_And(m_Value(), m_Power2(AndRHS))))
-        return nullptr;
+    if (!Res)
+      return nullptr;
 
-      AndMask = *AndRHS;
-    } else if (auto Res = decomposeBitTestICmp(CmpLHS, CmpRHS, Pred)) {
-      assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
-      AndMask = Res->Mask;
+    V = CmpLHS;
+    AndMask = Res->Mask;
+
+    if (!ICmpInst::isEquality(Pred)) {
----------------
nikic wrote:

I don't think we should do this. There is a lot of implicit API contract here. You're basically relying on the implementation detail that if the predicate is an equality, then you know that the decomposition will be of an and, so you can set CreateAnd=false and use the original CmpLHS instead of the decomposition result Res->X.

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


More information about the llvm-commits mailing list