13c256fbfSChandler Carruth //===- CodeMetrics.cpp - Code cost measurements ---------------------------===//
23c256fbfSChandler Carruth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63c256fbfSChandler Carruth //
73c256fbfSChandler Carruth //===----------------------------------------------------------------------===//
83c256fbfSChandler Carruth //
93c256fbfSChandler Carruth // This file implements code cost measurement utilities.
103c256fbfSChandler Carruth //
113c256fbfSChandler Carruth //===----------------------------------------------------------------------===//
123c256fbfSChandler Carruth 
133c256fbfSChandler Carruth #include "llvm/Analysis/CodeMetrics.h"
14ad572862SSimon Pilgrim #include "llvm/ADT/SmallPtrSet.h"
156bda14b3SChandler Carruth #include "llvm/Analysis/AssumptionCache.h"
1657f03ddaSHal Finkel #include "llvm/Analysis/LoopInfo.h"
17bb9caa92SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h"
189fb823bbSChandler Carruth #include "llvm/IR/Function.h"
1957f03ddaSHal Finkel #include "llvm/Support/Debug.h"
2041500836SSander de Smalen #include "llvm/Support/InstructionCost.h"
2157f03ddaSHal Finkel 
2257f03ddaSHal Finkel #define DEBUG_TYPE "code-metrics"
233c256fbfSChandler Carruth 
243c256fbfSChandler Carruth using namespace llvm;
253c256fbfSChandler Carruth 
26e2f36bcbSChandler Carruth static void
appendSpeculatableOperands(const Value * V,SmallPtrSetImpl<const Value * > & Visited,SmallVectorImpl<const Value * > & Worklist)27e2f36bcbSChandler Carruth appendSpeculatableOperands(const Value *V,
28e2f36bcbSChandler Carruth                            SmallPtrSetImpl<const Value *> &Visited,
29e2f36bcbSChandler Carruth                            SmallVectorImpl<const Value *> &Worklist) {
30e2f36bcbSChandler Carruth   const User *U = dyn_cast<User>(V);
31e2f36bcbSChandler Carruth   if (!U)
32e2f36bcbSChandler Carruth     return;
33e2f36bcbSChandler Carruth 
34e2f36bcbSChandler Carruth   for (const Value *Operand : U->operands())
35e2f36bcbSChandler Carruth     if (Visited.insert(Operand).second)
3618485258SNikita Popov       if (const auto *I = dyn_cast<Instruction>(Operand))
3718485258SNikita Popov         if (!I->mayHaveSideEffects() && !I->isTerminator())
3818485258SNikita Popov           Worklist.push_back(I);
39e2f36bcbSChandler Carruth }
40e2f36bcbSChandler Carruth 
completeEphemeralValues(SmallPtrSetImpl<const Value * > & Visited,SmallVectorImpl<const Value * > & Worklist,SmallPtrSetImpl<const Value * > & EphValues)41e2f36bcbSChandler Carruth static void completeEphemeralValues(SmallPtrSetImpl<const Value *> &Visited,
42e2f36bcbSChandler Carruth                                     SmallVectorImpl<const Value *> &Worklist,
4357f03ddaSHal Finkel                                     SmallPtrSetImpl<const Value *> &EphValues) {
4457f03ddaSHal Finkel   // Note: We don't speculate PHIs here, so we'll miss instruction chains kept
4557f03ddaSHal Finkel   // alive only by ephemeral values.
4657f03ddaSHal Finkel 
47e2f36bcbSChandler Carruth   // Walk the worklist using an index but without caching the size so we can
48e2f36bcbSChandler Carruth   // append more entries as we process the worklist. This forms a queue without
49e2f36bcbSChandler Carruth   // quadratic behavior by just leaving processed nodes at the head of the
50e2f36bcbSChandler Carruth   // worklist forever.
51e2f36bcbSChandler Carruth   for (int i = 0; i < (int)Worklist.size(); ++i) {
52e2f36bcbSChandler Carruth     const Value *V = Worklist[i];
538683d2b0SHal Finkel 
54e2f36bcbSChandler Carruth     assert(Visited.count(V) &&
55e2f36bcbSChandler Carruth            "Failed to add a worklist entry to our visited set!");
5657f03ddaSHal Finkel 
5757f03ddaSHal Finkel     // If all uses of this value are ephemeral, then so is this value.
580a16c228SDavid Majnemer     if (!all_of(V->users(), [&](const User *U) { return EphValues.count(U); }))
5957f03ddaSHal Finkel       continue;
6057f03ddaSHal Finkel 
6157f03ddaSHal Finkel     EphValues.insert(V);
62d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Ephemeral Value: " << *V << "\n");
6357f03ddaSHal Finkel 
64e2f36bcbSChandler Carruth     // Append any more operands to consider.
65e2f36bcbSChandler Carruth     appendSpeculatableOperands(V, Visited, Worklist);
6657f03ddaSHal Finkel   }
6757f03ddaSHal Finkel }
6857f03ddaSHal Finkel 
6957f03ddaSHal Finkel // Find all ephemeral values.
collectEphemeralValues(const Loop * L,AssumptionCache * AC,SmallPtrSetImpl<const Value * > & EphValues)7066b3130cSChandler Carruth void CodeMetrics::collectEphemeralValues(
71aec2fa35SDaniel Jasper     const Loop *L, AssumptionCache *AC,
72aec2fa35SDaniel Jasper     SmallPtrSetImpl<const Value *> &EphValues) {
73e2f36bcbSChandler Carruth   SmallPtrSet<const Value *, 32> Visited;
74e2f36bcbSChandler Carruth   SmallVector<const Value *, 16> Worklist;
7557f03ddaSHal Finkel 
76aec2fa35SDaniel Jasper   for (auto &AssumeVH : AC->assumptions()) {
77606aa622SMichael Kruse     if (!AssumeVH)
78606aa622SMichael Kruse       continue;
79606aa622SMichael Kruse     Instruction *I = cast<Instruction>(AssumeVH);
80aec2fa35SDaniel Jasper 
81aec2fa35SDaniel Jasper     // Filter out call sites outside of the loop so we don't do a function's
82aec2fa35SDaniel Jasper     // worth of work for each of its loops (and, in the common case, ephemeral
83aec2fa35SDaniel Jasper     // values in the loop are likely due to @llvm.assume calls in the loop).
84aec2fa35SDaniel Jasper     if (!L->contains(I->getParent()))
85aec2fa35SDaniel Jasper       continue;
86aec2fa35SDaniel Jasper 
87aec2fa35SDaniel Jasper     if (EphValues.insert(I).second)
88aec2fa35SDaniel Jasper       appendSpeculatableOperands(I, Visited, Worklist);
89aec2fa35SDaniel Jasper   }
9057f03ddaSHal Finkel 
91e2f36bcbSChandler Carruth   completeEphemeralValues(Visited, Worklist, EphValues);
9257f03ddaSHal Finkel }
9357f03ddaSHal Finkel 
collectEphemeralValues(const Function * F,AssumptionCache * AC,SmallPtrSetImpl<const Value * > & EphValues)9466b3130cSChandler Carruth void CodeMetrics::collectEphemeralValues(
95aec2fa35SDaniel Jasper     const Function *F, AssumptionCache *AC,
96aec2fa35SDaniel Jasper     SmallPtrSetImpl<const Value *> &EphValues) {
97e2f36bcbSChandler Carruth   SmallPtrSet<const Value *, 32> Visited;
98e2f36bcbSChandler Carruth   SmallVector<const Value *, 16> Worklist;
9957f03ddaSHal Finkel 
100aec2fa35SDaniel Jasper   for (auto &AssumeVH : AC->assumptions()) {
101606aa622SMichael Kruse     if (!AssumeVH)
102606aa622SMichael Kruse       continue;
103606aa622SMichael Kruse     Instruction *I = cast<Instruction>(AssumeVH);
104aec2fa35SDaniel Jasper     assert(I->getParent()->getParent() == F &&
105aec2fa35SDaniel Jasper            "Found assumption for the wrong function!");
106aec2fa35SDaniel Jasper 
107aec2fa35SDaniel Jasper     if (EphValues.insert(I).second)
108aec2fa35SDaniel Jasper       appendSpeculatableOperands(I, Visited, Worklist);
109aec2fa35SDaniel Jasper   }
11057f03ddaSHal Finkel 
111e2f36bcbSChandler Carruth   completeEphemeralValues(Visited, Worklist, EphValues);
11257f03ddaSHal Finkel }
11357f03ddaSHal Finkel 
114b8d071bcSSanjay Patel /// Fill in the current structure with information gleaned from the specified
115b8d071bcSSanjay Patel /// block.
analyzeBasicBlock(const BasicBlock * BB,const TargetTransformInfo & TTI,const SmallPtrSetImpl<const Value * > & EphValues,bool PrepareForLTO)11683daa497SFlorian Hahn void CodeMetrics::analyzeBasicBlock(
11783daa497SFlorian Hahn     const BasicBlock *BB, const TargetTransformInfo &TTI,
11883daa497SFlorian Hahn     const SmallPtrSetImpl<const Value *> &EphValues, bool PrepareForLTO) {
1190539c071SChandler Carruth   ++NumBlocks;
12041500836SSander de Smalen   InstructionCost NumInstsBeforeThisBB = NumInsts;
121b8d071bcSSanjay Patel   for (const Instruction &I : *BB) {
12257f03ddaSHal Finkel     // Skip ephemeral values.
123b8d071bcSSanjay Patel     if (EphValues.count(&I))
12457f03ddaSHal Finkel       continue;
12557f03ddaSHal Finkel 
1260539c071SChandler Carruth     // Special handling for calls.
1272d2a4359SChandler Carruth     if (const auto *Call = dyn_cast<CallBase>(&I)) {
1282d2a4359SChandler Carruth       if (const Function *F = Call->getCalledFunction()) {
1293747b69bSFlorian Hahn         bool IsLoweredToCall = TTI.isLoweredToCall(F);
1303c256fbfSChandler Carruth         // If a function is both internal and has a single use, then it is
1313c256fbfSChandler Carruth         // extremely likely to get inlined in the future (it was probably
1323c256fbfSChandler Carruth         // exposed by an interleaved devirtualization pass).
13383daa497SFlorian Hahn         // When preparing for LTO, liberally consider calls as inline
13483daa497SFlorian Hahn         // candidates.
1353747b69bSFlorian Hahn         if (!Call->isNoInline() && IsLoweredToCall &&
136*7a2f5dcaSSinan Lin             ((F->hasInternalLinkage() && F->hasOneLiveUse()) ||
137*7a2f5dcaSSinan Lin              PrepareForLTO)) {
1383c256fbfSChandler Carruth           ++NumInlineCandidates;
13983daa497SFlorian Hahn         }
1403c256fbfSChandler Carruth 
1413c256fbfSChandler Carruth         // If this call is to function itself, then the function is recursive.
1423c256fbfSChandler Carruth         // Inlining it into other functions is a bad idea, because this is
1433c256fbfSChandler Carruth         // basically just a form of loop peeling, and our metrics aren't useful
1443c256fbfSChandler Carruth         // for that case.
1453c256fbfSChandler Carruth         if (F == BB->getParent())
1463c256fbfSChandler Carruth           isRecursive = true;
1473c256fbfSChandler Carruth 
1483747b69bSFlorian Hahn         if (IsLoweredToCall)
1490ba8db45SChandler Carruth           ++NumCalls;
1500ba8db45SChandler Carruth       } else {
1513c256fbfSChandler Carruth         // We don't want inline asm to count as a call - that would prevent loop
1523c256fbfSChandler Carruth         // unrolling. The argument setup cost is still real, though.
1532d2a4359SChandler Carruth         if (!Call->isInlineAsm())
1543c256fbfSChandler Carruth           ++NumCalls;
1553c256fbfSChandler Carruth       }
1563c256fbfSChandler Carruth     }
1573c256fbfSChandler Carruth 
158b8d071bcSSanjay Patel     if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
1593c256fbfSChandler Carruth       if (!AI->isStaticAlloca())
1603c256fbfSChandler Carruth         this->usesDynamicAlloca = true;
1613c256fbfSChandler Carruth     }
1623c256fbfSChandler Carruth 
163b8d071bcSSanjay Patel     if (isa<ExtractElementInst>(I) || I.getType()->isVectorTy())
1643c256fbfSChandler Carruth       ++NumVectorInsts;
1653c256fbfSChandler Carruth 
166b8d071bcSSanjay Patel     if (I.getType()->isTokenTy() && I.isUsedOutsideOfBlock(BB))
167b611e3f5SDavid Majnemer       notDuplicatable = true;
168b611e3f5SDavid Majnemer 
169b8d071bcSSanjay Patel     if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
170576ef3c6SEli Bendersky       if (CI->cannotDuplicate())
1714f6fb953SJames Molloy         notDuplicatable = true;
172144c5a6cSJustin Lebar       if (CI->isConvergent())
173144c5a6cSJustin Lebar         convergent = true;
174144c5a6cSJustin Lebar     }
1754f6fb953SJames Molloy 
176b8d071bcSSanjay Patel     if (const InvokeInst *InvI = dyn_cast<InvokeInst>(&I))
177576ef3c6SEli Bendersky       if (InvI->cannotDuplicate())
1784f6fb953SJames Molloy         notDuplicatable = true;
1794f6fb953SJames Molloy 
180f85c5079SPhilip Reames     NumInsts += TTI.getUserCost(&I, TargetTransformInfo::TCK_CodeSize);
1813c256fbfSChandler Carruth   }
1823c256fbfSChandler Carruth 
1833c256fbfSChandler Carruth   if (isa<ReturnInst>(BB->getTerminator()))
1843c256fbfSChandler Carruth     ++NumRets;
1853c256fbfSChandler Carruth 
1863c256fbfSChandler Carruth   // We never want to inline functions that contain an indirectbr.  This is
1873c256fbfSChandler Carruth   // incorrect because all the blockaddress's (in static global initializers
1883c256fbfSChandler Carruth   // for example) would be referring to the original function, and this indirect
1893c256fbfSChandler Carruth   // jump would jump from the inlined copy of the function into the original
1903c256fbfSChandler Carruth   // function which is extremely undefined behavior.
1913c256fbfSChandler Carruth   // FIXME: This logic isn't really right; we can safely inline functions
1923c256fbfSChandler Carruth   // with indirectbr's as long as no other function or global references the
1933c256fbfSChandler Carruth   // blockaddress of a block within the current function.  And as a QOI issue,
1943c256fbfSChandler Carruth   // if someone is using a blockaddress without an indirectbr, and that
1953c256fbfSChandler Carruth   // reference somehow ends up in another function or global, we probably
1963c256fbfSChandler Carruth   // don't want to inline this function.
1974f6fb953SJames Molloy   notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
1983c256fbfSChandler Carruth 
1993c256fbfSChandler Carruth   // Remember NumInsts for this BB.
200f85c5079SPhilip Reames   InstructionCost NumInstsThisBB = NumInsts - NumInstsBeforeThisBB;
201f85c5079SPhilip Reames   NumBBInsts[BB] = NumInstsThisBB;
2023c256fbfSChandler Carruth }
203