-
Notifications
You must be signed in to change notification settings - Fork 23
/
ir_ra.c
4119 lines (3726 loc) · 114 KB
/
ir_ra.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
/*
* IR - Lightweight JIT Compilation Framework
* (RA - Register Allocation, Liveness, Coalescing, SSA Deconstruction)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <[email protected]>
*
* See: "Linear Scan Register Allocation on SSA Form", Christian Wimmer and
* Michael Franz, CGO'10 (2010)
* See: "Optimized Interval Splitting in a Linear Scan Register Allocator",
* Christian Wimmer VEE'10 (2005)
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stdlib.h>
#include "ir.h"
#if defined(IR_TARGET_X86) || defined(IR_TARGET_X64)
# include "ir_x86.h"
#elif defined(IR_TARGET_AARCH64)
# include "ir_aarch64.h"
#else
# error "Unknown IR target"
#endif
#include "ir_private.h"
int ir_regs_number(void)
{
return IR_REG_NUM;
}
bool ir_reg_is_int(int32_t reg)
{
IR_ASSERT(reg >= 0 && reg < IR_REG_NUM);
return reg >= IR_REG_GP_FIRST && reg <= IR_REG_GP_LAST;
}
static int ir_assign_virtual_registers_slow(ir_ctx *ctx)
{
uint32_t *vregs;
uint32_t vregs_count = 0;
uint32_t b;
ir_ref i, n;
ir_block *bb;
ir_insn *insn;
uint32_t flags;
/* Assign unique virtual register to each data node */
vregs = ir_mem_calloc(ctx->insns_count, sizeof(ir_ref));
n = 1;
for (b = 1, bb = ctx->cfg_blocks + b; b <= ctx->cfg_blocks_count; b++, bb++) {
IR_ASSERT(!(bb->flags & IR_BB_UNREACHABLE));
i = bb->start;
/* skip first instruction */
insn = ctx->ir_base + i;
n = ir_insn_len(insn);
i += n;
insn += n;
while (i < bb->end) {
flags = ir_op_flags[insn->op];
if (((flags & IR_OP_FLAG_DATA) && insn->op != IR_VAR && (insn->op != IR_PARAM || ctx->use_lists[i].count > 0))
|| ((flags & IR_OP_FLAG_MEM) && ctx->use_lists[i].count > 1)) {
if (!ctx->rules || !(ctx->rules[i] & (IR_FUSED|IR_SKIPPED))) {
vregs[i] = ++vregs_count;
}
}
n = ir_insn_len(insn);
i += n;
insn += n;
}
}
ctx->vregs_count = vregs_count;
ctx->vregs = vregs;
return 1;
}
int ir_assign_virtual_registers(ir_ctx *ctx)
{
uint32_t *vregs;
uint32_t vregs_count = 0;
ir_ref i;
ir_insn *insn;
if (!ctx->rules) {
return ir_assign_virtual_registers_slow(ctx);
}
/* Assign unique virtual register to each rule that needs it */
vregs = ir_mem_malloc(ctx->insns_count * sizeof(ir_ref));
for (i = 1, insn = &ctx->ir_base[1]; i < ctx->insns_count; i++, insn++) {
uint32_t v = 0;
if (ctx->rules[i] && !(ctx->rules[i] & (IR_FUSED|IR_SKIPPED))) {
uint32_t flags = ir_op_flags[insn->op];
if ((flags & IR_OP_FLAG_DATA)
|| ((flags & IR_OP_FLAG_MEM) && ctx->use_lists[i].count > 1)) {
v = ++vregs_count;
}
}
vregs[i] = v;
}
ctx->vregs_count = vregs_count;
ctx->vregs = vregs;
return 1;
}
/* Lifetime intervals construction */
static ir_live_interval *ir_new_live_range(ir_ctx *ctx, int v, ir_live_pos start, ir_live_pos end)
{
ir_live_interval *ival = ir_arena_alloc(&ctx->arena, sizeof(ir_live_interval));
ival->type = IR_VOID;
ival->reg = IR_REG_NONE;
ival->flags = 0;
ival->vreg = v;
ival->stack_spill_pos = -1; // not allocated
ival->range.start = start;
ival->range.end = ival->end = end;
ival->range.next = NULL;
ival->use_pos = NULL;
ival->next = NULL;
ctx->live_intervals[v] = ival;
return ival;
}
static ir_live_interval *ir_add_live_range(ir_ctx *ctx, int v, ir_live_pos start, ir_live_pos end)
{
ir_live_interval *ival = ctx->live_intervals[v];
ir_live_range *p, *q;
if (!ival) {
return ir_new_live_range(ctx, v, start, end);
}
p = &ival->range;
if (end >= p->start) {
ir_live_range *prev = NULL;
do {
if (p->end >= start) {
if (start < p->start) {
p->start = start;
}
if (end > p->end) {
/* merge with next */
ir_live_range *next = p->next;
p->end = end;
while (next && p->end >= next->start) {
if (next->end > p->end) {
p->end = next->end;
}
p->next = next->next;
/* remember in the "unused_ranges" list */
next->next = ctx->unused_ranges;
ctx->unused_ranges = next;
next = p->next;
}
if (!p->next) {
ival->end = p->end;
}
}
return ival;
}
prev = p;
p = prev->next;
} while (p && end >= p->start);
if (!p) {
ival->end = end;
}
if (prev) {
if (ctx->unused_ranges) {
/* reuse */
q = ctx->unused_ranges;
ctx->unused_ranges = q->next;
} else {
q = ir_arena_alloc(&ctx->arena, sizeof(ir_live_range));
}
prev->next = q;
q->start = start;
q->end = end;
q->next = p;
return ival;
}
}
if (ctx->unused_ranges) {
/* reuse */
q = ctx->unused_ranges;
ctx->unused_ranges = q->next;
} else {
q = ir_arena_alloc(&ctx->arena, sizeof(ir_live_range));
}
q->start = p->start;
q->end = p->end;
q->next = p->next;
p->start = start;
p->end = end;
p->next = q;
return ival;
}
IR_ALWAYS_INLINE ir_live_interval *ir_add_prev_live_range(ir_ctx *ctx, int v, ir_live_pos start, ir_live_pos end)
{
ir_live_interval *ival = ctx->live_intervals[v];
if (ival && ival->range.start == end) {
ival->range.start = start;
return ival;
}
return ir_add_live_range(ctx, v, start, end);
}
static void ir_add_fixed_live_range(ir_ctx *ctx, ir_reg reg, ir_live_pos start, ir_live_pos end)
{
int v = ctx->vregs_count + 1 + reg;
ir_live_interval *ival = ctx->live_intervals[v];
ir_live_range *q;
if (!ival) {
ival = ir_arena_alloc(&ctx->arena, sizeof(ir_live_interval));
ival->type = IR_VOID;
ival->reg = reg;
ival->flags = IR_LIVE_INTERVAL_FIXED;
ival->vreg = v;
ival->stack_spill_pos = -1; // not allocated
ival->range.start = start;
ival->range.end = ival->end = end;
ival->range.next = NULL;
ival->use_pos = NULL;
ival->next = NULL;
ctx->live_intervals[v] = ival;
} else if (EXPECTED(end < ival->range.start)) {
if (ctx->unused_ranges) {
/* reuse */
q = ctx->unused_ranges;
ctx->unused_ranges = q->next;
} else {
q = ir_arena_alloc(&ctx->arena, sizeof(ir_live_range));
}
q->start = ival->range.start;
q->end = ival->range.end;
q->next = ival->range.next;
ival->range.start = start;
ival->range.end = end;
ival->range.next = q;
} else if (end == ival->range.start) {
ival->range.start = start;
} else {
ir_add_live_range(ctx, v, start, end);
}
}
static void ir_add_tmp(ir_ctx *ctx, ir_ref ref, ir_ref tmp_ref, int32_t tmp_op_num, ir_tmp_reg tmp_reg)
{
ir_live_interval *ival = ir_arena_alloc(&ctx->arena, sizeof(ir_live_interval));
ival->type = tmp_reg.type;
ival->reg = IR_REG_NONE;
ival->flags = IR_LIVE_INTERVAL_TEMP;
ival->tmp_ref = tmp_ref;
ival->tmp_op_num = tmp_op_num;
ival->range.start = IR_START_LIVE_POS_FROM_REF(ref) + tmp_reg.start;
ival->range.end = ival->end = IR_START_LIVE_POS_FROM_REF(ref) + tmp_reg.end;
ival->range.next = NULL;
ival->use_pos = NULL;
if (!ctx->live_intervals[0]) {
ival->next = NULL;
ctx->live_intervals[0] = ival;
} else if (ival->range.start >= ctx->live_intervals[0]->range.start) {
ir_live_interval *prev = ctx->live_intervals[0];
while (prev->next && ival->range.start >= prev->next->range.start) {
prev = prev->next;
}
ival->next = prev->next;
prev->next = ival;
} else {
ir_live_interval *next = ctx->live_intervals[0];
ival->next = next;
ctx->live_intervals[0] = ival;
}
return;
}
static bool ir_has_tmp(ir_ctx *ctx, ir_ref ref, int32_t op_num)
{
ir_live_interval *ival = ctx->live_intervals[0];
if (ival) {
while (ival && IR_LIVE_POS_TO_REF(ival->range.start) <= ref) {
if (ival->tmp_ref == ref && ival->tmp_op_num == op_num) {
return 1;
}
ival = ival->next;
}
}
return 0;
}
static ir_live_interval *ir_fix_live_range(ir_ctx *ctx, int v, ir_live_pos old_start, ir_live_pos new_start)
{
ir_live_interval *ival = ctx->live_intervals[v];
ir_live_range *p = &ival->range;
#if 0
while (p && p->start < old_start) {
p = p->next;
}
#endif
IR_ASSERT(ival && p->start == old_start);
p->start = new_start;
return ival;
}
static void ir_add_use_pos(ir_ctx *ctx, ir_live_interval *ival, ir_use_pos *use_pos)
{
ir_use_pos *p = ival->use_pos;
if (EXPECTED(!p || p->pos > use_pos->pos)) {
use_pos->next = p;
ival->use_pos = use_pos;
} else {
ir_use_pos *prev;
do {
prev = p;
p = p->next;
} while (p && p->pos < use_pos->pos);
use_pos->next = prev->next;
prev->next = use_pos;
}
}
IR_ALWAYS_INLINE void ir_add_use(ir_ctx *ctx, ir_live_interval *ival, int op_num, ir_live_pos pos, ir_reg hint, uint8_t use_flags, ir_ref hint_ref)
{
ir_use_pos *use_pos;
use_pos = ir_arena_alloc(&ctx->arena, sizeof(ir_use_pos));
use_pos->op_num = op_num;
use_pos->hint = hint;
use_pos->flags = use_flags;
use_pos->hint_ref = hint_ref;
use_pos->pos = pos;
if (hint != IR_REG_NONE) {
ival->flags |= IR_LIVE_INTERVAL_HAS_HINT_REGS;
}
if (hint_ref > 0) {
ival->flags |= IR_LIVE_INTERVAL_HAS_HINT_REFS;
}
ir_add_use_pos(ctx, ival, use_pos);
}
static void ir_add_phi_use(ir_ctx *ctx, ir_live_interval *ival, int op_num, ir_live_pos pos, ir_ref phi_ref)
{
ir_use_pos *use_pos;
IR_ASSERT(phi_ref > 0);
use_pos = ir_arena_alloc(&ctx->arena, sizeof(ir_use_pos));
use_pos->op_num = op_num;
use_pos->hint = IR_REG_NONE;
use_pos->flags = IR_PHI_USE | IR_USE_SHOULD_BE_IN_REG; // TODO: ???
use_pos->hint_ref = -phi_ref;
use_pos->pos = pos;
ir_add_use_pos(ctx, ival, use_pos);
}
static void ir_add_hint(ir_ctx *ctx, ir_ref ref, ir_live_pos pos, ir_reg hint)
{
ir_live_interval *ival = ctx->live_intervals[ctx->vregs[ref]];
if (!(ival->flags & IR_LIVE_INTERVAL_HAS_HINT_REGS)) {
ir_use_pos *use_pos = ival->use_pos;
while (use_pos) {
if (use_pos->pos == pos) {
if (use_pos->hint == IR_REG_NONE) {
use_pos->hint = hint;
ival->flags |= IR_LIVE_INTERVAL_HAS_HINT_REGS;
}
}
use_pos = use_pos->next;
}
}
}
static void ir_hint_propagation(ir_ctx *ctx)
{
int i;
ir_live_interval *ival;
ir_use_pos *use_pos;
ir_use_pos *hint_use_pos;
for (i = ctx->vregs_count; i > 0; i--) {
ival = ctx->live_intervals[i];
if (ival
&& (ival->flags & (IR_LIVE_INTERVAL_HAS_HINT_REGS|IR_LIVE_INTERVAL_HAS_HINT_REFS)) == (IR_LIVE_INTERVAL_HAS_HINT_REGS|IR_LIVE_INTERVAL_HAS_HINT_REFS)) {
use_pos = ival->use_pos;
hint_use_pos = NULL;
while (use_pos) {
if (use_pos->op_num == 0) {
if (use_pos->hint_ref > 0) {
hint_use_pos = use_pos;
}
} else if (use_pos->hint != IR_REG_NONE) {
if (hint_use_pos) {
ir_add_hint(ctx, hint_use_pos->hint_ref, hint_use_pos->pos, use_pos->hint);
hint_use_pos = NULL;
}
}
use_pos = use_pos->next;
}
}
}
}
#ifdef IR_BITSET_LIVENESS
/* DFS + Loop-Forest livness for SSA using bitset(s) */
static void ir_add_osr_entry_loads(ir_ctx *ctx, ir_block *bb, ir_bitset live, uint32_t len, uint32_t b)
{
bool ok = 1;
int count = 0;
ir_list *list = (ir_list*)ctx->osr_entry_loads;
ir_ref i;
IR_BITSET_FOREACH(live, len, i) {
/* Skip live references from ENTRY to PARAM. TODO: duplicate PARAM in each ENTRY ??? */
ir_use_pos *use_pos = ctx->live_intervals[i]->use_pos;
ir_ref ref = (use_pos->hint_ref < 0) ? -use_pos->hint_ref : IR_LIVE_POS_TO_REF(use_pos->pos);
if (use_pos->op_num) {
ir_ref *ops = ctx->ir_base[ref].ops;
ref = ops[use_pos->op_num];
}
if (ctx->ir_base[ref].op == IR_PARAM) {
continue;
}
if (ctx->binding) {
ir_ref var = ir_binding_find(ctx, ref);
if (var < 0) {
/* We may load the value at OSR entry-point */
if (!count) {
bb->flags &= ~IR_BB_EMPTY;
bb->flags |= IR_BB_OSR_ENTRY_LOADS;
if (!ctx->osr_entry_loads) {
list = ctx->osr_entry_loads = ir_mem_malloc(sizeof(ir_list));
ir_list_init(list, 16);
}
ir_list_push(list, b);
ir_list_push(list, 0);
}
ir_list_push(list, ref);
count++;
continue;
}
}
fprintf(stderr, "ENTRY %d (block %d start %d) - live var %d\n", ctx->ir_base[bb->start].op2, b, bb->start, ref);
ok = 0;
} IR_BITSET_FOREACH_END();
if (!ok) {
IR_ASSERT(0);
}
if (count) {
ir_list_set(list, ir_list_len(ctx->osr_entry_loads) - (count + 1), count);
#if 0
/* ENTRY "clobbers" all registers */
ir_ref ref = ctx->ir_base[bb->start].op1;
ir_add_fixed_live_range(ctx, IR_REG_ALL,
IR_DEF_LIVE_POS_FROM_REF(ref),
IR_SAVE_LIVE_POS_FROM_REF(ref));
#endif
}
}
static void ir_add_fusion_ranges(ir_ctx *ctx, ir_ref ref, ir_ref input, ir_block *bb, ir_bitset live)
{
ir_ref stack[4];
int stack_pos = 0;
ir_target_constraints constraints;
ir_insn *insn;
uint32_t j, n, flags, def_flags;
ir_ref *p, child;
uint8_t use_flags;
ir_reg reg;
ir_live_pos use_pos;
ir_live_interval *ival;
while (1) {
IR_ASSERT(input > 0 && ctx->rules[input] & IR_FUSED);
if (!(ctx->rules[input] & IR_SIMPLE)) {
def_flags = ir_get_target_constraints(ctx, input, &constraints);
n = constraints.tmps_count;
while (n > 0) {
n--;
if (constraints.tmp_regs[n].type) {
ir_add_tmp(ctx, ref, input, constraints.tmp_regs[n].num, constraints.tmp_regs[n]);
} else {
/* CPU specific constraints */
ir_add_fixed_live_range(ctx, constraints.tmp_regs[n].reg,
IR_START_LIVE_POS_FROM_REF(ref) + constraints.tmp_regs[n].start,
IR_START_LIVE_POS_FROM_REF(ref) + constraints.tmp_regs[n].end);
}
}
} else {
def_flags = IR_OP1_MUST_BE_IN_REG | IR_OP2_MUST_BE_IN_REG | IR_OP3_MUST_BE_IN_REG;
constraints.hints_count = 0;
}
insn = &ctx->ir_base[input];
flags = ir_op_flags[insn->op];
n = IR_INPUT_EDGES_COUNT(flags);
j = 1;
p = insn->ops + j;
if (flags & IR_OP_FLAG_CONTROL) {
j++;
p++;
}
for (; j <= n; j++, p++) {
IR_ASSERT(IR_OPND_KIND(flags, j) == IR_OPND_DATA);
child = *p;
if (child > 0) {
uint32_t v = ctx->vregs[child];
if (v) {
use_flags = IR_FUSED_USE | IR_USE_FLAGS(def_flags, j);
reg = (j < constraints.hints_count) ? constraints.hints[j] : IR_REG_NONE;
use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref);
if (EXPECTED(reg == IR_REG_NONE)) {
use_pos += IR_USE_SUB_REF;
}
if (!ir_bitset_in(live, v)) {
/* live.add(opd) */
ir_bitset_incl(live, v);
/* intervals[opd].addRange(b.from, op.id) */
ival = ir_add_live_range(ctx, v,
IR_START_LIVE_POS_FROM_REF(bb->start), use_pos);
} else {
ival = ctx->live_intervals[v];
}
ir_add_use(ctx, ival, j, use_pos, reg, use_flags, -input);
} else if (ctx->rules[child] & IR_FUSED) {
IR_ASSERT(stack_pos < (int)(sizeof(stack)/sizeof(stack_pos)));
stack[stack_pos++] = child;
} else if (ctx->rules[child] == (IR_SKIPPED|IR_RLOAD)) {
ir_set_alocated_reg(ctx, input, j, ctx->ir_base[child].op2);
}
}
}
if (!stack_pos) {
break;
}
input = stack[--stack_pos];
}
}
int ir_compute_live_ranges(ir_ctx *ctx)
{
uint32_t b, i, j, k, n, succ, *p;
ir_ref ref;
uint32_t len;
ir_insn *insn;
ir_block *bb, *succ_bb;
#ifdef IR_DEBUG
ir_bitset visited;
#endif
ir_bitset live, bb_live;
ir_bitset loops = NULL;
ir_bitqueue queue;
ir_live_interval *ival;
if (!(ctx->flags2 & IR_LINEAR) || !ctx->vregs) {
return 0;
}
if (ctx->rules) {
ctx->regs = ir_mem_malloc(sizeof(ir_regs) * ctx->insns_count);
memset(ctx->regs, IR_REG_NONE, sizeof(ir_regs) * ctx->insns_count);
}
/* Root of the list of IR_VARs */
ctx->vars = IR_UNUSED;
/* Compute Live Ranges */
ctx->flags2 &= ~IR_LR_HAVE_DESSA_MOVES;
len = ir_bitset_len(ctx->vregs_count + 1);
bb_live = ir_mem_malloc((ctx->cfg_blocks_count + 1) * len * sizeof(ir_bitset_base_t));
/* vregs + tmp + fixed + SRATCH + ALL */
ctx->live_intervals = ir_mem_calloc(ctx->vregs_count + 1 + IR_REG_NUM + 2, sizeof(ir_live_interval*));
#ifdef IR_DEBUG
visited = ir_bitset_malloc(ctx->cfg_blocks_count + 1);
#endif
if (!ctx->arena) {
ctx->arena = ir_arena_create(16 * 1024);
}
/* for each basic block in reverse order */
for (b = ctx->cfg_blocks_count; b > 0; b--) {
bb = &ctx->cfg_blocks[b];
IR_ASSERT(!(bb->flags & IR_BB_UNREACHABLE));
/* for each successor of b */
#ifdef IR_DEBUG
ir_bitset_incl(visited, b);
#endif
live = bb_live + (len * b);
n = bb->successors_count;
if (n == 0) {
ir_bitset_clear(live, len);
} else {
p = &ctx->cfg_edges[bb->successors];
succ = *p;
#ifdef IR_DEBUG
/* blocks must be ordered where all dominators of a block are before this block */
IR_ASSERT(ir_bitset_in(visited, succ) || bb->loop_header == succ);
#endif
/* live = union of successors.liveIn */
if (EXPECTED(succ > b) && EXPECTED(!(ctx->cfg_blocks[succ].flags & IR_BB_ENTRY))) {
ir_bitset_copy(live, bb_live + (len * succ), len);
} else {
IR_ASSERT(succ > b || (ctx->cfg_blocks[succ].flags & IR_BB_LOOP_HEADER));
ir_bitset_clear(live, len);
}
if (n > 1) {
for (p++, n--; n > 0; p++, n--) {
succ = *p;
if (EXPECTED(succ > b) && EXPECTED(!(ctx->cfg_blocks[succ].flags & IR_BB_ENTRY))) {
ir_bitset_union(live, bb_live + (len * succ), len);
} else {
IR_ASSERT(succ > b || (ctx->cfg_blocks[succ].flags & IR_BB_LOOP_HEADER));
}
}
}
/* for each opd in live */
IR_BITSET_FOREACH(live, len, i) {
/* intervals[opd].addRange(b.from, b.to) */
ir_add_prev_live_range(ctx, i,
IR_START_LIVE_POS_FROM_REF(bb->start),
IR_END_LIVE_POS_FROM_REF(bb->end));
} IR_BITSET_FOREACH_END();
}
if (bb->successors_count == 1) {
/* for each phi function phi of successor */
succ = ctx->cfg_edges[bb->successors];
succ_bb = &ctx->cfg_blocks[succ];
if (succ_bb->flags & IR_BB_HAS_PHI) {
ir_use_list *use_list = &ctx->use_lists[succ_bb->start];
k = ir_phi_input_number(ctx, succ_bb, b);
IR_ASSERT(k != 0);
for (ref = 0; ref < use_list->count; ref++) {
ir_ref use = ctx->use_edges[use_list->refs + ref];
insn = &ctx->ir_base[use];
if (insn->op == IR_PHI) {
ir_ref input = ir_insn_op(insn, k);
if (input > 0) {
uint32_t v = ctx->vregs[input];
/* live.add(phi.inputOf(b)) */
IR_ASSERT(v);
ir_bitset_incl(live, v);
/* intervals[phi.inputOf(b)].addRange(b.from, b.to) */
ival = ir_add_prev_live_range(ctx, v,
IR_START_LIVE_POS_FROM_REF(bb->start),
IR_END_LIVE_POS_FROM_REF(bb->end));
ir_add_phi_use(ctx, ival, k, IR_DEF_LIVE_POS_FROM_REF(bb->end), use);
}
}
}
}
}
/* for each operation op of b in reverse order */
ref = bb->end;
insn = &ctx->ir_base[ref];
if (insn->op == IR_END || insn->op == IR_LOOP_END) {
ref = ctx->prev_ref[ref];
}
for (; ref > bb->start; ref = ctx->prev_ref[ref]) {
uint32_t def_flags;
uint32_t flags;
ir_ref *p;
ir_target_constraints constraints;
uint32_t v;
if (ctx->rules) {
int n;
if (ctx->rules[ref] & (IR_FUSED|IR_SKIPPED)) {
if (((ctx->rules[ref] & IR_RULE_MASK) == IR_VAR
|| (ctx->rules[ref] & IR_RULE_MASK) == IR_ALLOCA)
&& ctx->use_lists[ref].count > 0) {
insn = &ctx->ir_base[ref];
if (insn->op != IR_VADDR) {
insn->op3 = ctx->vars;
ctx->vars = ref;
}
}
continue;
}
def_flags = ir_get_target_constraints(ctx, ref, &constraints);
n = constraints.tmps_count;
while (n > 0) {
n--;
if (constraints.tmp_regs[n].type) {
ir_add_tmp(ctx, ref, ref, constraints.tmp_regs[n].num, constraints.tmp_regs[n]);
} else {
/* CPU specific constraints */
ir_add_fixed_live_range(ctx, constraints.tmp_regs[n].reg,
IR_START_LIVE_POS_FROM_REF(ref) + constraints.tmp_regs[n].start,
IR_START_LIVE_POS_FROM_REF(ref) + constraints.tmp_regs[n].end);
}
}
} else {
def_flags = 0;
constraints.def_reg = IR_REG_NONE;
constraints.hints_count = 0;
}
insn = &ctx->ir_base[ref];
v = ctx->vregs[ref];
if (v) {
IR_ASSERT(ir_bitset_in(live, v));
if (insn->op != IR_PHI) {
ir_live_pos def_pos;
ir_ref hint_ref = 0;
ir_reg reg = constraints.def_reg;
if (reg != IR_REG_NONE) {
def_pos = IR_SAVE_LIVE_POS_FROM_REF(ref);
if (insn->op == IR_PARAM || insn->op == IR_RLOAD) {
/* parameter register must be kept before it's copied */
ir_add_fixed_live_range(ctx, reg, IR_START_LIVE_POS_FROM_REF(bb->start), def_pos);
}
} else if (def_flags & IR_DEF_REUSES_OP1_REG) {
if (!IR_IS_CONST_REF(insn->op1) && ctx->vregs[insn->op1]) {
hint_ref = insn->op1;
}
def_pos = IR_LOAD_LIVE_POS_FROM_REF(ref);
} else if (def_flags & IR_DEF_CONFLICTS_WITH_INPUT_REGS) {
def_pos = IR_LOAD_LIVE_POS_FROM_REF(ref);
} else {
if (insn->op == IR_PARAM) {
/* We may reuse parameter stack slot for spilling */
ctx->live_intervals[v]->flags |= IR_LIVE_INTERVAL_MEM_PARAM;
} else if (insn->op == IR_VLOAD) {
/* Load may be fused into the usage instruction */
ctx->live_intervals[v]->flags |= IR_LIVE_INTERVAL_MEM_LOAD;
}
def_pos = IR_DEF_LIVE_POS_FROM_REF(ref);
}
/* live.remove(opd) */
ir_bitset_excl(live, v);
/* intervals[opd].setFrom(op.id) */
ival = ir_fix_live_range(ctx, v,
IR_START_LIVE_POS_FROM_REF(bb->start), def_pos);
ival->type = insn->type;
ir_add_use(ctx, ival, 0, def_pos, reg, def_flags, hint_ref);
} else {
/* live.remove(opd) */
ir_bitset_excl(live, v);
/* PHIs inputs must not be processed */
ival = ctx->live_intervals[v];
if (UNEXPECTED(!ival)) {
/* Dead PHI */
ival = ir_add_live_range(ctx, v, IR_DEF_LIVE_POS_FROM_REF(ref), IR_USE_LIVE_POS_FROM_REF(ref));
}
ival->type = insn->type;
ir_add_use(ctx, ival, 0, IR_DEF_LIVE_POS_FROM_REF(ref), IR_REG_NONE, IR_USE_SHOULD_BE_IN_REG, 0);
continue;
}
}
IR_ASSERT(insn->op != IR_PHI && (!ctx->rules || !(ctx->rules[ref] & (IR_FUSED|IR_SKIPPED))));
flags = ir_op_flags[insn->op];
j = 1;
p = insn->ops + 1;
if (flags & (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_PINNED)) {
j++;
p++;
}
for (; j <= insn->inputs_count; j++, p++) {
ir_ref input = *p;
ir_reg reg = (j < constraints.hints_count) ? constraints.hints[j] : IR_REG_NONE;
ir_live_pos use_pos;
ir_ref hint_ref = 0;
uint32_t v;
if (input > 0) {
v = ctx->vregs[input];
if (v) {
use_pos = IR_USE_LIVE_POS_FROM_REF(ref);
if (reg != IR_REG_NONE) {
use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref);
ir_add_fixed_live_range(ctx, reg, use_pos, use_pos + IR_USE_SUB_REF);
} else if (def_flags & IR_DEF_REUSES_OP1_REG) {
if (j == 1) {
use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref);
IR_ASSERT(ctx->vregs[ref]);
hint_ref = ref;
} else if (input == insn->op1) {
/* Input is the same as "op1" */
use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref);
}
}
if (!ir_bitset_in(live, v)) {
/* live.add(opd) */
ir_bitset_incl(live, v);
/* intervals[opd].addRange(b.from, op.id) */
ival = ir_add_live_range(ctx, v, IR_START_LIVE_POS_FROM_REF(bb->start), use_pos);
} else {
ival = ctx->live_intervals[v];
}
ir_add_use(ctx, ival, j, use_pos, reg, IR_USE_FLAGS(def_flags, j), hint_ref);
} else if (ctx->rules) {
if (ctx->rules[input] & IR_FUSED) {
ir_add_fusion_ranges(ctx, ref, input, bb, live);
} else if (ctx->rules[input] == (IR_SKIPPED|IR_RLOAD)) {
ir_set_alocated_reg(ctx, ref, j, ctx->ir_base[input].op2);
}
}
} else if (reg != IR_REG_NONE) {
use_pos = IR_LOAD_LIVE_POS_FROM_REF(ref);
ir_add_fixed_live_range(ctx, reg, use_pos, use_pos + IR_USE_SUB_REF);
}
}
}
/* if b is loop header */
if ((bb->flags & IR_BB_LOOP_HEADER)
&& !ir_bitset_empty(live, len)) {
/* variables live at loop header are alive at the whole loop body */
uint32_t bb_set_len = ir_bitset_len(ctx->cfg_blocks_count + 1);
uint32_t child;
ir_block *child_bb;
ir_bitset child_live_in;
if (!loops) {
loops = ir_bitset_malloc(ctx->cfg_blocks_count + 1);
ir_bitqueue_init(&queue, ctx->cfg_blocks_count + 1);
} else {
ir_bitset_clear(loops, bb_set_len);
ir_bitqueue_clear(&queue);
}
ir_bitset_incl(loops, b);
child = b;
do {
child_bb = &ctx->cfg_blocks[child];
child_live_in = bb_live + (len * child);
IR_BITSET_FOREACH(live, len, i) {
ir_bitset_incl(child_live_in, i);
ir_add_live_range(ctx, i,
IR_START_LIVE_POS_FROM_REF(child_bb->start),
IR_END_LIVE_POS_FROM_REF(child_bb->end));
} IR_BITSET_FOREACH_END();
child = child_bb->dom_child;
while (child) {
child_bb = &ctx->cfg_blocks[child];
if (child_bb->loop_header && ir_bitset_in(loops, child_bb->loop_header)) {
ir_bitqueue_add(&queue, child);
if (child_bb->flags & IR_BB_LOOP_HEADER) {
ir_bitset_incl(loops, child);
}
}
child = child_bb->dom_next_child;
}
} while ((child = ir_bitqueue_pop(&queue)) != (uint32_t)-1);
}
}
if (ctx->entries) {
for (i = 0; i < ctx->entries_count; i++) {
b = ctx->entries[i];
bb = &ctx->cfg_blocks[b];
live = bb_live + (len * b);
ir_add_osr_entry_loads(ctx, bb, live, len, b);
}
if (ctx->osr_entry_loads) {
ir_list_push((ir_list*)ctx->osr_entry_loads, 0);
}
}
if (loops) {
ir_mem_free(loops);
ir_bitqueue_free(&queue);
}
ir_mem_free(bb_live);
#ifdef IR_DEBUG
ir_mem_free(visited);
#endif
return 1;
}
#else
/* Path exploration by definition liveness for SSA using sets represented by linked lists */
#define IS_LIVE_IN_BLOCK(v, b) \
(live_in_block[v] == b)
#define SET_LIVE_IN_BLOCK(v, b) do { \
live_in_block[v] = b; \
} while (0)
/* Returns the last virtual register alive at the end of the block (it is used as an already-visited marker) */
IR_ALWAYS_INLINE uint32_t ir_live_out_top(ir_ctx *ctx, uint32_t *live_outs, ir_list *live_lists, uint32_t b)
{
#if 0
return live_outs[b];
#else
if (!live_outs[b]) {
return -1;
}
return ir_list_at(live_lists, live_outs[b]);
#endif
}
/* Remember a virtual register alive at the end of the block */
IR_ALWAYS_INLINE void ir_live_out_push(ir_ctx *ctx, uint32_t *live_outs, ir_list *live_lists, uint32_t b, uint32_t v)
{
#if 0
ir_block *bb = &ctx->cfg_blocks[b];
live_outs[b] = v;
ir_add_prev_live_range(ctx, v,
IR_START_LIVE_POS_FROM_REF(bb->start),
IR_END_LIVE_POS_FROM_REF(bb->end));
#else
if (live_lists->len >= live_lists->a.size) {
ir_array_grow(&live_lists->a, live_lists->a.size + 1024);
}
/* Form a linked list of virtual register live at the end of the block */
ir_list_push_unchecked(live_lists, live_outs[b]); /* push old root of the list (previous element of the list) */
live_outs[b] = ir_list_len(live_lists); /* remember the new root */
ir_list_push_unchecked(live_lists, v); /* push a virtual register */
#endif
}
/*
* Computes live-out sets for each basic-block per variable using def-use chains.
*
* The implementation is based on algorithms 6 and 7 desriebed in
* "Computing Liveness Sets for SSA-Form Programs", Florian Brandner, Benoit Boissinot.
* Alain Darte, Benoit Dupont de Dinechin, Fabrice Rastello. TR Inria RR-7503, 2011
*/
static void ir_compute_live_sets(ir_ctx *ctx, uint32_t *live_outs, ir_list *live_lists)
{
ir_list block_queue, fuse_queue;
ir_ref i;
ir_list_init(&fuse_queue, 16);
ir_list_init(&block_queue, 256);
/* For each virtual register explore paths from all uses to definition */
for (i = ctx->insns_count - 1; i > 0; i--) {
uint32_t v = ctx->vregs[i];
if (v) {
uint32_t def_block = ctx->cfg_map[i];
ir_use_list *use_list = &ctx->use_lists[i];
ir_ref *p, n = use_list->count;
/* Collect all blocks where 'v' is used into a 'block_queue' */
for (p = &ctx->use_edges[use_list->refs]; n > 0; p++, n--) {
ir_ref use = *p;
ir_insn *insn = &ctx->ir_base[use];