-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssa.c
More file actions
1781 lines (1702 loc) · 68.6 KB
/
ssa.c
File metadata and controls
1781 lines (1702 loc) · 68.6 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 "joecc_assert.h"
#include "ssa.h"
#include "opt.h"
#define X(op) case op:
//finds the block that dominates both of these blocks and does
//not dominate any other block that dominates both blocks (i.e. the lowest one)
BBLOCK* intersect(BBLOCK* n1, BBLOCK* n2) {
while(n1 != n2) {
while(n1->domind > n2->domind) n1 = n1->dom;
while(n2->domind > n1->domind) n2 = n2->dom;
}
return n1;
}
//same as above but for postdominators
static BBLOCK* postintersect(BBLOCK* n1, BBLOCK* n2) {
while(n1 != n2) {
while(n1->postdomind > n2->postdomind) n1 = n1->postdom;
while(n2->postdomind > n1->postdomind) n2 = n2->postdom;
}
return n1;
}
//Find if the fixed block dominates the other block
char fixedintersect(const BBLOCK* fb, BBLOCK* gb) {
while(fb->domind < gb->domind) gb = gb->dom;
return fb->domind == gb->domind;
}
//recursively populate dominator tree
static void rpdt(BBLOCK* root, BBLOCK** aclist, int* ind) {
if(!root) return;
if(root->visited) return;
root->visited = 1;
rpdt(root->nextblock, aclist, ind);
rpdt(root->branchblock, aclist, ind);
root->domind = *ind;
aclist[--(*ind)] = root;
}
//recursively populate postdominator tree
static void rupdt(BBLOCK* root, BBLOCK** aclist, int* ind) {
if(root->visited) return;
root->visited = 1;
for(int i = 0; i < root->inedges->length; i++)
rupdt(daget(root->inedges, i), aclist, ind);
root->postdomind = *ind;
aclist[--(*ind)] = root;
}
//calculate dominance frontier, dominator tree
static void dfpdt(BBLOCK* root) {
if(!root) return;
if(root->visited) return;
root->visited = 1;
//recursively handle immediately dominated nodes
if(root->idominates)
for(int i = 0; i < root->idominates->length; i++)
dfpdt(daget(root->idominates, i));
root->df = dactor(8);
//if the children aren't immediately dominated, they're in the dominance frontier
if(root->nextblock && root->nextblock->dom != root) { //only excludes last node
dapush(root->df, root->nextblock);
}
if(root->branchblock && root->branchblock->dom != root) {
dapush(root->df, root->branchblock);
}
if(root->idominates) {
//for each immediately dominated node of the root node, for each node in its dominance frontier,
//add to the root node's dominance frontier if its not already there
for(int j = 0; j < root->idominates->length; j++) {
BBLOCK* ib = daget(root->idominates, j);
for(int k = 0; k < ib->df->length; k++) {
BBLOCK* kb = daget(ib->df, k);
char flag = 1;
for(int i = 0; i < root->df->length; i++) {
//speed this up?
if(daget(root->df, i) == kb) {
flag = 0;
break;
}
}
if(flag && kb->dom != root) {
dapush(root->df, kb);
}
}
}
}
}
//rename all registers in block based on SSA
static void rrename(BBLOCK* block, int* C, DYNARR* S, PROGRAM* prog) {
if(!block || block->visited) return;
DYNINT* assigns = NULL;
block->visited = 1;
if(block->operations && block->operations->length) {
assigns = dictor(32);
for(int opind = 0; opind < block->operations->length; opind++) {
OPERATION* op = block->operations->arr[opind];
DYNINT* bdarr;
switch(op->opcode) {
OPS_3_3ac OPS_3_PTRDEST_3ac
//rename second input operand if it's a variable
if(op->addr1_type & ISVAR) {
bdarr = daget(S, op->addr1.varnum);
if(bdarr->length) //in case of addrsvar
op->addr1.ssaind = dipeek(bdarr);
}
__attribute__((fallthrough));
OPS_2_3ac case ADDR_3:
//rename first input operand if it's a variable
if(op->addr0_type & ISVAR) {
bdarr = daget(S, op->addr0.varnum);
if(bdarr->length)
op->addr0.ssaind = dipeek(bdarr);
}
__attribute__((fallthrough));
case CALL_3:/*must have constant input in alloc_3*/
//rename destination operand if it's a variable. if it is not a dereference, generate a new name
if(op->dest_type & ISVAR) {
bdarr = daget(S, op->dest.varnum);
if(bdarr->length) {
if(op->dest_type & ISDEREF) {
op->dest.ssaind = dipeek(bdarr);
} else {
C[op->dest.varnum] = prog->regcnt++;
op->dest.ssaind = C[op->dest.varnum];
dipush(bdarr, C[op->dest.varnum]);
dipush(assigns, op->dest.varnum);
}
}
}
break;
case PHI: //ternary phi, needs special case
if(op->addr0_type & GARBAGEVAL) {
for(int i = 0; i < block->inedges->length; i++) {
if(op->addr0.joins[i].addr_type & ISVAR) {
bdarr = daget(S, op->addr0.joins[i].addr.varnum);
if(bdarr->length) //in case of addrsvar
op->addr0.joins[i].addr.ssaind = dipeek(bdarr);
}
}
}
if(op->dest_type & ISVAR) {
bdarr = daget(S, op->dest.varnum);
if(bdarr->length) {
if(op->dest_type & ISDEREF) {
op->dest.ssaind = dipeek(bdarr);
} else {
C[op->dest.varnum] = prog->regcnt++;
op->dest.ssaind = C[op->dest.varnum];
dipush(bdarr, C[op->dest.varnum]);
dipush(assigns, op->dest.varnum);
}
}
}
break;
OPS_NODEST_3ac
//rename second input operand if it's a variable
if(op->addr1_type & ISVAR) {
bdarr = daget(S, op->addr1.varnum);
if(bdarr->length) //in case of addrsvar
op->addr1.ssaind = dipeek(bdarr);
}
__attribute__((fallthrough));
OPS_1_3ac
if(op->addr0_type & GARBAGEVAL) break;
__attribute__((fallthrough));
case DEALOC:
//rename first input operand if it's a variable
if(op->addr0_type & ISVAR) {
bdarr = daget(S, op->addr0.varnum);
if(bdarr->length)
op->addr0.ssaind = dipeek(bdarr);
}
break;
OPS_1_ASSIGN_3ac
//rename destination operand, if the variable is never dereferenced, do a bunch of asserts and rename
if(op->dest_type & ISVAR) {
FULLADDR* fad = daget(prog->dynvars, op->dest.varnum);
if(!(fad->addr_type & ADDRSVAR)) {
bdarr = daget(S, op->dest.varnum);
assert(!(op->dest_type & ISDEREF));
assert(!C[op->dest.varnum]);
C[op->dest.varnum] = prog->regcnt++;
op->dest.ssaind = C[op->dest.varnum];
dipush(bdarr, C[op->dest.varnum]);
dipush(assigns, op->dest.varnum);
} else {
op->dest_type |= ADDRSVAR;
}
}
break;
OPS_NOVAR_3ac
break;
case ASM:
assert(0);//unimplemented
}
}
}
//for nextblock, rename the values within the PHI, based on this block's names
if(block->nextblock && block->nextblock->operations && block->nextblock->operations->length) {
int i = -1;
while(daget(block->nextblock->inedges, ++i) != block) ;
for(int opind = 0; opind < block->nextblock->operations->length; opind++) {
OPERATION* op = block->nextblock->operations->arr[opind];
if(op->opcode != PHI)
break;
if(!(op->addr0_type & GARBAGEVAL)) {
DYNINT* bdarr = daget(S, op->dest.varnum);
if(bdarr->length) {
op->addr0.joins[i].addr.ssaind = dipeek(bdarr);
}
op->addr0.joins[i].addr.varnum = op->dest.varnum;
op->addr0.joins[i].addr_type = op->dest_type;
}
}
}
//for branch, rename the values within the PHI, based on this block's names
if(block->branchblock && block->branchblock->operations && block->branchblock->operations->length) {
int i = -1;
while(daget(block->branchblock->inedges, ++i) != block) ;
for(int opind = 0; opind < block->branchblock->operations->length; opind++) {
OPERATION* op = block->branchblock->operations->arr[i];
if(op->opcode != PHI)
break;
assert(!(op->addr0_type & GARBAGEVAL)); //no ternaries can join at a branch block
op->addr0.joins[i].addr.ssaind = dipeek((DYNINT*) daget(S, op->dest.varnum));
op->addr0.joins[i].addr.varnum = op->dest.varnum;
op->addr0.joins[i].addr_type = op->dest_type;
}
}
//Recurse on nextblock and branchblock
rrename(block->nextblock, C, S, prog);
rrename(block->branchblock, C, S, prog);
//free dynint "assigns" and associated resources in S
if(assigns) {
for(int i = 0; i < assigns->length; i++) {
int l = diget(assigns, i);
dipop((DYNINT*) daget(S, l));
}
didtor(assigns);
}
}
void ssa(PROGRAM* prog) {
DYNARR* blocks = prog->allblocks;
//reset flags in each block
for(int i = 0; i < blocks->length; i++) {
BBLOCK* dtn = daget(blocks, i);
dtn->visited = 0;
dtn->domind = -1;
}
//construct utility lists
BBLOCK** blocklist = calloc(sizeof(BBLOCK*), blocks->length + 1);
BBLOCK* first = daget(blocks, 0);
//populate dominator information for the first block in the CFG
first->dom = first; //we set the first block's dominator to be itself for simplifying later algorithms
first->domind = 1;
//Recursively populate dominator tree for other blocks
int ind = blocks->length;
rpdt(first->nextblock, blocklist, &ind);
rpdt(first->branchblock, blocklist, &ind);
//reset flags in blocks, mark unreachable blocks for removal
for(int i = 0; i < blocks->length; i++) {
BBLOCK* dtn = daget(blocks, i);
if(dtn->domind == -1) {
if(dtn->nextblock && dtn->nextblock->operations && dtn->nextblock->operations->length) {
//we don't need to check that it's a join and we also don't need to rearrange PHIs, because there must be a maximum of one ternary phi per block
OPERATION* possphi = dtn->nextblock->operations->arr[0];
if(possphi->opcode == PHI) {
DYNARR* possum = dtn->nextblock->inedges;
//ternary phis must have 2 source operands
assert(possum->length == 2);
assert(possphi->addr0_type & GARBAGEVAL);
//replace it with a mov from the operand of the not bogus block
FULLADDR source = possphi->addr0.joins[daget(possum, 0) == dtn ? 1 : 0];
free(possphi->addr0.joins);
possphi->opcode = MOV_3;
possphi->addr0_type = source.addr_type;
possphi->addr0 = source.addr;
}
}
domark(dtn);
}
}
//remove unreachable blocks
int oldlen = blocks->length;
rmunreach(prog);
blocks = prog->allblocks;
//find immediate dominators for each block, keep looping until nothing has changed since last iter.
char changed = 1;
while(changed) {
changed = 0;
//https://www.cs.rice.edu/~keith/EMBED/dom.pdf
for(int i = ind; i < oldlen; i++) {
BBLOCK* cb = blocklist[i];
if(!cb) continue;
BBLOCK* new_idom = NULL;
for(int i = 0; i < cb->inedges->length; i++) {
BBLOCK* pred = daget(cb->inedges, i);
if(pred->dom) {
//find the last shared predecessor of each parent block and the previously thought idominator
if(new_idom) {
new_idom = intersect(pred, new_idom);
} else {
new_idom = pred;
}
}
}
if(cb->dom != new_idom) {
cb->dom = new_idom;
changed = 1;
}
}
}
//reset flags in each block
for(int i = 0; i < blocks->length; i++) {
BBLOCK* dtn = daget(blocks, i);
dtn->visited = 0;
blocklist[i] = NULL;
dtn->postdomind = -1;
}
ind = blocks->length;
//if we don't have a final block (we're in an infinite loop and the final block was freed)
//Then make a dummy one for simplifying some other code.
if(!prog->finalblock) {
prog->finalblock = mpblk(); //pseudo final block
ind++;
}
//now populate postdominator information
prog->finalblock->postdom = prog->finalblock;
//recursively update from finalblock
rupdt(prog->finalblock, blocklist, &ind);
//if we haven't processed all the blocks we used a pseudo-final block
if(ind > 0) {
for(int i = 0; i < blocks->length; i++) {
BBLOCK* blk = daget(blocks, i);
if(blk->postdomind == -1) {
//check if it has a back-edge if so, add an inedge in finalblock then, recalculate postdom tree
if((blk->nextblock && blk->nextblock->domind < blk->domind) ||
(blk->branchblock && blk->branchblock->domind < blk->domind)) {
dapush(prog->finalblock->inedges, blk);
}
}
blk->visited = 0;
}
//recursively find postdominators for the edges with backedges that we found
for(int i = 0; i < prog->finalblock->inedges->length; i++)
rupdt(daget(prog->finalblock->inedges, i), blocklist, &ind);
prog->finalblock->visited = 0;
} else {
for(int i = 0; i < blocks->length; i++) {
BBLOCK* blk = daget(blocks, i);
blk->visited = 0;
}
}
//now actually populate the postdom field
changed = 1;
//recurse until none of the postdominators are incorrect
while(changed) {
changed = 0;
for(int i = ind; i < blocks->length; i++) {
BBLOCK* cb = blocklist[i];
if(!cb) continue;
BBLOCK* new_pidom = NULL;
if(cb->nextblock) {
if(!cb->branchblock) new_pidom = cb->nextblock;
else {
//same logic as normal dominator
if(cb->nextblock->postdom) {
if(cb->branchblock && cb->branchblock->postdom)
new_pidom = postintersect(cb->branchblock, cb->nextblock);
else
new_pidom = cb->nextblock;
} else {
if(cb->branchblock && cb->branchblock->postdom)
new_pidom = cb->branchblock;
}
}
}
if(new_pidom != NULL && cb->postdom != new_pidom) {
cb->postdom = new_pidom;
changed = 1;
}
}
}
free(blocklist);
//populate in parents
first->visited = 0;
//Construct dominator tree structure by allocating lists of immediately dominated nodes for every nonleaf
for(int i = 1; i < blocks->length; i++) {//start at one so as not to let start block idominate itself
BBLOCK* cb = daget(blocks, i);
if(cb->dom) {
if(!cb->dom->idominates)
cb->dom->idominates = dactor(8);
dapush(cb->dom->idominates, cb);
}
cb->visited = 0;
}
//Construct postdominator tree structure by allocating lists of immediately postdominated nodes for every nonleaf
for(int i = 0; i < prog->allblocks->length; i++) {
BBLOCK* blk = daget(prog->allblocks, i);
BBLOCK* pdblk = blk->postdom;
if(!pdblk) {
pdblk = prog->finalblock;
}
if(!pdblk->pidominates) pdblk->pidominates = dactor(8);
dapush(pdblk->pidominates, blk);
}
//say finalblock doesn't postdominate itself
if(prog->finalblock->pidominates) {
daremove_swap(prog->finalblock->pidominates, prog->finalblock);
}
//dominator tree (immediate dominators) calculated
dfpdt(first); //populate dominance frontiers dfs
for(int i = 0; i < blocks->length; i++) {
BBLOCK* cb = daget(blocks, i);
cb->visited = 0;
cb->work = 0;
}
//no need to handle globals
DYNARR* var_modifying_blocks = dactor(prog->dynvars->length);
for(int i = 0; i < var_modifying_blocks->maxlength; i++)
dapushc(var_modifying_blocks, dactor(16)); //initialize array for blocks that modify var
//variable modification annotation, pass 1
LOOPALLBLOCKS(
switch(op->opcode) {
case ADDR_3:
if((op->addr0_type & (ISVAR | ISDEREF)) == ISVAR) {
FULLADDR* fad = daget(prog->dynvars, op->addr0.varnum);
fad->addr_type |= ADDRSVAR;
}
__attribute__((fallthrough));
OPS_3_3ac OPS_2_3ac case CALL_3:
//ARRMOV, MTP_OFF, COPY_3 must have pointer dest
if((op->dest_type & (ISVAR | ISDEREF | ADDRSVAR)) == ISVAR) {
DYNARR* dda = daget(var_modifying_blocks, op->dest.varnum);
if(!dda->length || dapeek(dda) != blk)
dapush(dda, blk);
}
break;
OPS_1_ASSIGN_3ac
if(!(op->dest_type & ADDRSVAR)) {
DYNARR* dda = daget(var_modifying_blocks, op->dest.varnum);
dapush(dda, blk);
}
default:
break; //no possible correct destination
}
)
//join node insertion, pass 2
DYNARR* W = dactor(blocks->length);
int itercount = 0;
//TODO: figure out what this is actually doing for commenting it right
//for each ssa reg
for(int i = 0; i < prog->dynvars->length; i++) {
++itercount;
FULLADDR* fadr = daget(prog->dynvars, i);
if(fadr->addr_type & ADDRSVAR) continue;
//find which blocks modify it
DYNARR* blockassigns = daget(var_modifying_blocks, i);
for(int j = 0; j < blockassigns->length; j++) {
BBLOCK* block = daget(blockassigns, j);
//work here shows the most recent variable that this block modifies(?)
block->work = itercount;
dapush(W, block);
}
BBLOCK* initblock = daget(blockassigns, 0);
//for each modifying block, with some other things pushed
for(int j = 0; j < W->length; j++) {
BBLOCK* block = daget(W, j);
if(block->df) {
//for each block in the modifying block's dominance frontier
for(int k = 0; k < block->df->length; k++) {
BBLOCK* domblock = daget(block->df, k);
//if the block has not been visited yet, it's not its own dominance frontier value, and it is not the finalblock
if(domblock->visited < itercount && initblock != domblock && fixedintersect(initblock, domblock) && domblock != prog->finalblock) {
ADDRESS jadr;
jadr.joins = malloc(domblock->inedges->length * sizeof(FULLADDR));
//prepend phi to the block
if(!domblock->operations)
domblock->operations = dactor(4);
OPERATION* phi = ct_3ac_op2(PHI, ISCONST, jadr, fadr->addr_type, fadr->addr);
dainsertat(domblock->operations, 0, phi);
domblock->visited = itercount;
//if the block has not been edited yet for this variable, then put it back on the list??????????
if(domblock->work < itercount) {
domblock->work = itercount;
dapushc(W, domblock);
}
}
}
}
}
W->length = 0;
}
dadtor(W);
for(int i = 0; i < blocks->length; i++) {
BBLOCK* cb = daget(blocks, i);
cb->visited = 0;
}
//variable renaming, pass 3
int* C = calloc(sizeof(int), var_modifying_blocks->length);
for(int i = 0; i < var_modifying_blocks->length; i++) {
DYNARR* da = daget(var_modifying_blocks, i);
//convert each DYNARR* into a DYNINT*
da->length = 0;
da->maxlength *= sizeof(void*) / sizeof(int);
}
//now rename registers recursively
rrename(first, C, var_modifying_blocks, prog);
for(int i = 0; i < blocks->length; i++) {
BBLOCK* cb = daget(blocks, i);
cb->visited = 0;
}
dadtorcfr(var_modifying_blocks, (void(*)(void*))didtor);
free(C);
prog->pdone |= SSA;
}
//lengauer tarjan: https://www.cl.cam.ac.uk/~mr10/lengtarj.pdf
static GVNNUM* ctgvnnum(EQONTAINER* eq, int hc) {
GVNNUM* retval = malloc(sizeof(GVNNUM));
retval->hasconst = hc;
retval->equivs = dactor(8);
retval->index = eq->uniq_vals->length;
dapush(eq->uniq_vals, retval);
return retval;
}
static EQONTAINER* cteq(PROGRAM* prog) {
EQONTAINER* retval = malloc(sizeof(EQONTAINER));
retval->uniq_vals = dactor(1024);
ctgvnnum(retval, NOCONST); //have a dummy value in the zero position
retval->intconsthash = lvhtctor();
retval->floatconsthash = fvchtctor(32);
retval->strconsthash = qhtctor();
retval->ophash = opchtctor(8192);
return retval;
}
static void freegvnnum(GVNNUM* eqnode) {
dadtorfr(eqnode->equivs);
free(eqnode);
}
static void freeq(EQONTAINER* eq) {
dadtorcfr(eq->uniq_vals, (void(*)(void*)) freegvnnum);
lvhtdtor(eq->intconsthash);
fvhtdtor(eq->floatconsthash);
qhtdtor(eq->strconsthash);
ophtdtor(eq->ophash);
free(eq);
}
static VALUESTRUCT* valdup(VALUESTRUCT* original) {
VALUESTRUCT* duplicate = malloc(sizeof(VALUESTRUCT));
memcpy(duplicate, original, sizeof(VALUESTRUCT));
return duplicate;
}
//find which equivalence node, if any, this address corresponds to, and create one if it corresponds to nothing extant
static GVNNUM* nodefromaddr(EQONTAINER* eq, ADDRTYPE adt, ADDRESS adr, PROGRAM* prog) {
GVNNUM* cn;
if(adt & ISCONST) {
if(adt & ISSTRCONST) {
cn = qsearch(eq->strconsthash, adr.strconst);
if(!cn) {
cn = ctgvnnum(eq, STRCONST);
cn->strconst = adr.strconst;
qinsert(eq->strconsthash, adr.strconst, cn);
}
} else {
if(adt & ISFLOAT) {
cn = fvsearch(eq->floatconsthash, adr.floatconst_64);
if(!cn) {
cn = ctgvnnum(eq, FLOATCONST);
cn->floatconst = adr.floatconst_64;
fvinsert(eq->floatconsthash, adr.floatconst_64, cn);
}
} else {
cn = lvsearch(eq->intconsthash, adr.intconst_64);
if(!cn) {
cn = ctgvnnum(eq, INTCONST);
cn->intconst = adr.intconst_64;;
lvinsert(eq->intconsthash, adr.intconst_64, cn);
}
}
}
} else {
if(adt & (ISLABEL | ISDEREF)) {
//ignore (for now) TODO: pointer analysis
return NULL;
}
if(adt & ISVAR) {
FULLADDR* adstore = daget(prog->dynvars, adr.varnum);
if(adstore->addr_type & ADDRSVAR) return NULL;
}
VALUESTRUCT valst = {INIT_3, adr.regnum, 0, supersize(adt), 0};
cn = opsearch(eq->ophash, &valst);
if(!cn) {
cn = ctgvnnum(eq, NOCONST);
VALUESTRUCT* exn = ctvalstruct(INIT_3, adr.regnum, 0, supersize(adt), 0);
opinsert(eq->ophash, exn, cn);
dapush(cn->equivs, exn);
}
}
return cn;
}
static GVNNUM* supernodefromaddr(EQONTAINER* eq, char ty, ADDRESS adr, PROGRAM* prog) {
return nodefromaddr(eq, downsize(ty), adr, prog);
}
static GVNNUM* derefwithnodefromaddr(EQONTAINER* eq, ADDRTYPE ty, ADDRESS adr, PROGRAM* prog) {
if(ty & ISDEREF) {
ADDRTYPE newty = 8 | (ty & (ISVAR | ISLABEL | ISPOINTER | GARBAGEVAL | LASTUSE));
return nodefromaddr(eq, newty, adr, prog);
} else {
return nodefromaddr(eq, ty, adr, prog);
}
}
//replace operation via gvn
static void replaceop(BBLOCK* blk, EQONTAINER* eq, PROGRAM* prog, OPERATION* op) {
IIHASHTABLE* leader = blk->leader;
GVNNUM* val;
switch(op->opcode) {
OPS_3_3ac OPS_3_PTRDEST_3ac
val = nodefromaddr(eq, op->dest_type, op->dest, prog);
if(val) {
if(val->hasconst != NOCONST) {
op->opcode = NOP_3;
break;
}
if(op->dest.regnum != (unsigned) iisearch(leader, val->index)) {
op->opcode = NOP_3;
break;
}
} else {
val = derefwithnodefromaddr(eq, op->dest_type, op->dest, prog);
if(val && iiqueryval(leader, val->index)) {
op->dest.regnum = iisearch(leader, val->index);
}
}
__attribute__((fallthrough));
OPS_NODEST_3ac
val = derefwithnodefromaddr(eq, op->addr1_type, op->addr1, prog);
if(val) {
if(val->hasconst != NOCONST && !(op->addr1_type & ISDEREF)) {
op->addr1_type = (op->addr1_type & GENREGMASK) | ISCONST;
op->addr1.intconst_64 = val->intconst; //could be anything
} else if(iiqueryval(leader, val->index)) {
op->addr1.regnum = iisearch(leader, val->index);
}
}
__attribute__((fallthrough));
OPS_1_3ac
if(op->addr0_type & GARBAGEVAL) break;
val = derefwithnodefromaddr(eq, op->addr0_type, op->addr0, prog);
if(val) {
if(val->hasconst != NOCONST && !(op->addr0_type & ISDEREF)) {
op->addr0_type = (op->addr0_type & GENREGMASK) | ISCONST;
op->addr0.intconst_64 = val->intconst; //could be anything
} else {
if(iiqueryval(leader, val->index))
op->addr0.regnum = iisearch(leader, val->index);
}
}
break;
case DEALOC:
//don't really know how to handle this well
break;
OPS_2_3ac_MUT case MOV_3: case ADDR_3:
val = nodefromaddr(eq, op->dest_type, op->dest, prog);
if(val) {
if(val->hasconst != NOCONST) {
op->opcode = NOP_3;
break;
}
if(op->dest.regnum != (unsigned) iisearch(leader, val->index)) {
op->opcode = NOP_3;
break;
}
} else {
val = derefwithnodefromaddr(eq, op->dest_type, op->dest, prog);
if(val && iiqueryval(leader, val->index)) {
op->dest.regnum = iisearch(leader, val->index);
}
}
val = derefwithnodefromaddr(eq, op->addr0_type, op->addr0, prog);
if(val) {
if(val->hasconst != NOCONST && !(op->addr0_type & ISDEREF)) {
op->addr0_type = (op->addr0_type & GENREGMASK) | ISCONST;
op->addr0.intconst_64 = val->intconst; //could be anything
} else if(iiqueryval(leader, val->index)) {
op->addr0.regnum = iisearch(leader, val->index);
}
}
break;
case CALL_3:
val = nodefromaddr(eq, op->dest_type, op->dest, prog);
if(val) {
assert(val->hasconst == NOCONST);
assert(op->dest.regnum == (unsigned) iisearch(leader, val->index));
} else {
val = derefwithnodefromaddr(eq, op->dest_type, op->dest, prog);
if(val && iiqueryval(leader, val->index)) {
op->dest.regnum = iisearch(leader, val->index);
}
}
break;
OPS_1_ASSIGN_3ac
val = nodefromaddr(eq, op->dest_type, op->dest, prog);
if(val) {
assert(val->hasconst == NOCONST);
assert(op->dest.regnum == (unsigned) iisearch(leader, val->index));
} //no need to handle deref for i.e. PARAM_3, INIT_3
break;
case PHI:
for(int k = 0; k < blk->inedges->length; k++) {
FULLADDR* fadrs = op->addr0.joins;
val = derefwithnodefromaddr(eq, fadrs[k].addr_type, fadrs[k].addr, prog);
IIHASHTABLE* predled = ((BBLOCK*) daget(blk->inedges, k))->leader;
if(val) {
if(val->hasconst != NOCONST && !(fadrs[k].addr_type & ISDEREF)) {
fadrs[k].addr_type = (fadrs[k].addr_type & GENREGMASK) | ISCONST;
fadrs[k].addr.intconst_64 = val->intconst; //could be anything
} else if(iiqueryval(predled, val->index)) {
fadrs[k].addr.regnum = iisearch(predled, val->index);
}
}
}
val = nodefromaddr(eq, op->dest_type, op->dest, prog);
if(val) {
assert(val->hasconst == NOCONST);
assert(op->dest.regnum == (long) iisearch(leader, val->index));
} else {
//this should only probably be the case for ternary phis
val = derefwithnodefromaddr(eq, op->dest_type, op->dest, prog);
if(val && iiqueryval(leader, val->index)) {
op->dest.regnum = iisearch(leader, val->index);
}
}
break;
OPS_NOVAR_3ac
break;
case ASM:
assert(0); //unimplemented
}
}
static void replacegvn(EQONTAINER* eq, PROGRAM* prog) {
LOOPALLBLOCKS(
replaceop(blk, eq, prog, op);
)
}
static void debuggo(EQONTAINER* eq, PROGRAM* prog) {
for(int blockind = 0; blockind < prog->allblocks->length; blockind++) {
BBLOCK* blk = daget(prog->allblocks, blockind);
GVNNUM* gn;
printf("BBLOCK NUMBER %d\n", blk->domind);
LOOPOPS(
printf("%s ", opcode_3ac_names[op->opcode]);
OPARGCASES(
printaddr(op->addr0, op->addr0_type, 1, stdout, prog);
gn = derefwithnodefromaddr(eq, op->addr0_type, op->addr0, prog);
if(op->addr0_type & ISDEREF) {
printf("([%d]),", gn ? gn->index : -1);
} else {
printf("[%d],", gn ? gn->index : -1);
}
,
printaddr(op->addr1, op->addr1_type, 1, stdout, prog);
gn = derefwithnodefromaddr(eq, op->addr1_type, op->addr1, prog);
if(op->addr0_type & ISDEREF) {
printf("([%d]),", gn ? gn->index : -1);
} else {
printf("[%d],", gn ? gn->index : -1);
}
,
printaddr(op->dest, op->dest_type, 1, stdout, prog);
gn = derefwithnodefromaddr(eq, op->dest_type, op->dest, prog);
if(op->addr0_type & ISDEREF) {
printf("([%d]),", gn ? gn->index : -1);
} else {
printf("[%d],", gn ? gn->index : -1);
}
,
printaddr(phijoinaddr->addr, phijoinaddr->addr_type, 1, stdout, prog);
gn = derefwithnodefromaddr(eq, phijoinaddr->addr_type, phijoinaddr->addr, prog);
if(op->addr0_type & ISDEREF) {
printf("([%d]),", gn ? gn->index : -1);
} else {
printf("[%d],", gn ? gn->index : -1);
}
)
putchar('\n');
)
}
}
//number values
static void gensall(PROGRAM* prog, EQONTAINER* eq, BBLOCK* blk) {
blk->leader = iiclone(blk->dom->leader);
blk->antileader_in = lvhtctor();
blk->antileader_in_list = dictor(64);
if(blk->operations && blk->operations->length) {
blk->tmp_gen = dactor(32);
blk->exp_gen = lvhtctor();
blk->exp_gen_list = dictor(64);
blk->antileader_out = lvhtctor();
blk->antileader_out_list = dictor(64);
VALUESTRUCT valst = {INIT_3, 0, 0, 0, 0};
for(int opind = 0; opind < blk->operations->length; opind++) {
OPERATION* op = blk->operations->arr[opind];
GVNNUM* val1;
GVNNUM* val2;
GVNNUM* destval = NULL;
GVNNUM* otherval = NULL;
VALUESTRUCT finalval;
OPHASHTABLE* ophash = eq->ophash;
switch(op->opcode) {
OPS_NOVAR_3ac
break; //nothing for nop, lbl, jmp, branching ops, or arg/ret
OPS_3_3ac_NOCOM
val1 = nodefromaddr(eq, op->addr0_type, op->addr0, prog);
val2 = nodefromaddr(eq, op->addr1_type, op->addr1, prog);
if(!(op->dest_type & (ISLABEL | ISDEREF))) {
if(op->dest_type & ISVAR) {
FULLADDR* adstore = daget(prog->dynvars, op->dest.varnum);
if(adstore->addr_type & ADDRSVAR) break;
}
if(val1 && val2) {
VALUESTRUCT combind = {op->opcode, val1->index, val2->index, supersize(op->addr0_type), supersize(op->addr1_type)};
destval = opsearch(ophash, &combind);
if(!destval) {
destval = ctgvnnum(eq, NOCONST);
dapush(destval->equivs, valdup(&combind));
opinsert(ophash, &combind, destval);
}
} else {
destval = ctgvnnum(eq, NOCONST);
}
dapush(destval->equivs, ctvalstruct(INIT_3, op->dest.regnum, 0, supersize(op->dest_type), 0));
finalval.o = INIT_3;
finalval.p1 = op->dest.regnum;
finalval.p2 = 0;
finalval.size1 = supersize(op->dest_type);
finalval.size2 = 0;
opinsert(ophash, &finalval, destval);
} else if(val1 && val2) {
VALUESTRUCT combind = {op->opcode, val1->index, val2->index, supersize(op->addr0_type), supersize(op->addr1_type)};
otherval = opsearch(ophash, &combind);
if(!otherval) {
otherval = ctgvnnum(eq, NOCONST);
dapush(otherval->equivs, valdup(&combind));
opinsert(ophash, &combind, otherval);
}
}
break;
OPS_3_3ac_COM
val1 = nodefromaddr(eq, op->addr0_type, op->addr0, prog);
val2 = nodefromaddr(eq, op->addr1_type, op->addr1, prog);
if(!(op->dest_type & (ISLABEL | ISDEREF))) {
if(op->dest_type & ISVAR) {
FULLADDR* adstore = daget(prog->dynvars, op->dest.varnum);
if(adstore->addr_type & ADDRSVAR) break;
}
if(val1 && val2) {
VALUESTRUCT combind = {op->opcode, val1->index, val2->index, supersize(op->addr0_type), supersize(op->addr1_type)};
destval = opsearch(ophash, &combind);
if(!destval) {
destval = ctgvnnum(eq, NOCONST);
dapush(destval->equivs, valdup(&combind));
opinsert(ophash, &combind, destval);
VALUESTRUCT combind2 = {op->opcode, val2->index, val1->index, supersize(op->addr1_type), supersize(op->addr0_type)};
opinsert(ophash, &combind2, destval);
}
} else {
destval = ctgvnnum(eq, NOCONST);
}
dapush(destval->equivs, ctvalstruct(INIT_3, op->dest.regnum, 0, supersize(op->dest_type), 0));
finalval.o = INIT_3;
finalval.p1 = op->dest.regnum;
finalval.p2 = 0;
finalval.size1 = supersize(op->dest_type);
finalval.size2 = 0;
opinsert(ophash, &finalval, destval);
} else if(val1 && val2) {
VALUESTRUCT combind = {op->opcode, val1->index, val2->index, supersize(op->addr0_type), supersize(op->addr1_type)};
otherval = opsearch(ophash, &combind);
if(!otherval) {
otherval = ctgvnnum(eq, NOCONST);
dapush(otherval->equivs, valdup(&combind));
opinsert(ophash, &combind, otherval);
VALUESTRUCT combind2 = {op->opcode, val2->index, val1->index, supersize(op->addr1_type), supersize(op->addr0_type)};
opinsert(ophash, &combind2, otherval);
}
}
break;
OPS_2_3ac_MUT //Do we really want to handle ALOC_3 this way?
val1 = nodefromaddr(eq, op->addr0_type, op->addr0, prog);
if(!(op->dest_type & (ISLABEL | ISDEREF))) {
if(op->dest_type & ISVAR) {
FULLADDR* adstore = daget(prog->dynvars, op->dest.varnum);
if(adstore->addr_type & ADDRSVAR) break;
}
if(val1) {
VALUESTRUCT combind = {op->opcode, val1->index, 0, supersize(op->addr0_type), 0};
destval = opsearch(ophash, &combind);
if(!destval) {
destval = ctgvnnum(eq, NOCONST);
dapush(destval->equivs, valdup(&combind));
opinsert(ophash, &combind, destval);
}
} else {
destval = ctgvnnum(eq, NOCONST);
}
dapush(destval->equivs, ctvalstruct(INIT_3, op->dest.regnum, 0, supersize(op->dest_type), 0));
finalval.o = INIT_3;
finalval.p1 = op->dest.regnum;
finalval.p2 = 0;
finalval.size1 = supersize(op->dest_type);
finalval.size2 = 0;
opinsert(ophash, &finalval, destval);
} else if(val1) {
VALUESTRUCT combind = {op->opcode, val1->index, 0, supersize(op->addr0_type), 0};
otherval = opsearch(ophash, &combind);
if(!otherval) {
otherval = ctgvnnum(eq, NOCONST);
dapush(otherval->equivs, valdup(&combind));
opinsert(ophash, &combind, otherval);
}
}
break;
case MOV_3:
val1 = nodefromaddr(eq, op->addr0_type, op->addr0, prog);
if(!(op->dest_type & (ISLABEL | ISDEREF))) {
if(op->dest_type & ISVAR) {
FULLADDR* adstore = daget(prog->dynvars, op->dest.varnum);
if(adstore->addr_type & ADDRSVAR) break;
}
//hmm this should be fine to keep this way as size can't increase at mov?
if(val1) {
destval = val1;
} else {
destval = ctgvnnum(eq, NOCONST);
}
dapush(destval->equivs, ctvalstruct(INIT_3, op->dest.regnum, 0, supersize(op->dest_type), 0));
finalval.o = INIT_3;
finalval.p1 = op->dest.regnum;
finalval.p2 = 0;
finalval.size1 = supersize(op->dest_type);
finalval.size2 = 0;
opinsert(ophash, &finalval, destval);
}
break;
case ADDR_3:
//address should stay constant, so the value can be stored, as can the value of labels!
val1 = nodefromaddr(eq, op->dest_type, op->dest, prog);
if(!(op->dest_type & (ISDEREF | ISLABEL))) {
//addrsvar is permissible
if(val1) {
VALUESTRUCT combind = {op->opcode, val1->index, 0, supersize(op->addr0_type), 0};