Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 1 | //===- InlineSimple.cpp - Code to perform simple function inlining --------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 10 | // This file implements bottom-up inlining of functions into callees. |
Chris Lattner | 0154505 | 2002-04-18 18:52:03 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Tanya Lattner | 6f7426e | 2007-06-19 22:29:50 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "inline" |
Chris Lattner | 49fbff4 | 2005-05-18 04:30:33 +0000 | [diff] [blame] | 15 | #include "llvm/CallingConv.h" |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 16 | #include "llvm/Instructions.h" |
Chris Lattner | 8db93f1 | 2004-11-22 17:21:44 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CallGraph.h" |
Dan Gohman | e4aeec0 | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InlineCost.h" |
Chris Lattner | e544533 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/IPO.h" |
Tanya Lattner | 6f7426e | 2007-06-19 22:29:50 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/IPO/InlinerPass.h" |
Devang Patel | 29381fb | 2007-07-27 18:34:27 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 29 | namespace { |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 30 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 31 | class SimpleInliner : public Inliner { |
Devang Patel | 29381fb | 2007-07-27 18:34:27 +0000 | [diff] [blame] | 32 | // Functions that are never inlined |
| 33 | SmallPtrSet<const Function*, 16> NeverInline; |
Devang Patel | 6899b31 | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 34 | InlineCostAnalyzer CA; |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 35 | public: |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame^] | 36 | SimpleInliner() : Inliner(ID) { |
| 37 | initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); |
| 38 | } |
| 39 | SimpleInliner(int Threshold) : Inliner(ID, Threshold) { |
| 40 | initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); |
| 41 | } |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 42 | static char ID; // Pass identification, replacement for typeid |
Daniel Dunbar | c5e1ec4 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 43 | InlineCost getInlineCost(CallSite CS) { |
Devang Patel | 6899b31 | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 44 | return CA.getInlineCost(CS, NeverInline); |
| 45 | } |
Evan Cheng | 8d84d5b | 2008-03-24 06:37:48 +0000 | [diff] [blame] | 46 | float getInlineFudgeFactor(CallSite CS) { |
| 47 | return CA.getInlineFudgeFactor(CS); |
| 48 | } |
Dale Johannesen | e345566 | 2009-01-09 01:30:11 +0000 | [diff] [blame] | 49 | void resetCachedCostInfo(Function *Caller) { |
| 50 | CA.resetCachedCostInfo(Caller); |
| 51 | } |
Jakob Stoklund Olesen | f747747 | 2010-03-09 23:02:17 +0000 | [diff] [blame] | 52 | void growCachedCostInfo(Function* Caller, Function* Callee) { |
| 53 | CA.growCachedCostInfo(Caller, Callee); |
| 54 | } |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 55 | virtual bool doInitialization(CallGraph &CG); |
Nick Lewycky | 9a1581b | 2010-05-12 21:48:15 +0000 | [diff] [blame] | 56 | void releaseMemory() { |
| 57 | CA.clear(); |
| 58 | } |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 59 | }; |
| 60 | } |
| 61 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 62 | char SimpleInliner::ID = 0; |
Owen Anderson | ae0a7bc | 2010-10-13 22:00:45 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", |
| 64 | "Function Integration/Inlining", false, false) |
| 65 | INITIALIZE_AG_DEPENDENCY(CallGraph) |
| 66 | INITIALIZE_PASS_END(SimpleInliner, "inline", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 67 | "Function Integration/Inlining", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 68 | |
Devang Patel | c71ca3c | 2007-01-26 00:47:38 +0000 | [diff] [blame] | 69 | Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 120d053 | 2008-01-12 06:49:13 +0000 | [diff] [blame] | 71 | Pass *llvm::createFunctionInliningPass(int Threshold) { |
| 72 | return new SimpleInliner(Threshold); |
| 73 | } |
| 74 | |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 75 | // doInitialization - Initializes the vector of functions that have been |
| 76 | // annotated with the noinline attribute. |
| 77 | bool SimpleInliner::doInitialization(CallGraph &CG) { |
| 78 | |
| 79 | Module &M = CG.getModule(); |
| 80 | |
Devang Patel | 910c120 | 2008-09-03 18:10:21 +0000 | [diff] [blame] | 81 | for (Module::iterator I = M.begin(), E = M.end(); |
| 82 | I != E; ++I) |
Devang Patel | 2c9c3e7 | 2008-09-26 23:51:19 +0000 | [diff] [blame] | 83 | if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline)) |
Devang Patel | 910c120 | 2008-09-03 18:10:21 +0000 | [diff] [blame] | 84 | NeverInline.insert(I); |
| 85 | |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 86 | // Get llvm.noinline |
| 87 | GlobalVariable *GV = M.getNamedGlobal("llvm.noinline"); |
| 88 | |
Tanya Lattner | b62fa8a | 2007-06-07 17:12:16 +0000 | [diff] [blame] | 89 | if (GV == 0) |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 90 | return false; |
| 91 | |
Anton Korobeynikov | 311c4b6 | 2007-11-22 22:30:10 +0000 | [diff] [blame] | 92 | // Don't crash on invalid code |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 93 | if (!GV->hasDefinitiveInitializer()) |
Anton Korobeynikov | 311c4b6 | 2007-11-22 22:30:10 +0000 | [diff] [blame] | 94 | return false; |
| 95 | |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 96 | const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 97 | |
Tanya Lattner | b62fa8a | 2007-06-07 17:12:16 +0000 | [diff] [blame] | 98 | if (InitList == 0) |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 99 | 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 Lattner | b62fa8a | 2007-06-07 17:12:16 +0000 | [diff] [blame] | 112 | if (const Function *F = dyn_cast<Function>(Elt)) |
| 113 | NeverInline.insert(F); |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return false; |
| 117 | } |
Devang Patel | 6899b31 | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 118 | |