Merge branch 'master' into msp430
[llvm/msp430.git] / lib / Transforms / IPO / InlineSimple.cpp
blobe107a0023ce6ccad28bfefa11e465d164c094499
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.
12 //===----------------------------------------------------------------------===//
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"
25 #include "llvm/Transforms/Utils/InlineCost.h"
26 #include "llvm/ADT/SmallPtrSet.h"
28 using namespace llvm;
30 namespace {
32 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
33 // Functions that are never inlined
34 SmallPtrSet<const Function*, 16> NeverInline;
35 InlineCostAnalyzer CA;
36 public:
37 SimpleInliner() : Inliner(&ID) {}
38 SimpleInliner(int Threshold) : Inliner(&ID, Threshold) {}
39 static char ID; // Pass identification, replacement for typeid
40 InlineCost getInlineCost(CallSite CS) {
41 return CA.getInlineCost(CS, NeverInline);
43 float getInlineFudgeFactor(CallSite CS) {
44 return CA.getInlineFudgeFactor(CS);
46 void resetCachedCostInfo(Function *Caller) {
47 CA.resetCachedCostInfo(Caller);
49 virtual bool doInitialization(CallGraph &CG);
53 char SimpleInliner::ID = 0;
54 static RegisterPass<SimpleInliner>
55 X("inline", "Function Integration/Inlining");
57 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
59 Pass *llvm::createFunctionInliningPass(int Threshold) {
60 return new SimpleInliner(Threshold);
63 // doInitialization - Initializes the vector of functions that have been
64 // annotated with the noinline attribute.
65 bool SimpleInliner::doInitialization(CallGraph &CG) {
67 Module &M = CG.getModule();
69 for (Module::iterator I = M.begin(), E = M.end();
70 I != E; ++I)
71 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
72 NeverInline.insert(I);
74 // Get llvm.noinline
75 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
77 if (GV == 0)
78 return false;
80 // Don't crash on invalid code
81 if (!GV->hasInitializer())
82 return false;
84 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
86 if (InitList == 0)
87 return false;
89 // Iterate over each element and add to the NeverInline set
90 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
92 // Get Source
93 const Constant *Elt = InitList->getOperand(i);
95 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
96 if (CE->getOpcode() == Instruction::BitCast)
97 Elt = CE->getOperand(0);
99 // Insert into set of functions to never inline
100 if (const Function *F = dyn_cast<Function>(Elt))
101 NeverInline.insert(F);
104 return false;