forked from OSchip/llvm-project
[IRCE] consolidate profitability check
Use BFI if it is available and BPI otherwise. This is a promised follow-up after D89541. Reviewers: ebrevnov, mkazantsev Reviewed By: ebrevnov Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D89773
This commit is contained in:
parent
5b68772ca9
commit
75d0e0cd5f
|
@ -233,6 +233,8 @@ public:
|
||||||
SmallVectorImpl<InductiveRangeCheck> &Checks);
|
SmallVectorImpl<InductiveRangeCheck> &Checks);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct LoopStructure;
|
||||||
|
|
||||||
class InductiveRangeCheckElimination {
|
class InductiveRangeCheckElimination {
|
||||||
ScalarEvolution &SE;
|
ScalarEvolution &SE;
|
||||||
BranchProbabilityInfo *BPI;
|
BranchProbabilityInfo *BPI;
|
||||||
|
@ -243,6 +245,10 @@ class InductiveRangeCheckElimination {
|
||||||
llvm::Optional<llvm::function_ref<llvm::BlockFrequencyInfo &()> >;
|
llvm::Optional<llvm::function_ref<llvm::BlockFrequencyInfo &()> >;
|
||||||
GetBFIFunc GetBFI;
|
GetBFIFunc GetBFI;
|
||||||
|
|
||||||
|
// Returns true if it is profitable to do a transform basing on estimation of
|
||||||
|
// number of iterations.
|
||||||
|
bool isProfitableToTransform(const Loop &L, LoopStructure &LS);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
InductiveRangeCheckElimination(ScalarEvolution &SE,
|
InductiveRangeCheckElimination(ScalarEvolution &SE,
|
||||||
BranchProbabilityInfo *BPI, DominatorTree &DT,
|
BranchProbabilityInfo *BPI, DominatorTree &DT,
|
||||||
|
@ -505,9 +511,8 @@ struct LoopStructure {
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Optional<LoopStructure> parseLoopStructure(ScalarEvolution &,
|
static Optional<LoopStructure> parseLoopStructure(ScalarEvolution &, Loop &,
|
||||||
BranchProbabilityInfo *BPI,
|
const char *&);
|
||||||
Loop &, const char *&);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This class is used to constrain loops to run within a given iteration space.
|
/// This class is used to constrain loops to run within a given iteration space.
|
||||||
|
@ -751,8 +756,7 @@ static bool isSafeIncreasingBound(const SCEV *Start,
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<LoopStructure>
|
Optional<LoopStructure>
|
||||||
LoopStructure::parseLoopStructure(ScalarEvolution &SE,
|
LoopStructure::parseLoopStructure(ScalarEvolution &SE, Loop &L,
|
||||||
BranchProbabilityInfo *BPI, Loop &L,
|
|
||||||
const char *&FailureReason) {
|
const char *&FailureReason) {
|
||||||
if (!L.isLoopSimplifyForm()) {
|
if (!L.isLoopSimplifyForm()) {
|
||||||
FailureReason = "loop not in LoopSimplify form";
|
FailureReason = "loop not in LoopSimplify form";
|
||||||
|
@ -787,16 +791,6 @@ LoopStructure::parseLoopStructure(ScalarEvolution &SE,
|
||||||
|
|
||||||
unsigned LatchBrExitIdx = LatchBr->getSuccessor(0) == Header ? 1 : 0;
|
unsigned LatchBrExitIdx = LatchBr->getSuccessor(0) == Header ? 1 : 0;
|
||||||
|
|
||||||
BranchProbability ExitProbability =
|
|
||||||
BPI ? BPI->getEdgeProbability(LatchBr->getParent(), LatchBrExitIdx)
|
|
||||||
: BranchProbability::getZero();
|
|
||||||
|
|
||||||
if (!SkipProfitabilityChecks &&
|
|
||||||
ExitProbability > BranchProbability(1, MaxExitProbReciprocal)) {
|
|
||||||
FailureReason = "short running loop, not profitable";
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
ICmpInst *ICI = dyn_cast<ICmpInst>(LatchBr->getCondition());
|
ICmpInst *ICI = dyn_cast<ICmpInst>(LatchBr->getCondition());
|
||||||
if (!ICI || !isa<IntegerType>(ICI->getOperand(0)->getType())) {
|
if (!ICI || !isa<IntegerType>(ICI->getOperand(0)->getType())) {
|
||||||
FailureReason = "latch terminator branch not conditional on integral icmp";
|
FailureReason = "latch terminator branch not conditional on integral icmp";
|
||||||
|
@ -1855,6 +1849,37 @@ bool IRCELegacyPass::runOnFunction(Function &F) {
|
||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
InductiveRangeCheckElimination::isProfitableToTransform(const Loop &L,
|
||||||
|
LoopStructure &LS) {
|
||||||
|
if (SkipProfitabilityChecks)
|
||||||
|
return true;
|
||||||
|
if (GetBFI.hasValue()) {
|
||||||
|
BlockFrequencyInfo &BFI = (*GetBFI)();
|
||||||
|
uint64_t hFreq = BFI.getBlockFreq(LS.Header).getFrequency();
|
||||||
|
uint64_t phFreq = BFI.getBlockFreq(L.getLoopPreheader()).getFrequency();
|
||||||
|
if (phFreq != 0 && hFreq != 0 && (hFreq / phFreq < MinRuntimeIterations)) {
|
||||||
|
LLVM_DEBUG(dbgs() << "irce: could not prove profitability: "
|
||||||
|
<< "the estimated number of iterations basing on "
|
||||||
|
"frequency info is " << (hFreq / phFreq) << "\n";);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!BPI)
|
||||||
|
return true;
|
||||||
|
BranchProbability ExitProbability =
|
||||||
|
BPI->getEdgeProbability(LS.Latch, LS.LatchBrExitIdx);
|
||||||
|
if (ExitProbability > BranchProbability(1, MaxExitProbReciprocal)) {
|
||||||
|
LLVM_DEBUG(dbgs() << "irce: could not prove profitability: "
|
||||||
|
<< "the exit probability is too big " << ExitProbability
|
||||||
|
<< "\n";);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool InductiveRangeCheckElimination::run(
|
bool InductiveRangeCheckElimination::run(
|
||||||
Loop *L, function_ref<void(Loop *, bool)> LPMAddNewLoop) {
|
Loop *L, function_ref<void(Loop *, bool)> LPMAddNewLoop) {
|
||||||
if (L->getBlocks().size() >= LoopSizeCutoff) {
|
if (L->getBlocks().size() >= LoopSizeCutoff) {
|
||||||
|
@ -1894,25 +1919,15 @@ bool InductiveRangeCheckElimination::run(
|
||||||
|
|
||||||
const char *FailureReason = nullptr;
|
const char *FailureReason = nullptr;
|
||||||
Optional<LoopStructure> MaybeLoopStructure =
|
Optional<LoopStructure> MaybeLoopStructure =
|
||||||
LoopStructure::parseLoopStructure(SE, BPI, *L, FailureReason);
|
LoopStructure::parseLoopStructure(SE, *L, FailureReason);
|
||||||
if (!MaybeLoopStructure.hasValue()) {
|
if (!MaybeLoopStructure.hasValue()) {
|
||||||
LLVM_DEBUG(dbgs() << "irce: could not parse loop structure: "
|
LLVM_DEBUG(dbgs() << "irce: could not parse loop structure: "
|
||||||
<< FailureReason << "\n";);
|
<< FailureReason << "\n";);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LoopStructure LS = MaybeLoopStructure.getValue();
|
LoopStructure LS = MaybeLoopStructure.getValue();
|
||||||
// Profitability check.
|
if (!isProfitableToTransform(*L, LS))
|
||||||
if (!SkipProfitabilityChecks && GetBFI.hasValue()) {
|
|
||||||
BlockFrequencyInfo &BFI = (*GetBFI)();
|
|
||||||
uint64_t hFreq = BFI.getBlockFreq(LS.Header).getFrequency();
|
|
||||||
uint64_t phFreq = BFI.getBlockFreq(Preheader).getFrequency();
|
|
||||||
if (phFreq != 0 && hFreq != 0 && (hFreq / phFreq < MinRuntimeIterations)) {
|
|
||||||
LLVM_DEBUG(dbgs() << "irce: could not prove profitability: "
|
|
||||||
<< "the estimated number of iterations basing on "
|
|
||||||
"frequency info is " << (hFreq / phFreq) << "\n";);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
|
||||||
const SCEVAddRecExpr *IndVar =
|
const SCEVAddRecExpr *IndVar =
|
||||||
cast<SCEVAddRecExpr>(SE.getMinusSCEV(SE.getSCEV(LS.IndVarBase), SE.getSCEV(LS.IndVarStep)));
|
cast<SCEVAddRecExpr>(SE.getMinusSCEV(SE.getSCEV(LS.IndVarBase), SE.getSCEV(LS.IndVarStep)));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue