LLVM 24.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
16#include "AnalysisOptions.h"
18
19using namespace llvm;
20
21static void writeDDGToDotFile(const AnalysisOptions &Opts,
23
24//===--------------------------------------------------------------------===//
25// Implementation of DDG DOT Printer for a loop
26//===--------------------------------------------------------------------===//
33
34static void writeDDGToDotFile(const AnalysisOptions &Opts,
36 std::string Filename =
37 Twine(Opts.dot_ddg_filename_prefix + "." + G.getName() + ".dot").str();
38 errs() << "Writing '" << Filename << "'...";
39
40 std::error_code EC;
42
43 if (!EC)
44 // We only provide the constant verson of the DOTGraphTrait specialization,
45 // hence the conversion to const pointer
46 WriteGraph(File, (const DataDependenceGraph *)&G, Opts.dot_ddg_only);
47 else
48 errs() << " error opening file for writing!";
49 errs() << "\n";
50}
51
52//===--------------------------------------------------------------------===//
53// DDG DOT Printer Implementation
54//===--------------------------------------------------------------------===//
55std::string DDGDotGraphTraits::getNodeLabel(const DDGNode *Node,
57 if (isSimple())
58 return getSimpleNodeLabel(Node, Graph);
59 else
60 return getVerboseNodeLabel(Node, Graph);
61}
62
63std::string DDGDotGraphTraits::getEdgeAttributes(
65 const DataDependenceGraph *G) {
66 const DDGEdge *E = static_cast<const DDGEdge *>(*I.getCurrent());
67 if (isSimple())
68 return getSimpleEdgeAttributes(Node, E, G);
69 else
70 return getVerboseEdgeAttributes(Node, E, G);
71}
72
73bool DDGDotGraphTraits::isNodeHidden(const DDGNode *Node,
75 if (isSimple() && isa<RootDDGNode>(Node))
76 return true;
77 assert(Graph && "expected a valid graph pointer");
78 return Graph->getPiBlock(*Node) != nullptr;
79}
80
81std::string DDGDotGraphTraits::getNodeAttributes(const DDGNode *Node,
82 const DataDependenceGraph *) {
83 if (isa<PiBlockDDGNode>(Node))
84 return "shape=box, style=\"rounded,filled\", fillcolor=lightyellow, "
85 "color=darkorange, penwidth=3";
86 if (isa<SimpleDDGNode>(Node) &&
88 return "style=filled, fillcolor=lightcyan";
89 return "";
90}
91
92std::string
93DDGDotGraphTraits::getSimpleNodeLabel(const DDGNode *Node,
94 const DataDependenceGraph *G) {
95 std::string Str;
96 raw_string_ostream OS(Str);
97 if (isa<SimpleDDGNode>(Node))
98 for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
99 OS << *II << "\n";
100 else if (isa<PiBlockDDGNode>(Node))
101 OS << "pi-block\nwith\n"
102 << cast<PiBlockDDGNode>(Node)->getNodes().size() << " nodes\n";
103 else if (isa<RootDDGNode>(Node))
104 OS << "root\n";
105 else
106 llvm_unreachable("Unimplemented type of node");
107 return OS.str();
108}
109
110std::string
111DDGDotGraphTraits::getVerboseNodeLabel(const DDGNode *Node,
112 const DataDependenceGraph *G) {
113 std::string Str;
114 raw_string_ostream OS(Str);
115 OS << "<kind:" << Node->getKind() << ">\n";
116 if (isa<SimpleDDGNode>(Node))
117 for (auto *II : static_cast<const SimpleDDGNode *>(Node)->getInstructions())
118 OS << *II << "\n";
119 else if (isa<PiBlockDDGNode>(Node)) {
120 OS << "--- start of nodes in pi-block ---\n";
121 unsigned Count = 0;
122 const auto &PNodes = cast<PiBlockDDGNode>(Node)->getNodes();
123 for (auto *PN : PNodes) {
124 OS << getVerboseNodeLabel(PN, G);
125 if (++Count != PNodes.size())
126 OS << "\n";
127 }
128 OS << "--- end of nodes in pi-block ---\n";
129 } else if (isa<RootDDGNode>(Node))
130 OS << "root\n";
131 else
132 llvm_unreachable("Unimplemented type of node");
133 return OS.str();
134}
135
136/// EdgeKind to DOT color mapping:
137/// - MemoryDependence: red
138/// - RegisterDefUse: blue
139/// - Rooted/Unknown=default (no attribute).
142 OS << ", color=red";
143 else if (Kind == DDGEdge::EdgeKind::RegisterDefUse)
144 OS << ", color=blue";
145}
146
147std::string DDGDotGraphTraits::getSimpleEdgeAttributes(
148 const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
149 std::string Str;
150 raw_string_ostream OS(Str);
151 DDGEdge::EdgeKind Kind = Edge->getKind();
152 OS << "label=\"[" << Kind << "]\"";
153 writeEdgeKindColorAttr(OS, Kind);
154 return OS.str();
155}
156
157std::string DDGDotGraphTraits::getVerboseEdgeAttributes(
158 const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) {
159 std::string Str;
160 raw_string_ostream OS(Str);
161 DDGEdge::EdgeKind Kind = Edge->getKind();
162 OS << "label=\"[";
164 OS << G->getDependenceString(*Src, Edge->getTargetNode());
165 else
166 OS << Kind;
167 OS << "]\"";
168 writeEdgeKindColorAttr(OS, Kind);
169 return OS.str();
170}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void writeEdgeKindColorAttr(raw_ostream &OS, DDGEdge::EdgeKind Kind)
EdgeKind to DOT color mapping:
static void writeDDGToDotFile(const AnalysisOptions &Opts, DataDependenceGraph &G)
#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
LLVM_ABI 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
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.
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:793
This is an optimization pass for GlobalISel generic memory operations.
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
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.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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...