5 #include "corestructs.h"
7 #include "solver_interface.h"
15 /** This function creates a set containing the elements passed in the array. */
16 Set *createSet(VarType type, uint64_t *elements, uint num);
18 /** This function creates a set from lowrange to highrange (inclusive). */
20 Set *createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange);
22 bool itemExistInSet(Set *set, uint64_t item);
24 VarType getSetVarType(Set *set);
26 Element *createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange);
28 /** This function creates a mutable set.
29 * Note: You should use addItem for adding new item to Mutable sets, and
30 * at the end, you should call finalizeMutableSet!
33 MutableSet *createMutableSet(VarType type);
35 /** This function adds a new item to a set. */
37 //Deprecating this unless we need it...
38 void addItem(MutableSet *set, uint64_t element);
40 /** This function adds a new unique item to the set and returns it.
41 This function cannot be used in conjunction with manually adding
44 uint64_t createUniqueItem(MutableSet *set);
47 * Freeze and finalize the mutableSet ...
49 void finalizeMutableSet(MutableSet *set);
51 /** This function creates an element variable over a set. */
53 Element *getElementVar(Set *set);
55 /** This function creates an element constrant. */
56 Element *getElementConst(VarType type, uint64_t value);
58 Set *getElementRange (Element *element);
60 void mustHaveValue(Element *element);
62 BooleanEdge getBooleanTrue();
64 BooleanEdge getBooleanFalse();
66 /** This function creates a boolean variable. */
68 BooleanEdge getBooleanVar(VarType type);
70 /** This function creates a function operator. */
72 Function *createFunctionOperator(ArithOp op, Set *range,
73 OverFlowBehavior overflowbehavior);
75 /** This function creates a predicate operator. */
77 Predicate *createPredicateOperator(CompOp op);
79 Predicate *createPredicateTable(Table *table, UndefinedBehavior behavior);
81 /** This function creates an empty instance table.*/
83 Table *createTable(Set *range);
85 Table *createTableForPredicate();
86 /** This function adds an input output relation to a table. */
88 void addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
90 /** This function converts a completed table into a function. */
92 Function *completeTable(Table *, UndefinedBehavior behavior);
94 /** This function applies a function to the Elements in its input. */
96 Element *applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus);
98 /** This function applies a predicate to the Elements in its input. */
100 BooleanEdge applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus);
102 BooleanEdge applyPredicate(Predicate *predicate, Element **inputs, uint numInputs);
104 /** This exactly one element of array can be true! (i.e. a1 + a2 + a3 + ... + an = 1)*/
105 BooleanEdge applyExactlyOneConstraint (BooleanEdge *array, uint asize);
107 /** This function applies a logical operation to the Booleans in its input. */
109 BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
111 /** This function applies a logical operation to the Booleans in its input. */
113 BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2);
115 /** This function applies a logical operation to the Booleans in its input. */
117 BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg);
119 /** This function adds a boolean constraint to the set of constraints
122 void addConstraint(BooleanEdge constraint);
124 /** This function instantiates an order of type type over the set set. */
125 Order *createOrder(OrderType type, Set *set);
127 /** This function instantiates a boolean on two items in an order. */
128 BooleanEdge orderConstraint(Order *order, uint64_t first, uint64_t second);
130 /** When everything is done, the client calls this function and then csolver starts to encode*/
133 * Incremental Solving for SATUNE.
134 * It only supports incremental solving for elements!
135 * No support for BooleanVar, BooleanOrder or using interpreters
138 int solveIncremental();
140 /** After getting the solution from the SAT solver, client can get the value of an element via this function*/
141 uint64_t getElementValue(Element *element);
143 void freezeElement(Element *e);
145 void turnoffOptimizations(){optimizationsOff = true;}
147 /** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
148 bool getBooleanValue(BooleanEdge boolean);
150 bool getOrderConstraintValue(Order *order, uint64_t first, uint64_t second);
152 bool isTrue(BooleanEdge b);
153 bool isFalse(BooleanEdge b);
155 void setUnSAT() { model_print("Setting UNSAT %%%%%%\n"); unsat = true; }
156 void setSatSolverTimeout(long seconds) { satsolverTimeout = seconds;}
157 bool isUnSAT() { return unsat; }
158 bool isBooleanVarUsed() {return booleanVarUsed;}
159 void printConstraint(BooleanEdge boolean);
160 void printConstraints();
162 Vector<Order *> *getOrders() { return &allOrders;}
163 HashsetOrder *getActiveOrders() { return &activeOrders;}
165 Tuner *getTuner() { return tuner; }
167 SetIteratorBooleanEdge *getConstraints() { return constraints.iterator(); }
168 bool isConstraintEncoded(BooleanEdge be) { return encodedConstraints.contains(be);}
169 void addEncodedConstraint(BooleanEdge be) {encodedConstraints.add(be);}
171 SATEncoder *getSATEncoder() {return satEncoder;}
173 void replaceBooleanWithTrue(BooleanEdge bexpr);
174 void replaceBooleanWithTrueNoRemove(BooleanEdge bexpr);
175 void replaceBooleanWithFalseNoRemove(BooleanEdge bexpr);
176 void replaceBooleanWithFalse(BooleanEdge bexpr);
177 void replaceBooleanWithBoolean(BooleanEdge oldb, BooleanEdge newb);
180 static CSolver *deserialize(const char *file, InterpreterType itype = SATUNE);
181 void autoTune(uint budget);
182 void inferFixedOrders();
183 void inferFixedOrder(Order *order);
184 void setInterpreter(InterpreterType type);
185 bool useInterpreter() {return interpreter != NULL;}
186 void setTuner(Tuner *_tuner) { tuner = _tuner; }
187 long long getElapsedTime() { return elapsedTime; }
188 long long getEncodeTime();
189 long long getSolveTime();
190 long getSatSolverTimeout() { return satsolverTimeout;}
191 bool isIncrementalMode() {return incrementalMode;}
192 void freezeElementsVariables();
196 void handleIFFTrue(BooleanLogic *bexpr, BooleanEdge child);
197 void handleANDTrue(BooleanLogic *bexpr, BooleanEdge child);
198 void handleFunction(ElementFunction *ef, BooleanEdge child);
200 //These two functions are helpers if the client has a pointer to a
201 //Boolean object that we have since replaced
202 BooleanEdge rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
203 BooleanEdge doRewrite(BooleanEdge b);
204 /** This is a vector of constraints that must be satisfied. */
205 HashsetBooleanEdge constraints;
206 HashsetBooleanEdge encodedConstraints;
208 /** This is a vector of all boolean structs that we have allocated. */
209 Vector<Boolean *> allBooleans;
211 /** This is a vector of all set structs that we have allocated. */
212 Vector<Set *> allSets;
214 /** This is a vector of all element structs that we have allocated. */
215 Vector<Element *> allElements;
217 /** This is a vector of all predicate structs that we have allocated. */
218 Vector<Predicate *> allPredicates;
220 /** This is a vector of all table structs that we have allocated. */
221 Vector<Table *> allTables;
223 /** This is a vector of all order structs that we have allocated. */
224 Vector<Order *> allOrders;
226 HashsetOrder activeOrders;
228 /** This is a vector of all function structs that we have allocated. */
229 Vector<Function *> allFunctions;
231 BooleanEdge boolTrue;
232 BooleanEdge boolFalse;
234 /** These two tables are used for deduplicating entries. */
235 BooleanMatchMap boolMap;
236 ElementMatchMap elemMap;
238 SATEncoder *satEncoder;
241 bool incrementalMode;
242 bool optimizationsOff;
244 long long elapsedTime;
245 long satsolverTimeout;
246 Interpreter *interpreter;
248 friend class ElementOpt;
249 friend class VarOrderingOpt;
252 inline CompOp flipOp(CompOp op) {
268 inline CompOp negateOp(CompOp op) {