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"
23 boolTrue(BooleanEdge(new BooleanConst(true))),
24 boolFalse(boolTrue.negate()),
29 satEncoder = new SATEncoder(this);
32 /** This function tears down the solver and the entire AST */
35 uint size = allBooleans.getSize();
36 for (uint i = 0; i < size; i++) {
37 delete allBooleans.get(i);
40 size = allSets.getSize();
41 for (uint i = 0; i < size; i++) {
42 delete allSets.get(i);
45 size = allElements.getSize();
46 for (uint i = 0; i < size; i++) {
47 delete allElements.get(i);
50 size = allTables.getSize();
51 for (uint i = 0; i < size; i++) {
52 delete allTables.get(i);
55 size = allPredicates.getSize();
56 for (uint i = 0; i < size; i++) {
57 delete allPredicates.get(i);
60 size = allOrders.getSize();
61 for (uint i = 0; i < size; i++) {
62 delete allOrders.get(i);
65 size = allFunctions.getSize();
66 for (uint i = 0; i < size; i++) {
67 delete allFunctions.get(i);
70 delete boolTrue.getBoolean();
74 CSolver *CSolver::clone() {
75 CSolver *copy = new CSolver();
77 SetIteratorBooleanEdge *it = getConstraints();
78 while (it->hasNext()) {
79 BooleanEdge b = it->next();
80 copy->addConstraint(cloneEdge(copy, &map, b));
86 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
87 Set *set = new Set(type, elements, numelements);
92 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
93 Set *set = new Set(type, lowrange, highrange);
98 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
99 Set *s = createRangeSet(type, lowrange, highrange);
100 return getElementVar(s);
103 MutableSet *CSolver::createMutableSet(VarType type) {
104 MutableSet *set = new MutableSet(type);
109 void CSolver::addItem(MutableSet *set, uint64_t element) {
110 set->addElementMSet(element);
113 uint64_t CSolver::createUniqueItem(MutableSet *set) {
114 uint64_t element = set->getNewUniqueItem();
115 set->addElementMSet(element);
119 Element *CSolver::getElementVar(Set *set) {
120 Element *element = new ElementSet(set);
121 allElements.push(element);
125 Element *CSolver::getElementConst(VarType type, uint64_t value) {
126 uint64_t array[] = {value};
127 Set *set = new Set(type, array, 1);
128 Element *element = new ElementConst(value, type, set);
129 Element *e = elemMap.get(element);
132 allElements.push(element);
133 elemMap.put(element, element);
142 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
143 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
144 Element *e = elemMap.get(element);
146 element->updateParents();
147 allElements.push(element);
148 elemMap.put(element, element);
156 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
157 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
158 allFunctions.push(function);
162 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
163 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
164 allPredicates.push(predicate);
168 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
169 Predicate *predicate = new PredicateTable(table, behavior);
170 allPredicates.push(predicate);
174 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
175 Table *table = new Table(domains,numDomain,range);
176 allTables.push(table);
180 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
181 return createTable(domains, numDomain, NULL);
184 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
185 table->addNewTableEntry(inputs, inputSize, result);
188 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
189 Function *function = new FunctionTable(table, behavior);
190 allFunctions.push(function);
194 BooleanEdge CSolver::getBooleanVar(VarType type) {
195 Boolean *boolean = new BooleanVar(type);
196 allBooleans.push(boolean);
197 return BooleanEdge(boolean);
200 BooleanEdge CSolver::getBooleanTrue() {
204 BooleanEdge CSolver::getBooleanFalse() {
208 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
209 return applyPredicateTable(predicate, inputs, numInputs, NULL);
212 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
213 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
214 Boolean *b = boolMap.get(boolean);
216 boolean->updateParents();
217 boolMap.put(boolean, boolean);
218 allBooleans.push(boolean);
219 return BooleanEdge(boolean);
222 return BooleanEdge(b);
226 bool CSolver::isTrue(BooleanEdge b) {
227 return b.isNegated()?b->isFalse():b->isTrue();
230 bool CSolver::isFalse(BooleanEdge b) {
231 return b.isNegated()?b->isTrue():b->isFalse();
234 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
235 BooleanEdge array[] = {arg1, arg2};
236 return applyLogicalOperation(op, array, 2);
239 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
240 BooleanEdge array[] = {arg};
241 return applyLogicalOperation(op, array, 1);
244 static int ptrcompares(const void *p1, const void *p2) {
245 uintptr_t b1 = *(uintptr_t const *) p1;
246 uintptr_t b2 = *(uintptr_t const *) p2;
255 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge * array, uint asize) {
256 BooleanEdge newarray[asize];
257 memcpy(newarray, array, asize * sizeof(BooleanEdge));
258 for(uint i=0; i < asize; i++) {
259 BooleanEdge b=newarray[i];
260 if (b->type == LOGICOP) {
261 if (((BooleanLogic *) b.getBoolean())->replaced) {
262 newarray[i] = doRewrite(newarray[i]);
267 return applyLogicalOperation(op, newarray, asize);
270 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
271 BooleanEdge newarray[asize];
274 return array[0].negate();
277 for (uint i = 0; i < 2; i++) {
278 if (array[i]->type == BOOLCONST) {
279 if (array[i]->isTrue()) {
282 newarray[0] = array[1 - i];
283 return applyLogicalOperation(SATC_NOT, newarray, 1);
285 } else if (array[i]->type == LOGICOP) {
286 BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
288 return rewriteLogicalOperation(op, array, asize);
295 for (uint i =0; i <asize; i++) {
296 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
298 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
302 for (uint i = 0; i < asize; i++) {
303 BooleanEdge b = array[i];
304 if (b->type == LOGICOP) {
305 if (((BooleanLogic *)b.getBoolean())->replaced)
306 return rewriteLogicalOperation(op, array, asize);
308 if (b->type == BOOLCONST) {
314 newarray[newindex++] = b;
318 } else if (newindex == 1) {
321 qsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
328 //handle by translation
329 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
332 //handle by translation
333 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
338 Boolean *boolean = new BooleanLogic(this, op, array, asize);
339 Boolean *b = boolMap.get(boolean);
341 boolean->updateParents();
342 boolMap.put(boolean, boolean);
343 allBooleans.push(boolean);
344 return BooleanEdge(boolean);
347 return BooleanEdge(b);
351 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
352 Boolean *constraint = new BooleanOrder(order, first, second);
353 allBooleans.push(constraint);
354 return BooleanEdge(constraint);
357 void CSolver::addConstraint(BooleanEdge constraint) {
358 if (isTrue(constraint))
360 else if (isFalse(constraint))
363 if (constraint->type == LOGICOP) {
364 BooleanLogic *b=(BooleanLogic *) constraint.getBoolean();
365 if (!constraint.isNegated()) {
366 if (b->op==SATC_AND) {
367 for(uint i=0;i<b->inputs.getSize();i++) {
368 addConstraint(b->inputs.get(i));
374 addConstraint(doRewrite(constraint));
378 constraints.add(constraint);
379 Boolean *ptr=constraint.getBoolean();
381 if (ptr->boolVal == BV_UNSAT)
384 replaceBooleanWithTrueNoRemove(constraint);
385 constraint->parents.clear();
389 Order *CSolver::createOrder(OrderType type, Set *set) {
390 Order *order = new Order(type, set);
391 allOrders.push(order);
392 activeOrders.add(order);
396 int CSolver::solve() {
397 bool deleteTuner = false;
399 tuner = new DefaultTuner();
403 long long startTime = getTimeNano();
404 computePolarities(this);
406 DecomposeOrderTransform dot(this);
409 IntegerEncodingTransform iet(this);
412 naiveEncodingDecision(this);
413 satEncoder->encodeAllSATEncoder(this);
414 int result = unsat ? IS_UNSAT : satEncoder->solve();
415 long long finishTime = getTimeNano();
416 elapsedTime = finishTime - startTime;
424 uint64_t CSolver::getElementValue(Element *element) {
425 switch (element->type) {
429 return getElementValueSATTranslator(this, element);
436 bool CSolver::getBooleanValue(BooleanEdge bedge) {
437 Boolean *boolean=bedge.getBoolean();
438 switch (boolean->type) {
440 return getBooleanVariableValueSATTranslator(this, boolean);
447 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
448 return order->encoding.resolver->resolveOrder(first, second);
451 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
453 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
455 void CSolver::autoTune(uint budget) {
456 AutoTuner *autotuner = new AutoTuner(budget);
457 autotuner->addProblem(this);