2 * Copyright (c) 2017 Lima Project
3 * Copyright (c) 2013 Connor Abbott
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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 NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #ifndef LIMA_IR_PP_PPIR_H
26 #define LIMA_IR_PP_PPIR_H
28 #include "util/u_math.h"
29 #include "util/list.h"
32 #include "ir/lima_ir.h"
100 ppir_op_load_uniform
,
101 ppir_op_load_varying
,
103 ppir_op_load_coords_reg
,
104 ppir_op_load_fragcoord
,
105 ppir_op_load_pointcoord
,
106 ppir_op_load_frontface
,
107 ppir_op_load_texture
,
125 ppir_node_type_const
,
127 ppir_node_type_store
,
128 ppir_node_type_load_texture
,
129 ppir_node_type_discard
,
130 ppir_node_type_branch
,
139 extern const ppir_op_info ppir_op_infos
[];
143 ppir_dep_write_after_read
,
150 struct list_head pred_link
;
151 struct list_head succ_link
;
154 typedef struct ppir_node
{
155 struct list_head list
;
161 struct ppir_instr
*instr
;
163 struct ppir_block
*block
;
166 struct list_head succ_list
;
167 struct list_head pred_list
;
171 ppir_pipeline_reg_const0
,
172 ppir_pipeline_reg_const1
,
173 ppir_pipeline_reg_sampler
,
174 ppir_pipeline_reg_uniform
,
175 ppir_pipeline_reg_vmul
,
176 ppir_pipeline_reg_fmul
,
177 ppir_pipeline_reg_discard
, /* varying load */
180 typedef struct ppir_reg
{
181 struct list_head list
;
186 /* whether this reg has to start from the x component
187 * of a full physical reg, this is true for reg used
188 * in load/store instr which has no swizzle field */
196 ppir_target_pipeline
,
197 ppir_target_register
,
200 typedef struct ppir_src
{
207 ppir_pipeline pipeline
;
211 bool absolute
, negate
;
216 ppir_outmod_clamp_fraction
,
217 ppir_outmod_clamp_positive
,
221 typedef struct ppir_dest
{
227 ppir_pipeline pipeline
;
230 ppir_outmod modifier
;
231 unsigned write_mask
: 4;
239 int shift
: 3; /* Only used for ppir_op_mul */
242 typedef struct ppir_const
{
272 ppir_src src
[2]; /* src[0] temporarily stores src_coords,
273 not to be used after lowering */
279 } ppir_load_texture_node
;
285 enum ppir_instr_slot
{
286 PPIR_INSTR_SLOT_VARYING
,
287 PPIR_INSTR_SLOT_TEXLD
,
288 PPIR_INSTR_SLOT_UNIFORM
,
289 PPIR_INSTR_SLOT_ALU_VEC_MUL
,
290 PPIR_INSTR_SLOT_ALU_SCL_MUL
,
291 PPIR_INSTR_SLOT_ALU_VEC_ADD
,
292 PPIR_INSTR_SLOT_ALU_SCL_ADD
,
293 PPIR_INSTR_SLOT_ALU_COMBINE
,
294 PPIR_INSTR_SLOT_STORE_TEMP
,
295 PPIR_INSTR_SLOT_BRANCH
,
298 PPIR_INSTR_SLOT_ALU_START
= PPIR_INSTR_SLOT_ALU_VEC_MUL
,
299 PPIR_INSTR_SLOT_ALU_END
= PPIR_INSTR_SLOT_ALU_COMBINE
,
302 struct ppir_liveness
{
307 typedef struct ppir_instr
{
308 struct list_head list
;
311 int seq
; /* command sequence after schedule */
313 ppir_node
*slots
[PPIR_INSTR_SLOT_NUM
];
314 ppir_const constant
[2];
318 struct list_head succ_list
;
319 struct list_head pred_list
;
321 int est
; /* earliest start time */
327 /* for liveness analysis */
328 struct ppir_liveness
*live_in
;
329 struct ppir_liveness
*live_out
;
330 struct set
*live_in_set
;
331 struct set
*live_out_set
;
334 typedef struct ppir_block
{
335 struct list_head list
;
336 struct list_head node_list
;
337 struct list_head instr_list
;
339 struct ppir_block
*successors
[2];
341 struct ppir_compiler
*comp
;
344 int sched_instr_index
;
345 int sched_instr_base
;
348 /* for liveness analysis */
349 struct ppir_liveness
*live_in
;
350 struct ppir_liveness
*live_out
;
351 struct set
*live_in_set
;
352 struct set
*live_out_set
;
367 struct lima_fs_shader_state
;
369 typedef struct ppir_compiler
{
370 struct list_head block_list
;
371 struct hash_table_u64
*blocks
;
375 struct list_head reg_list
;
377 /* array for searching ssa/reg node */
378 ppir_node
**var_nodes
;
382 struct lima_fs_shader_state
*prog
;
385 int sched_instr_base
;
387 /* for regalloc spilling debug */
395 ppir_block
*discard_block
;
396 ppir_block
*current_block
;
397 ppir_block
*loop_break_block
;
398 ppir_block
*loop_cont_block
;
401 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
);
402 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
, ppir_dep_type type
);
403 void ppir_node_remove_dep(ppir_dep
*dep
);
404 void ppir_node_delete(ppir_node
*node
);
405 void ppir_node_print_prog(ppir_compiler
*comp
);
406 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
);
407 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
);
408 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
);
409 void ppir_delete_if_orphan(ppir_block
*block
, ppir_node
*node
);
410 ppir_dep
*ppir_dep_for_pred(ppir_node
*node
, ppir_node
*pred
);
411 ppir_node
*ppir_node_clone(ppir_block
*block
, ppir_node
*node
);
412 /* Assumes that node successors are in the same block */
413 ppir_node
*ppir_node_insert_mov(ppir_node
*node
);
415 static inline bool ppir_node_is_root(ppir_node
*node
)
417 return list_is_empty(&node
->succ_list
);
420 static inline bool ppir_node_is_leaf(ppir_node
*node
)
422 return list_is_empty(&node
->pred_list
);
425 static inline bool ppir_node_has_single_succ(ppir_node
*node
)
427 return list_is_singular(&node
->succ_list
);
430 bool ppir_node_has_single_src_succ(ppir_node
*node
);
432 static inline ppir_node
*ppir_node_first_succ(ppir_node
*node
)
434 return list_first_entry(&node
->succ_list
, ppir_dep
, succ_link
)->succ
;
437 static inline bool ppir_node_has_single_pred(ppir_node
*node
)
439 return list_is_singular(&node
->pred_list
);
442 static inline ppir_node
*ppir_node_first_pred(ppir_node
*node
)
444 return list_first_entry(&node
->pred_list
, ppir_dep
, pred_link
)->pred
;
447 #define ppir_node_foreach_succ(node, dep) \
448 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
449 #define ppir_node_foreach_succ_safe(node, dep) \
450 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
451 #define ppir_node_foreach_pred(node, dep) \
452 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
453 #define ppir_node_foreach_pred_safe(node, dep) \
454 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
456 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
457 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
458 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
459 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
460 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
461 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
462 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
464 static inline ppir_dest
*ppir_node_get_dest(ppir_node
*node
)
466 switch (node
->type
) {
467 case ppir_node_type_alu
:
468 return &ppir_node_to_alu(node
)->dest
;
469 case ppir_node_type_load
:
470 return &ppir_node_to_load(node
)->dest
;
471 case ppir_node_type_const
:
472 return &ppir_node_to_const(node
)->dest
;
473 case ppir_node_type_load_texture
:
474 return &ppir_node_to_load_texture(node
)->dest
;
480 static inline int ppir_src_get_mask(ppir_node
*node
)
482 ppir_dest
*dest
= ppir_node_get_dest(node
);
484 return dest
->write_mask
;
489 static inline int ppir_node_get_src_num(ppir_node
*node
)
491 switch (node
->type
) {
492 case ppir_node_type_alu
:
493 return ppir_node_to_alu(node
)->num_src
;
494 case ppir_node_type_branch
:
495 return ppir_node_to_branch(node
)->num_src
;
496 case ppir_node_type_load
:
497 return ppir_node_to_load(node
)->num_src
;
498 case ppir_node_type_load_texture
:
499 return ppir_node_to_load_texture(node
)->num_src
;
500 case ppir_node_type_store
:
509 static inline ppir_src
*ppir_node_get_src(ppir_node
*node
, int idx
)
511 if (idx
< 0 || idx
>= ppir_node_get_src_num(node
))
514 switch (node
->type
) {
515 case ppir_node_type_alu
:
516 return &ppir_node_to_alu(node
)->src
[idx
];
517 case ppir_node_type_branch
:
518 return &ppir_node_to_branch(node
)->src
[idx
];
519 case ppir_node_type_load_texture
:
520 return &ppir_node_to_load_texture(node
)->src
[idx
];
521 case ppir_node_type_load
:
522 return &ppir_node_to_load(node
)->src
;
523 case ppir_node_type_store
:
524 return &ppir_node_to_store(node
)->src
;
532 static inline ppir_reg
*ppir_src_get_reg(ppir_src
*src
)
535 case ppir_target_ssa
:
537 case ppir_target_register
:
544 static inline ppir_reg
*ppir_dest_get_reg(ppir_dest
*dest
)
546 switch (dest
->type
) {
547 case ppir_target_ssa
:
549 case ppir_target_register
:
556 static inline ppir_src
*ppir_node_get_src_for_pred(ppir_node
*node
, ppir_node
*pred
)
558 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
559 ppir_src
*src
= ppir_node_get_src(node
, i
);
560 if (src
&& src
->node
== pred
)
567 static inline void ppir_node_target_assign(ppir_src
*src
, ppir_node
*node
)
569 ppir_dest
*dest
= ppir_node_get_dest(node
);
570 src
->type
= dest
->type
;
572 case ppir_target_ssa
:
573 src
->ssa
= &dest
->ssa
;
576 case ppir_target_register
:
577 src
->reg
= dest
->reg
;
578 /* Registers can be assigned from multiple nodes, so don't keep
579 * pointer to the node here
583 case ppir_target_pipeline
:
584 src
->pipeline
= dest
->pipeline
;
590 static inline bool ppir_node_target_equal(ppir_src
*src
, ppir_dest
*dest
)
592 if (src
->type
!= dest
->type
||
593 (src
->type
== ppir_target_ssa
&& src
->ssa
!= &dest
->ssa
) ||
594 (src
->type
== ppir_target_register
&& src
->reg
!= dest
->reg
) ||
595 (src
->type
== ppir_target_pipeline
&& src
->pipeline
!= dest
->pipeline
))
601 static inline int ppir_target_get_src_reg_index(ppir_src
*src
)
604 case ppir_target_ssa
:
606 return src
->ssa
->index
;
608 case ppir_target_register
:
610 return src
->reg
->index
;
612 case ppir_target_pipeline
:
613 if (src
->pipeline
== ppir_pipeline_reg_discard
)
615 return (src
->pipeline
+ 12) * 4;
621 static inline int ppir_target_get_dest_reg_index(ppir_dest
*dest
)
623 switch (dest
->type
) {
624 case ppir_target_ssa
:
625 return dest
->ssa
.index
;
626 case ppir_target_register
:
627 return dest
->reg
->index
;
628 case ppir_target_pipeline
:
629 if (dest
->pipeline
== ppir_pipeline_reg_discard
)
631 return (dest
->pipeline
+ 12) * 4;
637 static inline bool ppir_target_is_scaler(ppir_dest
*dest
)
639 switch (dest
->type
) {
640 case ppir_target_ssa
:
641 return dest
->ssa
.num_components
== 1;
642 case ppir_target_register
:
643 /* only one bit in mask is set */
644 if ((dest
->write_mask
& 0x3) == 0x3 ||
645 (dest
->write_mask
& 0x5) == 0x5 ||
646 (dest
->write_mask
& 0x9) == 0x9 ||
647 (dest
->write_mask
& 0x6) == 0x6 ||
648 (dest
->write_mask
& 0xa) == 0xa ||
649 (dest
->write_mask
& 0xc) == 0xc)
653 case ppir_target_pipeline
:
654 if (dest
->pipeline
== ppir_pipeline_reg_fmul
)
663 ppir_instr
*ppir_instr_create(ppir_block
*block
);
664 bool ppir_instr_insert_node(ppir_instr
*instr
, ppir_node
*node
);
665 void ppir_instr_add_dep(ppir_instr
*succ
, ppir_instr
*pred
);
666 void ppir_instr_print_list(ppir_compiler
*comp
);
667 void ppir_instr_print_dep(ppir_compiler
*comp
);
668 void ppir_instr_insert_mul_node(ppir_node
*add
, ppir_node
*mul
);
670 #define ppir_instr_foreach_succ(instr, dep) \
671 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
672 #define ppir_instr_foreach_succ_safe(instr, dep) \
673 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
674 #define ppir_instr_foreach_pred(instr, dep) \
675 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
676 #define ppir_instr_foreach_pred_safe(instr, dep) \
677 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
679 static inline bool ppir_instr_is_root(ppir_instr
*instr
)
681 return list_is_empty(&instr
->succ_list
);
684 static inline bool ppir_instr_is_leaf(ppir_instr
*instr
)
686 return list_is_empty(&instr
->pred_list
);
689 bool ppir_lower_prog(ppir_compiler
*comp
);
690 bool ppir_node_to_instr(ppir_compiler
*comp
);
691 bool ppir_schedule_prog(ppir_compiler
*comp
);
692 bool ppir_regalloc_prog(ppir_compiler
*comp
);
693 bool ppir_codegen_prog(ppir_compiler
*comp
);
694 void ppir_liveness_analysis(ppir_compiler
*comp
);