Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 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 | |
| 14 | #include "llvm/Analysis/CodeMetrics.h" |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/Function.h" |
| 18 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CallSite.h" |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 23 | /// analyzeBasicBlock - Fill in the current structure with information gleaned |
| 24 | /// from the specified block. |
| 25 | void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 26 | const TargetTransformInfo &TTI) { |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 27 | ++NumBlocks; |
| 28 | unsigned NumInstsBeforeThisBB = NumInsts; |
| 29 | for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); |
| 30 | II != E; ++II) { |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 31 | // Special handling for calls. |
| 32 | if (isa<CallInst>(II) || isa<InvokeInst>(II)) { |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 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()) |
| 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 Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 48 | |
Chandler Carruth | 13086a6 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 49 | if (TTI.isLoweredToCall(F)) |
| 50 | ++NumCalls; |
| 51 | } else { |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 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()) |
| 65 | ++NumVectorInsts; |
| 66 | |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 13086a6 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 75 | NumInsts += TTI.getUserCost(&*II); |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 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. |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 92 | notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator()); |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 93 | |
| 94 | // Remember NumInsts for this BB. |
| 95 | NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; |
| 96 | } |