LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CodeMetrics.cpp
Go to the documentation of this file.
1 //===- 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 
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/Support/CallSite.h"
20 
21 using namespace llvm;
22 
23 /// analyzeBasicBlock - Fill in the current structure with information gleaned
24 /// from the specified block.
26  const TargetTransformInfo &TTI) {
27  ++NumBlocks;
28  unsigned NumInstsBeforeThisBB = NumInsts;
29  for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
30  II != E; ++II) {
31  // Special handling for calls.
32  if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
33  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())
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;
48 
49  if (TTI.isLoweredToCall(F))
50  ++NumCalls;
51  } else {
52  // 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())
66 
67  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 
75  NumInsts += TTI.getUserCost(&*II);
76  }
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.
92  notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
93 
94  // Remember NumInsts for this BB.
95  NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
96 }
bool isRecursive
True if this function calls itself.
Definition: CodeMetrics.h:45
unsigned NumVectorInsts
How many instructions produce vector values.
Definition: CodeMetrics.h:77
unsigned NumCalls
Keep track of the number of calls to 'big' functions.
Definition: CodeMetrics.h:66
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:116
unsigned NumInlineCandidates
The number of calls to internal functions with a single caller.
Definition: CodeMetrics.h:72
F(f)
bool notDuplicatable
True if this function cannot be duplicated.
Definition: CodeMetrics.h:51
iterator begin()
Definition: BasicBlock.h:193
unsigned NumBlocks
Number of analyzed blocks.
Definition: CodeMetrics.h:60
void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI)
Add information about a block to the current state.
Definition: CodeMetrics.cpp:25
bool usesDynamicAlloca
True if this function calls alloca (in the C sense).
Definition: CodeMetrics.h:54
virtual unsigned getUserCost(const User *U) const
Estimate the cost of a given IR user when lowered.
ValTy * getCalledValue() const
Definition: CallSite.h:85
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
Call cannot be duplicated.
Definition: Attributes.h:83
DenseMap< const BasicBlock *, unsigned > NumBBInsts
Keeps track of basic block code size estimates.
Definition: CodeMetrics.h:63
iterator end()
Definition: BasicBlock.h:195
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:318
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
unsigned NumRets
How many 'ret' instructions the blocks contain.
Definition: CodeMetrics.h:80
bool isNoInline() const
Return true if the call should not be inlined.
Definition: CallSite.h:208
virtual bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
unsigned NumInsts
Number of instructions in the analyzed blocks.
Definition: CodeMetrics.h:57
FunTy * getCalledFunction() const
Definition: CallSite.h:93