blob: 6b6b5f2814a9089848850ee5c2ae48aa5d7d2a2d [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();
55 yaml::Output *Out = Ctx.getDiagnosticsOutputFile();
56 if (Out) {
57 auto *P = &const_cast<DiagnosticInfoOptimizationBase &>(OptDiagCommon);
58 *Out << P;
59 }
60 // FIXME: now that IsVerbose is part of DI, filtering for this will be moved
61 // from here to clang.
62 if (!OptDiag.isVerbose() || shouldEmitVerbose())
63 Ctx.diagnose(OptDiag);
64}
65
66MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass()
67 : MachineFunctionPass(ID) {
68 initializeMachineOptimizationRemarkEmitterPassPass(
69 *PassRegistry::getPassRegistry());
70}
71
72bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
73 MachineFunction &MF) {
74 MachineBlockFrequencyInfo *MBFI;
75
76 if (MF.getFunction()->getContext().getDiagnosticHotnessRequested())
Adam Nemetbbb141c2017-02-14 17:21:09 +000077 MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
Adam Nemeta9640662017-01-25 23:20:33 +000078 else
79 MBFI = nullptr;
80
81 ORE = llvm::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
82 return false;
83}
84
85void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
86 AnalysisUsage &AU) const {
Adam Nemetb516cf32017-02-23 17:30:01 +000087 AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
Adam Nemeta9640662017-01-25 23:20:33 +000088 AU.setPreservesAll();
89 MachineFunctionPass::getAnalysisUsage(AU);
90}
91
92char MachineOptimizationRemarkEmitterPass::ID = 0;
93static const char ore_name[] = "Machine Optimization Remark Emitter";
94#define ORE_NAME "machine-opt-remark-emitter"
95
96INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
97 false, true)
Adam Nemetb516cf32017-02-23 17:30:01 +000098INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass)
Adam Nemeta9640662017-01-25 23:20:33 +000099INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
100 false, true)