Start using retnodes_* for iteration.
[oota-llvm.git] / lib / Analysis / DataStructure / Printer.cpp
index d6f6c02cc210ecf4c4a025fa2b1f139627324024..05fba215d51d1073658eda9c5a179f843bfda245 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/DataStructure.h"
-#include "llvm/Analysis/DSGraph.h"
-#include "llvm/Analysis/DSGraphTraits.h"
+#include "llvm/Analysis/DataStructure/DataStructure.h"
+#include "llvm/Analysis/DataStructure/EquivClassGraphs.h"
+#include "llvm/Analysis/DataStructure/DSGraph.h"
+#include "llvm/Analysis/DataStructure/DSGraphTraits.h"
 #include "llvm/Module.h"
 #include "llvm/Constants.h"
 #include "llvm/Assembly/Writer.h"
-#include "Support/CommandLine.h"
-#include "Support/GraphWriter.h"
-#include "Support/Statistic.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/GraphWriter.h"
+#include "llvm/ADT/Statistic.h"
 #include <fstream>
 #include <sstream>
-
-namespace llvm {
+using namespace llvm;
 
 // OnlyPrintMain - The DataStructure printer exposes this option to allow
 // printing of only the graph for "main".
 //
 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)");
+  cl::opt<bool> DontPrintAnything("dont-print-ds", cl::ReallyHidden);
+  Statistic<> MaxGraphSize   ("dsa", "Maximum graph size");
+  Statistic<> NumFoldedNodes ("dsa", "Number of folded nodes (in final graph)");
 }
 
 void DSNode::dump() const { print(std::cerr, 0); }
@@ -39,9 +40,18 @@ void DSNode::dump() const { print(std::cerr, 0); }
 static std::string getCaption(const DSNode *N, const DSGraph *G) {
   std::stringstream OS;
   Module *M = 0;
+
+  if (!G) G = N->getParentGraph();
+
   // Get the module from ONE of the functions in the graph it is available.
-  if (G && !G->getReturnNodes().empty())
-    M = G->getReturnNodes().begin()->first->getParent();
+  if (G && G->retnodes_begin() != G->retnodes_end())
+    M = G->retnodes_begin()->first->getParent();
+  if (M == 0 && G) {
+    // If there is a global in the graph, we can use it to find the module.
+    const DSScalarMap &SM = G->getScalarMap();
+    if (SM.global_begin() != SM.global_end())
+      M = (*SM.global_begin())->getParent();
+  }
 
   if (N->isNodeCompletelyFolded())
     OS << "COLLAPSED";
@@ -73,6 +83,7 @@ static std::string getCaption(const DSNode *N, const DSGraph *G) {
   return OS.str();
 }
 
+namespace llvm {
 template<>
 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
   static std::string getGraphName(const DSGraph *G) {
@@ -83,18 +94,31 @@ struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
     }
   }
 
-  static const char *getGraphProperties(const DSGraph *G) {
-    return "\tsize=\"10,7.5\";\n"
-           "\trotate=\"90\";\n";
-  }
-
   static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
     return getCaption(Node, Graph);
   }
 
   static std::string getNodeAttributes(const DSNode *N) {
-    return "shape=Mrecord";//fontname=Courier";
+    return "shape=Mrecord";
+  }
+
+  static bool edgeTargetsEdgeSource(const void *Node,
+                                    DSNode::const_iterator I) {
+    unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
+    return (O >> DS::PointerShift) != 0;
+  }
+
+  static DSNode::const_iterator getEdgeTarget(const DSNode *Node,
+                                              DSNode::const_iterator I) {
+    unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
+    unsigned LinkNo = O >> DS::PointerShift;
+    const DSNode *N = *I;
+    DSNode::const_iterator R = N->begin();
+    for (; LinkNo; --LinkNo)
+      ++R;
+    return R;
   }
+
   
   /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
   /// and the return node.
