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