[llvm] [InstCombine] optimize unnecessary sext instruction with add + cmp (PR #152291)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 6 06:36:16 PDT 2025


================
@@ -6381,6 +6381,36 @@ Instruction *InstCombinerImpl::foldICmpWithZextOrSext(ICmpInst &ICmp) {
   return new ICmpInst(CmpInst::ICMP_SLT, X, Constant::getNullValue(SrcTy));
 }
 
+Instruction *InstCombinerImpl::foldICmpWithSextAndAdd(ICmpInst &ICmp) {
+  Value *X;
+  ConstantInt *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_ConstantInt(Y)),
----------------
nikic wrote:

m_ConstantInt -> m_APInt and then work on APInt instead of int64_t returned by getSExtValue().

https://github.com/llvm/llvm-project/pull/152291


More information about the llvm-commits mailing list