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

Aleksandr Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 29 14:41:29 PDT 2023


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

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.


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
@@ -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.536007.patch
Type: text/x-patch
Size: 2474 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230629/ced97517/attachment.bin>


More information about the llvm-commits mailing list