Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 1 | //===- InlineSimple.cpp - Code to perform simple function inlining --------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | 05deb04 | 2005-05-18 04:30:33 +0000 | [diff] [blame^] | 15 | #include "llvm/CallingConv.h" |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 16 | #include "llvm/Instructions.h" |
Chris Lattner | 79e87e3 | 2004-11-22 17:21:44 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
Chris Lattner | d367d05 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CallSite.h" |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 24 | namespace { |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 25 | struct ArgInfo { |
| 26 | unsigned ConstantWeight; |
| 27 | unsigned AllocaWeight; |
| 28 | |
| 29 | ArgInfo(unsigned CWeight, unsigned AWeight) |
| 30 | : ConstantWeight(CWeight), AllocaWeight(AWeight) {} |
| 31 | }; |
| 32 | |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 33 | // FunctionInfo - For each function, calculate the size of it in blocks and |
| 34 | // instructions. |
| 35 | struct FunctionInfo { |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 36 | // HasAllocas - Keep track of whether or not a function contains an alloca |
| 37 | // instruction that is not in the entry block of the function. Inlining |
| 38 | // this call could cause us to blow out the stack, because the stack memory |
| 39 | // would never be released. |
| 40 | // |
| 41 | // FIXME: LLVM needs a way of dealloca'ing memory, which would make this |
| 42 | // irrelevant! |
| 43 | // |
| 44 | bool HasAllocas; |
| 45 | |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 46 | // NumInsts, NumBlocks - Keep track of how large each function is, which is |
| 47 | // used to estimate the code size cost of inlining it. |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 48 | unsigned NumInsts, NumBlocks; |
| 49 | |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 50 | // ArgumentWeights - Each formal argument of the function is inspected to |
| 51 | // 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] | 52 | // would reduce the code size. If so, we add some value to the argument |
| 53 | // entry here. |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 54 | std::vector<ArgInfo> ArgumentWeights; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 55 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 56 | FunctionInfo() : HasAllocas(false), NumInsts(0), NumBlocks(0) {} |
| 57 | |
| 58 | /// analyzeFunction - Fill in the current structure with information gleaned |
| 59 | /// from the specified function. |
| 60 | void analyzeFunction(Function *F); |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class SimpleInliner : public Inliner { |
| 64 | std::map<const Function*, FunctionInfo> CachedFunctionInfo; |
| 65 | public: |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 66 | int getInlineCost(CallSite CS); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 67 | }; |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 68 | RegisterOpt<SimpleInliner> X("inline", "Function Integration/Inlining"); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 71 | ModulePass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 72 | |
| 73 | // CountCodeReductionForConstant - Figure out an approximation for how many |
| 74 | // instructions will be constant folded if the specified value is constant. |
| 75 | // |
| 76 | static unsigned CountCodeReductionForConstant(Value *V) { |
| 77 | unsigned Reduction = 0; |
| 78 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 79 | if (isa<BranchInst>(*UI)) |
| 80 | Reduction += 40; // Eliminating a conditional branch is a big win |
| 81 | else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI)) |
| 82 | // Eliminating a switch is a big win, proportional to the number of edges |
| 83 | // deleted. |
| 84 | Reduction += (SI->getNumSuccessors()-1) * 40; |
| 85 | else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 86 | // Turning an indirect call into a direct call is a BIG win |
| 87 | Reduction += CI->getCalledValue() == V ? 500 : 0; |
| 88 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
| 89 | // Turning an indirect call into a direct call is a BIG win |
Chris Lattner | 61b3f20 | 2003-11-21 21:57:29 +0000 | [diff] [blame] | 90 | Reduction += II->getCalledValue() == V ? 500 : 0; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 91 | } else { |
| 92 | // Figure out if this instruction will be removed due to simple constant |
| 93 | // propagation. |
| 94 | Instruction &Inst = cast<Instruction>(**UI); |
| 95 | bool AllOperandsConstant = true; |
| 96 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) |
Reid Spencer | ef784f0 | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 97 | if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) { |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 98 | AllOperandsConstant = false; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | if (AllOperandsConstant) { |
| 103 | // We will get to remove this instruction... |
| 104 | Reduction += 7; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 106 | // And any other instructions that use it which become constants |
| 107 | // themselves. |
| 108 | Reduction += CountCodeReductionForConstant(&Inst); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return Reduction; |
| 113 | } |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 115 | // CountCodeReductionForAlloca - Figure out an approximation of how much smaller |
| 116 | // the function will be if it is inlined into a context where an argument |
| 117 | // becomes an alloca. |
| 118 | // |
| 119 | static unsigned CountCodeReductionForAlloca(Value *V) { |
| 120 | if (!isa<PointerType>(V->getType())) return 0; // Not a pointer |
| 121 | unsigned Reduction = 0; |
| 122 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ |
| 123 | Instruction *I = cast<Instruction>(*UI); |
| 124 | if (isa<LoadInst>(I) || isa<StoreInst>(I)) |
| 125 | Reduction += 10; |
| 126 | else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 127 | // If the GEP has variable indices, we won't be able to do much with it. |
| 128 | for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end(); |
| 129 | I != E; ++I) |
| 130 | if (!isa<Constant>(*I)) return 0; |
| 131 | Reduction += CountCodeReductionForAlloca(GEP)+15; |
| 132 | } else { |
| 133 | // If there is some other strange instruction, we're not going to be able |
| 134 | // to do much if we inline this. |
| 135 | return 0; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return Reduction; |
| 140 | } |
| 141 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 142 | /// analyzeFunction - Fill in the current structure with information gleaned |
| 143 | /// from the specified function. |
| 144 | void FunctionInfo::analyzeFunction(Function *F) { |
| 145 | unsigned NumInsts = 0, NumBlocks = 0; |
| 146 | |
| 147 | // Look at the size of the callee. Each basic block counts as 20 units, and |
| 148 | // each instruction counts as 10. |
| 149 | for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 150 | for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); |
| 151 | II != E; ++II) { |
Chris Lattner | 79e87e3 | 2004-11-22 17:21:44 +0000 | [diff] [blame] | 152 | if (!isa<DbgInfoIntrinsic>(II)) ++NumInsts; |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 153 | |
| 154 | // If there is an alloca in the body of the function, we cannot currently |
| 155 | // inline the function without the risk of exploding the stack. |
| 156 | if (isa<AllocaInst>(II) && BB != F->begin()) { |
| 157 | HasAllocas = true; |
| 158 | this->NumBlocks = this->NumInsts = 1; |
| 159 | return; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | ++NumBlocks; |
| 164 | } |
| 165 | |
| 166 | this->NumBlocks = NumBlocks; |
| 167 | this->NumInsts = NumInsts; |
| 168 | |
| 169 | // Check out all of the arguments to the function, figuring out how much |
| 170 | // code can be eliminated if one of the arguments is a constant. |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 171 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 172 | ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I), |
| 173 | CountCodeReductionForAlloca(I))); |
| 174 | } |
| 175 | |
| 176 | |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 177 | // getInlineCost - The heuristic used to determine if we should inline the |
| 178 | // function call or not. |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 179 | // |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 180 | int SimpleInliner::getInlineCost(CallSite CS) { |
Chris Lattner | d367d05 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 181 | Instruction *TheCall = CS.getInstruction(); |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 182 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | d367d05 | 2003-08-24 06:59:28 +0000 | [diff] [blame] | 183 | const Function *Caller = TheCall->getParent()->getParent(); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 184 | |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 185 | // Don't inline a directly recursive call. |
| 186 | if (Caller == Callee) return 2000000000; |
| 187 | |
| 188 | // InlineCost - This value measures how good of an inline candidate this call |
| 189 | // site is to inline. A lower inline cost make is more likely for the call to |
| 190 | // be inlined. This value may go negative. |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 191 | // |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 192 | int InlineCost = 0; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 193 | |
| 194 | // If there is only one call of the function, and it has internal linkage, |
| 195 | // make it almost guaranteed to be inlined. |
| 196 | // |
Chris Lattner | 2d9c117 | 2003-10-20 05:54:26 +0000 | [diff] [blame] | 197 | if (Callee->hasInternalLinkage() && Callee->hasOneUse()) |
Chris Lattner | 436285e | 2004-11-09 08:05:23 +0000 | [diff] [blame] | 198 | InlineCost -= 30000; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 05deb04 | 2005-05-18 04:30:33 +0000 | [diff] [blame^] | 200 | // If this function uses the coldcc calling convention, prefer not to inline |
| 201 | // it. |
| 202 | if (Callee->getCallingConv() == CallingConv::Cold) |
| 203 | InlineCost += 2000; |
| 204 | |
| 205 | // If the instruction after the call, or if the normal destination of the |
| 206 | // invoke is an unreachable instruction, the function is noreturn. As such, |
| 207 | // there is little point in inlining this. |
| 208 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
| 209 | if (isa<UnreachableInst>(II->getNormalDest()->begin())) |
| 210 | InlineCost += 10000; |
| 211 | } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall))) |
| 212 | InlineCost += 10000; |
| 213 | |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 214 | // Get information about the callee... |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 215 | FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 216 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 217 | // If we haven't calculated this information yet, do so now. |
| 218 | if (CalleeFI.NumBlocks == 0) |
| 219 | CalleeFI.analyzeFunction(Callee); |
Chris Lattner | 6dc0ae2 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 220 | |
Chris Lattner | cde351e | 2004-08-12 05:45:09 +0000 | [diff] [blame] | 221 | // Don't inline calls to functions with allocas that are not in the entry |
| 222 | // block of the function. |
| 223 | if (CalleeFI.HasAllocas) |
| 224 | return 2000000000; |
Chris Lattner | 51c28a5 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 225 | |
| 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 | // |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 248 | else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 249 | if (ArgNo < CalleeFI.ArgumentWeights.size()) |
| 250 | InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight; |
Chris Lattner | 51c28a5 | 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 | ef784f0 | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 255 | } else if (isa<Constant>(I)) { |
Chris Lattner | 2dc85b2 | 2004-03-13 23:15:45 +0000 | [diff] [blame] | 256 | if (ArgNo < CalleeFI.ArgumentWeights.size()) |
| 257 | InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight; |
Chris Lattner | 51c28a5 | 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 | f849253 | 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 | 95ce36d | 2004-03-15 06:38:14 +0000 | [diff] [blame] | 266 | // |
Chris Lattner | 4d25c86 | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 267 | InlineCost += Caller->size()/20; |
Chris Lattner | f849253 | 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 | 51c28a5 | 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 | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 273 | return InlineCost; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 274 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 275 | |