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
/*++
Copyright (c) 2017 Arie Gurfinkel
Module Name:
factor_equivs.cpp
Abstract:
Factor equivalence classes out of an expression.
"Equivalence class structure" for objs. Uses a union_find structure internally.
Operations are :
-Declare a new equivalence class with a single element
-Merge two equivalence classes
-Retrieve whether two elements are in the same equivalence class
-Iterate on all the elements of the equivalence class of a given element
-Iterate on all equivalence classes (and then within them)
Author:
Julien Braine
Arie Gurfinkel
Revision History:
*/
#include "ast/arith_decl_plugin.h"
#include "ast/for_each_expr.h"
#include "ast/ast_pp.h"
#include "ast/rewriter/expr_safe_replace.h"
#include "ast/rewriter/factor_equivs.h"
/**
Factors input vector v into equivalence classes and the rest
*/
void factor_eqs(expr_ref_vector &v, expr_equiv_class &equiv) {
ast_manager &m = v.get_manager();
arith_util arith(m);
expr *e1 = nullptr, *e2 = nullptr;
flatten_and(v);
unsigned j = 0;
for (unsigned i = 0; i < v.size(); ++i) {
if (m.is_eq(v.get(i), e1, e2)) {
if (arith.is_zero(e1)) {
std::swap(e1, e2);
}
// y + -1*x == 0
expr* a0 = nullptr, *a1 = nullptr, *x = nullptr;
if (arith.is_zero(e2) && arith.is_add(e1, a0, a1)) {
if (arith.is_times_minus_one(a1, x)) {
e1 = a0;
e2 = x;
}
else if (arith.is_times_minus_one(a0, x)) {
e1 = a1;
e2 = x;
}
}
equiv.merge(e1, e2);
}
else {
if (j < i) {
v[j] = v.get(i);
}
j++;
}
}
v.shrink(j);
}
/**
* Chooses a representative of an equivalence class
*/
expr *choose_rep(expr_equiv_class::eq_class &clazz, ast_manager &m) {
expr *rep = nullptr;
unsigned rep_sz = 0, elem_sz;
for (expr *elem : clazz) {
if (!m.is_value(elem)) {
elem_sz = get_num_exprs(elem);
if (!rep || (rep && rep_sz > elem_sz)) {
rep = elem;
rep_sz = elem_sz;
}
}
}
TRACE(equiv,
tout << "Rep: " << mk_pp(rep, m) << "\n";
for (expr *el : clazz)
tout << mk_pp(el, m) << "\n";
tout << "RepEnd\n";);
return rep;
}
void rewrite_eqs (expr_ref_vector &v, expr_equiv_class &equiv) {
ast_manager &m = v.m();
expr_safe_replace sub(m);
for (auto eq_class : equiv) {
expr *rep = choose_rep(eq_class, m);
for (expr *el : eq_class) {
if (el != rep) {
sub.insert (el, rep);
}
}
}
sub(v);
}
/**
* converts equivalence classes to equalities
*/
void equiv_to_expr(expr_equiv_class &equiv, expr_ref_vector &out) {
ast_manager &m = out.get_manager();
for (auto eq_class : equiv) {
expr *rep = choose_rep(eq_class, m);
SASSERT(rep);
for (expr *elem : eq_class) {
if (rep != elem) {
out.push_back (m.mk_eq (rep, elem));
}
}
}
}
/**
* expands equivalence classes to all derivable equalities
*/
bool equiv_to_expr_full(expr_equiv_class &equiv, expr_ref_vector &out) {
ast_manager &m = out.get_manager();
bool dirty = false;
for (auto eq_class : equiv) {
for (auto a = eq_class.begin(), end = eq_class.end(); a != end; ++a) {
expr_equiv_class::iterator b(a);
for (++b; b != end; ++b) {
out.push_back(m.mk_eq(*a, *b));
dirty = true;
}
}
}
return dirty;
}