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 allElements.push(element);
147 elemMap.put(element, element);
155 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
156 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
157 allFunctions.push(function);
161 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
162 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
163 allPredicates.push(predicate);
167 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
168 Predicate *predicate = new PredicateTable(table, behavior);
169 allPredicates.push(predicate);
173 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
174 Table *table = new Table(domains,numDomain,range);
175 allTables.push(table);
179 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
180 return createTable(domains, numDomain, NULL);
183 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
184 table->addNewTableEntry(inputs, inputSize, result);
187 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
188 Function *function = new FunctionTable(table, behavior);
189 allFunctions.push(function);
193 BooleanEdge CSolver::getBooleanVar(VarType type) {
194 Boolean *boolean = new BooleanVar(type);
195 allBooleans.push(boolean);
196 return BooleanEdge(boolean);
199 BooleanEdge CSolver::getBooleanTrue() {
203 BooleanEdge CSolver::getBooleanFalse() {
207 BooleanEdge CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
208 return applyPredicateTable(predicate, inputs, numInputs, NULL);
211 BooleanEdge CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus) {
212 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
213 Boolean *b = boolMap.get(boolean);
215 boolMap.put(boolean, boolean);
216 allBooleans.push(boolean);
217 return BooleanEdge(boolean);
220 return BooleanEdge(b);
224 bool CSolver::isTrue(BooleanEdge b) {
225 return b.isNegated()?b->isFalse():b->isTrue();
228 bool CSolver::isFalse(BooleanEdge b) {
229 return b.isNegated()?b->isTrue():b->isFalse();
232 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
233 BooleanEdge array[] = {arg1, arg2};
234 return applyLogicalOperation(op, array, 2);
237 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
238 BooleanEdge array[] = {arg};
239 return applyLogicalOperation(op, array, 1);
242 static int ptrcompares(const void *p1, const void *p2) {
243 uintptr_t b1 = *(uintptr_t const *) p1;
244 uintptr_t b2 = *(uintptr_t const *) p2;
253 BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge * array, uint asize) {
254 BooleanEdge newarray[asize];
255 memcpy(newarray, array, asize * sizeof(BooleanEdge));
256 for(uint i=0; i < asize; i++) {
257 BooleanEdge b=newarray[i];
258 if (b->type == LOGICOP) {
259 if (((BooleanLogic *) b.getBoolean())->replaced) {
260 newarray[i] = doRewrite(newarray[i]);
265 return applyLogicalOperation(op, newarray, asize);
268 BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
269 BooleanEdge newarray[asize];
272 return array[0].negate();
275 for (uint i = 0; i < 2; i++) {
276 if (array[i]->type == BOOLCONST) {
277 if (array[i]->isTrue()) {
280 newarray[0] = array[1 - i];
281 return applyLogicalOperation(SATC_NOT, newarray, 1);
283 } else if (array[i]->type == LOGICOP) {
284 BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
286 return rewriteLogicalOperation(op, array, asize);
293 for (uint i =0; i <asize; i++) {
294 newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
296 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
300 for (uint i = 0; i < asize; i++) {
301 BooleanEdge b = array[i];
302 if (b->type == LOGICOP) {
303 if (((BooleanLogic *)b.getBoolean())->replaced)
304 return rewriteLogicalOperation(op, array, asize);
306 if (b->type == BOOLCONST) {
312 newarray[newindex++] = b;
316 } else if (newindex == 1) {
319 qsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
326 //handle by translation
327 return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
330 //handle by translation
331 return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
336 Boolean *boolean = new BooleanLogic(this, op, array, asize);
337 Boolean *b = boolMap.get(boolean);
339 boolMap.put(boolean, boolean);
340 allBooleans.push(boolean);
341 return BooleanEdge(boolean);
344 return BooleanEdge(b);
348 BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
349 Boolean *constraint = new BooleanOrder(order, first, second);
350 allBooleans.push(constraint);
351 return BooleanEdge(constraint);
354 void CSolver::addConstraint(BooleanEdge constraint) {
355 if (isTrue(constraint))
357 else if (isFalse(constraint))
360 if (constraint->type == LOGICOP) {
361 BooleanLogic *b=(BooleanLogic *) constraint.getBoolean();
362 if (!constraint.isNegated()) {
363 if (b->op==SATC_AND) {
364 for(uint i=0;i<b->inputs.getSize();i++) {
365 addConstraint(b->inputs.get(i));
371 addConstraint(doRewrite(constraint));
375 constraints.add(constraint);
376 Boolean *ptr=constraint.getBoolean();
378 if (ptr->boolVal == BV_UNSAT)
381 replaceBooleanWithTrueNoRemove(constraint);
382 constraint->parents.clear();
386 Order *CSolver::createOrder(OrderType type, Set *set) {
387 Order *order = new Order(type, set);
388 allOrders.push(order);
389 activeOrders.add(order);
393 int CSolver::solve() {
394 bool deleteTuner = false;
396 tuner = new DefaultTuner();
400 long long startTime = getTimeNano();
401 computePolarities(this);
403 // DecomposeOrderTransform dot(this);
404 // dot.doTransform();
406 IntegerEncodingTransform iet(this);
409 naiveEncodingDecision(this);
410 satEncoder->encodeAllSATEncoder(this);
411 int result = unsat ? IS_UNSAT : satEncoder->solve();
412 long long finishTime = getTimeNano();
413 elapsedTime = finishTime - startTime;
421 uint64_t CSolver::getElementValue(Element *element) {
422 switch (element->type) {
426 return getElementValueSATTranslator(this, element);
433 bool CSolver::getBooleanValue(BooleanEdge bedge) {
434 Boolean *boolean=bedge.getBoolean();
435 switch (boolean->type) {
437 return getBooleanVariableValueSATTranslator(this, boolean);
444 bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
445 return order->encoding.resolver->resolveOrder(first, second);
448 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
450 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
452 void CSolver::autoTune(uint budget) {
453 AutoTuner *autotuner = new AutoTuner(budget);
454 autotuner->addProblem(this);