// getSpecializationCost - The heuristic used to determine the code-size // impact of creating a specialized version of Callee with argument // SpecializedArgNo replaced by a constant. InlineCost InlineCostAnalyzer::getSpecializationCost(Function *Callee, SmallVectorImpl<unsigned> &SpecializedArgNos) { // Don't specialize functions which can be redefined at link-time to mean // something else. if (Callee->mayBeOverridden()) return llvm::InlineCost::getNever(); // Get information about the callee. FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; // If we haven't calculated this information yet, do so now. if (CalleeFI->Metrics.NumBlocks == 0) CalleeFI->analyzeFunction(Callee); int Cost = 0; // Look at the orginal size of the callee. Each instruction counts as 5. Cost += CalleeFI->Metrics.NumInsts * InlineConstants::InstrCost; // Offset that with the amount of code that can be constant-folded // away with the given arguments replaced by constants. for (SmallVectorImpl<unsigned>::iterator an = SpecializedArgNos.begin(), ae = SpecializedArgNos.end(); an != ae; ++an) Cost -= CalleeFI->ArgumentWeights[*an].ConstantWeight; return llvm::InlineCost::get(Cost); }
// getSpecializationBonus - The heuristic used to determine the per-call // performance boost for using a specialization of Callee with argument // specializedArgNo replaced by a constant. int InlineCostAnalyzer::getSpecializationBonus(Function *Callee, SmallVectorImpl<unsigned> &SpecializedArgNos) { if (Callee->mayBeOverridden()) return 0; int Bonus = 0; // If this function uses the coldcc calling convention, prefer not to // specialize it. if (Callee->getCallingConv() == CallingConv::Cold) Bonus -= InlineConstants::ColdccPenalty; // Get information about the callee. FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; // If we haven't calculated this information yet, do so now. if (CalleeFI->Metrics.NumBlocks == 0) CalleeFI->analyzeFunction(Callee); unsigned ArgNo = 0; unsigned i = 0; for (Function::arg_iterator I = Callee->arg_begin(), E = Callee->arg_end(); I != E; ++I, ++ArgNo) if (ArgNo == SpecializedArgNos[i]) { ++i; Bonus += CountBonusForConstant(I); } // Calls usually take a long time, so they make the specialization gain // smaller. Bonus -= CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty; return Bonus; }
// getSpecializationBonus - The heuristic used to determine the per-call // performance boost for using a specialization of Callee with argument // specializedArgNo replaced by a constant. int InlineCostAnalyzer::getSpecializationBonus(Function *Callee, SmallVectorImpl<unsigned> &SpecializedArgNos) { if (Callee->mayBeOverridden()) return 0; int Bonus = 0; // If this function uses the coldcc calling convention, prefer not to // specialize it. if (Callee->getCallingConv() == CallingConv::Cold) Bonus -= InlineConstants::ColdccPenalty; // Get information about the callee. FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; // If we haven't calculated this information yet, do so now. if (CalleeFI->Metrics.NumBlocks == 0) CalleeFI->analyzeFunction(Callee); for (unsigned i = 0, s = SpecializedArgNos.size(); i < s; ++i ) { Bonus += CalleeFI->ArgumentWeights[SpecializedArgNos[i]].ConstantBonus; } // Calls usually take a long time, so they make the specialization gain // smaller. Bonus -= CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty; return Bonus; }
InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, Function *Callee) { Instruction *TheCall = CS.getInstruction(); Function *Caller = TheCall->getParent()->getParent(); // Don't inline functions which can be redefined at link-time to mean // something else. Don't inline functions marked noinline or call sites // marked noinline. if (Callee->mayBeOverridden() || Callee->hasFnAttr(Attribute::NoInline) || CS.isNoInline()) return llvm::InlineCost::getNever(); // Get information about the callee. FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; // If we haven't calculated this information yet, do so now. if (CalleeFI->Metrics.NumBlocks == 0) CalleeFI->analyzeFunction(Callee, TD); // If we should never inline this, return a huge cost. if (CalleeFI->NeverInline()) return InlineCost::getNever(); // FIXME: It would be nice to kill off CalleeFI->NeverInline. Then we // could move this up and avoid computing the FunctionInfo for // things we are going to just return always inline for. This // requires handling setjmp somewhere else, however. if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline)) return InlineCost::getAlways(); if (CalleeFI->Metrics.usesDynamicAlloca) { // Get information about the caller. FunctionInfo &CallerFI = CachedFunctionInfo[Caller]; // If we haven't calculated this information yet, do so now. if (CallerFI.Metrics.NumBlocks == 0) { CallerFI.analyzeFunction(Caller, TD); // Recompute the CalleeFI pointer, getting Caller could have invalidated // it. CalleeFI = &CachedFunctionInfo[Callee]; } // Don't inline a callee with dynamic alloca into a caller without them. // Functions containing dynamic alloca's are inefficient in various ways; // don't create more inefficiency. if (!CallerFI.Metrics.usesDynamicAlloca) return InlineCost::getNever(); } // InlineCost - This value measures how good of an inline candidate this call // site is to inline. A lower inline cost make is more likely for the call to // be inlined. This value may go negative due to the fact that bonuses // are negative numbers. // int InlineCost = getInlineSize(CS, Callee) + getInlineBonuses(CS, Callee); return llvm::InlineCost::get(InlineCost); }
int InlineCostAnalyzer::getInlineBonuses(CallSite CS, Function *Callee) { // Get information about the callee. FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; // If we haven't calculated this information yet, do so now. if (CalleeFI->Metrics.NumBlocks == 0) CalleeFI->analyzeFunction(Callee, TD); bool isDirectCall = CS.getCalledFunction() == Callee; Instruction *TheCall = CS.getInstruction(); int Bonus = 0; // If there is only one call of the function, and it has internal linkage, // make it almost guaranteed to be inlined. // if (Callee->hasLocalLinkage() && Callee->hasOneUse() && isDirectCall) Bonus += InlineConstants::LastCallToStaticBonus; // If the instruction after the call, or if the normal destination of the // invoke is an unreachable instruction, the function is noreturn. As such, // there is little point in inlining this. if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { if (isa<UnreachableInst>(II->getNormalDest()->begin())) Bonus += InlineConstants::NoreturnPenalty; } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall))) Bonus += InlineConstants::NoreturnPenalty; // If this function uses the coldcc calling convention, prefer not to inline // it. if (Callee->getCallingConv() == CallingConv::Cold) Bonus += InlineConstants::ColdccPenalty; // Add to the inline quality for properties that make the call valuable to // inline. This includes factors that indicate that the result of inlining // the function will be optimizable. Currently this just looks at arguments // passed into the function. // CallSite::arg_iterator I = CS.arg_begin(); for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end(); FI != FE; ++I, ++FI) // Compute any constant bonus due to inlining we want to give here. if (isa<Constant>(I)) Bonus += CountBonusForConstant(FI, cast<Constant>(I)); return Bonus; }
int InlineCostAnalyzer::getInlineSize(CallSite CS, Function *Callee) { // Get information about the callee. FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; // If we haven't calculated this information yet, do so now. if (CalleeFI->Metrics.NumBlocks == 0) CalleeFI->analyzeFunction(Callee, TD); // InlineCost - This value measures how good of an inline candidate this call // site is to inline. A lower inline cost make is more likely for the call to // be inlined. This value may go negative. // int InlineCost = 0; // Compute any size reductions we can expect due to arguments being passed into // the function. // unsigned ArgNo = 0; CallSite::arg_iterator I = CS.arg_begin(); for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end(); FI != FE; ++I, ++FI, ++ArgNo) { // If an alloca is passed in, inlining this function is likely to allow // significant future optimization possibilities (like scalar promotion, and // scalarization), so encourage the inlining of the function. // if (isa<AllocaInst>(I)) InlineCost -= CalleeFI->ArgumentWeights[ArgNo].AllocaWeight; // If this is a constant being passed into the function, use the argument // weights calculated for the callee to determine how much will be folded // away with this information. else if (isa<Constant>(I)) InlineCost -= CalleeFI->ArgumentWeights[ArgNo].ConstantWeight; } const DenseMap<std::pair<unsigned, unsigned>, unsigned> &ArgPairWeights = CalleeFI->PointerArgPairWeights; for (DenseMap<std::pair<unsigned, unsigned>, unsigned>::const_iterator I = ArgPairWeights.begin(), E = ArgPairWeights.end(); I != E; ++I) if (CS.getArgument(I->first.first)->stripInBoundsConstantOffsets() == CS.getArgument(I->first.second)->stripInBoundsConstantOffsets()) InlineCost -= I->second; // Each argument passed in has a cost at both the caller and the callee // sides. Measurements show that each argument costs about the same as an // instruction. InlineCost -= (CS.arg_size() * InlineConstants::InstrCost); // Now that we have considered all of the factors that make the call site more // likely to be inlined, look at factors that make us not want to inline it. // Calls usually take a long time, so they make the inlining gain smaller. InlineCost += CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty; // Look at the size of the callee. Each instruction counts as 5. InlineCost += CalleeFI->Metrics.NumInsts * InlineConstants::InstrCost; return InlineCost; }
InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, Function *Callee, SmallPtrSet<const Function*, 16> &NeverInline) { Instruction *TheCall = CS.getInstruction(); Function *Caller = TheCall->getParent()->getParent(); bool isDirectCall = CS.getCalledFunction() == Callee; // Don't inline functions which can be redefined at link-time to mean // something else. Don't inline functions marked noinline or call sites // marked noinline. if (Callee->mayBeOverridden() || Callee->hasFnAttr(Attribute::NoInline) || NeverInline.count(Callee) || CS.isNoInline()) return llvm::InlineCost::getNever(); // Get information about the callee. FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee]; // If we haven't calculated this information yet, do so now. if (CalleeFI->Metrics.NumBlocks == 0) CalleeFI->analyzeFunction(Callee); // If we should never inline this, return a huge cost. if (CalleeFI->NeverInline()) return InlineCost::getNever(); // FIXME: It would be nice to kill off CalleeFI->NeverInline. Then we // could move this up and avoid computing the FunctionInfo for // things we are going to just return always inline for. This // requires handling setjmp somewhere else, however. if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline)) return InlineCost::getAlways(); if (CalleeFI->Metrics.usesDynamicAlloca) { // Get infomation about the caller. FunctionInfo &CallerFI = CachedFunctionInfo[Caller]; // If we haven't calculated this information yet, do so now. if (CallerFI.Metrics.NumBlocks == 0) { CallerFI.analyzeFunction(Caller); // Recompute the CalleeFI pointer, getting Caller could have invalidated // it. CalleeFI = &CachedFunctionInfo[Callee]; } // Don't inline a callee with dynamic alloca into a caller without them. // Functions containing dynamic alloca's are inefficient in various ways; // don't create more inefficiency. if (!CallerFI.Metrics.usesDynamicAlloca) return InlineCost::getNever(); } // InlineCost - This value measures how good of an inline candidate this call // site is to inline. A lower inline cost make is more likely for the call to // be inlined. This value may go negative. // int InlineCost = 0; // Add to the inline quality for properties that make the call valuable to // inline. This includes factors that indicate that the result of inlining // the function will be optimizable. Currently this just looks at arguments // passed into the function. // unsigned ArgNo = 0; CallSite::arg_iterator I = CS.arg_begin(); for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end(); FI != FE; ++I, ++FI, ++ArgNo) { // If an alloca is passed in, inlining this function is likely to allow // significant future optimization possibilities (like scalar promotion, and // scalarization), so encourage the inlining of the function. // if (isa<AllocaInst>(I)) InlineCost -= CalleeFI->ArgumentWeights[ArgNo].AllocaWeight; // If this is a constant being passed into the function, use the argument // weights calculated for the callee to determine how much will be folded // away with this information. else if (isa<Constant>(I)) { InlineCost -= CalleeFI->ArgumentWeights[ArgNo].ConstantWeight; // Compute any constant bonus due to inlining we want to give here. InlineCost -= CountBonusForConstant(FI); } } // Each argument passed in has a cost at both the caller and the callee // sides. Measurements show that each argument costs about the same as an // instruction. InlineCost -= (CS.arg_size() * InlineConstants::InstrCost); // If there is only one call of the function, and it has internal linkage, // make it almost guaranteed to be inlined. // if (Callee->hasLocalLinkage() && Callee->hasOneUse() && isDirectCall) InlineCost += InlineConstants::LastCallToStaticBonus; // Now that we have considered all of the factors that make the call site more // likely to be inlined, look at factors that make us not want to inline it. // If the instruction after the call, or if the normal destination of the // invoke is an unreachable instruction, the function is noreturn. As such, // there is little point in inlining this. if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { if (isa<UnreachableInst>(II->getNormalDest()->begin())) InlineCost += InlineConstants::NoreturnPenalty; } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall))) InlineCost += InlineConstants::NoreturnPenalty; // If this function uses the coldcc calling convention, prefer not to inline // it. if (Callee->getCallingConv() == CallingConv::Cold) InlineCost += InlineConstants::ColdccPenalty; // Calls usually take a long time, so they make the inlining gain smaller. InlineCost += CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty; // Look at the size of the callee. Each instruction counts as 5. InlineCost += CalleeFI->Metrics.NumInsts*InlineConstants::InstrCost; return llvm::InlineCost::get(InlineCost); }