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"
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 bool hasNodeAddressLabel(const SDNode *Node,
82 const SelectionDAG *Graph) {
86 /// If you want to override the dot attributes printed for a particular
87 /// edge, override this method.
88 template<typename EdgeIter>
89 static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
90 const SelectionDAG *Graph) {
91 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
92 EVT VT = Op.getValueType();
94 return "color=red,style=bold";
95 else if (VT == MVT::Other)
96 return "color=blue,style=dashed";
101 static std::string getSimpleNodeLabel(const SDNode *Node,
102 const SelectionDAG *G) {
103 std::string Result = Node->getOperationName(G);
105 raw_string_ostream OS(Result);
106 Node->print_details(OS, G);
110 std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
111 static std::string getNodeAttributes(const SDNode *N,
112 const SelectionDAG *Graph) {
114 const std::string &Attrs = Graph->getGraphAttrs(N);
115 if (!Attrs.empty()) {
116 if (Attrs.find("shape=") == std::string::npos)
117 return std::string("shape=Mrecord,") + Attrs;
122 return "shape=Mrecord";
125 static void addCustomGraphFeatures(SelectionDAG *G,
126 GraphWriter<SelectionDAG*> &GW) {
127 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
128 if (G->getRoot().getNode())
129 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
130 "color=blue,style=dashed");
135 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
136 const SelectionDAG *G) {
137 return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
141 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
142 /// rendered using 'dot'.
144 void SelectionDAG::viewGraph(const std::string &Title) {
145 // This code is only for debugging!
147 ViewGraph(this, "dag." + getMachineFunction().getName(),
150 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
151 << "systems with Graphviz or gv!\n";
155 // This overload is defined out-of-line here instead of just using a
156 // default parameter because this is easiest for gdb to call.
157 void SelectionDAG::viewGraph() {
161 /// clearGraphAttrs - Clear all previously defined node graph attributes.
162 /// Intended to be used from a debugging tool (eg. gdb).
163 void SelectionDAG::clearGraphAttrs() {
165 NodeGraphAttrs.clear();
167 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
168 << " on systems with Graphviz or gv!\n";
173 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
175 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
177 NodeGraphAttrs[N] = Attrs;
179 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
180 << " on systems with Graphviz or gv!\n";
185 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
186 /// Used from getNodeAttributes.
187 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
189 std::map<const SDNode *, std::string>::const_iterator I =
190 NodeGraphAttrs.find(N);
192 if (I != NodeGraphAttrs.end())
197 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
198 << " on systems with Graphviz or gv!\n";
199 return std::string();
203 /// setGraphColor - Convenience for setting node color attribute.
205 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
207 NodeGraphAttrs[N] = std::string("color=") + Color;
209 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
210 << " on systems with Graphviz or gv!\n";
214 /// setSubgraphColorHelper - Implement setSubgraphColor. Return
215 /// whether we truncated the search.
217 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
218 int level, bool &printed) {
219 bool hit_limit = false;
225 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
230 unsigned oldSize = visited.size();
232 if (visited.size() != oldSize) {
233 setGraphColor(N, Color);
234 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
237 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
241 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
242 << " on systems with Graphviz or gv!\n";
247 /// setSubgraphColor - Convenience for setting subgraph color attribute.
249 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
251 DenseSet<SDNode *> visited;
252 bool printed = false;
253 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
254 // Visually mark that we hit the limit
255 if (strcmp(Color, "red") == 0) {
256 setSubgraphColorHelper(N, "blue", visited, 0, printed);
257 } else if (strcmp(Color, "yellow") == 0) {
258 setSubgraphColorHelper(N, "green", visited, 0, printed);
263 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
264 << " on systems with Graphviz or gv!\n";
268 std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
270 raw_string_ostream O(s);
271 O << "SU(" << SU->NodeNum << "): ";
273 SmallVector<SDNode *, 4> GluedNodes;
274 for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
275 GluedNodes.push_back(N);
276 while (!GluedNodes.empty()) {
277 O << DOTGraphTraits<SelectionDAG*>
278 ::getSimpleNodeLabel(GluedNodes.back(), DAG);
279 GluedNodes.pop_back();
280 if (!GluedNodes.empty())
284 O << "CROSS RC COPY";
289 void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
291 // Draw a special "GraphRoot" node to indicate the root of the graph.
292 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
293 const SDNode *N = DAG->getRoot().getNode();
294 if (N && N->getNodeId() != -1)
295 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1,
296 "color=blue,style=dashed");