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 "llvm/CodeGen/SelectionDAG.h"
15 #include "ScheduleDAGSDNodes.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/CodeGen/MachineConstantPool.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/GraphWriter.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
30 #define DEBUG_TYPE "dag-printer"
34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
36 explicit DOTGraphTraits(bool isSimple=false) :
37 DefaultDOTGraphTraits(isSimple) {}
39 static bool hasEdgeDestLabels() {
43 static unsigned numEdgeDestLabels(const void *Node) {
44 return ((const SDNode *) Node)->getNumValues();
47 static std::string getEdgeDestLabel(const void *Node, unsigned i) {
48 return ((const SDNode *) Node)->getValueType(i).getEVTString();
51 template<typename EdgeIter>
52 static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
53 return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
56 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
57 /// should actually target another edge source, not a node. If this method
58 /// is implemented, getEdgeTarget should be implemented.
59 template<typename EdgeIter>
60 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
64 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
65 /// called to determine which outgoing edge of Node is the target of this
67 template<typename EdgeIter>
68 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
69 SDNode *TargetNode = *I;
70 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
71 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
75 static std::string getGraphName(const SelectionDAG *G) {
76 return G->getMachineFunction().getName();
79 static bool renderGraphFromBottomUp() {
83 static std::string getNodeIdentifierLabel(const SDNode *Node,
84 const SelectionDAG *Graph) {
86 raw_string_ostream OS(R);
88 OS << 't' << Node->PersistentId;
90 OS << static_cast<const void *>(Node);
95 /// If you want to override the dot attributes printed for a particular
96 /// edge, override this method.
97 template<typename EdgeIter>
98 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
99 const SelectionDAG *Graph) {
100 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
101 EVT VT = Op.getValueType();
103 return "color=red,style=bold";
104 else if (VT == MVT::Other)
105 return "color=blue,style=dashed";
110 static std::string getSimpleNodeLabel(const SDNode *Node,
111 const SelectionDAG *G) {
112 std::string Result = Node->getOperationName(G);
114 raw_string_ostream OS(Result);
115 Node->print_details(OS, G);
119 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
120 static std::string getNodeAttributes(const SDNode *N,
121 const SelectionDAG *Graph) {
123 const std::string &Attrs = Graph->getGraphAttrs(N);
124 if (!Attrs.empty()) {
125 if (Attrs.find("shape=") == std::string::npos)
126 return std::string("shape=Mrecord,") + Attrs;
131 return "shape=Mrecord";
134 static void addCustomGraphFeatures(SelectionDAG *G,
135 GraphWriter<SelectionDAG*> &GW) {
136 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
137 if (G->getRoot().getNode())
138 GW.emitEdge(nullptr, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
139 "color=blue,style=dashed");
144 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
145 const SelectionDAG *G) {
146 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
150 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
151 /// rendered using 'dot'.
153 void SelectionDAG::viewGraph(const std::string &Title) {
154 // This code is only for debugging!
156 ViewGraph(this, "dag." + getMachineFunction().getName(),
159 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
160 << "systems with Graphviz or gv!\n";
164 // This overload is defined out-of-line here instead of just using a
165 // default parameter because this is easiest for gdb to call.
166 void SelectionDAG::viewGraph() {
170 /// clearGraphAttrs - Clear all previously defined node graph attributes.
171 /// Intended to be used from a debugging tool (eg. gdb).
172 void SelectionDAG::clearGraphAttrs() {
174 NodeGraphAttrs.clear();
176 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
177 << " on systems with Graphviz or gv!\n";
182 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
184 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
186 NodeGraphAttrs[N] = Attrs;
188 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
189 << " on systems with Graphviz or gv!\n";
194 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
195 /// Used from getNodeAttributes.
196 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
198 std::map<const SDNode *, std::string>::const_iterator I =
199 NodeGraphAttrs.find(N);
201 if (I != NodeGraphAttrs.end())
206 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
207 << " on systems with Graphviz or gv!\n";
208 return std::string();
212 /// setGraphColor - Convenience for setting node color attribute.
214 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
216 NodeGraphAttrs[N] = std::string("color=") + Color;
218 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
219 << " on systems with Graphviz or gv!\n";
223 /// setSubgraphColorHelper - Implement setSubgraphColor. Return
224 /// whether we truncated the search.
226 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
227 int level, bool &printed) {
228 bool hit_limit = false;
234 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
239 unsigned oldSize = visited.size();
241 if (visited.size() != oldSize) {
242 setGraphColor(N, Color);
243 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
246 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
250 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
251 << " on systems with Graphviz or gv!\n";
256 /// setSubgraphColor - Convenience for setting subgraph color attribute.
258 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
260 DenseSet<SDNode *> visited;
261 bool printed = false;
262 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
263 // Visually mark that we hit the limit
264 if (strcmp(Color, "red") == 0) {
265 setSubgraphColorHelper(N, "blue", visited, 0, printed);
266 } else if (strcmp(Color, "yellow") == 0) {
267 setSubgraphColorHelper(N, "green", visited, 0, printed);
272 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
273 << " on systems with Graphviz or gv!\n";
277 std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
279 raw_string_ostream O(s);
280 O << "SU(" << SU->NodeNum << "): ";
282 SmallVector<SDNode *, 4> GluedNodes;
283 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
284 GluedNodes.push_back(N);
285 while (!GluedNodes.empty()) {
286 O << DOTGraphTraits<SelectionDAG*>
287 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
288 GluedNodes.pop_back();
289 if (!GluedNodes.empty())
293 O << "CROSS RC COPY";
298 void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
300 // Draw a special "GraphRoot" node to indicate the root of the graph.
301 GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
302 const SDNode *N = DAG->getRoot().getNode();
303 if (N && N->getNodeId() != -1)
304 GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
305 "color=blue,style=dashed");