[llvm-commits] [llvm] r74084 - in /llvm/trunk: include/llvm/Support/DOTGraphTraits.h include/llvm/Support/GraphWriter.h lib/Analysis/CFGPrinter.cpp lib/CodeGen/MachineFunction.cpp lib/CodeGen/ScheduleDAGPrinter.cpp lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp lib/CompilerDriver/CompilationGraph.cpp tools/opt/GraphPrinters.cpp

Owen Anderson resistor at mac.com
Wed Jun 24 10:37:11 PDT 2009


Author: resistor
Date: Wed Jun 24 12:37:09 2009
New Revision: 74084

URL: http://llvm.org/viewvc/llvm-project?rev=74084&view=rev
Log:
Get rid of the global CFGOnly flag by threading a ShortNames parameters through the GraphViz rendering code.
Update other uses in the codebase for this change.

Modified:
    llvm/trunk/include/llvm/Support/DOTGraphTraits.h
    llvm/trunk/include/llvm/Support/GraphWriter.h
    llvm/trunk/lib/Analysis/CFGPrinter.cpp
    llvm/trunk/lib/CodeGen/MachineFunction.cpp
    llvm/trunk/lib/CodeGen/ScheduleDAGPrinter.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
    llvm/trunk/lib/CompilerDriver/CompilationGraph.cpp
    llvm/trunk/tools/opt/GraphPrinters.cpp

Modified: llvm/trunk/include/llvm/Support/DOTGraphTraits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/DOTGraphTraits.h?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/DOTGraphTraits.h (original)
+++ llvm/trunk/include/llvm/Support/DOTGraphTraits.h Wed Jun 24 12:37:09 2009
@@ -51,7 +51,8 @@
   /// getNodeLabel - Given a node and a pointer to the top level graph, return
   /// the label to print in the node.
   template<typename GraphType>
