[llvm] [InstCombine] Fold `(icmp pred (trunc nuw/nsw X), C)` -> `(icmp pred X, (zext/sext C))` (PR #87935)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat May 11 03:13:19 PDT 2024
================
@@ -1409,6 +1409,19 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
const APInt &C) {
ICmpInst::Predicate Pred = Cmp.getPredicate();
Value *X = Trunc->getOperand(0);
+ Type *SrcTy = X->getType();
+ unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
+ SrcBits = SrcTy->getScalarSizeInBits();
+
+ // Match (icmp pred (trunc nuw/nsw X), C)
+ // Which we can convert to (icmp pred X, (sext/zext C))
+ if (shouldChangeType(DstBits, SrcBits)) {
+ if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
+ return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
+ if (Trunc->hasNoSignedWrap())
+ return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
----------------
dtcxzyw wrote:
I prefer the sign-extended constant as it may be cheaper to materialize than the zero-extended one (e.g., -1 vs 4294967296).
Don't do this until we have some performance data.
https://github.com/llvm/llvm-project/pull/87935
More information about the llvm-commits
mailing list