Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / CodeGen / MachineOptimizationRemarkEmitter.h
blob9a0fd56253673b511586af17bd52bea3fa7ba14c
1 ///===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- 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 /// Optimization diagnostic interfaces for machine passes. It's packaged as an
10 /// analysis pass so that by using this service passes become dependent on MBFI
11 /// as well. MBFI is used to compute the "hotness" of the diagnostic message.
12 ///
13 ///===---------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
16 #define LLVM_CODEGEN_MACHINEOPTIMIZATIONREMARKEMITTER_H
18 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
21 namespace llvm {
22 class MachineBasicBlock;
23 class MachineBlockFrequencyInfo;
24 class MachineInstr;
26 /// Common features for diagnostics dealing with optimization remarks
27 /// that are used by machine passes.
28 class DiagnosticInfoMIROptimization : public DiagnosticInfoOptimizationBase {
29 public:
30 DiagnosticInfoMIROptimization(enum DiagnosticKind Kind, const char *PassName,
31 StringRef RemarkName,
32 const DiagnosticLocation &Loc,
33 const MachineBasicBlock *MBB)
34 : DiagnosticInfoOptimizationBase(Kind, DS_Remark, PassName, RemarkName,
35 MBB->getParent()->getFunction(), Loc),
36 MBB(MBB) {}
38 /// MI-specific kinds of diagnostic Arguments.
39 struct MachineArgument : public DiagnosticInfoOptimizationBase::Argument {
40 /// Print an entire MachineInstr.
41 MachineArgument(StringRef Key, const MachineInstr &MI);
44 static bool classof(const DiagnosticInfo *DI) {
45 return DI->getKind() >= DK_FirstMachineRemark &&
46 DI->getKind() <= DK_LastMachineRemark;
49 const MachineBasicBlock *getBlock() const { return MBB; }
51 private:
52 const MachineBasicBlock *MBB;
55 /// Diagnostic information for applied optimization remarks.
56 class MachineOptimizationRemark : public DiagnosticInfoMIROptimization {
57 public:
58 /// \p PassName is the name of the pass emitting this diagnostic. If this name
59 /// matches the regular expression given in -Rpass=, then the diagnostic will
60 /// be emitted. \p RemarkName is a textual identifier for the remark. \p
61 /// Loc is the debug location and \p MBB is the block that the optimization
62 /// operates in.
63 MachineOptimizationRemark(const char *PassName, StringRef RemarkName,
64 const DiagnosticLocation &Loc,
65 const MachineBasicBlock *MBB)
66 : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemark, PassName,
67 RemarkName, Loc, MBB) {}
69 static bool classof(const DiagnosticInfo *DI) {
70 return DI->getKind() == DK_MachineOptimizationRemark;
73 /// \see DiagnosticInfoOptimizationBase::isEnabled.
74 bool isEnabled() const override {
75 const Function &Fn = getFunction();
76 LLVMContext &Ctx = Fn.getContext();
77 return Ctx.getDiagHandlerPtr()->isPassedOptRemarkEnabled(getPassName());
81 /// Diagnostic information for missed-optimization remarks.
82 class MachineOptimizationRemarkMissed : public DiagnosticInfoMIROptimization {
83 public:
84 /// \p PassName is the name of the pass emitting this diagnostic. If this name
85 /// matches the regular expression given in -Rpass-missed=, then the
86 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
87 /// remark. \p Loc is the debug location and \p MBB is the block that the
88 /// optimization operates in.
89 MachineOptimizationRemarkMissed(const char *PassName, StringRef RemarkName,
90 const DiagnosticLocation &Loc,
91 const MachineBasicBlock *MBB)
92 : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemarkMissed,
93 PassName, RemarkName, Loc, MBB) {}
95 static bool classof(const DiagnosticInfo *DI) {
96 return DI->getKind() == DK_MachineOptimizationRemarkMissed;
99 /// \see DiagnosticInfoOptimizationBase::isEnabled.
100 bool isEnabled() const override {
101 const Function &Fn = getFunction();
102 LLVMContext &Ctx = Fn.getContext();
103 return Ctx.getDiagHandlerPtr()->isMissedOptRemarkEnabled(getPassName());
107 /// Diagnostic information for optimization analysis remarks.
108 class MachineOptimizationRemarkAnalysis : public DiagnosticInfoMIROptimization {
109 public:
110 /// \p PassName is the name of the pass emitting this diagnostic. If this name
111 /// matches the regular expression given in -Rpass-analysis=, then the
112 /// diagnostic will be emitted. \p RemarkName is a textual identifier for the
113 /// remark. \p Loc is the debug location and \p MBB is the block that the
114 /// optimization operates in.
115 MachineOptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName,
116 const DiagnosticLocation &Loc,
117 const MachineBasicBlock *MBB)
118 : DiagnosticInfoMIROptimization(DK_MachineOptimizationRemarkAnalysis,
119 PassName, RemarkName, Loc, MBB) {}
121 static bool classof(const DiagnosticInfo *DI) {
122 return DI->getKind() == DK_MachineOptimizationRemarkAnalysis;
125 /// \see DiagnosticInfoOptimizationBase::isEnabled.
126 bool isEnabled() const override {
127 const Function &Fn = getFunction();
128 LLVMContext &Ctx = Fn.getContext();
129 return Ctx.getDiagHandlerPtr()->isAnalysisRemarkEnabled(getPassName());
133 /// Extend llvm::ore:: with MI-specific helper names.
134 namespace ore {
135 using MNV = DiagnosticInfoMIROptimization::MachineArgument;
138 /// The optimization diagnostic interface.
140 /// It allows reporting when optimizations are performed and when they are not
141 /// along with the reasons for it. Hotness information of the corresponding
142 /// code region can be included in the remark if DiagnosticsHotnessRequested is
143 /// enabled in the LLVM context.
144 class MachineOptimizationRemarkEmitter {
145 public:
146 MachineOptimizationRemarkEmitter(MachineFunction &MF,
147 MachineBlockFrequencyInfo *MBFI)
148 : MF(MF), MBFI(MBFI) {}
150 /// Emit an optimization remark.
151 void emit(DiagnosticInfoOptimizationBase &OptDiag);
153 /// Whether we allow for extra compile-time budget to perform more
154 /// analysis to be more informative.
156 /// This is useful to enable additional missed optimizations to be reported
157 /// that are normally too noisy. In this mode, we can use the extra analysis
158 /// (1) to filter trivial false positives or (2) to provide more context so
159 /// that non-trivial false positives can be quickly detected by the user.
160 bool allowExtraAnalysis(StringRef PassName) const {
161 return (MF.getFunction().getContext().getDiagnosticsOutputFile() ||
162 MF.getFunction().getContext()
163 .getDiagHandlerPtr()->isAnyRemarkEnabled(PassName));
166 /// Take a lambda that returns a remark which will be emitted. Second
167 /// argument is only used to restrict this to functions.
168 template <typename T>
169 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
170 // Avoid building the remark unless we know there are at least *some*
171 // remarks enabled. We can't currently check whether remarks are requested
172 // for the calling pass since that requires actually building the remark.
174 if (MF.getFunction().getContext().getDiagnosticsOutputFile() ||
175 MF.getFunction().getContext().getDiagHandlerPtr()->isAnyRemarkEnabled()) {
176 auto R = RemarkBuilder();
177 emit((DiagnosticInfoOptimizationBase &)R);
181 private:
182 MachineFunction &MF;
184 /// MBFI is only set if hotness is requested.
185 MachineBlockFrequencyInfo *MBFI;
187 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
188 /// available.
189 Optional<uint64_t> computeHotness(const MachineBasicBlock &MBB);
191 /// Similar but use value from \p OptDiag and update hotness there.
192 void computeHotness(DiagnosticInfoMIROptimization &Remark);
194 /// Only allow verbose messages if we know we're filtering by hotness
195 /// (BFI is only set in this case).
196 bool shouldEmitVerbose() { return MBFI != nullptr; }
199 /// The analysis pass
201 /// Note that this pass shouldn't generally be marked as preserved by other
202 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
203 /// could be freed.
204 class MachineOptimizationRemarkEmitterPass : public MachineFunctionPass {
205 std::unique_ptr<MachineOptimizationRemarkEmitter> ORE;
207 public:
208 MachineOptimizationRemarkEmitterPass();
210 bool runOnMachineFunction(MachineFunction &MF) override;
212 void getAnalysisUsage(AnalysisUsage &AU) const override;
214 MachineOptimizationRemarkEmitter &getORE() {
215 assert(ORE && "pass not run yet");
216 return *ORE;
219 static char ID;
223 #endif