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