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/ProfileSummaryInfo.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Transforms/IPO.h"
28 #include "llvm/Transforms/IPO/InlinerPass.h"
29 
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "inline"
33 
34 namespace {
35 
36 /// \brief Actual inliner pass implementation.
37 ///
38 /// The common implementation of the inlining logic is shared between this
39 /// inliner pass and the always inliner pass. The two passes use different cost
40 /// analyses to determine when to inline.
41 class SimpleInliner : public Inliner {
42 
43   InlineParams Params;
44 
45 public:
46   SimpleInliner() : Inliner(ID), Params(llvm::getInlineParams()) {
47     initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
48   }
49 
50   explicit SimpleInliner(InlineParams Params) : Inliner(ID), Params(Params) {
51     initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
52   }
53 
54   static char ID; // Pass identification, replacement for typeid
55 
56   InlineCost getInlineCost(CallSite CS) override {
57     Function *Callee = CS.getCalledFunction();
58     TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
59     std::function<AssumptionCache &(Function &)> GetAssumptionCache =
60         [&](Function &F) -> AssumptionCache & {
61       return ACT->getAssumptionCache(F);
62     };
63     return llvm::getInlineCost(CS, Params, TTI, GetAssumptionCache, PSI);
64   }
65 
66   bool runOnSCC(CallGraphSCC &SCC) override;
67   void getAnalysisUsage(AnalysisUsage &AU) const override;
68 
69 private:
70   TargetTransformInfoWrapperPass *TTIWP;
71 
72 };
73 
74 } // end anonymous namespace
75 
76 char SimpleInliner::ID = 0;
77 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", "Function Integration/Inlining",
78                       false, false)
79 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
80 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
81 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
82 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
83 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
84 INITIALIZE_PASS_END(SimpleInliner, "inline", "Function Integration/Inlining",
85                     false, false)
86 
87 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
88 
89 Pass *llvm::createFunctionInliningPass(int Threshold) {
90   return new SimpleInliner(llvm::getInlineParams(Threshold));
91 }
92 
93 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
94                                        unsigned SizeOptLevel) {
95   return new SimpleInliner(llvm::getInlineParams(OptLevel, SizeOptLevel));
96 }
97 
98 Pass *llvm::createFunctionInliningPass(InlineParams &Params) {
99   return new SimpleInliner(Params);
100 }
101 
102 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
103   TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
104   return Inliner::runOnSCC(SCC);
105 }
106 
107 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
108   AU.addRequired<TargetTransformInfoWrapperPass>();
109   Inliner::getAnalysisUsage(AU);
110 }
111