Make error messages more useful than jsut an abort
[oota-llvm.git] / lib / Analysis / DataStructure / Printer.cpp
index bbd9c2d18b22a61b0678195f84e8d30ea530536b..7f0bf5a5120d6ae859a5b4d7660eead83813d9c1 100644 (file)
 #include "llvm/Assembly/Writer.h"
 #include "Support/CommandLine.h"
 #include "Support/GraphWriter.h"
+#include "Support/Statistic.h"
 #include <fstream>
 #include <sstream>
-using std::string;
 
 // OnlyPrintMain - The DataStructure printer exposes this option to allow
 // printing of only the graph for "main".
 //
-static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
+namespace {
+  cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
+  Statistic<> MaxGraphSize   ("dsnode", "Maximum graph size");
+  Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
+}
 
 
 void DSNode::dump() const { print(std::cerr, 0); }
 
-static string getCaption(const DSNode *N, const DSGraph *G) {
+static std::string getCaption(const DSNode *N, const DSGraph *G) {
   std::stringstream OS;
-  Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
+  Module *M = G && G->hasFunction() ? G->getFunction().getParent() : 0;
 
   if (N->isNodeCompletelyFolded())
     OS << "FOLDED";
   else {
-    WriteTypeSymbolic(OS, N->getType().Ty, M);
-    if (N->getType().isArray)
+    WriteTypeSymbolic(OS, N->getType(), M);
+    if (N->isArray())
       OS << " array";
   }
   if (N->NodeType) {
@@ -43,6 +47,7 @@ static string getCaption(const DSNode *N, const DSGraph *G) {
     if (N->NodeType & DSNode::Incomplete ) OS << "I";
     if (N->NodeType & DSNode::Modified   ) OS << "M";
     if (N->NodeType & DSNode::Read       ) OS << "R";
+    if (N->NodeType & DSNode::DEAD       ) OS << "<dead>";
     OS << "\n";
   }
 
@@ -60,12 +65,11 @@ struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
     if (G->hasFunction())
       return "Function " + G->getFunction().getName();
     else
-      return "Non-function graph";
+      return "Global graph";
   }
 
   static const char *getGraphProperties(const DSGraph *G) {
-    return "\tedge [arrowtail=\"dot\"];\n"
-           "\tsize=\"10,7.5\";\n"
+    return "\tsize=\"10,7.5\";\n"
            "\trotate=\"90\";\n";
   }
 
@@ -82,14 +86,16 @@ struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
   ///
   static void addCustomGraphFeatures(const DSGraph *G,
                                      GraphWriter<const DSGraph*> &GW) {
+    Module *CurMod = G->hasFunction() ? G->getFunction().getParent() : 0;
+
     // Add scalar nodes to the graph...
-    const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
-    for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
+    const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
+    for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
          I != VM.end(); ++I)
       if (!isa<GlobalValue>(I->first)) {
         std::stringstream OS;
-        WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
-        GW.emitSimpleNode(I->first, "plaintext=circle", OS.str());
+        WriteAsOperand(OS, I->first, false, true, CurMod);
+        GW.emitSimpleNode(I->first, "", OS.str());
         
         // Add edge from return node to real destination
         int EdgeDest = I->second.getOffset() >> DS::PointerShift;
@@ -112,26 +118,39 @@ struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
     }
 
     // Output all of the call nodes...
-    const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
+    const std::vector<DSCallSite> &FCs =
+      G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
+      : G->getFunctionCalls();
     for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
       const DSCallSite &Call = FCs[i];
-      GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
+      std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
+      EdgeSourceCaptions[0] = "r";
+      if (Call.isDirectCall())
+        EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
+      else
+        EdgeSourceCaptions[1] = "f";
+
+      GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
+                        &EdgeSourceCaptions);
 
       if (DSNode *N = Call.getRetVal().getNode()) {
         int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
         if (EdgeDest == 0) EdgeDest = -1;
-        GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
+        GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
       }
-      if (DSNode *N = Call.getCallee().getNode()) {
-        int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
-        if (EdgeDest == 0) EdgeDest = -1;
-        GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
+
+      // Print out the callee...
+      if (Call.isIndirectCall()) {
+        DSNode *N = Call.getCalleeNode();
+        assert(N && "Null call site callee node!");
+        GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
       }
+
       for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
         if (DSNode *N = Call.getPtrArg(j).getNode()) {
           int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
           if (EdgeDest == 0) EdgeDest = -1;
-          GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
+          GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
         }
     }
   }
