-//===- InlineCoast.cpp - Cost analysis for inliner ------------------------===//
+//===- InlineCost.cpp - Cost analysis for inliner -------------------------===//
//
// The LLVM Compiler Infrastructure
//
//
//===----------------------------------------------------------------------===//
-
#include "llvm/Transforms/Utils/InlineCost.h"
#include "llvm/Support/CallSite.h"
#include "llvm/CallingConv.h"
#include "llvm/IntrinsicInst.h"
-
+#include "llvm/ADT/SmallPtrSet.h"
using namespace llvm;
// CountCodeReductionForConstant - Figure out an approximation for how many
// Figure out if this instruction will be removed due to simple constant
// propagation.
Instruction &Inst = cast<Instruction>(**UI);
+
+ // We can't constant propagate instructions which have effects or
+ // read memory.
+ //
+ // FIXME: It would be nice to capture the fact that a load from a
+ // pointer-to-constant-global is actually a *really* good thing to zap.
+ // Unfortunately, we don't know the pointer that may get propagated here,
+ // so we can't make this decision.
+ if (Inst.mayReadFromMemory() || Inst.mayHaveSideEffects() ||
+ isa<AllocationInst>(Inst))
+ continue;
+
bool AllOperandsConstant = true;
for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
Reduction += 10;
else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
// If the GEP has variable indices, we won't be able to do much with it.
- for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end();
- I != E; ++I)
- if (!isa<Constant>(*I)) return 0;
- Reduction += CountCodeReductionForAlloca(GEP)+15;
+ if (!GEP->hasAllConstantIndices())
+ Reduction += CountCodeReductionForAlloca(GEP)+15;
} else {
// If there is some other strange instruction, we're not going to be able
// to do much if we inline this.
/// analyzeFunction - Fill in the current structure with information gleaned
/// from the specified function.
void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F) {
- unsigned NumInsts = 0, NumBlocks = 0, NumVectorInsts = 0;
+ unsigned NumInsts = 0, NumBlocks = 0, NumVectorInsts = 0, NumRets = 0;
// Look at the size of the callee. Each basic block counts as 20 units, and
// each instruction counts as 5.
// probably won't do this in callers.
if (Function *F = CS.getCalledFunction())
if (F->isDeclaration() &&
- (F->isName("setjmp") || F->isName("_setjmp"))) {
+ (F->getName() == "setjmp" || F->getName() == "_setjmp")) {
NeverInline = true;
return;
}
-
+
// Calls often compile into many machine instructions. Bump up their
// cost to reflect this.
if (!isa<IntrinsicInst>(II))
- NumInsts += 5;
+ NumInsts += InlineConstants::CallPenalty;
}
+ // These, too, are calls.
+ if (isa<MallocInst>(II) || isa<FreeInst>(II))
+ NumInsts += InlineConstants::CallPenalty;
+
+ if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
+ if (!AI->isStaticAlloca())
+ this->usesDynamicAlloca = true;
+ }
+
if (isa<ExtractElementInst>(II) || isa<VectorType>(II->getType()))
++NumVectorInsts;
dyn_cast<GetElementPtrInst>(II)) {
// If a GEP has all constant indices, it will probably be folded with
// a load/store.
- bool AllConstant = true;
- for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
- if (!isa<ConstantInt>(GEPI->getOperand(i))) {
- AllConstant = false;
- break;
- }
- if (AllConstant) continue;
+ if (GEPI->hasAllConstantIndices())
+ continue;
}
+
+ if (isa<ReturnInst>(II))
+ ++NumRets;
++NumInsts;
}
++NumBlocks;
}
+ // A function with exactly one return has it removed during the inlining
+ // process (see InlineFunction), so don't count it.
+ if (NumRets==1)
+ --NumInsts;
+
this->NumBlocks = NumBlocks;
this->NumInsts = NumInsts;
this->NumVectorInsts = NumVectorInsts;
SmallPtrSet<const Function *, 16> &NeverInline) {
Instruction *TheCall = CS.getInstruction();
Function *Callee = CS.getCalledFunction();
- const Function *Caller = TheCall->getParent()->getParent();
-
- // Don't inline a directly recursive call.
- if (Caller == Callee ||
- // Don't inline functions which can be redefined at link-time to mean
- // something else.
- // FIXME: We allow link-once linkage since in practice all versions of
- // the function have the same body (C++ ODR) - but the LLVM definition
- // of LinkOnceLinkage doesn't require this.
- (Callee->mayBeOverridden() && !Callee->hasLinkOnceLinkage()
- ) ||
-
- // Don't inline functions marked noinline.
- NeverInline.count(Callee))
+ 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.
+ if (Callee->mayBeOverridden() ||
+ Callee->hasFnAttr(Attribute::NoInline) || NeverInline.count(Callee))
return llvm::InlineCost::getNever();
// InlineCost - This value measures how good of an inline candidate this call
// If there is only one call of the function, and it has internal linkage,
// make it almost guaranteed to be inlined.
//
- if (Callee->hasInternalLinkage() && Callee->hasOneUse())
- InlineCost -= 15000;
+ if (Callee->hasLocalLinkage() && Callee->hasOneUse())
+ InlineCost += InlineConstants::LastCallToStaticBonus;
// If this function uses the coldcc calling convention, prefer not to inline
// it.
if (Callee->getCallingConv() == CallingConv::Cold)
- InlineCost += 2000;
+ InlineCost += InlineConstants::ColdccPenalty;
// 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 += 10000;
+ InlineCost += InlineConstants::NoreturnPenalty;
} else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall)))
- InlineCost += 10000;
+ InlineCost += InlineConstants::NoreturnPenalty;
// Get information about the callee...
FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
// If we haven't calculated this information yet, do so now.
if (CalleeFI.NumBlocks == 0)
CalleeFI.analyzeFunction(Callee);
-
+
// If we should never inline this, return a huge cost.
if (CalleeFI.NeverInline)
return InlineCost::getNever();
if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline))
return InlineCost::getAlways();
+ if (CalleeFI.usesDynamicAlloca) {
+ // Get infomation about the caller...
+ FunctionInfo &CallerFI = CachedFunctionInfo[Caller];
+
+ // If we haven't calculated this information yet, do so now.
+ if (CallerFI.NumBlocks == 0)
+ CallerFI.analyzeFunction(Caller);
+
+ // 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.usesDynamicAlloca)
+ return InlineCost::getNever();
+ }
+
// 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
// likely to be inlined, look at factors that make us not want to inline it.
// Don't inline into something too big, which would make it bigger.
+ // "size" here is the number of basic blocks, not instructions.
//
InlineCost += Caller->size()/15;