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