-  static std::string getNodeLabel(const void *Node, const GraphType& Graph) {
+  static std::string getNodeLabel(const void *Node,
+                                  const GraphType& Graph, bool ShortNames) {
     return "";
   }
 

Modified: llvm/trunk/include/llvm/Support/GraphWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/GraphWriter.h?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/GraphWriter.h (original)
+++ llvm/trunk/include/llvm/Support/GraphWriter.h Wed Jun 24 12:37:09 2009
@@ -72,6 +72,7 @@
 class GraphWriter {
   std::ostream &O;
   const GraphType &G;
+  bool ShortNames;
 
   typedef DOTGraphTraits<GraphType>           DOTTraits;
   typedef GraphTraits<GraphType>              GTraits;
@@ -79,7 +80,8 @@
   typedef typename GTraits::nodes_iterator    node_iterator;
   typedef typename GTraits::ChildIteratorType child_iterator;
 public:
-  GraphWriter(std::ostream &o, const GraphType &g) : O(o), G(g) {}
+  GraphWriter(std::ostream &o, const GraphType &g, bool SN) :
+    O(o), G(g), ShortNames(SN) {}
 
   void writeHeader(const std::string &Name) {
     std::string GraphName = DOTTraits::getGraphName(G);
@@ -130,7 +132,7 @@
     O << "label=\"{";
 
     if (!DOTTraits::renderGraphFromBottomUp()) {
-      O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
+      O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
 
       // If we should include the address of the node in the label, do so now.
       if (DOTTraits::hasNodeAddressLabel(Node, G))
@@ -156,7 +158,7 @@
     }
 
     if (DOTTraits::renderGraphFromBottomUp()) {
-      O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
+      O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
 
       // If we should include the address of the node in the label, do so now.
       if (DOTTraits::hasNodeAddressLabel(Node, G))
@@ -250,10 +252,11 @@
 
 template<typename GraphType>
 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
+                         bool ShortNames = false,
                          const std::string &Name = "",
                          const std::string &Title = "") {
   // Start the graph emission process...
-  GraphWriter<GraphType> W(O, G);
+  GraphWriter<GraphType> W(O, G, ShortNames);
 
   // Output the header for the graph...
   W.writeHeader(Title);
@@ -272,6 +275,7 @@
 template<typename GraphType>
 sys::Path WriteGraph(const GraphType &G,
                      const std::string& Name,
+                     bool ShortNames = false,
                      const std::string& Title = "") {
   std::string ErrMsg;
   sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
@@ -290,7 +294,7 @@
   std::ofstream O(Filename.c_str());
 
   if (O.good()) {
-    WriteGraph(O, G, Name, Title);
+    WriteGraph(O, G, ShortNames, Name, Title);
     cerr << " done. \n";
 
     O.close();
@@ -308,8 +312,9 @@
 template<typename GraphType>
 void ViewGraph(const GraphType& G,
                const std::string& Name,
+               bool ShortNames = false,
                const std::string& Title = "") {
-  sys::Path Filename =  WriteGraph(G, Name, Title);
+  sys::Path Filename =  WriteGraph(G, Name, ShortNames, Title);
 
   if (Filename.isEmpty()) {
     return;

Modified: llvm/trunk/lib/Analysis/CFGPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/CFGPrinter.cpp?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/CFGPrinter.cpp (original)
+++ llvm/trunk/lib/Analysis/CFGPrinter.cpp Wed Jun 24 12:37:09 2009
@@ -31,12 +31,6 @@
 #include <fstream>
 using namespace llvm;
 
-/// CFGOnly flag - This is used to control whether or not the CFG graph printer
-/// prints out the contents of basic blocks or not.  This is acceptable because
-/// this code is only really used for debugging purposes.
-///
-static bool CFGOnly = false;
-
 namespace llvm {
 template<>
 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
@@ -45,12 +39,13 @@
   }
 
   static std::string getNodeLabel(const BasicBlock *Node,
-                                  const Function *Graph) {
-    if (CFGOnly && !Node->getName().empty())
+                                  const Function *Graph,
+                                  bool ShortNames) {
+    if (ShortNames && !Node->getName().empty())
       return Node->getName() + ":";
 
     std::ostringstream Out;
-    if (CFGOnly) {
+    if (ShortNames) {
       WriteAsOperand(Out, Node, false);
       return Out.str();
     }
@@ -117,9 +112,7 @@
     CFGOnlyViewer() : FunctionPass(&ID) {}
 
     virtual bool runOnFunction(Function &F) {
-      CFGOnly = true;
       F.viewCFG();
-      CFGOnly = false;
       return false;
     }
 
@@ -168,14 +161,20 @@
 P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
 
 namespace {
-  struct VISIBILITY_HIDDEN CFGOnlyPrinter : public CFGPrinter {
+  struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
     static char ID; // Pass identification, replacement for typeid
-    CFGOnlyPrinter() : CFGPrinter(&ID) {}
+    CFGOnlyPrinter() : FunctionPass(&ID) {}
+    explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
     virtual bool runOnFunction(Function &F) {
-      bool OldCFGOnly = CFGOnly;
-      CFGOnly = true;
-      CFGPrinter::runOnFunction(F);
-      CFGOnly = OldCFGOnly;
+      std::string Filename = "cfg." + F.getName() + ".dot";
+      cerr << "Writing '" << Filename << "'...";
+      std::ofstream File(Filename.c_str());
+
+      if (File.good())
+        WriteGraph(File, (const Function*)&F, true);
+      else
+        cerr << "  error opening file for writing!";
+      cerr << "\n";
       return false;
     }
     void print(std::ostream &OS, const Module* = 0) const {}
@@ -206,9 +205,7 @@
 /// his can make the graph smaller.
 ///
 void Function::viewCFGOnly() const {
-  CFGOnly = true;
-  viewCFG();
-  CFGOnly = false;
+  ViewGraph(this, "cfg" + getName(), true);
 }
 
 FunctionPass *llvm::createCFGPrinterPass () {

Modified: llvm/trunk/lib/CodeGen/MachineFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineFunction.cpp?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/MachineFunction.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineFunction.cpp Wed Jun 24 12:37:09 2009
@@ -295,12 +295,6 @@
   OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
 }
 
-/// CFGOnly flag - This is used to control whether or not the CFG graph printer
-/// prints out the contents of basic blocks or not.  This is acceptable because
-/// this code is only really used for debugging purposes.
-///
-static bool CFGOnly = false;
-
 namespace llvm {
   template<>
   struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
@@ -309,13 +303,14 @@
     }
 
     static std::string getNodeLabel(const MachineBasicBlock *Node,
-                                    const MachineFunction *Graph) {
-      if (CFGOnly && Node->getBasicBlock() &&
+                                    const MachineFunction *Graph,
+                                    bool ShortNames) {
+      if (ShortNames && Node->getBasicBlock() &&
           !Node->getBasicBlock()->getName().empty())
         return Node->getBasicBlock()->getName() + ":";
 
       std::ostringstream Out;
-      if (CFGOnly) {
+      if (ShortNames) {
         Out << Node->getNumber() << ':';
         return Out.str();
       }
@@ -348,9 +343,12 @@
 
 void MachineFunction::viewCFGOnly() const
 {
-  CFGOnly = true;
-  viewCFG();
-  CFGOnly = false;
+#ifndef NDEBUG
+  ViewGraph(this, "mf" + getFunction()->getName(), true);
+#else
+  cerr << "SelectionDAG::viewGraph is only available in debug builds on "
+       << "systems with Graphviz or gv!\n";
+#endif // NDEBUG
 }
 
 // The next two methods are used to construct and to retrieve

Modified: llvm/trunk/lib/CodeGen/ScheduleDAGPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/ScheduleDAGPrinter.cpp?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/ScheduleDAGPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/ScheduleDAGPrinter.cpp Wed Jun 24 12:37:09 2009
@@ -59,7 +59,8 @@
     
 
     static std::string getNodeLabel(const SUnit *Node,
-                                    const ScheduleDAG *Graph);
+                                    const ScheduleDAG *Graph,
+                                    bool ShortNames);
     static std::string getNodeAttributes(const SUnit *N,
                                          const ScheduleDAG *Graph) {
       return "shape=Mrecord";
@@ -73,7 +74,8 @@
 }
 
 std::string DOTGraphTraits<ScheduleDAG*>::getNodeLabel(const SUnit *SU,
-                                                       const ScheduleDAG *G) {
+                                                       const ScheduleDAG *G,
+                                                       bool ShortNames) {
   return G->getGraphNodeLabel(SU);
 }
 
@@ -84,11 +86,11 @@
 // This code is only for debugging!
 #ifndef NDEBUG
   if (BB->getBasicBlock())
-    ViewGraph(this, "dag." + MF.getFunction()->getName(),
+    ViewGraph(this, "dag." + MF.getFunction()->getName(), false,
               "Scheduling-Units Graph for " + MF.getFunction()->getName() + ':' +
               BB->getBasicBlock()->getName());
   else
-    ViewGraph(this, "dag." + MF.getFunction()->getName(),
+    ViewGraph(this, "dag." + MF.getFunction()->getName(), false,
               "Scheduling-Units Graph for " + MF.getFunction()->getName());
 #else
   cerr << "ScheduleDAG::viewGraph is only available in debug builds on "

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp Wed Jun 24 12:37:09 2009
@@ -94,7 +94,8 @@
     
 
     static std::string getNodeLabel(const SDNode *Node,
-                                    const SelectionDAG *Graph);
+                                    const SelectionDAG *Graph,
+                                    bool ShortNames);
     static std::string getNodeAttributes(const SDNode *N,
                                          const SelectionDAG *Graph) {
 #ifndef NDEBUG
@@ -120,7 +121,8 @@
 }
 
 std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
-                                                        const SelectionDAG *G) {
+                                                        const SelectionDAG *G,
+                                                        bool ShortNames) {
   std::string Op = Node->getOperationName(G);
 
   if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(Node)) {
@@ -262,7 +264,7 @@
 void SelectionDAG::viewGraph(const std::string &Title) {
 // This code is only for debugging!
 #ifndef NDEBUG
-  ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(),
+  ViewGraph(this, "dag." + getMachineFunction().getFunction()->getName(), false,
             Title);
 #else
   cerr << "SelectionDAG::viewGraph is only available in debug builds on "
@@ -393,7 +395,8 @@
     for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode())
       FlaggedNodes.push_back(N);
     while (!FlaggedNodes.empty()) {
-      O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(), DAG);
+      O << DOTGraphTraits<SelectionDAG*>::getNodeLabel(FlaggedNodes.back(),
+                                                       DAG, false);
       FlaggedNodes.pop_back();
       if (!FlaggedNodes.empty())
         O << "\n    ";

Modified: llvm/trunk/lib/CompilerDriver/CompilationGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CompilerDriver/CompilationGraph.cpp?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/lib/CompilerDriver/CompilationGraph.cpp (original)
+++ llvm/trunk/lib/CompilerDriver/CompilationGraph.cpp Wed Jun 24 12:37:09 2009
@@ -477,7 +477,8 @@
   {
 
     template<typename GraphType>
-    static std::string getNodeLabel(const Node* N, const GraphType&)
+    static std::string getNodeLabel(const Node* N, const GraphType&, 
+                                    bool ShortNames)
     {
       if (N->ToolPtr)
         if (N->ToolPtr->IsJoin())

Modified: llvm/trunk/tools/opt/GraphPrinters.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/GraphPrinters.cpp?rev=74084&r1=74083&r2=74084&view=diff

==============================================================================
--- llvm/trunk/tools/opt/GraphPrinters.cpp (original)
+++ llvm/trunk/tools/opt/GraphPrinters.cpp Wed Jun 24 12:37:09 2009
@@ -49,7 +49,8 @@
       return "Call Graph";
     }
 
-    static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
+    static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph,
+                                    bool ShortNames) {
       if (Node->getFunction())
         return ((Value*)Node->getFunction())->getName();
       else





More information about the llvm-commits mailing list