aboutsummaryrefslogtreecommitdiff
path: root/rcc-legacy/include/rcc/all.h
blob: f81acd114bc53e689c18523d2fe8551ee4524ea0 (plain)
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
/* Source code for RCC-Legacy.
   Copyright (C) 2026 Richard Qin

This file is part of RCC.

RCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

RCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with RCC; see the file LICENSE.  If not see
<http://www.gnu.org/licenses/>.  */

#pragma once

#include <array>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>

namespace rcc {

// Basic type aliases

typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long bits;

// Forward declaration
struct BSet;
struct Ref;
struct Op;
struct Ins;
struct Phi;
struct Blk;
struct Use;
struct Sym;
struct Num;
struct Alias;
struct Tmp;
struct Con;
struct Fn;
struct Typ;
struct Field;
struct Dat;
struct Lnk;
struct Target;
struct SubPool;
struct Ctx;
struct Insert;   // load.cc
struct Name;     // ssa.cc
struct Asmbits;  // emit.cc
typedef struct Addr Mem;

enum {
    NIns = 1 << 20,
    NAlign = 3,
    NField = 32,
    NBit = CHAR_BIT * sizeof(bits),
    NPm = 64,  // Max copies for parallel move (rega)
};

struct Target {
    char name[16];
    char apple;
    char windows;
    int gpr0;
    int ngpr;
    int fpr0;
    int nfpr;
    bits rglob;
    int nrglob;
    int* rsave;
    int nrsave[2];
    bits (*retregs)(Ref, int[2]);
    bits (*argregs)(Ref, int[2]);
    int (*memargs)(int);
    void (*abi0)(Fn*);
    void (*abi1)(Ctx*, Fn*);
    void (*isel)(Ctx*, Fn*);
    void (*emitfn)(Fn*, FILE*);
    void (*emitfin)(Ctx*, FILE*);
    char asloc[4];
    char assym[4];
    uint cansel : 1;
};

#define BIT(n) (static_cast<bits>(1) << (n))

enum {
    RXX = 0,
    Tmp0 = NBit,
};

struct BSet {
    uint nt;
    bits* t;
};

enum {
    RTmp,
    RCon,
    RInt,
    RType, /* last kind to come out of the parser */
    RSlot,
    RCall,
    RMem,
};

struct Ref {
    uint type : 3;
    uint val : 29;

    // Factory methods
    static Ref none() { return {RTmp, 0}; }
    static Ref undef() { return {RCon, 0}; }
    static Ref con_z() { return {RCon, 1}; }
    static Ref tmp(uint v) { return {RTmp, v}; }
    static Ref con(uint v) { return {RCon, v}; }
    static Ref slot(int v) { return {RSlot, static_cast<uint>(v) & 0x1fffffffu}; }
    static Ref typ(uint v) { return {RType, v}; }
    static Ref call(uint v) { return {RCall, v}; }
    static Ref mem(uint v) { return {RMem, v}; }
    static Ref integer(int v) { return {RInt, static_cast<uint>(v) & 0x1fffffffu}; }

