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/Constants.h"
15 #include "llvm/Function.h"
16 #include "llvm/Assembly/Writer.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
18 #include "llvm/CodeGen/ScheduleDAG.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/Target/TargetRegisterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/GraphWriter.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/ADT/DenseSet.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/Config/config.h"
36 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
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).getMVTString();
49 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
50 /// should actually target another edge source, not a node. If this method is
51 /// implemented, getEdgeTarget should be implemented.
52 template<typename EdgeIter>
53 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
57 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
58 /// called to determine which outgoing edge of Node is the target of this
60 template<typename EdgeIter>
61 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
62 SDNode *TargetNode = *I;
63 SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
64 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
68 static std::string getGraphName(const SelectionDAG *G) {
69 return G->getMachineFunction().getFunction()->getName();
72 static bool renderGraphFromBottomUp() {
76 static bool hasNodeAddressLabel(const SDNode *Node,
77 const SelectionDAG *Graph) {
81 /// If you want to override the dot attributes printed for a particular
82 /// edge, override this method.
83 template<typename EdgeIter>
84 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
85 SDValue Op = EI.getNode()->getOperand(EI.getOperand());
86 MVT VT = Op.getValueType();
88 return "color=red,style=bold";
89 else if (VT == MVT::Other)
90 return "color=blue,style=dashed";
95 static std::string getNodeLabel(const SDNode *Node,
96 const SelectionDAG *Graph);
97 static std::string getNodeAttributes(const SDNode *N,
98 const SelectionDAG *Graph) {
100 const std::string &Attrs = Graph->getGraphAttrs(N);
101 if (!Attrs.empty()) {
102 if (Attrs.find("shape=") == std::string::npos)
103 return std::string("shape=Mrecord,") + Attrs;
108 return "shape=Mrecord";
111 static void addCustomGraphFeatures(SelectionDAG *G,
112 GraphWriter<SelectionDAG*> &GW) {
113 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
114 if (G->getRoot().getNode())
115 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
116 "color=blue,style=dashed");
121 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
122 const SelectionDAG *G) {
123 std::string Op = Node->getOperationName(G);
125 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
126 Op += ": " + utostr(CSDN->getZExtValue());
127 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
128 Op += ": " + ftostr(CSDN->getValueAPF());
129 } else if (const GlobalAddressSDNode *GADN =
130 dyn_cast<GlobalAddressSDNode>(Node)) {
131 Op += ": " + GADN->getGlobal()->getName();
132 if (int64_t Offset = GADN->getOffset()) {
134 Op += "+" + itostr(Offset);
136 Op += itostr(Offset);
138 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
139 Op += " " + itostr(FIDN->getIndex());
140 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
141 Op += " " + itostr(JTDN->getIndex());
142 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
143 if (CP->isMachineConstantPoolEntry()) {
146 raw_string_ostream OSS(Op);
147 OSS << *CP->getMachineCPVal();
151 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
152 Op += "<" + ftostr(CFP->getValueAPF()) + ">";
153 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
154 Op += "<" + utostr(CI->getZExtValue()) + ">";
158 raw_string_ostream OSS(Op);
159 WriteAsOperand(OSS, CP->getConstVal(), false);
164 Op += " A=" + itostr(1 << CP->getAlignment());
165 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
167 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
169 Op += LBB->getName();
170 //Op += " " + (const void*)BBDN->getBasicBlock();
171 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
172 if (G && R->getReg() != 0 &&
173 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
175 G->getTarget().getRegisterInfo()->getName(R->getReg());
177 Op += " #" + utostr(R->getReg());
179 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) {
180 Op += ": " + D->getCompileUnit()->getFileName();
181 Op += ":" + utostr(D->getLine());
182 if (D->getColumn() != 0)
183 Op += ":" + utostr(D->getColumn());
184 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) {
185 Op += ": LabelID=" + utostr(L->getLabelID());
186 } else if (const CallSDNode *C = dyn_cast<CallSDNode>(Node)) {
187 Op += ": CallingConv=" + utostr(C->getCallingConv());
191 Op += ", isTailCall";
192 } else if (const ExternalSymbolSDNode *ES =
193 dyn_cast<ExternalSymbolSDNode>(Node)) {
194 Op += "'" + std::string(ES->getSymbol()) + "'";
195 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
197 Op += "<" + M->getValue()->getName() + ">";
200 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
201 const Value *V = M->MO.getValue();
205 } else if (isa<PseudoSourceValue>(V)) {
206 // PseudoSourceValues don't have names, so use their print method.
208 raw_string_ostream OSS(Op);
209 OSS << *M->MO.getValue();
214 Op += '+' + itostr(M->MO.getOffset()) + '>';
215 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
216 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
217 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
218 Op = Op + " VT=" + N->getVT().getMVTString();
219 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
221 switch (LD->getExtensionType()) {
222 default: doExt = false; break;
224 Op = Op + "<anyext ";
234 Op += LD->getMemoryVT().getMVTString() + ">";
235 if (LD->isVolatile())
237 Op += LD->getIndexedModeName(LD->getAddressingMode());
238 if (LD->getAlignment() > 1)
239 Op += " A=" + utostr(LD->getAlignment());
240 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
241 if (ST->isTruncatingStore())
242 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
243 if (ST->isVolatile())
245 Op += ST->getIndexedModeName(ST->getAddressingMode());
246 if (ST->getAlignment() > 1)
247 Op += " A=" + utostr(ST->getAlignment());
251 Op += " Id=" + itostr(Node->getNodeId());
258 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
259 /// rendered using 'dot'.
261 void SelectionDAG::viewGraph(const std::string &Title) {
262 // This code is only for debugging!
264 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
267 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
268 << "systems with Graphviz or gv!\n";
272 // This overload is defined out-of-line here instead of just using a
273 // default parameter because this is easiest for gdb to call.
274 void SelectionDAG::viewGraph() {
278 /// clearGraphAttrs - Clear all previously defined node graph attributes.
279 /// Intended to be used from a debugging tool (eg. gdb).
280 void SelectionDAG::clearGraphAttrs() {
282 NodeGraphAttrs.clear();
284 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
285 << " on systems with Graphviz or gv!\n";
290 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
292 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
294 NodeGraphAttrs[N] = Attrs;
296 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
297 << " on systems with Graphviz or gv!\n";
302 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
303 /// Used from getNodeAttributes.
304 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
306 std::map<const SDNode *, std::string>::const_iterator I =
307 NodeGraphAttrs.find(N);
309 if (I != NodeGraphAttrs.end())
314 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
315 << " on systems with Graphviz or gv!\n";
316 return std::string("");
320 /// setGraphColor - Convenience for setting node color attribute.
322 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
324 NodeGraphAttrs[N] = std::string("color=") + Color;
326 cerr << "SelectionDAG::setGraphColor is only available in debug builds"
327 << " on systems with Graphviz or gv!\n";
331 /// setSubgraphColorHelper - Implement setSubgraphColor. Return
332 /// whether we truncated the search.
334 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
335 int level, bool &printed) {
336 bool hit_limit = false;
342 DOUT << "setSubgraphColor hit max level\n";
347 unsigned oldSize = visited.size();
349 if (visited.size() != oldSize) {
350 setGraphColor(N, Color);
351 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
354 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
358 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
359 << " on systems with Graphviz or gv!\n";
364 /// setSubgraphColor - Convenience for setting subgraph color attribute.
366 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
368 DenseSet<SDNode *> visited;
369 bool printed = false;
370 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
371 // Visually mark that we hit the limit
372 if (Color == "red" ) {
373 setSubgraphColorHelper(N, "blue", visited, 0, printed);
375 else if (Color == "yellow" ) {
376 setSubgraphColorHelper(N, "green", visited, 0, printed);
381 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds"
382 << " on systems with Graphviz or gv!\n";
388 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
389 static std::string getGraphName(const ScheduleDAG *G) {
390 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
393 static bool renderGraphFromBottomUp() {
397 static bool hasNodeAddressLabel(const SUnit *Node,
398 const ScheduleDAG *Graph) {
402 /// If you want to override the dot attributes printed for a particular
403 /// edge, override this method.
404 template<typename EdgeIter>
405 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
406 if (EI.isSpecialDep())
407 return "color=cyan,style=dashed";
409 return "color=blue,style=dashed";
414 static std::string getNodeLabel(const SUnit *Node,
415 const ScheduleDAG *Graph);
416 static std::string getNodeAttributes(const SUnit *N,
417 const ScheduleDAG *Graph) {
418 return "shape=Mrecord";
421 static void addCustomGraphFeatures(ScheduleDAG *G,
422 GraphWriter<ScheduleDAG*> &GW) {
423 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
424 const SDNode *N = G->DAG.getRoot().getNode();
425 if (N && N->getNodeId() != -1)
426 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1,
427 "color=blue,style=dashed");
432 std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
433 const ScheduleDAG *G) {
436 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
437 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
442 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
444 Op += "<CROSS RC COPY>";
450 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
451 /// rendered using 'dot'.
453 void ScheduleDAG::viewGraph() {
454 // This code is only for debugging!
456 ViewGraph(this, "dag." + MF->getFunction()->getName(),
457 "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' +
458 BB->getBasicBlock()->getName());
460 cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
461 << "systems with Graphviz or gv!\n";