CallGraphSCC passes implicity require CallGraph analysis.
[oota-llvm.git] / lib / Transforms / IPO / InlineSimple.cpp
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     SimpleInliner(int Threshold) : Inliner(ID, Threshold) {}
38     static char ID; // Pass identification, replacement for typeid
39     InlineCost getInlineCost(CallSite CS) {
40       return CA.getInlineCost(CS, NeverInline);
41     }
42     float getInlineFudgeFactor(CallSite CS) {
43       return CA.getInlineFudgeFactor(CS);
44     }
45     void resetCachedCostInfo(Function *Caller) {
46       CA.resetCachedCostInfo(Caller);
47     }
48     void growCachedCostInfo(Function* Caller, Function* Callee) {
49       CA.growCachedCostInfo(Caller, Callee);
50     }
51     virtual bool doInitialization(CallGraph &CG);
52     void releaseMemory() {
53       CA.clear();
54     }
55   };
56 }
57
58 char SimpleInliner::ID = 0;
59 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
60                 "Function Integration/Inlining", false, false)
61 INITIALIZE_AG_DEPENDENCY(CallGraph)
62 INITIALIZE_PASS_END(SimpleInliner, "inline",
63                 "Function Integration/Inlining", false, false)
64
65 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
66
67 Pass *llvm::createFunctionInliningPass(int Threshold) { 
68   return new SimpleInliner(Threshold);
69 }
70
71 // doInitialization - Initializes the vector of functions that have been
72 // annotated with the noinline attribute.
73 bool SimpleInliner::doInitialization(CallGraph &CG) {
74   
75   Module &M = CG.getModule();
76   
77   for (Module::iterator I = M.begin(), E = M.end();
78        I != E; ++I)
79     if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
80       NeverInline.insert(I);
81
82   // Get llvm.noinline
83   GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
84   
85   if (GV == 0)
86     return false;
87
88   // Don't crash on invalid code
89   if (!GV->hasDefinitiveInitializer())
90     return false;
91   
92   const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
93   
94   if (InitList == 0)
95     return false;
96
97   // Iterate over each element and add to the NeverInline set
98   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
99         
100     // Get Source
101     const Constant *Elt = InitList->getOperand(i);
102         
103     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
104       if (CE->getOpcode() == Instruction::BitCast) 
105         Elt = CE->getOperand(0);
106     
107     // Insert into set of functions to never inline
108     if (const Function *F = dyn_cast<Function>(Elt))
109       NeverInline.insert(F);
110   }
111   
112   return false;
113 }
114