Allow simple nodes to have outgoing edges
[oota-llvm.git] / include / Support / GraphWriter.h
index 0a7576cd7c516bd19e368df0642c38ffe33822cd..743e3a309e60aa13239e321b68d2b4308a54e648 100644 (file)
@@ -72,7 +72,11 @@ public:
     O << DOTTraits::getGraphProperties(G);
     O << "\n";
 
+    // Emit all of the nodes in the graph...
     writeNodes();
+
+    // Output any customizations on the graph
+    DOTTraits::addCustomGraphFeatures(G, *this);
   }
 
   ~GraphWriter() {
@@ -120,8 +124,7 @@ public:
 
   void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
     if (NodeType *TargetNode = *EI) {
-      O << "\tNode" << (void*)Node << ":g" << edgeidx << " -> Node"
-        << (void*)TargetNode;
+      int DestPort = -1;
       if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
         child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
 
@@ -129,14 +132,47 @@ public:
         unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
                                         TargetIt);
         if (Offset > 64) Offset = 64;  // Targetting the truncated part?
-        O << ":g" << Offset;        
+        DestPort = (int)Offset;
       }
+
+      emitEdge((void *)Node, edgeidx, (void*)TargetNode, DestPort,
+               DOTTraits::getEdgeAttributes(Node, EI));
+    }
+  }
+
+  /// emitSimpleNode - Outputs a simple (non-record) node
+  void emitSimpleNode(const void *ID, const std::string &Attr,
+                      const std::string &Label, unsigned NumEdgeSources = 0) {
+    O << "\tNode" << ID << "[ ";
+    if (!Attr.empty())
+      O << Attr << ",";
+    O << " label =\"{" << DOT::EscapeString(Label);
+    if (NumEdgeSources) {
+      O << "|{";
       
-      std::string EdgeAttributes = DOTTraits::getEdgeAttributes(Node, EI);
-      if (!EdgeAttributes.empty())
-        O << "[" << EdgeAttributes << "]";
-      O << ";\n";
+      for (unsigned i = 0; i != NumEdgeSources; ++i) {
+        if (i) O << "|";
+        O << "<g" << i << ">";
+      }
+      O << "}";
     }
+    O << "}\"];\n";
+  }
+
+  /// emitEdge - Output an edge from a simple node into the graph...
+  void emitEdge(const void *SrcNodeID, int SrcNodePort,
+                const void *DestNodeID, int DestNodePort,
+                const std::string &Attrs) {
+    O << "\tNode" << SrcNodeID;
+    if (SrcNodePort >= 0)
+      O << ":g" << SrcNodePort;
+    O << " -> Node" << (void*)DestNodeID;
+    if (DestNodePort >= 0)
+      O << ":g" << DestNodePort;    
+
+    if (!Attrs.empty())
+      O << "[" << Attrs << "]";
+    O << ";\n";
   }
 };