forked from freewilll/wcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssa.c
More file actions
1853 lines (1499 loc) · 71.2 KB
/
ssa.c
File metadata and controls
1853 lines (1499 loc) · 71.2 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wcc.h"
static List *allocated_phi_values;
static void make_live_range_spill_cost(Function *function);
void free_live_range_spill_cost(Function *function);
typedef struct live_range {
LongSet *set;
long weight;
} LiveRange;
typedef struct coalesce {
int
src, dst, cost;
} Coalesce;
// Optimize arithmetic operations for integer values
void optimize_integer_arithmetic_operation(Tac *tac) {
Value *v;
Value *cv = 0;
long c, l;
// If both are constants, don't optimizing, since the operation will be evaluated later on.
if (tac->src1 && tac->src1->is_constant && tac->src2 && tac->src2->is_constant) return;
if (tac->src1 && tac->src1->is_constant) {
cv = tac->src1;
v = tac->src2;
}
else if (tac->src2 && tac->src2->is_constant) {
v = tac->src1;
cv = tac->src2;
}
if (cv) c = cv->int_value;
if (tac->operation == IR_MUL && cv) {
if (c == 0) {
tac->operation = IR_MOVE;
tac->src1 = new_integral_constant(tac->dst->type->type, 0);
tac->src2 = 0;
}
else if (c == 1) {
tac->operation = IR_MOVE;
tac->src1 = v;
tac->src2 = 0;
}
else if ((c & (c - 1)) == 0) {
l = -1;
while (c > 0) { c >>= 1; l++; }
tac->operation = IR_BSHL;
tac->src1 = v;
tac->src2 = new_integral_constant(TYPE_INT, l);
}
}
else if (tac->operation == IR_DIV && cv && tac->src2->is_constant) {
if (c == 1) {
tac->operation = IR_MOVE;
tac->src1 = v;
tac->src2 = 0;
}
else if (tac->src1->type->is_unsigned && (c & (c - 1)) == 0) {
l = -1;
while (c > 0) { c >>= 1; l++; }
tac->operation = IR_BSHR;
tac->src1 = v;
tac->src2 = new_integral_constant(TYPE_INT, l);
}
}
else if (tac->operation == IR_MOD && cv && tac->src2->is_constant) {
if (c == 1) {
tac->operation = IR_MOVE;
tac->src1 = new_integral_constant(tac->dst->type->type, 0);
tac->src2 = 0;
}
else if ((c & (c - 1)) == 0) {
l = 0;
while (c > 1) { c = c >> 1; l = (l << 1) | 1; }
tac->operation = IR_BAND;
tac->src1 = v;
tac->src2 = new_integral_constant(TYPE_INT, l);
}
}
}
void optimize_floating_point_arithmetic_operation(Tac *tac) {
Value *v;
Value *cv = 0;
long double c;
if (tac->src1 && tac->src1->is_constant) {
cv = tac->src1;
v = tac->src2;
}
else if (tac->src2 && tac->src2->is_constant) {
v = tac->src1;
cv = tac->src2;
}
if (cv) c = cv->fp_value;
if (tac->operation == IR_MUL && cv) {
if (c == 0.0L) {
tac->operation = IR_MOVE;
tac->src1 = new_floating_point_constant(tac->dst->type->type, 0.0L);
tac->src2 = 0;
}
else if (c == 1.0L) {
tac->operation = IR_MOVE;
tac->src1 = v;
tac->src2 = 0;
}
}
else if (tac->operation == IR_DIV && cv && tac->src2->is_constant) {
c = tac->src2->fp_value;
if (c == 0.0L)
panic("Division by zero");
else if (c == 1.0L) {
tac->operation = IR_MOVE;
tac->src1 = v;
tac->src2 = 0;
}
}
}
void optimize_arithmetic_operations(Function *function) {
if (!opt_optimize_arithmetic_operations) return;
for (Tac *tac = function->ir; tac; tac = tac->next) {
Type *src1_type = tac->src1 && tac->src1->is_constant ? tac->src1->type : 0;
Type *src2_type = tac->src2 && tac->src2->is_constant ? tac->src2->type : 0;
if ((src1_type && is_floating_point_type(src1_type)) || (src2_type && is_floating_point_type(src2_type)))
optimize_floating_point_arithmetic_operation(tac);
else
optimize_integer_arithmetic_operation(tac);
}
}
// Convert an IR_MOVE with an dst is an lvalue in a register into IR_MOVE_TO_PTR.
// The difference with the regular IR_MOVE is that src1 is the destination and src2 is
// the src. The reason for that is that it makes the SSA calulation easier since both
// src1 and src2 are values in registers that are read but not written to in this
// instruction.
void rewrite_lvalue_reg_assignments(Function *function) {
for (Tac *tac = function->ir; tac; tac = tac->next) {
if (tac->operation == IR_MOVE && tac->dst->vreg && tac->dst->is_lvalue) {
tac->operation = IR_MOVE_TO_PTR;
tac->src2 = tac->src1;
tac->src1 = tac->dst;
tac->src1->is_lvalue_in_register = 1;
tac->dst = 0;
}
}
}
static void index_tac(Tac *ir) {
int i = 0;
for (Tac *tac = ir; tac; tac = tac->next) {
tac->index = i;
i++;
}
}
void make_control_flow_graph(Function *function) {
Block *blocks = wcalloc(MAX_BLOCKS, sizeof(Block));
blocks[0].start = function->ir;
int block_count = 1;
for (Tac *tac = function->ir; tac; tac = tac->next) {
if (tac->label) {
if (block_count == MAX_BLOCKS) panic("Exceeded max blocks %d", MAX_BLOCKS);
blocks[block_count - 1].end = tac->prev;
blocks[block_count++].start = tac;
}
// Start a new block after a conditional jump.
// Check if a label is set so that we don't get a double block
if (tac->next && !tac->next->label && (tac->operation == IR_JZ || tac->operation == IR_JNZ || tac->operation == X_JZ || tac->operation == X_JNZ || tac->operation == X_JE || tac->operation == X_JNE || tac->operation == X_JGT || tac->operation == X_JLT || tac->operation == X_JGE || tac->operation == X_JLE || tac->operation == X_JB || tac->operation == X_JA || tac->operation == X_JBE || tac->operation == X_JAE)) {
if (block_count == MAX_BLOCKS) panic("Exceeded max blocks %d", MAX_BLOCKS);
blocks[block_count - 1].end = tac;
blocks[block_count++].start = tac->next;
}
// Make instructions IR_NOP after with JMP operations until the next label since
// the instructions afterwards will never get executed. Furthermore, the
// instructions later on will mess with the liveness analysis, leading to
// incorrect live ranges for the code that _is_ executed, so they need to get
// excluded.
if ((tac->operation == IR_JMP || tac->operation == X_JMP) && tac->next && !tac->next->label) {
while (tac->next && !tac->next->label) {
tac = tac->next;
// Keep IR_DECL_LOCAL_COMP_OBJ, otherwise bad things will happen in the
// stack offset allocation in codegen.
if (tac->operation != IR_DECL_LOCAL_COMP_OBJ) {
tac->operation = IR_NOP;
tac->dst = 0;
tac->src1 = 0;
tac->src2 = 0;
}
}
tac = tac->prev;
}
}
Tac *tac = function->ir;
while (tac->next) tac = tac->next;
blocks[block_count - 1].end = tac;
index_tac(function->ir);
Graph *cfg = new_graph(block_count, 0);
for (int i = 0; i < block_count; i++) {
tac = blocks[i].start;
while (1) {
if (tac->operation == IR_JMP || tac->operation == IR_JZ || tac->operation == IR_JNZ || tac->operation == X_JMP || tac->operation == X_JZ || tac->operation == X_JNZ || tac->operation == X_JE || tac->operation == X_JNE || tac->operation == X_JGT || tac->operation == X_JLT || tac->operation == X_JGE || tac->operation == X_JLE || tac->operation == X_JB || tac->operation == X_JA || tac->operation == X_JBE || tac->operation == X_JAE) {
int label = tac->operation == IR_JMP || tac->operation == X_JMP || tac->operation == X_JZ || tac->operation == X_JNZ || tac->operation == X_JE || tac->operation == X_JNE || tac->operation == X_JGT || tac->operation == X_JLT || tac->operation == X_JGE || tac->operation == X_JLE || tac->operation == X_JB || tac->operation == X_JA || tac->operation == X_JBE || tac->operation == X_JAE
? tac->src1->label
: tac->src2->label;
for (int j = 0; j < block_count; j++)
if (blocks[j].start->label == label)
add_graph_edge(cfg, i, j);
}
else if (tac->operation != IR_RETURN && tac->next && tac->next->label)
// For normal instructions, check if the next instruction is a label, if so it's an edge
add_graph_edge(cfg, i, i + 1);
if (tac->operation == IR_JZ || tac->operation == IR_JNZ || tac->operation == X_JZ || tac->operation == X_JNZ || tac->operation == X_JE || tac->operation == X_JNE || tac->operation == X_JGT || tac->operation == X_JLT || tac->operation == X_JGE || tac->operation == X_JLE || tac->operation == X_JB || tac->operation == X_JA || tac->operation == X_JBE || tac->operation == X_JAE)
add_graph_edge(cfg, i, i + 1);
if (tac == blocks[i].end) break;
if (tac->operation == IR_JMP || tac->operation == X_JMP) break;
tac = tac->next;
}
}
function->blocks = blocks;
function->cfg = cfg;
index_tac(function->ir);
if (debug_ssa_cfg) {
print_ir(function, 0, 0);
printf("Blocks:\n");
for (int i = 0; i < block_count; i++) printf("%d: %d -> %d\n", i, blocks[i].start->index, blocks[i].end->index);
printf("\nEdges:\n");
for (int i = 0; i < cfg->edge_count; i++) printf("%d: %d -> %d\n", i, cfg->edges[i].from->id, cfg->edges[i].to->id);
}
}
void free_control_flow_graph(Function *function) {
wfree(function->blocks);
free_graph(function->cfg);
}
// Algorithm from page 503 of Engineering a compiler
void make_block_dominance(Function *function) {
Graph *cfg = function->cfg;
int block_count = cfg->node_count;
Set **dom = wcalloc(block_count, sizeof(Set));
// dom[0] = {0}
dom[0] = new_set(block_count);
add_to_set(dom[0], 0);
// dom[1 to n] = {0,1,2,..., n}, i.e. the set of all blocks
for (int i = 1; i < block_count; i++) {
dom[i] = new_set(block_count);
for (int j = 0; j < block_count; j++) add_to_set(dom[i], j);
}
Set *is1 = new_set(block_count);
Set *is2 = new_set(block_count);
// Recalculate dom by looking at each node's predecessors until nothing changes
// Dom(n) = {n} union (intersection of all predecessor's doms)
int changed = 1;
while (changed) {
changed = 0;
for (int i = 0; i < block_count; i++) {
Set *pred_intersections = new_set(block_count);
for (int j = 0; j < block_count; j++) add_to_set(pred_intersections, j);
int got_predecessors = 0;
GraphEdge *e = cfg->nodes[i].pred;
while (e) {
set_intersection_to(pred_intersections, pred_intersections, dom[e->from->id]);
got_predecessors = 1;
e = e->next_pred;
}
if (!got_predecessors) {
free_set(pred_intersections);
pred_intersections = new_set(block_count);
}
// Union with {i}
empty_set(is1);
add_to_set(is1, i);
set_union_to(is2, is1, pred_intersections);
// Update if changed & keep looping
if (!set_eq(is2, dom[i])) {
free_set(dom[i]);
dom[i] = copy_set(is2);
changed = 1;
}
free_set(pred_intersections);
}
}
free_set(is1);
free_set(is2);
function->dominance = dom;
if (debug_ssa_dominance) {
printf("\nDominance:\n");
for (int i = 0; i < block_count; i++) {
printf("%d: ", i);
print_set(dom[i]);
printf("\n");
}
}
}
void free_block_dominance(Function *function) {
int block_count = function->cfg->node_count;
for (int i = 0; i < block_count; i++) free_set(function->dominance[i]);
wfree(function->dominance);
}
static void make_rpo(Function *function, int *rpos, int *pos, int *visited, int block) {
if (visited[block]) return;
visited[block] = 1;
Graph *cfg = function->cfg;
GraphEdge *e = cfg->nodes[block].succ;
while (e) {
make_rpo(function, rpos, pos, visited, e->to->id);
e = e->next_succ;
}
rpos[block] = *pos;
(*pos)--;
}
static int intersect(int *rpos, int *idoms, int i, int j) {
int f1 = i;
int f2 = j;
while (f1 != f2) {
while (rpos[f1] > rpos[f2]) f1 = idoms[f1];
while (rpos[f2] > rpos[f1]) f2 = idoms[f2];
}
return f1;
}
// Algorithm on page 532 of engineering a compiler
static void make_block_immediate_dominators(Function *function) {
Graph *cfg = function->cfg;
int block_count = function->cfg->node_count;
int *rpos = wcalloc(block_count, sizeof(int));
int *visited = wcalloc(block_count, sizeof(int));
int pos = block_count - 1;
make_rpo(function, rpos, &pos, visited, 0);
int *rpos_order = wcalloc(block_count, sizeof(int));
for (int i = 0; i < block_count; i++) rpos_order[rpos[i]] = i;
int *idoms = wcalloc(block_count, sizeof(int));
for (int i = 0; i < block_count; i++) idoms[i] = -1;
idoms[0] = 0;
int changed = 1;
while (changed) {
changed = 0;
for (int i = 0; i < block_count; i++) {
int b = rpos_order[i];
if (b == 0) continue;
int new_idom = -1;
GraphEdge *e = cfg->nodes[b].pred;
while (e) {
int p = e->from->id;
if (idoms[p] != -1) {
if (new_idom == -1)
new_idom = p;
else
new_idom = intersect(rpos, idoms, p, new_idom);
}
e = e->next_pred;
}
if (idoms[b] != new_idom) {
idoms[b] = new_idom;
changed = 1;
}
}
}
idoms[0] = -1;
if (debug_ssa_idom) {
printf("\nIdoms:\n");
for (int i = 0; i < block_count; i++) printf("%d: %d\n", i, idoms[i]);
}
function->idom = idoms;
wfree(rpos_order);
wfree(rpos);
wfree(visited);
}
static void free_block_immediate_dominators(Function *function) {
wfree(function->idom);
}
// Algorithm on page 499 of engineering a compiler
// Walk the dominator tree defined by the idom (immediate dominator)s.
static void make_block_dominance_frontiers(Function *function) {
Graph *cfg = function->cfg;
int block_count = function->cfg->node_count;
Set **df = wcalloc(block_count, sizeof(Set *));
for (int i = 0; i < block_count; i++) df[i] = new_set(block_count);
int *predecessors = wmalloc(block_count * sizeof(int));
int *idom = function->idom;
for (int i = 0; i < block_count; i++) {
int predecessor_count = 0;
GraphEdge *e = cfg->nodes[i].pred;
while (e) {
predecessors[predecessor_count++] = e->from->id;
e = e ->next_pred;
}
if (predecessor_count > 1) {
for (int j = 0; j < predecessor_count; j++) {
int p = predecessors[j];
int runner = p;
while (runner != idom[i] && runner != -1) {
add_to_set(df[runner], i);
runner = idom[runner];
}
}
}
}
function->dominance_frontiers = df;
if (debug_ssa_dominance_frontiers) {
printf("\nDominance frontiers:\n");
for (int i = 0; i < block_count; i++) {
printf("%d: ", i);
print_set(df[i]);
printf("\n");
}
}
wfree(predecessors);
}
static void free_block_dominance_frontiers(Function *function) {
int block_count = function->cfg->node_count;
for (int i = 0; i < block_count; i++) free_set(function->dominance_frontiers[i]);
wfree(function->dominance_frontiers);
}
void analyze_dominance(Function *function) {
sanity_test_ir_linkage(function);
make_vreg_count(function, 0);
make_control_flow_graph(function);
make_block_dominance(function);
make_block_immediate_dominators(function);
make_block_dominance_frontiers(function);
}
void free_dominance(Function *function) {
free_block_dominance_frontiers(function);
free_block_immediate_dominators(function);
free_block_dominance(function);
free_uevar_and_varkill(function);
free_control_flow_graph(function);
}
int make_vreg_count(Function *function, int starting_count) {
int vreg_count = starting_count;
for (Tac *tac = function->ir; tac; tac = tac->next) {
if (tac->src1 && tac->src1->vreg && tac->src1->vreg > vreg_count) vreg_count = tac->src1->vreg;
if (tac->src2 && tac->src2->vreg && tac->src2->vreg > vreg_count) vreg_count = tac->src2->vreg;
if (tac->dst && tac->dst ->vreg && tac->dst ->vreg > vreg_count) vreg_count = tac->dst ->vreg;
}
function->vreg_count = vreg_count;
return vreg_count;
}
void make_uevar_and_varkill(Function *function) {
Block *blocks = function->blocks;
int block_count = function->cfg->node_count;
function->uevar = wcalloc(block_count, sizeof(LongSet *));
function->varkill = wcalloc(block_count, sizeof(LongSet *));
for (int i = 0; i < block_count; i++) {
LongSet *uevar = new_longset();
LongSet *varkill = new_longset();
function->uevar[i] = uevar;
function->varkill[i] = varkill;
Tac *tac = blocks[i].start;
while (1) {
if (tac->src1 && tac->src1->vreg && !longset_in(varkill, tac->src1->vreg)) longset_add(uevar, tac->src1->vreg);
if (tac->src2 && tac->src2->vreg && !longset_in(varkill, tac->src2->vreg)) longset_add(uevar, tac->src2->vreg);
if (tac->dst && tac->dst->vreg) longset_add(varkill, tac->dst->vreg);
if (tac == blocks[i].end) break;
tac = tac->next;
}
}
if (debug_ssa) {
printf("\nuevar & varkills:\n");
for (int i = 0; i < block_count; i++) {
printf("%d: uevar=", i);
print_longset(function->uevar[i]);
printf(" varkill=");
print_longset(function->varkill[i]);
printf("\n");
}
}
}
void free_uevar_and_varkill(Function *function) {
int block_count = function->cfg->node_count;
for (int i = 0; i < block_count; i++) {
free_longset(function->uevar[i]);
free_longset(function->varkill[i]);
}
wfree(function->uevar);
wfree(function->varkill);
}
// Page 447 of Engineering a compiler
void make_liveout(Function *function) {
Graph *cfg = function->cfg;
int block_count = cfg->node_count;
function->liveout = wcalloc(block_count, sizeof(LongSet *));
make_vreg_count(function, 0);
int vreg_count = function->vreg_count;
int **block_uevars = wmalloc(block_count * sizeof(int *));
// Keep track of versions of liveouts. Everytime a block liveout changes, the
// version number is incremented. This is used to avoid recalculating the liveouts
// repeatedly.
int *liveout_versions = wcalloc(block_count, sizeof(int));
int **successor_liveout_versions = wcalloc(block_count, sizeof(int *));
for (int i = 0; i < block_count; i++) {
block_uevars[i] = wcalloc(vreg_count + 1, sizeof(int));
successor_liveout_versions[i] = wcalloc(block_count, sizeof(int));
liveout_versions[i] = 1;
int j = 0;
int *bue = block_uevars[i];
LongSet *uevar = function->uevar[i];
for (LongSetIterator it = longset_iterator(uevar); !longset_iterator_finished(&it); longset_iterator_next(&it), j++)
bue[j] = longset_iterator_element(&it);
}
// Set all liveouts to {0}
for (int i = 0; i < block_count; i++)
function->liveout[i] = new_longset();
if (debug_ssa_liveout) printf("Doing liveout on %d blocks\n", block_count);
LongSet *unions = new_longset();
int inner_count = 0;
int changed = 1;
while (changed) {
changed = 0;
for (int i = 0; i < block_count; i++) {
int *block_successor_liveout_versions = successor_liveout_versions[i];
// Check to see if any of the successor liveouts have changed. If they
// haven't, the entire calculation can be skipped for this block.
int successor = 0;
int match = 1;
GraphEdge *e = cfg->nodes[i].succ;
while (e) {
int successor_block = e->to->id;
if (block_successor_liveout_versions[successor] != liveout_versions[successor_block]) {
match = 0;
break;
}
e = e->next_succ;
successor++;
}
if (match) continue;
successor = 0;
e = cfg->nodes[i].succ;
while (e) {
inner_count++;
// Got a successor edge from i -> successor_block
int successor_block = e->to->id;
LongSet *successor_block_liveout = function->liveout[successor_block];
LongSet *successor_block_varkill = function->varkill[successor_block];
int *successor_block_uevars = block_uevars[successor_block];
block_successor_liveout_versions[successor] = liveout_versions[successor_block];
for (int *i = successor_block_uevars; *i; i++)
longset_add(unions, *i);
longset_foreach(successor_block_liveout, it) {
long vreg = longset_iterator_element(&it);
if (!longset_in(successor_block_varkill, vreg))
longset_add(unions, vreg);
}
e = e->next_succ;
successor++;
}
block_successor_liveout_versions[successor] = 0;
// Ensure unions has been emptied before the next iteration
if (!longset_eq(function->liveout[i], unions)) {
liveout_versions[i]++;
free_longset(function->liveout[i]);
function->liveout[i] = unions;
unions = new_longset();
changed = 1;
}
else
longset_empty(unions);
}
}
free_longset(unions);
if (debug_ssa_liveout) {
printf("\nLiveouts:\n");
for (int i = 0; i < block_count; i++) {
printf("%d: ", i);
print_longset(function->liveout[i]);
printf("\n");
}
}
for (int i = 0; i < block_count; i++) {
wfree(block_uevars[i]);
wfree(successor_liveout_versions[i]);
}
wfree(block_uevars);
wfree(liveout_versions);
wfree(successor_liveout_versions);
}
void free_liveout(Function *function) {
int block_count = function->cfg->node_count;
for (int i = 0; i < block_count; i++)
free_longset(function->liveout[i]);
wfree(function->liveout);
}
void make_globals_and_var_blocks(Function *function) {
Block *blocks = function->blocks;
int block_count = function->cfg->node_count;
make_vreg_count(function, 0);
int vreg_count = function->vreg_count;
Set **var_blocks = wcalloc(vreg_count + 1, sizeof(Set *));
for (int i = 1; i <= vreg_count; i++) var_blocks[i] = new_set(block_count);
Set *globals = new_set(vreg_count);
for (int i = 0; i < block_count; i++) {
Set *varkill = new_set(vreg_count);
Tac *tac = blocks[i].start;
while (1) {
if (tac->src1 && tac->src1->vreg && !in_set(varkill, tac->src1->vreg)) add_to_set(globals, tac->src1->vreg);
if (tac->src2 && tac->src2->vreg && !in_set(varkill, tac->src2->vreg)) add_to_set(globals, tac->src2->vreg);
if (tac->dst && tac->dst->vreg) {
add_to_set(varkill, tac->dst->vreg);
add_to_set(var_blocks[tac->dst->vreg], i);
}
if (tac == blocks[i].end) break;
tac = tac->next;
}
free_set(varkill);
}
function->var_blocks = var_blocks;
if (debug_ssa) {
printf("\nVar write blocks:\n");
for (int i = 1; i <= vreg_count; i++) {
printf("%d: ", i);
print_set(var_blocks[i]);
printf("\n");
}
printf("\nFunction globals: ");
print_set(globals);
printf("\n");
}
function->globals = globals;
}
void free_globals_and_var_blocks(Function *function) {
int vreg_count = function->vreg_count;
for (int i = 1; i <= vreg_count; i++) free_set(function->var_blocks[i]);
wfree(function->var_blocks);
free_set(function->globals);
}
// Algorithm on page 501 of engineering a compiler
void insert_phi_functions(Function *function) {
allocated_phi_values = new_list(1024);
Block *blocks = function->blocks;
Graph *cfg = function->cfg;
int block_count = cfg->node_count;
Set *globals = function->globals;
int vreg_count = function->vreg_count;
Set **phi_functions = wmalloc(block_count * sizeof(Set *));
for (int i = 0; i < block_count; i++) phi_functions[i] = new_set(vreg_count);
for (int global = 0; global <= globals->max_value; global++) {
if (!globals->elements[global]) continue;
Set *work_list = copy_set(function->var_blocks[global]);
while (set_len(work_list)) {
for (int b = 0; b <= work_list->max_value; b++) {
if (!work_list->elements[b]) continue;
delete_from_set(work_list, b);
Set *df = function->dominance_frontiers[b];
for (int d = 0; d <= df->max_value; d++) {
if (!df->elements[d]) continue;
if (!in_set(phi_functions[d], global)) {
add_to_set(phi_functions[d], global);
add_to_set(work_list, d);
}
}
}
}
free_set(work_list);
}
function->phi_functions = phi_functions;
if (debug_ssa_phi_insertion) printf("phi functions to add:\n");
for (int b = 0; b < block_count; b++) {
if (debug_ssa_phi_insertion) {
printf("%d: ", b);
print_set(phi_functions[b]);
printf("\n");
}
int label = blocks[b].start->label;
blocks[b].start->label = 0;
Set *vars = phi_functions[b];
for (int v = vreg_count; v >= 0; v--) {
if (!vars->elements[v]) continue;
Tac *tac = new_instruction(IR_PHI_FUNCTION);
tac->dst = new_value();
tac->dst ->type = new_type(TYPE_LONG);
tac->dst-> vreg = v;
int predecessor_count = 0;
GraphEdge *e = cfg->nodes[b].pred;
while (e) { predecessor_count++; e = e->next_pred; }
Value *phi_values = wcalloc(predecessor_count + 1, sizeof(Value));
append_to_list(allocated_phi_values, phi_values);
for (int i = 0; i < predecessor_count; i++) {
init_value(&phi_values[i]);
phi_values[i].type = new_type(TYPE_LONG);
phi_values[i].vreg = v;
}
tac->phi_values = phi_values;
Tac *prev = blocks[b].start->prev;
tac->prev = prev;
tac->next = blocks[b].start;
prev->next = tac;
blocks[b].start->prev = tac;
blocks[b].start = tac;
}
blocks[b].start->label = label;
}
if (debug_ssa_phi_insertion) {
printf("\nIR with phi functions:\n");
print_ir(function, 0, 0);
}
}
void free_phi_functions(Function *function) {
int block_count = function->cfg->node_count;
for (int i = 0; i < block_count; i++) free_set(function->phi_functions[i]);
wfree(function->phi_functions);
for (int i = 0; i < allocated_phi_values->length; i++) {
wfree(allocated_phi_values->elements[i]);
}
free_list(allocated_phi_values);
}
static int new_subscript(Stack **stack, int *counters, int n) {
int i = counters[n]++;
push_onto_stack(stack[n], i);
return i;
}
static void print_stack_and_counters(Stack **stack, int *counters, int vreg_count) {
printf(" ");
for (int i = 1; i <= vreg_count; i++) printf("%-2d ", i);
printf("\n");
printf("counters ");
for (int i = 1; i <= vreg_count; i++) printf("%-2d ", counters[i]);
printf("\n");
printf("stack ");
for (int i = 1; i <= vreg_count; i++) {
if (stack[i]->pos == MAX_STACK_SIZE)
printf(" ");
else
printf("%-2d ", stack_top(stack[i]));
}
printf("\n");
}
// If nothing is on the stack, push one. This is to deal with undefined
// variables being used.
static int safe_stack_top(Stack **stack, int *counters, int n) {
if (stack[n]->pos == MAX_STACK_SIZE) new_subscript(stack, counters, n);
return stack_top(stack[n]);
}
// Algorithm on page 506 of engineering a compiler
static void rename_vars(Function *function, Stack **stack, int *counters, int block_number, int vreg_count) {
if (debug_ssa_phi_renumbering) {
printf("\n----------------------------------------\nrename_vars\n");
print_stack_and_counters(stack, counters, vreg_count);
printf("\n");
}
Block *blocks = function->blocks;
Graph *cfg = function->cfg;
int block_count = cfg->node_count;
int *idoms = function->idom;
Block *b = &blocks[block_number];
// Rewrite phi function dsts
if (debug_ssa_phi_renumbering) printf("Rewriting phi function dsts\n");
Tac *tac = b->start;
while (tac->operation == IR_PHI_FUNCTION) {
// Rewrite x as new_subscript(x)
if (debug_ssa_phi_renumbering) printf("Renaming %d ", tac->dst->vreg);
tac->dst->ssa_subscript = new_subscript(stack, counters, tac->dst->vreg);
if (debug_ssa_phi_renumbering) printf("to %d in phi function\n", tac->dst->vreg);
if (tac == b->end) break;
tac = tac->next;
}
// Rewrite operations
if (debug_ssa_phi_renumbering) printf("Rewriting operations\n");
tac = b->start;
while (1) {
if (tac->operation != IR_PHI_FUNCTION) {
if (tac->src1 && tac->src1->vreg) {
tac->src1->ssa_subscript = safe_stack_top(stack, counters, tac->src1->vreg);
if (debug_ssa_phi_renumbering) printf("rewrote src1 %d\n", tac->src1->vreg);
}
if (tac->src2 && tac->src2->vreg) {
tac->src2->ssa_subscript = safe_stack_top(stack, counters, tac->src2->vreg);
if (debug_ssa_phi_renumbering) printf("rewrote src2 %d\n", tac->src2->vreg);
}
if (tac->dst && tac->dst->vreg) {
tac->dst->ssa_subscript = new_subscript(stack, counters, tac->dst->vreg);
if (debug_ssa_phi_renumbering) printf("got new name for dst %d\n", tac->dst->vreg);
}
}
if (tac == b->end) break;
tac = tac->next;
}
// Rewrite phi function parameters in successors
if (debug_ssa_phi_renumbering) printf("Rewriting successor function params\n");
GraphEdge *e = cfg->nodes[block_number].succ;
while (e) {
if (debug_ssa_phi_renumbering) printf("Successor %d\n", e->to->id);
Tac *tac = function->blocks[e->to->id].start;
Tac *end = function->blocks[e->to->id].end;
while (1) {
if (tac->operation == IR_PHI_FUNCTION) {
Value *v = tac->phi_values;
while (v->type) {
if (v->ssa_subscript == -1) {
v->ssa_subscript = safe_stack_top(stack, counters, v->vreg);
if (debug_ssa_phi_renumbering) printf(" rewrote arg to %d\n", v->vreg);
break;
}
v++;
}
}
if (tac == end) break;
tac = tac->next;
}
e = e->next_succ;
}
// Recurse down the dominator tree
for (int i = 0; i < block_count; i++) {
if (idoms[i] == block_number) {
if (debug_ssa_phi_renumbering) printf("going into idom successor %d\n", i);
rename_vars(function, stack, counters, i, vreg_count);
}
}
// Pop contents of current block assignments off of the stack
if (debug_ssa_phi_renumbering) printf("Block done. Cleaning up stack\n");
tac = b->start;
while (1) {
if (tac->dst && tac->dst->vreg) {
pop_from_stack(stack[tac->dst->vreg]);
}