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