1 //===- OptimizationRemarkEmitter.cpp - Optimization Diagnostic --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
15 #include "llvm/Analysis/BranchProbabilityInfo.h"
16 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/LLVMContext.h"
24 OptimizationRemarkEmitter::OptimizationRemarkEmitter(const Function
*F
)
25 : F(F
), BFI(nullptr) {
26 if (!F
->getContext().getDiagnosticsHotnessRequested())
29 // First create a dominator tree.
31 DT
.recalculate(*const_cast<Function
*>(F
));
33 // Generate LoopInfo from it.
37 // Then compute BranchProbabilityInfo.
38 BranchProbabilityInfo BPI
;
39 BPI
.calculate(*F
, LI
);
41 // Finally compute BFI.
42 OwnedBFI
= llvm::make_unique
<BlockFrequencyInfo
>(*F
, BPI
, LI
);
46 bool OptimizationRemarkEmitter::invalidate(
47 Function
&F
, const PreservedAnalyses
&PA
,
48 FunctionAnalysisManager::Invalidator
&Inv
) {
49 // This analysis has no state and so can be trivially preserved but it needs
50 // a fresh view of BFI if it was constructed with one.
51 if (BFI
&& Inv
.invalidate
<BlockFrequencyAnalysis
>(F
, PA
))
54 // Otherwise this analysis result remains valid.
58 Optional
<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value
*V
) {
62 return BFI
->getBlockProfileCount(cast
<BasicBlock
>(V
));
65 void OptimizationRemarkEmitter::computeHotness(
66 DiagnosticInfoIROptimization
&OptDiag
) {
67 const Value
*V
= OptDiag
.getCodeRegion();
69 OptDiag
.setHotness(computeHotness(V
));
72 void OptimizationRemarkEmitter::emit(
73 DiagnosticInfoOptimizationBase
&OptDiagBase
) {
74 auto &OptDiag
= cast
<DiagnosticInfoIROptimization
>(OptDiagBase
);
75 computeHotness(OptDiag
);
77 // Only emit it if its hotness meets the threshold.
78 if (OptDiag
.getHotness().getValueOr(0) <
79 F
->getContext().getDiagnosticsHotnessThreshold()) {
83 F
->getContext().diagnose(OptDiag
);
86 OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass()
88 initializeOptimizationRemarkEmitterWrapperPassPass(
89 *PassRegistry::getPassRegistry());
92 bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function
&Fn
) {
93 BlockFrequencyInfo
*BFI
;
95 if (Fn
.getContext().getDiagnosticsHotnessRequested())
96 BFI
= &getAnalysis
<LazyBlockFrequencyInfoPass
>().getBFI();
100 ORE
= llvm::make_unique
<OptimizationRemarkEmitter
>(&Fn
, BFI
);
104 void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage(
105 AnalysisUsage
&AU
) const {
106 LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU
);
107 AU
.setPreservesAll();
110 AnalysisKey
OptimizationRemarkEmitterAnalysis::Key
;
112 OptimizationRemarkEmitter
113 OptimizationRemarkEmitterAnalysis::run(Function
&F
,
114 FunctionAnalysisManager
&AM
) {
115 BlockFrequencyInfo
*BFI
;
117 if (F
.getContext().getDiagnosticsHotnessRequested())
118 BFI
= &AM
.getResult
<BlockFrequencyAnalysis
>(F
);
122 return OptimizationRemarkEmitter(&F
, BFI
);
125 char OptimizationRemarkEmitterWrapperPass::ID
= 0;
126 static const char ore_name
[] = "Optimization Remark Emitter";
127 #define ORE_NAME "opt-remark-emitter"
129 INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass
, ORE_NAME
, ore_name
,
131 INITIALIZE_PASS_DEPENDENCY(LazyBFIPass
)
132 INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass
, ORE_NAME
, ore_name
,