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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
// Optimizations for the IR code
void ir_opt_add_operands(irValueArray *ops, irInstr *i) {
switch (i->kind) {
case irInstr_Comment:
break;
case irInstr_Local:
break;
case irInstr_ZeroInit:
array_add(ops, i->ZeroInit.address);
break;
case irInstr_Store:
array_add(ops, i->Store.address);
array_add(ops, i->Store.value);
break;
case irInstr_Load:
array_add(ops, i->Load.address);
break;
case irInstr_ArrayElementPtr:
array_add(ops, i->ArrayElementPtr.address);
array_add(ops, i->ArrayElementPtr.elem_index);
break;
case irInstr_StructElementPtr:
array_add(ops, i->StructElementPtr.address);
break;
case irInstr_PtrOffset:
array_add(ops, i->PtrOffset.address);
array_add(ops, i->PtrOffset.offset);
break;
case irInstr_ArrayExtractValue:
array_add(ops, i->ArrayExtractValue.address);
break;
case irInstr_StructExtractValue:
array_add(ops, i->StructExtractValue.address);
break;
case irInstr_Conv:
array_add(ops, i->Conv.value);
break;
case irInstr_Jump:
break;
case irInstr_If:
array_add(ops, i->If.cond);
break;
case irInstr_Return:
if (i->Return.value != NULL) {
array_add(ops, i->Return.value);
}
break;
case irInstr_Select:
array_add(ops, i->Select.cond);
break;
case irInstr_Phi:
for_array(j, i->Phi.edges) {
array_add(ops, i->Phi.edges.e[j]);
}
break;
case irInstr_Unreachable:
break;
case irInstr_UnaryOp:
array_add(ops, i->UnaryOp.expr);
break;
case irInstr_BinaryOp:
array_add(ops, i->BinaryOp.left);
array_add(ops, i->BinaryOp.right);
break;
case irInstr_Call:
array_add(ops, i->Call.value);
for (isize j = 0; j < i->Call.arg_count; j++) {
array_add(ops, i->Call.args[j]);
}
break;
case irInstr_VectorExtractElement:
array_add(ops, i->VectorExtractElement.vector);
array_add(ops, i->VectorExtractElement.index);
break;
case irInstr_VectorInsertElement:
array_add(ops, i->VectorInsertElement.vector);
array_add(ops, i->VectorInsertElement.elem);
array_add(ops, i->VectorInsertElement.index);
break;
case irInstr_VectorShuffle:
array_add(ops, i->VectorShuffle.vector);
break;
case irInstr_StartupRuntime:
break;
case irInstr_BoundsCheck:
array_add(ops, i->BoundsCheck.index);
array_add(ops, i->BoundsCheck.len);
break;
case irInstr_SliceBoundsCheck:
array_add(ops, i->SliceBoundsCheck.low);
array_add(ops, i->SliceBoundsCheck.high);
break;
}
}
void ir_opt_block_replace_pred(irBlock *b, irBlock *from, irBlock *to) {
for_array(i, b->preds) {
irBlock *pred = b->preds.e[i];
if (pred == from) {
b->preds.e[i] = to;
}
}
}
void ir_opt_block_replace_succ(irBlock *b, irBlock *from, irBlock *to) {
for_array(i, b->succs) {
irBlock *succ = b->succs.e[i];
if (succ == from) {
b->succs.e[i] = to;
}
}
}
bool ir_opt_block_has_phi(irBlock *b) {
return b->instrs.e[0]->Instr.kind == irInstr_Phi;
}
irValueArray ir_get_block_phi_nodes(irBlock *b) {
irValueArray phis = {0};
for_array(i, b->instrs) {
irInstr *instr = &b->instrs.e[i]->Instr;
if (instr->kind != irInstr_Phi) {
phis = b->instrs;
phis.count = i;
return phis;
}
}
return phis;
}
void ir_remove_pred(irBlock *b, irBlock *p) {
irValueArray phis = ir_get_block_phi_nodes(b);
isize i = 0;
for_array(j, b->preds) {
irBlock *pred = b->preds.e[j];
if (pred != p) {
b->preds.e[i] = b->preds.e[j];
for_array(k, phis) {
irInstrPhi *phi = &phis.e[k]->Instr.Phi;
phi->edges.e[i] = phi->edges.e[j];
}
i++;
}
}
b->preds.count = i;
for_array(k, phis) {
irInstrPhi *phi = &phis.e[k]->Instr.Phi;
phi->edges.count = i;
}
}
void ir_remove_dead_blocks(irProcedure *proc) {
isize j = 0;
for_array(i, proc->blocks) {
irBlock *b = proc->blocks.e[i];
if (b == NULL) {
continue;
}
// NOTE(bill): Swap order
b->index = j;
proc->blocks.e[j++] = b;
}
proc->blocks.count = j;
}
void ir_mark_reachable(irBlock *b) {
isize const WHITE = 0;
isize const BLACK = -1;
b->index = BLACK;
for_array(i, b->succs) {
irBlock *succ = b->succs.e[i];
if (succ->index == WHITE) {
ir_mark_reachable(succ);
}
}
}
void ir_remove_unreachable_blocks(irProcedure *proc) {
isize const WHITE = 0;
isize const BLACK = -1;
for_array(i, proc->blocks) {
proc->blocks.e[i]->index = WHITE;
}
ir_mark_reachable(proc->blocks.e[0]);
for_array(i, proc->blocks) {
irBlock *b = proc->blocks.e[i];
if (b->index == WHITE) {
for_array(j, b->succs) {
irBlock *c = b->succs.e[j];
if (c->index == BLACK) {
ir_remove_pred(c, b);
}
}
// NOTE(bill): Mark as empty but don't actually free it
// As it's been allocated with an arena
proc->blocks.e[i] = NULL;
}
}
ir_remove_dead_blocks(proc);
}
bool ir_opt_block_fusion(irProcedure *proc, irBlock *a) {
if (a->succs.count != 1) {
return false;
}
irBlock *b = a->succs.e[0];
if (b->preds.count != 1) {
return false;
}
if (ir_opt_block_has_phi(b)) {
return false;
}
array_pop(&a->instrs); // Remove branch at end
for_array(i, b->instrs) {
array_add(&a->instrs, b->instrs.e[i]);
ir_set_instr_parent(b->instrs.e[i], a);
}
array_clear(&a->succs);
for_array(i, b->succs) {
array_add(&a->succs, b->succs.e[i]);
}
// Fix preds links
for_array(i, b->succs) {
ir_opt_block_replace_pred(b->succs.e[i], b, a);
}
proc->blocks.e[b->index] = NULL;
return true;
}
void ir_opt_blocks(irProcedure *proc) {
ir_remove_unreachable_blocks(proc);
#if 1
bool changed = true;
while (changed) {
changed = false;
for_array(i, proc->blocks) {
irBlock *b = proc->blocks.e[i];
if (b == NULL) {
continue;
}
GB_ASSERT(b->index == i);
if (ir_opt_block_fusion(proc, b)) {
changed = true;
}
// TODO(bill): other simple block optimizations
}
}
#endif
ir_remove_dead_blocks(proc);
}
void ir_opt_build_referrers(irProcedure *proc) {
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&proc->module->tmp_arena);
irValueArray ops = {0}; // NOTE(bill): Act as a buffer
array_init_reserve(&ops, proc->module->tmp_allocator, 64); // HACK(bill): This _could_ overflow the temp arena
for_array(i, proc->blocks) {
irBlock *b = proc->blocks.e[i];
for_array(j, b->instrs) {
irValue *instr = b->instrs.e[j];
array_clear(&ops);
ir_opt_add_operands(&ops, &instr->Instr);
for_array(k, ops) {
irValue *op = ops.e[k];
if (op == NULL) {
continue;
}
irValueArray *refs = ir_value_referrers(op);
if (refs != NULL) {
array_add(refs, instr);
}
}
}
}
gb_temp_arena_memory_end(tmp);
}
// State of Lengauer-Tarjan algorithm
// Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
typedef struct irLTState {
isize count;
// NOTE(bill): These are arrays
irBlock **sdom; // Semidominator
irBlock **parent; // Parent in DFS traversal of CFG
irBlock **ancestor;
} irLTState;
// §2.2 - bottom of page
void ir_lt_link(irLTState *lt, irBlock *p, irBlock *q) {
lt->ancestor[q->index] = p;
}
i32 ir_lt_depth_first_search(irLTState *lt, irBlock *p, i32 i, irBlock **preorder) {
preorder[i] = p;
p->dom.pre = i++;
lt->sdom[p->index] = p;
ir_lt_link(lt, NULL, p);
for_array(index, p->succs) {
irBlock *q = p->succs.e[index];
if (lt->sdom[q->index] == NULL) {
lt->parent[q->index] = p;
i = ir_lt_depth_first_search(lt, q, i, preorder);
}
}
return i;
}
irBlock *ir_lt_eval(irLTState *lt, irBlock *v) {
irBlock *u = v;
for (;
lt->ancestor[v->index] != NULL;
v = lt->ancestor[v->index]) {
if (lt->sdom[v->index]->dom.pre < lt->sdom[u->index]->dom.pre) {
u = v;
}
}
return u;
}
typedef struct irDomPrePost {
i32 pre, post;
} irDomPrePost;
irDomPrePost ir_opt_number_dom_tree(irBlock *v, i32 pre, i32 post) {
irDomPrePost result = {pre, post};
v->dom.pre = pre++;
for_array(i, v->dom.children) {
result = ir_opt_number_dom_tree(v->dom.children.e[i], result.pre, result.post);
}
v->dom.post = post++;
result.pre = pre;
result.post = post;
return result;
}
// NOTE(bill): Requires `ir_opt_blocks` to be called before this
void ir_opt_build_dom_tree(irProcedure *proc) {
// Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&proc->module->tmp_arena);
isize n = proc->blocks.count;
irBlock **buf = gb_alloc_array(proc->module->tmp_allocator, irBlock *, 5*n);
irLTState lt = {0};
lt.count = n;
lt.sdom = &buf[0*n];
lt.parent = &buf[1*n];
lt.ancestor = &buf[2*n];
irBlock **preorder = &buf[3*n];
irBlock **buckets = &buf[4*n];
irBlock *root = proc->blocks.e[0];
// Step 1 - number vertices
i32 pre_num = ir_lt_depth_first_search(<, root, 0, preorder);
gb_memmove(buckets, preorder, n*gb_size_of(preorder[0]));
for (i32 i = n-1; i > 0; i--) {
irBlock *w = preorder[i];
// Step 3 - Implicitly define idom for nodes
for (irBlock *v = buckets[i]; v != w; v = buckets[v->dom.pre]) {
irBlock *u = ir_lt_eval(<, v);
if (lt.sdom[u->index]->dom.pre < i) {
v->dom.idom = u;
} else {
v->dom.idom = w;
}
}
// Step 2 - Compute all sdoms
lt.sdom[w->index] = lt.parent[w->index];
for_array(pred_index, w->preds) {
irBlock *v = w->preds.e[pred_index];
irBlock *u = ir_lt_eval(<, v);
if (lt.sdom[u->index]->dom.pre < lt.sdom[w->index]->dom.pre) {
lt.sdom[w->index] = lt.sdom[u->index];
}
}
ir_lt_link(<, lt.parent[w->index], w);
if (lt.parent[w->index] == lt.sdom[w->index]) {
w->dom.idom = lt.parent[w->index];
} else {
buckets[i] = buckets[lt.sdom[w->index]->dom.pre];
buckets[lt.sdom[w->index]->dom.pre] = w;
}
}
// The rest of Step 3
for (irBlock *v = buckets[0]; v != root; v = buckets[v->dom.pre]) {
v->dom.idom = root;
}
// Step 4 - Explicitly define idom for nodes (in preorder)
for (isize i = 1; i < n; i++) {
irBlock *w = preorder[i];
if (w == root) {
w->dom.idom = NULL;
} else {
// Weird tree relationships here!
if (w->dom.idom != lt.sdom[w->index]) {
w->dom.idom = w->dom.idom->dom.idom;
}
// Calculate children relation as inverse of idom
if (w->dom.idom->dom.children.e == NULL) {
// TODO(bill): Is this good enough for memory allocations?
array_init(&w->dom.idom->dom.children, heap_allocator());
}
array_add(&w->dom.idom->dom.children, w);
}
}
ir_opt_number_dom_tree(root, 0, 0);
gb_temp_arena_memory_end(tmp);
}
void ir_opt_mem2reg(irProcedure *proc) {
// TODO(bill): ir_opt_mem2reg
}
void ir_opt_tree(irGen *s) {
s->opt_called = true;
for_array(member_index, s->module.procs) {
irProcedure *proc = s->module.procs.e[member_index];
if (proc->blocks.count == 0) { // Prototype/external procedure
continue;
}
ir_opt_blocks(proc);
#if 1
ir_opt_build_referrers(proc);
ir_opt_build_dom_tree(proc);
// TODO(bill): ir optimization
// [ ] cse (common-subexpression) elim
// [ ] copy elim
// [ ] dead code elim
// [ ] dead store/load elim
// [ ] phi elim
// [ ] short circuit elim
// [ ] bounds check elim
// [ ] lift/mem2reg
// [ ] lift/mem2reg
ir_opt_mem2reg(proc);
#endif
GB_ASSERT(proc->blocks.count > 0);
ir_number_proc_registers(proc);
}
}
|