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/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/PseudoSourceValue.h"
23 #include "llvm/Analysis/DebugInfo.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/GraphWriter.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/ADT/DenseSet.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Config/config.h"
36 struct DOTGraphTraits
<SelectionDAG
*> : public DefaultDOTGraphTraits
{
38 explicit DOTGraphTraits(bool isSimple
=false) :
39 DefaultDOTGraphTraits(isSimple
) {}
41 static bool hasEdgeDestLabels() {
45 static unsigned numEdgeDestLabels(const void *Node
) {
46 return ((const SDNode
*) Node
)->getNumValues();
49 static std::string
getEdgeDestLabel(const void *Node
, unsigned i
) {
50 return ((const SDNode
*) Node
)->getValueType(i
).getEVTString();
53 template<typename EdgeIter
>
54 static std::string
getEdgeSourceLabel(const void *Node
, EdgeIter I
) {
55 return itostr(I
- SDNodeIterator::begin((SDNode
*) Node
));
58 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
59 /// should actually target another edge source, not a node. If this method
60 /// is implemented, getEdgeTarget should be implemented.
61 template<typename EdgeIter
>
62 static bool edgeTargetsEdgeSource(const void *Node
, EdgeIter I
) {
66 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
67 /// called to determine which outgoing edge of Node is the target of this
69 template<typename EdgeIter
>
70 static EdgeIter
getEdgeTarget(const void *Node
, EdgeIter I
) {
71 SDNode
*TargetNode
= *I
;
72 SDNodeIterator NI
= SDNodeIterator::begin(TargetNode
);
73 std::advance(NI
, I
.getNode()->getOperand(I
.getOperand()).getResNo());
77 static std::string
getGraphName(const SelectionDAG
*G
) {
78 return G
->getMachineFunction().getFunction()->getName();
81 static bool renderGraphFromBottomUp() {
85 static bool hasNodeAddressLabel(const SDNode
*Node
,
86 const SelectionDAG
*Graph
) {
90 /// If you want to override the dot attributes printed for a particular
91 /// edge, override this method.
92 template<typename EdgeIter
>
93 static std::string
getEdgeAttributes(const void *Node
, EdgeIter EI
) {
94 SDValue Op
= EI
.getNode()->getOperand(EI
.getOperand());
95 EVT VT
= Op
.getValueType();
97 return "color=red,style=bold";
98 else if (VT
== MVT::Other
)
99 return "color=blue,style=dashed";
104 static std::string
getSimpleNodeLabel(const SDNode
*Node
,
105 const SelectionDAG
*G
) {
106 std::string Result
= Node
->getOperationName(G
);
108 raw_string_ostream
OS(Result
);
109 Node
->print_details(OS
, G
);
113 std::string
getNodeLabel(const SDNode
*Node
, const SelectionDAG
*Graph
);
114 static std::string
getNodeAttributes(const SDNode
*N
,
115 const SelectionDAG
*Graph
) {
117 const std::string
&Attrs
= Graph
->getGraphAttrs(N
);
118 if (!Attrs
.empty()) {
119 if (Attrs
.find("shape=") == std::string::npos
)
120 return std::string("shape=Mrecord,") + Attrs
;
125 return "shape=Mrecord";
128 static void addCustomGraphFeatures(SelectionDAG
*G
,
129 GraphWriter
<SelectionDAG
*> &GW
) {
130 GW
.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
131 if (G
->getRoot().getNode())
132 GW
.emitEdge(0, -1, G
->getRoot().getNode(), G
->getRoot().getResNo(),
133 "color=blue,style=dashed");
138 std::string DOTGraphTraits
<SelectionDAG
*>::getNodeLabel(const SDNode
*Node
,
139 const SelectionDAG
*G
) {
140 return DOTGraphTraits
<SelectionDAG
*>::getSimpleNodeLabel(Node
, G
);
144 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
145 /// rendered using 'dot'.
147 void SelectionDAG::viewGraph(const std::string
&Title
) {
148 // This code is only for debugging!
150 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(),
153 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
154 << "systems with Graphviz or gv!\n";
158 // This overload is defined out-of-line here instead of just using a
159 // default parameter because this is easiest for gdb to call.
160 void SelectionDAG::viewGraph() {
164 /// clearGraphAttrs - Clear all previously defined node graph attributes.
165 /// Intended to be used from a debugging tool (eg. gdb).
166 void SelectionDAG::clearGraphAttrs() {
168 NodeGraphAttrs
.clear();
170 errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
171 << " on systems with Graphviz or gv!\n";
176 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
178 void SelectionDAG::setGraphAttrs(const SDNode
*N
, const char *Attrs
) {
180 NodeGraphAttrs
[N
] = Attrs
;
182 errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
183 << " on systems with Graphviz or gv!\n";
188 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
189 /// Used from getNodeAttributes.
190 const std::string
SelectionDAG::getGraphAttrs(const SDNode
*N
) const {
192 std::map
<const SDNode
*, std::string
>::const_iterator I
=
193 NodeGraphAttrs
.find(N
);
195 if (I
!= NodeGraphAttrs
.end())
200 errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
201 << " on systems with Graphviz or gv!\n";
202 return std::string();
206 /// setGraphColor - Convenience for setting node color attribute.
208 void SelectionDAG::setGraphColor(const SDNode
*N
, const char *Color
) {
210 NodeGraphAttrs
[N
] = std::string("color=") + Color
;
212 errs() << "SelectionDAG::setGraphColor is only available in debug builds"
213 << " on systems with Graphviz or gv!\n";
217 /// setSubgraphColorHelper - Implement setSubgraphColor. Return
218 /// whether we truncated the search.
220 bool SelectionDAG::setSubgraphColorHelper(SDNode
*N
, const char *Color
, DenseSet
<SDNode
*> &visited
,
221 int level
, bool &printed
) {
222 bool hit_limit
= false;
228 DEBUG(dbgs() << "setSubgraphColor hit max level\n");
233 unsigned oldSize
= visited
.size();
235 if (visited
.size() != oldSize
) {
236 setGraphColor(N
, Color
);
237 for(SDNodeIterator i
= SDNodeIterator::begin(N
), iend
= SDNodeIterator::end(N
);
240 hit_limit
= setSubgraphColorHelper(*i
, Color
, visited
, level
+1, printed
) || hit_limit
;
244 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
245 << " on systems with Graphviz or gv!\n";
250 /// setSubgraphColor - Convenience for setting subgraph color attribute.
252 void SelectionDAG::setSubgraphColor(SDNode
*N
, const char *Color
) {
254 DenseSet
<SDNode
*> visited
;
255 bool printed
= false;
256 if (setSubgraphColorHelper(N
, Color
, visited
, 0, printed
)) {
257 // Visually mark that we hit the limit
258 if (strcmp(Color
, "red") == 0) {
259 setSubgraphColorHelper(N
, "blue", visited
, 0, printed
);
260 } else if (strcmp(Color
, "yellow") == 0) {
261 setSubgraphColorHelper(N
, "green", visited
, 0, printed
);
266 errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
267 << " on systems with Graphviz or gv!\n";
271 std::string
ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit
*SU
) const {
273 raw_string_ostream
O(s
);
274 O
<< "SU(" << SU
->NodeNum
<< "): ";
276 SmallVector
<SDNode
*, 4> FlaggedNodes
;
277 for (SDNode
*N
= SU
->getNode(); N
; N
= N
->getFlaggedNode())
278 FlaggedNodes
.push_back(N
);
279 while (!FlaggedNodes
.empty()) {
280 O
<< DOTGraphTraits
<SelectionDAG
*>
281 ::getSimpleNodeLabel(FlaggedNodes
.back(), DAG
);
282 FlaggedNodes
.pop_back();
283 if (!FlaggedNodes
.empty())
287 O
<< "CROSS RC COPY";
292 void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter
<ScheduleDAG
*> &GW
) const {
294 // Draw a special "GraphRoot" node to indicate the root of the graph.
295 GW
.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
296 const SDNode
*N
= DAG
->getRoot().getNode();
297 if (N
&& N
->getNodeId() != -1)
298 GW
.emitEdge(0, -1, &SUnits
[N
->getNodeId()], -1,
299 "color=blue,style=dashed");