Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 1 | //===- OptimizationDiagnosticInfo.cpp - Optimization 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 | // |
| 10 | // Optimization diagnostic interfaces. It's packaged as an analysis pass so |
| 11 | // that by using this service passes become dependent on BFI as well. BFI is |
| 12 | // used to compute the "hotness" of the diagnostic message. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
| 16 | #include "llvm/Analysis/LazyBlockFrequencyInfo.h" |
| 17 | #include "llvm/Analysis/LoopInfo.h" |
| 18 | #include "llvm/IR/DiagnosticInfo.h" |
| 19 | #include "llvm/IR/LLVMContext.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
Adam Nemet | 6100d16 | 2016-07-20 21:44:22 +0000 | [diff] [blame^] | 23 | Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) { |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 24 | if (!BFI) |
| 25 | return None; |
| 26 | |
| 27 | return BFI->getBlockProfileCount(cast<BasicBlock>(V)); |
| 28 | } |
| 29 | |
| 30 | void OptimizationRemarkEmitter::emitOptimizationRemarkMissed( |
Adam Nemet | 6100d16 | 2016-07-20 21:44:22 +0000 | [diff] [blame^] | 31 | const char *PassName, const DebugLoc &DLoc, const Value *V, |
| 32 | const Twine &Msg) { |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 33 | LLVMContext &Ctx = F->getContext(); |
| 34 | Ctx.diagnose(DiagnosticInfoOptimizationRemarkMissed(PassName, *F, DLoc, Msg, |
| 35 | computeHotness(V))); |
| 36 | } |
| 37 | |
| 38 | void OptimizationRemarkEmitter::emitOptimizationRemarkMissed( |
| 39 | const char *PassName, Loop *L, const Twine &Msg) { |
| 40 | emitOptimizationRemarkMissed(PassName, L->getStartLoc(), L->getHeader(), Msg); |
| 41 | } |
| 42 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 43 | OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass() |
| 44 | : FunctionPass(ID) { |
| 45 | initializeOptimizationRemarkEmitterWrapperPassPass( |
| 46 | *PassRegistry::getPassRegistry()); |
| 47 | } |
| 48 | |
| 49 | bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function &Fn) { |
| 50 | BlockFrequencyInfo *BFI; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 51 | |
| 52 | if (Fn.getContext().getDiagnosticHotnessRequested()) |
| 53 | BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI(); |
| 54 | else |
| 55 | BFI = nullptr; |
| 56 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 57 | ORE = llvm::make_unique<OptimizationRemarkEmitter>(&Fn, BFI); |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 61 | void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage( |
| 62 | AnalysisUsage &AU) const { |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 63 | LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); |
| 64 | AU.setPreservesAll(); |
| 65 | } |
| 66 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 67 | char OptimizationRemarkEmitterAnalysis::PassID; |
| 68 | |
| 69 | OptimizationRemarkEmitter |
Adam Nemet | 78d1091 | 2016-07-20 21:44:18 +0000 | [diff] [blame] | 70 | OptimizationRemarkEmitterAnalysis::run(Function &F, |
| 71 | AnalysisManager<Function> &AM) { |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 72 | BlockFrequencyInfo *BFI; |
| 73 | |
| 74 | if (F.getContext().getDiagnosticHotnessRequested()) |
| 75 | BFI = &AM.getResult<BlockFrequencyAnalysis>(F); |
| 76 | else |
| 77 | BFI = nullptr; |
| 78 | |
| 79 | return OptimizationRemarkEmitter(&F, BFI); |
| 80 | } |
| 81 | |
| 82 | char OptimizationRemarkEmitterWrapperPass::ID = 0; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 83 | static const char ore_name[] = "Optimization Remark Emitter"; |
| 84 | #define ORE_NAME "opt-remark-emitter" |
| 85 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 86 | INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, |
| 87 | false, true) |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 88 | INITIALIZE_PASS_DEPENDENCY(LazyBFIPass) |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 89 | INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, |
| 90 | false, true) |