Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/OptimizationRemarkEmitter.cpp
Line
Count
Source
1
//===- OptimizationRemarkEmitter.cpp - Optimization Diagnostic --*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Optimization diagnostic interfaces.  It's packaged as an analysis pass so
10
// that by using this service passes become dependent on BFI as well.  BFI is
11
// used to compute the "hotness" of the diagnostic message.
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
15
#include "llvm/Analysis/BranchProbabilityInfo.h"
16
#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
17
#include "llvm/Analysis/LoopInfo.h"
18
#include "llvm/IR/DiagnosticInfo.h"
19
#include "llvm/IR/Dominators.h"
20
#include "llvm/IR/LLVMContext.h"
21
22
using namespace llvm;
23
24
OptimizationRemarkEmitter::OptimizationRemarkEmitter(const Function *F)
25
6.22M
    : F(F), BFI(nullptr) {
26
6.22M
  if (!F->getContext().getDiagnosticsHotnessRequested())
27
6.22M
    return;
28
178
29
178
  // First create a dominator tree.
30
178
  DominatorTree DT;
31
178
  DT.recalculate(*const_cast<Function *>(F));
32
178
33
178
  // Generate LoopInfo from it.
34
178
  LoopInfo LI;
35
178
  LI.analyze(DT);
36
178
37
178
  // Then compute BranchProbabilityInfo.
38
178
  BranchProbabilityInfo BPI;
39
178
  BPI.calculate(*F, LI);
40
178
41
178
  // Finally compute BFI.
42
178
  OwnedBFI = llvm::make_unique<BlockFrequencyInfo>(*F, BPI, LI);
43
178
  BFI = OwnedBFI.get();
44
178
}
45
46
bool OptimizationRemarkEmitter::invalidate(
47
    Function &F, const PreservedAnalyses &PA,
48
5.06k
    FunctionAnalysisManager::Invalidator &Inv) {
49
5.06k
  // This analysis has no state and so can be trivially preserved but it needs
50
5.06k
  // a fresh view of BFI if it was constructed with one.
51
5.06k
  if (BFI && 
Inv.invalidate<BlockFrequencyAnalysis>(F, PA)96
)
52
55
    return true;
53
5.01k
54
5.01k
  // Otherwise this analysis result remains valid.
55
5.01k
  return false;
56
5.01k
}
57
58
181k
Optional<uint64_t> OptimizationRemarkEmitter::computeHotness(const Value *V) {
59
181k
  if (!BFI)
60
181k
    return None;
61
154
62
154
  return BFI->getBlockProfileCount(cast<BasicBlock>(V));
63
154
}
64
65
void OptimizationRemarkEmitter::computeHotness(
66
181k
    DiagnosticInfoIROptimization &OptDiag) {
67
181k
  const Value *V = OptDiag.getCodeRegion();
68
181k
  if (V)
69
181k
    OptDiag.setHotness(computeHotness(V));
70
181k
}
71
72
void OptimizationRemarkEmitter::emit(
73
181k
    DiagnosticInfoOptimizationBase &OptDiagBase) {
74
181k
  auto &OptDiag = cast<DiagnosticInfoIROptimization>(OptDiagBase);
75
181k
  computeHotness(OptDiag);
76
181k
77
181k
  // Only emit it if its hotness meets the threshold.
78
181k
  if (OptDiag.getHotness().getValueOr(0) <
79
181k
      F->getContext().getDiagnosticsHotnessThreshold()) {
80
23
    return;
81
23
  }
82
181k
83
181k
  F->getContext().diagnose(OptDiag);
84
181k
}
85
86
OptimizationRemarkEmitterWrapperPass::OptimizationRemarkEmitterWrapperPass()
87
222k
    : FunctionPass(ID) {
88
222k
  initializeOptimizationRemarkEmitterWrapperPassPass(
89
222k
      *PassRegistry::getPassRegistry());
90
222k
}
91
92
6.22M
bool OptimizationRemarkEmitterWrapperPass::runOnFunction(Function &Fn) {
93
6.22M
  BlockFrequencyInfo *BFI;
94
6.22M
95
6.22M
  if (Fn.getContext().getDiagnosticsHotnessRequested())
96
223
    BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
97
6.22M
  else
98
6.22M
    BFI = nullptr;
99
6.22M
100
6.22M
  ORE = llvm::make_unique<OptimizationRemarkEmitter>(&Fn, BFI);
101
6.22M
  return false;
102
6.22M
}
103
104
void OptimizationRemarkEmitterWrapperPass::getAnalysisUsage(
105
222k
    AnalysisUsage &AU) const {
106
222k
  LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
107
222k
  AU.setPreservesAll();
108
222k
}
109
110
AnalysisKey OptimizationRemarkEmitterAnalysis::Key;
111
112
OptimizationRemarkEmitter
113
OptimizationRemarkEmitterAnalysis::run(Function &F,
114
4.21k
                                       FunctionAnalysisManager &AM) {
115
4.21k
  BlockFrequencyInfo *BFI;
116
4.21k
117
4.21k
  if (F.getContext().getDiagnosticsHotnessRequested())
118
91
    BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
119
4.12k
  else
120
4.12k
    BFI = nullptr;
121
4.21k
122
4.21k
  return OptimizationRemarkEmitter(&F, BFI);
123
4.21k
}
124
125
char OptimizationRemarkEmitterWrapperPass::ID = 0;
126
static const char ore_name[] = "Optimization Remark Emitter";
127
#define ORE_NAME "opt-remark-emitter"
128
129
73.7k
INITIALIZE_PASS_BEGIN(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name,
130
73.7k
                      false, true)
131
73.7k
INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
132
73.7k
INITIALIZE_PASS_END(OptimizationRemarkEmitterWrapperPass, ORE_NAME, ore_name,
133
                    false, true)