Debian Codegraph @

src/panfrost/compiler/bifrost/bi_iterator_schedule.c

raw
1 /*
2 * Copyright (C) 2025 Google LLC.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/u_dynarray.h"
7 #include "compiler.h"
8
9 struct iterator {
10 bi_block *block;
11 bi_instr *instr;
12 };
13
14 static bool
15 phi_src_is_simple_iterator(bi_instr *phi, bi_instr *phi_src, bi_block *block)
16 {
17 if (phi_src == NULL || block == NULL || phi_src->nr_dests != 1 ||
18 phi_src->op == BI_OPCODE_PHI) {
19 return false;
20 }
21 bi_foreach_src(phi_src, idx) {
22 if (bi_is_equiv(phi->dest[0], phi_src->src[idx]))
23 /* If phi_src uses phi, then phi_src is a simple iterator */
24 return true;
25 }
26 return false;
27 }
28
29 static bi_instr *
30 get_new_iterator_position(bi_block *block, bi_instr *instr)
31 {
32 bi_instr *prev = NULL;
33 bi_foreach_instr_in_block_from(block, I, instr) {
34 if (bi_is_branch(I))
35 return prev;
36 bi_foreach_src(I, s) {
37 if (bi_is_equiv(instr->dest[0], I->src[s]))
38 return prev;
39 }
40 prev = I;
41 }
42 return prev;
43 }
44
45 /* Try to move iterator as close to the end of the block as possible. The goal
46 * is to avoid the iterator being used after being updated, to prevent the need
47 * for an extra move instruction.
48 *
49 * Example:
50 * 1 = PHI <>, 2
51 * ...
52 * 2 = IADD_IMM.i32 1, index:1
53 * 3 = ICMP_OR.u32.ge.m1 1, ...
54 *
55 * Without this pass, after register allocation it can lead to:
56 * r1 = IADD_IMM.i32 r0, index:1
57 * r2 = ICMP_OR.u32.ge.m1 r0, ...
58 * r0 = MOV.i32 r1
59 *
60 * With this pass, we can get:
61 * r1 = ICMP_OR.u32.ge.m1 r0, ...
62 * r0 = IADD_IMM.i32 r0, index:1
63 *
64 */
65 void
66 bi_iterator_schedule(bi_context *ctx)
67 {
68 bi_instr **bi_index_to_instr = calloc(ctx->ssa_alloc, sizeof(bi_instr *));
69 bi_block **bi_index_to_block = calloc(ctx->ssa_alloc, sizeof(bi_block *));
70 bi_foreach_block(ctx, block) {
71 bi_foreach_instr_in_block(block, instr) {
72 bi_foreach_ssa_dest(instr, idx) {
73 uint32_t value = instr->dest[idx].value;
74 bi_index_to_instr[value] = instr;
75 bi_index_to_block[value] = block;
76 }
77 }
78 }
79
80 struct util_dynarray iterators = UTIL_DYNARRAY_INIT;
81 bi_foreach_instr_global(ctx, phi) {
82 if (phi->op != BI_OPCODE_PHI || phi->nr_dests != 1)
83 continue;
84
85 bi_foreach_ssa_src(phi, phi_src_idx) {
86 uint32_t value = phi->src[phi_src_idx].value;
87 struct iterator iterator = {.instr = bi_index_to_instr[value],
88 .block = bi_index_to_block[value]};
89 if (!phi_src_is_simple_iterator(phi, iterator.instr, iterator.block))
90 continue;
91 util_dynarray_append(&iterators, iterator);
92 }
93 }
94
95 util_dynarray_foreach(&iterators, struct iterator, iterator)
96 {
97 bi_instr *instr = iterator->instr;
98 bi_instr *prev = get_new_iterator_position(iterator->block, instr);
99 if (prev == NULL || prev == instr)
100 continue;
101 bi_remove_instruction(instr);
102 list_add(&instr->link, &prev->link);
103 }
104 util_dynarray_fini(&iterators);
105
106 free(bi_index_to_instr);
107 free(bi_index_to_block);
108 }