[llvm] [InstCombine] Add transforms for `icmp uPred (trunc x),(truncOrZext(y))` -> `icmp uPred x,y` (PR #71309)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 5 01:53:45 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);
+ if (KnownX.Zero.countl_one() < KnownX.getBitWidth() - TruncBits)
+ return nullptr;
+
+ if (!YIsZext) {
+ // If Y is also a trunc, make sure it is unneeded.
+ KnownBits KnownY = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
+ if (KnownY.Zero.countl_one() < KnownY.getBitWidth() - TruncBits)
+ return nullptr;
+ }
+
+ Value *NewY = IC.Builder.CreateZExtOrTrunc(Y, BaseTy);
+ return IC.replaceInstUsesWith(Cmp, IC.Builder.CreateICmp(Pred, X, NewY));
----------------
dtcxzyw wrote:
```suggestion
return new ICmpInst(Pred, X, NewY);
```
https://github.com/llvm/llvm-project/pull/71309
More information about the llvm-commits
mailing list