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