1 //===- OptimizationDiagnosticInfo.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"
33 /// The optimization diagnostic interface.
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 DiagnosticsHotnessRequested is
38 /// enabled in the LLVM context.
39 class OptimizationRemarkEmitter {
41 OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
44 /// \brief This variant can be used to generate ORE on demand (without the
47 /// Note that this ctor has a very different cost depending on whether
48 /// F->getContext().getDiagnosticsHotnessRequested() is on or not. If it's off
49 /// the operation is free.
51 /// Whereas if DiagnosticsHotnessRequested 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.
55 OptimizationRemarkEmitter(const Function *F);
57 OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
58 : F(Arg.F), BFI(Arg.BFI) {}
60 OptimizationRemarkEmitter &operator=(OptimizationRemarkEmitter &&RHS) {
66 /// Handle invalidation events in the new pass manager.
67 bool invalidate(Function &F, const PreservedAnalyses &PA,
68 FunctionAnalysisManager::Invalidator &Inv);
70 /// \brief Output the remark via the diagnostic handler and to the
71 /// optimization record file.
73 /// This is the new interface that should be now used rather than the legacy
75 void emit(DiagnosticInfoOptimizationBase &OptDiag);
77 /// \brief Whether we allow for extra compile-time budget to perform more
78 /// analysis to produce fewer false positives.
80 /// This is useful when reporting missed optimizations. In this case we can
81 /// use the extra analysis (1) to filter trivial false positives or (2) to
82 /// provide more context so that non-trivial false positives can be quickly
83 /// detected by the user.
84 bool allowExtraAnalysis() const {
85 // For now, only allow this with -fsave-optimization-record since the -Rpass
86 // options are handled in the front-end.
87 return F->getContext().getDiagnosticsOutputFile();
93 BlockFrequencyInfo *BFI;
95 /// If we generate BFI on demand, we need to free it when ORE is freed.
96 std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
98 /// Compute hotness from IR value (currently assumed to be a block) if PGO is
100 Optional<uint64_t> computeHotness(const Value *V);
102 /// Similar but use value from \p OptDiag and update hotness there.
103 void computeHotness(DiagnosticInfoIROptimization &OptDiag);
105 /// \brief Only allow verbose messages if we know we're filtering by hotness
106 /// (BFI is only set in this case).
107 bool shouldEmitVerbose() { return BFI != nullptr; }
109 OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
110 void operator=(const OptimizationRemarkEmitter &) = delete;
113 /// \brief Add a small namespace to avoid name clashes with the classes used in
114 /// the streaming interface. We want these to be short for better
115 /// write/readability.
117 using NV = DiagnosticInfoOptimizationBase::Argument;
118 using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose;
119 using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs;
122 /// OptimizationRemarkEmitter legacy analysis pass
124 /// Note that this pass shouldn't generally be marked as preserved by other
125 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
127 class OptimizationRemarkEmitterWrapperPass : public FunctionPass {
128 std::unique_ptr<OptimizationRemarkEmitter> ORE;
131 OptimizationRemarkEmitterWrapperPass();
133 bool runOnFunction(Function &F) override;
135 void getAnalysisUsage(AnalysisUsage &AU) const override;
137 OptimizationRemarkEmitter &getORE() {
138 assert(ORE && "pass not run yet");
145 class OptimizationRemarkEmitterAnalysis
146 : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
147 friend AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>;
148 static AnalysisKey Key;
151 /// \brief Provide the result typedef for this analysis pass.
152 typedef OptimizationRemarkEmitter Result;
154 /// \brief Run the analysis pass over a function and produce BFI.
155 Result run(Function &F, FunctionAnalysisManager &AM);
159 template <> struct MappingTraits<DiagnosticInfoOptimizationBase *> {
160 static void mapping(IO &io, DiagnosticInfoOptimizationBase *&OptDiag);
164 #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H