Fix some memory leaks
[satune.git] / src / ASTAnalyses / Encoding / subgraph.h
1 #ifndef SUBGRAPH_H
2 #define SUBGRAPH_H
3 #include "classlist.h"
4 #include "structs.h"
5 #include "graphstructs.h"
6
7 class NodeValuePair {
8 public:
9         NodeValuePair(EncodingNode *n, uint64_t val) : node(n), value(val) {}
10         EncodingNode *node;
11         uint64_t value;
12 };
13
14 class EncodingValue;
15
16 typedef Hashset<EncodingValue *, uintptr_t, PTRSHIFT> HashsetEncodingValue;
17 typedef SetIterator<EncodingValue *, uintptr_t, PTRSHIFT> SetIteratorEncodingValue;
18
19 class EncodingValue {
20 public:
21         EncodingValue(uint64_t _val) : value(_val), encoding(0), inComparison(false), assigned(false) {}
22         void merge(EncodingValue *value);
23         uint64_t value;
24         uint encoding;
25         bool inComparison;
26         bool assigned;
27         HashsetEncodingNode nodes;
28         HashsetEncodingValue larger;
29         HashsetEncodingValue notequals;
30 };
31
32 uint hashNodeValuePair(NodeValuePair *nvp);
33 bool equalsNodeValuePair(NodeValuePair *nvp1, NodeValuePair *nvp2);
34
35 typedef Hashtable<NodeValuePair *, EncodingValue *, uintptr_t, 0, hashNodeValuePair, equalsNodeValuePair> NVPMap;
36
37 class EncodingSubGraph {
38 public:
39         EncodingSubGraph();
40         ~EncodingSubGraph();
41         void addNode(EncodingNode *n);
42         SetIteratorEncodingNode *nodeIterator();
43         void encode();
44         uint getEncoding(EncodingNode *n, uint64_t val);
45         uint getEncodingMaxVal(EncodingNode *n) { return maxEncodingVal;}
46
47         CMEMALLOC;
48 private:
49         uint estimateNewSize(EncodingNode *n);
50         uint estimateNewSize(EncodingSubGraph *sg);
51         void traverseValue(EncodingNode *node, uint64_t value);
52         void computeEncodingValue();
53         void computeComparisons();
54         void computeEqualities();
55         void solveComparisons();
56         void solveEquals();
57         void generateComparison(EncodingNode *left, EncodingNode *right);
58         void generateEquals(EncodingNode *left, EncodingNode *right);
59         void orderEV(EncodingValue *smaller, EncodingValue *larger);
60
61         HashsetEncodingValue values;
62         HashsetEncodingNode nodes;
63         NVPMap map;
64         uint encodingSize;
65         uint numElements;
66         uint maxEncodingVal;
67
68         friend class EncodingGraph;
69 };
70
71 #endif