Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the SelectionDAG::viewGraph method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | 84fbac5 | 2009-02-06 17:22:58 +0000 | [diff] [blame] | 14 | #include "ScheduleDAGSDNodes.h" |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/Function.h" |
Chris Lattner | 5267651 | 2006-03-05 09:38:03 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/SelectionDAG.h" |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
Bill Wendling | 10fff60 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Dan Gohman | dc5f936 | 2008-07-17 21:12:16 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 7228aa7 | 2005-08-19 21:21:16 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/GraphWriter.h" |
Chris Lattner | 62ca325 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6e741f8 | 2005-07-15 22:48:31 +0000 | [diff] [blame] | 31 | #include "llvm/Config/config.h" |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 32 | #include <fstream> |
| 33 | using namespace llvm; |
| 34 | |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 35 | namespace llvm { |
| 36 | template<> |
| 37 | struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits { |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 38 | |
| 39 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 40 | |
Dan Gohman | 3580331 | 2008-07-21 21:06:55 +0000 | [diff] [blame] | 41 | static bool hasEdgeDestLabels() { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | static unsigned numEdgeDestLabels(const void *Node) { |
| 46 | return ((const SDNode *) Node)->getNumValues(); |
| 47 | } |
| 48 | |
| 49 | static std::string getEdgeDestLabel(const void *Node, unsigned i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 50 | return ((const SDNode *) Node)->getValueType(i).getEVTString(); |
Dan Gohman | 3580331 | 2008-07-21 21:06:55 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /// edgeTargetsEdgeSource - This method returns true if this outgoing edge |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 54 | /// should actually target another edge source, not a node. If this method |
| 55 | /// is implemented, getEdgeTarget should be implemented. |
Dan Gohman | 3580331 | 2008-07-21 21:06:55 +0000 | [diff] [blame] | 56 | template<typename EdgeIter> |
| 57 | static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is |
| 62 | /// called to determine which outgoing edge of Node is the target of this |
| 63 | /// edge. |
| 64 | template<typename EdgeIter> |
| 65 | static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) { |
| 66 | SDNode *TargetNode = *I; |
| 67 | SDNodeIterator NI = SDNodeIterator::begin(TargetNode); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 68 | std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo()); |
Dan Gohman | 3580331 | 2008-07-21 21:06:55 +0000 | [diff] [blame] | 69 | return NI; |
| 70 | } |
| 71 | |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 72 | static std::string getGraphName(const SelectionDAG *G) { |
| 73 | return G->getMachineFunction().getFunction()->getName(); |
| 74 | } |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 75 | |
| 76 | static bool renderGraphFromBottomUp() { |
| 77 | return true; |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 37345fe | 2005-10-01 00:17:07 +0000 | [diff] [blame] | 79 | |
| 80 | static bool hasNodeAddressLabel(const SDNode *Node, |
| 81 | const SelectionDAG *Graph) { |
| 82 | return true; |
| 83 | } |
Chris Lattner | 34ab4d4 | 2006-10-20 18:06:09 +0000 | [diff] [blame] | 84 | |
| 85 | /// If you want to override the dot attributes printed for a particular |
| 86 | /// edge, override this method. |
| 87 | template<typename EdgeIter> |
| 88 | static std::string getEdgeAttributes(const void *Node, EdgeIter EI) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 89 | SDValue Op = EI.getNode()->getOperand(EI.getOperand()); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 90 | EVT VT = Op.getValueType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 91 | if (VT == MVT::Flag) |
Chris Lattner | 34ab4d4 | 2006-10-20 18:06:09 +0000 | [diff] [blame] | 92 | return "color=red,style=bold"; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 93 | else if (VT == MVT::Other) |
Dan Gohman | 7a0a4fc | 2007-06-18 15:30:16 +0000 | [diff] [blame] | 94 | return "color=blue,style=dashed"; |
Chris Lattner | 34ab4d4 | 2006-10-20 18:06:09 +0000 | [diff] [blame] | 95 | return ""; |
| 96 | } |
| 97 | |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 98 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame^] | 99 | static std::string getSimpleNodeLabel(const SDNode *Node, |
| 100 | const SelectionDAG *G) { |
| 101 | std::string Result = Node->getOperationName(G); |
| 102 | { |
| 103 | raw_string_ostream OS(Result); |
| 104 | Node->print_details(OS, G); |
| 105 | } |
| 106 | return Result; |
| 107 | } |
| 108 | std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph); |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 109 | static std::string getNodeAttributes(const SDNode *N, |
| 110 | const SelectionDAG *Graph) { |
| 111 | #ifndef NDEBUG |
| 112 | const std::string &Attrs = Graph->getGraphAttrs(N); |
| 113 | if (!Attrs.empty()) { |
| 114 | if (Attrs.find("shape=") == std::string::npos) |
| 115 | return std::string("shape=Mrecord,") + Attrs; |
| 116 | else |
| 117 | return Attrs; |
| 118 | } |
| 119 | #endif |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 120 | return "shape=Mrecord"; |
| 121 | } |
Chris Lattner | fc08d9c | 2005-01-10 23:52:04 +0000 | [diff] [blame] | 122 | |
| 123 | static void addCustomGraphFeatures(SelectionDAG *G, |
| 124 | GraphWriter<SelectionDAG*> &GW) { |
| 125 | GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 126 | if (G->getRoot().getNode()) |
| 127 | GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(), |
Dan Gohman | 694caf5 | 2008-07-22 17:52:59 +0000 | [diff] [blame] | 128 | "color=blue,style=dashed"); |
Chris Lattner | fc08d9c | 2005-01-10 23:52:04 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 130 | }; |
| 131 | } |
| 132 | |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 133 | std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node, |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame^] | 134 | const SelectionDAG *G) { |
| 135 | DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel (Node, G); |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 136 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 137 | |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 139 | /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG |
| 140 | /// rendered using 'dot'. |
| 141 | /// |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 142 | void SelectionDAG::viewGraph(const std::string &Title) { |
Chris Lattner | e388b5e | 2005-07-14 05:17:43 +0000 | [diff] [blame] | 143 | // This code is only for debugging! |
Chris Lattner | c5f44ad | 2005-07-14 05:33:13 +0000 | [diff] [blame] | 144 | #ifndef NDEBUG |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 145 | ViewGraph(this, "dag." + getMachineFunction().getFunction()->getNameStr(), |
| 146 | false, Title); |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 147 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 148 | errs() << "SelectionDAG::viewGraph is only available in debug builds on " |
| 149 | << "systems with Graphviz or gv!\n"; |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 150 | #endif // NDEBUG |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 151 | } |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 152 | |
Dan Gohman | 0b12aef | 2008-07-30 18:48:53 +0000 | [diff] [blame] | 153 | // This overload is defined out-of-line here instead of just using a |
| 154 | // default parameter because this is easiest for gdb to call. |
| 155 | void SelectionDAG::viewGraph() { |
| 156 | viewGraph(""); |
| 157 | } |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 158 | |
| 159 | /// clearGraphAttrs - Clear all previously defined node graph attributes. |
| 160 | /// Intended to be used from a debugging tool (eg. gdb). |
| 161 | void SelectionDAG::clearGraphAttrs() { |
| 162 | #ifndef NDEBUG |
| 163 | NodeGraphAttrs.clear(); |
| 164 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 165 | errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds" |
| 166 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 167 | #endif |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".) |
| 172 | /// |
| 173 | void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) { |
| 174 | #ifndef NDEBUG |
| 175 | NodeGraphAttrs[N] = Attrs; |
| 176 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 177 | errs() << "SelectionDAG::setGraphAttrs is only available in debug builds" |
| 178 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 179 | #endif |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".) |
| 184 | /// Used from getNodeAttributes. |
| 185 | const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const { |
| 186 | #ifndef NDEBUG |
| 187 | std::map<const SDNode *, std::string>::const_iterator I = |
| 188 | NodeGraphAttrs.find(N); |
| 189 | |
| 190 | if (I != NodeGraphAttrs.end()) |
| 191 | return I->second; |
| 192 | else |
| 193 | return ""; |
| 194 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 195 | errs() << "SelectionDAG::getGraphAttrs is only available in debug builds" |
| 196 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 197 | return std::string(""); |
| 198 | #endif |
| 199 | } |
| 200 | |
| 201 | /// setGraphColor - Convenience for setting node color attribute. |
| 202 | /// |
| 203 | void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) { |
| 204 | #ifndef NDEBUG |
| 205 | NodeGraphAttrs[N] = std::string("color=") + Color; |
| 206 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 207 | errs() << "SelectionDAG::setGraphColor is only available in debug builds" |
| 208 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 209 | #endif |
| 210 | } |
| 211 | |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 212 | /// setSubgraphColorHelper - Implement setSubgraphColor. Return |
| 213 | /// whether we truncated the search. |
| 214 | /// |
| 215 | bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited, |
| 216 | int level, bool &printed) { |
| 217 | bool hit_limit = false; |
| 218 | |
| 219 | #ifndef NDEBUG |
| 220 | if (level >= 20) { |
| 221 | if (!printed) { |
| 222 | printed = true; |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 223 | DEBUG(errs() << "setSubgraphColor hit max level\n"); |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | unsigned oldSize = visited.size(); |
| 229 | visited.insert(N); |
| 230 | if (visited.size() != oldSize) { |
| 231 | setGraphColor(N, Color); |
| 232 | for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N); |
| 233 | i != iend; |
| 234 | ++i) { |
| 235 | hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit; |
| 236 | } |
| 237 | } |
| 238 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 239 | errs() << "SelectionDAG::setSubgraphColor is only available in debug builds" |
| 240 | << " on systems with Graphviz or gv!\n"; |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 241 | #endif |
| 242 | return hit_limit; |
| 243 | } |
| 244 | |
| 245 | /// setSubgraphColor - Convenience for setting subgraph color attribute. |
| 246 | /// |
| 247 | void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) { |
| 248 | #ifndef NDEBUG |
| 249 | DenseSet<SDNode *> visited; |
| 250 | bool printed = false; |
| 251 | if (setSubgraphColorHelper(N, Color, visited, 0, printed)) { |
| 252 | // Visually mark that we hit the limit |
Ted Kremenek | 8e7fa91 | 2008-10-27 22:43:07 +0000 | [diff] [blame] | 253 | if (strcmp(Color, "red") == 0) { |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 254 | setSubgraphColorHelper(N, "blue", visited, 0, printed); |
| 255 | } |
Ted Kremenek | 8e7fa91 | 2008-10-27 22:43:07 +0000 | [diff] [blame] | 256 | else if (strcmp(Color, "yellow") == 0) { |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 257 | setSubgraphColorHelper(N, "green", visited, 0, printed); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 262 | errs() << "SelectionDAG::setSubgraphColor is only available in debug builds" |
| 263 | << " on systems with Graphviz or gv!\n"; |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 264 | #endif |
| 265 | } |
| 266 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 267 | std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const { |
Dan Gohman | 7d1cd3f | 2008-11-19 22:09:45 +0000 | [diff] [blame] | 268 | std::string s; |
| 269 | raw_string_ostream O(s); |
| 270 | O << "SU(" << SU->NodeNum << "): "; |
| 271 | if (SU->getNode()) { |
| 272 | SmallVector<SDNode *, 4> FlaggedNodes; |
| 273 | for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) |
| 274 | FlaggedNodes.push_back(N); |
| 275 | while (!FlaggedNodes.empty()) { |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame^] | 276 | O << DOTGraphTraits<SelectionDAG*> |
| 277 | ::getSimpleNodeLabel(FlaggedNodes.back(), DAG); |
Dan Gohman | 7d1cd3f | 2008-11-19 22:09:45 +0000 | [diff] [blame] | 278 | FlaggedNodes.pop_back(); |
| 279 | if (!FlaggedNodes.empty()) |
| 280 | O << "\n "; |
| 281 | } |
| 282 | } else { |
| 283 | O << "CROSS RC COPY"; |
| 284 | } |
| 285 | return O.str(); |
| 286 | } |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 287 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 288 | void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const { |
| 289 | if (DAG) { |
| 290 | // Draw a special "GraphRoot" node to indicate the root of the graph. |
| 291 | GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); |
| 292 | const SDNode *N = DAG->getRoot().getNode(); |
| 293 | if (N && N->getNodeId() != -1) |
| 294 | GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1, |
| 295 | "color=blue,style=dashed"); |
| 296 | } |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 297 | } |