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 */
43 uint size = allBooleans.getSize();
44 for (uint i = 0; i < size; i++) {
45 delete allBooleans.get(i);
48 size = allSets.getSize();
49 for (uint i = 0; i < size; i++) {
50 delete allSets.get(i);
53 size = allElements.getSize();
54 for (uint i = 0; i < size; i++) {
55 Element* el = allElements.get(i);
59 size = allTables.getSize();
60 for (uint i = 0; i < size; i++) {
61 delete allTables.get(i);
64 size = allPredicates.getSize();
65 for (uint i = 0; i < size; i++) {
66 delete allPredicates.get(i);
69 size = allOrders.getSize();
70 for (uint i = 0; i < size; i++) {
71 delete allOrders.get(i);
73 size = allFunctions.getSize();
74 for (uint i = 0; i < size; i++) {
75 delete allFunctions.get(i);
78 delete boolTrue.getBoolean();
82 CSolver *CSolver::clone() {
83 CSolver *copy = new CSolver();
85 SetIteratorBooleanEdge *it = getConstraints();
86 while (it->hasNext()) {
87 BooleanEdge b = it->next();
88 copy->addConstraint(cloneEdge(copy, &map, b));
94 CSolver* CSolver::deserialize(const char * file){
95 model_print("deserializing ...\n");
96 Deserializer deserializer(file);
97 return deserializer.deserialize();
100 void CSolver::serialize() {
101 model_print("serializing ...\n");
104 clock_gettime(CLOCK_REALTIME, &t);
106 unsigned long long nanotime=t.tv_sec*1000000000+t.tv_nsec;
107 int numchars=sprintf(buffer, "DUMP%llu", nanotime);
108 Serializer serializer(buffer);
109 SetIteratorBooleanEdge *it = getConstraints();
110 while (it->hasNext()) {
111 BooleanEdge b = it->next();
112 serializeBooleanEdge(&serializer, b, true);
117 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
118 Set *set = new Set(type, elements, numelements);
123 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
124 Set *set = new Set(type, lowrange, highrange);
129 VarType CSolver::getSetVarType(Set *set) {
130 return set->getType();
133 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
134 Set *s = createRangeSet(type, lowrange, highrange);
135 return getElementVar(s);
138 MutableSet *CSolver::createMutableSet(VarType type) {
139 MutableSet *set = new MutableSet(type);
144 void CSolver::addItem(MutableSet *set, uint64_t element) {
145 set->addElementMSet(element);
148 uint64_t CSolver::createUniqueItem(MutableSet *set) {
149 uint64_t element = set->getNewUniqueItem();
150 set->addElementMSet(element);
154 void CSolver::finalizeMutableSet(MutableSet *set) {
158 Element *CSolver::getElementVar(Set *set) {
159 Element *element = new ElementSet(set);
160 allElements.push(element);
164 Set *CSolver::getElementRange (Element *element) {
165 return element->getRange();
169 Element *CSolver::getElementConst(VarType type, uint64_t value) {
170 uint64_t array[] = {value};
171 Set *set = new Set(type, array, 1);
172 Element *element = new ElementConst(value, set);
173 Element *e = elemMap.get(element);
176 allElements.push(element);
177 elemMap.put(element, element);
187 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
188 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
189 Element *e = elemMap.get(element);
191 element->updateParents();
192 allElements.push(element);
193 elemMap.put(element, element);
201 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
202 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
203 allFunctions.push(function);
207 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
208 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
209 allPredicates.push(predicate);
213 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
214 Predicate *predicate = new PredicateTable(table, behavior);
215 allPredicates.push(predicate);
219 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
220 Table *table = new Table(domains,numDomain,range);
221 allTables.push(table);
225 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
226 return createTable(domains, numDomain, NULL);
229 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
230 table->addNewTableEntry(inputs, inputSize, result);
233 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
234 Function *function = new FunctionTable(table, behavior);
235 allFunctions.push(function);
239 BooleanEdge CSolver::getBooleanVar(VarType type) {
240 Boolean *boolean = new BooleanVar(type);
241 allBooleans.push(boolean);
242 return BooleanEdge(boolean);
245 BooleanEdge CSolver::getBooleanTrue() {
249 BooleanEdge CSolver::getBooleanFalse() {
253 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
254 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
257 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
258 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
259 Boolean *b = boolMap.get(boolean);
261 boolean->updateParents();
262 boolMap.put(boolean, boolean);
263 allBooleans.push(boolean);
264 return BooleanEdge(boolean);
267 return BooleanEdge(b);
271 bool CSolver::isTrue(BooleanEdge b) {
272 return b.isNegated() ? b->isFalse() : b->isTrue();
275 bool CSolver::isFalse(BooleanEdge b) {
276 return b.isNegated() ? b->isTrue() : b->isFalse();
279 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
280 BooleanEdge array[] = {arg1, arg2};
281 return applyLogicalOperation(op, array, 2);
284 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
285 BooleanEdge array[] = {arg};
286 return applyLogicalOperation(op, array, 1);
289 static int ptrcompares(const void *p1, const void *p2) {
290 uintptr_t b1 = *(uintptr_t const *) p1;
291 uintptr_t b2 = *(uintptr_t const *) p2;
300 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
301 BooleanEdge newarray[asize];
302 memcpy(newarray, array, asize * sizeof(BooleanEdge));
303 for (uint i = 0; i < asize; i++) {
304 BooleanEdge b = newarray[i];
305 if (b->type == LOGICOP) {
306 if (((BooleanLogic *) b.getBoolean())->replaced) {
307 newarray[i] = doRewrite(newarray[i]);
312 return applyLogicalOperation(op, newarray, asize);
315 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
316 BooleanEdge newarray[asize];
319 return array[0].negate();
322 for (uint i = 0; i < 2; i++) {
323 if (isTrue(array[i])) { // It can be undefined
325 } else if (isFalse(array[i])) {
326 newarray[0] = array[1 - i];
327 return applyLogicalOperation(SATC_NOT, newarray, 1);
328 } else if (array[i]->type == LOGICOP) {
329 BooleanLogic *b = (BooleanLogic *)array[i].getBoolean();
331 return rewriteLogicalOperation(op, array, asize);
338 for (uint i = 0; i < asize; i++) {
339 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
341 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
345 for (uint i = 0; i < asize; i++) {
346 BooleanEdge b = array[i];
347 if (b->type == LOGICOP) {
348 if (((BooleanLogic *)b.getBoolean())->replaced)
349 return rewriteLogicalOperation(op, array, asize);
353 else if (isFalse(b)) {
356 newarray[newindex++] = b;
360 } else if (newindex == 1) {
363 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
370 //handle by translation
371 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
374 //handle by translation
375 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
380 Boolean *boolean = new BooleanLogic(this, op, array, asize);
381 Boolean *b = boolMap.get(boolean);
383 boolean->updateParents();
384 boolMap.put(boolean, boolean);
385 allBooleans.push(boolean);
386 return BooleanEdge(boolean);
389 return BooleanEdge(b);
393 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
394 // ASSERT(first != second);
396 return getBooleanFalse();
399 if (order->type == SATC_TOTAL) {
400 if (first > second) {
401 uint64_t tmp = first;
407 Boolean *constraint = new BooleanOrder(order, first, second);
408 Boolean *b = boolMap.get(constraint);
411 allBooleans.push(constraint);
412 boolMap.put(constraint, constraint);
413 constraint->updateParents();
414 if (order->graph != NULL) {
415 OrderGraph *graph=order->graph;
416 OrderNode *from=graph->lookupOrderNodeFromOrderGraph(first);
418 OrderNode *to=graph->lookupOrderNodeFromOrderGraph(second);
420 OrderEdge *edge=graph->lookupOrderEdgeFromOrderGraph(from, to);
423 if (edge != NULL && edge->mustPos) {
424 replaceBooleanWithTrueNoRemove(constraint);
425 } else if (edge != NULL && edge->mustNeg) {
426 replaceBooleanWithFalseNoRemove(constraint);
427 } else if ((invedge=graph->lookupOrderEdgeFromOrderGraph(to, from)) != NULL
428 && invedge->mustPos) {
429 replaceBooleanWithFalseNoRemove(constraint);
439 BooleanEdge be = BooleanEdge(constraint);
440 return negate ? be.negate() : be;
443 void CSolver::addConstraint(BooleanEdge constraint) {
444 if (isTrue(constraint))
446 else if (isFalse(constraint)) {
451 if (constraint->type == LOGICOP) {
452 BooleanLogic *b = (BooleanLogic *) constraint.getBoolean();
453 if (!constraint.isNegated()) {
454 if (b->op == SATC_AND) {
455 for (uint i = 0; i < b->inputs.getSize(); i++) {
456 addConstraint(b->inputs.get(i));
462 addConstraint(doRewrite(constraint));
466 constraints.add(constraint);
467 Boolean *ptr = constraint.getBoolean();
469 if (ptr->boolVal == BV_UNSAT) {
473 replaceBooleanWithTrueNoRemove(constraint);
474 constraint->parents.clear();
478 Order *CSolver::createOrder(OrderType type, Set *set) {
479 Order *order = new Order(type, set);
480 allOrders.push(order);
481 activeOrders.add(order);
485 /** Computes static ordering information to allow isTrue/isFalse
486 queries on newly created orders to work. */
488 void CSolver::inferFixedOrder(Order *order) {
489 if (order->graph != NULL) {
492 order->graph = buildMustOrderGraph(order);
493 reachMustAnalysis(this, order->graph, true);
496 void CSolver::inferFixedOrders() {
497 SetIteratorOrder *orderit = activeOrders.iterator();
498 while (orderit->hasNext()) {
499 Order *order = orderit->next();
500 inferFixedOrder(order);
504 int CSolver::solve() {
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);
529 DecomposeOrderTransform dot(this);
532 IntegerEncodingTransform iet(this);
535 EncodingGraph eg(this);
539 naiveEncodingDecision(this);
541 long long startTime = getTimeNano();
542 satEncoder->encodeAllSATEncoder(this);
543 model_print("Is problem UNSAT after encoding: %d\n", unsat);
544 int result = unsat ? IS_UNSAT : satEncoder->solve();
545 model_print("Result Computed in CSolver: %d\n", result);
546 long long finishTime = getTimeNano();
547 elapsedTime = finishTime - startTime;
555 void CSolver::printConstraints() {
556 SetIteratorBooleanEdge *it = getConstraints();
557 while (it->hasNext()) {
558 BooleanEdge b = it->next();
567 void CSolver::printConstraint(BooleanEdge b) {
574 uint64_t CSolver::getElementValue(Element *element) {
575 switch (element->type) {
579 return getElementValueSATTranslator(this, element);
586 bool CSolver::getBooleanValue(BooleanEdge bedge) {
587 Boolean *boolean = bedge.getBoolean();
588 switch (boolean->type) {
590 return getBooleanVariableValueSATTranslator(this, boolean);
597 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
598 return order->encoding.resolver->resolveOrder(first, second);
601 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
603 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
605 void CSolver::autoTune(uint budget) {
606 AutoTuner *autotuner = new AutoTuner(budget);
607 autotuner->addProblem(this);