ir3: Add foreach_dst/foreach_dst_n
authorConnor Abbott <cwabbott0@gmail.com>
Mon, 21 Jun 2021 15:46:33 +0000 (17:46 +0200)
committerMarge Bot <eric+marge@anholt.net>
Tue, 29 Jun 2021 08:08:12 +0000 (08:08 +0000)
And cleanup a few places I know of that are open-coding it

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11565>

src/freedreno/.clang-format
src/freedreno/ir3/ir3.c
src/freedreno/ir3/ir3.h
src/freedreno/ir3/ir3_array_to_ssa.c
src/freedreno/ir3/ir3_print.c
src/freedreno/ir3/ir3_sched.c
src/freedreno/ir3/ir3_validate.c

index 461df1c..4a5a4ba 100644 (file)
@@ -67,6 +67,8 @@ ForEachMacros:
   - foreach_sched_node
   - foreach_src
   - foreach_src_n
+  - foreach_dst
+  - foreach_dst_n
   - foreach_ssa_use
   - foreach_ssa_srcp_n
   - foreach_ssa_srcp
index f401745..4680be6 100644 (file)
@@ -426,7 +426,6 @@ struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr)
        struct ir3_instruction *new_instr = instr_create(instr->block, instr->opc,
                        instr->dsts_count, instr->srcs_count);
        struct ir3_register **dsts, **srcs;
-       unsigned i;
 
        dsts = new_instr->dsts;
        srcs = new_instr->srcs;
@@ -439,15 +438,13 @@ struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr)
        /* clone registers: */
        new_instr->dsts_count = 0;
        new_instr->srcs_count = 0;
