LLVM 18.0.0git
FunctionPropertiesAnalysis.cpp
Go to the documentation of this file.
1//===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
10// classes used to extract function properties.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SetVector.h"
18#include "llvm/IR/CFG.h"
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/Dominators.h"
24#include <deque>
25
26using namespace llvm;
27
29 "enable-detailed-function-properties", cl::Hidden, cl::init(false),
30 cl::desc("Whether or not to compute detailed function properties."));
31
33 "big-basic-block-instruction-threshold", cl::Hidden, cl::init(500),
34 cl::desc("The minimum number of instructions a basic block should contain "
35 "before being considered big."));
36
38 "medium-basic-block-instruction-threshold", cl::Hidden, cl::init(15),
39 cl::desc("The minimum number of instructions a basic block should contain "
40 "before being considered medium-sized."));
41
43 "call-with-many-arguments-threshold", cl::Hidden, cl::init(4),
44 cl::desc("The minimum number of arguments a function call must have before "
45 "it is considered having many arguments."));
46
47namespace {
48int64_t getNrBlocksFromCond(const BasicBlock &BB) {
49 int64_t Ret = 0;
50 if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
51 if (BI->isConditional())
52 Ret += BI->getNumSuccessors();
53 } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
54 Ret += (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
55 }
56 return Ret;
57}
58
59int64_t getUses(const Function &F) {
60 return ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
61}
62} // namespace
63
64void FunctionPropertiesInfo::reIncludeBB(const BasicBlock &BB) {
65 updateForBB(BB, +1);
66}
67
68void FunctionPropertiesInfo::updateForBB(const BasicBlock &BB,
69 int64_t Direction) {
70 assert(Direction == 1 || Direction == -1);
73 (Direction * getNrBlocksFromCond(BB));
74 for (const auto &I : BB) {
75 if (auto *CS = dyn_cast<CallBase>(&I)) {
76 const auto *Callee = CS->getCalledFunction();
77 if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
79 }
80 if (I.getOpcode() == Instruction::Load) {
82 } else if (I.getOpcode() == Instruction::Store) {
84 }
85 }
86 TotalInstructionCount += Direction * BB.sizeWithoutDebug();
87
89 unsigned SuccessorCount = succ_size(&BB);
90 if (SuccessorCount == 1)
92 else if (SuccessorCount == 2)
94 else if (SuccessorCount > 2)
96
97 unsigned PredecessorCount = pred_size(&BB);
98 if (PredecessorCount == 1)
100 else if (PredecessorCount == 2)
102 else if (PredecessorCount > 2)
104
109 else
111
112 // Calculate critical edges by looking through all successors of a basic
113 // block that has multiple successors and finding ones that have multiple
114 // predecessors, which represent critical edges.
115 if (SuccessorCount > 1) {
116 for (const auto *Successor : successors(&BB)) {
117 if (pred_size(Successor) > 1)
119 }
120 }
121
122 ControlFlowEdgeCount += Direction * SuccessorCount;
123
124 if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
125 if (!BI->isConditional())
127 }
128
129 for (const Instruction &I : BB.instructionsWithoutDebug()) {
130 if (I.isCast())
132
133 if (I.getType()->isFloatTy())
135 else if (I.getType()->isIntegerTy())
137
138 if (isa<IntrinsicInst>(I))
140
141 if (const auto *Call = dyn_cast<CallInst>(&I)) {
142 if (Call->isIndirectCall())
144 else
146
147 if (Call->getType()->isIntegerTy())
149 else if (Call->getType()->isFloatingPointTy())
151 else if (Call->getType()->isPointerTy())
153 else if (Call->getType()->isVectorTy()) {
154 if (Call->getType()->getScalarType()->isIntegerTy())
156 else if (Call->getType()->getScalarType()->isFloatingPointTy())
158 else if (Call->getType()->getScalarType()->isPointerTy())
160 }
161
162 if (Call->arg_size() > CallWithManyArgumentsThreshold)
164
165 for (const auto &Arg : Call->args()) {
166 if (Arg->getType()->isPointerTy()) {
168 break;
169 }
170 }
171 }
172
173#define COUNT_OPERAND(OPTYPE) \
174 if (isa<OPTYPE>(Operand)) { \
175 OPTYPE##OperandCount += Direction; \
176 continue; \
177 }
178
179 for (unsigned int OperandIndex = 0; OperandIndex < I.getNumOperands();
180 ++OperandIndex) {
181 Value *Operand = I.getOperand(OperandIndex);
190
191 // We only get to this point if we haven't matched any of the other
192 // operand types.
194 }
195
196#undef CHECK_OPERAND
197 }
198 }
199}
200
201void FunctionPropertiesInfo::updateAggregateStats(const Function &F,
202 const LoopInfo &LI) {
203
204 Uses = getUses(F);
206 MaxLoopDepth = 0;
207 std::deque<const Loop *> Worklist;
208 llvm::append_range(Worklist, LI);
209 while (!Worklist.empty()) {
210 const auto *L = Worklist.front();
212 std::max(MaxLoopDepth, static_cast<int64_t>(L->getLoopDepth()));
213 Worklist.pop_front();
214 llvm::append_range(Worklist, L->getSubLoops());
215 }
216}
217
222}
223
225 const Function &F, const DominatorTree &DT, const LoopInfo &LI) {
226
228 for (const auto &BB : F)
229 if (DT.isReachableFromEntry(&BB))
230 FPI.reIncludeBB(BB);
231 FPI.updateAggregateStats(F, LI);
232 return FPI;
233}
234
236#define PRINT_PROPERTY(PROP_NAME) OS << #PROP_NAME ": " << PROP_NAME << "\n";
237
247
284 }
285
286#undef PRINT_PROPERTY
287
288 OS << "\n";
289}
290
292
296}
297
300 OS << "Printing analysis results of CFA for function "
301 << "'" << F.getName() << "':"
302 << "\n";
304 return PreservedAnalyses::all();
305}
306
309 : FPI(FPI), CallSiteBB(*CB.getParent()), Caller(*CallSiteBB.getParent()) {
310 assert(isa<CallInst>(CB) || isa<InvokeInst>(CB));
311 // For BBs that are likely to change, we subtract from feature totals their
312 // contribution. Some features, like max loop counts or depths, are left
313 // invalid, as they will be updated post-inlining.
314 SmallPtrSet<const BasicBlock *, 4> LikelyToChangeBBs;
315 // The CB BB will change - it'll either be split or the callee's body (single
316 // BB) will be pasted in.
317 LikelyToChangeBBs.insert(&CallSiteBB);
318
319 // The caller's entry BB may change due to new alloca instructions.
320 LikelyToChangeBBs.insert(&*Caller.begin());
321
322 // The successors may become unreachable in the case of `invoke` inlining.
323 // We track successors separately, too, because they form a boundary, together
324 // with the CB BB ('Entry') between which the inlined callee will be pasted.
325 Successors.insert(succ_begin(&CallSiteBB), succ_end(&CallSiteBB));
326
327 // Inlining only handles invoke and calls. If this is an invoke, and inlining
328 // it pulls another invoke, the original landing pad may get split, so as to
329 // share its content with other potential users. So the edge up to which we
330 // need to invalidate and then re-account BB data is the successors of the
331 // current landing pad. We can leave the current lp, too - if it doesn't get
332 // split, then it will be the place traversal stops. Either way, the
333 // discounted BBs will be checked if reachable and re-added.
334 if (const auto *II = dyn_cast<InvokeInst>(&CB)) {
335 const auto *UnwindDest = II->getUnwindDest();
336 Successors.insert(succ_begin(UnwindDest), succ_end(UnwindDest));
337 }
338
339 // Exclude the CallSiteBB, if it happens to be its own successor (1-BB loop).
340 // We are only interested in BBs the graph moves past the callsite BB to
341 // define the frontier past which we don't want to re-process BBs. Including
342 // the callsite BB in this case would prematurely stop the traversal in
343 // finish().
344 Successors.erase(&CallSiteBB);
345
346 for (const auto *BB : Successors)
347 LikelyToChangeBBs.insert(BB);
348
349 // Commit the change. While some of the BBs accounted for above may play dual
350 // role - e.g. caller's entry BB may be the same as the callsite BB - set
351 // insertion semantics make sure we account them once. This needs to be
352 // followed in `finish`, too.
353 for (const auto *BB : LikelyToChangeBBs)
354 FPI.updateForBB(*BB, -1);
355}
356
358 // Update feature values from the BBs that were copied from the callee, or
359 // might have been modified because of inlining. The latter have been
360 // subtracted in the FunctionPropertiesUpdater ctor.
361 // There could be successors that were reached before but now are only
362 // reachable from elsewhere in the CFG.
363 // One example is the following diamond CFG (lines are arrows pointing down):
364 // A
365 // / \
366 // B C
367 // | |
368 // | D
369 // | |
370 // | E
371 // \ /
372 // F
373 // There's a call site in C that is inlined. Upon doing that, it turns out
374 // it expands to
375 // call void @llvm.trap()
376 // unreachable
377 // F isn't reachable from C anymore, but we did discount it when we set up
378 // FunctionPropertiesUpdater, so we need to re-include it here.
379 // At the same time, D and E were reachable before, but now are not anymore,
380 // so we need to leave D out (we discounted it at setup), and explicitly
381 // remove E.
384 const auto &DT =
385 FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(Caller));
386
387 if (&CallSiteBB != &*Caller.begin())
388 Reinclude.insert(&*Caller.begin());
389
390 // Distribute the successors to the 2 buckets.
391 for (const auto *Succ : Successors)
392 if (DT.isReachableFromEntry(Succ))
393 Reinclude.insert(Succ);
394 else
395 Unreachable.insert(Succ);
396
397 // For reinclusion, we want to stop at the reachable successors, who are at
398 // the beginning of the worklist; but, starting from the callsite bb and
399 // ending at those successors, we also want to perform a traversal.
400 // IncludeSuccessorsMark is the index after which we include successors.
401 const auto IncludeSuccessorsMark = Reinclude.size();
402 bool CSInsertion = Reinclude.insert(&CallSiteBB);
403 (void)CSInsertion;
404 assert(CSInsertion);
405 for (size_t I = 0; I < Reinclude.size(); ++I) {
406 const auto *BB = Reinclude[I];
407 FPI.reIncludeBB(*BB);
408 if (I >= IncludeSuccessorsMark)
409 Reinclude.insert(succ_begin(BB), succ_end(BB));
410 }
411
412 // For exclusion, we don't need to exclude the set of BBs that were successors
413 // before and are now unreachable, because we already did that at setup. For
414 // the rest, as long as a successor is unreachable, we want to explicitly
415 // exclude it.
416 const auto AlreadyExcludedMark = Unreachable.size();
417 for (size_t I = 0; I < Unreachable.size(); ++I) {
418 const auto *U = Unreachable[I];
419 if (I >= AlreadyExcludedMark)
420 FPI.updateForBB(*U, -1);
421 for (const auto *Succ : successors(U))
422 if (!DT.isReachableFromEntry(Succ))
423 Unreachable.insert(Succ);
424 }
425
426 const auto &LI = FAM.getResult<LoopAnalysis>(const_cast<Function &>(Caller));
427 FPI.updateAggregateStats(Caller, LI);
428}
429
430bool FunctionPropertiesUpdater::isUpdateValid(Function &F,
431 const FunctionPropertiesInfo &FPI,
433 DominatorTree DT(F);
434 LoopInfo LI(DT);
436 return FPI == Fresh;
437}
static const Function * getParent(const Value *V)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define PRINT_PROPERTY(PROP_NAME)
cl::opt< unsigned > CallWithManyArgumentsThreshold("call-with-many-arguments-threshold", cl::Hidden, cl::init(4), cl::desc("The minimum number of arguments a function call must have before " "it is considered having many arguments."))
#define COUNT_OPERAND(OPTYPE)
cl::opt< unsigned > BigBasicBlockInstructionThreshold("big-basic-block-instruction-threshold", cl::Hidden, cl::init(500), cl::desc("The minimum number of instructions a basic block should contain " "before being considered big."))
cl::opt< unsigned > MediumBasicBlockInstructionThreshold("medium-basic-block-instruction-threshold", cl::Hidden, cl::init(15), cl::desc("The minimum number of instructions a basic block should contain " "before being considered medium-sized."))
cl::opt< bool > EnableDetailedFunctionProperties("enable-detailed-function-properties", cl::Hidden, cl::init(false), cl::desc("Whether or not to compute detailed function properties."))
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Loop::LoopBounds::Direction Direction
Definition: LoopInfo.cpp:230
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionAnalysisManager FAM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:127
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1190
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:260
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
This is an important base class in LLVM.
Definition: Constant.h:41
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
FunctionPropertiesInfo run(Function &F, FunctionAnalysisManager &FAM)
int64_t BasicBlockCount
Number of basic blocks.
int64_t Uses
Number of uses of this function, plus 1 if the function is callable outside the module.
int64_t BlocksReachedFromConditionalInstruction
Number of blocks reached from a conditional instruction, or that are 'cases' of a SwitchInstr.
static FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F, const DominatorTree &DT, const LoopInfo &LI)
int64_t DirectCallsToDefinedFunctions
Number of direct calls made from this function to other functions defined in this module.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
FunctionPropertiesUpdater(FunctionPropertiesInfo &FPI, CallBase &CB)
void finish(FunctionAnalysisManager &FAM) const
iterator begin()
Definition: Function.h:763
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:569
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
A vector that has set insertion semantics.
Definition: SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:366
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:451
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:102
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1685
auto successors(const MachineBasicBlock *BB)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:99
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:2037
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
unsigned succ_size(const MachineBasicBlock *BB)
unsigned pred_size(const MachineBasicBlock *BB)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:69
Direction
An enum for the direction of the loop.
Definition: LoopInfo.h:223