Improve libc support
[satune.git] / src / ASTAnalyses / Encoding / subgraph.cc
index afddea10ebe0d9dda337f89188a045e48149d302..86937549fe578070a78914c0a27f9e9307e08b16 100644 (file)
@@ -9,6 +9,11 @@ EncodingSubGraph::EncodingSubGraph() :
        maxEncodingVal(0) {
 }
 
+EncodingSubGraph::~EncodingSubGraph() {
+       map.resetAndDeleteKeys();
+       values.resetAndDelete();
+}
+
 uint hashNodeValuePair(NodeValuePair *nvp) {
        return (uint) (nvp->value ^ ((uintptr_t)nvp->node));
 }
@@ -18,10 +23,10 @@ bool equalsNodeValuePair(NodeValuePair *nvp1, NodeValuePair *nvp2) {
 }
 
 int sortEncodingValue(const void *p1, const void *p2) {
-       const EncodingValue * e1 = * (const EncodingValue **) p1;
-       const EncodingValue * e2 = * (const EncodingValue **) p2;
-       uint se1=e1->notequals.getSize();
-       uint se2=e2->notequals.getSize();
+       const EncodingValue *e1 = *(const EncodingValue **) p1;
+       const EncodingValue *e2 = *(const EncodingValue **) p2;
+       uint se1 = e1->notequals.getSize();
+       uint se2 = e2->notequals.getSize();
        if (se1 > se2)
                return -1;
        else if (se2 == se1)
@@ -39,9 +44,9 @@ uint EncodingSubGraph::getEncoding(EncodingNode *n, uint64_t val) {
 void EncodingSubGraph::solveEquals() {
        Vector<EncodingValue *> toEncode;
        Vector<bool> encodingArray;
-       SetIteratorEncodingValue *valIt=values.iterator();
-       while(valIt->hasNext()) {
-               EncodingValue *ev=valIt->next();
+       SetIteratorEncodingValue *valIt = values.iterator();
+       while (valIt->hasNext()) {
+               EncodingValue *ev = valIt->next();
                if (!ev->inComparison)
                        toEncode.push(ev);
                else
@@ -49,20 +54,20 @@ void EncodingSubGraph::solveEquals() {
        }
        delete valIt;
        bsdqsort(toEncode.expose(), toEncode.getSize(), sizeof(EncodingValue *), sortEncodingValue);
-       uint toEncodeSize=toEncode.getSize();
-       for(uint i=0; i<toEncodeSize; i++) {
-               EncodingValue * ev=toEncode.get(i);
+       uint toEncodeSize = toEncode.getSize();
+       for (uint i = 0; i < toEncodeSize; i++) {
+               EncodingValue *ev = toEncode.get(i);
                encodingArray.clear();
-               SetIteratorEncodingValue *conflictIt=ev->notequals.iterator();
-               while(conflictIt->hasNext()) {
-                       EncodingValue * conflict=conflictIt->next();
+               SetIteratorEncodingValue *conflictIt = ev->notequals.iterator();
+               while (conflictIt->hasNext()) {
+                       EncodingValue *conflict = conflictIt->next();
                        if (conflict->assigned) {
                                encodingArray.setExpand(conflict->encoding, true);
                        }
                }
                delete conflictIt;
-               uint encoding=0;
-               for(;encoding<encodingArray.getSize();encoding++) {
+               uint encoding = 0;
+               for (; encoding < encodingArray.getSize(); encoding++) {
                        //See if this is unassigned
                        if (!encodingArray.get(encoding))
                                break;
@@ -77,17 +82,17 @@ void EncodingSubGraph::solveEquals() {
 void EncodingSubGraph::solveComparisons() {
        HashsetEncodingValue discovered;
        Vector<EncodingValue *> tovisit;
-       SetIteratorEncodingValue *valIt=values.iterator();
-       while(valIt->hasNext()) {
-               EncodingValue *ev=valIt->next();
+       SetIteratorEncodingValue *valIt = values.iterator();
+       while (valIt->hasNext()) {
+               EncodingValue *ev = valIt->next();
                if (discovered.add(ev)) {
                        tovisit.push(ev);
-                       while(tovisit.getSize()!=0) {
-                               EncodingValue * val=tovisit.last(); tovisit.pop();
-                               SetIteratorEncodingValue *nextIt=val->larger.iterator();
+                       while (tovisit.getSize() != 0) {
+                               EncodingValue *val = tovisit.last(); tovisit.pop();
+                               SetIteratorEncodingValue *nextIt = val->larger.iterator();
                                uint minVal = val->encoding + 1;
-                               while(nextIt->hasNext()) {
-                                       EncodingValue *nextVal=nextIt->next();
+                               while (nextIt->hasNext()) {
+                                       EncodingValue *nextVal = nextIt->next();
                                        if (nextVal->encoding < minVal) {
                                                if (minVal > maxEncodingVal)
                                                        maxEncodingVal = minVal;
@@ -104,11 +109,11 @@ void EncodingSubGraph::solveComparisons() {
 }
 
 uint EncodingSubGraph::estimateNewSize(EncodingSubGraph *sg) {
-       uint newSize=0;
-       SetIteratorEncodingNode * nit = sg->nodes.iterator();
-       while(nit->hasNext()) {
+       uint newSize = 0;
+       SetIteratorEncodingNode *nit = sg->nodes.iterator();
+       while (nit->hasNext()) {
                EncodingNode *en = nit->next();
-               uint size=estimateNewSize(en);
+               uint size = estimateNewSize(en);
                if (size > newSize)
                        newSize = size;
        }
@@ -117,10 +122,10 @@ uint EncodingSubGraph::estimateNewSize(EncodingSubGraph *sg) {
 }
 
 uint EncodingSubGraph::estimateNewSize(EncodingNode *n) {
-       SetIteratorEncodingEdge * eeit = n->edges.iterator();
-       uint newsize=n->getSize();
-       while(eeit->hasNext()) {
-               EncodingEdge * ee = eeit->next();
+       SetIteratorEncodingEdge *eeit = n->edges.iterator();
+       uint newsize = n->getSize();
+       while (eeit->hasNext()) {
+               EncodingEdge *ee = eeit->next();
                if (ee->left != NULL && ee->left != n && nodes.contains(ee->left)) {
                        uint intersectSize = n->s->getUnionSize(ee->left->s);
                        if (intersectSize > newsize)
@@ -143,13 +148,13 @@ uint EncodingSubGraph::estimateNewSize(EncodingNode *n) {
 
 void EncodingSubGraph::addNode(EncodingNode *n) {
        nodes.add(n);
-       uint newSize=estimateNewSize(n);
+       uint newSize = estimateNewSize(n);
        numElements += n->elements.getSize();
        if (newSize > encodingSize)
-               encodingSize=newSize;
+               encodingSize = newSize;
 }
 
-SetIteratorEncodingNode * EncodingSubGraph::nodeIterator() {
+SetIteratorEncodingNode *EncodingSubGraph::nodeIterator() {
        return nodes.iterator();
 }
 
@@ -162,14 +167,14 @@ void EncodingSubGraph::encode() {
 }
 
 void EncodingSubGraph::computeEqualities() {
-       SetIteratorEncodingNode *nodeit=nodes.iterator();
-       while(nodeit->hasNext()) {
-               EncodingNode *node=nodeit->next();
+       SetIteratorEncodingNode *nodeit = nodes.iterator();
+       while (nodeit->hasNext()) {
+               EncodingNode *node = nodeit->next();
                generateEquals(node, node);
-               
-               SetIteratorEncodingEdge *edgeit=node->edges.iterator();
-               while(edgeit->hasNext()) {
-                       EncodingEdge *edge=edgeit->next();
+
+               SetIteratorEncodingEdge *edgeit = node->edges.iterator();
+               while (edgeit->hasNext()) {
+                       EncodingEdge *edge = edgeit->next();
                        //skip over comparisons as we have already handled them
                        if (edge->numComparisons != 0)
                                continue;
@@ -194,12 +199,12 @@ void EncodingSubGraph::computeEqualities() {
 }
 
 void EncodingSubGraph::computeComparisons() {
-       SetIteratorEncodingNode *nodeit=nodes.iterator();
-       while(nodeit->hasNext()) {
-               EncodingNode *node=nodeit->next();
-               SetIteratorEncodingEdge *edgeit=node->edges.iterator();
-               while(edgeit->hasNext()) {
-                       EncodingEdge *edge=edgeit->next();
+       SetIteratorEncodingNode *nodeit = nodes.iterator();
+       while (nodeit->hasNext()) {
+               EncodingNode *node = nodeit->next();
+               SetIteratorEncodingEdge *edgeit = node->edges.iterator();
+               while (edgeit->hasNext()) {
+                       EncodingEdge *edge = edgeit->next();
                        if (edge->numComparisons == 0)
                                continue;
                        if (edge->left == NULL || !nodes.contains(edge->left))
@@ -222,15 +227,15 @@ void EncodingSubGraph::orderEV(EncodingValue *earlier, EncodingValue *later) {
 }
 
 void EncodingSubGraph::generateEquals(EncodingNode *left, EncodingNode *right) {
-       Set *lset=left->s;
-       Set *rset=right->s;
-       uint lSize=lset->getSize(), rSize=rset->getSize();
-       for(uint lindex=0; lindex < lSize; lindex++) {
-               for(uint rindex=0; rindex < rSize; rindex++) {
-                       uint64_t lVal=lset->getElement(lindex);
+       Set *lset = left->s;
+       Set *rset = right->s;
+       uint lSize = lset->getSize(), rSize = rset->getSize();
+       for (uint lindex = 0; lindex < lSize; lindex++) {
+               for (uint rindex = 0; rindex < rSize; rindex++) {
+                       uint64_t lVal = lset->getElement(lindex);
                        NodeValuePair nvp1(left, lVal);
                        EncodingValue *lev = map.get(&nvp1);
-                       uint64_t rVal=rset->getElement(rindex);
+                       uint64_t rVal = rset->getElement(rindex);
                        NodeValuePair nvp2(right, rVal);
                        EncodingValue *rev = map.get(&nvp2);
                        if (lev != rev) {
@@ -252,21 +257,21 @@ void EncodingSubGraph::generateEquals(EncodingNode *left, EncodingNode *right) {
 }
 
 void EncodingSubGraph::generateComparison(EncodingNode *left, EncodingNode *right) {
-       Set *lset=left->s;
-       Set *rset=right->s;
-       uint lindex=0, rindex=0;
-       uint lSize=lset->getSize(), rSize=rset->getSize();
-       uint64_t lVal=lset->getElement(lindex);
+       Set *lset = left->s;
+       Set *rset = right->s;
+       uint lindex = 0, rindex = 0;
+       uint lSize = lset->getSize(), rSize = rset->getSize();
+       uint64_t lVal = lset->getElement(lindex);
        NodeValuePair nvp1(left, lVal);
        EncodingValue *lev = map.get(&nvp1);
        lev->inComparison = true;
-       uint64_t rVal=rset->getElement(rindex);
+       uint64_t rVal = rset->getElement(rindex);
        NodeValuePair nvp2(right, rVal);
        EncodingValue *rev = map.get(&nvp2);
        rev->inComparison = true;
        EncodingValue *last = NULL;
 
-       while(lindex < lSize || rindex < rSize) {
+       while (lindex < lSize || rindex < rSize) {
                if (last != NULL) {
                        if (lev != NULL)
                                orderEV(last, lev);
@@ -280,7 +285,7 @@ void EncodingSubGraph::generateComparison(EncodingNode *left, EncodingNode *righ
                                        orderEV(lev, rev);
                                last = lev;
                                if (++lindex < lSize) {
-                                       lVal=lset->getElement(lindex);
+                                       lVal = lset->getElement(lindex);
                                        NodeValuePair nvpl(left, lVal);
                                        lev = map.get(&nvpl);
                                        lev->inComparison = true;
@@ -291,7 +296,7 @@ void EncodingSubGraph::generateComparison(EncodingNode *left, EncodingNode *righ
                                        orderEV(rev, lev);
                                last = rev;
                                if (++rindex < rSize) {
-                                       rVal=rset->getElement(rindex);
+                                       rVal = rset->getElement(rindex);
                                        NodeValuePair nvpr(right, rVal);
                                        rev = map.get(&nvpr);
                                        rev->inComparison = true;
@@ -301,7 +306,7 @@ void EncodingSubGraph::generateComparison(EncodingNode *left, EncodingNode *righ
                } else {
                        last = lev;
                        if (++lindex < lSize) {
-                               lVal=lset->getElement(lindex);
+                               lVal = lset->getElement(lindex);
                                NodeValuePair nvpl(left, lVal);
                                lev = map.get(&nvpl);
                                lev->inComparison = true;
@@ -309,7 +314,7 @@ void EncodingSubGraph::generateComparison(EncodingNode *left, EncodingNode *righ
                                lev = NULL;
 
                        if (++rindex < rSize) {
-                               rVal=rset->getElement(rindex);
+                               rVal = rset->getElement(rindex);
                                NodeValuePair nvpr(right, rVal);
                                rev = map.get(&nvpr);
                                rev->inComparison = true;
@@ -320,12 +325,12 @@ void EncodingSubGraph::generateComparison(EncodingNode *left, EncodingNode *righ
 }
 
 void EncodingSubGraph::computeEncodingValue() {
-       SetIteratorEncodingNode *nodeit=nodes.iterator();
-       while(nodeit->hasNext()) {
-               EncodingNode *node=nodeit->next();
-               Set * set = node->s;
+       SetIteratorEncodingNode *nodeit = nodes.iterator();
+       while (nodeit->hasNext()) {
+               EncodingNode *node = nodeit->next();
+               Set *set = node->s;
                uint setSize = set->getSize();
-               for(uint i=0; i<setSize; i++) {
+               for (uint i = 0; i < setSize; i++) {
                        uint64_t val = set->getElement(i);
                        NodeValuePair nvp(node, val);
                        if (!map.contains(&nvp)) {
@@ -337,21 +342,21 @@ void EncodingSubGraph::computeEncodingValue() {
 }
 
 void EncodingSubGraph::traverseValue(EncodingNode *node, uint64_t value) {
-       EncodingValue *ecv=new EncodingValue(value);
+       EncodingValue *ecv = new EncodingValue(value);
        values.add(ecv);
        HashsetEncodingNode discovered;
        Vector<EncodingNode *> tovisit;
        tovisit.push(node);
        discovered.add(node);
-       while(tovisit.getSize()!=0) {
-               EncodingNode *n=tovisit.last();tovisit.pop();
+       while (tovisit.getSize() != 0) {
+               EncodingNode *n = tovisit.last();tovisit.pop();
                //Add encoding node to structures
                ecv->nodes.add(n);
-               NodeValuePair *nvp=new NodeValuePair(n, value);
+               NodeValuePair *nvp = new NodeValuePair(n, value);
                map.put(nvp, ecv);
-               SetIteratorEncodingEdge *edgeit=node->edges.iterator();
-               while(edgeit->hasNext()) {
-                       EncodingEdge *ee=edgeit->next();
+               SetIteratorEncodingEdge *edgeit = node->edges.iterator();
+               while (edgeit->hasNext()) {
+                       EncodingEdge *ee = edgeit->next();
                        if (!discovered.contains(ee->left) && nodes.contains(ee->left) && ee->left->s->exists(value)) {
                                tovisit.push(ee->left);
                                discovered.add(ee->left);