[llvm] [InstCombine] Fold (X / C) < X and (X >> C) < X into X > 0 (PR #85555)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 16 15:45:42 PDT 2024


================
@@ -7406,6 +7406,31 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
   if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
     return Res;
 
+  // Folding (X / Y) < X => X > 0 for some constant Y other than 0 or 1
+  {
+    Constant *Divisor;
+    Value *Dividend;
+    if (match(Op0,
+              m_CombineOr(m_SDiv(m_Value(Dividend), m_ImmConstant(Divisor)),
+                          m_UDiv(m_Value(Dividend), m_ImmConstant(Divisor)))) &&
+        match(Op1, m_Deferred(Dividend)) &&
+        !(Divisor->isZeroValue() || Divisor->isOneValue())) {
+      return new ICmpInst(ICmpInst::ICMP_SGT, Dividend,
----------------
goldsteinn wrote:

You are missing a check that in the incoming Predicate was `ICMP_SLT`.

In general though, can you handle a lot more predicates and the general pattern `ICmpInst::getSwappedPredicate(I.getPredicate())`

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


More information about the llvm-commits mailing list