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