edit
[satune.git] / src / csolver.h
1 #ifndef CSOLVER_H
2 #define CSOLVER_H
3 #include "classes.h"
4 #include "ops.h"
5 #include "corestructs.h"
6 #include "asthash.h"
7 #include "solver_interface.h"
8 #include "common.h"
9
10 class CSolver {
11 public:
12         CSolver();
13         ~CSolver();
14         void resetSolver();
15         /** This function creates a set containing the elements passed in the array. */
16         Set *createSet(VarType type, uint64_t *elements, uint num);
17
18         /** This function creates a set from lowrange to highrange (inclusive). */
19
20         Set *createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange);
21         
22         bool itemExistInSet(Set *set, uint64_t item);
23
24         VarType getSetVarType(Set *set);
25
26         Element *createRangeVar(VarType type, uint64_t lowrange, uint64_t highrange);
27
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!
31          */
32
33         MutableSet *createMutableSet(VarType type);
34
35         /** This function adds a new item to a set. */
36
37         //Deprecating this unless we need it...
38         void addItem(MutableSet *set, uint64_t element);
39
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
42             items to the set. */
43
44         uint64_t createUniqueItem(MutableSet *set);
45
46         /**
47          * Freeze and finalize the mutableSet ...
48          */
49         void finalizeMutableSet(MutableSet *set);
50
51         /** This function creates an element variable over a set. */
52
53         Element *getElementVar(Set *set);
54
55         /** This function creates an element constrant. */
56         Element *getElementConst(VarType type, uint64_t value);
57
58         Set *getElementRange (Element *element);
59
60         BooleanEdge getBooleanTrue();
61
62         BooleanEdge getBooleanFalse();
63
64         /** This function creates a boolean variable. */
65
66         BooleanEdge getBooleanVar(VarType type);
67
68         /** This function creates a function operator. */
69
70         Function *createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,
71                                                                                                                                          OverFlowBehavior overflowbehavior);
72
73         /** This function creates a predicate operator. */
74
75         Predicate *createPredicateOperator(CompOp op, Set **domain, uint numDomain);
76
77         Predicate *createPredicateTable(Table *table, UndefinedBehavior behavior);
78
79         /** This function creates an empty instance table.*/
80
81         Table *createTable(Set **domains, uint numDomain, Set *range);
82
83         Table *createTableForPredicate(Set **domains, uint numDomain);
84         /** This function adds an input output relation to a table. */
85
86         void addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result);
87
88         /** This function converts a completed table into a function. */
89
90         Function *completeTable(Table *, UndefinedBehavior behavior);
91
92         /** This function applies a function to the Elements in its input. */
93
94         Element *applyFunction(Function *function, Element **array, uint numArrays, BooleanEdge overflowstatus);
95
96         /** This function applies a predicate to the Elements in its input. */
97
98         BooleanEdge applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, BooleanEdge undefinedStatus);
99
100         BooleanEdge applyPredicate(Predicate *predicate, Element **inputs, uint numInputs);
101
102         /** This function applies a logical operation to the Booleans in its input. */
103
104         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
105
106         /** This function applies a logical operation to the Booleans in its input. */
107
108         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2);
109
110         /** This function applies a logical operation to the Booleans in its input. */
111
112         BooleanEdge applyLogicalOperation(LogicOp op, BooleanEdge arg);
113
114         /** This function adds a boolean constraint to the set of constraints
115             to be satisfied */
116
117         void addConstraint(BooleanEdge constraint);
118
119         /** This function instantiates an order of type type over the set set. */
120         Order *createOrder(OrderType type, Set *set);
121
122         /** This function instantiates a boolean on two items in an order. */
123         BooleanEdge orderConstraint(Order *order, uint64_t first, uint64_t second);
124
125         /** When everything is done, the client calls this function and then csolver starts to encode*/
126         int solve();
127
128         /** After getting the solution from the SAT solver, client can get the value of an element via this function*/
129         uint64_t getElementValue(Element *element);
130
131         /** After getting the solution from the SAT solver, client can get the value of a boolean via this function*/
132         bool getBooleanValue(BooleanEdge boolean);
133
134         bool getOrderConstraintValue(Order *order, uint64_t first, uint64_t second);
135
136         bool isTrue(BooleanEdge b);
137         bool isFalse(BooleanEdge b);
138
139         void setUnSAT() { model_print("Setting UNSAT %%%%%%\n"); unsat = true; }
140         bool isUnSAT() { return unsat; }
141
142         void printConstraint(BooleanEdge boolean);
143         void printConstraints();
144
145         Vector<Order *> *getOrders() { return &allOrders;}
146         HashsetOrder *getActiveOrders() { return &activeOrders;}
147
148         Tuner *getTuner() { return tuner; }
149
150         SetIteratorBooleanEdge *getConstraints() { return constraints.iterator(); }
151
152         SATEncoder *getSATEncoder() {return satEncoder;}
153
154         void replaceBooleanWithTrue(BooleanEdge bexpr);
155         void replaceBooleanWithTrueNoRemove(BooleanEdge bexpr);
156         void replaceBooleanWithFalseNoRemove(BooleanEdge bexpr);
157         void replaceBooleanWithFalse(BooleanEdge bexpr);
158         void replaceBooleanWithBoolean(BooleanEdge oldb, BooleanEdge newb);
159         CSolver *clone();
160 //        Set* addItemsToRange(Element* element, uint num, ...);
161         void serialize();
162         static CSolver *deserialize(const char *file);
163         void autoTune(uint budget);
164         void inferFixedOrders();
165         void inferFixedOrder(Order *order);
166
167
168         void setTuner(Tuner *_tuner) { tuner = _tuner; }
169         long long getElapsedTime() { return elapsedTime; }
170         long long getEncodeTime();
171         long long getSolveTime();
172
173         CMEMALLOC;
174
175 private:
176         void handleIFFTrue(BooleanLogic *bexpr, BooleanEdge child);
177         void handleANDTrue(BooleanLogic *bexpr, BooleanEdge child);
178         void handleFunction(ElementFunction * ef, BooleanEdge child);
179         
180         //These two functions are helpers if the client has a pointer to a
181         //Boolean object that we have since replaced
182         BooleanEdge rewriteLogicalOperation(LogicOp op, BooleanEdge *array, uint asize);
183         BooleanEdge doRewrite(BooleanEdge b);
184         /** This is a vector of constraints that must be satisfied. */
185         HashsetBooleanEdge constraints;
186
187         /** This is a vector of all boolean structs that we have allocated. */
188         Vector<Boolean *> allBooleans;
189
190         /** This is a vector of all set structs that we have allocated. */
191         Vector<Set *> allSets;
192
193         /** This is a vector of all element structs that we have allocated. */
194         Vector<Element *> allElements;
195
196         /** This is a vector of all predicate structs that we have allocated. */
197         Vector<Predicate *> allPredicates;
198
199         /** This is a vector of all table structs that we have allocated. */
200         Vector<Table *> allTables;
201
202         /** This is a vector of all order structs that we have allocated. */
203         Vector<Order *> allOrders;
204
205         HashsetOrder activeOrders;
206
207         /** This is a vector of all function structs that we have allocated. */
208         Vector<Function *> allFunctions;
209
210         BooleanEdge boolTrue;
211         BooleanEdge boolFalse;
212
213         /** These two tables are used for deduplicating entries. */
214         BooleanMatchMap boolMap;
215         ElementMatchMap elemMap;
216
217         SATEncoder *satEncoder;
218         bool unsat;
219         Tuner *tuner;
220         long long elapsedTime;
221 };
222 #endif