-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathir.c
455 lines (391 loc) · 11.8 KB
/
ir.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "ir.h"
#include "lex.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int ir_is_jmp(u32 op) {
return op & IR_OP_JMP;
}
int ir_is_mark(u32 op) {
return op & IR_MARK;
}
int ir_current(ir *c) {
return vsize(c->ops);
}
int ir_newvar(ir *c) {
return c->iv++;
}
int ir_init(ir *c) {
if (rhhm_init(&c->ctt_map, IR_CTT_MAX*2, 0)) {
return 1;
}
vclear(c->ctts);
vclear(c->ops);
c->iv = 0;
c->iphi = IR_OP_MAX;
c->phidepth = 0;
return 0;
}
void ir_destroy(ir *c) {
rhhm_destroy(&c->ctt_map);
}
int ir_ctt(ir *c, bv v) {
if (vfull(c->ctts)) abort();
bv idx = hm_get(&c->ctt_map, v);
if (idx.u != bv_nil) return idx.i;
vpush(c->ctts, v); //c->ctts[c->ic++] = v;
idx.i = -vsize(c->ctts); //-c->ic;
hm_set(&c->ctt_map, v, idx);
return -vsize(c->ctts); //-c->ic;
}
int ir_op(ir *c, i16 op, i16 a, i16 b, u16 t) {
if (vfull(c->ops)) abort();
tac tmp;
tmp.op = op;
tmp.a = a;
tmp.b = b;
tmp.target = t;
vpush(c->ops, tmp);
/*int i = c->io++;
c->ops[i].op = op;
c->ops[i].a = a;
c->ops[i].b = b;
c->ops[i].target = t;*/
return t;
}
int ir_phi_begin(ir *c, int type) {
int i = --c->iphi;
vget(c->ops, i).op = IR_OP_NOOP; // mark
c->phidepth++;
c->phi_join_pos[c->phidepth] = vsize(c->ops);
c->phi_join_type[c->phidepth] = type;
return c->phidepth;
}
int ir_phi_ins(ir *c, int val, int old, u16 *assignment) {
if (!c->phidepth) return 1;
int i = c->iphi;
while (vget(c->ops, i).op != IR_OP_NOOP) {
if (vget(c->ops, i).a == old || vget(c->ops, i).b == old) {
goto found;
}
i++;
}
i = --c->iphi;
vget(c->ops, i).op = IR_OP_PHI;
vget(c->ops, i).b = old;
vget(c->ops, i).target = old;
found:
vget(c->ops, i).a = val;
return 0;
}
int ir_phi_commit(ir *c, u16 *assignment) {
int i, j;
i = j = c->iphi;
while (vget(c->ops, c->iphi++).op != IR_OP_NOOP) j++;
j--;
int jointype = c->phi_join_type[c->phidepth];
int joinpos = c->phi_join_pos[c->phidepth];
int until = vsize(c->ops);
int shift = (j-i)+1;
c->phidepth--;
while (j >= i) {
int old = vget(c->ops, j).target;
int olda = vget(c->ops, j).a;
int oldb = vget(c->ops, j).b;
int nv = ir_newvar(c);
assignment[nv] = assignment[old];
assignment[old] = nv;
assignment[olda] = nv;
assignment[oldb] = nv;
if (jointype != PHI_COND) { // fix loop var usages
for (int k = joinpos; k < until; k++) {
if (vget(c->ops, k).op == IR_OP_NOOP) continue;
int replace = vget(c->ops, j).target; //a;
if (vget(c->ops, k).a == replace) vget(c->ops, k).a = nv;
if (vget(c->ops, k).b == replace && vget(c->ops, k).op != IR_OP_CALL)
vget(c->ops, k).b = nv;
}
}
vget(c->ops, j).target = nv;
vpush(c->ops, vget(c->ops, j));
if (c->phidepth) { // commit to upper level
ir_phi_ins(c, nv, old, assignment);
}
j--;
}
if (jointype != PHI_COND && shift > 0) {
for (int k = joinpos; k < until; k++) // fix jumps
if (ir_is_jmp(vget(c->ops, k).op) && vget(c->ops, k).target > joinpos)
vget(c->ops, k).target += shift;
//printf("shifting block of sz %d down %d positions\n", (until-joinpos) + shift, shift);
memmove(vbegin(c->ops) + joinpos + shift, vbegin(c->ops) + joinpos, ((until-joinpos) + shift) * sizeof(tac));
memcpy(vbegin(c->ops) + joinpos, vbegin(c->ops) + joinpos + shift + (until-joinpos), shift * sizeof(tac));
}
return shift;
}
void ir_phi_elim(ir *c) { // eliminate phi nodes
u16 var_live_end[IR_OP_MAX] = {0}; // last use of var
for (int i = 0; i < vsize(c->ops); i++) {
int op = vget(c->ops, i).op;
if (op == IR_OP_NOOP) continue;
int a = vget(c->ops, i).a;
int b = vget(c->ops, i).b;
int target = vget(c->ops, i).target;
if (op == IR_FUNCTION_BEGIN || op == IR_FUNCTION_END) continue;
if (!ir_is_jmp(op) && target != IR_NO_TARGET) var_live_end[target] = i;
if (op != IR_OP_PHI) {
if (a != IR_NO_ARG) var_live_end[a] = i;
if (b != IR_NO_ARG && op != IR_OP_CALL) var_live_end[b] = i;
}
}
u16 var_live_ini[IR_OP_MAX]; // first use of var
memset(var_live_ini, 0xff, sizeof var_live_ini);
for (int i = vsize(c->ops); i-- > 0; ) {
int op = vget(c->ops, i).op;
if (op == IR_OP_NOOP || op == IR_FUNCTION_BEGIN || op == IR_FUNCTION_END) continue;
int target = vget(c->ops, i).target;
if (!ir_is_jmp(op) && target != IR_NO_TARGET) var_live_ini[target] = i;
}
#ifdef DBG
printf(" .");
for (int j = 0; j < c->iv; j++) printf("%2d", j % 10);
puts("");
for (int i = 0; i < vsize(c->ops); i++) {
printf("%2d. ", i);
for (int j = 0; j < c->iv; j++) {
if (i >= var_live_ini[j] && i <= var_live_end[j]) {
printf("@ ");
} else {
printf(". ");
}
}
printf("\n");
}
#endif
for (int i = 0; i < vsize(c->ops); i++) {
int op = vget(c->ops, i).op;
int a = vget(c->ops, i).a;
int b = vget(c->ops, i).b;
int target = vget(c->ops, i).target;
if (op == IR_OP_PHI) {
int ai = var_live_ini[a];
int ae = var_live_end[a];
int bi = var_live_ini[b];
int be = var_live_end[b];
#ifdef DBG
printf(" 1.sub %d for %d\n", a, target);
printf(" 2.sub %d for %d\n", b, target);
#endif
//if (ae < bi || be < ai) {
// CSSA only
while (ai <= ae) {
if (vget(c->ops, ai).a == a) vget(c->ops, ai).a = target;
if (vget(c->ops, ai).b == a) vget(c->ops, ai).b = target;
if (!ir_is_jmp(vget(c->ops, ai).op) && vget(c->ops, ai).target == a)
vget(c->ops, ai).target = target;
ai++;
}
while (bi <= be) {
if (vget(c->ops, bi).a == b) vget(c->ops, bi).a = target;
if (vget(c->ops, bi).b == b) vget(c->ops, bi).b = target;
if (!ir_is_jmp(vget(c->ops, bi).op) && vget(c->ops, bi).target == b)
vget(c->ops, bi).target = target;
bi++;
}
vget(c->ops, i).op = IR_OP_NOOP;
if (ai < var_live_ini[target]) var_live_ini[target] = ai;
if (bi < var_live_ini[target]) var_live_ini[target] = bi;
if (ae > var_live_end[target]) var_live_ini[target] = ae;
if (be > var_live_end[target]) var_live_ini[target] = be;
/*} else {
c->ops[i].op = IR_OP_NOOP;
}*/
}
}
}
/*opt*/
static int fold(ir *c, tac *t) {
if (t->op == IR_OP_LCOPY && t->a < 0) return t->a;
if (t->a >= 0 || t->b >= 0) return 0;
bv a = vget(c->ctts, -t->a-1);
bv b = vget(c->ctts, -t->b-1);
bv v;
switch (t->op) {
case '+': v = bv_add(NULL, a, b); break;
case '*': v = bv_mul(NULL, a, b); break;
case '-': v = bv_sub(NULL, a, b); break;
case '/': v = bv_div(NULL, a, b); break;
default: return 0;
}
return ir_ctt(c, v);
}
void ir_opt(ir *c) {
BSET(stored, IR_OP_MAX);
u16 var_live_end[IR_OP_MAX] = {0}; // last use of var
for (int i = 0; i < IR_OP_MAX; i++) var_live_end[i] = -1;
for (int i = 0; i < vsize(c->ops); i++) {
int op = vget(c->ops, i).op;
if (op == IR_OP_NOOP || ir_is_mark(op)) continue;
int a = vget(c->ops, i).a;
int b = vget(c->ops, i).b;
//int target = vget(c->ops, i).target;
if (op == IR_OP_PHI) {
if (a >= 0) BSET_SET(stored, a);
if (b >= 0) BSET_SET(stored, b);
}
if (a >= 0 && a != IR_NO_ARG) var_live_end[a] = i;
if (b >= 0 && b != IR_NO_ARG && op != IR_OP_CALL) var_live_end[b] = i;
}
u16 var_live_ini[IR_OP_MAX]; // first use of var
memset(var_live_ini, 0xff, sizeof var_live_ini);
for (int i = vsize(c->ops); i-- > 0; ) {
int op = vget(c->ops, i).op;
if (op == IR_OP_NOOP || ir_is_mark(op)) continue;
int target = vget(c->ops, i).target;
if (!ir_is_jmp(op) && target != IR_NO_TARGET) var_live_ini[target] = i;
}
for (int i = 1; i < vsize(c->ops); i++) { // peephole
int o0 = vget(c->ops, i-1).op;
int a0 = vget(c->ops, i-1).a;
int b0 = vget(c->ops, i-1).b;
int t0 = vget(c->ops, i-1).target;
int o1 = vget(c->ops, i ).op;
int a1 = vget(c->ops, i ).a;
//int b1 = vget(c->ops, i ).b;
int t1 = vget(c->ops, i ).target;
if (o0 == LEX_NE && o1 == IR_OP_JZ && a1 == t0) {
vget(c->ops, i).op = IR_OP_JE; vget(c->ops, i).a = a0; vget(c->ops, i).b = b0;
vget(c->ops, i-1).op = IR_OP_NOOP;
} else if (o0 == LEX_NE && o1 == IR_OP_JNZ && a1 == t0) {
vget(c->ops, i).op = IR_OP_JNE; vget(c->ops, i).a = a0; vget(c->ops, i).b = b0;
vget(c->ops, i-1).op = IR_OP_NOOP;
} else if (o0 == LEX_EQ && o1 == IR_OP_JZ && a1 == t0) {
vget(c->ops, i).op = IR_OP_JNE; vget(c->ops, i).a = a0; vget(c->ops, i).b = b0;
vget(c->ops, i-1).op = IR_OP_NOOP;
} else if (o0 == LEX_EQ && o1 == IR_OP_JNZ && a1 == t0) {
vget(c->ops, i).op = IR_OP_JE; vget(c->ops, i).a = a0; vget(c->ops, i).b = b0;
vget(c->ops, i-1).op = IR_OP_NOOP;
}
if (o1 == IR_OP_LCOPY && t0 == a1 && var_live_end[a1] == i) {
vget(c->ops, i-1).target = t1;
vget(c->ops, i).op = IR_OP_NOOP;
}
}
}
#define COLORF(c) "\x1b[3" #c "m"
#define COLORFB(c) "\x1b[9" #c "m"
#define COLORB(c) "\x1b[4" #c "m"
//#define RESETF "\x1b[39m"
#define RESETB "\x1b[49m"
#define RESET "\x1b[0m"
#define RESETF RESET
void ir_disp(ir *c) {
int i;
for (i = 0; i < vsize(c->ops); i++) {
tac cur = vget(c->ops, i);
if (cur.op == IR_OP_NOOP) {
//puts("--- nop ---");
continue;
}
printf("%04d ", i);
if (cur.op == IR_LOOP_HEADER) {
puts("--- loop-header ---");
continue;
}
if (cur.op == IR_LOOP_BEGIN) {
puts("--- loop-block ---");
continue;
}
if (cur.op == IR_LOOP_END) {
puts("--- loop-end ---");
continue;
}
if (cur.op == IR_FUNCTION_BEGIN) {
printf("--- function-header --- %d\n", cur.target);
continue;
}
if (cur.op == IR_FUNCTION_END) {
printf("--- function-end --- %d\n", cur.target);
continue;
}
printf(COLORF(4));
if (!ir_is_jmp(cur.op)) {
switch (c->types[cur.target]) {
/*case IR_TYPE_NONE: printf("%3s ", "<?>"); break;
case IR_TYPE_ANY: printf("%3s ", "any"); break;
case IR_TYPE_NUM: printf("%3s ", "num"); break;
case IR_TYPE_INT: printf("%3s ", "int"); break;*/
}
} else {
//printf(" ");
}
printf(RESETF);
if (cur.op < 256) {
printf("%c ", cur.op);
} else {
switch (cur.op) {
case LEX_EQ: printf("eq "); break;
case LEX_NE: printf("ne "); break;
case IR_OP_NEWTBL: printf("tnew "); break;
case IR_OP_LCOPY: printf("lcpy "); break;
case IR_OP_COPY: printf("cpy "); break;
case IR_OP_TLOAD: printf("tget "); break;
case IR_OP_TSTORE: printf("tset "); break;
case IR_OP_GLOAD: printf(COLORF(5) "gget " RESETF); break;
case IR_OP_GSTORE: printf(COLORF(5) "gset " RESETF); break;
case IR_OP_JZ: printf("jz "); break;
case IR_OP_JNZ: printf("jnz "); break;
case IR_OP_JMP: printf("jmp "); break;
case IR_OP_PHI: printf("phi "); break;
case IR_OP_RET: printf("ret "); break;
case IR_OP_INC: printf("inc "); break;
case IR_OP_DEC: printf("dec "); break;
case IR_OP_JE: printf("JE "); break;
case IR_OP_JNE: printf("JNE "); break;
case IR_OP_CALL: printf("call "); break;
case IR_OP_ARG: printf("arg "); break;
case IR_OP_PARAM: printf("par "); break;
}
}
if (cur.a != IR_NO_ARG) {
if (cur.a < 0) {
if (bv_is_str(vget(c->ctts, -cur.a-1))) {
printf("%10s", "str");
} else if (bv_is_sstr(vget(c->ctts, -cur.a-1))) {
printf(COLORFB(2));
printf("%10.*s",
bv_get_sstr_len(vget(c->ctts, -cur.a-1)),
bv_get_sstr(&vget(c->ctts, -cur.a-1)));
printf(RESETF);
} else {
printf(COLORFB(4));
printf("%10.5g", vget(c->ctts, -cur.a-1).d);
printf(RESETF);
}
} else printf("%10d", cur.a);
} else printf("%10s", "-");
printf(" ");
if (cur.b != IR_NO_ARG) {
if (cur.b < 0) {
if (bv_is_str(vget(c->ctts, -cur.b-1))) {
printf("%10s", "str");
} else if (bv_is_sstr(vget(c->ctts, -cur.b-1))) {
printf(COLORFB(2));
printf("%10.*s",
bv_get_sstr_len(vget(c->ctts, -cur.b-1)),
bv_get_sstr(&vget(c->ctts, -cur.b-1)));
printf(RESETF);
} else {
printf(COLORFB(4));
printf("%10.5g", vget(c->ctts, -cur.b-1).d);
printf(RESETF);
}
} else printf("%10d", cur.b);
} else printf("%10s", "-");
if (cur.target != IR_NO_TARGET) printf("%10d", cur.target);
else printf("%10s", "-");
if (ir_is_jmp(cur.op)) printf(" ~>" RESETF);
puts("");
}
}