Skip to content

Commit a39b807

Browse files
committed
[IRCE][NFC] Refactor parseRangeCheckICmp to compute SCEVs instead of Values
The motivation is to make an opportunity to compute and return expressions after parsing ICmp into a range check (e.g. Length + 1). Patch by Aleksandr Popov! Differential Revision: https://reviews.llvm.org/D148205
1 parent 854686f commit a39b807

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class InductiveRangeCheck {
145145
Use *CheckUse = nullptr;
146146

147147
static bool parseRangeCheckICmp(Loop *L, ICmpInst *ICI, ScalarEvolution &SE,
148-
Value *&Index, Value *&Length);
148+
const SCEV *&Index, const SCEV *&End);
149149

150150
static void
151151
extractRangeChecksFromCond(Loop *L, ScalarEvolution &SE, Use &ConditionUse,
@@ -284,13 +284,13 @@ INITIALIZE_PASS_END(IRCELegacyPass, "irce", "Inductive range check elimination",
284284
false, false)
285285

286286
/// Parse a single ICmp instruction, `ICI`, into a range check. If `ICI` cannot
287-
/// be interpreted as a range check, return false and set `Index` and `Length`
288-
/// to `nullptr`. Otherwise set `Index` to the value being range checked, and
289-
/// set `Length` to the upper limit `Index` is being range checked.
290-
bool
291-
InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
292-
ScalarEvolution &SE, Value *&Index,
293-
Value *&Length) {
287+
/// be interpreted as a range check, return false and set `Index` and `End`
288+
/// to `nullptr`. Otherwise set `Index` to the SCEV being range checked, and
289+
/// set `End` to the upper limit `Index` is being range checked.
290+
bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
291+
ScalarEvolution &SE,
292+
const SCEV *&Index,
293+
const SCEV *&End) {
294294
auto IsLoopInvariant = [&SE, L](Value *V) {
295295
return SE.isLoopInvariant(SE.getSCEV(V), L);
296296
};
@@ -308,7 +308,7 @@ InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
308308
[[fallthrough]];
309309
case ICmpInst::ICMP_SGE:
310310
if (match(RHS, m_ConstantInt<0>())) {
311-
Index = LHS;
311+
Index = SE.getSCEV(LHS);
312312
return true; // Lower.
313313
}
314314
return false;
@@ -318,13 +318,13 @@ InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
318318
[[fallthrough]];
319319
case ICmpInst::ICMP_SGT:
320320
if (match(RHS, m_ConstantInt<-1>())) {
321-
Index = LHS;
321+
Index = SE.getSCEV(LHS);
322322
return true; // Lower.
323323
}
324324

325325
if (IsLoopInvariant(LHS)) {
326-
Index = RHS;
327-
Length = LHS;
326+
Index = SE.getSCEV(RHS);
327+
End = SE.getSCEV(LHS);
328328
return true; // Upper.
329329
}
330330
return false;
@@ -334,8 +334,8 @@ InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
334334
[[fallthrough]];
335335
case ICmpInst::ICMP_UGT:
336336
if (IsLoopInvariant(LHS)) {
337-
Index = RHS;
338-
Length = LHS;
337+
Index = SE.getSCEV(RHS);
338+
End = SE.getSCEV(LHS);
339339
return true; // Both lower and upper.
340340
}
341341
return false;
@@ -365,23 +365,20 @@ void InductiveRangeCheck::extractRangeChecksFromCond(
365365
if (!ICI)
366366
return;
367367

368-
Value *Length = nullptr, *Index;
369-
if (!parseRangeCheckICmp(L, ICI, SE, Index, Length))
368+
const SCEV *End = nullptr, *Index = nullptr;
369+
if (!parseRangeCheckICmp(L, ICI, SE, Index, End))
370370
return;
371371

372-
const auto *IndexAddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Index));
372+
const auto *IndexAddRec = dyn_cast<SCEVAddRecExpr>(Index);
373373
bool IsAffineIndex =
374374
IndexAddRec && (IndexAddRec->getLoop() == L) && IndexAddRec->isAffine();
375375

376376
if (!IsAffineIndex)
377377
return;
378378

379-
const SCEV *End = nullptr;
380379
// We strengthen "0 <= I" to "0 <= I < INT_SMAX" and "I < L" to "0 <= I < L".
381380
// We can potentially do much better here.
382-
if (Length)
383-
End = SE.getSCEV(Length);
384-
else {
381+
if (!End) {
385382
// So far we can only reach this point for Signed range check. This may
386383
// change in future. In this case we will need to pick Unsigned max for the
387384
// unsigned range check.

0 commit comments

Comments
 (0)