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 | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 23 | Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(Value *V) { |
| 24 | if (!BFI) |
| 25 | return None; |
| 26 | |
| 27 | return BFI->getBlockProfileCount(cast<BasicBlock>(V)); |
| 28 | } |
| 29 | |
| 30 | void OptimizationRemarkEmitter::emitOptimizationRemarkMissed( |
| 31 | const char *PassName, const DebugLoc &DLoc, Value *V, const Twine &Msg) { |
| 32 | LLVMContext &Ctx = F->getContext(); |
| 33 | Ctx.diagnose(DiagnosticInfoOptimizationRemarkMissed(PassName, *F, DLoc, Msg, |
| 34 | computeHotness(V))); |
| 35 | } |
| 36 | |
| 37 | void OptimizationRemarkEmitter::emitOptimizationRemarkMissed( |
| 38 | const char *PassName, Loop *L, const Twine &Msg) { |
| 39 | emitOptimizationRemarkMissed(PassName, L->getStartLoc(), L->getHeader(), Msg); |
| 40 | } |
| 41 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame^] | 42 | OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass() |
| 43 | : FunctionPass(ID) { |
| 44 | initializeOptimizationRemarkEmitterWrapperPassPass( |
| 45 | *PassRegistry::getPassRegistry()); |
| 46 | } |
| 47 | |
| 48 | bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function &Fn) { |
| 49 | BlockFrequencyInfo *BFI; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 50 | |
| 51 | if (Fn.getContext().getDiagnosticHotnessRequested()) |
| 52 | BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI(); |
| 53 | else |
| 54 | BFI = nullptr; |
| 55 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame^] | 56 | ORE = llvm::make_unique<OptimizationRemarkEmitter>(&Fn, BFI); |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame^] | 60 | void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage( |
| 61 | AnalysisUsage &AU) const { |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 62 | LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU); |
| 63 | AU.setPreservesAll(); |
| 64 | } |
| 65 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame^] | 66 | char OptimizationRemarkEmitterAnalysis::PassID; |
| 67 | |
| 68 | OptimizationRemarkEmitter |
| 69 | OptimizationRemarkEmitterAnalysis::run(Function &F, AnalysisManager<Function> &AM) { |
| 70 | BlockFrequencyInfo *BFI; |
| 71 | |
| 72 | if (F.getContext().getDiagnosticHotnessRequested()) |
| 73 | BFI = &AM.getResult<BlockFrequencyAnalysis>(F); |
| 74 | else |
| 75 | BFI = nullptr; |
| 76 | |
| 77 | return OptimizationRemarkEmitter(&F, BFI); |
| 78 | } |
| 79 | |
| 80 | char OptimizationRemarkEmitterWrapperPass::ID = 0; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 81 | static const char ore_name[] = "Optimization Remark Emitter"; |
| 82 | #define ORE_NAME "opt-remark-emitter" |
| 83 | |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame^] | 84 | INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, |
| 85 | false, true) |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame] | 86 | INITIALIZE_PASS_DEPENDENCY(LazyBFIPass) |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame^] | 87 | INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name, |
| 88 | false, true) |