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"
26 boolTrue(BooleanEdge(new BooleanConst(true))),
27 boolFalse(boolTrue.negate()),
32 satEncoder = new SATEncoder(this);
35 /** This function tears down the solver and the entire AST */
38 uint size = allBooleans.getSize();
39 for (uint i = 0; i < size; i++) {
40 delete allBooleans.get(i);
43 size = allSets.getSize();
44 for (uint i = 0; i < size; i++) {
45 delete allSets.get(i);
48 size = allElements.getSize();
49 for (uint i = 0; i < size; i++) {
50 delete allElements.get(i);
53 size = allTables.getSize();
54 for (uint i = 0; i < size; i++) {
55 delete allTables.get(i);
58 size = allPredicates.getSize();
59 for (uint i = 0; i < size; i++) {
60 delete allPredicates.get(i);
63 size = allOrders.getSize();
64 for (uint i = 0; i < size; i++) {
65 delete allOrders.get(i);
68 size = allFunctions.getSize();
69 for (uint i = 0; i < size; i++) {
70 delete allFunctions.get(i);
73 delete boolTrue.getBoolean();
77 CSolver *CSolver::clone() {
78 CSolver *copy = new CSolver();
80 SetIteratorBooleanEdge *it = getConstraints();
81 while (it->hasNext()) {
82 BooleanEdge b = it->next();
83 copy->addConstraint(cloneEdge(copy, &map, b));
89 void CSolver::serialize() {
90 model_print("serializing ...\n");
92 Serializer serializer("dump");
93 SetIteratorBooleanEdge *it = getConstraints();
94 while (it->hasNext()) {
95 BooleanEdge b = it->next();
96 serializeBooleanEdge(&serializer, b);
100 model_print("deserializing ...\n");
102 Deserializer deserializer("dump");
103 deserializer.deserialize();
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 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
121 Set *s = createRangeSet(type, lowrange, highrange);
122 return getElementVar(s);
125 MutableSet *CSolver::createMutableSet(VarType type) {
126 MutableSet *set = new MutableSet(type);
131 void CSolver::addItem(MutableSet *set, uint64_t element) {
132 set->addElementMSet(element);
135 uint64_t CSolver::createUniqueItem(MutableSet *set) {
136 uint64_t element = set->getNewUniqueItem();
137 set->addElementMSet(element);
141 Element *CSolver::getElementVar(Set *set) {
142 Element *element = new ElementSet(set);
143 allElements.push(element);
147 Element *CSolver::getElementConst(VarType type, uint64_t value) {
148 uint64_t array[] = {value};
149 Set *set = new Set(type, array, 1);
150 Element *element = new ElementConst(value, set);
151 Element *e = elemMap.get(element);
154 allElements.push(element);
155 elemMap.put(element, element);
164 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
165 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
166 Element *e = elemMap.get(element);
168 element->updateParents();
169 allElements.push(element);
170 elemMap.put(element, element);
178 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
179 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
180 allFunctions.push(function);
184 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
185 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
186 allPredicates.push(predicate);
190 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
191 Predicate *predicate = new PredicateTable(table, behavior);
192 allPredicates.push(predicate);
196 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
197 Table *table = new Table(domains,numDomain,range);
198 allTables.push(table);
202 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
203 return createTable(domains, numDomain, NULL);
206 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
207 table->addNewTableEntry(inputs, inputSize, result);
210 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
211 Function *function = new FunctionTable(table, behavior);
212 allFunctions.push(function);
216 BooleanEdge CSolver::getBooleanVar(VarType type) {
217 Boolean *boolean = new BooleanVar(type);
218 allBooleans.push(boolean);
219 return BooleanEdge(boolean);
222 BooleanEdge CSolver::getBooleanTrue() {
226 BooleanEdge CSolver::getBooleanFalse() {
230 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
231 return applyPredicateTable(predicate, inputs, numInputs, BooleanEdge(NULL));
234 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
235 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
236 Boolean *b = boolMap.get(boolean);
238 boolean->updateParents();
239 boolMap.put(boolean, boolean);
240 allBooleans.push(boolean);
241 return BooleanEdge(boolean);
244 return BooleanEdge(b);
248 bool CSolver::isTrue(BooleanEdge b) {
249 return b.isNegated()?b->isFalse():b->isTrue();
252 bool CSolver::isFalse(BooleanEdge b) {
253 return b.isNegated()?b->isTrue():b->isFalse();
256 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
257 BooleanEdge array[] = {arg1, arg2};
258 return applyLogicalOperation(op, array, 2);
261 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
262 BooleanEdge array[] = {arg};
263 return applyLogicalOperation(op, array, 1);
266 static int ptrcompares(const void *p1, const void *p2) {
267 uintptr_t b1 = *(uintptr_t const *) p1;
268 uintptr_t b2 = *(uintptr_t const *) p2;
277 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge * array, uint asize) {
278 BooleanEdge newarray[asize];
279 memcpy(newarray, array, asize * sizeof(BooleanEdge));
280 for(uint i=0; i < asize; i++) {
281 BooleanEdge b=newarray[i];
282 if (b->type == LOGICOP) {
283 if (((BooleanLogic *) b.getBoolean())->replaced) {
284 newarray[i] = doRewrite(newarray[i]);
289 return applyLogicalOperation(op, newarray, asize);
292 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
293 BooleanEdge newarray[asize];
296 return array[0].negate();
299 for (uint i = 0; i < 2; i++) {
300 if (array[i]->type == BOOLCONST) {
301 if (array[i]->isTrue()) {
304 newarray[0] = array[1 - i];
305 return applyLogicalOperation(SATC_NOT, newarray, 1);
307 } else if (array[i]->type == LOGICOP) {
308 BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
310 return rewriteLogicalOperation(op, array, asize);
317 for (uint i =0; i <asize; i++) {
318 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
320 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
324 for (uint i = 0; i < asize; i++) {
325 BooleanEdge b = array[i];
326 if (b->type == LOGICOP) {
327 if (((BooleanLogic *)b.getBoolean())->replaced)
328 return rewriteLogicalOperation(op, array, asize);
330 if (b->type == BOOLCONST) {
336 newarray[newindex++] = b;
340 } else if (newindex == 1) {
343 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
350 //handle by translation
351 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
354 //handle by translation
355 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
360 Boolean *boolean = new BooleanLogic(this, op, array, asize);
361 Boolean *b = boolMap.get(boolean);
363 boolean->updateParents();
364 boolMap.put(boolean, boolean);
365 allBooleans.push(boolean);
366 return BooleanEdge(boolean);
369 return BooleanEdge(b);
373 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
374 Boolean *constraint = new BooleanOrder(order, first, second);
375 allBooleans.push(constraint);
376 return BooleanEdge(constraint);
379 void CSolver::addConstraint(BooleanEdge constraint) {
380 if (isTrue(constraint))
382 else if (isFalse(constraint))
385 if (constraint->type == LOGICOP) {
386 BooleanLogic *b=(BooleanLogic *) constraint.getBoolean();
387 if (!constraint.isNegated()) {
388 if (b->op==SATC_AND) {
389 for(uint i=0;i<b->inputs.getSize();i++) {
390 addConstraint(b->inputs.get(i));
396 addConstraint(doRewrite(constraint));
400 constraints.add(constraint);
401 Boolean *ptr=constraint.getBoolean();
403 if (ptr->boolVal == BV_UNSAT)
406 replaceBooleanWithTrueNoRemove(constraint);
407 constraint->parents.clear();
411 Order *CSolver::createOrder(OrderType type, Set *set) {
412 Order *order = new Order(type, set);
413 allOrders.push(order);
414 activeOrders.add(order);
418 int CSolver::solve() {
419 bool deleteTuner = false;
421 tuner = new DefaultTuner();
425 long long startTime = getTimeNano();
426 computePolarities(this);
431 DecomposeOrderTransform dot(this);
434 IntegerEncodingTransform iet(this);
437 naiveEncodingDecision(this);
438 satEncoder->encodeAllSATEncoder(this);
439 int result = unsat ? IS_UNSAT : satEncoder->solve();
440 long long finishTime = getTimeNano();
441 elapsedTime = finishTime - startTime;
449 uint64_t CSolver::getElementValue(Element *element) {
450 switch (element->type) {
454 return getElementValueSATTranslator(this, element);
461 bool CSolver::getBooleanValue(BooleanEdge bedge) {
462 Boolean *boolean=bedge.getBoolean();
463 switch (boolean->type) {
465 return getBooleanVariableValueSATTranslator(this, boolean);
472 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
473 return order->encoding.resolver->resolveOrder(first, second);
476 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
478 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
480 void CSolver::autoTune(uint budget) {
481 AutoTuner *autotuner = new AutoTuner(budget);
482 autotuner->addProblem(this);