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"
28 #include "elementopt.h"
29 #include "varorderingopt.h"
35 boolTrue(BooleanEdge(new BooleanConst(true))),
36 boolFalse(boolTrue.negate()),
38 booleanVarUsed(false),
41 satsolverTimeout(NOTIMEOUT),
44 satEncoder = new SATEncoder(this);
47 /** This function tears down the solver and the entire AST */
51 uint size = allBooleans.getSize();
52 for (uint i = 0; i < size; i++) {
53 delete allBooleans.get(i);
56 size = allSets.getSize();
57 for (uint i = 0; i < size; i++) {
58 delete allSets.get(i);
61 size = allElements.getSize();
62 for (uint i = 0; i < size; i++) {
63 Element *el = allElements.get(i);
67 size = allTables.getSize();
68 for (uint i = 0; i < size; i++) {
69 delete allTables.get(i);
72 size = allPredicates.getSize();
73 for (uint i = 0; i < size; i++) {
74 delete allPredicates.get(i);
77 size = allOrders.getSize();
78 for (uint i = 0; i < size; i++) {
79 delete allOrders.get(i);
81 size = allFunctions.getSize();
82 for (uint i = 0; i < size; i++) {
83 delete allFunctions.get(i);
86 if(interpreter != NULL){
90 delete boolTrue.getBoolean();
94 void CSolver::resetSolver() {
96 uint size = allBooleans.getSize();
97 for (uint i = 0; i < size; i++) {
98 delete allBooleans.get(i);
101 size = allSets.getSize();
102 for (uint i = 0; i < size; i++) {
103 delete allSets.get(i);
106 size = allElements.getSize();
107 for (uint i = 0; i < size; i++) {
108 Element *el = allElements.get(i);
112 size = allTables.getSize();
113 for (uint i = 0; i < size; i++) {
114 delete allTables.get(i);
117 size = allPredicates.getSize();
118 for (uint i = 0; i < size; i++) {
119 delete allPredicates.get(i);
122 size = allOrders.getSize();
123 for (uint i = 0; i < size; i++) {
124 delete allOrders.get(i);
126 size = allFunctions.getSize();
127 for (uint i = 0; i < size; i++) {
128 delete allFunctions.get(i);
130 delete boolTrue.getBoolean();
135 allPredicates.clear();
137 allFunctions.clear();
139 activeOrders.reset();
143 boolTrue = BooleanEdge(new BooleanConst(true));
144 boolFalse = boolTrue.negate();
146 booleanVarUsed = false;
149 satEncoder->resetSATEncoder();
153 CSolver *CSolver::clone() {
154 CSolver *copy = new CSolver();
156 SetIteratorBooleanEdge *it = getConstraints();
157 while (it->hasNext()) {
158 BooleanEdge b = it->next();
159 copy->addConstraint(cloneEdge(copy, &map, b));
165 CSolver *CSolver::deserialize(const char *file, bool alloy) {
166 model_print("deserializing %s ...\n", file);
167 Deserializer deserializer(file, alloy);
168 return deserializer.deserialize();
171 void CSolver::serialize() {
172 model_print("serializing ...\n");
174 long long nanotime = getTimeNano();
175 int numchars = sprintf(buffer, "DUMP%llu", nanotime);
176 Serializer serializer(buffer);
177 SetIteratorBooleanEdge *it = getConstraints();
178 while (it->hasNext()) {
179 BooleanEdge b = it->next();
180 serializeBooleanEdge(&serializer, b, true);
185 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
186 Set *set = new Set(type, elements, numelements);
191 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
192 Set *set = new Set(type, lowrange, highrange);
197 bool CSolver::itemExistInSet(Set *set, uint64_t item) {
198 return set->exists(item);
201 VarType CSolver::getSetVarType(Set *set) {
202 return set->getType();
205 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
206 Set *s = createRangeSet(type, lowrange, highrange);
207 return getElementVar(s);
210 MutableSet *CSolver::createMutableSet(VarType type) {
211 MutableSet *set = new MutableSet(type);
216 void CSolver::addItem(MutableSet *set, uint64_t element) {
217 set->addElementMSet(element);
220 uint64_t CSolver::createUniqueItem(MutableSet *set) {
221 uint64_t element = set->getNewUniqueItem();
222 set->addElementMSet(element);
226 void CSolver::finalizeMutableSet(MutableSet *set) {
230 Element *CSolver::getElementVar(Set *set) {
231 Element *element = new ElementSet(set);
232 allElements.push(element);
236 void CSolver::mustHaveValue(Element *element) {
237 element->anyValue = true;
240 Set *CSolver::getElementRange (Element *element) {
241 return element->getRange();
245 Element *CSolver::getElementConst(VarType type, uint64_t value) {
246 uint64_t array[] = {value};
247 Set *set = new Set(type, array, 1);
248 Element *element = new ElementConst(value, set);
249 Element *e = elemMap.get(element);
252 allElements.push(element);
253 elemMap.put(element, element);
263 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
264 ASSERT(numArrays == 2);
265 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
266 Element *e = elemMap.get(element);
268 element->updateParents();
269 allElements.push(element);
270 elemMap.put(element, element);
278 Function *CSolver::createFunctionOperator(ArithOp op, Set *range, OverFlowBehavior overflowbehavior) {
279 Function *function = new FunctionOperator(op, range, overflowbehavior);
280 allFunctions.push(function);
284 Predicate *CSolver::createPredicateOperator(CompOp op) {
285 Predicate *predicate = new PredicateOperator(op);
286 allPredicates.push(predicate);
290 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
291 Predicate *predicate = new PredicateTable(table, behavior);
292 allPredicates.push(predicate);
296 Table *CSolver::createTable(Set *range) {
297 Table *table = new Table(range);
298 allTables.push(table);
302 Table *CSolver::createTableForPredicate() {
303 return createTable(NULL);
306 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
307 table->addNewTableEntry(inputs, inputSize, result);
310 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
311 Function *function = new FunctionTable(table, behavior);
312 allFunctions.push(function);
316 BooleanEdge CSolver::getBooleanVar(VarType type) {
317 Boolean *boolean = new BooleanVar(type);
318 allBooleans.push(boolean);
320 booleanVarUsed = true;
321 return BooleanEdge(boolean);
324 BooleanEdge CSolver::getBooleanTrue() {
328 BooleanEdge CSolver::getBooleanFalse() {
332 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
333 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
336 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
337 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
338 Boolean *b = boolMap.get(boolean);
340 boolean->updateParents();
341 boolMap.put(boolean, boolean);
342 allBooleans.push(boolean);
343 return BooleanEdge(boolean);
346 return BooleanEdge(b);
350 bool CSolver::isTrue(BooleanEdge b) {
351 return b.isNegated() ? b->isFalse() : b->isTrue();
354 bool CSolver::isFalse(BooleanEdge b) {
355 return b.isNegated() ? b->isTrue() : b->isFalse();
358 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
359 BooleanEdge array[] = {arg1, arg2};
360 return applyLogicalOperation(op, array, 2);
363 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
364 BooleanEdge array[] = {arg};
365 return applyLogicalOperation(op, array, 1);
368 static int booleanEdgeCompares(const void *p1, const void *p2) {
369 BooleanEdge be1 = *(BooleanEdge const *) p1;
370 BooleanEdge be2 = *(BooleanEdge const *) p2;
371 uint64_t b1 = be1->id;
372 uint64_t b2 = be2->id;
381 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
382 BooleanEdge newarray[asize];
383 memcpy(newarray, array, asize * sizeof(BooleanEdge));
384 for (uint i = 0; i < asize; i++) {
385 BooleanEdge b = newarray[i];
386 if (b->type == LOGICOP) {
387 if (((BooleanLogic *) b.getBoolean())->replaced) {
388 newarray[i] = doRewrite(newarray[i]);
393 return applyLogicalOperation(op, newarray, asize);
396 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
397 if(!useAlloyCompiler()){
398 BooleanEdge newarray[asize];
401 return array[0].negate();
404 for (uint i = 0; i < 2; i++) {
405 if (isTrue(array[i])) { // It can be undefined
407 } else if (isFalse(array[i])) {
408 newarray[0] = array[1 - i];
409 return applyLogicalOperation(SATC_NOT, newarray, 1);
410 } else if (array[i]->type == LOGICOP) {
411 BooleanLogic *b = (BooleanLogic *)array[i].getBoolean();
413 return rewriteLogicalOperation(op, array, asize);
420 for (uint i = 0; i < asize; i++) {
421 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
423 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
427 for (uint i = 0; i < asize; i++) {
428 BooleanEdge b = array[i];
429 if (b->type == LOGICOP) {
430 if (((BooleanLogic *)b.getBoolean())->replaced)
431 return rewriteLogicalOperation(op, array, asize);
435 else if (isFalse(b)) {
438 newarray[newindex++] = b;
442 } else if (newindex == 1) {
445 bsdqsort(newarray, newindex, sizeof(BooleanEdge), booleanEdgeCompares);
452 //handle by translation
453 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
456 //handle by translation
457 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
462 Boolean *boolean = new BooleanLogic(this, op, array, asize);
463 Boolean *b = boolMap.get(boolean);
465 boolean->updateParents();
466 boolMap.put(boolean, boolean);
467 allBooleans.push(boolean);
468 return BooleanEdge(boolean);
471 return BooleanEdge(b);
475 Boolean *boolean = new BooleanLogic(this, op, array, asize);
476 allBooleans.push(boolean);
477 return BooleanEdge(boolean);
482 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
483 // ASSERT(first != second);
485 return getBooleanFalse();
488 if (order->type == SATC_TOTAL) {
489 if (first > second) {
490 uint64_t tmp = first;
496 Boolean *constraint = new BooleanOrder(order, first, second);
497 if (!useAlloyCompiler() ){
498 Boolean *b = boolMap.get(constraint);
501 allBooleans.push(constraint);
502 boolMap.put(constraint, constraint);
503 constraint->updateParents();
504 if ( order->graph != NULL) {
505 OrderGraph *graph = order->graph;
506 OrderNode *from = graph->lookupOrderNodeFromOrderGraph(first);
508 OrderNode *to = graph->lookupOrderNodeFromOrderGraph(second);
510 OrderEdge *edge = graph->lookupOrderEdgeFromOrderGraph(from, to);
513 if (edge != NULL && edge->mustPos) {
514 replaceBooleanWithTrueNoRemove(constraint);
515 } else if (edge != NULL && edge->mustNeg) {
516 replaceBooleanWithFalseNoRemove(constraint);
517 } else if ((invedge = graph->lookupOrderEdgeFromOrderGraph(to, from)) != NULL
518 && invedge->mustPos) {
519 replaceBooleanWithFalseNoRemove(constraint);
529 BooleanEdge be = BooleanEdge(constraint);
530 return negate ? be.negate() : be;
533 void CSolver::addConstraint(BooleanEdge constraint) {
534 if(!useAlloyCompiler()){
535 if (isTrue(constraint))
537 else if (isFalse(constraint)) {
541 if (constraint->type == LOGICOP) {
542 BooleanLogic *b = (BooleanLogic *) constraint.getBoolean();
543 if (!constraint.isNegated()) {
544 if (b->op == SATC_AND) {
545 uint size = b->inputs.getSize();
546 //Handle potential concurrent modification
547 BooleanEdge array[size];
548 for (uint i = 0; i < size; i++) {
549 array[i] = b->inputs.get(i);
551 for (uint i = 0; i < size; i++) {
552 addConstraint(array[i]);
558 addConstraint(doRewrite(constraint));
562 constraints.add(constraint);
563 Boolean *ptr = constraint.getBoolean();
565 if (ptr->boolVal == BV_UNSAT) {
569 replaceBooleanWithTrueNoRemove(constraint);
570 constraint->parents.clear();
573 constraints.add(constraint);
574 constraint->parents.clear();
578 Order *CSolver::createOrder(OrderType type, Set *set) {
579 Order *order = new Order(type, set);
580 allOrders.push(order);
581 activeOrders.add(order);
585 /** Computes static ordering information to allow isTrue/isFalse
586 queries on newly created orders to work. */
588 void CSolver::inferFixedOrder(Order *order) {
589 if (order->graph != NULL) {
592 order->graph = buildMustOrderGraph(order);
593 reachMustAnalysis(this, order->graph, true);
596 void CSolver::inferFixedOrders() {
597 SetIteratorOrder *orderit = activeOrders.iterator();
598 while (orderit->hasNext()) {
599 Order *order = orderit->next();
600 inferFixedOrder(order);
604 int CSolver::solve() {
605 long long startTime = getTimeNano();
606 bool deleteTuner = false;
608 tuner = new DefaultTuner();
611 int result = IS_INDETER;
612 if(useAlloyCompiler()){
613 interpreter->encode();
614 model_print("Problem encoded in Alloy\n");
615 result = interpreter->solve();
616 model_print("Problem solved by Alloy\n");
620 SetIteratorOrder *orderit = activeOrders.iterator();
621 while (orderit->hasNext()) {
622 Order *order = orderit->next();
623 if (order->graph != NULL) {
630 computePolarities(this);
631 long long time1 = getTimeNano();
632 model_print("Polarity time: %f\n", (time1 - startTime) / NANOSEC);
635 long long time2 = getTimeNano();
636 model_print("Preprocess time: %f\n", (time2 - time1) / NANOSEC);
638 DecomposeOrderTransform dot(this);
640 time1 = getTimeNano();
641 model_print("Decompose Order: %f\n", (time1 - time2) / NANOSEC);
643 IntegerEncodingTransform iet(this);
646 ElementOpt eop(this);
649 EncodingGraph eg(this);
652 naiveEncodingDecision(this);
655 VarOrderingOpt bor(this, satEncoder);
658 time2 = getTimeNano();
659 model_print("Encoding Graph Time: %f\n", (time2 - time1) / NANOSEC);
661 satEncoder->encodeAllSATEncoder(this);
662 time1 = getTimeNano();
664 model_print("Elapse Encode time: %f\n", (time1 - startTime) / NANOSEC);
666 model_print("Is problem UNSAT after encoding: %d\n", unsat);
669 result = unsat ? IS_UNSAT : satEncoder->solve(satsolverTimeout);
670 model_print("Result Computed in SAT solver:\t%s\n", result == IS_SAT ? "SAT" : result == IS_INDETER ? "INDETERMINATE" : " UNSAT");
671 time2 = getTimeNano();
672 elapsedTime = time2 - startTime;
673 model_print("CSOLVER solve time: %f\n", elapsedTime / NANOSEC);
682 void CSolver::setAlloyEncoder(){
683 if(interpreter == NULL){
684 interpreter = new AlloyEnc(this);
688 void CSolver::printConstraints() {
689 SetIteratorBooleanEdge *it = getConstraints();
690 while (it->hasNext()) {
691 BooleanEdge b = it->next();
697 void CSolver::printConstraint(BooleanEdge b) {
701 uint64_t CSolver::getElementValue(Element *element) {
702 switch (element->type) {
706 return useAlloyCompiler()? interpreter->getValue(element):
707 getElementValueSATTranslator(this, element);
714 bool CSolver::getBooleanValue(BooleanEdge bedge) {
715 Boolean *boolean = bedge.getBoolean();
716 switch (boolean->type) {
718 return useAlloyCompiler()? interpreter->getBooleanValue(boolean):
719 getBooleanVariableValueSATTranslator(this, boolean);
726 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
727 return order->encoding.resolver->resolveOrder(first, second);
730 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
732 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
734 void CSolver::autoTune(uint budget) {
735 AutoTuner *autotuner = new AutoTuner(budget);
736 autotuner->addProblem(this);