From 4cc2427ad64862d4327d0c7e017feca78937522a Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Sat, 8 Oct 2022 21:02:03 -0400 Subject: [PATCH] agx: Introduce agx_foreach_ssa_{src,dest} macros These are convenient iterators especially in the register allocator. Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/compiler/agx_compiler.h | 8 +++++ src/asahi/compiler/agx_liveness.c | 19 +++++----- src/asahi/compiler/agx_optimizer.c | 15 ++++---- src/asahi/compiler/agx_register_allocate.c | 41 +++++++++------------- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 6d734818575..fb079fcae68 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -549,6 +549,14 @@ agx_start_block(agx_context *ctx) #define agx_foreach_dest(ins, v) \ for (unsigned v = 0; v < ins->nr_dests; ++v) +#define agx_foreach_ssa_src(ins, v) \ + agx_foreach_src(ins, v) \ + if (ins->src[v].type == AGX_INDEX_NORMAL) + +#define agx_foreach_ssa_dest(ins, v) \ + agx_foreach_dest(ins, v) \ + if (ins->dest[v].type == AGX_INDEX_NORMAL) + /* Phis only come at the start so we stop as soon as we hit a non-phi */ #define agx_foreach_phi_in_block(block, v) \ agx_foreach_instr_in_block(block, v) \ diff --git a/src/asahi/compiler/agx_liveness.c b/src/asahi/compiler/agx_liveness.c index ccb89dcef69..75ff0dcef73 100644 --- a/src/asahi/compiler/agx_liveness.c +++ b/src/asahi/compiler/agx_liveness.c @@ -36,18 +36,15 @@ void agx_liveness_ins_update(BITSET_WORD *live, agx_instr *I) { - agx_foreach_dest(I, d) { - if (I->dest[d].type == AGX_INDEX_NORMAL) - BITSET_CLEAR(live, I->dest[d].value); - } + agx_foreach_ssa_dest(I, d) + BITSET_CLEAR(live, I->dest[d].value); - agx_foreach_src(I, s) { - if (I->src[s].type == AGX_INDEX_NORMAL) { - /* If the source is not live after this instruction, but becomes live - * at this instruction, this is the use that kills the source */ - I->src[s].kill = !BITSET_TEST(live, I->src[s].value); - BITSET_SET(live, I->src[s].value); - } + agx_foreach_ssa_src(I, s) { + /* If the source is not live after this instruction, but becomes live + * at this instruction, this is the use that kills the source + */ + I->src[s].kill = !BITSET_TEST(live, I->src[s].value); + BITSET_SET(live, I->src[s].value); } } diff --git a/src/asahi/compiler/agx_optimizer.c b/src/asahi/compiler/agx_optimizer.c index d5891ecc6e5..b9f612cd948 100644 --- a/src/asahi/compiler/agx_optimizer.c +++ b/src/asahi/compiler/agx_optimizer.c @@ -94,11 +94,10 @@ agx_compose_float_src(agx_index to, agx_index from) static void agx_optimizer_fmov(agx_instr **defs, agx_instr *ins) { - agx_foreach_src(ins, s) { + agx_foreach_ssa_src(ins, s) { agx_index src = ins->src[s]; - if (src.type != AGX_INDEX_NORMAL) continue; - agx_instr *def = defs[src.value]; + if (def == NULL) continue; /* happens for phis in loops */ if (!agx_is_fmov(def)) continue; if (def->saturate) continue; @@ -153,11 +152,10 @@ agx_optimizer_fmov_rev(agx_instr *I, agx_instr *use) static void agx_optimizer_copyprop(agx_instr **defs, agx_instr *I) { - agx_foreach_src(I, s) { + agx_foreach_ssa_src(I, s) { agx_index src = I->src[s]; - if (src.type != AGX_INDEX_NORMAL) continue; - agx_instr *def = defs[src.value]; + if (def == NULL) continue; /* happens for phis in loops */ if (def->op != AGX_OPCODE_MOV) continue; @@ -204,9 +202,8 @@ agx_optimizer_forward(agx_context *ctx) agx_foreach_instr_global(ctx, I) { struct agx_opcode_info info = agx_opcodes_info[I->op]; - agx_foreach_dest(I, d) { - if (I->dest[d].type == AGX_INDEX_NORMAL) - defs[I->dest[d].value] = I; + agx_foreach_ssa_dest(I, d) { + defs[I->dest[d].value] = I; } /* Optimize moves */ diff --git a/src/asahi/compiler/agx_register_allocate.c b/src/asahi/compiler/agx_register_allocate.c index 941ec1a97c2..4269b852215 100644 --- a/src/asahi/compiler/agx_register_allocate.c +++ b/src/asahi/compiler/agx_register_allocate.c @@ -70,7 +70,7 @@ agx_split_width(const agx_instr *I) enum agx_size width = ~0; agx_foreach_dest(I, d) { - if (agx_is_null(I->dest[d])) + if (I->dest[d].type == AGX_INDEX_NULL) continue; else if (width != ~0) assert(width == I->dest[d].size); @@ -188,8 +188,8 @@ agx_ra_assign_local(struct ra_ctx *rctx) } /* First, free killed sources */ - agx_foreach_src(I, s) { - if (I->src[s].type == AGX_INDEX_NORMAL && I->src[s].kill) { + agx_foreach_ssa_src(I, s) { + if (I->src[s].kill) { unsigned reg = ssa_to_reg[I->src[s].value]; unsigned count = ncomps[I->src[s].value]; @@ -200,14 +200,12 @@ agx_ra_assign_local(struct ra_ctx *rctx) /* Next, assign destinations one at a time. This is always legal * because of the SSA form. */ - agx_foreach_dest(I, d) { - if (I->dest[d].type == AGX_INDEX_NORMAL) { - unsigned count = agx_write_registers(I, d); - unsigned align = agx_size_align_16(I->dest[d].size); + agx_foreach_ssa_dest(I, d) { + unsigned count = agx_write_registers(I, d); + unsigned align = agx_size_align_16(I->dest[d].size); - assign_regs(rctx, I->dest[d], - find_regs(used_regs, count, align, rctx->bound)); - } + assign_regs(rctx, I->dest[d], + find_regs(used_regs, count, align, rctx->bound)); } } @@ -302,9 +300,7 @@ agx_ra(agx_context *ctx) BITSET_WORD *visited = calloc(BITSET_WORDS(ctx->alloc), sizeof(BITSET_WORD)); agx_foreach_instr_global(ctx, I) { - agx_foreach_dest(I, d) { - if (I->dest[d].type != AGX_INDEX_NORMAL) continue; - + agx_foreach_ssa_dest(I, d) { unsigned v = I->dest[d].value; assert(ncomps[v] == 0 && "broken SSA"); ncomps[v] = agx_write_registers(I, d); @@ -331,18 +327,14 @@ agx_ra(agx_context *ctx) } agx_foreach_instr_global(ctx, ins) { - agx_foreach_src(ins, s) { - if (ins->src[s].type == AGX_INDEX_NORMAL) { - unsigned v = ssa_to_reg[ins->src[s].value]; - ins->src[s] = agx_replace_index(ins->src[s], agx_register(v, ins->src[s].size)); - } + agx_foreach_ssa_src(ins, s) { + unsigned v = ssa_to_reg[ins->src[s].value]; + ins->src[s] = agx_replace_index(ins->src[s], agx_register(v, ins->src[s].size)); } - agx_foreach_dest(ins, d) { - if (ins->dest[d].type == AGX_INDEX_NORMAL) { - unsigned v = ssa_to_reg[ins->dest[d].value]; - ins->dest[d] = agx_replace_index(ins->dest[d], agx_register(v, ins->dest[d].size)); - } + agx_foreach_ssa_dest(ins, d) { + unsigned v = ssa_to_reg[ins->dest[d].value]; + ins->dest[d] = agx_replace_index(ins->dest[d], agx_register(v, ins->dest[d].size)); } } @@ -386,7 +378,8 @@ agx_ra(agx_context *ctx) /* Move the sources */ agx_foreach_dest(ins, i) { - if (agx_is_null(ins->dest[i])) continue; + if (ins->dest[i].type != AGX_INDEX_REGISTER) + continue; copies[n++] = (struct agx_copy) { .dest = agx_index_to_reg(ssa_to_reg, ins->dest[i]),