[PATCH] D148720: [IRCE] Refactor parseRangeCheckICmp

Aleksandr Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 19 08:46:39 PDT 2023


aleksandr.popov created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
aleksandr.popov requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Simplify parseRangeCheckICmp:

- If RHS is loop-variant, swap LHS/RHS and swap predicate
- all checks are either IV >(=) const or IV <(=) RHS (maybe not const)


https://reviews.llvm.org/D148720

Files:
  llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp


Index: llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
+++ llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
@@ -300,46 +300,30 @@
   Value *LHS = ICI->getOperand(0);
   Value *RHS = ICI->getOperand(1);
 
+  // Canonicalize to the `Index Pred Invariant` comparison
+  if (IsLoopInvariant(LHS)) {
+    std::swap(LHS, RHS);
+    Pred = CmpInst::getSwappedPredicate(Pred);
+  } else if (!IsLoopInvariant(RHS)) {
+    // Both LHS and RHS are loop variant
+    return false;
+  }
+  Index = SE.getSCEV(LHS);
+
   switch (Pred) {
   default:
     return false;
 
-  case ICmpInst::ICMP_SLE:
-    std::swap(LHS, RHS);
-    [[fallthrough]];
   case ICmpInst::ICMP_SGE:
-    if (match(RHS, m_ConstantInt<0>())) {
-      Index = SE.getSCEV(LHS);
-      return true; // Lower.
-    }
-    return false;
+    return match(RHS, m_ConstantInt<0>()); // Lower.
 
-  case ICmpInst::ICMP_SLT:
-    std::swap(LHS, RHS);
-    [[fallthrough]];
   case ICmpInst::ICMP_SGT:
-    if (match(RHS, m_ConstantInt<-1>())) {
-      Index = SE.getSCEV(LHS);
-      return true; // Lower.
-    }
-
-    if (IsLoopInvariant(LHS)) {
-      Index = SE.getSCEV(RHS);
-      End = SE.getSCEV(LHS);
-      return true; // Upper.
-    }
-    return false;
+    return match(RHS, m_ConstantInt<-1>()); // Lower.
 
+  case ICmpInst::ICMP_SLT:
   case ICmpInst::ICMP_ULT:
-    std::swap(LHS, RHS);
-    [[fallthrough]];
-  case ICmpInst::ICMP_UGT:
-    if (IsLoopInvariant(LHS)) {
-      Index = SE.getSCEV(RHS);
-      End = SE.getSCEV(LHS);
-      return true; // Both lower and upper.
-    }
-    return false;
+    End = SE.getSCEV(RHS);
+    return true; // Both lower and upper for ULT, upper for SLT
   }
 
   llvm_unreachable("default clause returns!");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148720.514968.patch
Type: text/x-patch
Size: 1881 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230419/2c37dfdf/attachment.bin>


More information about the llvm-commits mailing list