1 //===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
3 // This file defines several printers for various different types of graphs used
4 // by the LLVM infrastructure. It uses the generic graph interface to convert
5 // the graph into a .dot graph. These graphs can then be processed with the
6 // "dot" tool to convert them to postscript or some other suitable format.
8 //===----------------------------------------------------------------------===//
10 #include "Support/GraphWriter.h"
11 #include "llvm/Pass.h"
12 #include "llvm/iTerminators.h"
13 #include "llvm/Analysis/CallGraph.h"
14 #include "llvm/Support/CFG.h"
18 //===----------------------------------------------------------------------===//
19 // Control Flow Graph Printer
20 //===----------------------------------------------------------------------===//
23 struct DOTGraphTraits<Function*> : public DefaultDOTGraphTraits {
24 static std::string getGraphName(Function *F) {
25 return "CFG for '" + F->getName() + "' function";
28 static std::string getNodeLabel(BasicBlock *Node, Function *Graph) {
29 std::ostringstream Out;
31 std::string OutStr = Out.str();
32 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
34 // Process string output to make it nicer...
35 for (unsigned i = 0; i != OutStr.length(); ++i)
36 if (OutStr[i] == '\n') { // Left justify
38 OutStr.insert(OutStr.begin()+i+1, 'l');
39 } else if (OutStr[i] == ';') { // Delete comments!
40 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
41 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
48 static std::string getNodeAttributes(BasicBlock *N) {
49 return "fontname=Courier";
52 static std::string getEdgeSourceLabel(BasicBlock *Node, succ_iterator I) {
53 // Label source of conditional branches with "T" or "F"
54 if (BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
55 if (BI->isConditional())
56 return (I == succ_begin(Node)) ? "T" : "F";
61 template<typename GraphType>
62 static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
63 const GraphType >) {
64 std::string Filename = GraphName + ".dot";
65 O << "Writing '" << Filename << "'...";
66 std::ofstream F(Filename.c_str());
71 O << " error opening file for writing!";
77 struct CFGPrinter : public FunctionPass {
78 virtual bool runOnFunction(Function &Func) {
79 WriteGraphToFile(std::cerr, "cfg."+Func.getName(), &Func);
83 void print(std::ostream &OS) const {}
85 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
90 RegisterAnalysis<CFGPrinter> P1("print-cfg",
91 "Print CFG of function to 'dot' file");
96 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
101 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
102 static std::string getGraphName(CallGraph *F) {
106 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
107 if (Node->getFunction())
108 return Node->getFunction()->getName();
110 return "Indirect call node";
116 struct CallGraphPrinter : public Pass {
117 virtual bool run(Module &M) {
118 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
122 void print(std::ostream &OS) const {}
124 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
125 AU.addRequired<CallGraph>();
126 AU.setPreservesAll();
130 RegisterAnalysis<CallGraphPrinter> P2("print-callgraph",
131 "Print Call Graph to 'dot' file");