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