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"
24 #include "encodinggraph.h"
25 #include "ordergraph.h"
26 #include "orderedge.h"
27 #include "orderanalysis.h"
31 boolTrue(BooleanEdge(new BooleanConst(true))),
32 boolFalse(boolTrue.negate()),
37 satEncoder = new SATEncoder(this);
40 /** This function tears down the solver and the entire AST */
44 uint size = allBooleans.getSize();
45 for (uint i = 0; i < size; i++) {
46 delete allBooleans.get(i);
49 size = allSets.getSize();
50 for (uint i = 0; i < size; i++) {
51 delete allSets.get(i);
54 size = allElements.getSize();
55 for (uint i = 0; i < size; i++) {
56 Element* el = allElements.get(i);
60 size = allTables.getSize();
61 for (uint i = 0; i < size; i++) {
62 delete allTables.get(i);
65 size = allPredicates.getSize();
66 for (uint i = 0; i < size; i++) {
67 delete allPredicates.get(i);
70 size = allOrders.getSize();
71 for (uint i = 0; i < size; i++) {
72 delete allOrders.get(i);
74 size = allFunctions.getSize();
75 for (uint i = 0; i < size; i++) {
76 delete allFunctions.get(i);
79 delete boolTrue.getBoolean();
83 void CSolver::resetSolver(){
85 uint size = allBooleans.getSize();
86 for (uint i = 0; i < size; i++) {
87 delete allBooleans.get(i);
90 size = allSets.getSize();
91 for (uint i = 0; i < size; i++) {
92 delete allSets.get(i);
95 size = allElements.getSize();
96 for (uint i = 0; i < size; i++) {
97 Element* el = allElements.get(i);
101 size = allTables.getSize();
102 for (uint i = 0; i < size; i++) {
103 delete allTables.get(i);
106 size = allPredicates.getSize();
107 for (uint i = 0; i < size; i++) {
108 delete allPredicates.get(i);
111 size = allOrders.getSize();
112 for (uint i = 0; i < size; i++) {
113 delete allOrders.get(i);
115 size = allFunctions.getSize();
116 for (uint i = 0; i < size; i++) {
117 delete allFunctions.get(i);
119 delete boolTrue.getBoolean();
124 allPredicates.clear();
126 allFunctions.clear();
128 activeOrders.reset();
132 boolTrue = BooleanEdge(new BooleanConst(true));
133 boolFalse = boolTrue.negate();
137 satEncoder->resetSATEncoder();
141 CSolver *CSolver::clone() {
142 CSolver *copy = new CSolver();
144 SetIteratorBooleanEdge *it = getConstraints();
145 while (it->hasNext()) {
146 BooleanEdge b = it->next();
147 copy->addConstraint(cloneEdge(copy, &map, b));
153 CSolver* CSolver::deserialize(const char * file){
154 model_print("deserializing ...\n");
155 Deserializer deserializer(file);
156 return deserializer.deserialize();
159 void CSolver::serialize() {
160 model_print("serializing ...\n");
162 long long nanotime=getTimeNano();
163 int numchars=sprintf(buffer, "DUMP%llu", nanotime);
164 Serializer serializer(buffer);
165 SetIteratorBooleanEdge *it = getConstraints();
166 while (it->hasNext()) {
167 BooleanEdge b = it->next();
168 serializeBooleanEdge(&serializer, b, true);
173 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
174 Set *set = new Set(type, elements, numelements);
179 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
180 Set *set = new Set(type, lowrange, highrange);
185 VarType CSolver::getSetVarType(Set *set) {
186 return set->getType();
189 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
190 Set *s = createRangeSet(type, lowrange, highrange);
191 return getElementVar(s);
194 MutableSet *CSolver::createMutableSet(VarType type) {
195 MutableSet *set = new MutableSet(type);
200 void CSolver::addItem(MutableSet *set, uint64_t element) {
201 set->addElementMSet(element);
204 uint64_t CSolver::createUniqueItem(MutableSet *set) {
205 uint64_t element = set->getNewUniqueItem();
206 set->addElementMSet(element);
210 void CSolver::finalizeMutableSet(MutableSet *set) {
214 Element *CSolver::getElementVar(Set *set) {
215 Element *element = new ElementSet(set);
216 allElements.push(element);
220 Set *CSolver::getElementRange (Element *element) {
221 return element->getRange();
225 Element *CSolver::getElementConst(VarType type, uint64_t value) {
226 uint64_t array[] = {value};
227 Set *set = new Set(type, array, 1);
228 Element *element = new ElementConst(value, set);
229 Element *e = elemMap.get(element);
232 allElements.push(element);
233 elemMap.put(element, element);
243 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
244 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
245 Element *e = elemMap.get(element);
247 element->updateParents();
248 allElements.push(element);
249 elemMap.put(element, element);
257 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
258 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
259 allFunctions.push(function);
263 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
264 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
265 allPredicates.push(predicate);
269 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
270 Predicate *predicate = new PredicateTable(table, behavior);
271 allPredicates.push(predicate);
275 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
276 Table *table = new Table(domains,numDomain,range);
277 allTables.push(table);
281 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
282 return createTable(domains, numDomain, NULL);
285 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
286 table->addNewTableEntry(inputs, inputSize, result);
289 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
290 Function *function = new FunctionTable(table, behavior);
291 allFunctions.push(function);
295 BooleanEdge CSolver::getBooleanVar(VarType type) {
296 Boolean *boolean = new BooleanVar(type);
297 allBooleans.push(boolean);
298 return BooleanEdge(boolean);
301 BooleanEdge CSolver::getBooleanTrue() {
305 BooleanEdge CSolver::getBooleanFalse() {
309 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
310 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
313 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
314 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
315 Boolean *b = boolMap.get(boolean);
317 boolean->updateParents();
318 boolMap.put(boolean, boolean);
319 allBooleans.push(boolean);
320 return BooleanEdge(boolean);
323 return BooleanEdge(b);
327 bool CSolver::isTrue(BooleanEdge b) {
328 return b.isNegated() ? b->isFalse() : b->isTrue();
331 bool CSolver::isFalse(BooleanEdge b) {
332 return b.isNegated() ? b->isTrue() : b->isFalse();
335 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
336 BooleanEdge array[] = {arg1, arg2};
337 return applyLogicalOperation(op, array, 2);
340 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
341 BooleanEdge array[] = {arg};
342 return applyLogicalOperation(op, array, 1);
345 static int ptrcompares(const void *p1, const void *p2) {
346 uintptr_t b1 = *(uintptr_t const *) p1;
347 uintptr_t b2 = *(uintptr_t const *) p2;
356 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
357 BooleanEdge newarray[asize];
358 memcpy(newarray, array, asize * sizeof(BooleanEdge));
359 for (uint i = 0; i < asize; i++) {
360 BooleanEdge b = newarray[i];
361 if (b->type == LOGICOP) {
362 if (((BooleanLogic *) b.getBoolean())->replaced) {
363 newarray[i] = doRewrite(newarray[i]);
368 return applyLogicalOperation(op, newarray, asize);
371 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
372 BooleanEdge newarray[asize];
375 return array[0].negate();
378 for (uint i = 0; i < 2; i++) {
379 if (isTrue(array[i])) { // It can be undefined
381 } else if (isFalse(array[i])) {
382 newarray[0] = array[1 - i];
383 return applyLogicalOperation(SATC_NOT, newarray, 1);
384 } else if (array[i]->type == LOGICOP) {
385 BooleanLogic *b = (BooleanLogic *)array[i].getBoolean();
387 return rewriteLogicalOperation(op, array, asize);
394 for (uint i = 0; i < asize; i++) {
395 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
397 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
401 for (uint i = 0; i < asize; i++) {
402 BooleanEdge b = array[i];
403 if (b->type == LOGICOP) {
404 if (((BooleanLogic *)b.getBoolean())->replaced)
405 return rewriteLogicalOperation(op, array, asize);
409 else if (isFalse(b)) {
412 newarray[newindex++] = b;
416 } else if (newindex == 1) {
419 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
426 //handle by translation
427 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
430 //handle by translation
431 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
436 Boolean *boolean = new BooleanLogic(this, op, array, asize);
437 Boolean *b = boolMap.get(boolean);
439 boolean->updateParents();
440 boolMap.put(boolean, boolean);
441 allBooleans.push(boolean);
442 return BooleanEdge(boolean);
445 return BooleanEdge(b);
449 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
450 // ASSERT(first != second);
452 return getBooleanFalse();
455 if (order->type == SATC_TOTAL) {
456 if (first > second) {
457 uint64_t tmp = first;
463 Boolean *constraint = new BooleanOrder(order, first, second);
464 Boolean *b = boolMap.get(constraint);
467 allBooleans.push(constraint);
468 boolMap.put(constraint, constraint);
469 constraint->updateParents();
470 if (order->graph != NULL) {
471 OrderGraph *graph=order->graph;
472 OrderNode *from=graph->lookupOrderNodeFromOrderGraph(first);
474 OrderNode *to=graph->lookupOrderNodeFromOrderGraph(second);
476 OrderEdge *edge=graph->lookupOrderEdgeFromOrderGraph(from, to);
479 if (edge != NULL && edge->mustPos) {
480 replaceBooleanWithTrueNoRemove(constraint);
481 } else if (edge != NULL && edge->mustNeg) {
482 replaceBooleanWithFalseNoRemove(constraint);
483 } else if ((invedge=graph->lookupOrderEdgeFromOrderGraph(to, from)) != NULL
484 && invedge->mustPos) {
485 replaceBooleanWithFalseNoRemove(constraint);
495 BooleanEdge be = BooleanEdge(constraint);
496 return negate ? be.negate() : be;
499 void CSolver::addConstraint(BooleanEdge constraint) {
500 if (isTrue(constraint))
502 else if (isFalse(constraint)) {
507 if (constraint->type == LOGICOP) {
508 BooleanLogic *b = (BooleanLogic *) constraint.getBoolean();
509 if (!constraint.isNegated()) {
510 if (b->op == SATC_AND) {
511 for (uint i = 0; i < b->inputs.getSize(); i++) {
512 addConstraint(b->inputs.get(i));
518 addConstraint(doRewrite(constraint));
522 constraints.add(constraint);
523 Boolean *ptr = constraint.getBoolean();
525 if (ptr->boolVal == BV_UNSAT) {
529 replaceBooleanWithTrueNoRemove(constraint);
530 constraint->parents.clear();
534 Order *CSolver::createOrder(OrderType type, Set *set) {
535 Order *order = new Order(type, set);
536 allOrders.push(order);
537 activeOrders.add(order);
541 /** Computes static ordering information to allow isTrue/isFalse
542 queries on newly created orders to work. */
544 void CSolver::inferFixedOrder(Order *order) {
545 if (order->graph != NULL) {
548 order->graph = buildMustOrderGraph(order);
549 reachMustAnalysis(this, order->graph, true);
552 void CSolver::inferFixedOrders() {
553 SetIteratorOrder *orderit = activeOrders.iterator();
554 while (orderit->hasNext()) {
555 Order *order = orderit->next();
556 inferFixedOrder(order);
560 #define NANOSEC 1000000000.0
561 int CSolver::solve() {
562 long long starttime = getTimeNano();
563 bool deleteTuner = false;
565 tuner = new DefaultTuner();
571 SetIteratorOrder *orderit = activeOrders.iterator();
572 while (orderit->hasNext()) {
573 Order *order = orderit->next();
574 if (order->graph != NULL) {
582 computePolarities(this);
583 long long time2 = getTimeNano();
584 model_print("Polarity time: %f\n", (time2-starttime)/NANOSEC);
587 long long time3 = getTimeNano();
588 model_print("Preprocess time: %f\n", (time3-time2)/NANOSEC);
590 DecomposeOrderTransform dot(this);
592 long long time4 = getTimeNano();
593 model_print("Decompose Order: %f\n", (time4-time3)/NANOSEC);
595 IntegerEncodingTransform iet(this);
598 EncodingGraph eg(this);
602 naiveEncodingDecision(this);
603 long long time5 = getTimeNano();
604 model_print("Encoding Graph Time: %f\n", (time5-time4)/NANOSEC);
606 long long startTime = getTimeNano();
607 satEncoder->encodeAllSATEncoder(this);
608 long long endTime = getTimeNano();
610 elapsedTime = endTime - startTime;
611 model_print("Elapse Encode time: %f\n", elapsedTime/NANOSEC);
613 model_print("Is problem UNSAT after encoding: %d\n", unsat);
614 int result = unsat ? IS_UNSAT : satEncoder->solve();
615 model_print("Result Computed in CSolver: %d\n", result);
624 void CSolver::printConstraints() {
625 SetIteratorBooleanEdge *it = getConstraints();
626 while (it->hasNext()) {
627 BooleanEdge b = it->next();
636 void CSolver::printConstraint(BooleanEdge b) {
643 uint64_t CSolver::getElementValue(Element *element) {
644 switch (element->type) {
648 return getElementValueSATTranslator(this, element);
655 bool CSolver::getBooleanValue(BooleanEdge bedge) {
656 Boolean *boolean = bedge.getBoolean();
657 switch (boolean->type) {
659 return getBooleanVariableValueSATTranslator(this, boolean);
666 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
667 return order->encoding.resolver->resolveOrder(first, second);
670 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
672 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
674 void CSolver::autoTune(uint budget) {
675 AutoTuner *autotuner = new AutoTuner(budget);
676 autotuner->addProblem(this);