-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathterm_analysis.cpp
358 lines (316 loc) · 10.3 KB
/
term_analysis.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
/********************* */
/*! \file term_analysis.cpp
** \verbatim
** Top contributors (to current version):
** Makai Mann
** This file is part of the pono project.
** Copyright (c) 2019 by the authors listed in the file AUTHORS
** in the top-level source directory) and their institutional affiliations.
** All rights reserved. See the file LICENSE in the top-level source
** directory for licensing information.\endverbatim
**
** \brief Useful functions for term analysis.
**
**
**/
#include "assert.h"
#include "smt-switch/smt.h"
#include "smt-switch/utils.h"
#include "utils/exceptions.h"
using namespace smt;
using namespace std;
namespace pono {
// set of operators which cannot be predicates
// mostly boolean operators plus some special cases
// and the bit-vector versions for boolector
// boolean terms with these operators are not predicates
unordered_set<PrimOp> nonpred_ops({
And,
Or,
Xor,
Not,
Implies,
Ite,
// Note: also including bit-vector operators for solvers that
// alias bool and bv of size 1
// should not make a difference for solvers that don't
// alias, because it will check if it's a boolean first.
// so this is just to make this method work for all solvers
BVAnd,
BVOr,
BVXor,
BVNand,
BVNot,
Extract // otherwise boolector will count single-bit extracts
});
// helper functions
/** Helper function for get_combinations. Builds the output vector recursively
* @param options a sequence of non-empty vectors containing each option for
* this position
* @param output vector (should start empty) - gets incrementally populated
* with a flattened sequence of vectors with each option
* @param i the index this recursive function is currently working on
* NOTE: this is not the most performant implementation, but in practice we
* don't expect too many options so this is not expected to be a bottleneck
*/
void get_combinations_helper(const vector<TermVec> & options,
vector<TermVec> & out,
size_t i)
{
if (i == options.size()) {
return;
}
if (i == 0) {
if (!out.empty()) {
throw PonoException("Expecting empty vector");
}
// need to initialize the vector
for (auto c : options[i]) {
out.push_back({ c });
}
} else {
size_t orig_out_size = out.size();
for (size_t j = 0; j < orig_out_size; ++j) {
if (options[i].empty()) {
throw PonoException("Each element of options must be non-empty");
}
for (int k = options[i].size() - 1; k >= 0; --k) {
// each element of out should be the same length
// exactly i because that's how far into the options we've gotten
assert(out[j].size() == i);
if (k != 0) {
// copy the vector and add it to out with this option appended
TermVec outj = out[j];
outj.push_back(options[i][k]);
out.push_back(outj);
} else {
// for element 0, just add it to the existing vector instead of
// creating a new one NOTE: this is the last option we're appending so
// we don't need to keep the old
// version of this vector
out[j].push_back(options[i][k]);
}
}
}
}
// recursive call
get_combinations_helper(options, out, i + 1);
}
/** Generate all combinations of the terms in the vector
* e.g. if the input is
* [[v, w],
* [x],
* [y, z]
* ]
* then the output should be [[v, x, y], [v, x, z], [w, x, y], [w, x, z]]
* @param options a sequence of non-empty vectors containing each option for
* this position
* @param a flattened sequence of vectors with each option
*/
vector<TermVec> get_combinations(const vector<TermVec> & options)
{
vector<TermVec> out;
get_combinations_helper(options, out, 0);
return out;
}
// end helper functions
bool is_predicate(const Term & t, const Sort & boolsort, bool include_symbols)
{
if (t->get_sort() != boolsort) {
return false;
}
const Op & op = t->get_op();
if (include_symbols && t->is_symbolic_const()) {
return true;
} else if (op.is_null()) {
// cannot be a predicate with a null op unless including symbols
return false;
}
assert(!op.is_null());
if (nonpred_ops.find(op.prim_op) != nonpred_ops.end()) {
// boolean operators cannot make predicates
// also included extract because otherwise boolector
// would include single-bit extracts
return false;
}
TermVec children(t->begin(), t->end());
// boolean terms that do not use a boolean combination operator are
// predicates
// one special case is equality between two booleans is not a predicate
// this is an iff essentially
if (op.prim_op == Equal && (*t->begin())->get_sort() == boolsort) {
return false;
}
// if it made it through all the checks, then it's a predicate
return true;
}
UnorderedTermSet get_free_symbols(const Term & term)
{
UnorderedTermSet free_symbols;
get_free_symbols(term, free_symbols);
return free_symbols;
}
void get_leaves(const Term & term, UnorderedTermSet & leaves)
{
TermVec to_visit({ term });
UnorderedTermSet visited;
Term t;
while (!to_visit.empty()) {
t = to_visit.back();
to_visit.pop_back();
if (visited.find(t) != visited.end()) {
// cache hit
continue;
}
visited.insert(t);
for (const auto & tt : t) {
to_visit.push_back(tt);
}
if (t->get_op().is_null()) {
assert(t->is_symbol() || t->is_value());
leaves.insert(t);
}
}
}
void get_predicates(const SmtSolver & solver,
const Term & term,
UnorderedTermSet & out,
bool include_symbols,
bool search_subterms,
bool split_ites)
{
// NOTE: this is better than checking the SortKind of a sort
// some solvers alias sorts and might return a SortKind
// of BV (with width 1) for a boolean
// But, even those solvers will be consistent and a term
// t with boolean sort will satisfy
// t->get_sort() == boolsort
// even if
// t->get_sort()->get_sort_kind() != BOOL
Sort boolsort = solver->make_sort(BOOL);
TermVec to_visit({ term });
UnorderedTermSet visited;
Term t;
while (to_visit.size()) {
t = to_visit.back();
assert(t); // non-null term
to_visit.pop_back();
if (visited.find(t) == visited.end()) {
visited.insert(t);
TermVec children(t->begin(), t->end());
// later we will want to know which children are ITEs (if any)
unordered_set<size_t> ite_indices;
// add children to stack
Term c;
for (size_t i = 0; i < children.size(); ++i) {
c = children[i];
if (c->get_op() == Ite) {
ite_indices.insert(i);
}
}
if (!search_subterms && t->get_sort() != boolsort) {
// not a candidate for predicates
continue;
}
if (t->is_value()) {
// values are not predicates
continue;
}
bool is_pred = false;
// special case for ITE children
// Note: we're trying to never include an ITE in a predicate
// so if we get y = ite(x < 10, x+1, 0), we want to add
// y = x+1 and y = 0 as the predicates instead of the
// whole formula
if (split_ites && ite_indices.size()) {
vector<TermVec> options;
for (size_t i = 0; i < children.size(); ++i) {
if (ite_indices.find(i) != ite_indices.end()) {
TermVec ite_children(children[i]->begin(), children[i]->end());
assert(ite_children.size() == 3);
options.push_back({ ite_children[1], ite_children[2] });
// look for predicates in the ite condition
to_visit.push_back(ite_children[0]);
} else {
options.push_back({ children[i] });
}
}
assert(options.size() == children.size());
// generate all combinations of options
vector<TermVec> all_combinations = get_combinations(options);
// then rebuild for each TermVec of children
const Op & op = t->get_op();
Term res;
for (auto comb : all_combinations) {
// construct a new term with the given combination of children
assert(comb.size() == children.size());
res = solver->make_term(op, comb);
// add this term to the stack of terms to check for predicates
to_visit.push_back(res);
}
} else if (is_predicate(t, boolsort, include_symbols)) {
out.insert(t);
is_pred = true;
}
if (!is_pred || search_subterms) {
to_visit.insert(to_visit.end(), children.begin(), children.end());
}
}
}
}
TermVec remove_ites_under_model(const SmtSolver & solver, const TermVec & terms)
{
UnorderedTermSet visited;
UnorderedTermMap cache;
TermVec to_visit = terms;
Term solver_true = solver->make_term(true);
Term t;
while (to_visit.size()) {
t = to_visit.back();
to_visit.pop_back();
if (visited.find(t) == visited.end()) {
to_visit.push_back(t);
visited.insert(t);
for (const auto & tt : t) {
to_visit.push_back(tt);
}
} else {
// post-order case
TermVec cached_children;
for (const auto & tt : t) {
cached_children.push_back(tt);
}
Op op = t->get_op();
if (op == Ite) {
if (solver->get_value(cached_children[0]) == solver_true) {
// if case
cache[t] = cached_children[1];
} else {
// else case
cache[t] = cached_children[2];
}
} else if (cached_children.size()) {
// rebuild to take into account any changes
if (!op.is_null()) {
cache[t] = solver->make_term(op, cached_children);
} else {
assert(cached_children.size() == 1); // must be a constant array
assert(t->get_sort()->get_sort_kind() == ARRAY);
cache[t] = solver->make_term(cached_children[0], t->get_sort());
}
} else {
// just map to itself in the cache
// when there's no children
cache[t] = t;
}
assert(cache.find(t) != cache.end());
}
}
TermVec res;
res.reserve(terms.size());
for (const auto & tt : terms) {
res.push_back(cache.at(tt));
}
return res;
}
} // namespace pono