Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
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
// Loops should be simplified before this analysis.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
14
#include "llvm/ADT/DenseMap.h"
15
#include "llvm/ADT/None.h"
16
#include "llvm/ADT/iterator.h"
17
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
18
#include "llvm/CodeGen/MachineBasicBlock.h"
19
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
20
#include "llvm/CodeGen/MachineFunction.h"
21
#include "llvm/CodeGen/MachineLoopInfo.h"
22
#include "llvm/Pass.h"
23
#include "llvm/Support/CommandLine.h"
24
#include "llvm/Support/GraphWriter.h"
25
#include <string>
26
27
using namespace llvm;
28
29
#define DEBUG_TYPE "machine-block-freq"
30
31
static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
32
    "view-machine-block-freq-propagation-dags", cl::Hidden,
33
    cl::desc("Pop up a window to show a dag displaying how machine block "
34
             "frequencies propagate through the CFG."),
35
    cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
36
               clEnumValN(GVDT_Fraction, "fraction",
37
                          "display a graph using the "
38
                          "fractional block frequency representation."),
39
               clEnumValN(GVDT_Integer, "integer",
40
                          "display a graph using the raw "
41
                          "integer fractional block frequency representation."),
42
               clEnumValN(GVDT_Count, "count", "display a graph using the real "
43
                                               "profile count if available.")));
44
45
// Similar option above, but used to control BFI display only after MBP pass
46
cl::opt<GVDAGType> ViewBlockLayoutWithBFI(
47
    "view-block-layout-with-bfi", cl::Hidden,
48
    cl::desc(
49
        "Pop up a window to show a dag displaying MBP layout and associated "
50
        "block frequencies of the CFG."),
51
    cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
52
               clEnumValN(GVDT_Fraction, "fraction",
53
                          "display a graph using the "
54
                          "fractional block frequency representation."),
55
               clEnumValN(GVDT_Integer, "integer",
56
                          "display a graph using the raw "
57
                          "integer fractional block frequency representation."),
58
               clEnumValN(GVDT_Count, "count",
59
                          "display a graph using the real "
60
                          "profile count if available.")));
61
62
// Command line option to specify the name of the function for CFG dump
63
// Defined in Analysis/BlockFrequencyInfo.cpp:  -view-bfi-func-name=
64
extern cl::opt<std::string> ViewBlockFreqFuncName;
65
66
// Command line option to specify hot frequency threshold.
67
// Defined in Analysis/BlockFrequencyInfo.cpp:  -view-hot-freq-perc=
68
extern cl::opt<unsigned> ViewHotFreqPercent;
69
70
static cl::opt<bool> PrintMachineBlockFreq(
71
    "print-machine-bfi", cl::init(false), cl::Hidden,
72
    cl::desc("Print the machine block frequency info."));
73
74
// Command line option to specify the name of the function for block frequency
75
// dump. Defined in Analysis/BlockFrequencyInfo.cpp.
76
extern cl::opt<std::string> PrintBlockFreqFuncName;
77
78
0
static GVDAGType getGVDT() {
79
0
  if (ViewBlockLayoutWithBFI != GVDT_None)
80
0
    return ViewBlockLayoutWithBFI;
81
0
82
0
  return ViewMachineBlockFreqPropagationDAG;
83
0
}
84
85
namespace llvm {
86
87
template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
88
  using NodeRef = const MachineBasicBlock *;
89
  using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
90
  using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
91
92
0
  static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
93
0
    return &G->getFunction()->front();
94
0
  }
95
96
0
  static ChildIteratorType child_begin(const NodeRef N) {
97
0
    return N->succ_begin();
98
0
  }
99
100
0
  static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
101
102
0
  static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
103
0
    return nodes_iterator(G->getFunction()->begin());
104
0
  }
105
106
0
  static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
107
0
    return nodes_iterator(G->getFunction()->end());
108
0
  }
109
};
110
111
using MBFIDOTGraphTraitsBase =
112
    BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
113
                          MachineBranchProbabilityInfo>;
114
115
template <>
116
struct DOTGraphTraits<MachineBlockFrequencyInfo *>
117
    : public MBFIDOTGraphTraitsBase {
118
  const MachineFunction *CurFunc = nullptr;
119
  DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
120
121
  explicit DOTGraphTraits(bool isSimple = false)
122
0
      : MBFIDOTGraphTraitsBase(isSimple) {}
123
124
  std::string getNodeLabel(const MachineBasicBlock *Node,
125
0
                           const MachineBlockFrequencyInfo *Graph) {
126
0
    int layout_order = -1;
127
0
    // Attach additional ordering information if 'isSimple' is false.
128
0
    if (!isSimple()) {
129
0
      const MachineFunction *F = Node->getParent();
130
0
      if (!CurFunc || F != CurFunc) {
131
0
        if (CurFunc)
132
0
          LayoutOrderMap.clear();
133
0
134
0
        CurFunc = F;
135
0
        int O = 0;
136
0
        for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
137
0
          LayoutOrderMap[&*MBI] = O;
138
0
        }
139
0
      }
140
0
      layout_order = LayoutOrderMap[Node];
141
0
    }
142
0
    return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
143
0
                                                layout_order);
