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_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
158 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
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
,
179 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
186 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
199 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
200 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
207 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
208 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
215 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
216 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
223 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_ALU_SCL_ADD
,
224 PPIR_INSTR_SLOT_ALU_VEC_MUL
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
228 [ppir_op_sel_cond
] = {
229 /* effectively mov, but must be scheduled only to
230 * PPIR_INSTR_SLOT_ALU_SCL_MUL */
233 PPIR_INSTR_SLOT_ALU_SCL_MUL
, PPIR_INSTR_SLOT_END
239 PPIR_INSTR_SLOT_ALU_SCL_ADD
, PPIR_INSTR_SLOT_ALU_VEC_ADD
,
246 PPIR_INSTR_SLOT_ALU_COMBINE
, PPIR_INSTR_SLOT_END
249 [ppir_op_load_varying
] = {
251 .type
= ppir_node_type_load
,
253 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
256 [ppir_op_load_coords
] = {
258 .type
= ppir_node_type_load
,
260 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
263 [ppir_op_load_fragcoord
] = {
264 .name
= "ld_fragcoord",
265 .type
= ppir_node_type_load
,
267 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
270 [ppir_op_load_pointcoord
] = {
271 .name
= "ld_pointcoord",
272 .type
= ppir_node_type_load
,
274 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
277 [ppir_op_load_frontface
] = {
278 .name
= "ld_frontface",
279 .type
= ppir_node_type_load
,
281 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
284 [ppir_op_load_uniform
] = {
286 .type
= ppir_node_type_load
,
288 PPIR_INSTR_SLOT_UNIFORM
, PPIR_INSTR_SLOT_END
291 [ppir_op_load_texture
] = {
293 .type
= ppir_node_type_load_texture
,
295 PPIR_INSTR_SLOT_TEXLD
, PPIR_INSTR_SLOT_END
298 [ppir_op_load_temp
] = {
300 .type
= ppir_node_type_load
,
302 PPIR_INSTR_SLOT_UNIFORM
, PPIR_INSTR_SLOT_END
307 .type
= ppir_node_type_const
,
309 [ppir_op_store_color
] = {
311 .type
= ppir_node_type_store
,
313 [ppir_op_store_temp
] = {
315 .type
= ppir_node_type_store
,
317 PPIR_INSTR_SLOT_STORE_TEMP
, PPIR_INSTR_SLOT_END
320 [ppir_op_discard
] = {
322 .type
= ppir_node_type_discard
,
324 PPIR_INSTR_SLOT_BRANCH
, PPIR_INSTR_SLOT_END
329 .type
= ppir_node_type_branch
,
331 PPIR_INSTR_SLOT_BRANCH
, PPIR_INSTR_SLOT_END
336 .type
= ppir_node_type_alu
,
342 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
)
344 ppir_compiler
*comp
= block
->comp
;
345 static const int node_size
[] = {
346 [ppir_node_type_alu
] = sizeof(ppir_alu_node
),
347 [ppir_node_type_const
] = sizeof(ppir_const_node
),
348 [ppir_node_type_load
] = sizeof(ppir_load_node
),
349 [ppir_node_type_store
] = sizeof(ppir_store_node
),
350 [ppir_node_type_load_texture
] = sizeof(ppir_load_texture_node
),
351 [ppir_node_type_discard
] = sizeof(ppir_discard_node
),
352 [ppir_node_type_branch
] = sizeof(ppir_branch_node
),
355 ppir_node_type type
= ppir_op_infos
[op
].type
;
356 int size
= node_size
[type
];
357 ppir_node
*node
= rzalloc_size(block
, size
);
361 list_inithead(&node
->succ_list
);
362 list_inithead(&node
->pred_list
);
366 /* reg has 4 slots for each componemt write node */
368 comp
->var_nodes
[(index
<< 2) + comp
->reg_base
+ u_bit_scan(&mask
)] = node
;
369 snprintf(node
->name
, sizeof(node
->name
), "reg%d", index
);
371 comp
->var_nodes
[index
] = node
;
372 snprintf(node
->name
, sizeof(node
->name
), "ssa%d", index
);
376 snprintf(node
->name
, sizeof(node
->name
), "new");
380 node
->index
= comp
->cur_index
++;
386 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
)
388 /* don't add dep for two nodes from different block */
389 if (succ
->block
!= pred
->block
)
392 /* don't add duplicated dep */
393 ppir_node_foreach_pred(succ
, dep
) {
394 if (dep
->pred
== pred
)
398 ppir_dep
*dep
= ralloc(succ
, ppir_dep
);
401 list_addtail(&dep
->pred_link
, &succ
->pred_list
);
402 list_addtail(&dep
->succ_link
, &pred
->succ_list
);
405 void ppir_node_remove_dep(ppir_dep
*dep
)
407 list_del(&dep
->succ_link
);
408 list_del(&dep
->pred_link
);
412 static void _ppir_node_replace_child(ppir_src
*src
, ppir_node
*old_child
, ppir_node
*new_child
)
414 ppir_dest
*od
= ppir_node_get_dest(old_child
);
415 if (ppir_node_target_equal(src
, od
)) {
416 ppir_node_target_assign(src
, new_child
);
420 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
)
422 switch (parent
->type
) {
423 case ppir_node_type_alu
:
425 ppir_alu_node
*alu
= ppir_node_to_alu(parent
);
426 for (int i
= 0; i
< alu
->num_src
; i
++)
427 _ppir_node_replace_child(alu
->src
+ i
, old_child
, new_child
);
430 case ppir_node_type_branch
:
432 ppir_branch_node
*branch
= ppir_node_to_branch(parent
);
433 for (int i
= 0; i
< 2; i
++)
434 _ppir_node_replace_child(branch
->src
+ i
, old_child
, new_child
);
437 case ppir_node_type_load
:
439 ppir_load_node
*load
= ppir_node_to_load(parent
);
440 _ppir_node_replace_child(&load
->src
, old_child
, new_child
);
443 case ppir_node_type_load_texture
:
445 ppir_load_texture_node
*load_texture
= ppir_node_to_load_texture(parent
);
446 _ppir_node_replace_child(&load_texture
->src_coords
, old_child
, new_child
);
449 case ppir_node_type_store
:
451 ppir_store_node
*store
= ppir_node_to_store(parent
);
452 _ppir_node_replace_child(&store
->src
, old_child
, new_child
);
456 ppir_debug("unknown node type in %s\n", __func__
);
461 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
)
463 list_del(&dep
->succ_link
);
464 dep
->pred
= new_pred
;
465 list_addtail(&dep
->succ_link
, &new_pred
->succ_list
);
468 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
)
470 ppir_node_foreach_succ_safe(src
, dep
) {
471 ppir_node_replace_pred(dep
, dst
);
472 ppir_node_replace_child(dep
->succ
, src
, dst
);
476 void ppir_node_delete(ppir_node
*node
)
478 ppir_node_foreach_succ_safe(node
, dep
)
479 ppir_node_remove_dep(dep
);
481 ppir_node_foreach_pred_safe(node
, dep
)
482 ppir_node_remove_dep(dep
);
484 list_del(&node
->list
);
488 static void ppir_node_print_dest(ppir_dest
*dest
)
490 switch (dest
->type
) {
491 case ppir_target_ssa
:
492 printf("ssa%d", dest
->ssa
.index
);
494 case ppir_target_pipeline
:
495 printf("pipeline %d", dest
->pipeline
);
497 case ppir_target_register
:
498 printf("reg %d", dest
->reg
->index
);
503 static void ppir_node_print_src(ppir_src
*src
)
506 case ppir_target_ssa
: {
508 printf("ssa node %d", src
->node
->index
);
510 printf("ssa idx %d", src
->ssa
? src
->ssa
->index
: -1);
513 case ppir_target_pipeline
:
515 printf("pipeline %d node %d", src
->pipeline
, src
->node
->index
);
517 printf("pipeline %d", src
->pipeline
);
519 case ppir_target_register
:
520 printf("reg %d", src
->reg
->index
);
525 static void ppir_node_print_node(ppir_node
*node
, int space
)
527 for (int i
= 0; i
< space
; i
++)
530 printf("%s%d: %s %s: ", node
->printed
&& !ppir_node_is_leaf(node
) ? "+" : "",
531 node
->index
, ppir_op_infos
[node
->op
].name
, node
->name
);
533 ppir_dest
*dest
= ppir_node_get_dest(node
);
536 ppir_node_print_dest(dest
);
539 if (ppir_node_get_src_num(node
) > 0) {
542 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
543 ppir_node_print_src(ppir_node_get_src(node
, i
));
544 if (i
!= (ppir_node_get_src_num(node
) - 1))
549 if (!node
->printed
) {
550 ppir_node_foreach_pred(node
, dep
) {
551 ppir_node
*pred
= dep
->pred
;
552 ppir_node_print_node(pred
, space
+ 2);
555 node
->printed
= true;
559 void ppir_node_print_prog(ppir_compiler
*comp
)
561 if (!(lima_debug
& LIMA_DEBUG_PP
))
564 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
565 list_for_each_entry(ppir_node
, node
, &block
->node_list
, list
) {
566 node
->printed
= false;
570 printf("========prog========\n");
571 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
572 printf("-------block------\n");
573 list_for_each_entry(ppir_node
, node
, &block
->node_list
, list
) {
574 if (ppir_node_is_root(node
))
575 ppir_node_print_node(node
, 0);
578 printf("====================\n");
581 static ppir_node
*ppir_node_clone_const(ppir_block
*block
, ppir_node
*node
)
583 ppir_const_node
*cnode
= ppir_node_to_const(node
);
584 ppir_const_node
*new_cnode
= ppir_node_create(block
, ppir_op_const
, -1, 0);
589 list_addtail(&new_cnode
->node
.list
, &block
->node_list
);
591 new_cnode
->constant
.num
= cnode
->constant
.num
;
592 for (int i
= 0; i
< cnode
->constant
.num
; i
++) {
593 new_cnode
->constant
.value
[i
] = cnode
->constant
.value
[i
];
595 new_cnode
->dest
.type
= ppir_target_ssa
;
596 new_cnode
->dest
.ssa
.num_components
= cnode
->dest
.ssa
.num_components
;
597 new_cnode
->dest
.ssa
.live_in
= INT_MAX
;
598 new_cnode
->dest
.ssa
.live_out
= 0;
599 new_cnode
->dest
.write_mask
= cnode
->dest
.write_mask
;
601 return &new_cnode
->node
;
605 ppir_node_clone_tex(ppir_block
*block
, ppir_node
*node
)
607 ppir_load_texture_node
*tex_node
= ppir_node_to_load_texture(node
);
608 ppir_load_texture_node
*new_tnode
= ppir_node_create(block
, ppir_op_load_texture
, -1, 0);
613 list_addtail(&new_tnode
->node
.list
, &block
->node_list
);
615 ppir_dest
*dest
= ppir_node_get_dest(node
);
616 new_tnode
->dest
= *dest
;
618 new_tnode
->sampler_dim
= tex_node
->sampler_dim
;
620 for (int i
= 0; i
< 4; i
++)
621 new_tnode
->src_coords
.swizzle
[i
] = tex_node
->src_coords
.swizzle
[i
];
623 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
624 ppir_src
*src
= ppir_node_get_src(node
, i
);
625 ppir_src
*new_src
= ppir_node_get_src(&new_tnode
->node
, i
);
627 case ppir_target_ssa
: {
628 ppir_node_target_assign(new_src
, src
->node
);
629 ppir_node_add_dep(&new_tnode
->node
, src
->node
);
632 case ppir_target_register
: {
633 new_src
->type
= src
->type
;
634 new_src
->reg
= src
->reg
;
635 new_src
->node
= NULL
;
639 /* pipeline is not expected here */
644 return &new_tnode
->node
;
648 ppir_node_clone_load(ppir_block
*block
, ppir_node
*node
)
650 ppir_load_node
*load_node
= ppir_node_to_load(node
);
651 ppir_load_node
*new_lnode
= ppir_node_create(block
, node
->op
, -1, 0);
656 list_addtail(&new_lnode
->node
.list
, &block
->node_list
);
658 new_lnode
->num_components
= load_node
->num_components
;
659 new_lnode
->index
= load_node
->index
;
661 ppir_dest
*dest
= ppir_node_get_dest(node
);
662 new_lnode
->dest
= *dest
;
664 return &new_lnode
->node
;
667 ppir_node
*ppir_node_clone(ppir_block
*block
, ppir_node
*node
)
671 return ppir_node_clone_const(block
, node
);
672 case ppir_op_load_texture
:
673 return ppir_node_clone_tex(block
, node
);
674 case ppir_op_load_uniform
:
675 case ppir_op_load_varying
:
676 case ppir_op_load_temp
:
677 return ppir_node_clone_load(block
, node
);