1 //===- InlineCost.cpp - Cost analysis for inliner -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements inline cost analysis. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/InlineCost.h" 15 #include "llvm/Support/CallSite.h" 16 #include "llvm/CallingConv.h" 17 #include "llvm/IntrinsicInst.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 20 using namespace llvm; 21 22 /// callIsSmall - If a call is likely to lower to a single target instruction, 23 /// or is otherwise deemed small return true. 24 /// TODO: Perhaps calls like memcpy, strcpy, etc? 25 bool llvm::callIsSmall(const Function *F) { 26 if (!F) return false; 27 28 if (F->hasLocalLinkage()) return false; 29 30 if (!F->hasName()) return false; 31 32 StringRef Name = F->getName(); 33 34 // These will all likely lower to a single selection DAG node. 35 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" || 36 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || 37 Name == "sin" || Name == "sinf" || Name == "sinl" || 38 Name == "cos" || Name == "cosf" || Name == "cosl" || 39 Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl" ) 40 return true; 41 42 // These are all likely to be optimized into something smaller. 43 if (Name == "pow" || Name == "powf" || Name == "powl" || 44 Name == "exp2" || Name == "exp2l" || Name == "exp2f" || 45 Name == "floor" || Name == "floorf" || Name == "ceil" || 46 Name == "round" || Name == "ffs" || Name == "ffsl" || 47 Name == "abs" || Name == "labs" || Name == "llabs") 48 return true; 49 50 return false; 51 } 52 53 /// analyzeBasicBlock - Fill in the current structure with information gleaned 54 /// from the specified block. 55 void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB) { 56 ++NumBlocks; 57 unsigned NumInstsBeforeThisBB = NumInsts; 58 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); 59 II != E; ++II) { 60 if (isa<PHINode>(II)) continue; // PHI nodes don't count. 61 62 // Special handling for calls. 63 if (isa<CallInst>(II) || isa<InvokeInst>(II)) { 64 if (isa<DbgInfoIntrinsic>(II)) 65 continue; // Debug intrinsics don't count as size. 66 67 ImmutableCallSite CS(cast<Instruction>(II)); 68 69 if (const Function *F = CS.getCalledFunction()) { 70 // If a function is both internal and has a single use, then it is 71 // extremely likely to get inlined in the future (it was probably 72 // exposed by an interleaved devirtualization pass). 73 if (F->hasInternalLinkage() && F->hasOneUse()) 74 ++NumInlineCandidates; 75 76 // If this call is to function itself, then the function is recursive. 77 // Inlining it into other functions is a bad idea, because this is 78 // basically just a form of loop peeling, and our metrics aren't useful 79 // for that case. 80 if (F == BB->getParent()) 81 isRecursive = true; 82 } 83 84 if (!isa<IntrinsicInst>(II) && !callIsSmall(CS.getCalledFunction())) { 85 // Each argument to a call takes on average one instruction to set up. 86 NumInsts += CS.arg_size(); 87 88 // We don't want inline asm to count as a call - that would prevent loop 89 // unrolling. The argument setup cost is still real, though. 90 if (!isa<InlineAsm>(CS.getCalledValue())) 91 ++NumCalls; 92 } 93 } 94 95 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 96 if (!AI->isStaticAlloca()) 97 this->usesDynamicAlloca = true; 98 } 99 100 if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy()) 101 ++NumVectorInsts; 102 103 if (const CastInst *CI = dyn_cast<CastInst>(II)) { 104 // Noop casts, including ptr <-> int, don't count. 105 if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) || 106 isa<PtrToIntInst>(CI)) 107 continue; 108 // Result of a cmp instruction is often extended (to be used by other 109 // cmp instructions, logical or return instructions). These are usually 110 // nop on most sane targets. 111 if (isa<CmpInst>(CI->getOperand(0))) 112 continue; 113 } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(II)){ 114 // If a GEP has all constant indices, it will probably be folded with 115 // a load/store. 116 if (GEPI->hasAllConstantIndices()) 117 continue; 118 } 119 120 ++NumInsts; 121 } 122 123 if (isa<ReturnInst>(BB->getTerminator())) 124 ++NumRets; 125 126 // We never want to inline functions that contain an indirectbr. This is 127 // incorrect because all the blockaddress's (in static global initializers 128 // for example) would be referring to the original function, and this indirect 129 // jump would jump from the inlined copy of the function into the original 130 // function which is extremely undefined behavior. 131 if (isa<IndirectBrInst>(BB->getTerminator())) 132 containsIndirectBr = true; 133 134 // Remember NumInsts for this BB. 135 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; 136 } 137 138 // CountCodeReductionForConstant - Figure out an approximation for how many 139 // instructions will be constant folded if the specified value is constant. 140 // 141 unsigned CodeMetrics::CountCodeReductionForConstant(Value *V) { 142 unsigned Reduction = 0; 143 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ 144 User *U = *UI; 145 if (isa<BranchInst>(U) || isa<SwitchInst>(U)) { 146 // We will be able to eliminate all but one of the successors. 147 const TerminatorInst &TI = cast<TerminatorInst>(*U); 148 const unsigned NumSucc = TI.getNumSuccessors(); 149 unsigned Instrs = 0; 150 for (unsigned I = 0; I != NumSucc; ++I) 151 Instrs += NumBBInsts[TI.getSuccessor(I)]; 152 // We don't know which blocks will be eliminated, so use the average size. 153 Reduction += InlineConstants::InstrCost*Instrs*(NumSucc-1)/NumSucc; 154 } else { 155 // Figure out if this instruction will be removed due to simple constant 156 // propagation. 157 Instruction &Inst = cast<Instruction>(*U); 158 159 // We can't constant propagate instructions which have effects or 160 // read memory. 161 // 162 // FIXME: It would be nice to capture the fact that a load from a 163 // pointer-to-constant-global is actually a *really* good thing to zap. 164 // Unfortunately, we don't know the pointer that may get propagated here, 165 // so we can't make this decision. 166 if (Inst.mayReadFromMemory() || Inst.mayHaveSideEffects() || 167 isa<AllocaInst>(Inst)) 168 continue; 169 170 bool AllOperandsConstant = true; 171 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) 172 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) { 173 AllOperandsConstant = false; 174 break; 175 } 176 177 if (AllOperandsConstant) { 178 // We will get to remove this instruction... 179 Reduction += InlineConstants::InstrCost; 180 181 // And any other instructions that use it which become constants 182 // themselves. 183 Reduction += CountCodeReductionForConstant(&Inst); 184 } 185 } 186 } 187 return Reduction; 188 } 189 190 // CountCodeReductionForAlloca - Figure out an approximation of how much smaller 191 // the function will be if it is inlined into a context where an argument 192 // becomes an alloca. 193 // 194 unsigned CodeMetrics::CountCodeReductionForAlloca(Value *V) { 195 if (!V->getType()->isPointerTy()) return 0; // Not a pointer 196 unsigned Reduction = 0; 197 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ 198 Instruction *I = cast<Instruction>(*UI); 199 if (isa<LoadInst>(I) || isa<StoreInst>(I)) 200 Reduction += InlineConstants::InstrCost; 201 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { 202 // If the GEP has variable indices, we won't be able to do much with it. 203 if (GEP->hasAllConstantIndices()) 204 Reduction += CountCodeReductionForAlloca(GEP); 205 } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(I)) { 206 // Track pointer through bitcasts. 207 Reduction += CountCodeReductionForAlloca(BCI); 208 } else { 209 // If there is some other strange instruction, we're not going to be able 210 // to do much if we inline this. 211 return 0; 212 } 213 } 214 215 return Reduction; 216 } 217 218 /// analyzeFunction - Fill in the current structure with information gleaned 219 /// from the specified function. 220 void CodeMetrics::analyzeFunction(Function *F) { 221 // If this function contains a call to setjmp or _setjmp, never inline 222 // it. This is a hack because we depend on the user marking their local 223 // variables as volatile if they are live across a setjmp call, and they 224 // probably won't do this in callers. 225 if (F->callsFunctionThatReturnsTwice()) 226 callsSetJmp = true; 227 228 // Look at the size of the callee. 229 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 230 analyzeBasicBlock(&*BB); 231 } 232 233 /// analyzeFunction - Fill in the current structure with information gleaned 234 /// from the specified function. 235 void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F) { 236 Metrics.analyzeFunction(F); 237 238 // A function with exactly one return has it removed during the inlining 239 // process (see InlineFunction), so don't count it. 240 // FIXME: This knowledge should really be encoded outside of FunctionInfo. 241 if (Metrics.NumRets==1) 242 --Metrics.NumInsts; 243 244 // Check out all of the arguments to the function, figuring out how much 245 // code can be eliminated if one of the arguments is a constant. 246 ArgumentWeights.reserve(F->arg_size()); 247 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) 248 ArgumentWeights.push_back(ArgInfo(Metrics.CountCodeReductionForConstant(I), 249 Metrics.CountCodeReductionForAlloca(I))); 250 } 251 252 /// NeverInline - returns true if the function should never be inlined into 253 /// any caller 254 bool InlineCostAnalyzer::FunctionInfo::NeverInline() { 255 return (Metrics.callsSetJmp || Metrics.isRecursive || 256 Metrics.containsIndirectBr); 257 } 258 // getSpecializationBonus - The heuristic used to determine the per-call 259 // performance boost for using a specialization of Callee with argument 260 // specializedArgNo replaced by a constant. 261 int InlineCostAnalyzer::getSpecializationBonus(Function *Callee, 262 SmallVectorImpl<unsigned> &SpecializedArgNos) 263 { 264 if (Callee->mayBeOverridden()) 265 return 0; 266 267 int Bonus = 0; 268 // If this function uses the coldcc calling convention, prefer not to 269 // specialize it. 270 if (Callee->getCallingConv() == CallingConv::Cold) 271 Bonus -= InlineConstants::ColdccPenalty; 272 273 // Get information about the callee. 274 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; 275 276 // If we haven't calculated this information yet, do so now. 277 if (CalleeFI->Metrics.NumBlocks == 0) 278 CalleeFI->analyzeFunction(Callee); 279 280 unsigned ArgNo = 0; 281 unsigned i = 0; 282 for (Function::arg_iterator I = Callee->arg_begin(), E = Callee->arg_end(); 283 I != E; ++I, ++ArgNo) 284 if (ArgNo == SpecializedArgNos[i]) { 285 ++i; 286 Bonus += CountBonusForConstant(I); 287 } 288 289 // Calls usually take a long time, so they make the specialization gain 290 // smaller. 291 Bonus -= CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty; 292 293 return Bonus; 294 } 295 296 // ConstantFunctionBonus - Figure out how much of a bonus we can get for 297 // possibly devirtualizing a function. We'll subtract the size of the function 298 // we may wish to inline from the indirect call bonus providing a limit on 299 // growth. Leave an upper limit of 0 for the bonus - we don't want to penalize 300 // inlining because we decide we don't want to give a bonus for 301 // devirtualizing. 302 int InlineCostAnalyzer::ConstantFunctionBonus(CallSite CS, Constant *C) { 303 304 // This could just be NULL. 305 if (!C) return 0; 306 307 Function *F = dyn_cast<Function>(C); 308 if (!F) return 0; 309 310 int Bonus = InlineConstants::IndirectCallBonus + getInlineSize(CS, F); 311 return (Bonus > 0) ? 0 : Bonus; 312 } 313 314 // CountBonusForConstant - Figure out an approximation for how much per-call 315 // performance boost we can expect if the specified value is constant. 316 int InlineCostAnalyzer::CountBonusForConstant(Value *V, Constant *C) { 317 unsigned Bonus = 0; 318 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ 319 User *U = *UI; 320 if (CallInst *CI = dyn_cast<CallInst>(U)) { 321 // Turning an indirect call into a direct call is a BIG win 322 if (CI->getCalledValue() == V) 323 Bonus += ConstantFunctionBonus(CallSite(CI), C); 324 } else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) { 325 // Turning an indirect call into a direct call is a BIG win 326 if (II->getCalledValue() == V) 327 Bonus += ConstantFunctionBonus(CallSite(II), C); 328 } 329 // FIXME: Eliminating conditional branches and switches should 330 // also yield a per-call performance boost. 331 else { 332 // Figure out the bonuses that wll accrue due to simple constant 333 // propagation. 334 Instruction &Inst = cast<Instruction>(*U); 335 336 // We can't constant propagate instructions which have effects or 337 // read memory. 338 // 339 // FIXME: It would be nice to capture the fact that a load from a 340 // pointer-to-constant-global is actually a *really* good thing to zap. 341 // Unfortunately, we don't know the pointer that may get propagated here, 342 // so we can't make this decision. 343 if (Inst.mayReadFromMemory() || Inst.mayHaveSideEffects() || 344 isa<AllocaInst>(Inst)) 345 continue; 346 347 bool AllOperandsConstant = true; 348 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) 349 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) { 350 AllOperandsConstant = false; 351 break; 352 } 353 354 if (AllOperandsConstant) 355 Bonus += CountBonusForConstant(&Inst); 356 } 357 } 358 359 return Bonus; 360 } 361 362 int InlineCostAnalyzer::getInlineSize(CallSite CS, Function *Callee) { 363 // Get information about the callee. 364 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; 365 366 // If we haven't calculated this information yet, do so now. 367 if (CalleeFI->Metrics.NumBlocks == 0) 368 CalleeFI->analyzeFunction(Callee); 369 370 // InlineCost - This value measures how good of an inline candidate this call 371 // site is to inline. A lower inline cost make is more likely for the call to 372 // be inlined. This value may go negative. 373 // 374 int InlineCost = 0; 375 376 // Compute any size reductions we can expect due to arguments being passed into 377 // the function. 378 // 379 unsigned ArgNo = 0; 380 CallSite::arg_iterator I = CS.arg_begin(); 381 for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end(); 382 FI != FE; ++I, ++FI, ++ArgNo) { 383 384 // If an alloca is passed in, inlining this function is likely to allow 385 // significant future optimization possibilities (like scalar promotion, and 386 // scalarization), so encourage the inlining of the function. 387 // 388 if (isa<AllocaInst>(I)) 389 InlineCost -= CalleeFI->ArgumentWeights[ArgNo].AllocaWeight; 390 391 // If this is a constant being passed into the function, use the argument 392 // weights calculated for the callee to determine how much will be folded 393 // away with this information. 394 else if (isa<Constant>(I)) 395 InlineCost -= CalleeFI->ArgumentWeights[ArgNo].ConstantWeight; 396 } 397 398 // Each argument passed in has a cost at both the caller and the callee 399 // sides. Measurements show that each argument costs about the same as an 400 // instruction. 401 InlineCost -= (CS.arg_size() * InlineConstants::InstrCost); 402 403 // Now that we have considered all of the factors that make the call site more 404 // likely to be inlined, look at factors that make us not want to inline it. 405 406 // Calls usually take a long time, so they make the inlining gain smaller. 407 InlineCost += CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty; 408 409 // Look at the size of the callee. Each instruction counts as 5. 410 InlineCost += CalleeFI->Metrics.NumInsts*InlineConstants::InstrCost; 411 412 return InlineCost; 413 } 414 415 int InlineCostAnalyzer::getInlineBonuses(CallSite CS, Function *Callee) { 416 // Get information about the callee. 417 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; 418 419 // If we haven't calculated this information yet, do so now. 420 if (CalleeFI->Metrics.NumBlocks == 0) 421 CalleeFI->analyzeFunction(Callee); 422 423 bool isDirectCall = CS.getCalledFunction() == Callee; 424 Instruction *TheCall = CS.getInstruction(); 425 int Bonus = 0; 426 427 // If there is only one call of the function, and it has internal linkage, 428 // make it almost guaranteed to be inlined. 429 // 430 if (Callee->hasLocalLinkage() && Callee->hasOneUse() && isDirectCall) 431 Bonus += InlineConstants::LastCallToStaticBonus; 432 433 // If the instruction after the call, or if the normal destination of the 434 // invoke is an unreachable instruction, the function is noreturn. As such, 435 // there is little point in inlining this. 436 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { 437 if (isa<UnreachableInst>(II->getNormalDest()->begin())) 438 Bonus += InlineConstants::NoreturnPenalty; 439 } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall))) 440 Bonus += InlineConstants::NoreturnPenalty; 441 442 // If this function uses the coldcc calling convention, prefer not to inline 443 // it. 444 if (Callee->getCallingConv() == CallingConv::Cold) 445 Bonus += InlineConstants::ColdccPenalty; 446 447 // Add to the inline quality for properties that make the call valuable to 448 // inline. This includes factors that indicate that the result of inlining 449 // the function will be optimizable. Currently this just looks at arguments 450 // passed into the function. 451 // 452 CallSite::arg_iterator I = CS.arg_begin(); 453 for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end(); 454 FI != FE; ++I, ++FI) 455 // Compute any constant bonus due to inlining we want to give here. 456 if (isa<Constant>(I)) 457 Bonus += CountBonusForConstant(FI, cast<Constant>(I)); 458 459 return Bonus; 460 } 461 462 // getInlineCost - The heuristic used to determine if we should inline the 463 // function call or not. 464 // 465 InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, 466 SmallPtrSet<const Function*, 16> &NeverInline) { 467 return getInlineCost(CS, CS.getCalledFunction(), NeverInline); 468 } 469 470 InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, 471 Function *Callee, 472 SmallPtrSet<const Function*, 16> &NeverInline) { 473 Instruction *TheCall = CS.getInstruction(); 474 Function *Caller = TheCall->getParent()->getParent(); 475 476 // Don't inline functions which can be redefined at link-time to mean 477 // something else. Don't inline functions marked noinline or call sites 478 // marked noinline. 479 if (Callee->mayBeOverridden() || 480 Callee->hasFnAttr(Attribute::NoInline) || NeverInline.count(Callee) || 481 CS.isNoInline()) 482 return llvm::InlineCost::getNever(); 483 484 // Get information about the callee. 485 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; 486 487 // If we haven't calculated this information yet, do so now. 488 if (CalleeFI->Metrics.NumBlocks == 0) 489 CalleeFI->analyzeFunction(Callee); 490 491 // If we should never inline this, return a huge cost. 492 if (CalleeFI->NeverInline()) 493 return InlineCost::getNever(); 494 495 // FIXME: It would be nice to kill off CalleeFI->NeverInline. Then we 496 // could move this up and avoid computing the FunctionInfo for 497 // things we are going to just return always inline for. This 498 // requires handling setjmp somewhere else, however. 499 if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline)) 500 return InlineCost::getAlways(); 501 502 if (CalleeFI->Metrics.usesDynamicAlloca) { 503 // Get information about the caller. 504 FunctionInfo &CallerFI = CachedFunctionInfo[Caller]; 505 506 // If we haven't calculated this information yet, do so now. 507 if (CallerFI.Metrics.NumBlocks == 0) { 508 CallerFI.analyzeFunction(Caller); 509 510 // Recompute the CalleeFI pointer, getting Caller could have invalidated 511 // it. 512 CalleeFI = &CachedFunctionInfo[Callee]; 513 } 514 515 // Don't inline a callee with dynamic alloca into a caller without them. 516 // Functions containing dynamic alloca's are inefficient in various ways; 517 // don't create more inefficiency. 518 if (!CallerFI.Metrics.usesDynamicAlloca) 519 return InlineCost::getNever(); 520 } 521 522 // InlineCost - This value measures how good of an inline candidate this call 523 // site is to inline. A lower inline cost make is more likely for the call to 524 // be inlined. This value may go negative due to the fact that bonuses 525 // are negative numbers. 526 // 527 int InlineCost = getInlineSize(CS, Callee) + getInlineBonuses(CS, Callee); 528 return llvm::InlineCost::get(InlineCost); 529 } 530 531 // getSpecializationCost - The heuristic used to determine the code-size 532 // impact of creating a specialized version of Callee with argument 533 // SpecializedArgNo replaced by a constant. 534 InlineCost InlineCostAnalyzer::getSpecializationCost(Function *Callee, 535 SmallVectorImpl<unsigned> &SpecializedArgNos) 536 { 537 // Don't specialize functions which can be redefined at link-time to mean 538 // something else. 539 if (Callee->mayBeOverridden()) 540 return llvm::InlineCost::getNever(); 541 542 // Get information about the callee. 543 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; 544 545 // If we haven't calculated this information yet, do so now. 546 if (CalleeFI->Metrics.NumBlocks == 0) 547 CalleeFI->analyzeFunction(Callee); 548 549 int Cost = 0; 550 551 // Look at the original size of the callee. Each instruction counts as 5. 552 Cost += CalleeFI->Metrics.NumInsts * InlineConstants::InstrCost; 553 554 // Offset that with the amount of code that can be constant-folded 555 // away with the given arguments replaced by constants. 556 for (SmallVectorImpl<unsigned>::iterator an = SpecializedArgNos.begin(), 557 ae = SpecializedArgNos.end(); an != ae; ++an) 558 Cost -= CalleeFI->ArgumentWeights[*an].ConstantWeight; 559 560 return llvm::InlineCost::get(Cost); 561 } 562 563 // getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a 564 // higher threshold to determine if the function call should be inlined. 565 float InlineCostAnalyzer::getInlineFudgeFactor(CallSite CS) { 566 Function *Callee = CS.getCalledFunction(); 567 568 // Get information about the callee. 569 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; 570 571 // If we haven't calculated this information yet, do so now. 572 if (CalleeFI.Metrics.NumBlocks == 0) 573 CalleeFI.analyzeFunction(Callee); 574 575 float Factor = 1.0f; 576 // Single BB functions are often written to be inlined. 577 if (CalleeFI.Metrics.NumBlocks == 1) 578 Factor += 0.5f; 579 580 // Be more aggressive if the function contains a good chunk (if it mades up 581 // at least 10% of the instructions) of vector instructions. 582 if (CalleeFI.Metrics.NumVectorInsts > CalleeFI.Metrics.NumInsts/2) 583 Factor += 2.0f; 584 else if (CalleeFI.Metrics.NumVectorInsts > CalleeFI.Metrics.NumInsts/10) 585 Factor += 1.5f; 586 return Factor; 587 } 588 589 /// growCachedCostInfo - update the cached cost info for Caller after Callee has 590 /// been inlined. 591 void 592 InlineCostAnalyzer::growCachedCostInfo(Function *Caller, Function *Callee) { 593 CodeMetrics &CallerMetrics = CachedFunctionInfo[Caller].Metrics; 594 595 // For small functions we prefer to recalculate the cost for better accuracy. 596 if (CallerMetrics.NumBlocks < 10 && CallerMetrics.NumInsts < 1000) { 597 resetCachedCostInfo(Caller); 598 return; 599 } 600 601 // For large functions, we can save a lot of computation time by skipping 602 // recalculations. 603 if (CallerMetrics.NumCalls > 0) 604 --CallerMetrics.NumCalls; 605 606 if (Callee == 0) return; 607 608 CodeMetrics &CalleeMetrics = CachedFunctionInfo[Callee].Metrics; 609 610 // If we don't have metrics for the callee, don't recalculate them just to 611 // update an approximation in the caller. Instead, just recalculate the 612 // caller info from scratch. 613 if (CalleeMetrics.NumBlocks == 0) { 614 resetCachedCostInfo(Caller); 615 return; 616 } 617 618 // Since CalleeMetrics were already calculated, we know that the CallerMetrics 619 // reference isn't invalidated: both were in the DenseMap. 620 CallerMetrics.usesDynamicAlloca |= CalleeMetrics.usesDynamicAlloca; 621 622 // FIXME: If any of these three are true for the callee, the callee was 623 // not inlined into the caller, so I think they're redundant here. 624 CallerMetrics.callsSetJmp |= CalleeMetrics.callsSetJmp; 625 CallerMetrics.isRecursive |= CalleeMetrics.isRecursive; 626 CallerMetrics.containsIndirectBr |= CalleeMetrics.containsIndirectBr; 627 628 CallerMetrics.NumInsts += CalleeMetrics.NumInsts; 629 CallerMetrics.NumBlocks += CalleeMetrics.NumBlocks; 630 CallerMetrics.NumCalls += CalleeMetrics.NumCalls; 631 CallerMetrics.NumVectorInsts += CalleeMetrics.NumVectorInsts; 632 CallerMetrics.NumRets += CalleeMetrics.NumRets; 633 634 // analyzeBasicBlock counts each function argument as an inst. 635 if (CallerMetrics.NumInsts >= Callee->arg_size()) 636 CallerMetrics.NumInsts -= Callee->arg_size(); 637 else 638 CallerMetrics.NumInsts = 0; 639 640 // We are not updating the argument weights. We have already determined that 641 // Caller is a fairly large function, so we accept the loss of precision. 642 } 643 644 /// clear - empty the cache of inline costs 645 void InlineCostAnalyzer::clear() { 646 CachedFunctionInfo.clear(); 647 } 648