Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 1 | //===- InlineSimple.cpp - Code to perform simple function inlining --------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 10 | // This file implements bottom-up inlining of functions into callees. |
Chris Lattner | 0154505 | 2002-04-18 18:52:03 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 14 | #include "Inliner.h" |
Chris Lattner | 49fbff4 | 2005-05-18 04:30:33 +0000 | [diff] [blame] | 15 | #include "llvm/CallingConv.h" |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 16 | #include "llvm/Instructions.h" |
Chris Lattner | 8db93f1 | 2004-11-22 17:21:44 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
Chris Lattner | e544533 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 25 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 26 | struct VISIBILITY_HIDDEN ArgInfo { |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 27 | unsigned ConstantWeight; |
| 28 | unsigned AllocaWeight; |
| 29 | |
| 30 | ArgInfo(unsigned CWeight, unsigned AWeight) |
| 31 | : ConstantWeight(CWeight), AllocaWeight(AWeight) {} |
| 32 | }; |
| 33 | |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 34 | // FunctionInfo - For each function, calculate the size of it in blocks and |
| 35 | // instructions. |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 36 | struct VISIBILITY_HIDDEN FunctionInfo { |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 37 | // NumInsts, NumBlocks - Keep track of how large each function is, which is |
| 38 | // used to estimate the code size cost of inlining it. |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 39 | unsigned NumInsts, NumBlocks; |
| 40 | |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 41 | // ArgumentWeights - Each formal argument of the function is inspected to |
| 42 | // see if it is used in any contexts where making it a constant or alloca |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 43 | // would reduce the code size. If so, we add some value to the argument |
| 44 | // entry here. |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 45 | std::vector<ArgInfo> ArgumentWeights; |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 46 | |
Chris Lattner | ccca3ca | 2006-01-13 19:35:43 +0000 | [diff] [blame] | 47 | FunctionInfo() : NumInsts(0), NumBlocks(0) {} |
Chris Lattner | ee45f4c | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 48 | |
| 49 | /// analyzeFunction - Fill in the current structure with information gleaned |
| 50 | /// from the specified function. |
| 51 | void analyzeFunction(Function *F); |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 54 | class VISIBILITY_HIDDEN SimpleInliner : public Inliner { |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 55 | std::map<const Function*, FunctionInfo> CachedFunctionInfo; |
| 56 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 57 | static char ID; // Pass identifcation, replacement for typeid |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 58 | int getInlineCost(CallSite CS); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 59 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 60 | char SimpleInliner::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 61 | RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining"); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Devang Patel | c71ca3c | 2007-01-26 00:47:38 +0000 | [diff] [blame] | 64 | Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 65 | |
| 66 | // CountCodeReductionForConstant - Figure out an approximation for how many |
| 67 | // instructions will be constant folded if the specified value is constant. |
| 68 | // |
| 69 | static unsigned CountCodeReductionForConstant(Value *V) { |
| 70 | unsigned Reduction = 0; |
| 71 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 72 | if (isa<BranchInst>(*UI)) |
| 73 | Reduction += 40; // Eliminating a conditional branch is a big win |
| 74 | else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI)) |
| 75 | // Eliminating a switch is a big win, proportional to the number of edges |
| 76 | // deleted. |
| 77 | Reduction += (SI->getNumSuccessors()-1) * 40; |
| 78 | else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 79 | // Turning an indirect call into a direct call is a BIG win |
| 80 | Reduction += CI->getCalledValue() == V ? 500 : 0; |
| 81 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
| 82 | // Turning an indirect call into a direct call is a BIG win |
Chris Lattner | 6d1db01 | 2003-11-21 21:57:29 +0000 | [diff] [blame] | 83 | Reduction += II->getCalledValue() == V ? 500 : 0; |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 84 | } else { |
| 85 | // Figure out if this instruction will be removed due to simple constant |
| 86 | // propagation. |
| 87 | Instruction &Inst = cast<Instruction>(**UI); |
| 88 | bool AllOperandsConstant = true; |
| 89 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) |
Reid Spencer | 460f16c | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 90 | if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) { |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 91 | AllOperandsConstant = false; |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | if (AllOperandsConstant) { |
| 96 | // We will get to remove this instruction... |
| 97 | Reduction += 7; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 99 | // And any other instructions that use it which become constants |
| 100 | // themselves. |
| 101 | Reduction += CountCodeReductionForConstant(&Inst); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return Reduction; |
| 106 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 108 | // CountCodeReductionForAlloca - Figure out an approximation of how much smaller |
| 109 | // the function will be if it is inlined into a context where an argument |
| 110 | // becomes an alloca. |
| 111 | // |
| 112 | static unsigned CountCodeReductionForAlloca(Value *V) { |
| 113 | if (!isa<PointerType>(V->getType())) return 0; // Not a pointer |
| 114 | unsigned Reduction = 0; |
| 115 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ |
| 116 | Instruction *I = cast<Instruction>(*UI); |
| 117 | if (isa<LoadInst>(I) || isa<StoreInst>(I)) |
| 118 | Reduction += 10; |
| 119 | else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 120 | // If the GEP has variable indices, we won't be able to do much with it. |
| 121 | for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end(); |
| 122 | I != E; ++I) |
| 123 | if (!isa<Constant>(*I)) return 0; |
| 124 | Reduction += CountCodeReductionForAlloca(GEP)+15; |
| 125 | } else { |
| 126 | // If there is some other strange instruction, we're not going to be able |
| 127 | // to do much if we inline this. |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return Reduction; |
| 133 | } |
| 134 | |
Chris Lattner | ee45f4c | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 135 | /// analyzeFunction - Fill in the current structure with information gleaned |
| 136 | /// from the specified function. |
| 137 | void FunctionInfo::analyzeFunction(Function *F) { |
| 138 | unsigned NumInsts = 0, NumBlocks = 0; |
| 139 | |
| 140 | // Look at the size of the callee. Each basic block counts as 20 units, and |
| 141 | // each instruction counts as 10. |
| 142 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 143 | for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); |
Chris Lattner | 7b166d9 | 2006-09-09 20:40:44 +0000 | [diff] [blame] | 144 | II != E; ++II) { |
| 145 | if (isa<DbgInfoIntrinsic>(II)) continue; // Debug intrinsics don't count. |
| 146 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 147 | // Noop casts, including ptr <-> int, don't count. |
Chris Lattner | 7b166d9 | 2006-09-09 20:40:44 +0000 | [diff] [blame] | 148 | if (const CastInst *CI = dyn_cast<CastInst>(II)) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 149 | if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) || |
| 150 | isa<PtrToIntInst>(CI)) |
Chris Lattner | 7b166d9 | 2006-09-09 20:40:44 +0000 | [diff] [blame] | 151 | continue; |
Chris Lattner | 7b166d9 | 2006-09-09 20:40:44 +0000 | [diff] [blame] | 152 | } else if (const GetElementPtrInst *GEPI = |
| 153 | dyn_cast<GetElementPtrInst>(II)) { |
| 154 | // If a GEP has all constant indices, it will probably be folded with |
| 155 | // a load/store. |
| 156 | bool AllConstant = true; |
| 157 | for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i) |
| 158 | if (!isa<ConstantInt>(GEPI->getOperand(i))) { |
| 159 | AllConstant = false; |
| 160 | break; |
| 161 | } |
| 162 | if (AllConstant) continue; |
| 163 | } |
| 164 | |
| 165 | ++NumInsts; |
| 166 | } |
Chris Lattner | ee45f4c | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 167 | |
| 168 | ++NumBlocks; |
| 169 | } |
| 170 | |
| 171 | this->NumBlocks = NumBlocks; |
| 172 | this->NumInsts = NumInsts; |
| 173 | |
| 174 | // Check out all of the arguments to the function, figuring out how much |
| 175 | // code can be eliminated if one of the arguments is a constant. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 176 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
Chris Lattner | ee45f4c | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 177 | ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I), |
| 178 | CountCodeReductionForAlloca(I))); |
| 179 | } |
| 180 | |
| 181 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 182 | // getInlineCost - The heuristic used to determine if we should inline the |
| 183 | // function call or not. |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 184 | // |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 185 | int SimpleInliner::getInlineCost(CallSite CS) { |
Chris Lattner | e544533 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 186 | Instruction *TheCall = CS.getInstruction(); |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 187 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | e544533 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 188 | const Function *Caller = TheCall->getParent()->getParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 190 | // Don't inline a directly recursive call. |
| 191 | if (Caller == Callee) return 2000000000; |
| 192 | |
| 193 | // InlineCost - This value measures how good of an inline candidate this call |
| 194 | // site is to inline. A lower inline cost make is more likely for the call to |
| 195 | // be inlined. This value may go negative. |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 196 | // |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 197 | int InlineCost = 0; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 198 | |
| 199 | // If there is only one call of the function, and it has internal linkage, |
| 200 | // make it almost guaranteed to be inlined. |
| 201 | // |
Chris Lattner | bacc773 | 2003-10-20 05:54:26 +0000 | [diff] [blame] | 202 | if (Callee->hasInternalLinkage() && Callee->hasOneUse()) |
Chris Lattner | a8e4750 | 2004-11-09 08:05:23 +0000 | [diff] [blame] | 203 | InlineCost -= 30000; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 49fbff4 | 2005-05-18 04:30:33 +0000 | [diff] [blame] | 205 | // If this function uses the coldcc calling convention, prefer not to inline |
| 206 | // it. |
| 207 | if (Callee->getCallingConv() == CallingConv::Cold) |
| 208 | InlineCost += 2000; |
| 209 | |
| 210 | // If the instruction after the call, or if the normal destination of the |
| 211 | // invoke is an unreachable instruction, the function is noreturn. As such, |
| 212 | // there is little point in inlining this. |
| 213 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
| 214 | if (isa<UnreachableInst>(II->getNormalDest()->begin())) |
| 215 | InlineCost += 10000; |
| 216 | } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall))) |
| 217 | InlineCost += 10000; |
| 218 | |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 219 | // Get information about the callee... |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 220 | FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 221 | |
Chris Lattner | ee45f4c | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 222 | // If we haven't calculated this information yet, do so now. |
| 223 | if (CalleeFI.NumBlocks == 0) |
| 224 | CalleeFI.analyzeFunction(Callee); |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 226 | // Add to the inline quality for properties that make the call valuable to |
| 227 | // inline. This includes factors that indicate that the result of inlining |
| 228 | // the function will be optimizable. Currently this just looks at arguments |
| 229 | // passed into the function. |
| 230 | // |
| 231 | unsigned ArgNo = 0; |
| 232 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 233 | I != E; ++I, ++ArgNo) { |
| 234 | // Each argument passed in has a cost at both the caller and the callee |
| 235 | // sides. This favors functions that take many arguments over functions |
| 236 | // that take few arguments. |
| 237 | InlineCost -= 20; |
| 238 | |
| 239 | // If this is a function being passed in, it is very likely that we will be |
| 240 | // able to turn an indirect function call into a direct function call. |
| 241 | if (isa<Function>(I)) |
| 242 | InlineCost -= 100; |
| 243 | |
| 244 | // If an alloca is passed in, inlining this function is likely to allow |
| 245 | // significant future optimization possibilities (like scalar promotion, and |
| 246 | // scalarization), so encourage the inlining of the function. |
| 247 | // |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 248 | else if (isa<AllocaInst>(I)) { |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 249 | if (ArgNo < CalleeFI.ArgumentWeights.size()) |
| 250 | InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight; |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 251 | |
| 252 | // If this is a constant being passed into the function, use the argument |
| 253 | // weights calculated for the callee to determine how much will be folded |
| 254 | // away with this information. |
Reid Spencer | 460f16c | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 255 | } else if (isa<Constant>(I)) { |
Chris Lattner | 619d354 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 256 | if (ArgNo < CalleeFI.ArgumentWeights.size()) |
| 257 | InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight; |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | // Now that we have considered all of the factors that make the call site more |
| 262 | // likely to be inlined, look at factors that make us not want to inline it. |
| 263 | |
Chris Lattner | da78b00 | 2003-10-07 19:33:31 +0000 | [diff] [blame] | 264 | // Don't inline into something too big, which would make it bigger. Here, we |
| 265 | // count each basic block as a single unit. |
Chris Lattner | 6dd196f | 2004-03-15 06:38:14 +0000 | [diff] [blame] | 266 | // |
Chris Lattner | 775cbdd | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 267 | InlineCost += Caller->size()/20; |
Chris Lattner | da78b00 | 2003-10-07 19:33:31 +0000 | [diff] [blame] | 268 | |
| 269 | |
| 270 | // Look at the size of the callee. Each basic block counts as 20 units, and |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 271 | // each instruction counts as 5. |
| 272 | InlineCost += CalleeFI.NumInsts*5 + CalleeFI.NumBlocks*20; |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 273 | return InlineCost; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 274 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 275 | |