1 //===- CodeMetrics.cpp - Code cost measurements ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements code cost measurement utilities.
12 //===----------------------------------------------------------------------===//
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"
26 #define DEBUG_TYPE "code-metrics"
30 static void completeEphemeralValues(SmallVector<const Value *, 16> &WorkSet,
31 SmallPtrSetImpl<const Value*> &EphValues) {
32 SmallPtrSet<const Value *, 32> Visited;
34 // Make sure that all of the items in WorkSet are in our EphValues set.
35 EphValues.insert(WorkSet.begin(), WorkSet.end());
37 // Note: We don't speculate PHIs here, so we'll miss instruction chains kept
38 // alive only by ephemeral values.
40 while (!WorkSet.empty()) {
41 const Value *V = WorkSet.front();
42 WorkSet.erase(WorkSet.begin());
44 if (!Visited.insert(V).second)
47 // If all uses of this value are ephemeral, then so is this value.
48 bool FoundNEUse = false;
49 for (const User *I : V->users())
50 if (!EphValues.count(I)) {
59 DEBUG(dbgs() << "Ephemeral Value: " << *V << "\n");
61 if (const User *U = dyn_cast<User>(V))
62 for (const Value *J : U->operands()) {
63 if (isSafeToSpeculativelyExecute(J))
69 // Find all ephemeral values.
70 void CodeMetrics::collectEphemeralValues(
71 const Loop *L, AssumptionCache *AC,
72 SmallPtrSetImpl<const Value *> &EphValues) {
73 SmallVector<const Value *, 16> WorkSet;
75 for (auto &AssumeVH : AC->assumptions()) {
78 Instruction *I = cast<Instruction>(AssumeVH);
80 // Filter out call sites outside of the loop so we don't to a function's
81 // worth of work for each of its loops (and, in the common case, ephemeral
82 // values in the loop are likely due to @llvm.assume calls in the loop).
83 if (!L->contains(I->getParent()))
89 completeEphemeralValues(WorkSet, EphValues);
92 void CodeMetrics::collectEphemeralValues(
93 const Function *F, AssumptionCache *AC,
94 SmallPtrSetImpl<const Value *> &EphValues) {
95 SmallVector<const Value *, 16> WorkSet;
97 for (auto &AssumeVH : AC->assumptions()) {
100 Instruction *I = cast<Instruction>(AssumeVH);
101 assert(I->getParent()->getParent() == F &&
102 "Found assumption for the wrong function!");
103 WorkSet.push_back(I);
106 completeEphemeralValues(WorkSet, EphValues);
109 /// analyzeBasicBlock - Fill in the current structure with information gleaned
110 /// from the specified block.
111 void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
112 const TargetTransformInfo &TTI,
113 SmallPtrSetImpl<const Value*> &EphValues) {
115 unsigned NumInstsBeforeThisBB = NumInsts;
116 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
118 // Skip ephemeral values.
119 if (EphValues.count(II))
122 // Special handling for calls.
123 if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
124 ImmutableCallSite CS(cast<Instruction>(II));
126 if (const Function *F = CS.getCalledFunction()) {
127 // If a function is both internal and has a single use, then it is
128 // extremely likely to get inlined in the future (it was probably
129 // exposed by an interleaved devirtualization pass).
130 if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse())
131 ++NumInlineCandidates;
133 // If this call is to function itself, then the function is recursive.
134 // Inlining it into other functions is a bad idea, because this is
135 // basically just a form of loop peeling, and our metrics aren't useful
137 if (F == BB->getParent())
140 if (TTI.isLoweredToCall(F))
143 // We don't want inline asm to count as a call - that would prevent loop
144 // unrolling. The argument setup cost is still real, though.
145 if (!isa<InlineAsm>(CS.getCalledValue()))
150 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
151 if (!AI->isStaticAlloca())
152 this->usesDynamicAlloca = true;
155 if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy())
158 if (const CallInst *CI = dyn_cast<CallInst>(II))
159 if (CI->cannotDuplicate())
160 notDuplicatable = true;
162 if (const InvokeInst *InvI = dyn_cast<InvokeInst>(II))
163 if (InvI->cannotDuplicate())
164 notDuplicatable = true;
166 NumInsts += TTI.getUserCost(&*II);
169 if (isa<ReturnInst>(BB->getTerminator()))
172 // We never want to inline functions that contain an indirectbr. This is
173 // incorrect because all the blockaddress's (in static global initializers
174 // for example) would be referring to the original function, and this indirect
175 // jump would jump from the inlined copy of the function into the original
176 // function which is extremely undefined behavior.
177 // FIXME: This logic isn't really right; we can safely inline functions
178 // with indirectbr's as long as no other function or global references the
179 // blockaddress of a block within the current function. And as a QOI issue,
180 // if someone is using a blockaddress without an indirectbr, and that
181 // reference somehow ends up in another function or global, we probably
182 // don't want to inline this function.
183 notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
185 // Remember NumInsts for this BB.
186 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;