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