Bug fix in merge heuristic
[satune.git] / src / ASTAnalyses / Encoding / encodinggraph.cc
index 43043a71b3f00bd73a5f5d8e05368647c85fb2c6..8029049c5a165710238b2083d96b65dd7c699546 100644 (file)
@@ -114,6 +114,7 @@ void EncodingGraph::validate() {
 
 void EncodingGraph::encode() {
        SetIteratorEncodingSubGraph *itesg = subgraphs.iterator();
+       model_print("#SubGraph = %u", subgraphs.getSize());
        while (itesg->hasNext()) {
                EncodingSubGraph *sg = itesg->next();
                sg->encode();
@@ -154,6 +155,9 @@ void EncodingGraph::encode() {
                                                ASSERT(encoding->isinUseElement(encodingIndex));
                                                encoding->encodingArray[encodingIndex] = value;
                                        }
+                               } else{
+                                       model_print("DAMN in encode()\n");
+                                       e->print();
                                }
                        }
                        break;
@@ -183,8 +187,11 @@ void EncodingGraph::encodeParent(Element *e) {
                                if (left == NULL || right == NULL)
                                        return;
                                EncodingEdge *edge = getEdge(left, right, NULL);
-                               if (edge != NULL && edge->getEncoding() == EDGE_MATCH) {
-                                       fenc->setFunctionEncodingType(CIRCUIT);
+                               if (edge != NULL) {
+                                       EncodingSubGraph *leftGraph = graphMap.get(left);
+                                       if (leftGraph != NULL && leftGraph == graphMap.get(right)) {
+                                               fenc->setFunctionEncodingType(CIRCUIT);
+                                       }
                                }
                        }
                }
@@ -216,6 +223,9 @@ void EncodingGraph::mergeNodes(EncodingNode *first, EncodingNode *second) {
                first = tmp;
        }
        if (graph1 != NULL && graph2 != NULL) {
+               if (graph1 == graph2)
+                       return;
+
                SetIteratorEncodingNode *nodeit = graph2->nodeIterator();
                while (nodeit->hasNext()) {
                        EncodingNode *node = nodeit->next();
@@ -323,9 +333,15 @@ void EncodingGraph::decideEdges() {
                        EncodingNode *tmp = left; left = right; right = tmp;
                        EncodingSubGraph *tmpsg = leftGraph; leftGraph = rightGraph; rightGraph = tmpsg;
                }
-
-               uint leftSize = 0, rightSize = 0, newSize = 0;
+               //model_print("Right=%p RGraph=%p\tLeft=%p LGraph=%p\n", right, rightGraph, left, leftGraph);
+               uint leftSize = 0, rightSize = 0, newSize = 0, max=0;
                uint64_t totalCost = 0;
+               bool merge = false;
+//             model_print("**************decideEdge*************\n");
+//             model_print("LeftNode Size = %u\n", left->getSize());
+//             model_print("rightNode Size = %u\n", right->getSize());
+//             model_print("UnionSize = %u\n", left->s->getUnionSize(right->s));
+                       
                if (leftGraph == NULL && rightGraph == NULL) {
                        leftSize = convertSize(left->getSize());
                        rightSize = convertSize(right->getSize());
@@ -334,6 +350,11 @@ void EncodingGraph::decideEdges() {
                        newSize = (rightSize > newSize) ? rightSize : newSize;
                        totalCost = (newSize - leftSize) * left->elements.getSize() +
                                                                        (newSize - rightSize) * right->elements.getSize();
+                       //model_print("leftSize=%u\trighSize=%u\tnewSize=%u\n", leftSize, rightSize, newSize);
+                       max = rightSize > leftSize? rightSize : leftSize;
+                       if(newSize == max){
+                               merge = true;
+                       }
                } else if (leftGraph != NULL && rightGraph == NULL) {
                        leftSize = convertSize(leftGraph->encodingSize);
                        rightSize = convertSize(right->getSize());
@@ -342,13 +363,13 @@ void EncodingGraph::decideEdges() {
                        newSize = (rightSize > newSize) ? rightSize : newSize;
                        totalCost = (newSize - leftSize) * leftGraph->numElements +
                                                                        (newSize - rightSize) * right->elements.getSize();
+                       //model_print("leftSize=%u\trighSize=%u\tnewSize=%u\n", leftSize, rightSize, newSize);
+                       max = rightSize > leftSize? rightSize : leftSize;
+                       if(newSize == max){
+                               merge = true;
+                       }
                } else {
                        //Neither are null
-
-                       //Are we already merged?
-                       if (leftGraph == rightGraph)
-                               continue;
-
                        leftSize = convertSize(leftGraph->encodingSize);
                        rightSize = convertSize(rightGraph->encodingSize);
                        newSize = convertSize(leftGraph->estimateNewSize(rightGraph));
@@ -356,9 +377,15 @@ void EncodingGraph::decideEdges() {
                        newSize = (rightSize > newSize) ? rightSize : newSize;
                        totalCost = (newSize - leftSize) * leftGraph->numElements +
                                                                        (newSize - rightSize) * rightGraph->numElements;
+//                     model_print("LeftGraph size=%u\n", leftGraph->encodingSize);
+//                     model_print("RightGraph size=%u\n", rightGraph->encodingSize);
+//                     model_print("UnionGraph size = %u\n", leftGraph->estimateNewSize(rightGraph));
+                       if(rightSize < 64 && leftSize < 64){
+                               merge = true;
+                       }
                }
-               
-               if ((totalCost * CONVERSIONFACTOR) < eeValue) {
+//             model_print("******************************\n");
+               if (merge) {
                        //add the edge
                        mergeNodes(left, right);
                }