[llvm] [InstCombine] Fold icmp with clamp into unsigned bound check (PR #161303)

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 1 09:13:27 PDT 2025


================
@@ -5780,6 +5780,44 @@ Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
   return nullptr;
 }
 
+/// Match and fold patterns like:
+///   icmp eq/ne X, min(max(X, Lo), Hi)
+/// which represents a range check and can be repsented as a ConstantRange.
+///
+/// For icmp eq, build ConstantRange [Lo, Hi + 1) and convert to:
+///   (X - Lo) u< (Hi + 1 - Lo)
+/// For icmp ne, build ConstantRange [Hi + 1, Lo) and convert to:
+///   (X - (Hi + 1)) u< (Lo - (Hi + 1))
+Instruction *InstCombinerImpl::foldICmpWithClamp(ICmpInst &I, Value *X,
+                                                 MinMaxIntrinsic *Min) {
+  if (!I.isEquality() || !Min->hasOneUse())
----------------
brandonxin wrote:

Yes you are right. Without those preliminary transforms, there will be a problem here. I added `isMin` and `isMax` helper methods to `MinMaxIntrinsic` and then added a check here. Thanks for pointing that out.

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


More information about the llvm-commits mailing list