From 8cbc94afb71fd2da72d8f1284f7f53e39019fdec Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Wed, 24 Jun 2009 17:37:09 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74084 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/DOTGraphTraits.h | 3 +- include/llvm/Support/GraphWriter.h | 17 ++++++--- lib/Analysis/CFGPrinter.cpp | 37 +++++++++---------- lib/CodeGen/MachineFunction.cpp | 22 +++++------ lib/CodeGen/ScheduleDAGPrinter.cpp | 10 +++-- .../SelectionDAG/SelectionDAGPrinter.cpp | 11 ++++-- lib/CompilerDriver/CompilationGraph.cpp | 3 +- tools/opt/GraphPrinters.cpp | 3 +- 8 files changed, 57 insertions(+), 49 deletions(-) diff --git a/include/llvm/Support/DOTGraphTraits.h b/include/llvm/Support/DOTGraphTraits.h index 7a61b2b1651..080297f8297 100644 --- a/include/llvm/Support/DOTGraphTraits.h +++ b/include/llvm/Support/DOTGraphTraits.h @@ -51,7 +51,8 @@ struct DefaultDOTGraphTraits { /// getNodeLabel - Given a node and a pointer to the top level graph, return /// the label to print in the node. template - static std::string getNodeLabel(const void *Node, const GraphType& Graph) { + static std::string getNodeLabel(const void *Node, + const GraphType& Graph, bool ShortNames) { return ""; } diff --git a/include/llvm/Support/GraphWriter.h b/include/llvm/Support/GraphWriter.h index ca28aafa789..01b44d0b8e2 100644 --- a/include/llvm/Support/GraphWriter.h +++ b/include/llvm/Support/GraphWriter.h @@ -72,6 +72,7 @@ template class GraphWriter { std::ostream &O; const GraphType &G; + bool ShortNames; typedef DOTGraphTraits DOTTraits; typedef GraphTraits GTraits; @@ -79,7 +80,8 @@ class GraphWriter { 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 @@ public: 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 @@ public: } 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 @@ public: template 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 W(O, G); + GraphWriter W(O, G, ShortNames); // Output the header for the graph... W.writeHeader(Title); @@ -272,6 +275,7 @@ std::ostream &WriteGraph(std::ostream &O, const GraphType &G, template 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 @@ sys::Path WriteGraph(const GraphType &G, 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 @@ sys::Path WriteGraph(const GraphType &G, template 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; diff --git a/lib/Analysis/CFGPrinter.cpp b/lib/Analysis/CFGPrinter.cpp index 143220ce388..8ada5a3f74c 100644 --- a/lib/Analysis/CFGPrinter.cpp +++ b/lib/Analysis/CFGPrinter.cpp @@ -31,12 +31,6 @@ #include 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 : public DefaultDOTGraphTraits { @@ -45,12 +39,13 @@ struct DOTGraphTraits : public DefaultDOTGraphTraits { } 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 @@ namespace { CFGOnlyViewer() : FunctionPass(&ID) {} virtual bool runOnFunction(Function &F) { - CFGOnly = true; F.viewCFG(); - CFGOnly = false; return false; } @@ -168,14 +161,20 @@ static RegisterPass 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 @@ void Function::viewCFG() const { /// his can make the graph smaller. /// void Function::viewCFGOnly() const { - CFGOnly = true; - viewCFG(); - CFGOnly = false; + ViewGraph(this, "cfg" + getName(), true); } FunctionPass *llvm::createCFGPrinterPass () { diff --git a/lib/CodeGen/MachineFunction.cpp b/lib/CodeGen/MachineFunction.cpp index cacfed1d9f7..8bcf0124a8f 100644 --- a/lib/CodeGen/MachineFunction.cpp +++ b/lib/CodeGen/MachineFunction.cpp @@ -295,12 +295,6 @@ void MachineFunction::print(std::ostream &OS) const { 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 : public DefaultDOTGraphTraits { @@ -309,13 +303,14 @@ namespace llvm { } 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::viewCFG() const 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 diff --git a/lib/CodeGen/ScheduleDAGPrinter.cpp b/lib/CodeGen/ScheduleDAGPrinter.cpp index 594c24d11d1..5efd274eea5 100644 --- a/lib/CodeGen/ScheduleDAGPrinter.cpp +++ b/lib/CodeGen/ScheduleDAGPrinter.cpp @@ -59,7 +59,8 @@ namespace llvm { 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 @@ namespace llvm { } std::string DOTGraphTraits::getNodeLabel(const SUnit *SU, - const ScheduleDAG *G) { + const ScheduleDAG *G, + bool ShortNames) { return G->getGraphNodeLabel(SU); } @@ -84,11 +86,11 @@ void ScheduleDAG::viewGraph() { // 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 " diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp index 3eec684c6f8..5a7b3fd75dc 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp @@ -94,7 +94,8 @@ namespace llvm { 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 @@ namespace llvm { } std::string DOTGraphTraits::getNodeLabel(const SDNode *Node, - const SelectionDAG *G) { + const SelectionDAG *G, + bool ShortNames) { std::string Op = Node->getOperationName(G); if (const ConstantSDNode *CSDN = dyn_cast(Node)) { @@ -262,7 +264,7 @@ std::string DOTGraphTraits::getNodeLabel(const SDNode *Node, 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 @@ std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const { for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) FlaggedNodes.push_back(N); while (!FlaggedNodes.empty()) { - O << DOTGraphTraits::getNodeLabel(FlaggedNodes.back(), DAG); + O << DOTGraphTraits::getNodeLabel(FlaggedNodes.back(), + DAG, false); FlaggedNodes.pop_back(); if (!FlaggedNodes.empty()) O << "\n "; diff --git a/lib/CompilerDriver/CompilationGraph.cpp b/lib/CompilerDriver/CompilationGraph.cpp index dece4e8e0ae..c7302afefb2 100644 --- a/lib/CompilerDriver/CompilationGraph.cpp +++ b/lib/CompilerDriver/CompilationGraph.cpp @@ -477,7 +477,8 @@ namespace llvm { { template - 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()) diff --git a/tools/opt/GraphPrinters.cpp b/tools/opt/GraphPrinters.cpp index a52baf7dae3..5d581e4af0a 100644 --- a/tools/opt/GraphPrinters.cpp +++ b/tools/opt/GraphPrinters.cpp @@ -49,7 +49,8 @@ namespace llvm { 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 -- 2.34.1