LLVM  3.7.0
CFGPrinter.h
Go to the documentation of this file.
1 //===-- CFGPrinter.h - CFG printer external interface -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines external functions that can be called to explicitly
11 // instantiate the CFG printer.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ANALYSIS_CFGPRINTER_H
16 #define LLVM_ANALYSIS_CFGPRINTER_H
17 
18 #include "llvm/IR/CFG.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
23 
24 namespace llvm {
25 template<>
27 
28  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
29 
30  static std::string getGraphName(const Function *F) {
31  return "CFG for '" + F->getName().str() + "' function";
32  }
33 
34  static std::string getSimpleNodeLabel(const BasicBlock *Node,
35  const Function *) {
36  if (!Node->getName().empty())
37  return Node->getName().str();
38 
39  std::string Str;
40  raw_string_ostream OS(Str);
41 
42  Node->printAsOperand(OS, false);
43  return OS.str();
44  }
45 
46  static std::string getCompleteNodeLabel(const BasicBlock *Node,
47  const Function *) {
48  enum { MaxColumns = 80 };
49  std::string Str;
50  raw_string_ostream OS(Str);
51 
52  if (Node->getName().empty()) {
53  Node->printAsOperand(OS, false);
54  OS << ":";
55  }
56 
57  OS << *Node;
58  std::string OutStr = OS.str();
59  if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
60 
61  // Process string output to make it nicer...
62  unsigned ColNum = 0;
63  unsigned LastSpace = 0;
64  for (unsigned i = 0; i != OutStr.length(); ++i) {
65  if (OutStr[i] == '\n') { // Left justify
66  OutStr[i] = '\\';
67  OutStr.insert(OutStr.begin()+i+1, 'l');
68  ColNum = 0;
69  LastSpace = 0;
70  } else if (OutStr[i] == ';') { // Delete comments!
71  unsigned Idx = OutStr.find('\n', i+1); // Find end of line
72  OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
73  --i;
74  } else if (ColNum == MaxColumns) { // Wrap lines.
75  // Wrap very long names even though we can't find a space.
76  if (!LastSpace)
77  LastSpace = i;
78  OutStr.insert(LastSpace, "\\l...");
79  ColNum = i - LastSpace;
80  LastSpace = 0;
81  i += 3; // The loop will advance 'i' again.
82  }
83  else
84  ++ColNum;
85  if (OutStr[i] == ' ')
86  LastSpace = i;
87  }
88  return OutStr;
89  }
90 
91  std::string getNodeLabel(const BasicBlock *Node,
92  const Function *Graph) {
93  if (isSimple())
94  return getSimpleNodeLabel(Node, Graph);
95  else
96  return getCompleteNodeLabel(Node, Graph);
97  }
98 
99  static std::string getEdgeSourceLabel(const BasicBlock *Node,
101  // Label source of conditional branches with "T" or "F"
102  if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
103  if (BI->isConditional())
104  return (I == succ_begin(Node)) ? "T" : "F";
105 
106  // Label source of switch edges with the associated value.
107  if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
108  unsigned SuccNo = I.getSuccessorIndex();
109 
110  if (SuccNo == 0) return "def";
111 
112  std::string Str;
113  raw_string_ostream OS(Str);
116  OS << Case.getCaseValue()->getValue();
117  return OS.str();
118  }
119  return "";
120  }
121 };
122 } // End llvm namespace
123 
124 namespace llvm {
125  class FunctionPass;
126  FunctionPass *createCFGPrinterPass ();
127  FunctionPass *createCFGOnlyPrinterPass ();
128 } // End llvm namespace
129 
130 #endif
ConstantIntTy * getCaseValue()
Resolves case value for current case.
static std::string getGraphName(const Function *F)
Definition: CFGPrinter.h:30
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:188
F(f)
static std::string getCompleteNodeLabel(const BasicBlock *Node, const Function *)
Definition: CFGPrinter.h:46
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
std::string getNodeLabel(const BasicBlock *Node, const Function *Graph)
Definition: CFGPrinter.h:91
FunctionPass * createCFGPrinterPass()
Definition: CFGPrinter.cpp:158
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:104
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:3287
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
BranchInst - Conditional or Unconditional Branch instruction.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex)
Initializes case iterator for given SwitchInst and for given TerminatorInst's successor index...
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:480
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
static std::string getSimpleNodeLabel(const BasicBlock *Node, const Function *)
Definition: CFGPrinter.h:34
unsigned getSuccessorIndex() const
getSuccessorIndex - This is used to interface between code that wants to operate on terminator instru...
Definition: IR/CFG.h:172
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
FunctionPass * createCFGOnlyPrinterPass()
Definition: CFGPrinter.cpp:162
SwitchInst - Multiway switch.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
aarch64 promote const
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
static std::string getEdgeSourceLabel(const BasicBlock *Node, succ_const_iterator I)
Definition: CFGPrinter.h:99
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110