blob: 073234bf7cfad6ff96f84fd6844f6bef0941ae6a [file] [log] [blame]
Chandler Carruth9b081d92012-03-16 05:51:52 +00001//===- CodeMetrics.cpp - Code cost measurements ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements code cost measurement utilities.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/CodeMetrics.h"
Chandler Carrutha5157e62013-01-21 13:04:33 +000015#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/IntrinsicInst.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/Support/CallSite.h"
Chandler Carruth9b081d92012-03-16 05:51:52 +000020
21using namespace llvm;
22
23/// callIsSmall - If a call is likely to lower to a single target instruction,
24/// or is otherwise deemed small return true.
25/// TODO: Perhaps calls like memcpy, strcpy, etc?
Chandler Carruthd5003ca2012-05-04 00:58:03 +000026bool llvm::callIsSmall(ImmutableCallSite CS) {
27 if (isa<IntrinsicInst>(CS.getInstruction()))
28 return true;
29
30 const Function *F = CS.getCalledFunction();
Chandler Carruth9b081d92012-03-16 05:51:52 +000031 if (!F) return false;
32
33 if (F->hasLocalLinkage()) return false;
34
35 if (!F->hasName()) return false;
36
37 StringRef Name = F->getName();
38
39 // These will all likely lower to a single selection DAG node.
40 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
41 Name == "fabs" || Name == "fabsf" || Name == "fabsl" ||
42 Name == "sin" || Name == "sinf" || Name == "sinl" ||
43 Name == "cos" || Name == "cosf" || Name == "cosl" ||
44 Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl" )
45 return true;
46
47 // These are all likely to be optimized into something smaller.
48 if (Name == "pow" || Name == "powf" || Name == "powl" ||
49 Name == "exp2" || Name == "exp2l" || Name == "exp2f" ||
50 Name == "floor" || Name == "floorf" || Name == "ceil" ||
51 Name == "round" || Name == "ffs" || Name == "ffsl" ||
52 Name == "abs" || Name == "labs" || Name == "llabs")
53 return true;
54
55 return false;
56}
57
58/// analyzeBasicBlock - Fill in the current structure with information gleaned
59/// from the specified block.
60void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
Chandler Carrutha5157e62013-01-21 13:04:33 +000061 const TargetTransformInfo &TTI) {
Chandler Carruth9b081d92012-03-16 05:51:52 +000062 ++NumBlocks;
63 unsigned NumInstsBeforeThisBB = NumInsts;
64 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
65 II != E; ++II) {
Chandler Carrutha5157e62013-01-21 13:04:33 +000066 if (TargetTransformInfo::TCC_Free == TTI.getUserCost(&*II))
Chandler Carruthf2286b02012-03-31 12:42:41 +000067 continue;
Chandler Carruth9b081d92012-03-16 05:51:52 +000068
69 // Special handling for calls.
70 if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
Chandler Carruth9b081d92012-03-16 05:51:52 +000071 ImmutableCallSite CS(cast<Instruction>(II));
72
73 if (const Function *F = CS.getCalledFunction()) {
74 // If a function is both internal and has a single use, then it is
75 // extremely likely to get inlined in the future (it was probably
76 // exposed by an interleaved devirtualization pass).
77 if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse())
78 ++NumInlineCandidates;
79
80 // If this call is to function itself, then the function is recursive.
81 // Inlining it into other functions is a bad idea, because this is
82 // basically just a form of loop peeling, and our metrics aren't useful
83 // for that case.
84 if (F == BB->getParent())
85 isRecursive = true;
86 }
87
Chandler Carruthd5003ca2012-05-04 00:58:03 +000088 if (!callIsSmall(CS)) {
Chandler Carruth9b081d92012-03-16 05:51:52 +000089 // Each argument to a call takes on average one instruction to set up.
90 NumInsts += CS.arg_size();
91
92 // We don't want inline asm to count as a call - that would prevent loop
93 // unrolling. The argument setup cost is still real, though.
94 if (!isa<InlineAsm>(CS.getCalledValue()))
95 ++NumCalls;
96 }
97 }
98
99 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
100 if (!AI->isStaticAlloca())
101 this->usesDynamicAlloca = true;
102 }
103
104 if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy())
105 ++NumVectorInsts;
106
James Molloy67ae1352012-12-20 16:04:27 +0000107 if (const CallInst *CI = dyn_cast<CallInst>(II))
108 if (CI->hasFnAttr(Attribute::NoDuplicate))
109 notDuplicatable = true;
110
111 if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II))
112 if (InvI->hasFnAttr(Attribute::NoDuplicate))
113 notDuplicatable = true;
114
Chandler Carruth9b081d92012-03-16 05:51:52 +0000115 ++NumInsts;
116 }
117
118 if (isa<ReturnInst>(BB->getTerminator()))
119 ++NumRets;
120
121 // We never want to inline functions that contain an indirectbr. This is
122 // incorrect because all the blockaddress's (in static global initializers
123 // for example) would be referring to the original function, and this indirect
124 // jump would jump from the inlined copy of the function into the original
125 // function which is extremely undefined behavior.
126 // FIXME: This logic isn't really right; we can safely inline functions
127 // with indirectbr's as long as no other function or global references the
128 // blockaddress of a block within the current function. And as a QOI issue,
129 // if someone is using a blockaddress without an indirectbr, and that
130 // reference somehow ends up in another function or global, we probably
131 // don't want to inline this function.
James Molloy67ae1352012-12-20 16:04:27 +0000132 notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
Chandler Carruth9b081d92012-03-16 05:51:52 +0000133
134 // Remember NumInsts for this BB.
135 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
136}