1//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
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// This implements the SelectionDAG::viewGraph method.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ScheduleDAGSDNodes.h"
14#include "llvm/ADT/DenseSet.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/SelectionDAG.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/GraphWriter.h"
20#include "llvm/Support/raw_ostream.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "dag-printer"
24
25namespace llvm {
26 template<>
27 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
28
29 explicit DOTGraphTraits(bool isSimple=false) :
30 DefaultDOTGraphTraits(isSimple) {}
31
32 static bool hasEdgeDestLabels() {
33 return true;
34 }
35
36 static unsigned numEdgeDestLabels(const void *Node) {
37 return ((const SDNode *) Node)->getNumValues();
38 }
39
40 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
41 return ((const SDNode *) Node)->getValueType(ResNo: i).getEVTString();
42 }
43
44 template<typename EdgeIter>
45 static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
46 return itostr(I - SDNodeIterator::begin(N: (const SDNode *) Node));
47 }
48
49 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
50 /// should actually target another edge source, not a node. If this method
51 /// is implemented, getEdgeTarget should be implemented.
52 template<typename EdgeIter>
53 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
54 return true;
55 }
56
57 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
58 /// called to determine which outgoing edge of Node is the target of this
59 /// edge.
60 template<typename EdgeIter>
61 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
62 SDNode *TargetNode = *I;
63 SDNodeIterator NI = SDNodeIterator::begin(N: TargetNode);
64 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
65 return NI;
66 }
67
68 static std::string getGraphName(const SelectionDAG *G) {
69 return std::string(G->getMachineFunction().getName());
70 }
71
72 static bool renderGraphFromBottomUp() {
73 return true;
74 }
75
76 static std::string getNodeIdentifierLabel(const SDNode *Node,
77 const SelectionDAG *Graph) {
78 std::string R;
79 raw_string_ostream OS(R);
80#ifndef NDEBUG
81 OS << 't' << Node->PersistentId;
82#else
83 OS << static_cast<const void *>(Node);
84#endif
85 return R;
86 }
87
88 /// If you want to override the dot attributes printed for a particular
89 /// edge, override this method.
90 template<typename EdgeIter>
91 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
92 const SelectionDAG *Graph) {
93 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
94 EVT VT = Op.getValueType();
95 if (VT == MVT::Glue)
96 return "color=red,style=bold";
97 else if (VT == MVT::Other)
98 return "color=blue,style=dashed";
99 return "";
100 }
101
102 /// Return the opcode from its SelectionDAG or machine-opcode domain.
103 static unsigned getRawNodeOpcode(const SDNode *Node) {
104 if (Node->isMachineOpcode())
105 return Node->getMachineOpcode();
106 return Node->getOpcode();
107 }
108
109 static std::string getSimpleNodeLabel(const SDNode *Node,
110 const SelectionDAG *G) {
111 std::string Result = Node->getOperationName(G);
112 {
113 raw_string_ostream OS(Result);
114 Node->print_details(OS, G);
115 }
116 return Result;
117 }
118
119 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
120 static std::string getNodeAttributes(const SDNode *N,
121 const SelectionDAG *Graph) {
122 const std::string OpcodeAttrs =
123 std::string("llvm_opcode=\"") + utostr(X: getRawNodeOpcode(Node: N)) +
124 "\",llvm_is_machine_opcode=\"" +
125 (N->isMachineOpcode() ? "true\"" : "false\"");
126
127#ifndef NDEBUG
128 const std::string &Attrs = Graph->getGraphAttrs(N);
129 if (!Attrs.empty()) {
130 if (Attrs.find("shape=") == std::string::npos)
131 return std::string("shape=Mrecord,") + OpcodeAttrs + "," + Attrs;
132 else
133 return Attrs + "," + OpcodeAttrs;
134 }
135#endif
136 return std::string("shape=Mrecord,") + OpcodeAttrs;
137 }
138
139 static void addCustomGraphFeatures(SelectionDAG *G,
140 GraphWriter<SelectionDAG *> &GW) {
141 GW.emitSimpleNode(ID: nullptr, Attr: "plaintext=circle", Label: "GraphRoot");
142 if (G->getRoot().getNode())
143 GW.emitEdge(SrcNodeID: nullptr, SrcNodePort: -1, DestNodeID: G->getRoot().getNode(),
144 DestNodePort: G->getRoot().getResNo(), Attrs: "color=blue,style=dashed");
145 }
146 };
147}
148
149std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
150 const SelectionDAG *G) {
151 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
152}
153
154
155/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
156/// rendered using 'dot'.
157///
158void SelectionDAG::viewGraph(const std::string &Title) {
159// This code is only for debugging!
160#ifndef NDEBUG
161 ViewGraph(this, "dag." + getMachineFunction().getName(),
162 false, Title);
163#else
164 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
165 << "systems with Graphviz or gv!\n";
166#endif // NDEBUG
167}
168
169// This overload is defined out-of-line here instead of just using a
170// default parameter because this is easiest for gdb to call.
171void SelectionDAG::viewGraph() {
172 viewGraph(Title: "");
173}
174
175/// Just dump dot graph to a user-provided path and title.
176/// This doesn't open the dot viewer program and
177/// helps visualization when outside debugging session.
178/// FileName expects absolute path. If provided
179/// without any path separators then the file
180/// will be created in the current directory.
181/// Error will be emitted if the path is insane.
182#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
183LLVM_DUMP_METHOD void SelectionDAG::dumpDotGraph(const Twine &FileName,
184 const Twine &Title) {
185 dumpDotGraphToFile(this, FileName, Title);
186}
187#endif
188
189/// clearGraphAttrs - Clear all previously defined node graph attributes.
190/// Intended to be used from a debugging tool (eg. gdb).
191void SelectionDAG::clearGraphAttrs() {
192#if LLVM_ENABLE_ABI_BREAKING_CHECKS
193 NodeGraphAttrs.clear();
194#else
195 errs() << "SelectionDAG::clearGraphAttrs is only available in builds with "
196 << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
197#endif
198}
199
200
201/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
202///
203void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
204#if LLVM_ENABLE_ABI_BREAKING_CHECKS
205 NodeGraphAttrs[N] = Attrs;
206#else
207 errs() << "SelectionDAG::setGraphAttrs is only available in builds with "
208 << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
209#endif
210}
211
212
213/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
214/// Used from getNodeAttributes.
215std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
216#if LLVM_ENABLE_ABI_BREAKING_CHECKS
217 std::map<const SDNode *, std::string>::const_iterator I =
218 NodeGraphAttrs.find(N);
219
220 if (I != NodeGraphAttrs.end())
221 return I->second;
222 else
223 return "";
224#else
225 errs() << "SelectionDAG::getGraphAttrs is only available in builds with "
226 << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
227 return std::string();
228#endif
229}
230
231/// setGraphColor - Convenience for setting node color attribute.
232///
233void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
234#if LLVM_ENABLE_ABI_BREAKING_CHECKS
235 NodeGraphAttrs[N] = std::string("color=") + Color;
236#else
237 errs() << "SelectionDAG::setGraphColor is only available in builds with "
238 << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
239#endif
240}
241
242/// setSubgraphColorHelper - Implement setSubgraphColor. Return
243/// whether we truncated the search.
244///
245bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
246 int level, bool &printed) {
247 bool hit_limit = false;
248
249#ifndef NDEBUG
250 if (level >= 20) {
251 if (!printed) {
252 printed = true;
253 LLVM_DEBUG(dbgs() << "setSubgraphColor hit max level\n");
254 }
255 return true;
256 }
257
258 unsigned oldSize = visited.size();
259 visited.insert(N);
260 if (visited.size() != oldSize) {
261 setGraphColor(N, Color);
262 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
263 i != iend;
264 ++i) {
265 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
266 }
267 }
268#else
269 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
270 << " on systems with Graphviz or gv!\n";
271#endif
272 return hit_limit;
273}
274
275/// setSubgraphColor - Convenience for setting subgraph color attribute.
276///
277void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
278#ifndef NDEBUG
279 DenseSet<SDNode *> visited;
280 bool printed = false;
281 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
282 // Visually mark that we hit the limit
283 if (strcmp(Color, "red") == 0) {
284 setSubgraphColorHelper(N, "blue", visited, 0, printed);
285 } else if (strcmp(Color, "yellow") == 0) {
286 setSubgraphColorHelper(N, "green", visited, 0, printed);
287 }
288 }
289
290#else
291 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
292 << " on systems with Graphviz or gv!\n";
293#endif
294}
295
296#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
297std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
298 std::string s;
299 raw_string_ostream O(s);
300 O << *SU << ": ";
301 if (SU->getNode()) {
302 SmallVector<SDNode *, 4> GluedNodes;
303 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
304 GluedNodes.push_back(N);
305 while (!GluedNodes.empty()) {
306 O << DOTGraphTraits<SelectionDAG*>
307 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
308 GluedNodes.pop_back();
309 if (!GluedNodes.empty())
310 O << "\n ";
311 }
312 } else {
313 O << "CROSS RC COPY";
314 }
315 return s;
316}
317#endif
318
319void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
320 if (DAG) {
321 // Draw a special "GraphRoot" node to indicate the root of the graph.
322 GW.emitSimpleNode(ID: nullptr, Attr: "plaintext=circle", Label: "GraphRoot");
323 const SDNode *N = DAG->getRoot().getNode();
324 if (N && N->getNodeId() != -1)
325 GW.emitEdge(SrcNodeID: nullptr, SrcNodePort: -1, DestNodeID: &SUnits[N->getNodeId()], DestNodePort: -1,
326 Attrs: "color=blue,style=dashed");
327 }
328}
329