1 //===- CodeMetrics.cpp - Code cost measurements ---------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements code cost measurement utilities.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/CodeMetrics.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/Analysis/AssumptionCache.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/InstructionCost.h"
23 #define DEBUG_TYPE "code-metrics"
28 appendSpeculatableOperands(const Value *V,
29 SmallPtrSetImpl<const Value *> &Visited,
30 SmallVectorImpl<const Value *> &Worklist) {
31 const User *U = dyn_cast<User>(V);
35 for (const Value *Operand : U->operands())
36 if (Visited.insert(Operand).second)
37 if (const auto *I = dyn_cast<Instruction>(Operand))
38 if (!I->mayHaveSideEffects() && !I->isTerminator())
39 Worklist.push_back(I);
42 static void completeEphemeralValues(SmallPtrSetImpl<const Value *> &Visited,
43 SmallVectorImpl<const Value *> &Worklist,
44 SmallPtrSetImpl<const Value *> &EphValues) {
45 // Note: We don't speculate PHIs here, so we'll miss instruction chains kept
46 // alive only by ephemeral values.
48 // Walk the worklist using an index but without caching the size so we can
49 // append more entries as we process the worklist. This forms a queue without
50 // quadratic behavior by just leaving processed nodes at the head of the
52 for (int i = 0; i < (int)Worklist.size(); ++i) {
53 const Value *V = Worklist[i];
55 assert(Visited.count(V) &&
56 "Failed to add a worklist entry to our visited set!");
58 // If all uses of this value are ephemeral, then so is this value.
59 if (!all_of(V->users(), [&](const User *U) { return EphValues.count(U); }))
63 LLVM_DEBUG(dbgs() << "Ephemeral Value: " << *V << "\n");
65 // Append any more operands to consider.
66 appendSpeculatableOperands(V, Visited, Worklist);
70 // Find all ephemeral values.
71 void CodeMetrics::collectEphemeralValues(
72 const Loop *L, AssumptionCache *AC,
73 SmallPtrSetImpl<const Value *> &EphValues) {
74 SmallPtrSet<const Value *, 32> Visited;
75 SmallVector<const Value *, 16> Worklist;
77 for (auto &AssumeVH : AC->assumptions()) {
80 Instruction *I = cast<Instruction>(AssumeVH);
82 // Filter out call sites outside of the loop so we don't do a function's
83 // worth of work for each of its loops (and, in the common case, ephemeral
84 // values in the loop are likely due to @llvm.assume calls in the loop).
85 if (!L->contains(I->getParent()))
88 if (EphValues.insert(I).second)
89 appendSpeculatableOperands(I, Visited, Worklist);
92 completeEphemeralValues(Visited, Worklist, EphValues);
95 void CodeMetrics::collectEphemeralValues(
96 const Function *F, AssumptionCache *AC,
97 SmallPtrSetImpl<const Value *> &EphValues) {
98 SmallPtrSet<const Value *, 32> Visited;
99 SmallVector<const Value *, 16> Worklist;
101 for (auto &AssumeVH : AC->assumptions()) {
104 Instruction *I = cast<Instruction>(AssumeVH);
105 assert(I->getParent()->getParent() == F &&
106 "Found assumption for the wrong function!");
108 if (EphValues.insert(I).second)
109 appendSpeculatableOperands(I, Visited, Worklist);
112 completeEphemeralValues(Visited, Worklist, EphValues);
115 /// Fill in the current structure with information gleaned from the specified
117 void CodeMetrics::analyzeBasicBlock(
118 const BasicBlock *BB, const TargetTransformInfo &TTI,
119 const SmallPtrSetImpl<const Value *> &EphValues, bool PrepareForLTO) {
121 // Use a proxy variable for NumInsts of type InstructionCost, so that it can
122 // use InstructionCost's arithmetic properties such as saturation when this
123 // feature is added to InstructionCost.
124 // When storing the value back to NumInsts, we can assume all costs are Valid
125 // because the IR should not contain any nodes that cannot be costed. If that
126 // happens the cost-model is broken.
127 InstructionCost NumInstsProxy = NumInsts;
128 InstructionCost NumInstsBeforeThisBB = NumInsts;
129 for (const Instruction &I : *BB) {
130 // Skip ephemeral values.
131 if (EphValues.count(&I))
134 // Special handling for calls.
135 if (const auto *Call = dyn_cast<CallBase>(&I)) {
136 if (const Function *F = Call->getCalledFunction()) {
137 bool IsLoweredToCall = TTI.isLoweredToCall(F);
138 // If a function is both internal and has a single use, then it is
139 // extremely likely to get inlined in the future (it was probably
140 // exposed by an interleaved devirtualization pass).
141 // When preparing for LTO, liberally consider calls as inline
143 if (!Call->isNoInline() && IsLoweredToCall &&
144 ((F->hasInternalLinkage() && F->hasOneUse()) || PrepareForLTO)) {
145 ++NumInlineCandidates;
148 // If this call is to function itself, then the function is recursive.
149 // Inlining it into other functions is a bad idea, because this is
150 // basically just a form of loop peeling, and our metrics aren't useful
152 if (F == BB->getParent())
158 // We don't want inline asm to count as a call - that would prevent loop
159 // unrolling. The argument setup cost is still real, though.
160 if (!Call->isInlineAsm())
165 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
166 if (!AI->isStaticAlloca())
167 this->usesDynamicAlloca = true;
170 if (isa<ExtractElementInst>(I) || I.getType()->isVectorTy())
173 if (I.getType()->isTokenTy() && I.isUsedOutsideOfBlock(BB))
174 notDuplicatable = true;
176 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
177 if (CI->cannotDuplicate())
178 notDuplicatable = true;
179 if (CI->isConvergent())
183 if (const InvokeInst *InvI = dyn_cast<InvokeInst>(&I))
184 if (InvI->cannotDuplicate())
185 notDuplicatable = true;
187 NumInstsProxy += TTI.getUserCost(&I, TargetTransformInfo::TCK_CodeSize);
188 NumInsts = *NumInstsProxy.getValue();
191 if (isa<ReturnInst>(BB->getTerminator()))
194 // We never want to inline functions that contain an indirectbr. This is
195 // incorrect because all the blockaddress's (in static global initializers
196 // for example) would be referring to the original function, and this indirect
197 // jump would jump from the inlined copy of the function into the original
198 // function which is extremely undefined behavior.
199 // FIXME: This logic isn't really right; we can safely inline functions
200 // with indirectbr's as long as no other function or global references the
201 // blockaddress of a block within the current function. And as a QOI issue,
202 // if someone is using a blockaddress without an indirectbr, and that
203 // reference somehow ends up in another function or global, we probably
204 // don't want to inline this function.
205 notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
207 // Remember NumInsts for this BB.
208 InstructionCost NumInstsThisBB = NumInstsProxy - NumInstsBeforeThisBB;
209 NumBBInsts[BB] = *NumInstsThisBB.getValue();