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_varying
,
102 ppir_op_load_coords_reg
,
103 ppir_op_load_fragcoord
,
104 ppir_op_load_pointcoord
,
105 ppir_op_load_frontface
,
106 ppir_op_load_texture
,
124 ppir_node_type_const
,
126 ppir_node_type_store
,
127 ppir_node_type_load_texture
,
128 ppir_node_type_discard
,
129 ppir_node_type_branch
,
138 extern const ppir_op_info ppir_op_infos
[];
142 ppir_dep_write_after_read
,
149 struct list_head pred_link
;
150 struct list_head succ_link
;
153 typedef struct ppir_node
{
154 struct list_head list
;
160 struct ppir_instr
*instr
;
162 struct ppir_block
*block
;
164 bool succ_different_block
;
167 struct list_head succ_list
;
168 struct list_head pred_list
;
172 ppir_pipeline_reg_const0
,
173 ppir_pipeline_reg_const1
,
174 ppir_pipeline_reg_sampler
,
175 ppir_pipeline_reg_uniform
,
176 ppir_pipeline_reg_vmul
,
177 ppir_pipeline_reg_fmul
,
178 ppir_pipeline_reg_discard
, /* varying load */
181 typedef struct ppir_reg
{
182 struct list_head list
;
187 /* whether this reg has to start from the x component
188 * of a full physical reg, this is true for reg used
189 * in load/store instr which has no swizzle field */
197 ppir_target_pipeline
,
198 ppir_target_register
,
201 typedef struct ppir_src
{
208 ppir_pipeline pipeline
;
212 bool absolute
, negate
;
217 ppir_outmod_clamp_fraction
,
218 ppir_outmod_clamp_positive
,
222 typedef struct ppir_dest
{
228 ppir_pipeline pipeline
;
231 ppir_outmod modifier
;
232 unsigned write_mask
: 4;
240 int shift
: 3; /* Only used for ppir_op_mul */
243 typedef struct ppir_const
{
273 ppir_src src
[2]; /* src[0] temporarily stores src_coords,
274 not to be used after lowering */
280 } ppir_load_texture_node
;
286 enum ppir_instr_slot
{
287 PPIR_INSTR_SLOT_VARYING
,
288 PPIR_INSTR_SLOT_TEXLD
,
289 PPIR_INSTR_SLOT_UNIFORM
,
290 PPIR_INSTR_SLOT_ALU_VEC_MUL
,
291 PPIR_INSTR_SLOT_ALU_SCL_MUL
,
292 PPIR_INSTR_SLOT_ALU_VEC_ADD
,
293 PPIR_INSTR_SLOT_ALU_SCL_ADD
,
294 PPIR_INSTR_SLOT_ALU_COMBINE
,
295 PPIR_INSTR_SLOT_STORE_TEMP
,
296 PPIR_INSTR_SLOT_BRANCH
,
299 PPIR_INSTR_SLOT_ALU_START
= PPIR_INSTR_SLOT_ALU_VEC_MUL
,
300 PPIR_INSTR_SLOT_ALU_END
= PPIR_INSTR_SLOT_ALU_COMBINE
,
303 struct ppir_liveness
{
308 typedef struct ppir_instr
{
309 struct list_head list
;
312 int seq
; /* command sequence after schedule */
314 ppir_node
*slots
[PPIR_INSTR_SLOT_NUM
];
315 ppir_const constant
[2];
319 struct list_head succ_list
;
320 struct list_head pred_list
;
322 int est
; /* earliest start time */
328 /* for liveness analysis */
329 struct ppir_liveness
*live_in
;
330 struct ppir_liveness
*live_out
;
331 /* live_internal is to mark registers only live within an
332 * instruction, without propagation */
333 struct ppir_liveness
*live_internal
;
334 struct set
*live_in_set
;
335 struct set
*live_out_set
;
336 struct set
*live_internal_set
;
339 typedef struct ppir_block
{
340 struct list_head list
;
341 struct list_head node_list
;
342 struct list_head instr_list
;
344 struct ppir_block
*successors
[2];
346 struct ppir_compiler
*comp
;
349 int sched_instr_index
;
350 int sched_instr_base
;
353 /* for liveness analysis */
354 struct ppir_liveness
*live_in
;
355 struct ppir_liveness
*live_out
;
356 struct set
*live_in_set
;
357 struct set
*live_out_set
;
372 struct lima_fs_shader_state
;
374 typedef struct ppir_compiler
{
375 struct list_head block_list
;
376 struct hash_table_u64
*blocks
;
380 struct list_head reg_list
;
382 /* array for searching ssa/reg node */
383 ppir_node
**var_nodes
;
387 struct lima_fs_shader_state
*prog
;
391 int sched_instr_base
;
393 /* for regalloc spilling debug */
401 ppir_block
*discard_block
;
402 ppir_block
*current_block
;
403 ppir_block
*loop_break_block
;
404 ppir_block
*loop_cont_block
;
407 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
);
408 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
, ppir_dep_type type
);
409 void ppir_node_remove_dep(ppir_dep
*dep
);
410 void ppir_node_delete(ppir_node
*node
);
411 void ppir_node_print_prog(ppir_compiler
*comp
);
412 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
);
413 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
);
414 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
);
415 ppir_dep
*ppir_dep_for_pred(ppir_node
*node
, ppir_node
*pred
);
416 /* Assumes that node successors are in the same block */
417 ppir_node
*ppir_node_insert_mov(ppir_node
*node
);
419 static inline bool ppir_node_is_root(ppir_node
*node
)
421 return list_is_empty(&node
->succ_list
);
424 static inline bool ppir_node_is_leaf(ppir_node
*node
)
426 return list_is_empty(&node
->pred_list
);
429 static inline bool ppir_node_has_single_succ(ppir_node
*node
)
431 return list_is_singular(&node
->succ_list
)
432 && !node
->succ_different_block
;
435 bool ppir_node_has_single_src_succ(ppir_node
*node
);
437 static inline ppir_node
*ppir_node_first_succ(ppir_node
*node
)
439 return list_first_entry(&node
->succ_list
, ppir_dep
, succ_link
)->succ
;
442 static inline bool ppir_node_has_single_pred(ppir_node
*node
)
444 return list_is_singular(&node
->pred_list
);
447 static inline ppir_node
*ppir_node_first_pred(ppir_node
*node
)
449 return list_first_entry(&node
->pred_list
, ppir_dep
, pred_link
)->pred
;
452 #define ppir_node_foreach_succ(node, dep) \
453 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
454 #define ppir_node_foreach_succ_safe(node, dep) \
455 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
456 #define ppir_node_foreach_pred(node, dep) \
457 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
458 #define ppir_node_foreach_pred_safe(node, dep) \
459 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
461 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
462 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
463 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
464 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
465 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
466 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
467 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
469 static inline ppir_dest
*ppir_node_get_dest(ppir_node
*node
)
471 switch (node
->type
) {
472 case ppir_node_type_alu
:
473 return &ppir_node_to_alu(node
)->dest
;
474 case ppir_node_type_load
:
475 return &ppir_node_to_load(node
)->dest
;
476 case ppir_node_type_const
:
477 return &ppir_node_to_const(node
)->dest
;
478 case ppir_node_type_load_texture
:
479 return &ppir_node_to_load_texture(node
)->dest
;
485 static inline int ppir_node_get_src_num(ppir_node
*node
)
487 switch (node
->type
) {
488 case ppir_node_type_alu
:
489 return ppir_node_to_alu(node
)->num_src
;
490 case ppir_node_type_branch
:
491 return ppir_node_to_branch(node
)->num_src
;
492 case ppir_node_type_load
:
493 return ppir_node_to_load(node
)->num_src
;
494 case ppir_node_type_load_texture
:
495 return ppir_node_to_load_texture(node
)->num_src
;
496 case ppir_node_type_store
:
505 static inline ppir_src
*ppir_node_get_src(ppir_node
*node
, int idx
)
507 if (idx
< 0 || idx
>= ppir_node_get_src_num(node
))
510 switch (node
->type
) {
511 case ppir_node_type_alu
:
512 return &ppir_node_to_alu(node
)->src
[idx
];
513 case ppir_node_type_branch
:
514 return &ppir_node_to_branch(node
)->src
[idx
];
515 case ppir_node_type_load_texture
:
516 return &ppir_node_to_load_texture(node
)->src
[idx
];
517 case ppir_node_type_load
:
518 return &ppir_node_to_load(node
)->src
;
519 case ppir_node_type_store
:
520 return &ppir_node_to_store(node
)->src
;
528 static inline ppir_reg
*ppir_src_get_reg(ppir_src
*src
)
531 case ppir_target_ssa
:
533 case ppir_target_register
:
540 static inline ppir_reg
*ppir_dest_get_reg(ppir_dest
*dest
)
542 switch (dest
->type
) {
543 case ppir_target_ssa
:
545 case ppir_target_register
:
552 static inline void ppir_node_target_assign(ppir_src
*src
, ppir_node
*node
)
554 ppir_dest
*dest
= ppir_node_get_dest(node
);
555 src
->type
= dest
->type
;
557 case ppir_target_ssa
:
558 src
->ssa
= &dest
->ssa
;
561 case ppir_target_register
:
562 src
->reg
= dest
->reg
;
563 /* Registers can be assigned from multiple nodes, so don't keep
564 * pointer to the node here
568 case ppir_target_pipeline
:
569 src
->pipeline
= dest
->pipeline
;
575 static inline bool ppir_node_target_equal(ppir_src
*src
, ppir_dest
*dest
)
577 if (src
->type
!= dest
->type
||
578 (src
->type
== ppir_target_ssa
&& src
->ssa
!= &dest
->ssa
) ||
579 (src
->type
== ppir_target_register
&& src
->reg
!= dest
->reg
) ||
580 (src
->type
== ppir_target_pipeline
&& src
->pipeline
!= dest
->pipeline
))
586 static inline int ppir_target_get_src_reg_index(ppir_src
*src
)
589 case ppir_target_ssa
:
591 return src
->ssa
->index
;
593 case ppir_target_register
:
595 return src
->reg
->index
;
597 case ppir_target_pipeline
:
598 if (src
->pipeline
== ppir_pipeline_reg_discard
)
600 return (src
->pipeline
+ 12) * 4;
606 static inline int ppir_target_get_dest_reg_index(ppir_dest
*dest
)
608 switch (dest
->type
) {
609 case ppir_target_ssa
:
610 return dest
->ssa
.index
;
611 case ppir_target_register
:
612 return dest
->reg
->index
;
613 case ppir_target_pipeline
:
614 if (dest
->pipeline
== ppir_pipeline_reg_discard
)
616 return (dest
->pipeline
+ 12) * 4;
622 static inline int ppir_src_get_mask(ppir_src
*src
)
624 ppir_reg
*reg
= ppir_src_get_reg(src
);
627 for (int i
= 0; i
< reg
->num_components
; i
++)
628 mask
|= (1 << src
->swizzle
[i
]);
633 static inline bool ppir_target_is_scalar(ppir_dest
*dest
)
635 switch (dest
->type
) {
636 case ppir_target_ssa
:
637 return dest
->ssa
.num_components
== 1;
638 case ppir_target_register
:
639 /* only one bit in mask is set */
640 if ((dest
->write_mask
& 0x3) == 0x3 ||
641 (dest
->write_mask
& 0x5) == 0x5 ||
642 (dest
->write_mask
& 0x9) == 0x9 ||
643 (dest
->write_mask
& 0x6) == 0x6 ||
644 (dest
->write_mask
& 0xa) == 0xa ||
645 (dest
->write_mask
& 0xc) == 0xc)
649 case ppir_target_pipeline
:
650 if (dest
->pipeline
== ppir_pipeline_reg_fmul
)
659 static inline bool ppir_node_schedulable_slot(ppir_node
*node
,
660 enum ppir_instr_slot slot
)
662 int *slots
= ppir_op_infos
[node
->op
].slots
;
663 for (int i
= 0; slots
[i
] != PPIR_INSTR_SLOT_END
; i
++)
664 if (slots
[i
] == slot
)
670 ppir_instr
*ppir_instr_create(ppir_block
*block
);
671 bool ppir_instr_insert_node(ppir_instr
*instr
, ppir_node
*node
);
672 void ppir_instr_add_dep(ppir_instr
*succ
, ppir_instr
*pred
);
673 void ppir_instr_print_list(ppir_compiler
*comp
);
674 void ppir_instr_print_dep(ppir_compiler
*comp
);
675 void ppir_instr_insert_mul_node(ppir_node
*add
, ppir_node
*mul
);
677 #define ppir_instr_foreach_succ(instr, dep) \
678 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
679 #define ppir_instr_foreach_succ_safe(instr, dep) \
680 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
681 #define ppir_instr_foreach_pred(instr, dep) \
682 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
683 #define ppir_instr_foreach_pred_safe(instr, dep) \
684 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
686 static inline bool ppir_instr_is_root(ppir_instr
*instr
)
688 return list_is_empty(&instr
->succ_list
);
691 static inline bool ppir_instr_is_leaf(ppir_instr
*instr
)
693 return list_is_empty(&instr
->pred_list
);
696 bool ppir_lower_prog(ppir_compiler
*comp
);
697 bool ppir_node_to_instr(ppir_compiler
*comp
);
698 bool ppir_schedule_prog(ppir_compiler
*comp
);
699 bool ppir_regalloc_prog(ppir_compiler
*comp
);
700 bool ppir_codegen_prog(ppir_compiler
*comp
);
701 void ppir_liveness_analysis(ppir_compiler
*comp
);