Debian Codegraph @

src/panfrost/compiler/bifrost/bi_opt_control_flow.c

raw
1 /*
2 * Copyright (C) 2025 Google LLC.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/set.h"
7 #include "compiler.h"
8
9 /* A simple block is defined here as:
10 * - a block with only 1 predecessor and 1 successor
11 * - a block with no instruction or a single branch instruction (in which case
12 * the branch condition does not matter as the block has only 1 successor).
13 */
14 static bool
15 is_simple_block(bi_block *block)
16 {
17 assert(block != NULL);
18 if (bi_num_successors(block) != 1 || bi_num_predecessors(block) != 1)
19 return false;
20 if (list_is_empty(&block->instructions))
21 return true;
22 if (!list_is_singular(&block->instructions))
23 return false;
24 return bi_is_branch(list_first_entry(&block->instructions, bi_instr, link));
25 }
26
27 static bi_block *
28 get_simple_block_predecessor(bi_block *block)
29 {
30 assert(is_simple_block(block));
31 bi_foreach_predecessor(block, pred) {
32 return *pred;
33 }
34 /* Because block is a 'simple block' we should not be able to get here. */
35 UNREACHABLE("We should not get here");
36 }
37
38 static void
39 remove_simple_block(bi_block *block)
40 {
41 assert(is_simple_block(block));
42 bi_block *pred = get_simple_block_predecessor(block);
43 bi_block *succ = block->successors[0];
44
45 uint8_t block_idx = pred->successors[0] == block ? 0 : 1;
46 pred->successors[block_idx] = succ;
47 if (pred->successors[block_idx ^ 0x1] == succ)
48 pred->successors[1] = NULL;
49
50 struct util_dynarray new_preds;
51 util_dynarray_init(&new_preds, succ);
52 bi_foreach_predecessor(succ, succ_pred) {
53 bi_block *append = *succ_pred;
54 /* replace 'block' by 'pred'. If 'pred' is seen, do not add it as it will
55 * already be added in replacement of 'block'. */
56 if (*succ_pred == block)
57 append = pred;
58 else if (*succ_pred == pred)
59 continue;
60 util_dynarray_append(&new_preds, append);
61 }
62 succ->predecessors = new_preds;
63 block->link.prev->next = block->link.next;
64 block->link.next->prev = block->link.prev;
65 }
66
67 static bool
68 inverse_cmpf(enum bi_cmpf *cond)
69 {
70 switch (*cond) {
71 case BI_CMPF_EQ:
72 *cond = BI_CMPF_NE;
73 break;
74 case BI_CMPF_GT:
75 *cond = BI_CMPF_LE;
76 break;
77 case BI_CMPF_GE:
78 *cond = BI_CMPF_LT;
79 break;
80 case BI_CMPF_NE:
81 *cond = BI_CMPF_EQ;
82 break;
83 case BI_CMPF_LT:
84 *cond = BI_CMPF_GE;
85 break;
86 case BI_CMPF_LE:
87 *cond = BI_CMPF_GT;
88 break;
89 case BI_CMPF_GTLT:
90 return false;
91 default:
92 UNREACHABLE("unexpected condition");
93 }
94 return true;
95 }
96
97 static bool
98 try_remove_simple_block(bi_block *prev, bi_block *block, bi_block *next,
99 struct set *blocks_seen)
100 {
101 if (block == NULL)
102 return false;
103 _mesa_set_add(blocks_seen, block);
104
105 if (!is_simple_block(block))
106 return false;
107 bi_block *pred = get_simple_block_predecessor(block);
108 bi_block *succ = block->successors[0];
109
110 bi_instr *last_instr =
111 list_last_entry(&(pred->instructions), bi_instr, link);
112 bool last_instr_is_branch = bi_is_branch(last_instr);
113
114 /* if 'succ' has already been seen, it means that the jump between 'block'
115 * and 'succ' is a backward jump. This kind of jump can lead to issue
116 * regarding reconvergence
117 * (https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36021#note_3020690).
118 * Thus we don't remove those simple block to avoid reconvergence issue.
119 */
120 bool succ_seen = _mesa_set_search(blocks_seen, succ) != NULL;
121
122 if (last_instr_is_branch && !succ_seen &&
123 (last_instr->branch_target == block ||
124 (pred == prev && last_instr->branch_target == next &&
125 inverse_cmpf(&last_instr->cmpf)))) {
126 /* Remove 'simple_block' when either:
127 * - 'pred' jumps to 'simple_block'
128 * - or:
129 * - 'pred' is the previous block thus it falls through 'simple block'
130 * - 'pred' jumps to 'next'
131 * - We can rework the condition to have:
132 * - 'pred' jumping to 'simple block' successor
133 * - 'pred' falls through 'next'.
134 */
135 last_instr->branch_target = succ;
136 remove_simple_block(block);
137 return true;
138 } else if (!last_instr_is_branch && pred == prev && succ == next) {
139 /* Remove 'simple block' when it is between its predecessor and successor
140 * when predecessor fall through 'simple block' (without a branch).
141 */
142 remove_simple_block(block);
143 return true;
144 }
145 return false;
146 }
147
148 static bool
149 try_remove_simple_blocks(bi_context *ctx)
150 {
151 bool changed = false;
152 struct set *blocks_seen =
153 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
154 bi_block *prev = NULL;
155 bi_block *block = NULL;
156 bi_foreach_block(ctx, next) {
157 bool removed = try_remove_simple_block(prev, block, next, blocks_seen);
158 changed |= removed;
159 if (!removed) {
160 prev = block;
161 }
162 block = next;
163 }
164 _mesa_set_destroy(blocks_seen, NULL);
165 return changed;
166 }
167
168 static void
169 remove_branch_to_next_block(bi_context *ctx)
170 {
171 bi_block *block = NULL;
172 bi_foreach_block(ctx, next) {
173 if (block != NULL) {
174 bi_instr *last_instr =
175 list_last_entry(&(block->instructions), bi_instr, link);
176 if (bi_num_successors(block) == 1 && last_instr &&
177 bi_is_branch(last_instr) && last_instr->branch_target == next) {
178 bi_remove_instruction(last_instr);
179 }
180 }
181 block = next;
182 }
183 }
184
185 void
186 bi_opt_control_flow(bi_context *ctx)
187 {
188 while (try_remove_simple_blocks(ctx))
189 ;
190 remove_branch_to_next_block(ctx);
191 }