[llvm] [InstCombine] optimize unnecessary sext instruction with add + cmp (PR #152291)
Gaurav Dhingra via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 14 07:39:16 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:
@nikic , can you kindly help me how to avoid using `int64_t` here? (You suggested in another comment that I should use APInt instead)
I've tried using:
```
OutValue = C->getValue().sextOrTrunc(TargetType->getIntegerBitWidth());
```
but I'm not sure if it's an approach in the right direction.
https://github.com/llvm/llvm-project/pull/152291
More information about the llvm-commits
mailing list