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);
52 model_print("deleting ...%u", i);
57 size = allTables.getSize();
58 for (uint i = 0; i < size; i++) {
59 delete allTables.get(i);
62 size = allPredicates.getSize();
63 for (uint i = 0; i < size; i++) {
64 delete allPredicates.get(i);
67 size = allOrders.getSize();
68 for (uint i = 0; i < size; i++) {
69 delete allOrders.get(i);
72 size = allFunctions.getSize();
73 for (uint i = 0; i < size; i++) {
74 delete allFunctions.get(i);
77 delete boolTrue.getBoolean();
81 CSolver *CSolver::clone() {
82 CSolver *copy = new CSolver();
84 SetIteratorBooleanEdge *it = getConstraints();
85 while (it->hasNext()) {
86 BooleanEdge b = it->next();
87 copy->addConstraint(cloneEdge(copy, &map, b));
93 void CSolver::serialize() {
94 model_print("serializing ...\n");
96 Serializer serializer("dump");
97 SetIteratorBooleanEdge *it = getConstraints();
98 while (it->hasNext()) {
99 BooleanEdge b = it->next();
100 serializeBooleanEdge(&serializer, b);
104 // model_print("deserializing ...\n");
106 // Deserializer deserializer("dump");
107 // deserializer.deserialize();
111 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
112 Set *set = new Set(type, elements, numelements);
117 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
118 Set *set = new Set(type, lowrange, highrange);
123 VarType CSolver::getSetVarType(Set *set) {
124 return set->getType();
127 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
128 Set *s = createRangeSet(type, lowrange, highrange);
129 return getElementVar(s);
132 MutableSet *CSolver::createMutableSet(VarType type) {
133 MutableSet *set = new MutableSet(type);
138 void CSolver::addItem(MutableSet *set, uint64_t element) {
139 set->addElementMSet(element);
142 uint64_t CSolver::createUniqueItem(MutableSet *set) {
143 uint64_t element = set->getNewUniqueItem();
144 set->addElementMSet(element);
148 void CSolver::finalizeMutableSet(MutableSet *set) {
152 Element *CSolver::getElementVar(Set *set) {
153 Element *element = new ElementSet(set);
154 model_println("%%%%ElementVar:%u", allElements.getSize());
155 allElements.push(element);
159 Set *CSolver::getElementRange (Element *element) {
160 return element->getRange();
164 Element *CSolver::getElementConst(VarType type, uint64_t value) {
165 uint64_t array[] = {value};
166 Set *set = new Set(type, array, 1);
167 Element *element = new ElementConst(value, set);
168 Element *e = elemMap.get(element);
171 model_println("%%%%ElementConst:%u", allElements.getSize());
172 allElements.push(element);
173 elemMap.put(element, element);
182 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
183 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
184 Element *e = elemMap.get(element);
186 element->updateParents();
187 model_println("%%%%ElementFunction:%u", allElements.getSize());
188 allElements.push(element);
189 elemMap.put(element, element);
197 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
198 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
199 allFunctions.push(function);
203 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
204 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
205 allPredicates.push(predicate);
209 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
210 Predicate *predicate = new PredicateTable(table, behavior);
211 allPredicates.push(predicate);
215 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
216 Table *table = new Table(domains,numDomain,range);
217 allTables.push(table);
221 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
222 return createTable(domains, numDomain, NULL);
225 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
226 table->addNewTableEntry(inputs, inputSize, result);
229 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
230 Function *function = new FunctionTable(table, behavior);
231 allFunctions.push(function);
235 BooleanEdge CSolver::getBooleanVar(VarType type) {
236 Boolean *boolean = new BooleanVar(type);
237 allBooleans.push(boolean);
238 return BooleanEdge(boolean);
241 BooleanEdge CSolver::getBooleanTrue() {
245 BooleanEdge CSolver::getBooleanFalse() {
249 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
250 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
253 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
254 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
255 Boolean *b = boolMap.get(boolean);
257 boolean->updateParents();
258 boolMap.put(boolean, boolean);
259 allBooleans.push(boolean);
260 return BooleanEdge(boolean);
263 return BooleanEdge(b);
267 bool CSolver::isTrue(BooleanEdge b) {
268 return b.isNegated() ? b->isFalse() : b->isTrue();
271 bool CSolver::isFalse(BooleanEdge b) {
272 return b.isNegated() ? b->isTrue() : b->isFalse();
275 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
276 BooleanEdge array[] = {arg1, arg2};
277 return applyLogicalOperation(op, array, 2);
280 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
281 BooleanEdge array[] = {arg};
282 return applyLogicalOperation(op, array, 1);
285 static int ptrcompares(const void *p1, const void *p2) {
286 uintptr_t b1 = *(uintptr_t const *) p1;
287 uintptr_t b2 = *(uintptr_t const *) p2;
296 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
297 BooleanEdge newarray[asize];
298 memcpy(newarray, array, asize * sizeof(BooleanEdge));
299 for (uint i = 0; i < asize; i++) {
300 BooleanEdge b = newarray[i];
301 if (b->type == LOGICOP) {
302 if (((BooleanLogic *) b.getBoolean())->replaced) {
303 newarray[i] = doRewrite(newarray[i]);
308 return applyLogicalOperation(op, newarray, asize);
311 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
312 BooleanEdge newarray[asize];
315 return array[0].negate();
318 for (uint i = 0; i < 2; i++) {
319 if (array[i]->type == BOOLCONST) {
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);
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);
349 if (b->type == BOOLCONST) {
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);
395 Boolean *constraint = new BooleanOrder(order, first, second);
396 allBooleans.push(constraint);
397 return BooleanEdge(constraint);
400 void CSolver::addConstraint(BooleanEdge constraint) {
402 model_println("****New Constraint******");
404 if (isTrue(constraint))
406 else if (isFalse(constraint)) {
411 if (constraint->type == LOGICOP) {
412 BooleanLogic *b = (BooleanLogic *) constraint.getBoolean();
413 if (!constraint.isNegated()) {
414 if (b->op == SATC_AND) {
415 for (uint i = 0; i < b->inputs.getSize(); i++) {
416 addConstraint(b->inputs.get(i));
422 addConstraint(doRewrite(constraint));
426 constraints.add(constraint);
427 Boolean *ptr = constraint.getBoolean();
429 if (ptr->boolVal == BV_UNSAT) {
433 replaceBooleanWithTrueNoRemove(constraint);
434 constraint->parents.clear();
438 Order *CSolver::createOrder(OrderType type, Set *set) {
439 Order *order = new Order(type, set);
440 allOrders.push(order);
441 activeOrders.add(order);
445 int CSolver::solve() {
446 bool deleteTuner = false;
448 tuner = new DefaultTuner();
452 long long startTime = getTimeNano();
453 computePolarities(this);
458 DecomposeOrderTransform dot(this);
461 IntegerEncodingTransform iet(this);
464 EncodingGraph eg(this);
468 naiveEncodingDecision(this);
469 satEncoder->encodeAllSATEncoder(this);
470 model_print("Is problem UNSAT after encoding: %d\n", unsat);
471 int result = unsat ? IS_UNSAT : satEncoder->solve();
472 model_print("Result Computed in CSolver: %d\n", result);
473 long long finishTime = getTimeNano();
474 elapsedTime = finishTime - startTime;
482 void CSolver::printConstraints() {
483 SetIteratorBooleanEdge *it = getConstraints();
484 while (it->hasNext()) {
485 BooleanEdge b = it->next();
495 uint64_t CSolver::getElementValue(Element *element) {
496 switch (element->type) {
500 return getElementValueSATTranslator(this, element);
507 bool CSolver::getBooleanValue(BooleanEdge bedge) {
508 Boolean *boolean = bedge.getBoolean();
509 switch (boolean->type) {
511 return getBooleanVariableValueSATTranslator(this, boolean);
518 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
519 return order->encoding.resolver->resolveOrder(first, second);
522 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
524 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
526 void CSolver::autoTune(uint budget) {
527 AutoTuner *autotuner = new AutoTuner(budget);
528 autotuner->addProblem(this);