#include <climits>
#include <map>
#include <vector>
+#include "llvm/ADT/DenseMap.h"
namespace llvm {
/// is used to estimate the code size cost of inlining it.
unsigned NumInsts, NumBlocks;
+ /// NumBBInsts - Keeps track of basic block code size estimates.
+ DenseMap<const BasicBlock *, unsigned> NumBBInsts;
+
/// NumCalls - Keep track of the number of calls to 'big' functions.
unsigned NumCalls;
/// CountCodeReductionForConstant - Figure out an approximation for how
/// many instructions will be constant folded if the specified value is
/// constant.
- unsigned CountCodeReductionForConstant(Value *V);
+ unsigned CountCodeReductionForConstant(Value *V, CodeMetrics &M);
/// CountCodeReductionForAlloca - Figure out an approximation of how much
/// smaller the function will be if it is inlined into a context where an
// instructions will be constant folded if the specified value is constant.
//
unsigned InlineCostAnalyzer::FunctionInfo::
- CountCodeReductionForConstant(Value *V) {
+CountCodeReductionForConstant(Value *V, CodeMetrics &Metrics) {
unsigned Reduction = 0;
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
if (isa<BranchInst>(*UI) || isa<SwitchInst>(*UI)) {
const unsigned NumSucc = TI.getNumSuccessors();
unsigned Instrs = 0;
for (unsigned I = 0; I != NumSucc; ++I)
- Instrs += TI.getSuccessor(I)->size();
+ Instrs += Metrics.NumBBInsts[TI.getSuccessor(I)];
// We don't know which blocks will be eliminated, so use the average size.
Reduction += InlineConstants::InstrCost*Instrs*(NumSucc-1)/NumSucc;
} else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
// And any other instructions that use it which become constants
// themselves.
- Reduction += CountCodeReductionForConstant(&Inst);
+ Reduction += CountCodeReductionForConstant(&Inst, Metrics);
}
}
/// from the specified block.
void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB) {
++NumBlocks;
-
+ unsigned NumInstsInThisBB = 0;
for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
II != E; ++II) {
if (isa<PHINode>(II)) continue; // PHI nodes don't count.
}
++NumInsts;
+ ++NumInstsInThisBB;
}
if (isa<ReturnInst>(BB->getTerminator()))
// function which is extremely undefined behavior.
if (isa<IndirectBrInst>(BB->getTerminator()))
NeverInline = true;
+
+ // Remember NumInsts for this BB.
+ NumBBInsts[BB] = NumInstsInThisBB;
}
/// analyzeFunction - Fill in the current structure with information gleaned
// code can be eliminated if one of the arguments is a constant.
ArgumentWeights.reserve(F->arg_size());
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
- ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I),
- CountCodeReductionForAlloca(I)));
+ ArgumentWeights.
+ push_back(ArgInfo(CountCodeReductionForConstant(I, Metrics),
+ CountCodeReductionForAlloca(I)));
}
// getInlineCost - The heuristic used to determine if we should inline the