[llvm] [InstCombine] Move foldLogOpOfMaskedICmps to make it possible to handle trunc to i1. (PR #122179)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 8 14:10:04 PST 2025


================
@@ -199,113 +199,132 @@ static bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pre
 /// the right hand side as a pair.
 /// LHS and RHS are the left hand side and the right hand side ICmps and PredL
 /// and PredR are their predicates, respectively.
-static std::optional<std::pair<unsigned, unsigned>> getMaskedTypeForICmpPair(
-    Value *&A, Value *&B, Value *&C, Value *&D, Value *&E, ICmpInst *LHS,
-    ICmpInst *RHS, ICmpInst::Predicate &PredL, ICmpInst::Predicate &PredR) {
-  // Don't allow pointers. Splat vectors are fine.
-  if (!LHS->getOperand(0)->getType()->isIntOrIntVectorTy() ||
-      !RHS->getOperand(0)->getType()->isIntOrIntVectorTy())
-    return std::nullopt;
+static std::optional<std::pair<unsigned, unsigned>>
+getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C, Value *&D, Value *&E,
+                         Value *LHS, Value *RHS, ICmpInst::Predicate &PredL,
+                         ICmpInst::Predicate &PredR) {
 
-  // Here comes the tricky part:
-  // LHS might be of the form L11 & L12 == X, X == L21 & L22,
-  // and L11 & L12 == L21 & L22. The same goes for RHS.
-  // Now we must find those components L** and R**, that are equal, so
-  // that we can extract the parameters A, B, C, D, and E for the canonical
-  // above.
-  Value *L1 = LHS->getOperand(0);
-  Value *L2 = LHS->getOperand(1);
-  Value *L11, *L12, *L21, *L22;
-  // Check whether the icmp can be decomposed into a bit test.
-  if (decomposeBitTestICmp(L1, L2, PredL, L11, L12, L2)) {
-    L21 = L22 = L1 = nullptr;
-  } else {
-    // Look for ANDs in the LHS icmp.
-    if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
-      // Any icmp can be viewed as being trivially masked; if it allows us to
-      // remove one, it's worth it.
-      L11 = L1;
-      L12 = Constant::getAllOnesValue(L1->getType());
-    }
+  Value *L1, *L11, *L12, *L2, *L21, *L22;
+  if (auto *LHSCMP = dyn_cast<ICmpInst>(LHS)) {
+
+    // Don't allow pointers. Splat vectors are fine.
+    if (!LHSCMP->getOperand(0)->getType()->isIntOrIntVectorTy())
+      return std::nullopt;
+
+    PredL = LHSCMP->getPredicate();
+
+    // Here comes the tricky part:
+    // LHS might be of the form L11 & L12 == X, X == L21 & L22,
+    // and L11 & L12 == L21 & L22. The same goes for RHS.
+    // Now we must find those components L** and R**, that are equal, so
+    // that we can extract the parameters A, B, C, D, and E for the canonical
+    // above.
+    L1 = LHSCMP->getOperand(0);
+    L2 = LHSCMP->getOperand(1);
+    // Check whether the icmp can be decomposed into a bit test.
+    if (decomposeBitTestICmp(L1, L2, PredL, L11, L12, L2)) {
+      L21 = L22 = L1 = nullptr;
+    } else {
+      // Look for ANDs in the LHS icmp.
+      if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
+        // Any icmp can be viewed as being trivially masked; if it allows us to
+        // remove one, it's worth it.
+        L11 = L1;
+        L12 = Constant::getAllOnesValue(L1->getType());
+      }
 
-    if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
-      L21 = L2;
-      L22 = Constant::getAllOnesValue(L2->getType());
+      if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
+        L21 = L2;
+        L22 = Constant::getAllOnesValue(L2->getType());
+      }
     }
-  }
+    // Bail if LHS was a icmp that can't be decomposed into an equality.
+    if (!ICmpInst::isEquality(PredL))
+      return std::nullopt;
 
-  // Bail if LHS was a icmp that can't be decomposed into an equality.
-  if (!ICmpInst::isEquality(PredL))
+  } else {
----------------
andjo403 wrote:

will add a "else if" to handled  trunc here in a follow up commit

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


More information about the llvm-commits mailing list