kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

commit 60a5068f6d4ba9b42645f2f1b01fabe1a9bf6a6e
parent 681d084ed3a38962acdd1fe99f8c2c0b7143aec1
Author: Ryan Sepassi <rsepassi@gmail.com>
Date:   Tue, 26 May 2026 16:16:38 -0700

arch: rm old regalloc.h/c

Diffstat:
Msrc/arch/native_target.h | 7++++++-
Dsrc/arch/regalloc.c | 127-------------------------------------------------------------------------------
Dsrc/arch/regalloc.h | 50--------------------------------------------------
Mtest/opt/opt_test.c | 26--------------------------
4 files changed, 6 insertions(+), 204 deletions(-)

diff --git a/src/arch/native_target.h b/src/arch/native_target.h @@ -4,7 +4,6 @@ #include <string.h> #include "arch/arch.h" -#include "arch/regalloc.h" #include "cg/cgtarget.h" #include "core/core.h" @@ -51,6 +50,12 @@ typedef struct NativeKnownFrameDesc { u32 align; } NativeKnownFrameDesc; +typedef enum NativeAllocClass { + NATIVE_REG_INT, + NATIVE_REG_FP, + NATIVE_REG_VEC, +} NativeAllocClass; + typedef enum NativeRegFlag { NATIVE_REG_NONE = 0, NATIVE_REG_ALLOCABLE = 1u << 0, diff --git a/src/arch/regalloc.c b/src/arch/regalloc.c @@ -1,127 +0,0 @@ -#include "arch/regalloc.h" - -#include <string.h> - -static u32 pool_mask(u32 nregs) { - if (nregs > CG_SIMPLE_REGALLOC_MAX_REGS) nregs = CG_SIMPLE_REGALLOC_MAX_REGS; - return (nregs >= 32u) ? 0xFFFFFFFFu : ((1u << nregs) - 1u); -} - -void cg_simple_regpool_init_range(CGSimpleRegPool* p, Reg base, u32 nregs) { - if (nregs > CG_SIMPLE_REGALLOC_MAX_REGS) nregs = CG_SIMPLE_REGALLOC_MAX_REGS; - p->free = pool_mask(nregs); - p->used = 0; - p->order = NULL; - p->base = base; - p->nregs = nregs; -} - -void cg_simple_regpool_init_ordered(CGSimpleRegPool* p, const Reg* regs, - u32 nregs) { - if (nregs > CG_SIMPLE_REGALLOC_MAX_REGS) nregs = CG_SIMPLE_REGALLOC_MAX_REGS; - p->free = pool_mask(nregs); - p->used = 0; - p->order = regs; - p->base = 0; - p->nregs = nregs; -} - -Reg cg_simple_regpool_reg_at(const CGSimpleRegPool* p, u32 idx) { - if (idx >= p->nregs) return (Reg)REG_NONE; - return p->order ? p->order[idx] : (Reg)(p->base + idx); -} - -Reg cg_simple_regpool_alloc(CGSimpleRegPool* p) { - if (p->free == 0) return (Reg)REG_NONE; - u32 idx = (u32)__builtin_ctz(p->free); - p->free &= ~(1u << idx); - p->used |= 1u << idx; - return cg_simple_regpool_reg_at(p, idx); -} - -int cg_simple_regpool_free(CGSimpleRegPool* p, Reg r) { - for (u32 i = 0; i < p->nregs; ++i) { - if (cg_simple_regpool_reg_at(p, i) == r) { - u32 bit = 1u << i; - if (p->free & bit) return -1; - p->free |= bit; - return 1; - } - } - return 0; -} - -void cg_simple_regpool_reserve(CGSimpleRegPool* p, Reg r) { - for (u32 i = 0; i < p->nregs; ++i) { - if (cg_simple_regpool_reg_at(p, i) == r) { - p->free &= ~(1u << i); - p->used |= 1u << i; - return; - } - } -} - -u32 cg_simple_regpool_used_regs(const CGSimpleRegPool* p, Reg* out, u32 cap) { - u32 n = 0; - u32 bits = p->used & pool_mask(p->nregs); - while (bits) { - u32 idx = (u32)__builtin_ctz(bits); - bits &= bits - 1u; - if (n < cap) out[n++] = cg_simple_regpool_reg_at(p, idx); - } - return n; -} - -void cg_simple_regalloc_init(CGSimpleRegAlloc* a) { memset(a, 0, sizeof *a); } - -void cg_simple_regalloc_init_virtual(CGSimpleRegAlloc* a) { - memset(a, 0, sizeof *a); - a->virtual_regs = 1; - a->next_virtual = 1; -} - -void cg_simple_regalloc_set_range(CGSimpleRegAlloc* a, NativeAllocClass cls, Reg base, - u32 nregs) { - if ((u32)cls >= 3u) return; - cg_simple_regpool_init_range(&a->pools[cls], base, nregs); -} - -void cg_simple_regalloc_set_ordered(CGSimpleRegAlloc* a, NativeAllocClass cls, - const Reg* regs, u32 nregs) { - if ((u32)cls >= 3u) return; - cg_simple_regpool_init_ordered(&a->pools[cls], regs, nregs); -} - -Reg cg_simple_regalloc_alloc(CGSimpleRegAlloc* a, NativeAllocClass cls) { - if ((u32)cls >= 3u) return (Reg)REG_NONE; - if (a->virtual_regs) return a->next_virtual++; - return cg_simple_regpool_alloc(&a->pools[cls]); -} - -int cg_simple_regalloc_free(CGSimpleRegAlloc* a, NativeAllocClass cls, Reg r) { - if ((u32)cls >= 3u) return -2; - if (a->virtual_regs) { - (void)r; - return 1; - } - return cg_simple_regpool_free(&a->pools[cls], r); -} - -void cg_simple_regalloc_reserve(CGSimpleRegAlloc* a, NativeAllocClass cls, Reg r) { - if ((u32)cls >= 3u) return; - if (a->virtual_regs) { - if (r != (Reg)REG_NONE && r >= a->next_virtual) a->next_virtual = r + 1u; - return; - } - cg_simple_regpool_reserve(&a->pools[cls], r); -} - -u32 cg_simple_regalloc_used_regs(const CGSimpleRegAlloc* a, NativeAllocClass cls, - Reg* out, u32 cap) { - if ((u32)cls >= 3u || a->virtual_regs) return 0; - return cg_simple_regpool_used_regs(&a->pools[cls], out, cap); -} - -int cg_simple_regalloc_is_virtual(const CGSimpleRegAlloc* a) { - return a->virtual_regs != 0; -} diff --git a/src/arch/regalloc.h b/src/arch/regalloc.h @@ -1,50 +0,0 @@ -#ifndef CFREE_ARCH_REGALLOC_H -#define CFREE_ARCH_REGALLOC_H - -#include "arch/arch.h" - -#define CG_SIMPLE_REGALLOC_MAX_REGS 32u - -typedef enum NativeAllocClass { - NATIVE_REG_INT, - NATIVE_REG_FP, - NATIVE_REG_VEC, -} NativeAllocClass; - -typedef struct CGSimpleRegPool { - u32 free; /* bit i set iff entry i is free */ - u32 used; /* bit i set iff entry i was allocated/reserved */ - const Reg* order; /* optional ordered physical-id table */ - Reg base; /* used when order is NULL: entry i = base + i */ - u32 nregs; -} CGSimpleRegPool; - -typedef struct CGSimpleRegAlloc { - CGSimpleRegPool pools[3]; /* indexed by NativeAllocClass */ - u32 virtual_regs; - Reg next_virtual; -} CGSimpleRegAlloc; - -void cg_simple_regpool_init_range(CGSimpleRegPool* p, Reg base, u32 nregs); -void cg_simple_regpool_init_ordered(CGSimpleRegPool* p, const Reg* regs, - u32 nregs); -Reg cg_simple_regpool_alloc(CGSimpleRegPool* p); -int cg_simple_regpool_free(CGSimpleRegPool* p, Reg r); -void cg_simple_regpool_reserve(CGSimpleRegPool* p, Reg r); -Reg cg_simple_regpool_reg_at(const CGSimpleRegPool* p, u32 idx); -u32 cg_simple_regpool_used_regs(const CGSimpleRegPool* p, Reg* out, u32 cap); - -void cg_simple_regalloc_init(CGSimpleRegAlloc* a); -void cg_simple_regalloc_init_virtual(CGSimpleRegAlloc* a); -void cg_simple_regalloc_set_range(CGSimpleRegAlloc* a, NativeAllocClass cls, Reg base, - u32 nregs); -void cg_simple_regalloc_set_ordered(CGSimpleRegAlloc* a, NativeAllocClass cls, - const Reg* regs, u32 nregs); -Reg cg_simple_regalloc_alloc(CGSimpleRegAlloc* a, NativeAllocClass cls); -int cg_simple_regalloc_free(CGSimpleRegAlloc* a, NativeAllocClass cls, Reg r); -void cg_simple_regalloc_reserve(CGSimpleRegAlloc* a, NativeAllocClass cls, Reg r); -u32 cg_simple_regalloc_used_regs(const CGSimpleRegAlloc* a, NativeAllocClass cls, - Reg* out, u32 cap); -int cg_simple_regalloc_is_virtual(const CGSimpleRegAlloc* a); - -#endif diff --git a/test/opt/opt_test.c b/test/opt/opt_test.c @@ -9,7 +9,6 @@ #include "abi/abi.h" #include "arch/arch.h" -#include "arch/regalloc.h" #include "arch/rv64/isa.h" #include "arch/x64/isa.h" #include "core/core.h" @@ -6649,30 +6648,6 @@ static void opt_register_local_addr_frame_homes(void) { tc_fini(&tc); } -static void simple_regalloc_reports_exact_used_regs(void) { - CGSimpleRegAlloc a; - static const Reg regs[] = {3, 7, 11}; - Reg used[CG_SIMPLE_REGALLOC_MAX_REGS]; - for (u32 i = 0; i < CG_SIMPLE_REGALLOC_MAX_REGS; ++i) used[i] = REG_NONE; - cg_simple_regalloc_init(&a); - cg_simple_regalloc_set_ordered(&a, RC_INT, regs, 3); - - cg_simple_regalloc_reserve(&a, RC_INT, 11); - u32 nused = cg_simple_regalloc_used_regs(&a, RC_INT, used, - CG_SIMPLE_REGALLOC_MAX_REGS); - EXPECT(nused == 1, "reserve should report one exact used reg, got %u", - (unsigned)nused); - EXPECT(used[0] == 11, "reserve should report r11, got r%u", - (unsigned)used[0]); - - EXPECT(cg_simple_regalloc_alloc(&a, RC_INT) == 3, - "first alloc should skip no lower unreserved reg"); - EXPECT(cg_simple_regalloc_alloc(&a, RC_INT) == 7, - "second alloc should use remaining unreserved reg"); - EXPECT(cg_simple_regalloc_alloc(&a, RC_INT) == (Reg)REG_NONE, - "reserved reg should not be allocated"); -} - static void opt_inline_direct_wrapper(void) { TestCtx tc; tc_init(&tc); @@ -7110,7 +7085,6 @@ int main(void) { opt_inline_refuses_recursive_and_unsupported(); opt_inline_caller_growth_cap(); opt_inline_cleanup_promotes_cloned_param_frame(); - simple_regalloc_reports_exact_used_regs(); if (g_fails) { fprintf(stderr, "opt tests: %d failed (%d checks)\n", g_fails, g_checks); return 1;