blob: 9a22633b6e4b464fa97692ee21a69ebb2855afc8 [file] [log] [blame]
Adam Nemetaad81602016-07-15 17:23:20 +00001//===- 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
21using namespace llvm;
22
23OptimizationRemarkEmitter::OptimizationRemarkEmitter() : FunctionPass(ID) {
24 initializeOptimizationRemarkEmitterPass(*PassRegistry::getPassRegistry());
25}
26
27Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(Value *V) {
28 if (!BFI)
29 return None;
30
31 return BFI->getBlockProfileCount(cast<BasicBlock>(V));
32}
33
34void OptimizationRemarkEmitter::emitOptimizationRemarkMissed(
35 const char *PassName, const DebugLoc &DLoc, Value *V, const Twine &Msg) {
36 LLVMContext &Ctx = F->getContext();
37 Ctx.diagnose(DiagnosticInfoOptimizationRemarkMissed(PassName, *F, DLoc, Msg,
38 computeHotness(V)));
39}
40
41void OptimizationRemarkEmitter::emitOptimizationRemarkMissed(
42 const char *PassName, Loop *L, const Twine &Msg) {
43 emitOptimizationRemarkMissed(PassName, L->getStartLoc(), L->getHeader(), Msg);
44}
45
46bool OptimizationRemarkEmitter::runOnFunction(Function &Fn) {
47 F = &Fn;
48
49 if (Fn.getContext().getDiagnosticHotnessRequested())
50 BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
51 else
52 BFI = nullptr;
53
54 return false;
55}
56
57void OptimizationRemarkEmitter::getAnalysisUsage(AnalysisUsage &AU) const {
58 LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
59 AU.setPreservesAll();
60}
61
62char OptimizationRemarkEmitter::ID = 0;
63static const char ore_name[] = "Optimization Remark Emitter";
64#define ORE_NAME "opt-remark-emitter"
65
66INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitter, ORE_NAME, ore_name, false,
67 true)
68INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
69INITIALIZE_PASS_END(OptimizationRemarkEmitter, ORE_NAME, ore_name, false, true)