[llvm] f56e847 - [IRCE][NFC] Set Index and End together inside parseRangeCheckICmp

Aleksandr Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 4 01:44:13 PDT 2023


Author: Aleksandr Popov
Date: 2023-07-03T05:21:05+02:00
New Revision: f56e84736297a12c39cf8fd69b279ba44a15ba82

URL: https://github.com/llvm/llvm-project/commit/f56e84736297a12c39cf8fd69b279ba44a15ba82
DIFF: https://github.com/llvm/llvm-project/commit/f56e84736297a12c39cf8fd69b279ba44a15ba82.diff

LOG: [IRCE][NFC] Set Index and End together inside parseRangeCheckICmp

Preparatory refactoring for the upcoming support of new range check form
to parse.

With this change we always set Index and End values together in the same
place.

parseRangeCheckICmp specification updated.

Reviewed By: skatkov
Differential Revision: https://reviews.llvm.org/D154156

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
index 344916bc53dd0c..f5deff564d0ca0 100644
--- a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
@@ -249,9 +249,9 @@ class InductiveRangeCheckElimination {
 } // end anonymous namespace
 
 /// 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 `End`
-/// to `nullptr`.  Otherwise set `Index` to the SCEV being range checked, and
-/// set `End` to the upper limit `Index` is being range checked.
+/// be interpreted as a range check, return false.  Otherwise set `Index` to the
+/// SCEV being range checked, and set `End` to the upper or lower limit `Index`
+/// is being range checked.
 bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
                                               ScalarEvolution &SE,
                                               const SCEV *&Index,
@@ -260,6 +260,11 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
     return SE.isLoopInvariant(SE.getSCEV(V), L);
   };
 
+  auto SIntMaxSCEV = [&](Type *T) {
+    unsigned BitWidth = cast<IntegerType>(T)->getBitWidth();
+    return SE.getConstant(APInt::getSignedMaxValue(BitWidth));
+  };
+
   ICmpInst::Predicate Pred = ICI->getPredicate();
   Value *LHS = ICI->getOperand(0);
   Value *RHS = ICI->getOperand(1);
@@ -272,6 +277,10 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
     // Both LHS and RHS are loop variant
     return false;
 
+  // We strengthen "0 <= I" to "0 <= I < INT_SMAX" and "I < L" to "0 <= I < L".
+  // We can potentially do much better here.
+  // If we want to adjust upper bound for the unsigned range check as we do it
+  // for signed one, we will need to pick Unsigned max
   switch (Pred) {
   default:
     return false;
@@ -279,6 +288,7 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
   case ICmpInst::ICMP_SGE:
     if (match(RHS, m_ConstantInt<0>())) {
       Index = SE.getSCEV(LHS);
+      End = SIntMaxSCEV(Index->getType());
       return true;
     }
     return false;
@@ -286,6 +296,7 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
   case ICmpInst::ICMP_SGT:
     if (match(RHS, m_ConstantInt<-1>())) {
       Index = SE.getSCEV(LHS);
+      End = SIntMaxSCEV(Index->getType());
       return true;
     }
     return false;
@@ -337,6 +348,9 @@ void InductiveRangeCheck::extractRangeChecksFromCond(
   if (!parseRangeCheckICmp(L, ICI, SE, Index, End))
     return;
 
+  assert(Index && "Index was not computed");
+  assert(End && "End was not computed");
+
   const auto *IndexAddRec = dyn_cast<SCEVAddRecExpr>(Index);
   bool IsAffineIndex =
       IndexAddRec && (IndexAddRec->getLoop() == L) && IndexAddRec->isAffine();
@@ -344,17 +358,6 @@ void InductiveRangeCheck::extractRangeChecksFromCond(
   if (!IsAffineIndex)
     return;
 
-  // We strengthen "0 <= I" to "0 <= I < INT_SMAX" and "I < L" to "0 <= I < L".
-  // We can potentially do much better here.
-  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.
-    unsigned BitWidth = cast<IntegerType>(IndexAddRec->getType())->getBitWidth();
-    const SCEV *SIntMax = SE.getConstant(APInt::getSignedMaxValue(BitWidth));
-    End = SIntMax;
-  }
-
   InductiveRangeCheck IRC;
   IRC.End = End;
   IRC.Begin = IndexAddRec->getStart();


        


More information about the llvm-commits mailing list