Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Analysis/RegionInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
// Detects single entry single exit regions in the control flow graph.
9
//===----------------------------------------------------------------------===//
10
11
#include "llvm/Analysis/RegionInfo.h"
12
#include "llvm/ADT/Statistic.h"
13
#ifndef NDEBUG
14
#include "llvm/Analysis/RegionPrinter.h"
15
#endif
16
#include "llvm/Analysis/RegionInfoImpl.h"
17
#include "llvm/Config/llvm-config.h"
18
#include "llvm/IR/Function.h"
19
#include "llvm/IR/PassManager.h"
20
#include "llvm/Pass.h"
21
#include "llvm/Support/CommandLine.h"
22
#include "llvm/Support/Compiler.h"
23
#include "llvm/Support/raw_ostream.h"
24
25
using namespace llvm;
26
27
#define DEBUG_TYPE "region"
28
29
namespace llvm {
30
31
template class RegionBase<RegionTraits<Function>>;
32
template class RegionNodeBase<RegionTraits<Function>>;
33
template class RegionInfoBase<RegionTraits<Function>>;
34
35
} // end namespace llvm
36
37
STATISTIC(numRegions,       "The # of regions");
38
STATISTIC(numSimpleRegions, "The # of simple regions");
39
40
// Always verify if expensive checking is enabled.
41
42
static cl::opt<bool,true>
43
VerifyRegionInfoX(
44
  "verify-region-info",
45
  cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
46
  cl::desc("Verify region info (time consuming)"));
47
48
static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
49
  cl::location(RegionInfo::printStyle),
50
  cl::Hidden,
51
  cl::desc("style of printing regions"),
52
  cl::values(
53
    clEnumValN(Region::PrintNone, "none",  "print no details"),
54
    clEnumValN(Region::PrintBB, "bb",
55
               "print regions in detail with block_iterator"),
56
    clEnumValN(Region::PrintRN, "rn",
57
               "print regions in detail with element_iterator")));
58
59
//===----------------------------------------------------------------------===//
60
// Region implementation
61
//
62
63
Region::Region(BasicBlock *Entry, BasicBlock *Exit,
64
               RegionInfo* RI,
65
               DominatorTree *DT, Region *Parent) :
66
34.2k
  RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
67
34.2k
68
34.2k
}
69
70
34.1k
Region::~Region() = default;
71
72
//===----------------------------------------------------------------------===//
73
// RegionInfo implementation
74
//
75
76
4.05k
RegionInfo::RegionInfo() = default;
77
78
4.07k
RegionInfo::~RegionInfo() = default;
79
80
bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
81
21
                            FunctionAnalysisManager::Invalidator &) {
82
21
  // Check whether the analysis, all analyses on functions, or the function's
83
21
  // CFG has been preserved.
84
21
  auto PAC = PA.getChecker<RegionInfoAnalysis>();
85
21
  return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
86
21
           PAC.preservedSet<CFGAnalyses>());
87
21
}
88
89
33.6k
void RegionInfo::updateStatistics(Region *R) {
90
33.6k
  ++numRegions;
91
33.6k
92
33.6k
  // TODO: Slow. Should only be enabled if -stats is used.
93
33.6k
  if (R->isSimple())
94
2.41k
    ++numSimpleRegions;
95
33.6k
}
96
97
void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
98
29.0k
                             PostDominatorTree *PDT_, DominanceFrontier *DF_) {
99
29.0k
  DT = DT_;
100
29.0k
  PDT = PDT_;
101
29.0k
  DF = DF_;
102
29.0k
103
29.0k
  TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
104
29.0k
                              this, DT, nullptr);
105
29.0k
  updateStatistics(TopLevelRegion);
106
29.0k
  calculate(F);
107
29.0k
}
108
109
#ifndef NDEBUG
110
void RegionInfo::view() { viewRegion(this); }
111
112
void RegionInfo::viewOnly() { viewRegionOnly(this); }
113
#endif
114
115
//===----------------------------------------------------------------------===//
116
// RegionInfoPass implementation
117
//
118
119
4.02k
RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
120
4.02k
  initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
121
4.02k
}
122
123
3.98k
RegionInfoPass::~RegionInfoPass() = default;
124
125
29.0k
bool RegionInfoPass::runOnFunction(Function &F) {
126
29.0k
  releaseMemory();
127
29.0k
128
29.0k
  auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
129
29.0k
  auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
130
29.0k
  auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
131
29.0k
132
29.0k
  RI.recalculate(F, DT, PDT, DF);
133
29.0k
  return false;
134
29.0k
}
135
136
58.0k
void RegionInfoPass::releaseMemory() {
137
58.0k
  RI.releaseMemory();
138
58.0k
}
139
140
0
void RegionInfoPass::verifyAnalysis() const {
141
0
    RI.verifyAnalysis();
142
0
}
143
144
4.02k
void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
145
4.02k
  AU.setPreservesAll();
146
4.02k
  AU.addRequiredTransitive<DominatorTreeWrapperPass>();
147
4.02k
  AU.addRequired<PostDominatorTreeWrapperPass>();
148
4.02k
  AU.addRequired<DominanceFrontierWrapperPass>();
149
4.02k
}
150
151
8
void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
152
8
  RI.print(OS);
153
8
}
154
155
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
156
LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
157
  RI.dump();
158
}
159
#endif
160
161
char RegionInfoPass::ID = 0;
162
163
73.2k
INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
164
73.2k
                "Detect single entry single exit regions", true, true)
165
73.2k
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
166
73.2k
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
167
73.2k
INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
168
73.2k
INITIALIZE_PASS_END(RegionInfoPass, "regions",
169
                "Detect single entry single exit regions", true, true)
170
171
// Create methods available outside of this file, to use them
172
// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
173
// the link time optimization.
174
175
namespace llvm {
176
177
0
  FunctionPass *createRegionInfoPass() {
178
0
    return new RegionInfoPass();
179
0
  }
180
181
} // end namespace llvm
182
183
//===----------------------------------------------------------------------===//
184
// RegionInfoAnalysis implementation
185
//
186
187
AnalysisKey RegionInfoAnalysis::Key;
188
189
29
RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
190
29
  RegionInfo RI;
191
29
  auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
192
29
  auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
193
29
  auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
194
29
195
29
  RI.recalculate(F, DT, PDT, DF);
196
29
  return RI;
197
29
}
198
199
RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
200
2
  : OS(OS) {}
201
202
PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
203
2
                                             FunctionAnalysisManager &AM) {
204
2
  OS << "Region Tree for function: " << F.getName() << "\n";
205
2
  AM.getResult<RegionInfoAnalysis>(F).print(OS);
206
2
207
2
  return PreservedAnalyses::all();
208
2
}
209
210
PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
211
0
                                              FunctionAnalysisManager &AM) {
212
0
  AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
213
0
214
0
  return PreservedAnalyses::all();
215
0
}