LLVM  14.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  : Argument() {
27  Key = std::string(MKey);
28 
30  MI.print(OS, /*IsStandalone=*/true, /*SkipOpers=*/false,
31  /*SkipDebugLoc=*/true);
32 }
33 
35 MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
36  if (!MBFI)
37  return None;
38 
39  return MBFI->getBlockProfileCount(&MBB);
40 }
41 
42 void MachineOptimizationRemarkEmitter::computeHotness(
44  const MachineBasicBlock *MBB = Remark.getBlock();
45  if (MBB)
46  Remark.setHotness(computeHotness(*MBB));
47 }
48 
50  DiagnosticInfoOptimizationBase &OptDiagCommon) {
51  auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
52  computeHotness(OptDiag);
53 
54  LLVMContext &Ctx = MF.getFunction().getContext();
55 
56  // Only emit it if its hotness meets the threshold.
57  if (OptDiag.getHotness().getValueOr(0) <
59  return;
60  }
61 
62  Ctx.diagnose(OptDiag);
63 }
64 
69 }
70 
72  MachineFunction &MF) {
74 
76  MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
77  else
78  MBFI = nullptr;
79 
80  ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
81  return false;
82 }
83 
85  AnalysisUsage &AU) const {
87  AU.setPreservesAll();
89 }
90 
92 static const char ore_name[] = "Machine Optimization Remark Emitter";
93 #define ORE_NAME "machine-opt-remark-emitter"
94 
96  true, true)
llvm::DiagnosticInfoOptimizationBase::Argument::Val
std::string Val
Definition: DiagnosticInfo.h:423
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
MachineInstr.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::DiagnosticInfoMIROptimization
Common features for diagnostics dealing with optimization remarks that are used by machine passes.
Definition: MachineOptimizationRemarkEmitter.h:28
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:625
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:92
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:321
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:139
llvm::DiagnosticInfoOptimizationBase
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
Definition: DiagnosticInfo.h:408
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:102
llvm::MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass
MachineOptimizationRemarkEmitterPass()
Definition: MachineOptimizationRemarkEmitter.cpp:65
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:23
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
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:125
MachineOptimizationRemarkEmitter.h
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
llvm::HighlightColor::Remark
@ Remark
LazyMachineBlockFrequencyInfo.h
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
llvm::DiagnosticInfoOptimizationBase::Argument
Used in the streaming interface as the general argument type.
Definition: DiagnosticInfo.h:421
llvm::MachineOptimizationRemarkEmitter::emit
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
Definition: MachineOptimizationRemarkEmitter.cpp:49
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
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:84
llvm::DiagnosticInfoOptimizationBase::Argument::Key
std::string Key
Definition: DiagnosticInfo.h:422
llvm::LazyMachineBlockFrequencyInfoPass
This is an alternative analysis pass to MachineBlockFrequencyInfo.
Definition: LazyMachineBlockFrequencyInfo.h:37
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MachineOptimizationRemarkEmitterPass
The analysis pass.
Definition: MachineOptimizationRemarkEmitter.h:218
llvm::MachineOptimizationRemarkEmitterPass::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: MachineOptimizationRemarkEmitter.cpp:71
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:228
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:592
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:131
ORE_NAME
#define ORE_NAME
Definition: MachineOptimizationRemarkEmitter.cpp:93
true
true
Definition: MachineOptimizationRemarkEmitter.cpp:99
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
LLVMContext.h
llvm::MachineOptimizationRemarkEmitterPass::ID
static char ID
Definition: MachineOptimizationRemarkEmitter.h:233
InitializePasses.h
llvm::DiagnosticInfoMIROptimization::MachineArgument::MachineArgument
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.
Definition: MachineOptimizationRemarkEmitter.cpp:24
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37