-       for (i = 0; i < instr->dsts_count; i++) {
-               struct ir3_register *reg = instr->dsts[i];
+       foreach_dst (reg, instr) {
                struct ir3_register *new_reg = ir3_dst_create(new_instr, reg->num, reg->flags);
                *new_reg = *reg;
                if (new_reg->instr)
                        new_reg->instr = new_instr;
        }
-       for (i = 0; i < instr->srcs_count; i++) {
-               struct ir3_register *reg = instr->srcs[i];
+       foreach_src (reg, instr) {
                struct ir3_register *new_reg = ir3_src_create(new_instr, reg->num, reg->flags);
                *new_reg = *reg;
        }
index 4145285..5bbdc4c 100644 (file)
@@ -1363,6 +1363,17 @@ ir3_try_swap_signedness(opc_t opc, bool *can_swap)
 #define foreach_src(__srcreg, __instr) \
        foreach_src_n(__srcreg, __i, __instr)
 
+/* iterator for an instructions's destinations (reg), also returns dst #: */
+#define foreach_dst_n(__dstreg, __n, __instr) \
+       if ((__instr)->dsts_count) \
+               for (struct ir3_register *__dstreg = (void *)~0; __dstreg; __dstreg = NULL) \
+                       for (unsigned __cnt = (__instr)->dsts_count, __n = 0; __n < __cnt; __n++) \
+                               if ((__dstreg = (__instr)->dsts[__n]))
+
+/* iterator for an instructions's destinations (reg): */
+#define foreach_dst(__dstreg, __instr) \
+       foreach_dst_n(__dstreg, __i, __instr)
+
 static inline unsigned __ssa_src_cnt(struct ir3_instruction *instr)
 {
        return instr->srcs_count + instr->deps_count;
index 499002a..6fdf0ee 100644 (file)
@@ -221,11 +221,11 @@ ir3_array_to_ssa(struct ir3 *ir)
 
        foreach_block (block, &ir->block_list) {
                foreach_instr (instr, &block->instr_list) {
-                       for (unsigned i = 0; i < instr->dsts_count; i++) {
-                               if (instr->dsts[i]->flags & IR3_REG_ARRAY) {
+                       foreach_dst (dst, instr) {
+                               if (dst->flags & IR3_REG_ARRAY) {
                                        struct array_state *state =
-                                               get_state(&ctx, block, instr->dsts[i]->array.id);
-                                       state->live_out_definition = instr->dsts[i];
+                                               get_state(&ctx, block, dst->array.id);
+                                       state->live_out_definition = dst;
                                }
                        }
                }
@@ -236,8 +236,7 @@ ir3_array_to_ssa(struct ir3 *ir)
                        if (instr->opc == OPC_META_PHI)
                                continue;
 
-                       for (unsigned i = 0; i < instr->dsts_count; i++) {
-                               struct ir3_register *reg = instr->dsts[i];
+                       foreach_dst (reg, instr) {
                                if ((reg->flags & IR3_REG_ARRAY) && !reg->tied) {
                                        struct ir3_array *arr = ir3_lookup_array(ir, reg->array.id);
 
@@ -245,8 +244,7 @@ ir3_array_to_ssa(struct ir3 *ir)
                                        read_value_beginning(&ctx, block, arr);
                                }
                        }
-                       for (unsigned i = 0; i < instr->srcs_count; i++) {
-                               struct ir3_register *reg = instr->srcs[i];
+                       foreach_src (reg, instr) {
                                if ((reg->flags & IR3_REG_ARRAY) && !reg->def) {
                                        struct ir3_array *arr = ir3_lookup_array(ir, reg->array.id);
 
@@ -279,8 +277,7 @@ ir3_array_to_ssa(struct ir3 *ir)
                                        instr->srcs[i] = lookup_value(instr->srcs[i]);
                                }
                        } else {
-                               for (unsigned i = 0; i < instr->dsts_count; i++) {
-                                       struct ir3_register *reg = instr->dsts[i];
+                               foreach_dst (reg, instr) {
                                        if ((reg->flags & IR3_REG_ARRAY)) {
                                                if (!reg->tied) {
                                                        struct ir3_register *def =
@@ -291,8 +288,7 @@ ir3_array_to_ssa(struct ir3 *ir)
                                                reg->flags |= IR3_REG_SSA;
                                        }
                                }
-                               for (unsigned i = 0; i < instr->srcs_count; i++) {
-                                       struct ir3_register *reg = instr->srcs[i];
+                               foreach_src (reg, instr) {
                                        if ((reg->flags & IR3_REG_ARRAY)) {
                                                /* It is assumed that before calling
                                                 * ir3_array_to_ssa(), reg->def was set to the
index db2190a..14fc4ef 100644 (file)
@@ -275,8 +275,7 @@ print_instr(struct log_stream *stream, struct ir3_instruction *instr, int lvl)
 
        if (!is_flow(instr) || instr->opc == OPC_END || instr->opc == OPC_CHMASK) {
                bool first = true;
-               for (unsigned i = 0; i < instr->dsts_count; i++) {
-                       struct ir3_register *reg = instr->dsts[i];
+               foreach_dst (reg, instr) {
                        if (dest_regs(instr) == 0)
                                continue;
                        if (!first)
@@ -284,8 +283,7 @@ print_instr(struct log_stream *stream, struct ir3_instruction *instr, int lvl)
                        print_reg_name(stream, instr, reg, true);
                        first = false;
                }
-               for (unsigned i = 0; i < instr->srcs_count; i++) {
-                       struct ir3_register *reg = instr->srcs[i];
+               foreach_src (reg, instr) {
                        if (!first)
                                mesa_log_stream_printf(stream, ", ");
                        print_reg_name(stream, instr, reg, false);
index 69281d3..08ce91c 100644 (file)
@@ -1235,12 +1235,12 @@ get_array_id(struct ir3_instruction *instr)
         * src or dst, ir3_cp should enforce this.
         */
 
-       for (unsigned i = 0; i < instr->dsts_count; i++)
-               if (instr->dsts[i]->flags & IR3_REG_ARRAY)
-                       return instr->dsts[i]->array.id;
-       for (unsigned i = 0; i < instr->srcs_count; i++)
-               if (instr->srcs[i]->flags & IR3_REG_ARRAY)
-                       return instr->srcs[i]->array.id;
+       foreach_dst (dst, instr)
+               if (dst->flags & IR3_REG_ARRAY)
+                       return dst->array.id;
+       foreach_src (src, instr)
+               if (src->flags & IR3_REG_ARRAY)
+                       return src->array.id;
 
        unreachable("this was unexpected");
 }
index 38e3eca..d091031 100644 (file)
@@ -75,8 +75,8 @@ validate_src(struct ir3_validate_ctx *ctx, struct ir3_instruction *instr,
        if (reg->tied) {
                validate_assert(ctx, reg->tied->tied == reg);
                bool found = false;
-               for (unsigned i = 0; i < instr->dsts_count; i++) {
-                       if (instr->dsts[i] == reg->tied) {
+               foreach_dst (dst, instr) {
+                       if (dst == reg->tied) {
                                found = true;
                                break;
                        }
@@ -124,8 +124,8 @@ validate_dst(struct ir3_validate_ctx *ctx, struct ir3_instruction *instr,
                        validate_assert(ctx, reg->tied->size == reg->size);
                }
                bool found = false;
-               for (unsigned i = 0; i < instr->srcs_count; i++) {
-                       if (instr->srcs[i] == reg->tied) {
+               foreach_src (src, instr) {
+                       if (src == reg->tied) {
                                found = true;
                                break;
                        }