LLVM API Documentation
00001 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a '-dot-cfg' analysis pass, which emits the 00011 // cfg.<fnname>.dot file for each function in the program, with a graph of the 00012 // CFG for that function. 00013 // 00014 // The other main feature of this file is that it implements the 00015 // Function::viewCFG method, which is useful for debugging passes which operate 00016 // on the CFG. 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #include "llvm/Function.h" 00021 #include "llvm/Instructions.h" 00022 #include "llvm/Pass.h" 00023 #include "llvm/Analysis/CFGPrinter.h" 00024 #include "llvm/Assembly/Writer.h" 00025 #include "llvm/Support/CFG.h" 00026 #include "llvm/Support/Compiler.h" 00027 #include "llvm/Support/GraphWriter.h" 00028 #include "llvm/Config/config.h" 00029 #include <iosfwd> 00030 #include <sstream> 00031 #include <fstream> 00032 using namespace llvm; 00033 00034 namespace llvm { 00035 template<> 00036 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits { 00037 static std::string getGraphName(const Function *F) { 00038 return "CFG for '" + F->getNameStr() + "' function"; 00039 } 00040 00041 static std::string getNodeLabel(const BasicBlock *Node, 00042 const Function *Graph, 00043 bool ShortNames) { 00044 if (ShortNames && !Node->getName().empty()) 00045 return Node->getNameStr() + ":"; 00046 00047 std::ostringstream Out; 00048 if (ShortNames) { 00049 WriteAsOperand(Out, Node, false); 00050 return Out.str(); 00051 } 00052 00053 if (Node->getName().empty()) { 00054 WriteAsOperand(Out, Node, false); 00055 Out << ":"; 00056 } 00057 00058 Out << *Node; 00059 std::string OutStr = Out.str(); 00060 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 00061 00062 // Process string output to make it nicer... 00063 for (unsigned i = 0; i != OutStr.length(); ++i) 00064 if (OutStr[i] == '\n') { // Left justify 00065 OutStr[i] = '\\'; 00066 OutStr.insert(OutStr.begin()+i+1, 'l'); 00067 } else if (OutStr[i] == ';') { // Delete comments! 00068 unsigned Idx = OutStr.find('\n', i+1); // Find end of line 00069 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx); 00070 --i; 00071 } 00072 00073 return OutStr; 00074 } 00075 00076 static std::string getEdgeSourceLabel(const BasicBlock *Node, 00077 succ_const_iterator I) { 00078 // Label source of conditional branches with "T" or "F" 00079 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator())) 00080 if (BI->isConditional()) 00081 return (I == succ_begin(Node)) ? "T" : "F"; 00082 return ""; 00083 } 00084 }; 00085 } 00086 00087 namespace { 00088 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass { 00089 static char ID; // Pass identifcation, replacement for typeid 00090 CFGViewer() : FunctionPass(&ID) {} 00091 00092 virtual bool runOnFunction(Function &F) { 00093 F.viewCFG(); 00094 return false; 00095 } 00096 00097 void print(std::ostream &OS, const Module* = 0) const {} 00098 00099 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00100 AU.setPreservesAll(); 00101 } 00102 }; 00103 } 00104 00105 char CFGViewer::ID = 0; 00106 static RegisterPass<CFGViewer> 00107 V0("view-cfg", "View CFG of function", false, true); 00108 00109 namespace { 00110 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass { 00111 static char ID; // Pass identifcation, replacement for typeid 00112 CFGOnlyViewer() : FunctionPass(&ID) {} 00113 00114 virtual bool runOnFunction(Function &F) { 00115 F.viewCFG(); 00116 return false; 00117 } 00118 00119 void print(std::ostream &OS, const Module* = 0) const {} 00120 00121 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00122 AU.setPreservesAll(); 00123 } 00124 }; 00125 } 00126 00127 char CFGOnlyViewer::ID = 0; 00128 static RegisterPass<CFGOnlyViewer> 00129 V1("view-cfg-only", 00130 "View CFG of function (with no function bodies)", false, true); 00131 00132 namespace { 00133 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass { 00134 static char ID; // Pass identification, replacement for typeid 00135 CFGPrinter() : FunctionPass(&ID) {} 00136 explicit CFGPrinter(void *pid) : FunctionPass(pid) {} 00137 00138 virtual bool runOnFunction(Function &F) { 00139 std::string Filename = "cfg." + F.getNameStr() + ".dot"; 00140 cerr << "Writing '" << Filename << "'..."; 00141 std::ofstream File(Filename.c_str()); 00142 00143 if (File.good()) 00144 WriteGraph(File, (const Function*)&F); 00145 else 00146 cerr << " error opening file for writing!"; 00147 cerr << "\n"; 00148 return false; 00149 } 00150 00151 void print(std::ostream &OS, const Module* = 0) const {} 00152 00153 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00154 AU.setPreservesAll(); 00155 } 00156 }; 00157 } 00158 00159 char CFGPrinter::ID = 0; 00160 static RegisterPass<CFGPrinter> 00161 P1("dot-cfg", "Print CFG of function to 'dot' file", false, true); 00162 00163 namespace { 00164 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass { 00165 static char ID; // Pass identification, replacement for typeid 00166 CFGOnlyPrinter() : FunctionPass(&ID) {} 00167 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {} 00168 virtual bool runOnFunction(Function &F) { 00169 std::string Filename = "cfg." + F.getNameStr() + ".dot"; 00170 cerr << "Writing '" << Filename << "'..."; 00171 std::ofstream File(Filename.c_str()); 00172 00173 if (File.good()) 00174 WriteGraph(File, (const Function*)&F, true); 00175 else 00176 cerr << " error opening file for writing!"; 00177 cerr << "\n"; 00178 return false; 00179 } 00180 void print(std::ostream &OS, const Module* = 0) const {} 00181 00182 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00183 AU.setPreservesAll(); 00184 } 00185 }; 00186 } 00187 00188 char CFGOnlyPrinter::ID = 0; 00189 static RegisterPass<CFGOnlyPrinter> 00190 P2("dot-cfg-only", 00191 "Print CFG of function to 'dot' file (with no function bodies)", false, true); 00192 00193 /// viewCFG - This function is meant for use from the debugger. You can just 00194 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 00195 /// program, displaying the CFG of the current function. This depends on there 00196 /// being a 'dot' and 'gv' program in your path. 00197 /// 00198 void Function::viewCFG() const { 00199 ViewGraph(this, "cfg" + getNameStr()); 00200 } 00201 00202 /// viewCFGOnly - This function is meant for use from the debugger. It works 00203 /// just like viewCFG, but it does not include the contents of basic blocks 00204 /// into the nodes, just the label. If you are only interested in the CFG t 00205 /// his can make the graph smaller. 00206 /// 00207 void Function::viewCFGOnly() const { 00208 ViewGraph(this, "cfg" + getNameStr(), true); 00209 } 00210 00211 FunctionPass *llvm::createCFGPrinterPass () { 00212 return new CFGPrinter(); 00213 } 00214 00215 FunctionPass *llvm::createCFGOnlyPrinterPass () { 00216 return new CFGOnlyPrinter(); 00217 } 00218