Pull the implementation of the code metrics out of the inline cost

analysis implementation. The header was already separated. Also cleanup
all the comments in the header to follow a nice modern doxygen form.

There is still plenty of cruft here, but some of that will fall out in
subsequent refactorings and this was an easy step in the right
direction. No functionality changed here.

llvm-svn: 152898
This commit is contained in:
Chandler Carruth 2012-03-16 05:51:52 +00:00
parent d6a4b2cdb2
commit 3c256fbf2d
4 changed files with 209 additions and 192 deletions

View File

@ -1,4 +1,4 @@
//===- CodeMetrics.h - Measures the weight of a function---------*- C++ -*-===// //===- CodeMetrics.h - Code cost measurements -------------------*- C++ -*-===//
// //
// The LLVM Compiler Infrastructure // The LLVM Compiler Infrastructure
// //
@ -18,53 +18,58 @@
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
namespace llvm { namespace llvm {
class BasicBlock; class BasicBlock;
class Function; class Function;
class TargetData; class TargetData;
class Value; class Value;
// CodeMetrics - Calculate size and a few similar metrics for a set of /// \brief Check whether a call will lower to something small.
// basic blocks. ///
struct CodeMetrics { /// This tests checks whether calls to this function will lower to something
/// NeverInline - True if this callee should never be inlined into a /// significantly cheaper than a traditional call, often a single
/// caller. /// instruction.
// bool NeverInline; bool callIsSmall(const Function *F);
// True if this function contains a call to setjmp or other functions /// \brief Utility to calculate the size and a few similar metrics for a set
// with attribute "returns twice" without having the attribute itself. /// of basic blocks.
struct CodeMetrics {
/// \brief True if this function contains a call to setjmp or other functions
/// with attribute "returns twice" without having the attribute itself.
bool exposesReturnsTwice; bool exposesReturnsTwice;
// True if this function calls itself /// \brief True if this function calls itself.
bool isRecursive; bool isRecursive;
// True if this function contains one or more indirect branches /// \brief True if this function contains one or more indirect branches.
bool containsIndirectBr; bool containsIndirectBr;
/// usesDynamicAlloca - True if this function calls alloca (in the C sense). /// \brief True if this function calls alloca (in the C sense).
bool usesDynamicAlloca; bool usesDynamicAlloca;
/// NumInsts, NumBlocks - Keep track of how large each function is, which /// \brief Number of instructions in the analyzed blocks.
/// is used to estimate the code size cost of inlining it. unsigned NumInsts;
unsigned NumInsts, NumBlocks;
/// NumBBInsts - Keeps track of basic block code size estimates. /// \brief Number of analyzed blocks.
unsigned NumBlocks;
/// \brief Keeps track of basic block code size estimates.
DenseMap<const BasicBlock *, unsigned> NumBBInsts; DenseMap<const BasicBlock *, unsigned> NumBBInsts;
/// NumCalls - Keep track of the number of calls to 'big' functions. /// \brief Keep track of the number of calls to 'big' functions.
unsigned NumCalls; unsigned NumCalls;
/// NumInlineCandidates - Keep track of the number of calls to internal /// \brief The number of calls to internal functions with a single caller.
/// functions with only a single caller. These are likely targets for ///
/// future inlining, likely exposed by interleaved devirtualization. /// These are likely targets for future inlining, likely exposed by
/// interleaved devirtualization.
unsigned NumInlineCandidates; unsigned NumInlineCandidates;
/// NumVectorInsts - Keep track of how many instructions produce vector /// \brief How many instructions produce vector values.
/// values. The inliner is being more aggressive with inlining vector ///
/// kernels. /// The inliner is more aggressive with inlining vector kernels.
unsigned NumVectorInsts; unsigned NumVectorInsts;
/// NumRets - Keep track of how many Ret instructions the block contains. /// \brief How many 'ret' instructions the blocks contain.
unsigned NumRets; unsigned NumRets;
CodeMetrics() : exposesReturnsTwice(false), isRecursive(false), CodeMetrics() : exposesReturnsTwice(false), isRecursive(false),
@ -73,18 +78,11 @@ namespace llvm {
NumInlineCandidates(0), NumVectorInsts(0), NumInlineCandidates(0), NumVectorInsts(0),
NumRets(0) {} NumRets(0) {}
/// analyzeBasicBlock - Add information about the specified basic block /// \brief Add information about a block to the current state.
/// to the current structure.
void analyzeBasicBlock(const BasicBlock *BB, const TargetData *TD = 0); void analyzeBasicBlock(const BasicBlock *BB, const TargetData *TD = 0);
/// analyzeFunction - Add information about the specified function /// \brief Add information about a function to the current state.
/// to the current structure.
void analyzeFunction(Function *F, const TargetData *TD = 0); void analyzeFunction(Function *F, const TargetData *TD = 0);
/// CountBonusForConstant - Figure out an approximation for how much
/// per-call performance boost we can expect if the specified value is
/// constant.
unsigned CountBonusForConstant(Value *V);
}; };
} }

View File

@ -10,6 +10,7 @@ add_llvm_library(LLVMAnalysis
BranchProbabilityInfo.cpp BranchProbabilityInfo.cpp
CFGPrinter.cpp CFGPrinter.cpp
CaptureTracking.cpp CaptureTracking.cpp
CodeMetrics.cpp
ConstantFolding.cpp ConstantFolding.cpp
DIBuilder.cpp DIBuilder.cpp
DbgInfoPrinter.cpp DbgInfoPrinter.cpp

View File

@ -0,0 +1,176 @@
//===- CodeMetrics.cpp - Code cost measurements ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements code cost measurement utilities.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CodeMetrics.h"
#include "llvm/Function.h"
#include "llvm/Support/CallSite.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Target/TargetData.h"
using namespace llvm;
/// callIsSmall - If a call is likely to lower to a single target instruction,
/// or is otherwise deemed small return true.
/// TODO: Perhaps calls like memcpy, strcpy, etc?
bool llvm::callIsSmall(const Function *F) {
if (!F) return false;
if (F->hasLocalLinkage()) return false;
if (!F->hasName()) return false;
StringRef Name = F->getName();
// These will all likely lower to a single selection DAG node.
if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
Name == "fabs" || Name == "fabsf" || Name == "fabsl" ||
Name == "sin" || Name == "sinf" || Name == "sinl" ||
Name == "cos" || Name == "cosf" || Name == "cosl" ||
Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl" )
return true;
// These are all likely to be optimized into something smaller.
if (Name == "pow" || Name == "powf" || Name == "powl" ||
Name == "exp2" || Name == "exp2l" || Name == "exp2f" ||
Name == "floor" || Name == "floorf" || Name == "ceil" ||
Name == "round" || Name == "ffs" || Name == "ffsl" ||
Name == "abs" || Name == "labs" || Name == "llabs")
return true;
return false;
}
/// analyzeBasicBlock - Fill in the current structure with information gleaned
/// from the specified block.
void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
const TargetData *TD) {
++NumBlocks;
unsigned NumInstsBeforeThisBB = NumInsts;
for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
II != E; ++II) {
if (isa<PHINode>(II)) continue; // PHI nodes don't count.
// Special handling for calls.
if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
if (const IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(II)) {
switch (IntrinsicI->getIntrinsicID()) {
default: break;
case Intrinsic::dbg_declare:
case Intrinsic::dbg_value:
case Intrinsic::invariant_start:
case Intrinsic::invariant_end:
case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end:
case Intrinsic::objectsize:
case Intrinsic::ptr_annotation:
case Intrinsic::var_annotation:
// These intrinsics don't count as size.
continue;
}
}
ImmutableCallSite CS(cast<Instruction>(II));
if (const Function *F = CS.getCalledFunction()) {
// If a function is both internal and has a single use, then it is
// extremely likely to get inlined in the future (it was probably
// exposed by an interleaved devirtualization pass).
if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse())
++NumInlineCandidates;
// If this call is to function itself, then the function is recursive.
// Inlining it into other functions is a bad idea, because this is
// basically just a form of loop peeling, and our metrics aren't useful
// for that case.
if (F == BB->getParent())
isRecursive = true;
}
if (!isa<IntrinsicInst>(II) && !callIsSmall(CS.getCalledFunction())) {
// Each argument to a call takes on average one instruction to set up.
NumInsts += CS.arg_size();
// We don't want inline asm to count as a call - that would prevent loop
// unrolling. The argument setup cost is still real, though.
if (!isa<InlineAsm>(CS.getCalledValue()))
++NumCalls;
}
}
if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
if (!AI->isStaticAlloca())
this->usesDynamicAlloca = true;
}
if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy())
++NumVectorInsts;
if (const CastInst *CI = dyn_cast<CastInst>(II)) {
// Noop casts, including ptr <-> int, don't count.
if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) ||
isa<PtrToIntInst>(CI))
continue;
// trunc to a native type is free (assuming the target has compare and
// shift-right of the same width).
if (isa<TruncInst>(CI) && TD &&
TD->isLegalInteger(TD->getTypeSizeInBits(CI->getType())))
continue;
// Result of a cmp instruction is often extended (to be used by other
// cmp instructions, logical or return instructions). These are usually
// nop on most sane targets.
if (isa<CmpInst>(CI->getOperand(0)))
continue;
} else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(II)){
// If a GEP has all constant indices, it will probably be folded with
// a load/store.
if (GEPI->hasAllConstantIndices())
continue;
}
++NumInsts;
}
if (isa<ReturnInst>(BB->getTerminator()))
++NumRets;
// We never want to inline functions that contain an indirectbr. This is
// incorrect because all the blockaddress's (in static global initializers
// for example) would be referring to the original function, and this indirect
// jump would jump from the inlined copy of the function into the original
// function which is extremely undefined behavior.
// FIXME: This logic isn't really right; we can safely inline functions
// with indirectbr's as long as no other function or global references the
// blockaddress of a block within the current function. And as a QOI issue,
// if someone is using a blockaddress without an indirectbr, and that
// reference somehow ends up in another function or global, we probably
// don't want to inline this function.
if (isa<IndirectBrInst>(BB->getTerminator()))
containsIndirectBr = true;
// Remember NumInsts for this BB.
NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
}
void CodeMetrics::analyzeFunction(Function *F, const TargetData *TD) {
// If this function contains a call that "returns twice" (e.g., setjmp or
// _setjmp) and it isn't marked with "returns twice" itself, never inline it.
// This is a hack because we depend on the user marking their local variables
// as volatile if they are live across a setjmp call, and they probably
// won't do this in callers.
exposesReturnsTwice = F->callsFunctionThatReturnsTwice() &&
!F->hasFnAttr(Attribute::ReturnsTwice);
// Look at the size of the callee.
for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
analyzeBasicBlock(&*BB, TD);
}

