CFGBuilder.cpp
9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//===- llvm/Testing/Support/CFGBuilder.cpp --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "CFGBuilder.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#define DEBUG_TYPE "cfg-builder"
using namespace llvm;
CFGHolder::CFGHolder(StringRef ModuleName, StringRef FunctionName)
: Context(std::make_unique<LLVMContext>()),
M(std::make_unique<Module>(ModuleName, *Context)) {
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Context), {}, false);
F = Function::Create(FTy, Function::ExternalLinkage, FunctionName, M.get());
}
CFGHolder::~CFGHolder() = default;
CFGBuilder::CFGBuilder(Function *F, const std::vector<Arc> &InitialArcs,
std::vector<Update> Updates)
: F(F), Updates(std::move(Updates)) {
assert(F);
buildCFG(InitialArcs);
}
static void ConnectBlocks(BasicBlock *From, BasicBlock *To) {
LLVM_DEBUG(dbgs() << "Creating BB arc " << From->getName() << " -> "
<< To->getName() << "\n";
dbgs().flush());
auto *IntTy = IntegerType::get(From->getContext(), 32);
if (isa<UnreachableInst>(From->getTerminator()))
From->getTerminator()->eraseFromParent();
if (!From->getTerminator()) {
IRBuilder<> IRB(From);
IRB.CreateSwitch(ConstantInt::get(IntTy, 0), To);
return;
}
SwitchInst *SI = cast<SwitchInst>(From->getTerminator());
const auto Last = SI->getNumCases();
auto *IntVal = ConstantInt::get(IntTy, Last);
SI->addCase(IntVal, To);
}
static void DisconnectBlocks(BasicBlock *From, BasicBlock *To) {
LLVM_DEBUG(dbgs() << "Deleting BB arc " << From->getName() << " -> "
<< To->getName() << "\n";
dbgs().flush());
SwitchInst *SI = cast<SwitchInst>(From->getTerminator());
if (SI->getNumCases() == 0) {
SI->eraseFromParent();
IRBuilder<> IRB(From);
IRB.CreateUnreachable();
return;
}
if (SI->getDefaultDest() == To) {
auto FirstC = SI->case_begin();
SI->setDefaultDest(FirstC->getCaseSuccessor());
SI->removeCase(FirstC);
return;
}
for (auto CIt = SI->case_begin(); CIt != SI->case_end(); ++CIt)
if (CIt->getCaseSuccessor() == To) {
SI->removeCase(CIt);
return;
}
}
BasicBlock *CFGBuilder::getOrAddBlock(StringRef BlockName) {
auto BIt = NameToBlock.find(BlockName);
if (BIt != NameToBlock.end())
return BIt->second;
auto *BB = BasicBlock::Create(F->getParent()->getContext(), BlockName, F);
IRBuilder<> IRB(BB);
IRB.CreateUnreachable();
NameToBlock[BlockName] = BB;
return BB;
}
bool CFGBuilder::connect(const Arc &A) {
BasicBlock *From = getOrAddBlock(A.From);
BasicBlock *To = getOrAddBlock(A.To);
if (Arcs.count(A) != 0)
return false;
Arcs.insert(A);
ConnectBlocks(From, To);
return true;
}
bool CFGBuilder::disconnect(const Arc &A) {
assert(NameToBlock.count(A.From) != 0 && "No block to disconnect (From)");
assert(NameToBlock.count(A.To) != 0 && "No block to disconnect (To)");
if (Arcs.count(A) == 0)
return false;
BasicBlock *From = getOrAddBlock(A.From);
BasicBlock *To = getOrAddBlock(A.To);
Arcs.erase(A);
DisconnectBlocks(From, To);
return true;
}
void CFGBuilder::buildCFG(const std::vector<Arc> &NewArcs) {
for (const auto &A : NewArcs) {
const bool Connected = connect(A);
(void)Connected;
assert(Connected);
}
}
Optional<CFGBuilder::Update> CFGBuilder::getNextUpdate() const {
if (UpdateIdx == Updates.size())
return None;
return Updates[UpdateIdx];
}
Optional<CFGBuilder::Update> CFGBuilder::applyUpdate() {
if (UpdateIdx == Updates.size())
return None;
Update NextUpdate = Updates[UpdateIdx++];
if (NextUpdate.Action == ActionKind::Insert)
connect(NextUpdate.Edge);
else
disconnect(NextUpdate.Edge);
return NextUpdate;
}
void CFGBuilder::dump(raw_ostream &OS) const {
OS << "Arcs:\n";
size_t i = 0;
for (const auto &A : Arcs)
OS << " " << i++ << ":\t" << A.From << " -> " << A.To << "\n";
OS << "Updates:\n";
i = 0;
for (const auto &U : Updates) {
OS << (i + 1 == UpdateIdx ? "->" : " ") << i
<< ((U.Action == ActionKind::Insert) ? "\tIns " : "\tDel ")
<< U.Edge.From << " -> " << U.Edge.To << "\n";
++i;
}
}
//---- CFGBuilder tests ---------------------------------------------------===//
TEST(CFGBuilder, Construction) {
CFGHolder Holder;
std::vector<CFGBuilder::Arc> Arcs = {{"entry", "a"}, {"a", "b"}, {"a", "c"},
{"c", "d"}, {"d", "b"}, {"d", "e"},
{"d", "f"}, {"e", "f"}};
CFGBuilder B(Holder.F, Arcs, {});
EXPECT_TRUE(B.getOrAddBlock("entry") == &Holder.F->getEntryBlock());
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("entry")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("a")->getTerminator()));
EXPECT_TRUE(isa<UnreachableInst>(B.getOrAddBlock("b")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("d")->getTerminator()));
auto *DSwitch = cast<SwitchInst>(B.getOrAddBlock("d")->getTerminator());
// d has 3 successors, but one of them if going to be a default case
EXPECT_EQ(DSwitch->getNumCases(), 2U);
EXPECT_FALSE(B.getNextUpdate()); // No updates to apply.
}
TEST(CFGBuilder, Insertions) {
CFGHolder Holder;
const auto Insert = CFGBuilder::ActionKind::Insert;
std::vector<CFGBuilder::Update> Updates = {
{Insert, {"entry", "a"}}, {Insert, {"a", "b"}}, {Insert, {"a", "c"}},
{Insert, {"c", "d"}}, {Insert, {"d", "b"}}, {Insert, {"d", "e"}},
{Insert, {"d", "f"}}, {Insert, {"e", "f"}}};
const size_t NumUpdates = Updates.size();
CFGBuilder B(Holder.F, {}, Updates);
size_t i = 0;
while (B.applyUpdate())
++i;
EXPECT_EQ(i, NumUpdates);
EXPECT_TRUE(B.getOrAddBlock("entry") == &Holder.F->getEntryBlock());
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("entry")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("a")->getTerminator()));
EXPECT_TRUE(isa<UnreachableInst>(B.getOrAddBlock("b")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("d")->getTerminator()));
auto *DSwitch = cast<SwitchInst>(B.getOrAddBlock("d")->getTerminator());
// d has 3 successors, but one of them if going to be a default case
EXPECT_EQ(DSwitch->getNumCases(), 2U);
EXPECT_FALSE(B.getNextUpdate()); // No updates to apply.
}
TEST(CFGBuilder, Deletions) {
CFGHolder Holder;
std::vector<CFGBuilder::Arc> Arcs = {
{"entry", "a"}, {"a", "b"}, {"a", "c"}, {"c", "d"}, {"d", "b"}};
const auto Delete = CFGBuilder::ActionKind::Delete;
std::vector<CFGBuilder::Update> Updates = {
{Delete, {"c", "d"}}, {Delete, {"a", "c"}}, {Delete, {"entry", "a"}},
};
const size_t NumUpdates = Updates.size();
CFGBuilder B(Holder.F, Arcs, Updates);
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("entry")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("a")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("c")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("d")->getTerminator()));
auto UpdateC = B.applyUpdate();
EXPECT_TRUE(UpdateC);
EXPECT_EQ(UpdateC->Action, CFGBuilder::ActionKind::Delete);
EXPECT_EQ(UpdateC->Edge.From, "c");
EXPECT_EQ(UpdateC->Edge.To, "d");
EXPECT_TRUE(isa<UnreachableInst>(B.getOrAddBlock("c")->getTerminator()));
size_t i = 1;
while (B.applyUpdate())
++i;
EXPECT_EQ(i, NumUpdates);
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("a")->getTerminator()));
EXPECT_TRUE(isa<UnreachableInst>(B.getOrAddBlock("entry")->getTerminator()));
}
TEST(CFGBuilder, Rebuild) {
CFGHolder Holder;
std::vector<CFGBuilder::Arc> Arcs = {
{"entry", "a"}, {"a", "b"}, {"a", "c"}, {"c", "d"}, {"d", "b"}};
const auto Insert = CFGBuilder::ActionKind::Insert;
const auto Delete = CFGBuilder::ActionKind::Delete;
std::vector<CFGBuilder::Update> Updates = {
{Delete, {"c", "d"}}, {Delete, {"a", "c"}}, {Delete, {"entry", "a"}},
{Insert, {"c", "d"}}, {Insert, {"a", "c"}}, {Insert, {"entry", "a"}},
};
const size_t NumUpdates = Updates.size();
CFGBuilder B(Holder.F, Arcs, Updates);
size_t i = 0;
while (B.applyUpdate())
++i;
EXPECT_EQ(i, NumUpdates);
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("entry")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("a")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("c")->getTerminator()));
EXPECT_TRUE(isa<SwitchInst>(B.getOrAddBlock("d")->getTerminator()));
}
static_assert(is_trivially_copyable<succ_iterator>::value,
"trivially copyable");
static_assert(is_trivially_copyable<succ_const_iterator>::value,
"trivially copyable");
static_assert(is_trivially_copyable<succ_range>::value, "trivially copyable");
static_assert(is_trivially_copyable<succ_const_range>::value,
"trivially copyable");