MachineOptimizationRemarkEmitter.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
  16. #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
  17. #include "llvm/CodeGen/MachineInstr.h"
  18. #include "llvm/IR/DebugInfo.h"
  19. #include "llvm/IR/DiagnosticInfo.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. using namespace llvm;
  22. DiagnosticInfoMIROptimization::MachineArgument::MachineArgument(
  23. StringRef MKey, const MachineInstr &MI)
  24. : Argument() {
  25. Key = MKey;
  26. raw_string_ostream OS(Val);
  27. MI.print(OS, /*SkipOpers=*/false, /*SkipDebugLoc=*/true);
  28. }
  29. Optional<uint64_t>
  30. MachineOptimizationRemarkEmitter::computeHotness(const MachineBasicBlock &MBB) {
  31. if (!MBFI)
  32. return None;
  33. return MBFI->getBlockProfileCount(&MBB);
  34. }
  35. void MachineOptimizationRemarkEmitter::computeHotness(
  36. DiagnosticInfoMIROptimization &Remark) {
  37. const MachineBasicBlock *MBB = Remark.getBlock();
  38. if (MBB)
  39. Remark.setHotness(computeHotness(*MBB));
  40. }
  41. void MachineOptimizationRemarkEmitter::emit(
  42. DiagnosticInfoOptimizationBase &OptDiagCommon) {
  43. auto &OptDiag = cast<DiagnosticInfoMIROptimization>(OptDiagCommon);
  44. computeHotness(OptDiag);
  45. LLVMContext &Ctx = MF.getFunction()->getContext();
  46. // If a diagnostic has a hotness value, then only emit it if its hotness
  47. // meets the threshold.
  48. if (OptDiag.getHotness() &&
  49. *OptDiag.getHotness() < Ctx.getDiagnosticsHotnessThreshold()) {
  50. return;
  51. }
  52. Ctx.diagnose(OptDiag);
  53. }
  54. MachineOptimizationRemarkEmitterPass::MachineOptimizationRemarkEmitterPass()
  55. : MachineFunctionPass(ID) {
  56. initializeMachineOptimizationRemarkEmitterPassPass(
  57. *PassRegistry::getPassRegistry());
  58. }
  59. bool MachineOptimizationRemarkEmitterPass::runOnMachineFunction(
  60. MachineFunction &MF) {
  61. MachineBlockFrequencyInfo *MBFI;
  62. if (MF.getFunction()->getContext().getDiagnosticsHotnessRequested())
  63. MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
  64. else
  65. MBFI = nullptr;
  66. ORE = llvm::make_unique<MachineOptimizationRemarkEmitter>(MF, MBFI);
  67. return false;
  68. }
  69. void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
  70. AnalysisUsage &AU) const {
  71. AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
  72. AU.setPreservesAll();
  73. MachineFunctionPass::getAnalysisUsage(AU);
  74. }
  75. char MachineOptimizationRemarkEmitterPass::ID = 0;
  76. static const char ore_name[] = "Machine Optimization Remark Emitter";
  77. #define ORE_NAME "machine-opt-remark-emitter"
  78. INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
  79. false, true)
  80. INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass)
  81. INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
  82. false, true)