[llvm] [InstCombine] Fold `(icmp pred (trunc nuw/nsw X), C)` -> `(icmp pred X, (zext/sext C))` (PR #87935)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 30 03:21:10 PDT 2024
================
@@ -5705,6 +5705,22 @@ Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) {
ICmpInst::Predicate Pred = ICmp.getPredicate();
Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
+ // Match (icmp pred (trunc nuw/nsw X), C)
+ // Which we can convert to (icmp pred X, (sext/zext C))
+ // TODO: Maybe this makes sense as a general canonicalization?
+ if (match(Op1, m_ImmConstant())) {
+ if (auto *TI = dyn_cast<TruncInst>(Op0)) {
+ Value *ExtOp0 = TI->getOperand(0);
+ Value *ExtOp1 = nullptr;
+ if (!ICmp.isSigned() && TI->hasNoUnsignedWrap())
+ ExtOp1 = Builder.CreateZExt(Op1, ExtOp0->getType());
+ else if (!ICmp.isUnsigned() && TI->hasNoSignedWrap())
----------------
nikic wrote:
This is actually also valid for `isUnsigned()` predicates: https://alive2.llvm.org/ce/z/MiDiYZ
https://github.com/llvm/llvm-project/pull/87935
More information about the llvm-commits
mailing list