From e38bdecd21c8bb2103b14bb1da813a991df9c67f Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 20 Dec 2012 23:48:41 +0100 Subject: [PATCH 1/2] Be more correct with sizes; use a better local-alloc strategy --- gmqcc.h | 1 - ir.c | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/gmqcc.h b/gmqcc.h index 1adcd52..c14778c 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -290,7 +290,6 @@ enum { #define CV_WRONG 0x8000 extern const char *type_name [TYPE_COUNT]; -extern size_t type_sizeof [TYPE_COUNT]; extern uint16_t type_store_instr [TYPE_COUNT]; extern uint16_t field_store_instr[TYPE_COUNT]; diff --git a/ir.c b/ir.c index 18b6387..c5cf508 100644 --- a/ir.c +++ b/ir.c @@ -45,7 +45,7 @@ const char *type_name[TYPE_COUNT] = { "array" }; -size_t type_sizeof[TYPE_COUNT] = { +size_t type_sizeof_[TYPE_COUNT] = { 1, /* TYPE_VOID */ 1, /* TYPE_STRING */ 1, /* TYPE_FLOAT */ @@ -1038,6 +1038,13 @@ ir_value* ir_value_vector_member(ir_value *self, unsigned int member) return m; } +static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self) +{ + if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR) + return type_sizeof_[TYPE_VECTOR]; + return type_sizeof_[self->vtype]; +} + ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype) { ir_value *v = ir_value_var(name, storetype, vtype); @@ -2242,7 +2249,7 @@ typedef struct { static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var) { ir_value *slot; - size_t vsize = type_sizeof[var->vtype]; + size_t vsize = ir_value_sizeof(var); slot = ir_value_var("reg", store_global, var->vtype); if (!slot) @@ -2309,7 +2316,7 @@ bool ir_function_allocate_locals(ir_function *self) * will be required later when overlapping temps + locals */ if (a < vec_size(self->params) && - alloc.sizes[a] < type_sizeof[v->vtype]) + alloc.sizes[a] < ir_value_sizeof(v)) { continue; } @@ -2321,8 +2328,8 @@ bool ir_function_allocate_locals(ir_function *self) goto error; /* adjust size for this slot */ - if (alloc.sizes[a] < type_sizeof[v->vtype]) - alloc.sizes[a] = type_sizeof[v->vtype]; + if (alloc.sizes[a] < ir_value_sizeof(v)) + alloc.sizes[a] = ir_value_sizeof(v); self->values[i]->code.local = a; break; @@ -3045,7 +3052,7 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) if ((int32_t)i >= fun.nargs) fun.argsize[i] = 0; else - fun.argsize[i] = type_sizeof[irfun->params[i]]; + fun.argsize[i] = type_sizeof_[irfun->params[i]]; } fun.firstlocal = vec_size(code_globals); @@ -3061,21 +3068,27 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) if (vec_size(irfun->locals)) { ir_value *last = vec_last(irfun->locals); local_var_end = last->code.globaladdr; - if (last->vtype == TYPE_FIELD && last->fieldtype == TYPE_VECTOR) - local_var_end += type_sizeof[TYPE_VECTOR]; - else - local_var_end += type_sizeof[last->vtype]; + local_var_end += ir_value_sizeof(last); } for (i = 0; i < vec_size(irfun->values); ++i) { /* generate code.globaladdr for ssa values */ ir_value *v = irfun->values[i]; +#if 1 + ir_value_code_setaddr(v, fun.firstlocal + v->code.local); +#else ir_value_code_setaddr(v, local_var_end + v->code.local); +#endif } +#if 1 + for (i = vec_size(code_globals); i < fun.firstlocal + irfun->allocated_locals; ++i) + vec_push(code_globals, 0); +#else for (i = 0; i < irfun->allocated_locals; ++i) { /* fill the locals with zeros */ vec_push(code_globals, 0); } +#endif fun.locals = vec_size(code_globals) - fun.firstlocal; #else @@ -3352,14 +3365,14 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc vec_push(code_globals, iptr[0]); if (global->code.globaladdr < 0) return false; - for (d = 1; d < type_sizeof[global->vtype]; ++d) { + for (d = 1; d < type_sizeof_[global->vtype]; ++d) { vec_push(code_globals, iptr[d]); } } else { vec_push(code_globals, 0); if (global->code.globaladdr < 0) return false; - for (d = 1; d < type_sizeof[global->vtype]; ++d) { + for (d = 1; d < type_sizeof_[global->vtype]; ++d) { vec_push(code_globals, 0); } } @@ -3390,7 +3403,7 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc /* assume biggest type */ ir_value_code_setaddr(global, vec_size(code_globals)); vec_push(code_globals, 0); - for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i) + for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i) vec_push(code_globals, 0); return true; default: @@ -3403,7 +3416,7 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc static void ir_builder_prepare_field(ir_value *field) { - field->code.fieldaddr = code_alloc_field(type_sizeof[field->fieldtype]); + field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]); } static bool ir_builder_gen_field(ir_builder *self, ir_value *field) From 0bbc8a33509dfce06eae404ce2e008a4f119f675 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 20 Dec 2012 23:52:54 +0100 Subject: [PATCH 2/2] Remove old tempalloc code; disable -Olocaltemps for now --- ir.c | 46 ++++------------------------------------------ 1 file changed, 4 insertions(+), 42 deletions(-) diff --git a/ir.c b/ir.c index c5cf508..84832fe 100644 --- a/ir.c +++ b/ir.c @@ -2290,7 +2290,9 @@ bool ir_function_allocate_locals(ir_function *self) for (i = 0; i < vec_size(self->locals); ++i) { +#if 0 if (!OPTS_OPTIMIZATION(OPTIM_LOCALTEMPS)) +#endif self->locals[i]->unique_life = true; if (!function_allocator_alloc(&alloc, self->locals[i])) goto error; @@ -3029,9 +3031,6 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) ir_function *irfun; size_t i; -#ifndef NEW_ALLOC_STRAT - size_t local_var_end; -#endif if (!global->hasvalue || (!global->constval.vfunc)) { @@ -3057,41 +3056,6 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) fun.firstlocal = vec_size(code_globals); -#ifndef NEW_ALLOC_STRAT - local_var_end = fun.firstlocal; - for (i = 0; i < vec_size(irfun->locals); ++i) { - if (!ir_builder_gen_global(ir, irfun->locals[i], true)) { - irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name); - return false; - } - } - if (vec_size(irfun->locals)) { - ir_value *last = vec_last(irfun->locals); - local_var_end = last->code.globaladdr; - local_var_end += ir_value_sizeof(last); - } - for (i = 0; i < vec_size(irfun->values); ++i) - { - /* generate code.globaladdr for ssa values */ - ir_value *v = irfun->values[i]; -#if 1 - ir_value_code_setaddr(v, fun.firstlocal + v->code.local); -#else - ir_value_code_setaddr(v, local_var_end + v->code.local); -#endif - } -#if 1 - for (i = vec_size(code_globals); i < fun.firstlocal + irfun->allocated_locals; ++i) - vec_push(code_globals, 0); -#else - for (i = 0; i < irfun->allocated_locals; ++i) { - /* fill the locals with zeros */ - vec_push(code_globals, 0); - } -#endif - - fun.locals = vec_size(code_globals) - fun.firstlocal; -#else fun.locals = irfun->allocated_locals; for (i = 0; i < vec_size(irfun->locals); ++i) { if (!ir_builder_gen_global(ir, irfun->locals[i], true)) { @@ -3100,16 +3064,14 @@ static bool gen_global_function(ir_builder *ir, ir_value *global) } ir_value_code_setaddr(irfun->locals[i], fun.firstlocal + irfun->locals[i]->code.local); } - for (i = vec_size(code_globals) - fun.firstlocal; i < fun.locals; ++i) { - vec_push(code_globals, 0); - } for (i = 0; i < vec_size(irfun->values); ++i) { /* generate code.globaladdr for ssa values */ ir_value *v = irfun->values[i]; ir_value_code_setaddr(v, fun.firstlocal + v->code.local); } -#endif + for (i = vec_size(code_globals); i < fun.firstlocal + irfun->allocated_locals; ++i) + vec_push(code_globals, 0); if (irfun->builtin) fun.entry = irfun->builtin+1;