1 //===- InlineSimple.cpp - Code to perform simple function inlining --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 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/Analysis/InlineCost.h" 22 #include "llvm/Support/CallSite.h" 23 #include "llvm/Transforms/IPO.h" 24 #include "llvm/Transforms/IPO/InlinerPass.h" 25 #include "llvm/ADT/SmallPtrSet.h" 26 27 using namespace llvm; 28 29 namespace { 30 31 class SimpleInliner : public Inliner { 32 // Functions that are never inlined 33 SmallPtrSet<const Function*, 16> NeverInline; 34 InlineCostAnalyzer CA; 35 public: 36 SimpleInliner() : Inliner(ID) { 37 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); 38 } 39 SimpleInliner(int Threshold) : Inliner(ID, Threshold) { 40 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); 41 } 42 static char ID; // Pass identification, replacement for typeid 43 InlineCost getInlineCost(CallSite CS) { 44 return CA.getInlineCost(CS, NeverInline); 45 } 46 float getInlineFudgeFactor(CallSite CS) { 47 return CA.getInlineFudgeFactor(CS); 48 } 49 void resetCachedCostInfo(Function *Caller) { 50 CA.resetCachedCostInfo(Caller); 51 } 52 void growCachedCostInfo(Function* Caller, Function* Callee) { 53 CA.growCachedCostInfo(Caller, Callee); 54 } 55 virtual bool doInitialization(CallGraph &CG); 56 void releaseMemory() { 57 CA.clear(); 58 } 59 }; 60 } 61 62 char SimpleInliner::ID = 0; 63 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", 64 "Function Integration/Inlining", false, false) 65 INITIALIZE_AG_DEPENDENCY(CallGraph) 66 INITIALIZE_PASS_END(SimpleInliner, "inline", 67 "Function Integration/Inlining", false, false) 68 69 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } 70 71 Pass *llvm::createFunctionInliningPass(int Threshold) { 72 return new SimpleInliner(Threshold); 73 } 74 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 81 for (Module::iterator I = M.begin(), E = M.end(); 82 I != E; ++I) 83 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline)) 84 NeverInline.insert(I); 85 86 // Get llvm.noinline 87 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline"); 88 89 if (GV == 0) 90 return false; 91 92 // Don't crash on invalid code 93 if (!GV->hasDefinitiveInitializer()) 94 return false; 95 96 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 97 98 if (InitList == 0) 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 112 if (const Function *F = dyn_cast<Function>(Elt)) 113 NeverInline.insert(F); 114 } 115 116 return false; 117 } 118 119