LLVM 23.0.0git
DDGPrinter.cpp
Go to the documentation of this file.
1//===- DDGPrinter.cpp - DOT printer for the data dependence graph ----------==//
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//===----------------------------------------------------------------------===//
10//
11// This file defines the `-dot-ddg` analysis pass, which emits DDG in DOT format
12// in a file named `ddg.<graph-name>.dot` for each loop in a function.
13//===----------------------------------------------------------------------===//
14
18
19using namespace llvm;
20
21static cl::opt<bool> DotOnly("dot-ddg-only", cl::Hidden,
22 cl::desc("simple ddg dot graph"));
24 "dot-ddg-filename-prefix", cl::init("ddg"), cl::Hidden,
25 cl::desc("The prefix used for the DDG dot file names."));
26
27static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly = false);
28
29//===--------------------------------------------------------------------===//
30// Implementation of DDG DOT Printer for a loop
31//===--------------------------------------------------------------------===//
38
39static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly) {
40 std::string Filename =
41 Twine(DDGDotFilenamePrefix + "." + G.getName() + ".dot").str();
42 errs() << "Writing '" << Filename << "'...";
43
44 std::error_code EC;
46
47 if (!EC)
48 // We only provide the constant verson of the DOTGraphTrait specialization,
49 // hence the conversion to const pointer
50 WriteGraph(File, (const DataDependenceGraph *)&G, DOnly);
51 else
52 errs() << " error opening file for writing!";
53 errs() << "\n";
54}
55
56//===--------------------------------------------------------------------===//
57// DDG DOT Printer Implementation
58//===--------------------------------------------------------------------===//
59std::string DDGDotGraphTraits::getNodeLabel(const DDGNode *Node,
60 const DataDependenceGraph *Graph) {
61 if (isSimple())
62 return getSimpleNodeLabel(Node, Graph);
63 else
64 return getVerboseNodeLabel(Node, Graph);
65}
66
67std::string DDGDotGraphTraits::getEdgeAttributes(
69 const DataDependenceGraph *G) {
70 const DDGEdge *E = static_cast<const DDGEdge *>(*I.getCurrent());
71 if (isSimple())
72 return getSimpleEdgeAttributes(Node, E, G);
73 else
74 return getVerboseEdgeAttributes(Node, E, G);
75}
76
77bool DDGDotGraphTraits::isNodeHidden(const DDGNode *Node,
78 const DataDependenceGraph *Graph) {
79 if (isSimple() && isa<RootDDGNode>(Node))
80 return true;
81 assert(Graph && "expected a valid graph pointer");
82 return Graph->getPiBlock(*Node) != nullptr;
83}
84
85std::string DDGDotGraphTraits::getNodeAttributes(const DDGNode *Node,
86 const DataDependenceGraph *) {
87 if (isa<PiBlockDDGNode>(Node))
88 return "shape=box, style=\"rounded,filled\", fillcolor=lightyellow, "
89 "color=darkorange, penwidth=3";
90 if (isa<SimpleDDGNode>(Node) &&
92 return "style=filled, fillcolor=lightcyan";
93 return "";
94}
95
96std::string
97DDGDotGraphTraits::getSimpleNodeLabel(const DDGNode *Node,
98 const DataDependenceGraph *G) {
99 std::string Str;
100 raw_string_ostream OS(Str);
101 if (isa<SimpleDDGNode>(Node))
102 for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
103 OS << *II << "\n";
104 else if (isa<PiBlockDDGNode>(Node))
105 OS << "pi-block\nwith\n"
106 << cast<PiBlockDDGNode>(Node)->getNodes().size() << " nodes\n";
107 else if (isa<RootDDGNode>(Node))
108 OS << "root\n";
109 else
110 llvm_unreachable("Unimplemented type of node");
111 return OS.str();
112}
113
114std::string
115DDGDotGraphTraits::getVerboseNodeLabel(const DDGNode *Node,
116 const DataDependenceGraph *G) {
117 std::string Str;
118 raw_string_ostream OS(Str);
119 OS << "<kind:" << Node->getKind() << ">\n";
120 if (isa<SimpleDDGNode>(Node))
121 for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
122 OS << *II << "\n";
123 else if (isa<PiBlockDDGNode>(Node)) {
124 OS << "--- start of nodes in pi-block ---\n";
125 unsigned Count = 0;
126 const auto &PNodes = cast<PiBlockDDGNode>(Node)->getNodes();
127 for (auto *PN : PNodes) {
128 OS << getVerboseNodeLabel(PN, G);
129 if (++Count != PNodes.size())
130 OS << "\n";
131 }
132 OS << "--- end of nodes in pi-block ---\n";
133 } else if (isa<RootDDGNode>(Node))
134 OS << "root\n";
135 else
136 llvm_unreachable("Unimplemented type of node");
137 return OS.str();
138}
139
140/// EdgeKind to DOT color mapping:
141/// - MemoryDependence: red
142/// - RegisterDefUse: blue
143/// - Rooted/Unknown=default (no attribute).
146 OS << ", color=red";
147 else if (Kind == DDGEdge::EdgeKind::RegisterDefUse)
148 OS << ", color=blue";
149}
150
151std::string DDGDotGraphTraits::getSimpleEdgeAttributes(
152 const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
153 std::string Str;
154 raw_string_ostream OS(Str);
155 DDGEdge::EdgeKind Kind = Edge->getKind();
156 OS << "label=\"[" << Kind << "]\"";
157 writeEdgeKindColorAttr(OS, Kind);
158 return OS.str();
159}
160
161std::string DDGDotGraphTraits::getVerboseEdgeAttributes(
162 const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
163 std::string Str;
164 raw_string_ostream OS(Str);
165 DDGEdge::EdgeKind Kind = Edge->getKind();
166 OS << "label=\"[";
168 OS << G->getDependenceString(*Src, Edge->getTargetNode());
169 else
170 OS << Kind;
171 OS << "]\"";
172 writeEdgeKindColorAttr(OS, Kind);
173 return OS.str();
174}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly=false)
static void writeEdgeKindColorAttr(raw_ostream &OS, DDGEdge::EdgeKind Kind)
EdgeKind to DOT color mapping:
static cl::opt< bool > DotOnly("dot-ddg-only", cl::Hidden, cl::desc("simple ddg dot graph"))
static cl::opt< std::string > DDGDotFilenamePrefix("dot-ddg-filename-prefix", cl::init("ddg"), cl::Hidden, cl::desc("The prefix used for the DDG dot file names."))
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
uint64_t IntrinsicInst * II
static constexpr StringLiteral Filename
std::pair< BasicBlock *, BasicBlock * > Edge
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Analysis pass that builds the DDG for a loop.
Definition DDG.h:412
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Data Dependency Graph Edge.
Definition DDG.h:210
EdgeKind
The kind of edge in the DDG.
Definition DDG.h:213
Data Dependence Graph Node The graph can represent the following types of nodes:
Definition DDG.h:45
Data Dependency Graph.
Definition DDG.h:303
const PiBlockDDGNode * getPiBlock(const NodeType &N) const
If node N belongs to a pi-block return a pointer to the pi-block, otherwise return null.
Definition DDG.cpp:243
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:755
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...