Fixing memory buggit status
[satune.git] / src / ASTAnalyses / Encoding / encodinggraph.cc
1 #include "encodinggraph.h"
2 #include "iterator.h"
3 #include "element.h"
4 #include "function.h"
5 #include "predicate.h"
6 #include "set.h"
7 #include "csolver.h"
8 #include "tunable.h"
9 #include "qsort.h"
10 #include "subgraph.h"
11 #include "elementencoding.h"
12
13 EncodingGraph::EncodingGraph(CSolver *_solver) :
14         solver(_solver) {
15 }
16
17 EncodingGraph::~EncodingGraph() {
18         subgraphs.resetAndDelete();
19         encodingMap.resetAndDeleteVals();
20         edgeMap.resetAndDeleteVals();
21 }
22
23 int sortEncodingEdge(const void *p1, const void *p2) {
24         const EncodingEdge *e1 = *(const EncodingEdge **) p1;
25         const EncodingEdge *e2 = *(const EncodingEdge **) p2;
26         uint64_t v1 = e1->getValue();
27         uint64_t v2 = e2->getValue();
28         if (v1 < v2)
29                 return 1;
30         else if (v1 == v2)
31                 return 0;
32         else
33                 return -1;
34 }
35
36 void EncodingGraph::buildGraph() {
37         ElementIterator it(solver);
38         while (it.hasNext()) {
39                 Element *e = it.next();
40                 switch (e->type) {
41                 case ELEMSET:
42                 case ELEMFUNCRETURN:
43                         processElement(e);
44                         break;
45                 case ELEMCONST:
46                         break;
47                 default:
48                         ASSERT(0);
49                 }
50         }
51         bsdqsort(edgeVector.expose(), edgeVector.getSize(), sizeof(EncodingEdge *), sortEncodingEdge);
52         decideEdges();
53 }
54
55 void EncodingGraph::encode() {
56         SetIteratorEncodingSubGraph *itesg = subgraphs.iterator();
57         while (itesg->hasNext()) {
58                 EncodingSubGraph *sg = itesg->next();
59                 sg->encode();
60         }
61         delete itesg;
62
63         ElementIterator it(solver);
64         while (it.hasNext()) {
65                 Element *e = it.next();
66                 switch (e->type) {
67                 case ELEMSET:
68                 case ELEMFUNCRETURN: {
69                         ElementEncoding *encoding = e->getElementEncoding();
70                         if (encoding->getElementEncodingType() == ELEM_UNASSIGNED) {
71                                 EncodingNode *n = getNode(e);
72                                 if (n == NULL)
73                                         continue;
74                                 ElementEncodingType encodetype = n->getEncoding();
75                                 encoding->setElementEncodingType(encodetype);
76                                 if (encodetype == UNARY || encodetype == ONEHOT) {
77                                         encoding->encodingArrayInitialization();
78                                 } else if (encodetype == BINARYINDEX) {
79                                         EncodingSubGraph *subgraph = graphMap.get(n);
80                                         DEBUG("graphMap.get(subgraph=%p, n=%p)\n", subgraph, n);
81                                         if (subgraph == NULL)
82                                                 continue;
83                                         uint encodingSize = subgraph->getEncodingMaxVal(n) + 1;
84                                         uint paddedSize = encoding->getSizeEncodingArray(encodingSize);
85                                         encoding->allocInUseArrayElement(paddedSize);
86                                         encoding->allocEncodingArrayElement(paddedSize);
87                                         Set *s = e->getRange();
88                                         for (uint i = 0; i < s->getSize(); i++) {
89                                                 uint64_t value = s->getElement(i);
90                                                 uint encodingIndex = subgraph->getEncoding(n, value);
91                                                 encoding->setInUseElement(encodingIndex);
92                                                 encoding->encodingArray[encodingIndex] = value;
93                                         }
94                                 }
95                         }
96                         break;
97                 }
98                 default:
99                         break;
100                 }
101                 encodeParent(e);
102         }
103 }
104
105 void EncodingGraph::encodeParent(Element *e) {
106         uint size = e->parents.getSize();
107         for (uint i = 0; i < size; i++) {
108                 ASTNode *n = e->parents.get(i);
109                 if (n->type == PREDICATEOP) {
110                         BooleanPredicate *b = (BooleanPredicate *)n;
111                         FunctionEncoding *fenc = b->getFunctionEncoding();
112                         if (fenc->getFunctionEncodingType() != FUNC_UNASSIGNED)
113                                 continue;
114                         Predicate *p = b->getPredicate();
115                         if (p->type == OPERATORPRED) {
116                                 PredicateOperator *po = (PredicateOperator *)p;
117                                 ASSERT(b->inputs.getSize() == 2);
118                                 EncodingNode *left = createNode(b->inputs.get(0));
119                                 EncodingNode *right = createNode(b->inputs.get(1));
120                                 if (left == NULL || right == NULL)
121                                         return;
122                                 EncodingEdge *edge = getEdge(left, right, NULL);
123                                 if (edge != NULL && edge->getEncoding() == EDGE_MATCH) {
124                                         fenc->setFunctionEncodingType(CIRCUIT);
125                                 }
126                         }
127                 }
128         }
129 }
130
131 void EncodingGraph::mergeNodes(EncodingNode *first, EncodingNode *second) {
132         EncodingSubGraph *graph1 = graphMap.get(first);
133         DEBUG("graphMap.get(first=%p, graph1=%p)\n", first, graph1);
134         EncodingSubGraph *graph2 = graphMap.get(second);
135         DEBUG("graphMap.get(second=%p, graph2=%p)\n", second, graph2);
136         if (graph1 == NULL)
137                 first->setEncoding(BINARYINDEX);
138         if (graph2 == NULL)
139                 second->setEncoding(BINARYINDEX);
140
141         if (graph1 == NULL && graph2 == NULL) {
142                 graph1 = new EncodingSubGraph();
143                 subgraphs.add(graph1);
144                 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
145                 graphMap.put(first, graph1);
146                 graph1->addNode(first);
147         }
148         if (graph1 == NULL && graph2 != NULL) {
149                 graph1 = graph2;
150                 graph2 = NULL;
151                 EncodingNode *tmp = second;
152                 second = first;
153                 first = tmp;
154         }
155         if (graph1 != NULL && graph2 != NULL) {
156                 SetIteratorEncodingNode *nodeit = graph2->nodeIterator();
157                 while (nodeit->hasNext()) {
158                         EncodingNode *node = nodeit->next();
159                         graph1->addNode(node);
160                         DEBUG("graphMap.put(node=%p, graph1=%p)\n", node, graph1);
161                         graphMap.put(node, graph1);
162                 }
163                 subgraphs.remove(graph2);
164                 delete nodeit;
165                 DEBUG("Deleting graph2 =%p \n", graph2);
166                 delete graph2;
167         } else {
168                 ASSERT(graph1 != NULL && graph2 == NULL);
169                 graph1->addNode(first);
170                 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
171                 graphMap.put(first, graph1);
172         }
173 }
174
175 void EncodingGraph::processElement(Element *e) {
176         uint size = e->parents.getSize();
177         for (uint i = 0; i < size; i++) {
178                 ASTNode *n = e->parents.get(i);
179                 switch (n->type) {
180                 case PREDICATEOP:
181                         processPredicate((BooleanPredicate *)n);
182                         break;
183                 case ELEMFUNCRETURN:
184                         processFunction((ElementFunction *)n);
185                         break;
186                 default:
187                         ASSERT(0);
188                 }
189         }
190 }
191
192 void EncodingGraph::processFunction(ElementFunction *ef) {
193         Function *f = ef->getFunction();
194         if (f->type == OPERATORFUNC) {
195                 FunctionOperator *fo = (FunctionOperator *)f;
196                 ASSERT(ef->inputs.getSize() == 2);
197                 EncodingNode *left = createNode(ef->inputs.get(0));
198                 EncodingNode *right = createNode(ef->inputs.get(1));
199                 if (left == NULL && right == NULL)
200                         return;
201                 EncodingNode *dst = createNode(ef);
202                 EncodingEdge *edge = createEdge(left, right, dst);
203                 edge->numArithOps++;
204         }
205 }
206
207 void EncodingGraph::processPredicate(BooleanPredicate *b) {
208         Predicate *p = b->getPredicate();
209         if (p->type == OPERATORPRED) {
210                 PredicateOperator *po = (PredicateOperator *)p;
211                 ASSERT(b->inputs.getSize() == 2);
212                 EncodingNode *left = createNode(b->inputs.get(0));
213                 EncodingNode *right = createNode(b->inputs.get(1));
214                 if (left == NULL || right == NULL)
215                         return;
216                 EncodingEdge *edge = createEdge(left, right, NULL);
217                 CompOp op = po->getOp();
218                 switch (op) {
219                 case SATC_EQUALS:
220                         edge->numEquals++;
221                         break;
222                 case SATC_LT:
223                 case SATC_LTE:
224                 case SATC_GT:
225                 case SATC_GTE:
226                         edge->numComparisons++;
227                         break;
228                 default:
229                         ASSERT(0);
230                 }
231         }
232 }
233
234 uint convertSize(uint cost) {
235         cost = 1.2 * cost;// fudge factor
236         return NEXTPOW2(cost);
237 }
238
239 void EncodingGraph::decideEdges() {
240         uint size = edgeVector.getSize();
241         for (uint i = 0; i < size; i++) {
242                 EncodingEdge *ee = edgeVector.get(i);
243                 EncodingNode *left = ee->left;
244                 EncodingNode *right = ee->right;
245
246                 if (ee->encoding != EDGE_UNASSIGNED ||
247                                 !left->couldBeBinaryIndex() ||
248                                 !right->couldBeBinaryIndex())
249                         continue;
250
251                 uint64_t eeValue = ee->getValue();
252                 if (eeValue == 0)
253                         return;
254
255                 EncodingSubGraph *leftGraph = graphMap.get(left);
256                 DEBUG("graphMap.get(left=%p, leftgraph=%p)\n", left, leftGraph);
257                 EncodingSubGraph *rightGraph = graphMap.get(right);
258                 DEBUG("graphMap.get(right=%p, rightgraph=%p)\n", right, rightGraph);
259                 if (leftGraph == NULL && rightGraph != NULL) {
260                         EncodingNode *tmp = left; left = right; right = tmp;
261                         EncodingSubGraph *tmpsg = leftGraph; leftGraph = rightGraph; rightGraph = tmpsg;
262                 }
263
264                 uint leftSize = 0, rightSize = 0, newSize = 0;
265                 uint64_t totalCost = 0;
266                 if (leftGraph == NULL && rightGraph == NULL) {
267                         leftSize = convertSize(left->getSize());
268                         rightSize = convertSize(right->getSize());
269                         newSize = convertSize(left->s->getUnionSize(right->s));
270                         newSize = (leftSize > newSize) ? leftSize : newSize;
271                         newSize = (rightSize > newSize) ? rightSize : newSize;
272                         totalCost = (newSize - leftSize) * left->elements.getSize() +
273                                                                         (newSize - rightSize) * right->elements.getSize();
274                 } else if (leftGraph != NULL && rightGraph == NULL) {
275                         leftSize = convertSize(leftGraph->encodingSize);
276                         rightSize = convertSize(right->getSize());
277                         newSize = convertSize(leftGraph->estimateNewSize(right));
278                         newSize = (leftSize > newSize) ? leftSize : newSize;
279                         newSize = (rightSize > newSize) ? rightSize : newSize;
280                         totalCost = (newSize - leftSize) * leftGraph->numElements +
281                                                                         (newSize - rightSize) * right->elements.getSize();
282                 } else {
283                         //Neither are null
284                         leftSize = convertSize(leftGraph->encodingSize);
285                         rightSize = convertSize(rightGraph->encodingSize);
286                         newSize = convertSize(leftGraph->estimateNewSize(rightGraph));
287                         newSize = (leftSize > newSize) ? leftSize : newSize;
288                         newSize = (rightSize > newSize) ? rightSize : newSize;
289                         totalCost = (newSize - leftSize) * leftGraph->numElements +
290                                                                         (newSize - rightSize) * rightGraph->numElements;
291                 }
292                 double conversionfactor = 0.5;
293                 if (leftGraph != rightGraph && (totalCost * conversionfactor) < eeValue) {
294                         //add the edge
295                         mergeNodes(left, right);
296                 }
297         }
298 }
299
300 static TunableDesc EdgeEncodingDesc(EDGE_UNASSIGNED, EDGE_MATCH, EDGE_UNASSIGNED);
301
302 EncodingEdge *EncodingGraph::getEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
303         EncodingEdge e(left, right, dst);
304         EncodingEdge *result = edgeMap.get(&e);
305         return result;
306 }
307
308 EncodingEdge *EncodingGraph::createEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
309         EncodingEdge e(left, right, dst);
310         EncodingEdge *result = edgeMap.get(&e);
311         if (result == NULL) {
312                 result = new EncodingEdge(left, right, dst);
313                 VarType v1 = left->getType();
314                 VarType v2 = right->getType();
315                 if (v1 > v2) {
316                         VarType tmp = v2;
317                         v2 = v1;
318                         v1 = tmp;
319                 }
320
321                 if ((left != NULL && left->couldBeBinaryIndex()) &&
322                                 (right != NULL) && right->couldBeBinaryIndex()) {
323                         EdgeEncodingType type = (EdgeEncodingType)solver->getTuner()->getVarTunable(v1, v2, EDGEENCODING, &EdgeEncodingDesc);
324                         result->setEncoding(type);
325                         if (type == EDGE_MATCH) {
326                                 mergeNodes(left, right);
327                         }
328                 }
329                 edgeMap.put(result, result);
330                 edgeVector.push(result);
331                 if (left != NULL)
332                         left->edges.add(result);
333                 if (right != NULL)
334                         right->edges.add(result);
335                 if (dst != NULL)
336                         dst->edges.add(result);
337         }
338         return result;
339 }
340
341 EncodingNode::EncodingNode(Set *_s) :
342         s(_s) {
343 }
344
345 uint EncodingNode::getSize() const {
346         return s->getSize();
347 }
348
349 VarType EncodingNode::getType() const {
350         return s->getType();
351 }
352
353 static TunableDesc NodeEncodingDesc(ELEM_UNASSIGNED, BINARYINDEX, ELEM_UNASSIGNED);
354
355 EncodingNode *EncodingGraph::createNode(Element *e) {
356         if (e->type == ELEMCONST)
357                 return NULL;
358         Set *s = e->getRange();
359         EncodingNode *n = encodingMap.get(s);
360         if (n == NULL) {
361                 n = new EncodingNode(s);
362                 n->setEncoding((ElementEncodingType)solver->getTuner()->getVarTunable(n->getType(), NODEENCODING, &NodeEncodingDesc));
363
364                 encodingMap.put(s, n);
365         }
366         n->addElement(e);
367         return n;
368 }
369
370 EncodingNode *EncodingGraph::getNode(Element *e) {
371         if (e->type == ELEMCONST)
372                 return NULL;
373         Set *s = e->getRange();
374         EncodingNode *n = encodingMap.get(s);
375         return n;
376 }
377
378 void EncodingNode::addElement(Element *e) {
379         elements.add(e);
380 }
381
382 EncodingEdge::EncodingEdge(EncodingNode *_l, EncodingNode *_r) :
383         left(_l),
384         right(_r),
385         dst(NULL),
386         encoding(EDGE_UNASSIGNED),
387         numArithOps(0),
388         numEquals(0),
389         numComparisons(0)
390 {
391 }
392
393 EncodingEdge::EncodingEdge(EncodingNode *_left, EncodingNode *_right, EncodingNode *_dst) :
394         left(_left),
395         right(_right),
396         dst(_dst),
397         encoding(EDGE_UNASSIGNED),
398         numArithOps(0),
399         numEquals(0),
400         numComparisons(0)
401 {
402 }
403
404 uint hashEncodingEdge(EncodingEdge *edge) {
405         uintptr_t hash = (((uintptr_t) edge->left) >> 2) ^ (((uintptr_t)edge->right) >> 4) ^ (((uintptr_t)edge->dst) >> 6);
406         return (uint) hash;
407 }
408
409 bool equalsEncodingEdge(EncodingEdge *e1, EncodingEdge *e2) {
410         return e1->left == e2->left && e1->right == e2->right && e1->dst == e2->dst;
411 }
412
413 uint64_t EncodingEdge::getValue() const {
414         uint lSize = (left != NULL) ? left->getSize() : 1;
415         uint rSize = (right != NULL) ? right->getSize() : 1;
416         uint min = (lSize < rSize) ? lSize : rSize;
417         return numEquals * min + numComparisons * lSize * rSize;
418 }
419
420