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"
31 #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
;
165 struct list_head succ_list
;
166 struct list_head pred_list
;
170 ppir_pipeline_reg_const0
,
171 ppir_pipeline_reg_const1
,
172 ppir_pipeline_reg_sampler
,
173 ppir_pipeline_reg_uniform
,
174 ppir_pipeline_reg_vmul
,
175 ppir_pipeline_reg_fmul
,
176 ppir_pipeline_reg_discard
, /* varying load */
179 typedef struct ppir_reg
{
180 struct list_head list
;
184 /* whether this reg has to start from the x component
185 * of a full physical reg, this is true for reg used
186 * in load/store instr which has no swizzle field
189 /* instr live range */
190 int live_in
, live_out
;
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_coords
; /* 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 typedef struct ppir_instr
{
303 struct list_head list
;
306 int seq
; /* command sequence after schedule */
308 ppir_node
*slots
[PPIR_INSTR_SLOT_NUM
];
309 ppir_const constant
[2];
313 struct list_head succ_list
;
314 struct list_head pred_list
;
316 int est
; /* earliest start time */
323 typedef struct ppir_block
{
324 struct list_head list
;
325 struct list_head node_list
;
326 struct list_head instr_list
;
328 struct ppir_block
*successors
[2];
330 struct ppir_compiler
*comp
;
333 int sched_instr_index
;
334 int sched_instr_base
;
337 /* for liveness analysis */
340 BITSET_WORD
*live_in
;
341 BITSET_WORD
*live_out
;
356 struct lima_fs_shader_state
;
358 typedef struct ppir_compiler
{
359 struct list_head block_list
;
360 struct hash_table_u64
*blocks
;
364 struct list_head reg_list
;
366 /* array for searching ssa/reg node */
367 ppir_node
**var_nodes
;
371 struct lima_fs_shader_state
*prog
;
374 int sched_instr_base
;
376 /* for regalloc spilling debug */
384 ppir_block
*discard_block
;
385 ppir_block
*current_block
;
386 ppir_block
*loop_break_block
;
387 ppir_block
*loop_cont_block
;
390 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
);
391 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
, ppir_dep_type type
);
392 void ppir_node_remove_dep(ppir_dep
*dep
);
393 void ppir_node_delete(ppir_node
*node
);
394 void ppir_node_print_prog(ppir_compiler
*comp
);
395 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
);
396 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
);
397 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
);
398 ppir_dep
*ppir_dep_for_pred(ppir_node
*node
, ppir_node
*pred
);
399 ppir_node
*ppir_node_clone(ppir_block
*block
, ppir_node
*node
);
400 /* Assumes that node successors are in the same block */
401 ppir_node
*ppir_node_insert_mov(ppir_node
*node
);
403 static inline bool ppir_node_is_root(ppir_node
*node
)
405 return list_is_empty(&node
->succ_list
);
408 static inline bool ppir_node_is_leaf(ppir_node
*node
)
410 return list_is_empty(&node
->pred_list
);
413 static inline bool ppir_node_has_single_succ(ppir_node
*node
)
415 return list_is_singular(&node
->succ_list
);
418 bool ppir_node_has_single_src_succ(ppir_node
*node
);
420 static inline ppir_node
*ppir_node_first_succ(ppir_node
*node
)
422 return list_first_entry(&node
->succ_list
, ppir_dep
, succ_link
)->succ
;
425 static inline bool ppir_node_has_single_pred(ppir_node
*node
)
427 return list_is_singular(&node
->pred_list
);
430 static inline ppir_node
*ppir_node_first_pred(ppir_node
*node
)
432 return list_first_entry(&node
->pred_list
, ppir_dep
, pred_link
)->pred
;
435 #define ppir_node_foreach_succ(node, dep) \
436 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
437 #define ppir_node_foreach_succ_safe(node, dep) \
438 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
439 #define ppir_node_foreach_pred(node, dep) \
440 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
441 #define ppir_node_foreach_pred_safe(node, dep) \
442 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
444 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
445 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
446 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
447 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
448 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
449 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
450 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
452 static inline ppir_dest
*ppir_node_get_dest(ppir_node
*node
)
454 switch (node
->type
) {
455 case ppir_node_type_alu
:
456 return &ppir_node_to_alu(node
)->dest
;
457 case ppir_node_type_load
:
458 return &ppir_node_to_load(node
)->dest
;
459 case ppir_node_type_const
:
460 return &ppir_node_to_const(node
)->dest
;
461 case ppir_node_type_load_texture
:
462 return &ppir_node_to_load_texture(node
)->dest
;
468 static inline int ppir_node_get_src_num(ppir_node
*node
)
470 switch (node
->type
) {
471 case ppir_node_type_alu
:
472 return ppir_node_to_alu(node
)->num_src
;
473 case ppir_node_type_branch
:
474 return ppir_node_to_branch(node
)->num_src
;
475 case ppir_node_type_load
:
476 return ppir_node_to_load(node
)->num_src
;
477 case ppir_node_type_load_texture
:
478 case ppir_node_type_store
:
487 static inline ppir_src
*ppir_node_get_src(ppir_node
*node
, int idx
)
489 if (idx
< 0 || idx
>= ppir_node_get_src_num(node
))
492 switch (node
->type
) {
493 case ppir_node_type_alu
:
494 return &ppir_node_to_alu(node
)->src
[idx
];
495 case ppir_node_type_branch
:
496 return &ppir_node_to_branch(node
)->src
[idx
];
497 case ppir_node_type_load_texture
:
498 return &ppir_node_to_load_texture(node
)->src_coords
;
499 case ppir_node_type_load
:
500 return &ppir_node_to_load(node
)->src
;
501 case ppir_node_type_store
:
502 return &ppir_node_to_store(node
)->src
;
510 static inline ppir_reg
*ppir_src_get_reg(ppir_src
*src
)
513 case ppir_target_ssa
:
515 case ppir_target_register
:
522 static inline ppir_reg
*ppir_dest_get_reg(ppir_dest
*dest
)
524 switch (dest
->type
) {
525 case ppir_target_ssa
:
527 case ppir_target_register
:
534 static inline ppir_src
*ppir_node_get_src_for_pred(ppir_node
*node
, ppir_node
*pred
)
536 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
537 ppir_src
*src
= ppir_node_get_src(node
, i
);
538 if (src
&& src
->node
== pred
)
545 static inline void ppir_node_target_assign(ppir_src
*src
, ppir_node
*node
)
547 ppir_dest
*dest
= ppir_node_get_dest(node
);
548 src
->type
= dest
->type
;
550 case ppir_target_ssa
:
551 src
->ssa
= &dest
->ssa
;
554 case ppir_target_register
:
555 src
->reg
= dest
->reg
;
556 /* Registers can be assigned from multiple nodes, so don't keep
557 * pointer to the node here
561 case ppir_target_pipeline
:
562 src
->pipeline
= dest
->pipeline
;
568 static inline bool ppir_node_target_equal(ppir_src
*src
, ppir_dest
*dest
)
570 if (src
->type
!= dest
->type
||
571 (src
->type
== ppir_target_ssa
&& src
->ssa
!= &dest
->ssa
) ||
572 (src
->type
== ppir_target_register
&& src
->reg
!= dest
->reg
) ||
573 (src
->type
== ppir_target_pipeline
&& src
->pipeline
!= dest
->pipeline
))
579 static inline int ppir_target_get_src_reg_index(ppir_src
*src
)
582 case ppir_target_ssa
:
584 return src
->ssa
->index
;
586 case ppir_target_register
:
588 return src
->reg
->index
;
590 case ppir_target_pipeline
:
591 if (src
->pipeline
== ppir_pipeline_reg_discard
)
593 return (src
->pipeline
+ 12) * 4;
599 static inline int ppir_target_get_dest_reg_index(ppir_dest
*dest
)
601 switch (dest
->type
) {
602 case ppir_target_ssa
:
603 return dest
->ssa
.index
;
604 case ppir_target_register
:
605 return dest
->reg
->index
;
606 case ppir_target_pipeline
:
607 if (dest
->pipeline
== ppir_pipeline_reg_discard
)
609 return (dest
->pipeline
+ 12) * 4;
615 static inline bool ppir_target_is_scaler(ppir_dest
*dest
)
617 switch (dest
->type
) {
618 case ppir_target_ssa
:
619 return dest
->ssa
.num_components
== 1;
620 case ppir_target_register
:
621 /* only one bit in mask is set */
622 if ((dest
->write_mask
& 0x3) == 0x3 ||
623 (dest
->write_mask
& 0x5) == 0x5 ||
624 (dest
->write_mask
& 0x9) == 0x9 ||
625 (dest
->write_mask
& 0x6) == 0x6 ||
626 (dest
->write_mask
& 0xa) == 0xa ||
627 (dest
->write_mask
& 0xc) == 0xc)
631 case ppir_target_pipeline
:
632 if (dest
->pipeline
== ppir_pipeline_reg_fmul
)
641 ppir_instr
*ppir_instr_create(ppir_block
*block
);
642 bool ppir_instr_insert_node(ppir_instr
*instr
, ppir_node
*node
);
643 void ppir_instr_add_dep(ppir_instr
*succ
, ppir_instr
*pred
);
644 void ppir_instr_print_list(ppir_compiler
*comp
);
645 void ppir_instr_print_dep(ppir_compiler
*comp
);
646 void ppir_instr_insert_mul_node(ppir_node
*add
, ppir_node
*mul
);
648 #define ppir_instr_foreach_succ(instr, dep) \
649 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
650 #define ppir_instr_foreach_succ_safe(instr, dep) \
651 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
652 #define ppir_instr_foreach_pred(instr, dep) \
653 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
654 #define ppir_instr_foreach_pred_safe(instr, dep) \
655 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
657 static inline bool ppir_instr_is_root(ppir_instr
*instr
)
659 return list_is_empty(&instr
->succ_list
);
662 static inline bool ppir_instr_is_leaf(ppir_instr
*instr
)
664 return list_is_empty(&instr
->pred_list
);
667 bool ppir_lower_prog(ppir_compiler
*comp
);
668 bool ppir_node_to_instr(ppir_compiler
*comp
);
669 bool ppir_schedule_prog(ppir_compiler
*comp
);
670 bool ppir_regalloc_prog(ppir_compiler
*comp
);
671 bool ppir_codegen_prog(ppir_compiler
*comp
);
672 void ppir_liveness_analysis(ppir_compiler
*comp
);