[llvm] [InstCombine] Add transforms for `icmp uPred (trunc x),(truncOrZext(y))` -> `icmp uPred x,y` (PR #71309)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 5 04:04:56 PST 2023


================
@@ -1545,6 +1545,53 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
   return nullptr;
 }
 
+/// Fold icmp (trunc X), (trunc Y).
+/// Fold icmp (trunc X), (zext Y).
+static Instruction *foldICmpTruncWithTruncOrExt(ICmpInst &Cmp,
+                                                InstCombinerImpl &IC,
+                                                const SimplifyQuery &Q) {
+  if (!Cmp.isEquality() && !Cmp.isUnsigned())
+    return nullptr;
+
+  Value *X, *Y;
+  ICmpInst::Predicate Pred;
+  bool YIsZext = false;
+  // Try to match icmp (trunc X), (trunc Y)
+  if (match(&Cmp, m_ICmp(Pred, m_Trunc(m_Value(X)), m_Trunc(m_Value(Y))))) {
+    if (X->getType() != Y->getType() &&
+        (!Cmp.getOperand(0)->hasOneUse() || !Cmp.getOperand(1)->hasOneUse()))
+      return nullptr;
+  }
+  // Try to match icmp (trunc X), (zext Y)
+  else if (match(&Cmp, m_c_ICmp(Pred, m_Trunc(m_Value(X)),
+                                m_OneUse(m_ZExt(m_Value(Y)))))) {
+
+    YIsZext = true;
+  } else {
+    return nullptr;
+  }
+
+  Type *TruncTy = Cmp.getOperand(0)->getType();
+  Type *BaseTy = X->getType();
+
+  unsigned TruncBits = TruncTy->getScalarSizeInBits();
+
+  // Check if the trunc is unneeded.
+  KnownBits KnownX = computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
----------------
nikic wrote:

I believe we have a public overload that directly takes SimplifyQuery nowadays.

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


More information about the llvm-commits mailing list