blob: 7141064b3b0502f1938445db572828b6ef894958 [file] [log] [blame]
Chris Lattner237ef562003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnerca398dc2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner01545052002-04-18 18:52:03 +000011//
Chris Lattner00950542001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Tanya Lattner6f7426e2007-06-19 22:29:50 +000014#define DEBUG_TYPE "inline"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Transforms/IPO.h"
16#include "llvm/Analysis/CallGraph.h"
17#include "llvm/Analysis/InlineCost.h"
Stephen Hines36b56882014-04-23 16:57:46 -070018#include "llvm/IR/CallSite.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#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"
Tanya Lattner6f7426e2007-06-19 22:29:50 +000025#include "llvm/Transforms/IPO/InlinerPass.h"
Tanya Lattner682f6832007-06-06 21:59:26 +000026
Chris Lattner869adc22003-11-21 21:46:09 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029namespace {
Chris Lattner884d6c42003-10-06 15:52:43 +000030
David Majnemer42864072013-11-03 11:09:39 +000031/// \brief Actual inliner pass implementation.
Chandler Carruth5a471272013-01-21 11:39:14 +000032///
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.
36class SimpleInliner : public Inliner {
Chandler Carruth86953b52013-01-21 11:39:18 +000037 InlineCostAnalysis *ICA;
Chandler Carruth5a471272013-01-21 11:39:14 +000038
39public:
Chandler Carruth86953b52013-01-21 11:39:18 +000040 SimpleInliner() : Inliner(ID), ICA(0) {
Chandler Carruth5a471272013-01-21 11:39:14 +000041 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
42 }
43
44 SimpleInliner(int Threshold)
Chandler Carruth86953b52013-01-21 11:39:18 +000045 : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(0) {
Chandler Carruth5a471272013-01-21 11:39:14 +000046 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
47 }
48
49 static char ID; // Pass identification, replacement for typeid
50
Stephen Hines36b56882014-04-23 16:57:46 -070051 InlineCost getInlineCost(CallSite CS) override {
Chandler Carruth86953b52013-01-21 11:39:18 +000052 return ICA->getInlineCost(CS, getInlineThreshold(CS));
Chandler Carruth5a471272013-01-21 11:39:14 +000053 }
54
Stephen Hines36b56882014-04-23 16:57:46 -070055 bool runOnSCC(CallGraphSCC &SCC) override;
56 void getAnalysisUsage(AnalysisUsage &AU) const override;
Chandler Carruth5a471272013-01-21 11:39:14 +000057};
58
Stephen Hines36b56882014-04-23 16:57:46 -070059static int computeThresholdFromOptLevels(unsigned OptLevel,
60 unsigned SizeOptLevel) {
61 if (OptLevel > 2)
62 return 275;
63 if (SizeOptLevel == 1) // -Os
64 return 75;
65 if (SizeOptLevel == 2) // -Oz
66 return 25;
67 return 225;
68}
69
Chandler Carruth5a471272013-01-21 11:39:14 +000070} // end anonymous namespace
Chris Lattnerbd0ef772002-02-26 21:46:54 +000071
Dan Gohman844731a2008-05-13 00:00:25 +000072char SimpleInliner::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +000073INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
74 "Function Integration/Inlining", false, false)
Stephen Hines36b56882014-04-23 16:57:46 -070075INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth86953b52013-01-21 11:39:18 +000076INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
Owen Andersonae0a7bc2010-10-13 22:00:45 +000077INITIALIZE_PASS_END(SimpleInliner, "inline",
Owen Andersonce665bd2010-10-07 22:25:06 +000078 "Function Integration/Inlining", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000079
Devang Patelc71ca3c2007-01-26 00:47:38 +000080Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner869adc22003-11-21 21:46:09 +000081
Andrew Trick5c655412011-10-01 01:27:56 +000082Pass *llvm::createFunctionInliningPass(int Threshold) {
Chris Lattner120d0532008-01-12 06:49:13 +000083 return new SimpleInliner(Threshold);
84}
85
Stephen Hines36b56882014-04-23 16:57:46 -070086Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
87 unsigned SizeOptLevel) {
88 return new SimpleInliner(
89 computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
90}
91
Chandler Carruth86953b52013-01-21 11:39:18 +000092bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
93 ICA = &getAnalysis<InlineCostAnalysis>();
94 return Inliner::runOnSCC(SCC);
Tanya Lattner682f6832007-06-06 21:59:26 +000095}
Devang Patel6899b312007-07-25 18:00:25 +000096
Chandler Carruth86953b52013-01-21 11:39:18 +000097void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.addRequired<InlineCostAnalysis>();
99 Inliner::getAnalysisUsage(AU);
100}