1 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the SelectionDAG::viewGraph method.
12 //===----------------------------------------------------------------------===//
14 #include "ScheduleDAGSDNodes.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/GraphWriter.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetMachine.h"
28 #define DEBUG_TYPE "dag-printer"
32 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
34 explicit DOTGraphTraits(bool isSimple=false) :
35 DefaultDOTGraphTraits(isSimple) {}
37 static bool hasEdgeDestLabels() {
41 static unsigned numEdgeDestLabels(const void *Node) {
42 return ((const SDNode *) Node)->getNumValues();
45 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
46 return ((const SDNode *) Node)->getValueType(i).getEVTString();
49 template<typename EdgeIter>
50 static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
51 return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
54 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
55 /// should actually target another edge source, not a node. If this method
56 /// is implemented, getEdgeTarget should be implemented.
57 template<typename EdgeIter>
58 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
62 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
63 /// called to determine which outgoing edge of Node is the target of this
65 template<typename EdgeIter>
66 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
67 SDNode *TargetNode = *I;
68 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
69 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
73 static std::string getGraphName(const SelectionDAG *G) {
74 return G->getMachineFunction().getName();
77 static bool renderGraphFromBottomUp() {
81 static std::string getNodeIdentifierLabel(const SDNode *Node,
82 const SelectionDAG *Graph) {
84 raw_string_ostream OS(R);
86 OS << 't' << Node->PersistentId;
88 OS << static_cast<const void *>(Node);
93 /// If you want to override the dot attributes printed for a particular
94 /// edge, override this method.
95 template<typename EdgeIter>
96 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
97 const SelectionDAG *Graph) {
98 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
99 EVT VT = Op.getValueType();
101 return "color=red,style=bold";
102 else if (VT == MVT::Other)
103 return "color=blue,style=dashed";
108 static std::string getSimpleNodeLabel(const SDNode *Node,
109 const SelectionDAG *G) {
110 std::string Result = Node->getOperationName(G);
112 raw_string_ostream OS(Result);
113 Node->print_details(OS, G);
117 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
118 static std::string getNodeAttributes(const SDNode *N,
119 const SelectionDAG *Graph) {
121 const std::string &Attrs = Graph->getGraphAttrs(N);
122 if (!Attrs.empty()) {
123 if (Attrs.find("shape=") == std::string::npos)
124 return std::string("shape=Mrecord,") + Attrs;
129 return "shape=Mrecord";
132 static void addCustomGraphFeatures(SelectionDAG *G,
133 GraphWriter<SelectionDAG*> &GW) {
134 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
135 if (G->getRoot().getNode())
136 GW.emitEdge(nullptr, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
137 "color=blue,style=dashed");
142 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
143 const SelectionDAG *G) {
144 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
148 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
149 /// rendered using 'dot'.
151 void SelectionDAG::viewGraph(const std::string &Title) {
152 // This code is only for debugging!
154 ViewGraph(this, "dag." + getMachineFunction().getName(),
157 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
158 << "systems with Graphviz or gv!\n";
162 // This overload is defined out-of-line here instead of just using a
163 // default parameter because this is easiest for gdb to call.
164 void SelectionDAG::viewGraph() {
168 /// clearGraphAttrs - Clear all previously defined node graph attributes.
169 /// Intended to be used from a debugging tool (eg. gdb).
170 void SelectionDAG::clearGraphAttrs() {
172 NodeGraphAttrs.clear();
174 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
175 << " on systems with Graphviz or gv!\n";
180 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
182 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
184 NodeGraphAttrs[N] = Attrs;
186 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
187 << " on systems with Graphviz or gv!\n";
192 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
193 /// Used from getNodeAttributes.
194 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
196 std::map<const SDNode *, std::string>::const_iterator I =
197 NodeGraphAttrs.find(N);
199 if (I != NodeGraphAttrs.end())
204 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
205 << " on systems with Graphviz or gv!\n";
206 return std::string();
210 /// setGraphColor - Convenience for setting node color attribute.
212 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
214 NodeGraphAttrs[N] = std::string("color=") + Color;
216 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
217 << " on systems with Graphviz or gv!\n";
221 /// setSubgraphColorHelper - Implement setSubgraphColor. Return
222 /// whether we truncated the search.
224 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
225 int level, bool &printed) {
226 bool hit_limit = false;
232 LLVM_DEBUG(dbgs() << "setSubgraphColor hit max level\n");
237 unsigned oldSize = visited.size();
239 if (visited.size() != oldSize) {
240 setGraphColor(N, Color);
241 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
244 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
248 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
249 << " on systems with Graphviz or gv!\n";
254 /// setSubgraphColor - Convenience for setting subgraph color attribute.
256 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
258 DenseSet<SDNode *> visited;
259 bool printed = false;
260 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
261 // Visually mark that we hit the limit
262 if (strcmp(Color, "red") == 0) {
263 setSubgraphColorHelper(N, "blue", visited, 0, printed);
264 } else if (strcmp(Color, "yellow") == 0) {
265 setSubgraphColorHelper(N, "green", visited, 0, printed);
270 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
271 << " on systems with Graphviz or gv!\n";
275 std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
277 raw_string_ostream O(s);
278 O << "SU(" << SU->NodeNum << "): ";
280 SmallVector<SDNode *, 4> GluedNodes;
281 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
282 GluedNodes.push_back(N);
283 while (!GluedNodes.empty()) {
284 O << DOTGraphTraits<SelectionDAG*>
285 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
286 GluedNodes.pop_back();
287 if (!GluedNodes.empty())
291 O << "CROSS RC COPY";
296 void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
298 // Draw a special "GraphRoot" node to indicate the root of the graph.
299 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
300 const SDNode *N = DAG->getRoot().getNode();
301 if (N && N->getNodeId() != -1)
302 GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
303 "color=blue,style=dashed");