LLVM  15.0.0git
MachineOptimizationRemarkEmitter.cpp
Go to the documentation of this file.
1 ///===- MachineOptimizationRemarkEmitter.cpp - Opt 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 /// \file
9 /// Optimization diagnostic interfaces for machine passes. It's packaged as an
10 /// analysis pass so that by using this service passes become dependent on MBFI
11 /// as well. MBFI is used to compute the "hotness" of the diagnostic message.
12 ///
13 ///===---------------------------------------------------------------------===//
14 
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/InitializePasses.h"
21 
22 using namespace llvm;
23 
25  StringRef MKey, const MachineInstr &MI) {
26  Key = std::string(MKey);
27 
29  MI.print(OS, /*IsStandalone=*/true, /*SkipOpers=*/false,
30  /*SkipDebugLoc=*/true);
31 }
32 
34 MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
35  if (!MBFI)
36  return None;
37 
38  return MBFI->getBlockProfileCount(&MBB);
39 }
40 
41 void MachineOptimizationRemarkEmitter::computeHotness(
43  const MachineBasicBlock *MBB = Remark.getBlock();
44  if (MBB)
45  Remark.setHotness(computeHotness(*MBB));
46 }
47 
49  DiagnosticInfoOptimizationBase &OptDiagCommon) {
50  auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
51  computeHotness(OptDiag);
52 
53  LLVMContext &Ctx = MF.getFunction().getContext();
54 
55  // Only emit it if its hotness meets the threshold.
56  if (OptDiag.getHotness().getValueOr(0) <
58  return;
59  }
60 
61  Ctx.diagnose(OptDiag);
62 }
63 
68 }
69 
71  MachineFunction &MF) {
73 
75  MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
76  else
77  MBFI = nullptr;
78 
79  ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
80  return false;
81 }
82 
84  AnalysisUsage &AU) const {
86  AU.setPreservesAll();
88 }
89 
91 static const char ore_name[] = "Machine Optimization Remark Emitter";
92 #define ORE_NAME "machine-opt-remark-emitter"
93 
95  true, true)
llvm::DiagnosticInfoOptimizationBase::Argument::Val
std::string Val
Definition: DiagnosticInfo.h:429
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DiagnosticInfoMIROptimization
Common features for diagnostics dealing with optimization remarks that are used by machine passes.
Definition: MachineOptimizationRemarkEmitter.h:29
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
ore_name
static const char ore_name[]
Definition: MachineOptimizationRemarkEmitter.cpp:91
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:319
llvm::initializeMachineOptimizationRemarkEmitterPassPass
void initializeMachineOptimizationRemarkEmitterPassPass(PassRegistry &)
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name, true, true) INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass
llvm::Optional< uint64_t >
llvm::LLVMContext::getDiagnosticsHotnessThreshold
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
Definition: LLVMContext.cpp:147
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:414
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:103
llvm::MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass
MachineOptimizationRemarkEmitterPass()
Definition: MachineOptimizationRemarkEmitter.cpp:64
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::MachineBlockFrequencyInfo
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
Definition: MachineBlockFrequencyInfo.h:33
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::None
const NoneType None
Definition: None.h:24
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::Pass::print
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:129
MachineOptimizationRemarkEmitter.h
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
llvm::HighlightColor::Remark
@ Remark
LazyMachineBlockFrequencyInfo.h
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
llvm::MachineOptimizationRemarkEmitter::emit
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
Definition: MachineOptimizationRemarkEmitter.cpp:48
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::MachineOptimizationRemarkEmitterPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineOptimizationRemarkEmitter.cpp:83
llvm::DiagnosticInfoOptimizationBase::Argument::Key
std::string Key
Definition: DiagnosticInfo.h:428
llvm::LazyMachineBlockFrequencyInfoPass
This is an alternative analysis pass to MachineBlockFrequencyInfo.
Definition: LazyMachineBlockFrequencyInfo.h:37
llvm::MachineFunction
Definition: MachineFunction.h:241
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MachineOptimizationRemarkEmitterPass
The analysis pass.
Definition: MachineOptimizationRemarkEmitter.h:219
llvm::MachineOptimizationRemarkEmitterPass::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: MachineOptimizationRemarkEmitter.cpp:70
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::LLVMContext::diagnose
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Definition: LLVMContext.cpp:243
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:606
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
DiagnosticInfo.h
llvm::LLVMContext::getDiagnosticsHotnessRequested
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
Definition: LLVMContext.cpp:134
ORE_NAME
#define ORE_NAME
Definition: MachineOptimizationRemarkEmitter.cpp:92
true
true
Definition: MachineOptimizationRemarkEmitter.cpp:98
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
LLVMContext.h
llvm::MachineOptimizationRemarkEmitterPass::ID
static char ID
Definition: MachineOptimizationRemarkEmitter.h:234
InitializePasses.h
llvm::DiagnosticInfoMIROptimization::MachineArgument::MachineArgument
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.
Definition: MachineOptimizationRemarkEmitter.cpp:24