1 //===-- CFGPrinter.h - CFG printer external interface ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines external functions that can be called to explicitly
11 // instantiate the CFG printer.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_CFGPRINTER_H
16 #define LLVM_ANALYSIS_CFGPRINTER_H
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/Support/CFG.h"
22 #include "llvm/Support/GraphWriter.h"
26 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
28 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
30 static std::string getGraphName(const Function *F) {
31 return "CFG for '" + F->getNameStr() + "' function";
34 static std::string getSimpleNodeLabel(const BasicBlock *Node,
35 const Function *Graph) {
36 if (!Node->getName().empty())
37 return Node->getNameStr();
40 raw_string_ostream OS(Str);
42 WriteAsOperand(OS, Node, false);
46 static std::string getCompleteNodeLabel(const BasicBlock *Node,
47 const Function *Graph) {
49 raw_string_ostream OS(Str);
51 if (Node->getName().empty()) {
52 WriteAsOperand(OS, Node, false);
57 std::string OutStr = OS.str();
58 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
60 // Process string output to make it nicer...
61 for (unsigned i = 0; i != OutStr.length(); ++i)
62 if (OutStr[i] == '\n') { // Left justify
64 OutStr.insert(OutStr.begin()+i+1, 'l');
65 } else if (OutStr[i] == ';') { // Delete comments!
66 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
67 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
74 std::string getNodeLabel(const BasicBlock *Node,
75 const Function *Graph) {
77 return getSimpleNodeLabel(Node, Graph);
79 return getCompleteNodeLabel(Node, Graph);
82 static std::string getEdgeSourceLabel(const BasicBlock *Node,
83 succ_const_iterator I) {
84 // Label source of conditional branches with "T" or "F"
85 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
86 if (BI->isConditional())
87 return (I == succ_begin(Node)) ? "T" : "F";
89 // Label source of switch edges with the associated value.
90 if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
91 unsigned SuccNo = I.getSuccessorIndex();
93 if (SuccNo == 0) return "def";
96 raw_string_ostream OS(Str);
97 OS << SI->getCaseValue(SuccNo)->getValue();
103 } // End llvm namespace
107 FunctionPass *createCFGPrinterPass ();
108 FunctionPass *createCFGOnlyPrinterPass ();
109 } // End llvm namespace