2 * Copyright (c) 2017 Lima Project
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "util/u_math.h"
26 #include "util/ralloc.h"
27 #include "util/bitscan.h"
31 const ppir_op_info ppir_op_infos
[] = {
35 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_SCL_MUL
,
36 PPIR_INSTR_SLOT_ALU_VEC_ADD
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
52 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
59 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
66 PPIR_INSTR_SLOT_ALU_VEC_ADD
, PPIR_INSTR_SLOT_END
72 PPIR_INSTR_SLOT_ALU_VEC_ADD
, PPIR_INSTR_SLOT_END
78 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
84 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
90 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
96 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
102 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
108 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
114 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_SCL_MUL
,
115 PPIR_INSTR_SLOT_ALU_VEC_ADD
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
122 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_SCL_MUL
,
123 PPIR_INSTR_SLOT_ALU_VEC_ADD
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
130 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
137 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
144 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
151 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
158 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
165 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
172 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
185 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
186 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
193 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
194 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
201 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
202 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
209 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
210 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
214 [ppir_op_sel_cond
] = {
215 /* effectively mov, but must be scheduled only to
216 * PPIR_INSTR_SLOT_ALU_SCL_MUL */
219 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_END
225 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
232 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
235 [ppir_op_load_varying
] = {
237 .type
= ppir_node_type_load
,
239 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
242 [ppir_op_load_coords
] = {
244 .type
= ppir_node_type_load
,
246 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
249 [ppir_op_load_fragcoord
] = {
250 .name
= "ld_fragcoord",
251 .type
= ppir_node_type_load
,
253 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
256 [ppir_op_load_pointcoord
] = {
257 .name
= "ld_pointcoord",
258 .type
= ppir_node_type_load
,
260 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
263 [ppir_op_load_frontface
] = {
264 .name
= "ld_frontface",
265 .type
= ppir_node_type_load
,
267 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
270 [ppir_op_load_uniform
] = {
272 .type
= ppir_node_type_load
,
274 PPIR_INSTR_SLOT_UNIFORM
, PPIR_INSTR_SLOT_END
277 [ppir_op_load_texture
] = {
279 .type
= ppir_node_type_load_texture
,
281 PPIR_INSTR_SLOT_TEXLD
, PPIR_INSTR_SLOT_END
284 [ppir_op_load_temp
] = {
286 .type
= ppir_node_type_load
,
288 PPIR_INSTR_SLOT_UNIFORM
, PPIR_INSTR_SLOT_END
293 .type
= ppir_node_type_const
,
295 [ppir_op_store_color
] = {
297 .type
= ppir_node_type_store
,
299 [ppir_op_store_temp
] = {
301 .type
= ppir_node_type_store
,
303 PPIR_INSTR_SLOT_STORE_TEMP
, PPIR_INSTR_SLOT_END
306 [ppir_op_discard
] = {
308 .type
= ppir_node_type_discard
,
310 PPIR_INSTR_SLOT_BRANCH
, PPIR_INSTR_SLOT_END
315 .type
= ppir_node_type_branch
,
317 PPIR_INSTR_SLOT_BRANCH
, PPIR_INSTR_SLOT_END
322 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
)
324 ppir_compiler
*comp
= block
->comp
;
325 static const int node_size
[] = {
326 [ppir_node_type_alu
] = sizeof(ppir_alu_node
),
327 [ppir_node_type_const
] = sizeof(ppir_const_node
),
328 [ppir_node_type_load
] = sizeof(ppir_load_node
),
329 [ppir_node_type_store
] = sizeof(ppir_store_node
),
330 [ppir_node_type_load_texture
] = sizeof(ppir_load_texture_node
),
331 [ppir_node_type_discard
] = sizeof(ppir_discard_node
),
332 [ppir_node_type_branch
] = sizeof(ppir_branch_node
),
335 ppir_node_type type
= ppir_op_infos
[op
].type
;
336 int size
= node_size
[type
];
337 ppir_node
*node
= rzalloc_size(block
, size
);
341 list_inithead(&node
->succ_list
);
342 list_inithead(&node
->pred_list
);
346 /* reg has 4 slots for each componemt write node */
348 comp
->var_nodes
[(index
<< 2) + comp
->reg_base
+ u_bit_scan(&mask
)] = node
;
349 snprintf(node
->name
, sizeof(node
->name
), "reg%d", index
);
351 comp
->var_nodes
[index
] = node
;
352 snprintf(node
->name
, sizeof(node
->name
), "ssa%d", index
);
356 snprintf(node
->name
, sizeof(node
->name
), "new");
360 node
->index
= comp
->cur_index
++;
366 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
)
368 /* don't add dep for two nodes from different block */
369 if (succ
->block
!= pred
->block
)
372 /* don't add duplicated dep */
373 ppir_node_foreach_pred(succ
, dep
) {
374 if (dep
->pred
== pred
)
378 ppir_dep
*dep
= ralloc(succ
, ppir_dep
);
381 list_addtail(&dep
->pred_link
, &succ
->pred_list
);
382 list_addtail(&dep
->succ_link
, &pred
->succ_list
);
385 void ppir_node_remove_dep(ppir_dep
*dep
)
387 list_del(&dep
->succ_link
);
388 list_del(&dep
->pred_link
);
392 static void _ppir_node_replace_child(ppir_src
*src
, ppir_node
*old_child
, ppir_node
*new_child
)
394 ppir_dest
*od
= ppir_node_get_dest(old_child
);
395 if (ppir_node_target_equal(src
, od
)) {
396 ppir_dest
*nd
= ppir_node_get_dest(new_child
);
397 ppir_node_target_assign(src
, nd
);
401 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
)
403 switch (parent
->type
) {
404 case ppir_node_type_alu
:
406 ppir_alu_node
*alu
= ppir_node_to_alu(parent
);
407 for (int i
= 0; i
< alu
->num_src
; i
++)
408 _ppir_node_replace_child(alu
->src
+ i
, old_child
, new_child
);
411 case ppir_node_type_branch
:
413 ppir_branch_node
*branch
= ppir_node_to_branch(parent
);
414 for (int i
= 0; i
< 2; i
++)
415 _ppir_node_replace_child(branch
->src
+ i
, old_child
, new_child
);
418 case ppir_node_type_load
:
420 ppir_load_node
*load
= ppir_node_to_load(parent
);
421 _ppir_node_replace_child(&load
->src
, old_child
, new_child
);
424 case ppir_node_type_load_texture
:
426 ppir_load_texture_node
*load_texture
= ppir_node_to_load_texture(parent
);
427 _ppir_node_replace_child(&load_texture
->src_coords
, old_child
, new_child
);
430 case ppir_node_type_store
:
432 ppir_store_node
*store
= ppir_node_to_store(parent
);
433 _ppir_node_replace_child(&store
->src
, old_child
, new_child
);
437 ppir_debug("unknown node type in %s\n", __func__
);
442 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
)
444 list_del(&dep
->succ_link
);
445 dep
->pred
= new_pred
;
446 list_addtail(&dep
->succ_link
, &new_pred
->succ_list
);
449 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
)
451 ppir_node_foreach_succ_safe(src
, dep
) {
452 ppir_node_replace_pred(dep
, dst
);
453 ppir_node_replace_child(dep
->succ
, src
, dst
);
457 void ppir_node_delete(ppir_node
*node
)
459 ppir_node_foreach_succ_safe(node
, dep
)
460 ppir_node_remove_dep(dep
);
462 ppir_node_foreach_pred_safe(node
, dep
)
463 ppir_node_remove_dep(dep
);
465 list_del(&node
->list
);
469 static void ppir_node_print_node(ppir_node
*node
, int space
)
471 for (int i
= 0; i
< space
; i
++)
473 printf("%s%s %d %s\n", node
->printed
&& !ppir_node_is_leaf(node
) ? "+" : "",
474 ppir_op_infos
[node
->op
].name
, node
->index
, node
->name
);
476 if (!node
->printed
) {
477 ppir_node_foreach_pred(node
, dep
) {
478 ppir_node
*pred
= dep
->pred
;
479 ppir_node_print_node(pred
, space
+ 2);
482 node
->printed
= true;
486 void ppir_node_print_prog(ppir_compiler
*comp
)
488 if (!(lima_debug
& LIMA_DEBUG_PP
))
491 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
492 list_for_each_entry(ppir_node
, node
, &block
->node_list
, list
) {
493 node
->printed
= false;
497 printf("========prog========\n");
498 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
499 printf("-------block------\n");
500 list_for_each_entry(ppir_node
, node
, &block
->node_list
, list
) {
501 if (ppir_node_is_root(node
))
502 ppir_node_print_node(node
, 0);
505 printf("====================\n");