Start removing the use of an ad-hoc 'never inline' set and instead
[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/Target/TargetData.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27
28 using namespace llvm;
29
30 namespace {
31
32   class 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       initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
39     }
40     SimpleInliner(int Threshold) : Inliner(ID, Threshold,
41                                            /*InsertLifetime*/true) {
42       initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
43     }
44     static char ID; // Pass identification, replacement for typeid
45     InlineCost getInlineCost(CallSite CS) {
46       // Filter out functions which should never be inlined due to the global
47       // 'llvm.noinline'.
48       // FIXME: I'm 99% certain that this is an ancient bit of legacy that we
49       // no longer need to support, but I don't want to blindly nuke it just
50       // yet.
51       if (Function *Callee = CS.getCalledFunction())
52         if (NeverInline.count(Callee))
53           return InlineCost::getNever();
54
55       return CA.getInlineCost(CS);
56     }
57     float getInlineFudgeFactor(CallSite CS) {
58       return CA.getInlineFudgeFactor(CS);
59     }
60     void resetCachedCostInfo(Function *Caller) {
61       CA.resetCachedCostInfo(Caller);
62     }
63     void growCachedCostInfo(Function* Caller, Function* Callee) {
64       CA.growCachedCostInfo(Caller, Callee);
65     }
66     virtual bool doInitialization(CallGraph &CG);
67     void releaseMemory() {
68       CA.clear();
69     }
70   };
71 }
72
73 char SimpleInliner::ID = 0;
74 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
75                 "Function Integration/Inlining", false, false)
76 INITIALIZE_AG_DEPENDENCY(CallGraph)
77 INITIALIZE_PASS_END(SimpleInliner, "inline",
78                 "Function Integration/Inlining", false, false)
79
80 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
81
82 Pass *llvm::createFunctionInliningPass(int Threshold) {
83   return new SimpleInliner(Threshold);
84 }
85
86 // doInitialization - Initializes the vector of functions that have been
87 // annotated with the noinline attribute.
88 bool SimpleInliner::doInitialization(CallGraph &CG) {
89   CA.setTargetData(getAnalysisIfAvailable<TargetData>());
90
91   Module &M = CG.getModule();
92
93   // Get llvm.noinline
94   GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
95
96   if (GV == 0)
97     return false;
98
99   // Don't crash on invalid code
100   if (!GV->hasDefinitiveInitializer())
101     return false;
102
103   const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
104
105   if (InitList == 0)
106     return false;
107
108   // Iterate over each element and add to the NeverInline set
109   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
110
111     // Get Source
112     const Constant *Elt = InitList->getOperand(i);
113
114     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
115       if (CE->getOpcode() == Instruction::BitCast)
116         Elt = CE->getOperand(0);
117
118     // Insert into set of functions to never inline
119     if (const Function *F = dyn_cast<Function>(Elt))
120       NeverInline.insert(F);
121   }
122
123   return false;
124 }
125