summaryrefslogtreecommitdiff
path: root/lib/Analysis/CodeMetrics.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2012-05-04 00:58:03 +0000
committerChandler Carruth <chandlerc@gmail.com>2012-05-04 00:58:03 +0000
commitd5003cafd6fc86acaf8e09ef0ca1dc899da8850e (patch)
tree16436eec4ec4c6a95b3d9d05d07c64aacebb18f1 /lib/Analysis/CodeMetrics.cpp
parenta83a6d3725a9a488e2d10c1f2af46d9c6d82958d (diff)
downloadllvm-d5003cafd6fc86acaf8e09ef0ca1dc899da8850e.tar.gz
llvm-d5003cafd6fc86acaf8e09ef0ca1dc899da8850e.tar.bz2
llvm-d5003cafd6fc86acaf8e09ef0ca1dc899da8850e.tar.xz
A pile of long over-due refactorings here. There are some very, *very*
minor behavior changes with this, but nothing I have seen evidence of in the wild or expect to be meaningful. The real goal is unifying our logic and simplifying the interfaces. A summary of the changes follows: - Make 'callIsSmall' actually accept a callsite so it can handle intrinsics, and simplify callers appropriately. - Nuke a completely bogus declaration of 'callIsSmall' that was still lurking in InlineCost.h... No idea how this got missed. - Teach the 'isInstructionFree' about the various more intelligent 'free' heuristics that got added to the inline cost analysis during review and testing. This mostly surrounds int->ptr and ptr->int casts. - Switch most of the interesting parts of the inline cost analysis that were essentially computing 'is this instruction free?' to use the code metrics routine instead. This way we won't keep duplicating logic. All of this is motivated by the desire to allow other passes to compute a roughly equivalent 'cost' metric for a particular basic block as the inline cost analysis. Sadly, re-using the same analysis for both is really messy because only the actual inline cost analysis is ever going to go to the contortions required for simplification, SROA analysis, etc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156140 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/CodeMetrics.cpp')
-rw-r--r--lib/Analysis/CodeMetrics.cpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/lib/Analysis/CodeMetrics.cpp b/lib/Analysis/CodeMetrics.cpp
index 316e7bc934..acda34ba14 100644
--- a/lib/Analysis/CodeMetrics.cpp
+++ b/lib/Analysis/CodeMetrics.cpp
@@ -22,7 +22,11 @@ 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) {
+bool llvm::callIsSmall(ImmutableCallSite CS) {
+ if (isa<IntrinsicInst>(CS.getInstruction()))
+ return true;
+
+ const Function *F = CS.getCalledFunction();
if (!F) return false;
if (F->hasLocalLinkage()) return false;
@@ -79,8 +83,24 @@ bool llvm::isInstructionFree(const Instruction *I, const TargetData *TD) {
if (const CastInst *CI = dyn_cast<CastInst>(I)) {
// Noop casts, including ptr <-> int, don't count.
- if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) || isa<PtrToIntInst>(CI))
+ if (CI->isLosslessCast())
+ return true;
+
+ Value *Op = CI->getOperand(0);
+ // An inttoptr cast is free so long as the input is a legal integer type
+ // which doesn't contain values outside the range of a pointer.
+ if (isa<IntToPtrInst>(CI) && TD &&
+ TD->isLegalInteger(Op->getType()->getScalarSizeInBits()) &&
+ Op->getType()->getScalarSizeInBits() <= TD->getPointerSizeInBits())
return true;
+
+ // A ptrtoint cast is free so long as the result is large enough to store
+ // the pointer, and a legal integer type.
+ if (isa<PtrToIntInst>(CI) && TD &&
+ TD->isLegalInteger(Op->getType()->getScalarSizeInBits()) &&
+ Op->getType()->getScalarSizeInBits() >= TD->getPointerSizeInBits())
+ return true;
+
// trunc to a native type is free (assuming the target has compare and
// shift-right of the same width).
if (TD && isa<TruncInst>(CI) &&
@@ -126,7 +146,7 @@ void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
isRecursive = true;
}
- if (!isa<IntrinsicInst>(II) && !callIsSmall(CS.getCalledFunction())) {
+ if (!callIsSmall(CS)) {
// Each argument to a call takes on average one instruction to set up.
NumInsts += CS.arg_size();