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/Assembly/Writer.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Support/GraphWriter.h"
27 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
29 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
31 static std::string getGraphName(const Function *F) {
32 return "CFG for '" + F->getName().str() + "' function";
35 static std::string getSimpleNodeLabel(const BasicBlock *Node,
37 if (!Node->getName().empty())
38 return Node->getName().str();
41 raw_string_ostream OS(Str);
43 WriteAsOperand(OS, Node, false);
47 static std::string getCompleteNodeLabel(const BasicBlock *Node,
50 raw_string_ostream OS(Str);
52 if (Node->getName().empty()) {
53 WriteAsOperand(OS, Node, false);
58 std::string OutStr = OS.str();
59 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
61 // Process string output to make it nicer...
62 for (unsigned i = 0; i != OutStr.length(); ++i)
63 if (OutStr[i] == '\n') { // Left justify
65 OutStr.insert(OutStr.begin()+i+1, 'l');
66 } else if (OutStr[i] == ';') { // Delete comments!
67 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
68 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
75 std::string getNodeLabel(const BasicBlock *Node,
76 const Function *Graph) {
78 return getSimpleNodeLabel(Node, Graph);
80 return getCompleteNodeLabel(Node, Graph);
83 static std::string getEdgeSourceLabel(const BasicBlock *Node,
84 succ_const_iterator I) {
85 // Label source of conditional branches with "T" or "F"
86 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
87 if (BI->isConditional())
88 return (I == succ_begin(Node)) ? "T" : "F";
90 // Label source of switch edges with the associated value.
91 if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
92 unsigned SuccNo = I.getSuccessorIndex();
94 if (SuccNo == 0) return "def";
97 raw_string_ostream OS(Str);
98 SwitchInst::ConstCaseIt Case =
99 SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
100 OS << Case.getCaseValue()->getValue();
106 } // End llvm namespace
110 FunctionPass *createCFGPrinterPass ();
111 FunctionPass *createCFGOnlyPrinterPass ();
112 } // End llvm namespace