SelectionDAGPrinter.cpp revision ab16291b9f0afc54993f31c5ba171c9dd7cb4049
1//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG::viewGraph method.
11//
12//===----------------------------------------------------------------------===//
13
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/Target/TargetRegisterInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Support/GraphWriter.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/Config/config.h"
26#include <fstream>
27#include <sstream>
28using namespace llvm;
29
30namespace llvm {
31  template<>
32  struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
33    static std::string getGraphName(const SelectionDAG *G) {
34      return G->getMachineFunction().getFunction()->getName();
35    }
36
37    static bool renderGraphFromBottomUp() {
38      return true;
39    }
40
41    static bool hasNodeAddressLabel(const SDNode *Node,
42                                    const SelectionDAG *Graph) {
43      return true;
44    }
45
46    /// If you want to override the dot attributes printed for a particular
47    /// edge, override this method.
48    template<typename EdgeIter>
49    static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
50      SDOperand Op = EI.getNode()->getOperand(EI.getOperand());
51      MVT VT = Op.getValueType();
52      if (VT == MVT::Flag)
53        return "color=red,style=bold";
54      else if (VT == MVT::Other)
55        return "color=blue,style=dashed";
56      return "";
57    }
58
59
60    static std::string getNodeLabel(const SDNode *Node,
61                                    const SelectionDAG *Graph);
62    static std::string getNodeAttributes(const SDNode *N,
63                                         const SelectionDAG *Graph) {
64#ifndef NDEBUG
65      const std::string &Attrs = Graph->getGraphAttrs(N);
66      if (!Attrs.empty()) {
67        if (Attrs.find("shape=") == std::string::npos)
68          return std::string("shape=Mrecord,") + Attrs;
69        else
70          return Attrs;
71      }
72#endif
73      return "shape=Mrecord";
74    }
75
76    static void addCustomGraphFeatures(SelectionDAG *G,
77                                       GraphWriter<SelectionDAG*> &GW) {
78      GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
79      if (G->getRoot().Val)
80        GW.emitEdge(0, -1, G->getRoot().Val, -1, "");
81    }
82  };
83}
84
85std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
86                                                        const SelectionDAG *G) {
87  std::string Op = Node->getOperationName(G);
88
89  for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
90    if (Node->getValueType(i) == MVT::Other)
91      Op += ":ch";
92    else
93      Op = Op + ":" + Node->getValueType(i).getMVTString();
94
95  if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
96    Op += ": " + utostr(CSDN->getValue());
97  } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) {
98    Op += ": " + ftostr(CSDN->getValueAPF());
99  } else if (const GlobalAddressSDNode *GADN =
100             dyn_cast<GlobalAddressSDNode>(Node)) {
101    int offset = GADN->getOffset();
102    Op += ": " + GADN->getGlobal()->getName();
103    if (offset > 0)
104      Op += "+" + itostr(offset);
105    else
106      Op += itostr(offset);
107  } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) {
108    Op += " " + itostr(FIDN->getIndex());
109  } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) {
110    Op += " " + itostr(JTDN->getIndex());
111  } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){
112    if (CP->isMachineConstantPoolEntry()) {
113      std::ostringstream SS;
114      CP->getMachineCPVal()->print(SS);
115      Op += "<" + SS.str() + ">";
116    } else {
117      if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
118        Op += "<" + ftostr(CFP->getValueAPF()) + ">";
119      else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal()))
120        Op += "<" + utostr(CI->getZExtValue()) + ">";
121      else {
122        std::ostringstream SS;
123        WriteAsOperand(SS, CP->getConstVal(), false);
124        Op += "<" + SS.str() + ">";
125      }
126    }
127  } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) {
128    Op = "BB: ";
129    const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
130    if (LBB)
131      Op += LBB->getName();
132    //Op += " " + (const void*)BBDN->getBasicBlock();
133  } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) {
134    if (G && R->getReg() != 0 &&
135        TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
136      Op = Op + " " +
137        G->getTarget().getRegisterInfo()->getName(R->getReg());
138    } else {
139      Op += " #" + utostr(R->getReg());
140    }
141  } else if (const ExternalSymbolSDNode *ES =
142             dyn_cast<ExternalSymbolSDNode>(Node)) {
143    Op += "'" + std::string(ES->getSymbol()) + "'";
144  } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) {
145    if (M->getValue())
146      Op += "<" + M->getValue()->getName() + ">";
147    else
148      Op += "<null>";
149  } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) {
150    if (M->MO.getValue())
151      Op += "<" + M->MO.getValue()->getName() + ":" + itostr(M->MO.getOffset()) + ">";
152    else
153      Op += "<null:" + itostr(M->MO.getOffset()) + ">";
154  } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) {
155    Op = Op + " AF=" + N->getArgFlags().getArgFlagsString();
156  } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) {
157    Op = Op + " VT=" + N->getVT().getMVTString();
158  } else if (const StringSDNode *N = dyn_cast<StringSDNode>(Node)) {
159    Op = Op + "\"" + N->getValue() + "\"";
160  } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) {
161    bool doExt = true;
162    switch (LD->getExtensionType()) {
163    default: doExt = false; break;
164    case ISD::EXTLOAD:
165      Op = Op + "<anyext ";
166      break;
167    case ISD::SEXTLOAD:
168      Op = Op + " <sext ";
169      break;
170    case ISD::ZEXTLOAD:
171      Op = Op + " <zext ";
172      break;
173    }
174    if (doExt)
175      Op += LD->getMemoryVT().getMVTString() + ">";
176    if (LD->isVolatile())
177      Op += "<V>";
178    Op += LD->getIndexedModeName(LD->getAddressingMode());
179    if (LD->getAlignment() > 1)
180      Op += " A=" + utostr(LD->getAlignment());
181  } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) {
182    if (ST->isTruncatingStore())
183      Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">";
184    if (ST->isVolatile())
185      Op += "<V>";
186    Op += ST->getIndexedModeName(ST->getAddressingMode());
187    if (ST->getAlignment() > 1)
188      Op += " A=" + utostr(ST->getAlignment());
189  }
190
191#if 0
192  Op += " Id=" + itostr(Node->getNodeId());
193#endif
194
195  return Op;
196}
197
198
199/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
200/// rendered using 'dot'.
201///
202void SelectionDAG::viewGraph() {
203// This code is only for debugging!
204#ifndef NDEBUG
205  ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName());
206#else
207  cerr << "SelectionDAG::viewGraph is only available in debug builds on "
208       << "systems with Graphviz or gv!\n";
209#endif  // NDEBUG
210}
211
212
213/// clearGraphAttrs - Clear all previously defined node graph attributes.
214/// Intended to be used from a debugging tool (eg. gdb).
215void SelectionDAG::clearGraphAttrs() {
216#ifndef NDEBUG
217  NodeGraphAttrs.clear();
218#else
219  cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds"
220       << " on systems with Graphviz or gv!\n";
221#endif
222}
223
224
225/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
226///
227void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
228#ifndef NDEBUG
229  NodeGraphAttrs[N] = Attrs;
230#else
231  cerr << "SelectionDAG::setGraphAttrs is only available in debug builds"
232       << " on systems with Graphviz or gv!\n";
233#endif
234}
235
236
237/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
238/// Used from getNodeAttributes.
239const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
240#ifndef NDEBUG
241  std::map<const SDNode *, std::string>::const_iterator I =
242    NodeGraphAttrs.find(N);
243
244  if (I != NodeGraphAttrs.end())
245    return I->second;
246  else
247    return "";
248#else
249  cerr << "SelectionDAG::getGraphAttrs is only available in debug builds"
250       << " on systems with Graphviz or gv!\n";
251  return std::string("");
252#endif
253}
254
255/// setGraphColor - Convenience for setting node color attribute.
256///
257void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
258#ifndef NDEBUG
259  NodeGraphAttrs[N] = std::string("color=") + Color;
260#else
261  cerr << "SelectionDAG::setGraphColor is only available in debug builds"
262       << " on systems with Graphviz or gv!\n";
263#endif
264}
265
266namespace llvm {
267  template<>
268  struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
269    static std::string getGraphName(const ScheduleDAG *G) {
270      return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG);
271    }
272
273    static bool renderGraphFromBottomUp() {
274      return true;
275    }
276
277    static bool hasNodeAddressLabel(const SUnit *Node,
278                                    const ScheduleDAG *Graph) {
279      return true;
280    }
281
282    /// If you want to override the dot attributes printed for a particular
283    /// edge, override this method.
284    template<typename EdgeIter>
285    static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
286      if (EI.isSpecialDep())
287        return "color=cyan,style=dashed";
288      if (EI.isCtrlDep())
289        return "color=blue,style=dashed";
290      return "";
291    }
292
293
294    static std::string getNodeLabel(const SUnit *Node,
295                                    const ScheduleDAG *Graph);
296    static std::string getNodeAttributes(const SUnit *N,
297                                         const ScheduleDAG *Graph) {
298      return "shape=Mrecord";
299    }
300
301    static void addCustomGraphFeatures(ScheduleDAG *G,
302                                       GraphWriter<ScheduleDAG*> &GW) {
303      GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot");
304      if (G->DAG.getRoot().Val &&
305          G->SUnitMap.find(G->DAG.getRoot().Val) != G->SUnitMap.end())
306        GW.emitEdge(0, -1, G->SUnitMap[G->DAG.getRoot().Val], -1, "");
307    }
308  };
309}
310
311std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
312                                                       const ScheduleDAG *G) {
313  std::string Op;
314
315  for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) {
316    Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i],
317                                                      &G->DAG) + "\n";
318  }
319
320  if (SU->Node)
321    Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG);
322  else
323    Op += "<CROSS RC COPY>";
324
325  return Op;
326}
327
328
329/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
330/// rendered using 'dot'.
331///
332void ScheduleDAG::viewGraph() {
333// This code is only for debugging!
334#ifndef NDEBUG
335  ViewGraph(this, "dag." + DAG.getMachineFunction().getFunction()->getName());
336#else
337  cerr << "ScheduleDAG::viewGraph is only available in debug builds on "
338       << "systems with Graphviz or gv!\n";
339#endif  // NDEBUG
340}
341