Debian Codegraph @

src/imagination/pco/pco_builder.h

raw
1 /*
2 * Copyright © 2024 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef PCO_BUILDER_H
8 #define PCO_BUILDER_H
9
10 /**
11 * \\file pco_builder.h
12 *
13 * \\brief PCO builder header.
14 */
15
16 #include "pco.h"
17 #include "pco_internal.h"
18 #include "util/list.h"
19 #include "util/macros.h"
20
21 #include <stdbool.h>
22
23 /** Cursor pointer. */
24 enum pco_cursor_option {
25 PCO_CURSOR_BEFORE_CF_NODE,
26 PCO_CURSOR_AFTER_CF_NODE,
27
28 PCO_CURSOR_BEFORE_INSTR,
29 PCO_CURSOR_AFTER_INSTR,
30
31 PCO_CURSOR_BEFORE_IGRP,
32 PCO_CURSOR_AFTER_IGRP,
33 };
34
35 /** Cursor for PCO instructions/groups and basic blocks. */
36 typedef struct pco_cursor {
37 enum pco_cursor_option option; /** Cursor pointer option. */
38 union {
39 pco_cf_node *cf_node;
40 pco_instr *instr;
41 pco_igrp *igrp;
42 };
43 } pco_cursor;
44
45 /** PCO builder context. */
46 typedef struct pco_builder {
47 pco_func *func; /** Target function. */
48 pco_cursor cursor; /** Current position in the function. */
49 } pco_builder;
50
51 /* Cursor position setters. */
52 /**
53 * \brief Returns a cursor set to before a cf node.
54 *
55 * \param[in] cf_node The cf node.
56 * \return The cursor.
57 */
58 static inline pco_cursor pco_cursor_before_cf_node(pco_cf_node *cf_node)
59 {
60 return (pco_cursor){
61 .option = PCO_CURSOR_BEFORE_CF_NODE,
62 .cf_node = cf_node,
63 };
64 }
65
66 /**
67 * \brief Returns a cursor set to after a cf node.
68 *
69 * \param[in] cf_node The cf node.
70 * \return The cursor.
71 */
72 static inline pco_cursor pco_cursor_after_cf_node(pco_cf_node *cf_node)
73 {
74 return (pco_cursor){
75 .option = PCO_CURSOR_AFTER_CF_NODE,
76 .cf_node = cf_node,
77 };
78 }
79
80 /**
81 * \brief Returns a cursor set to before a block.
82 *
83 * \param[in] block The block.
84 * \return The cursor.
85 */
86 static inline pco_cursor pco_cursor_before_block(pco_block *block)
87 {
88 return pco_cursor_before_cf_node(&block->cf_node);
89 }
90
91 /**
92 * \brief Returns a cursor set to after a block.
93 *
94 * \param[in] block The block.
95 * \return The cursor.
96 */
97 static inline pco_cursor pco_cursor_after_block(pco_block *block)
98 {
99 return pco_cursor_after_cf_node(&block->cf_node);
100 }
101
102 /**
103 * \brief Returns a cursor set to before a list.
104 *
105 * \param[in] list The list.
106 * \return The cursor.
107 */
108 static inline pco_cursor pco_cursor_before_list(struct list_head *list)
109 {
110 return pco_cursor_before_cf_node(pco_cf_node_head(list));
111 }
112
113 /**
114 * \brief Returns a cursor set to after an exec_list.
115 *
116 * \param[in] list The list.
117 * \return The list.
118 */
119 static inline pco_cursor pco_cursor_after_list(struct list_head *list)
120 {
121 return pco_cursor_after_cf_node(pco_cf_node_tail(list));
122 }
123
124 /**
125 * \brief Returns a cursor set to before an instruction.
126 *
127 * \param[in] instr The instruction.
128 * \return The cursor.
129 */
130 static inline pco_cursor pco_cursor_before_instr(pco_instr *instr)
131 {
132 return (pco_cursor){
133 .option = PCO_CURSOR_BEFORE_INSTR,
134 .instr = instr,
135 };
136 }
137
138 /**
139 * \brief Returns a cursor set to after an instruction.
140 *
141 * \param[in] instr The instruction.
142 * \return The cursor.
143 */
144 static inline pco_cursor pco_cursor_after_instr(pco_instr *instr)
145 {
146 return (pco_cursor){
147 .option = PCO_CURSOR_AFTER_INSTR,
148 .instr = instr,
149 };
150 }
151
152 /**
153 * \brief Returns a cursor set to before an instruction group.
154 *
155 * \param[in] igrp The instruction group.
156 * \return The cursor.
157 */
158 static inline pco_cursor pco_cursor_before_igrp(pco_igrp *igrp)
159 {
160 return (pco_cursor){
161 .option = PCO_CURSOR_BEFORE_IGRP,
162 .igrp = igrp,
163 };
164 }
165
166 /**
167 * \brief Returns a cursor set to after an instruction group.
168 *
169 * \param[in] igrp The instruction group.
170 * \return The cursor.
171 */
172 static inline pco_cursor pco_cursor_after_igrp(pco_igrp *igrp)
173 {
174 return (pco_cursor){
175 .option = PCO_CURSOR_AFTER_IGRP,
176 .igrp = igrp,
177 };
178 }
179
180 /**
181 * \brief Returns whether a cursor is set to before a construct.
182 *
183 * \param[in] cursor The cursor.
184 * \return True if the cursor is set to before a construct.
185 */
186 static inline bool pco_cursor_is_before(pco_cursor cursor)
187 {
188 return cursor.option == PCO_CURSOR_BEFORE_CF_NODE ||
189 cursor.option == PCO_CURSOR_BEFORE_INSTR ||
190 cursor.option == PCO_CURSOR_BEFORE_IGRP;
191 }
192
193 /* Cursor get functions. */
194 /**
195 * \brief Returns the function being pointed to by the cursor.
196 *
197 * \param[in] cursor The cursor.
198 * \return The function being pointed to.
199 */
200 static inline pco_func *pco_cursor_func(pco_cursor cursor)
201 {
202 switch (cursor.option) {
203 case PCO_CURSOR_BEFORE_CF_NODE:
204 case PCO_CURSOR_AFTER_CF_NODE:
205 switch (cursor.cf_node->type) {
206 case PCO_CF_NODE_TYPE_BLOCK:
207 return pco_cf_node_as_block(cursor.cf_node)->parent_func;
208
209 case PCO_CF_NODE_TYPE_IF:
210 return pco_cf_node_as_if(cursor.cf_node)->parent_func;
211
212 case PCO_CF_NODE_TYPE_LOOP:
213 return pco_cf_node_as_loop(cursor.cf_node)->parent_func;
214
215 case PCO_CF_NODE_TYPE_FUNC:
216 return pco_cf_node_as_func(cursor.cf_node);
217
218 default:
219 break;
220 }
221 break;
222
223 case PCO_CURSOR_BEFORE_INSTR:
224 case PCO_CURSOR_AFTER_INSTR:
225 return cursor.instr->parent_func;
226
227 case PCO_CURSOR_BEFORE_IGRP:
228 case PCO_CURSOR_AFTER_IGRP:
229 return cursor.igrp->parent_func;
230
231 default:
232 break;
233 }
234
235 UNREACHABLE("");
236 }
237
238 /**
239 * \brief Returns the cf node being pointed to by the cursor.
240 *
241 * \param[in] cursor The cursor.
242 * \return The cf node being pointed to.
243 */
244 static inline pco_cf_node *pco_cursor_cf_node(pco_cursor cursor)
245 {
246 switch (cursor.option) {
247 case PCO_CURSOR_BEFORE_CF_NODE:
248 case PCO_CURSOR_AFTER_CF_NODE:
249 return cursor.cf_node;
250
251 case PCO_CURSOR_BEFORE_INSTR:
252 case PCO_CURSOR_AFTER_INSTR:
253 return &cursor.instr->parent_block->cf_node;
254
255 case PCO_CURSOR_BEFORE_IGRP:
256 case PCO_CURSOR_AFTER_IGRP:
257 return &cursor.igrp->parent_block->cf_node;
258
259 default:
260 break;
261 }
262
263 UNREACHABLE("");
264 }
265
266 /**
267 * \brief Returns the block being pointed to by the cursor.
268 *
269 * \param[in] cursor The cursor.
270 * \return The block being pointed to.
271 */
272 static inline pco_block *pco_cursor_block(pco_cursor cursor)
273 {
274 switch (cursor.option) {
275 case PCO_CURSOR_BEFORE_CF_NODE:
276 case PCO_CURSOR_AFTER_CF_NODE:
277 switch (cursor.cf_node->type) {
278 case PCO_CF_NODE_TYPE_BLOCK:
279 return pco_cf_node_as_block(cursor.cf_node);
280
281 /* TODO: other cf node types? */
282 default:
283 break;
284 }
285 break;
286
287 case PCO_CURSOR_BEFORE_INSTR:
288 case PCO_CURSOR_AFTER_INSTR:
289 return cursor.instr->parent_block;
290
291 case PCO_CURSOR_BEFORE_IGRP:
292 case PCO_CURSOR_AFTER_IGRP:
293 return cursor.igrp->parent_block;
294
295 default:
296 break;
297 }
298
299 UNREACHABLE("");
300 }
301
302 /**
303 * \brief Returns the instruction being pointed to by the cursor.
304 *
305 * \param[in] cursor The cursor.
306 * \return The instruction being pointed to.
307 */
308 static inline pco_instr *pco_cursor_instr(pco_cursor cursor)
309 {
310 bool before = pco_cursor_is_before(cursor);
311
312 switch (cursor.option) {
313 case PCO_CURSOR_BEFORE_CF_NODE:
314 case PCO_CURSOR_AFTER_CF_NODE: {
315 pco_block *block = NULL;
316
317 switch (cursor.cf_node->type) {
318 case PCO_CF_NODE_TYPE_BLOCK:
319 block = pco_cf_node_as_block(cursor.cf_node);
320 return before ? pco_first_instr(block) : pco_last_instr(block);
321
322 /* TODO: other cf node types? */
323 default:
324 break;
325 }
326 break;
327 }
328
329 case PCO_CURSOR_BEFORE_INSTR:
330 case PCO_CURSOR_AFTER_INSTR:
331 return cursor.instr;
332
333 default:
334 break;
335 }
336
337 UNREACHABLE("");
338 }
339
340 /**
341 * \brief Returns the instruction group being pointed to by the cursor.
342 *
343 * \param[in] cursor The cursor.
344 * \return The instruction group being pointed to.
345 */
346 static inline pco_igrp *pco_cursor_igrp(pco_cursor cursor)
347 {
348 bool before = pco_cursor_is_before(cursor);
349
350 switch (cursor.option) {
351 case PCO_CURSOR_BEFORE_CF_NODE:
352 case PCO_CURSOR_AFTER_CF_NODE: {
353 pco_block *block = NULL;
354
355 switch (cursor.cf_node->type) {
356 case PCO_CF_NODE_TYPE_BLOCK:
357 block = pco_cf_node_as_block(cursor.cf_node);
358 /* Special case: we're in pco_group_instrs and want to go from
359 * the start.
360 */
361 if (!block->parent_func->parent_shader->is_grouped)
362 return NULL;
363 return before ? pco_first_igrp(block) : pco_last_igrp(block);
364
365 /* TODO: other cf node types? */
366 default:
367 break;
368 }
369 break;
370 }
371
372 case PCO_CURSOR_BEFORE_IGRP:
373 case PCO_CURSOR_AFTER_IGRP:
374 return cursor.igrp;
375
376 default:
377 break;
378 }
379
380 UNREACHABLE("");
381 }
382
383 /* Builder functions. */
384 /**
385 * \brief Creates a builder.
386 *
387 * \param[in] func The function being targeted.
388 * \param[in] cursor The cursor.
389 * \return The builder.
390 */
391 static pco_builder pco_builder_create(pco_func *func, pco_cursor cursor)
392 {
393 return (pco_builder){
394 .func = func,
395 .cursor = cursor,
396 };
397 }
398
399 /**
400 * \brief Inserts a instruction at a position specified by the builder.
401 *
402 * \param[in] b The builder.
403 * \param[in] instr The instruction.
404 */
405 static inline void pco_builder_insert_instr(pco_builder *b, pco_instr *instr)
406 {
407 pco_block *block = pco_cursor_block(b->cursor);
408 instr->parent_block = block;
409
410 pco_instr *cursor_instr = pco_cursor_instr(b->cursor);
411 bool before = pco_cursor_is_before(b->cursor);
412
413 if (cursor_instr) {
414 if (before)
415 list_add(&instr->link, cursor_instr->link.prev);
416 else
417 list_add(&instr->link, &cursor_instr->link);
418 } else {
419 if (before)
420 list_add(&instr->link, &block->instrs);
421 else
422 list_addtail(&instr->link, &block->instrs);
423 }
424
425 b->cursor = pco_cursor_after_instr(instr);
426 }
427
428 /**
429 * \brief Inserts a instruction group at a position specified by the builder.
430 *
431 * \param[in] b The builder.
432 * \param[in] igrp The instruction group.
433 */
434 static inline void pco_builder_insert_igrp(pco_builder *b, pco_igrp *igrp)
435 {
436 pco_block *block = pco_cursor_block(b->cursor);
437 igrp->parent_block = block;
438
439 pco_igrp *cursor_igrp = pco_cursor_igrp(b->cursor);
440 bool before = pco_cursor_is_before(b->cursor);
441
442 if (cursor_igrp) {
443 if (before)
444 list_add(&igrp->link, cursor_igrp->link.prev);
445 else
446 list_add(&igrp->link, &cursor_igrp->link);
447 } else {
448 if (before)
449 list_add(&igrp->link, &block->instrs);
450 else
451 list_addtail(&igrp->link, &block->instrs);
452 }
453
454 b->cursor = pco_cursor_after_igrp(igrp);
455 }
456
457 /* Generated op building functions. */
458 #include "pco_builder_ops.h"
459
460 /**
461 * \brief Returns whether the instruction has the default execution condition.
462 *
463 * \param[in] instr The instruction.
464 * \return True if the instruction has the default execution condition.
465 */
466 static inline bool pco_instr_has_default_exec(pco_instr *instr)
467 {
468 if (!pco_instr_has_exec_cnd(instr))
469 return true;
470
471 return pco_instr_get_exec_cnd(instr) == PCO_EXEC_CND_E1_ZX;
472 }
473 #endif /* PCO_BUILDER_H */