1 //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements bottom-up inlining of functions into callees.
12 //===----------------------------------------------------------------------===//
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"
31 #define DEBUG_TYPE "inline"
35 /// \brief Actual inliner pass implementation.
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 {
43 SimpleInliner() : Inliner(ID) {
44 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
47 SimpleInliner(int Threshold)
48 : Inliner(ID, Threshold, /*InsertLifetime*/ true) {
49 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
52 static char ID; // Pass identification, replacement for typeid
54 InlineCost getInlineCost(CallSite CS) override {
55 Function *Callee = CS.getCalledFunction();
56 TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
57 return llvm::getInlineCost(CS, getInlineThreshold(CS), TTI, ACT);
60 bool runOnSCC(CallGraphSCC &SCC) override;
61 void getAnalysisUsage(AnalysisUsage &AU) const override;
64 TargetTransformInfoWrapperPass *TTIWP;
67 static int computeThresholdFromOptLevels(unsigned OptLevel,
68 unsigned SizeOptLevel) {
71 if (SizeOptLevel == 1) // -Os
73 if (SizeOptLevel == 2) // -Oz
78 } // end anonymous namespace
80 char SimpleInliner::ID = 0;
81 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
82 "Function Integration/Inlining", false, false)
83 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
84 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
85 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
86 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
87 INITIALIZE_PASS_END(SimpleInliner, "inline",
88 "Function Integration/Inlining", false, false)
90 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
92 Pass *llvm::createFunctionInliningPass(int Threshold) {
93 return new SimpleInliner(Threshold);
96 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
97 unsigned SizeOptLevel) {
98 return new SimpleInliner(
99 computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
102 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
103 TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
104 return Inliner::runOnSCC(SCC);
107 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.addRequired<TargetTransformInfoWrapperPass>();
109 Inliner::getAnalysisUsage(AU);