diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common.h | 9 | ||||
-rw-r--r-- | src/compile/compile.c | 70 | ||||
-rw-r--r-- | src/ejit.c | 8 |
3 files changed, 87 insertions, 0 deletions
diff --git a/src/common.h b/src/common.h index 1eb762e..41a17cf 100644 --- a/src/common.h +++ b/src/common.h @@ -4,6 +4,14 @@ #include <ejit/ejit.h> #include <stdbool.h> +struct barrier_tuple { + size_t start, end; +}; + +#define VEC_TYPE struct barrier_tuple +#define VEC_NAME barriers +#include <conts/vec.h> + #define VEC_TYPE struct ejit_arg #define VEC_NAME args #include <conts/vec.h> @@ -291,6 +299,7 @@ struct ejit_func { struct types sign; struct insns insns; struct labels labels; + struct barriers barriers; enum ejit_type rtype; struct gpr_stats gpr; diff --git a/src/compile/compile.c b/src/compile/compile.c index f552ae3..8e0e250 100644 --- a/src/compile/compile.c +++ b/src/compile/compile.c @@ -2856,6 +2856,18 @@ static int spill_cost_sort(struct alive_slot *a, struct alive_slot *b) return a->cost < b->cost; } +/* sort barriers according to starting address, smallest to largest */ +static int barrier_sort(struct barrier_tuple *a, struct barrier_tuple *b) +{ + if (a->start < b->start) + return -1; + + if (a->start > b->start) + return 1; + + return a->end - b->end; +} + /* slightly more parameters than I would like but I guess it's fine */ static void calculate_alive(struct alive *alive, size_t idx, size_t prio, size_t start, size_t end, size_t *rno, @@ -2924,6 +2936,40 @@ static void linear_gpr_alloc(struct ejit_func *f) } } +static void extend_gpr_lifetime(struct gpr_stat *gpr, struct barriers *barriers, size_t bi) +{ + if (bi >= barriers_len(barriers)) + return; + + struct barrier_tuple *barrier = barriers_at(barriers, bi); + if (gpr->end < barrier->start) + return; + + /* we cross a barrier */ + gpr->end = gpr->end > barrier->end ? gpr->end : barrier->end; + + /* check if we cross the next barrier as well, + * hopefully gets optimized into a tail call */ + return extend_gpr_lifetime(gpr, barriers, bi + 1); +} + +static void extend_fpr_lifetime(struct fpr_stat *fpr, struct barriers *barriers, size_t bi) +{ + if (bi >= barriers_len(barriers)) + return; + + struct barrier_tuple *barrier = barriers_at(barriers, bi); + if (fpr->end < barrier->start) + return; + + /* we cross a barrier */ + fpr->end = fpr->end > barrier->end ? fpr->end : barrier->end; + + /* check if we cross the next barrier as well, + * hopefully gets optimized into a tail call */ + return extend_fpr_lifetime(fpr, barriers, bi + 1); +} + /* there's a fair bit of repetition between this and the gpr case, hmm */ static void assign_gprs(struct ejit_func *f) { @@ -2937,8 +2983,20 @@ static void assign_gprs(struct ejit_func *f) struct alive_slot a = {.r = -1, .cost = 0, .idx = 0}; alive_append(&alive, a); + /* barrier index, keeps track of earliest possible barrier we can be + * dealing with. Since register start addresses grow upward, we can + * fairly easily keep track of which barrier a register cannot cross */ + size_t bi = 0; for (size_t gi = 0; gi < gpr_stats_len(&f->gpr); ++gi) { struct gpr_stat *gpr = gpr_stats_at(&f->gpr, gi); + + extend_gpr_lifetime(gpr, &f->barriers, bi); + if (bi < barriers_len(&f->barriers)) { + struct barrier_tuple *barrier = barriers_at(&f->barriers, bi); + if (gpr->start >= barrier->start) + bi++; + } + calculate_alive(&alive, gi, gpr->prio, gpr->start, gpr->end, &gpr->rno, &f->gpr, gpr_dead); @@ -2989,8 +3047,17 @@ static void assign_fprs(struct ejit_func *f) struct alive_slot a = {.r = -1, .cost = 0, .idx = 0}; alive_append(&alive, a); + size_t bi = 0; for (size_t fi = 0; fi < fpr_stats_len(&f->fpr); ++fi) { struct fpr_stat *fpr = fpr_stats_at(&f->fpr, fi); + + extend_fpr_lifetime(fpr, &f->barriers, bi); + if (bi < barriers_len(&f->barriers)) { + struct barrier_tuple *barrier = barriers_at(&f->barriers, bi); + if (fpr->start >= barrier->start) + bi++; + } + calculate_alive(&alive, fi, fpr->prio, fpr->start, fpr->end, &fpr->fno, &f->fpr, fpr_dead); @@ -3035,6 +3102,9 @@ bool ejit_compile(struct ejit_func *f, bool use_64, bool im_scawed) if (!init_jit()) return false; + /* sort barriers so they can be used to extend register life times in + * loops */ + barriers_sort(&f->barriers, barrier_sort); assign_gprs(f); assign_fprs(f); @@ -341,6 +341,7 @@ struct ejit_func *ejit_create_func(enum ejit_type rtype, size_t argc, f->sign = types_create(0); f->insns = insns_create(0); f->labels = labels_create(0); + f->barriers = barriers_create(0); f->gpr = gpr_stats_create(0); f->fpr = fpr_stats_create(0); f->arena = NULL; @@ -434,6 +435,7 @@ void ejit_destroy_func(struct ejit_func *f) types_destroy(&f->sign); insns_destroy(&f->insns); labels_destroy(&f->labels); + barriers_destroy(&f->barriers); gpr_stats_destroy(&f->gpr); fpr_stats_destroy(&f->fpr); free(f); @@ -452,6 +454,12 @@ void ejit_patch(struct ejit_func *f, struct ejit_reloc r, struct ejit_label l) /** @todo some assert that checks the opcode? */ i.r0 = l.addr; *insns_at(&f->insns, r.insn) = i; + + struct barrier_tuple tuple = { + .start = r.insn > l.addr ? l.addr : r.insn, + .end = r.insn > l.addr ? r.insn : l.addr + }; + barriers_append(&f->barriers, tuple); } void ejit_taili(struct ejit_func *s, struct ejit_func *f, |