1193323Sed//===-- ScheduleDAGPrinter.cpp - Implement ScheduleDAG::viewGraph() -------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This implements the ScheduleDAG::viewGraph method.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14249423Sdim#include "llvm/CodeGen/ScheduleDAG.h"
15249423Sdim#include "llvm/ADT/DenseSet.h"
16249423Sdim#include "llvm/ADT/StringExtras.h"
17193323Sed#include "llvm/Assembly/Writer.h"
18193323Sed#include "llvm/CodeGen/MachineConstantPool.h"
19193323Sed#include "llvm/CodeGen/MachineFunction.h"
20193323Sed#include "llvm/CodeGen/MachineModuleInfo.h"
21249423Sdim#include "llvm/IR/Constants.h"
22193323Sed#include "llvm/Support/Debug.h"
23193323Sed#include "llvm/Support/GraphWriter.h"
24193323Sed#include "llvm/Support/raw_ostream.h"
25249423Sdim#include "llvm/Target/TargetMachine.h"
26249423Sdim#include "llvm/Target/TargetRegisterInfo.h"
27193323Sed#include <fstream>
28193323Sedusing namespace llvm;
29193323Sed
30193323Sednamespace llvm {
31193323Sed  template<>
32193323Sed  struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
33199989Srdivacky
34199989Srdivacky  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
35199989Srdivacky
36193323Sed    static std::string getGraphName(const ScheduleDAG *G) {
37243830Sdim      return G->MF.getName();
38193323Sed    }
39193323Sed
40193323Sed    static bool renderGraphFromBottomUp() {
41193323Sed      return true;
42193323Sed    }
43234353Sdim
44249423Sdim    static bool isNodeHidden(const SUnit *Node) {
45249423Sdim      return (Node->NumPreds > 10 || Node->NumSuccs > 10);
46249423Sdim    }
47249423Sdim
48193323Sed    static bool hasNodeAddressLabel(const SUnit *Node,
49193323Sed                                    const ScheduleDAG *Graph) {
50193323Sed      return true;
51193323Sed    }
52234353Sdim
53193323Sed    /// If you want to override the dot attributes printed for a particular
54193323Sed    /// edge, override this method.
55193323Sed    static std::string getEdgeAttributes(const SUnit *Node,
56221345Sdim                                         SUnitIterator EI,
57221345Sdim                                         const ScheduleDAG *Graph) {
58193323Sed      if (EI.isArtificialDep())
59193323Sed        return "color=cyan,style=dashed";
60193323Sed      if (EI.isCtrlDep())
61193323Sed        return "color=blue,style=dashed";
62193323Sed      return "";
63193323Sed    }
64193323Sed
65234353Sdim
66199989Srdivacky    std::string getNodeLabel(const SUnit *Node, const ScheduleDAG *Graph);
67193323Sed    static std::string getNodeAttributes(const SUnit *N,
68193323Sed                                         const ScheduleDAG *Graph) {
69193323Sed      return "shape=Mrecord";
70193323Sed    }
71193323Sed
72193323Sed    static void addCustomGraphFeatures(ScheduleDAG *G,
73193323Sed                                       GraphWriter<ScheduleDAG*> &GW) {
74193323Sed      return G->addCustomGraphFeatures(GW);
75193323Sed    }
76193323Sed  };
77193323Sed}
78193323Sed
79193323Sedstd::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
80199989Srdivacky                                                       const ScheduleDAG *G) {
81193323Sed  return G->getGraphNodeLabel(SU);
82193323Sed}
83193323Sed
84193323Sed/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
85193323Sed/// rendered using 'dot'.
86193323Sed///
87234353Sdimvoid ScheduleDAG::viewGraph(const Twine &Name, const Twine &Title) {
88234353Sdim  // This code is only for debugging!
89193323Sed#ifndef NDEBUG
90234353Sdim  ViewGraph(this, Name, false, Title);
91193323Sed#else
92198090Srdivacky  errs() << "ScheduleDAG::viewGraph is only available in debug builds on "
93198090Srdivacky         << "systems with Graphviz or gv!\n";
94193323Sed#endif  // NDEBUG
95193323Sed}
96234353Sdim
97234353Sdim/// Out-of-line implementation with no arguments is handy for gdb.
98234353Sdimvoid ScheduleDAG::viewGraph() {
99234353Sdim  viewGraph(getDAGName(), "Scheduling-Units Graph for " + getDAGName());
100234353Sdim}
101