Work on canonicalizing the AST so our analysis can handle fewer cases
[satune.git] / src / csolver.cc
index 814a140a1499189a8a387db86822a5be53bb02d2..c3208b91ec5fdd2747ffc763fa3e3b52dd60291a 100644 (file)
 #include "sattranslator.h"
 #include "tunable.h"
 #include "polarityassignment.h"
-#include "analyzer.h"
-
-CSolver::CSolver() : unsat(false) {
-       tuner = new Tuner();
-       satEncoder = allocSATEncoder(this);
+#include "decomposeordertransform.h"
+#include "autotuner.h"
+#include "astops.h"
+#include "structs.h"
+#include "orderresolver.h"
+#include "integerencoding.h"
+#include <stdlib.h>
+
+CSolver::CSolver() :
+       boolTrue(new BooleanConst(true)),
+       boolFalse(new BooleanConst(false)),
+       unsat(false),
+       tuner(NULL),
+       elapsedTime(0)
+{
+       satEncoder = new SATEncoder(this);
 }
 
 /** This function tears down the solver and the entire AST */
@@ -56,8 +67,21 @@ CSolver::~CSolver() {
                delete allFunctions.get(i);
        }
 
-       deleteSATEncoder(satEncoder);
-       delete tuner;
+       delete boolTrue;
+       delete boolFalse;
+       delete satEncoder;
+}
+
+CSolver *CSolver::clone() {
+       CSolver *copy = new CSolver();
+       CloneMap map;
+       SetIteratorBoolean *it = getConstraints();
+       while (it->hasNext()) {
+               Boolean *b = it->next();
+               copy->addConstraint(b->clone(copy, &map));
+       }
+       delete it;
+       return copy;
 }
 
 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
@@ -72,6 +96,11 @@ Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange
        return set;
 }
 
+Element *CSolver::createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange) {
+       Set *s = createRangeSet(type, lowrange, highrange);
+       return getElementVar(s);
+}
+
 MutableSet *CSolver::createMutableSet(VarType type) {
        MutableSet *set = new MutableSet(type);
        allSets.push(set);
@@ -83,7 +112,7 @@ void CSolver::addItem(MutableSet *set, uint64_t element) {
 }
 
 uint64_t CSolver::createUniqueItem(MutableSet *set) {
-       uint64_t element = set->low++;
+       uint64_t element = set->getNewUniqueItem();
        set->addElementMSet(element);
        return element;
 }
@@ -95,15 +124,33 @@ Element *CSolver::getElementVar(Set *set) {
 }
 
 Element *CSolver::getElementConst(VarType type, uint64_t value) {
-       Element *element = new ElementConst(value, type);
-       allElements.push(element);
-       return element;
+       uint64_t array[] = {value};
+       Set *set = new Set(type, array, 1);
+       Element *element = new ElementConst(value, type, set);
+       Element *e = elemMap.get(element);
+       if (e == NULL) {
+               allSets.push(set);
+               allElements.push(element);
+               elemMap.put(element, element);
+               return element;
+       } else {
+               delete set;
+               delete element;
+               return e;
+       }
 }
 
-Boolean *CSolver::getBooleanVar(VarType type) {
-       Boolean *boolean = new BooleanVar(type);
-       allBooleans.push(boolean);
-       return boolean;
+Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
+       Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
+       Element *e = elemMap.get(element);
+       if (e == NULL) {
+               allElements.push(element);
+               elemMap.put(element, element);
+               return element;
+       } else {
+               delete element;
+               return e;
+       }
 }
 
 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
@@ -144,10 +191,18 @@ Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
        return function;
 }
 
-Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
-       Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
-       allElements.push(element);
-       return element;
+Boolean *CSolver::getBooleanVar(VarType type) {
+       Boolean *boolean = new BooleanVar(type);
+       allBooleans.push(boolean);
+       return boolean;
+}
+
+Boolean *CSolver::getBooleanTrue() {
+       return boolTrue;
+}
+
+Boolean *CSolver::getBooleanFalse() {
+       return boolFalse;
 }
 
 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
@@ -156,48 +211,186 @@ Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint nu
 
 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
        BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
-       allBooleans.push(boolean);
-       return boolean;
+       Boolean *b = boolMap.get(boolean);
+       if (b == NULL) {
+               boolMap.put(boolean, boolean);
+               allBooleans.push(boolean);
+               return boolean;
+       } else {
+               delete boolean;
+               return b;
+       }
+}
+
+bool CSolver::isTrue(Boolean *b) {
+       return b->isTrue();
+}
+
+bool CSolver::isFalse(Boolean *b) {
+       return b->isFalse();
+}
+
+Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean *arg1, Boolean *arg2) {
+       Boolean *array[] = {arg1, arg2};
+       return applyLogicalOperation(op, array, 2);
+}
+
+Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean *arg) {
+       Boolean *array[] = {arg};
+       return applyLogicalOperation(op, array, 1);
+}
+
+static int ptrcompares(const void *p1, const void *p2) {
+       uintptr_t b1 = *(uintptr_t const *) p1;
+  uintptr_t b2 = *(uintptr_t const *) p2;
+       if (b1 < b2)
+               return -1;
+       else if (b1 == b2)
+               return 0;
+       else
+               return 1;
 }
 
 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
