Skip to content

Commit 400ceda

Browse files
committed
[SCEV][IndVars] Always provide insertion point to the SCEVExpander::isHighCostExpansion()
Summary: This addresses the `llvm/test/Transforms/IndVarSimplify/elim-extend.ll` `@nestedIV` regression from D73728 Reviewers: reames, mkazantsev, wmi, sanjoy Reviewed By: mkazantsev Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D73777
1 parent 44edc6f commit 400ceda

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

llvm/include/llvm/Analysis/ScalarEvolutionExpander.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,19 @@ namespace llvm {
176176
/// Return true for expressions that can't be evaluated at runtime
177177
/// within given \b Budget.
178178
///
179-
/// At is an optional parameter which specifies point in code where user is
180-
/// going to expand this expression. Sometimes this knowledge can lead to a
181-
/// more accurate cost estimation.
179+
/// At is a parameter which specifies point in code where user is going to
180+
/// expand this expression. Sometimes this knowledge can lead to
181+
/// a less pessimistic cost estimation.
182182
bool isHighCostExpansion(const SCEV *Expr, Loop *L, unsigned Budget,
183183
const TargetTransformInfo *TTI,
184-
const Instruction *At = nullptr) {
184+
const Instruction *At) {
185185
assert(TTI && "This function requires TTI to be provided.");
186+
assert(At && "This function requires At instruction to be provided.");
186187
if (!TTI) // In assert-less builds, avoid crashing
187188
return true; // by always claiming to be high-cost.
188189
SmallPtrSet<const SCEV *, 8> Processed;
189190
int BudgetRemaining = Budget * TargetTransformInfo::TCC_Basic;
190-
return isHighCostExpansionHelper(Expr, L, At, BudgetRemaining, *TTI,
191+
return isHighCostExpansionHelper(Expr, L, *At, BudgetRemaining, *TTI,
191192
Processed);
192193
}
193194

@@ -331,7 +332,7 @@ namespace llvm {
331332

332333
/// Recursive helper function for isHighCostExpansion.
333334
bool isHighCostExpansionHelper(const SCEV *S, Loop *L,
334-
const Instruction *At, int &BudgetRemaining,
335+
const Instruction &At, int &BudgetRemaining,
335336
const TargetTransformInfo &TTI,
336337
SmallPtrSetImpl<const SCEV *> &Processed);
337338

llvm/lib/Analysis/ScalarEvolutionExpander.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,7 @@ SCEVExpander::getRelatedExistingExpansion(const SCEV *S, const Instruction *At,
21362136
}
21372137

21382138
bool SCEVExpander::isHighCostExpansionHelper(
2139-
const SCEV *S, Loop *L, const Instruction *At, int &BudgetRemaining,
2139+
const SCEV *S, Loop *L, const Instruction &At, int &BudgetRemaining,
21402140
const TargetTransformInfo &TTI, SmallPtrSetImpl<const SCEV *> &Processed) {
21412141
if (BudgetRemaining < 0)
21422142
return true; // Already run out of budget, give up.
@@ -2147,7 +2147,7 @@ bool SCEVExpander::isHighCostExpansionHelper(
21472147

21482148
// If we can find an existing value for this scev available at the point "At"
21492149
// then consider the expression cheap.
2150-
if (At && getRelatedExistingExpansion(S, At, L))
2150+
if (getRelatedExistingExpansion(S, &At, L))
21512151
return false; // Consider the expression to be free.
21522152

21532153
switch (S->getSCEVType()) {
@@ -2198,18 +2198,12 @@ bool SCEVExpander::isHighCostExpansionHelper(
21982198
// UDiv from the user's code. If we can't find a UDiv in the code with some
21992199
// simple searching, we need to account for it's cost.
22002200

2201-
BasicBlock *ExitingBB = L->getExitingBlock();
2202-
if (At || ExitingBB) {
2203-
if (!At)
2204-
At = &ExitingBB->back();
2205-
2206-
// At the beginning of this function we already tried to find existing
2207-
// value for plain 'S'. Now try to lookup 'S + 1' since it is common
2208-
// pattern involving division. This is just a simple search heuristic.
2209-
if (getRelatedExistingExpansion(
2210-
SE.getAddExpr(S, SE.getConstant(S->getType(), 1)), At, L))
2211-
return false; // Consider it to be free.
2212-
}
2201+
// At the beginning of this function we already tried to find existing
2202+
// value for plain 'S'. Now try to lookup 'S + 1' since it is common
2203+
// pattern involving division. This is just a simple search heuristic.
2204+
if (getRelatedExistingExpansion(
2205+
SE.getAddExpr(S, SE.getConstant(S->getType(), 1)), &At, L))
2206+
return false; // Consider it to be free.
22132207

22142208
// Need to count the cost of this UDiv.
22152209
BudgetRemaining -= TTI.getOperationCost(Instruction::UDiv, S->getType());

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2726,6 +2726,9 @@ bool IndVarSimplify::run(Loop *L) {
27262726
// If we have a trip count expression, rewrite the loop's exit condition
27272727
// using it.
27282728
if (!DisableLFTR) {
2729+
BasicBlock *PreHeader = L->getLoopPreheader();
2730+
BranchInst *PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
2731+
27292732
SmallVector<BasicBlock*, 16> ExitingBlocks;
27302733
L->getExitingBlocks(ExitingBlocks);
27312734
for (BasicBlock *ExitingBB : ExitingBlocks) {
@@ -2760,7 +2763,7 @@ bool IndVarSimplify::run(Loop *L) {
27602763
// Avoid high cost expansions. Note: This heuristic is questionable in
27612764
// that our definition of "high cost" is not exactly principled.
27622765
if (Rewriter.isHighCostExpansion(ExitCount, L, SCEVCheapExpansionBudget,
2763-
TTI))
2766+
TTI, PreHeaderBR))
27642767
continue;
27652768

27662769
// Check preconditions for proper SCEVExpander operation. SCEV does not

llvm/test/Transforms/IndVarSimplify/elim-extend.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ define void @nestedIV(i8* %address, i32 %limit) nounwind {
140140
; CHECK-NEXT: store i8 0, i8* [[ADR2]]
141141
; CHECK-NEXT: [[ADR3:%.*]] = getelementptr i8, i8* [[ADDRESS]], i64 [[INDVARS_IV_NEXT]]
142142
; CHECK-NEXT: store i8 0, i8* [[ADR3]]
143-
; CHECK-NEXT: [[INNERCMP:%.*]] = icmp sgt i64 [[TMP0]], [[INDVARS_IV_NEXT]]
144-
; CHECK-NEXT: br i1 [[INNERCMP]], label [[INNERLOOP]], label [[INNEREXIT:%.*]]
143+
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT]], [[TMP0]]
144+
; CHECK-NEXT: br i1 [[EXITCOND]], label [[INNERLOOP]], label [[INNEREXIT:%.*]]
145145
; CHECK: innerexit:
146146
; CHECK-NEXT: [[TMP4:%.*]] = trunc i64 [[TMP0]] to i32
147147
; CHECK-NEXT: br label [[OUTERMERGE]]
@@ -153,8 +153,8 @@ define void @nestedIV(i8* %address, i32 %limit) nounwind {
153153
; CHECK-NEXT: [[ADR5:%.*]] = getelementptr i8, i8* [[ADDRESS]], i64 [[OFS5]]
154154
; CHECK-NEXT: store i8 0, i8* [[ADR5]]
155155
; CHECK-NEXT: [[INDVARS_IV_NEXT2]] = add nuw nsw i64 [[INDVARS_IV1]], 1
156-
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT2]], [[WIDE_TRIP_COUNT]]
157-
; CHECK-NEXT: br i1 [[EXITCOND]], label [[OUTERLOOP]], label [[RETURN:%.*]]
156+
; CHECK-NEXT: [[EXITCOND4:%.*]] = icmp ne i64 [[INDVARS_IV_NEXT2]], [[WIDE_TRIP_COUNT]]
157+
; CHECK-NEXT: br i1 [[EXITCOND4]], label [[OUTERLOOP]], label [[RETURN:%.*]]
158158
; CHECK: return:
159159
; CHECK-NEXT: ret void
160160
;

0 commit comments

Comments
 (0)