LLVM  4.0.0
OptimizationDiagnosticInfo.h
Go to the documentation of this file.
1 //===- OptimizationDiagnosticInfo.h - Optimization Diagnostic ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Optimization diagnostic interfaces. It's packaged as an analysis pass so
11 // that by using this service passes become dependent on BFI as well. BFI is
12 // used to compute the "hotness" of the diagnostic message.
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
16 #define LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
17 
18 #include "llvm/ADT/Optional.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
24 
25 namespace llvm {
26 class DebugLoc;
27 class LLVMContext;
28 class Loop;
29 class Pass;
30 class Twine;
31 class Value;
32 
33 /// The optimization diagnostic interface.
34 ///
35 /// It allows reporting when optimizations are performed and when they are not
36 /// along with the reasons for it. Hotness information of the corresponding
37 /// code region can be included in the remark if DiagnosticHotnessRequested is
38 /// enabled in the LLVM context.
40 public:
42  : F(F), BFI(BFI) {}
43 
44  /// \brief This variant can be used to generate ORE on demand (without the
45  /// analysis pass).
46  ///
47  /// Note that this ctor has a very different cost depending on whether
48  /// F->getContext().getDiagnosticHotnessRequested() is on or not. If it's off
49  /// the operation is free.
50  ///
51  /// Whereas if DiagnosticHotnessRequested is on, it is fairly expensive
52  /// operation since BFI and all its required analyses are computed. This is
53  /// for example useful for CGSCC passes that can't use function analyses
54  /// passes in the old PM.
56 
58  : F(Arg.F), BFI(Arg.BFI) {}
59 
61  F = RHS.F;
62  BFI = RHS.BFI;
63  return *this;
64  }
65 
66  /// The new interface to emit remarks.
67  void emit(DiagnosticInfoOptimizationBase &OptDiag);
68 
69  /// Emit an optimization-applied message.
70  ///
71  /// \p PassName is the name of the pass emitting the message. If -Rpass= is
72  /// given and \p PassName matches the regular expression in -Rpass, then the
73  /// remark will be emitted. \p Fn is the function triggering the remark, \p
74  /// DLoc is the debug location where the diagnostic is generated. \p V is the
75  /// IR Value that identifies the code region. \p Msg is the message string to
76  /// use.
77  void emitOptimizationRemark(const char *PassName, const DebugLoc &DLoc,
78  const Value *V, const Twine &Msg);
79 
80  /// \brief Same as above but derives the IR Value for the code region and the
81  /// debug location from the Loop parameter \p L.
82  void emitOptimizationRemark(const char *PassName, Loop *L, const Twine &Msg);
83 
84  /// \brief Same as above but derives the debug location and the code region
85  /// from the debug location and the basic block of \p Inst, respectively.
86  void emitOptimizationRemark(const char *PassName, Instruction *Inst,
87  const Twine &Msg) {
88  emitOptimizationRemark(PassName, Inst->getDebugLoc(), Inst->getParent(),
89  Msg);
90  }
91 
92  /// Emit an optimization-missed message.
93  ///
94  /// \p PassName is the name of the pass emitting the message. If
95  /// -Rpass-missed= is given and the name matches the regular expression in
96  /// -Rpass, then the remark will be emitted. \p DLoc is the debug location
97  /// where the diagnostic is generated. \p V is the IR Value that identifies
98  /// the code region. \p Msg is the message string to use. If \p IsVerbose is
99  /// true, the message is considered verbose and will only be emitted when
100  /// verbose output is turned on.
101  void emitOptimizationRemarkMissed(const char *PassName, const DebugLoc &DLoc,
102  const Value *V, const Twine &Msg,
103  bool IsVerbose = false);
104 
105  /// \brief Same as above but derives the IR Value for the code region and the
106  /// debug location from the Loop parameter \p L.
107  void emitOptimizationRemarkMissed(const char *PassName, Loop *L,
108  const Twine &Msg, bool IsVerbose = false);
109 
110  /// \brief Same as above but derives the debug location and the code region
111  /// from the debug location and the basic block of \p Inst, respectively.
112  void emitOptimizationRemarkMissed(const char *PassName, Instruction *Inst,
113  const Twine &Msg, bool IsVerbose = false) {
114  emitOptimizationRemarkMissed(PassName, Inst->getDebugLoc(),
115  Inst->getParent(), Msg, IsVerbose);
116  }
117 
118  /// Emit an optimization analysis remark message.
119  ///
120  /// \p PassName is the name of the pass emitting the message. If
121  /// -Rpass-analysis= is given and \p PassName matches the regular expression
122  /// in -Rpass, then the remark will be emitted. \p DLoc is the debug location
123  /// where the diagnostic is generated. \p V is the IR Value that identifies
124  /// the code region. \p Msg is the message string to use. If \p IsVerbose is
125  /// true, the message is considered verbose and will only be emitted when
126  /// verbose output is turned on.
127  void emitOptimizationRemarkAnalysis(const char *PassName,
128  const DebugLoc &DLoc, const Value *V,
129  const Twine &Msg, bool IsVerbose = false);
130 
131  /// \brief Same as above but derives the IR Value for the code region and the
132  /// debug location from the Loop parameter \p L.
133  void emitOptimizationRemarkAnalysis(const char *PassName, Loop *L,
134  const Twine &Msg, bool IsVerbose = false);
135 
136  /// \brief Same as above but derives the debug location and the code region
137  /// from the debug location and the basic block of \p Inst, respectively.
138  void emitOptimizationRemarkAnalysis(const char *PassName, Instruction *Inst,
139  const Twine &Msg,
140  bool IsVerbose = false) {
142  Inst->getParent(), Msg, IsVerbose);
143  }
144 
145  /// \brief This variant allows specifying what should be emitted for missed
146  /// and analysis remarks in one call.
147  ///
148  /// \p PassName is the name of the pass emitting the message. If
149  /// -Rpass-missed= is given and \p PassName matches the regular expression, \p
150  /// MsgForMissedRemark is emitted.
151  ///
152  /// If -Rpass-analysis= is given and \p PassName matches the regular
153  /// expression, \p MsgForAnalysisRemark is emitted.
154  ///
155  /// The debug location and the code region is derived from \p Inst. If \p
156  /// IsVerbose is true, the message is considered verbose and will only be
157  /// emitted when verbose output is turned on.
159  const char *PassName, Instruction *Inst, const Twine &MsgForMissedRemark,
160  const Twine &MsgForAnalysisRemark, bool IsVerbose = false) {
161  emitOptimizationRemarkAnalysis(PassName, Inst, MsgForAnalysisRemark,
162  IsVerbose);
163  emitOptimizationRemarkMissed(PassName, Inst, MsgForMissedRemark, IsVerbose);
164  }
165 
166  /// \brief Emit an optimization analysis remark related to floating-point
167  /// non-commutativity.
168  ///
169  /// \p PassName is the name of the pass emitting the message. If
170  /// -Rpass-analysis= is given and \p PassName matches the regular expression
171  /// in -Rpass, then the remark will be emitted. \p Fn is the function
172  /// triggering the remark, \p DLoc is the debug location where the diagnostic
173  /// is generated.\p V is the IR Value that identifies the code region. \p Msg
174  /// is the message string to use.
175  void emitOptimizationRemarkAnalysisFPCommute(const char *PassName,
176  const DebugLoc &DLoc,
177  const Value *V,
178  const Twine &Msg);
179 
180  /// \brief Emit an optimization analysis remark related to pointer aliasing.
181  ///
182  /// \p PassName is the name of the pass emitting the message. If
183  /// -Rpass-analysis= is given and \p PassName matches the regular expression
184  /// in -Rpass, then the remark will be emitted. \p Fn is the function
185  /// triggering the remark, \p DLoc is the debug location where the diagnostic
186  /// is generated.\p V is the IR Value that identifies the code region. \p Msg
187  /// is the message string to use.
188  void emitOptimizationRemarkAnalysisAliasing(const char *PassName,
189  const DebugLoc &DLoc,
190  const Value *V, const Twine &Msg);
191 
192  /// \brief Same as above but derives the IR Value for the code region and the
193  /// debug location from the Loop parameter \p L.
194  void emitOptimizationRemarkAnalysisAliasing(const char *PassName, Loop *L,
195  const Twine &Msg);
196 
197  /// \brief Whether we allow for extra compile-time budget to perform more
198  /// analysis to produce fewer false positives.
199  ///
200  /// This is useful when reporting missed optimizations. In this case we can
201  /// use the extra analysis (1) to filter trivial false positives or (2) to
202  /// provide more context so that non-trivial false positives can be quickly
203  /// detected by the user.
204  bool allowExtraAnalysis() const {
205  // For now, only allow this with -fsave-optimization-record since the -Rpass
206  // options are handled in the front-end.
207  return F->getContext().getDiagnosticsOutputFile();
208  }
209 
210 private:
211  Function *F;
212 
213  BlockFrequencyInfo *BFI;
214 
215  /// If we generate BFI on demand, we need to free it when ORE is freed.
216  std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
217 
218  /// Compute hotness from IR value (currently assumed to be a block) if PGO is
219  /// available.
220  Optional<uint64_t> computeHotness(const Value *V);
221 
222  /// Similar but use value from \p OptDiag and update hotness there.
223  void computeHotness(DiagnosticInfoOptimizationBase &OptDiag);
224 
225  /// \brief Only allow verbose messages if we know we're filtering by hotness
226  /// (BFI is only set in this case).
227  bool shouldEmitVerbose() { return BFI != nullptr; }
228 
230  void operator=(const OptimizationRemarkEmitter &) = delete;
231 };
232 
233 /// \brief Add a small namespace to avoid name clashes with the classes used in
234 /// the streaming interface. We want these to be short for better
235 /// write/readability.
236 namespace ore {
240 }
241 
242 /// OptimizationRemarkEmitter legacy analysis pass
243 ///
244 /// Note that this pass shouldn't generally be marked as preserved by other
245 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
246 /// could be freed.
248  std::unique_ptr<OptimizationRemarkEmitter> ORE;
249 
250 public:
252 
253  bool runOnFunction(Function &F) override;
254 
255  void getAnalysisUsage(AnalysisUsage &AU) const override;
256 
258  assert(ORE && "pass not run yet");
259  return *ORE;
260  }
261 
262  static char ID;
263 };
264 
266  : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
268  static AnalysisKey Key;
269 
270 public:
271  /// \brief Provide the result typedef for this analysis pass.
273 
274  /// \brief Run the analysis pass over a function and produce BFI.
276 };
277 }
278 #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
MachineLoop * L
void emitOptimizationRemarkAnalysisFPCommute(const char *PassName, const DebugLoc &DLoc, const Value *V, const Twine &Msg)
Emit an optimization analysis remark related to floating-point non-commutativity. ...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:226
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
aarch64 AArch64 CCMP Pass
void emitOptimizationRemarkMissedAndAnalysis(const char *PassName, Instruction *Inst, const Twine &MsgForMissedRemark, const Twine &MsgForAnalysisRemark, bool IsVerbose=false)
This variant allows specifying what should be emitted for missed and analysis remarks in one call...
Used to set IsVerbose via the stream interface.
OptimizationRemarkEmitter Result
Provide the result typedef for this analysis pass.
A debug info location.
Definition: DebugLoc.h:34
void emitOptimizationRemarkAnalysis(const char *PassName, const DebugLoc &DLoc, const Value *V, const Twine &Msg, bool IsVerbose=false)
Emit an optimization analysis remark message.
OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
OptimizationRemarkEmitter(Function *F, BlockFrequencyInfo *BFI)
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
#define F(x, y, z)
Definition: MD5.cpp:51
OptimizationRemarkEmitter & operator=(OptimizationRemarkEmitter &&RHS)
DiagnosticInfoOptimizationBase::setExtraArgs setExtraArgs
DiagnosticInfoOptimizationBase::setIsVerbose setIsVerbose
yaml::Output * getDiagnosticsOutputFile()
Return the YAML file used by the backend to save optimization diagnostics.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool allowExtraAnalysis() const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
void emitOptimizationRemarkMissed(const char *PassName, const DebugLoc &DLoc, const Value *V, const Twine &Msg, bool IsVerbose=false)
Emit an optimization-missed message.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:328
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:259
Represent the analysis usage information of a pass.
void emitOptimizationRemark(const char *PassName, const DebugLoc &DLoc, const Value *V, const Twine &Msg)
Emit an optimization-applied message.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Used in the streaming interface as the general argument type.
void emit(DiagnosticInfoOptimizationBase &OptDiag)
The new interface to emit remarks.
Common features for diagnostics dealing with optimization remarks.
void emitOptimizationRemarkAnalysisAliasing(const char *PassName, const DebugLoc &DLoc, const Value *V, const Twine &Msg)
Emit an optimization analysis remark related to pointer aliasing.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:368
void emitOptimizationRemarkMissed(const char *PassName, Instruction *Inst, const Twine &Msg, bool IsVerbose=false)
Same as above but derives the debug location and the code region from the debug location and the basi...
void emitOptimizationRemarkAnalysis(const char *PassName, Instruction *Inst, const Twine &Msg, bool IsVerbose=false)
Same as above but derives the debug location and the code region from the debug location and the basi...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
OptimizationRemarkEmitter legacy analysis pass.
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void emitOptimizationRemark(const char *PassName, Instruction *Inst, const Twine &Msg)
Same as above but derives the debug location and the code region from the debug location and the basi...
The optimization diagnostic interface.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64
const BasicBlock * getParent() const
Definition: Instruction.h:62