forked from dstogov/ir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir_private.h
1378 lines (1138 loc) · 35.2 KB
/
ir_private.h
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
* (Common data structures and non public definitions)
* Copyright (C) 2022 Zend by Perforce.
* Authors: Dmitry Stogov <[email protected]>
*/
#ifndef IR_PRIVATE_H
#define IR_PRIVATE_H
#include <string.h>
#include <stdlib.h>
#ifdef IR_DEBUG
# include <assert.h>
# define IR_ASSERT(x) assert(x)
#else
# define IR_ASSERT(x)
#endif
#ifdef _WIN32
# include <intrin.h>
# ifdef _M_X64
# pragma intrinsic(_BitScanForward64)
# pragma intrinsic(_BitScanReverse64)
# endif
# pragma intrinsic(_BitScanForward)
# pragma intrinsic(_BitScanReverse)
#endif
#ifdef __has_builtin
# if __has_builtin(__builtin_expect)
# define EXPECTED(condition) __builtin_expect(!!(condition), 1)
# define UNEXPECTED(condition) __builtin_expect(!!(condition), 0)
# endif
# if __has_attribute(__aligned__)
# define IR_SET_ALIGNED(alignment, decl) decl __attribute__ ((__aligned__ (alignment)))
# endif
# if __has_attribute(__fallthrough__)
# define IR_FALLTHROUGH __attribute__((__fallthrough__))
# endif
#elif defined(_WIN32)
# define IR_SET_ALIGNED(alignment, decl) __declspec(align(alignment)) decl
#else /* GCC prior to 10 or non-clang/msvc compilers */
#define __has_builtin(x) 0
#endif
#ifndef EXPECTED
# define EXPECTED(condition) (condition)
# define UNEXPECTED(condition) (condition)
#endif
#ifndef IR_SET_ALIGNED
# define IR_SET_ALIGNED(alignment, decl) decl
#endif
#ifndef IR_FALLTHROUGH
# define IR_FALLTHROUGH ((void)0)
#endif
/*** Helper routines ***/
#define IR_ALIGNED_SIZE(size, alignment) \
(((size) + ((alignment) - 1)) & ~((alignment) - 1))
#define IR_MAX(a, b) (((a) > (b)) ? (a) : (b))
#define IR_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define IR_IS_POWER_OF_TWO(x) (!((x) & ((x) - 1)))
#define IR_LOG2(x) ir_ntzl(x)
IR_ALWAYS_INLINE uint8_t ir_rol8(uint8_t op1, uint8_t op2)
{
return (op1 << op2) | (op1 >> (8 - op2));
}
IR_ALWAYS_INLINE uint16_t ir_rol16(uint16_t op1, uint16_t op2)
{
return (op1 << op2) | (op1 >> (16 - op2));
}
IR_ALWAYS_INLINE uint32_t ir_rol32(uint32_t op1, uint32_t op2)
{
return (op1 << op2) | (op1 >> (32 - op2));
}
IR_ALWAYS_INLINE uint64_t ir_rol64(uint64_t op1, uint64_t op2)
{
return (op1 << op2) | (op1 >> (64 - op2));
}
IR_ALWAYS_INLINE uint8_t ir_ror8(uint8_t op1, uint8_t op2)
{
return (op1 >> op2) | (op1 << (8 - op2));
}
IR_ALWAYS_INLINE uint16_t ir_ror16(uint16_t op1, uint16_t op2)
{
return (op1 >> op2) | (op1 << (16 - op2));
}
IR_ALWAYS_INLINE uint32_t ir_ror32(uint32_t op1, uint32_t op2)
{
return (op1 >> op2) | (op1 << (32 - op2));
}
IR_ALWAYS_INLINE uint64_t ir_ror64(uint64_t op1, uint64_t op2)
{
return (op1 >> op2) | (op1 << (64 - op2));
}
/* Number of trailing zero bits (0x01 -> 0; 0x40 -> 6; 0x00 -> LEN) */
IR_ALWAYS_INLINE uint32_t ir_ntz(uint32_t num)
{
#if (defined(__GNUC__) || __has_builtin(__builtin_ctz))
return __builtin_ctz(num);
#elif defined(_WIN32)
uint32_t index;
if (!_BitScanForward(&index, num)) {
/* undefined behavior */
return 32;
}
return index;
#else
int n;
if (num == 0) return 32;
n = 1;
if ((num & 0x0000ffff) == 0) {n += 16; num = num >> 16;}
if ((num & 0x000000ff) == 0) {n += 8; num = num >> 8;}
if ((num & 0x0000000f) == 0) {n += 4; num = num >> 4;}
if ((num & 0x00000003) == 0) {n += 2; num = num >> 2;}
return n - (num & 1);
#endif
}
/* Number of trailing zero bits (0x01 -> 0; 0x40 -> 6; 0x00 -> LEN) */
IR_ALWAYS_INLINE uint32_t ir_ntzl(uint64_t num)
{
#if (defined(__GNUC__) || __has_builtin(__builtin_ctzl))
return __builtin_ctzl(num);
#elif defined(_WIN64)
unsigned long index;
if (!_BitScanForward64(&index, num)) {
/* undefined behavior */
return 64;
}
return (uint32_t) index;
#else
uint32_t n;
if (num == 0) return 64;
n = 1;
if ((num & 0xffffffff) == 0) {n += 32; num = num >> 32;}
if ((num & 0x0000ffff) == 0) {n += 16; num = num >> 16;}
if ((num & 0x000000ff) == 0) {n += 8; num = num >> 8;}
if ((num & 0x0000000f) == 0) {n += 4; num = num >> 4;}
if ((num & 0x00000003) == 0) {n += 2; num = num >> 2;}
return n - (uint32_t)(num & 1);
#endif
}
/* Number of leading zero bits (Undefined for zero) */
IR_ALWAYS_INLINE int ir_nlz(uint32_t num)
{
#if (defined(__GNUC__) || __has_builtin(__builtin_clz))
return __builtin_clz(num);
#elif defined(_WIN32)
uint32_t index;
if (!_BitScanReverse(&index, num)) {
/* undefined behavior */
return 32;
}
return (int) (32 - 1) - index;
#else
uint32_t x;
uint32_t n;
n = 32;
x = num >> 16; if (x != 0) {n -= 16; num = x;}
x = num >> 8; if (x != 0) {n -= 8; num = x;}
x = num >> 4; if (x != 0) {n -= 4; num = x;}
x = num >> 2; if (x != 0) {n -= 2; num = x;}
x = num >> 1; if (x != 0) return n - 2;
return n - num;
#endif
}
IR_ALWAYS_INLINE int ir_nlzl(uint64_t num)
{
#if (defined(__GNUC__) || __has_builtin(__builtin_clzll))
return __builtin_clzll(num);
#elif defined(_WIN64)
unsigned long index;
if (!_BitScanReverse64(&index, num)) {
/* undefined behavior */
return 64;
}
return (int) (64 - 1) - index;
#else
uint64_t x;
uint32_t n;
n = 64;
x = num >> 32; if (x != 0) {n -= 32; num = x;}
x = num >> 16; if (x != 0) {n -= 16; num = x;}
x = num >> 8; if (x != 0) {n -= 8; num = x;}
x = num >> 4; if (x != 0) {n -= 4; num = x;}
x = num >> 2; if (x != 0) {n -= 2; num = x;}
x = num >> 1; if (x != 0) return n - 2;
return n - (uint32_t)num;
#endif
}
/*** Helper data types ***/
/* Arena */
struct _ir_arena {
char *ptr;
char *end;
ir_arena *prev;
};
IR_ALWAYS_INLINE ir_arena* ir_arena_create(size_t size)
{
ir_arena *arena;
IR_ASSERT(size >= IR_ALIGNED_SIZE(sizeof(ir_arena), 8));
arena = (ir_arena*)ir_mem_malloc(size);
arena->ptr = (char*) arena + IR_ALIGNED_SIZE(sizeof(ir_arena), 8);
arena->end = (char*) arena + size;
arena->prev = NULL;
return arena;
}
IR_ALWAYS_INLINE void ir_arena_free(ir_arena *arena)
{
do {
ir_arena *prev = arena->prev;
ir_mem_free(arena);
arena = prev;
} while (arena);
}
IR_ALWAYS_INLINE void* ir_arena_alloc(ir_arena **arena_ptr, size_t size)
{
ir_arena *arena = *arena_ptr;
char *ptr = arena->ptr;
size = IR_ALIGNED_SIZE(size, 8);
if (EXPECTED(size <= (size_t)(arena->end - ptr))) {
arena->ptr = ptr + size;
} else {
size_t arena_size =
UNEXPECTED((size + IR_ALIGNED_SIZE(sizeof(ir_arena), 8)) > (size_t)(arena->end - (char*) arena)) ?
(size + IR_ALIGNED_SIZE(sizeof(ir_arena), 8)) :
(size_t)(arena->end - (char*) arena);
ir_arena *new_arena = (ir_arena*)ir_mem_malloc(arena_size);
ptr = (char*) new_arena + IR_ALIGNED_SIZE(sizeof(ir_arena), 8);
new_arena->ptr = (char*) new_arena + IR_ALIGNED_SIZE(sizeof(ir_arena), 8) + size;
new_arena->end = (char*) new_arena + arena_size;
new_arena->prev = arena;
*arena_ptr = new_arena;
}
return (void*) ptr;
}
IR_ALWAYS_INLINE void* ir_arena_checkpoint(ir_arena *arena)
{
return arena->ptr;
}
IR_ALWAYS_INLINE void ir_release(ir_arena **arena_ptr, void *checkpoint)
{
ir_arena *arena = *arena_ptr;
while (UNEXPECTED((char*)checkpoint > arena->end) ||
UNEXPECTED((char*)checkpoint <= (char*)arena)) {
ir_arena *prev = arena->prev;
ir_mem_free(arena);
*arena_ptr = arena = prev;
}
IR_ASSERT((char*)checkpoint > (char*)arena && (char*)checkpoint <= arena->end);
arena->ptr = (char*)checkpoint;
}
/* Bitsets */
#if defined(IR_TARGET_X86)
# define IR_BITSET_BITS 32
# define IR_BITSET_ONE 1U
# define ir_bitset_base_t uint32_t
# define ir_bitset_ntz ir_ntz
#else
# define IR_BITSET_BITS 64
# ifdef _M_X64 /* MSVC*/
# define IR_BITSET_ONE 1ui64
# else
# define IR_BITSET_ONE 1UL
# endif
# define ir_bitset_base_t uint64_t
# define ir_bitset_ntz ir_ntzl
#endif
typedef ir_bitset_base_t *ir_bitset;
IR_ALWAYS_INLINE uint32_t ir_bitset_len(uint32_t n)
{
return (n + (IR_BITSET_BITS - 1)) / IR_BITSET_BITS;
}
IR_ALWAYS_INLINE ir_bitset ir_bitset_malloc(uint32_t n)
{
return ir_mem_calloc(ir_bitset_len(n), IR_BITSET_BITS / 8);
}
IR_ALWAYS_INLINE void ir_bitset_incl(ir_bitset set, uint32_t n)
{
set[n / IR_BITSET_BITS] |= IR_BITSET_ONE << (n % IR_BITSET_BITS);
}
IR_ALWAYS_INLINE void ir_bitset_excl(ir_bitset set, uint32_t n)
{
set[n / IR_BITSET_BITS] &= ~(IR_BITSET_ONE << (n % IR_BITSET_BITS));
}
IR_ALWAYS_INLINE bool ir_bitset_in(const ir_bitset set, uint32_t n)
{
return (set[(n / IR_BITSET_BITS)] & (IR_BITSET_ONE << (n % IR_BITSET_BITS))) != 0;
}
IR_ALWAYS_INLINE void ir_bitset_clear(ir_bitset set, uint32_t len)
{
memset(set, 0, len * (IR_BITSET_BITS / 8));
}
IR_ALWAYS_INLINE void ir_bitset_fill(ir_bitset set, uint32_t len)
{
memset(set, 0xff, len * (IR_BITSET_BITS / 8));
}
IR_ALWAYS_INLINE bool ir_bitset_empty(const ir_bitset set, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++) {
if (set[i]) {
return 0;
}
}
return 1;
}
IR_ALWAYS_INLINE bool ir_bitset_equal(const ir_bitset set1, const ir_bitset set2, uint32_t len)
{
return memcmp(set1, set2, len * (IR_BITSET_BITS / 8)) == 0;
}
IR_ALWAYS_INLINE void ir_bitset_copy(ir_bitset set1, const ir_bitset set2, uint32_t len)
{
memcpy(set1, set2, len * (IR_BITSET_BITS / 8));
}
IR_ALWAYS_INLINE void ir_bitset_intersection(ir_bitset set1, const ir_bitset set2, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++) {
set1[i] &= set2[i];
}
}
IR_ALWAYS_INLINE void ir_bitset_union(ir_bitset set1, const ir_bitset set2, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++) {
set1[i] |= set2[i];
}
}
IR_ALWAYS_INLINE void ir_bitset_difference(ir_bitset set1, const ir_bitset set2, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++) {
set1[i] = set1[i] & ~set2[i];
}
}
IR_ALWAYS_INLINE bool ir_bitset_is_subset(const ir_bitset set1, const ir_bitset set2, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++) {
if (set1[i] & ~set2[i]) {
return 0;
}
}
return 1;
}
IR_ALWAYS_INLINE int ir_bitset_first(const ir_bitset set, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++) {
if (set[i]) {
return IR_BITSET_BITS * i + ir_bitset_ntz(set[i]);
}
}
return -1; /* empty set */
}
IR_ALWAYS_INLINE int ir_bitset_last(const ir_bitset set, uint32_t len)
{
uint32_t i = len;
while (i > 0) {
i--;
if (set[i]) {
uint32_t j = IR_BITSET_BITS * i - 1;
ir_bitset_base_t x = set[i];
do {
x = x >> 1;
j++;
} while (x != 0);
return j;
}
}
return -1; /* empty set */
}
IR_ALWAYS_INLINE int ir_bitset_pop_first(ir_bitset set, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++) {
ir_bitset_base_t x = set[i];
if (x) {
int bit = IR_BITSET_BITS * i + ir_bitset_ntz(x);
set[i] = x & (x - 1);
return bit;
}
}
return -1; /* empty set */
}
#define IR_BITSET_FOREACH(set, len, bit) do { \
ir_bitset _set = (set); \
uint32_t _i, _len = (len); \
for (_i = 0; _i < _len; _set++, _i++) { \
ir_bitset_base_t _x = *_set; \
while (_x) { \
(bit) = IR_BITSET_BITS * _i + ir_bitset_ntz(_x); \
_x &= _x - 1;
#define IR_BITSET_FOREACH_DIFFERENCE(set1, set2, len, bit) do { \
ir_bitset _set1 = (set1); \
ir_bitset _set2 = (set2); \
uint32_t _i, _len = (len); \
for (_i = 0; _i < _len; _i++) { \
ir_bitset_base_t _x = _set1[_i] & ~_set2[_i]; \
while (_x) { \
(bit) = IR_BITSET_BITS * _i + ir_bitset_ntz(_x); \
_x &= _x - 1;
#define IR_BITSET_FOREACH_END() \
} \
} \
} while (0)
/* Sparse Set */
typedef struct _ir_sparse_set {
uint32_t size;
uint32_t len;
uint32_t *data;
} ir_sparse_set;
#define IR_SPARSE_SET_DENSE(set, n) (set)->data[n]
#define IR_SPARSE_SET_SPARSE(set, n) (set)->data[-1 - ((int32_t)(n))]
IR_ALWAYS_INLINE void ir_sparse_set_init(ir_sparse_set *set, uint32_t size)
{
set->size = size;
set->len = 0;
set->data = (uint32_t*)ir_mem_malloc(sizeof(uint32_t) * 2 * size) + size;
#ifdef IR_DEBUG
/* initialize sparse part to avoid valgrind warnings */
memset(&IR_SPARSE_SET_SPARSE(set, size - 1), 0, size * sizeof(uint32_t));
#endif
}
IR_ALWAYS_INLINE void ir_sparse_set_clear(ir_sparse_set *set)
{
set->len = 0;
}
IR_ALWAYS_INLINE void ir_sparse_set_free(ir_sparse_set *set)
{
ir_mem_free(set->data - set->size);
}
IR_ALWAYS_INLINE bool ir_sparse_set_empty(const ir_sparse_set *set)
{
return set->len == 0;
}
IR_ALWAYS_INLINE bool ir_sparse_set_in(const ir_sparse_set *set, uint32_t n)
{
uint32_t idx = IR_SPARSE_SET_SPARSE(set, n);
return idx < set->len && IR_SPARSE_SET_DENSE(set, idx) == n;
}
IR_ALWAYS_INLINE void ir_sparse_set_add(ir_sparse_set *set, uint32_t n)
{
uint32_t idx;
IR_ASSERT(!ir_sparse_set_in(set, n));
idx = set->len++;
IR_SPARSE_SET_DENSE(set, idx) = n;
IR_SPARSE_SET_SPARSE(set, n) = idx;
}
IR_ALWAYS_INLINE void ir_sparse_set_del(ir_sparse_set *set, uint32_t n)
{
uint32_t last;
IR_ASSERT(ir_sparse_set_in(set, n));
last = IR_SPARSE_SET_DENSE(set, set->len - 1);
if (last != n) {
uint32_t idx = IR_SPARSE_SET_SPARSE(set, n);
IR_SPARSE_SET_DENSE(set, idx) = last;
IR_SPARSE_SET_SPARSE(set, last) = idx;
}
set->len--;
}
IR_ALWAYS_INLINE uint32_t ir_sparse_set_pop(ir_sparse_set *set)
{
if (set->len > 0) {
set->len--;
return IR_SPARSE_SET_DENSE(set, set->len);
}
return -1; /* empty set */
}
#define IR_SPARSE_SET_FOREACH(set, bit) do { \
ir_sparse_set *_set = (set); \
uint32_t _i, _len = _set->len; \
uint32_t *_p = _set->data; \
for (_i = 0; _i < _len; _p++, _i++) { \
(bit) = *_p; \
#define IR_SPARSE_SET_FOREACH_END() \
} \
} while (0)
/* Bit Queue */
typedef struct _ir_bitqueue {
uint32_t len;
uint32_t pos;
ir_bitset set;
} ir_bitqueue;
IR_ALWAYS_INLINE void ir_bitqueue_init(ir_bitqueue *q, uint32_t n)
{
q->len = ir_bitset_len(n);
q->pos = q->len - 1;
q->set = ir_bitset_malloc(n);
}
IR_ALWAYS_INLINE void ir_bitqueue_grow(ir_bitqueue *q, uint32_t n)
{
uint32_t len = ir_bitset_len(n);
IR_ASSERT(len >= q->len);
if (len > q->len) {
q->set = ir_mem_realloc(q->set, len * (IR_BITSET_BITS / 8));
memset(q->set + q->len, 0, (len - q->len) * (IR_BITSET_BITS / 8));
q->len = len;
}
}
IR_ALWAYS_INLINE void ir_bitqueue_free(ir_bitqueue *q)
{
ir_mem_free(q->set);
}
IR_ALWAYS_INLINE void ir_bitqueue_clear(ir_bitqueue *q)
{
q->pos = q->len - 1;
ir_bitset_clear(q->set, q->len);
}
IR_ALWAYS_INLINE int ir_bitqueue_pop(ir_bitqueue *q)
{
uint32_t i = q->pos;
ir_bitset_base_t x, *p = q->set + i;
do {
x = *p;
if (x) {
int bit = IR_BITSET_BITS * i + ir_bitset_ntz(x);
*p = x & (x - 1);
q->pos = i;
return bit;
}
p++;
i++;
} while (i < q->len);
q->pos = q->len - 1;
return -1; /* empty set */
}
IR_ALWAYS_INLINE void ir_bitqueue_add(ir_bitqueue *q, uint32_t n)
{
uint32_t i = n / IR_BITSET_BITS;
q->set[i] |= IR_BITSET_ONE << (n % IR_BITSET_BITS);
if (i < q->pos) {
q->pos = i;
}
}
IR_ALWAYS_INLINE void ir_bitqueue_del(ir_bitqueue *q, uint32_t n)
{
ir_bitset_excl(q->set, n);
}
IR_ALWAYS_INLINE bool ir_bitqueue_in(const ir_bitqueue *q, uint32_t n)
{
return ir_bitset_in(q->set, n);
}
/* Dynamic array of numeric references */
typedef struct _ir_array {
ir_ref *refs;
uint32_t size;
} ir_array;
void ir_array_grow(ir_array *a, uint32_t size);
void ir_array_insert(ir_array *a, uint32_t i, ir_ref val);
void ir_array_remove(ir_array *a, uint32_t i);
IR_ALWAYS_INLINE void ir_array_init(ir_array *a, uint32_t size)
{
a->refs = ir_mem_malloc(size * sizeof(ir_ref));
a->size = size;
}
IR_ALWAYS_INLINE void ir_array_free(ir_array *a)
{
ir_mem_free(a->refs);
a->refs = NULL;
a->size = 0;
}
IR_ALWAYS_INLINE uint32_t ir_array_size(const ir_array *a)
{
return a->size;
}
IR_ALWAYS_INLINE ir_ref ir_array_get(const ir_array *a, uint32_t i)
{
return (i < a->size) ? a->refs[i] : IR_UNUSED;
}
IR_ALWAYS_INLINE ir_ref ir_array_at(const ir_array *a, uint32_t i)
{
IR_ASSERT(i < a->size);
return a->refs[i];
}
IR_ALWAYS_INLINE void ir_array_set(ir_array *a, uint32_t i, ir_ref val)
{
if (i >= a->size) {
ir_array_grow(a, i + 1);
}
a->refs[i] = val;
}
IR_ALWAYS_INLINE void ir_array_set_unchecked(ir_array *a, uint32_t i, ir_ref val)
{
IR_ASSERT(i < a->size);
a->refs[i] = val;
}
/* List/Stack of numeric references */
typedef struct _ir_list {
ir_array a;
uint32_t len;
} ir_list;
bool ir_list_contains(const ir_list *l, ir_ref val);
void ir_list_insert(ir_list *l, uint32_t i, ir_ref val);
void ir_list_remove(ir_list *l, uint32_t i);
IR_ALWAYS_INLINE void ir_list_init(ir_list *l, uint32_t size)
{
ir_array_init(&l->a, size);
l->len = 0;
}
IR_ALWAYS_INLINE void ir_list_free(ir_list *l)
{
ir_array_free(&l->a);
l->len = 0;
}
IR_ALWAYS_INLINE void ir_list_clear(ir_list *l)
{
l->len = 0;
}
IR_ALWAYS_INLINE uint32_t ir_list_len(const ir_list *l)
{
return l->len;
}
IR_ALWAYS_INLINE uint32_t ir_list_capasity(const ir_list *l)
{
return ir_array_size(&l->a);
}
IR_ALWAYS_INLINE void ir_list_push(ir_list *l, ir_ref val)
{
ir_array_set(&l->a, l->len++, val);
}
IR_ALWAYS_INLINE void ir_list_push_unchecked(ir_list *l, ir_ref val)
{
ir_array_set_unchecked(&l->a, l->len++, val);
}
IR_ALWAYS_INLINE ir_ref ir_list_pop(ir_list *l)
{
IR_ASSERT(l->len > 0);
return ir_array_at(&l->a, --l->len);
}
IR_ALWAYS_INLINE ir_ref ir_list_peek(const ir_list *l)
{
IR_ASSERT(l->len > 0);
return ir_array_at(&l->a, l->len - 1);
}
IR_ALWAYS_INLINE ir_ref ir_list_at(const ir_list *l, uint32_t i)
{
IR_ASSERT(i < l->len);
return ir_array_at(&l->a, i);
}
IR_ALWAYS_INLINE void ir_list_set(ir_list *l, uint32_t i, ir_ref val)
{
IR_ASSERT(i < l->len);
ir_array_set_unchecked(&l->a, i, val);
}
/* Worklist (unique list) */
typedef struct _ir_worklist {
ir_list l;
ir_bitset visited;
} ir_worklist;
IR_ALWAYS_INLINE void ir_worklist_init(ir_worklist *w, uint32_t size)
{
ir_list_init(&w->l, size);
w->visited = ir_bitset_malloc(size);
}
IR_ALWAYS_INLINE void ir_worklist_free(ir_worklist *w)
{
ir_list_free(&w->l);
ir_mem_free(w->visited);
}
IR_ALWAYS_INLINE uint32_t ir_worklist_len(const ir_worklist *w)
{
return ir_list_len(&w->l);
}
IR_ALWAYS_INLINE uint32_t ir_worklist_capasity(const ir_worklist *w)
{
return ir_list_capasity(&w->l);
}
IR_ALWAYS_INLINE void ir_worklist_clear(ir_worklist *w)
{
ir_list_clear(&w->l);
ir_bitset_clear(w->visited, ir_bitset_len(ir_worklist_capasity(w)));
}
IR_ALWAYS_INLINE bool ir_worklist_push(ir_worklist *w, ir_ref val)
{
IR_ASSERT(val >= 0 && (uint32_t)val < ir_worklist_capasity(w));
if (ir_bitset_in(w->visited, val)) {
return 0;
}
ir_bitset_incl(w->visited, val);
IR_ASSERT(ir_list_len(&w->l) < ir_list_capasity(&w->l));
ir_list_push_unchecked(&w->l, val);
return 1;
}
IR_ALWAYS_INLINE ir_ref ir_worklist_pop(ir_worklist *w)
{
return ir_list_pop(&w->l);
}
IR_ALWAYS_INLINE ir_ref ir_worklist_peek(const ir_worklist *w)
{
return ir_list_peek(&w->l);
}
/* IR Hash Table */
#define IR_INVALID_IDX 0xffffffff
#define IR_INVALID_VAL 0x80000000
typedef struct _ir_hashtab_bucket {
uint32_t key;
ir_ref val;
uint32_t next;
} ir_hashtab_bucket;
typedef struct _ir_hashtab {
void *data;
uint32_t mask;
uint32_t size;
uint32_t count;
uint32_t pos;
} ir_hashtab;
void ir_hashtab_init(ir_hashtab *tab, uint32_t size);
void ir_hashtab_free(ir_hashtab *tab);
ir_ref ir_hashtab_find(const ir_hashtab *tab, uint32_t key);
bool ir_hashtab_add(ir_hashtab *tab, uint32_t key, ir_ref val);
void ir_hashtab_key_sort(ir_hashtab *tab);
/* IR Addr Table */
typedef struct _ir_addrtab_bucket {
uint64_t key;
ir_ref val;
uint32_t next;
} ir_addrtab_bucket;
void ir_addrtab_init(ir_hashtab *tab, uint32_t size);
void ir_addrtab_free(ir_hashtab *tab);
ir_ref ir_addrtab_find(const ir_hashtab *tab, uint64_t key);
void ir_addrtab_set(ir_hashtab *tab, uint64_t key, ir_ref val);
/*** IR OP info ***/
extern const uint8_t ir_type_flags[IR_LAST_TYPE];
extern const char *ir_type_name[IR_LAST_TYPE];
extern const char *ir_type_cname[IR_LAST_TYPE];
extern const uint8_t ir_type_size[IR_LAST_TYPE];
extern const uint32_t ir_op_flags[IR_LAST_OP];
extern const char *ir_op_name[IR_LAST_OP];
void ir_print_escaped_str(const char *s, size_t len, FILE *f);
#define IR_IS_CONST_OP(op) ((op) > IR_NOP && (op) <= IR_C_FLOAT)
#define IR_IS_FOLDABLE_OP(op) ((op) <= IR_LAST_FOLDABLE_OP)
#define IR_IS_SYM_CONST(op) ((op) == IR_STR || (op) == IR_SYM || (op) == IR_FUNC)
ir_ref ir_const_ex(ir_ctx *ctx, ir_val val, uint8_t type, uint32_t optx);
IR_ALWAYS_INLINE bool ir_const_is_true(const ir_insn *v)
{
if (IR_IS_SYM_CONST(v->op)) {
return 1;
} else if (v->type == IR_BOOL) {
return v->val.b;
} else if (IR_IS_TYPE_INT(v->type)) {
return v->val.i64 != 0;
} else if (v->type == IR_DOUBLE) {
return v->val.d != 0.0;
} else {
IR_ASSERT(v->type == IR_FLOAT);
return v->val.f != 0.0;
}
return 0;
}
IR_ALWAYS_INLINE bool ir_ref_is_true(ir_ctx *ctx, ir_ref ref)
{
if (ref == IR_TRUE) {
return 1;
} else if (ref == IR_FALSE) {
return 0;
} else {
IR_ASSERT(IR_IS_CONST_REF(ref));
return ir_const_is_true(&ctx->ir_base[ref]);
}
}
/* IR OP flags */
#define IR_OP_FLAG_OPERANDS_SHIFT 3
#define IR_OP_FLAG_EDGES_MASK 0x03
#define IR_OP_FLAG_VAR_INPUTS 0x04
#define IR_OP_FLAG_OPERANDS_MASK 0x18
#define IR_OP_FLAG_MEM_MASK ((1<<6)|(1<<7))
#define IR_OP_FLAG_DATA (1<<8)
#define IR_OP_FLAG_CONTROL (1<<9)
#define IR_OP_FLAG_MEM (1<<10)
#define IR_OP_FLAG_COMMUTATIVE (1<<11)
#define IR_OP_FLAG_BB_START (1<<12)
#define IR_OP_FLAG_BB_END (1<<13)
#define IR_OP_FLAG_TERMINATOR (1<<14)
#define IR_OP_FLAG_PINNED (1<<15)
#define IR_OP_FLAG_MEM_LOAD ((0<<6)|(0<<7))
#define IR_OP_FLAG_MEM_STORE ((0<<6)|(1<<7))
#define IR_OP_FLAG_MEM_CALL ((1<<6)|(0<<7))
#define IR_OP_FLAG_MEM_ALLOC ((1<<6)|(1<<7))
#define IR_OP_FLAG_MEM_MASK ((1<<6)|(1<<7))
#define IR_OPND_UNUSED 0x0
#define IR_OPND_DATA 0x1
#define IR_OPND_CONTROL 0x2
#define IR_OPND_CONTROL_DEP 0x3
#define IR_OPND_CONTROL_REF 0x4
#define IR_OPND_STR 0x5
#define IR_OPND_NUM 0x6
#define IR_OPND_PROB 0x7
#define IR_OPND_PROTO 0x8
#define IR_OP_FLAGS(op_flags, op1_flags, op2_flags, op3_flags) \
((op_flags) | ((op1_flags) << 20) | ((op2_flags) << 24) | ((op3_flags) << 28))
#define IR_INPUT_EDGES_COUNT(flags) (flags & IR_OP_FLAG_EDGES_MASK)
#define IR_OPERANDS_COUNT(flags) ((flags & IR_OP_FLAG_OPERANDS_MASK) >> IR_OP_FLAG_OPERANDS_SHIFT)
#define IR_OP_HAS_VAR_INPUTS(flags) ((flags) & IR_OP_FLAG_VAR_INPUTS)
#define IR_OPND_KIND(flags, i) \
(((flags) >> (16 + (4 * (((i) > 3) ? 3 : (i))))) & 0xf)
#define IR_IS_REF_OPND_KIND(kind) \
((kind) >= IR_OPND_DATA && (kind) <= IR_OPND_CONTROL_REF)
IR_ALWAYS_INLINE ir_ref ir_operands_count(const ir_ctx *ctx, const ir_insn *insn)
{
uint32_t flags = ir_op_flags[insn->op];
uint32_t n = IR_OPERANDS_COUNT(flags);
if (UNEXPECTED(IR_OP_HAS_VAR_INPUTS(flags))) {
/* MERGE, PHI, CALL, etc */
n = insn->inputs_count;
}
return n;
}
IR_ALWAYS_INLINE ir_ref ir_input_edges_count(const ir_ctx *ctx, const ir_insn *insn)
{
uint32_t flags = ir_op_flags[insn->op];
uint32_t n = IR_INPUT_EDGES_COUNT(flags);
if (UNEXPECTED(IR_OP_HAS_VAR_INPUTS(flags))) {
/* MERGE, PHI, CALL, etc */
n = insn->inputs_count;
}
return n;
}
IR_ALWAYS_INLINE uint32_t ir_insn_inputs_to_len(uint32_t inputs_count)
{
return 1 + (inputs_count >> 2);
}
IR_ALWAYS_INLINE uint32_t ir_insn_len(const ir_insn *insn)
{
return ir_insn_inputs_to_len(insn->inputs_count);
}
/*** IR Context Private Flags (ir_ctx->flags2) ***/
#define IR_CFG_HAS_LOOPS (1<<0)
#define IR_IRREDUCIBLE_CFG (1<<1)
#define IR_HAS_ALLOCA (1<<2)
#define IR_HAS_CALLS (1<<3)
#define IR_OPT_IN_SCCP (1<<4)
#define IR_LINEAR (1<<5)
#define IR_HAS_VA_START (1<<6)
#define IR_HAS_VA_COPY (1<<7)
#define IR_HAS_VA_ARG_GP (1<<8)
#define IR_HAS_VA_ARG_FP (1<<9)
#define IR_HAS_FP_RET_SLOT (1<<10)
#define IR_16B_FRAME_ALIGNMENT (1<<11)
/* Temporary: SCCP -> CFG */