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 CSolver *CSolver::clone() {
84 CSolver *copy = new CSolver();
86 SetIteratorBooleanEdge *it = getConstraints();
87 while (it->hasNext()) {
88 BooleanEdge b = it->next();
89 copy->addConstraint(cloneEdge(copy, &map, b));
95 CSolver* CSolver::deserialize(const char * file){
96 model_print("deserializing ...\n");
97 Deserializer deserializer(file);
98 return deserializer.deserialize();
101 void CSolver::serialize() {
102 model_print("serializing ...\n");
104 long long nanotime=getTimeNano();
105 int numchars=sprintf(buffer, "DUMP%llu", nanotime);
106 Serializer serializer(buffer);
107 SetIteratorBooleanEdge *it = getConstraints();
108 while (it->hasNext()) {
109 BooleanEdge b = it->next();
110 serializeBooleanEdge(&serializer, b, true);
115 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
116 Set *set = new Set(type, elements, numelements);
121 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
122 Set *set = new Set(type, lowrange, highrange);
127 VarType CSolver::getSetVarType(Set *set) {
128 return set->getType();
131 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
132 Set *s = createRangeSet(type, lowrange, highrange);
133 return getElementVar(s);
136 MutableSet *CSolver::createMutableSet(VarType type) {
137 MutableSet *set = new MutableSet(type);
142 void CSolver::addItem(MutableSet *set, uint64_t element) {
143 set->addElementMSet(element);
146 uint64_t CSolver::createUniqueItem(MutableSet *set) {
147 uint64_t element = set->getNewUniqueItem();
148 set->addElementMSet(element);
152 void CSolver::finalizeMutableSet(MutableSet *set) {
156 Element *CSolver::getElementVar(Set *set) {
157 Element *element = new ElementSet(set);
158 allElements.push(element);
162 Set *CSolver::getElementRange (Element *element) {
163 return element->getRange();
167 Element *CSolver::getElementConst(VarType type, uint64_t value) {
168 uint64_t array[] = {value};
169 Set *set = new Set(type, array, 1);
170 Element *element = new ElementConst(value, set);
171 Element *e = elemMap.get(element);
174 allElements.push(element);
175 elemMap.put(element, element);
185 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
186 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
187 Element *e = elemMap.get(element);
189 element->updateParents();
190 allElements.push(element);
191 elemMap.put(element, element);
199 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
200 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
201 allFunctions.push(function);
205 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
206 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
207 allPredicates.push(predicate);
211 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
212 Predicate *predicate = new PredicateTable(table, behavior);
213 allPredicates.push(predicate);
217 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
218 Table *table = new Table(domains,numDomain,range);
219 allTables.push(table);
223 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
224 return createTable(domains, numDomain, NULL);
227 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
228 table->addNewTableEntry(inputs, inputSize, result);
231 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
232 Function *function = new FunctionTable(table, behavior);
233 allFunctions.push(function);
237 BooleanEdge CSolver::getBooleanVar(VarType type) {
238 Boolean *boolean = new BooleanVar(type);
239 allBooleans.push(boolean);
240 return BooleanEdge(boolean);
243 BooleanEdge CSolver::getBooleanTrue() {
247 BooleanEdge CSolver::getBooleanFalse() {
251 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
252 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
255 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
256 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
257 Boolean *b = boolMap.get(boolean);
259 boolean->updateParents();
260 boolMap.put(boolean, boolean);
261 allBooleans.push(boolean);
262 return BooleanEdge(boolean);
265 return BooleanEdge(b);
269 bool CSolver::isTrue(BooleanEdge b) {
270 return b.isNegated() ? b->isFalse() : b->isTrue();
273 bool CSolver::isFalse(BooleanEdge b) {
274 return b.isNegated() ? b->isTrue() : b->isFalse();
277 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
278 BooleanEdge array[] = {arg1, arg2};
279 return applyLogicalOperation(op, array, 2);
282 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
283 BooleanEdge array[] = {arg};
284 return applyLogicalOperation(op, array, 1);
287 static int ptrcompares(const void *p1, const void *p2) {
288 uintptr_t b1 = *(uintptr_t const *) p1;
289 uintptr_t b2 = *(uintptr_t const *) p2;
298 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
299 BooleanEdge newarray[asize];
300 memcpy(newarray, array, asize * sizeof(BooleanEdge));
301 for (uint i = 0; i < asize; i++) {
302 BooleanEdge b = newarray[i];
303 if (b->type == LOGICOP) {
304 if (((BooleanLogic *) b.getBoolean())->replaced) {
305 newarray[i] = doRewrite(newarray[i]);
310 return applyLogicalOperation(op, newarray, asize);
313 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
314 BooleanEdge newarray[asize];
317 return array[0].negate();
320 for (uint i = 0; i < 2; i++) {
321 if (isTrue(array[i])) { // It can be undefined
323 } else if (isFalse(array[i])) {
324 newarray[0] = array[1 - i];
325 return applyLogicalOperation(SATC_NOT, newarray, 1);
326 } else if (array[i]->type == LOGICOP) {
327 BooleanLogic *b = (BooleanLogic *)array[i].getBoolean();
329 return rewriteLogicalOperation(op, array, asize);
336 for (uint i = 0; i < asize; i++) {
337 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
339 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
343 for (uint i = 0; i < asize; i++) {
344 BooleanEdge b = array[i];
345 if (b->type == LOGICOP) {
346 if (((BooleanLogic *)b.getBoolean())->replaced)
347 return rewriteLogicalOperation(op, array, asize);
351 else if (isFalse(b)) {
354 newarray[newindex++] = b;
358 } else if (newindex == 1) {
361 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
368 //handle by translation
369 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
372 //handle by translation
373 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
378 Boolean *boolean = new BooleanLogic(this, op, array, asize);
379 Boolean *b = boolMap.get(boolean);
381 boolean->updateParents();
382 boolMap.put(boolean, boolean);
383 allBooleans.push(boolean);
384 return BooleanEdge(boolean);
387 return BooleanEdge(b);
391 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
392 // ASSERT(first != second);
394 return getBooleanFalse();
397 if (order->type == SATC_TOTAL) {
398 if (first > second) {
399 uint64_t tmp = first;
405 Boolean *constraint = new BooleanOrder(order, first, second);
406 Boolean *b = boolMap.get(constraint);
409 allBooleans.push(constraint);
410 boolMap.put(constraint, constraint);
411 constraint->updateParents();
412 if (order->graph != NULL) {
413 OrderGraph *graph=order->graph;
414 OrderNode *from=graph->lookupOrderNodeFromOrderGraph(first);
416 OrderNode *to=graph->lookupOrderNodeFromOrderGraph(second);
418 OrderEdge *edge=graph->lookupOrderEdgeFromOrderGraph(from, to);
421 if (edge != NULL && edge->mustPos) {
422 replaceBooleanWithTrueNoRemove(constraint);
423 } else if (edge != NULL && edge->mustNeg) {
424 replaceBooleanWithFalseNoRemove(constraint);
425 } else if ((invedge=graph->lookupOrderEdgeFromOrderGraph(to, from)) != NULL
426 && invedge->mustPos) {
427 replaceBooleanWithFalseNoRemove(constraint);
437 BooleanEdge be = BooleanEdge(constraint);
438 return negate ? be.negate() : be;
441 void CSolver::addConstraint(BooleanEdge constraint) {
442 if (isTrue(constraint))
444 else if (isFalse(constraint)) {
449 if (constraint->type == LOGICOP) {
450 BooleanLogic *b = (BooleanLogic *) constraint.getBoolean();
451 if (!constraint.isNegated()) {
452 if (b->op == SATC_AND) {
453 for (uint i = 0; i < b->inputs.getSize(); i++) {
454 addConstraint(b->inputs.get(i));
460 addConstraint(doRewrite(constraint));
464 constraints.add(constraint);
465 Boolean *ptr = constraint.getBoolean();
467 if (ptr->boolVal == BV_UNSAT) {
471 replaceBooleanWithTrueNoRemove(constraint);
472 constraint->parents.clear();
476 Order *CSolver::createOrder(OrderType type, Set *set) {
477 Order *order = new Order(type, set);
478 allOrders.push(order);
479 activeOrders.add(order);
483 /** Computes static ordering information to allow isTrue/isFalse
484 queries on newly created orders to work. */
486 void CSolver::inferFixedOrder(Order *order) {
487 if (order->graph != NULL) {
490 order->graph = buildMustOrderGraph(order);
491 reachMustAnalysis(this, order->graph, true);
494 void CSolver::inferFixedOrders() {
495 SetIteratorOrder *orderit = activeOrders.iterator();
496 while (orderit->hasNext()) {
497 Order *order = orderit->next();
498 inferFixedOrder(order);
502 #define NANOSEC 1000000000.0
503 int CSolver::solve() {
504 long long starttime = getTimeNano();
505 bool deleteTuner = false;
507 tuner = new DefaultTuner();
513 SetIteratorOrder *orderit = activeOrders.iterator();
514 while (orderit->hasNext()) {
515 Order *order = orderit->next();
516 if (order->graph != NULL) {
524 computePolarities(this);
525 long long time2 = getTimeNano();
526 model_print("Polarity time: %f\n", (time2-starttime)/NANOSEC);
529 long long time3 = getTimeNano();
530 model_print("Preprocess time: %f\n", (time3-time2)/NANOSEC);
532 DecomposeOrderTransform dot(this);
534 long long time4 = getTimeNano();
535 model_print("Decompose Order: %f\n", (time4-time3)/NANOSEC);
537 IntegerEncodingTransform iet(this);
540 EncodingGraph eg(this);
544 naiveEncodingDecision(this);
545 long long time5 = getTimeNano();
546 model_print("Encoding Graph Time: %f\n", (time5-time4)/NANOSEC);
548 long long startTime = getTimeNano();
549 satEncoder->encodeAllSATEncoder(this);
550 long long endTime = getTimeNano();
552 elapsedTime = endTime - startTime;
553 model_print("Elapse Encode time: %f\n", elapsedTime/NANOSEC);
555 model_print("Is problem UNSAT after encoding: %d\n", unsat);
556 int result = unsat ? IS_UNSAT : satEncoder->solve();
557 model_print("Result Computed in CSolver: %d\n", result);
566 void CSolver::printConstraints() {
567 SetIteratorBooleanEdge *it = getConstraints();
568 while (it->hasNext()) {
569 BooleanEdge b = it->next();
578 void CSolver::printConstraint(BooleanEdge b) {
585 uint64_t CSolver::getElementValue(Element *element) {
586 switch (element->type) {
590 return getElementValueSATTranslator(this, element);
597 bool CSolver::getBooleanValue(BooleanEdge bedge) {
598 Boolean *boolean = bedge.getBoolean();
599 switch (boolean->type) {
601 return getBooleanVariableValueSATTranslator(this, boolean);
608 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
609 return order->encoding.resolver->resolveOrder(first, second);
612 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
614 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
616 void CSolver::autoTune(uint budget) {
617 AutoTuner *autotuner = new AutoTuner(budget);
618 autotuner->addProblem(this);