LLVM API Documentation
00001 //===- InlineSimple.cpp - Code to perform simple function inlining --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements bottom-up inlining of functions into callees. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "inline" 00015 #include "llvm/CallingConv.h" 00016 #include "llvm/Instructions.h" 00017 #include "llvm/IntrinsicInst.h" 00018 #include "llvm/Module.h" 00019 #include "llvm/Type.h" 00020 #include "llvm/Analysis/CallGraph.h" 00021 #include "llvm/Support/CallSite.h" 00022 #include "llvm/Support/Compiler.h" 00023 #include "llvm/Transforms/IPO.h" 00024 #include "llvm/Transforms/IPO/InlinerPass.h" 00025 #include "llvm/Transforms/Utils/InlineCost.h" 00026 #include "llvm/ADT/SmallPtrSet.h" 00027 00028 using namespace llvm; 00029 00030 namespace { 00031 00032 class VISIBILITY_HIDDEN SimpleInliner : public Inliner { 00033 // Functions that are never inlined 00034 SmallPtrSet<const Function*, 16> NeverInline; 00035 InlineCostAnalyzer CA; 00036 public: 00037 SimpleInliner() : Inliner(&ID) {} 00038 SimpleInliner(int Threshold) : Inliner(&ID, Threshold) {} 00039 static char ID; // Pass identification, replacement for typeid 00040 InlineCost getInlineCost(CallSite CS) { 00041 return CA.getInlineCost(CS, NeverInline); 00042 } 00043 float getInlineFudgeFactor(CallSite CS) { 00044 return CA.getInlineFudgeFactor(CS); 00045 } 00046 void resetCachedCostInfo(Function *Caller) { 00047 CA.resetCachedCostInfo(Caller); 00048 } 00049 virtual bool doInitialization(CallGraph &CG); 00050 }; 00051 } 00052 00053 char SimpleInliner::ID = 0; 00054 static RegisterPass<SimpleInliner> 00055 X("inline", "Function Integration/Inlining"); 00056 00057 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } 00058 00059 Pass *llvm::createFunctionInliningPass(int Threshold) { 00060 return new SimpleInliner(Threshold); 00061 } 00062 00063 // doInitialization - Initializes the vector of functions that have been 00064 // annotated with the noinline attribute. 00065 bool SimpleInliner::doInitialization(CallGraph &CG) { 00066 00067 Module &M = CG.getModule(); 00068 00069 for (Module::iterator I = M.begin(), E = M.end(); 00070 I != E; ++I) 00071 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline)) 00072 NeverInline.insert(I); 00073 00074 // Get llvm.noinline 00075 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline"); 00076 00077 if (GV == 0) 00078 return false; 00079 00080 // Don't crash on invalid code 00081 if (!GV->hasInitializer()) 00082 return false; 00083 00084 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 00085 00086 if (InitList == 0) 00087 return false; 00088 00089 // Iterate over each element and add to the NeverInline set 00090 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 00091 00092 // Get Source 00093 const Constant *Elt = InitList->getOperand(i); 00094 00095 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt)) 00096 if (CE->getOpcode() == Instruction::BitCast) 00097 Elt = CE->getOperand(0); 00098 00099 // Insert into set of functions to never inline 00100 if (const Function *F = dyn_cast<Function>(Elt)) 00101 NeverInline.insert(F); 00102 } 00103 00104 return false; 00105 } 00106