1 ///===- MachineOptimizationRemarkEmitter.cpp - Opt 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 for machine passes. It's packaged as an
11 /// analysis pass so that by using this service passes become dependent on MBFI
12 /// as well. MBFI is used to compute the "hotness" of the diagnostic message.
14 ///===---------------------------------------------------------------------===//
16 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
17 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/LLVMContext.h"
24 DiagnosticInfoMIROptimization::MachineArgument::MachineArgument(
25 StringRef MKey
, const MachineInstr
&MI
)
29 raw_string_ostream
OS(Val
);
30 MI
.print(OS
, /*IsStandalone=*/true, /*SkipOpers=*/false,
31 /*SkipDebugLoc=*/true);
35 MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock
&MBB
) {
39 return MBFI
->getBlockProfileCount(&MBB
);
42 void MachineOptimizationRemarkEmitter::computeHotness(
43 DiagnosticInfoMIROptimization
&Remark
) {
44 const MachineBasicBlock
*MBB
= Remark
.getBlock();
46 Remark
.setHotness(computeHotness(*MBB
));
49 void MachineOptimizationRemarkEmitter::emit(
50 DiagnosticInfoOptimizationBase
&OptDiagCommon
) {
51 auto &OptDiag
= cast
<DiagnosticInfoMIROptimization
>(OptDiagCommon
);
52 computeHotness(OptDiag
);
54 LLVMContext
&Ctx
= MF
.getFunction().getContext();
56 // Only emit it if its hotness meets the threshold.
57 if (OptDiag
.getHotness().getValueOr(0) <
58 Ctx
.getDiagnosticsHotnessThreshold()) {
62 Ctx
.diagnose(OptDiag
);
65 MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass()
66 : MachineFunctionPass(ID
) {
67 initializeMachineOptimizationRemarkEmitterPassPass(
68 *PassRegistry::getPassRegistry());
71 bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
72 MachineFunction
&MF
) {
73 MachineBlockFrequencyInfo
*MBFI
;
75 if (MF
.getFunction().getContext().getDiagnosticsHotnessRequested())
76 MBFI
= &getAnalysis
<LazyMachineBlockFrequencyInfoPass
>().getBFI();
80 ORE
= llvm::make_unique
<MachineOptimizationRemarkEmitter
>(MF
, MBFI
);
84 void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
85 AnalysisUsage
&AU
) const {
86 AU
.addRequired
<LazyMachineBlockFrequencyInfoPass
>();
88 MachineFunctionPass::getAnalysisUsage(AU
);
91 char MachineOptimizationRemarkEmitterPass::ID
= 0;
92 static const char ore_name
[] = "Machine Optimization Remark Emitter";
93 #define ORE_NAME "machine-opt-remark-emitter"
95 INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass
, ORE_NAME
, ore_name
,
97 INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass
)
98 INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass
, ORE_NAME
, ore_name
,