[llvm] [InstCombine] optimize unnecessary sext instruction with add + cmp (PR #152291)
Gaurav Dhingra via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 17 01:58:08 PDT 2025
================
@@ -6381,6 +6381,82 @@ Instruction *InstCombinerImpl::foldICmpWithZextOrSext(ICmpInst &ICmp) {
return new ICmpInst(CmpInst::ICMP_SLT, X, Constant::getNullValue(SrcTy));
}
+Instruction *InstCombinerImpl::foldICmpWithSextAndAdd(ICmpInst &ICmp) {
+ Value *X;
+ Value *Y, *Z;
+ // Match the pattern: icmp ult (add (sext X), Y), Z
+ // where X is a value, Y and Z are integer constants
+ // icmp ult (add(sext(X), Y)), Z -> icmp ult (add(X, Y)), Z
+ if (match(&ICmp, m_SpecificICmp(CmpInst::ICMP_ULT,
+ m_Add(m_SExt(m_Value(X)), m_Value(Y)),
+ m_Value(Z)))) {
+ Type *XType = X->getType();
+ if (!XType->isIntegerTy())
+ return nullptr;
+
+ unsigned XBitWidth = XType->getIntegerBitWidth();
+ auto ExtractValue = [&](Value *V, Type *TargetType,
+ int64_t &OutValue) -> Value * {
+ if (auto *C = dyn_cast<ConstantInt>(V)) {
+ OutValue = C->getSExtValue();
----------------
gxyd wrote:
@dtcxzyw maybe you can help me out with a possible solution to this problem? Thank you.
https://github.com/llvm/llvm-project/pull/152291
More information about the llvm-commits
mailing list