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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/********************************************************************
* AUTHORS: Vijay Ganesh, David L. Dill
*
* BEGIN DATE: April, 2006
*
* LICENSE: Please view LICENSE file in the home dir of this Program
********************************************************************/
// -*- c++ -*-
// Simplifying create methods for Boolean operations.
// These are only very simple local simplifications.
// This is somewhat redundant with Vijay's simplifier code. They
// need to be merged.
// FIXME: control with optimize flag.
static bool _trace_simpbool = 0;
static bool _disable_simpbool = 0;
#include "AST.h"
// SMTLIB experimental hack. Try allocating a single stack here for
// children to reduce growing of vectors.
//BEEV::ASTVec child_stack;
namespace BEEV {
ASTNode BeevMgr::CreateSimpForm(Kind kind, ASTVec &children = _empty_ASTVec) {
if (_disable_simpbool) {
return CreateNode(kind, children);
}
else {
switch (kind) {
case NOT: return CreateSimpNot(children[0]); break;
case AND: return CreateSimpAndOr(1, children); break;
case OR: return CreateSimpAndOr(0, children); break;
case NAND: return CreateSimpNot(CreateSimpAndOr(1, children)); break;
case NOR: return CreateSimpNot(CreateSimpAndOr(0, children)); break;
case IFF: {
// Not sure children can ever be empty, but what the heck.
// if (children.size() == 0) {
// return ASTTrue;
// }
// Convert IFF to XOR ASAP. IFF is not associative, so this makes
// flattening much easier.
children[0] = CreateSimpNot(children[0]);
return CreateSimpXor(children); break;
}
case XOR:
return CreateSimpXor(children); break;
// FIXME: Earlier, check that this only has two arguments
case IMPLIES: return CreateSimpAndOr(0, CreateSimpNot(children[0]), children[1]); break;
case ITE: return CreateSimpFormITE(children[0], children[1], children[2]);
default: return CreateNode(kind, children);
}
}
}
// specialized versions
ASTNode BeevMgr::CreateSimpForm(Kind kind,
const ASTNode& child0) {
ASTVec children;
//child_stack.clear(); // could just reset top pointer.
children.push_back(child0);
//child_stack.push_back(child0);
return CreateSimpForm(kind, children);
//return CreateSimpForm(kind, child_stack);
}
ASTNode BeevMgr::CreateSimpForm(Kind kind,
const ASTNode& child0,
const ASTNode& child1) {
ASTVec children;
//child_stack.clear(); // could just reset top pointer.
children.push_back(child0);
//child_stack.push_back(child0);
children.push_back(child1);
//child_stack.push_back(child1);
return CreateSimpForm(kind, children);
//return CreateSimpForm(kind, child_stack);
}
ASTNode BeevMgr::CreateSimpForm(Kind kind,
const ASTNode& child0,
const ASTNode& child1,
const ASTNode& child2) {
ASTVec children;
//child_stack.clear(); // could just reset top pointer.
children.push_back(child0);
//child_stack.push_back(child0);
children.push_back(child1);
//child_stack.push_back(child1);
children.push_back(child2);
//child_stack.push_back(child2);
return CreateSimpForm(kind, children);
//return CreateSimpForm(kind, child_stack);
}
ASTNode BeevMgr::CreateSimpNot(const ASTNode& form) {
Kind k = form.GetKind();
switch (k) {
case FALSE: { return ASTTrue; }
case TRUE: { return ASTFalse; }
case NOT: { return form[0]; } // NOT NOT cancellation
case XOR: {
// Push negation down in this case.
// FIXME: Separate pre-pass to push negation down?
// CreateSimp should be local, and this isn't.
// It isn't memoized. Arg.
ASTVec children = form.GetChildren();
children[0] = CreateSimpNot(children[0]);
return CreateSimpXor(children);
}
default: { return CreateNode(NOT, form); }
}
}
// I don't think this is even called, since it called
// CreateSimpAndOr instead of CreateSimpXor until 1/9/07 with no
// ill effects. Calls seem to go to the version that takes a vector
// of children.
ASTNode BeevMgr::CreateSimpXor(const ASTNode& form1, const ASTNode& form2) {
ASTVec children;
children.push_back(form1);
children.push_back(form2);
return CreateSimpXor(children);
}
ASTNode BeevMgr::CreateSimpAndOr(bool IsAnd, const ASTNode& form1, const ASTNode& form2) {
ASTVec children;
children.push_back(form1);
children.push_back(form2);
return CreateSimpAndOr(IsAnd, children);
}
ASTNode BeevMgr::CreateSimpAndOr(bool IsAnd, ASTVec &children) {
if (_trace_simpbool) {
cout << "========" << endl << "CreateSimpAndOr " << (IsAnd ? "AND " : "OR ") ;
lpvec(children);
cout << endl;
}
ASTVec new_children;
// sort so that identical nodes occur in sequential runs, followed by
// their negations.
SortByExprNum(children);
ASTNode annihilator = (IsAnd ? ASTFalse : ASTTrue);
ASTNode identity = (IsAnd ? ASTTrue : ASTFalse);
ASTNode retval;
ASTVec::const_iterator it_end = children.end();
ASTVec::const_iterator next_it;
for(ASTVec::const_iterator it = children.begin(); it != it_end; it = next_it) {
next_it = it + 1;
bool nextexists = (next_it < it_end);
if (*it == annihilator) {
retval = annihilator;
if (_trace_simpbool) {
cout << "returns " << retval << endl;
}
return retval;
}
else if (*it == identity) {
// just drop it
}
else if (nextexists && (*next_it == *it)) {
// drop it
// cout << "Dropping [" << it->GetNodeNum() << "]" << endl;
}
else if (nextexists && (next_it->GetKind() == NOT) && ((*next_it)[0] == *it)) {
// form and negation -- return FALSE for AND, TRUE for OR.
retval = annihilator;
// cout << "X and/or NOT X" << endl;
if (_trace_simpbool) {
cout << "returns " << retval << endl;
}
return retval;
}
else {
// add to children
new_children.push_back(*it);
}
}
// If we get here, we saw no annihilators, and children should
// be only the non-True nodes.
if (new_children.size() < 2) {
if (0 == new_children.size()) {
retval = identity;
}
else {
// there is just one child
retval = new_children[0];
}
}
else {
// 2 or more children. Create a new node.
retval = CreateNode(IsAnd ? AND : OR, new_children);
}
if (_trace_simpbool) {
cout << "returns " << retval << endl;
}
return retval;
}
// Constant children are accumulated in "accumconst".
ASTNode BeevMgr::CreateSimpXor(ASTVec &children) {
if (_trace_simpbool) {
cout << "========" << endl
<< "CreateSimpXor ";
lpvec(children);
cout << endl;
}
// Change this not to init to children if flattening code is present.
// ASTVec flat_children = children; // empty vector
ASTVec flat_children; // empty vector
ASTVec::const_iterator it_end = children.end();
if (xor_flatten) {
bool fflag = 0; // ***Temp debugging
// Experimental flattening code.
for(ASTVec::iterator it = children.begin(); it != it_end; it++) {
Kind ck = it->GetKind();
const ASTVec &gchildren = it->GetChildren();
if (XOR == ck) {
fflag = 1;
// append grandchildren to children
flat_children.insert(flat_children.end(), gchildren.begin(), gchildren.end());
}
else {
flat_children.push_back(*it);
}
}
if (_trace_simpbool && fflag) {
cout << "========" << endl;
cout << "Flattening: " << endl;
lpvec(children);
cout << "--------" << endl;
cout << "Flattening result: " << endl;
lpvec(flat_children);
}
}
else {
flat_children = children;
}
// sort so that identical nodes occur in sequential runs, followed by
// their negations.
SortByExprNum(flat_children);
ASTNode retval;
// This is the C Boolean value of all constant args seen. It is initially
// 0. TRUE children cause it to change value.
bool accumconst = 0;
ASTVec new_children;
it_end = flat_children.end();
ASTVec::iterator next_it;
for(ASTVec::iterator it = flat_children.begin(); it != it_end; it++) {
next_it = it + 1;
bool nextexists = (next_it < it_end);
if (ASTTrue == *it) {
accumconst = !accumconst;
}
else if (ASTFalse == *it) {
// Ignore it
}
else if (nextexists && (*next_it == *it)) {
// x XOR x = FALSE. Skip current, write "false" into next_it
// so that it gets tossed, too.
*next_it = ASTFalse;
}
else if (nextexists && (next_it->GetKind() == NOT) && ((*next_it)[0] == *it)) {
// x XOR NOT x = TRUE. Skip current, write "true" into next_it
// so that it gets tossed, too.
*next_it = ASTTrue;
}
else if (NOT == it->GetKind()) {
// If child is (NOT alpha), we can flip accumconst and use alpha.
// This is ok because (NOT alpha) == TRUE XOR alpha
accumconst = !accumconst;
// CreateSimpNot just takes child of not.
new_children.push_back(CreateSimpNot(*it));
}
else {
new_children.push_back(*it);
}
}
// Children should be non-constant.
if (new_children.size() < 2) {
if (0 == new_children.size()) {
// XOR(TRUE, FALSE) -- accumconst will be 1.
if (accumconst) {
retval = ASTTrue;
}
else {
retval = ASTFalse;
}
}
else {
// there is just one child
// XOR(x, TRUE) -- accumconst will be 1.
if (accumconst) {
retval = CreateSimpNot(new_children[0]);
}
else {
retval = new_children[0];
}
}
}
else {
// negate first child if accumconst == 1
if (accumconst) {
new_children[0] = CreateSimpNot(new_children[0]);
}
retval = CreateNode(XOR, new_children);
}
if (_trace_simpbool) {
cout << "returns " << retval << endl;
}
return retval;
}
// FIXME: How do I know whether ITE is a formula or not?
ASTNode BeevMgr::CreateSimpFormITE(const ASTNode& child0,
const ASTNode& child1,
const ASTNode& child2) {
ASTNode retval;
if (_trace_simpbool) {
cout << "========" << endl << "CreateSimpFormITE "
<< child0
<< child1
<< child2 << endl;
}
if (ASTTrue == child0) {
retval = child1;
}
else if (ASTFalse == child0) {
retval = child2;
}
else if (child1 == child2) {
retval = child1;
}
// ITE(x, TRUE, y ) == x OR y
else if (ASTTrue == child1) {
retval = CreateSimpAndOr(0, child0, child2);
}
// ITE(x, FALSE, y ) == (!x AND y)
else if (ASTFalse == child1) {
retval = CreateSimpAndOr(1, CreateSimpNot(child0), child2);
}
// ITE(x, y, TRUE ) == (!x OR y)
else if (ASTTrue == child2) {
retval = CreateSimpAndOr(0, CreateSimpNot(child0), child1);
}
// ITE(x, y, FALSE ) == (x AND y)
else if (ASTFalse == child2) {
retval = CreateSimpAndOr(1, child0, child1);
}
// ITE (x, !y, y) == x XOR y
// else if (NOT == child1.GetKind() && (child1[0] == child2)) {
// retval = CreateSimpXor(child0, child2);
// }
// // ITE (x, y, !y) == x IFF y. I think other cases are covered
// // by XOR/IFF optimizations
// else if (NOT == child2.GetKind() && (child2[0] == child1)) {
// retval = CreateSimpXor(CreateSimpNot(child0), child2);
// }
else {
retval = CreateNode(ITE, child0, child1, child2);
}
if (_trace_simpbool) {
cout << "returns " << retval << endl;
}
return retval;
}
} // BEEV namespace
|