[PATCH] D148205: [IRCE] Refactor parseRangeCheckICmp to compute SCEVs instead of Values

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 13 22:59:11 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGa39b807d41f1: [IRCE][NFC] Refactor parseRangeCheckICmp to compute SCEVs instead of Values (authored by mkazantsev).

Changed prior to commit:
  https://reviews.llvm.org/D148205?vs=513133&id=513441#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148205/new/

https://reviews.llvm.org/D148205

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
@@ -145,7 +145,7 @@
   Use *CheckUse = nullptr;
 
   static bool parseRangeCheckICmp(Loop *L, ICmpInst *ICI, ScalarEvolution &SE,
-                                  Value *&Index, Value *&Length);
+                                  const SCEV *&Index, const SCEV *&End);
 
   static void
   extractRangeChecksFromCond(Loop *L, ScalarEvolution &SE, Use &ConditionUse,
@@ -284,13 +284,13 @@
                     false, false)
 
 /// Parse a single ICmp instruction, `ICI`, into a range check.  If `ICI` cannot
-/// be interpreted as a range check, return false and set `Index` and `Length`
-/// to `nullptr`.  Otherwise set `Index` to the value being range checked, and
-/// set `Length` to the upper limit `Index` is being range checked.
-bool
-InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
-                                         ScalarEvolution &SE, Value *&Index,
-                                         Value *&Length) {
+/// be interpreted as a range check, return false and set `Index` and `End`
+/// to `nullptr`.  Otherwise set `Index` to the SCEV being range checked, and
+/// set `End` to the upper limit `Index` is being range checked.
+bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
+                                              ScalarEvolution &SE,
+                                              const SCEV *&Index,
+                                              const SCEV *&End) {
   auto IsLoopInvariant = [&SE, L](Value *V) {
     return SE.isLoopInvariant(SE.getSCEV(V), L);
   };
@@ -308,7 +308,7 @@
     [[fallthrough]];
   case ICmpInst::ICMP_SGE:
     if (match(RHS, m_ConstantInt<0>())) {
-      Index = LHS;
+      Index = SE.getSCEV(LHS);
       return true; // Lower.
     }
     return false;
@@ -318,13 +318,13 @@
     [[fallthrough]];
   case ICmpInst::ICMP_SGT:
     if (match(RHS, m_ConstantInt<-1>())) {
-      Index = LHS;
+      Index = SE.getSCEV(LHS);
       return true; // Lower.
     }
 
     if (IsLoopInvariant(LHS)) {
-      Index = RHS;
-      Length = LHS;
+      Index = SE.getSCEV(RHS);
+      End = SE.getSCEV(LHS);
       return true; // Upper.
     }
     return false;
@@ -334,8 +334,8 @@
     [[fallthrough]];
   case ICmpInst::ICMP_UGT:
     if (IsLoopInvariant(LHS)) {
-      Index = RHS;
-      Length = LHS;
+      Index = SE.getSCEV(RHS);
+      End = SE.getSCEV(LHS);
       return true; // Both lower and upper.
     }
     return false;
@@ -365,23 +365,20 @@
   if (!ICI)
     return;
 
-  Value *Length = nullptr, *Index;
-  if (!parseRangeCheckICmp(L, ICI, SE, Index, Length))
+  const SCEV *End = nullptr, *Index = nullptr;
+  if (!parseRangeCheckICmp(L, ICI, SE, Index, End))
     return;
 
-  const auto *IndexAddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Index));
+  const auto *IndexAddRec = dyn_cast<SCEVAddRecExpr>(Index);
   bool IsAffineIndex =
       IndexAddRec && (IndexAddRec->getLoop() == L) && IndexAddRec->isAffine();
 
   if (!IsAffineIndex)
     return;
 
-  const SCEV *End = nullptr;
   // We strengthen "0 <= I" to "0 <= I < INT_SMAX" and "I < L" to "0 <= I < L".
   // We can potentially do much better here.
-  if (Length)
-    End = SE.getSCEV(Length);
-  else {
+  if (!End) {
     // So far we can only reach this point for Signed range check. This may
     // change in future. In this case we will need to pick Unsigned max for the
     // unsigned range check.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148205.513441.patch
Type: text/x-patch
Size: 3665 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230414/3a9df928/attachment.bin>


More information about the llvm-commits mailing list