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