Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- InlineSimple.cpp - Code to perform simple function inlining --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements bottom-up inlining of functions into callees. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "inline" |
| 15 | #include "llvm/CallingConv.h" |
| 16 | #include "llvm/Instructions.h" |
| 17 | #include "llvm/IntrinsicInst.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/Analysis/CallGraph.h" |
| 21 | #include "llvm/Support/CallSite.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Transforms/IPO.h" |
| 24 | #include "llvm/Transforms/IPO/InlinerPass.h" |
Devang Patel | 7c2e99c | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/InlineCost.h" |
Devang Patel | d6605a8 | 2007-07-27 18:34:27 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 31 | |
| 32 | class VISIBILITY_HIDDEN SimpleInliner : public Inliner { |
Devang Patel | d6605a8 | 2007-07-27 18:34:27 +0000 | [diff] [blame] | 33 | // Functions that are never inlined |
| 34 | SmallPtrSet<const Function*, 16> NeverInline; |
Devang Patel | 7c2e99c | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 35 | InlineCostAnalyzer CA; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | public: |
| 37 | SimpleInliner() : Inliner(&ID) {} |
Chris Lattner | 758296d | 2008-01-12 06:49:13 +0000 | [diff] [blame] | 38 | SimpleInliner(int Threshold) : Inliner(&ID, Threshold) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 7c2e99c | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 40 | int getInlineCost(CallSite CS) { |
| 41 | return CA.getInlineCost(CS, NeverInline); |
| 42 | } |
Evan Cheng | 9880e57 | 2008-03-24 06:37:48 +0000 | [diff] [blame] | 43 | float getInlineFudgeFactor(CallSite CS) { |
| 44 | return CA.getInlineFudgeFactor(CS); |
| 45 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | virtual bool doInitialization(CallGraph &CG); |
| 47 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 50 | char SimpleInliner::ID = 0; |
| 51 | static RegisterPass<SimpleInliner> |
| 52 | X("inline", "Function Integration/Inlining"); |
| 53 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } |
| 55 | |
Chris Lattner | 758296d | 2008-01-12 06:49:13 +0000 | [diff] [blame] | 56 | Pass *llvm::createFunctionInliningPass(int Threshold) { |
| 57 | return new SimpleInliner(Threshold); |
| 58 | } |
| 59 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 60 | // doInitialization - Initializes the vector of functions that have been |
| 61 | // annotated with the noinline attribute. |
| 62 | bool SimpleInliner::doInitialization(CallGraph &CG) { |
| 63 | |
| 64 | Module &M = CG.getModule(); |
| 65 | |
Devang Patel | 41b0339 | 2008-09-03 18:10:21 +0000 | [diff] [blame] | 66 | for (Module::iterator I = M.begin(), E = M.end(); |
| 67 | I != E; ++I) |
Devang Patel | 008cd3e | 2008-09-26 23:51:19 +0000 | [diff] [blame] | 68 | if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline)) |
Devang Patel | 41b0339 | 2008-09-03 18:10:21 +0000 | [diff] [blame] | 69 | NeverInline.insert(I); |
| 70 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | // Get llvm.noinline |
| 72 | GlobalVariable *GV = M.getNamedGlobal("llvm.noinline"); |
| 73 | |
| 74 | if (GV == 0) |
| 75 | return false; |
| 76 | |
Anton Korobeynikov | 5286e13 | 2007-11-22 22:30:10 +0000 | [diff] [blame] | 77 | // Don't crash on invalid code |
| 78 | if (!GV->hasInitializer()) |
| 79 | return false; |
| 80 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 82 | |
| 83 | if (InitList == 0) |
| 84 | return false; |
| 85 | |
| 86 | // Iterate over each element and add to the NeverInline set |
| 87 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 88 | |
| 89 | // Get Source |
| 90 | const Constant *Elt = InitList->getOperand(i); |
| 91 | |
| 92 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt)) |
| 93 | if (CE->getOpcode() == Instruction::BitCast) |
| 94 | Elt = CE->getOperand(0); |
| 95 | |
| 96 | // Insert into set of functions to never inline |
| 97 | if (const Function *F = dyn_cast<Function>(Elt)) |
| 98 | NeverInline.insert(F); |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
Devang Patel | 7c2e99c | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 103 | |