blob: c5d71a1815ed8214ca9398a435b1df3777655a51 [file] [log] [blame]
Adam Nemeta9640662017-01-25 23:20:33 +00001///===- 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
16#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
Adam Nemetbbb141c2017-02-14 17:21:09 +000017#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
Ahmed Bougacha27273972017-02-23 21:05:33 +000018#include "llvm/CodeGen/MachineInstr.h"
Adam Nemeta9640662017-01-25 23:20:33 +000019#include "llvm/IR/DebugInfo.h"
20#include "llvm/IR/DiagnosticInfo.h"
21#include "llvm/IR/LLVMContext.h"
22
23using namespace llvm;
24
Ahmed Bougacha27273972017-02-23 21:05:33 +000025DiagnosticInfoMIROptimization::MachineArgument::MachineArgument(
26 StringRef MKey, const MachineInstr &MI)
27 : Argument() {
28 Key = MKey;
29
30 raw_string_ostream OS(Val);
31 MI.print(OS, /*SkipOpers=*/false, /*SkipDebugLoc=*/true);
32}
33
Adam Nemeta9640662017-01-25 23:20:33 +000034Optional<uint64_t>
35MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
36 if (!MBFI)
37 return None;
38
39 return MBFI->getBlockProfileCount(&MBB);
40}
41
42void MachineOptimizationRemarkEmitter::computeHotness(
43 DiagnosticInfoMIROptimization &Remark) {
44 const MachineBasicBlock *MBB = Remark.getBlock();
45 if (MBB)
46 Remark.setHotness(computeHotness(*MBB));
47}
48
49void MachineOptimizationRemarkEmitter::emit(
50 DiagnosticInfoOptimizationBase &OptDiagCommon) {
51 auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
52 computeHotness(OptDiag);
53
54 LLVMContext &Ctx = MF.getFunction()->getContext();
Brian Gesiak4ef3daa2017-06-30 23:14:53 +000055
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
Adam Nemeta9640662017-01-25 23:20:33 +000063 yaml::Output *Out = Ctx.getDiagnosticsOutputFile();
64 if (Out) {
65 auto *P = &const_cast<DiagnosticInfoOptimizationBase &>(OptDiagCommon);
66 *Out << P;
67 }
Adam Nemetf1bea0a2017-10-04 15:18:07 +000068
69 Ctx.diagnose(OptDiag);
Adam Nemeta9640662017-01-25 23:20:33 +000070}
71
72MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass()
73 : MachineFunctionPass(ID) {
74 initializeMachineOptimizationRemarkEmitterPassPass(
75 *PassRegistry::getPassRegistry());
76}
77
78bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
79 MachineFunction &MF) {
80 MachineBlockFrequencyInfo *MBFI;
81
Brian Gesiak44e5f6c2017-06-30 18:13:59 +000082 if (MF.getFunction()->getContext().getDiagnosticsHotnessRequested())
Adam Nemetbbb141c2017-02-14 17:21:09 +000083 MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
Adam Nemeta9640662017-01-25 23:20:33 +000084 else
85 MBFI = nullptr;
86
87 ORE = llvm::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
88 return false;
89}
90
91void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
92 AnalysisUsage &AU) const {
Adam Nemetb516cf32017-02-23 17:30:01 +000093 AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
Adam Nemeta9640662017-01-25 23:20:33 +000094 AU.setPreservesAll();
95 MachineFunctionPass::getAnalysisUsage(AU);
96}
97
98char MachineOptimizationRemarkEmitterPass::ID = 0;
99static const char ore_name[] = "Machine Optimization Remark Emitter";
100#define ORE_NAME "machine-opt-remark-emitter"
101
102INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
103 false, true)
Adam Nemetb516cf32017-02-23 17:30:01 +0000104INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass)
Adam Nemeta9640662017-01-25 23:20:33 +0000105INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
106 false, true)