@@ -102,32 +126,39 @@ struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
   static void addCustomGraphFeatures(const DSGraph *G,
                                      GraphWriter<const DSGraph*> &GW) {
     Module *CurMod = 0;
-    if (!G->getReturnNodes().empty())
-      CurMod = G->getReturnNodes().begin()->first->getParent();
+    if (G->retnodes_begin() != G->retnodes_end())
+      CurMod = G->retnodes_begin()->first->getParent();
+    else {
+      // If there is a global in the graph, we can use it to find the module.
+      const DSScalarMap &SM = G->getScalarMap();
+      if (SM.global_begin() != SM.global_end())
+        CurMod = (*SM.global_begin())->getParent();
+    }
+
 
     // Add scalar nodes to the graph...
     const DSGraph::ScalarMapTy &VM = G->getScalarMap();
     for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
-      if (!isa<GlobalValue>(I->first) && !isa<ConstantPointerRef>(I->first)) {
+      if (!isa<GlobalValue>(I->first)) {
         std::stringstream OS;
         WriteAsOperand(OS, I->first, false, true, CurMod);
         GW.emitSimpleNode(I->first, "", OS.str());
         
         // Add edge from return node to real destination
+        DSNode *DestNode = I->second.getNode();
         int EdgeDest = I->second.getOffset() >> DS::PointerShift;
         if (EdgeDest == 0) EdgeDest = -1;
-        GW.emitEdge(I->first, -1, I->second.getNode(),
+        GW.emitEdge(I->first, -1, DestNode,
                     EdgeDest, "arrowtail=tee,color=gray63");
       }
 
 
     // Output the returned value pointer...
-    const DSGraph::ReturnNodesTy &RetNodes = G->getReturnNodes();
-    for (DSGraph::ReturnNodesTy::const_iterator I = RetNodes.begin(),
-           E = RetNodes.end(); I != E; ++I)
+    for (DSGraph::retnodes_iterator I = G->retnodes_begin(),
+           E = G->retnodes_end(); I != E; ++I)
       if (I->second.getNode()) {
         std::string Label;
-        if (RetNodes.size() == 1)
+        if (G->getReturnNodes().size() == 1)
           Label = "returning";
         else
           Label = I->first->getName() + " ret node";
@@ -135,18 +166,20 @@ struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
         GW.emitSimpleNode((void*)I->first, "plaintext=circle", Label);
 
         // Add edge from return node to real destination
+        DSNode *RetNode = I->second.getNode();
         int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
         if (RetEdgeDest == 0) RetEdgeDest = -1;
-        GW.emitEdge((void*)I->first, -1, I->second.getNode(),
+        GW.emitEdge((void*)I->first, -1, RetNode,
                     RetEdgeDest, "arrowtail=tee,color=gray63");
       }
 
     // Output all of the call nodes...
-    const std::vector<DSCallSite> &FCs =
+    const std::list<DSCallSite> &FCs =
       G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
       : G->getFunctionCalls();
-    for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
-      const DSCallSite &Call = FCs[i];
+    for (std::list<DSCallSite>::const_iterator I = FCs.begin(), E = FCs.end();
+         I != E; ++I) {
+      const DSCallSite &Call = *I;
       std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
       EdgeSourceCaptions[0] = "r";
       if (Call.isDirectCall())
@@ -179,6 +212,7 @@ struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
     }
   }
 };
+}   // end namespace llvm
 
 void DSNode::print(std::ostream &O, const DSGraph *G) const {
   GraphWriter<const DSGraph *> W(O, G);
@@ -216,7 +250,7 @@ void DSGraph::viewGraph() const {
   }
   print(F);
   F.close();
-  if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
+  if (system("dot -Tps -Gsize=10,7.5 -Grotate=90 /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");
@@ -240,17 +274,25 @@ static void printCollection(const Collection &C, std::ostream &O,
         Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
 
       TotalCallNodes += NumCalls;
-      if (I->getName() == "main" || !OnlyPrintMain)
-        Gr.writeGraphToFile(O, Prefix+I->getName());
-      else {
+      if (I->getName() == "main" || !OnlyPrintMain) {
+        Function *SCCFn = Gr.retnodes_begin()->first;
+        if (&*I == SCCFn)
+          Gr.writeGraphToFile(O, Prefix+I->getName());
+        else
+          O << "Didn't write '" << Prefix+I->getName()
+            << ".dot' - Graph already emitted to '" << Prefix+SCCFn->getName()
+            << "\n";
+      } else {
         O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
           << 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())
+      unsigned GraphSize = Gr.getGraphSize();
+      if (MaxGraphSize < GraphSize) MaxGraphSize = GraphSize;
+
+      for (DSGraph::node_iterator NI = Gr.node_begin(), E = Gr.node_end();
+           NI != E; ++NI)
+        if ((*NI)->isNodeCompletelyFolded())
           ++NumFoldedNodes;
     }
 
@@ -271,15 +313,28 @@ static void printCollection(const Collection &C, std::ostream &O,
 
 // print - Print out the analysis results...
 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
+  if (DontPrintAnything) return;
   printCollection(*this, O, M, "ds.");
 }
 
 void BUDataStructures::print(std::ostream &O, const Module *M) const {
+  if (DontPrintAnything) return;
   printCollection(*this, O, M, "bu.");
 }
 
 void TDDataStructures::print(std::ostream &O, const Module *M) const {
+  if (DontPrintAnything) return;
   printCollection(*this, O, M, "td.");
 }
 
-} // End llvm namespace
+void CompleteBUDataStructures::print(std::ostream &O, const Module *M) const {
+  if (DontPrintAnything) return;
+  printCollection(*this, O, M, "cbu.");
+}
+
+
+void EquivClassGraphs::print(std::ostream &O, const Module *M) const {
+  if (DontPrintAnything) return;
+  printCollection(*this, O, M, "eq.");
+}
+