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);
55 void EncodingGraph::encode() {
56 SetIteratorEncodingSubGraph *itesg = subgraphs.iterator();
57 while (itesg->hasNext()) {
58 EncodingSubGraph *sg = itesg->next();
63 ElementIterator it(solver);
64 while (it.hasNext()) {
65 Element *e = it.next();
68 case ELEMFUNCRETURN: {
69 ElementEncoding *encoding = e->getElementEncoding();
70 if (encoding->getElementEncodingType() == ELEM_UNASSIGNED) {
71 EncodingNode *n = getNode(e);
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) {
84 uint encodingSize = subgraph->getEncodingMaxVal(n) + 1;
85 uint paddedSize = encoding->getSizeEncodingArray(encodingSize);
86 encoding->allocInUseArrayElement(paddedSize);
87 encoding->allocEncodingArrayElement(paddedSize);
88 Set *s = e->getRange();
89 for (uint i = 0; i < s->getSize(); i++) {
90 uint64_t value = s->getElement(i);
91 uint encodingIndex = subgraph->getEncoding(n, value);
92 encoding->setInUseElement(encodingIndex);
93 encoding->encodingArray[encodingIndex] = value;
106 void EncodingGraph::encodeParent(Element *e) {
107 uint size = e->parents.getSize();
108 for (uint i = 0; i < size; i++) {
109 ASTNode *n = e->parents.get(i);
110 if (n->type == PREDICATEOP) {
111 BooleanPredicate *b = (BooleanPredicate *)n;
112 FunctionEncoding *fenc = b->getFunctionEncoding();
113 if (fenc->getFunctionEncodingType() != FUNC_UNASSIGNED)
115 Predicate *p = b->getPredicate();
116 if (p->type == OPERATORPRED) {
117 PredicateOperator *po = (PredicateOperator *)p;
118 ASSERT(b->inputs.getSize() == 2);
119 EncodingNode *left = createNode(b->inputs.get(0));
120 EncodingNode *right = createNode(b->inputs.get(1));
121 if (left == NULL || right == NULL)
123 EncodingEdge *edge = getEdge(left, right, NULL);
124 if (edge != NULL && edge->getEncoding() == EDGE_MATCH) {
125 fenc->setFunctionEncodingType(CIRCUIT);
132 void EncodingGraph::mergeNodes(EncodingNode *first, EncodingNode *second) {
133 EncodingSubGraph *graph1 = graphMap.get(first);
134 DEBUG("graphMap.get(first=%p, graph1=%p)\n", first, graph1);
135 EncodingSubGraph *graph2 = graphMap.get(second);
136 DEBUG("graphMap.get(second=%p, graph2=%p)\n", second, graph2);
138 first->setEncoding(BINARYINDEX);
140 second->setEncoding(BINARYINDEX);
142 if (graph1 == NULL && graph2 == NULL) {
143 graph1 = new EncodingSubGraph();
144 subgraphs.add(graph1);
145 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
146 graphMap.put(first, graph1);
147 graph1->addNode(first);
149 if (graph1 == NULL && graph2 != NULL) {
152 EncodingNode *tmp = second;
156 if (graph1 != NULL && graph2 != NULL) {
157 SetIteratorEncodingNode *nodeit = graph2->nodeIterator();
158 while (nodeit->hasNext()) {
159 EncodingNode *node = nodeit->next();
160 graph1->addNode(node);
161 DEBUG("graphMap.put(node=%p, graph1=%p)\n", node, graph1);
162 graphMap.put(node, graph1);
164 subgraphs.remove(graph2);
166 DEBUG("Deleting graph2 =%p \n", graph2);
169 ASSERT(graph1 != NULL && graph2 == NULL);
170 graph1->addNode(second);
171 DEBUG("graphMap.put(first=%p, graph1=%p)\n", first, graph1);
172 graphMap.put(second, graph1);
176 void EncodingGraph::processElement(Element *e) {
177 uint size = e->parents.getSize();
178 for (uint i = 0; i < size; i++) {
179 ASTNode *n = e->parents.get(i);
182 processPredicate((BooleanPredicate *)n);
185 processFunction((ElementFunction *)n);
193 void EncodingGraph::processFunction(ElementFunction *ef) {
194 Function *f = ef->getFunction();
195 if (f->type == OPERATORFUNC) {
196 FunctionOperator *fo = (FunctionOperator *)f;
197 ASSERT(ef->inputs.getSize() == 2);
198 EncodingNode *left = createNode(ef->inputs.get(0));
199 EncodingNode *right = createNode(ef->inputs.get(1));
200 if (left == NULL && right == NULL)
202 EncodingNode *dst = createNode(ef);
203 EncodingEdge *edge = createEdge(left, right, dst);
208 void EncodingGraph::processPredicate(BooleanPredicate *b) {
209 Predicate *p = b->getPredicate();
210 if (p->type == OPERATORPRED) {
211 PredicateOperator *po = (PredicateOperator *)p;
212 ASSERT(b->inputs.getSize() == 2);
213 EncodingNode *left = createNode(b->inputs.get(0));
214 EncodingNode *right = createNode(b->inputs.get(1));
215 if (left == NULL || right == NULL)
217 EncodingEdge *edge = createEdge(left, right, NULL);
218 CompOp op = po->getOp();
227 edge->numComparisons++;
235 uint convertSize(uint cost) {
236 cost = 1.2 * cost;// fudge factor
237 return NEXTPOW2(cost);
240 void EncodingGraph::decideEdges() {
241 uint size = edgeVector.getSize();
242 for (uint i = 0; i < size; i++) {
243 EncodingEdge *ee = edgeVector.get(i);
244 EncodingNode *left = ee->left;
245 EncodingNode *right = ee->right;
247 if (ee->encoding != EDGE_UNASSIGNED ||
248 !left->couldBeBinaryIndex() ||
249 !right->couldBeBinaryIndex())
252 uint64_t eeValue = ee->getValue();
256 EncodingSubGraph *leftGraph = graphMap.get(left);
257 DEBUG("graphMap.get(left=%p, leftgraph=%p)\n", left, leftGraph);
258 EncodingSubGraph *rightGraph = graphMap.get(right);
259 DEBUG("graphMap.get(right=%p, rightgraph=%p)\n", right, rightGraph);
260 if (leftGraph == NULL && rightGraph != NULL) {
261 EncodingNode *tmp = left; left = right; right = tmp;
262 EncodingSubGraph *tmpsg = leftGraph; leftGraph = rightGraph; rightGraph = tmpsg;
265 uint leftSize = 0, rightSize = 0, newSize = 0;
266 uint64_t totalCost = 0;
267 if (leftGraph == NULL && rightGraph == NULL) {
268 leftSize = convertSize(left->getSize());
269 rightSize = convertSize(right->getSize());
270 newSize = convertSize(left->s->getUnionSize(right->s));
271 newSize = (leftSize > newSize) ? leftSize : newSize;
272 newSize = (rightSize > newSize) ? rightSize : newSize;
273 totalCost = (newSize - leftSize) * left->elements.getSize() +
274 (newSize - rightSize) * right->elements.getSize();
275 } else if (leftGraph != NULL && rightGraph == NULL) {
276 leftSize = convertSize(leftGraph->encodingSize);
277 rightSize = convertSize(right->getSize());
278 newSize = convertSize(leftGraph->estimateNewSize(right));
279 newSize = (leftSize > newSize) ? leftSize : newSize;
280 newSize = (rightSize > newSize) ? rightSize : newSize;
281 totalCost = (newSize - leftSize) * leftGraph->numElements +
282 (newSize - rightSize) * right->elements.getSize();
285 leftSize = convertSize(leftGraph->encodingSize);
286 rightSize = convertSize(rightGraph->encodingSize);
287 newSize = convertSize(leftGraph->estimateNewSize(rightGraph));
288 newSize = (leftSize > newSize) ? leftSize : newSize;
289 newSize = (rightSize > newSize) ? rightSize : newSize;
290 totalCost = (newSize - leftSize) * leftGraph->numElements +
291 (newSize - rightSize) * rightGraph->numElements;
293 double conversionfactor = 0.5;
294 if ((totalCost * conversionfactor) < eeValue) {
296 mergeNodes(left, right);
301 static TunableDesc EdgeEncodingDesc(EDGE_UNASSIGNED, EDGE_MATCH, EDGE_UNASSIGNED);
303 EncodingEdge *EncodingGraph::getEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
304 EncodingEdge e(left, right, dst);
305 EncodingEdge *result = edgeMap.get(&e);
309 EncodingEdge *EncodingGraph::createEdge(EncodingNode *left, EncodingNode *right, EncodingNode *dst) {
310 EncodingEdge e(left, right, dst);
311 EncodingEdge *result = edgeMap.get(&e);
312 if (result == NULL) {
313 result = new EncodingEdge(left, right, dst);
314 VarType v1 = left->getType();
315 VarType v2 = right->getType();
322 if ((left != NULL && left->couldBeBinaryIndex()) &&
323 (right != NULL) && right->couldBeBinaryIndex()) {
324 EdgeEncodingType type = (EdgeEncodingType)solver->getTuner()->getVarTunable(v1, v2, EDGEENCODING, &EdgeEncodingDesc);
325 result->setEncoding(type);
326 if (type == EDGE_MATCH) {
327 mergeNodes(left, right);
330 edgeMap.put(result, result);
331 edgeVector.push(result);
333 left->edges.add(result);
335 right->edges.add(result);
337 dst->edges.add(result);
342 EncodingNode::EncodingNode(Set *_s) :
346 uint EncodingNode::getSize() const {
350 VarType EncodingNode::getType() const {
354 static TunableDesc NodeEncodingDesc(ELEM_UNASSIGNED, BINARYINDEX, ELEM_UNASSIGNED);
356 EncodingNode *EncodingGraph::createNode(Element *e) {
357 if (e->type == ELEMCONST)
359 Set *s = e->getRange();
360 EncodingNode *n = encodingMap.get(s);
362 n = new EncodingNode(s);
363 n->setEncoding((ElementEncodingType)solver->getTuner()->getVarTunable(n->getType(), NODEENCODING, &NodeEncodingDesc));
365 encodingMap.put(s, n);
371 EncodingNode *EncodingGraph::getNode(Element *e) {
372 if (e->type == ELEMCONST)
374 Set *s = e->getRange();
375 EncodingNode *n = encodingMap.get(s);
379 void EncodingNode::addElement(Element *e) {
383 EncodingEdge::EncodingEdge(EncodingNode *_l, EncodingNode *_r) :
387 encoding(EDGE_UNASSIGNED),
394 EncodingEdge::EncodingEdge(EncodingNode *_left, EncodingNode *_right, EncodingNode *_dst) :
398 encoding(EDGE_UNASSIGNED),
405 uint hashEncodingEdge(EncodingEdge *edge) {
406 uintptr_t hash = (((uintptr_t) edge->left) >> 2) ^ (((uintptr_t)edge->right) >> 4) ^ (((uintptr_t)edge->dst) >> 6);
410 bool equalsEncodingEdge(EncodingEdge *e1, EncodingEdge *e2) {
411 return e1->left == e2->left && e1->right == e2->right && e1->dst == e2->dst;
414 uint64_t EncodingEdge::getValue() const {
415 uint lSize = (left != NULL) ? left->getSize() : 1;
416 uint rSize = (right != NULL) ? right->getSize() : 1;
417 uint min = (lSize < rSize) ? lSize : rSize;
418 return numEquals * min + numComparisons * lSize * rSize;