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 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
| 15 | #include "llvm/Function.h" |
Chris Lattner | 5267651 | 2006-03-05 09:38:03 +0000 | [diff] [blame] | 16 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ScheduleDAG.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" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 7228aa7 | 2005-08-19 21:21:16 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/GraphWriter.h" |
Chris Lattner | 62ca325 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6e741f8 | 2005-07-15 22:48:31 +0000 | [diff] [blame] | 30 | #include "llvm/Config/config.h" |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 31 | #include <fstream> |
| 32 | using namespace llvm; |
| 33 | |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 34 | namespace llvm { |
| 35 | template<> |
| 36 | struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits { |
Dan Gohman | 3580331 | 2008-07-21 21:06:55 +0000 | [diff] [blame] | 37 | static bool hasEdgeDestLabels() { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | static unsigned numEdgeDestLabels(const void *Node) { |
| 42 | return ((const SDNode *) Node)->getNumValues(); |
| 43 | } |
| 44 | |
| 45 | static std::string getEdgeDestLabel(const void *Node, unsigned i) { |
| 46 | return ((const SDNode *) Node)->getValueType(i).getMVTString(); |
| 47 | } |
| 48 | |
| 49 | /// edgeTargetsEdgeSource - This method returns true if this outgoing edge |
| 50 | /// should actually target another edge source, not a node. If this method is |
| 51 | /// implemented, getEdgeTarget should be implemented. |
| 52 | template<typename EdgeIter> |
| 53 | static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is |
| 58 | /// called to determine which outgoing edge of Node is the target of this |
| 59 | /// edge. |
| 60 | template<typename EdgeIter> |
| 61 | static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) { |
| 62 | SDNode *TargetNode = *I; |
| 63 | SDNodeIterator NI = SDNodeIterator::begin(TargetNode); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 64 | std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo()); |
Dan Gohman | 3580331 | 2008-07-21 21:06:55 +0000 | [diff] [blame] | 65 | return NI; |
| 66 | } |
| 67 | |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 68 | static std::string getGraphName(const SelectionDAG *G) { |
| 69 | return G->getMachineFunction().getFunction()->getName(); |
| 70 | } |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 71 | |
| 72 | static bool renderGraphFromBottomUp() { |
| 73 | return true; |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 37345fe | 2005-10-01 00:17:07 +0000 | [diff] [blame] | 75 | |
| 76 | static bool hasNodeAddressLabel(const SDNode *Node, |
| 77 | const SelectionDAG *Graph) { |
| 78 | return true; |
| 79 | } |
Chris Lattner | 34ab4d4 | 2006-10-20 18:06:09 +0000 | [diff] [blame] | 80 | |
| 81 | /// If you want to override the dot attributes printed for a particular |
| 82 | /// edge, override this method. |
| 83 | template<typename EdgeIter> |
| 84 | static std::string getEdgeAttributes(const void *Node, EdgeIter EI) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 85 | SDValue Op = EI.getNode()->getOperand(EI.getOperand()); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 86 | MVT VT = Op.getValueType(); |
Chris Lattner | 34ab4d4 | 2006-10-20 18:06:09 +0000 | [diff] [blame] | 87 | if (VT == MVT::Flag) |
| 88 | return "color=red,style=bold"; |
| 89 | else if (VT == MVT::Other) |
Dan Gohman | 7a0a4fc | 2007-06-18 15:30:16 +0000 | [diff] [blame] | 90 | return "color=blue,style=dashed"; |
Chris Lattner | 34ab4d4 | 2006-10-20 18:06:09 +0000 | [diff] [blame] | 91 | return ""; |
| 92 | } |
| 93 | |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 94 | |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 95 | static std::string getNodeLabel(const SDNode *Node, |
| 96 | const SelectionDAG *Graph); |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 97 | static std::string getNodeAttributes(const SDNode *N, |
| 98 | const SelectionDAG *Graph) { |
| 99 | #ifndef NDEBUG |
| 100 | const std::string &Attrs = Graph->getGraphAttrs(N); |
| 101 | if (!Attrs.empty()) { |
| 102 | if (Attrs.find("shape=") == std::string::npos) |
| 103 | return std::string("shape=Mrecord,") + Attrs; |
| 104 | else |
| 105 | return Attrs; |
| 106 | } |
| 107 | #endif |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 108 | return "shape=Mrecord"; |
| 109 | } |
Chris Lattner | fc08d9c | 2005-01-10 23:52:04 +0000 | [diff] [blame] | 110 | |
| 111 | static void addCustomGraphFeatures(SelectionDAG *G, |
| 112 | GraphWriter<SelectionDAG*> &GW) { |
| 113 | GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 114 | if (G->getRoot().getNode()) |
| 115 | GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(), |
Dan Gohman | 694caf5 | 2008-07-22 17:52:59 +0000 | [diff] [blame] | 116 | "color=blue,style=dashed"); |
Chris Lattner | fc08d9c | 2005-01-10 23:52:04 +0000 | [diff] [blame] | 117 | } |
Chris Lattner | e0646b8 | 2005-01-10 23:26:00 +0000 | [diff] [blame] | 118 | }; |
| 119 | } |
| 120 | |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 121 | std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node, |
| 122 | const SelectionDAG *G) { |
Chris Lattner | ad95d6a | 2005-08-16 18:31:23 +0000 | [diff] [blame] | 123 | std::string Op = Node->getOperationName(G); |
Chris Lattner | c871e1d | 2005-01-11 22:21:04 +0000 | [diff] [blame] | 124 | |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 125 | if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 126 | Op += ": " + utostr(CSDN->getZExtValue()); |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 127 | } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) { |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 128 | Op += ": " + ftostr(CSDN->getValueAPF()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 129 | } else if (const GlobalAddressSDNode *GADN = |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 130 | dyn_cast<GlobalAddressSDNode>(Node)) { |
| 131 | Op += ": " + GADN->getGlobal()->getName(); |
Dan Gohman | 668aff6 | 2008-10-18 18:22:42 +0000 | [diff] [blame] | 132 | if (int64_t Offset = GADN->getOffset()) { |
Chris Lattner | ca19a3f | 2008-09-21 18:38:31 +0000 | [diff] [blame] | 133 | if (Offset > 0) |
| 134 | Op += "+" + itostr(Offset); |
| 135 | else |
| 136 | Op += itostr(Offset); |
| 137 | } |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 138 | } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) { |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 139 | Op += " " + itostr(FIDN->getIndex()); |
Evan Cheng | 6cc31ae | 2006-11-01 04:48:30 +0000 | [diff] [blame] | 140 | } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) { |
| 141 | Op += " " + itostr(JTDN->getIndex()); |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 142 | } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){ |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 143 | if (CP->isMachineConstantPoolEntry()) { |
Chris Lattner | 62ca325 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 144 | Op += '<'; |
| 145 | { |
| 146 | raw_string_ostream OSS(Op); |
| 147 | OSS << *CP->getMachineCPVal(); |
| 148 | } |
| 149 | Op += '>'; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 150 | } else { |
| 151 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 152 | Op += "<" + ftostr(CFP->getValueAPF()) + ">"; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 153 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal())) |
| 154 | Op += "<" + utostr(CI->getZExtValue()) + ">"; |
| 155 | else { |
Chris Lattner | 62ca325 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 156 | Op += '<'; |
| 157 | { |
| 158 | raw_string_ostream OSS(Op); |
| 159 | WriteAsOperand(OSS, CP->getConstVal(), false); |
| 160 | } |
| 161 | Op += '>'; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 162 | } |
Chris Lattner | 5267651 | 2006-03-05 09:38:03 +0000 | [diff] [blame] | 163 | } |
Dan Gohman | aed48bf | 2008-09-16 21:18:22 +0000 | [diff] [blame] | 164 | Op += " A=" + itostr(1 << CP->getAlignment()); |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 165 | } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) { |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 166 | Op = "BB: "; |
| 167 | const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); |
| 168 | if (LBB) |
| 169 | Op += LBB->getName(); |
| 170 | //Op += " " + (const void*)BBDN->getBasicBlock(); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 171 | } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) { |
Chris Lattner | 34ab4d4 | 2006-10-20 18:06:09 +0000 | [diff] [blame] | 172 | if (G && R->getReg() != 0 && |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 173 | TargetRegisterInfo::isPhysicalRegister(R->getReg())) { |
Bill Wendling | 74ab84c | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 174 | Op = Op + " " + |
Bill Wendling | 6ef781f | 2008-02-27 06:33:05 +0000 | [diff] [blame] | 175 | G->getTarget().getRegisterInfo()->getName(R->getReg()); |
Chris Lattner | 7228aa7 | 2005-08-19 21:21:16 +0000 | [diff] [blame] | 176 | } else { |
| 177 | Op += " #" + utostr(R->getReg()); |
| 178 | } |
Dan Gohman | 7f46020 | 2008-06-30 20:59:49 +0000 | [diff] [blame] | 179 | } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) { |
| 180 | Op += ": " + D->getCompileUnit()->getFileName(); |
| 181 | Op += ":" + utostr(D->getLine()); |
| 182 | if (D->getColumn() != 0) |
| 183 | Op += ":" + utostr(D->getColumn()); |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 184 | } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) { |
| 185 | Op += ": LabelID=" + utostr(L->getLabelID()); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 186 | } else if (const CallSDNode *C = dyn_cast<CallSDNode>(Node)) { |
| 187 | Op += ": CallingConv=" + utostr(C->getCallingConv()); |
| 188 | if (C->isVarArg()) |
| 189 | Op += ", isVarArg"; |
| 190 | if (C->isTailCall()) |
| 191 | Op += ", isTailCall"; |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 192 | } else if (const ExternalSymbolSDNode *ES = |
| 193 | dyn_cast<ExternalSymbolSDNode>(Node)) { |
| 194 | Op += "'" + std::string(ES->getSymbol()) + "'"; |
Chris Lattner | 2bf3c26 | 2005-05-09 04:08:27 +0000 | [diff] [blame] | 195 | } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) { |
| 196 | if (M->getValue()) |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 197 | Op += "<" + M->getValue()->getName() + ">"; |
Chris Lattner | 2bf3c26 | 2005-05-09 04:08:27 +0000 | [diff] [blame] | 198 | else |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 199 | Op += "<null>"; |
| 200 | } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) { |
Dan Gohman | dc5f936 | 2008-07-17 21:12:16 +0000 | [diff] [blame] | 201 | const Value *V = M->MO.getValue(); |
| 202 | Op += '<'; |
| 203 | if (!V) { |
| 204 | Op += "(unknown)"; |
Duncan Sands | f8ac645 | 2008-07-18 21:07:41 +0000 | [diff] [blame] | 205 | } else if (isa<PseudoSourceValue>(V)) { |
Dan Gohman | dc5f936 | 2008-07-17 21:12:16 +0000 | [diff] [blame] | 206 | // PseudoSourceValues don't have names, so use their print method. |
Chris Lattner | 62ca325 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 207 | { |
| 208 | raw_string_ostream OSS(Op); |
| 209 | OSS << *M->MO.getValue(); |
| 210 | } |
Dan Gohman | 91d49f5 | 2008-07-14 17:51:24 +0000 | [diff] [blame] | 211 | } else { |
Dan Gohman | dc5f936 | 2008-07-17 21:12:16 +0000 | [diff] [blame] | 212 | Op += V->getName(); |
Dan Gohman | 91d49f5 | 2008-07-14 17:51:24 +0000 | [diff] [blame] | 213 | } |
Dan Gohman | dc5f936 | 2008-07-17 21:12:16 +0000 | [diff] [blame] | 214 | Op += '+' + itostr(M->MO.getOffset()) + '>'; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 215 | } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) { |
| 216 | Op = Op + " AF=" + N->getArgFlags().getArgFlagsString(); |
Chris Lattner | a23e815 | 2005-08-18 03:31:02 +0000 | [diff] [blame] | 217 | } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 218 | Op = Op + " VT=" + N->getVT().getMVTString(); |
Evan Cheng | 45aeccc | 2006-10-10 20:11:26 +0000 | [diff] [blame] | 219 | } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) { |
| 220 | bool doExt = true; |
| 221 | switch (LD->getExtensionType()) { |
| 222 | default: doExt = false; break; |
| 223 | case ISD::EXTLOAD: |
| 224 | Op = Op + "<anyext "; |
| 225 | break; |
| 226 | case ISD::SEXTLOAD: |
| 227 | Op = Op + " <sext "; |
| 228 | break; |
| 229 | case ISD::ZEXTLOAD: |
| 230 | Op = Op + " <zext "; |
| 231 | break; |
| 232 | } |
| 233 | if (doExt) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 234 | Op += LD->getMemoryVT().getMVTString() + ">"; |
Chris Lattner | 94ffc7e | 2008-01-25 06:40:45 +0000 | [diff] [blame] | 235 | if (LD->isVolatile()) |
| 236 | Op += "<V>"; |
Evan Cheng | 0030582 | 2006-11-09 18:44:21 +0000 | [diff] [blame] | 237 | Op += LD->getIndexedModeName(LD->getAddressingMode()); |
Chris Lattner | 94ffc7e | 2008-01-25 06:40:45 +0000 | [diff] [blame] | 238 | if (LD->getAlignment() > 1) |
| 239 | Op += " A=" + utostr(LD->getAlignment()); |
Evan Cheng | 649b7ef | 2006-10-17 21:18:26 +0000 | [diff] [blame] | 240 | } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) { |
| 241 | if (ST->isTruncatingStore()) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 242 | Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">"; |
Chris Lattner | 94ffc7e | 2008-01-25 06:40:45 +0000 | [diff] [blame] | 243 | if (ST->isVolatile()) |
| 244 | Op += "<V>"; |
Evan Cheng | 0030582 | 2006-11-09 18:44:21 +0000 | [diff] [blame] | 245 | Op += ST->getIndexedModeName(ST->getAddressingMode()); |
Chris Lattner | 94ffc7e | 2008-01-25 06:40:45 +0000 | [diff] [blame] | 246 | if (ST->getAlignment() > 1) |
| 247 | Op += " A=" + utostr(ST->getAlignment()); |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | 59afba0 | 2007-10-15 05:32:43 +0000 | [diff] [blame] | 249 | |
| 250 | #if 0 |
| 251 | Op += " Id=" + itostr(Node->getNodeId()); |
| 252 | #endif |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 253 | |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 254 | return Op; |
| 255 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 256 | |
Chris Lattner | e9c44cd | 2005-01-11 00:34:33 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 258 | /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG |
| 259 | /// rendered using 'dot'. |
| 260 | /// |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 261 | void SelectionDAG::viewGraph(const std::string &Title) { |
Chris Lattner | e388b5e | 2005-07-14 05:17:43 +0000 | [diff] [blame] | 262 | // This code is only for debugging! |
Chris Lattner | c5f44ad | 2005-07-14 05:33:13 +0000 | [diff] [blame] | 263 | #ifndef NDEBUG |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 264 | ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(), |
| 265 | Title); |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 266 | #else |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 267 | cerr << "SelectionDAG::viewGraph is only available in debug builds on " |
| 268 | << "systems with Graphviz or gv!\n"; |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 269 | #endif // NDEBUG |
Chris Lattner | 6632848 | 2005-01-10 23:08:40 +0000 | [diff] [blame] | 270 | } |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 271 | |
Dan Gohman | 0b12aef | 2008-07-30 18:48:53 +0000 | [diff] [blame] | 272 | // This overload is defined out-of-line here instead of just using a |
| 273 | // default parameter because this is easiest for gdb to call. |
| 274 | void SelectionDAG::viewGraph() { |
| 275 | viewGraph(""); |
| 276 | } |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 277 | |
| 278 | /// clearGraphAttrs - Clear all previously defined node graph attributes. |
| 279 | /// Intended to be used from a debugging tool (eg. gdb). |
| 280 | void SelectionDAG::clearGraphAttrs() { |
| 281 | #ifndef NDEBUG |
| 282 | NodeGraphAttrs.clear(); |
| 283 | #else |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 284 | cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds" |
| 285 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 286 | #endif |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".) |
| 291 | /// |
| 292 | void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) { |
| 293 | #ifndef NDEBUG |
| 294 | NodeGraphAttrs[N] = Attrs; |
| 295 | #else |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 296 | cerr << "SelectionDAG::setGraphAttrs is only available in debug builds" |
| 297 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 298 | #endif |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".) |
| 303 | /// Used from getNodeAttributes. |
| 304 | const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const { |
| 305 | #ifndef NDEBUG |
| 306 | std::map<const SDNode *, std::string>::const_iterator I = |
| 307 | NodeGraphAttrs.find(N); |
| 308 | |
| 309 | if (I != NodeGraphAttrs.end()) |
| 310 | return I->second; |
| 311 | else |
| 312 | return ""; |
| 313 | #else |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 314 | cerr << "SelectionDAG::getGraphAttrs is only available in debug builds" |
| 315 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 316 | return std::string(""); |
| 317 | #endif |
| 318 | } |
| 319 | |
| 320 | /// setGraphColor - Convenience for setting node color attribute. |
| 321 | /// |
| 322 | void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) { |
| 323 | #ifndef NDEBUG |
| 324 | NodeGraphAttrs[N] = std::string("color=") + Color; |
| 325 | #else |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 326 | cerr << "SelectionDAG::setGraphColor is only available in debug builds" |
| 327 | << " on systems with Graphviz or gv!\n"; |
Jim Laskey | ec20402 | 2006-10-02 12:26:53 +0000 | [diff] [blame] | 328 | #endif |
| 329 | } |
| 330 | |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 331 | /// setSubgraphColorHelper - Implement setSubgraphColor. Return |
| 332 | /// whether we truncated the search. |
| 333 | /// |
| 334 | bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited, |
| 335 | int level, bool &printed) { |
| 336 | bool hit_limit = false; |
| 337 | |
| 338 | #ifndef NDEBUG |
| 339 | if (level >= 20) { |
| 340 | if (!printed) { |
| 341 | printed = true; |
| 342 | DOUT << "setSubgraphColor hit max level\n"; |
| 343 | } |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | unsigned oldSize = visited.size(); |
| 348 | visited.insert(N); |
| 349 | if (visited.size() != oldSize) { |
| 350 | setGraphColor(N, Color); |
| 351 | for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N); |
| 352 | i != iend; |
| 353 | ++i) { |
| 354 | hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit; |
| 355 | } |
| 356 | } |
| 357 | #else |
| 358 | cerr << "SelectionDAG::setSubgraphColor is only available in debug builds" |
| 359 | << " on systems with Graphviz or gv!\n"; |
| 360 | #endif |
| 361 | return hit_limit; |
| 362 | } |
| 363 | |
| 364 | /// setSubgraphColor - Convenience for setting subgraph color attribute. |
| 365 | /// |
| 366 | void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) { |
| 367 | #ifndef NDEBUG |
| 368 | DenseSet<SDNode *> visited; |
| 369 | bool printed = false; |
| 370 | if (setSubgraphColorHelper(N, Color, visited, 0, printed)) { |
| 371 | // Visually mark that we hit the limit |
Ted Kremenek | 8e7fa91 | 2008-10-27 22:43:07 +0000 | [diff] [blame^] | 372 | if (strcmp(Color, "red") == 0) { |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 373 | setSubgraphColorHelper(N, "blue", visited, 0, printed); |
| 374 | } |
Ted Kremenek | 8e7fa91 | 2008-10-27 22:43:07 +0000 | [diff] [blame^] | 375 | else if (strcmp(Color, "yellow") == 0) { |
David Greene | c5e7e8d | 2008-10-27 18:17:03 +0000 | [diff] [blame] | 376 | setSubgraphColorHelper(N, "green", visited, 0, printed); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | #else |
| 381 | cerr << "SelectionDAG::setSubgraphColor is only available in debug builds" |
| 382 | << " on systems with Graphviz or gv!\n"; |
| 383 | #endif |
| 384 | } |
| 385 | |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 386 | namespace llvm { |
| 387 | template<> |
| 388 | struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits { |
| 389 | static std::string getGraphName(const ScheduleDAG *G) { |
| 390 | return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG); |
| 391 | } |
| 392 | |
| 393 | static bool renderGraphFromBottomUp() { |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | static bool hasNodeAddressLabel(const SUnit *Node, |
| 398 | const ScheduleDAG *Graph) { |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | /// If you want to override the dot attributes printed for a particular |
| 403 | /// edge, override this method. |
| 404 | template<typename EdgeIter> |
| 405 | static std::string getEdgeAttributes(const void *Node, EdgeIter EI) { |
Dan Gohman | 89bf0a6 | 2008-04-14 23:15:07 +0000 | [diff] [blame] | 406 | if (EI.isSpecialDep()) |
| 407 | return "color=cyan,style=dashed"; |
Evan Cheng | 713a98d | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 408 | if (EI.isCtrlDep()) |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 409 | return "color=blue,style=dashed"; |
| 410 | return ""; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | static std::string getNodeLabel(const SUnit *Node, |
| 415 | const ScheduleDAG *Graph); |
| 416 | static std::string getNodeAttributes(const SUnit *N, |
| 417 | const ScheduleDAG *Graph) { |
| 418 | return "shape=Mrecord"; |
| 419 | } |
| 420 | |
| 421 | static void addCustomGraphFeatures(ScheduleDAG *G, |
| 422 | GraphWriter<ScheduleDAG*> &GW) { |
| 423 | GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 424 | const SDNode *N = G->DAG.getRoot().getNode(); |
Dan Gohman | 94d7a5f | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 425 | if (N && N->getNodeId() != -1) |
Dan Gohman | 29cdb26 | 2008-07-27 22:46:49 +0000 | [diff] [blame] | 426 | GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, |
| 427 | "color=blue,style=dashed"); |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 428 | } |
| 429 | }; |
| 430 | } |
| 431 | |
| 432 | std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU, |
| 433 | const ScheduleDAG *G) { |
| 434 | std::string Op; |
| 435 | |
| 436 | for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) { |
| 437 | Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i], |
| 438 | &G->DAG) + "\n"; |
| 439 | } |
| 440 | |
Dan Gohman | 4145bd5 | 2008-03-21 22:51:06 +0000 | [diff] [blame] | 441 | if (SU->Node) |
| 442 | Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG); |
| 443 | else |
| 444 | Op += "<CROSS RC COPY>"; |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 445 | |
| 446 | return Op; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG |
| 451 | /// rendered using 'dot'. |
| 452 | /// |
| 453 | void ScheduleDAG::viewGraph() { |
| 454 | // This code is only for debugging! |
| 455 | #ifndef NDEBUG |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 456 | ViewGraph(this, "dag." + MF->getFunction()->getName(), |
| 457 | "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' + |
| 458 | BB->getBasicBlock()->getName()); |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 459 | #else |
| 460 | cerr << "ScheduleDAG::viewGraph is only available in debug builds on " |
| 461 | << "systems with Graphviz or gv!\n"; |
| 462 | #endif // NDEBUG |
| 463 | } |