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"
28 boolTrue(BooleanEdge(new BooleanConst(true))),
29 boolFalse(boolTrue.negate()),
34 satEncoder = new SATEncoder(this);
37 /** This function tears down the solver and the entire AST */
40 uint size = allBooleans.getSize();
41 for (uint i = 0; i < size; i++) {
42 delete allBooleans.get(i);
45 size = allSets.getSize();
46 for (uint i = 0; i < size; i++) {
47 delete allSets.get(i);
50 size = allElements.getSize();
51 for (uint i = 0; i < size; i++) {
52 Element* el = allElements.get(i);
56 size = allTables.getSize();
57 for (uint i = 0; i < size; i++) {
58 delete allTables.get(i);
61 size = allPredicates.getSize();
62 for (uint i = 0; i < size; i++) {
63 delete allPredicates.get(i);
66 size = allOrders.getSize();
67 for (uint i = 0; i < size; i++) {
68 delete allOrders.get(i);
70 size = allFunctions.getSize();
71 for (uint i = 0; i < size; i++) {
72 delete allFunctions.get(i);
75 delete boolTrue.getBoolean();
79 CSolver *CSolver::clone() {
80 CSolver *copy = new CSolver();
82 SetIteratorBooleanEdge *it = getConstraints();
83 while (it->hasNext()) {
84 BooleanEdge b = it->next();
85 copy->addConstraint(cloneEdge(copy, &map, b));
91 CSolver* CSolver::deserialize(const char * file){
92 model_print("deserializing ...\n");
93 Deserializer deserializer(file);
94 return deserializer.deserialize();
97 void CSolver::serialize() {
98 model_print("serializing ...\n");
101 clock_gettime(CLOCK_REALTIME, &t);
103 unsigned long long nanotime=t.tv_sec*1000000000+t.tv_nsec;
104 int numchars=sprintf(buffer, "DUMP%llu", nanotime);
105 Serializer serializer(buffer);
106 SetIteratorBooleanEdge *it = getConstraints();
107 while (it->hasNext()) {
108 BooleanEdge b = it->next();
109 serializeBooleanEdge(&serializer, b, true);
114 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
115 Set *set = new Set(type, elements, numelements);
120 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
121 Set *set = new Set(type, lowrange, highrange);
126 VarType CSolver::getSetVarType(Set *set) {
127 return set->getType();
130 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
131 Set *s = createRangeSet(type, lowrange, highrange);
132 return getElementVar(s);
135 MutableSet *CSolver::createMutableSet(VarType type) {
136 MutableSet *set = new MutableSet(type);
141 void CSolver::addItem(MutableSet *set, uint64_t element) {
142 set->addElementMSet(element);
145 uint64_t CSolver::createUniqueItem(MutableSet *set) {
146 uint64_t element = set->getNewUniqueItem();
147 set->addElementMSet(element);
151 void CSolver::finalizeMutableSet(MutableSet *set) {
155 Element *CSolver::getElementVar(Set *set) {
156 Element *element = new ElementSet(set);
157 allElements.push(element);
161 Set *CSolver::getElementRange (Element *element) {
162 return element->getRange();
166 Element *CSolver::getElementConst(VarType type, uint64_t value) {
167 uint64_t array[] = {value};
168 Set *set = new Set(type, array, 1);
169 Element *element = new ElementConst(value, set);
170 Element *e = elemMap.get(element);
173 allElements.push(element);
174 elemMap.put(element, element);
184 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
185 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
186 Element *e = elemMap.get(element);
188 element->updateParents();
189 allElements.push(element);
190 elemMap.put(element, element);
198 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
199 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
200 allFunctions.push(function);
204 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
205 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
206 allPredicates.push(predicate);
210 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
211 Predicate *predicate = new PredicateTable(table, behavior);
212 allPredicates.push(predicate);
216 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
217 Table *table = new Table(domains,numDomain,range);
218 allTables.push(table);
222 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
223 return createTable(domains, numDomain, NULL);
226 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
227 table->addNewTableEntry(inputs, inputSize, result);
230 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
231 Function *function = new FunctionTable(table, behavior);
232 allFunctions.push(function);
236 BooleanEdge CSolver::getBooleanVar(VarType type) {
237 Boolean *boolean = new BooleanVar(type);
238 allBooleans.push(boolean);
239 return BooleanEdge(boolean);
242 BooleanEdge CSolver::getBooleanTrue() {
246 BooleanEdge CSolver::getBooleanFalse() {
250 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
251 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
254 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
255 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
256 Boolean *b = boolMap.get(boolean);
258 boolean->updateParents();
259 boolMap.put(boolean, boolean);
260 allBooleans.push(boolean);
261 return BooleanEdge(boolean);
264 return BooleanEdge(b);
268 bool CSolver::isTrue(BooleanEdge b) {
269 return b.isNegated() ? b->isFalse() : b->isTrue();
272 bool CSolver::isFalse(BooleanEdge b) {
273 return b.isNegated() ? b->isTrue() : b->isFalse();
276 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
277 BooleanEdge array[] = {arg1, arg2};
278 return applyLogicalOperation(op, array, 2);
281 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
282 BooleanEdge array[] = {arg};
283 return applyLogicalOperation(op, array, 1);
286 static int ptrcompares(const void *p1, const void *p2) {
287 uintptr_t b1 = *(uintptr_t const *) p1;
288 uintptr_t b2 = *(uintptr_t const *) p2;
297 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
298 BooleanEdge newarray[asize];
299 memcpy(newarray, array, asize * sizeof(BooleanEdge));
300 for (uint i = 0; i < asize; i++) {
301 BooleanEdge b = newarray[i];
302 if (b->type == LOGICOP) {
303 if (((BooleanLogic *) b.getBoolean())->replaced) {
304 newarray[i] = doRewrite(newarray[i]);
309 return applyLogicalOperation(op, newarray, asize);
312 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
313 BooleanEdge newarray[asize];
316 return array[0].negate();
319 for (uint i = 0; i < 2; i++) {
320 if (isTrue(array[i])) { // It can be undefined
322 } else if (isFalse(array[i])) {
323 newarray[0] = array[1 - i];
324 return applyLogicalOperation(SATC_NOT, newarray, 1);
325 } else if (array[i]->type == LOGICOP) {
326 BooleanLogic *b = (BooleanLogic *)array[i].getBoolean();
328 return rewriteLogicalOperation(op, array, asize);
335 for (uint i = 0; i < asize; i++) {
336 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
338 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
342 for (uint i = 0; i < asize; i++) {
343 BooleanEdge b = array[i];
344 if (b->type == LOGICOP) {
345 if (((BooleanLogic *)b.getBoolean())->replaced)
346 return rewriteLogicalOperation(op, array, asize);
350 else if (isFalse(b)) {
353 newarray[newindex++] = b;
357 } else if (newindex == 1) {
360 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
367 //handle by translation
368 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
371 //handle by translation
372 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
377 Boolean *boolean = new BooleanLogic(this, op, array, asize);
378 Boolean *b = boolMap.get(boolean);
380 boolean->updateParents();
381 boolMap.put(boolean, boolean);
382 allBooleans.push(boolean);
383 return BooleanEdge(boolean);
386 return BooleanEdge(b);
390 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
391 // ASSERT(first != second);
393 return getBooleanFalse();
396 if (order->type == SATC_TOTAL) {
397 if (first > second) {
398 uint64_t tmp = first;
404 Boolean *constraint = new BooleanOrder(order, first, second);
405 Boolean *b = boolMap.get(constraint);
408 allBooleans.push(constraint);
409 boolMap.put(constraint, constraint);
410 constraint->updateParents();
416 BooleanEdge be = BooleanEdge(constraint);
417 return negate ? be.negate() : be;
420 void CSolver::addConstraint(BooleanEdge constraint) {
421 if (isTrue(constraint))
423 else if (isFalse(constraint)) {
428 if (constraint->type == LOGICOP) {
429 BooleanLogic *b = (BooleanLogic *) constraint.getBoolean();
430 if (!constraint.isNegated()) {
431 if (b->op == SATC_AND) {
432 for (uint i = 0; i < b->inputs.getSize(); i++) {
433 addConstraint(b->inputs.get(i));
439 addConstraint(doRewrite(constraint));
443 constraints.add(constraint);
444 Boolean *ptr = constraint.getBoolean();
446 if (ptr->boolVal == BV_UNSAT) {
450 replaceBooleanWithTrueNoRemove(constraint);
451 constraint->parents.clear();
455 Order *CSolver::createOrder(OrderType type, Set *set) {
456 Order *order = new Order(type, set);
457 allOrders.push(order);
458 activeOrders.add(order);
462 int CSolver::solve() {
463 bool deleteTuner = false;
465 tuner = new DefaultTuner();
469 long long startTime = getTimeNano();
470 computePolarities(this);
475 DecomposeOrderTransform dot(this);
478 IntegerEncodingTransform iet(this);
481 EncodingGraph eg(this);
484 // printConstraints();
485 naiveEncodingDecision(this);
486 satEncoder->encodeAllSATEncoder(this);
487 model_print("Is problem UNSAT after encoding: %d\n", unsat);
488 int result = unsat ? IS_UNSAT : satEncoder->solve();
489 model_print("Result Computed in CSolver: %d\n", result);
490 long long finishTime = getTimeNano();
491 elapsedTime = finishTime - startTime;
499 void CSolver::printConstraints() {
500 SetIteratorBooleanEdge *it = getConstraints();
501 while (it->hasNext()) {
502 BooleanEdge b = it->next();
511 void CSolver::printConstraint(BooleanEdge b) {
518 uint64_t CSolver::getElementValue(Element *element) {
519 switch (element->type) {
523 return getElementValueSATTranslator(this, element);
530 bool CSolver::getBooleanValue(BooleanEdge bedge) {
531 Boolean *boolean = bedge.getBoolean();
532 switch (boolean->type) {
534 return getBooleanVariableValueSATTranslator(this, boolean);
541 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
542 return order->encoding.resolver->resolveOrder(first, second);
545 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
547 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
549 void CSolver::autoTune(uint budget) {
550 AutoTuner *autotuner = new AutoTuner(budget);
551 autotuner->addProblem(this);