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,
49 enum { MaxColumns = 80 };
51 raw_string_ostream OS(Str);
53 if (Node->getName().empty()) {
54 WriteAsOperand(OS, Node, false);
59 std::string OutStr = OS.str();
60 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
62 // Process string output to make it nicer...
64 unsigned LastSpace = 0;
65 for (unsigned i = 0; i != OutStr.length(); ++i) {
66 if (OutStr[i] == '\n') { // Left justify
68 OutStr.insert(OutStr.begin()+i+1, 'l');
71 } else if (OutStr[i] == ';') { // Delete comments!
72 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
73 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
75 } else if (ColNum == MaxColumns) { // Wrap lines.
77 OutStr.insert(LastSpace, "\\l...");
78 ColNum = i - LastSpace;
80 i += 3; // The loop will advance 'i' again.
82 // Else keep trying to find a space.
92 std::string getNodeLabel(const BasicBlock *Node,
93 const Function *Graph) {
95 return getSimpleNodeLabel(Node, Graph);
97 return getCompleteNodeLabel(Node, Graph);
100 static std::string getEdgeSourceLabel(const BasicBlock *Node,
101 succ_const_iterator I) {
102 // Label source of conditional branches with "T" or "F"
103 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
104 if (BI->isConditional())
105 return (I == succ_begin(Node)) ? "T" : "F";
107 // Label source of switch edges with the associated value.
108 if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
109 unsigned SuccNo = I.getSuccessorIndex();
111 if (SuccNo == 0) return "def";
114 raw_string_ostream OS(Str);
115 SwitchInst::ConstCaseIt Case =
116 SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
117 OS << Case.getCaseValue()->getValue();
123 } // End llvm namespace
127 FunctionPass *createCFGPrinterPass ();
128 FunctionPass *createCFGOnlyPrinterPass ();
129 } // End llvm namespace