Skip to content

Commit 0e3aa08

Browse files
committed
[LSR] Provide TTI hook to enable dropping solutions deemed to be unprofitable
<https://reviews.llvm.org/D126043> introduced a flag to drop solutions if deemed unprofitable. As noted there, introducing a TTI hook enables backends to individually opt into this behaviour.
1 parent 282b56f commit 0e3aa08

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ class TargetTransformInfo {
740740
/// When successful, makes the primary IV dead.
741741
bool shouldFoldTerminatingConditionAfterLSR() const;
742742

743+
/// Return true if LSR should drop a found solution if it's calculated to be
744+
/// less profitable than the baseline.
745+
bool shouldDropLSRSolutionIfLessProfitable() const;
746+
743747
/// \returns true if LSR should not optimize a chain that includes \p I.
744748
bool isProfitableLSRChainElement(Instruction *I) const;
745749

@@ -1861,6 +1865,7 @@ class TargetTransformInfo::Concept {
18611865
const TargetTransformInfo::LSRCost &C2) = 0;
18621866
virtual bool isNumRegsMajorCostOfLSR() = 0;
18631867
virtual bool shouldFoldTerminatingConditionAfterLSR() const = 0;
1868+
virtual bool shouldDropLSRSolutionIfLessProfitable() const = 0;
18641869
virtual bool isProfitableLSRChainElement(Instruction *I) = 0;
18651870
virtual bool canMacroFuseCmp() = 0;
18661871
virtual bool canSaveCmp(Loop *L, BranchInst **BI, ScalarEvolution *SE,
@@ -2333,6 +2338,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
23332338
bool shouldFoldTerminatingConditionAfterLSR() const override {
23342339
return Impl.shouldFoldTerminatingConditionAfterLSR();
23352340
}
2341+
bool shouldDropLSRSolutionIfLessProfitable() const override {
2342+
return Impl.shouldDropLSRSolutionIfLessProfitable();
2343+
}
23362344
bool isProfitableLSRChainElement(Instruction *I) override {
23372345
return Impl.isProfitableLSRChainElement(I);
23382346
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ class TargetTransformInfoImplBase {
240240

241241
bool shouldFoldTerminatingConditionAfterLSR() const { return false; }
242242

243+
bool shouldDropLSRSolutionIfLessProfitable() const { return false; }
244+
243245
bool isProfitableLSRChainElement(Instruction *I) const { return false; }
244246

245247
bool canMacroFuseCmp() const { return false; }

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
398398
shouldFoldTerminatingConditionAfterLSR();
399399
}
400400

401+
bool shouldDropLSRSolutionIfLessProfitable() const {
402+
return TargetTransformInfoImplBase::shouldDropLSRSolutionIfLessProfitable();
403+
}
404+
401405
bool isProfitableLSRChainElement(Instruction *I) {
402406
return TargetTransformInfoImplBase::isProfitableLSRChainElement(I);
403407
}

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ bool TargetTransformInfo::shouldFoldTerminatingConditionAfterLSR() const {
427427
return TTIImpl->shouldFoldTerminatingConditionAfterLSR();
428428
}
429429

430+
bool TargetTransformInfo::shouldDropLSRSolutionIfLessProfitable() const {
431+
return TTIImpl->shouldDropLSRSolutionIfLessProfitable();
432+
}
433+
430434
bool TargetTransformInfo::isProfitableLSRChainElement(Instruction *I) const {
431435
return TTIImpl->isProfitableLSRChainElement(I);
432436
}

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ static cl::opt<cl::boolOrDefault> AllowTerminatingConditionFoldingAfterLSR(
193193
"lsr-term-fold", cl::Hidden,
194194
cl::desc("Attempt to replace primary IV with other IV."));
195195

196-
static cl::opt<bool> AllowDropSolutionIfLessProfitable(
197-
"lsr-drop-solution", cl::Hidden, cl::init(false),
196+
static cl::opt<cl::boolOrDefault> AllowDropSolutionIfLessProfitable(
197+
"lsr-drop-solution", cl::Hidden,
198198
cl::desc("Attempt to drop solution if it is less profitable"));
199199

200200
STATISTIC(NumTermFold,
@@ -5248,10 +5248,22 @@ void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {
52485248

52495249
assert(Solution.size() == Uses.size() && "Malformed solution!");
52505250

5251+
const bool EnableDropUnprofitableSolution = [&] {
5252+
switch (AllowDropSolutionIfLessProfitable) {
5253+
case cl::BOU_TRUE:
5254+
return true;
5255+
case cl::BOU_FALSE:
5256+
return false;
5257+
case cl::BOU_UNSET:
5258+
return TTI.shouldDropLSRSolutionIfLessProfitable();
5259+
}
5260+
llvm_unreachable("Unhandled cl::boolOrDefault enum");
5261+
}();
5262+
52515263
if (BaselineCost.isLess(SolutionCost)) {
52525264
LLVM_DEBUG(dbgs() << "The baseline solution requires ";
52535265
BaselineCost.print(dbgs()); dbgs() << "\n");
5254-
if (!AllowDropSolutionIfLessProfitable)
5266+
if (!EnableDropUnprofitableSolution)
52555267
LLVM_DEBUG(
52565268
dbgs() << "Baseline is more profitable than chosen solution, "
52575269
"add option 'lsr-drop-solution' to drop LSR solution.\n");

0 commit comments

Comments
 (0)