    bool operator==(Ref o) const { return type == o.type && val == o.val; }
    bool operator!=(Ref o) const { return !(*this == o); }
};

static inline int req(Ref a, Ref b) {
    return a.type == b.type && a.val == b.val;
}

static inline int rtype(Ref r) {
    if (req(r, Ref::none()))
        return -1;
    return r.type;
}

static inline int rsval(Ref r) {
    return (static_cast<int>(r.val) ^ 0x10000000) - 0x10000000;
}

static inline int isreg(Ref r) {
    return rtype(r) == RTmp && r.val < Tmp0;
}

enum CmpI {
    Cieq,
    Cine,
    Cisge,
    Cisgt,
    Cisle,
    Cislt,
    Ciuge,
    Ciugt,
    Ciule,
    Ciult,
    NCmpI,
};

enum CmpF {
    Cfeq,
    Cfge,
    Cfgt,
    Cfle,
    Cflt,
    Cfne,
    Cfo,
    Cfuo,
    NCmpF,
    NCmp = NCmpI + NCmpF,
};

enum { Kx = -1, Kw, Kl, Ks, Kd };

#define KWIDE(k) ((k) & 1)
#define KBASE(k) ((k) >> 1)

#include "ops.h"  // enum O { Oxxx, ..., NOp } + OpInfo / kops[] single source attribute table

enum J {
    Jxxx,
#define JMPS(X)                                                                             \
    X(retw)                                                                                 \
    X(retl)                                                                                 \
    X(rets)                                                                                 \
    X(retd)                                                                                 \
    X(retsb)                                                                                \
    X(retub)                                                                                \
    X(retsh)                                                                                \
    X(retuh)                                                                                \
    X(retc)                                                                                 \
    X(ret0)                                                                                 \
    X(jmp)                                                                                  \
    X(jnz)                                                                                  \
    X(jfieq)                                                                                \
    X(jfine)                                                                                \
    X(jfisge)                                                                               \
    X(jfisgt) X(jfisle) X(jfislt) X(jfiuge) X(jfiugt) X(jfiule) X(jfiult) X(jffeq) X(jffge) \
        X(jffgt) X(jffle) X(jfflt) X(jffne) X(jffo) X(jffuo) X(hlt)
#define X(j) J##j,
    JMPS(X)
#undef X
        NJmp
};

enum {
    Ocmpw = Oceqw,
    Ocmpw1 = Ocultw,
    Ocmpl = Oceql,
    Ocmpl1 = Ocultl,
    Ocmps = Oceqs,
    Ocmps1 = Ocuos,
    Ocmpd = Oceqd,
    Ocmpd1 = Ocuod,
    Oalloc = Oalloc4,
    Oalloc1 = Oalloc16,
    Oflag = Oflagieq,
    Oflag1 = Oflagfuo,
    Oxsel = Oxselieq,
    Oxsel1 = Oxselfuo,
    NPubOp = Onop,
    Jjf = Jjfieq,
    Jjf1 = Jjffuo,
};

#define INRANGE(x, l, u)                                    \
    (static_cast<unsigned>(x) - static_cast<unsigned>(l) <= \
     static_cast<unsigned>(u) - static_cast<unsigned>(l))
#define isstore(o) INRANGE(o, Ostoreb, Ostored)
#define isload(o) INRANGE(o, Oloadsb, Oload)
#define isalloc(o) INRANGE(o, Oalloc4, Oalloc16)
#define isext(o) INRANGE(o, Oextsb, Oextuw)
#define ispar(o) INRANGE(o, Opar, Opare)
#define isarg(o) INRANGE(o, Oarg, Oargv)
#define isret(j) INRANGE(j, Jretw, Jret0)
#define isparbh(o) INRANGE(o, Oparsb, Oparuh)
#define isargbh(o) INRANGE(o, Oargsb, Oarguh)
#define isretbh(j) INRANGE(j, Jretsb, Jretuh)
#define isxsel(o) INRANGE(o, Oxsel, Oxsel1)

struct Op {
    char* name;
    short argcls[2][4];
    uint canfold : 1;
    uint hasid : 1;
    uint idval : 1;
    uint commutes : 1;
    uint assoc : 1;
    uint idemp : 1;
    uint cmpeqwl : 1;
    uint cmplgtewl : 1;
    uint eqval : 1;
    uint pinned : 1;
};

struct Ins {
    uint op : 30;
    uint cls : 2;
    Ref to;
    Ref arg[2];
};

struct Phi {
    Ref to;
    short cls;
    int visit;
    uint narg;
    Ref* arg;
    Blk** blk;
    Phi* link;
};

struct Blk {
    Phi* phi;
    Ins* ins;
    uint nins;
    struct {
        short type;
        short cls;  // Jjfi* / Jjff* carried cmp operand cls; other jmp types are 0
        Ref arg;
        Ref arg2;  // Used only by Jjfi* / Jjff* (carries 2nd cmp operand when fusing cmp+jnz)
    } jmp;
    Blk* s1;
    Blk* s2;
    Blk* link;

    uint id;
    uint visit;

    Blk* idom;
    Blk *dom, *dlink;
    Blk** fron;
    uint nfron;
    int depth;

