blob: a7aeed8afc3b08482ff1eb8160f0015c2b20f2c6 [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"
Chris Lattner49fbff42005-05-18 04:30:33 +000015#include "llvm/CallingConv.h"
Chris Lattner869adc22003-11-21 21:46:09 +000016#include "llvm/Instructions.h"
Chris Lattner8db93f12004-11-22 17:21:44 +000017#include "llvm/IntrinsicInst.h"
Tanya Lattner682f6832007-06-06 21:59:26 +000018#include "llvm/Module.h"
Chris Lattner619d3542004-03-13 23:15:45 +000019#include "llvm/Type.h"
Tanya Lattner682f6832007-06-06 21:59:26 +000020#include "llvm/Analysis/CallGraph.h"
Dan Gohmane4aeec02009-10-13 18:30:07 +000021#include "llvm/Analysis/InlineCost.h"
Chris Lattnere5445332003-08-24 06:59:28 +000022#include "llvm/Support/CallSite.h"
Chris Lattner237ef562003-08-31 19:10:30 +000023#include "llvm/Transforms/IPO.h"
Tanya Lattner6f7426e2007-06-19 22:29:50 +000024#include "llvm/Transforms/IPO/InlinerPass.h"
Andrew Trickb2ab2fa2011-10-01 01:39:05 +000025#include "llvm/Target/TargetData.h"
Devang Patel29381fb2007-07-27 18:34:27 +000026#include "llvm/ADT/SmallPtrSet.h"
Tanya Lattner682f6832007-06-06 21:59:26 +000027
Chris Lattner869adc22003-11-21 21:46:09 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattnerbd0ef772002-02-26 21:46:54 +000030namespace {
Chris Lattner884d6c42003-10-06 15:52:43 +000031
Nick Lewycky6726b6d2009-10-25 06:33:48 +000032 class SimpleInliner : public Inliner {
Devang Patel29381fb2007-07-27 18:34:27 +000033 // Functions that are never inlined
Andrew Trick5c655412011-10-01 01:27:56 +000034 SmallPtrSet<const Function*, 16> NeverInline;
Devang Patel6899b312007-07-25 18:00:25 +000035 InlineCostAnalyzer CA;
Chris Lattner884d6c42003-10-06 15:52:43 +000036 public:
Owen Anderson081c34b2010-10-19 17:21:58 +000037 SimpleInliner() : Inliner(ID) {
38 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
39 }
Chad Rosierdfba3ad2012-02-25 03:07:57 +000040 SimpleInliner(int Threshold) : Inliner(ID, Threshold,
41 /*InsertLifetime*/true) {
Owen Anderson081c34b2010-10-19 17:21:58 +000042 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
43 }
Nick Lewyckyecd94c82007-05-06 13:37:16 +000044 static char ID; // Pass identification, replacement for typeid
Daniel Dunbarc5e1ec42008-10-30 19:26:59 +000045 InlineCost getInlineCost(CallSite CS) {
Chandler Carruthf91f5af2012-03-16 06:10:13 +000046 // Filter out functions which should never be inlined due to the global
47 // 'llvm.noinline'.
48 // FIXME: I'm 99% certain that this is an ancient bit of legacy that we
49 // no longer need to support, but I don't want to blindly nuke it just
50 // yet.
51 if (Function *Callee = CS.getCalledFunction())
52 if (NeverInline.count(Callee))
53 return InlineCost::getNever();
54
55 return CA.getInlineCost(CS);
Devang Patel6899b312007-07-25 18:00:25 +000056 }
Evan Cheng8d84d5b2008-03-24 06:37:48 +000057 float getInlineFudgeFactor(CallSite CS) {
58 return CA.getInlineFudgeFactor(CS);
59 }
Dale Johannesene3455662009-01-09 01:30:11 +000060 void resetCachedCostInfo(Function *Caller) {
61 CA.resetCachedCostInfo(Caller);
62 }
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +000063 void growCachedCostInfo(Function* Caller, Function* Callee) {
64 CA.growCachedCostInfo(Caller, Callee);
65 }
Tanya Lattner682f6832007-06-06 21:59:26 +000066 virtual bool doInitialization(CallGraph &CG);
Nick Lewycky9a1581b2010-05-12 21:48:15 +000067 void releaseMemory() {
68 CA.clear();
69 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070 };
71}
72
Dan Gohman844731a2008-05-13 00:00:25 +000073char SimpleInliner::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +000074INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
75 "Function Integration/Inlining", false, false)
76INITIALIZE_AG_DEPENDENCY(CallGraph)
77INITIALIZE_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
Tanya Lattner682f6832007-06-06 21:59:26 +000086// doInitialization - Initializes the vector of functions that have been
87// annotated with the noinline attribute.
88bool SimpleInliner::doInitialization(CallGraph &CG) {
Andrew Trickb2ab2fa2011-10-01 01:39:05 +000089 CA.setTargetData(getAnalysisIfAvailable<TargetData>());
Andrew Trick5c655412011-10-01 01:27:56 +000090
Tanya Lattner682f6832007-06-06 21:59:26 +000091 Module &M = CG.getModule();
Andrew Trick5c655412011-10-01 01:27:56 +000092
Tanya Lattner682f6832007-06-06 21:59:26 +000093 // Get llvm.noinline
94 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
Andrew Trick5c655412011-10-01 01:27:56 +000095
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +000096 if (GV == 0)
Tanya Lattner682f6832007-06-06 21:59:26 +000097 return false;
98
Anton Korobeynikov311c4b62007-11-22 22:30:10 +000099 // Don't crash on invalid code
Dan Gohman82555732009-08-19 18:20:44 +0000100 if (!GV->hasDefinitiveInitializer())
Anton Korobeynikov311c4b62007-11-22 22:30:10 +0000101 return false;
Andrew Trick5c655412011-10-01 01:27:56 +0000102
Tanya Lattner682f6832007-06-06 21:59:26 +0000103 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
Andrew Trick5c655412011-10-01 01:27:56 +0000104
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +0000105 if (InitList == 0)
Tanya Lattner682f6832007-06-06 21:59:26 +0000106 return false;
107
108 // Iterate over each element and add to the NeverInline set
109 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Andrew Trick5c655412011-10-01 01:27:56 +0000110
Tanya Lattner682f6832007-06-06 21:59:26 +0000111 // Get Source
112 const Constant *Elt = InitList->getOperand(i);
Andrew Trick5c655412011-10-01 01:27:56 +0000113
Tanya Lattner682f6832007-06-06 21:59:26 +0000114 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
Andrew Trick5c655412011-10-01 01:27:56 +0000115 if (CE->getOpcode() == Instruction::BitCast)
Tanya Lattner682f6832007-06-06 21:59:26 +0000116 Elt = CE->getOperand(0);
Andrew Trick5c655412011-10-01 01:27:56 +0000117
Tanya Lattner682f6832007-06-06 21:59:26 +0000118 // Insert into set of functions to never inline
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +0000119 if (const Function *F = dyn_cast<Function>(Elt))
120 NeverInline.insert(F);
Tanya Lattner682f6832007-06-06 21:59:26 +0000121 }
Andrew Trick5c655412011-10-01 01:27:56 +0000122
Tanya Lattner682f6832007-06-06 21:59:26 +0000123 return false;
124}
Devang Patel6899b312007-07-25 18:00:25 +0000125