blob: 576dca4b42c003d1d18d1bbfaaf8f8074ae44cdd [file] [log] [blame]
Chandler Carruth3c256fb2012-03-16 05:51:52 +00001//===- 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
Chandler Carruth66b31302015-01-04 12:03:27 +000014#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth3c256fb2012-03-16 05:51:52 +000015#include "llvm/Analysis/CodeMetrics.h"
Hal Finkel57f03dd2014-09-07 13:49:57 +000016#include "llvm/Analysis/LoopInfo.h"
Chandler Carruthbb9caa92013-01-21 13:04:33 +000017#include "llvm/Analysis/TargetTransformInfo.h"
Hal Finkel57f03dd2014-09-07 13:49:57 +000018#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000019#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/IntrinsicInst.h"
Hal Finkel57f03dd2014-09-07 13:49:57 +000023#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000024#include "llvm/Support/raw_ostream.h"
Hal Finkel57f03dd2014-09-07 13:49:57 +000025
26#define DEBUG_TYPE "code-metrics"
Chandler Carruth3c256fb2012-03-16 05:51:52 +000027
28using namespace llvm;
29
Hal Finkel57f03dd2014-09-07 13:49:57 +000030static void completeEphemeralValues(SmallVector<const Value *, 16> &WorkSet,
31 SmallPtrSetImpl<const Value*> &EphValues) {
32 SmallPtrSet<const Value *, 32> Visited;
33
34 // Make sure that all of the items in WorkSet are in our EphValues set.
35 EphValues.insert(WorkSet.begin(), WorkSet.end());
36
37 // Note: We don't speculate PHIs here, so we'll miss instruction chains kept
38 // alive only by ephemeral values.
39
40 while (!WorkSet.empty()) {
Hal Finkel8683d2b2014-10-15 17:34:48 +000041 const Value *V = WorkSet.front();
42 WorkSet.erase(WorkSet.begin());
43
David Blaikie70573dc2014-11-19 07:49:26 +000044 if (!Visited.insert(V).second)
Hal Finkel57f03dd2014-09-07 13:49:57 +000045 continue;
46
47 // If all uses of this value are ephemeral, then so is this value.
Benjamin Kramer56115612015-10-24 19:30:37 +000048 if (!std::all_of(V->user_begin(), V->user_end(),
49 [&](const User *U) { return EphValues.count(U); }))
Hal Finkel57f03dd2014-09-07 13:49:57 +000050 continue;
51
52 EphValues.insert(V);
53 DEBUG(dbgs() << "Ephemeral Value: " << *V << "\n");
54
55 if (const User *U = dyn_cast<User>(V))
56 for (const Value *J : U->operands()) {
57 if (isSafeToSpeculativelyExecute(J))
58 WorkSet.push_back(J);
59 }
60 }
61}
62
63// Find all ephemeral values.
Chandler Carruth66b31302015-01-04 12:03:27 +000064void CodeMetrics::collectEphemeralValues(
65 const Loop *L, AssumptionCache *AC,
66 SmallPtrSetImpl<const Value *> &EphValues) {
Hal Finkel57f03dd2014-09-07 13:49:57 +000067 SmallVector<const Value *, 16> WorkSet;
68
Chandler Carruth66b31302015-01-04 12:03:27 +000069 for (auto &AssumeVH : AC->assumptions()) {
70 if (!AssumeVH)
71 continue;
72 Instruction *I = cast<Instruction>(AssumeVH);
73
Hal Finkel57f03dd2014-09-07 13:49:57 +000074 // Filter out call sites outside of the loop so we don't to a function's
75 // worth of work for each of its loops (and, in the common case, ephemeral
76 // values in the loop are likely due to @llvm.assume calls in the loop).
77 if (!L->contains(I->getParent()))
78 continue;
79
80 WorkSet.push_back(I);
81 }
82
83 completeEphemeralValues(WorkSet, EphValues);
84}
85
Chandler Carruth66b31302015-01-04 12:03:27 +000086void CodeMetrics::collectEphemeralValues(
87 const Function *F, AssumptionCache *AC,
88 SmallPtrSetImpl<const Value *> &EphValues) {
Hal Finkel57f03dd2014-09-07 13:49:57 +000089 SmallVector<const Value *, 16> WorkSet;
90
Chandler Carruth66b31302015-01-04 12:03:27 +000091 for (auto &AssumeVH : AC->assumptions()) {
92 if (!AssumeVH)
93 continue;
94 Instruction *I = cast<Instruction>(AssumeVH);
95 assert(I->getParent()->getParent() == F &&
96 "Found assumption for the wrong function!");
Hal Finkel57f03dd2014-09-07 13:49:57 +000097 WorkSet.push_back(I);
Chandler Carruth66b31302015-01-04 12:03:27 +000098 }
Hal Finkel57f03dd2014-09-07 13:49:57 +000099
100 completeEphemeralValues(WorkSet, EphValues);
101}
102
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000103/// Fill in the current structure with information gleaned from the specified
104/// block.
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000105void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB,
Hal Finkel57f03dd2014-09-07 13:49:57 +0000106 const TargetTransformInfo &TTI,
Sebastian Pop031b1bc2016-08-03 19:13:50 +0000107 const SmallPtrSetImpl<const Value*> &EphValues) {
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000108 ++NumBlocks;
109 unsigned NumInstsBeforeThisBB = NumInsts;
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000110 for (const Instruction &I : *BB) {
Hal Finkel57f03dd2014-09-07 13:49:57 +0000111 // Skip ephemeral values.
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000112 if (EphValues.count(&I))
Hal Finkel57f03dd2014-09-07 13:49:57 +0000113 continue;
114
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000115 // Special handling for calls.
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000116 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
117 ImmutableCallSite CS(&I);
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000118
119 if (const Function *F = CS.getCalledFunction()) {
120 // If a function is both internal and has a single use, then it is
121 // extremely likely to get inlined in the future (it was probably
122 // exposed by an interleaved devirtualization pass).
123 if (!CS.isNoInline() && F->hasInternalLinkage() && F->hasOneUse())
124 ++NumInlineCandidates;
125
126 // If this call is to function itself, then the function is recursive.
127 // Inlining it into other functions is a bad idea, because this is
128 // basically just a form of loop peeling, and our metrics aren't useful
129 // for that case.
130 if (F == BB->getParent())
131 isRecursive = true;
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000132
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000133 if (TTI.isLoweredToCall(F))
134 ++NumCalls;
135 } else {
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000136 // We don't want inline asm to count as a call - that would prevent loop
137 // unrolling. The argument setup cost is still real, though.
138 if (!isa<InlineAsm>(CS.getCalledValue()))
139 ++NumCalls;
140 }
141 }
142
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000143 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000144 if (!AI->isStaticAlloca())
145 this->usesDynamicAlloca = true;
146 }
147
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000148 if (isa<ExtractElementInst>(I) || I.getType()->isVectorTy())
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000149 ++NumVectorInsts;
150
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000151 if (I.getType()->isTokenTy() && I.isUsedOutsideOfBlock(BB))
David Majnemerb611e3f2015-08-14 05:09:07 +0000152 notDuplicatable = true;
153
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000154 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
Eli Bendersky576ef3c2014-03-17 16:19:07 +0000155 if (CI->cannotDuplicate())
James Molloy4f6fb952012-12-20 16:04:27 +0000156 notDuplicatable = true;
Justin Lebar144c5a62016-02-12 21:01:31 +0000157 if (CI->isConvergent())
158 convergent = true;
159 }
James Molloy4f6fb952012-12-20 16:04:27 +0000160
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000161 if (const InvokeInst *InvI = dyn_cast<InvokeInst>(&I))
Eli Bendersky576ef3c2014-03-17 16:19:07 +0000162 if (InvI->cannotDuplicate())
James Molloy4f6fb952012-12-20 16:04:27 +0000163 notDuplicatable = true;
164
Sanjay Patelb8d071b2016-03-08 20:53:48 +0000165 NumInsts += TTI.getUserCost(&I);
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000166 }
167
168 if (isa<ReturnInst>(BB->getTerminator()))
169 ++NumRets;
170
171 // We never want to inline functions that contain an indirectbr. This is
172 // incorrect because all the blockaddress's (in static global initializers
173 // for example) would be referring to the original function, and this indirect
174 // jump would jump from the inlined copy of the function into the original
175 // function which is extremely undefined behavior.
176 // FIXME: This logic isn't really right; we can safely inline functions
177 // with indirectbr's as long as no other function or global references the
178 // blockaddress of a block within the current function. And as a QOI issue,
179 // if someone is using a blockaddress without an indirectbr, and that
180 // reference somehow ends up in another function or global, we probably
181 // don't want to inline this function.
James Molloy4f6fb952012-12-20 16:04:27 +0000182 notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
Chandler Carruth3c256fb2012-03-16 05:51:52 +0000183
184 // Remember NumInsts for this BB.
185 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
186}