LLVM  9.0.0svn
OptimizationRemarkEmitter.h
Go to the documentation of this file.
1 //===- OptimizationRemarkEmitter.h - Optimization Diagnostic ----*- 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 //
9 // Optimization diagnostic interfaces. It's packaged as an analysis pass so
10 // that by using this service passes become dependent on BFI as well. BFI is
11 // used to compute the "hotness" of the diagnostic message.
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
15 #define LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
16 
17 #include "llvm/ADT/Optional.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/Pass.h"
23 
24 namespace llvm {
25 class DebugLoc;
26 class Loop;
27 class Pass;
28 class Twine;
29 class Value;
30 
31 /// The optimization diagnostic interface.
32 ///
33 /// It allows reporting when optimizations are performed and when they are not
34 /// along with the reasons for it. Hotness information of the corresponding
35 /// code region can be included in the remark if DiagnosticsHotnessRequested is
36 /// enabled in the LLVM context.
38 public:
40  : F(F), BFI(BFI) {}
41 
42  /// This variant can be used to generate ORE on demand (without the
43  /// analysis pass).
44  ///
45  /// Note that this ctor has a very different cost depending on whether
46  /// F->getContext().getDiagnosticsHotnessRequested() is on or not. If it's off
47  /// the operation is free.
48  ///
49  /// Whereas if DiagnosticsHotnessRequested is on, it is fairly expensive
50  /// operation since BFI and all its required analyses are computed. This is
51  /// for example useful for CGSCC passes that can't use function analyses
52  /// passes in the old PM.
54 
56  : F(Arg.F), BFI(Arg.BFI) {}
57 
59  F = RHS.F;
60  BFI = RHS.BFI;
61  return *this;
62  }
63 
64  /// Handle invalidation events in the new pass manager.
65  bool invalidate(Function &F, const PreservedAnalyses &PA,
67 
68  /// Output the remark via the diagnostic handler and to the
69  /// optimization record file.
70  void emit(DiagnosticInfoOptimizationBase &OptDiag);
71 
72  /// Take a lambda that returns a remark which will be emitted. Second
73  /// argument is only used to restrict this to functions.
74  template <typename T>
75  void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
76  // Avoid building the remark unless we know there are at least *some*
77  // remarks enabled. We can't currently check whether remarks are requested
78  // for the calling pass since that requires actually building the remark.
79 
80  if (F->getContext().getRemarkStreamer() ||
82  auto R = RemarkBuilder();
84  }
85  }
86 
87  /// Whether we allow for extra compile-time budget to perform more
88  /// analysis to produce fewer false positives.
89  ///
90  /// This is useful when reporting missed optimizations. In this case we can
91  /// use the extra analysis (1) to filter trivial false positives or (2) to
92  /// provide more context so that non-trivial false positives can be quickly
93  /// detected by the user.
94  bool allowExtraAnalysis(StringRef PassName) const {
95  return (F->getContext().getRemarkStreamer() ||
97  }
98 
99 private:
100  const Function *F;
101 
102  BlockFrequencyInfo *BFI;
103 
104  /// If we generate BFI on demand, we need to free it when ORE is freed.
105  std::unique_ptr<BlockFrequencyInfo> OwnedBFI;
106 
107  /// Compute hotness from IR value (currently assumed to be a block) if PGO is
108  /// available.
109  Optional<uint64_t> computeHotness(const Value *V);
110 
111  /// Similar but use value from \p OptDiag and update hotness there.
112  void computeHotness(DiagnosticInfoIROptimization &OptDiag);
113 
114  /// Only allow verbose messages if we know we're filtering by hotness
115  /// (BFI is only set in this case).
116  bool shouldEmitVerbose() { return BFI != nullptr; }
117 
119  void operator=(const OptimizationRemarkEmitter &) = delete;
120 };
121 
122 /// Add a small namespace to avoid name clashes with the classes used in
123 /// the streaming interface. We want these to be short for better
124 /// write/readability.
125 namespace ore {
129 }
130 
131 /// OptimizationRemarkEmitter legacy analysis pass
132 ///
133 /// Note that this pass shouldn't generally be marked as preserved by other
134 /// passes. It's holding onto BFI, so if the pass does not preserve BFI, BFI
135 /// could be freed.
137  std::unique_ptr<OptimizationRemarkEmitter> ORE;
138 
139 public:
141 
142  bool runOnFunction(Function &F) override;
143 
144  void getAnalysisUsage(AnalysisUsage &AU) const override;
145 
147  assert(ORE && "pass not run yet");
148  return *ORE;
149  }
150 
151  static char ID;
152 };
153 
155  : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
157  static AnalysisKey Key;
158 
159 public:
160  /// Provide the result typedef for this analysis pass.
162 
163  /// Run the analysis pass over a function and produce BFI.
164  Result run(Function &F, FunctionAnalysisManager &AM);
165 };
166 }
167 #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
RemarkStreamer * getRemarkStreamer()
Return the streamer used by the backend to save remark diagnostics.
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 ...
Key
PAL metadata keys.
OptimizationRemarkEmitter & operator=(OptimizationRemarkEmitter &&RHS)
DiagnosticInfoOptimizationBase::setExtraArgs setExtraArgs
DiagnosticInfoOptimizationBase::setIsVerbose setIsVerbose
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
void emit(T RemarkBuilder, decltype(RemarkBuilder()) *=nullptr)
Take a lambda that returns a remark which will be emitted.
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:284
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:192
Used in the streaming interface as the general argument type.
print lazy value Lazy Value Info Printer Pass
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
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)
bool isAnyRemarkEnabled(StringRef PassName) const
Return true if any type of remarks are enabled for this pass.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:641
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
OptimizationRemarkEmitter legacy analysis pass.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
The optimization diagnostic interface.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70