3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "polarityassignment.h"
14 #include "decomposeordertransform.h"
15 #include "autotuner.h"
18 #include "orderresolver.h"
19 #include "integerencoding.h"
21 #include "preprocess.h"
22 #include "serializer.h"
23 #include "deserializer.h"
26 boolTrue(BooleanEdge(new BooleanConst(true))),
27 boolFalse(boolTrue.negate()),
32 satEncoder = new SATEncoder(this);
35 /** This function tears down the solver and the entire AST */
38 uint size = allBooleans.getSize();
39 for (uint i = 0; i < size; i++) {
40 delete allBooleans.get(i);
43 size = allSets.getSize();
44 for (uint i = 0; i < size; i++) {
45 delete allSets.get(i);
48 size = allElements.getSize();
49 for (uint i = 0; i < size; i++) {
50 delete allElements.get(i);
53 size = allTables.getSize();
54 for (uint i = 0; i < size; i++) {
55 delete allTables.get(i);
58 size = allPredicates.getSize();
59 for (uint i = 0; i < size; i++) {
60 delete allPredicates.get(i);
63 size = allOrders.getSize();
64 for (uint i = 0; i < size; i++) {
65 delete allOrders.get(i);
68 size = allFunctions.getSize();
69 for (uint i = 0; i < size; i++) {
70 delete allFunctions.get(i);
73 delete boolTrue.getBoolean();
77 CSolver *CSolver::clone() {
78 CSolver *copy = new CSolver();
80 SetIteratorBooleanEdge *it = getConstraints();
81 while (it->hasNext()) {
82 BooleanEdge b = it->next();
83 copy->addConstraint(cloneEdge(copy, &map, b));
89 void CSolver::serialize() {
90 model_print("serializing ...\n");
92 Serializer serializer("dump");
93 SetIteratorBooleanEdge *it = getConstraints();
94 while (it->hasNext()) {
95 BooleanEdge b = it->next();
96 serializeBooleanEdge(&serializer, b);
100 model_print("deserializing ...\n");
102 Deserializer deserializer("dump");
103 deserializer.deserialize();
108 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
109 Set *set = new Set(type, elements, numelements);
114 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
115 Set *set = new Set(type, lowrange, highrange);
120 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
121 Set *s = createRangeSet(type, lowrange, highrange);
122 return getElementVar(s);
125 MutableSet *CSolver::createMutableSet(VarType type) {
126 MutableSet *set = new MutableSet(type);
131 void CSolver::addItem(MutableSet *set, uint64_t element) {
132 set->addElementMSet(element);
135 uint64_t CSolver::createUniqueItem(MutableSet *set) {
136 uint64_t element = set->getNewUniqueItem();
137 set->addElementMSet(element);
141 void CSolver::finalizeMutableSet(MutableSet* set){
145 Element *CSolver::getElementVar(Set *set) {
146 Element *element = new ElementSet(set);
147 allElements.push(element);
151 Element *CSolver::getElementConst(VarType type, uint64_t value) {
152 uint64_t array[] = {value};
153 Set *set = new Set(type, array, 1);
154 Element *element = new ElementConst(value, set);
155 Element *e = elemMap.get(element);
158 allElements.push(element);
159 elemMap.put(element, element);
168 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
169 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
170 Element *e = elemMap.get(element);
172 element->updateParents();
173 allElements.push(element);
174 elemMap.put(element, element);
182 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
183 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
184 allFunctions.push(function);
188 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
189 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
190 allPredicates.push(predicate);
194 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
195 Predicate *predicate = new PredicateTable(table, behavior);
196 allPredicates.push(predicate);
200 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
201 Table *table = new Table(domains,numDomain,range);
202 allTables.push(table);
206 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
207 return createTable(domains, numDomain, NULL);
210 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
211 table->addNewTableEntry(inputs, inputSize, result);
214 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
215 Function *function = new FunctionTable(table, behavior);
216 allFunctions.push(function);
220 BooleanEdge CSolver::getBooleanVar(VarType type) {
221 Boolean *boolean = new BooleanVar(type);
222 allBooleans.push(boolean);
223 return BooleanEdge(boolean);
226 BooleanEdge CSolver::getBooleanTrue() {
230 BooleanEdge CSolver::getBooleanFalse() {
234 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
235 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
238 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
239 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
240 Boolean *b = boolMap.get(boolean);
242 boolean->updateParents();
243 boolMap.put(boolean, boolean);
244 allBooleans.push(boolean);
245 return BooleanEdge(boolean);
248 return BooleanEdge(b);
252 bool CSolver::isTrue(BooleanEdge b) {
253 return b.isNegated()?b->isFalse():b->isTrue();
256 bool CSolver::isFalse(BooleanEdge b) {
257 return b.isNegated()?b->isTrue():b->isFalse();
260 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
261 BooleanEdge array[] = {arg1, arg2};
262 return applyLogicalOperation(op, array, 2);
265 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
266 BooleanEdge array[] = {arg};
267 return applyLogicalOperation(op, array, 1);
270 static int ptrcompares(const void *p1, const void *p2) {
271 uintptr_t b1 = *(uintptr_t const *) p1;
272 uintptr_t b2 = *(uintptr_t const *) p2;
281 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge * array, uint asize) {
282 BooleanEdge newarray[asize];
283 memcpy(newarray, array, asize * sizeof(BooleanEdge));
284 for(uint i=0; i < asize; i++) {
285 BooleanEdge b=newarray[i];
286 if (b->type == LOGICOP) {
287 if (((BooleanLogic *) b.getBoolean())->replaced) {
288 newarray[i] = doRewrite(newarray[i]);
293 return applyLogicalOperation(op, newarray, asize);
296 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
297 BooleanEdge newarray[asize];
300 return array[0].negate();
303 for (uint i = 0; i < 2; i++) {
304 if (array[i]->type == BOOLCONST) {
305 if (array[i]->isTrue()) {
308 newarray[0] = array[1 - i];
309 return applyLogicalOperation(SATC_NOT, newarray, 1);
311 } else if (array[i]->type == LOGICOP) {
312 BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
314 return rewriteLogicalOperation(op, array, asize);
321 for (uint i =0; i <asize; i++) {
322 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
324 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
328 for (uint i = 0; i < asize; i++) {
329 BooleanEdge b = array[i];
330 if (b->type == LOGICOP) {
331 if (((BooleanLogic *)b.getBoolean())->replaced)
332 return rewriteLogicalOperation(op, array, asize);
334 if (b->type == BOOLCONST) {
340 newarray[newindex++] = b;
344 } else if (newindex == 1) {
347 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
354 //handle by translation
355 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
358 //handle by translation
359 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
364 Boolean *boolean = new BooleanLogic(this, op, array, asize);
365 Boolean *b = boolMap.get(boolean);
367 boolean->updateParents();
368 boolMap.put(boolean, boolean);
369 allBooleans.push(boolean);
370 return BooleanEdge(boolean);
373 return BooleanEdge(b);
377 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
378 Boolean *constraint = new BooleanOrder(order, first, second);
379 allBooleans.push(constraint);
380 return BooleanEdge(constraint);
383 void CSolver::addConstraint(BooleanEdge constraint) {
384 if (isTrue(constraint))
386 else if (isFalse(constraint))
389 if (constraint->type == LOGICOP) {
390 BooleanLogic *b=(BooleanLogic *) constraint.getBoolean();
391 if (!constraint.isNegated()) {
392 if (b->op==SATC_AND) {
393 for(uint i=0;i<b->inputs.getSize();i++) {
394 addConstraint(b->inputs.get(i));
400 addConstraint(doRewrite(constraint));
404 constraints.add(constraint);
405 Boolean *ptr=constraint.getBoolean();
407 if (ptr->boolVal == BV_UNSAT)
410 replaceBooleanWithTrueNoRemove(constraint);
411 constraint->parents.clear();
415 Order *CSolver::createOrder(OrderType type, Set *set) {
416 Order *order = new Order(type, set);
417 allOrders.push(order);
418 activeOrders.add(order);
422 int CSolver::solve() {
423 bool deleteTuner = false;
425 tuner = new DefaultTuner();
429 long long startTime = getTimeNano();
430 computePolarities(this);
435 DecomposeOrderTransform dot(this);
438 IntegerEncodingTransform iet(this);
441 naiveEncodingDecision(this);
442 satEncoder->encodeAllSATEncoder(this);
443 int result = unsat ? IS_UNSAT : satEncoder->solve();
444 long long finishTime = getTimeNano();
445 elapsedTime = finishTime - startTime;
453 uint64_t CSolver::getElementValue(Element *element) {
454 switch (element->type) {
458 return getElementValueSATTranslator(this, element);
465 bool CSolver::getBooleanValue(BooleanEdge bedge) {
466 Boolean *boolean=bedge.getBoolean();
467 switch (boolean->type) {
469 return getBooleanVariableValueSATTranslator(this, boolean);
476 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
477 return order->encoding.resolver->resolveOrder(first, second);
480 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
482 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
484 void CSolver::autoTune(uint budget) {
485 AutoTuner *autotuner = new AutoTuner(budget);
486 autotuner->addProblem(this);