View File

@ -20,148 +20,6 @@
using namespace llvm; using namespace llvm;
/// callIsSmall - If a call is likely to lower to a single target instruction,
/// or is otherwise deemed small return true.
/// TODO: Perhaps calls like memcpy, strcpy, etc?
bool llvm::callIsSmall(const Function *F) {
if (!F) return false;
if (F->hasLocalLinkage()) return false;
if (!F->hasName()) return false;
StringRef Name = F->getName();
// These will all likely lower to a single selection DAG node.
if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
Name == "fabs" || Name == "fabsf" || Name == "fabsl" ||
Name == "sin" || Name == "sinf" || Name == "sinl" ||
Name == "cos" || Name == "cosf" || Name == "cosl" ||
Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl" )
return true;
// These are all likely to be optimized into something smaller.
if (Name == "pow" || Name == "powf" || Name == "powl" ||
Name == "exp2" || Name == "exp2l" || Name == "exp2f" ||
Name == "floor" || Name == "floorf" || Name == "ceil" ||
Name == "round" || Name == "ffs" || Name == "ffsl" ||
Name == "abs" || Name == "labs" || Name == "llabs")
return true;
return false;
}
/// analyzeBasicBlock - Fill in the current structure with information gleaned
/// from the specified block.
void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
const TargetData *TD) {
++NumBlocks;
unsigned NumInstsBeforeThisBB = NumInsts;
for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
II != E; ++II) {
if (isa<PHINode>(II)) continue; // PHI nodes don't count.
// Special handling for calls.
if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
if (const IntrinsicInst *IntrinsicI = dyn_cast<IntrinsicInst>(II)) {
switch (IntrinsicI->getIntrinsicID()) {
default: break;
case Intrinsic::dbg_declare:
case Intrinsic::dbg_value:
case Intrinsic::invariant_start:
case Intrinsic::invariant_end:
case Intrinsic::lifetime_start:
case Intrinsic::lifetime_end:
case Intrinsic::objectsize:
case Intrinsic::ptr_annotation:
case Intrinsic::var_annotation:
// These intrinsics don't count as size.
continue;
}
}
ImmutableCallSite CS(cast<Instruction>(II));
if (const Function *F = CS.getCalledFunction()) {
// If a function is both internal and has a single use, then it is
// extremely likely to get inlined in the future (it was probably
// exposed by an interleaved devirtualization pass).
if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse())
++NumInlineCandidates;
// If this call is to function itself, then the function is recursive.
// Inlining it into other functions is a bad idea, because this is
// basically just a form of loop peeling, and our metrics aren't useful
// for that case.
if (F == BB->getParent())
isRecursive = true;
}
if (!isa<IntrinsicInst>(II) && !callIsSmall(CS.getCalledFunction())) {
// Each argument to a call takes on average one instruction to set up.
NumInsts += CS.arg_size();
// We don't want inline asm to count as a call - that would prevent loop
// unrolling. The argument setup cost is still real, though.
if (!isa<InlineAsm>(CS.getCalledValue()))
++NumCalls;
}
}
if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
if (!AI->isStaticAlloca())
this->usesDynamicAlloca = true;
}
if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy())
++NumVectorInsts;
if (const CastInst *CI = dyn_cast<CastInst>(II)) {
// Noop casts, including ptr <-> int, don't count.
if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) ||
isa<PtrToIntInst>(CI))
continue;
// trunc to a native type is free (assuming the target has compare and
// shift-right of the same width).
if (isa<TruncInst>(CI) && TD &&
TD->isLegalInteger(TD->getTypeSizeInBits(CI->getType())))
continue;
// Result of a cmp instruction is often extended (to be used by other
// cmp instructions, logical or return instructions). These are usually
// nop on most sane targets.
if (isa<CmpInst>(CI->getOperand(0)))
continue;
} else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(II)){
// If a GEP has all constant indices, it will probably be folded with
// a load/store.
if (GEPI->hasAllConstantIndices())
continue;
}
++NumInsts;
}
if (isa<ReturnInst>(BB->getTerminator()))
++NumRets;
// We never want to inline functions that contain an indirectbr. This is
// incorrect because all the blockaddress's (in static global initializers
// for example) would be referring to the original function, and this indirect
// jump would jump from the inlined copy of the function into the original
// function which is extremely undefined behavior.
// FIXME: This logic isn't really right; we can safely inline functions
// with indirectbr's as long as no other function or global references the
// blockaddress of a block within the current function. And as a QOI issue,
// if someone is using a blockaddress without an indirectbr, and that
// reference somehow ends up in another function or global, we probably
// don't want to inline this function.
if (isa<IndirectBrInst>(BB->getTerminator()))
containsIndirectBr = true;
// Remember NumInsts for this BB.
NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
}
unsigned InlineCostAnalyzer::FunctionInfo::countCodeReductionForConstant( unsigned InlineCostAnalyzer::FunctionInfo::countCodeReductionForConstant(
const CodeMetrics &Metrics, Value *V) { const CodeMetrics &Metrics, Value *V) {
unsigned Reduction = 0; unsigned Reduction = 0;
@ -444,22 +302,6 @@ void InlineCostAnalyzer::FunctionInfo::countCodeReductionForPointerPair(
} while (!Worklist.empty()); } while (!Worklist.empty());
} }
/// analyzeFunction - Fill in the current structure with information gleaned
/// from the specified function.
void CodeMetrics::analyzeFunction(Function *F, const TargetData *TD) {
// If this function contains a call that "returns twice" (e.g., setjmp or
// _setjmp) and it isn't marked with "returns twice" itself, never inline it.
// This is a hack because we depend on the user marking their local variables
// as volatile if they are live across a setjmp call, and they probably
// won't do this in callers.
exposesReturnsTwice = F->callsFunctionThatReturnsTwice() &&
!F->hasFnAttr(Attribute::ReturnsTwice);
// Look at the size of the callee.
for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
analyzeBasicBlock(&*BB, TD);
}
/// analyzeFunction - Fill in the current structure with information gleaned /// analyzeFunction - Fill in the current structure with information gleaned
/// from the specified function. /// from the specified function.
void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F, void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F,