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/Transforms/IPO.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/Analysis/AssumptionCache.h"
17 #include "llvm/Analysis/CallGraph.h"
18 #include "llvm/Analysis/InlineCost.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/InlinerPass.h"
30 #define DEBUG_TYPE "inline"
34 /// \brief Actual inliner pass implementation.
36 /// The common implementation of the inlining logic is shared between this
37 /// inliner pass and the always inliner pass. The two passes use different cost
38 /// analyses to determine when to inline.
39 class SimpleInliner : public Inliner {
40 InlineCostAnalysis *ICA;
43 SimpleInliner() : Inliner(ID), ICA(nullptr) {
44 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
47 SimpleInliner(int Threshold)
48 : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(nullptr) {
49 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
52 static char ID; // Pass identification, replacement for typeid
54 InlineCost getInlineCost(CallSite CS) override {
55 return ICA->getInlineCost(CS, getInlineThreshold(CS));
58 bool runOnSCC(CallGraphSCC &SCC) override;
59 void getAnalysisUsage(AnalysisUsage &AU) const override;
62 static int computeThresholdFromOptLevels(unsigned OptLevel,
63 unsigned SizeOptLevel) {
66 if (SizeOptLevel == 1) // -Os
68 if (SizeOptLevel == 2) // -Oz
73 } // end anonymous namespace
75 char SimpleInliner::ID = 0;
76 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
77 "Function Integration/Inlining", false, false)
78 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
79 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
80 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
81 INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
82 INITIALIZE_PASS_END(SimpleInliner, "inline",
83 "Function Integration/Inlining", false, false)
85 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
87 Pass *llvm::createFunctionInliningPass(int Threshold) {
88 return new SimpleInliner(Threshold);
91 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
92 unsigned SizeOptLevel) {
93 return new SimpleInliner(
94 computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
97 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
98 ICA = &getAnalysis<InlineCostAnalysis>();
99 return Inliner::runOnSCC(SCC);
102 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
103 AU.addRequired<InlineCostAnalysis>();
104 Inliner::getAnalysisUsage(AU);