LLVM 22.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 {
24class BasicBlock;
25class CallBase;
26class DominatorTree;
27class Function;
28class LoopInfo;
29
32 void updateForBB(const BasicBlock &BB, int64_t Direction);
33 void updateAggregateStats(const Function &F, const LoopInfo &LI);
34 void reIncludeBB(const BasicBlock &BB);
35
36 ir2vec::Embedding FunctionEmbedding = ir2vec::Embedding(0.0);
37 const ir2vec::Vocabulary *IR2VecVocab = nullptr;
38
39public:
42 const LoopInfo &LI,
43 const ir2vec::Vocabulary *Vocabulary);
44
47
48 LLVM_ABI bool operator==(const FunctionPropertiesInfo &FPI) const;
49
50 bool operator!=(const FunctionPropertiesInfo &FPI) const {
51 return !(*this == FPI);
52 }
53
54 LLVM_ABI void print(raw_ostream &OS) const;
55
56 /// Number of basic blocks
57 int64_t BasicBlockCount = 0;
58
59 /// Number of blocks reached from a conditional instruction, or that are
60 /// 'cases' of a SwitchInstr.
61 // FIXME: We may want to replace this with a more meaningful metric, like
62 // number of conditionally executed blocks:
63 // 'if (a) s();' would be counted here as 2 blocks, just like
64 // 'if (a) s(); else s2(); s3();' would.
66
67 /// Number of uses of this function, plus 1 if the function is callable
68 /// outside the module.
69 int64_t Uses = 0;
70
71 /// Number of direct calls made from this function to other functions
72 /// defined in this module.
74
75 // Load Instruction Count
76 int64_t LoadInstCount = 0;
77
78 // Store Instruction Count
79 int64_t StoreInstCount = 0;
80
81 // Maximum Loop Depth in the Function
82 int64_t MaxLoopDepth = 0;
83
84 // Number of Top Level Loops in the Function
85 int64_t TopLevelLoopCount = 0;
86
87 // All non-debug instructions
89
90 // Basic blocks grouped by number of successors.
94
95 // Basic blocks grouped by number of predecessors.
99
100 // Basic blocks grouped by size as determined by the number of non-debug
101 // instructions that they contain.
102 int64_t BigBasicBlocks = 0;
103 int64_t MediumBasicBlocks = 0;
104 int64_t SmallBasicBlocks = 0;
105
106 // The number of cast instructions inside the function.
108
109 // The number of floating point instructions inside the function.
111
112 // The number of integer instructions inside the function.
114
115 // Operand type couns
125
126 // Additional CFG Properties
127 int64_t CriticalEdgeCount = 0;
130
131 // Call related instructions
132 int64_t IntrinsicCount = 0;
133 int64_t DirectCallCount = 0;
134 int64_t IndirectCallCount = 0;
143
145 return FunctionEmbedding;
146 }
147
148 const ir2vec::Vocabulary *getIR2VecVocab() const { return IR2VecVocab; }
149
150 // Helper intended to be useful for unittests
152 FunctionEmbedding = Embedding;
153 }
154};
155
156// Analysis pass
158 : public AnalysisInfoMixin<FunctionPropertiesAnalysis> {
159
160public:
162
164
167};
168
169/// Printer pass for the FunctionPropertiesAnalysis results.
171 : public PassInfoMixin<FunctionPropertiesPrinterPass> {
172 raw_ostream &OS;
173
174public:
176
178
179 static bool isRequired() { return true; }
180};
181
182/// Correctly update FunctionPropertiesInfo post-inlining. A
183/// FunctionPropertiesUpdater keeps the state necessary for tracking the changes
184/// llvm::InlineFunction makes. The idea is that inlining will at most modify
185/// a few BBs of the Caller (maybe the entry BB and definitely the callsite BB)
186/// and potentially affect exception handling BBs in the case of invoke
187/// inlining.
189public:
191
194 finish(FAM);
195 return isUpdateValid(Caller, FPI, FAM);
196 }
197
198private:
200 BasicBlock &CallSiteBB;
201 Function &Caller;
202
203 LLVM_ABI static bool isUpdateValid(Function &F,
204 const FunctionPropertiesInfo &FPI,
206
207 DominatorTree &getUpdatedDominatorTree(FunctionAnalysisManager &FAM) const;
208
211
212 // Edges we might potentially need to remove from the dominator tree.
214};
215} // namespace llvm
216#endif // LLVM_ANALYSIS_FUNCTIONPROPERTIESANALYSIS_H
#define LLVM_ABI
Definition: Compiler.h:213
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:243
#define F(x, y, z)
Definition: MD5.cpp:55
FunctionAnalysisManager FAM
raw_pwrite_stream & OS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
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)
Printer pass for the FunctionPropertiesAnalysis results.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Correctly update FunctionPropertiesInfo post-inlining.
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.
Definition: SmallVector.h:1197
Class for storing and accessing the IR2Vec vocabulary.
Definition: IR2Vec.h:157
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70
Embedding is a datatype that wraps std::vector<double>.
Definition: IR2Vec.h:79