1 //===- OptimizationRemarkEmitter.h - Optimization Diagnostic ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
16 #define LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/Analysis/BlockFrequencyInfo.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
32 /// The optimization diagnostic interface.
34 /// It allows reporting when optimizations are performed and when they are not
35 /// along with the reasons for it. Hotness information of the corresponding
36 /// code region can be included in the remark if DiagnosticsHotnessRequested is
37 /// enabled in the LLVM context.
38 class OptimizationRemarkEmitter {
40 OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
43 /// \brief This variant can be used to generate ORE on demand (without the
46 /// Note that this ctor has a very different cost depending on whether
47 /// F->getContext().getDiagnosticsHotnessRequested() is on or not. If it's off
48 /// the operation is free.
50 /// Whereas if DiagnosticsHotnessRequested is on, it is fairly expensive
51 /// operation since BFI and all its required analyses are computed. This is
52 /// for example useful for CGSCC passes that can't use function analyses
53 /// passes in the old PM.
54 OptimizationRemarkEmitter(const Function *F);
56 OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
57 : F(Arg.F), BFI(Arg.BFI) {}
59 OptimizationRemarkEmitter &operator=(OptimizationRemarkEmitter &&RHS) {
65 /// Handle invalidation events in the new pass manager.
66 bool invalidate(Function &F, const PreservedAnalyses &PA,
67 FunctionAnalysisManager::Invalidator &Inv);
69 /// \brief Output the remark via the diagnostic handler and to the
70 /// optimization record file.
71 void emit(DiagnosticInfoOptimizationBase &OptDiag);
73 /// \brief Take a lambda that returns a remark which will be emitted. Second
74 /// argument is only used to restrict this to functions.
76 void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
77 // Avoid building the remark unless we know there are at least *some*
78 // remarks enabled. We can't currently check whether remarks are requested
79 // for the calling pass since that requires actually building the remark.
81 if (F->getContext().getDiagnosticsOutputFile() ||
82 F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled()) {
83 auto R = RemarkBuilder();
84 emit((DiagnosticInfoOptimizationBase &)R);
88 /// \brief Whether we allow for extra compile-time budget to perform more
89 /// analysis to produce fewer false positives.
91 /// This is useful when reporting missed optimizations. In this case we can
92 /// use the extra analysis (1) to filter trivial false positives or (2) to
93 /// provide more context so that non-trivial false positives can be quickly
94 /// detected by the user.
95 bool allowExtraAnalysis(StringRef PassName) const {
96 return (F->getContext().getDiagnosticsOutputFile() ||
97 F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled(PassName));
103 BlockFrequencyInfo *BFI;
105 /// If we generate BFI on demand, we need to free it when ORE is freed.
106 std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
108 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
110 Optional<uint64_t> computeHotness(const Value *V);
112 /// Similar but use value from \p OptDiag and update hotness there.
113 void computeHotness(DiagnosticInfoIROptimization &OptDiag);
115 /// \brief Only allow verbose messages if we know we're filtering by hotness
116 /// (BFI is only set in this case).
117 bool shouldEmitVerbose() { return BFI != nullptr; }
119 OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
120 void operator=(const OptimizationRemarkEmitter &) = delete;
123 /// \brief Add a small namespace to avoid name clashes with the classes used in
124 /// the streaming interface. We want these to be short for better
125 /// write/readability.
127 using NV = DiagnosticInfoOptimizationBase::Argument;
128 using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose;
129 using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs;
132 /// OptimizationRemarkEmitter legacy analysis pass
134 /// Note that this pass shouldn't generally be marked as preserved by other
135 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
137 class OptimizationRemarkEmitterWrapperPass : public FunctionPass {
138 std::unique_ptr<OptimizationRemarkEmitter> ORE;
141 OptimizationRemarkEmitterWrapperPass();
143 bool runOnFunction(Function &F) override;
145 void getAnalysisUsage(AnalysisUsage &AU) const override;
147 OptimizationRemarkEmitter &getORE() {
148 assert(ORE && "pass not run yet");
155 class OptimizationRemarkEmitterAnalysis
156 : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
157 friend AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>;
158 static AnalysisKey Key;
161 /// \brief Provide the result typedef for this analysis pass.
162 typedef OptimizationRemarkEmitter Result;
164 /// \brief Run the analysis pass over a function and produce BFI.
165 Result run(Function &F, FunctionAnalysisManager &AM);
168 #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H