3d589b18f94da8668ecfc1233eef4c05ee3ee243
[satune.git] / src / csolver.cc
1 #include "csolver.h"
2 #include "set.h"
3 #include "mutableset.h"
4 #include "element.h"
5 #include "boolean.h"
6 #include "predicate.h"
7 #include "order.h"
8 #include "table.h"
9 #include "function.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
12 #include "tunable.h"
13 #include "polarityassignment.h"
14 #include "transformer.h"
15 #include "autotuner.h"
16 #include "astops.h"
17 #include "structs.h"
18
19 CSolver::CSolver() :
20         boolTrue(new BooleanConst(true)),
21         boolFalse(new BooleanConst(false)),
22         unsat(false),
23         tuner(NULL),
24         elapsedTime(0)
25 {
26         satEncoder = new SATEncoder(this);
27         transformer = new Transformer(this);
28 }
29
30 /** This function tears down the solver and the entire AST */
31
32 CSolver::~CSolver() {
33         uint size = allBooleans.getSize();
34         for (uint i = 0; i < size; i++) {
35                 delete allBooleans.get(i);
36         }
37
38         size = allSets.getSize();
39         for (uint i = 0; i < size; i++) {
40                 delete allSets.get(i);
41         }
42
43         size = allElements.getSize();
44         for (uint i = 0; i < size; i++) {
45                 delete allElements.get(i);
46         }
47
48         size = allTables.getSize();
49         for (uint i = 0; i < size; i++) {
50                 delete allTables.get(i);
51         }
52
53         size = allPredicates.getSize();
54         for (uint i = 0; i < size; i++) {
55                 delete allPredicates.get(i);
56         }
57
58         size = allOrders.getSize();
59         for (uint i = 0; i < size; i++) {
60                 delete allOrders.get(i);
61         }
62
63         size = allFunctions.getSize();
64         for (uint i = 0; i < size; i++) {
65                 delete allFunctions.get(i);
66         }
67
68         delete boolTrue;
69         delete boolFalse;
70         delete satEncoder;
71         delete transformer;
72 }
73
74 CSolver *CSolver::clone() {
75         CSolver *copy = new CSolver();
76         CloneMap map;
77         SetIteratorBoolean *it = getConstraints();
78         while (it->hasNext()) {
79                 Boolean *b = it->next();
80                 copy->addConstraint(b->clone(copy, &map));
81         }
82         delete it;
83         return copy;
84 }
85
86 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
87         Set *set = new Set(type, elements, numelements);
88         allSets.push(set);
89         return set;
90 }
91
92 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
93         Set *set = new Set(type, lowrange, highrange);
94         allSets.push(set);
95         return set;
96 }
97
98 MutableSet *CSolver::createMutableSet(VarType type) {
99         MutableSet *set = new MutableSet(type);
100         allSets.push(set);
101         return set;
102 }
103
104 void CSolver::addItem(MutableSet *set, uint64_t element) {
105         set->addElementMSet(element);
106 }
107
108 uint64_t CSolver::createUniqueItem(MutableSet *set) {
109         uint64_t element = set->getNewUniqueItem();
110         set->addElementMSet(element);
111         return element;
112 }
113
114 Element *CSolver::getElementVar(Set *set) {
115         Element *element = new ElementSet(set);
116         allElements.push(element);
117         return element;
118 }
119
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);
125         if (e == NULL) {
126                 allSets.push(set);
127                 allElements.push(element);
128                 elemMap.put(element, element);
129                 return element;
130         } else {
131                 delete set;
132                 delete element;
133                 return e;
134         }
135 }
136
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);
140         if (e == NULL) {
141                 allElements.push(element);
142                 elemMap.put(element, element);
143                 return element;
144         } else {
145                 delete element;
146                 return e;
147         }
148 }
149
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);
153         return function;
154 }
155
156 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
157         Predicate *predicate = new PredicateOperator(op, domain,numDomain);
158         allPredicates.push(predicate);
159         return predicate;
160 }
161
162 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
163         Predicate *predicate = new PredicateTable(table, behavior);
164         allPredicates.push(predicate);
165         return predicate;
166 }
167
168 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
169         Table *table = new Table(domains,numDomain,range);
170         allTables.push(table);
171         return table;
172 }
173
174 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
175         return createTable(domains, numDomain, NULL);
176 }
177
178 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
179         table->addNewTableEntry(inputs, inputSize, result);
180 }
181
182 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
183         Function *function = new FunctionTable(table, behavior);
184         allFunctions.push(function);
185         return function;
186 }
187
188 Boolean *CSolver::getBooleanVar(VarType type) {
189         Boolean *boolean = new BooleanVar(type);
190         allBooleans.push(boolean);
191         return boolean;
192 }
193
194 Boolean *CSolver::getBooleanTrue() {
195         return boolTrue;
196 }
197
198 Boolean *CSolver::getBooleanFalse() {
199         return boolFalse;
200 }
201
202 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
203         return applyPredicateTable(predicate, inputs, numInputs, NULL);
204 }
205
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);
209         if (b == NULL) {
210                 boolMap.put(boolean, boolean);
211                 allBooleans.push(boolean);
212                 return boolean;
213         } else {
214                 delete boolean;
215                 return b;
216         }
217 }
218
219 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
220         Boolean * newarray[asize];
221         switch(op) {
222         case SATC_NOT: {
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;
228                 }
229                 break;
230         }
231         case SATC_XOR: {
232                 for(uint i=0;i<2;i++) {
233                         if (array[i]->type == BOOLCONST) {
234                                 bool isTrue = ((BooleanConst *) array[i])->isTrue;
235                                 if (isTrue) {
236                                         newarray[0]=array[1-i];
237                                         return applyLogicalOperation(SATC_NOT, newarray, 1);
238                                 } else
239                                         return array[1-i];
240                         }
241                 }
242                 break;
243         }
244         case SATC_OR: {
245                 uint newindex=0;
246                 for(uint i=0;i<asize;i++) {
247                         Boolean *b=array[i];
248                         if (b->type == BOOLCONST) {
249                                 bool isTrue = ((BooleanConst *) b)->isTrue;
250                                 if (isTrue)
251                                         return b;
252                                 else
253                                         continue;
254                         } else
255                                 newarray[newindex++]=b;
256                 }
257                 if (newindex==1)
258                         return newarray[0];
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;
262
263                         if (isNot0 != isNot1) {
264                                 if (isNot0) {
265                                         newarray[0] = ((BooleanLogic *) newarray[0])->inputs.get(0);
266                                 } else {
267                                         Boolean *tmp =  ((BooleanLogic *) array[1])->inputs.get(0);
268                                         array[1] = array[0];
269                                         array[0] = tmp;
270                                 }
271                                 return applyLogicalOperation(SATC_IMPLIES, newarray, 2);
272                         }
273                 } else {
274                         array = newarray;
275                         asize = newindex;
276                 }
277                 break;
278         }
279         case SATC_AND: {
280                 uint newindex=0;
281                 for(uint i=0;i<asize;i++) {
282                         Boolean *b=array[i];
283                         if (b->type == BOOLCONST) {
284                                 bool isTrue = ((BooleanConst *) b)->isTrue;
285                                 if (isTrue)
286                                         continue;
287                                 else
288                                         return b;
289                         } else
290                                 newarray[newindex++]=b;
291                 }
292                 if(newindex==1) {
293                         return newarray[0];
294                 } else {
295                         array = newarray;
296                         asize = newindex;
297                 }
298                 break;
299         }
300         case SATC_IMPLIES: {
301                 if (array[0]->type == BOOLCONST) {
302                         BooleanConst *b=(BooleanConst *) array[0];
303                         if (b->isTrue) {
304                                 return array[1];
305                         } else {
306                                 return boolTrue;
307                         }
308                 } else if (array[1]->type == BOOLCONST) {
309                         BooleanConst *b=(BooleanConst *) array[0];
310                         if (b->isTrue) {
311                                 return b;
312                         } else {
313                                 return applyLogicalOperation(SATC_NOT, array, 1);
314                         }
315                 }
316                 break;
317         }
318         }
319         
320         Boolean *boolean = new BooleanLogic(this, op, array, asize);
321         Boolean *b = boolMap.get(boolean);
322         if (b == NULL) {
323                 boolMap.put(boolean, boolean);
324                 allBooleans.push(boolean);
325                 return boolean;         
326         } else {
327                 delete boolean;
328                 return b;
329         }
330 }
331
332 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
333         Boolean *constraint = new BooleanOrder(order, first, second);
334         allBooleans.push(constraint);
335         return constraint;
336 }
337
338 void CSolver::addConstraint(Boolean *constraint) {
339         if (constraint == boolTrue)
340                 return;
341         else if (constraint == boolFalse)
342                 setUnSAT();
343         else
344                 constraints.add(constraint);
345 }
346
347 Order *CSolver::createOrder(OrderType type, Set *set) {
348         Order *order = new Order(type, set);
349         allOrders.push(order);
350         return order;
351 }
352
353 int CSolver::startEncoding() {
354         bool deleteTuner = false;
355         if (tuner == NULL) {
356                 tuner = new DefaultTuner();
357                 deleteTuner = true;
358         }
359                 
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;
368         if (deleteTuner) {
369                 delete tuner;
370                 tuner = NULL;
371         }
372         return result;
373 }
374
375 uint64_t CSolver::getElementValue(Element *element) {
376         switch (element->type) {
377         case ELEMSET:
378         case ELEMCONST:
379         case ELEMFUNCRETURN:
380                 return getElementValueSATTranslator(this, element);
381         default:
382                 ASSERT(0);
383         }
384         exit(-1);
385 }
386
387 bool CSolver::getBooleanValue(Boolean *boolean) {
388         switch (boolean->type) {
389         case BOOLEANVAR:
390                 return getBooleanVariableValueSATTranslator(this, boolean);
391         default:
392                 ASSERT(0);
393         }
394         exit(-1);
395 }
396
397 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
398         return getOrderConstraintValueSATTranslator(this, order, first, second);
399 }
400
401 long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
402
403 long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
404
405 void CSolver::autoTune(uint budget) {
406         AutoTuner * autotuner=new AutoTuner(budget);
407         autotuner->addProblem(this);
408         autotuner->tune();
409         delete autotuner;
410 }