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() {
91 Serializer serializer("dump");
92 SetIteratorBooleanEdge *it = getConstraints();
93 while (it->hasNext()) {
94 BooleanEdge b = it->next();
95 serializeBooleanEdge(&serializer, b);
99 model_print("deserializing ...\n");
101 Deserializer deserializer("dump");
102 deserializer.deserialize();
107 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
108 Set *set = new Set(type, elements, numelements);
113 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
114 Set *set = new Set(type, lowrange, highrange);
119 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
120 Set *s = createRangeSet(type, lowrange, highrange);
121 return getElementVar(s);
124 MutableSet *CSolver::createMutableSet(VarType type) {
125 MutableSet *set = new MutableSet(type);
130 void CSolver::addItem(MutableSet *set, uint64_t element) {
131 set->addElementMSet(element);
134 uint64_t CSolver::createUniqueItem(MutableSet *set) {
135 uint64_t element = set->getNewUniqueItem();
136 set->addElementMSet(element);
140 Element *CSolver::getElementVar(Set *set) {
141 Element *element = new ElementSet(set);
142 allElements.push(element);
146 Element *CSolver::getElementConst(VarType type, uint64_t value) {
147 uint64_t array[] = {value};
148 Set *set = new Set(type, array, 1);
149 Element *element = new ElementConst(value, type, set);
150 Element *e = elemMap.get(element);
153 allElements.push(element);
154 elemMap.put(element, element);
163 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
164 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
165 Element *e = elemMap.get(element);
167 element->updateParents();
168 allElements.push(element);
169 elemMap.put(element, element);
177 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
178 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
179 allFunctions.push(function);
183 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
184 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
185 allPredicates.push(predicate);
189 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
190 Predicate *predicate = new PredicateTable(table, behavior);
191 allPredicates.push(predicate);
195 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
196 Table *table = new Table(domains,numDomain,range);
197 allTables.push(table);
201 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
202 return createTable(domains, numDomain, NULL);
205 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
206 table->addNewTableEntry(inputs, inputSize, result);
209 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
210 Function *function = new FunctionTable(table, behavior);
211 allFunctions.push(function);
215 BooleanEdge CSolver::getBooleanVar(VarType type) {
216 Boolean *boolean = new BooleanVar(type);
217 allBooleans.push(boolean);
218 return BooleanEdge(boolean);
221 BooleanEdge CSolver::getBooleanTrue() {
225 BooleanEdge CSolver::getBooleanFalse() {
229 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
230 return applyPredicateTable(predicate, inputs, numInputs, NULL);
233 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
234 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
235 Boolean *b = boolMap.get(boolean);
237 boolean->updateParents();
238 boolMap.put(boolean, boolean);
239 allBooleans.push(boolean);
240 return BooleanEdge(boolean);
243 return BooleanEdge(b);
247 bool CSolver::isTrue(BooleanEdge b) {
248 return b.isNegated()?b->isFalse():b->isTrue();
251 bool CSolver::isFalse(BooleanEdge b) {
252 return b.isNegated()?b->isTrue():b->isFalse();
255 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
256 BooleanEdge array[] = {arg1, arg2};
257 return applyLogicalOperation(op, array, 2);
260 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
261 BooleanEdge array[] = {arg};
262 return applyLogicalOperation(op, array, 1);
265 static int ptrcompares(const void *p1, const void *p2) {
266 uintptr_t b1 = *(uintptr_t const *) p1;
267 uintptr_t b2 = *(uintptr_t const *) p2;
276 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge * array, uint asize) {
277 BooleanEdge newarray[asize];
278 memcpy(newarray, array, asize * sizeof(BooleanEdge));
279 for(uint i=0; i < asize; i++) {
280 BooleanEdge b=newarray[i];
281 if (b->type == LOGICOP) {
282 if (((BooleanLogic *) b.getBoolean())->replaced) {
283 newarray[i] = doRewrite(newarray[i]);
288 return applyLogicalOperation(op, newarray, asize);
291 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
292 BooleanEdge newarray[asize];
295 return array[0].negate();
298 for (uint i = 0; i < 2; i++) {
299 if (array[i]->type == BOOLCONST) {
300 if (array[i]->isTrue()) {
303 newarray[0] = array[1 - i];
304 return applyLogicalOperation(SATC_NOT, newarray, 1);
306 } else if (array[i]->type == LOGICOP) {
307 BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
309 return rewriteLogicalOperation(op, array, asize);
316 for (uint i =0; i <asize; i++) {
317 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
319 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
323 for (uint i = 0; i < asize; i++) {
324 BooleanEdge b = array[i];
325 if (b->type == LOGICOP) {
326 if (((BooleanLogic *)b.getBoolean())->replaced)
327 return rewriteLogicalOperation(op, array, asize);
329 if (b->type == BOOLCONST) {
335 newarray[newindex++] = b;
339 } else if (newindex == 1) {
342 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
349 //handle by translation
350 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
353 //handle by translation
354 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
359 Boolean *boolean = new BooleanLogic(this, op, array, asize);
360 Boolean *b = boolMap.get(boolean);
362 boolean->updateParents();
363 boolMap.put(boolean, boolean);
364 allBooleans.push(boolean);
365 return BooleanEdge(boolean);
368 return BooleanEdge(b);
372 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
373 Boolean *constraint = new BooleanOrder(order, first, second);
374 allBooleans.push(constraint);
375 return BooleanEdge(constraint);
378 void CSolver::addConstraint(BooleanEdge constraint) {
379 if (isTrue(constraint))
381 else if (isFalse(constraint))
384 if (constraint->type == LOGICOP) {
385 BooleanLogic *b=(BooleanLogic *) constraint.getBoolean();
386 if (!constraint.isNegated()) {
387 if (b->op==SATC_AND) {
388 for(uint i=0;i<b->inputs.getSize();i++) {
389 addConstraint(b->inputs.get(i));
395 addConstraint(doRewrite(constraint));
399 constraints.add(constraint);
400 Boolean *ptr=constraint.getBoolean();
402 if (ptr->boolVal == BV_UNSAT)
405 replaceBooleanWithTrueNoRemove(constraint);
406 constraint->parents.clear();
410 Order *CSolver::createOrder(OrderType type, Set *set) {
411 Order *order = new Order(type, set);
412 allOrders.push(order);
413 activeOrders.add(order);
417 int CSolver::solve() {
418 bool deleteTuner = false;
420 tuner = new DefaultTuner();
424 long long startTime = getTimeNano();
425 computePolarities(this);
430 DecomposeOrderTransform dot(this);
433 IntegerEncodingTransform iet(this);
436 naiveEncodingDecision(this);
437 satEncoder->encodeAllSATEncoder(this);
438 int result = unsat ? IS_UNSAT : satEncoder->solve();
439 long long finishTime = getTimeNano();
440 elapsedTime = finishTime - startTime;
448 uint64_t CSolver::getElementValue(Element *element) {
449 switch (element->type) {
453 return getElementValueSATTranslator(this, element);
460 bool CSolver::getBooleanValue(BooleanEdge bedge) {
461 Boolean *boolean=bedge.getBoolean();
462 switch (boolean->type) {
464 return getBooleanVariableValueSATTranslator(this, boolean);
471 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
472 return order->encoding.resolver->resolveOrder(first, second);
475 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
477 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
479 void CSolver::autoTune(uint budget) {
480 AutoTuner *autotuner = new AutoTuner(budget);
481 autotuner->addProblem(this);