blob: a4f7026041886ba8f28193578bd8a5b49ab06215 [file] [log] [blame]
Chris Lattnerd075cc22003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner530d4bf2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner1c8d3f82002-04-18 18:52:03 +000011//
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000014#define DEBUG_TYPE "inline"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Transforms/IPO.h"
16#include "llvm/Analysis/CallGraph.h"
17#include "llvm/Analysis/InlineCost.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#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"
Chris Lattnerd367d052003-08-24 06:59:28 +000024#include "llvm/Support/CallSite.h"
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000025#include "llvm/Transforms/IPO/InlinerPass.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000026
Chris Lattner51c28a52003-11-21 21:46:09 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner04805fa2002-02-26 21:46:54 +000029namespace {
Chris Lattner6dc0ae22003-10-06 15:52:43 +000030
Chandler Carruth0df3e532013-01-21 11:39:14 +000031/// \brief Actaul inliner pass implementation.
32///
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 Carruth4319e292013-01-21 11:39:18 +000037 InlineCostAnalysis *ICA;
Chandler Carruth0df3e532013-01-21 11:39:14 +000038
39public:
Chandler Carruth4319e292013-01-21 11:39:18 +000040 SimpleInliner() : Inliner(ID), ICA(0) {
Chandler Carruth0df3e532013-01-21 11:39:14 +000041 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
42 }
43
44 SimpleInliner(int Threshold)
Chandler Carruth4319e292013-01-21 11:39:18 +000045 : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(0) {
Chandler Carruth0df3e532013-01-21 11:39:14 +000046 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
47 }
48
49 static char ID; // Pass identification, replacement for typeid
50
51 InlineCost getInlineCost(CallSite CS) {
Chandler Carruth4319e292013-01-21 11:39:18 +000052 return ICA->getInlineCost(CS, getInlineThreshold(CS));
Chandler Carruth0df3e532013-01-21 11:39:14 +000053 }
54
Chandler Carruth4319e292013-01-21 11:39:18 +000055 virtual bool runOnSCC(CallGraphSCC &SCC);
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Chandler Carruth0df3e532013-01-21 11:39:14 +000057};
58
59} // end anonymous namespace
Chris Lattner04805fa2002-02-26 21:46:54 +000060
Dan Gohmand78c4002008-05-13 00:00:25 +000061char SimpleInliner::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000062INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
63 "Function Integration/Inlining", false, false)
64INITIALIZE_AG_DEPENDENCY(CallGraph)
Chandler Carruth4319e292013-01-21 11:39:18 +000065INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
Owen Anderson071cee02010-10-13 22:00:45 +000066INITIALIZE_PASS_END(SimpleInliner, "inline",
Owen Andersondf7a4f22010-10-07 22:25:06 +000067 "Function Integration/Inlining", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000068
Devang Patel13058a52007-01-26 00:47:38 +000069Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner51c28a52003-11-21 21:46:09 +000070
Andrew Trickcaa500b2011-10-01 01:27:56 +000071Pass *llvm::createFunctionInliningPass(int Threshold) {
Chris Lattner22ad7ab2008-01-12 06:49:13 +000072 return new SimpleInliner(Threshold);
73}
74
Chandler Carruth4319e292013-01-21 11:39:18 +000075bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
76 ICA = &getAnalysis<InlineCostAnalysis>();
77 return Inliner::runOnSCC(SCC);
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000078}
Devang Patel33227112007-07-25 18:00:25 +000079
Chandler Carruth4319e292013-01-21 11:39:18 +000080void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
81 AU.addRequired<InlineCostAnalysis>();
82 Inliner::getAnalysisUsage(AU);
83}