forked from dstogov/ir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.c
2918 lines (2593 loc) · 73.3 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
/*
* IR - Lightweight JIT Compilation Framework
* (IR construction, folding, utilities)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <[email protected]>
*
* The logical IR representation is based on Cliff Click's Sea of Nodes.
* See: C. Click, M. Paleczny. "A Simple Graph-Based Intermediate
* Representation" In ACM SIGPLAN Workshop on Intermediate Representations
* (IR '95), pages 35-49, Jan. 1995.
*
* The physical IR representation is based on Mike Pall's LuaJIT IR.
* See: M. Pall. "LuaJIT 2.0 intellectual property disclosure and research
* opportunities" November 2009 http://lua-users.org/lists/lua-l/2009-11/msg00089.html
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#ifndef _WIN32
# include <sys/mman.h>
# if defined(__linux__) || defined(__sun)
# include <alloca.h>
# endif
# if defined(__APPLE__) && defined(__aarch64__)
# include <libkern/OSCacheControl.h>
# endif
#else
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#include "ir.h"
#include "ir_private.h"
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#ifdef HAVE_VALGRIND
# include <valgrind/valgrind.h>
#endif
#define IR_TYPE_FLAGS(name, type, field, flags) ((flags)|sizeof(type)),
#define IR_TYPE_NAME(name, type, field, flags) #name,
#define IR_TYPE_CNAME(name, type, field, flags) #type,
#define IR_TYPE_SIZE(name, type, field, flags) sizeof(type),
#define IR_OP_NAME(name, flags, op1, op2, op3) #name,
const uint8_t ir_type_flags[IR_LAST_TYPE] = {
0,
IR_TYPES(IR_TYPE_FLAGS)
};
const char *ir_type_name[IR_LAST_TYPE] = {
"void",
IR_TYPES(IR_TYPE_NAME)
};
const uint8_t ir_type_size[IR_LAST_TYPE] = {
0,
IR_TYPES(IR_TYPE_SIZE)
};
const char *ir_type_cname[IR_LAST_TYPE] = {
"void",
IR_TYPES(IR_TYPE_CNAME)
};
const char *ir_op_name[IR_LAST_OP] = {
IR_OPS(IR_OP_NAME)
#ifdef IR_PHP
IR_PHP_OPS(IR_OP_NAME)
#endif
};
void ir_print_escaped_str(const char *s, size_t len, FILE *f)
{
char ch;
while (len > 0) {
ch = *s;
switch (ch) {
case '\\': fputs("\\\\", f); break;
case '\'': fputs("'", f); break;
case '\"': fputs("\\\"", f); break;
case '\a': fputs("\\a", f); break;
case '\b': fputs("\\b", f); break;
case '\033': fputs("\\e", f); break;
case '\f': fputs("\\f", f); break;
case '\n': fputs("\\n", f); break;
case '\r': fputs("\\r", f); break;
case '\t': fputs("\\t", f); break;
case '\v': fputs("\\v", f); break;
case '\?': fputs("\\?", f); break;
default:
#ifdef __aarch64__
if (ch < 32) {
#else
if (ch >= 0 && ch < 32) {
#endif
fprintf(f, "\\%c%c%c",
'0' + ((ch >> 6) % 8),
'0' + ((ch >> 3) % 8),
'0' + (ch % 8));
break;
} else {
fputc(ch, f);
}
}
s++;
len--;
}
}
void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted)
{
char buf[128];
if (insn->op == IR_FUNC || insn->op == IR_SYM) {
fprintf(f, "%s", ir_get_str(ctx, insn->val.name));
return;
} else if (insn->op == IR_STR) {
size_t len;
const char *str = ir_get_strl(ctx, insn->val.str, &len);
if (quoted) {
fprintf(f, "\"");
ir_print_escaped_str(str, len, f);
fprintf(f, "\"");
} else {
ir_print_escaped_str(str, len, f);
}
return;
}
IR_ASSERT(IR_IS_CONST_OP(insn->op) || insn->op == IR_FUNC_ADDR);
switch (insn->type) {
case IR_BOOL:
fprintf(f, "%u", insn->val.b);
break;
case IR_U8:
fprintf(f, "%u", insn->val.u8);
break;
case IR_U16:
fprintf(f, "%u", insn->val.u16);
break;
case IR_U32:
fprintf(f, "%u", insn->val.u32);
break;
case IR_U64:
fprintf(f, "%" PRIu64, insn->val.u64);
break;
case IR_ADDR:
if (insn->val.addr) {
fprintf(f, "0x%" PRIxPTR, insn->val.addr);
} else {
fprintf(f, "0");
}
break;
case IR_CHAR:
if (insn->val.c == '\\') {
fprintf(f, "'\\\\'");
} else if (insn->val.c >= ' ') {
fprintf(f, "'%c'", insn->val.c);
} else if (insn->val.c == '\t') {
fprintf(f, "'\\t'");
} else if (insn->val.c == '\r') {
fprintf(f, "'\\r'");
} else if (insn->val.c == '\n') {
fprintf(f, "'\\n'");
} else if (insn->val.c == '\0') {
fprintf(f, "'\\0'");
} else {
fprintf(f, "%u", insn->val.c);
}
break;
case IR_I8:
fprintf(f, "%d", insn->val.i8);
break;
case IR_I16:
fprintf(f, "%d", insn->val.i16);
break;
case IR_I32:
fprintf(f, "%d", insn->val.i32);
break;
case IR_I64:
fprintf(f, "%" PRIi64, insn->val.i64);
break;
case IR_DOUBLE:
if (isnan(insn->val.d)) {
fprintf(f, "nan");
} else {
snprintf(buf, sizeof(buf), "%g", insn->val.d);
if (strtod(buf, NULL) != insn->val.d) {
snprintf(buf, sizeof(buf), "%.53e", insn->val.d);
if (strtod(buf, NULL) != insn->val.d) {
IR_ASSERT(0 && "can't format double");
}
}
fprintf(f, "%s", buf);
}
break;
case IR_FLOAT:
if (isnan(insn->val.f)) {
fprintf(f, "nan");
} else {
snprintf(buf, sizeof(buf), "%g", insn->val.f);
if (strtod(buf, NULL) != insn->val.f) {
snprintf(buf, sizeof(buf), "%.24e", insn->val.f);
if (strtod(buf, NULL) != insn->val.f) {
IR_ASSERT(0 && "can't format float");
}
}
fprintf(f, "%s", buf);
}
break;
default:
IR_ASSERT(0);
break;
}
}
#define ir_op_flag_v 0
#define ir_op_flag_v0X3 (0 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_d IR_OP_FLAG_DATA
#define ir_op_flag_d0 ir_op_flag_d
#define ir_op_flag_d1 (ir_op_flag_d | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_d1X1 (ir_op_flag_d | 1 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_d2 (ir_op_flag_d | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_d2C (ir_op_flag_d | IR_OP_FLAG_COMMUTATIVE | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_d3 (ir_op_flag_d | 3 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_r IR_OP_FLAG_DATA // "d" and "r" are the same now
#define ir_op_flag_r0 ir_op_flag_r
#define ir_op_flag_p (IR_OP_FLAG_DATA | IR_OP_FLAG_PINNED)
#define ir_op_flag_p1 (ir_op_flag_p | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_p1X1 (ir_op_flag_p | 1 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_p1X2 (ir_op_flag_p | 1 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_p2 (ir_op_flag_p | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_pN (ir_op_flag_p | IR_OP_FLAG_VAR_INPUTS)
#define ir_op_flag_c IR_OP_FLAG_CONTROL
#define ir_op_flag_c1X2 (ir_op_flag_c | 1 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_c3 (ir_op_flag_c | 3 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_S (IR_OP_FLAG_CONTROL|IR_OP_FLAG_BB_START)
#define ir_op_flag_S0X1 (ir_op_flag_S | 0 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_S1 (ir_op_flag_S | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_S1X1 (ir_op_flag_S | 1 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_S2 (ir_op_flag_S | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_S2X1 (ir_op_flag_S | 2 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_SN (ir_op_flag_S | IR_OP_FLAG_VAR_INPUTS)
#define ir_op_flag_E (IR_OP_FLAG_CONTROL|IR_OP_FLAG_BB_END)
#define ir_op_flag_E1 (ir_op_flag_E | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_E2 (ir_op_flag_E | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_T (IR_OP_FLAG_CONTROL|IR_OP_FLAG_BB_END|IR_OP_FLAG_TERMINATOR)
#define ir_op_flag_T2X1 (ir_op_flag_T | 2 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_T1X2 (ir_op_flag_T | 1 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_l (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_LOAD)
#define ir_op_flag_l0 ir_op_flag_l
#define ir_op_flag_l1 (ir_op_flag_l | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_l1X1 (ir_op_flag_l | 1 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_l1X2 (ir_op_flag_l | 1 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_l2 (ir_op_flag_l | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_l3 (ir_op_flag_l | 3 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_s (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_STORE)
#define ir_op_flag_s0 ir_op_flag_s
#define ir_op_flag_s1 (ir_op_flag_s | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_s2 (ir_op_flag_s | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_s2X1 (ir_op_flag_s | 2 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_s3 (ir_op_flag_s | 3 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_x1 (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_CALL | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_x2 (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_CALL | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_x3 (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_CALL | 3 | (3 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_xN (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_CALL | IR_OP_FLAG_VAR_INPUTS)
#define ir_op_flag_a1 (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_ALLOC | 1 | (1 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_flag_a2 (IR_OP_FLAG_CONTROL|IR_OP_FLAG_MEM|IR_OP_FLAG_MEM_ALLOC | 2 | (2 << IR_OP_FLAG_OPERANDS_SHIFT))
#define ir_op_kind____ IR_OPND_UNUSED
#define ir_op_kind_def IR_OPND_DATA
#define ir_op_kind_ref IR_OPND_DATA
#define ir_op_kind_src IR_OPND_CONTROL
#define ir_op_kind_reg IR_OPND_CONTROL_DEP
#define ir_op_kind_ret IR_OPND_CONTROL_REF
#define ir_op_kind_str IR_OPND_STR
#define ir_op_kind_num IR_OPND_NUM
#define ir_op_kind_fld IR_OPND_STR
#define ir_op_kind_var IR_OPND_DATA
#define ir_op_kind_prb IR_OPND_PROB
#define ir_op_kind_opt IR_OPND_PROB
#define ir_op_kind_pro IR_OPND_PROTO
#define _IR_OP_FLAGS(name, flags, op1, op2, op3) \
IR_OP_FLAGS(ir_op_flag_ ## flags, ir_op_kind_ ## op1, ir_op_kind_ ## op2, ir_op_kind_ ## op3),
const uint32_t ir_op_flags[IR_LAST_OP] = {
IR_OPS(_IR_OP_FLAGS)
#ifdef IR_PHP
IR_PHP_OPS(_IR_OP_FLAGS)
#endif
};
static void ir_grow_bottom(ir_ctx *ctx)
{
ir_insn *buf = ctx->ir_base - ctx->consts_limit;
ir_ref old_consts_limit = ctx->consts_limit;
if (ctx->consts_limit < 1024 * 4) {
ctx->consts_limit *= 2;
} else if (ctx->consts_limit < 1024 * 4 * 2) {
ctx->consts_limit = 1024 * 4 * 2;
} else {
ctx->consts_limit += 1024 * 4;
}
buf = ir_mem_realloc(buf, (ctx->consts_limit + ctx->insns_limit) * sizeof(ir_insn));
memmove(buf + (ctx->consts_limit - old_consts_limit),
buf,
(old_consts_limit + ctx->insns_count) * sizeof(ir_insn));
ctx->ir_base = buf + ctx->consts_limit;
}
static ir_ref ir_next_const(ir_ctx *ctx)
{
ir_ref ref = ctx->consts_count;
if (UNEXPECTED(ref >= ctx->consts_limit)) {
ir_grow_bottom(ctx);
}
ctx->consts_count = ref + 1;
return -ref;
}
static void ir_grow_top(ir_ctx *ctx)
{
ir_insn *buf = ctx->ir_base - ctx->consts_limit;
if (ctx->insns_limit < 1024 * 4) {
ctx->insns_limit *= 2;
} else if (ctx->insns_limit < 1024 * 4 * 2) {
ctx->insns_limit = 1024 * 4 * 2;
} else {
ctx->insns_limit += 1024 * 4;
}
buf = ir_mem_realloc(buf, (ctx->consts_limit + ctx->insns_limit) * sizeof(ir_insn));
ctx->ir_base = buf + ctx->consts_limit;
}
static ir_ref ir_next_insn(ir_ctx *ctx)
{
ir_ref ref = ctx->insns_count;
if (UNEXPECTED(ref >= ctx->insns_limit)) {
ir_grow_top(ctx);
}
ctx->insns_count = ref + 1;
return ref;
}
void ir_truncate(ir_ctx *ctx)
{
ir_insn *buf = ir_mem_malloc((ctx->consts_count + ctx->insns_count) * sizeof(ir_insn));
memcpy(buf, ctx->ir_base - ctx->consts_count, (ctx->consts_count + ctx->insns_count) * sizeof(ir_insn));
ir_mem_free(ctx->ir_base - ctx->consts_limit);
ctx->insns_limit = ctx->insns_count;
ctx->consts_limit = ctx->consts_count;
ctx->ir_base = buf + ctx->consts_limit;
}
void ir_init(ir_ctx *ctx, uint32_t flags, ir_ref consts_limit, ir_ref insns_limit)
{
ir_insn *buf;
IR_ASSERT(consts_limit >= IR_CONSTS_LIMIT_MIN);
IR_ASSERT(insns_limit >= IR_INSNS_LIMIT_MIN);
memset(ctx, 0, sizeof(ir_ctx));
ctx->insns_count = IR_UNUSED + 1;
ctx->insns_limit = insns_limit;
ctx->consts_count = -(IR_TRUE - 1);
ctx->consts_limit = consts_limit;
ctx->fold_cse_limit = IR_UNUSED + 1;
ctx->flags = flags;
ctx->spill_base = -1;
ctx->fixed_stack_frame_size = -1;
buf = ir_mem_malloc((consts_limit + insns_limit) * sizeof(ir_insn));
ctx->ir_base = buf + consts_limit;
MAKE_NOP(&ctx->ir_base[IR_UNUSED]);
ctx->ir_base[IR_NULL].optx = IR_OPT(IR_C_ADDR, IR_ADDR);
ctx->ir_base[IR_NULL].val.u64 = 0;
ctx->ir_base[IR_FALSE].optx = IR_OPT(IR_C_BOOL, IR_BOOL);
ctx->ir_base[IR_FALSE].val.u64 = 0;
ctx->ir_base[IR_TRUE].optx = IR_OPT(IR_C_BOOL, IR_BOOL);
ctx->ir_base[IR_TRUE].val.u64 = 1;
}
void ir_free(ir_ctx *ctx)
{
ir_insn *buf = ctx->ir_base - ctx->consts_limit;
ir_mem_free(buf);
if (ctx->strtab.data) {
ir_strtab_free(&ctx->strtab);
}
if (ctx->binding) {
ir_hashtab_free(ctx->binding);
ir_mem_free(ctx->binding);
}
if (ctx->use_lists) {
ir_mem_free(ctx->use_lists);
}
if (ctx->use_edges) {
ir_mem_free(ctx->use_edges);
}
if (ctx->cfg_blocks) {
ir_mem_free(ctx->cfg_blocks);
}
if (ctx->cfg_edges) {
ir_mem_free(ctx->cfg_edges);
}
if (ctx->cfg_map) {
ir_mem_free(ctx->cfg_map);
}
if (ctx->cfg_schedule) {
ir_mem_free(ctx->cfg_schedule);
}
if (ctx->rules) {
ir_mem_free(ctx->rules);
}
if (ctx->vregs) {
ir_mem_free(ctx->vregs);
}
if (ctx->live_intervals) {
ir_mem_free(ctx->live_intervals);
}
if (ctx->arena) {
ir_arena_free(ctx->arena);
}
if (ctx->regs) {
ir_mem_free(ctx->regs);
if (ctx->fused_regs) {
ir_strtab_free(ctx->fused_regs);
ir_mem_free(ctx->fused_regs);
}
}
if (ctx->prev_ref) {
ir_mem_free(ctx->prev_ref);
}
if (ctx->entries) {
ir_mem_free(ctx->entries);
}
if (ctx->osr_entry_loads) {
ir_list_free((ir_list*)ctx->osr_entry_loads);
ir_mem_free(ctx->osr_entry_loads);
}
}
ir_ref ir_unique_const_addr(ir_ctx *ctx, uintptr_t addr)
{
ir_ref ref = ir_next_const(ctx);
ir_insn *insn = &ctx->ir_base[ref];
insn->optx = IR_OPT(IR_ADDR, IR_ADDR);
insn->val.u64 = addr;
/* don't insert into constants chain */
insn->prev_const = IR_UNUSED;
#if 0
insn->prev_const = ctx->prev_const_chain[IR_ADDR];
ctx->prev_const_chain[IR_ADDR] = ref;
#endif
#if 0
ir_insn *prev_insn, *next_insn;
ir_ref next;
prev_insn = NULL;
next = ctx->prev_const_chain[IR_ADDR];
while (next) {
next_insn = &ctx->ir_base[next];
if (UNEXPECTED(next_insn->val.u64 >= addr)) {
break;
}
prev_insn = next_insn;
next = next_insn->prev_const;
}
if (prev_insn) {
insn->prev_const = prev_insn->prev_const;
prev_insn->prev_const = ref;
} else {
insn->prev_const = ctx->prev_const_chain[IR_ADDR];
ctx->prev_const_chain[IR_ADDR] = ref;
}
#endif
return ref;
}
ir_ref ir_const_ex(ir_ctx *ctx, ir_val val, uint8_t type, uint32_t optx)
{
ir_insn *insn, *prev_insn;
ir_ref ref, prev;
if (type == IR_BOOL) {
return val.u64 ? IR_TRUE : IR_FALSE;
} else if (type == IR_ADDR && val.u64 == 0) {
return IR_NULL;
}
prev_insn = NULL;
ref = ctx->prev_const_chain[type];
while (ref) {
insn = &ctx->ir_base[ref];
if (UNEXPECTED(insn->val.u64 >= val.u64)) {
if (insn->val.u64 == val.u64) {
if (insn->optx == optx) {
return ref;
}
} else {
break;
}
}
prev_insn = insn;
ref = insn->prev_const;
}
if (prev_insn) {
prev = prev_insn->prev_const;
prev_insn->prev_const = -ctx->consts_count;
} else {
prev = ctx->prev_const_chain[type];
ctx->prev_const_chain[type] = -ctx->consts_count;
}
ref = ir_next_const(ctx);
insn = &ctx->ir_base[ref];
insn->prev_const = prev;
insn->optx = optx;
insn->val.u64 = val.u64;
return ref;
}
ir_ref ir_const(ir_ctx *ctx, ir_val val, uint8_t type)
{
return ir_const_ex(ctx, val, type, IR_OPT(type, type));
}
ir_ref ir_const_i8(ir_ctx *ctx, int8_t c)
{
ir_val val;
val.i64 = c;
return ir_const(ctx, val, IR_I8);
}
ir_ref ir_const_i16(ir_ctx *ctx, int16_t c)
{
ir_val val;
val.i64 = c;
return ir_const(ctx, val, IR_I16);
}
ir_ref ir_const_i32(ir_ctx *ctx, int32_t c)
{
ir_val val;
val.i64 = c;
return ir_const(ctx, val, IR_I32);
}
ir_ref ir_const_i64(ir_ctx *ctx, int64_t c)
{
ir_val val;
val.i64 = c;
return ir_const(ctx, val, IR_I64);
}
ir_ref ir_const_u8(ir_ctx *ctx, uint8_t c)
{
ir_val val;
val.u64 = c;
return ir_const(ctx, val, IR_U8);
}
ir_ref ir_const_u16(ir_ctx *ctx, uint16_t c)
{
ir_val val;
val.u64 = c;
return ir_const(ctx, val, IR_U16);
}
ir_ref ir_const_u32(ir_ctx *ctx, uint32_t c)
{
ir_val val;
val.u64 = c;
return ir_const(ctx, val, IR_U32);
}
ir_ref ir_const_u64(ir_ctx *ctx, uint64_t c)
{
ir_val val;
val.u64 = c;
return ir_const(ctx, val, IR_U64);
}
ir_ref ir_const_bool(ir_ctx *ctx, bool c)
{
return (c) ? IR_TRUE : IR_FALSE;
}
ir_ref ir_const_char(ir_ctx *ctx, char c)
{
ir_val val;
val.i64 = c;
return ir_const(ctx, val, IR_CHAR);
}
ir_ref ir_const_float(ir_ctx *ctx, float c)
{
ir_val val;
val.u32_hi = 0;
val.f = c;
return ir_const(ctx, val, IR_FLOAT);
}
ir_ref ir_const_double(ir_ctx *ctx, double c)
{
ir_val val;
val.d = c;
return ir_const(ctx, val, IR_DOUBLE);
}
ir_ref ir_const_addr(ir_ctx *ctx, uintptr_t c)
{
if (c == 0) {
return IR_NULL;
}
ir_val val;
val.u64 = c;
return ir_const(ctx, val, IR_ADDR);
}
ir_ref ir_const_func_addr(ir_ctx *ctx, uintptr_t c, ir_ref proto)
{
if (c == 0) {
return IR_NULL;
}
ir_val val;
val.u64 = c;
IR_ASSERT(proto >= 0 && proto < 0xffff);
return ir_const_ex(ctx, val, IR_ADDR, IR_OPTX(IR_FUNC_ADDR, IR_ADDR, proto));
}
ir_ref ir_const_func(ir_ctx *ctx, ir_ref str, ir_ref proto)
{
ir_val val;
val.u64 = str;
IR_ASSERT(proto >= 0 && proto < 0xffff);
return ir_const_ex(ctx, val, IR_ADDR, IR_OPTX(IR_FUNC, IR_ADDR, proto));
}
ir_ref ir_const_sym(ir_ctx *ctx, ir_ref str)
{
ir_val val;
val.u64 = str;
return ir_const_ex(ctx, val, IR_ADDR, IR_OPTX(IR_SYM, IR_ADDR, 0));
}
ir_ref ir_const_str(ir_ctx *ctx, ir_ref str)
{
ir_val val;
val.u64 = str;
return ir_const_ex(ctx, val, IR_ADDR, IR_OPTX(IR_STR, IR_ADDR, 0));
}
ir_ref ir_str(ir_ctx *ctx, const char *s)
{
size_t len;
if (!ctx->strtab.data) {
ir_strtab_init(&ctx->strtab, 64, 4096);
}
len = strlen(s);
IR_ASSERT(len <= 0xffffffff);
return ir_strtab_lookup(&ctx->strtab, s, (uint32_t)len, ir_strtab_count(&ctx->strtab) + 1);
}
ir_ref ir_strl(ir_ctx *ctx, const char *s, size_t len)
{
if (!ctx->strtab.data) {
ir_strtab_init(&ctx->strtab, 64, 4096);
}
IR_ASSERT(len <= 0xffffffff);
return ir_strtab_lookup(&ctx->strtab, s, (uint32_t)len, ir_strtab_count(&ctx->strtab) + 1);
}
const char *ir_get_str(const ir_ctx *ctx, ir_ref idx)
{
IR_ASSERT(ctx->strtab.data);
return ir_strtab_str(&ctx->strtab, idx - 1);
}
const char *ir_get_strl(const ir_ctx *ctx, ir_ref idx, size_t *len)
{
IR_ASSERT(ctx->strtab.data);
return ir_strtab_strl(&ctx->strtab, idx - 1, len);
}
ir_ref ir_proto_0(ir_ctx *ctx, uint8_t flags, ir_type ret_type)
{
ir_proto_t proto;
proto.flags = flags;
proto.ret_type = ret_type;
proto.params_count = 0;
return ir_strl(ctx, (const char *)&proto, offsetof(ir_proto_t, param_types) + 0);
}
ir_ref ir_proto_1(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1)
{
ir_proto_t proto;
proto.flags = flags;
proto.ret_type = ret_type;
proto.params_count = 1;
proto.param_types[0] = t1;
return ir_strl(ctx, (const char *)&proto, offsetof(ir_proto_t, param_types) + 1);
}
ir_ref ir_proto_2(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2)
{
ir_proto_t proto;
proto.flags = flags;
proto.ret_type = ret_type;
proto.params_count = 2;
proto.param_types[0] = t1;
proto.param_types[1] = t2;
return ir_strl(ctx, (const char *)&proto, offsetof(ir_proto_t, param_types) + 2);
}
ir_ref ir_proto_3(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2, ir_type t3)
{
ir_proto_t proto;
proto.flags = flags;
proto.ret_type = ret_type;
proto.params_count = 3;
proto.param_types[0] = t1;
proto.param_types[1] = t2;
proto.param_types[2] = t3;
return ir_strl(ctx, (const char *)&proto, offsetof(ir_proto_t, param_types) + 3);
}
ir_ref ir_proto_4(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2, ir_type t3,
ir_type t4)
{
ir_proto_t proto;
proto.flags = flags;
proto.ret_type = ret_type;
proto.params_count = 4;
proto.param_types[0] = t1;
proto.param_types[1] = t2;
proto.param_types[2] = t3;
proto.param_types[3] = t4;
return ir_strl(ctx, (const char *)&proto, offsetof(ir_proto_t, param_types) + 4);
}
ir_ref ir_proto_5(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2, ir_type t3,
ir_type t4, ir_type t5)
{
ir_proto_t proto;
proto.flags = flags;
proto.ret_type = ret_type;
proto.params_count = 5;
proto.param_types[0] = t1;
proto.param_types[1] = t2;
proto.param_types[2] = t3;
proto.param_types[3] = t4;
proto.param_types[4] = t5;
return ir_strl(ctx, (const char *)&proto, offsetof(ir_proto_t, param_types) + 5);
}
ir_ref ir_proto(ir_ctx *ctx, uint8_t flags, ir_type ret_type, uint32_t params_count, uint8_t *param_types)
{
ir_proto_t *proto = alloca(offsetof(ir_proto_t, param_types) + params_count);
IR_ASSERT(params_count <= IR_MAX_PROTO_PARAMS);
proto->flags = flags;
proto->ret_type = ret_type;
proto->params_count = params_count;
memcpy(proto->param_types, param_types, params_count);
return ir_strl(ctx, (const char *)proto, offsetof(ir_proto_t, param_types) + params_count);
}
/* IR construction */
ir_ref ir_emit(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3)
{
ir_ref ref = ir_next_insn(ctx);
ir_insn *insn = &ctx->ir_base[ref];
insn->optx = opt;
insn->op1 = op1;
insn->op2 = op2;
insn->op3 = op3;
return ref;
}
ir_ref ir_emit0(ir_ctx *ctx, uint32_t opt)
{
return ir_emit(ctx, opt, IR_UNUSED, IR_UNUSED, IR_UNUSED);
}
ir_ref ir_emit1(ir_ctx *ctx, uint32_t opt, ir_ref op1)
{
return ir_emit(ctx, opt, op1, IR_UNUSED, IR_UNUSED);
}
ir_ref ir_emit2(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2)
{
return ir_emit(ctx, opt, op1, op2, IR_UNUSED);
}
ir_ref ir_emit3(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3)
{
return ir_emit(ctx, opt, op1, op2, op3);
}
static ir_ref _ir_fold_cse(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3)
{
ir_ref ref = ctx->prev_insn_chain[opt & IR_OPT_OP_MASK];
ir_insn *insn;
if (ref) {
ir_ref limit = ctx->fold_cse_limit;
if (op1 > limit) {
limit = op1;
}
if (op2 > limit) {
limit = op2;
}
if (op3 > limit) {
limit = op3;
}
while (ref >= limit) {
insn = &ctx->ir_base[ref];
if (insn->opt == opt && insn->op1 == op1 && insn->op2 == op2 && insn->op3 == op3) {
return ref;
}
if (!insn->prev_insn_offset) {
break;
}
ref = ref - (ir_ref)(uint32_t)insn->prev_insn_offset;
}
}
return IR_UNUSED;
}
#define IR_FOLD(X) IR_FOLD1(X, __LINE__)
#define IR_FOLD1(X, Y) IR_FOLD2(X, Y)
#define IR_FOLD2(X, Y) case IR_RULE_ ## Y:
#define IR_FOLD_ERROR(msg) do { \
IR_ASSERT(0 && (msg)); \
goto ir_fold_emit; \
} while (0)
#define IR_FOLD_CONST_U(_val) do { \
val.u64 = (_val); \
goto ir_fold_const; \
} while (0)
#define IR_FOLD_CONST_I(_val) do { \
val.i64 = (_val); \
goto ir_fold_const; \
} while (0)
#define IR_FOLD_CONST_D(_val) do { \
val.d = (_val); \
goto ir_fold_const; \
} while (0)
#define IR_FOLD_CONST_F(_val) do { \
val.f = (_val); \
val.u32_hi = 0; \
goto ir_fold_const; \
} while (0)
#define IR_FOLD_COPY(op) do { \
ref = (op); \
goto ir_fold_copy; \
} while (0)
#define IR_FOLD_BOOL(cond) \
IR_FOLD_COPY((cond) ? IR_TRUE : IR_FALSE)
#define IR_FOLD_NAMED(name) ir_fold_ ## name:
#define IR_FOLD_DO_NAMED(name) goto ir_fold_ ## name
#define IR_FOLD_RESTART goto ir_fold_restart
#define IR_FOLD_CSE goto ir_fold_cse
#define IR_FOLD_EMIT goto ir_fold_emit
#define IR_FOLD_NEXT break
#include "ir_fold_hash.h"
#define IR_FOLD_RULE(x) ((x) >> 21)
#define IR_FOLD_KEY(x) ((x) & 0x1fffff)
/*
* key = insn->op | (insn->op1->op << 7) | (insn->op2->op << 14)
*
* ANY and UNUSED ops are represented by 0
*/
ir_ref ir_folding(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3, ir_insn *op1_insn, ir_insn *op2_insn, ir_insn *op3_insn)
{
uint8_t op;
ir_ref ref;
ir_val val;
uint32_t key, any;
(void) op3_insn;
restart:
key = (opt & IR_OPT_OP_MASK) + ((uint32_t)op1_insn->op << 7) + ((uint32_t)op2_insn->op << 14);
any = 0x1fffff;
do {
uint32_t k = key & any;
uint32_t h = _ir_fold_hashkey(k);
uint32_t fh = _ir_fold_hash[h];
if (IR_FOLD_KEY(fh) == k
#ifdef IR_FOLD_SEMI_PERFECT_HASH
|| (fh = _ir_fold_hash[h+1], (fh & 0x1fffff) == k)
#endif
) {
switch (IR_FOLD_RULE(fh)) {
#include "ir_fold.h"
default:
break;
}
}
if (any == 0x7f) {
/* All parrerns are checked. Pass on to CSE. */
goto ir_fold_cse;
}
/* op2/op1/op op2/_/op _/op1/op _/_/op
* 0x1fffff -> 0x1fc07f -> 0x003fff -> 0x00007f
* from masks to bis: 11 -> 10 -> 01 -> 00
*
* a b => x y
* 1 1 1 0
* 1 0 0 1
* 0 1 0 0
*
* x = a & b; y = !b
*/
any = ((any & (any << 7)) & 0x1fc000) | (~any & 0x3f80) | 0x7f;
} while (1);
ir_fold_restart:
if (!(ctx->flags2 & IR_OPT_IN_SCCP)) {
op1_insn = ctx->ir_base + op1;
op2_insn = ctx->ir_base + op2;
op3_insn = ctx->ir_base + op3;
goto restart;
} else {
ctx->fold_insn.optx = opt;
ctx->fold_insn.op1 = op1;
ctx->fold_insn.op2 = op2;
ctx->fold_insn.op3 = op3;
return IR_FOLD_DO_RESTART;
}
ir_fold_cse:
if (!(ctx->flags2 & IR_OPT_IN_SCCP)) {
/* Local CSE */
ref = _ir_fold_cse(ctx, opt, op1, op2, op3);
if (ref) {
return ref;
}
ref = ir_emit(ctx, opt, op1, op2, op3);
/* Update local CSE chain */
op = opt & IR_OPT_OP_MASK;
ir_ref prev = ctx->prev_insn_chain[op];
ir_insn *insn = ctx->ir_base + ref;
if (!prev || ref - prev > 0xffff) {
/* can't fit into 16-bit */
insn->prev_insn_offset = 0;
} else {
insn->prev_insn_offset = ref - prev;
}
ctx->prev_insn_chain[op] = ref;
return ref;
}
ir_fold_emit: