Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 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 | |
| 16 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h" |
Ahmed Bougacha | 2727397 | 2017-02-23 21:05:33 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstr.h" |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DebugInfo.h" |
| 20 | #include "llvm/IR/DiagnosticInfo.h" |
| 21 | #include "llvm/IR/LLVMContext.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
Ahmed Bougacha | 2727397 | 2017-02-23 21:05:33 +0000 | [diff] [blame] | 25 | DiagnosticInfoMIROptimization::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 Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 34 | Optional<uint64_t> |
| 35 | MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) { |
| 36 | if (!MBFI) |
| 37 | return None; |
| 38 | |
| 39 | return MBFI->getBlockProfileCount(&MBB); |
| 40 | } |
| 41 | |
| 42 | void MachineOptimizationRemarkEmitter::computeHotness( |
| 43 | DiagnosticInfoMIROptimization &Remark) { |
| 44 | const MachineBasicBlock *MBB = Remark.getBlock(); |
| 45 | if (MBB) |
| 46 | Remark.setHotness(computeHotness(*MBB)); |
| 47 | } |
| 48 | |
| 49 | void MachineOptimizationRemarkEmitter::emit( |
| 50 | DiagnosticInfoOptimizationBase &OptDiagCommon) { |
| 51 | auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon); |
| 52 | computeHotness(OptDiag); |
| 53 | |
| 54 | LLVMContext &Ctx = MF.getFunction()->getContext(); |
Brian Gesiak | 4ef3daa | 2017-06-30 23:14:53 +0000 | [diff] [blame] | 55 | |
Adam Nemet | 9303f62 | 2017-12-01 20:41:38 +0000 | [diff] [blame^] | 56 | // Only emit it if its hotness meets the threshold. |
| 57 | if (OptDiag.getHotness().getValueOr(0) < |
| 58 | Ctx.getDiagnosticsHotnessThreshold()) { |
Brian Gesiak | 4ef3daa | 2017-06-30 23:14:53 +0000 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
Adam Nemet | f1bea0a | 2017-10-04 15:18:07 +0000 | [diff] [blame] | 62 | Ctx.diagnose(OptDiag); |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass() |
| 66 | : MachineFunctionPass(ID) { |
| 67 | initializeMachineOptimizationRemarkEmitterPassPass( |
| 68 | *PassRegistry::getPassRegistry()); |
| 69 | } |
| 70 | |
| 71 | bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction( |
| 72 | MachineFunction &MF) { |
| 73 | MachineBlockFrequencyInfo *MBFI; |
| 74 | |
Brian Gesiak | 44e5f6c | 2017-06-30 18:13:59 +0000 | [diff] [blame] | 75 | if (MF.getFunction()->getContext().getDiagnosticsHotnessRequested()) |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 76 | MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI(); |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 77 | else |
| 78 | MBFI = nullptr; |
| 79 | |
| 80 | ORE = llvm::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | void MachineOptimizationRemarkEmitterPass::getAnalysisUsage( |
| 85 | AnalysisUsage &AU) const { |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 86 | AU.addRequired<LazyMachineBlockFrequencyInfoPass>(); |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 87 | AU.setPreservesAll(); |
| 88 | MachineFunctionPass::getAnalysisUsage(AU); |
| 89 | } |
| 90 | |
| 91 | char MachineOptimizationRemarkEmitterPass::ID = 0; |
| 92 | static const char ore_name[] = "Machine Optimization Remark Emitter"; |
| 93 | #define ORE_NAME "machine-opt-remark-emitter" |
| 94 | |
| 95 | INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name, |
| 96 | false, true) |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 97 | INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass) |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 98 | INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name, |
| 99 | false, true) |