blob: 8cda01a24c0d4a3ea72df95b8732eeff42a50d63 [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
Chandler Carruth9b081d92012-03-16 05:51:52 +000023/// analyzeBasicBlock - Fill in the current structure with information gleaned
24/// from the specified block.
25void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
Chandler Carrutha5157e62013-01-21 13:04:33 +000026 const TargetTransformInfo &TTI) {
Chandler Carruth9b081d92012-03-16 05:51:52 +000027 ++NumBlocks;
28 unsigned NumInstsBeforeThisBB = NumInsts;
29 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
30 II != E; ++II) {
Chandler Carruth9b081d92012-03-16 05:51:52 +000031 // Special handling for calls.
32 if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
Chandler Carruth9b081d92012-03-16 05:51:52 +000033 ImmutableCallSite CS(cast<Instruction>(II));
34
35 if (const Function *F = CS.getCalledFunction()) {
36 // If a function is both internal and has a single use, then it is
37 // extremely likely to get inlined in the future (it was probably
38 // exposed by an interleaved devirtualization pass).
39 if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse())
40 ++NumInlineCandidates;
41
42 // If this call is to function itself, then the function is recursive.
43 // Inlining it into other functions is a bad idea, because this is
44 // basically just a form of loop peeling, and our metrics aren't useful
45 // for that case.
46 if (F == BB->getParent())
47 isRecursive = true;
Chandler Carruth9b081d92012-03-16 05:51:52 +000048
Chandler Carruth13086a62013-01-22 11:26:02 +000049 if (TTI.isLoweredToCall(F))
50 ++NumCalls;
51 } else {
Chandler Carruth9b081d92012-03-16 05:51:52 +000052 // We don't want inline asm to count as a call - that would prevent loop
53 // unrolling. The argument setup cost is still real, though.
54 if (!isa<InlineAsm>(CS.getCalledValue()))
55 ++NumCalls;
56 }
57 }
58
59 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
60 if (!AI->isStaticAlloca())
61 this->usesDynamicAlloca = true;
62 }
63
64 if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy())
65 ++NumVectorInsts;
66
James Molloy67ae1352012-12-20 16:04:27 +000067 if (const CallInst *CI = dyn_cast<CallInst>(II))
68 if (CI->hasFnAttr(Attribute::NoDuplicate))
69 notDuplicatable = true;
70
71 if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II))
72 if (InvI->hasFnAttr(Attribute::NoDuplicate))
73 notDuplicatable = true;
74
Chandler Carruth13086a62013-01-22 11:26:02 +000075 NumInsts += TTI.getUserCost(&*II);
Chandler Carruth9b081d92012-03-16 05:51:52 +000076 }
77
78 if (isa<ReturnInst>(BB->getTerminator()))
79 ++NumRets;
80
81 // We never want to inline functions that contain an indirectbr. This is
82 // incorrect because all the blockaddress's (in static global initializers
83 // for example) would be referring to the original function, and this indirect
84 // jump would jump from the inlined copy of the function into the original
85 // function which is extremely undefined behavior.
86 // FIXME: This logic isn't really right; we can safely inline functions
87 // with indirectbr's as long as no other function or global references the
88 // blockaddress of a block within the current function. And as a QOI issue,
89 // if someone is using a blockaddress without an indirectbr, and that
90 // reference somehow ends up in another function or global, we probably
91 // don't want to inline this function.
James Molloy67ae1352012-12-20 16:04:27 +000092 notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
Chandler Carruth9b081d92012-03-16 05:51:52 +000093
94 // Remember NumInsts for this BB.
95 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
96}