-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize.c
365 lines (357 loc) · 7.12 KB
/
optimize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "hash/hash.h"
int gcd(int a, int b)
{
while (1)
{
if (b == 0) {
return a;
}
a %= b;
if (a == 0) {
return b;
}
b %= a;
}
}
int callAll(Node* n, int(*f)(Node**)) {
int changed = 0;
if (n == NULL) {
return 0;
}
switch (n->type) {
case LOOP:
return f(&n->n[0].n);
break;
case STMTS:
changed |= f(&n->n[0].n);
changed |= f(&n->n[1].n);
return changed;
break;
case SUM:
case SHIFT:
case OUT:
case IN:
case SET:
return 0;
break;
}
return 0;
}
int clipBranch(Node**);
/*
* concatenate multiple adds/subs together
*/
int join(Node** np) {
Node *n = *np;
if (n == NULL) {
return 0;
}
int changed = 0;
if (n->type == STMTS) {
Node* lchild = n->n[0].n;
Node* rtmp = n;
while (1) {
//get next STMTS
rtmp = n->n[1].n;
if (rtmp == NULL) {
break;
}
//get next STMT from code
Node* rlchild = rtmp->n[0].n;
//concatenate if same type of SUMs and SHIFTs
if (lchild->type == rlchild->type && ((lchild->type == SUM && lchild->n[1].i == rlchild->n[1].i) || lchild->type == SHIFT)) {
lchild->n[0].i += rlchild->n[0].i;
//remove the STMT and STMTS from tree and free them
n->n[1].n = rtmp->n[1].n;
free(rlchild);
free(rtmp);
changed = 1;
} else {
break;
}
}
}
changed |= callAll(n, join);
return changed;
}
int nullify(Node** np) {
Node* n = *np;
if (n == NULL) {
return 0;
}
int changed = 0;
if (n->type == STMTS) {
Node* left = n->n[0].n;
//eliminate +- and ><
if (((left->type == SUM || left->type == SHIFT) && left->n[0].i == 0)) {
//remove n from tree and free it
*np = n->n[1].n;
free(n);
free(left);
changed = 1;
}
}
changed |= callAll(*np, nullify);
return changed;
}
/*
* This optimization should always run before the useSet and after nullify
* also maybe after useSet has been run this optimization should no longer be deployed
*/
int destroyLoops(Node** np) {
Node* n = *np;
if (n == NULL) {
return 0;
}
int changed = 0;
if (n->type == STMTS) {
Node* left = n->n[0].n;
Node* right = n->n[1].n;
Node* rl = right ? right->n[0].n : NULL;
if (left->type == LOOP && rl && rl->type == LOOP) {
//delete right
n->n[1].n = right->n[1].n;
right->n[1].n = NULL;
clipBranch(&right);
}
}
changed |= callAll(*np, nullify);
return changed;
}
int useSet(Node** np) {
Node *n = *np;
if (n == NULL) {
return 0;
}
int changed = 0;
if (n->type == LOOP) {
if (n->n[0].n == NULL) {
return 0;
}
Node* body = n->n[0].n;
if (body->type != STMTS) {
goto ret;
}
/*
* perform static analysis to see if it's only composed of
* +-<> and always lands on the same cell.
*/
int netMove = 0;
for (Node* node = body; node != NULL; node = node->n[1].n) {
Node* left = node->n[0].n;
if (!(left && (left->type == SHIFT || left->type == SUM))) {
goto ret;
}
if (left->type == SHIFT) {
netMove += left->n[0].i;
}
}
if (netMove == 0) {
/*
* the cell the loop starts on/ends on must be zero to exit the loop.
* This means we can set it to 0 and add it/subtract it to the other
* cells.
*/
Map* m = newIntPtrMap(100);
/*
* guarenty that if someone does [>+<] we don't
* assume wrongly that zero is in the map
*/
int* zero = calloc(1, sizeof(int));
mPut(m, zero, zero);
for (Node* node = body; node != NULL; node = node->n[1].n) {
Node* left = node->n[0].n;
if (left->type == SHIFT) {
netMove += left->n[0].i;
} else {
int* n = mGet(m, &netMove);
if (n == NULL) {
n = calloc(1, sizeof(int));
mPut(m, &netMove, n);
}
*n += left->n[0].i;
}
}
void* keys[m->size];
mGetKeys(m, keys);
/*
* first prune the map of redundant values
*/
int scale = 0;
for (int i = 0; i < m->size; i++) {
int* k = keys[i];
int* val = mGet(m, k);
if (*k == 0) {
scale = - *val;
} else if (*val == 0) {
mDel(m, k);
free(val);
}
}
mGetKeys(m, keys);
int quantity = m->size;
Node* new = mkNode(quantity, SET);
int newIndx = 1;
for (int i = 0; i < m->size; i++) {
int* k = keys[i];
int* val = mGet(m, k);
if (*k == 0) {
//TODO warn if loop is infinite
} else {
Point *p = new->n[newIndx].p = calloc(1, sizeof(Point));
//offset
p->x = *k;
int v = gcd(*val, scale);
//numerator
p->y = *val/v;
//denominator
p->z = scale/v;
newIndx++;
}
free(val);
}
destroyMap(m);
clipBranch(np);
*np = new;
n = NULL;
}
}
ret:
changed |= callAll(*np, useSet);
return changed;
}
int delayShift(Node** np) {
Node* n = *np;
if (n == NULL) {
return 0;
}
Node** pRef = np;
int changed = 0;
if (n->type == STMTS) {
Node* shift = NULL;
while (n != NULL) {
Node* nl = n->n[0].n;
int t = nl->type;
if (t == SHIFT) {
if (shift == NULL) {
shift = n;
//remove from tree
*pRef = n->n[1].n;
n = *pRef;
shift->n[1].n = NULL;
} else {
shift->n[0].n->n[0].i += nl->n[0].i;
Node* old = n;
//remove from tree
*pRef = n->n[1].n;
n = *pRef;
//free
free(nl);
free(old);
changed |= 1;
}
} else if (t == SUM || t == OUT || t == IN || t == SET) {
int* off;
if (t == SUM) {
off = &nl->n[1].i;
} else {
off = &nl->n[0].i;
}
if (shift != NULL) {
*off += shift->n[0].n->n[0].i;
changed |= 1;
}
pRef = &n->n[1].n;
n = *pRef;
} else {
break;
}
}
if (shift != NULL) {
shift->n[1].n = *pRef;
*pRef = shift;
}
}
return changed | callAll(*pRef, delayShift);
}
int compNodes(const void* a, const void* b) {
const Node* na = *((Node**)a);
const Node* nb = *((Node**)b);
int ia = na->n[0].n->n[1].i;
int ib = nb->n[0].n->n[1].i;
return ia - ib;
}
int sort(Node** np) {
Node *n = *np;
if (n == NULL) {
return 0;
}
int changed = 0;
if (n->type == STMTS) {
Node** pRef = np;
Node** buff = NULL;
int nodes = 0;
while (n != NULL && n->n[0].n->type == SUM) {
nodes++;
buff = realloc(buff, nodes*sizeof(*buff));
buff[nodes-1] = n;
n = n->n[1].n;
}
if (nodes > 1) {
qsort(buff, nodes, sizeof(*buff), compNodes);
*np = buff[0];
for (int i = 1; i < nodes; i++) {
buff[i-1]->n[1].n = buff[i];
}
buff[nodes-1]->n[1].n = n;
}
free(buff);
}
changed |= callAll(n, join);
return changed;
}
void optimize(Node** n, int optLevel) {
if (optLevel <= 0) {
return;
}
/*
* TODO a lot of the optimization functions are both iterative
* and recursive O(n**2). Try to go through and make them O(n)
*/
int changed = 1;
while (changed) {
changed = 0;
changed |= join(n);
changed |= nullify(n);
changed |= destroyLoops(n);
if (!changed) {
if (optLevel >= 2) {
changed |= useSet(n);
}
if (optLevel >= 3) {
changed |= delayShift(n);
changed |= sort(n);
}
}
}
}
int clipBranch(Node** np) {
if (*np == NULL) {
return 0;
}
Node* n = *np;
//handle alloc children that aren't nodes
if (n->type == SET) {
for (int i = 1; i < n->sz; i++) {
free(n->n[i].p);
}
}
//handle regular nodes
callAll(*np, clipBranch);
free(*np);
*np = NULL;
return 0;
}