1*9880d681SAndroid Build Coastguard Worker //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements bottom-up inlining of functions into callees.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraph.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/InlineCost.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ProfileSummaryInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallingConv.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO/InlinerPass.h"
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker using namespace llvm;
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "inline"
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker namespace {
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker /// \brief Actual inliner pass implementation.
37*9880d681SAndroid Build Coastguard Worker ///
38*9880d681SAndroid Build Coastguard Worker /// The common implementation of the inlining logic is shared between this
39*9880d681SAndroid Build Coastguard Worker /// inliner pass and the always inliner pass. The two passes use different cost
40*9880d681SAndroid Build Coastguard Worker /// analyses to determine when to inline.
41*9880d681SAndroid Build Coastguard Worker class SimpleInliner : public Inliner {
42*9880d681SAndroid Build Coastguard Worker // This field is populated based on one of the following:
43*9880d681SAndroid Build Coastguard Worker // * optimization or size-optimization levels,
44*9880d681SAndroid Build Coastguard Worker // * the --inline-threshold flag, or
45*9880d681SAndroid Build Coastguard Worker // * a user specified value.
46*9880d681SAndroid Build Coastguard Worker int DefaultThreshold;
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker public:
SimpleInliner()49*9880d681SAndroid Build Coastguard Worker SimpleInliner()
50*9880d681SAndroid Build Coastguard Worker : Inliner(ID), DefaultThreshold(llvm::getDefaultInlineThreshold()) {
51*9880d681SAndroid Build Coastguard Worker initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker
SimpleInliner(int Threshold)54*9880d681SAndroid Build Coastguard Worker explicit SimpleInliner(int Threshold)
55*9880d681SAndroid Build Coastguard Worker : Inliner(ID), DefaultThreshold(Threshold) {
56*9880d681SAndroid Build Coastguard Worker initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
60*9880d681SAndroid Build Coastguard Worker
getInlineCost(CallSite CS)61*9880d681SAndroid Build Coastguard Worker InlineCost getInlineCost(CallSite CS) override {
62*9880d681SAndroid Build Coastguard Worker Function *Callee = CS.getCalledFunction();
63*9880d681SAndroid Build Coastguard Worker TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
64*9880d681SAndroid Build Coastguard Worker return llvm::getInlineCost(CS, DefaultThreshold, TTI, ACT, PSI);
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker bool runOnSCC(CallGraphSCC &SCC) override;
68*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override;
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker private:
71*9880d681SAndroid Build Coastguard Worker TargetTransformInfoWrapperPass *TTIWP;
72*9880d681SAndroid Build Coastguard Worker };
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker char SimpleInliner::ID = 0;
77*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
78*9880d681SAndroid Build Coastguard Worker "Function Integration/Inlining", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)79*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
80*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
81*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
82*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
83*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
84*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(SimpleInliner, "inline",
85*9880d681SAndroid Build Coastguard Worker "Function Integration/Inlining", false, false)
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
88*9880d681SAndroid Build Coastguard Worker
createFunctionInliningPass(int Threshold)89*9880d681SAndroid Build Coastguard Worker Pass *llvm::createFunctionInliningPass(int Threshold) {
90*9880d681SAndroid Build Coastguard Worker return new SimpleInliner(Threshold);
91*9880d681SAndroid Build Coastguard Worker }
92*9880d681SAndroid Build Coastguard Worker
createFunctionInliningPass(unsigned OptLevel,unsigned SizeOptLevel)93*9880d681SAndroid Build Coastguard Worker Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
94*9880d681SAndroid Build Coastguard Worker unsigned SizeOptLevel) {
95*9880d681SAndroid Build Coastguard Worker return new SimpleInliner(
96*9880d681SAndroid Build Coastguard Worker llvm::computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker
runOnSCC(CallGraphSCC & SCC)99*9880d681SAndroid Build Coastguard Worker bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
100*9880d681SAndroid Build Coastguard Worker TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
101*9880d681SAndroid Build Coastguard Worker return Inliner::runOnSCC(SCC);
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const104*9880d681SAndroid Build Coastguard Worker void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
105*9880d681SAndroid Build Coastguard Worker AU.addRequired<TargetTransformInfoWrapperPass>();
106*9880d681SAndroid Build Coastguard Worker Inliner::getAnalysisUsage(AU);
107*9880d681SAndroid Build Coastguard Worker }
108