X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=ir.c;h=05f21d5c700365ad2e03183b23c627752e5dfa72;hb=cd543aad5e2291ca1e1d86d45728fafadb31776b;hp=8aef17ff6258743b328c0f1f92e615cbcf4489ca;hpb=ae97ff7a80cd7b048fd6203789a468c2f7bccd01;p=xonotic%2Fgmqcc.git diff --git a/ir.c b/ir.c index 8aef17f..05f21d5 100644 --- a/ir.c +++ b/ir.c @@ -320,6 +320,8 @@ ir_builder* ir_builder_new(const char *modulename) self->nil = ir_value_var("nil", store_value, TYPE_NIL); self->nil->cvq = CV_CONST; + self->reserved_va_count = NULL; + return self; } @@ -418,6 +420,13 @@ ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype return ve; } +ir_value* ir_builder_get_va_count(ir_builder *self) +{ + if (self->reserved_va_count) + return self->reserved_va_count; + return (self->reserved_va_count = ir_builder_create_global(self, "reserved:va_count", TYPE_FLOAT)); +} + ir_value* ir_builder_get_field(ir_builder *self, const char *name) { return (ir_value*)util_htget(self->htfields, name); @@ -476,6 +485,8 @@ ir_function* ir_function_new(ir_builder* owner, int outtype) self->values = NULL; self->locals = NULL; + self->max_varargs = NULL; + self->code_function_def = -1; self->allocated_locals = 0; self->globaltemps = 0; @@ -1018,6 +1029,7 @@ ir_value* ir_value_var(const char *name, int storetype, int vtype) self->fieldtype = TYPE_VOID; self->outtype = TYPE_VOID; self->store = storetype; + self->flags = 0; self->reads = NULL; self->writes = NULL; @@ -1991,9 +2003,15 @@ static void ir_block_enumerate(ir_block *self, size_t *_eid) void ir_function_enumerate(ir_function *self) { size_t i; - size_t instruction_id = 1; + size_t instruction_id = 0; for (i = 0; i < vec_size(self->blocks); ++i) { + /* each block now gets an additional "entry" instruction id + * we can use to avoid point-life issues + */ + self->blocks[i]->entry_id = instruction_id; + ++instruction_id; + self->blocks[i]->eid = i; self->blocks[i]->run_id = 0; ir_block_enumerate(self->blocks[i], &instruction_id); @@ -2160,16 +2178,6 @@ static bool ir_function_allocator_assign(ir_function *self, function_allocator * return true; } -static bool ir_value_has_point_life(const ir_value *v) -{ - size_t i, vs = vec_size(v->life); - for (i = 0; i < vs; ++i) { - if (v->life[i].start == v->life[i].end) - return true; - } - return false; -} - bool ir_function_allocate_locals(ir_function *self) { size_t i; @@ -2196,9 +2204,7 @@ bool ir_function_allocate_locals(ir_function *self) for (i = 0; i < vec_size(self->locals); ++i) { v = self->locals[i]; - if (ir_value_has_point_life(v)) - v->unique_life = true; - if (!OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) { + if ((self->flags & IR_FLAG_MASK_NO_LOCAL_TEMPS) || !OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) { v->locked = true; v->unique_life = true; } @@ -2214,8 +2220,6 @@ bool ir_function_allocate_locals(ir_function *self) v = self->locals[i]; if (!vec_size(v->life)) continue; - if (ir_value_has_point_life(v)) - v->unique_life = true; if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v)) goto error; } @@ -2227,15 +2231,6 @@ bool ir_function_allocate_locals(ir_function *self) if (!vec_size(v->life)) continue; - if (ir_value_has_point_life(v)) { - /* happens on free ternarys like: - if (x) { - cond ? a : b; - } - irerror(v->context, "internal error: point life SSA value leaked: %s", v->name); - */ - v->unique_life = true; - } /* CALL optimization: * If the value is a parameter-temp: 1 write, 1 read from a CALL @@ -2481,19 +2476,6 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change /* See which operands are read and write operands */ ir_op_read_write(instr->opcode, &read, &write); - if (instr->opcode == INSTR_MUL_VF) - { - /* the float source will get an additional lifetime */ - tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1); - *changed = *changed || tempbool; - } - else if (instr->opcode == INSTR_MUL_FV) - { - /* the float source will get an additional lifetime */ - tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1); - *changed = *changed || tempbool; - } - /* Go through the 3 main operands * writes first, then reads */ @@ -2567,6 +2549,25 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change } } + if (instr->opcode == INSTR_MUL_VF) + { + value = instr->_ops[2]; + /* the float source will get an additional lifetime */ + if (ir_value_life_merge(value, instr->eid+1)) + *changed = true; + if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1)) + *changed = true; + } + else if (instr->opcode == INSTR_MUL_FV) + { + value = instr->_ops[1]; + /* the float source will get an additional lifetime */ + if (ir_value_life_merge(value, instr->eid+1)) + *changed = true; + if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1)) + *changed = true; + } + for (o = 0; o < 3; ++o) { if (!instr->_ops[o]) /* no such operand */ @@ -2635,8 +2636,10 @@ static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *change tempbool = ir_block_living_add_instr(self, instr->eid); /*con_err( "living added values\n");*/ *changed = *changed || tempbool; - } + /* the "entry" instruction ID */ + tempbool = ir_block_living_add_instr(self, self->entry_id); + *changed = *changed || tempbool; if (self->run_id == self->owner->run_id) return true; @@ -2750,7 +2753,6 @@ static bool gen_blocks_recursive(ir_function *func, ir_block *block) size_t stidx; size_t i; -tailcall: block->generated = true; block->code_start = vec_size(code_statements); for (i = 0; i < vec_size(block->instr); ++i) @@ -2767,10 +2769,8 @@ tailcall: /* for uncoditional jumps, if the target hasn't been generated * yet, we generate them right here. */ - if (!target->generated) { - block = target; - goto tailcall; - } + if (!target->generated) + return gen_blocks_recursive(func, target); /* otherwise we generate a jump instruction */ stmt.opcode = INSTR_GOTO; @@ -2808,16 +2808,12 @@ tailcall: code_push_statement(&stmt, instr->context.line); } if (!ontrue->generated) { - if (onfalse->generated) { - block = ontrue; - goto tailcall; - } + if (onfalse->generated) + return gen_blocks_recursive(func, ontrue); } if (!onfalse->generated) { - if (ontrue->generated) { - block = onfalse; - goto tailcall; - } + if (ontrue->generated) + return gen_blocks_recursive(func, onfalse); } /* neither ontrue nor onfalse exist */ stmt.opcode = INSTR_IFNOT; @@ -2871,8 +2867,7 @@ tailcall: code_pop_statement(); } /* if not, generate now */ - block = onfalse; - goto tailcall; + return gen_blocks_recursive(func, onfalse); } if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) @@ -3181,6 +3176,42 @@ static bool gen_function_extparam_copy(ir_function *self) return true; } +static bool gen_function_varargs_copy(ir_function *self) +{ + size_t i, ext, numparams, maxparams; + + ir_builder *ir = self->owner; + ir_value *ep; + prog_section_statement stmt; + + numparams = vec_size(self->params); + if (!numparams) + return true; + + stmt.opcode = INSTR_STORE_V; + stmt.o3.s1 = 0; + maxparams = numparams + self->max_varargs; + for (i = numparams; i < maxparams; ++i) { + if (i <= 8) { + stmt.o1.u1 = OFS_PARM0 + 3*i; + stmt.o2.u1 = ir_value_code_addr(self->locals[i]); + code_push_statement(&stmt, self->context.line); + continue; + } + ext = i - 8; + if (ext >= vec_size(ir->extparams)) + ir_gen_extparam(ir); + + ep = ir->extparams[ext]; + + stmt.o1.u1 = ir_value_code_addr(ep); + stmt.o2.u1 = ir_value_code_addr(self->locals[i]); + code_push_statement(&stmt, self->context.line); + } + + return true; +} + static bool gen_function_locals(ir_builder *ir, ir_value *global) { prog_section_function *def; @@ -3262,6 +3293,10 @@ static bool gen_global_function_code(ir_builder *ir, ir_value *global) irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name); return false; } + if (irfun->max_varargs && !gen_function_varargs_copy(irfun)) { + irerror(irfun->context, "Failed to generate vararg-copy code for function %s", irfun->name); + return false; + } if (!gen_function_code(irfun)) { irerror(irfun->context, "Failed to generate code for function %s", irfun->name); return false; @@ -3340,6 +3375,7 @@ static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool isloc pushdef = true; if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) && + !(global->flags & IR_FLAG_INCLUDE_DEF) && (global->name[0] == '#' || global->cvq == CV_CONST)) { pushdef = false; @@ -3727,6 +3763,10 @@ void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...)) oprintf("endmodule %s\n", b->name); } +static const char *storenames[] = { + "[global]", "[local]", "[param]", "[value]", "[return]" +}; + void ir_function_dump(ir_function *f, char *ind, int (*oprintf)(const char*, ...)) { @@ -3757,9 +3797,12 @@ void ir_function_dump(ir_function *f, char *ind, attr = "unique "; else if (v->locked) attr = "locked "; - oprintf("%s\t%s: %s %s%s@%i ", ind, v->name, type_name[v->vtype], + oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype], + storenames[v->store], attr, (v->callparam ? "callparam " : ""), (int)v->code.local); + if (!v->life) + oprintf("[null]"); for (l = 0; l < vec_size(v->life); ++l) { oprintf("[%i,%i] ", v->life[l].start, v->life[l].end); } @@ -3768,13 +3811,7 @@ void ir_function_dump(ir_function *f, char *ind, ir_value *vm = v->members[m]; if (!vm) continue; - if (vm->unique_life && vm->locked) - attr = "unique,locked "; - else if (vm->unique_life) - attr = "unique "; - else if (vm->locked) - attr = "locked "; - oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local); + oprintf("%s\t%s: @%i ", ind, vm->name, (int)vm->code.local); for (l = 0; l < vec_size(vm->life); ++l) { oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end); } @@ -3791,9 +3828,12 @@ void ir_function_dump(ir_function *f, char *ind, attr = "unique "; else if (v->locked) attr = "locked "; - oprintf("%s\t%s: %s %s%s@%i ", ind, v->name, type_name[v->vtype], + oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype], + storenames[v->store], attr, (v->callparam ? "callparam " : ""), (int)v->code.local); + if (!v->life) + oprintf("[null]"); for (l = 0; l < vec_size(v->life); ++l) { oprintf("[%i,%i] ", v->life[l].start, v->life[l].end); } @@ -3837,6 +3877,8 @@ void ir_block_dump(ir_block* b, char *ind, oprintf("%s:%s\n", ind, b->label); strncat(ind, "\t", IND_BUFSZ); + if (b->instr && b->instr[0]) + oprintf("%s (%i) [entry]\n", ind, (int)(b->instr[0]->eid-1)); for (i = 0; i < vec_size(b->instr); ++i) ir_instr_dump(b->instr[i], ind, oprintf); ind[strlen(ind)-1] = 0;