LLVM  5.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 DiagnosticsHotnessRequested 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().getDiagnosticsHotnessRequested() is on or not. If it's off
49  /// the operation is free.
50  ///
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.
56 
58  : F(Arg.F), BFI(Arg.BFI) {}
59 
61  F = RHS.F;
62  BFI = RHS.BFI;
63  return *this;
64  }
65 
66  /// Handle invalidation events in the new pass manager.
67  bool invalidate(Function &F, const PreservedAnalyses &PA,
69 
70  /// \brief Output the remark via the diagnostic handler and to the
71  /// optimization record file.
72  ///
73  /// This is the new interface that should be now used rather than the legacy
74  /// emit* APIs.
75  void emit(DiagnosticInfoOptimizationBase &OptDiag);
76 
77  /// \brief Whether we allow for extra compile-time budget to perform more
78  /// analysis to produce fewer false positives.
79  ///
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.
88  }
89 
90 private:
91  const Function *F;
92 
93  BlockFrequencyInfo *BFI;
94 
95  /// If we generate BFI on demand, we need to free it when ORE is freed.
96  std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
97 
98  /// Compute hotness from IR value (currently assumed to be a block) if PGO is
99  /// available.
100  Optional<uint64_t> computeHotness(const Value *V);
101 
102  /// Similar but use value from \p OptDiag and update hotness there.
103  void computeHotness(DiagnosticInfoIROptimization &OptDiag);
104 
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; }
108 
110  void operator=(const OptimizationRemarkEmitter &) = delete;
111 };
112 
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.
116 namespace ore {
120 }
121 
122 /// OptimizationRemarkEmitter legacy analysis pass
123 ///
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
126 /// could be freed.
128  std::unique_ptr<OptimizationRemarkEmitter> ORE;
129 
130 public:
132 
133  bool runOnFunction(Function &F) override;
134 
135  void getAnalysisUsage(AnalysisUsage &AU) const override;
136 
138  assert(ORE && "pass not run yet");
139  return *ORE;
140  }
141 
142  static char ID;
143 };
144 
146  : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
148  static AnalysisKey Key;
149 
150 public:
151  /// \brief Provide the result typedef for this analysis pass.
153 
154  /// \brief Run the analysis pass over a function and produce BFI.
156 };
157 
158 namespace yaml {
160  static void mapping(IO &io, DiagnosticInfoOptimizationBase *&OptDiag);
161 };
162 }
163 }
164 #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
Result run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BFI.
Used to set IsVerbose via the stream interface.
OptimizationRemarkEmitter Result
Provide the result typedef for this analysis pass.
OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
When an instance of this is inserted into the stream, the arguments following will not appear in the ...
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:55
This class should be specialized by any type that needs to be converted to/from a YAML mapping...
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...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
Represent the analysis usage information of a pass.
Common features for diagnostics dealing with optimization remarks that are used by IR passes...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
Used in the streaming interface as the general argument type.
print lazy value Lazy Value Info Printer Pass
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file. ...
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:559
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
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...
The optimization diagnostic interface.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70