LLVM  14.0.0git
PassInstrumentation.h
Go to the documentation of this file.
1 //===- llvm/IR/PassInstrumentation.h ----------------------*- 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 /// \file
9 ///
10 /// This file defines the Pass Instrumentation classes that provide
11 /// instrumentation points into the pass execution by PassManager.
12 ///
13 /// There are two main classes:
14 /// - PassInstrumentation provides a set of instrumentation points for
15 /// pass managers to call on.
16 ///
17 /// - PassInstrumentationCallbacks registers callbacks and provides access
18 /// to them for PassInstrumentation.
19 ///
20 /// PassInstrumentation object is being used as a result of
21 /// PassInstrumentationAnalysis (so it is intended to be easily copyable).
22 ///
23 /// Intended scheme of use for Pass Instrumentation is as follows:
24 /// - register instrumentation callbacks in PassInstrumentationCallbacks
25 /// instance. PassBuilder provides helper for that.
26 ///
27 /// - register PassInstrumentationAnalysis with all the PassManagers.
28 /// PassBuilder handles that automatically when registering analyses.
29 ///
30 /// - Pass Manager requests PassInstrumentationAnalysis from analysis manager
31 /// and gets PassInstrumentation as its result.
32 ///
33 /// - Pass Manager invokes PassInstrumentation entry points appropriately,
34 /// passing StringRef identification ("name") of the pass currently being
35 /// executed and IRUnit it works on. There can be different schemes of
36 /// providing names in future, currently it is just a name() of the pass.
37 ///
38 /// - PassInstrumentation wraps address of IRUnit into llvm::Any and passes
39 /// control to all the registered callbacks. Note that we specifically wrap
40 /// 'const IRUnitT*' so as to avoid any accidental changes to IR in
41 /// instrumenting callbacks.
42 ///
43 /// - Some instrumentation points (BeforePass) allow to control execution
44 /// of a pass. For those callbacks returning false means pass will not be
45 /// executed.
46 ///
47 //===----------------------------------------------------------------------===//
48 
49 #ifndef LLVM_IR_PASSINSTRUMENTATION_H
50 #define LLVM_IR_PASSINSTRUMENTATION_H
51 
52 #include "llvm/ADT/Any.h"
54 #include "llvm/ADT/SmallVector.h"
55 #include "llvm/ADT/StringMap.h"
56 #include <type_traits>
57 #include <vector>
58 
59 namespace llvm {
60 
61 class PreservedAnalyses;
62 class StringRef;
63 
64 /// This class manages callbacks registration, as well as provides a way for
65 /// PassInstrumentation to pass control to the registered callbacks.
67 public:
68  // Before/After callbacks accept IRUnits whenever appropriate, so they need
69  // to take them as constant pointers, wrapped with llvm::Any.
70  // For the case when IRUnit has been invalidated there is a different
71  // callback to use - AfterPassInvalidated.
72  // We call all BeforePassFuncs to determine if a pass should run or not.
73  // BeforeNonSkippedPassFuncs are called only if the pass should run.
74  // TODO: currently AfterPassInvalidated does not accept IRUnit, since passing
75  // already invalidated IRUnit is unsafe. There are ways to handle invalidated
76  // IRUnits in a safe way, and we might pursue that as soon as there is a
77  // useful instrumentation that needs it.
78  using BeforePassFunc = bool(StringRef, Any);
81  using AfterPassFunc = void(StringRef, Any, const PreservedAnalyses &);
87 
88 public:
90 
91  /// Copying PassInstrumentationCallbacks is not intended.
93  void operator=(const PassInstrumentationCallbacks &) = delete;
94 
95  template <typename CallableT>
97  ShouldRunOptionalPassCallbacks.emplace_back(std::move(C));
98  }
99 
100  template <typename CallableT>
102  BeforeSkippedPassCallbacks.emplace_back(std::move(C));
103  }
104 
105  template <typename CallableT>
107  BeforeNonSkippedPassCallbacks.emplace_back(std::move(C));
108  }
109 
110  template <typename CallableT> void registerAfterPassCallback(CallableT C) {
111  AfterPassCallbacks.emplace_back(std::move(C));
112  }
113 
114  template <typename CallableT>
116  AfterPassInvalidatedCallbacks.emplace_back(std::move(C));
117  }
118 
119  template <typename CallableT>
121  BeforeAnalysisCallbacks.emplace_back(std::move(C));
122  }
123 
124  template <typename CallableT>
126  AfterAnalysisCallbacks.emplace_back(std::move(C));
127  }
128 
129  template <typename CallableT>
131  AnalysisInvalidatedCallbacks.emplace_back(std::move(C));
132  }
133 
134  template <typename CallableT>
136  AnalysesClearedCallbacks.emplace_back(std::move(C));
137  }
138 
139  /// Add a class name to pass name mapping for use by pass instrumentation.
141  /// Get the pass name for a given pass class name.
143 
144 private:
145  friend class PassInstrumentation;
146 
147  /// These are only run on passes that are not required. They return false when
148  /// an optional pass should be skipped.
150  ShouldRunOptionalPassCallbacks;
151  /// These are run on passes that are skipped.
153  BeforeSkippedPassCallbacks;
154  /// These are run on passes that are about to be run.
156  BeforeNonSkippedPassCallbacks;
157  /// These are run on passes that have just run.
159  /// These are run passes that have just run on invalidated IR.
161  AfterPassInvalidatedCallbacks;
162  /// These are run on analyses that are about to be run.
164  BeforeAnalysisCallbacks;
165  /// These are run on analyses that have been run.
167  AfterAnalysisCallbacks;
168  /// These are run on analyses that have been invalidated.
170  AnalysisInvalidatedCallbacks;
171  /// These are run on analyses that have been cleared.
173  AnalysesClearedCallbacks;
174 
175  StringMap<std::string> ClassToPassName;
176 };
177 
178 /// This class provides instrumentation entry points for the Pass Manager,
179 /// doing calls to callbacks registered in PassInstrumentationCallbacks.
181  PassInstrumentationCallbacks *Callbacks;
182 
183  // Template argument PassT of PassInstrumentation::runBeforePass could be two
184  // kinds: (1) a regular pass inherited from PassInfoMixin (happen when
185  // creating a adaptor pass for a regular pass); (2) a type-erased PassConcept
186  // created from (1). Here we want to make case (1) skippable unconditionally
187  // since they are regular passes. We call PassConcept::isRequired to decide
188  // for case (2).
189  template <typename PassT>
190  using has_required_t = decltype(std::declval<PassT &>().isRequired());
191 
192  template <typename PassT>
193  static std::enable_if_t<is_detected<has_required_t, PassT>::value, bool>
194  isRequired(const PassT &Pass) {
195  return Pass.isRequired();
196  }
197  template <typename PassT>
198  static std::enable_if_t<!is_detected<has_required_t, PassT>::value, bool>
199  isRequired(const PassT &Pass) {
200  return false;
201  }
202 
203 public:
204  /// Callbacks object is not owned by PassInstrumentation, its life-time
205  /// should at least match the life-time of corresponding
206  /// PassInstrumentationAnalysis (which usually is till the end of current
207  /// compilation).
209  : Callbacks(CB) {}
210 
211  /// BeforePass instrumentation point - takes \p Pass instance to be executed
212  /// and constant reference to IR it operates on. \Returns true if pass is
213  /// allowed to be executed. These are only run on optional pass since required
214  /// passes must always be run. This allows these callbacks to print info when
215  /// they want to skip a pass.
216  template <typename IRUnitT, typename PassT>
217  bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const {
218  if (!Callbacks)
219  return true;
220 
221  bool ShouldRun = true;
222  if (!isRequired(Pass)) {
223  for (auto &C : Callbacks->ShouldRunOptionalPassCallbacks)
224  ShouldRun &= C(Pass.name(), llvm::Any(&IR));
225  }
226 
227  if (ShouldRun) {
228  for (auto &C : Callbacks->BeforeNonSkippedPassCallbacks)
229  C(Pass.name(), llvm::Any(&IR));
230  } else {
231  for (auto &C : Callbacks->BeforeSkippedPassCallbacks)
232  C(Pass.name(), llvm::Any(&IR));
233  }
234 
235  return ShouldRun;
236  }
237 
238  /// AfterPass instrumentation point - takes \p Pass instance that has
239  /// just been executed and constant reference to \p IR it operates on.
240  /// \p IR is guaranteed to be valid at this point.
241  template <typename IRUnitT, typename PassT>
242  void runAfterPass(const PassT &Pass, const IRUnitT &IR,
243  const PreservedAnalyses &PA) const {
244  if (Callbacks)
245  for (auto &C : Callbacks->AfterPassCallbacks)
246  C(Pass.name(), llvm::Any(&IR), PA);
247  }
248 
249  /// AfterPassInvalidated instrumentation point - takes \p Pass instance
250  /// that has just been executed. For use when IR has been invalidated
251  /// by \p Pass execution.
252  template <typename IRUnitT, typename PassT>
253  void runAfterPassInvalidated(const PassT &Pass,
254  const PreservedAnalyses &PA) const {
255  if (Callbacks)
256  for (auto &C : Callbacks->AfterPassInvalidatedCallbacks)
257  C(Pass.name(), PA);
258  }
259 
260  /// BeforeAnalysis instrumentation point - takes \p Analysis instance
261  /// to be executed and constant reference to IR it operates on.
262  template <typename IRUnitT, typename PassT>
263  void runBeforeAnalysis(const PassT &Analysis, const IRUnitT &IR) const {
264  if (Callbacks)
265  for (auto &C : Callbacks->BeforeAnalysisCallbacks)
266  C(Analysis.name(), llvm::Any(&IR));
267  }
268 
269  /// AfterAnalysis instrumentation point - takes \p Analysis instance
270  /// that has just been executed and constant reference to IR it operated on.
271  template <typename IRUnitT, typename PassT>
272  void runAfterAnalysis(const PassT &Analysis, const IRUnitT &IR) const {
273  if (Callbacks)
274  for (auto &C : Callbacks->AfterAnalysisCallbacks)
275  C(Analysis.name(), llvm::Any(&IR));
276  }
277 
278  /// AnalysisInvalidated instrumentation point - takes \p Analysis instance
279  /// that has just been invalidated and constant reference to IR it operated
280  /// on.
281  template <typename IRUnitT, typename PassT>
282  void runAnalysisInvalidated(const PassT &Analysis, const IRUnitT &IR) const {
283  if (Callbacks)
284  for (auto &C : Callbacks->AnalysisInvalidatedCallbacks)
285  C(Analysis.name(), llvm::Any(&IR));
286  }
287 
288  /// AnalysesCleared instrumentation point - takes name of IR that analyses
289  /// operated on.
291  if (Callbacks)
292  for (auto &C : Callbacks->AnalysesClearedCallbacks)
293  C(Name);
294  }
295 
296  /// Handle invalidation from the pass manager when PassInstrumentation
297  /// is used as the result of PassInstrumentationAnalysis.
298  ///
299  /// On attempt to invalidate just return false. There is nothing to become
300  /// invalid here.
301  template <typename IRUnitT, typename... ExtraArgsT>
302  bool invalidate(IRUnitT &, const class llvm::PreservedAnalyses &,
303  ExtraArgsT...) {
304  return false;
305  }
306 
307  template <typename CallableT>
309  if (Callbacks)
310  Callbacks->BeforeNonSkippedPassCallbacks.emplace_back(std::move(C));
311  }
313  if (Callbacks)
314  Callbacks->BeforeNonSkippedPassCallbacks.pop_back();
315  }
316 };
317 
318 bool isSpecialPass(StringRef PassID, const std::vector<StringRef> &Specials);
319 
320 } // namespace llvm
321 
322 #endif
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::PassInstrumentationCallbacks::BeforeNonSkippedPassFunc
void(StringRef, Any) BeforeNonSkippedPassFunc
Definition: PassInstrumentation.h:80
llvm::PassInstrumentationCallbacks::BeforeAnalysisFunc
void(StringRef, Any) BeforeAnalysisFunc
Definition: PassInstrumentation.h:83
llvm::PassInstrumentationCallbacks::AfterPassInvalidatedFunc
void(StringRef, const PreservedAnalyses &) AfterPassInvalidatedFunc
Definition: PassInstrumentation.h:82
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
FunctionExtras.h
llvm::PassInstrumentationCallbacks::registerBeforeNonSkippedPassCallback
void registerBeforeNonSkippedPassCallback(CallableT C)
Definition: PassInstrumentation.h:106
llvm::PassInstrumentationCallbacks::BeforeSkippedPassFunc
void(StringRef, Any) BeforeSkippedPassFunc
Definition: PassInstrumentation.h:79
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::PassInstrumentation::runBeforeAnalysis
void runBeforeAnalysis(const PassT &Analysis, const IRUnitT &IR) const
BeforeAnalysis instrumentation point - takes Analysis instance to be executed and constant reference ...
Definition: PassInstrumentation.h:263
llvm::PassInstrumentation::pushBeforeNonSkippedPassCallback
void pushBeforeNonSkippedPassCallback(CallableT C)
Definition: PassInstrumentation.h:308
Analysis
block Block Frequency Analysis
Definition: BlockFrequencyInfo.cpp:302
llvm::PassInstrumentationCallbacks::registerAnalysesClearedCallback
void registerAnalysesClearedCallback(CallableT C)
Definition: PassInstrumentation.h:135
llvm::PassInstrumentationCallbacks::getPassNameForClassName
StringRef getPassNameForClassName(StringRef ClassName)
Get the pass name for a given pass class name.
Definition: PassInstrumentation.cpp:27
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::isSpecialPass
bool isSpecialPass(StringRef PassID, const std::vector< StringRef > &Specials)
Definition: PassInstrumentation.cpp:33
llvm::PassInstrumentation::invalidate
bool invalidate(IRUnitT &, const class llvm::PreservedAnalyses &, ExtraArgsT...)
Handle invalidation from the pass manager when PassInstrumentation is used as the result of PassInstr...
Definition: PassInstrumentation.h:302
IR
Statically lint checks LLVM IR
Definition: Lint.cpp:746
StringMap.h
llvm::PassInstrumentationCallbacks::registerShouldRunOptionalPassCallback
void registerShouldRunOptionalPassCallback(CallableT C)
Definition: PassInstrumentation.h:96
llvm::StringMap< std::string >
llvm::PassInstrumentationCallbacks::registerBeforeAnalysisCallback
void registerBeforeAnalysisCallback(CallableT C)
Definition: PassInstrumentation.h:120
llvm::PassInstrumentation::runAnalysisInvalidated
void runAnalysisInvalidated(const PassT &Analysis, const IRUnitT &IR) const
AnalysisInvalidated instrumentation point - takes Analysis instance that has just been invalidated an...
Definition: PassInstrumentation.h:282
llvm::PassInstrumentationCallbacks::AfterPassFunc
void(StringRef, Any, const PreservedAnalyses &) AfterPassFunc
Definition: PassInstrumentation.h:81
llvm::PassInstrumentationCallbacks::AnalysesClearedFunc
void(StringRef) AnalysesClearedFunc
Definition: PassInstrumentation.h:86
llvm::PassInstrumentationCallbacks::registerAnalysisInvalidatedCallback
void registerAnalysisInvalidatedCallback(CallableT C)
Definition: PassInstrumentation.h:130
llvm::PassInstrumentation::runAfterPass
void runAfterPass(const PassT &Pass, const IRUnitT &IR, const PreservedAnalyses &PA) const
AfterPass instrumentation point - takes Pass instance that has just been executed and constant refere...
Definition: PassInstrumentation.h:242
llvm::PassInstrumentation::PassInstrumentation
PassInstrumentation(PassInstrumentationCallbacks *CB=nullptr)
Callbacks object is not owned by PassInstrumentation, its life-time should at least match the life-ti...
Definition: PassInstrumentation.h:208
llvm::PassInstrumentationCallbacks::AfterAnalysisFunc
void(StringRef, Any) AfterAnalysisFunc
Definition: PassInstrumentation.h:84
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::PassInstrumentation::runAfterPassInvalidated
void runAfterPassInvalidated(const PassT &Pass, const PreservedAnalyses &PA) const
AfterPassInvalidated instrumentation point - takes Pass instance that has just been executed.
Definition: PassInstrumentation.h:253
llvm::PassInstrumentationCallbacks::BeforePassFunc
bool(StringRef, Any) BeforePassFunc
Definition: PassInstrumentation.h:78
llvm::PassInstrumentation
This class provides instrumentation entry points for the Pass Manager, doing calls to callbacks regis...
Definition: PassInstrumentation.h:180
llvm::PassInstrumentationCallbacks::operator=
void operator=(const PassInstrumentationCallbacks &)=delete
llvm::PassInstrumentation::runAfterAnalysis
void runAfterAnalysis(const PassT &Analysis, const IRUnitT &IR) const
AfterAnalysis instrumentation point - takes Analysis instance that has just been executed and constan...
Definition: PassInstrumentation.h:272
llvm::PassInstrumentationCallbacks::addClassToPassName
void addClassToPassName(StringRef ClassName, StringRef PassName)
Add a class name to pass name mapping for use by pass instrumentation.
Definition: PassInstrumentation.cpp:20
llvm::PassInstrumentation::popBeforeNonSkippedPassCallback
void popBeforeNonSkippedPassCallback()
Definition: PassInstrumentation.h:312
llvm::PassInstrumentationCallbacks::AnalysisInvalidatedFunc
void(StringRef, Any) AnalysisInvalidatedFunc
Definition: PassInstrumentation.h:85
llvm::PassInstrumentationCallbacks::registerAfterAnalysisCallback
void registerAfterAnalysisCallback(CallableT C)
Definition: PassInstrumentation.h:125
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::PassInstrumentationCallbacks::registerAfterPassInvalidatedCallback
void registerAfterPassInvalidatedCallback(CallableT C)
Definition: PassInstrumentation.h:115
llvm::Any
Definition: Any.h:26
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::PassInstrumentation::runBeforePass
bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const
BeforePass instrumentation point - takes Pass instance to be executed and constant reference to IR it...
Definition: PassInstrumentation.h:217
llvm::PassInstrumentationCallbacks::PassInstrumentationCallbacks
PassInstrumentationCallbacks()
Definition: PassInstrumentation.h:89
llvm::PassInstrumentationCallbacks::registerBeforeSkippedPassCallback
void registerBeforeSkippedPassCallback(CallableT C)
Definition: PassInstrumentation.h:101
Any.h
llvm::PassInstrumentationCallbacks::registerAfterPassCallback
void registerAfterPassCallback(CallableT C)
Definition: PassInstrumentation.h:110
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
SmallVector.h
llvm::PassInstrumentationCallbacks
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
Definition: PassInstrumentation.h:66
llvm::PassInstrumentation::runAnalysesCleared
void runAnalysesCleared(StringRef Name) const
AnalysesCleared instrumentation point - takes name of IR that analyses operated on.
Definition: PassInstrumentation.h:290
PassName
static const char PassName[]
Definition: X86LowerAMXIntrinsics.cpp:670
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:908