144
0
  }
145
146
  std::string getNodeAttributes(const MachineBasicBlock *Node,
147
0
                                const MachineBlockFrequencyInfo *Graph) {
148
0
    return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
149
0
                                                     ViewHotFreqPercent);
150
0
  }
151
152
  std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
153
0
                                const MachineBlockFrequencyInfo *MBFI) {
154
0
    return MBFIDOTGraphTraitsBase::getEdgeAttributes(
155
0
        Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
156
0
  }
157
};
158
159
} // end namespace llvm
160
161
102k
INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE,
162
102k
                      "Machine Block Frequency Analysis", true, true)
163
102k
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
164
102k
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
165
102k
INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE,
166
                    "Machine Block Frequency Analysis", true, true)
167
168
char MachineBlockFrequencyInfo::ID = 0;
169
170
MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
171
148k
    : MachineFunctionPass(ID) {
172
148k
  initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
173
148k
}
174
175
147k
MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;
176
177
148k
void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
178
148k
  AU.addRequired<MachineBranchProbabilityInfo>();
179
148k
  AU.addRequired<MachineLoopInfo>();
180
148k
  AU.setPreservesAll();
181
148k
  MachineFunctionPass::getAnalysisUsage(AU);
182
148k
}
183
184
void MachineBlockFrequencyInfo::calculate(
185
    const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
186
2.03M
    const MachineLoopInfo &MLI) {
187
2.03M
  if (!MBFI)
188
2.03M
    MBFI.reset(new ImplType);
189
2.03M
  MBFI->calculate(F, MBPI, MLI);
190
2.03M
  if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
191
2.03M
      
(0
ViewBlockFreqFuncName.empty()0
||
192
0
       F.getName().equals(ViewBlockFreqFuncName))) {
193
0
    view("MachineBlockFrequencyDAGS." + F.getName());
194
0
  }
195
2.03M
  if (PrintMachineBlockFreq &&
196
2.03M
      
(6
PrintBlockFreqFuncName.empty()6
||
197
6
       
F.getName().equals(PrintBlockFreqFuncName)0
)) {
198
6
    MBFI->print(dbgs());
199
6
  }
200
2.03M
}
201
202
2.03M
bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
203
2.03M
  MachineBranchProbabilityInfo &MBPI =
204
2.03M
      getAnalysis<MachineBranchProbabilityInfo>();
205
2.03M
  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
206
2.03M
  calculate(F, MBPI, MLI);
207
2.03M
  return false;
208
2.03M
}
209
210
2.03M
void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
211
212
/// Pop up a ghostview window with the current block frequency propagation
213
/// rendered using dot.
214
0
void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
215
0
  // This code is only for debugging.
216
0
  ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
217
0
}
218
219
BlockFrequency
220
45.4M
MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
221
18.4E
  return MBFI ? 
MBFI->getBlockFreq(MBB)45.4M
: 0;
222
45.4M
}
223
224
Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
225
58
    const MachineBasicBlock *MBB) const {
226
58
  const Function &F = MBFI->getFunction()->getFunction();
227
58
  return MBFI ? MBFI->getBlockProfileCount(F, MBB) : 
None0
;
228
58
}
229
230
Optional<uint64_t>
231
0
MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
232
0
  const Function &F = MBFI->getFunction()->getFunction();
233
0
  return MBFI ? MBFI->getProfileCountFromFreq(F, Freq) : None;
234
0
}
235
236
bool
237
0
MachineBlockFrequencyInfo::isIrrLoopHeader(const MachineBasicBlock *MBB) {
238
0
  assert(MBFI && "Expected analysis to be available");
239
0
  return MBFI->isIrrLoopHeader(MBB);
240
0
}
241
242
0
const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
243
0
  return MBFI ? MBFI->getFunction() : nullptr;
244
0
}
245
246
0
const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
247
0
  return MBFI ? &MBFI->getBPI() : nullptr;
248
0
}
249
250
raw_ostream &
251
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
252
0
                                          const BlockFrequency Freq) const {
253
0
  return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
254
0
}
255
256
raw_ostream &
257
MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
258
0
                                          const MachineBasicBlock *MBB) const {
259
0
  return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
260
0
}
261
262
25.9M
uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
263
18.4E
  return MBFI ? 
MBFI->getEntryFreq()25.9M
: 0;
264
25.9M
}