SelectionDAGPrinter.cpp revision 1c80d116c6a34e02059593964233f2e641bdbc5b
1//===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 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" 18#include "llvm/CodeGen/ScheduleDAG.h" 19#include "llvm/CodeGen/MachineConstantPool.h" 20#include "llvm/CodeGen/MachineFunction.h" 21#include "llvm/CodeGen/MachineModuleInfo.h" 22#include "llvm/CodeGen/PseudoSourceValue.h" 23#include "llvm/Target/TargetRegisterInfo.h" 24#include "llvm/Target/TargetMachine.h" 25#include "llvm/Support/GraphWriter.h" 26#include "llvm/Support/raw_ostream.h" 27#include "llvm/ADT/StringExtras.h" 28#include "llvm/Config/config.h" 29#include <fstream> 30using namespace llvm; 31 32namespace llvm { 33 template<> 34 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits { 35 static bool hasEdgeDestLabels() { 36 return true; 37 } 38 39 static unsigned numEdgeDestLabels(const void *Node) { 40 return ((const SDNode *) Node)->getNumValues(); 41 } 42 43 static std::string getEdgeDestLabel(const void *Node, unsigned i) { 44 return ((const SDNode *) Node)->getValueType(i).getMVTString(); 45 } 46 47 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge 48 /// should actually target another edge source, not a node. If this method is 49 /// implemented, getEdgeTarget should be implemented. 50 template<typename EdgeIter> 51 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) { 52 return true; 53 } 54 55 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is 56 /// called to determine which outgoing edge of Node is the target of this 57 /// edge. 58 template<typename EdgeIter> 59 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) { 60 SDNode *TargetNode = *I; 61 SDNodeIterator NI = SDNodeIterator::begin(TargetNode); 62 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo()); 63 return NI; 64 } 65 66 static std::string getGraphName(const SelectionDAG *G) { 67 return G->getMachineFunction().getFunction()->getName(); 68 } 69 70 static bool renderGraphFromBottomUp() { 71 return true; 72 } 73 74 static bool hasNodeAddressLabel(const SDNode *Node, 75 const SelectionDAG *Graph) { 76 return true; 77 } 78 79 /// If you want to override the dot attributes printed for a particular 80 /// edge, override this method. 81 template<typename EdgeIter> 82 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) { 83 SDValue Op = EI.getNode()->getOperand(EI.getOperand()); 84 MVT VT = Op.getValueType(); 85 if (VT == MVT::Flag) 86 return "color=red,style=bold"; 87 else if (VT == MVT::Other) 88 return "color=blue,style=dashed"; 89 return ""; 90 } 91 92 93 static std::string getNodeLabel(const SDNode *Node, 94 const SelectionDAG *Graph); 95 static std::string getNodeAttributes(const SDNode *N, 96 const SelectionDAG *Graph) { 97#ifndef NDEBUG 98 const std::string &Attrs = Graph->getGraphAttrs(N); 99 if (!Attrs.empty()) { 100 if (Attrs.find("shape=") == std::string::npos) 101 return std::string("shape=Mrecord,") + Attrs; 102 else 103 return Attrs; 104 } 105#endif 106 return "shape=Mrecord"; 107 } 108 109 static void addCustomGraphFeatures(SelectionDAG *G, 110 GraphWriter<SelectionDAG*> &GW) { 111 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); 112 if (G->getRoot().getNode()) 113 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(), 114 "color=blue,style=dashed"); 115 } 116 }; 117} 118 119std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node, 120 const SelectionDAG *G) { 121 std::string Op = Node->getOperationName(G); 122 123 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) { 124 Op += ": " + utostr(CSDN->getValue()); 125 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) { 126 Op += ": " + ftostr(CSDN->getValueAPF()); 127 } else if (const GlobalAddressSDNode *GADN = 128 dyn_cast<GlobalAddressSDNode>(Node)) { 129 int offset = GADN->getOffset(); 130 Op += ": " + GADN->getGlobal()->getName(); 131 if (offset > 0) 132 Op += "+" + itostr(offset); 133 else 134 Op += itostr(offset); 135 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) { 136 Op += " " + itostr(FIDN->getIndex()); 137 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) { 138 Op += " " + itostr(JTDN->getIndex()); 139 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){ 140 if (CP->isMachineConstantPoolEntry()) { 141 Op += '<'; 142 { 143 raw_string_ostream OSS(Op); 144 OSS << *CP->getMachineCPVal(); 145 } 146 Op += '>'; 147 } else { 148 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 149 Op += "<" + ftostr(CFP->getValueAPF()) + ">"; 150 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal())) 151 Op += "<" + utostr(CI->getZExtValue()) + ">"; 152 else { 153 Op += '<'; 154 { 155 raw_string_ostream OSS(Op); 156 WriteAsOperand(OSS, CP->getConstVal(), false); 157 } 158 Op += '>'; 159 } 160 } 161 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) { 162 Op = "BB: "; 163 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 164 if (LBB) 165 Op += LBB->getName(); 166 //Op += " " + (const void*)BBDN->getBasicBlock(); 167 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) { 168 if (G && R->getReg() != 0 && 169 TargetRegisterInfo::isPhysicalRegister(R->getReg())) { 170 Op = Op + " " + 171 G->getTarget().getRegisterInfo()->getName(R->getReg()); 172 } else { 173 Op += " #" + utostr(R->getReg()); 174 } 175 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) { 176 Op += ": " + D->getCompileUnit()->getFileName(); 177 Op += ":" + utostr(D->getLine()); 178 if (D->getColumn() != 0) 179 Op += ":" + utostr(D->getColumn()); 180 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) { 181 Op += ": LabelID=" + utostr(L->getLabelID()); 182 } else if (const ExternalSymbolSDNode *ES = 183 dyn_cast<ExternalSymbolSDNode>(Node)) { 184 Op += "'" + std::string(ES->getSymbol()) + "'"; 185 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) { 186 if (M->getValue()) 187 Op += "<" + M->getValue()->getName() + ">"; 188 else 189 Op += "<null>"; 190 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) { 191 const Value *V = M->MO.getValue(); 192 Op += '<'; 193 if (!V) { 194 Op += "(unknown)"; 195 } else if (isa<PseudoSourceValue>(V)) { 196 // PseudoSourceValues don't have names, so use their print method. 197 { 198 raw_string_ostream OSS(Op); 199 OSS << *M->MO.getValue(); 200 } 201 } else { 202 Op += V->getName(); 203 } 204 Op += '+' + itostr(M->MO.getOffset()) + '>'; 205 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) { 206 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString(); 207 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) { 208 Op = Op + " VT=" + N->getVT().getMVTString(); 209 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) { 210 bool doExt = true; 211 switch (LD->getExtensionType()) { 212 default: doExt = false; break; 213 case ISD::EXTLOAD: 214 Op = Op + "<anyext "; 215 break; 216 case ISD::SEXTLOAD: 217 Op = Op + " <sext "; 218 break; 219 case ISD::ZEXTLOAD: 220 Op = Op + " <zext "; 221 break; 222 } 223 if (doExt) 224 Op += LD->getMemoryVT().getMVTString() + ">"; 225 if (LD->isVolatile()) 226 Op += "<V>"; 227 Op += LD->getIndexedModeName(LD->getAddressingMode()); 228 if (LD->getAlignment() > 1) 229 Op += " A=" + utostr(LD->getAlignment()); 230 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) { 231 if (ST->isTruncatingStore()) 232 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">"; 233 if (ST->isVolatile()) 234 Op += "<V>"; 235 Op += ST->getIndexedModeName(ST->getAddressingMode()); 236 if (ST->getAlignment() > 1) 237 Op += " A=" + utostr(ST->getAlignment()); 238 } 239 240#if 0 241 Op += " Id=" + itostr(Node->getNodeId()); 242#endif 243 244 return Op; 245} 246 247 248/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG 249/// rendered using 'dot'. 250/// 251void SelectionDAG::viewGraph(const std::string &Title) { 252// This code is only for debugging! 253#ifndef NDEBUG 254 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(), 255 Title); 256#else 257 cerr << "SelectionDAG::viewGraph is only available in debug builds on " 258 << "systems with Graphviz or gv!\n"; 259#endif // NDEBUG 260} 261 262// This overload is defined out-of-line here instead of just using a 263// default parameter because this is easiest for gdb to call. 264void SelectionDAG::viewGraph() { 265 viewGraph(""); 266} 267 268/// clearGraphAttrs - Clear all previously defined node graph attributes. 269/// Intended to be used from a debugging tool (eg. gdb). 270void SelectionDAG::clearGraphAttrs() { 271#ifndef NDEBUG 272 NodeGraphAttrs.clear(); 273#else 274 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds" 275 << " on systems with Graphviz or gv!\n"; 276#endif 277} 278 279 280/// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".) 281/// 282void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) { 283#ifndef NDEBUG 284 NodeGraphAttrs[N] = Attrs; 285#else 286 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds" 287 << " on systems with Graphviz or gv!\n"; 288#endif 289} 290 291 292/// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".) 293/// Used from getNodeAttributes. 294const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const { 295#ifndef NDEBUG 296 std::map<const SDNode *, std::string>::const_iterator I = 297 NodeGraphAttrs.find(N); 298 299 if (I != NodeGraphAttrs.end()) 300 return I->second; 301 else 302 return ""; 303#else 304 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds" 305 << " on systems with Graphviz or gv!\n"; 306 return std::string(""); 307#endif 308} 309 310/// setGraphColor - Convenience for setting node color attribute. 311/// 312void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) { 313#ifndef NDEBUG 314 NodeGraphAttrs[N] = std::string("color=") + Color; 315#else 316 cerr << "SelectionDAG::setGraphColor is only available in debug builds" 317 << " on systems with Graphviz or gv!\n"; 318#endif 319} 320 321namespace llvm { 322 template<> 323 struct DOTGraphTraits<ScheduleDAG*> : public DefaultDOTGraphTraits { 324 static std::string getGraphName(const ScheduleDAG *G) { 325 return DOTGraphTraits<SelectionDAG*>::getGraphName(&G->DAG); 326 } 327 328 static bool renderGraphFromBottomUp() { 329 return true; 330 } 331 332 static bool hasNodeAddressLabel(const SUnit *Node, 333 const ScheduleDAG *Graph) { 334 return true; 335 } 336 337 /// If you want to override the dot attributes printed for a particular 338 /// edge, override this method. 339 template<typename EdgeIter> 340 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) { 341 if (EI.isSpecialDep()) 342 return "color=cyan,style=dashed"; 343 if (EI.isCtrlDep()) 344 return "color=blue,style=dashed"; 345 return ""; 346 } 347 348 349 static std::string getNodeLabel(const SUnit *Node, 350 const ScheduleDAG *Graph); 351 static std::string getNodeAttributes(const SUnit *N, 352 const ScheduleDAG *Graph) { 353 return "shape=Mrecord"; 354 } 355 356 static void addCustomGraphFeatures(ScheduleDAG *G, 357 GraphWriter<ScheduleDAG*> &GW) { 358 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); 359 const SDNode *N = G->DAG.getRoot().getNode(); 360 if (N && N->getNodeId() != -1) 361 GW.emitEdge(0, -1, &G->SUnits[N->getNodeId()], -1, 362 "color=blue,style=dashed"); 363 } 364 }; 365} 366 367std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU, 368 const ScheduleDAG *G) { 369 std::string Op; 370 371 for (unsigned i = 0; i < SU->FlaggedNodes.size(); ++i) { 372 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->FlaggedNodes[i], 373 &G->DAG) + "\n"; 374 } 375 376 if (SU->Node) 377 Op += DOTGraphTraits<SelectionDAG*>::getNodeLabel(SU->Node, &G->DAG); 378 else 379 Op += "<CROSS RC COPY>"; 380 381 return Op; 382} 383 384 385/// viewGraph - Pop up a ghostview window with the reachable parts of the DAG 386/// rendered using 'dot'. 387/// 388void ScheduleDAG::viewGraph() { 389// This code is only for debugging! 390#ifndef NDEBUG 391 ViewGraph(this, "dag." + MF->getFunction()->getName(), 392 "Scheduling-Units Graph for " + MF->getFunction()->getName() + ':' + 393 BB->getBasicBlock()->getName()); 394#else 395 cerr << "ScheduleDAG::viewGraph is only available in debug builds on " 396 << "systems with Graphviz or gv!\n"; 397#endif // NDEBUG 398} 399