DEBUG got moved to Support/Debug.h
[oota-llvm.git] / lib / Transforms / IPO / InlineSimple.cpp
1 //===- FunctionInlining.cpp - Code to perform function inlining -----------===//
2 //
3 // This file implements bottom-up inlining of functions into callees.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Transforms/IPO.h"
8 #include "llvm/Transforms/Utils/Cloning.h"
9 #include "llvm/Module.h"
10 #include "llvm/Pass.h"
11 #include "llvm/iOther.h"
12 #include "llvm/iMemory.h"
13 #include "Support/CommandLine.h"
14 #include "Support/Debug.h"
15 #include "Support/Statistic.h"
16 #include <set>
17
18 namespace {
19   Statistic<> NumInlined("inline", "Number of functions inlined");
20   cl::opt<unsigned>             // FIXME: 200 is VERY conservative
21   InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
22               cl::desc("Control the amount of inlining to perform (default = 200)"));
23
24   struct FunctionInlining : public Pass {
25     virtual bool run(Module &M) {
26       bool Changed = false;
27       for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
28         Changed |= doInlining(I);
29       ProcessedFunctions.clear();
30       return Changed;
31     }
32
33   private:
34     std::set<Function*> ProcessedFunctions;  // Prevent infinite recursion
35     bool doInlining(Function *F);
36   };
37   RegisterOpt<FunctionInlining> X("inline", "Function Integration/Inlining");
38 }
39
40 Pass *createFunctionInliningPass() { return new FunctionInlining(); }
41
42
43 // ShouldInlineFunction - The heuristic used to determine if we should inline
44 // the function call or not.
45 //
46 static inline bool ShouldInlineFunction(const CallInst *CI) {
47   assert(CI->getParent() && CI->getParent()->getParent() && 
48          "Call not embedded into a function!");
49
50   const Function *Callee = CI->getCalledFunction();
51   if (Callee == 0 || Callee->isExternal())
52     return false;  // Cannot inline an indirect call... or external function.
53
54   // Don't inline a recursive call.
55   const Function *Caller = CI->getParent()->getParent();
56   if (Caller == Callee) return false;
57
58   // InlineQuality - This value measures how good of an inline candidate this
59   // call site is to inline.  The initial value determines how aggressive the
60   // inliner is.  If this value is negative after the final computation,
61   // inlining is not performed.
62   //
63   int InlineQuality = InlineLimit;
64
65   // If there is only one call of the function, and it has internal linkage,
66   // make it almost guaranteed to be inlined.
67   //
68   if (Callee->use_size() == 1 && Callee->hasInternalLinkage())
69     InlineQuality += 30000;
70
71   // Add to the inline quality for properties that make the call valueable to
72   // inline.  This includes factors that indicate that the result of inlining
73   // the function will be optimizable.  Currently this just looks at arguments
74   // passed into the function.
75   //
76   for (User::const_op_iterator I = CI->op_begin()+1, E = CI->op_end();
77        I != E; ++I){
78     // Each argument passed in has a cost at both the caller and the callee
79     // sides.  This favors functions that take many arguments over functions
80     // that take few arguments.
81     InlineQuality += 20;
82
83     // If this is a function being passed in, it is very likely that we will be
84     // able to turn an indirect function call into a direct function call.
85     if (isa<Function>(I))
86       InlineQuality += 100;
87
88     // If a constant, global variable or alloca is passed in, inlining this
89     // function is likely to allow significant future optimization possibilities
90     // (constant propagation, scalar promotion, and scalarization), so encourage
91     // the inlining of the function.
92     //
93     else if (isa<Constant>(I) || isa<GlobalVariable>(I) || isa<AllocaInst>(I))
94       InlineQuality += 60;
95   }
96
97   // Now that we have considered all of the factors that make the call site more
98   // likely to be inlined, look at factors that make us not want to inline it.
99   // As soon as the inline quality gets negative, bail out.
100
101   // Look at the size of the callee.  Each basic block counts as 20 units, and
102   // each instruction counts as 10.
103   for (Function::const_iterator BB = Callee->begin(), E = Callee->end();
104        BB != E; ++BB) {
105     InlineQuality -= BB->size()*10 + 20;
106     if (InlineQuality < 0) return false;
107   }
108
109   // Don't inline into something too big, which would make it bigger.  Here, we
110   // count each basic block as a single unit.
111   for (Function::const_iterator BB = Caller->begin(), E = Caller->end();
112        BB != E; ++BB) {
113     --InlineQuality;
114     if (InlineQuality < 0) return false;
115   }
116
117   // If we get here, this call site is high enough "quality" to inline.
118   DEBUG(std::cerr << "Inlining in '" << Caller->getName()
119                   << "', quality = " << InlineQuality << ": " << *CI);
120   return true;
121 }
122
123
124 // doInlining - Use a heuristic based approach to inline functions that seem to
125 // look good.
126 //
127 bool FunctionInlining::doInlining(Function *F) {
128   // If we have already processed this function (ie, it is recursive) don't
129   // revisit.
130   std::set<Function*>::iterator PFI = ProcessedFunctions.lower_bound(F);
131   if (PFI != ProcessedFunctions.end() && *PFI == F) return false;
132
133   // Insert the function in the set so it doesn't get revisited.
134   ProcessedFunctions.insert(PFI, F);
135
136   bool Changed = false;
137   for (Function::iterator BB = F->begin(); BB != F->end(); ++BB)
138     for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
139       bool ShouldInc = true;
140       // Found a call instruction? FIXME: This should also handle INVOKEs
141       if (CallInst *CI = dyn_cast<CallInst>(I)) {
142         if (Function *Callee = CI->getCalledFunction())
143           doInlining(Callee);  // Inline in callees before callers!
144
145         // Decide whether we should inline this function...
146         if (ShouldInlineFunction(CI)) {
147           // Save an iterator to the instruction before the call if it exists,
148           // otherwise get an iterator at the end of the block... because the
149           // call will be destroyed.
150           //
151           BasicBlock::iterator SI;
152           if (I != BB->begin()) {
153             SI = I; --SI;           // Instruction before the call...
154           } else {
155             SI = BB->end();
156           }
157
158           // Attempt to inline the function...
159           if (InlineFunction(CI)) {
160             ++NumInlined;
161             Changed = true;
162             // Move to instruction before the call...
163             I = (SI == BB->end()) ? BB->begin() : SI;
164             ShouldInc = false;  // Don't increment iterator until next time
165           }
166         }
167       }
168       if (ShouldInc) ++I;
169     }
170
171   return Changed;
172 }
173