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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- llvm/Analysis/DDG.h --------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the Data-Dependence Graph (DDG).
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ANALYSIS_DDG_H
#define LLVM_ANALYSIS_DDG_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DirectedGraph.h"
#include "llvm/Analysis/DependenceAnalysis.h"
#include "llvm/Analysis/DependenceGraphBuilder.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/IR/Instructions.h"
namespace llvm {
class DDGNode;
class DDGEdge;
using DDGNodeBase = DGNode<DDGNode, DDGEdge>;
using DDGEdgeBase = DGEdge<DDGNode, DDGEdge>;
using DDGBase = DirectedGraph<DDGNode, DDGEdge>;
class LPMUpdater;
/// Data Dependence Graph Node
/// The graph can represent the following types of nodes:
/// 1. Single instruction node containing just one instruction.
/// 2. Multiple instruction node where two or more instructions from
/// the same basic block are merged into one node.
/// 3. Pi-block node which is a group of other DDG nodes that are part of a
/// strongly-connected component of the graph.
/// A pi-block node contains more than one single or multiple instruction
/// nodes. The root node cannot be part of a pi-block.
/// 4. Root node is a special node that connects to all components such that
/// there is always a path from it to any node in the graph.
class DDGNode : public DDGNodeBase {
public:
using InstructionListType = SmallVectorImpl<Instruction *>;
enum class NodeKind {
Unknown,
SingleInstruction,
MultiInstruction,
PiBlock,
Root,
};
DDGNode() = delete;
DDGNode(const NodeKind K) : DDGNodeBase(), Kind(K) {}
DDGNode(const DDGNode &N) : DDGNodeBase(N), Kind(N.Kind) {}
DDGNode(DDGNode &&N) : DDGNodeBase(std::move(N)), Kind(N.Kind) {}
virtual ~DDGNode() = 0;
DDGNode &operator=(const DDGNode &N) {
DGNode::operator=(N);
Kind = N.Kind;
return *this;
}
DDGNode &operator=(DDGNode &&N) {
DGNode::operator=(std::move(N));
Kind = N.Kind;
return *this;
}
/// Getter for the kind of this node.
NodeKind getKind() const { return Kind; }
/// Collect a list of instructions, in \p IList, for which predicate \p Pred
/// evaluates to true when iterating over instructions of this node. Return
/// true if at least one instruction was collected, and false otherwise.
bool collectInstructions(llvm::function_ref<bool(Instruction *)> const &Pred,
InstructionListType &IList) const;
protected:
/// Setter for the kind of this node.
void setKind(NodeKind K) { Kind = K; }
private:
NodeKind Kind;
};
/// Subclass of DDGNode representing the root node of the graph.
/// There should only be one such node in a given graph.
class RootDDGNode : public DDGNode {
public:
RootDDGNode() : DDGNode(NodeKind::Root) {}
RootDDGNode(const RootDDGNode &N) = delete;
RootDDGNode(RootDDGNode &&N) : DDGNode(std::move(N)) {}
~RootDDGNode() {}
/// Define classof to be able to use isa<>, cast<>, dyn_cast<>, etc.
static bool classof(const DDGNode *N) {
return N->getKind() == NodeKind::Root;
}
static bool classof(const RootDDGNode *N) { return true; }
};
/// Subclass of DDGNode representing single or multi-instruction nodes.
class SimpleDDGNode : public DDGNode {
friend class DDGBuilder;
public:
SimpleDDGNode() = delete;
SimpleDDGNode(Instruction &I);
SimpleDDGNode(const SimpleDDGNode &N);
SimpleDDGNode(SimpleDDGNode &&N);
~SimpleDDGNode();
SimpleDDGNode &operator=(const SimpleDDGNode &N) {
DDGNode::operator=(N);
InstList = N.InstList;
return *this;
}
SimpleDDGNode &operator=(SimpleDDGNode &&N) {
DDGNode::operator=(std::move(N));
InstList = std::move(N.InstList);
return *this;
}
/// Get the list of instructions in this node.
const InstructionListType &getInstructions() const {
assert(!InstList.empty() && "Instruction List is empty.");
return InstList;
}
InstructionListType &getInstructions() {
return const_cast<InstructionListType &>(
static_cast<const SimpleDDGNode *>(this)->getInstructions());
}
/// Get the first/last instruction in the node.
Instruction *getFirstInstruction() const { return getInstructions().front(); }
Instruction *getLastInstruction() const { return getInstructions().back(); }
/// Define classof to be able to use isa<>, cast<>, dyn_cast<>, etc.
static bool classof(const DDGNode *N) {
return N->getKind() == NodeKind::SingleInstruction ||
N->getKind() == NodeKind::MultiInstruction;
}
static bool classof(const SimpleDDGNode *N) { return true; }
private:
/// Append the list of instructions in \p Input to this node.
void appendInstructions(const InstructionListType &Input) {
setKind((InstList.size() == 0 && Input.size() == 1)
? NodeKind::SingleInstruction
: NodeKind::MultiInstruction);
llvm::append_range(InstList, Input);
}
void appendInstructions(const SimpleDDGNode &Input) {
appendInstructions(Input.getInstructions());
}
/// List of instructions associated with a single or multi-instruction node.
SmallVector<Instruction *, 2> InstList;
};
/// Subclass of DDGNode representing a pi-block. A pi-block represents a group
/// of DDG nodes that are part of a strongly-connected component of the graph.
/// Replacing all the SCCs with pi-blocks results in an acyclic representation
/// of the DDG. For example if we have:
/// {a -> b}, {b -> c, d}, {c -> a}
/// the cycle a -> b -> c -> a is abstracted into a pi-block "p" as follows:
/// {p -> d} with "p" containing: {a -> b}, {b -> c}, {c -> a}
class PiBlockDDGNode : public DDGNode {
public:
using PiNodeList = SmallVector<DDGNode *, 4>;
PiBlockDDGNode() = delete;
PiBlockDDGNode(const PiNodeList &List);
PiBlockDDGNode(const PiBlockDDGNode &N);
PiBlockDDGNode(PiBlockDDGNode &&N);
~PiBlockDDGNode();
PiBlockDDGNode &operator=(const PiBlockDDGNode &N) {
DDGNode::operator=(N);
NodeList = N.NodeList;
return *this;
}
PiBlockDDGNode &operator=(PiBlockDDGNode &&N) {
DDGNode::operator=(std::move(N));
NodeList = std::move(N.NodeList);
return *this;
}
/// Get the list of nodes in this pi-block.
const PiNodeList &getNodes() const {
assert(!NodeList.empty() && "Node list is empty.");
return NodeList;
}
PiNodeList &getNodes() {
return const_cast<PiNodeList &>(
static_cast<const PiBlockDDGNode *>(this)->getNodes());
}
/// Define classof to be able to use isa<>, cast<>, dyn_cast<>, etc.
static bool classof(const DDGNode *N) {
return N->getKind() == NodeKind::PiBlock;
}
private:
/// List of nodes in this pi-block.
PiNodeList NodeList;
};
/// Data Dependency Graph Edge.
/// An edge in the DDG can represent a def-use relationship or
/// a memory dependence based on the result of DependenceAnalysis.
/// A rooted edge connects the root node to one of the components
/// of the graph.
class DDGEdge : public DDGEdgeBase {
public:
/// The kind of edge in the DDG
enum class EdgeKind {
Unknown,
RegisterDefUse,
MemoryDependence,
Rooted,
Last = Rooted // Must be equal to the largest enum value.
};
explicit DDGEdge(DDGNode &N) = delete;
DDGEdge(DDGNode &N, EdgeKind K) : DDGEdgeBase(N), Kind(K) {}
DDGEdge(const DDGEdge &E) : DDGEdgeBase(E), Kind(E.getKind()) {}
DDGEdge(DDGEdge &&E) : DDGEdgeBase(std::move(E)), Kind(E.Kind) {}
DDGEdge &operator=(const DDGEdge &E) {
DDGEdgeBase::operator=(E);
Kind = E.Kind;
return *this;
}
DDGEdge &operator=(DDGEdge &&E) {
DDGEdgeBase::operator=(std::move(E));
Kind = E.Kind;
return *this;
}
/// Get the edge kind
EdgeKind getKind() const { return Kind; };
/// Return true if this is a def-use edge, and false otherwise.
bool isDefUse() const { return Kind == EdgeKind::RegisterDefUse; }
/// Return true if this is a memory dependence edge, and false otherwise.
bool isMemoryDependence() const { return Kind == EdgeKind::MemoryDependence; }
/// Return true if this is an edge stemming from the root node, and false
/// otherwise.
bool isRooted() const { return Kind == EdgeKind::Rooted; }
private:
EdgeKind Kind;
};
/// Encapsulate some common data and functionality needed for different
/// variations of data dependence graphs.
template <typename NodeType> class DependenceGraphInfo {
public:
using DependenceList = SmallVector<std::unique_ptr<Dependence>, 1>;
DependenceGraphInfo() = delete;
DependenceGraphInfo(const DependenceGraphInfo &G) = delete;
DependenceGraphInfo(const std::string &N, const DependenceInfo &DepInfo)
: Name(N), DI(DepInfo), Root(nullptr) {}
DependenceGraphInfo(DependenceGraphInfo &&G)
: Name(std::move(G.Name)), DI(std::move(G.DI)), Root(G.Root) {}
virtual ~DependenceGraphInfo() {}
/// Return the label that is used to name this graph.
const StringRef getName() const { return Name; }
/// Return the root node of the graph.
NodeType &getRoot() const {
assert(Root && "Root node is not available yet. Graph construction may "
"still be in progress\n");
return *Root;
}
/// Collect all the data dependency infos coming from any pair of memory
/// accesses from \p Src to \p Dst, and store them into \p Deps. Return true
/// if a dependence exists, and false otherwise.
bool getDependencies(const NodeType &Src, const NodeType &Dst,
DependenceList &Deps) const;
/// Return a string representing the type of dependence that the dependence
/// analysis identified between the two given nodes. This function assumes
/// that there is a memory dependence between the given two nodes.
const std::string getDependenceString(const NodeType &Src,
const NodeType &Dst) const;
protected:
// Name of the graph.
std::string Name;
// Store a copy of DependenceInfo in the graph, so that individual memory
// dependencies don't need to be stored. Instead when the dependence is
// queried it is recomputed using @DI.
const DependenceInfo DI;
// A special node in the graph that has an edge to every connected component of
// the graph, to ensure all nodes are reachable in a graph walk.
NodeType *Root = nullptr;
};
using DDGInfo = DependenceGraphInfo<DDGNode>;
/// Data Dependency Graph
class DataDependenceGraph : public DDGBase, public DDGInfo {
friend AbstractDependenceGraphBuilder<DataDependenceGraph>;
friend class DDGBuilder;
public:
using NodeType = DDGNode;
using EdgeType = DDGEdge;
DataDependenceGraph() = delete;
DataDependenceGraph(const DataDependenceGraph &G) = delete;
DataDependenceGraph(DataDependenceGraph &&G)
: DDGBase(std::move(G)), DDGInfo(std::move(G)) {}
DataDependenceGraph(Function &F, DependenceInfo &DI);
DataDependenceGraph(Loop &L, LoopInfo &LI, DependenceInfo &DI);
~DataDependenceGraph();
/// If node \p N belongs to a pi-block return a pointer to the pi-block,
/// otherwise return null.
const PiBlockDDGNode *getPiBlock(const NodeType &N) const;
protected:
/// Add node \p N to the graph, if it's not added yet, and keep track of the
/// root node as well as pi-blocks and their members. Return true if node is
/// successfully added.
bool addNode(NodeType &N);
private:
using PiBlockMapType = DenseMap<const NodeType *, const PiBlockDDGNode *>;
/// Mapping from graph nodes to their containing pi-blocks. If a node is not
/// part of a pi-block, it will not appear in this map.
PiBlockMapType PiBlockMap;
};
/// Concrete implementation of a pure data dependence graph builder. This class
/// provides custom implementation for the pure-virtual functions used in the
/// generic dependence graph build algorithm.
///
/// For information about time complexity of the build algorithm see the
/// comments near the declaration of AbstractDependenceGraphBuilder.
class DDGBuilder : public AbstractDependenceGraphBuilder<DataDependenceGraph> {
public:
DDGBuilder(DataDependenceGraph &G, DependenceInfo &D,
const BasicBlockListType &BBs)
: AbstractDependenceGraphBuilder(G, D, BBs) {}
DDGNode &createRootNode() final override {
auto *RN = new RootDDGNode();
assert(RN && "Failed to allocate memory for DDG root node.");
Graph.addNode(*RN);
return *RN;
}
DDGNode &createFineGrainedNode(Instruction &I) final override {
auto *SN = new SimpleDDGNode(I);
assert(SN && "Failed to allocate memory for simple DDG node.");
Graph.addNode(*SN);
return *SN;
}
DDGNode &createPiBlock(const NodeListType &L) final override {
auto *Pi = new PiBlockDDGNode(L);
assert(Pi && "Failed to allocate memory for pi-block node.");
Graph.addNode(*Pi);
return *Pi;
}
DDGEdge &createDefUseEdge(DDGNode &Src, DDGNode &Tgt) final override {
auto *E = new DDGEdge(Tgt, DDGEdge::EdgeKind::RegisterDefUse);
assert(E && "Failed to allocate memory for edge");
Graph.connect(Src, Tgt, *E);
return *E;
}
DDGEdge &createMemoryEdge(DDGNode &Src, DDGNode &Tgt) final override {
auto *E = new DDGEdge(Tgt, DDGEdge::EdgeKind::MemoryDependence);
assert(E && "Failed to allocate memory for edge");
Graph.connect(Src, Tgt, *E);
return *E;
}
DDGEdge &createRootedEdge(DDGNode &Src, DDGNode &Tgt) final override {
auto *E = new DDGEdge(Tgt, DDGEdge::EdgeKind::Rooted);
assert(E && "Failed to allocate memory for edge");
assert(isa<RootDDGNode>(Src) && "Expected root node");
Graph.connect(Src, Tgt, *E);
return *E;
}
const NodeListType &getNodesInPiBlock(const DDGNode &N) final override {
auto *PiNode = dyn_cast<const PiBlockDDGNode>(&N);
assert(PiNode && "Expected a pi-block node.");
return PiNode->getNodes();
}
/// Return true if the two nodes \pSrc and \pTgt are both simple nodes and
/// the consecutive instructions after merging belong to the same basic block.
bool areNodesMergeable(const DDGNode &Src,
const DDGNode &Tgt) const final override;
void mergeNodes(DDGNode &Src, DDGNode &Tgt) final override;
bool shouldSimplify() const final override;
bool shouldCreatePiBlocks() const final override;
};
raw_ostream &operator<<(raw_ostream &OS, const DDGNode &N);
raw_ostream &operator<<(raw_ostream &OS, const DDGNode::NodeKind K);
raw_ostream &operator<<(raw_ostream &OS, const DDGEdge &E);
raw_ostream &operator<<(raw_ostream &OS, const DDGEdge::EdgeKind K);
raw_ostream &operator<<(raw_ostream &OS, const DataDependenceGraph &G);
//===--------------------------------------------------------------------===//
// DDG Analysis Passes
//===--------------------------------------------------------------------===//
/// Analysis pass that builds the DDG for a loop.
class DDGAnalysis : public AnalysisInfoMixin<DDGAnalysis> {
public:
using Result = std::unique_ptr<DataDependenceGraph>;
Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
private:
friend AnalysisInfoMixin<DDGAnalysis>;
static AnalysisKey Key;
};
/// Textual printer pass for the DDG of a loop.
class DDGAnalysisPrinterPass : public PassInfoMixin<DDGAnalysisPrinterPass> {
public:
explicit DDGAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U);
private:
raw_ostream &OS;
};
//===--------------------------------------------------------------------===//
// DependenceGraphInfo Implementation
//===--------------------------------------------------------------------===//
template <typename NodeType>
bool DependenceGraphInfo<NodeType>::getDependencies(
const NodeType &Src, const NodeType &Dst, DependenceList &Deps) const {
assert(Deps.empty() && "Expected empty output list at the start.");
// List of memory access instructions from src and dst nodes.
SmallVector<Instruction *, 8> SrcIList, DstIList;
auto isMemoryAccess = [](const Instruction *I) {
return I->mayReadOrWriteMemory();
};
Src.collectInstructions(isMemoryAccess, SrcIList);
Dst.collectInstructions(isMemoryAccess, DstIList);
for (auto *SrcI : SrcIList)
for (auto *DstI : DstIList)
if (auto Dep =
const_cast<DependenceInfo *>(&DI)->depends(SrcI, DstI, true))
Deps.push_back(std::move(Dep));
return !Deps.empty();
}
template <typename NodeType>
const std::string
DependenceGraphInfo<NodeType>::getDependenceString(const NodeType &Src,
const NodeType &Dst) const {
std::string Str;
raw_string_ostream OS(Str);
DependenceList Deps;
if (!getDependencies(Src, Dst, Deps))
return OS.str();
interleaveComma(Deps, OS, [&](const std::unique_ptr<Dependence> &D) {
D->dump(OS);
// Remove the extra new-line character printed by the dump
// method
if (OS.str().back() == '\n')
OS.str().pop_back();
});
return OS.str();
}
//===--------------------------------------------------------------------===//
// GraphTraits specializations for the DDG
//===--------------------------------------------------------------------===//
/// non-const versions of the grapth trait specializations for DDG
template <> struct GraphTraits<DDGNode *> {
using NodeRef = DDGNode *;
static DDGNode *DDGGetTargetNode(DGEdge<DDGNode, DDGEdge> *P) {
return &P->getTargetNode();
}
// Provide a mapped iterator so that the GraphTrait-based implementations can
// find the target nodes without having to explicitly go through the edges.
using ChildIteratorType =
mapped_iterator<DDGNode::iterator, decltype(&DDGGetTargetNode)>;
using ChildEdgeIteratorType = DDGNode::iterator;
static NodeRef getEntryNode(NodeRef N) { return N; }
static ChildIteratorType child_begin(NodeRef N) {
return ChildIteratorType(N->begin(), &DDGGetTargetNode);
}
static ChildIteratorType child_end(NodeRef N) {
return ChildIteratorType(N->end(), &DDGGetTargetNode);
}
static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
return N->begin();
}
static ChildEdgeIteratorType child_edge_end(NodeRef N) { return N->end(); }
};
template <>
struct GraphTraits<DataDependenceGraph *> : public GraphTraits<DDGNode *> {
using nodes_iterator = DataDependenceGraph::iterator;
static NodeRef getEntryNode(DataDependenceGraph *DG) {
return &DG->getRoot();
}
static nodes_iterator nodes_begin(DataDependenceGraph *DG) {
return DG->begin();
}
static nodes_iterator nodes_end(DataDependenceGraph *DG) { return DG->end(); }
};
/// const versions of the grapth trait specializations for DDG
template <> struct GraphTraits<const DDGNode *> {
using NodeRef = const DDGNode *;
static const DDGNode *DDGGetTargetNode(const DGEdge<DDGNode, DDGEdge> *P) {
return &P->getTargetNode();
}
// Provide a mapped iterator so that the GraphTrait-based implementations can
// find the target nodes without having to explicitly go through the edges.
using ChildIteratorType =
mapped_iterator<DDGNode::const_iterator, decltype(&DDGGetTargetNode)>;
using ChildEdgeIteratorType = DDGNode::const_iterator;
static NodeRef getEntryNode(NodeRef N) { return N; }
static ChildIteratorType child_begin(NodeRef N) {
return ChildIteratorType(N->begin(), &DDGGetTargetNode);
}
static ChildIteratorType child_end(NodeRef N) {
return ChildIteratorType(N->end(), &DDGGetTargetNode);
}
static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
return N->begin();
}
static ChildEdgeIteratorType child_edge_end(NodeRef N) { return N->end(); }
};
template <>
struct GraphTraits<const DataDependenceGraph *>
: public GraphTraits<const DDGNode *> {
using nodes_iterator = DataDependenceGraph::const_iterator;
static NodeRef getEntryNode(const DataDependenceGraph *DG) {
return &DG->getRoot();
}
static nodes_iterator nodes_begin(const DataDependenceGraph *DG) {
return DG->begin();
}
static nodes_iterator nodes_end(const DataDependenceGraph *DG) {
return DG->end();
}
};
} // namespace llvm
#endif // LLVM_ANALYSIS_DDG_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|