LLVM  5.0.0
MachineOptimizationRemarkEmitter.cpp
Go to the documentation of this file.
1 ///===- MachineOptimizationRemarkEmitter.cpp - Opt Diagnostic -*- C++ -*---===//
2 ///
3 /// The LLVM Compiler Infrastructure
4 ///
5 /// This file is distributed under the University of Illinois Open Source
6 /// License. See LICENSE.TXT for details.
7 ///
8 ///===---------------------------------------------------------------------===//
9 /// \file
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.
13 ///
14 ///===---------------------------------------------------------------------===//
15 
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/LLVMContext.h"
22 
23 using namespace llvm;
24 
26  StringRef MKey, const MachineInstr &MI)
27  : Argument() {
28  Key = MKey;
29 
31  MI.print(OS, /*SkipOpers=*/false, /*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  // If a diagnostic has a hotness value, then only emit it if its hotness
57  // meets the threshold.
58  if (OptDiag.getHotness() &&
59  *OptDiag.getHotness() < Ctx.getDiagnosticsHotnessThreshold()) {
60  return;
61  }
62 
64  if (Out) {
65  auto *P = &const_cast<DiagnosticInfoOptimizationBase &>(OptDiagCommon);
66  *Out << P;
67  }
68  // FIXME: now that IsVerbose is part of DI, filtering for this will be moved
69  // from here to clang.
70  if (!OptDiag.isVerbose() || shouldEmitVerbose())
71  Ctx.diagnose(OptDiag);
72 }
73 
78 }
79 
81  MachineFunction &MF) {
83 
85  MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
86  else
87  MBFI = nullptr;
88 
89  ORE = llvm::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
90  return false;
91 }
92 
94  AnalysisUsage &AU) const {
96  AU.setPreservesAll();
98 }
99 
101 static const char ore_name[] = "Machine Optimization Remark Emitter";
102 #define ORE_NAME "machine-opt-remark-emitter"
103 
105  false, true)
108  false, true)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
void emit(DiagnosticInfoOptimizationBase &OptDiag)
Emit an optimization remark.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
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++ -*–===//
yaml::Output * getDiagnosticsOutputFile()
Return the YAML file used by the backend to save optimization diagnostics.
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
MachineArgument(StringRef Key, const MachineInstr &MI)
Print an entire MachineInstr.
#define P(N)
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
This is an alternative analysis pass to MachineBlockFrequencyInfo.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
The Output class is used to generate a yaml document from in-memory structs and vectors.
Definition: YAMLTraits.h:1260
Used in the streaming interface as the general argument type.
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
const MachineBasicBlock * getBlock() const
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:59
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:463
IRTranslator LLVM IR MI
void setHotness(Optional< uint64_t > H)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Common features for diagnostics dealing with optimization remarks that are used by machine passes...
void initializeMachineOptimizationRemarkEmitterPassPass(PassRegistry &)
INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name, false, true) INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass