Move duplicated code into a helper function (exposed through overload).

There's a bit of duplicated "magic" code in opt.cpp and Clang's CodeGen that
computes the inliner threshold from opt level and size opt level.

This patch moves the code to a function that lives alongside the inliner itself,
providing a convenient overload to the inliner creation.

A separate patch can be committed to Clang to use this once it's committed to
LLVM. Standalone tools that use the inlining pass can also avoid duplicating
this code and fearing it will go out of sync.

Note: this patch also restructures the conditinal logic of the computation to
be cleaner.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203669 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eli Bendersky 2014-03-12 16:12:36 +00:00
parent 3d37204ca6
commit ce306f9f99
3 changed files with 26 additions and 12 deletions

View File

@ -85,10 +85,14 @@ ModulePass *createGVExtractionPass(std::vector<GlobalValue*>& GVs, bool
/// createFunctionInliningPass - Return a new pass object that uses a heuristic /// createFunctionInliningPass - Return a new pass object that uses a heuristic
/// to inline direct function calls to small functions. /// to inline direct function calls to small functions.
/// ///
/// The Threshold can be passed directly, or asked to be computed from the
/// given optimization and size optimization arguments.
///
/// The -inline-threshold command line option takes precedence over the /// The -inline-threshold command line option takes precedence over the
/// threshold given here. /// threshold given here.
Pass *createFunctionInliningPass(); Pass *createFunctionInliningPass();
Pass *createFunctionInliningPass(int Threshold); Pass *createFunctionInliningPass(int Threshold);
Pass *createFunctionInliningPass(unsigned OptLevel, unsigned SizeOptLevel);
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// createAlwaysInlinerPass - Return a new pass object that inlines only /// createAlwaysInlinerPass - Return a new pass object that inlines only

View File

@ -56,6 +56,17 @@ public:
void getAnalysisUsage(AnalysisUsage &AU) const override; void getAnalysisUsage(AnalysisUsage &AU) const override;
}; };
static int computeThresholdFromOptLevels(unsigned OptLevel,
unsigned SizeOptLevel) {
if (OptLevel > 2)
return 275;
if (SizeOptLevel == 1)
return 75;
if (SizeOptLevel == 2)
return 25;
return 225;
}
} // end anonymous namespace } // end anonymous namespace
char SimpleInliner::ID = 0; char SimpleInliner::ID = 0;
@ -72,6 +83,12 @@ Pass *llvm::createFunctionInliningPass(int Threshold) {
return new SimpleInliner(Threshold); return new SimpleInliner(Threshold);
} }
Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
unsigned SizeOptLevel) {
return new SimpleInliner(
computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
}
bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) { bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
ICA = &getAnalysis<InlineCostAnalysis>(); ICA = &getAnalysis<InlineCostAnalysis>();
return Inliner::runOnSCC(SCC); return Inliner::runOnSCC(SCC);

View File

@ -210,14 +210,7 @@ static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
if (DisableInline) { if (DisableInline) {
// No inlining pass // No inlining pass
} else if (OptLevel > 1) { } else if (OptLevel > 1) {
unsigned Threshold = 225; Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
if (SizeLevel == 1) // -Os
Threshold = 75;
else if (SizeLevel == 2) // -Oz
Threshold = 25;
if (OptLevel > 2)
Threshold = 275;
Builder.Inliner = createFunctionInliningPass(Threshold);
} else { } else {
Builder.Inliner = createAlwaysInlinerPass(); Builder.Inliner = createAlwaysInlinerPass();
} }