forked from facebook/SPARTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonotonicFixpointIteratorTest.cpp
362 lines (310 loc) · 12.6 KB
/
MonotonicFixpointIteratorTest.cpp
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "MonotonicFixpointIterator.h"
#include <functional>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <boost/functional/hash.hpp>
#include "HashedSetAbstractDomain.h"
using namespace sparta;
/*
* In order to test the fixpoint iterator, we implement a liveness analysis on a
* skeleton language. A statement simply contains the variables it defines and
* the variables it uses, which is all we need to perform liveness analysis.
*/
struct Statement {
Statement() = default;
Statement(std::initializer_list<std::string> use,
std::initializer_list<std::string> def)
: use(use), def(def) {}
std::vector<std::string> use;
std::vector<std::string> def;
};
struct ControlPoint {
std::string label;
explicit ControlPoint(const std::string& l) : label(l) {}
};
bool operator==(const ControlPoint& cp1, const ControlPoint& cp2) {
return cp1.label == cp2.label;
}
size_t hash_value(const ControlPoint& cp) {
boost::hash<std::string> hasher;
return hasher(cp.label);
}
/*
* A program is a control-flow graph where each node is labeled with a
* statement.
*/
class Program final {
public:
using Edge = std::pair<ControlPoint, ControlPoint>;
using EdgeId = std::shared_ptr<Edge>;
explicit Program(const std::string& entry) : m_entry(entry), m_exit(entry) {}
std::vector<EdgeId> successors(const ControlPoint& node) const {
auto& succs = m_successors.at(node);
return std::vector<EdgeId>(succs.begin(), succs.end());
}
std::vector<EdgeId> predecessors(const ControlPoint& node) const {
auto& preds = m_predecessors.at(node);
return std::vector<EdgeId>(preds.begin(), preds.end());
}
const Statement& statement_at(const ControlPoint& node) const {
auto it = m_statements.find(node);
if (it == m_statements.end()) {
fail(node);
}
return it->second;
}
void add(const std::string& node, const Statement& stmt) {
ControlPoint cp(node);
m_statements[cp] = stmt;
// Ensure that the pred/succ entries for the node are initialized
m_predecessors[cp];
m_successors[cp];
}
void add_edge(const std::string& src, const std::string& dst) {
ControlPoint src_cp(src);
ControlPoint dst_cp(dst);
auto edge = std::make_shared<Edge>(src_cp, dst_cp);
m_successors[src_cp].insert(edge);
m_predecessors[dst_cp].insert(edge);
}
void set_exit(const std::string& exit) { m_exit = ControlPoint(exit); }
private:
// In gtest, FAIL (or any ASSERT_* statement) can only be called from within a
// function that returns void.
void fail(const ControlPoint& node) const {
FAIL() << "No statement at node " << node.label;
}
ControlPoint m_entry;
ControlPoint m_exit;
std::unordered_map<ControlPoint, Statement, boost::hash<ControlPoint>>
m_statements;
std::unordered_map<ControlPoint,
std::unordered_set<EdgeId>,
boost::hash<ControlPoint>>
m_successors;
std::unordered_map<ControlPoint,
std::unordered_set<EdgeId>,
boost::hash<ControlPoint>>
m_predecessors;
friend class ProgramInterface;
};
class ProgramInterface {
public:
using Graph = Program;
using NodeId = ControlPoint;
using EdgeId = Program::EdgeId;
static NodeId entry(const Graph& graph) { return graph.m_entry; }
static NodeId exit(const Graph& graph) { return graph.m_exit; }
static std::vector<EdgeId> predecessors(const Graph& graph,
const NodeId& node) {
return graph.predecessors(node);
}
static std::vector<EdgeId> successors(const Graph& graph,
const NodeId& node) {
return graph.successors(node);
}
static NodeId source(const Graph&, const EdgeId& e) { return e->first; }
static NodeId target(const Graph&, const EdgeId& e) { return e->second; }
};
/*
* The abstract domain for liveness is just the powerset domain of variables.
*/
using LivenessDomain = HashedSetAbstractDomain<std::string>;
class FixpointEngine final
: public MonotonicFixpointIterator<
BackwardsFixpointIterationAdaptor<ProgramInterface>,
LivenessDomain,
boost::hash<ControlPoint>> {
public:
explicit FixpointEngine(const Program& program)
: MonotonicFixpointIterator(program), m_program(program) {}
void analyze_node(const ControlPoint& node,
LivenessDomain* current_state) const override {
const Statement& stmt = m_program.statement_at(node);
// This is the standard semantic definition of liveness.
current_state->remove(stmt.def.begin(), stmt.def.end());
current_state->add(stmt.use.begin(), stmt.use.end());
}
LivenessDomain analyze_edge(
const EdgeId&,
const LivenessDomain& exit_state_at_source) const override {
// Edges have no semantic transformers attached.
return exit_state_at_source;
}
LivenessDomain get_live_in_vars_at(const std::string& node) {
// Since we performed a backward analysis by reversing the control-flow
// graph, the set of live variables before executing a node is given by
// the exit state at the node.
return get_exit_state_at(ControlPoint(node));
}
LivenessDomain get_live_out_vars_at(const std::string& node) {
// Similarly, the set of live variables after executing a node is given by
// the entry state at the node.
return get_entry_state_at(ControlPoint(node));
}
private:
const Program& m_program;
};
class MonotonicFixpointIteratorTest : public ::testing::Test {
protected:
MonotonicFixpointIteratorTest() : m_program1("1"), m_program2("1") {}
virtual void SetUp() {
build_program1();
build_program2();
}
Program m_program1;
Program m_program2;
private:
/*
* live in live out
* 1: a = 0; {c} {a, c}
* 2: b = a + 1; {a, c} {b, c}
* 3: c = c + b; {b, c} {b, c}
* 4: a = b * 2; {b, c} {a, c}
* 5: if (a < 9) { {a, c} {a, c}
* goto 2;
* } else {
* 6: return c; {c} {}
* }
*/
void build_program1() {
m_program1.add("1", Statement(/* use: */ {}, /* def: */ {"a"}));
m_program1.add("2", Statement(/* use: */ {"a"}, /* def: */ {"b"}));
m_program1.add("3", Statement(/* use: */ {"c", "b"}, /* def: */ {"c"}));
m_program1.add("4", Statement(/* use: */ {"b"}, /* def: */ {"a"}));
m_program1.add("5", Statement(/* use: */ {"a"}, /* def: */ {}));
m_program1.add("6", Statement(/* use: */ {"c"}, /* def: */ {}));
m_program1.add_edge("1", "2");
m_program1.add_edge("2", "3");
m_program1.add_edge("3", "4");
m_program1.add_edge("4", "5");
m_program1.add_edge("5", "6");
m_program1.add_edge("5", "2");
m_program1.set_exit("6");
}
/*
* live in live out
* 1: x = a + b; {a, b} {x, a, b}
* 2: y = a * b; {x, a, b} {x, y, a, b}
* 3: if (y > a) { {x, y, a, b} {x, y, a, b}
* 4: return x; {x} {}
* }
* 5: a = a + 1; {y, a, b} {y, a, b}
* 6: x = a + b; {y, a, b} {x, y, a, b}
* if (...) {
* goto 7;
* }
* goto 3;
* 7: x = y + a;
*
*/
void build_program2() {
m_program2.add("1", Statement(/* use: */ {"a", "b"}, /* def: */ {"x"}));
m_program2.add("2", Statement(/* use: */ {"a", "b"}, /* def: */ {"y"}));
m_program2.add("3", Statement(/* use: */ {"y", "a"}, /* def: */ {}));
m_program2.add("4", Statement(/* use: */ {"x"}, /* def: */ {}));
m_program2.add("5", Statement(/* use: */ {"a"}, /* def: */ {"a"}));
m_program2.add("6", Statement(/* use: */ {"a", "b"}, /* def: */ {"x"}));
m_program2.add("7", Statement(/* use: */ {"y", "a"}, /* def: */ {"x"}));
m_program2.add_edge("1", "2");
m_program2.add_edge("2", "3");
m_program2.add_edge("3", "4");
m_program2.add_edge("3", "5");
m_program2.add_edge("5", "6");
m_program2.add_edge("6", "3");
m_program2.add_edge("6", "7");
m_program2.set_exit("4");
}
};
TEST_F(MonotonicFixpointIteratorTest, program1) {
using namespace std::placeholders;
FixpointEngine fp(this->m_program1);
fp.run(LivenessDomain());
ASSERT_TRUE(fp.get_live_in_vars_at("1").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("1").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("1").elements(),
::testing::UnorderedElementsAre("c"));
EXPECT_THAT(fp.get_live_out_vars_at("1").elements(),
::testing::UnorderedElementsAre("a", "c"));
ASSERT_TRUE(fp.get_live_in_vars_at("2").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("2").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("2").elements(),
::testing::UnorderedElementsAre("a", "c"));
EXPECT_THAT(fp.get_live_out_vars_at("2").elements(),
::testing::UnorderedElementsAre("b", "c"));
ASSERT_TRUE(fp.get_live_in_vars_at("3").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("3").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("3").elements(),
::testing::UnorderedElementsAre("b", "c"));
EXPECT_THAT(fp.get_live_out_vars_at("3").elements(),
::testing::UnorderedElementsAre("b", "c"));
ASSERT_TRUE(fp.get_live_in_vars_at("4").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("4").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("4").elements(),
::testing::UnorderedElementsAre("b", "c"));
EXPECT_THAT(fp.get_live_out_vars_at("4").elements(),
::testing::UnorderedElementsAre("a", "c"));
ASSERT_TRUE(fp.get_live_in_vars_at("5").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("5").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("5").elements(),
::testing::UnorderedElementsAre("a", "c"));
EXPECT_THAT(fp.get_live_out_vars_at("5").elements(),
::testing::UnorderedElementsAre("a", "c"));
ASSERT_TRUE(fp.get_live_in_vars_at("6").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("6").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("6").elements(),
::testing::UnorderedElementsAre("c"));
EXPECT_TRUE(fp.get_live_out_vars_at("6").elements().empty());
}
TEST_F(MonotonicFixpointIteratorTest, program2) {
using namespace std::placeholders;
FixpointEngine fp(this->m_program2);
fp.run(LivenessDomain());
ASSERT_TRUE(fp.get_live_in_vars_at("1").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("1").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("1").elements(),
::testing::UnorderedElementsAre("a", "b"));
EXPECT_THAT(fp.get_live_out_vars_at("1").elements(),
::testing::UnorderedElementsAre("x", "a", "b"));
ASSERT_TRUE(fp.get_live_in_vars_at("2").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("2").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("2").elements(),
::testing::UnorderedElementsAre("x", "a", "b"));
EXPECT_THAT(fp.get_live_out_vars_at("2").elements(),
::testing::UnorderedElementsAre("x", "y", "a", "b"));
ASSERT_TRUE(fp.get_live_in_vars_at("3").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("3").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("3").elements(),
::testing::UnorderedElementsAre("x", "y", "a", "b"));
EXPECT_THAT(fp.get_live_out_vars_at("3").elements(),
::testing::UnorderedElementsAre("x", "y", "a", "b"));
ASSERT_TRUE(fp.get_live_in_vars_at("4").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("4").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("4").elements(),
::testing::UnorderedElementsAre("x"));
EXPECT_TRUE(fp.get_live_out_vars_at("4").elements().empty());
ASSERT_TRUE(fp.get_live_in_vars_at("5").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("5").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("5").elements(),
::testing::UnorderedElementsAre("y", "a", "b"));
EXPECT_THAT(fp.get_live_out_vars_at("5").elements(),
::testing::UnorderedElementsAre("y", "a", "b"));
ASSERT_TRUE(fp.get_live_in_vars_at("6").is_value());
ASSERT_TRUE(fp.get_live_out_vars_at("6").is_value());
EXPECT_THAT(fp.get_live_in_vars_at("6").elements(),
::testing::UnorderedElementsAre("y", "a", "b"));
EXPECT_THAT(fp.get_live_out_vars_at("6").elements(),
::testing::UnorderedElementsAre("x", "y", "a", "b"));
ASSERT_TRUE(fp.get_live_in_vars_at("7").is_bottom());
ASSERT_TRUE(fp.get_live_out_vars_at("7").is_bottom());
}