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"
27 boolTrue(BooleanEdge(new BooleanConst(true))),
28 boolFalse(boolTrue.negate()),
33 satEncoder = new SATEncoder(this);
36 /** This function tears down the solver and the entire AST */
39 uint size = allBooleans.getSize();
40 for (uint i = 0; i < size; i++) {
41 delete allBooleans.get(i);
44 size = allSets.getSize();
45 for (uint i = 0; i < size; i++) {
46 delete allSets.get(i);
49 size = allElements.getSize();
50 for (uint i = 0; i < size; i++) {
51 Element* el = allElements.get(i);
55 size = allTables.getSize();
56 for (uint i = 0; i < size; i++) {
57 delete allTables.get(i);
60 size = allPredicates.getSize();
61 for (uint i = 0; i < size; i++) {
62 delete allPredicates.get(i);
65 size = allOrders.getSize();
66 for (uint i = 0; i < size; i++) {
67 delete allOrders.get(i);
69 size = allFunctions.getSize();
70 for (uint i = 0; i < size; i++) {
71 delete allFunctions.get(i);
74 delete boolTrue.getBoolean();
78 CSolver *CSolver::clone() {
79 CSolver *copy = new CSolver();
81 SetIteratorBooleanEdge *it = getConstraints();
82 while (it->hasNext()) {
83 BooleanEdge b = it->next();
84 copy->addConstraint(cloneEdge(copy, &map, b));
90 CSolver* CSolver::deserialize(const char * file){
91 model_print("deserializing ...\n");
92 Deserializer deserializer(file);
93 return deserializer.deserialize();
96 void CSolver::serialize() {
97 model_print("serializing ...\n");
99 Serializer serializer("dump");
100 SetIteratorBooleanEdge *it = getConstraints();
101 while (it->hasNext()) {
102 BooleanEdge b = it->next();
103 serializeBooleanEdge(&serializer, b, true);
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 VarType CSolver::getSetVarType(Set *set) {
121 return set->getType();
124 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
125 Set *s = createRangeSet(type, lowrange, highrange);
126 return getElementVar(s);
129 MutableSet *CSolver::createMutableSet(VarType type) {
130 MutableSet *set = new MutableSet(type);
135 void CSolver::addItem(MutableSet *set, uint64_t element) {
136 set->addElementMSet(element);
139 uint64_t CSolver::createUniqueItem(MutableSet *set) {
140 uint64_t element = set->getNewUniqueItem();
141 set->addElementMSet(element);
145 void CSolver::finalizeMutableSet(MutableSet *set) {
149 Element *CSolver::getElementVar(Set *set) {
150 Element *element = new ElementSet(set);
151 allElements.push(element);
155 Set *CSolver::getElementRange (Element *element) {
156 return element->getRange();
160 Element *CSolver::getElementConst(VarType type, uint64_t value) {
161 uint64_t array[] = {value};
162 Set *set = new Set(type, array, 1);
163 Element *element = new ElementConst(value, set);
164 Element *e = elemMap.get(element);
167 allElements.push(element);
168 elemMap.put(element, element);
178 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
179 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
180 Element *e = elemMap.get(element);
182 element->updateParents();
183 allElements.push(element);
184 elemMap.put(element, element);
192 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
193 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
194 allFunctions.push(function);
198 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
199 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
200 allPredicates.push(predicate);
204 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
205 Predicate *predicate = new PredicateTable(table, behavior);
206 allPredicates.push(predicate);
210 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
211 Table *table = new Table(domains,numDomain,range);
212 allTables.push(table);
216 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
217 return createTable(domains, numDomain, NULL);
220 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
221 table->addNewTableEntry(inputs, inputSize, result);
224 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
225 Function *function = new FunctionTable(table, behavior);
226 allFunctions.push(function);
230 BooleanEdge CSolver::getBooleanVar(VarType type) {
231 Boolean *boolean = new BooleanVar(type);
232 allBooleans.push(boolean);
233 return BooleanEdge(boolean);
236 BooleanEdge CSolver::getBooleanTrue() {
240 BooleanEdge CSolver::getBooleanFalse() {
244 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
245 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
248 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
249 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
250 Boolean *b = boolMap.get(boolean);
252 boolean->updateParents();
253 boolMap.put(boolean, boolean);
254 allBooleans.push(boolean);
255 return BooleanEdge(boolean);
258 return BooleanEdge(b);
262 bool CSolver::isTrue(BooleanEdge b) {
263 return b.isNegated() ? b->isFalse() : b->isTrue();
266 bool CSolver::isFalse(BooleanEdge b) {
267 return b.isNegated() ? b->isTrue() : b->isFalse();
270 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
271 BooleanEdge array[] = {arg1, arg2};
272 return applyLogicalOperation(op, array, 2);
275 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
276 BooleanEdge array[] = {arg};
277 return applyLogicalOperation(op, array, 1);
280 static int ptrcompares(const void *p1, const void *p2) {
281 uintptr_t b1 = *(uintptr_t const *) p1;
282 uintptr_t b2 = *(uintptr_t const *) p2;
291 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
292 BooleanEdge newarray[asize];
293 memcpy(newarray, array, asize * sizeof(BooleanEdge));
294 for (uint i = 0; i < asize; i++) {
295 BooleanEdge b = newarray[i];
296 if (b->type == LOGICOP) {
297 if (((BooleanLogic *) b.getBoolean())->replaced) {
298 newarray[i] = doRewrite(newarray[i]);
303 return applyLogicalOperation(op, newarray, asize);
306 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
307 BooleanEdge newarray[asize];
310 return array[0].negate();
313 for (uint i = 0; i < 2; i++) {
314 if (isTrue(array[i])) { // It can be undefined
316 } else if (isFalse(array[i])) {
317 newarray[0] = array[1 - i];
318 return applyLogicalOperation(SATC_NOT, newarray, 1);
319 } else if (array[i]->type == LOGICOP) {
320 BooleanLogic *b = (BooleanLogic *)array[i].getBoolean();
322 return rewriteLogicalOperation(op, array, asize);
329 for (uint i = 0; i < asize; i++) {
330 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
332 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
336 for (uint i = 0; i < asize; i++) {
337 BooleanEdge b = array[i];
338 if (b->type == LOGICOP) {
339 if (((BooleanLogic *)b.getBoolean())->replaced)
340 return rewriteLogicalOperation(op, array, asize);
344 else if (isFalse(b)) {
347 newarray[newindex++] = b;
351 } else if (newindex == 1) {
354 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
361 //handle by translation
362 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
365 //handle by translation
366 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
371 Boolean *boolean = new BooleanLogic(this, op, array, asize);
372 Boolean *b = boolMap.get(boolean);
374 boolean->updateParents();
375 boolMap.put(boolean, boolean);
376 allBooleans.push(boolean);
377 return BooleanEdge(boolean);
380 return BooleanEdge(b);
384 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
385 // ASSERT(first != second);
387 return getBooleanFalse();
390 if (order->type == SATC_TOTAL) {
391 if (first > second) {
392 uint64_t tmp = first;
398 Boolean *constraint = new BooleanOrder(order, first, second);
399 Boolean *b = boolMap.get(constraint);
402 allBooleans.push(constraint);
403 boolMap.put(constraint, constraint);
404 constraint->updateParents();
410 BooleanEdge be = BooleanEdge(constraint);
411 return negate ? be.negate() : be;
414 void CSolver::addConstraint(BooleanEdge constraint) {
415 if(constraint.isNegated())
417 constraint.getBoolean()->print();
418 if (isTrue(constraint))
420 else if (isFalse(constraint)) {
425 if (constraint->type == LOGICOP) {
426 BooleanLogic *b = (BooleanLogic *) constraint.getBoolean();
427 if (!constraint.isNegated()) {
428 if (b->op == SATC_AND) {
429 for (uint i = 0; i < b->inputs.getSize(); i++) {
430 addConstraint(b->inputs.get(i));
436 addConstraint(doRewrite(constraint));
440 constraints.add(constraint);
441 Boolean *ptr = constraint.getBoolean();
443 if (ptr->boolVal == BV_UNSAT) {
447 replaceBooleanWithTrueNoRemove(constraint);
448 constraint->parents.clear();
452 Order *CSolver::createOrder(OrderType type, Set *set) {
453 Order *order = new Order(type, set);
454 allOrders.push(order);
455 activeOrders.add(order);
459 int CSolver::solve() {
460 bool deleteTuner = false;
462 tuner = new DefaultTuner();
466 long long startTime = getTimeNano();
467 computePolarities(this);
472 DecomposeOrderTransform dot(this);
475 //IntegerEncodingTransform iet(this);
478 EncodingGraph eg(this);
481 // printConstraints();
482 naiveEncodingDecision(this);
483 satEncoder->encodeAllSATEncoder(this);
484 model_print("Is problem UNSAT after encoding: %d\n", unsat);
485 int result = unsat ? IS_UNSAT : satEncoder->solve();
486 model_print("Result Computed in CSolver: %d\n", result);
487 long long finishTime = getTimeNano();
488 elapsedTime = finishTime - startTime;
496 void CSolver::printConstraints() {
497 SetIteratorBooleanEdge *it = getConstraints();
498 while (it->hasNext()) {
499 BooleanEdge b = it->next();
508 void CSolver::printConstraint(BooleanEdge b) {
515 uint64_t CSolver::getElementValue(Element *element) {
516 switch (element->type) {
520 return getElementValueSATTranslator(this, element);
527 bool CSolver::getBooleanValue(BooleanEdge bedge) {
528 Boolean *boolean = bedge.getBoolean();
529 switch (boolean->type) {
531 return getBooleanVariableValueSATTranslator(this, boolean);
538 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
539 return order->encoding.resolver->resolveOrder(first, second);
542 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
544 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
546 void CSolver::autoTune(uint budget) {
547 AutoTuner *autotuner = new AutoTuner(budget);
548 autotuner->addProblem(this);