-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.c
1880 lines (1560 loc) · 51 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
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 "ir.h"
#include "../alloc.h"
#include "../log.h"
#include "../target.h"
#include "../vector.h"
enum ir_var_primitive_ty var_ty_pointer_primitive_ty(struct ir_unit *iru) {
switch (iru->target->lp_sz) {
case TARGET_LP_SZ_LP32:
return IR_VAR_PRIMITIVE_TY_I32;
case TARGET_LP_SZ_LP64:
return IR_VAR_PRIMITIVE_TY_I64;
}
}
bool binary_op_is_comparison(enum ir_op_binary_op_ty ty) {
switch (ty) {
case IR_OP_BINARY_OP_TY_EQ:
case IR_OP_BINARY_OP_TY_NEQ:
case IR_OP_BINARY_OP_TY_UGT:
case IR_OP_BINARY_OP_TY_SGT:
case IR_OP_BINARY_OP_TY_UGTEQ:
case IR_OP_BINARY_OP_TY_SGTEQ:
case IR_OP_BINARY_OP_TY_ULT:
case IR_OP_BINARY_OP_TY_SLT:
case IR_OP_BINARY_OP_TY_ULTEQ:
case IR_OP_BINARY_OP_TY_SLTEQ:
case IR_OP_BINARY_OP_TY_FEQ:
case IR_OP_BINARY_OP_TY_FNEQ:
case IR_OP_BINARY_OP_TY_FGT:
case IR_OP_BINARY_OP_TY_FLT:
case IR_OP_BINARY_OP_TY_FGTEQ:
case IR_OP_BINARY_OP_TY_FLTEQ:
return true;
case IR_OP_BINARY_OP_TY_AND:
case IR_OP_BINARY_OP_TY_OR:
case IR_OP_BINARY_OP_TY_XOR:
case IR_OP_BINARY_OP_TY_LSHIFT:
case IR_OP_BINARY_OP_TY_SRSHIFT:
case IR_OP_BINARY_OP_TY_URSHIFT:
case IR_OP_BINARY_OP_TY_ADD:
case IR_OP_BINARY_OP_TY_SUB:
case IR_OP_BINARY_OP_TY_MUL:
case IR_OP_BINARY_OP_TY_SDIV:
case IR_OP_BINARY_OP_TY_UDIV:
case IR_OP_BINARY_OP_TY_SQUOT:
case IR_OP_BINARY_OP_TY_UQUOT:
case IR_OP_BINARY_OP_TY_FADD:
case IR_OP_BINARY_OP_TY_FSUB:
case IR_OP_BINARY_OP_TY_FMUL:
case IR_OP_BINARY_OP_TY_FDIV:
case IR_OP_BINARY_OP_TY_FMAX:
case IR_OP_BINARY_OP_TY_FMIN:
return false;
}
}
bool op_has_side_effects(const struct ir_op *op) {
if (op->flags & IR_OP_FLAG_SIDE_EFFECTS) {
return true;
}
switch (op->ty) {
case IR_OP_TY_UNKNOWN:
BUG("unknown op ty");
case IR_OP_TY_PHI:
case IR_OP_TY_UNDF:
case IR_OP_TY_CNST:
case IR_OP_TY_CAST_OP:
case IR_OP_TY_LOAD:
case IR_OP_TY_LOAD_BITFIELD:
case IR_OP_TY_BITFIELD_EXTRACT:
case IR_OP_TY_BITFIELD_INSERT:
case IR_OP_TY_ADDR:
case IR_OP_TY_ADDR_OFFSET:
case IR_OP_TY_BINARY_OP:
case IR_OP_TY_UNARY_OP:
return false;
case IR_OP_TY_MOV:
case IR_OP_TY_CALL:
case IR_OP_TY_STORE:
case IR_OP_TY_MEM_SET:
case IR_OP_TY_STORE_BITFIELD:
case IR_OP_TY_BR_COND:
case IR_OP_TY_BR_SWITCH:
case IR_OP_TY_RET:
case IR_OP_TY_BR:
return true;
case IR_OP_TY_CUSTOM:
BUG("not well defined for IR_OP_TY_CUSTOM");
}
}
bool op_produces_value(const struct ir_op *op) {
switch (op->ty) {
case IR_OP_TY_UNKNOWN:
BUG("unknown op ty");
case IR_OP_TY_PHI:
case IR_OP_TY_UNDF:
case IR_OP_TY_MOV:
case IR_OP_TY_CNST:
case IR_OP_TY_BINARY_OP:
case IR_OP_TY_UNARY_OP:
case IR_OP_TY_CAST_OP:
case IR_OP_TY_LOAD:
case IR_OP_TY_LOAD_BITFIELD:
case IR_OP_TY_BITFIELD_INSERT:
case IR_OP_TY_BITFIELD_EXTRACT:
case IR_OP_TY_ADDR:
case IR_OP_TY_ADDR_OFFSET:
return true;
case IR_OP_TY_CALL:
return op->call.func_ty.func.ret_ty->ty != IR_VAR_TY_TY_NONE;
case IR_OP_TY_STORE:
case IR_OP_TY_STORE_BITFIELD:
case IR_OP_TY_BR_COND:
case IR_OP_TY_BR_SWITCH:
case IR_OP_TY_RET:
case IR_OP_TY_BR:
case IR_OP_TY_MEM_SET:
return false;
case IR_OP_TY_CUSTOM:
BUG("`op_produces_value` not well defined for IR_OP_TY_CUSTOM");
}
}
bool op_is_branch(enum ir_op_ty ty) {
switch (ty) {
case IR_OP_TY_UNKNOWN:
BUG("unknown op ty");
case IR_OP_TY_BR_COND:
case IR_OP_TY_BR_SWITCH:
case IR_OP_TY_RET:
case IR_OP_TY_BR:
return true;
// calls are NOT branches, because while they do leave, they guarantee return
case IR_OP_TY_CALL:
case IR_OP_TY_UNDF:
case IR_OP_TY_PHI:
case IR_OP_TY_MOV:
case IR_OP_TY_CNST:
case IR_OP_TY_BINARY_OP:
case IR_OP_TY_UNARY_OP:
case IR_OP_TY_CAST_OP:
case IR_OP_TY_STORE:
case IR_OP_TY_LOAD:
case IR_OP_TY_MEM_SET:
case IR_OP_TY_STORE_BITFIELD:
case IR_OP_TY_LOAD_BITFIELD:
case IR_OP_TY_BITFIELD_EXTRACT:
case IR_OP_TY_BITFIELD_INSERT:
case IR_OP_TY_ADDR:
case IR_OP_TY_ADDR_OFFSET:
return false;
case IR_OP_TY_CUSTOM:
BUG("`op_produces_value` not well defined for IR_OP_TY_CUSTOM");
}
}
bool var_ty_eq(struct ir_func *irb, const struct ir_var_ty *l,
const struct ir_var_ty *r) {
if (l == r) {
return true;
}
if (l->ty != r->ty) {
return false;
}
switch (l->ty) {
case IR_VAR_TY_TY_NONE:
return r->ty == IR_VAR_TY_TY_NONE;
case IR_VAR_TY_TY_PRIMITIVE:
return l->primitive == r->primitive;
case IR_VAR_TY_TY_VARIADIC:
return r->ty == IR_VAR_TY_TY_VARIADIC;
case IR_VAR_TY_TY_POINTER:
return true;
case IR_VAR_TY_TY_ARRAY:
return l->array.num_elements == r->array.num_elements &&
var_ty_eq(irb, l->array.underlying, r->array.underlying);
case IR_VAR_TY_TY_FUNC:
if (!var_ty_eq(irb, l->func.ret_ty, r->func.ret_ty)) {
return false;
}
if (l->func.num_params != r->func.num_params) {
return false;
}
for (size_t i = 0; i < l->func.num_params; i++) {
if (!var_ty_eq(irb, &l->func.params[i], &r->func.params[i])) {
return false;
}
}
return true;
case IR_VAR_TY_TY_STRUCT: {
if (l->struct_ty.num_fields != r->struct_ty.num_fields) {
return false;
}
struct ir_var_ty_info l_info = var_ty_info(irb->unit, l);
struct ir_var_ty_info r_info = var_ty_info(irb->unit, r);
// currently we do not have custom alignment/size but it is possible
if (l_info.size != r_info.size || l_info.alignment != r_info.alignment) {
return false;
}
for (size_t i = 0; i < l->struct_ty.num_fields; i++) {
if (!var_ty_eq(irb, &l->struct_ty.fields[i], &r->struct_ty.fields[i])) {
return false;
}
}
return true;
}
case IR_VAR_TY_TY_UNION: {
if (l->union_ty.num_fields != r->union_ty.num_fields) {
return false;
}
struct ir_var_ty_info l_info = var_ty_info(irb->unit, l);
struct ir_var_ty_info r_info = var_ty_info(irb->unit, r);
// currently we do not have custom alignment/size but it is possible
if (l_info.size != r_info.size || l_info.alignment != r_info.alignment) {
return false;
}
for (size_t i = 0; i < l->union_ty.num_fields; i++) {
if (!var_ty_eq(irb, &l->union_ty.fields[i], &r->union_ty.fields[i])) {
return false;
}
}
return true;
}
}
unreachable();
}
static void walk_br_cond(struct ir_op_br_cond *br_cond, walk_op_callback *cb,
void *cb_metadata) {
cb(&br_cond->cond, cb_metadata);
}
static void walk_cnst(UNUSED_ARG(struct ir_op_cnst *cnst),
UNUSED_ARG(walk_op_callback *cb),
UNUSED_ARG(void *cb_metadata)) {
// nada
}
static void walk_binary_op(struct ir_op_binary_op *binary_op,
walk_op_callback *cb, void *cb_metadata) {
walk_op(binary_op->lhs, cb, cb_metadata);
walk_op(binary_op->rhs, cb, cb_metadata);
}
static void walk_unary_op(struct ir_op_unary_op *unary_op, walk_op_callback *cb,
void *cb_metadata) {
walk_op(unary_op->value, cb, cb_metadata);
}
static void walk_cast_op(struct ir_op_cast_op *cast_op, walk_op_callback *cb,
void *cb_metadata) {
walk_op(cast_op->value, cb, cb_metadata);
}
static void walk_ret(struct ir_op_ret *ret, walk_op_callback *cb,
void *cb_metadata) {
if (ret->value) {
walk_op(ret->value, cb, cb_metadata);
}
}
void walk_op_uses(struct ir_op *op, walk_op_callback *cb, void *cb_metadata) {
switch (op->ty) {
case IR_OP_TY_UNKNOWN:
BUG("unknown op!");
case IR_OP_TY_CUSTOM:
case IR_OP_TY_UNDF:
break;
case IR_OP_TY_CALL: {
cb(&op->call.target, cb_metadata);
for (size_t i = 0; i < op->call.num_args; i++) {
cb(&op->call.args[i], cb_metadata);
}
break;
}
case IR_OP_TY_PHI: {
for (size_t i = 0; i < op->phi.num_values; i++) {
cb(&op->phi.values[i].value, cb_metadata);
}
break;
}
case IR_OP_TY_CNST:
break;
case IR_OP_TY_BINARY_OP:
cb(&op->binary_op.lhs, cb_metadata);
cb(&op->binary_op.rhs, cb_metadata);
break;
case IR_OP_TY_UNARY_OP:
cb(&op->unary_op.value, cb_metadata);
break;
case IR_OP_TY_CAST_OP:
cb(&op->cast_op.value, cb_metadata);
break;
case IR_OP_TY_STORE:
cb(&op->store.value, cb_metadata);
switch (op->store.ty) {
case IR_OP_STORE_TY_LCL:
case IR_OP_STORE_TY_GLB:
break;
case IR_OP_STORE_TY_ADDR:
cb(&op->store.addr, cb_metadata);
break;
}
break;
case IR_OP_TY_STORE_BITFIELD:
cb(&op->store_bitfield.value, cb_metadata);
switch (op->store_bitfield.ty) {
case IR_OP_STORE_TY_LCL:
case IR_OP_STORE_TY_GLB:
break;
case IR_OP_STORE_TY_ADDR:
cb(&op->store_bitfield.addr, cb_metadata);
break;
}
break;
case IR_OP_TY_LOAD:
switch (op->load.ty) {
case IR_OP_LOAD_TY_LCL:
case IR_OP_LOAD_TY_GLB:
break;
case IR_OP_LOAD_TY_ADDR:
cb(&op->load.addr, cb_metadata);
break;
}
break;
case IR_OP_TY_LOAD_BITFIELD:
switch (op->load_bitfield.ty) {
case IR_OP_LOAD_TY_LCL:
case IR_OP_LOAD_TY_GLB:
break;
case IR_OP_LOAD_TY_ADDR:
cb(&op->load_bitfield.addr, cb_metadata);
break;
}
break;
case IR_OP_TY_ADDR:
break;
case IR_OP_TY_ADDR_OFFSET:
cb(&op->addr_offset.base, cb_metadata);
cb(&op->addr_offset.offset, cb_metadata);
break;
case IR_OP_TY_BR:
break;
case IR_OP_TY_BR_SWITCH:
cb(&op->br_switch.value, cb_metadata);
break;
case IR_OP_TY_BR_COND:
cb(&op->br_cond.cond, cb_metadata);
break;
case IR_OP_TY_MOV:
if (op->mov.value) {
cb(&op->mov.value, cb_metadata);
}
break;
case IR_OP_TY_RET:
if (op->ret.value) {
cb(&op->ret.value, cb_metadata);
}
break;
case IR_OP_TY_BITFIELD_EXTRACT:
cb(&op->bitfield_extract.value, cb_metadata);
break;
case IR_OP_TY_BITFIELD_INSERT:
cb(&op->bitfield_insert.target, cb_metadata);
cb(&op->bitfield_insert.value, cb_metadata);
break;
case IR_OP_TY_MEM_SET:
cb(&op->mem_set.addr, cb_metadata);
break;
}
}
void walk_op(struct ir_op *op, walk_op_callback *cb, void *cb_metadata) {
cb(&op, cb_metadata);
switch (op->ty) {
case IR_OP_TY_UNKNOWN:
BUG("unknown op!");
case IR_OP_TY_CUSTOM:
TODO("walk custom");
case IR_OP_TY_CALL:
TODO("walk call");
case IR_OP_TY_PHI:
TODO("walk phi");
case IR_OP_TY_MOV:
TODO("walk mov");
case IR_OP_TY_LOAD:
TODO("walk load");
case IR_OP_TY_STORE:
TODO("walk store");
case IR_OP_TY_LOAD_BITFIELD:
TODO("walk load bitfield");
case IR_OP_TY_STORE_BITFIELD:
TODO("walk store bitfield");
case IR_OP_TY_ADDR:
TODO("walk addr");
case IR_OP_TY_BR_SWITCH:
TODO("walk br.switch");
case IR_OP_TY_UNDF:
break;
case IR_OP_TY_CNST:
walk_cnst(&op->cnst, cb, cb_metadata);
break;
case IR_OP_TY_BINARY_OP:
walk_binary_op(&op->binary_op, cb, cb_metadata);
break;
case IR_OP_TY_UNARY_OP:
walk_unary_op(&op->unary_op, cb, cb_metadata);
break;
case IR_OP_TY_CAST_OP:
walk_cast_op(&op->cast_op, cb, cb_metadata);
break;
case IR_OP_TY_BR:
// nada
break;
case IR_OP_TY_BR_COND:
walk_br_cond(&op->br_cond, cb, cb_metadata);
break;
case IR_OP_TY_RET:
walk_ret(&op->ret, cb, cb_metadata);
break;
default:
TODO("");
}
}
void walk_stmt(struct ir_stmt *stmt, walk_op_callback *cb, void *cb_metadata) {
struct ir_op *op = stmt->last;
walk_op(op, cb, cb_metadata);
}
enum ir_op_sign binary_op_sign(enum ir_op_binary_op_ty ty) {
switch (ty) {
case IR_OP_BINARY_OP_TY_OR:
case IR_OP_BINARY_OP_TY_XOR:
case IR_OP_BINARY_OP_TY_AND:
case IR_OP_BINARY_OP_TY_LSHIFT:
case IR_OP_BINARY_OP_TY_ADD:
case IR_OP_BINARY_OP_TY_SUB:
case IR_OP_BINARY_OP_TY_MUL:
case IR_OP_BINARY_OP_TY_EQ:
case IR_OP_BINARY_OP_TY_NEQ:
case IR_OP_BINARY_OP_TY_FADD:
case IR_OP_BINARY_OP_TY_FSUB:
case IR_OP_BINARY_OP_TY_FMUL:
case IR_OP_BINARY_OP_TY_FDIV:
case IR_OP_BINARY_OP_TY_FEQ:
case IR_OP_BINARY_OP_TY_FNEQ:
case IR_OP_BINARY_OP_TY_FGT:
case IR_OP_BINARY_OP_TY_FGTEQ:
case IR_OP_BINARY_OP_TY_FLT:
case IR_OP_BINARY_OP_TY_FLTEQ:
case IR_OP_BINARY_OP_TY_FMAX:
case IR_OP_BINARY_OP_TY_FMIN:
return IR_OP_SIGN_NA;
case IR_OP_BINARY_OP_TY_SRSHIFT:
case IR_OP_BINARY_OP_TY_SDIV:
case IR_OP_BINARY_OP_TY_SQUOT:
case IR_OP_BINARY_OP_TY_SLT:
case IR_OP_BINARY_OP_TY_SLTEQ:
case IR_OP_BINARY_OP_TY_SGT:
case IR_OP_BINARY_OP_TY_SGTEQ:
return IR_OP_SIGN_SIGNED;
case IR_OP_BINARY_OP_TY_URSHIFT:
case IR_OP_BINARY_OP_TY_UDIV:
case IR_OP_BINARY_OP_TY_UQUOT:
case IR_OP_BINARY_OP_TY_ULT:
case IR_OP_BINARY_OP_TY_ULTEQ:
case IR_OP_BINARY_OP_TY_UGT:
case IR_OP_BINARY_OP_TY_UGTEQ:
return IR_OP_SIGN_UNSIGNED;
}
}
const struct ir_var_ty IR_VAR_TY_NONE = {.ty = IR_VAR_TY_TY_NONE};
const struct ir_var_ty IR_VAR_TY_POINTER = {.ty = IR_VAR_TY_TY_POINTER};
const struct ir_var_ty IR_VAR_TY_I8 = {.ty = IR_VAR_TY_TY_PRIMITIVE,
.primitive = IR_VAR_PRIMITIVE_TY_I8};
const struct ir_var_ty IR_VAR_TY_I16 = {.ty = IR_VAR_TY_TY_PRIMITIVE,
.primitive = IR_VAR_PRIMITIVE_TY_I16};
const struct ir_var_ty IR_VAR_TY_I32 = {.ty = IR_VAR_TY_TY_PRIMITIVE,
.primitive = IR_VAR_PRIMITIVE_TY_I32};
const struct ir_var_ty IR_VAR_TY_I64 = {.ty = IR_VAR_TY_TY_PRIMITIVE,
.primitive = IR_VAR_PRIMITIVE_TY_I64};
const struct ir_var_ty IR_VAR_TY_F32 = {.ty = IR_VAR_TY_TY_PRIMITIVE,
.primitive = IR_VAR_PRIMITIVE_TY_F32};
const struct ir_var_ty IR_VAR_TY_F64 = {.ty = IR_VAR_TY_TY_PRIMITIVE,
.primitive = IR_VAR_PRIMITIVE_TY_F64};
const struct ir_var_ty IR_VAR_TY_VARIADIC = {.ty = IR_VAR_TY_TY_VARIADIC};
bool is_func_variadic(const struct ir_var_func_ty *ty) {
return ty->flags & IR_VAR_FUNC_TY_FLAG_VARIADIC;
}
void initialise_ir_op(struct ir_op *op, size_t id, enum ir_op_ty ty,
struct ir_var_ty var_ty, struct ir_reg reg,
struct ir_lcl *lcl) {
op->id = id;
op->ty = ty;
op->flags = IR_OP_FLAG_NONE;
op->var_ty = var_ty;
op->pred = NULL;
op->succ = NULL;
op->stmt = NULL;
op->reg = reg;
op->lcl = lcl;
op->metadata = NULL;
op->comment = NULL;
}
#define DETACHED_BASICBLOCK (SIZE_MAX)
void detach_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *basicblock) {
invariant_assert(irb->basicblock_count,
"`detach_ir_basicblock` would underflow basicblock count "
"for `ir_builder`");
invariant_assert(!basicblock->num_preds, "trying to detach BB with preds");
size_t stmt_count = 0;
size_t op_count = 0;
struct ir_stmt *stmt = basicblock->first;
while (stmt) {
stmt_count++;
struct ir_op *op = stmt->first;
while (op) {
op_count++;
op = op->succ;
}
stmt = stmt->succ;
}
irb->basicblock_count--;
irb->stmt_count -= stmt_count;
irb->op_count -= op_count;
switch (basicblock->ty) {
case IR_BASICBLOCK_TY_RET:
break;
case IR_BASICBLOCK_TY_SPLIT:
case IR_BASICBLOCK_TY_SWITCH:
BUG("can't detach SPLIT/SWITCH bb");
case IR_BASICBLOCK_TY_MERGE: {
struct ir_basicblock *target = basicblock->merge.target;
for (size_t i = 0; i < target->num_preds; i++) {
if (target->preds[i] == basicblock) {
if (i + 1 < target->num_preds) {
memmove(&target->preds[i], &target->preds[i + 1],
(target->num_preds - i - 1) * sizeof(struct ir_basicblock *));
}
target->num_preds--;
break;
}
}
break;
}
}
basicblock->id = DETACHED_BASICBLOCK;
// fix links on either side of basicblock
if (basicblock->pred) {
basicblock->pred->succ = basicblock->succ;
} else {
irb->first = basicblock->succ;
}
if (basicblock->succ) {
basicblock->succ->pred = basicblock->pred;
} else {
irb->last = basicblock->pred;
}
basicblock->irb = NULL;
}
void detach_ir_stmt(struct ir_func *irb, struct ir_stmt *stmt) {
invariant_assert(
irb->stmt_count,
"`detach_ir_stmt` would underflow stmt count for `ir_builder`");
irb->stmt_count--;
// fix links on either side of stmt
if (stmt->pred) {
stmt->pred->succ = stmt->succ;
} else {
stmt->basicblock->first = stmt->succ;
}
if (stmt->succ) {
stmt->succ->pred = stmt->pred;
} else {
stmt->basicblock->last = stmt->pred;
}
stmt->basicblock = NULL;
}
void detach_ir_op(struct ir_func *irb, struct ir_op *op) {
invariant_assert(irb->op_count,
"`detach_ir_op` would underflow op count for `ir_builder`");
invariant_assert(op->stmt, "can't detach `op` not attached to stmt");
irb->op_count--;
// fix links on either side of op
if (op->pred) {
op->pred->succ = op->succ;
} else {
op->stmt->first = op->succ;
}
if (op->succ) {
op->succ->pred = op->pred;
} else {
op->stmt->last = op->pred;
}
op->stmt = NULL;
op->pred = NULL;
op->succ = NULL;
}
bool stmt_is_empty(struct ir_stmt *stmt) {
invariant_assert(
(stmt->first && stmt->last) || (!stmt->first && !stmt->last),
"stmt had `first` or `last` NULL but not both; this is badly formed");
// first will be NULL
return !stmt->first;
}
bool basicblock_is_empty(struct ir_basicblock *basicblock) {
invariant_assert((basicblock->first && basicblock->last) ||
(!basicblock->first && !basicblock->last),
"basicblock had `first` or `last` NULL but not both; this "
"is badly formed");
return !basicblock->first || (!basicblock->first->first->succ &&
basicblock->first->first->ty == IR_OP_TY_BR);
}
void prune_basicblocks(struct ir_func *irb) {
if (!irb->first) {
return;
}
// skip first BB as it has an implicit predecessor
prune_stmts(irb, irb->first);
struct ir_basicblock *basicblock = irb->first->succ;
while (basicblock) {
prune_stmts(irb, basicblock);
// save succ before we detach
struct ir_basicblock *succ = basicblock->succ;
bool has_preds = basicblock->num_preds;
bool has_ops = basicblock->first &&
basicblock->first->first != basicblock->first->last;
// remove if it has no preds (if it has preds, it is needed as a target)
if (!has_preds && !has_ops) {
detach_ir_basicblock(irb, basicblock);
}
basicblock = succ;
}
// means bb->id < bb_count for all bbs
rebuild_ids(irb);
}
void prune_stmts(struct ir_func *irb, struct ir_basicblock *basicblock) {
struct ir_stmt *stmt = basicblock->first;
while (stmt) {
// save succ before we detach
struct ir_stmt *succ = stmt->succ;
if (stmt_is_empty(stmt)) {
detach_ir_stmt(irb, stmt);
}
stmt = succ;
}
}
void clear_metadata(struct ir_func *irb) {
struct ir_basicblock *basicblock = irb->first;
while (basicblock) {
basicblock->metadata = NULL;
struct ir_stmt *stmt = basicblock->first;
while (stmt) {
struct ir_op *op = stmt->first;
while (op) {
op->metadata = NULL;
op = op->succ;
}
stmt = stmt->succ;
}
basicblock = basicblock->succ;
}
}
void rebuild_ids(struct ir_func *irb) {
irb->next_basicblock_id = 0;
irb->next_stmt_id = 0;
irb->next_op_id = 0;
struct ir_basicblock *basicblock = irb->first;
while (basicblock) {
basicblock->id = irb->next_basicblock_id++;
struct ir_stmt *stmt = basicblock->first;
while (stmt) {
stmt->id = irb->next_stmt_id++;
struct ir_op *op = stmt->first;
while (op) {
op->id = irb->next_op_id++;
op = op->succ;
}
stmt = stmt->succ;
}
basicblock = basicblock->succ;
}
}
void attach_ir_op(struct ir_func *irb, struct ir_op *op, struct ir_stmt *stmt,
struct ir_op *pred, struct ir_op *succ) {
invariant_assert(!op->stmt && !op->pred && !op->succ,
"non-detached op trying to be attached");
invariant_assert((!pred || pred->succ == succ) &&
(!succ || succ->pred == pred),
"`pred` and `succ` are not next to each other");
invariant_assert(stmt, "cannot attach op without stmt");
irb->op_count++;
op->stmt = stmt;
if (op != pred) {
op->pred = pred;
}
if (op != succ) {
op->succ = succ;
}
if (op->pred) {
op->pred->succ = op;
} else {
op->stmt->first = op;
}
if (op->succ) {
op->succ->pred = op;
} else {
op->stmt->last = op;
}
}
void attach_ir_basicblock(struct ir_func *irb, struct ir_basicblock *basicblock,
struct ir_basicblock *pred,
struct ir_basicblock *succ) {
invariant_assert(!basicblock->pred && !basicblock->succ,
"non-detached basicblock trying to be attached");
invariant_assert((!pred || pred->succ == succ) &&
(!succ || succ->pred == pred),
"`pred` and `succ` are not next to each other");
irb->basicblock_count++;
if (basicblock != pred) {
basicblock->pred = pred;
}
if (basicblock != succ) {
basicblock->succ = succ;
}
if (basicblock->pred) {
basicblock->pred->succ = basicblock;
} else {
irb->first = basicblock;
}
if (basicblock->succ) {
basicblock->succ->pred = basicblock;
} else {
irb->last = basicblock;
}
}
void move_after_ir_op(struct ir_func *irb, struct ir_op *op,
struct ir_op *move_after) {
invariant_assert(op->id != move_after->id, "trying to move op after itself!");
invariant_assert(op->stmt == NULL || op->stmt == move_after->stmt,
"moving between ir_stmts not supported");
if (op->stmt) {
detach_ir_op(irb, op);
}
attach_ir_op(irb, op, move_after->stmt, move_after, move_after->succ);
}
void move_before_ir_op(struct ir_func *irb, struct ir_op *op,
struct ir_op *move_before) {
invariant_assert(op->id != move_before->id,
"trying to move op before itself!");
invariant_assert(op->stmt == NULL || op->stmt == move_before->stmt,
"moving between ir_stmts not supported");
if (op->stmt) {
detach_ir_op(irb, op);
}
attach_ir_op(irb, op, move_before->stmt, move_before->pred, move_before);
}
void move_after_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *basicblock,
struct ir_basicblock *move_after) {
invariant_assert(basicblock->id != move_after->id,
"trying to move basicblock after itself!");
if (basicblock->irb) {
detach_ir_basicblock(irb, basicblock);
}
attach_ir_basicblock(irb, basicblock, move_after, move_after->succ);
}
void move_before_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *basicblock,
struct ir_basicblock *move_before) {
invariant_assert(basicblock->id != move_before->id,
"trying to move basicblock before itself!");
if (basicblock->irb) {
detach_ir_basicblock(irb, basicblock);
}
attach_ir_basicblock(irb, basicblock, move_before->pred, move_before);
}
struct ir_op *replace_ir_op(UNUSED struct ir_func *irb, struct ir_op *op,
enum ir_op_ty ty, struct ir_var_ty var_ty) {
DEBUG_ASSERT(op, "invalid replacement point!");
op->ty = ty;
op->var_ty = var_ty;
return op;
}
struct ir_op *insert_before_ir_op(struct ir_func *irb,
struct ir_op *insert_before, enum ir_op_ty ty,
struct ir_var_ty var_ty) {
DEBUG_ASSERT(insert_before, "invalid insertion point!");
struct ir_op *op = arena_alloc(irb->arena, sizeof(*op));
initialise_ir_op(op, irb->next_op_id++, ty, var_ty, NO_REG, NULL);
move_before_ir_op(irb, op, insert_before);
return op;
}
void initialise_ir_basicblock(struct ir_basicblock *basicblock, size_t id) {
basicblock->id = id;
basicblock->irb = NULL;
basicblock->pred = NULL;
basicblock->succ = NULL;
basicblock->first = NULL;
basicblock->last = NULL;
basicblock->metadata = NULL;
basicblock->comment = NULL;
basicblock->first_instr = NULL;
basicblock->last_instr = NULL;
basicblock->preds = NULL;
basicblock->num_preds = 0;
}
struct ir_op *insert_after_ir_op(struct ir_func *irb,
struct ir_op *insert_after, enum ir_op_ty ty,
struct ir_var_ty var_ty) {
DEBUG_ASSERT(insert_after, "invalid insertion point!");
struct ir_op *op = arena_alloc(irb->arena, sizeof(*op));
initialise_ir_op(op, irb->next_op_id++, ty, var_ty, NO_REG, NULL);
move_after_ir_op(irb, op, insert_after);
return op;
}
struct ir_op *insert_phi(struct ir_func *irb, struct ir_basicblock *basicblock,
struct ir_var_ty var_ty) {
struct ir_stmt *stmt = basicblock->first;
if (!stmt) {
stmt = alloc_ir_stmt(irb, basicblock);
}
struct ir_op *op = stmt->first;
struct ir_op *phi;
if (op) {
phi = insert_before_ir_op(irb, op, IR_OP_TY_PHI, var_ty);
} else {
phi = alloc_ir_op(irb, stmt);
phi->ty = IR_OP_TY_PHI;
phi->var_ty = var_ty;
}
phi->phi = (struct ir_op_phi){.num_values = 0, .values = NULL};
return phi;
}
struct ir_basicblock *
insert_before_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *insert_before) {
DEBUG_ASSERT(insert_before, "invalid insertion point!");
struct ir_basicblock *basicblock =
arena_alloc(irb->arena, sizeof(*basicblock));
initialise_ir_basicblock(basicblock, irb->next_basicblock_id++);
move_before_ir_basicblock(irb, basicblock, insert_before);
return basicblock;
}
struct ir_basicblock *
insert_after_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *insert_after) {
DEBUG_ASSERT(insert_after, "invalid insertion point!");
struct ir_basicblock *basicblock =
arena_alloc(irb->arena, sizeof(*basicblock));
initialise_ir_basicblock(basicblock, irb->next_basicblock_id++);
move_after_ir_basicblock(irb, basicblock, insert_after);
return basicblock;
}
void swap_ir_ops(struct ir_func *irb, struct ir_op *left, struct ir_op *right) {
if (left == right) {
return;
}
// if they are next to each other, normalize to `left` being pred to `right`
if (right->succ == left) {