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