1 //===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines several printers for various different types of graphs used
11 // by the LLVM infrastructure. It uses the generic graph interface to convert
12 // the graph into a .dot graph. These graphs can then be processed with the
13 // "dot" tool to convert them to postscript or some other suitable format.
15 //===----------------------------------------------------------------------===//
17 #include "Support/GraphWriter.h"
18 #include "llvm/Pass.h"
19 #include "llvm/iTerminators.h"
20 #include "llvm/Analysis/CallGraph.h"
21 #include "llvm/Support/CFG.h"
25 //===----------------------------------------------------------------------===//
26 // Control Flow Graph Printer
27 //===----------------------------------------------------------------------===//
30 struct DOTGraphTraits<Function*> : public DefaultDOTGraphTraits {
31 static std::string getGraphName(Function *F) {
32 return "CFG for '" + F->getName() + "' function";
35 static std::string getNodeLabel(BasicBlock *Node, Function *Graph) {
36 std::ostringstream Out;
38 std::string OutStr = Out.str();
39 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
41 // Process string output to make it nicer...
42 for (unsigned i = 0; i != OutStr.length(); ++i)
43 if (OutStr[i] == '\n') { // Left justify
45 OutStr.insert(OutStr.begin()+i+1, 'l');
46 } else if (OutStr[i] == ';') { // Delete comments!
47 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
48 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
55 static std::string getNodeAttributes(BasicBlock *N) {
56 return "fontname=Courier";
59 static std::string getEdgeSourceLabel(BasicBlock *Node, succ_iterator I) {
60 // Label source of conditional branches with "T" or "F"
61 if (BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
62 if (BI->isConditional())
63 return (I == succ_begin(Node)) ? "T" : "F";
68 template<typename GraphType>
69 static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
70 const GraphType >) {
71 std::string Filename = GraphName + ".dot";
72 O << "Writing '" << Filename << "'...";
73 std::ofstream F(Filename.c_str());
78 O << " error opening file for writing!";
84 struct CFGPrinter : public FunctionPass {
85 virtual bool runOnFunction(Function &Func) {
86 WriteGraphToFile(std::cerr, "cfg."+Func.getName(), &Func);
90 void print(std::ostream &OS) const {}
92 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 RegisterAnalysis<CFGPrinter> P1("print-cfg",
98 "Print CFG of function to 'dot' file");
103 //===----------------------------------------------------------------------===//
104 // Call Graph Printer
105 //===----------------------------------------------------------------------===//
108 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
109 static std::string getGraphName(CallGraph *F) {
113 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
114 if (Node->getFunction())
115 return Node->getFunction()->getName();
117 return "Indirect call node";
123 struct CallGraphPrinter : public Pass {
124 virtual bool run(Module &M) {
125 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
129 void print(std::ostream &OS) const {}
131 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
132 AU.addRequired<CallGraph>();
133 AU.setPreservesAll();
137 RegisterAnalysis<CallGraphPrinter> P2("print-callgraph",
138 "Print Call Graph to 'dot' file");