1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Dejan Jovanovic, Liana Hadarean, Clark Barrett |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* [[ Add one-line brief description here ]] |
14 |
|
* |
15 |
|
* [[ Add lengthier description here ]] |
16 |
|
* \todo document this file |
17 |
|
*/ |
18 |
|
|
19 |
|
#include "cvc5_private.h" |
20 |
|
|
21 |
|
#pragma once |
22 |
|
|
23 |
|
#include "theory/bv/theory_bv_rewrite_rules.h" |
24 |
|
#include "theory/bv/theory_bv_utils.h" |
25 |
|
|
26 |
|
namespace cvc5 { |
27 |
|
namespace theory { |
28 |
|
namespace bv { |
29 |
|
|
30 |
|
/* -------------------------------------------------------------------------- */ |
31 |
|
|
32 |
|
template<> inline |
33 |
339008 |
bool RewriteRule<ConcatFlatten>::applies(TNode node) { |
34 |
339008 |
return (node.getKind() == kind::BITVECTOR_CONCAT); |
35 |
|
} |
36 |
|
|
37 |
|
template<> inline |
38 |
169504 |
Node RewriteRule<ConcatFlatten>::apply(TNode node) { |
39 |
169504 |
Debug("bv-rewrite") << "RewriteRule<ConcatFlatten>(" << node << ")" << std::endl; |
40 |
339008 |
NodeBuilder result(kind::BITVECTOR_CONCAT); |
41 |
339008 |
std::vector<Node> processing_stack; |
42 |
169504 |
processing_stack.push_back(node); |
43 |
1650994 |
while (!processing_stack.empty()) { |
44 |
1481490 |
Node current = processing_stack.back(); |
45 |
740745 |
processing_stack.pop_back(); |
46 |
740745 |
if (current.getKind() == kind::BITVECTOR_CONCAT) { |
47 |
747708 |
for (int i = current.getNumChildren() - 1; i >= 0; i--) |
48 |
571241 |
processing_stack.push_back(current[i]); |
49 |
|
} else { |
50 |
564278 |
result << current; |
51 |
|
} |
52 |
|
} |
53 |
169504 |
Node resultNode = result; |
54 |
169504 |
Debug("bv-rewrite") << "RewriteRule<ConcatFlatten>(" << resultNode << ")" << std::endl; |
55 |
339008 |
return resultNode; |
56 |
|
} |
57 |
|
|
58 |
|
/* -------------------------------------------------------------------------- */ |
59 |
|
|
60 |
|
template<> inline |
61 |
339008 |
bool RewriteRule<ConcatExtractMerge>::applies(TNode node) { |
62 |
339008 |
return (node.getKind() == kind::BITVECTOR_CONCAT); |
63 |
|
} |
64 |
|
|
65 |
|
template<> inline |
66 |
169504 |
Node RewriteRule<ConcatExtractMerge>::apply(TNode node) { |
67 |
|
|
68 |
169504 |
Debug("bv-rewrite") << "RewriteRule<ConcatExtractMerge>(" << node << ")" << std::endl; |
69 |
|
|
70 |
339008 |
std::vector<Node> mergedExtracts; |
71 |
|
|
72 |
339008 |
Node current = node[0]; |
73 |
169504 |
bool mergeStarted = false; |
74 |
169504 |
unsigned currentHigh = 0; |
75 |
169504 |
unsigned currentLow = 0; |
76 |
|
|
77 |
564278 |
for(size_t i = 1, end = node.getNumChildren(); i < end; ++ i) { |
78 |
|
// The next candidate for merging |
79 |
467261 |
Node next = node[i]; |
80 |
|
// If the current is not an extract we just go to the next |
81 |
717061 |
if (current.getKind() != kind::BITVECTOR_EXTRACT) { |
82 |
322287 |
mergedExtracts.push_back(current); |
83 |
322287 |
current = next; |
84 |
322287 |
continue; |
85 |
|
} |
86 |
|
// If it is an extract and the first one, get the extract parameters |
87 |
72487 |
else if (!mergeStarted) { |
88 |
72454 |
currentHigh = utils::getExtractHigh(current); |
89 |
72454 |
currentLow = utils::getExtractLow(current); |
90 |
|
} |
91 |
|
|
92 |
|
// If the next one can be merged, try to merge |
93 |
72487 |
bool merged = false; |
94 |
72487 |
if (next.getKind() == kind::BITVECTOR_EXTRACT && current[0] == next[0]) { |
95 |
|
// x[i : j] @ x[j - 1 : k] -> c x[i : k] |
96 |
25425 |
unsigned nextHigh = utils::getExtractHigh(next); |
97 |
25425 |
unsigned nextLow = utils::getExtractLow(next); |
98 |
25425 |
if(nextHigh + 1 == currentLow) { |
99 |
116 |
currentLow = nextLow; |
100 |
116 |
mergeStarted = true; |
101 |
116 |
merged = true; |
102 |
|
} |
103 |
|
} |
104 |
|
// If we haven't merged anything, add the previous merge and continue with the next |
105 |
72487 |
if (!merged) { |
106 |
72371 |
if (!mergeStarted) mergedExtracts.push_back(current); |
107 |
9 |
else mergedExtracts.push_back(utils::mkExtract(current[0], currentHigh, currentLow)); |
108 |
72371 |
current = next; |
109 |
72371 |
mergeStarted = false; |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
// Add the last child |
114 |
169504 |
if (!mergeStarted) mergedExtracts.push_back(current); |
115 |
83 |
else mergedExtracts.push_back(utils::mkExtract(current[0], currentHigh, currentLow)); |
116 |
|
|
117 |
|
// Return the result |
118 |
339008 |
return utils::mkConcat(mergedExtracts); |
119 |
|
} |
120 |
|
|
121 |
|
/* -------------------------------------------------------------------------- */ |
122 |
|
|
123 |
|
template<> inline |
124 |
338951 |
bool RewriteRule<ConcatConstantMerge>::applies(TNode node) { |
125 |
338951 |
return node.getKind() == kind::BITVECTOR_CONCAT; |
126 |
|
} |
127 |
|
|
128 |
|
template<> inline |
129 |
169447 |
Node RewriteRule<ConcatConstantMerge>::apply(TNode node) { |
130 |
|
|
131 |
169447 |
Debug("bv-rewrite") << "RewriteRule<ConcatConstantMerge>(" << node << ")" << std::endl; |
132 |
|
|
133 |
338894 |
std::vector<Node> mergedConstants; |
134 |
697974 |
for (unsigned i = 0, end = node.getNumChildren(); i < end;) { |
135 |
528527 |
if (node[i].getKind() != kind::CONST_BITVECTOR) { |
136 |
|
// If not a constant, just add it |
137 |
387873 |
mergedConstants.push_back(node[i]); |
138 |
387873 |
++i; |
139 |
|
} else { |
140 |
|
// Find the longest sequence of constants |
141 |
140654 |
unsigned j = i + 1; |
142 |
211810 |
while (j < end) { |
143 |
117184 |
if (node[j].getKind() != kind::CONST_BITVECTOR) { |
144 |
81606 |
break; |
145 |
|
} else { |
146 |
35578 |
++ j; |
147 |
|
} |
148 |
|
} |
149 |
|
// Append all the constants |
150 |
281308 |
BitVector current = node[i].getConst<BitVector>(); |
151 |
176232 |
for (unsigned k = i + 1; k < j; ++ k) { |
152 |
35578 |
current = current.concat(node[k].getConst<BitVector>()); |
153 |
|
} |
154 |
|
// Add the new merged constant |
155 |
140654 |
mergedConstants.push_back(utils::mkConst(current)); |
156 |
140654 |
i = j; |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
169447 |
Debug("bv-rewrite") << "RewriteRule<ConcatConstantMerge>(" << node << ") => " << utils::mkConcat(mergedConstants) << std::endl; |
161 |
|
|
162 |
338894 |
return utils::mkConcat(mergedConstants); |
163 |
|
} |
164 |
|
|
165 |
|
/* -------------------------------------------------------------------------- */ |
166 |
|
|
167 |
|
template<> inline |
168 |
637890 |
bool RewriteRule<ExtractWhole>::applies(TNode node) { |
169 |
637890 |
if (node.getKind() != kind::BITVECTOR_EXTRACT) return false; |
170 |
184501 |
unsigned length = utils::getSize(node[0]); |
171 |
184501 |
unsigned extractHigh = utils::getExtractHigh(node); |
172 |
184501 |
if (extractHigh != length - 1) return false; |
173 |
89811 |
unsigned extractLow = utils::getExtractLow(node); |
174 |
89811 |
if (extractLow != 0) return false; |
175 |
21124 |
return true; |
176 |
|
} |
177 |
|
|
178 |
|
template<> inline |
179 |
16085 |
Node RewriteRule<ExtractWhole>::apply(TNode node) { |
180 |
16085 |
Debug("bv-rewrite") << "RewriteRule<ExtractWhole>(" << node << ")" << std::endl; |
181 |
16085 |
return node[0]; |
182 |
|
} |
183 |
|
|
184 |
|
/* -------------------------------------------------------------------------- */ |
185 |
|
|
186 |
|
template<> inline |
187 |
128691 |
bool RewriteRule<ExtractConstant>::applies(TNode node) { |
188 |
128691 |
if (node.getKind() != kind::BITVECTOR_EXTRACT) return false; |
189 |
128691 |
if (node[0].getKind() != kind::CONST_BITVECTOR) return false; |
190 |
48848 |
return true; |
191 |
|
} |
192 |
|
|
193 |
|
template<> inline |
194 |
24424 |
Node RewriteRule<ExtractConstant>::apply(TNode node) { |
195 |
24424 |
Debug("bv-rewrite") << "RewriteRule<ExtractConstant>(" << node << ")" << std::endl; |
196 |
48848 |
Node child = node[0]; |
197 |
48848 |
BitVector childValue = child.getConst<BitVector>(); |
198 |
48848 |
return utils::mkConst(childValue.extract(utils::getExtractHigh(node), utils::getExtractLow(node))); |
199 |
|
} |
200 |
|
|
201 |
|
/* -------------------------------------------------------------------------- */ |
202 |
|
|
203 |
|
template<> inline |
204 |
128578 |
bool RewriteRule<ExtractConcat>::applies(TNode node) { |
205 |
|
//Debug("bv-rewrite") << "RewriteRule<ExtractConcat>(" << node << ")" << std::endl; |
206 |
128578 |
if (node.getKind() != kind::BITVECTOR_EXTRACT) return false; |
207 |
128578 |
if (node[0].getKind() != kind::BITVECTOR_CONCAT) return false; |
208 |
22096 |
return true; |
209 |
|
} |
210 |
|
|
211 |
|
template<> inline |
212 |
11048 |
Node RewriteRule<ExtractConcat>::apply(TNode node) { |
213 |
11048 |
Debug("bv-rewrite") << "RewriteRule<ExtractConcat>(" << node << ")" << std::endl; |
214 |
11048 |
int extract_high = utils::getExtractHigh(node); |
215 |
11048 |
int extract_low = utils::getExtractLow(node); |
216 |
|
|
217 |
22096 |
std::vector<Node> resultChildren; |
218 |
|
|
219 |
22096 |
Node concat = node[0]; |
220 |
35744 |
for (int i = concat.getNumChildren() - 1; i >= 0 && extract_high >= 0; i--) { |
221 |
49392 |
Node concatChild = concat[i]; |
222 |
24696 |
int concatChildSize = utils::getSize(concatChild); |
223 |
24696 |
if (extract_low < concatChildSize) { |
224 |
21251 |
int extract_start = extract_low < 0 ? 0 : extract_low; |
225 |
21251 |
int extract_end = extract_high < concatChildSize ? extract_high : concatChildSize - 1; |
226 |
21251 |
resultChildren.push_back(utils::mkExtract(concatChild, extract_end, extract_start)); |
227 |
|
} |
228 |
24696 |
extract_low -= concatChildSize; |
229 |
24696 |
extract_high -= concatChildSize; |
230 |
|
} |
231 |
|
|
232 |
11048 |
std::reverse(resultChildren.begin(), resultChildren.end()); |
233 |
|
|
234 |
22096 |
return utils::mkConcat(resultChildren); |
235 |
|
} |
236 |
|
|
237 |
|
/* -------------------------------------------------------------------------- */ |
238 |
|
|
239 |
|
template<> inline |
240 |
106423 |
bool RewriteRule<ExtractExtract>::applies(TNode node) { |
241 |
106423 |
if (node.getKind() != kind::BITVECTOR_EXTRACT) return false; |
242 |
81999 |
if (node[0].getKind() != kind::BITVECTOR_EXTRACT) return false; |
243 |
4312 |
return true; |
244 |
|
} |
245 |
|
|
246 |
|
template<> inline |
247 |
2156 |
Node RewriteRule<ExtractExtract>::apply(TNode node) { |
248 |
2156 |
Debug("bv-rewrite") << "RewriteRule<ExtractExtract>(" << node << ")" << std::endl; |
249 |
|
|
250 |
|
// x[i:j][k:l] ~> x[k+j:l+j] |
251 |
4312 |
Node child = node[0]; |
252 |
2156 |
unsigned k = utils::getExtractHigh(node); |
253 |
2156 |
unsigned l = utils::getExtractLow(node); |
254 |
2156 |
unsigned j = utils::getExtractLow(child); |
255 |
|
|
256 |
2156 |
Node result = utils::mkExtract(child[0], k + j, l + j); |
257 |
4312 |
return result; |
258 |
|
} |
259 |
|
|
260 |
|
/* -------------------------------------------------------------------------- */ |
261 |
|
|
262 |
|
template<> inline |
263 |
416364 |
bool RewriteRule<FailEq>::applies(TNode node) { |
264 |
|
//Debug("bv-rewrite") << "RewriteRule<FailEq>(" << node << ")" << std::endl; |
265 |
416364 |
if (node.getKind() != kind::EQUAL) return false; |
266 |
416364 |
if (node[0].getKind() != kind::CONST_BITVECTOR) return false; |
267 |
68165 |
if (node[1].getKind() != kind::CONST_BITVECTOR) return false; |
268 |
50522 |
return node[0] != node[1]; |
269 |
|
} |
270 |
|
|
271 |
|
template<> inline |
272 |
24080 |
Node RewriteRule<FailEq>::apply(TNode node) { |
273 |
24080 |
return utils::mkFalse(); |
274 |
|
} |
275 |
|
|
276 |
|
/* -------------------------------------------------------------------------- */ |
277 |
|
|
278 |
|
template<> inline |
279 |
399034 |
bool RewriteRule<SimplifyEq>::applies(TNode node) { |
280 |
399034 |
if (node.getKind() != kind::EQUAL) return false; |
281 |
374954 |
return node[0] == node[1]; |
282 |
|
} |
283 |
|
|
284 |
|
template<> inline |
285 |
6750 |
Node RewriteRule<SimplifyEq>::apply(TNode node) { |
286 |
6750 |
Debug("bv-rewrite") << "RewriteRule<SimplifyEq>(" << node << ")" << std::endl; |
287 |
6750 |
return utils::mkTrue(); |
288 |
|
} |
289 |
|
|
290 |
|
/* -------------------------------------------------------------------------- */ |
291 |
|
|
292 |
|
template<> inline |
293 |
433798 |
bool RewriteRule<ReflexivityEq>::applies(TNode node) { |
294 |
433798 |
return (node.getKind() == kind::EQUAL && node[0] < node[1]); |
295 |
|
} |
296 |
|
|
297 |
|
template<> inline |
298 |
41514 |
Node RewriteRule<ReflexivityEq>::apply(TNode node) { |
299 |
41514 |
Debug("bv-rewrite") << "RewriteRule<ReflexivityEq>(" << node << ")" << std::endl; |
300 |
41514 |
Node res = node[1].eqNode(node[0]); |
301 |
41514 |
return res; |
302 |
|
} |
303 |
|
|
304 |
|
} |
305 |
|
} |
306 |
|
} // namespace cvc5 |