Coverage Report

Created: 2018-09-25 00:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/CFGPrinter.h
Line
Count
Source (jump to first uncovered line)
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 a 'dot-cfg' analysis pass, which emits the
11
// cfg.<fnname>.dot file for each function in the program, with a graph of the
12
// CFG for that function.
13
//
14
// This file defines external functions that can be called to explicitly
15
// instantiate the CFG printer.
16
//
17
//===----------------------------------------------------------------------===//
18
19
#ifndef LLVM_ANALYSIS_CFGPRINTER_H
20
#define LLVM_ANALYSIS_CFGPRINTER_H
21
22
#include "llvm/IR/CFG.h"
23
#include "llvm/IR/Constants.h"
24
#include "llvm/IR/Function.h"
25
#include "llvm/IR/Instructions.h"
26
#include "llvm/IR/PassManager.h"
27
#include "llvm/Support/GraphWriter.h"
28
29
namespace llvm {
30
class CFGViewerPass
31
    : public PassInfoMixin<CFGViewerPass> {
32
public:
33
  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
34
};
35
36
class CFGOnlyViewerPass
37
    : public PassInfoMixin<CFGOnlyViewerPass> {
38
public:
39
  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
40
};
41
42
class CFGPrinterPass
43
    : public PassInfoMixin<CFGPrinterPass> {
44
public:
45
  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
46
};
47
48
class CFGOnlyPrinterPass
49
    : public PassInfoMixin<CFGOnlyPrinterPass> {
50
public:
51
  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
52
};
53
54
template<>
55
struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
56
57
4
  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
58
59
2
  static std::string getGraphName(const Function *F) {
60
2
    return "CFG for '" + F->getName().str() + "' function";
61
2
  }
62
63
  static std::string getSimpleNodeLabel(const BasicBlock *Node,
64
4
                                        const Function *) {
65
4
    if (!Node->getName().empty())
66
4
      return Node->getName().str();
67
0
68
0
    std::string Str;
69
0
    raw_string_ostream OS(Str);
70
0
71
0
    Node->printAsOperand(OS, false);
72
0
    return OS.str();
73
0
  }
74
75
  static std::string getCompleteNodeLabel(const BasicBlock *Node,
76
9
                                          const Function *) {
77
9
    enum { MaxColumns = 80 };
78
9
    std::string Str;
79
9
    raw_string_ostream OS(Str);
80
9
81
9
    if (Node->getName().empty()) {
82
0
      Node->printAsOperand(OS, false);
83
0
      OS << ":";
84
0
    }
85
9
86
9
    OS << *Node;
87
9
    std::string OutStr = OS.str();
88
9
    if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
89
9
90
9
    // Process string output to make it nicer...
91
9
    unsigned ColNum = 0;
92
9
    unsigned LastSpace = 0;
93
1.16k
    for (unsigned i = 0; i != OutStr.length(); 
++i1.15k
) {
94
1.15k
      if (OutStr[i] == '\n') {                            // Left justify
95
29
        OutStr[i] = '\\';
96
29
        OutStr.insert(OutStr.begin()+i+1, 'l');
97
29
        ColNum = 0;
98
29
        LastSpace = 0;
99
1.12k
      } else if (OutStr[i] == ';') {                      // Delete comments!
100
8
        unsigned Idx = OutStr.find('\n', i+1);            // Find end of line
101
8
        OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
102
8
        --i;
103
1.12k
      } else if (ColNum == MaxColumns) {                  // Wrap lines.
104
0
        // Wrap very long names even though we can't find a space.
105
0
        if (!LastSpace)
106
0
          LastSpace = i;
107
0
        OutStr.insert(LastSpace, "\\l...");
108
0
        ColNum = i - LastSpace;
109
0
        LastSpace = 0;
110
0
        i += 3; // The loop will advance 'i' again.
111
0
      }
112
1.12k
      else
113
1.12k
        ++ColNum;
114
1.15k
      if (OutStr[i] == ' ')
115
478
        LastSpace = i;
116
1.15k
    }
117
9
    return OutStr;
118
9
  }
119
120
  std::string getNodeLabel(const BasicBlock *Node,
121
4
                           const Function *Graph) {
122
4
    if (isSimple())
123
4
      return getSimpleNodeLabel(Node, Graph);
124
0
    else
125
0
      return getCompleteNodeLabel(Node, Graph);
126
4
  }
127
128
  static std::string getEdgeSourceLabel(const BasicBlock *Node,
129
4
                                        succ_const_iterator I) {
130
4
    // Label source of conditional branches with "T" or "F"
131
4
    if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
132
4
      if (BI->isConditional())
133
0
        return (I == succ_begin(Node)) ? "T" : "F";
134
4
135
4
    // Label source of switch edges with the associated value.
136
4
    if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
137
0
      unsigned SuccNo = I.getSuccessorIndex();
138
0
139
0
      if (SuccNo == 0) return "def";
140
0
141
0
      std::string Str;
142
0
      raw_string_ostream OS(Str);
143
0
      auto Case = *SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
144
0
      OS << Case.getCaseValue()->getValue();
145
0
      return OS.str();
146
0
    }
147
4
    return "";
148
4
  }
149
150
  /// Display the raw branch weights from PGO.
151
  std::string getEdgeAttributes(const BasicBlock *Node, succ_const_iterator I,
152
2
                                const Function *F) {
153
2
    const TerminatorInst *TI = Node->getTerminator();
154
2
    if (TI->getNumSuccessors() == 1)
155
2
      return "";
156
0
157
0
    MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);
158
0
    if (!WeightsNode)
159
0
      return "";
160
0
161
0
    MDString *MDName = cast<MDString>(WeightsNode->getOperand(0));
162
0
    if (MDName->getString() != "branch_weights")
163
0
      return "";
164
0
165
0
    unsigned OpNo = I.getSuccessorIndex() + 1;
166
0
    if (OpNo >= WeightsNode->getNumOperands())
167
0
      return "";
168
0
    ConstantInt *Weight =
169
0
        mdconst::dyn_extract<ConstantInt>(WeightsNode->getOperand(OpNo));
170
0
    if (!Weight)
171
0
      return "";
172
0
173
0
    // Prepend a 'W' to indicate that this is a weight rather than the actual
174
0
    // profile count (due to scaling).
175
0
    Twine Attrs = "label=\"W:" + Twine(Weight->getZExtValue()) + "\"";
176
0
    return Attrs.str();
177
0
  }
178
};
179
} // End llvm namespace
180
181
namespace llvm {
182
  class FunctionPass;
183
  FunctionPass *createCFGPrinterLegacyPassPass ();
184
  FunctionPass *createCFGOnlyPrinterLegacyPassPass ();
185
} // End llvm namespace
186
187
#endif