blob: dcb8e4073ea344cb28c856be5eec17e3f1d15f63 [file] [log] [blame]
Adam Nemeta9640662017-01-25 23:20:33 +00001///===- MachineOptimizationRemarkEmitter.cpp - Opt Diagnostic -*- C++ -*---===//
2///
Chandler Carruth2946cd72019-01-19 08:50:56 +00003/// 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
Adam Nemeta9640662017-01-25 23:20:33 +00006///
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
15#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
Adam Nemetbbb141c2017-02-14 17:21:09 +000016#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
Ahmed Bougacha27273972017-02-23 21:05:33 +000017#include "llvm/CodeGen/MachineInstr.h"
Adam Nemeta9640662017-01-25 23:20:33 +000018#include "llvm/IR/DiagnosticInfo.h"
19#include "llvm/IR/LLVMContext.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080020#include "llvm/InitializePasses.h"
Adam Nemeta9640662017-01-25 23:20:33 +000021
22using namespace llvm;
23
Ahmed Bougacha27273972017-02-23 21:05:33 +000024DiagnosticInfoMIROptimization::MachineArgument::MachineArgument(
25 StringRef MKey, const MachineInstr &MI)
26 : Argument() {
Benjamin Krameradcd0262020-01-28 20:23:46 +010027 Key = std::string(MKey);
Ahmed Bougacha27273972017-02-23 21:05:33 +000028
29 raw_string_ostream OS(Val);
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +000030 MI.print(OS, /*IsStandalone=*/true, /*SkipOpers=*/false,
31 /*SkipDebugLoc=*/true);
Ahmed Bougacha27273972017-02-23 21:05:33 +000032}
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
Matthias Braunf1caa282017-12-15 22:22:58 +000054 LLVMContext &Ctx = MF.getFunction().getContext();
Brian Gesiak4ef3daa2017-06-30 23:14:53 +000055
Adam Nemet9303f622017-12-01 20:41:38 +000056 // Only emit it if its hotness meets the threshold.
57 if (OptDiag.getHotness().getValueOr(0) <
58 Ctx.getDiagnosticsHotnessThreshold()) {
Brian Gesiak4ef3daa2017-06-30 23:14:53 +000059 return;
60 }
61
Adam Nemetf1bea0a2017-10-04 15:18:07 +000062 Ctx.diagnose(OptDiag);
Adam Nemeta9640662017-01-25 23:20:33 +000063}
64
65MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass()
66 : MachineFunctionPass(ID) {
67 initializeMachineOptimizationRemarkEmitterPassPass(
68 *PassRegistry::getPassRegistry());
69}
70
71bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
72 MachineFunction &MF) {
73 MachineBlockFrequencyInfo *MBFI;
74
Matthias Braunf1caa282017-12-15 22:22:58 +000075 if (MF.getFunction().getContext().getDiagnosticsHotnessRequested())
Adam Nemetbbb141c2017-02-14 17:21:09 +000076 MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
Adam Nemeta9640662017-01-25 23:20:33 +000077 else
78 MBFI = nullptr;
79
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000080 ORE = std::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
Adam Nemeta9640662017-01-25 23:20:33 +000081 return false;
82}
83
84void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
85 AnalysisUsage &AU) const {
Adam Nemetb516cf32017-02-23 17:30:01 +000086 AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
Adam Nemeta9640662017-01-25 23:20:33 +000087 AU.setPreservesAll();
88 MachineFunctionPass::getAnalysisUsage(AU);
89}
90
91char MachineOptimizationRemarkEmitterPass::ID = 0;
92static const char ore_name[] = "Machine Optimization Remark Emitter";
93#define ORE_NAME "machine-opt-remark-emitter"
94
95INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
96 false, true)
Adam Nemetb516cf32017-02-23 17:30:01 +000097INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass)
Adam Nemeta9640662017-01-25 23:20:33 +000098INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
99 false, true)