blob: 0c5b3be8f9832e6be8006caee6836d120c41a301 [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"
Chris Lattner05deb042005-05-18 04:30:33 +000015#include "llvm/CallingConv.h"
Chris Lattner51c28a52003-11-21 21:46:09 +000016#include "llvm/Instructions.h"
Chris Lattner79e87e32004-11-22 17:21:44 +000017#include "llvm/IntrinsicInst.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000018#include "llvm/Module.h"
Chris Lattner2dc85b22004-03-13 23:15:45 +000019#include "llvm/Type.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000020#include "llvm/Analysis/CallGraph.h"
Dan Gohman4552e3c2009-10-13 18:30:07 +000021#include "llvm/Analysis/InlineCost.h"
Chris Lattnerd367d052003-08-24 06:59:28 +000022#include "llvm/Support/CallSite.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000023#include "llvm/Transforms/IPO.h"
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000024#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Patele3206cb2007-07-27 18:34:27 +000025#include "llvm/ADT/SmallPtrSet.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
Nick Lewycky02d5f772009-10-25 06:33:48 +000031 class SimpleInliner : public Inliner {
Devang Patele3206cb2007-07-27 18:34:27 +000032 // Functions that are never inlined
33 SmallPtrSet<const Function*, 16> NeverInline;
Devang Patel33227112007-07-25 18:00:25 +000034 InlineCostAnalyzer CA;
Chris Lattner6dc0ae22003-10-06 15:52:43 +000035 public:
Owen Anderson6c18d1a2010-10-19 17:21:58 +000036 SimpleInliner() : Inliner(ID) {
37 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
38 }
39 SimpleInliner(int Threshold) : Inliner(ID, Threshold) {
40 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
41 }
Nick Lewyckye7da2d62007-05-06 13:37:16 +000042 static char ID; // Pass identification, replacement for typeid
Daniel Dunbar3933e662008-10-30 19:26:59 +000043 InlineCost getInlineCost(CallSite CS) {
Devang Patel33227112007-07-25 18:00:25 +000044 return CA.getInlineCost(CS, NeverInline);
45 }
Evan Cheng3471ae82008-03-24 06:37:48 +000046 float getInlineFudgeFactor(CallSite CS) {
47 return CA.getInlineFudgeFactor(CS);
48 }
Dale Johannesen4755d9d2009-01-09 01:30:11 +000049 void resetCachedCostInfo(Function *Caller) {
50 CA.resetCachedCostInfo(Caller);
51 }
Jakob Stoklund Olesenb495cad2010-03-09 23:02:17 +000052 void growCachedCostInfo(Function* Caller, Function* Callee) {
53 CA.growCachedCostInfo(Caller, Callee);
54 }
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000055 virtual bool doInitialization(CallGraph &CG);
Nick Lewyckyc63aa1e2010-05-12 21:48:15 +000056 void releaseMemory() {
57 CA.clear();
58 }
Chris Lattner04805fa2002-02-26 21:46:54 +000059 };
60}
61
Dan Gohmand78c4002008-05-13 00:00:25 +000062char SimpleInliner::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000063INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
64 "Function Integration/Inlining", false, false)
65INITIALIZE_AG_DEPENDENCY(CallGraph)
66INITIALIZE_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
Chris Lattner22ad7ab2008-01-12 06:49:13 +000071Pass *llvm::createFunctionInliningPass(int Threshold) {
72 return new SimpleInliner(Threshold);
73}
74
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000075// doInitialization - Initializes the vector of functions that have been
76// annotated with the noinline attribute.
77bool SimpleInliner::doInitialization(CallGraph &CG) {
78
79 Module &M = CG.getModule();
80
Devang Patel62be9ad2008-09-03 18:10:21 +000081 for (Module::iterator I = M.begin(), E = M.end();
82 I != E; ++I)
Devang Patel9eb525d2008-09-26 23:51:19 +000083 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
Devang Patel62be9ad2008-09-03 18:10:21 +000084 NeverInline.insert(I);
85
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000086 // Get llvm.noinline
87 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
88
Tanya Lattner5801c232007-06-07 17:12:16 +000089 if (GV == 0)
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000090 return false;
91
Anton Korobeynikov59c11682007-11-22 22:30:10 +000092 // Don't crash on invalid code
Dan Gohman5d5bc6d2009-08-19 18:20:44 +000093 if (!GV->hasDefinitiveInitializer())
Anton Korobeynikov59c11682007-11-22 22:30:10 +000094 return false;
95
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000096 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
97
Tanya Lattner5801c232007-06-07 17:12:16 +000098 if (InitList == 0)
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000099 return false;
100
101 // Iterate over each element and add to the NeverInline set
102 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
103
104 // Get Source
105 const Constant *Elt = InitList->getOperand(i);
106
107 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
108 if (CE->getOpcode() == Instruction::BitCast)
109 Elt = CE->getOperand(0);
110
111 // Insert into set of functions to never inline
Tanya Lattner5801c232007-06-07 17:12:16 +0000112 if (const Function *F = dyn_cast<Function>(Elt))
113 NeverInline.insert(F);
Tanya Lattnercb90f1d2007-06-06 21:59:26 +0000114 }
115
116 return false;
117}
Devang Patel33227112007-07-25 18:00:25 +0000118