LLVM 24.0.0git
FunctionPropertiesAnalysis.h
Go to the documentation of this file.
1//=- FunctionPropertiesAnalysis.h - Function Properties Analysis --*- C++ -*-=//
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
14#ifndef LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
15#define LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
16
17#include "llvm/ADT/DenseSet.h"
19#include "llvm/IR/Dominators.h"
20#include "llvm/IR/PassManager.h"
22
23namespace llvm {
24struct AnalysisOptions;
25class BasicBlock;
26class CallBase;
27class DominatorTree;
28class Function;
29class LoopInfo;
30
33 void updateForBB(const BasicBlock &BB, int64_t Direction);
34 void updateAggregateStats(const Function &F, const LoopInfo &LI);
35 void reIncludeBB(const BasicBlock &BB);
36
37 ir2vec::Embedding FunctionEmbedding = ir2vec::Embedding(0.0);
38 const ir2vec::Vocabulary *IR2VecVocab = nullptr;
39 // Non-null iff the detailed properties are computed.
40 const AnalysisOptions *DetailedOpts = nullptr;
41
42public:
45 const LoopInfo &LI,
46 const ir2vec::Vocabulary *Vocabulary);
47
50
51 LLVM_ABI bool operator==(const FunctionPropertiesInfo &FPI) const;
52
53 bool operator!=(const FunctionPropertiesInfo &FPI) const {
54 return !(*this == FPI);
55 }
56
57 LLVM_ABI void print(raw_ostream &OS) const;
58
59 /// Number of basic blocks
60 int64_t BasicBlockCount = 0;
61
62 /// Number of blocks reached from a conditional instruction, or that are
63 /// 'cases' of a SwitchInstr.
64 // FIXME: We may want to replace this with a more meaningful metric, like
65 // number of conditionally executed blocks:
66 // 'if (a) s();' would be counted here as 2 blocks, just like
67 // 'if (a) s(); else s2(); s3();' would.
69
70 /// Number of uses of this function, plus 1 if the function is callable
71 /// outside the module.
72 int64_t Uses = 0;
73
74 /// Number of direct calls made from this function to other functions
75 /// defined in this module.
77
78 // Load Instruction Count
79 int64_t LoadInstCount = 0;
80
81 // Store Instruction Count
82 int64_t StoreInstCount = 0;
83
84 // Maximum Loop Depth in the Function
85 int64_t MaxLoopDepth = 0;
86
87 // Number of Top Level Loops in the Function
88 int64_t TopLevelLoopCount = 0;
89
90 // All non-debug instructions
92
93 // Basic blocks grouped by number of successors.
97
98 // Basic blocks grouped by number of predecessors.
102
103 // Basic blocks grouped by size as determined by the number of non-debug
104 // instructions that they contain.
105 int64_t BigBasicBlocks = 0;
106 int64_t MediumBasicBlocks = 0;
107 int64_t SmallBasicBlocks = 0;
108
109 // The number of cast instructions inside the function.
111
112 // The number of floating point instructions inside the function.
114
115 // The number of integer instructions inside the function.
117
118 // Operand type couns
128
129 // Additional CFG Properties
130 int64_t CriticalEdgeCount = 0;
138
139 // Call related instructions
140 int64_t IntrinsicCount = 0;
141 int64_t NoReturnCallCount = 0;
142 int64_t DirectCallCount = 0;
143 int64_t IndirectCallCount = 0;
152
154 return FunctionEmbedding;
155 }
156
157 const ir2vec::Vocabulary *getIR2VecVocab() const { return IR2VecVocab; }
158
159 // Helper intended to be useful for unittests
161 FunctionEmbedding = Embedding;
162 }
163};
164
165// Analysis pass
167 : public AnalysisInfoMixin<FunctionPropertiesAnalysis> {
168
169public:
171
173
176};
177
178/// Printer pass for the FunctionPropertiesAnalysis results.
180 : public RequiredPassInfoMixin<FunctionPropertiesPrinterPass> {
181 raw_ostream &OS;
182
183public:
185
187};
188
189/// Statistics pass for the FunctionPropertiesAnalysis results.
191 : public RequiredPassInfoMixin<FunctionPropertiesStatisticsPass> {
192 bool IsPreOptimization;
193
194public:
195 explicit FunctionPropertiesStatisticsPass(bool IsPreOptimization = false)
196 : IsPreOptimization(IsPreOptimization) {}
197
199};
200
201/// Correctly update FunctionPropertiesInfo post-inlining. A
202/// FunctionPropertiesUpdater keeps the state necessary for tracking the changes
203/// llvm::InlineFunction makes. The idea is that inlining will at most modify
204/// a few BBs of the Caller (maybe the entry BB and definitely the callsite BB)
205/// and potentially affect exception handling BBs in the case of invoke
206/// inlining.
208public:
210
213 finish(FAM);
214 return isUpdateValid(Caller, FPI, FAM);
215 }
216
217private:
219 BasicBlock &CallSiteBB;
220 Function &Caller;
221
222 LLVM_ABI static bool isUpdateValid(Function &F,
223 const FunctionPropertiesInfo &FPI,
225
226 DominatorTree &getUpdatedDominatorTree(FunctionAnalysisManager &FAM) const;
227
230
231 // Edges we might potentially need to remove from the dominator tree.
233};
234} // namespace llvm
235#endif // LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseSet and SmallDenseSet classes.
This file defines the IR2Vec vocabulary analysis(IR2VecVocabAnalysis), the core ir2vec::Embedder inte...
This header defines various interfaces for pass management in LLVM.
Loop::LoopBounds::Direction Direction
Definition LoopInfo.cpp:254
#define F(x, y, z)
Definition MD5.cpp:54
FunctionAnalysisManager FAM
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI FunctionPropertiesInfo run(Function &F, FunctionAnalysisManager &FAM)
const ir2vec::Vocabulary * getIR2VecVocab() const
bool operator!=(const FunctionPropertiesInfo &FPI) const
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.
const ir2vec::Embedding & getFunctionEmbedding() const
int64_t BlocksReachedFromConditionalInstruction
Number of blocks reached from a conditional instruction, or that are 'cases' of a SwitchInstr.
LLVM_ABI bool operator==(const FunctionPropertiesInfo &FPI) const
static LLVM_ABI FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F, const DominatorTree &DT, const LoopInfo &LI, const ir2vec::Vocabulary *Vocabulary)
LLVM_ABI void print(raw_ostream &OS) const
int64_t DirectCallsToDefinedFunctions
Number of direct calls made from this function to other functions defined in this module.
void setFunctionEmbeddingForTest(const ir2vec::Embedding &Embedding)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
FunctionPropertiesStatisticsPass(bool IsPreOptimization=false)
LLVM_ABI FunctionPropertiesUpdater(FunctionPropertiesInfo &FPI, CallBase &CB)
LLVM_ABI void finish(FunctionAnalysisManager &FAM) const
bool finishAndTest(FunctionAnalysisManager &FAM) const
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class for storing and accessing the IR2Vec vocabulary.
Definition IR2Vec.h:245
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in for passes that should not be skipped.
Embedding is a datatype that wraps std::vector<double>.
Definition IR2Vec.h:85