xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1e8d8bef9SDimitry Andric //===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
10e8d8bef9SDimitry Andric // classes used to extract function properties.
11e8d8bef9SDimitry Andric //
12e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13e8d8bef9SDimitry Andric 
14e8d8bef9SDimitry Andric #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
15*81ad6265SDimitry Andric #include "llvm/ADT/STLExtras.h"
16*81ad6265SDimitry Andric #include "llvm/ADT/SetVector.h"
17*81ad6265SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
18*81ad6265SDimitry Andric #include "llvm/IR/CFG.h"
19*81ad6265SDimitry Andric #include "llvm/IR/Dominators.h"
20e8d8bef9SDimitry Andric #include "llvm/IR/Instructions.h"
21*81ad6265SDimitry Andric #include <deque>
22e8d8bef9SDimitry Andric 
23e8d8bef9SDimitry Andric using namespace llvm;
24e8d8bef9SDimitry Andric 
25*81ad6265SDimitry Andric namespace {
26*81ad6265SDimitry Andric int64_t getNrBlocksFromCond(const BasicBlock &BB) {
27*81ad6265SDimitry Andric   int64_t Ret = 0;
28e8d8bef9SDimitry Andric   if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
29e8d8bef9SDimitry Andric     if (BI->isConditional())
30*81ad6265SDimitry Andric       Ret += BI->getNumSuccessors();
31e8d8bef9SDimitry Andric   } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
32*81ad6265SDimitry Andric     Ret += (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
33*81ad6265SDimitry Andric   }
34*81ad6265SDimitry Andric   return Ret;
35e8d8bef9SDimitry Andric }
36e8d8bef9SDimitry Andric 
37*81ad6265SDimitry Andric int64_t getUses(const Function &F) {
38*81ad6265SDimitry Andric   return ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
39*81ad6265SDimitry Andric }
40*81ad6265SDimitry Andric } // namespace
41*81ad6265SDimitry Andric 
42*81ad6265SDimitry Andric void FunctionPropertiesInfo::reIncludeBB(const BasicBlock &BB) {
43*81ad6265SDimitry Andric   updateForBB(BB, +1);
44*81ad6265SDimitry Andric }
45*81ad6265SDimitry Andric 
46*81ad6265SDimitry Andric void FunctionPropertiesInfo::updateForBB(const BasicBlock &BB,
47*81ad6265SDimitry Andric                                          int64_t Direction) {
48*81ad6265SDimitry Andric   assert(Direction == 1 || Direction == -1);
49*81ad6265SDimitry Andric   BasicBlockCount += Direction;
50*81ad6265SDimitry Andric   BlocksReachedFromConditionalInstruction +=
51*81ad6265SDimitry Andric       (Direction * getNrBlocksFromCond(BB));
52e8d8bef9SDimitry Andric   for (const auto &I : BB) {
53e8d8bef9SDimitry Andric     if (auto *CS = dyn_cast<CallBase>(&I)) {
54e8d8bef9SDimitry Andric       const auto *Callee = CS->getCalledFunction();
55e8d8bef9SDimitry Andric       if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
56*81ad6265SDimitry Andric         DirectCallsToDefinedFunctions += Direction;
57e8d8bef9SDimitry Andric     }
58e8d8bef9SDimitry Andric     if (I.getOpcode() == Instruction::Load) {
59*81ad6265SDimitry Andric       LoadInstCount += Direction;
60e8d8bef9SDimitry Andric     } else if (I.getOpcode() == Instruction::Store) {
61*81ad6265SDimitry Andric       StoreInstCount += Direction;
62e8d8bef9SDimitry Andric     }
63e8d8bef9SDimitry Andric   }
64*81ad6265SDimitry Andric   TotalInstructionCount += Direction * BB.sizeWithoutDebug();
65e8d8bef9SDimitry Andric }
66*81ad6265SDimitry Andric 
67*81ad6265SDimitry Andric void FunctionPropertiesInfo::updateAggregateStats(const Function &F,
68*81ad6265SDimitry Andric                                                   const LoopInfo &LI) {
69*81ad6265SDimitry Andric 
70*81ad6265SDimitry Andric   Uses = getUses(F);
71*81ad6265SDimitry Andric   TopLevelLoopCount = llvm::size(LI);
72*81ad6265SDimitry Andric   MaxLoopDepth = 0;
73*81ad6265SDimitry Andric   std::deque<const Loop *> Worklist;
74*81ad6265SDimitry Andric   llvm::append_range(Worklist, LI);
75*81ad6265SDimitry Andric   while (!Worklist.empty()) {
76*81ad6265SDimitry Andric     const auto *L = Worklist.front();
77*81ad6265SDimitry Andric     MaxLoopDepth =
78*81ad6265SDimitry Andric         std::max(MaxLoopDepth, static_cast<int64_t>(L->getLoopDepth()));
79*81ad6265SDimitry Andric     Worklist.pop_front();
80*81ad6265SDimitry Andric     llvm::append_range(Worklist, L->getSubLoops());
81*81ad6265SDimitry Andric   }
82*81ad6265SDimitry Andric }
83*81ad6265SDimitry Andric 
84*81ad6265SDimitry Andric FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
85*81ad6265SDimitry Andric     const Function &F, FunctionAnalysisManager &FAM) {
86*81ad6265SDimitry Andric 
87*81ad6265SDimitry Andric   FunctionPropertiesInfo FPI;
88*81ad6265SDimitry Andric   // The const casts are due to the getResult API - there's no mutation of F.
89*81ad6265SDimitry Andric   const auto &LI = FAM.getResult<LoopAnalysis>(const_cast<Function &>(F));
90*81ad6265SDimitry Andric   const auto &DT =
91*81ad6265SDimitry Andric       FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(F));
92*81ad6265SDimitry Andric   for (const auto &BB : F)
93*81ad6265SDimitry Andric     if (DT.isReachableFromEntry(&BB))
94*81ad6265SDimitry Andric       FPI.reIncludeBB(BB);
95*81ad6265SDimitry Andric   FPI.updateAggregateStats(F, LI);
96e8d8bef9SDimitry Andric   return FPI;
97e8d8bef9SDimitry Andric }
98e8d8bef9SDimitry Andric 
99e8d8bef9SDimitry Andric void FunctionPropertiesInfo::print(raw_ostream &OS) const {
100e8d8bef9SDimitry Andric   OS << "BasicBlockCount: " << BasicBlockCount << "\n"
101e8d8bef9SDimitry Andric      << "BlocksReachedFromConditionalInstruction: "
102e8d8bef9SDimitry Andric      << BlocksReachedFromConditionalInstruction << "\n"
103e8d8bef9SDimitry Andric      << "Uses: " << Uses << "\n"
104e8d8bef9SDimitry Andric      << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions
105e8d8bef9SDimitry Andric      << "\n"
106e8d8bef9SDimitry Andric      << "LoadInstCount: " << LoadInstCount << "\n"
107e8d8bef9SDimitry Andric      << "StoreInstCount: " << StoreInstCount << "\n"
108e8d8bef9SDimitry Andric      << "MaxLoopDepth: " << MaxLoopDepth << "\n"
109*81ad6265SDimitry Andric      << "TopLevelLoopCount: " << TopLevelLoopCount << "\n"
110*81ad6265SDimitry Andric      << "TotalInstructionCount: " << TotalInstructionCount << "\n\n";
111e8d8bef9SDimitry Andric }
112e8d8bef9SDimitry Andric 
113e8d8bef9SDimitry Andric AnalysisKey FunctionPropertiesAnalysis::Key;
114e8d8bef9SDimitry Andric 
115e8d8bef9SDimitry Andric FunctionPropertiesInfo
116e8d8bef9SDimitry Andric FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
117*81ad6265SDimitry Andric   return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, FAM);
118e8d8bef9SDimitry Andric }
119e8d8bef9SDimitry Andric 
120e8d8bef9SDimitry Andric PreservedAnalyses
121e8d8bef9SDimitry Andric FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
122e8d8bef9SDimitry Andric   OS << "Printing analysis results of CFA for function "
123e8d8bef9SDimitry Andric      << "'" << F.getName() << "':"
124e8d8bef9SDimitry Andric      << "\n";
125e8d8bef9SDimitry Andric   AM.getResult<FunctionPropertiesAnalysis>(F).print(OS);
126e8d8bef9SDimitry Andric   return PreservedAnalyses::all();
127e8d8bef9SDimitry Andric }
128*81ad6265SDimitry Andric 
129*81ad6265SDimitry Andric FunctionPropertiesUpdater::FunctionPropertiesUpdater(
130*81ad6265SDimitry Andric     FunctionPropertiesInfo &FPI, const CallBase &CB)
131*81ad6265SDimitry Andric     : FPI(FPI), CallSiteBB(*CB.getParent()), Caller(*CallSiteBB.getParent()) {
132*81ad6265SDimitry Andric   assert(isa<CallInst>(CB) || isa<InvokeInst>(CB));
133*81ad6265SDimitry Andric   // For BBs that are likely to change, we subtract from feature totals their
134*81ad6265SDimitry Andric   // contribution. Some features, like max loop counts or depths, are left
135*81ad6265SDimitry Andric   // invalid, as they will be updated post-inlining.
136*81ad6265SDimitry Andric   SmallPtrSet<const BasicBlock *, 4> LikelyToChangeBBs;
137*81ad6265SDimitry Andric   // The CB BB will change - it'll either be split or the callee's body (single
138*81ad6265SDimitry Andric   // BB) will be pasted in.
139*81ad6265SDimitry Andric   LikelyToChangeBBs.insert(&CallSiteBB);
140*81ad6265SDimitry Andric 
141*81ad6265SDimitry Andric   // The caller's entry BB may change due to new alloca instructions.
142*81ad6265SDimitry Andric   LikelyToChangeBBs.insert(&*Caller.begin());
143*81ad6265SDimitry Andric 
144*81ad6265SDimitry Andric   // The successors may become unreachable in the case of `invoke` inlining.
145*81ad6265SDimitry Andric   // We track successors separately, too, because they form a boundary, together
146*81ad6265SDimitry Andric   // with the CB BB ('Entry') between which the inlined callee will be pasted.
147*81ad6265SDimitry Andric   Successors.insert(succ_begin(&CallSiteBB), succ_end(&CallSiteBB));
148*81ad6265SDimitry Andric 
149*81ad6265SDimitry Andric   // Inlining only handles invoke and calls. If this is an invoke, and inlining
150*81ad6265SDimitry Andric   // it pulls another invoke, the original landing pad may get split, so as to
151*81ad6265SDimitry Andric   // share its content with other potential users. So the edge up to which we
152*81ad6265SDimitry Andric   // need to invalidate and then re-account BB data is the successors of the
153*81ad6265SDimitry Andric   // current landing pad. We can leave the current lp, too - if it doesn't get
154*81ad6265SDimitry Andric   // split, then it will be the place traversal stops. Either way, the
155*81ad6265SDimitry Andric   // discounted BBs will be checked if reachable and re-added.
156*81ad6265SDimitry Andric   if (const auto *II = dyn_cast<InvokeInst>(&CB)) {
157*81ad6265SDimitry Andric     const auto *UnwindDest = II->getUnwindDest();
158*81ad6265SDimitry Andric     Successors.insert(succ_begin(UnwindDest), succ_end(UnwindDest));
159*81ad6265SDimitry Andric   }
160*81ad6265SDimitry Andric 
161*81ad6265SDimitry Andric   // Exclude the CallSiteBB, if it happens to be its own successor (1-BB loop).
162*81ad6265SDimitry Andric   // We are only interested in BBs the graph moves past the callsite BB to
163*81ad6265SDimitry Andric   // define the frontier past which we don't want to re-process BBs. Including
164*81ad6265SDimitry Andric   // the callsite BB in this case would prematurely stop the traversal in
165*81ad6265SDimitry Andric   // finish().
166*81ad6265SDimitry Andric   Successors.erase(&CallSiteBB);
167*81ad6265SDimitry Andric 
168*81ad6265SDimitry Andric   for (const auto *BB : Successors)
169*81ad6265SDimitry Andric     LikelyToChangeBBs.insert(BB);
170*81ad6265SDimitry Andric 
171*81ad6265SDimitry Andric   // Commit the change. While some of the BBs accounted for above may play dual
172*81ad6265SDimitry Andric   // role - e.g. caller's entry BB may be the same as the callsite BB - set
173*81ad6265SDimitry Andric   // insertion semantics make sure we account them once. This needs to be
174*81ad6265SDimitry Andric   // followed in `finish`, too.
175*81ad6265SDimitry Andric   for (const auto *BB : LikelyToChangeBBs)
176*81ad6265SDimitry Andric     FPI.updateForBB(*BB, -1);
177*81ad6265SDimitry Andric }
178*81ad6265SDimitry Andric 
179*81ad6265SDimitry Andric void FunctionPropertiesUpdater::finish(FunctionAnalysisManager &FAM) const {
180*81ad6265SDimitry Andric   // Update feature values from the BBs that were copied from the callee, or
181*81ad6265SDimitry Andric   // might have been modified because of inlining. The latter have been
182*81ad6265SDimitry Andric   // subtracted in the FunctionPropertiesUpdater ctor.
183*81ad6265SDimitry Andric   // There could be successors that were reached before but now are only
184*81ad6265SDimitry Andric   // reachable from elsewhere in the CFG.
185*81ad6265SDimitry Andric   // One example is the following diamond CFG (lines are arrows pointing down):
186*81ad6265SDimitry Andric   //    A
187*81ad6265SDimitry Andric   //  /   \
188*81ad6265SDimitry Andric   // B     C
189*81ad6265SDimitry Andric   // |     |
190*81ad6265SDimitry Andric   // |     D
191*81ad6265SDimitry Andric   // |     |
192*81ad6265SDimitry Andric   // |     E
193*81ad6265SDimitry Andric   //  \   /
194*81ad6265SDimitry Andric   //    F
195*81ad6265SDimitry Andric   // There's a call site in C that is inlined. Upon doing that, it turns out
196*81ad6265SDimitry Andric   // it expands to
197*81ad6265SDimitry Andric   //   call void @llvm.trap()
198*81ad6265SDimitry Andric   //   unreachable
199*81ad6265SDimitry Andric   // F isn't reachable from C anymore, but we did discount it when we set up
200*81ad6265SDimitry Andric   // FunctionPropertiesUpdater, so we need to re-include it here.
201*81ad6265SDimitry Andric   // At the same time, D and E were reachable before, but now are not anymore,
202*81ad6265SDimitry Andric   // so we need to leave D out (we discounted it at setup), and explicitly
203*81ad6265SDimitry Andric   // remove E.
204*81ad6265SDimitry Andric   SetVector<const BasicBlock *> Reinclude;
205*81ad6265SDimitry Andric   SetVector<const BasicBlock *> Unreachable;
206*81ad6265SDimitry Andric   const auto &DT =
207*81ad6265SDimitry Andric       FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(Caller));
208*81ad6265SDimitry Andric 
209*81ad6265SDimitry Andric   if (&CallSiteBB != &*Caller.begin())
210*81ad6265SDimitry Andric     Reinclude.insert(&*Caller.begin());
211*81ad6265SDimitry Andric 
212*81ad6265SDimitry Andric   // Distribute the successors to the 2 buckets.
213*81ad6265SDimitry Andric   for (const auto *Succ : Successors)
214*81ad6265SDimitry Andric     if (DT.isReachableFromEntry(Succ))
215*81ad6265SDimitry Andric       Reinclude.insert(Succ);
216*81ad6265SDimitry Andric     else
217*81ad6265SDimitry Andric       Unreachable.insert(Succ);
218*81ad6265SDimitry Andric 
219*81ad6265SDimitry Andric   // For reinclusion, we want to stop at the reachable successors, who are at
220*81ad6265SDimitry Andric   // the beginning of the worklist; but, starting from the callsite bb and
221*81ad6265SDimitry Andric   // ending at those successors, we also want to perform a traversal.
222*81ad6265SDimitry Andric   // IncludeSuccessorsMark is the index after which we include successors.
223*81ad6265SDimitry Andric   const auto IncludeSuccessorsMark = Reinclude.size();
224*81ad6265SDimitry Andric   bool CSInsertion = Reinclude.insert(&CallSiteBB);
225*81ad6265SDimitry Andric   (void)CSInsertion;
226*81ad6265SDimitry Andric   assert(CSInsertion);
227*81ad6265SDimitry Andric   for (size_t I = 0; I < Reinclude.size(); ++I) {
228*81ad6265SDimitry Andric     const auto *BB = Reinclude[I];
229*81ad6265SDimitry Andric     FPI.reIncludeBB(*BB);
230*81ad6265SDimitry Andric     if (I >= IncludeSuccessorsMark)
231*81ad6265SDimitry Andric       Reinclude.insert(succ_begin(BB), succ_end(BB));
232*81ad6265SDimitry Andric   }
233*81ad6265SDimitry Andric 
234*81ad6265SDimitry Andric   // For exclusion, we don't need to exclude the set of BBs that were successors
235*81ad6265SDimitry Andric   // before and are now unreachable, because we already did that at setup. For
236*81ad6265SDimitry Andric   // the rest, as long as a successor is unreachable, we want to explicitly
237*81ad6265SDimitry Andric   // exclude it.
238*81ad6265SDimitry Andric   const auto AlreadyExcludedMark = Unreachable.size();
239*81ad6265SDimitry Andric   for (size_t I = 0; I < Unreachable.size(); ++I) {
240*81ad6265SDimitry Andric     const auto *U = Unreachable[I];
241*81ad6265SDimitry Andric     if (I >= AlreadyExcludedMark)
242*81ad6265SDimitry Andric       FPI.updateForBB(*U, -1);
243*81ad6265SDimitry Andric     for (const auto *Succ : successors(U))
244*81ad6265SDimitry Andric       if (!DT.isReachableFromEntry(Succ))
245*81ad6265SDimitry Andric         Unreachable.insert(Succ);
246*81ad6265SDimitry Andric   }
247*81ad6265SDimitry Andric 
248*81ad6265SDimitry Andric   const auto &LI = FAM.getResult<LoopAnalysis>(const_cast<Function &>(Caller));
249*81ad6265SDimitry Andric   FPI.updateAggregateStats(Caller, LI);
250*81ad6265SDimitry Andric   assert(FPI == FunctionPropertiesInfo::getFunctionPropertiesInfo(Caller, FAM));
251*81ad6265SDimitry Andric }
252