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 | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/AssumptionCache.h" |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/TargetTransformInfo.h" |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 4bbfbdf | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 19 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DataLayout.h" |
| 21 | #include "llvm/IR/Function.h" |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 1bfcd1f | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 24 | |
| 25 | #define DEBUG_TYPE "code-metrics" |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 29 | static void |
| 30 | appendSpeculatableOperands(const Value *V, |
| 31 | SmallPtrSetImpl<const Value *> &Visited, |
| 32 | SmallVectorImpl<const Value *> &Worklist) { |
| 33 | const User *U = dyn_cast<User>(V); |
| 34 | if (!U) |
| 35 | return; |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 36 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 37 | for (const Value *Operand : U->operands()) |
| 38 | if (Visited.insert(Operand).second) |
| 39 | if (isSafeToSpeculativelyExecute(Operand)) |
| 40 | Worklist.push_back(Operand); |
| 41 | } |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 42 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 43 | static void completeEphemeralValues(SmallPtrSetImpl<const Value *> &Visited, |
| 44 | SmallVectorImpl<const Value *> &Worklist, |
| 45 | SmallPtrSetImpl<const Value *> &EphValues) { |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 46 | // Note: We don't speculate PHIs here, so we'll miss instruction chains kept |
| 47 | // alive only by ephemeral values. |
| 48 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 49 | // Walk the worklist using an index but without caching the size so we can |
| 50 | // append more entries as we process the worklist. This forms a queue without |
| 51 | // quadratic behavior by just leaving processed nodes at the head of the |
| 52 | // worklist forever. |
| 53 | for (int i = 0; i < (int)Worklist.size(); ++i) { |
| 54 | const Value *V = Worklist[i]; |
Hal Finkel | 9819bcf | 2014-10-15 17:34:48 +0000 | [diff] [blame] | 55 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 56 | assert(Visited.count(V) && |
| 57 | "Failed to add a worklist entry to our visited set!"); |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 58 | |
| 59 | // If all uses of this value are ephemeral, then so is this value. |
David Majnemer | dc9c737 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 60 | if (!all_of(V->users(), [&](const User *U) { return EphValues.count(U); })) |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 61 | continue; |
| 62 | |
| 63 | EphValues.insert(V); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 64 | LLVM_DEBUG(dbgs() << "Ephemeral Value: " << *V << "\n"); |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 65 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 66 | // Append any more operands to consider. |
| 67 | appendSpeculatableOperands(V, Visited, Worklist); |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | // Find all ephemeral values. |
Chandler Carruth | 5a9cd4d | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 72 | void CodeMetrics::collectEphemeralValues( |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 73 | const Loop *L, AssumptionCache *AC, |
| 74 | SmallPtrSetImpl<const Value *> &EphValues) { |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 75 | SmallPtrSet<const Value *, 32> Visited; |
| 76 | SmallVector<const Value *, 16> Worklist; |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 77 | |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 78 | for (auto &AssumeVH : AC->assumptions()) { |
| 79 | if (!AssumeVH) |
| 80 | continue; |
| 81 | Instruction *I = cast<Instruction>(AssumeVH); |
| 82 | |
| 83 | // Filter out call sites outside of the loop so we don't do a function's |
| 84 | // worth of work for each of its loops (and, in the common case, ephemeral |
| 85 | // values in the loop are likely due to @llvm.assume calls in the loop). |
| 86 | if (!L->contains(I->getParent())) |
| 87 | continue; |
| 88 | |
| 89 | if (EphValues.insert(I).second) |
| 90 | appendSpeculatableOperands(I, Visited, Worklist); |
| 91 | } |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 92 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 93 | completeEphemeralValues(Visited, Worklist, EphValues); |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chandler Carruth | 5a9cd4d | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 96 | void CodeMetrics::collectEphemeralValues( |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 97 | const Function *F, AssumptionCache *AC, |
| 98 | SmallPtrSetImpl<const Value *> &EphValues) { |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 99 | SmallPtrSet<const Value *, 32> Visited; |
| 100 | SmallVector<const Value *, 16> Worklist; |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 101 | |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 102 | for (auto &AssumeVH : AC->assumptions()) { |
| 103 | if (!AssumeVH) |
| 104 | continue; |
| 105 | Instruction *I = cast<Instruction>(AssumeVH); |
| 106 | assert(I->getParent()->getParent() == F && |
| 107 | "Found assumption for the wrong function!"); |
| 108 | |
| 109 | if (EphValues.insert(I).second) |
| 110 | appendSpeculatableOperands(I, Visited, Worklist); |
| 111 | } |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 112 | |
Chandler Carruth | 7306a61 | 2016-08-18 17:51:24 +0000 | [diff] [blame] | 113 | completeEphemeralValues(Visited, Worklist, EphValues); |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 116 | /// Fill in the current structure with information gleaned from the specified |
| 117 | /// block. |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 118 | void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 119 | const TargetTransformInfo &TTI, |
Sebastian Pop | 52f0f65 | 2016-08-03 19:13:50 +0000 | [diff] [blame] | 120 | const SmallPtrSetImpl<const Value*> &EphValues) { |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 121 | ++NumBlocks; |
| 122 | unsigned NumInstsBeforeThisBB = NumInsts; |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 123 | for (const Instruction &I : *BB) { |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 124 | // Skip ephemeral values. |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 125 | if (EphValues.count(&I)) |
Hal Finkel | 3d03d60 | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 126 | continue; |
| 127 | |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 128 | // Special handling for calls. |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 129 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
| 130 | ImmutableCallSite CS(&I); |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 131 | |
| 132 | if (const Function *F = CS.getCalledFunction()) { |
| 133 | // If a function is both internal and has a single use, then it is |
| 134 | // extremely likely to get inlined in the future (it was probably |
| 135 | // exposed by an interleaved devirtualization pass). |
| 136 | if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse()) |
| 137 | ++NumInlineCandidates; |
| 138 | |
| 139 | // If this call is to function itself, then the function is recursive. |
| 140 | // Inlining it into other functions is a bad idea, because this is |
| 141 | // basically just a form of loop peeling, and our metrics aren't useful |
| 142 | // for that case. |
| 143 | if (F == BB->getParent()) |
| 144 | isRecursive = true; |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 145 | |
Chandler Carruth | 13086a6 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 146 | if (TTI.isLoweredToCall(F)) |
| 147 | ++NumCalls; |
| 148 | } else { |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 149 | // We don't want inline asm to count as a call - that would prevent loop |
| 150 | // unrolling. The argument setup cost is still real, though. |
| 151 | if (!isa<InlineAsm>(CS.getCalledValue())) |
| 152 | ++NumCalls; |
| 153 | } |
| 154 | } |
| 155 | |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 156 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 157 | if (!AI->isStaticAlloca()) |
| 158 | this->usesDynamicAlloca = true; |
| 159 | } |
| 160 | |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 161 | if (isa<ExtractElementInst>(I) || I.getType()->isVectorTy()) |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 162 | ++NumVectorInsts; |
| 163 | |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 164 | if (I.getType()->isTokenTy() && I.isUsedOutsideOfBlock(BB)) |
David Majnemer | 2dacece | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 165 | notDuplicatable = true; |
| 166 | |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 167 | if (const CallInst *CI = dyn_cast<CallInst>(&I)) { |
Eli Bendersky | bbbc2b1 | 2014-03-17 16:19:07 +0000 | [diff] [blame] | 168 | if (CI->cannotDuplicate()) |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 169 | notDuplicatable = true; |
Justin Lebar | 9693e08 | 2016-02-12 21:01:31 +0000 | [diff] [blame] | 170 | if (CI->isConvergent()) |
| 171 | convergent = true; |
| 172 | } |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 173 | |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 174 | if (const InvokeInst *InvI = dyn_cast<InvokeInst>(&I)) |
Eli Bendersky | bbbc2b1 | 2014-03-17 16:19:07 +0000 | [diff] [blame] | 175 | if (InvI->cannotDuplicate()) |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 176 | notDuplicatable = true; |
| 177 | |
Sanjay Patel | 1ede7cb | 2016-03-08 20:53:48 +0000 | [diff] [blame] | 178 | NumInsts += TTI.getUserCost(&I); |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (isa<ReturnInst>(BB->getTerminator())) |
| 182 | ++NumRets; |
| 183 | |
| 184 | // We never want to inline functions that contain an indirectbr. This is |
| 185 | // incorrect because all the blockaddress's (in static global initializers |
| 186 | // for example) would be referring to the original function, and this indirect |
| 187 | // jump would jump from the inlined copy of the function into the original |
| 188 | // function which is extremely undefined behavior. |
| 189 | // FIXME: This logic isn't really right; we can safely inline functions |
| 190 | // with indirectbr's as long as no other function or global references the |
| 191 | // blockaddress of a block within the current function. And as a QOI issue, |
| 192 | // if someone is using a blockaddress without an indirectbr, and that |
| 193 | // reference somehow ends up in another function or global, we probably |
| 194 | // don't want to inline this function. |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 195 | notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator()); |
Chandler Carruth | 9b081d9 | 2012-03-16 05:51:52 +0000 | [diff] [blame] | 196 | |
| 197 | // Remember NumInsts for this BB. |
| 198 | NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; |
| 199 | } |