3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "polarityassignment.h"
14 #include "orderdecompose.h"
15 #include "autotuner.h"
18 boolTrue(new BooleanConst(true)),
19 boolFalse(new BooleanConst(false)),
24 satEncoder = new SATEncoder(this);
27 /** This function tears down the solver and the entire AST */
30 uint size = allBooleans.getSize();
31 for (uint i = 0; i < size; i++) {
32 delete allBooleans.get(i);
35 size = allSets.getSize();
36 for (uint i = 0; i < size; i++) {
37 delete allSets.get(i);
40 size = allElements.getSize();
41 for (uint i = 0; i < size; i++) {
42 delete allElements.get(i);
45 size = allTables.getSize();
46 for (uint i = 0; i < size; i++) {
47 delete allTables.get(i);
50 size = allPredicates.getSize();
51 for (uint i = 0; i < size; i++) {
52 delete allPredicates.get(i);
55 size = allOrders.getSize();
56 for (uint i = 0; i < size; i++) {
57 delete allOrders.get(i);
60 size = allFunctions.getSize();
61 for (uint i = 0; i < size; i++) {
62 delete allFunctions.get(i);
70 CSolver *CSolver::clone() {
71 CSolver *copy = new CSolver();
73 HSIteratorBoolean *it = getConstraints();
74 while (it->hasNext()) {
75 Boolean *b = it->next();
76 copy->addConstraint(b->clone(copy, &map));
82 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
83 Set *set = new Set(type, elements, numelements);
88 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
89 Set *set = new Set(type, lowrange, highrange);
94 MutableSet *CSolver::createMutableSet(VarType type) {
95 MutableSet *set = new MutableSet(type);
100 void CSolver::addItem(MutableSet *set, uint64_t element) {
101 set->addElementMSet(element);
104 uint64_t CSolver::createUniqueItem(MutableSet *set) {
105 uint64_t element = set->low++;
106 set->addElementMSet(element);
110 Element *CSolver::getElementVar(Set *set) {
111 Element *element = new ElementSet(set);
112 allElements.push(element);
116 Element *CSolver::getElementConst(VarType type, uint64_t value) {
117 uint64_t array[] = {value};
118 Set *set = new Set(type, array, 1);
119 Element *element = new ElementConst(value, type, set);
120 Element *e = elemMap.get(element);
123 allElements.push(element);
124 elemMap.put(element, element);
133 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
134 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
135 Element *e = elemMap.get(element);
137 allElements.push(element);
138 elemMap.put(element, element);
146 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
147 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
148 allFunctions.push(function);
152 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
153 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
154 allPredicates.push(predicate);
158 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
159 Predicate *predicate = new PredicateTable(table, behavior);
160 allPredicates.push(predicate);
164 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
165 Table *table = new Table(domains,numDomain,range);
166 allTables.push(table);
170 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
171 return createTable(domains, numDomain, NULL);
174 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
175 table->addNewTableEntry(inputs, inputSize, result);
178 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
179 Function *function = new FunctionTable(table, behavior);
180 allFunctions.push(function);
184 Boolean *CSolver::getBooleanVar(VarType type) {
185 Boolean *boolean = new BooleanVar(type);
186 allBooleans.push(boolean);
190 Boolean *CSolver::getBooleanTrue() {
194 Boolean *CSolver::getBooleanFalse() {
198 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
199 return applyPredicateTable(predicate, inputs, numInputs, NULL);
202 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
203 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
204 Boolean * b = boolMap.get(boolean);
206 boolMap.put(boolean, boolean);
207 allBooleans.push(boolean);
215 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
216 Boolean * newarray[asize];
219 if (array[0]->type == LOGICOP && ((BooleanLogic *)array[0])->op==L_NOT) {
220 return ((BooleanLogic *) array[0])->inputs.get(0);
221 } else if (array[0]->type == BOOLCONST) {
222 bool isTrue = ((BooleanConst *) array[0])->isTrue;
223 return isTrue ? boolFalse : boolTrue;
228 for(uint i=0;i<2;i++) {
229 if (array[i]->type == BOOLCONST) {
230 bool isTrue = ((BooleanConst *) array[i])->isTrue;
232 newarray[0]=array[1-i];
233 return applyLogicalOperation(L_NOT, newarray, 1);
242 for(uint i=0;i<asize;i++) {
244 if (b->type == BOOLCONST) {
245 bool isTrue = ((BooleanConst *) b)->isTrue;
251 newarray[newindex++]=b;
255 else if (newindex == 2) {
256 bool isNot0 = (newarray[0]->type==BOOLCONST) && ((BooleanLogic *)newarray[0])->op == L_NOT;
257 bool isNot1 = (newarray[1]->type==BOOLCONST) && ((BooleanLogic *)newarray[1])->op == L_NOT;
259 if (isNot0 != isNot1) {
261 newarray[0] = ((BooleanLogic *) newarray[0])->inputs.get(0);
263 Boolean *tmp = ((BooleanLogic *) array[1])->inputs.get(0);
267 return applyLogicalOperation(L_IMPLIES, newarray, 2);
277 for(uint i=0;i<asize;i++) {
279 if (b->type == BOOLCONST) {
280 bool isTrue = ((BooleanConst *) b)->isTrue;
286 newarray[newindex++]=b;
297 if (array[0]->type == BOOLCONST) {
298 BooleanConst *b=(BooleanConst *) array[0];
304 } else if (array[1]->type == BOOLCONST) {
305 BooleanConst *b=(BooleanConst *) array[0];
309 return applyLogicalOperation(L_NOT, array, 1);
316 Boolean *boolean = new BooleanLogic(this, op, array, asize);
317 Boolean *b = boolMap.get(boolean);
319 boolMap.put(boolean, boolean);
320 allBooleans.push(boolean);
328 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
329 Boolean *constraint = new BooleanOrder(order, first, second);
330 allBooleans.push(constraint);
334 void CSolver::addConstraint(Boolean *constraint) {
335 if (constraint == boolTrue)
337 else if (constraint == boolFalse)
340 constraints.add(constraint);
343 Order *CSolver::createOrder(OrderType type, Set *set) {
344 Order *order = new Order(type, set);
345 allOrders.push(order);
349 int CSolver::startEncoding() {
350 bool deleteTuner = false;
352 tuner = new DefaultTuner();
356 long long startTime = getTimeNano();
357 computePolarities(this);
359 naiveEncodingDecision(this);
360 satEncoder->encodeAllSATEncoder(this);
361 int result = unsat ? IS_UNSAT : satEncoder->solve();
362 long long finishTime = getTimeNano();
363 elapsedTime = finishTime - startTime;
371 uint64_t CSolver::getElementValue(Element *element) {
372 switch (element->type) {
376 return getElementValueSATTranslator(this, element);
383 bool CSolver::getBooleanValue(Boolean *boolean) {
384 switch (boolean->type) {
386 return getBooleanVariableValueSATTranslator(this, boolean);
393 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
394 return getOrderConstraintValueSATTranslator(this, order, first, second);
397 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
399 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
401 void CSolver::autoTune(uint budget) {
402 AutoTuner * autotuner=new AutoTuner(budget);
403 autotuner->addProblem(this);