    Blk** pred;
    uint npred;
    BSet in[1], out[1], gen[1];
    int nlive[2];
    int loop;
    char* name;
};

struct Use {
    enum {
        UXXX,
        UPhi,
        UIns,
        UJmp,
    } type;
    uint bid;
    union {
        Ins* ins;
        Phi* phi;
    } u;
};

struct Sym {
    enum {
        SGlo = 0,
        SThr = 1,
        SExt = 2,
        SExtThr = SExt | SThr,
    } type;
    uint32_t id;
};

struct Num {
    uchar n;
    uchar nl, nr;
    Ref l, r;
};

enum { NoAlias, MayAlias, MustAlias };

struct Alias {
    enum {
        ABot = 0,
        ALoc = 1,
        ACon = 2,
        AEsc = 3,
        ASym = 4,
        AUnk = 6,
#define astack(t) ((t) & 1)
    } type;
    int base;
    int64_t offset;
    // True only for ASym path: Oadd(ASym, AUnk) preserves symbol identity, but offset is
    // unreliable. Makes alias() degrade to MayAlias on "same symbol / same base" branch, while
    // "different ASym" can still NoAlias. Not propagated to ALoc/AEsc: stack slot offset is
    // controlled by IRGen and always known.
    bool offset_unknown;
    union {
        Sym sym;
        struct {
            int sz;
            bits m;
        } loc;
    } u;
    Alias* slot;
};

struct Tmp {
    char* name;
    Ins* def;
    Use* use;
    uint ndef, nuse;
    uint bid;
    uint cost;
    int slot;
    short cls;
    struct {
        int r;
        int w;
        bits m;
    } hint;
    int phi;
    Alias alias;
    enum { WFull, Wsb, Wub, Wsh, Wuh, Wsw, Wuw } width;
    int visit;
    uint gcmbid;
};

struct Con {
    enum {
        CUndef,
        CBits,
        CAddr,
    } type;
    Sym sym;
    union {
        int64_t i;
        double d;
        float s;
    } bits;
    char flt;
};

struct Addr {
    Con offset;
    Ref base;
    Ref index;
    int scale;
};

struct Lnk {
    char export_;  // 'export' is C++ reserved word (C++20 module)
    char thread;
    char common;
    char align;
    char* sec;
    char* secf;
};

struct Fn {
    Blk* start;
    Tmp* tmp;
    Con* con;
    Mem* mem;
    int ntmp;
    int ncon;
    int nmem;
    uint nblk;
    int retty;
    Ref retr;
    Blk** rpo;
    bits reg;
    int slot;
    int salign;
    char vararg;
    char dynalloc;
    char leaf;
    char* name;
    Lnk lnk;
    SubPool* _subpool;  // Memory pool ownership for this fn; parse attaches it when generating fn,
                        // worker calls subpool_free after processing
};

struct Field {
    enum {
        FEnd,
        Fb,
        Fh,
        Fw,
        Fl,
        Fs,
        Fd,
        FPad,
        FTyp,
    } type;
    uint len;
};

struct Typ {
    char* name;
    char isdark;
    char isunion;
    int align;
    uint64_t size;
    uint nunion;
    Field (*fields)[NField + 1];
};

struct Dat {
    enum { DStart, DEnd, DB, DH, DW, DL, DZ } type;
    char* name;
    Lnk* lnk;
    union {
        int64_t num;
        double fltd;
        float flts;
        char* str;
        struct {
            char* name;
            int64_t off;
        } ref;
    } u;
    char isref;
    char isstr;
};

extern Target T;
extern char debug['Z' + 1];

typedef enum {
    PHeap,
    PFn,
} Pool;

// SubPool / Ctx

struct SubPool {
    std::vector<void*> chunks;  // Each chunk is a pointer from emalloc
};

void* subpool_alloc(SubPool*, size_t);
void subpool_free(SubPool*);

struct Ctx {
    SubPool* subpool = nullptr;

    Ins* insb = nullptr;  // 8MB, emalloc during init(); freed during ~Ctx
    Ins* curi = nullptr;  // Reverse fill pointer, reset to insb + NIns during bind()

    Fn* load_curf = nullptr;
    uint load_inum = 0;
    Insert* load_ilog = nullptr;
    uint load_nlog = 0;

    Name* ssa_namel = nullptr;

    Ref gvn_con01[2]{};
    Ins** gvn_tbl = nullptr;
    uint gvn_tbln = 0;

    bits rega_regu = 0;
    Tmp* rega_tmp = nullptr;
    Mem* rega_mem = nullptr;
    struct {
        Ref src, dst;
        int cls;
    } rega_pm[NPm]{};
    int rega_npm = 0;
    int rega_loop = 0;
    uint rega_stmov = 0, rega_stblk = 0;

    Asmbits* emit_stash = nullptr;

    // die context: during spill, record "peak FPR live set when limit_one"; dumped during emit die
    int spill_fp_live_peak = 0;

    FILE* out_buf = nullptr;
    FILE* dbg_buf = nullptr;

    Fn* fn = nullptr;

