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 #define DEBUG_TYPE "inline"
15 #include "llvm/Transforms/IPO.h"
16 #include "llvm/Analysis/CallGraph.h"
17 #include "llvm/Analysis/InlineCost.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/CallingConv.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/Transforms/IPO/InlinerPass.h"
31 /// \brief Actual inliner pass implementation.
33 /// The common implementation of the inlining logic is shared between this
34 /// inliner pass and the always inliner pass. The two passes use different cost
35 /// analyses to determine when to inline.
36 class SimpleInliner : public Inliner {
37 InlineCostAnalysis *ICA;
40 SimpleInliner() : Inliner(ID), ICA(0) {
41 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
44 SimpleInliner(int Threshold)
45 : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(0) {
46 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
49 static char ID; // Pass identification, replacement for typeid
51 InlineCost getInlineCost(CallSite CS) override {
52 return ICA->getInlineCost(CS, getInlineThreshold(CS));
55 bool runOnSCC(CallGraphSCC &SCC) override;
56 void getAnalysisUsage(AnalysisUsage &AU) const override;
59 static int computeThresholdFromOptLevels(unsigned OptLevel,
60 unsigned SizeOptLevel) {
63 if (SizeOptLevel == 1) // -Os
65 if (SizeOptLevel == 2) // -Oz
70 } // end anonymous namespace
72 char SimpleInliner::ID = 0;
73 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
74 "Function Integration/Inlining", false, false)
75 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
76 INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
77 INITIALIZE_PASS_END(SimpleInliner, "inline",
78 "Function Integration/Inlining", false, false)
80 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
82 Pass *llvm::createFunctionInliningPass(int Threshold) {
83 return new SimpleInliner(Threshold);
86 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
87 unsigned SizeOptLevel) {
88 return new SimpleInliner(
89 computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
92 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
93 ICA = &getAnalysis<InlineCostAnalysis>();
94 return Inliner::runOnSCC(SCC);
97 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.addRequired<InlineCostAnalysis>();
99 Inliner::getAnalysisUsage(AU);