Chandler Carruth | 3c256fb | 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 | |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/AssumptionTracker.h" |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/CodeMetrics.h" |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/TargetTransformInfo.h" |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 19 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DataLayout.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/IntrinsicInst.h" |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | |
| 25 | #define DEBUG_TYPE "code-metrics" |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 29 | static void completeEphemeralValues(SmallVector<const Value *, 16> &WorkSet, |
| 30 | SmallPtrSetImpl<const Value*> &EphValues) { |
| 31 | SmallPtrSet<const Value *, 32> Visited; |
| 32 | |
| 33 | // Make sure that all of the items in WorkSet are in our EphValues set. |
| 34 | EphValues.insert(WorkSet.begin(), WorkSet.end()); |
| 35 | |
| 36 | // Note: We don't speculate PHIs here, so we'll miss instruction chains kept |
| 37 | // alive only by ephemeral values. |
| 38 | |
| 39 | while (!WorkSet.empty()) { |
Hal Finkel | 8683d2b | 2014-10-15 17:34:48 +0000 | [diff] [blame] | 40 | const Value *V = WorkSet.front(); |
| 41 | WorkSet.erase(WorkSet.begin()); |
| 42 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame^] | 43 | if (!Visited.insert(V).second) |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 44 | continue; |
| 45 | |
| 46 | // If all uses of this value are ephemeral, then so is this value. |
| 47 | bool FoundNEUse = false; |
| 48 | for (const User *I : V->users()) |
| 49 | if (!EphValues.count(I)) { |
| 50 | FoundNEUse = true; |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | if (FoundNEUse) |
| 55 | continue; |
| 56 | |
| 57 | EphValues.insert(V); |
| 58 | DEBUG(dbgs() << "Ephemeral Value: " << *V << "\n"); |
| 59 | |
| 60 | if (const User *U = dyn_cast<User>(V)) |
| 61 | for (const Value *J : U->operands()) { |
| 62 | if (isSafeToSpeculativelyExecute(J)) |
| 63 | WorkSet.push_back(J); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Find all ephemeral values. |
| 69 | void CodeMetrics::collectEphemeralValues(const Loop *L, AssumptionTracker *AT, |
| 70 | SmallPtrSetImpl<const Value*> &EphValues) { |
| 71 | SmallVector<const Value *, 16> WorkSet; |
| 72 | |
| 73 | for (auto &I : AT->assumptions(L->getHeader()->getParent())) { |
| 74 | // Filter out call sites outside of the loop so we don't to a function's |
| 75 | // worth of work for each of its loops (and, in the common case, ephemeral |
| 76 | // values in the loop are likely due to @llvm.assume calls in the loop). |
| 77 | if (!L->contains(I->getParent())) |
| 78 | continue; |
| 79 | |
| 80 | WorkSet.push_back(I); |
| 81 | } |
| 82 | |
| 83 | completeEphemeralValues(WorkSet, EphValues); |
| 84 | } |
| 85 | |
| 86 | void CodeMetrics::collectEphemeralValues(const Function *F, AssumptionTracker *AT, |
| 87 | SmallPtrSetImpl<const Value*> &EphValues) { |
| 88 | SmallVector<const Value *, 16> WorkSet; |
| 89 | |
| 90 | for (auto &I : AT->assumptions(const_cast<Function*>(F))) |
| 91 | WorkSet.push_back(I); |
| 92 | |
| 93 | completeEphemeralValues(WorkSet, EphValues); |
| 94 | } |
| 95 | |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 96 | /// analyzeBasicBlock - Fill in the current structure with information gleaned |
| 97 | /// from the specified block. |
| 98 | void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 99 | const TargetTransformInfo &TTI, |
| 100 | SmallPtrSetImpl<const Value*> &EphValues) { |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 101 | ++NumBlocks; |
| 102 | unsigned NumInstsBeforeThisBB = NumInsts; |
| 103 | for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); |
| 104 | II != E; ++II) { |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 105 | // Skip ephemeral values. |
| 106 | if (EphValues.count(II)) |
| 107 | continue; |
| 108 | |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 109 | // Special handling for calls. |
| 110 | if (isa<CallInst>(II) || isa<InvokeInst>(II)) { |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 111 | ImmutableCallSite CS(cast<Instruction>(II)); |
| 112 | |
| 113 | if (const Function *F = CS.getCalledFunction()) { |
| 114 | // If a function is both internal and has a single use, then it is |
| 115 | // extremely likely to get inlined in the future (it was probably |
| 116 | // exposed by an interleaved devirtualization pass). |
| 117 | if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse()) |
| 118 | ++NumInlineCandidates; |
| 119 | |
| 120 | // If this call is to function itself, then the function is recursive. |
| 121 | // Inlining it into other functions is a bad idea, because this is |
| 122 | // basically just a form of loop peeling, and our metrics aren't useful |
| 123 | // for that case. |
| 124 | if (F == BB->getParent()) |
| 125 | isRecursive = true; |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 126 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 127 | if (TTI.isLoweredToCall(F)) |
| 128 | ++NumCalls; |
| 129 | } else { |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 130 | // We don't want inline asm to count as a call - that would prevent loop |
| 131 | // unrolling. The argument setup cost is still real, though. |
| 132 | if (!isa<InlineAsm>(CS.getCalledValue())) |
| 133 | ++NumCalls; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
| 138 | if (!AI->isStaticAlloca()) |
| 139 | this->usesDynamicAlloca = true; |
| 140 | } |
| 141 | |
| 142 | if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy()) |
| 143 | ++NumVectorInsts; |
| 144 | |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 145 | if (const CallInst *CI = dyn_cast<CallInst>(II)) |
Eli Bendersky | 576ef3c | 2014-03-17 16:19:07 +0000 | [diff] [blame] | 146 | if (CI->cannotDuplicate()) |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 147 | notDuplicatable = true; |
| 148 | |
| 149 | if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II)) |
Eli Bendersky | 576ef3c | 2014-03-17 16:19:07 +0000 | [diff] [blame] | 150 | if (InvI->cannotDuplicate()) |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 151 | notDuplicatable = true; |
| 152 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 153 | NumInsts += TTI.getUserCost(&*II); |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (isa<ReturnInst>(BB->getTerminator())) |
| 157 | ++NumRets; |
| 158 | |
| 159 | // We never want to inline functions that contain an indirectbr. This is |
| 160 | // incorrect because all the blockaddress's (in static global initializers |
| 161 | // for example) would be referring to the original function, and this indirect |
| 162 | // jump would jump from the inlined copy of the function into the original |
| 163 | // function which is extremely undefined behavior. |
| 164 | // FIXME: This logic isn't really right; we can safely inline functions |
| 165 | // with indirectbr's as long as no other function or global references the |
| 166 | // blockaddress of a block within the current function. And as a QOI issue, |
| 167 | // if someone is using a blockaddress without an indirectbr, and that |
| 168 | // reference somehow ends up in another function or global, we probably |
| 169 | // don't want to inline this function. |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 170 | notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator()); |
Chandler Carruth | 3c256fb | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 171 | |
| 172 | // Remember NumInsts for this BB. |
| 173 | NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; |
| 174 | } |