From: Tobias Grosser Date: Sat, 16 Jan 2010 10:56:41 +0000 (+0000) Subject: Create Generic DOTGraphTraits Printer/Viewer X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=23279f18a9759d94579de7e79be61c7aa790b8ba;p=oota-llvm.git Create Generic DOTGraphTraits Printer/Viewer Move the DOTGraphTraits dotty printer/viewer templates, that were developed for the dominance tree into their own header file. This will allow reuse in future passes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93632 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Analysis/DOTGraphTraitsPass.h b/include/llvm/Analysis/DOTGraphTraitsPass.h new file mode 100644 index 00000000000..4828eba5b52 --- /dev/null +++ b/include/llvm/Analysis/DOTGraphTraitsPass.h @@ -0,0 +1,83 @@ +//===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Templates to create dotty viewer and printer passes for GraphTraits graphs. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ANALYSIS_DOT_GRAPHTRAITS_PASS_H +#define LLVM_ANALYSIS_DOT_GRAPHTRAITS_PASS_H + +#include "llvm/Pass.h" +#include "llvm/Analysis/CFGPrinter.h" + +namespace llvm { +template +struct DOTGraphTraitsViewer : public FunctionPass { + std::string Name; + + DOTGraphTraitsViewer(std::string GraphName, const void *ID) : FunctionPass(ID) { + Name = GraphName; + } + + virtual bool runOnFunction(Function &F) { + Analysis *Graph; + std::string Title, GraphName; + Graph = &getAnalysis(); + GraphName = DOTGraphTraits::getGraphName(Graph); + Title = GraphName + " for '" + F.getNameStr() + "' function"; + ViewGraph(Graph, Name, Simple, Title); + + return false; + } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired(); + } +}; + +template +struct DOTGraphTraitsPrinter : public FunctionPass { + + std::string Name; + + DOTGraphTraitsPrinter(std::string GraphName, const void *ID) + : FunctionPass(ID) { + Name = GraphName; + } + + virtual bool runOnFunction(Function &F) { + Analysis *Graph; + std::string Filename = Name + "." + F.getNameStr() + ".dot"; + errs() << "Writing '" << Filename << "'..."; + + std::string ErrorInfo; + raw_fd_ostream File(Filename.c_str(), ErrorInfo); + Graph = &getAnalysis(); + + std::string Title, GraphName; + GraphName = DOTGraphTraits::getGraphName(Graph); + Title = GraphName + " for '" + F.getNameStr() + "' function"; + + if (ErrorInfo.empty()) + WriteGraph(File, Graph, Simple, Name, Title); + else + errs() << " error opening file for writing!"; + errs() << "\n"; + return false; + } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired(); + } +}; +} +#endif diff --git a/lib/Analysis/DomPrinter.cpp b/lib/Analysis/DomPrinter.cpp index 32b8994f028..3af687af184 100644 --- a/lib/Analysis/DomPrinter.cpp +++ b/lib/Analysis/DomPrinter.cpp @@ -19,10 +19,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/DomPrinter.h" -#include "llvm/Pass.h" -#include "llvm/Function.h" -#include "llvm/Analysis/CFGPrinter.h" + #include "llvm/Analysis/Dominators.h" +#include "llvm/Analysis/DOTGraphTraitsPass.h" #include "llvm/Analysis/PostDominators.h" using namespace llvm; @@ -110,29 +109,29 @@ struct GenericGraphViewer : public FunctionPass { }; struct DomViewer - : public GenericGraphViewer { + : public DOTGraphTraitsViewer { static char ID; - DomViewer() : GenericGraphViewer("dom", &ID){} + DomViewer() : DOTGraphTraitsViewer("dom", &ID){} }; struct DomOnlyViewer - : public GenericGraphViewer { + : public DOTGraphTraitsViewer { static char ID; - DomOnlyViewer() : GenericGraphViewer("domonly", &ID){} + DomOnlyViewer() : DOTGraphTraitsViewer("domonly", &ID){} }; struct PostDomViewer - : public GenericGraphViewer { + : public DOTGraphTraitsViewer { static char ID; PostDomViewer() : - GenericGraphViewer("postdom", &ID){} + DOTGraphTraitsViewer("postdom", &ID){} }; struct PostDomOnlyViewer - : public GenericGraphViewer { + : public DOTGraphTraitsViewer { static char ID; PostDomOnlyViewer() : - GenericGraphViewer("postdomonly", &ID){} + DOTGraphTraitsViewer("postdomonly", &ID){} }; } // end anonymous namespace @@ -155,67 +154,30 @@ RegisterPass D("view-postdom-only", "(with no function bodies)"); namespace { -template -struct GenericGraphPrinter : public FunctionPass { - - std::string Name; - - GenericGraphPrinter(std::string GraphName, const void *ID) - : FunctionPass(ID) { - Name = GraphName; - } - - virtual bool runOnFunction(Function &F) { - Analysis *Graph; - std::string Filename = Name + "." + F.getNameStr() + ".dot"; - errs() << "Writing '" << Filename << "'..."; - - std::string ErrorInfo; - raw_fd_ostream File(Filename.c_str(), ErrorInfo); - Graph = &getAnalysis(); - - std::string Title, GraphName; - GraphName = DOTGraphTraits::getGraphName(Graph); - Title = GraphName + " for '" + F.getNameStr() + "' function"; - - if (ErrorInfo.empty()) - WriteGraph(File, Graph, OnlyBBS, Name, Title); - else - errs() << " error opening file for writing!"; - errs() << "\n"; - return false; - } - - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesAll(); - AU.addRequired(); - } -}; - struct DomPrinter - : public GenericGraphPrinter { + : public DOTGraphTraitsPrinter { static char ID; - DomPrinter() : GenericGraphPrinter("dom", &ID) {} + DomPrinter() : DOTGraphTraitsPrinter("dom", &ID) {} }; struct DomOnlyPrinter - : public GenericGraphPrinter { + : public DOTGraphTraitsPrinter { static char ID; - DomOnlyPrinter() : GenericGraphPrinter("domonly", &ID) {} + DomOnlyPrinter() : DOTGraphTraitsPrinter("domonly", &ID) {} }; struct PostDomPrinter - : public GenericGraphPrinter { + : public DOTGraphTraitsPrinter { static char ID; PostDomPrinter() : - GenericGraphPrinter("postdom", &ID) {} + DOTGraphTraitsPrinter("postdom", &ID) {} }; struct PostDomOnlyPrinter - : public GenericGraphPrinter { + : public DOTGraphTraitsPrinter { static char ID; PostDomOnlyPrinter() : - GenericGraphPrinter("postdomonly", &ID) {} + DOTGraphTraitsPrinter("postdomonly", &ID) {} }; } // end anonymous namespace