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"
24 boolTrue(BooleanEdge(new BooleanConst(true))),
25 boolFalse(boolTrue.negate()),
30 satEncoder = new SATEncoder(this);
33 /** This function tears down the solver and the entire AST */
36 uint size = allBooleans.getSize();
37 for (uint i = 0; i < size; i++) {
38 delete allBooleans.get(i);
41 size = allSets.getSize();
42 for (uint i = 0; i < size; i++) {
43 delete allSets.get(i);
46 size = allElements.getSize();
47 for (uint i = 0; i < size; i++) {
48 delete allElements.get(i);
51 size = allTables.getSize();
52 for (uint i = 0; i < size; i++) {
53 delete allTables.get(i);
56 size = allPredicates.getSize();
57 for (uint i = 0; i < size; i++) {
58 delete allPredicates.get(i);
61 size = allOrders.getSize();
62 for (uint i = 0; i < size; i++) {
63 delete allOrders.get(i);
66 size = allFunctions.getSize();
67 for (uint i = 0; i < size; i++) {
68 delete allFunctions.get(i);
71 delete boolTrue.getBoolean();
75 CSolver *CSolver::clone() {
76 CSolver *copy = new CSolver();
78 SetIteratorBooleanEdge *it = getConstraints();
79 while (it->hasNext()) {
80 BooleanEdge b = it->next();
81 copy->addConstraint(cloneEdge(copy, &map, b));
87 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
88 Set *set = new Set(type, elements, numelements);
93 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
94 Set *set = new Set(type, lowrange, highrange);
99 Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
100 Set *s = createRangeSet(type, lowrange, highrange);
101 return getElementVar(s);
104 MutableSet *CSolver::createMutableSet(VarType type) {
105 MutableSet *set = new MutableSet(type);
110 void CSolver::addItem(MutableSet *set, uint64_t element) {
111 set->addElementMSet(element);
114 uint64_t CSolver::createUniqueItem(MutableSet *set) {
115 uint64_t element = set->getNewUniqueItem();
116 set->addElementMSet(element);
120 Element *CSolver::getElementVar(Set *set) {
121 Element *element = new ElementSet(set);
122 allElements.push(element);
126 Element *CSolver::getElementConst(VarType type, uint64_t value) {
127 uint64_t array[] = {value};
128 Set *set = new Set(type, array, 1);
129 Element *element = new ElementConst(value, set);
130 Element *e = elemMap.get(element);
133 allElements.push(element);
134 elemMap.put(element, element);
143 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus) {
144 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
145 Element *e = elemMap.get(element);
147 element->updateParents();
148 allElements.push(element);
149 elemMap.put(element, element);
157 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
158 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
159 allFunctions.push(function);
163 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
164 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
165 allPredicates.push(predicate);
169 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
170 Predicate *predicate = new PredicateTable(table, behavior);
171 allPredicates.push(predicate);
175 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
176 Table *table = new Table(domains,numDomain,range);
177 allTables.push(table);
181 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
182 return createTable(domains, numDomain, NULL);
185 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
186 table->addNewTableEntry(inputs, inputSize, result);
189 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
190 Function *function = new FunctionTable(table, behavior);
191 allFunctions.push(function);
195 BooleanEdge CSolver::getBooleanVar(VarType type) {
196 Boolean *boolean = new BooleanVar(type);
197 allBooleans.push(boolean);
198 return BooleanEdge(boolean);
201 BooleanEdge CSolver::getBooleanTrue() {
205 BooleanEdge CSolver::getBooleanFalse() {
209 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
210 return applyPredicateTable(predicate, inputs, numInputs, NULL);
213 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
214 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
215 Boolean *b = boolMap.get(boolean);
217 boolean->updateParents();
218 boolMap.put(boolean, boolean);
219 allBooleans.push(boolean);
220 return BooleanEdge(boolean);
223 return BooleanEdge(b);
227 bool CSolver::isTrue(BooleanEdge b) {
228 return b.isNegated()?b->isFalse():b->isTrue();
231 bool CSolver::isFalse(BooleanEdge b) {
232 return b.isNegated()?b->isTrue():b->isFalse();
235 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
236 BooleanEdge array[] = {arg1, arg2};
237 return applyLogicalOperation(op, array, 2);
240 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
241 BooleanEdge array[] = {arg};
242 return applyLogicalOperation(op, array, 1);
245 static int ptrcompares(const void *p1, const void *p2) {
246 uintptr_t b1 = *(uintptr_t const *) p1;
247 uintptr_t b2 = *(uintptr_t const *) p2;
256 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge * array, uint asize) {
257 BooleanEdge newarray[asize];
258 memcpy(newarray, array, asize * sizeof(BooleanEdge));
259 for(uint i=0; i < asize; i++) {
260 BooleanEdge b=newarray[i];
261 if (b->type == LOGICOP) {
262 if (((BooleanLogic *) b.getBoolean())->replaced) {
263 newarray[i] = doRewrite(newarray[i]);
268 return applyLogicalOperation(op, newarray, asize);
271 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
272 BooleanEdge newarray[asize];
275 return array[0].negate();
278 for (uint i = 0; i < 2; i++) {
279 if (array[i]->type == BOOLCONST) {
280 if (array[i]->isTrue()) {
283 newarray[0] = array[1 - i];
284 return applyLogicalOperation(SATC_NOT, newarray, 1);
286 } else if (array[i]->type == LOGICOP) {
287 BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
289 return rewriteLogicalOperation(op, array, asize);
296 for (uint i =0; i <asize; i++) {
297 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
299 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
303 for (uint i = 0; i < asize; i++) {
304 BooleanEdge b = array[i];
305 if (b->type == LOGICOP) {
306 if (((BooleanLogic *)b.getBoolean())->replaced)
307 return rewriteLogicalOperation(op, array, asize);
309 if (b->type == BOOLCONST) {
315 newarray[newindex++] = b;
319 } else if (newindex == 1) {
322 bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
329 //handle by translation
330 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
333 //handle by translation
334 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
339 Boolean *boolean = new BooleanLogic(this, op, array, asize);
340 Boolean *b = boolMap.get(boolean);
342 boolean->updateParents();
343 boolMap.put(boolean, boolean);
344 allBooleans.push(boolean);
345 return BooleanEdge(boolean);
348 return BooleanEdge(b);
352 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
353 Boolean *constraint = new BooleanOrder(order, first, second);
354 allBooleans.push(constraint);
355 return BooleanEdge(constraint);
358 void CSolver::addConstraint(BooleanEdge constraint) {
359 if (isTrue(constraint))
361 else if (isFalse(constraint))
364 if (constraint->type == LOGICOP) {
365 BooleanLogic *b=(BooleanLogic *) constraint.getBoolean();
366 if (!constraint.isNegated()) {
367 if (b->op==SATC_AND) {
368 for(uint i=0;i<b->inputs.getSize();i++) {
369 addConstraint(b->inputs.get(i));
375 addConstraint(doRewrite(constraint));
379 constraints.add(constraint);
380 Boolean *ptr=constraint.getBoolean();
382 if (ptr->boolVal == BV_UNSAT)
385 replaceBooleanWithTrueNoRemove(constraint);
386 constraint->parents.clear();
390 Order *CSolver::createOrder(OrderType type, Set *set) {
391 Order *order = new Order(type, set);
392 allOrders.push(order);
393 activeOrders.add(order);
397 int CSolver::solve() {
398 bool deleteTuner = false;
400 tuner = new DefaultTuner();
404 long long startTime = getTimeNano();
405 computePolarities(this);
410 DecomposeOrderTransform dot(this);
413 IntegerEncodingTransform iet(this);
416 naiveEncodingDecision(this);
417 satEncoder->encodeAllSATEncoder(this);
418 int result = unsat ? IS_UNSAT : satEncoder->solve();
419 long long finishTime = getTimeNano();
420 elapsedTime = finishTime - startTime;
428 uint64_t CSolver::getElementValue(Element *element) {
429 switch (element->type) {
433 return getElementValueSATTranslator(this, element);
440 bool CSolver::getBooleanValue(BooleanEdge bedge) {
441 Boolean *boolean=bedge.getBoolean();
442 switch (boolean->type) {
444 return getBooleanVariableValueSATTranslator(this, boolean);
451 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
452 return order->encoding.resolver->resolveOrder(first, second);
455 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
457 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
459 void CSolver::autoTune(uint budget) {
460 AutoTuner *autotuner = new AutoTuner(budget);
461 autotuner->addProblem(this);