agx: Introduce agx_foreach_ssa_{src,dest} macros
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Sun, 9 Oct 2022 01:02:03 +0000 (21:02 -0400)
committerMarge Bot <emma+marge@anholt.net>
Thu, 10 Nov 2022 02:25:09 +0000 (02:25 +0000)
These are convenient iterators especially in the register allocator.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19590>

src/asahi/compiler/agx_compiler.h
src/asahi/compiler/agx_liveness.c
src/asahi/compiler/agx_optimizer.c
src/asahi/compiler/agx_register_allocate.c

index 6d73481..fb079fc 100644 (file)
@@ -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) \
index ccb89dc..75ff0dc 100644 (file)
 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);
    }
 }
 
index d5891ec..b9f612c 100644 (file)
@@ -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 */
index 941ec1a..4269b85 100644 (file)
@@ -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]),