]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/OptimizationDiagnosticInfo.h
Merge ACPICA 20170929.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / OptimizationDiagnosticInfo.h
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"
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"
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.
39 class OptimizationRemarkEmitter {
40 public:
41   OptimizationRemarkEmitter(const Function *F, BlockFrequencyInfo *BFI)
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.
55   OptimizationRemarkEmitter(const Function *F);
56
57   OptimizationRemarkEmitter(OptimizationRemarkEmitter &&Arg)
58       : F(Arg.F), BFI(Arg.BFI) {}
59
60   OptimizationRemarkEmitter &operator=(OptimizationRemarkEmitter &&RHS) {
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,
68                   FunctionAnalysisManager::Invalidator &Inv);
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.
87     return F->getContext().getDiagnosticsOutputFile();
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
109   OptimizationRemarkEmitter(const OptimizationRemarkEmitter &) = delete;
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 {
117 using NV = DiagnosticInfoOptimizationBase::Argument;
118 using setIsVerbose = DiagnosticInfoOptimizationBase::setIsVerbose;
119 using setExtraArgs = DiagnosticInfoOptimizationBase::setExtraArgs;
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.
127 class OptimizationRemarkEmitterWrapperPass : public FunctionPass {
128   std::unique_ptr<OptimizationRemarkEmitter> ORE;
129
130 public:
131   OptimizationRemarkEmitterWrapperPass();
132
133   bool runOnFunction(Function &F) override;
134
135   void getAnalysisUsage(AnalysisUsage &AU) const override;
136
137   OptimizationRemarkEmitter &getORE() {
138     assert(ORE && "pass not run yet");
139     return *ORE;
140   }
141
142   static char ID;
143 };
144
145 class OptimizationRemarkEmitterAnalysis
146     : public AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis> {
147   friend AnalysisInfoMixin<OptimizationRemarkEmitterAnalysis>;
148   static AnalysisKey Key;
149
150 public:
151   /// \brief Provide the result typedef for this analysis pass.
152   typedef OptimizationRemarkEmitter Result;
153
154   /// \brief Run the analysis pass over a function and produce BFI.
155   Result run(Function &F, FunctionAnalysisManager &AM);
156 };
157
158 namespace yaml {
159 template <> struct MappingTraits<DiagnosticInfoOptimizationBase *> {
160   static void mapping(IO &io, DiagnosticInfoOptimizationBase *&OptDiag);
161 };
162 }
163 }
164 #endif // LLVM_IR_OPTIMIZATIONDIAGNOSTICINFO_H