LLVM API Documentation
00001 //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This implements the SelectionDAG::viewGraph method. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Constants.h" 00015 #include "llvm/Function.h" 00016 #include "llvm/Assembly/Writer.h" 00017 #include "llvm/CodeGen/SelectionDAG.h" 00018 #include "llvm/CodeGen/ScheduleDAGSDNodes.h" 00019 #include "llvm/CodeGen/MachineConstantPool.h" 00020 #include "llvm/CodeGen/MachineFunction.h" 00021 #include "llvm/CodeGen/MachineModuleInfo.h" 00022 #include "llvm/CodeGen/PseudoSourceValue.h" 00023 #include "llvm/Analysis/DebugInfo.h" 00024 #include "llvm/Target/TargetRegisterInfo.h" 00025 #include "llvm/Target/TargetMachine.h" 00026 #include "llvm/Support/Debug.h" 00027 #include "llvm/Support/GraphWriter.h" 00028 #include "llvm/Support/raw_ostream.h" 00029 #include "llvm/ADT/DenseSet.h" 00030 #include "llvm/ADT/StringExtras.h" 00031 #include "llvm/Config/config.h" 00032 #include <fstream> 00033 using namespace llvm; 00034 00035 namespace llvm { 00036 template<> 00037 struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits { 00038 static bool hasEdgeDestLabels() { 00039 return true; 00040 } 00041 00042 static unsigned numEdgeDestLabels(const void *Node) { 00043 return ((const SDNode *) Node)->getNumValues(); 00044 } 00045 00046 static std::string getEdgeDestLabel(const void *Node, unsigned i) { 00047 return ((const SDNode *) Node)->getValueType(i).getMVTString(); 00048 } 00049 00050 /// edgeTargetsEdgeSource - This method returns true if this outgoing edge 00051 /// should actually target another edge source, not a node. If this method is 00052 /// implemented, getEdgeTarget should be implemented. 00053 template<typename EdgeIter> 00054 static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) { 00055 return true; 00056 } 00057 00058 /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is 00059 /// called to determine which outgoing edge of Node is the target of this 00060 /// edge. 00061 template<typename EdgeIter> 00062 static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) { 00063 SDNode *TargetNode = *I; 00064 SDNodeIterator NI = SDNodeIterator::begin(TargetNode); 00065 std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo()); 00066 return NI; 00067 } 00068 00069 static std::string getGraphName(const SelectionDAG *G) { 00070 return G->getMachineFunction().getFunction()->getName(); 00071 } 00072 00073 static bool renderGraphFromBottomUp() { 00074 return true; 00075 } 00076 00077 static bool hasNodeAddressLabel(const SDNode *Node, 00078 const SelectionDAG *Graph) { 00079 return true; 00080 } 00081 00082 /// If you want to override the dot attributes printed for a particular 00083 /// edge, override this method. 00084 template<typename EdgeIter> 00085 static std::string getEdgeAttributes(const void *Node, EdgeIter EI) { 00086 SDValue Op = EI.getNode()->getOperand(EI.getOperand()); 00087 MVT VT = Op.getValueType(); 00088 if (VT == MVT::Flag) 00089 return "color=red,style=bold"; 00090 else if (VT == MVT::Other) 00091 return "color=blue,style=dashed"; 00092 return ""; 00093 } 00094 00095 00096 static std::string getNodeLabel(const SDNode *Node, 00097 const SelectionDAG *Graph); 00098 static std::string getNodeAttributes(const SDNode *N, 00099 const SelectionDAG *Graph) { 00100 #ifndef NDEBUG 00101 const std::string &Attrs = Graph->getGraphAttrs(N); 00102 if (!Attrs.empty()) { 00103 if (Attrs.find("shape=") == std::string::npos) 00104 return std::string("shape=Mrecord,") + Attrs; 00105 else 00106 return Attrs; 00107 } 00108 #endif 00109 return "shape=Mrecord"; 00110 } 00111 00112 static void addCustomGraphFeatures(SelectionDAG *G, 00113 GraphWriter<SelectionDAG*> &GW) { 00114 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); 00115 if (G->getRoot().getNode()) 00116 GW.emitEdge(0, -1, G->getRoot().getNode(), G->getRoot().getResNo(), 00117 "color=blue,style=dashed"); 00118 } 00119 }; 00120 } 00121 00122 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node, 00123 const SelectionDAG *G) { 00124 std::string Op = Node->getOperationName(G); 00125 00126 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) { 00127 Op += ": " + utostr(CSDN->getZExtValue()); 00128 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(Node)) { 00129 Op += ": " + ftostr(CSDN->getValueAPF()); 00130 } else if (const GlobalAddressSDNode *GADN = 00131 dyn_cast<GlobalAddressSDNode>(Node)) { 00132 Op += ": " + GADN->getGlobal()->getName(); 00133 if (int64_t Offset = GADN->getOffset()) { 00134 if (Offset > 0) 00135 Op += "+" + itostr(Offset); 00136 else 00137 Op += itostr(Offset); 00138 } 00139 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(Node)) { 00140 Op += " " + itostr(FIDN->getIndex()); 00141 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(Node)) { 00142 Op += " " + itostr(JTDN->getIndex()); 00143 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Node)){ 00144 if (CP->isMachineConstantPoolEntry()) { 00145 Op += '<'; 00146 { 00147 raw_string_ostream OSS(Op); 00148 OSS << *CP->getMachineCPVal(); 00149 } 00150 Op += '>'; 00151 } else { 00152 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) 00153 Op += "<" + ftostr(CFP->getValueAPF()) + ">"; 00154 else if (ConstantInt *CI = dyn_cast<ConstantInt>(CP->getConstVal())) 00155 Op += "<" + utostr(CI->getZExtValue()) + ">"; 00156 else { 00157 Op += '<'; 00158 { 00159 raw_string_ostream OSS(Op); 00160 WriteAsOperand(OSS, CP->getConstVal(), false); 00161 } 00162 Op += '>'; 00163 } 00164 } 00165 Op += " A=" + itostr(1 << CP->getAlignment()); 00166 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(Node)) { 00167 Op = "BB: "; 00168 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); 00169 if (LBB) 00170 Op += LBB->getName(); 00171 //Op += " " + (const void*)BBDN->getBasicBlock(); 00172 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node)) { 00173 if (G && R->getReg() != 0 && 00174 TargetRegisterInfo::isPhysicalRegister(R->getReg())) { 00175 Op = Op + " " + 00176 G->getTarget().getRegisterInfo()->getName(R->getReg()); 00177 } else { 00178 Op += " #" + utostr(R->getReg()); 00179 } 00180 } else if (const DbgStopPointSDNode *D = dyn_cast<DbgStopPointSDNode>(Node)) { 00181 DICompileUnit CU(cast<GlobalVariable>(D->getCompileUnit())); 00182 Op += ": " + CU.getFilename(); 00183 Op += ":" + utostr(D->getLine()); 00184 if (D->getColumn() != 0) 00185 Op += ":" + utostr(D->getColumn()); 00186 } else if (const LabelSDNode *L = dyn_cast<LabelSDNode>(Node)) { 00187 Op += ": LabelID=" + utostr(L->getLabelID()); 00188 } else if (const CallSDNode *C = dyn_cast<CallSDNode>(Node)) { 00189 Op += ": CallingConv=" + utostr(C->getCallingConv()); 00190 if (C->isVarArg()) 00191 Op += ", isVarArg"; 00192 if (C->isTailCall()) 00193 Op += ", isTailCall"; 00194 } else if (const ExternalSymbolSDNode *ES = 00195 dyn_cast<ExternalSymbolSDNode>(Node)) { 00196 Op += "'" + std::string(ES->getSymbol()) + "'"; 00197 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(Node)) { 00198 if (M->getValue()) 00199 Op += "<" + M->getValue()->getName() + ">"; 00200 else 00201 Op += "<null>"; 00202 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(Node)) { 00203 const Value *V = M->MO.getValue(); 00204 Op += '<'; 00205 if (!V) { 00206 Op += "(unknown)"; 00207 } else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { 00208 // PseudoSourceValues don't have names, so use their print method. 00209 raw_string_ostream OSS(Op); 00210 PSV->print(OSS); 00211 } else { 00212 Op += V->getName(); 00213 } 00214 Op += '+' + itostr(M->MO.getOffset()) + '>'; 00215 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(Node)) { 00216 Op = Op + " AF=" + N->getArgFlags().getArgFlagsString(); 00217 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(Node)) { 00218 Op = Op + " VT=" + N->getVT().getMVTString(); 00219 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(Node)) { 00220 bool doExt = true; 00221 switch (LD->getExtensionType()) { 00222 default: doExt = false; break; 00223 case ISD::EXTLOAD: 00224 Op = Op + "<anyext "; 00225 break; 00226 case ISD::SEXTLOAD: 00227 Op = Op + " <sext "; 00228 break; 00229 case ISD::ZEXTLOAD: 00230 Op = Op + " <zext "; 00231 break; 00232 } 00233 if (doExt) 00234 Op += LD->getMemoryVT().getMVTString() + ">"; 00235 if (LD->isVolatile()) 00236 Op += "<V>"; 00237 Op += LD->getIndexedModeName(LD->getAddressingMode()); 00238 if (LD->getAlignment() > 1) 00239 Op += " A=" + utostr(LD->getAlignment()); 00240 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(Node)) { 00241 if (ST->isTruncatingStore()) 00242 Op += "<trunc " + ST->getMemoryVT().getMVTString() + ">"; 00243 if (ST->isVolatile()) 00244 Op += "<V>"; 00245 Op += ST->getIndexedModeName(ST->getAddressingMode()); 00246 if (ST->getAlignment() > 1) 00247 Op += " A=" + utostr(ST->getAlignment()); 00248 } 00249 00250 #if 0 00251 Op += " Id=" + itostr(Node->getNodeId()); 00252 #endif 00253 00254 return Op; 00255 } 00256 00257 00258 /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG 00259 /// rendered using 'dot'. 00260 /// 00261 void SelectionDAG::viewGraph(const std::string &Title) { 00262 // This code is only for debugging! 00263 #ifndef NDEBUG 00264 ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(), 00265 Title); 00266 #else 00267 cerr << "SelectionDAG::viewGraph is only available in debug builds on " 00268 << "systems with Graphviz or gv!\n"; 00269 #endif // NDEBUG 00270 } 00271 00272 // This overload is defined out-of-line here instead of just using a 00273 // default parameter because this is easiest for gdb to call. 00274 void SelectionDAG::viewGraph() { 00275 viewGraph(""); 00276 } 00277 00278 /// clearGraphAttrs - Clear all previously defined node graph attributes. 00279 /// Intended to be used from a debugging tool (eg. gdb). 00280 void SelectionDAG::clearGraphAttrs() { 00281 #ifndef NDEBUG 00282 NodeGraphAttrs.clear(); 00283 #else 00284 cerr << "SelectionDAG::clearGraphAttrs is only available in debug builds" 00285 << " on systems with Graphviz or gv!\n"; 00286 #endif 00287 } 00288 00289 00290 /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".) 00291 /// 00292 void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) { 00293 #ifndef NDEBUG 00294 NodeGraphAttrs[N] = Attrs; 00295 #else 00296 cerr << "SelectionDAG::setGraphAttrs is only available in debug builds" 00297 << " on systems with Graphviz or gv!\n"; 00298 #endif 00299 } 00300 00301 00302 /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".) 00303 /// Used from getNodeAttributes. 00304 const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const { 00305 #ifndef NDEBUG 00306 std::map<const SDNode *, std::string>::const_iterator I = 00307 NodeGraphAttrs.find(N); 00308 00309 if (I != NodeGraphAttrs.end()) 00310 return I->second; 00311 else 00312 return ""; 00313 #else 00314 cerr << "SelectionDAG::getGraphAttrs is only available in debug builds" 00315 << " on systems with Graphviz or gv!\n"; 00316 return std::string(""); 00317 #endif 00318 } 00319 00320 /// setGraphColor - Convenience for setting node color attribute. 00321 /// 00322 void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) { 00323 #ifndef NDEBUG 00324 NodeGraphAttrs[N] = std::string("color=") + Color; 00325 #else 00326 cerr << "SelectionDAG::setGraphColor is only available in debug builds" 00327 << " on systems with Graphviz or gv!\n"; 00328 #endif 00329 } 00330 00331 /// setSubgraphColorHelper - Implement setSubgraphColor. Return 00332 /// whether we truncated the search. 00333 /// 00334 bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited, 00335 int level, bool &printed) { 00336 bool hit_limit = false; 00337 00338 #ifndef NDEBUG 00339 if (level >= 20) { 00340 if (!printed) { 00341 printed = true; 00342 DOUT << "setSubgraphColor hit max level\n"; 00343 } 00344 return true; 00345 } 00346 00347 unsigned oldSize = visited.size(); 00348 visited.insert(N); 00349 if (visited.size() != oldSize) { 00350 setGraphColor(N, Color); 00351 for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N); 00352 i != iend; 00353 ++i) { 00354 hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit; 00355 } 00356 } 00357 #else 00358 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds" 00359 << " on systems with Graphviz or gv!\n"; 00360 #endif 00361 return hit_limit; 00362 } 00363 00364 /// setSubgraphColor - Convenience for setting subgraph color attribute. 00365 /// 00366 void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) { 00367 #ifndef NDEBUG 00368 DenseSet<SDNode *> visited; 00369 bool printed = false; 00370 if (setSubgraphColorHelper(N, Color, visited, 0, printed)) { 00371 // Visually mark that we hit the limit 00372 if (strcmp(Color, "red") == 0) { 00373 setSubgraphColorHelper(N, "blue", visited, 0, printed); 00374 } 00375 else if (strcmp(Color, "yellow") == 0) { 00376 setSubgraphColorHelper(N, "green", visited, 0, printed); 00377 } 00378 } 00379 00380 #else 00381 cerr << "SelectionDAG::setSubgraphColor is only available in debug builds" 00382 << " on systems with Graphviz or gv!\n"; 00383 #endif 00384 } 00385 00386 std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const { 00387 std::string s; 00388 raw_string_ostream O(s); 00389 O << "SU(" << SU->NodeNum << "): "; 00390 if (SU->getNode()) { 00391 SmallVector<SDNode *, 4> FlaggedNodes; 00392 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) 00393 FlaggedNodes.push_back(N); 00394 while (!FlaggedNodes.empty()) { 00395 O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(), DAG); 00396 FlaggedNodes.pop_back(); 00397 if (!FlaggedNodes.empty()) 00398 O << "\n "; 00399 } 00400 } else { 00401 O << "CROSS RC COPY"; 00402 } 00403 return O.str(); 00404 } 00405 00406 void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const { 00407 if (DAG) { 00408 // Draw a special "GraphRoot" node to indicate the root of the graph. 00409 GW.emitSimpleNode(0, "plaintext=circle", "GraphRoot"); 00410 const SDNode *N = DAG->getRoot().getNode(); 00411 if (N && N->getNodeId() != -1) 00412 GW.emitEdge(0, -1, &SUnits[N->getNodeId()], -1, 00413 "color=blue,style=dashed"); 00414 } 00415 }