@@ -146,22 +165,43 @@ void DSGraph::print(std::ostream &O) const {
   WriteGraph(O, this, "DataStructures");
 }
 
-void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
-  string Filename = GraphName + ".dot";
+void DSGraph::writeGraphToFile(std::ostream &O,
+                               const std::string &GraphName) const {
+  std::string Filename = GraphName + ".dot";
   O << "Writing '" << Filename << "'...";
   std::ofstream F(Filename.c_str());
   
   if (F.good()) {
     print(F);
-    O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
+    unsigned NumCalls = shouldPrintAuxCalls() ?
+      getAuxFunctionCalls().size() : getFunctionCalls().size();
+    O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
   } else {
     O << "  error opening file for writing!\n";
   }
 }
 
+/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
+/// then cleanup.  For use from the debugger.
+///
+void DSGraph::viewGraph() const {
+  std::ofstream F("/tmp/tempgraph.dot");
+  if (!F.good()) {
+    std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
+    return;
+  }
+  print(F);
+  F.close();
+  if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
+    std::cerr << "Error running dot: 'dot' not in path?\n";
+  system("gv /tmp/tempgraph.ps");
+  system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
+}
+
+
 template <typename Collection>
 static void printCollection(const Collection &C, std::ostream &O,
-                            const Module *M, const string &Prefix) {
+                            const Module *M, const std::string &Prefix) {
   if (M == 0) {
     O << "Null Module pointer, cannot continue!\n";
     return;
@@ -169,22 +209,31 @@ static void printCollection(const Collection &C, std::ostream &O,
 
   unsigned TotalNumNodes = 0, TotalCallNodes = 0;
   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
-    if (!I->isExternal()) {
+    if (C.hasGraph(*I)) {
       DSGraph &Gr = C.getDSGraph((Function&)*I);
       TotalNumNodes += Gr.getGraphSize();
-      TotalCallNodes += Gr.getFunctionCalls().size();
+      unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
+        Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
+
+      TotalCallNodes += NumCalls;
       if (I->getName() == "main" || !OnlyPrintMain)
         Gr.writeGraphToFile(O, Prefix+I->getName());
       else {
         O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
-          << Gr.getGraphSize() << "+" << Gr.getFunctionCalls().size() << "]\n";
+          << Gr.getGraphSize() << "+" << NumCalls << "]\n";
       }
+
+      if (MaxGraphSize < Gr.getNodes().size())
+        MaxGraphSize = Gr.getNodes().size();
+      for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
+        if (Gr.getNodes()[i]->isNodeCompletelyFolded())
+          ++NumFoldedNodes;
     }
 
   DSGraph &GG = C.getGlobalsGraph();
   TotalNumNodes  += GG.getGraphSize();
   TotalCallNodes += GG.getFunctionCalls().size();
-  if (OnlyPrintMain) {
+  if (!OnlyPrintMain) {
     GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
   } else {
     O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
@@ -203,22 +252,8 @@ void LocalDataStructures::print(std::ostream &O, const Module *M) const {
 
 void BUDataStructures::print(std::ostream &O, const Module *M) const {
   printCollection(*this, O, M, "bu.");
-#if 0
-  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
-    if (!I->isExternal()) {
-      (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
-      break;
-    }
-#endif
 }
 
 void TDDataStructures::print(std::ostream &O, const Module *M) const {
   printCollection(*this, O, M, "td.");
-#if 0
-  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
-    if (!I->isExternal()) {
-      (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
-      break;
-    }
-#endif
 }