-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.h
821 lines (630 loc) · 19.7 KB
/
ir.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
#ifndef IR_IR_H
#define IR_IR_H
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>
enum ir_op_ty {
IR_OP_TY_UNKNOWN,
IR_OP_TY_PHI,
IR_OP_TY_UNDF,
// only used late in the pipeline for eliminating phi nodes
IR_OP_TY_MOV,
IR_OP_TY_CNST,
IR_OP_TY_BINARY_OP,
IR_OP_TY_UNARY_OP,
IR_OP_TY_CAST_OP,
// we could merge this all into LOAD/STORE ops
IR_OP_TY_STORE_GLB,
IR_OP_TY_LOAD_GLB,
IR_OP_TY_STORE_LCL,
IR_OP_TY_LOAD_LCL,
IR_OP_TY_STORE_ADDR,
IR_OP_TY_LOAD_ADDR,
IR_OP_TY_ADDR,
IR_OP_TY_BR,
IR_OP_TY_BR_COND,
IR_OP_TY_BR_SWITCH,
IR_OP_TY_RET,
IR_OP_TY_CALL,
// represents a custom instruction understood by a particular backend
IR_OP_TY_CUSTOM,
};
struct ir_op_mov {
struct ir_op *value;
};
struct ir_op_phi {
// not elegant, but phi needs ref to var so it can build itself in
// `find_phi_exprs`
// FIXME: ?
struct td_var *var;
struct ir_op **values;
size_t num_values;
};
enum ir_op_cnst_ty {
IR_OP_CNST_TY_FLT,
IR_OP_CNST_TY_INT,
};
struct ir_op_cnst {
enum ir_op_cnst_ty ty;
union {
unsigned long long int_value;
long double flt_value;
};
};
struct ir_op_ret {
struct ir_op *value;
};
enum ir_op_cast_op_ty {
IR_OP_CAST_OP_TY_SEXT,
IR_OP_CAST_OP_TY_ZEXT,
IR_OP_CAST_OP_TY_TRUNC,
// convert between float types
IR_OP_CAST_OP_TY_CONV,
// convert float <-> unsigned
IR_OP_CAST_OP_TY_UCONV,
// convert float <-> signed
IR_OP_CAST_OP_TY_SCONV,
};
struct ir_op_cast_op {
enum ir_op_cast_op_ty ty;
struct ir_op *value;
};
enum ir_op_unary_op_ty {
IR_OP_UNARY_OP_TY_NEG,
IR_OP_UNARY_OP_TY_LOGICAL_NOT,
IR_OP_UNARY_OP_TY_NOT,
};
struct ir_op_unary_op {
enum ir_op_unary_op_ty ty;
struct ir_op *value;
};
enum ir_op_binary_op_ty {
IR_OP_BINARY_OP_TY_EQ,
IR_OP_BINARY_OP_TY_NEQ,
IR_OP_BINARY_OP_TY_UGT,
IR_OP_BINARY_OP_TY_SGT,
IR_OP_BINARY_OP_TY_UGTEQ,
IR_OP_BINARY_OP_TY_SGTEQ,
IR_OP_BINARY_OP_TY_ULT,
IR_OP_BINARY_OP_TY_SLT,
IR_OP_BINARY_OP_TY_ULTEQ,
IR_OP_BINARY_OP_TY_SLTEQ,
IR_OP_BINARY_OP_TY_FEQ,
IR_OP_BINARY_OP_TY_FNEQ,
IR_OP_BINARY_OP_TY_FGT,
IR_OP_BINARY_OP_TY_FGTEQ,
IR_OP_BINARY_OP_TY_FLT,
IR_OP_BINARY_OP_TY_FLTEQ,
IR_OP_BINARY_OP_TY_LSHIFT,
IR_OP_BINARY_OP_TY_SRSHIFT,
IR_OP_BINARY_OP_TY_URSHIFT,
IR_OP_BINARY_OP_TY_AND,
IR_OP_BINARY_OP_TY_OR,
IR_OP_BINARY_OP_TY_XOR,
IR_OP_BINARY_OP_TY_ADD,
IR_OP_BINARY_OP_TY_SUB,
IR_OP_BINARY_OP_TY_MUL,
IR_OP_BINARY_OP_TY_SDIV,
IR_OP_BINARY_OP_TY_UDIV,
IR_OP_BINARY_OP_TY_SQUOT,
IR_OP_BINARY_OP_TY_UQUOT,
IR_OP_BINARY_OP_TY_FADD,
IR_OP_BINARY_OP_TY_FSUB,
IR_OP_BINARY_OP_TY_FMUL,
IR_OP_BINARY_OP_TY_FDIV,
};
bool binary_op_is_comparison(enum ir_op_binary_op_ty ty);
enum ir_op_sign { IR_OP_SIGN_NA, IR_OP_SIGN_SIGNED, IR_OP_SIGN_UNSIGNED };
enum ir_op_sign binary_op_sign(enum ir_op_binary_op_ty);
struct ir_op_binary_op {
enum ir_op_binary_op_ty ty;
struct ir_op *lhs;
struct ir_op *rhs;
};
// IR does not have sign encoded in type (so `int` and `unsigned` are both
// IR_OP_VAR_TY_32) and instead encodes it in operations (e.g there are
// different IR ops for signed and unsigned division)
enum ir_op_var_primitive_ty {
IR_OP_VAR_PRIMITIVE_TY_I8,
IR_OP_VAR_PRIMITIVE_TY_I16,
IR_OP_VAR_PRIMITIVE_TY_I32,
IR_OP_VAR_PRIMITIVE_TY_I64,
IR_OP_VAR_PRIMITIVE_TY_F16,
IR_OP_VAR_PRIMITIVE_TY_F32,
IR_OP_VAR_PRIMITIVE_TY_F64,
};
enum ir_op_var_ty_ty {
/* Does not produce a value */
IR_OP_VAR_TY_TY_NONE,
/* Primitives - integers, floats, pointers */
IR_OP_VAR_TY_TY_PRIMITIVE,
IR_OP_VAR_TY_TY_FUNC,
IR_OP_VAR_TY_TY_POINTER,
/* Aggregate */
IR_OP_VAR_TY_TY_ARRAY,
IR_OP_VAR_TY_TY_STRUCT,
IR_OP_VAR_TY_TY_UNION,
/* Variadic */
IR_OP_VAR_TY_TY_VARIADIC,
};
enum ir_op_var_func_ty_flags {
IR_OP_VAR_FUNC_TY_FLAG_NONE = 0,
IR_OP_VAR_FUNC_TY_FLAG_VARIADIC = 1
};
struct ir_op_var_func_ty {
struct ir_op_var_ty *ret_ty;
size_t num_params;
struct ir_op_var_ty *params;
enum ir_op_var_func_ty_flags flags;
};
bool is_func_variadic(const struct ir_op_var_func_ty *ty);
struct ir_op_var_struct_ty {
size_t num_fields;
struct ir_op_var_ty *fields;
};
struct ir_op_var_union_ty {
size_t num_fields;
struct ir_op_var_ty *fields;
};
struct ir_op_var_array_ty {
struct ir_op_var_ty *underlying;
union {
size_t num_elements;
};
};
struct ir_op_var_pointer_ty {
struct ir_op_var_ty *underlying;
};
struct ir_op_var_ty {
enum ir_op_var_ty_ty ty;
union {
enum ir_op_var_primitive_ty primitive;
struct ir_op_var_func_ty func;
struct ir_op_var_pointer_ty pointer;
struct ir_op_var_array_ty array;
struct ir_op_var_struct_ty struct_ty;
struct ir_op_var_union_ty union_ty;
};
};
extern const struct ir_op_var_ty IR_OP_VAR_TY_UNKNOWN;
extern const struct ir_op_var_ty IR_OP_VAR_TY_NONE;
extern const struct ir_op_var_ty IR_OP_VAR_TY_I8;
extern const struct ir_op_var_ty IR_OP_VAR_TY_I16;
extern const struct ir_op_var_ty IR_OP_VAR_TY_I32;
extern const struct ir_op_var_ty IR_OP_VAR_TY_I64;
extern const struct ir_op_var_ty IR_OP_VAR_TY_F32;
extern const struct ir_op_var_ty IR_OP_VAR_TY_F64;
extern const struct ir_op_var_ty IR_OP_VAR_TY_VARIADIC;
struct ir_op_call {
struct ir_op_var_ty func_ty;
struct ir_op *target;
size_t num_args;
struct ir_op **args;
};
struct ir_op_store_lcl {
// the local stored into is just the `lcl` assigned to this op
struct ir_op *value;
};
struct ir_op_load_lcl {
struct ir_lcl *lcl;
};
struct ir_op_store_addr {
struct ir_op *value;
struct ir_op *addr;
};
struct ir_op_load_addr {
struct ir_op *addr;
};
struct ir_op_store_glb {
struct ir_op *value;
struct ir_glb *glb;
};
struct ir_op_load_glb {
struct ir_glb *glb;
};
enum ir_op_addr_ty {
IR_OP_ADDR_TY_LCL,
IR_OP_ADDR_TY_GLB,
};
struct ir_op_addr {
enum ir_op_addr_ty ty;
union {
struct ir_lcl *lcl;
struct ir_glb *glb;
};
};
struct ir_op_br_switch {
struct ir_op *value;
// targets on `ir_basicblock`
};
struct ir_op_br_cond {
struct ir_op *cond;
// targets on `ir_basicblock`
};
struct aarch64_op;
struct eep_op;
struct ir_op_custom {
union {
struct aarch64_op *aarch64;
struct eep_op *eep;
};
};
enum ir_reg_ty {
IR_REG_TY_NONE,
IR_REG_TY_SPILLED,
IR_REG_TY_FLAGS,
IR_REG_TY_INTEGRAL,
IR_REG_TY_FP,
};
struct ir_reg {
enum ir_reg_ty ty;
union {
unsigned long idx;
};
};
#define NO_REG \
(struct ir_reg) { .ty = IR_REG_TY_NONE }
#define REG_SPILLED \
(struct ir_reg) { .ty = IR_REG_TY_SPILLED }
#define REG_FLAGS \
(struct ir_reg) { .ty = IR_REG_TY_FLAGS }
enum ir_op_flags {
IR_OP_FLAG_NONE = 0,
IR_OP_FLAG_MUST_SPILL = 1,
IR_OP_FLAG_PARAM = 2,
// indicates this value is passed as a variadic
IR_OP_FLAG_VARIADIC_PARAM = 4,
// do not give this a reg/local as it is a phi node that will take the
// reg/local of that phi
IR_OP_FLAG_DONT_GIVE_REG = 8,
// indicates the op is a load_lcl/store_lcl used for a spill
IR_OP_FLAG_SPILL = 16,
// indicates this op does not generate any instructions and it is encoded in
// its uses
IR_OP_FLAG_CONTAINED = 32,
// reg is assigned and cannot change
IR_OP_FLAG_FIXED_REG = 64,
// op has side effects (e.g is an assignment)
IR_OP_FLAG_SIDE_EFFECTS = 128,
// op has been spilled and all consumers must reload it
IR_OP_FLAG_SPILLED = 256,
};
typedef unsigned long long regpool_t;
struct ir_reg_state {
regpool_t live_gp;
regpool_t live_fp;
};
struct ir_op {
size_t id;
enum ir_op_ty ty;
enum ir_op_flags flags;
struct ir_op_var_ty var_ty;
struct ir_op *pred;
struct ir_op *succ;
struct ir_stmt *stmt;
union {
struct ir_op_cnst cnst;
struct ir_op_call call;
struct ir_op_binary_op binary_op;
struct ir_op_unary_op unary_op;
struct ir_op_cast_op cast_op;
struct ir_op_ret ret;
struct ir_op_store_glb store_glb;
struct ir_op_load_glb load_glb;
struct ir_op_store_lcl store_lcl;
struct ir_op_load_lcl load_lcl;
struct ir_op_store_addr store_addr;
struct ir_op_load_addr load_addr;
struct ir_op_addr addr;
struct ir_op_br_cond br_cond;
struct ir_op_br_switch br_switch;
/* br has no entry, as its target is on `ir_basicblock` and it has no
* condition */
struct ir_op_phi phi;
struct ir_op_mov mov;
struct ir_op_custom custom;
};
struct ir_lcl *lcl;
// only meaningful post register-allocation
struct ir_reg reg;
void *metadata;
const char *comment;
};
// set of ops with no SEQ_POINTs
struct ir_stmt {
size_t id;
// a NULL bb means a pruned stmt
struct ir_basicblock *basicblock;
struct ir_stmt *pred;
struct ir_stmt *succ;
// the links between ops (`pred` & `succ`) have no significance to the
// compilation and are just for traversal. meaningful links between operations
// are with in the op data, such as `ir_op->ret.value`, which points to the op
// whos result is returned
struct ir_op *first;
// last is the dominating op of the statement, and can be used as its "root".
// all other ops in the statement are reachable from it
struct ir_op *last;
};
// ensures the basic block ends with an appropriate branch and does not contain
// any within it
bool valid_basicblock(struct ir_basicblock *basicblock);
enum ir_basicblock_ty {
// a split basicblock has 2 successors and occurs when there is a conditional
// branch
IR_BASICBLOCK_TY_SPLIT,
// a merge basicblock has 1 successor (but its successor will have at least 2
// predecessors)
// and occurs when multiple basicblocks rejoin
IR_BASICBLOCK_TY_MERGE,
// a list of possible values to jump to, and then false branch
IR_BASICBLOCK_TY_SWITCH,
// a return has no explicit successors
IR_BASICBLOCK_TY_RET,
};
struct ir_basicblock_merge {
struct ir_basicblock *target;
};
struct ir_basicblock_split {
struct ir_basicblock *true_target;
struct ir_basicblock *false_target;
};
struct ir_split_case {
unsigned long long value;
struct ir_basicblock *target;
};
struct ir_basicblock_switch {
size_t num_cases;
struct ir_split_case *cases;
struct ir_basicblock *default_target;
};
struct ir_basicblock {
size_t id;
// a NULL irb means a pruned basicblock
struct ir_func *irb;
// these are creation order traversal methods, and do not signify edges
// between BBs
struct ir_basicblock *pred;
struct ir_basicblock *succ;
struct ir_stmt *first;
struct ir_stmt *last;
struct ir_basicblock **preds;
size_t num_preds;
enum ir_basicblock_ty ty;
union {
struct ir_basicblock_merge merge;
struct ir_basicblock_split split;
struct ir_basicblock_switch switch_case;
};
struct instr *first_instr;
struct instr *last_instr;
void *metadata;
const char *comment;
};
enum ir_func_flags { IR_FUNC_FLAG_NONE = 0, IR_FUNC_FLAG_MAKES_CALL = 1 };
enum ir_var_ty {
IR_VAR_TY_STRING_LITERAL,
IR_VAR_TY_CONST_DATA,
IR_VAR_TY_DATA
};
struct ir_var_value_list {
struct ir_var_value *values;
size_t *offsets;
size_t num_values;
};
enum ir_var_value_ty {
IR_VAR_VALUE_TY_ZERO,
IR_VAR_VALUE_TY_INT,
IR_VAR_VALUE_TY_FLT,
IR_VAR_VALUE_TY_VALUE_LIST,
};
struct ir_var_value {
enum ir_var_value_ty ty;
struct ir_op_var_ty var_ty;
union {
const char *str_value;
unsigned long long int_value;
long double flt_value;
struct ir_var_value_list value_list;
};
};
struct ir_var {
enum ir_var_ty ty;
struct ir_op_var_ty var_ty;
struct ir_var_value value;
};
enum ir_glb_ty {
IR_GLB_TY_DATA,
IR_GLB_TY_FUNC,
};
enum ir_glb_def_ty {
IR_GLB_DEF_TY_DEFINED,
IR_GLB_DEF_TY_UNDEFINED,
IR_GLB_DEF_TY_TENTATIVE
};
enum ir_linkage {
IR_LINKAGE_NONE,
IR_LINKAGE_INTERNAL,
IR_LINKAGE_EXTERNAL
};
struct ir_glb {
size_t id;
enum ir_glb_ty ty;
enum ir_glb_def_ty def_ty;
enum ir_linkage linkage;
struct ir_glb *pred;
struct ir_glb *succ;
const char *name;
struct ir_op_var_ty var_ty;
union {
struct ir_var *var;
struct ir_func *func;
};
};
struct ir_lcl {
size_t id;
struct ir_op_var_ty var_ty;
struct ir_lcl *pred;
struct ir_lcl *succ;
// each local is effectively in SSA, where it is stored to only once
struct ir_op *store;
void *metadata;
// HACK: this sucks, stores the current offset of the local but means they
// cannot be compacted
size_t offset;
};
struct ir_reg_usage {
struct bitset *gp_registers_used;
struct bitset *fp_registers_used;
};
struct ir_func {
struct ir_unit *unit;
const char *name;
struct ir_op_var_func_ty func_ty;
struct arena_allocator *arena;
struct ir_basicblock *first;
struct ir_basicblock *last;
enum ir_func_flags flags;
size_t basicblock_count;
size_t stmt_count;
size_t op_count;
size_t next_basicblock_id;
size_t next_stmt_id;
size_t next_op_id;
size_t num_locals;
struct ir_lcl *first_local;
struct ir_lcl *last_local;
struct ir_reg_usage reg_usage;
// number of stack local variables
size_t total_locals_size;
};
struct ir_unit {
struct arena_allocator *arena;
struct parser *parser;
struct typechk *tchk;
struct ir_glb *first_global;
struct ir_glb *last_global;
size_t num_globals;
};
typedef void(walk_op_callback)(struct ir_op **op, void *metadata);
bool op_has_side_effects(const struct ir_op *ty);
bool op_produces_value(const struct ir_op *ty);
bool op_is_branch(enum ir_op_ty ty);
void walk_stmt(struct ir_stmt *stmt, walk_op_callback *cb, void *cb_metadata);
void walk_op(struct ir_op *op, walk_op_callback *cb, void *cb_metadata);
void walk_op_uses(struct ir_op *op, walk_op_callback *cb, void *cb_metadata);
bool stmt_is_empty(struct ir_stmt *stmt);
bool basicblock_is_empty(struct ir_basicblock *basicblock);
void prune_basicblocks(struct ir_func *irb);
void prune_stmts(struct ir_func *irb, struct ir_basicblock *basicblock);
void clear_metadata(struct ir_func *irb);
void rebuild_ids(struct ir_func *irb);
struct ir_lcl *add_local(struct ir_func *irb, struct ir_op_var_ty *var_ty);
struct ir_glb *add_global(struct ir_unit *iru, enum ir_glb_ty ty,
const struct ir_op_var_ty *var_ty,
enum ir_glb_def_ty def_ty, const char *name);
struct ir_op *alloc_ir_op(struct ir_func *irb, struct ir_stmt *stmt);
// clones an op so it can be marked contained
// else we would need to ensure all consumers can contain it
struct ir_op *alloc_contained_ir_op(struct ir_func *irb, struct ir_op *op,
struct ir_op *consumer);
void make_integral_constant(struct ir_unit *iru, struct ir_op *op,
enum ir_op_var_primitive_ty ty,
unsigned long long value);
void make_pointer_constant(struct ir_unit *iru, struct ir_op *op,
unsigned long long value);
struct ir_stmt *alloc_ir_stmt(struct ir_func *irb,
struct ir_basicblock *basicblock);
struct ir_basicblock *alloc_ir_basicblock(struct ir_func *irb);
void add_pred_to_basicblock(struct ir_func *irb,
struct ir_basicblock *basicblock,
struct ir_basicblock *pred);
// NOTE: does NOT connect the blocks to the end (return) block, must be done
// manually
struct ir_basicblock *insert_basicblocks_after_op(struct ir_func *irb,
struct ir_op *insert_after,
struct ir_basicblock *first);
void make_basicblock_split(struct ir_func *irb,
struct ir_basicblock *basicblock,
struct ir_basicblock *true_target,
struct ir_basicblock *false_target);
void make_basicblock_merge(struct ir_func *irb,
struct ir_basicblock *basicblock,
struct ir_basicblock *target);
void make_basicblock_switch(struct ir_func *irb,
struct ir_basicblock *basicblock, size_t num_cases,
struct ir_split_case *cases,
struct ir_basicblock *default_target);
void detach_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *basicblock);
void detach_ir_stmt(struct ir_func *irb, struct ir_stmt *stmt);
void detach_ir_op(struct ir_func *irb, struct ir_op *op);
void initialise_ir_basicblock(struct ir_basicblock *basicblock, size_t id);
// Helper method that ensures the essential fields in IR op are initialised
void initialise_ir_op(struct ir_op *op, size_t id, enum ir_op_ty ty,
struct ir_op_var_ty var_ty, struct ir_reg,
struct ir_lcl *lcl);
void move_after_ir_op(struct ir_func *irb, struct ir_op *op,
struct ir_op *move_after);
void move_before_ir_op(struct ir_func *irb, struct ir_op *op,
struct ir_op *move_before);
void attach_ir_op(struct ir_func *irb, struct ir_op *op, struct ir_stmt *stmt,
struct ir_op *pred, struct ir_op *succ);
void move_after_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *basicblock,
struct ir_basicblock *move_after);
void move_before_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *basicblock,
struct ir_basicblock *move_before);
void attach_ir_basicblock(struct ir_func *irb, struct ir_basicblock *basicblock,
struct ir_basicblock *pred,
struct ir_basicblock *succ);
// swaps ops but does NOT swap their uses - expressions pointing to `left` will
// now point to `right`
void swap_ir_ops(struct ir_func *irb, struct ir_op *left, struct ir_op *right);
struct ir_op *replace_ir_op(struct ir_func *irb, struct ir_op *op,
enum ir_op_ty ty, struct ir_op_var_ty var_ty);
struct ir_op *insert_before_ir_op(struct ir_func *irb,
struct ir_op *insert_before, enum ir_op_ty ty,
struct ir_op_var_ty var_ty);
struct ir_op *insert_after_ir_op(struct ir_func *irb,
struct ir_op *insert_after, enum ir_op_ty ty,
struct ir_op_var_ty var_ty);
struct ir_basicblock *
insert_before_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *insert_before);
struct ir_basicblock *
insert_after_ir_basicblock(struct ir_func *irb,
struct ir_basicblock *insert_after);
struct ir_var_ty_info {
size_t size;
size_t alignment;
size_t num_fields;
size_t *offsets;
};
struct ir_var_ty_info var_ty_info(struct ir_unit *iru,
const struct ir_op_var_ty *ty);
struct ir_op_var_ty var_ty_get_underlying(const struct ir_op_var_ty *var_ty);
struct ir_op_var_ty var_ty_for_pointer_size(struct ir_unit *iru);
struct ir_op_var_ty var_ty_make_pointer(struct ir_unit *iru,
const struct ir_op_var_ty *underlying);
struct ir_op_var_ty var_ty_make_array(struct ir_unit *iru,
const struct ir_op_var_ty *underlying,
size_t num_elements);
bool var_ty_is_primitive(const struct ir_op_var_ty *var_ty,
enum ir_op_var_primitive_ty primitive);
bool var_ty_is_integral(const struct ir_op_var_ty *var_ty);
bool var_ty_is_fp(const struct ir_op_var_ty *var_ty);
bool var_ty_is_aggregate(const struct ir_op_var_ty *var_ty);
struct ir_op *spill_op(struct ir_func *irb, struct ir_op *op);
struct ir_op_use {
struct ir_op *op;
struct ir_op *uses;
size_t num_uses;
};
struct ir_op_uses {
struct ir_op_use *use_datas;
size_t num_use_datas;
};
struct ir_op_uses build_op_uses_map(struct ir_func *func);
#endif