1 //===- OptimizationRemarkEmitter.cpp - 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 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
16 #include "llvm/Analysis/BranchProbabilityInfo.h"
17 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/LLVMContext.h"
25 OptimizationRemarkEmitter::OptimizationRemarkEmitter(const Function
*F
)
26 : F(F
), BFI(nullptr) {
27 if (!F
->getContext().getDiagnosticsHotnessRequested())
30 // First create a dominator tree.
32 DT
.recalculate(*const_cast<Function
*>(F
));
34 // Generate LoopInfo from it.
38 // Then compute BranchProbabilityInfo.
39 BranchProbabilityInfo BPI
;
40 BPI
.calculate(*F
, LI
);
42 // Finally compute BFI.
43 OwnedBFI
= llvm::make_unique
<BlockFrequencyInfo
>(*F
, BPI
, LI
);
47 bool OptimizationRemarkEmitter::invalidate(
48 Function
&F
, const PreservedAnalyses
&PA
,
49 FunctionAnalysisManager::Invalidator
&Inv
) {
50 // This analysis has no state and so can be trivially preserved but it needs
51 // a fresh view of BFI if it was constructed with one.
52 if (BFI
&& Inv
.invalidate
<BlockFrequencyAnalysis
>(F
, PA
))
55 // Otherwise this analysis result remains valid.
59 Optional
<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value
*V
) {
63 return BFI
->getBlockProfileCount(cast
<BasicBlock
>(V
));
66 void OptimizationRemarkEmitter::computeHotness(
67 DiagnosticInfoIROptimization
&OptDiag
) {
68 const Value
*V
= OptDiag
.getCodeRegion();
70 OptDiag
.setHotness(computeHotness(V
));
73 void OptimizationRemarkEmitter::emit(
74 DiagnosticInfoOptimizationBase
&OptDiagBase
) {
75 auto &OptDiag
= cast
<DiagnosticInfoIROptimization
>(OptDiagBase
);
76 computeHotness(OptDiag
);
78 // Only emit it if its hotness meets the threshold.
79 if (OptDiag
.getHotness().getValueOr(0) <
80 F
->getContext().getDiagnosticsHotnessThreshold()) {
84 F
->getContext().diagnose(OptDiag
);
87 OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass()
89 initializeOptimizationRemarkEmitterWrapperPassPass(
90 *PassRegistry::getPassRegistry());
93 bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function
&Fn
) {
94 BlockFrequencyInfo
*BFI
;
96 if (Fn
.getContext().getDiagnosticsHotnessRequested())
97 BFI
= &getAnalysis
<LazyBlockFrequencyInfoPass
>().getBFI();
101 ORE
= llvm::make_unique
<OptimizationRemarkEmitter
>(&Fn
, BFI
);
105 void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage(
106 AnalysisUsage
&AU
) const {
107 LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU
);
108 AU
.setPreservesAll();
111 AnalysisKey
OptimizationRemarkEmitterAnalysis::Key
;
113 OptimizationRemarkEmitter
114 OptimizationRemarkEmitterAnalysis::run(Function
&F
,
115 FunctionAnalysisManager
&AM
) {
116 BlockFrequencyInfo
*BFI
;
118 if (F
.getContext().getDiagnosticsHotnessRequested())
119 BFI
= &AM
.getResult
<BlockFrequencyAnalysis
>(F
);
123 return OptimizationRemarkEmitter(&F
, BFI
);
126 char OptimizationRemarkEmitterWrapperPass::ID
= 0;
127 static const char ore_name
[] = "Optimization Remark Emitter";
128 #define ORE_NAME "opt-remark-emitter"
130 INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass
, ORE_NAME
, ore_name
,
132 INITIALIZE_PASS_DEPENDENCY(LazyBFIPass
)
133 INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass
, ORE_NAME
, ore_name
,