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_coords_reg
] = {
264 .name
= "ld_coords_reg",
265 .type
= ppir_node_type_load
,
267 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
270 [ppir_op_load_fragcoord
] = {
271 .name
= "ld_fragcoord",
272 .type
= ppir_node_type_load
,
274 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
277 [ppir_op_load_pointcoord
] = {
278 .name
= "ld_pointcoord",
279 .type
= ppir_node_type_load
,
281 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
284 [ppir_op_load_frontface
] = {
285 .name
= "ld_frontface",
286 .type
= ppir_node_type_load
,
288 PPIR_INSTR_SLOT_VARYING
, PPIR_INSTR_SLOT_END
291 [ppir_op_load_uniform
] = {
293 .type
= ppir_node_type_load
,
295 PPIR_INSTR_SLOT_UNIFORM
, PPIR_INSTR_SLOT_END
298 [ppir_op_load_texture
] = {
300 .type
= ppir_node_type_load_texture
,
302 PPIR_INSTR_SLOT_TEXLD
, PPIR_INSTR_SLOT_END
305 [ppir_op_load_temp
] = {
307 .type
= ppir_node_type_load
,
309 PPIR_INSTR_SLOT_UNIFORM
, PPIR_INSTR_SLOT_END
314 .type
= ppir_node_type_const
,
316 [ppir_op_store_color
] = {
318 .type
= ppir_node_type_alu
,
320 PPIR_INSTR_SLOT_ALU_VEC_ADD
, PPIR_INSTR_SLOT_ALU_VEC_MUL
,
324 [ppir_op_store_temp
] = {
326 .type
= ppir_node_type_store
,
328 PPIR_INSTR_SLOT_STORE_TEMP
, PPIR_INSTR_SLOT_END
331 [ppir_op_discard
] = {
333 .type
= ppir_node_type_discard
,
335 PPIR_INSTR_SLOT_BRANCH
, PPIR_INSTR_SLOT_END
340 .type
= ppir_node_type_branch
,
342 PPIR_INSTR_SLOT_BRANCH
, PPIR_INSTR_SLOT_END
347 .type
= ppir_node_type_alu
,
353 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
)
355 ppir_compiler
*comp
= block
->comp
;
356 static const int node_size
[] = {
357 [ppir_node_type_alu
] = sizeof(ppir_alu_node
),
358 [ppir_node_type_const
] = sizeof(ppir_const_node
),
359 [ppir_node_type_load
] = sizeof(ppir_load_node
),
360 [ppir_node_type_store
] = sizeof(ppir_store_node
),
361 [ppir_node_type_load_texture
] = sizeof(ppir_load_texture_node
),
362 [ppir_node_type_discard
] = sizeof(ppir_discard_node
),
363 [ppir_node_type_branch
] = sizeof(ppir_branch_node
),
366 ppir_node_type type
= ppir_op_infos
[op
].type
;
367 int size
= node_size
[type
];
368 ppir_node
*node
= rzalloc_size(block
, size
);
372 list_inithead(&node
->succ_list
);
373 list_inithead(&node
->pred_list
);
377 /* reg has 4 slots for each componemt write node */
379 comp
->var_nodes
[(index
<< 2) + comp
->reg_base
+ u_bit_scan(&mask
)] = node
;
380 snprintf(node
->name
, sizeof(node
->name
), "reg%d", index
);
382 comp
->var_nodes
[index
] = node
;
383 snprintf(node
->name
, sizeof(node
->name
), "ssa%d", index
);
387 snprintf(node
->name
, sizeof(node
->name
), "new");
391 node
->index
= comp
->cur_index
++;
397 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
,
400 /* don't add dep for two nodes from different block */
401 if (succ
->block
!= pred
->block
)
404 /* don't add duplicated dep */
405 ppir_node_foreach_pred(succ
, dep
) {
406 if (dep
->pred
== pred
)
410 ppir_dep
*dep
= ralloc(succ
, ppir_dep
);
414 list_addtail(&dep
->pred_link
, &succ
->pred_list
);
415 list_addtail(&dep
->succ_link
, &pred
->succ_list
);
418 void ppir_node_remove_dep(ppir_dep
*dep
)
420 list_del(&dep
->succ_link
);
421 list_del(&dep
->pred_link
);
425 static void _ppir_node_replace_child(ppir_src
*src
, ppir_node
*old_child
, ppir_node
*new_child
)
427 ppir_dest
*od
= ppir_node_get_dest(old_child
);
428 if (ppir_node_target_equal(src
, od
)) {
429 ppir_node_target_assign(src
, new_child
);
433 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
)
435 switch (parent
->type
) {
436 case ppir_node_type_alu
:
438 ppir_alu_node
*alu
= ppir_node_to_alu(parent
);
439 for (int i
= 0; i
< alu
->num_src
; i
++)
440 _ppir_node_replace_child(alu
->src
+ i
, old_child
, new_child
);
443 case ppir_node_type_branch
:
445 ppir_branch_node
*branch
= ppir_node_to_branch(parent
);
446 for (int i
= 0; i
< 2; i
++)
447 _ppir_node_replace_child(branch
->src
+ i
, old_child
, new_child
);
450 case ppir_node_type_load
:
452 ppir_load_node
*load
= ppir_node_to_load(parent
);
453 _ppir_node_replace_child(&load
->src
, old_child
, new_child
);
456 case ppir_node_type_load_texture
:
458 ppir_load_texture_node
*load_texture
= ppir_node_to_load_texture(parent
);
459 _ppir_node_replace_child(&load_texture
->src_coords
, old_child
, new_child
);
462 case ppir_node_type_store
:
464 ppir_store_node
*store
= ppir_node_to_store(parent
);
465 _ppir_node_replace_child(&store
->src
, old_child
, new_child
);
469 ppir_debug("unknown node type in %s\n", __func__
);
474 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
)
476 list_del(&dep
->succ_link
);
477 dep
->pred
= new_pred
;
478 list_addtail(&dep
->succ_link
, &new_pred
->succ_list
);
481 ppir_dep
*ppir_dep_for_pred(ppir_node
*node
, ppir_node
*pred
)
486 if (node
->block
!= pred
->block
)
489 ppir_node_foreach_pred(node
, dep
) {
490 if (dep
->pred
== pred
)
496 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
)
498 ppir_node_foreach_succ_safe(src
, dep
) {
499 ppir_node_replace_pred(dep
, dst
);
500 ppir_node_replace_child(dep
->succ
, src
, dst
);
504 void ppir_node_delete(ppir_node
*node
)
506 ppir_node_foreach_succ_safe(node
, dep
)
507 ppir_node_remove_dep(dep
);
509 ppir_node_foreach_pred_safe(node
, dep
)
510 ppir_node_remove_dep(dep
);
512 list_del(&node
->list
);
516 static void ppir_node_print_dest(ppir_dest
*dest
)
518 switch (dest
->type
) {
519 case ppir_target_ssa
:
520 printf("ssa%d", dest
->ssa
.index
);
522 case ppir_target_pipeline
:
523 printf("pipeline %d", dest
->pipeline
);
525 case ppir_target_register
:
526 printf("reg %d", dest
->reg
->index
);
531 static void ppir_node_print_src(ppir_src
*src
)
534 case ppir_target_ssa
: {
536 printf("ssa node %d", src
->node
->index
);
538 printf("ssa idx %d", src
->ssa
? src
->ssa
->index
: -1);
541 case ppir_target_pipeline
:
543 printf("pipeline %d node %d", src
->pipeline
, src
->node
->index
);
545 printf("pipeline %d", src
->pipeline
);
547 case ppir_target_register
:
548 printf("reg %d", src
->reg
->index
);
553 static void ppir_node_print_node(ppir_node
*node
, int space
)
555 for (int i
= 0; i
< space
; i
++)
558 printf("%s%d: %s %s: ", node
->printed
&& !ppir_node_is_leaf(node
) ? "+" : "",
559 node
->index
, ppir_op_infos
[node
->op
].name
, node
->name
);
561 ppir_dest
*dest
= ppir_node_get_dest(node
);
564 ppir_node_print_dest(dest
);
567 if (ppir_node_get_src_num(node
) > 0) {
570 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
571 ppir_node_print_src(ppir_node_get_src(node
, i
));
572 if (i
!= (ppir_node_get_src_num(node
) - 1))
577 if (!node
->printed
) {
578 ppir_node_foreach_pred(node
, dep
) {
579 ppir_node
*pred
= dep
->pred
;
580 ppir_node_print_node(pred
, space
+ 2);
583 node
->printed
= true;
587 void ppir_node_print_prog(ppir_compiler
*comp
)
589 if (!(lima_debug
& LIMA_DEBUG_PP
))
592 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
593 list_for_each_entry(ppir_node
, node
, &block
->node_list
, list
) {
594 node
->printed
= false;
598 printf("========prog========\n");
599 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
600 printf("-------block %3d-------\n", block
->index
);
601 list_for_each_entry(ppir_node
, node
, &block
->node_list
, list
) {
602 if (ppir_node_is_root(node
))
603 ppir_node_print_node(node
, 0);
606 printf("====================\n");
609 static ppir_node
*ppir_node_clone_const(ppir_block
*block
, ppir_node
*node
)
611 ppir_const_node
*cnode
= ppir_node_to_const(node
);
612 ppir_const_node
*new_cnode
= ppir_node_create(block
, ppir_op_const
, -1, 0);
617 list_addtail(&new_cnode
->node
.list
, &block
->node_list
);
619 new_cnode
->constant
.num
= cnode
->constant
.num
;
620 for (int i
= 0; i
< cnode
->constant
.num
; i
++) {
621 new_cnode
->constant
.value
[i
] = cnode
->constant
.value
[i
];
623 new_cnode
->dest
.type
= ppir_target_ssa
;
624 new_cnode
->dest
.ssa
.num_components
= cnode
->dest
.ssa
.num_components
;
625 new_cnode
->dest
.ssa
.live_in
= INT_MAX
;
626 new_cnode
->dest
.ssa
.live_out
= 0;
627 new_cnode
->dest
.write_mask
= cnode
->dest
.write_mask
;
629 return &new_cnode
->node
;
633 ppir_node_clone_load(ppir_block
*block
, ppir_node
*node
)
635 ppir_load_node
*load_node
= ppir_node_to_load(node
);
636 ppir_load_node
*new_lnode
= ppir_node_create(block
, node
->op
, -1, 0);
641 list_addtail(&new_lnode
->node
.list
, &block
->node_list
);
643 new_lnode
->num_components
= load_node
->num_components
;
644 new_lnode
->index
= load_node
->index
;
646 ppir_dest
*dest
= ppir_node_get_dest(node
);
647 new_lnode
->dest
= *dest
;
649 ppir_src
*src
= ppir_node_get_src(node
, 0);
651 new_lnode
->num_src
= 1;
653 case ppir_target_ssa
:
654 ppir_node_target_assign(&new_lnode
->src
, src
->node
);
655 ppir_node_add_dep(&new_lnode
->node
, src
->node
, ppir_dep_src
);
657 case ppir_target_register
:
658 new_lnode
->src
.type
= src
->type
;
659 new_lnode
->src
.reg
= src
->reg
;
660 new_lnode
->src
.node
= NULL
;
663 /* Load nodes can't consume pipeline registers */
668 return &new_lnode
->node
;
671 ppir_node
*ppir_node_clone(ppir_block
*block
, ppir_node
*node
)
675 return ppir_node_clone_const(block
, node
);
676 case ppir_op_load_uniform
:
677 case ppir_op_load_varying
:
678 case ppir_op_load_temp
:
679 case ppir_op_load_coords
:
680 case ppir_op_load_coords_reg
:
681 return ppir_node_clone_load(block
, node
);
687 ppir_node
*ppir_node_insert_mov(ppir_node
*node
)
689 ppir_node
*move
= ppir_node_create(node
->block
, ppir_op_mov
, -1, 0);
693 ppir_dest
*dest
= ppir_node_get_dest(node
);
694 ppir_alu_node
*alu
= ppir_node_to_alu(move
);
697 ppir_node_target_assign(alu
->src
, node
);
699 for (int s
= 0; s
< 4; s
++)
700 alu
->src
->swizzle
[s
] = s
;
702 ppir_node_replace_all_succ(move
, node
);
703 ppir_node_add_dep(move
, node
, ppir_dep_src
);
704 list_addtail(&move
->list
, &node
->list
);
709 bool ppir_node_has_single_src_succ(ppir_node
*node
)
711 if (list_is_singular(&node
->succ_list
) &&
712 list_first_entry(&node
->succ_list
,
713 ppir_dep
, succ_link
)->type
== ppir_dep_src
)
717 ppir_node_foreach_succ(node
, dep
) {
718 if (dep
->type
!= ppir_dep_src
)