3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "polarityassignment.h"
14 #include "transformer.h"
15 #include "autotuner.h"
20 boolTrue(new BooleanConst(true)),
21 boolFalse(new BooleanConst(false)),
26 satEncoder = new SATEncoder(this);
27 transformer = new Transformer(this);
30 /** This function tears down the solver and the entire AST */
33 uint size = allBooleans.getSize();
34 for (uint i = 0; i < size; i++) {
35 delete allBooleans.get(i);
38 size = allSets.getSize();
39 for (uint i = 0; i < size; i++) {
40 delete allSets.get(i);
43 size = allElements.getSize();
44 for (uint i = 0; i < size; i++) {
45 delete allElements.get(i);
48 size = allTables.getSize();
49 for (uint i = 0; i < size; i++) {
50 delete allTables.get(i);
53 size = allPredicates.getSize();
54 for (uint i = 0; i < size; i++) {
55 delete allPredicates.get(i);
58 size = allOrders.getSize();
59 for (uint i = 0; i < size; i++) {
60 delete allOrders.get(i);
63 size = allFunctions.getSize();
64 for (uint i = 0; i < size; i++) {
65 delete allFunctions.get(i);
74 CSolver *CSolver::clone() {
75 CSolver *copy = new CSolver();
77 SetIteratorBoolean *it = getConstraints();
78 while (it->hasNext()) {
79 Boolean *b = it->next();
80 copy->addConstraint(b->clone(copy, &map));
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 MutableSet *CSolver::createMutableSet(VarType type) {
99 MutableSet *set = new MutableSet(type);
104 void CSolver::addItem(MutableSet *set, uint64_t element) {
105 set->addElementMSet(element);
108 uint64_t CSolver::createUniqueItem(MutableSet *set) {
109 uint64_t element = set->getNewUniqueItem();
110 set->addElementMSet(element);
114 Element *CSolver::getElementVar(Set *set) {
115 Element *element = new ElementSet(set);
116 allElements.push(element);
120 Element *CSolver::getElementConst(VarType type, uint64_t value) {
121 uint64_t array[] = {value};
122 Set *set = new Set(type, array, 1);
123 Element *element = new ElementConst(value, type, set);
124 Element *e = elemMap.get(element);
127 allElements.push(element);
128 elemMap.put(element, element);
137 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
138 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
139 Element *e = elemMap.get(element);
141 allElements.push(element);
142 elemMap.put(element, element);
150 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
151 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
152 allFunctions.push(function);
156 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
157 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
158 allPredicates.push(predicate);
162 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
163 Predicate *predicate = new PredicateTable(table, behavior);
164 allPredicates.push(predicate);
168 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
169 Table *table = new Table(domains,numDomain,range);
170 allTables.push(table);
174 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
175 return createTable(domains, numDomain, NULL);
178 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
179 table->addNewTableEntry(inputs, inputSize, result);
182 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
183 Function *function = new FunctionTable(table, behavior);
184 allFunctions.push(function);
188 Boolean *CSolver::getBooleanVar(VarType type) {
189 Boolean *boolean = new BooleanVar(type);
190 allBooleans.push(boolean);
194 Boolean *CSolver::getBooleanTrue() {
198 Boolean *CSolver::getBooleanFalse() {
202 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
203 return applyPredicateTable(predicate, inputs, numInputs, NULL);
206 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
207 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
208 Boolean * b = boolMap.get(boolean);
210 boolMap.put(boolean, boolean);
211 allBooleans.push(boolean);
219 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
220 Boolean * newarray[asize];
223 if (array[0]->type == LOGICOP && ((BooleanLogic *)array[0])->op==SATC_NOT) {
224 return ((BooleanLogic *) array[0])->inputs.get(0);
225 } else if (array[0]->type == BOOLCONST) {
226 bool isTrue = ((BooleanConst *) array[0])->isTrue;
227 return isTrue ? boolFalse : boolTrue;
232 for(uint i=0;i<2;i++) {
233 if (array[i]->type == BOOLCONST) {
234 bool isTrue = ((BooleanConst *) array[i])->isTrue;
236 newarray[0]=array[1-i];
237 return applyLogicalOperation(SATC_NOT, newarray, 1);
246 for(uint i=0;i<asize;i++) {
248 if (b->type == BOOLCONST) {
249 bool isTrue = ((BooleanConst *) b)->isTrue;
255 newarray[newindex++]=b;
259 else if (newindex == 2) {
260 bool isNot0 = (newarray[0]->type==BOOLCONST) && ((BooleanLogic *)newarray[0])->op == SATC_NOT;
261 bool isNot1 = (newarray[1]->type==BOOLCONST) && ((BooleanLogic *)newarray[1])->op == SATC_NOT;
263 if (isNot0 != isNot1) {
265 newarray[0] = ((BooleanLogic *) newarray[0])->inputs.get(0);
267 Boolean *tmp = ((BooleanLogic *) array[1])->inputs.get(0);
271 return applyLogicalOperation(SATC_IMPLIES, newarray, 2);
281 for(uint i=0;i<asize;i++) {
283 if (b->type == BOOLCONST) {
284 bool isTrue = ((BooleanConst *) b)->isTrue;
290 newarray[newindex++]=b;
301 if (array[0]->type == BOOLCONST) {
302 BooleanConst *b=(BooleanConst *) array[0];
308 } else if (array[1]->type == BOOLCONST) {
309 BooleanConst *b=(BooleanConst *) array[0];
313 return applyLogicalOperation(SATC_NOT, array, 1);
320 Boolean *boolean = new BooleanLogic(this, op, array, asize);
321 Boolean *b = boolMap.get(boolean);
323 boolMap.put(boolean, boolean);
324 allBooleans.push(boolean);
332 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
333 Boolean *constraint = new BooleanOrder(order, first, second);
334 allBooleans.push(constraint);
338 void CSolver::addConstraint(Boolean *constraint) {
339 if (constraint == boolTrue)
341 else if (constraint == boolFalse)
344 constraints.add(constraint);
347 Order *CSolver::createOrder(OrderType type, Set *set) {
348 Order *order = new Order(type, set);
349 allOrders.push(order);
353 int CSolver::startEncoding() {
354 bool deleteTuner = false;
356 tuner = new DefaultTuner();
360 long long startTime = getTimeNano();
361 computePolarities(this);
362 transformer->orderAnalysis();
363 naiveEncodingDecision(this);
364 satEncoder->encodeAllSATEncoder(this);
365 int result = unsat ? IS_UNSAT : satEncoder->solve();
366 long long finishTime = getTimeNano();
367 elapsedTime = finishTime - startTime;
375 uint64_t CSolver::getElementValue(Element *element) {
376 switch (element->type) {
380 return getElementValueSATTranslator(this, element);
387 bool CSolver::getBooleanValue(Boolean *boolean) {
388 switch (boolean->type) {
390 return getBooleanVariableValueSATTranslator(this, boolean);
397 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
398 return getOrderConstraintValueSATTranslator(this, order, first, second);
401 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
403 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
405 void CSolver::autoTune(uint budget) {
406 AutoTuner * autotuner=new AutoTuner(budget);
407 autotuner->addProblem(this);