LLVM  5.0.0
MachineOptimizationRemarkEmitter.h
Go to the documentation of this file.
1 ///===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- 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 /// \file
10 /// Optimization diagnostic interfaces for machine passes. It's packaged as an
11 /// analysis pass so that by using this service passes become dependent on MBFI
12 /// as well. MBFI is used to compute the "hotness" of the diagnostic message.
13 ///
14 ///===---------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
17 #define LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
18 
21 
22 namespace llvm {
23 class MachineBasicBlock;
24 class MachineBlockFrequencyInfo;
25 class MachineInstr;
26 
27 /// \brief Common features for diagnostics dealing with optimization remarks
28 /// that are used by machine passes.
30 public:
33  const DiagnosticLocation &Loc,
34  const MachineBasicBlock *MBB)
35  : DiagnosticInfoOptimizationBase(Kind, DS_Remark, PassName, RemarkName,
36  *MBB->getParent()->getFunction(), Loc),
37  MBB(MBB) {}
38 
39  /// MI-specific kinds of diagnostic Arguments.
41  /// Print an entire MachineInstr.
43  };
44 
45  static bool classof(const DiagnosticInfo *DI) {
46  return DI->getKind() >= DK_FirstMachineRemark &&
48  }
49 
50  const MachineBasicBlock *getBlock() const { return MBB; }
51 
52 private:
53  const MachineBasicBlock *MBB;
54 };
55 
56 /// Diagnostic information for applied optimization remarks.
58 public:
59  /// \p PassName is the name of the pass emitting this diagnostic. If this name
60  /// matches the regular expression given in -Rpass=, then the diagnostic will
61  /// be emitted. \p RemarkName is a textual identifier for the remark. \p
62  /// Loc is the debug location and \p MBB is the block that the optimization
63  /// operates in.
65  const DiagnosticLocation &Loc,
66  const MachineBasicBlock *MBB)
68  RemarkName, Loc, MBB) {}
69 
70  static bool classof(const DiagnosticInfo *DI) {
71  return DI->getKind() == DK_MachineOptimizationRemark;
72  }
73 
74  /// \see DiagnosticInfoOptimizationBase::isEnabled.
75  bool isEnabled() const override {
77  }
78 };
79 
80 /// Diagnostic information for missed-optimization remarks.
82 public:
83  /// \p PassName is the name of the pass emitting this diagnostic. If this name
84  /// matches the regular expression given in -Rpass-missed=, then the
85  /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
86  /// remark. \p Loc is the debug location and \p MBB is the block that the
87  /// optimization operates in.
89  const DiagnosticLocation &Loc,
90  const MachineBasicBlock *MBB)
92  PassName, RemarkName, Loc, MBB) {}
93 
94  static bool classof(const DiagnosticInfo *DI) {
96  }
97 
98  /// \see DiagnosticInfoOptimizationBase::isEnabled.
99  bool isEnabled() const override {
101  }
102 };
103 
104 /// Diagnostic information for optimization analysis remarks.
106 public:
107  /// \p PassName is the name of the pass emitting this diagnostic. If this name
108  /// matches the regular expression given in -Rpass-analysis=, then the
109  /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
110  /// remark. \p Loc is the debug location and \p MBB is the block that the
111  /// optimization operates in.
113  const DiagnosticLocation &Loc,
114  const MachineBasicBlock *MBB)
116  PassName, RemarkName, Loc, MBB) {}
117 
118  static bool classof(const DiagnosticInfo *DI) {
120  }
121 
122  /// \see DiagnosticInfoOptimizationBase::isEnabled.
123  bool isEnabled() const override {
125  }
126 };
127 
128 /// Extend llvm::ore:: with MI-specific helper names.
129 namespace ore {
131 }
132 
133 /// The optimization diagnostic interface.
134 ///
135 /// It allows reporting when optimizations are performed and when they are not
136 /// along with the reasons for it. Hotness information of the corresponding
137 /// code region can be included in the remark if DiagnosticsHotnessRequested is
138 /// enabled in the LLVM context.
140 public:
143  : MF(MF), MBFI(MBFI) {}
144 
145  /// Emit an optimization remark.
146  void emit(DiagnosticInfoOptimizationBase &OptDiag);
147 
148  /// \brief Whether we allow for extra compile-time budget to perform more
149  /// analysis to be more informative.
150  ///
151  /// This is useful to enable additional missed optimizations to be reported
152  /// that are normally too noisy. In this mode, we can use the extra analysis
153  /// (1) to filter trivial false positives or (2) to provide more context so
154  /// that non-trivial false positives can be quickly detected by the user.
155  bool allowExtraAnalysis() const {
156  // For now, only allow this with -fsave-optimization-record since the -Rpass
157  // options are handled in the front-end.
159  }
160 
161 private:
162  MachineFunction &MF;
163 
164  /// MBFI is only set if hotness is requested.
166 
167  /// Compute hotness from IR value (currently assumed to be a block) if PGO is
168  /// available.
169  Optional<uint64_t> computeHotness(const MachineBasicBlock &MBB);
170 
171  /// Similar but use value from \p OptDiag and update hotness there.
172  void computeHotness(DiagnosticInfoMIROptimization &Remark);
173 
174  /// \brief Only allow verbose messages if we know we're filtering by hotness
175  /// (BFI is only set in this case).
176  bool shouldEmitVerbose() { return MBFI != nullptr; }
177 };
178 
179 /// The analysis pass
180 ///
181 /// Note that this pass shouldn't generally be marked as preserved by other
182 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
183 /// could be freed.
185  std::unique_ptr<MachineOptimizationRemarkEmitter> ORE;
186 
187 public:
189 
190  bool runOnMachineFunction(MachineFunction &MF) override;
191 
192  void getAnalysisUsage(AnalysisUsage &AU) const override;
193 
195  assert(ORE && "pass not run yet");
196  return *ORE;
197  }
198 
199  static char ID;
200 };
201 }
202 
203 #endif
const char * PassName
Name of the pass that triggers this report.
DiagnosticKind
Defines the different supported kind of a diagnostic.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
bool isEnabled() const override
MachineOptimizationRemarkEmitter(MachineFunction &MF, MachineBlockFrequencyInfo *MBFI)
Diagnostic information for applied optimization remarks.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
DiagnosticInfoMIROptimization(enum DiagnosticKind Kind, const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
Diagnostic information for optimization analysis remarks.
static bool classof(const DiagnosticInfo *DI)
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
static bool classof(const DiagnosticInfo *DI)
bool isEnabled() const override
yaml::Output * getDiagnosticsOutputFile()
Return the YAML file used by the backend to save optimization diagnostics.
const Function & getFunction() const
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.
This is the base abstract class for diagnostic reporting in the backend.
Represent the analysis usage information of a pass.
MachineOptimizationRemarkMissed(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
Used in the streaming interface as the general argument type.
bool isEnabled() const override
MachineOptimizationRemark(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
const MachineBasicBlock * getBlock() const
bool allowExtraAnalysis() const
Whether we allow for extra compile-time budget to perform more analysis to be more informative...
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
The optimization diagnostic interface.
Representation of each machine instruction.
Definition: MachineInstr.h:59
Diagnostic information for missed-optimization remarks.
MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName, const DiagnosticLocation &Loc, const MachineBasicBlock *MBB)
PassName is the name of the pass emitting this diagnostic.
static bool classof(const DiagnosticInfo *DI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const Function * getParent(const Value *V)
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Common features for diagnostics dealing with optimization remarks that are used by machine passes...