diff --git a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp --- a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp +++ b/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(SE.getSCEV(Index)); + const auto *IndexAddRec = dyn_cast(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.