    void init();     // Allocate insb (8MB) and initial SubPool; other fields zero-initialized
    void bind(Fn*);  // Take over fn's SubPool, reset curi and per-Fn pass states
    void release();  // Release current SubPool, clear states; do not release insb
    ~Ctx();          // Release insb and remaining SubPool
};

extern Typ* typ;
extern Op optab[NOp];

// util.cc

uint32_t hash(const char*);
[[noreturn]] void die_(const char*, const char*, ...) __attribute__((format(printf, 2, 3)));

// die-context utilities
struct Ins;
void set_die_ctx(Ctx*);
const char* die_ctx_str(Ctx*, Ins* cur);  // cur can be nullptr
void* emalloc(size_t);
void* alloc(Ctx*, size_t);
void freeall(Ctx*);
void* vnew(Ctx*, ulong, size_t, Pool);
void vfree(void*);
void vgrow(Ctx*, void*, ulong);

/// Lazy-allocating vgrow
inline void vensure(Ctx* ctx, void* vp, ulong len, size_t esz, Pool pl) {
    void** pp = static_cast<void**>(vp);
    if (*pp == nullptr)
        *pp = vnew(ctx, len, esz, pl);
    else
        vgrow(ctx, vp, len);
}
void addins(Ctx*, Ins**, uint*, Ins*);
void addbins(Ctx*, Ins**, uint*, Blk*);
char* strf(Ctx*, Pool, const char*, ...) __attribute__((format(printf, 3, 4)));
uint32_t intern(const char*);
char* str(uint32_t);
int argcls(Ins*, int);
int iscmp(int, int*, int*);
void igroup(Blk*, Ins*, Ins**, Ins**);
void emit(Ctx*, int, int, Ref, Ref, Ref);
void emiti(Ctx*, Ins);
void idup(Ctx*, Blk*, Ins*, ulong);
Ins* icpy(Ins*, Ins*, ulong);
int cmpop(int);
int cmpwlneg(int);
int clsmerge(short*, short);
int phicls(int, Tmp*);
uint phiargn(Phi*, Blk*);
Ref phiarg(Phi*, Blk*);
Ref newtmp(Ctx*, const char*, int, Fn*);
void chuse(Ctx*, Ref, int, Fn*);
int symeq(Sym, Sym);
Ref newcon(Ctx*, Con*, Fn*);
Ref getcon(Ctx*, int64_t, Fn*);
int addcon(Con*, Con*, int);
int isconbits(Fn* fn, Ref r, int64_t* v);
void salloc(Ctx*, Ref, Ref, Fn*);
int lowest_bit(bits);
void dumpts(BSet*, Tmp*, FILE*);
void runmatch(uchar*, Num*, Ref, Ref*);

void bsinit(Ctx*, BSet*, uint);
void bszero(BSet*);
uint bscount(BSet*);
void bsset(BSet*, uint);
void bsclr(BSet*, uint);
void bscopy(BSet*, BSet*);
void bsunion(BSet*, BSet*);
void bsinter(BSet*, BSet*);
void bsdiff(BSet*, BSet*);
int bsequal(BSet*, BSet*);
int bsiter(BSet*, int*);

static inline int bshas(BSet* bs, uint elt) {
    assert(elt < bs->nt * NBit);
    return (bs->t[elt / NBit] & BIT(elt % NBit)) != 0;
}

// parse.cc

void parse(FILE*, char*, void (*)(char*), void (*)(Dat*), void (*)(Fn*));
void printfn(Fn*, FILE*);
void printref(Ref, Fn*, FILE*);
[[noreturn]] void err(char*, ...) __attribute__((format(printf, 1, 2)));

// abi.cc

void elimsb(Fn*);

// cfg.cc - CFG analysis and simplification (Ctx*, Fn*) conventions

Blk* newblk(Ctx*);
void fillpreds(Ctx*, Fn*);
void fillcfg(Ctx*, Fn*);
void filldom(Fn*);
int sdom(Blk*, Blk*);
int dom(Blk*, Blk*);
void fillfron(Ctx*, Fn*);
void loopiter(Fn*, void (*)(Blk*, Blk*));
void filldepth(Fn*);
Blk* lca(Blk*, Blk*);
void fillloop(Fn*);
void simpljmp(Ctx*, Fn*);
int reaches(Fn*, Blk*, Blk*);
int reachesnotvia(Fn*, Blk*, Blk*, Blk*);
int ifgraph(Blk*, Blk**, Blk**, Blk**);
void simplcfg(Ctx*, Fn*);

// mem.cc

void promote(Ctx*, Fn*);
void coalesce(Ctx*, Fn*);

// alias.cc

void fillalias(Fn*);
void getalias(Alias*, Ref, Fn*);
int alias(Ref, int, int, Ref, int, int*, Fn*);
int escapes(Ref, Fn*);

// load.cc

int loadsz(Ins*);
int storesz(Ins*);
void loadopt(Ctx*, Fn*);

// ssa.cc - Use chain population / SSA construction / SSA invariant verification

void adduse_ins(Ctx*, Tmp*, Blk*, Ins*);
void adduse_phi(Ctx*, Tmp*, Blk*, Phi*);
void adduse_jmp(Ctx*, Tmp*, Blk*);
void filluse(Ctx*, Fn*);
void ssa(Ctx*, Fn*);
void ssacheck(Fn*);
void ssa_drain_namel(Ctx*);  // Called by ~Ctx to drain emalloc nodes from ssa_namel free list

// copy.cc

void narrowpars(Ctx*, Fn*);
Ref copyref(Ctx*, Fn*, Blk*, Ins*);
Ref phicopyref(Ctx*, Fn*, Blk*, Phi*);

// fold.cc

int foldint(Con*, int, int, Con*, Con*);
Ref foldref(Ctx*, Fn*, Ins*);

// gvn.cc
//

int zeroval(Fn*, Blk*, Ref, int, int*);
void gvn(Ctx*, Fn*);

// gcm.cc

int pinned(Ins*);
void gcm(Ctx*, Fn*);

// ifopt.cc

void ifconvert(Ctx*, Fn*);

// divopt.cc

void divopt(Ctx*, Fn*);

// mulopt.cc - Constant multiplication strength reduction (x * c -> <=3 shift/add/sub/neg).

void mulopt(Ctx*, Fn*);

// storemrg.cc - Merge adjacent same-base 4B-zero stores into 8B storel.

void storemrg(Ctx*, Fn*);

// licm.cc

void licm(Ctx*, Fn*);

// scalar_repl.cc - Hoist intra-loop same-address load+store accumulators to reg + phi.

void scalar_repl(Ctx*, Fn*);

// unroll.cc - Loop unrolling (Phase 1: Full unroll for constant trips).

void unroll(Ctx*, Fn*);

// tre.cc - Tail recursion elimination (Phase 1: Self-recursion + normalized tail call form).

void tre(Ctx*, Fn*);

// lsr.cc - Loop strength reduction (Phase 1: Constant basic induction variable * constant).

void lsr(Ctx*, Fn*);

// loop_idiom.cc - memset / memcpy / memmove loop idiom recognition -> Rewrite as Ocall to builtin
// runtime.

void loop_idiom(Ctx*, Fn*);

// reduction_unroll.cc - Integer reduction loop K-way parallel accumulator unrolling, breaking
// serial dependency chains.

void reduction_unroll(Ctx*, Fn*);

// loopinterchange.cc - Integer perfect 2-level nested loop interchange (row-major column access ->
// row access).

void loopinterchange(Ctx*, Fn*);

// unswitch.cc - Loop unswitching (hoisting if (loop_invariant_cond) out of loop).

void unswitch(Ctx*, Fn*);

// sccp.cc — Sparse Conditional Constant Propagation(Wegman-Zadeck)。

void sccp(Ctx*, Fn*);

// reassoc.cc - Expression reassociation (rank-based, constant gathering + normalization + common
// term elimination).

void reassoc(Ctx*, Fn*);

// dce.cc - Global dead code elimination (mark-and-sweep, roots = side-effect Ins + jmp.arg).

void dce(Ctx*, Fn*);

// dead_loop.cc - Dead loop elimination (no side-effects + provably terminating + no live-out loop).

void dead_loop(Ctx*, Fn*);

// inline.cc - Function inlining (leaf-only + fixpoint), whole-module perspective, called once after
// IRGen and before run_midend.

void inline_module(Ctx*, std::vector<Fn*>&);

// simpl.cc

void simpl(Ctx*, Fn*);

// live.cc - Backward dataflow liveness analysis

void liveon(BSet*, Blk*, Blk*);
void filllive(Ctx*, Fn*);

// spill.cc

void fillcost(Ctx*, Fn*);
void spill(Ctx*, Fn*);

// rega.cc

void rega(Ctx*, Fn*);

// peephole.cc

void peephole(Ctx*, Fn*);

// emit.cc

void emitfnlnk(char*, Lnk*, FILE*);
void emitdat(Dat*, FILE*);
void emitdbgfile(char*, FILE*);
void emitdbgloc(uint, uint, FILE*);
int stashbits(Ctx*, bits, int);
void elf_emitfnfin(char*, FILE*);
void elf_emitfin(Ctx*, FILE*);
void macho_emitfin(Ctx*, FILE*);
void pe_emitfin(Ctx*, FILE*);
void rv64_emitfin(Ctx*, FILE*);

// macro
#define die(...) die_(__FILE__, __VA_ARGS__)
#define MAKESURE(what, x) static_assert((x), #what)

}  // namespace rcc