Merging with branch Master
[satune.git] / src / Backend / satorderencoder.cc
1 #include "satencoder.h"
2 #include "structs.h"
3 #include "common.h"
4 #include "order.h"
5 #include "csolver.h"
6 #include "orderpair.h"
7 #include "set.h"
8 #include "tunable.h"
9 #include "orderanalysis.h"
10 #include "ordergraph.h"
11 #include "orderedge.h"
12 #include "element.h"
13 #include "predicate.h"
14 #include "orderelement.h"
15
16 Edge SATEncoder::encodeOrderSATEncoder(BooleanOrder *constraint) {
17         switch ( constraint->order->type) {
18         case SATC_PARTIAL:
19                 return encodePartialOrderSATEncoder(constraint);
20         case SATC_TOTAL:
21                 return encodeTotalOrderSATEncoder(constraint);
22         default:
23                 ASSERT(0);
24         }
25         return E_BOGUS;
26 }
27
28 Edge SATEncoder::inferOrderConstraintFromGraph(Order *order, uint64_t _first, uint64_t _second) {
29         if (order->graph != NULL) {
30                 OrderGraph *graph = order->graph;
31                 OrderNode *first = graph->lookupOrderNodeFromOrderGraph(_first);
32                 OrderNode *second = graph->lookupOrderNodeFromOrderGraph(_second);
33                 if ((first != NULL) && (second != NULL)) {
34                         OrderEdge *edge = graph->lookupOrderEdgeFromOrderGraph(first, second);
35                         if (edge != NULL) {
36                                 if (edge->mustPos)
37                                         return E_True;
38                                 else if (edge->mustNeg)
39                                         return E_False;
40                         }
41                         OrderEdge *invedge = graph->lookupOrderEdgeFromOrderGraph(second, first);
42                         if (invedge != NULL) {
43                                 if (invedge->mustPos)
44                                         return E_False;
45                                 else if (invedge->mustNeg)
46                                         return E_True;
47                         }
48                 }
49         }
50         return E_NULL;
51 }
52
53 Edge SATEncoder::getPairConstraint(Order *order, OrderPair *pair) {
54         Edge gvalue = inferOrderConstraintFromGraph(order, pair->first, pair->second);
55         if (!edgeIsNull(gvalue))
56                 return gvalue;
57
58         HashtableOrderPair *table = order->orderPairTable;
59         bool negate = false;
60         OrderPair flipped;
61         if (pair->first < pair->second) {
62                 negate = true;
63                 flipped.first = pair->second;
64                 flipped.second = pair->first;
65                 pair = &flipped;
66         }
67         Edge constraint;
68         if (!(table->contains(pair))) {
69                 constraint = getNewVarSATEncoder();
70                 OrderPair *paircopy = new OrderPair(pair->first, pair->second, constraint);
71                 table->put(paircopy, paircopy);
72         } else
73                 constraint = table->get(pair)->constraint;
74
75         return negate ? constraintNegate(constraint) : constraint;
76 }
77
78 Edge SATEncoder::encodeTotalOrderSATEncoder(BooleanOrder *boolOrder) {
79         ASSERT(boolOrder->order->type == SATC_TOTAL);
80         if (boolOrder->order->orderPairTable == NULL) {
81                 boolOrder->order->initializeOrderHashtable();
82                 bool doOptOrderStructure = GETVARTUNABLE(solver->getTuner(), boolOrder->order->type, OPTIMIZEORDERSTRUCTURE, &onoff);
83                 if (doOptOrderStructure) {
84                         boolOrder->order->graph = buildMustOrderGraph(boolOrder->order);
85                         reachMustAnalysis(solver, boolOrder->order->graph, true);
86                 }
87                 createAllTotalOrderConstraintsSATEncoder(boolOrder->order);
88         }
89         OrderPair pair(boolOrder->first, boolOrder->second, E_NULL);
90         Edge constraint = getPairConstraint(boolOrder->order, &pair);
91         return constraint;
92 }
93
94
95 void SATEncoder::createAllTotalOrderConstraintsSATEncoder(Order *order) {
96 #ifdef CONFIG_DEBUG
97         model_print("in total order ...\n");
98 #endif
99         ASSERT(order->type == SATC_TOTAL);
100         Set *set = order->set;
101         uint size = order->set->getSize();
102         for (uint i = 0; i < size; i++) {
103                 uint64_t valueI = set->getMemberAt(i);
104                 for (uint j = i + 1; j < size; j++) {
105                         uint64_t valueJ = set->getMemberAt(j);
106                         OrderPair pairIJ(valueI, valueJ, E_NULL);
107                         Edge constIJ = getPairConstraint(order, &pairIJ);
108                         for (uint k = j + 1; k < size; k++) {
109                                 uint64_t valueK = set->getMemberAt(k);
110                                 OrderPair pairJK(valueJ, valueK, E_NULL);
111                                 OrderPair pairIK(valueI, valueK, E_NULL);
112                                 Edge constIK = getPairConstraint(order, &pairIK);
113                                 Edge constJK = getPairConstraint(order, &pairJK);
114                                 addConstraintCNF(cnf, generateTransOrderConstraintSATEncoder(constIJ, constJK, constIK));
115                         }
116                 }
117         }
118 }
119
120 Edge getOrderConstraint(HashtableOrderPair *table, OrderPair *pair) {
121         ASSERT(pair->first != pair->second);
122         bool negate = false;
123         OrderPair flipped;
124         if (pair->first < pair->second) {
125                 negate = true;
126                 flipped.first = pair->second;
127                 flipped.second = pair->first;
128                 pair = &flipped;
129         }
130         if (!table->contains(pair)) {
131                 return E_NULL;
132         }
133         Edge constraint = table->get(pair)->constraint;
134         ASSERT(!edgeIsNull(constraint));
135         return negate ? constraintNegate(constraint) : constraint;
136 }
137
138 Edge SATEncoder::generateTransOrderConstraintSATEncoder(Edge constIJ,Edge constJK,Edge constIK) {
139         Edge carray[] = {constIJ, constJK, constraintNegate(constIK)};
140         Edge loop1 = constraintOR(cnf, 3, carray);
141         Edge carray2[] = {constraintNegate(constIJ), constraintNegate(constJK), constIK};
142         Edge loop2 = constraintOR(cnf, 3, carray2 );
143         return constraintAND2(cnf, loop1, loop2);
144 }
145
146 Edge SATEncoder::encodePartialOrderSATEncoder(BooleanOrder *constraint) {
147         ASSERT(constraint->order->type == SATC_PARTIAL);
148         return E_BOGUS;
149 }