1343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===-- ScheduleDAGPrinter.cpp - Implement ScheduleDAG::viewGraph() -------===//
2343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
3343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//                     The LLVM Compiler Infrastructure
4343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
5343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// This file is distributed under the University of Illinois Open Source
6343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// License. See LICENSE.TXT for details.
7343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
8343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===//
9343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
10343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// This implements the ScheduleDAG::viewGraph method.
11343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//
12343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===//
13343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
14343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/CodeGen/ScheduleDAG.h"
15d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/StringExtras.h"
16343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/CodeGen/MachineConstantPool.h"
17343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/CodeGen/MachineFunction.h"
18343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/CodeGen/MachineModuleInfo.h"
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
20343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/Support/Debug.h"
21343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/Support/GraphWriter.h"
22343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/Support/raw_ostream.h"
23d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetRegisterInfo.h"
24343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include <fstream>
25343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanusing namespace llvm;
26343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
27343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmannamespace llvm {
28343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  template<>
29343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits {
30a10d598602308549d87d2c5d9848f5a72fda2b43Tobias Grosser
31a10d598602308549d87d2c5d9848f5a72fda2b43Tobias Grosser  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
32a10d598602308549d87d2c5d9848f5a72fda2b43Tobias Grosser
33343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    static std::string getGraphName(const ScheduleDAG *G) {
3496601ca332ab388754ca4673be8973396fea2dddCraig Topper      return G->MF.getName();
35343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
36343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
37343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    static bool renderGraphFromBottomUp() {
38343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      return true;
39343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
40acddd4963d2f3b5fd94ab872b4fd393b34c80e5fAndrew Trick
41c6ada8e5f38168f13830e448f2b9e2d8e3eac72bAndrew Trick    static bool isNodeHidden(const SUnit *Node) {
42c6ada8e5f38168f13830e448f2b9e2d8e3eac72bAndrew Trick      return (Node->NumPreds > 10 || Node->NumSuccs > 10);
43c6ada8e5f38168f13830e448f2b9e2d8e3eac72bAndrew Trick    }
44c6ada8e5f38168f13830e448f2b9e2d8e3eac72bAndrew Trick
45f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    static std::string getNodeIdentifierLabel(const SUnit *Node,
46f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                                              const ScheduleDAG *Graph) {
47f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      std::string R;
48f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      raw_string_ostream OS(R);
49f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      OS << static_cast<const void *>(Node);
50f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      return R;
51343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
52acddd4963d2f3b5fd94ab872b4fd393b34c80e5fAndrew Trick
53343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    /// If you want to override the dot attributes printed for a particular
54343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    /// edge, override this method.
553ee7449f3973fa467388d66d27124da22f45df85Dan Gohman    static std::string getEdgeAttributes(const SUnit *Node,
56a91f86c49a63f7a37c662c3a9553055bff33a84bTobias Grosser                                         SUnitIterator EI,
57a91f86c49a63f7a37c662c3a9553055bff33a84bTobias Grosser                                         const ScheduleDAG *Graph) {
5898adea11496400c8385b774b4d9f9acd4c99d254Dan Gohman      if (EI.isArtificialDep())
59343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        return "color=cyan,style=dashed";
60343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      if (EI.isCtrlDep())
61343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        return "color=blue,style=dashed";
62343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      return "";
63343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
64acddd4963d2f3b5fd94ab872b4fd393b34c80e5fAndrew Trick
65343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
6656f4ef3232850e29c4635d0923910acce8887bd0Tobias Grosser    std::string getNodeLabel(const SUnit *Node, const ScheduleDAG *Graph);
67343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    static std::string getNodeAttributes(const SUnit *N,
68343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman                                         const ScheduleDAG *Graph) {
69343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      return "shape=Mrecord";
70343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
71343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
72343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    static void addCustomGraphFeatures(ScheduleDAG *G,
73343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman                                       GraphWriter<ScheduleDAG*> &GW) {
74343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      return G->addCustomGraphFeatures(GW);
75343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
76343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  };
77343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
78343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
79343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanstd::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
8056f4ef3232850e29c4635d0923910acce8887bd0Tobias Grosser                                                       const ScheduleDAG *G) {
81343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  return G->getGraphNodeLabel(SU);
82343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
83343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
84343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
85343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// rendered using 'dot'.
86343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman///
8756b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trickvoid ScheduleDAG::viewGraph(const Twine &Name, const Twine &Title) {
8856b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick  // This code is only for debugging!
89343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#ifndef NDEBUG
9056b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick  ViewGraph(this, Name, false, Title);
91343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#else
9243ed267db3512823a9698f810be4e64bee227270Daniel Dunbar  errs() << "ScheduleDAG::viewGraph is only available in debug builds on "
9343ed267db3512823a9698f810be4e64bee227270Daniel Dunbar         << "systems with Graphviz or gv!\n";
94343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#endif  // NDEBUG
95343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
9656b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick
9756b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick/// Out-of-line implementation with no arguments is handy for gdb.
9856b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trickvoid ScheduleDAG::viewGraph() {
9956b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick  viewGraph(getDAGName(), "Scheduling-Units Graph for " + getDAGName());
10056b94c52c9bf0342106ca7d274b9bb469d5ef619Andrew Trick}
101