[llvm] 69f99f5 - [NFC][IRCE] Check that Index is AddRec in the parseRangeCheckICmp

Aleksandr Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 4 06:04:26 PDT 2023


Author: Aleksandr Popov
Date: 2023-07-03T10:02:42+02:00
New Revision: 69f99f5308edf7dcb0cd688d7028733088305946

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

LOG: [NFC][IRCE] Check that Index is AddRec in the parseRangeCheckICmp

Next step of the preparatory refactoring for the upcoming support of
new range check form to parse.

Previous one: https://reviews.llvm.org/D154156

With this change we avoid meaningless parsing after realizing that Index
is not AddRec

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

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 f5deff564d0ca0..c3955382d92c35 100644
--- a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
@@ -142,7 +142,8 @@ class InductiveRangeCheck {
   Use *CheckUse = nullptr;
 
   static bool parseRangeCheckICmp(Loop *L, ICmpInst *ICI, ScalarEvolution &SE,
-                                  const SCEV *&Index, const SCEV *&End);
+                                  const SCEVAddRecExpr *&Index,
+                                  const SCEV *&End);
 
   static void
   extractRangeChecksFromCond(Loop *L, ScalarEvolution &SE, Use &ConditionUse,
@@ -254,7 +255,7 @@ class InductiveRangeCheckElimination {
 /// is being range checked.
 bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
                                               ScalarEvolution &SE,
-                                              const SCEV *&Index,
+                                              const SCEVAddRecExpr *&Index,
                                               const SCEV *&End) {
   auto IsLoopInvariant = [&SE, L](Value *V) {
     return SE.isLoopInvariant(SE.getSCEV(V), L);
@@ -277,6 +278,10 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
     // Both LHS and RHS are loop variant
     return false;
 
+  const auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(LHS));
+  if (!AddRec)
+    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
@@ -287,7 +292,7 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
 
   case ICmpInst::ICMP_SGE:
     if (match(RHS, m_ConstantInt<0>())) {
-      Index = SE.getSCEV(LHS);
+      Index = AddRec;
       End = SIntMaxSCEV(Index->getType());
       return true;
     }
@@ -295,7 +300,7 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
 
   case ICmpInst::ICMP_SGT:
     if (match(RHS, m_ConstantInt<-1>())) {
-      Index = SE.getSCEV(LHS);
+      Index = AddRec;
       End = SIntMaxSCEV(Index->getType());
       return true;
     }
@@ -303,7 +308,7 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
 
   case ICmpInst::ICMP_SLT:
   case ICmpInst::ICMP_ULT:
-    Index = SE.getSCEV(LHS);
+    Index = AddRec;
     End = SE.getSCEV(RHS);
     return true;
 
@@ -313,7 +318,7 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
     const SCEV *RHSS = SE.getSCEV(RHS);
     bool Signed = Pred == ICmpInst::ICMP_SLE;
     if (SE.willNotOverflow(Instruction::BinaryOps::Add, Signed, RHSS, One)) {
-      Index = SE.getSCEV(LHS);
+      Index = AddRec;
       End = SE.getAddExpr(RHSS, One);
       return true;
     }
@@ -344,18 +349,15 @@ void InductiveRangeCheck::extractRangeChecksFromCond(
   if (!ICI)
     return;
 
-  const SCEV *End = nullptr, *Index = nullptr;
-  if (!parseRangeCheckICmp(L, ICI, SE, Index, End))
+  const SCEV *End = nullptr;
+  const SCEVAddRecExpr *IndexAddRec = nullptr;
+  if (!parseRangeCheckICmp(L, ICI, SE, IndexAddRec, End))
     return;
 
-  assert(Index && "Index was not computed");
+  assert(IndexAddRec && "IndexAddRec was not computed");
   assert(End && "End was not computed");
 
-  const auto *IndexAddRec = dyn_cast<SCEVAddRecExpr>(Index);
-  bool IsAffineIndex =
-      IndexAddRec && (IndexAddRec->getLoop() == L) && IndexAddRec->isAffine();
-
-  if (!IsAffineIndex)
+  if ((IndexAddRec->getLoop() != L) || !IndexAddRec->isAffine())
     return;
 
   InductiveRangeCheck IRC;


        


More information about the llvm-commits mailing list