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" |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DebugInfo.h" |
| 19 | #include "llvm/IR/DiagnosticInfo.h" |
| 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | Optional<uint64_t> |
| 25 | MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) { |
| 26 | if (!MBFI) |
| 27 | return None; |
| 28 | |
| 29 | return MBFI->getBlockProfileCount(&MBB); |
| 30 | } |
| 31 | |
| 32 | void MachineOptimizationRemarkEmitter::computeHotness( |
| 33 | DiagnosticInfoMIROptimization &Remark) { |
| 34 | const MachineBasicBlock *MBB = Remark.getBlock(); |
| 35 | if (MBB) |
| 36 | Remark.setHotness(computeHotness(*MBB)); |
| 37 | } |
| 38 | |
| 39 | void MachineOptimizationRemarkEmitter::emit( |
| 40 | DiagnosticInfoOptimizationBase &OptDiagCommon) { |
| 41 | auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon); |
| 42 | computeHotness(OptDiag); |
| 43 | |
| 44 | LLVMContext &Ctx = MF.getFunction()->getContext(); |
| 45 | yaml::Output *Out = Ctx.getDiagnosticsOutputFile(); |
| 46 | if (Out) { |
| 47 | auto *P = &const_cast<DiagnosticInfoOptimizationBase &>(OptDiagCommon); |
| 48 | *Out << P; |
| 49 | } |
| 50 | // FIXME: now that IsVerbose is part of DI, filtering for this will be moved |
| 51 | // from here to clang. |
| 52 | if (!OptDiag.isVerbose() || shouldEmitVerbose()) |
| 53 | Ctx.diagnose(OptDiag); |
| 54 | } |
| 55 | |
| 56 | MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass() |
| 57 | : MachineFunctionPass(ID) { |
| 58 | initializeMachineOptimizationRemarkEmitterPassPass( |
| 59 | *PassRegistry::getPassRegistry()); |
| 60 | } |
| 61 | |
| 62 | bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction( |
| 63 | MachineFunction &MF) { |
| 64 | MachineBlockFrequencyInfo *MBFI; |
| 65 | |
| 66 | if (MF.getFunction()->getContext().getDiagnosticHotnessRequested()) |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame^] | 67 | MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI(); |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 68 | else |
| 69 | MBFI = nullptr; |
| 70 | |
| 71 | ORE = llvm::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | void MachineOptimizationRemarkEmitterPass::getAnalysisUsage( |
| 76 | AnalysisUsage &AU) const { |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame^] | 77 | LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(AU); |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 78 | AU.setPreservesAll(); |
| 79 | MachineFunctionPass::getAnalysisUsage(AU); |
| 80 | } |
| 81 | |
| 82 | char MachineOptimizationRemarkEmitterPass::ID = 0; |
| 83 | static const char ore_name[] = "Machine Optimization Remark Emitter"; |
| 84 | #define ORE_NAME "machine-opt-remark-emitter" |
| 85 | |
| 86 | INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name, |
| 87 | false, true) |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame^] | 88 | INITIALIZE_PASS_DEPENDENCY(LazyMachineBFIPass) |
Adam Nemet | a964066 | 2017-01-25 23:20:33 +0000 | [diff] [blame] | 89 | INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name, |
| 90 | false, true) |