Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 1 | //===- InlineSimple.cpp - Code to perform simple function inlining --------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 10 | // This file implements bottom-up inlining of functions into callees. |
Chris Lattner | 1c8d3f8 | 2002-04-18 18:52:03 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 14 | #include "Inliner.h" |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 15 | #include "llvm/Instructions.h" |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 17 | #include "llvm/Type.h" |
Chris Lattner | d367d05 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CallSite.h" |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 22 | namespace { |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 23 | struct ArgInfo { |
| 24 | unsigned ConstantWeight; |
| 25 | unsigned AllocaWeight; |
| 26 | |
| 27 | ArgInfo(unsigned CWeight, unsigned AWeight) |
| 28 | : ConstantWeight(CWeight), AllocaWeight(AWeight) {} |
| 29 | }; |
| 30 | |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 31 | // FunctionInfo - For each function, calculate the size of it in blocks and |
| 32 | // instructions. |
| 33 | struct FunctionInfo { |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame^] | 34 | // HasAllocas - Keep track of whether or not a function contains an alloca |
| 35 | // instruction that is not in the entry block of the function. Inlining |
| 36 | // this call could cause us to blow out the stack, because the stack memory |
| 37 | // would never be released. |
| 38 | // |
| 39 | // FIXME: LLVM needs a way of dealloca'ing memory, which would make this |
| 40 | // irrelevant! |
| 41 | // |
| 42 | bool HasAllocas; |
| 43 | |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 44 | // NumInsts, NumBlocks - Keep track of how large each function is, which is |
| 45 | // used to estimate the code size cost of inlining it. |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 46 | unsigned NumInsts, NumBlocks; |
| 47 | |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 48 | // ArgumentWeights - Each formal argument of the function is inspected to |
| 49 | // see if it is used in any contexts where making it a constant or alloca |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 50 | // would reduce the code size. If so, we add some value to the argument |
| 51 | // entry here. |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 52 | std::vector<ArgInfo> ArgumentWeights; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 53 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame^] | 54 | FunctionInfo() : HasAllocas(false), NumInsts(0), NumBlocks(0) {} |
| 55 | |
| 56 | /// analyzeFunction - Fill in the current structure with information gleaned |
| 57 | /// from the specified function. |
| 58 | void analyzeFunction(Function *F); |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class SimpleInliner : public Inliner { |
| 62 | std::map<const Function*, FunctionInfo> CachedFunctionInfo; |
| 63 | public: |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 64 | int getInlineCost(CallSite CS); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 65 | }; |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 66 | RegisterOpt<SimpleInliner> X("inline", "Function Integration/Inlining"); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 69 | Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } |
| 70 | |
| 71 | // CountCodeReductionForConstant - Figure out an approximation for how many |
| 72 | // instructions will be constant folded if the specified value is constant. |
| 73 | // |
| 74 | static unsigned CountCodeReductionForConstant(Value *V) { |
| 75 | unsigned Reduction = 0; |
| 76 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 77 | if (isa<BranchInst>(*UI)) |
| 78 | Reduction += 40; // Eliminating a conditional branch is a big win |
| 79 | else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI)) |
| 80 | // Eliminating a switch is a big win, proportional to the number of edges |
| 81 | // deleted. |
| 82 | Reduction += (SI->getNumSuccessors()-1) * 40; |
| 83 | else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 84 | // Turning an indirect call into a direct call is a BIG win |
| 85 | Reduction += CI->getCalledValue() == V ? 500 : 0; |
| 86 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
| 87 | // Turning an indirect call into a direct call is a BIG win |
Chris Lattner | 61b3f20 | 2003-11-21 21:57:29 +0000 | [diff] [blame] | 88 | Reduction += II->getCalledValue() == V ? 500 : 0; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 89 | } else { |
| 90 | // Figure out if this instruction will be removed due to simple constant |
| 91 | // propagation. |
| 92 | Instruction &Inst = cast<Instruction>(**UI); |
| 93 | bool AllOperandsConstant = true; |
| 94 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) |
Reid Spencer | ef784f0 | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 95 | if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) { |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 96 | AllOperandsConstant = false; |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | if (AllOperandsConstant) { |
| 101 | // We will get to remove this instruction... |
| 102 | Reduction += 7; |
| 103 | |
| 104 | // And any other instructions that use it which become constants |
| 105 | // themselves. |
| 106 | Reduction += CountCodeReductionForConstant(&Inst); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return Reduction; |
| 111 | } |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 113 | // CountCodeReductionForAlloca - Figure out an approximation of how much smaller |
| 114 | // the function will be if it is inlined into a context where an argument |
| 115 | // becomes an alloca. |
| 116 | // |
| 117 | static unsigned CountCodeReductionForAlloca(Value *V) { |
| 118 | if (!isa<PointerType>(V->getType())) return 0; // Not a pointer |
| 119 | unsigned Reduction = 0; |
| 120 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ |
| 121 | Instruction *I = cast<Instruction>(*UI); |
| 122 | if (isa<LoadInst>(I) || isa<StoreInst>(I)) |
| 123 | Reduction += 10; |
| 124 | else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 125 | // If the GEP has variable indices, we won't be able to do much with it. |
| 126 | for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end(); |
| 127 | I != E; ++I) |
| 128 | if (!isa<Constant>(*I)) return 0; |
| 129 | Reduction += CountCodeReductionForAlloca(GEP)+15; |
| 130 | } else { |
| 131 | // If there is some other strange instruction, we're not going to be able |
| 132 | // to do much if we inline this. |
| 133 | return 0; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return Reduction; |
| 138 | } |
| 139 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame^] | 140 | /// analyzeFunction - Fill in the current structure with information gleaned |
| 141 | /// from the specified function. |
| 142 | void FunctionInfo::analyzeFunction(Function *F) { |
| 143 | unsigned NumInsts = 0, NumBlocks = 0; |
| 144 | |
| 145 | // Look at the size of the callee. Each basic block counts as 20 units, and |
| 146 | // each instruction counts as 10. |
| 147 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 148 | for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); |
| 149 | II != E; ++II) { |
| 150 | ++NumInsts; |
| 151 | |
| 152 | // If there is an alloca in the body of the function, we cannot currently |
| 153 | // inline the function without the risk of exploding the stack. |
| 154 | if (isa<AllocaInst>(II) && BB != F->begin()) { |
| 155 | HasAllocas = true; |
| 156 | this->NumBlocks = this->NumInsts = 1; |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | ++NumBlocks; |
| 162 | } |
| 163 | |
| 164 | this->NumBlocks = NumBlocks; |
| 165 | this->NumInsts = NumInsts; |
| 166 | |
| 167 | // Check out all of the arguments to the function, figuring out how much |
| 168 | // code can be eliminated if one of the arguments is a constant. |
| 169 | for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I) |
| 170 | ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I), |
| 171 | CountCodeReductionForAlloca(I))); |
| 172 | } |
| 173 | |
| 174 | |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 175 | // getInlineCost - The heuristic used to determine if we should inline the |
| 176 | // function call or not. |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 177 | // |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 178 | int SimpleInliner::getInlineCost(CallSite CS) { |
Chris Lattner | d367d05 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 179 | Instruction *TheCall = CS.getInstruction(); |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 180 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | d367d05 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 181 | const Function *Caller = TheCall->getParent()->getParent(); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 182 | |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 183 | // Don't inline a directly recursive call. |
| 184 | if (Caller == Callee) return 2000000000; |
| 185 | |
| 186 | // InlineCost - This value measures how good of an inline candidate this call |
| 187 | // site is to inline. A lower inline cost make is more likely for the call to |
| 188 | // be inlined. This value may go negative. |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 189 | // |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 190 | int InlineCost = 0; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 191 | |
| 192 | // If there is only one call of the function, and it has internal linkage, |
| 193 | // make it almost guaranteed to be inlined. |
| 194 | // |
Chris Lattner | 2d9c117 | 2003-10-20 05:54:26 +0000 | [diff] [blame] | 195 | if (Callee->hasInternalLinkage() && Callee->hasOneUse()) |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 196 | InlineCost -= 30000; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 198 | // Get information about the callee... |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 199 | FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 200 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame^] | 201 | // If we haven't calculated this information yet, do so now. |
| 202 | if (CalleeFI.NumBlocks == 0) |
| 203 | CalleeFI.analyzeFunction(Callee); |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 204 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame^] | 205 | // Don't inline calls to functions with allocas that are not in the entry |
| 206 | // block of the function. |
| 207 | if (CalleeFI.HasAllocas) |
| 208 | return 2000000000; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 209 | |
| 210 | // Add to the inline quality for properties that make the call valuable to |
| 211 | // inline. This includes factors that indicate that the result of inlining |
| 212 | // the function will be optimizable. Currently this just looks at arguments |
| 213 | // passed into the function. |
| 214 | // |
| 215 | unsigned ArgNo = 0; |
| 216 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 217 | I != E; ++I, ++ArgNo) { |
| 218 | // Each argument passed in has a cost at both the caller and the callee |
| 219 | // sides. This favors functions that take many arguments over functions |
| 220 | // that take few arguments. |
| 221 | InlineCost -= 20; |
| 222 | |
| 223 | // If this is a function being passed in, it is very likely that we will be |
| 224 | // able to turn an indirect function call into a direct function call. |
| 225 | if (isa<Function>(I)) |
| 226 | InlineCost -= 100; |
| 227 | |
| 228 | // If an alloca is passed in, inlining this function is likely to allow |
| 229 | // significant future optimization possibilities (like scalar promotion, and |
| 230 | // scalarization), so encourage the inlining of the function. |
| 231 | // |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 232 | else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 233 | if (ArgNo < CalleeFI.ArgumentWeights.size()) |
| 234 | InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 235 | |
| 236 | // If this is a constant being passed into the function, use the argument |
| 237 | // weights calculated for the callee to determine how much will be folded |
| 238 | // away with this information. |
Reid Spencer | ef784f0 | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 239 | } else if (isa<Constant>(I)) { |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 240 | if (ArgNo < CalleeFI.ArgumentWeights.size()) |
| 241 | InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | // Now that we have considered all of the factors that make the call site more |
| 246 | // likely to be inlined, look at factors that make us not want to inline it. |
| 247 | |
Chris Lattner | f849253 | 2003-10-07 19:33:31 +0000 | [diff] [blame] | 248 | // Don't inline into something too big, which would make it bigger. Here, we |
| 249 | // count each basic block as a single unit. |
Chris Lattner | 95ce36d | 2004-03-15 06:38:14 +0000 | [diff] [blame] | 250 | // |
Chris Lattner | 4d25c86 | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 251 | InlineCost += Caller->size()/20; |
Chris Lattner | f849253 | 2003-10-07 19:33:31 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | // Look at the size of the callee. Each basic block counts as 20 units, and |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 255 | // each instruction counts as 5. |
| 256 | InlineCost += CalleeFI.NumInsts*5 + CalleeFI.NumBlocks*20; |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 257 | return InlineCost; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 258 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 259 | |