Skip to content

Commit fc5254c

Browse files
committed
[LoopUtils] Simplify code for runtime check generation a bit (NFCI).
Store getSE result in variable to re-use and use structured bindings when looping over bounds.
1 parent 9372e1a commit fc5254c

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,24 +1743,24 @@ static PointerBounds expandBounds(const RuntimeCheckingPtrGroup *CG,
17431743
auto *HighAR = cast<SCEVAddRecExpr>(High);
17441744
auto *LowAR = cast<SCEVAddRecExpr>(Low);
17451745
const Loop *OuterLoop = TheLoop->getParentLoop();
1746-
const SCEV *Recur = LowAR->getStepRecurrence(*Exp.getSE());
1747-
if (Recur == HighAR->getStepRecurrence(*Exp.getSE()) &&
1746+
ScalarEvolution &SE = *Exp.getSE();
1747+
const SCEV *Recur = LowAR->getStepRecurrence(SE);
1748+
if (Recur == HighAR->getStepRecurrence(SE) &&
17481749
HighAR->getLoop() == OuterLoop && LowAR->getLoop() == OuterLoop) {
17491750
BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
1750-
const SCEV *OuterExitCount =
1751-
Exp.getSE()->getExitCount(OuterLoop, OuterLoopLatch);
1751+
const SCEV *OuterExitCount = SE.getExitCount(OuterLoop, OuterLoopLatch);
17521752
if (!isa<SCEVCouldNotCompute>(OuterExitCount) &&
17531753
OuterExitCount->getType()->isIntegerTy()) {
1754-
const SCEV *NewHigh = cast<SCEVAddRecExpr>(High)->evaluateAtIteration(
1755-
OuterExitCount, *Exp.getSE());
1754+
const SCEV *NewHigh =
1755+
cast<SCEVAddRecExpr>(High)->evaluateAtIteration(OuterExitCount, SE);
17561756
if (!isa<SCEVCouldNotCompute>(NewHigh)) {
17571757
LLVM_DEBUG(dbgs() << "LAA: Expanded RT check for range to include "
17581758
"outer loop in order to permit hoisting\n");
17591759
High = NewHigh;
17601760
Low = cast<SCEVAddRecExpr>(Low)->getStart();
17611761
// If there is a possibility that the stride is negative then we have
17621762
// to generate extra checks to ensure the stride is positive.
1763-
if (!Exp.getSE()->isKnownNonNegative(Recur)) {
1763+
if (!SE.isKnownNonNegative(Recur)) {
17641764
Stride = Recur;
17651765
LLVM_DEBUG(dbgs() << "LAA: ... but need to check stride is "
17661766
"positive: "
@@ -1821,8 +1821,7 @@ Value *llvm::addRuntimeChecks(
18211821
// Our instructions might fold to a constant.
18221822
Value *MemoryRuntimeCheck = nullptr;
18231823

1824-
for (const auto &Check : ExpandedChecks) {
1825-
const PointerBounds &A = Check.first, &B = Check.second;
1824+
for (const auto &[A, B] : ExpandedChecks) {
18261825
// Check if two pointers (A and B) conflict where conflict is computed as:
18271826
// start(A) <= end(B) && start(B) <= end(A)
18281827

@@ -1880,14 +1879,14 @@ Value *llvm::addDiffRuntimeChecks(
18801879
// Map to keep track of created compares, The key is the pair of operands for
18811880
// the compare, to allow detecting and re-using redundant compares.
18821881
DenseMap<std::pair<Value *, Value *>, Value *> SeenCompares;
1883-
for (const auto &C : Checks) {
1884-
Type *Ty = C.SinkStart->getType();
1882+
for (const auto &[SrcStart, SinkStart, AccessSize, NeedsFreeze] : Checks) {
1883+
Type *Ty = SinkStart->getType();
18851884
// Compute VF * IC * AccessSize.
18861885
auto *VFTimesUFTimesSize =
18871886
ChkBuilder.CreateMul(GetVF(ChkBuilder, Ty->getScalarSizeInBits()),
1888-
ConstantInt::get(Ty, IC * C.AccessSize));
1889-
Value *Diff = Expander.expandCodeFor(
1890-
SE.getMinusSCEV(C.SinkStart, C.SrcStart), Ty, Loc);
1887+
ConstantInt::get(Ty, IC * AccessSize));
1888+
Value *Diff =
1889+
Expander.expandCodeFor(SE.getMinusSCEV(SinkStart, SrcStart), Ty, Loc);
18911890

18921891
// Check if the same compare has already been created earlier. In that case,
18931892
// there is no need to check it again.
@@ -1898,7 +1897,7 @@ Value *llvm::addDiffRuntimeChecks(
18981897
IsConflict =
18991898
ChkBuilder.CreateICmpULT(Diff, VFTimesUFTimesSize, "diff.check");
19001899
SeenCompares.insert({{Diff, VFTimesUFTimesSize}, IsConflict});
1901-
if (C.NeedsFreeze)
1900+
if (NeedsFreeze)
19021901
IsConflict =
19031902
ChkBuilder.CreateFreeze(IsConflict, IsConflict->getName() + ".fr");
19041903
if (MemoryRuntimeCheck) {

0 commit comments

Comments
 (0)