[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)),
+                            m_ConstantInt(Z)))) {
+    Type *XType = X->getType();
+    if (!XType->isIntegerTy())
----------------
nikic wrote:

X must be an integer or vector of integers. We should handle both. Please remove this check and add a vector test.

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


More information about the llvm-commits mailing list