Add facility to dump pass manager structure
[oota-llvm.git] / include / llvm / Support / GraphWriter.h
index ac0d52379f061a12c0ccdcce08ccaad370439e9c..b9566b84f8b69426bfac0831b97aa0b1ac67b8b0 100644 (file)
 #ifndef LLVM_SUPPORT_GRAPHWRITER_H
 #define LLVM_SUPPORT_GRAPHWRITER_H
 
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/DOTGraphTraits.h"
 #include "llvm/ADT/GraphTraits.h"
+#include "llvm/System/Path.h"
+#include <fstream>
 #include <vector>
-#include <iostream>
 
 namespace llvm {
 
@@ -59,6 +61,8 @@ namespace DOT {  // Private functions...
   }
 }
 
+void DisplayGraph(const sys::Path& Filename);
+  
 template<typename GraphType>
 class GraphWriter {
   std::ostream &O;
@@ -105,7 +109,7 @@ public:
   }
 
   void writeNode(NodeType *Node) {
-    std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
+    std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
 
     O << "\tNode" << reinterpret_cast<const void*>(Node) << " [shape=record,";
     if (!NodeAttributes.empty()) O << NodeAttributes << ",";
@@ -236,6 +240,69 @@ std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
   return O;
 }
 
+template<typename GraphType>
+sys::Path WriteGraph(const GraphType &G,
+                     const std::string& Name, 
+                     const std::string& Title = "") {
+  std::string ErrMsg;
+  sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
+  if (Filename.isEmpty()) {
+    cerr << "Error: " << ErrMsg << "\n";
+    return Filename;
+  }
+  Filename.appendComponent(Name + ".dot");
+  if (Filename.makeUnique(true,&ErrMsg)) {
+    cerr << "Error: " << ErrMsg << "\n";
+    return sys::Path();
+  }
+
+  cerr << "Writing '" << Filename << "'... ";
+  
+  std::ofstream O(Filename.c_str());
+
+  if (O.good()) {
+    // Start the graph emission process...
+    GraphWriter<GraphType> W(O, G);
+
+    // Output the header for the graph...
+    W.writeHeader(Title);
+
+    // Emit all of the nodes in the graph...
+    W.writeNodes();
+
+    // Output any customizations on the graph
+    DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
+
+    // Output the end of the graph
+    W.writeFooter();
+    cerr << " done. \n";
+
+    O.close();
+    
+  } else {
+    cerr << "error opening file for writing!\n";
+    Filename.clear();
+  }
+  
+  return Filename;
+}
+  
+/// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
+/// then cleanup.  For use from the debugger.
+///
+template<typename GraphType>
+void ViewGraph(const GraphType& G, 
+               const std::string& Name, 
+               const std::string& Title = "") {
+  sys::Path Filename =  WriteGraph(G, Name, Title);
+
+  if (Filename.isEmpty()) {
+    return;
+  }
+  
+  DisplayGraph(Filename);
+}
+
 } // End llvm namespace
 
 #endif