LLVM  12.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  false, true)
99  false, true)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineBasicBlock & MBB
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
AnalysisUsage & addRequired()
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
This is an alternative analysis pass to MachineBlockFrequencyInfo.
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
Represent the analysis usage information of a pass.
void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:252
Used in the streaming interface as the general argument type.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
static const char ore_name[]
Common features for diagnostics dealing with optimization remarks that are used by both IR and MIR pa...
void setPreservesAll()
Set by analyses that do not transform their input at all.
Representation of each machine instruction.
Definition: MachineInstr.h:62
basic Basic Alias true
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:521
IRTranslator LLVM IR MI
void setHotness(Optional< uint64_t > H)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Common features for diagnostics dealing with optimization remarks that are used by machine passes...
void initializeMachineOptimizationRemarkEmitterPassPass(PassRegistry &)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name, false, true) INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass