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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rGf56e84736297: [IRCE][NFC] Set Index and End together inside parseRangeCheckICmp (authored by aleksandr.popov).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154156

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
@@ -249,9 +249,9 @@
 } // 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 @@
     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 @@
     // 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 @@
   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 @@
   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 @@
   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 @@
   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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154156.536988.patch
Type: text/x-patch
Size: 3228 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230704/2da2232a/attachment.bin>


More information about the llvm-commits mailing list