1 #include "encodinggraph.h"
11 #include "elementencoding.h"
13 EncodingGraph::EncodingGraph(CSolver *_solver) :
17 EncodingGraph::~EncodingGraph() {
18 subgraphs.resetAndDelete();
19 encodingMap.resetAndDeleteVals();
20 edgeMap.resetAndDeleteVals();
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();
36 void EncodingGraph::buildGraph() {
37 ElementIterator it(solver);
38 while (it.hasNext()) {
39 Element *e = it.next();
51 bsdqsort(edgeVector.expose(), edgeVector.getSize(), sizeof(EncodingEdge *), sortEncodingEdge);
56 void EncodingGraph::validate() {
57 SetIteratorBooleanEdge* it= solver->getConstraints();
59 BooleanEdge be = it->next();
60 if(be->type == PREDICATEOP){
61 BooleanPredicate *b = (BooleanPredicate *)be.getBoolean();
62 if(b->predicate->type == OPERATORPRED){
63 PredicateOperator* predicate = (PredicateOperator*) b->predicate;
64 if(predicate->getOp() == SATC_EQUALS){
65 ASSERT(b->inputs.getSize() == 2);
66 Element* e1= b->inputs.get(0);
67 Element* e2= b->inputs.get(1);
68 if(e1->type == ELEMCONST || e1->type == ELEMCONST)
70 ElementEncoding *enc1 = e1->getElementEncoding();
71 ElementEncoding *enc2 = e2->getElementEncoding();
72 ASSERT(enc1->getElementEncodingType() != ELEM_UNASSIGNED);
73 ASSERT(enc2->getElementEncodingType() != ELEM_UNASSIGNED);
74 if(enc1->getElementEncodingType() == enc2->getElementEncodingType() && enc1->getElementEncodingType() == BINARYINDEX && b->getFunctionEncoding()->type == CIRCUIT){
75 for(uint i=0; i<enc1->encArraySize; i++){
76 if(enc1->isinUseElement(i)){
77 uint64_t val1 = enc1->encodingArray[i];
78 if(enc2->isinUseElement(i)){
79 ASSERT(val1 == enc2->encodingArray[i]);
81 for(uint j=0; j< enc2->encArraySize; j++){
82 if(enc2->isinUseElement(j)){
83 ASSERT(val1 != enc2->encodingArray[j]);
90 //Now make sure that all the elements in the set are appeared in the encoding array!
91 for(uint k=0; k< b->inputs.getSize(); k++){
92 Element *e = b->inputs.get(k);
93 ElementEncoding *enc = e->getElementEncoding();
94 Set *s = e->getRange();
95 for (uint i = 0; i < s->getSize(); i++) {
96 uint64_t value = s->getElement(i);
98 for(uint j=0; j< enc->encArraySize; j++){
99 if(enc->isinUseElement(j) && enc->encodingArray[j] == value){
115 void EncodingGraph::encode() {
116 SetIteratorEncodingSubGraph *itesg = subgraphs.iterator();
117 DEBUG("#SubGraph = %u", subgraphs.getSize());
118 while (itesg->hasNext()) {
119 EncodingSubGraph *sg = itesg->next();
124 ElementIterator it(solver);
125 while (it.hasNext()) {
126 Element *e = it.next();
129 case ELEMFUNCRETURN: {
130 ElementEncoding *encoding = e->getElementEncoding();
131 if (encoding->getElementEncodingType() == ELEM_UNASSIGNED) {
132 EncodingNode *n = getNode(e);
135 ElementEncodingType encodetype = n->getEncoding();
136 encoding->setElementEncodingType(encodetype);
137 if (encodetype == UNARY || encodetype == ONEHOT) {
138 encoding->encodingArrayInitialization();
139 } else if (encodetype == BINARYINDEX) {
140 EncodingSubGraph *subgraph = graphMap.get(n);
141 DEBUG("graphMap.get(subgraph=%p, n=%p)\n", subgraph, n);
142 if (subgraph == NULL) {
143 encoding->encodingArrayInitialization();
146 uint encodingSize = subgraph->getEncodingMaxVal(n) + 1;
147 uint paddedSize = encoding->getSizeEncodingArray(encodingSize);
148 encoding->allocInUseArrayElement(paddedSize);
149 encoding->allocEncodingArrayElement(paddedSize);
150 Set *s = e->getRange();
151 for (uint i = 0; i < s->getSize(); i++) {
152 uint64_t value = s->getElement(i);
153 uint encodingIndex = subgraph->getEncoding(n, value);
154 encoding->setInUseElement(encodingIndex);
155 ASSERT(encoding->isinUseElement(encodingIndex));
156 encoding->encodingArray[encodingIndex] = value;
169 void EncodingGraph::encodeParent(Element *e) {
170 uint size = e->parents.getSize();
171 for (uint i = 0; i < size; i++) {
172 ASTNode *n = e->parents.get(i);
173 if (n->type == PREDICATEOP) {
174 BooleanPredicate *b = (BooleanPredicate *)n;
175 FunctionEncoding *fenc = b->getFunctionEncoding();
176 if (fenc->getFunctionEncodingType() != FUNC_UNASSIGNED)
178 Predicate *p = b->getPredicate();
179 if (p->type == OPERATORPRED) {
180 PredicateOperator *po = (PredicateOperator *)p;
181 ASSERT(b->inputs.getSize() == 2);
182 EncodingNode *left = createNode(b->inputs.get(0));
183 EncodingNode *right = createNode(b->inputs.get(1));
184 if (left == NULL || right == NULL)
186 EncodingEdge *edge = getEdge(left, right, NULL);
188 EncodingSubGraph *leftGraph = graphMap.get(left);
189 if (leftGraph != NULL && leftGraph == graphMap.get(right)) {
190 fenc->setFunctionEncodingType(CIRCUIT);
198 void EncodingGraph::mergeNodes(EncodingNode *first, EncodingNode *second) {
199 EncodingSubGraph *graph1 = graphMap.get(first);
200 DEBUG("graphMap.get(first=%p, graph1=%p)\n", first, graph1);
201 EncodingSubGraph *graph2 = graphMap.get(second);
202 DEBUG("graphMap.get(second=%p, graph2=%p)\n", second, graph2);
204 first->setEncoding(BINARYINDEX);
206 second->setEncoding(BINARYINDEX);
208 if (graph1 == NULL && graph2 == NULL) {
209 graph1 = new EncodingSubGraph();
210 subgraphs.add(graph1);
211 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
212 graphMap.put(first, graph1);
213 graph1->addNode(first);
215 if (graph1 == NULL && graph2 != NULL) {
218 EncodingNode *tmp = second;
222 if (graph1 != NULL && graph2 != NULL) {
223 if (graph1 == graph2)
226 SetIteratorEncodingNode *nodeit = graph2->nodeIterator();
227 while (nodeit->hasNext()) {
228 EncodingNode *node = nodeit->next();
229 graph1->addNode(node);
230 DEBUG("graphMap.put(node=%p, graph1=%p)\n", node, graph1);
231 graphMap.put(node, graph1);
233 subgraphs.remove(graph2);
235 DEBUG("Deleting graph2 =%p \n", graph2);
238 ASSERT(graph1 != NULL && graph2 == NULL);
239 graph1->addNode(second);
240 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
241 graphMap.put(second, graph1);
245 void EncodingGraph::processElement(Element *e) {
246 uint size = e->parents.getSize();
247 for (uint i = 0; i < size; i++) {
248 ASTNode *n = e->parents.get(i);
251 processPredicate((BooleanPredicate *)n);
254 processFunction((ElementFunction *)n);
262 void EncodingGraph::processFunction(ElementFunction *ef) {
263 Function *f = ef->getFunction();
264 if (f->type == OPERATORFUNC) {
265 FunctionOperator *fo = (FunctionOperator *)f;
266 ASSERT(ef->inputs.getSize() == 2);
267 EncodingNode *left = createNode(ef->inputs.get(0));
268 EncodingNode *right = createNode(ef->inputs.get(1));
269 if (left == NULL && right == NULL)
271 EncodingNode *dst = createNode(ef);
272 EncodingEdge *edge = createEdge(left, right, dst);
277 void EncodingGraph::processPredicate(BooleanPredicate *b) {
278 Predicate *p = b->getPredicate();
279 if (p->type == OPERATORPRED) {
280 PredicateOperator *po = (PredicateOperator *)p;
281 ASSERT(b->inputs.getSize() == 2);
282 EncodingNode *left = createNode(b->inputs.get(0));
283 EncodingNode *right = createNode(b->inputs.get(1));
284 if (left == NULL || right == NULL)
286 EncodingEdge *edge = createEdge(left, right, NULL);
287 CompOp op = po->getOp();
296 edge->numComparisons++;
304 uint convertSize(uint cost) {
305 cost = FUDGEFACTOR * cost;// fudge factor
306 return NEXTPOW2(cost);
309 void EncodingGraph::decideEdges() {
310 uint size = edgeVector.getSize();
311 for (uint i = 0; i < size; i++) {
312 EncodingEdge *ee = edgeVector.get(i);
313 EncodingNode *left = ee->left;
314 EncodingNode *right = ee->right;
316 if (ee->encoding != EDGE_UNASSIGNED ||
317 !left->couldBeBinaryIndex() ||
318 !right->couldBeBinaryIndex())
321 uint64_t eeValue = ee->getValue();
325 EncodingSubGraph *leftGraph = graphMap.get(left);
326 DEBUG("graphMap.get(left=%p, leftgraph=%p)\n", left, leftGraph);
327 EncodingSubGraph *rightGraph = graphMap.get(right);
328 DEBUG("graphMap.get(right=%p, rightgraph=%p)\n", right, rightGraph);
329 if (leftGraph == NULL && rightGraph != NULL) {
330 EncodingNode *tmp = left; left = right; right = tmp;
331 EncodingSubGraph *tmpsg = leftGraph; leftGraph = rightGraph; rightGraph = tmpsg;
334 uint leftSize = 0, rightSize = 0, newSize = 0, max=0;
335 uint64_t totalCost = 0;
337 if (leftGraph == NULL && rightGraph == NULL) {
338 leftSize = convertSize(left->getSize());
339 rightSize = convertSize(right->getSize());
340 newSize = convertSize(left->s->getUnionSize(right->s));
341 newSize = (leftSize > newSize) ? leftSize : newSize;
342 newSize = (rightSize > newSize) ? rightSize : newSize;
343 totalCost = (newSize - leftSize) * left->elements.getSize() +
344 (newSize - rightSize) * right->elements.getSize();
345 max = rightSize > leftSize? rightSize : leftSize;
349 } else if (leftGraph != NULL && rightGraph == NULL) {
350 leftSize = convertSize(leftGraph->encodingSize);
351 rightSize = convertSize(right->getSize());
352 newSize = convertSize(leftGraph->estimateNewSize(right));
353 newSize = (leftSize > newSize) ? leftSize : newSize;
354 newSize = (rightSize > newSize) ? rightSize : newSize;
355 totalCost = (newSize - leftSize) * leftGraph->numElements +
356 (newSize - rightSize) * right->elements.getSize();
357 max = rightSize > leftSize? rightSize : leftSize;
363 leftSize = convertSize(leftGraph->encodingSize);
364 rightSize = convertSize(rightGraph->encodingSize);
365 newSize = convertSize(leftGraph->estimateNewSize(rightGraph));
366 newSize = (leftSize > newSize) ? leftSize : newSize;
367 newSize = (rightSize > newSize) ? rightSize : newSize;
368 totalCost = (newSize - leftSize) * leftGraph->numElements +
369 (newSize - rightSize) * rightGraph->numElements;
370 if(rightSize < 64 && leftSize < 64){
376 mergeNodes(left, right);
381 static TunableDesc EdgeEncodingDesc(EDGE_UNASSIGNED, EDGE_MATCH, EDGE_UNASSIGNED);
383 EncodingEdge *EncodingGraph::getEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
384 EncodingEdge e(left, right, dst);
385 EncodingEdge *result = edgeMap.get(&e);
389 EncodingEdge *EncodingGraph::createEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
390 EncodingEdge e(left, right, dst);
391 EncodingEdge *result = edgeMap.get(&e);
392 if (result == NULL) {
393 result = new EncodingEdge(left, right, dst);
394 VarType v1 = left->getType();
395 VarType v2 = right->getType();
402 if ((left != NULL && left->couldBeBinaryIndex()) &&
403 (right != NULL) && right->couldBeBinaryIndex()) {
404 EdgeEncodingType type = (EdgeEncodingType)solver->getTuner()->getVarTunable(v1, v2, EDGEENCODING, &EdgeEncodingDesc);
405 result->setEncoding(type);
406 if (type == EDGE_MATCH) {
407 mergeNodes(left, right);
410 edgeMap.put(result, result);
411 edgeVector.push(result);
413 left->edges.add(result);
415 right->edges.add(result);
417 dst->edges.add(result);
422 EncodingNode::EncodingNode(Set *_s) :
426 uint EncodingNode::getSize() const {
430 VarType EncodingNode::getType() const {
434 static TunableDesc NodeEncodingDesc(ELEM_UNASSIGNED, BINARYINDEX, ELEM_UNASSIGNED);
436 EncodingNode *EncodingGraph::createNode(Element *e) {
437 if (e->type == ELEMCONST)
439 Set *s = e->getRange();
440 EncodingNode *n = encodingMap.get(s);
442 n = new EncodingNode(s);
443 n->setEncoding((ElementEncodingType)solver->getTuner()->getVarTunable(n->getType(), NODEENCODING, &NodeEncodingDesc));
445 encodingMap.put(s, n);
451 EncodingNode *EncodingGraph::getNode(Element *e) {
452 if (e->type == ELEMCONST)
454 Set *s = e->getRange();
455 EncodingNode *n = encodingMap.get(s);
459 void EncodingNode::addElement(Element *e) {
463 EncodingEdge::EncodingEdge(EncodingNode *_l, EncodingNode *_r) :
467 encoding(EDGE_UNASSIGNED),
474 EncodingEdge::EncodingEdge(EncodingNode *_left, EncodingNode *_right, EncodingNode *_dst) :
478 encoding(EDGE_UNASSIGNED),
485 uint hashEncodingEdge(EncodingEdge *edge) {
486 uintptr_t hash = (((uintptr_t) edge->left) >> 2) ^ (((uintptr_t)edge->right) >> 4) ^ (((uintptr_t)edge->dst) >> 6);
490 bool equalsEncodingEdge(EncodingEdge *e1, EncodingEdge *e2) {
491 return e1->left == e2->left && e1->right == e2->right && e1->dst == e2->dst;
494 uint64_t EncodingEdge::getValue() const {
495 uint lSize = (left != NULL) ? left->getSize() : 1;
496 uint rSize = (right != NULL) ? right->getSize() : 1;
497 uint min = (lSize < rSize) ? lSize : rSize;
498 return numEquals * min + numComparisons * lSize * rSize;