+       Boolean *newarray[asize];
+       switch (op) {
+       case SATC_NOT: {
+               if (array[0]->type == LOGICOP && ((BooleanLogic *)array[0])->op == SATC_NOT) {
+                       return ((BooleanLogic *) array[0])->inputs.get(0);
+               } else if (array[0]->type == BOOLCONST) {
+                       return array[0]->isTrue() ? boolFalse : boolTrue;
+               }
+               break;
+       }
+       case SATC_IFF: {
+               for (uint i = 0; i < 2; i++) {
+                       if (array[i]->type == BOOLCONST) {
+                               if (array[i]->isTrue()) {
+                                       return array[1 - i];
+                               } else {
+                                       newarray[0] = array[1 - i];
+                                       return applyLogicalOperation(SATC_NOT, newarray, 1);
+                               }
+                       }
+               }
+               break;
+       }
+       case SATC_OR: {
+               for (uint i =0; i <asize; i++) {
+                       newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
+               }
+               return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
+       }
+       case SATC_AND: {
+               uint newindex = 0;
+               for (uint i = 0; i < asize; i++) {
+                       Boolean *b = array[i];
+                       if (b->type == BOOLCONST) {
+                               if (b->isTrue())
+                                       continue;
+                               else
+                                       return boolFalse;
+                       } else
+                               newarray[newindex++] = b;
+               }
+               if (newindex == 0) {
+                       return boolTrue;
+               } else if (newindex == 1) {
+                       return newarray[0];
+               } else {
+                       qsort(newarray, asize, sizeof(Boolean *), ptrcompares);
+                       array = newarray;
+                       asize = newindex;
+               }
+               break;
+       }
+       case SATC_XOR: {
+               //handle by translation
+               return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
+       }
+       case SATC_IMPLIES: {
+               //handle by translation
+               return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
+       }
+       }
+
+       ASSERT(asize != 0);
        Boolean *boolean = new BooleanLogic(this, op, array, asize);
-       allBooleans.push(boolean);
-       return boolean;
+       Boolean *b = boolMap.get(boolean);
+       if (b == NULL) {
+               boolMap.put(boolean, boolean);
+               allBooleans.push(boolean);
+               return boolean;
+       } else {
+               delete boolean;
+               return b;
+       }
+}
+
+Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
+       Boolean *constraint = new BooleanOrder(order, first, second);
+       allBooleans.push(constraint);
+       return constraint;
 }
 
 void CSolver::addConstraint(Boolean *constraint) {
-       constraints.add(constraint);
+       if (constraint == boolTrue)
+               return;
+       else if (constraint == boolFalse)
+               setUnSAT();
+       else {
+               if (constraint->type == LOGICOP) {
+                       BooleanLogic *b=(BooleanLogic *) constraint;
+                       if (b->op==SATC_AND) {
+                               for(uint i=0;i<b->inputs.getSize();i++) {
+                                       addConstraint(b->inputs.get(i));
+                               }
+                               return;
+                       }
+               }
+
+               constraints.add(constraint);
+       }
 }
 
 Order *CSolver::createOrder(OrderType type, Set *set) {
        Order *order = new Order(type, set);
        allOrders.push(order);
+       activeOrders.add(order);
        return order;
 }
 
-Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
-       Boolean *constraint = new BooleanOrder(order, first, second);
-       allBooleans.push(constraint);
-       return constraint;
-}
+int CSolver::solve() {
+       bool deleteTuner = false;
+       if (tuner == NULL) {
+               tuner = new DefaultTuner();
+               deleteTuner = true;
+       }
 
-int CSolver::startEncoding() {
+       long long startTime = getTimeNano();
        computePolarities(this);
-       orderAnalysis(this);
+
+       DecomposeOrderTransform dot(this);
+       dot.doTransform();
+
+       //This leaks like crazy
+       //      IntegerEncodingTransform iet(this);
+       //iet.doTransform();
+
        naiveEncodingDecision(this);
-       encodeAllSATEncoder(this, satEncoder);
-       int result = solveCNF(satEncoder->cnf);
-       model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
-       for (int i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
-               model_print("%d, ", satEncoder->cnf->solver->solution[i]);
+       satEncoder->encodeAllSATEncoder(this);
+       int result = unsat ? IS_UNSAT : satEncoder->solve();
+       long long finishTime = getTimeNano();
+       elapsedTime = finishTime - startTime;
+       if (deleteTuner) {
+               delete tuner;
+               tuner = NULL;
        }
-       model_print("\n");
        return result;
 }
 
 uint64_t CSolver::getElementValue(Element *element) {
-       switch (GETELEMENTTYPE(element)) {
+       switch (element->type) {
        case ELEMSET:
        case ELEMCONST:
        case ELEMFUNCRETURN:
@@ -209,7 +402,7 @@ uint64_t CSolver::getElementValue(Element *element) {
 }
 
 bool CSolver::getBooleanValue(Boolean *boolean) {
-       switch (GETBOOLEANTYPE(boolean)) {
+       switch (boolean->type) {
        case BOOLEANVAR:
                return getBooleanVariableValueSATTranslator(this, boolean);
        default:
@@ -218,7 +411,17 @@ bool CSolver::getBooleanValue(Boolean *boolean) {
        exit(-1);
 }
 
-HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
-       return getOrderConstraintValueSATTranslator(this, order, first, second);
+bool CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
+       return order->encoding.resolver->resolveOrder(first, second);
 }
 
+long long CSolver::getEncodeTime() { return satEncoder->getEncodeTime(); }
+
+long long CSolver::getSolveTime() { return satEncoder->getSolveTime(); }
+
+void CSolver::autoTune(uint budget) {
+       AutoTuner *autotuner = new AutoTuner(budget);
+       autotuner->addProblem(this);
+       autotuner->tune();
+       delete autotuner;
+}