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
[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 typedef struct ppir_instr
{
304 struct list_head list
;
307 int seq
; /* command sequence after schedule */
309 ppir_node
*slots
[PPIR_INSTR_SLOT_NUM
];
310 ppir_const constant
[2];
314 struct list_head succ_list
;
315 struct list_head pred_list
;
317 int est
; /* earliest start time */
324 typedef struct ppir_block
{
325 struct list_head list
;
326 struct list_head node_list
;
327 struct list_head instr_list
;
329 struct ppir_block
*successors
[2];
331 struct ppir_compiler
*comp
;
334 int sched_instr_index
;
335 int sched_instr_base
;
338 /* for liveness analysis */
341 BITSET_WORD
*live_in
;
342 BITSET_WORD
*live_out
;
357 struct lima_fs_shader_state
;
359 typedef struct ppir_compiler
{
360 struct list_head block_list
;
361 struct hash_table_u64
*blocks
;
365 struct list_head reg_list
;
367 /* array for searching ssa/reg node */
368 ppir_node
**var_nodes
;
372 struct lima_fs_shader_state
*prog
;
375 int sched_instr_base
;
377 /* for regalloc spilling debug */
385 ppir_block
*discard_block
;
386 ppir_block
*current_block
;
387 ppir_block
*loop_break_block
;
388 ppir_block
*loop_cont_block
;
391 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
);
392 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
, ppir_dep_type type
);
393 void ppir_node_remove_dep(ppir_dep
*dep
);
394 void ppir_node_delete(ppir_node
*node
);
395 void ppir_node_print_prog(ppir_compiler
*comp
);
396 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
);
397 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
);
398 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
);
399 ppir_dep
*ppir_dep_for_pred(ppir_node
*node
, ppir_node
*pred
);
400 ppir_node
*ppir_node_clone(ppir_block
*block
, ppir_node
*node
);
401 /* Assumes that node successors are in the same block */
402 ppir_node
*ppir_node_insert_mov(ppir_node
*node
);
404 static inline bool ppir_node_is_root(ppir_node
*node
)
406 return list_is_empty(&node
->succ_list
);
409 static inline bool ppir_node_is_leaf(ppir_node
*node
)
411 return list_is_empty(&node
->pred_list
);
414 static inline bool ppir_node_has_single_succ(ppir_node
*node
)
416 return list_is_singular(&node
->succ_list
);
419 bool ppir_node_has_single_src_succ(ppir_node
*node
);
421 static inline ppir_node
*ppir_node_first_succ(ppir_node
*node
)
423 return list_first_entry(&node
->succ_list
, ppir_dep
, succ_link
)->succ
;
426 static inline bool ppir_node_has_single_pred(ppir_node
*node
)
428 return list_is_singular(&node
->pred_list
);
431 static inline ppir_node
*ppir_node_first_pred(ppir_node
*node
)
433 return list_first_entry(&node
->pred_list
, ppir_dep
, pred_link
)->pred
;
436 #define ppir_node_foreach_succ(node, dep) \
437 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
438 #define ppir_node_foreach_succ_safe(node, dep) \
439 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
440 #define ppir_node_foreach_pred(node, dep) \
441 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
442 #define ppir_node_foreach_pred_safe(node, dep) \
443 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
445 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
446 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
447 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
448 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
449 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
450 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
451 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
453 static inline ppir_dest
*ppir_node_get_dest(ppir_node
*node
)
455 switch (node
->type
) {
456 case ppir_node_type_alu
:
457 return &ppir_node_to_alu(node
)->dest
;
458 case ppir_node_type_load
:
459 return &ppir_node_to_load(node
)->dest
;
460 case ppir_node_type_const
:
461 return &ppir_node_to_const(node
)->dest
;
462 case ppir_node_type_load_texture
:
463 return &ppir_node_to_load_texture(node
)->dest
;
469 static inline int ppir_node_get_src_num(ppir_node
*node
)
471 switch (node
->type
) {
472 case ppir_node_type_alu
:
473 return ppir_node_to_alu(node
)->num_src
;
474 case ppir_node_type_branch
:
475 return ppir_node_to_branch(node
)->num_src
;
476 case ppir_node_type_load
:
477 return ppir_node_to_load(node
)->num_src
;
478 case ppir_node_type_load_texture
:
479 return ppir_node_to_load_texture(node
)->num_src
;
480 case ppir_node_type_store
:
489 static inline ppir_src
*ppir_node_get_src(ppir_node
*node
, int idx
)
491 if (idx
< 0 || idx
>= ppir_node_get_src_num(node
))
494 switch (node
->type
) {
495 case ppir_node_type_alu
:
496 return &ppir_node_to_alu(node
)->src
[idx
];
497 case ppir_node_type_branch
:
498 return &ppir_node_to_branch(node
)->src
[idx
];
499 case ppir_node_type_load_texture
:
500 return &ppir_node_to_load_texture(node
)->src
[idx
];
501 case ppir_node_type_load
:
502 return &ppir_node_to_load(node
)->src
;
503 case ppir_node_type_store
:
504 return &ppir_node_to_store(node
)->src
;
512 static inline ppir_reg
*ppir_src_get_reg(ppir_src
*src
)
515 case ppir_target_ssa
:
517 case ppir_target_register
:
524 static inline ppir_reg
*ppir_dest_get_reg(ppir_dest
*dest
)
526 switch (dest
->type
) {
527 case ppir_target_ssa
:
529 case ppir_target_register
:
536 static inline ppir_src
*ppir_node_get_src_for_pred(ppir_node
*node
, ppir_node
*pred
)
538 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
539 ppir_src
*src
= ppir_node_get_src(node
, i
);
540 if (src
&& src
->node
== pred
)
547 static inline void ppir_node_target_assign(ppir_src
*src
, ppir_node
*node
)
549 ppir_dest
*dest
= ppir_node_get_dest(node
);
550 src
->type
= dest
->type
;
552 case ppir_target_ssa
:
553 src
->ssa
= &dest
->ssa
;
556 case ppir_target_register
:
557 src
->reg
= dest
->reg
;
558 /* Registers can be assigned from multiple nodes, so don't keep
559 * pointer to the node here
563 case ppir_target_pipeline
:
564 src
->pipeline
= dest
->pipeline
;
570 static inline bool ppir_node_target_equal(ppir_src
*src
, ppir_dest
*dest
)
572 if (src
->type
!= dest
->type
||
573 (src
->type
== ppir_target_ssa
&& src
->ssa
!= &dest
->ssa
) ||
574 (src
->type
== ppir_target_register
&& src
->reg
!= dest
->reg
) ||
575 (src
->type
== ppir_target_pipeline
&& src
->pipeline
!= dest
->pipeline
))
581 static inline int ppir_target_get_src_reg_index(ppir_src
*src
)
584 case ppir_target_ssa
:
586 return src
->ssa
->index
;
588 case ppir_target_register
:
590 return src
->reg
->index
;
592 case ppir_target_pipeline
:
593 if (src
->pipeline
== ppir_pipeline_reg_discard
)
595 return (src
->pipeline
+ 12) * 4;
601 static inline int ppir_target_get_dest_reg_index(ppir_dest
*dest
)
603 switch (dest
->type
) {
604 case ppir_target_ssa
:
605 return dest
->ssa
.index
;
606 case ppir_target_register
:
607 return dest
->reg
->index
;
608 case ppir_target_pipeline
:
609 if (dest
->pipeline
== ppir_pipeline_reg_discard
)
611 return (dest
->pipeline
+ 12) * 4;
617 static inline bool ppir_target_is_scaler(ppir_dest
*dest
)
619 switch (dest
->type
) {
620 case ppir_target_ssa
:
621 return dest
->ssa
.num_components
== 1;
622 case ppir_target_register
:
623 /* only one bit in mask is set */
624 if ((dest
->write_mask
& 0x3) == 0x3 ||
625 (dest
->write_mask
& 0x5) == 0x5 ||
626 (dest
->write_mask
& 0x9) == 0x9 ||
627 (dest
->write_mask
& 0x6) == 0x6 ||
628 (dest
->write_mask
& 0xa) == 0xa ||
629 (dest
->write_mask
& 0xc) == 0xc)
633 case ppir_target_pipeline
:
634 if (dest
->pipeline
== ppir_pipeline_reg_fmul
)
643 ppir_instr
*ppir_instr_create(ppir_block
*block
);
644 bool ppir_instr_insert_node(ppir_instr
*instr
, ppir_node
*node
);
645 void ppir_instr_add_dep(ppir_instr
*succ
, ppir_instr
*pred
);
646 void ppir_instr_print_list(ppir_compiler
*comp
);
647 void ppir_instr_print_dep(ppir_compiler
*comp
);
648 void ppir_instr_insert_mul_node(ppir_node
*add
, ppir_node
*mul
);
650 #define ppir_instr_foreach_succ(instr, dep) \
651 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
652 #define ppir_instr_foreach_succ_safe(instr, dep) \
653 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
654 #define ppir_instr_foreach_pred(instr, dep) \
655 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
656 #define ppir_instr_foreach_pred_safe(instr, dep) \
657 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
659 static inline bool ppir_instr_is_root(ppir_instr
*instr
)
661 return list_is_empty(&instr
->succ_list
);
664 static inline bool ppir_instr_is_leaf(ppir_instr
*instr
)
666 return list_is_empty(&instr
->pred_list
);
669 bool ppir_lower_prog(ppir_compiler
*comp
);
670 bool ppir_node_to_instr(ppir_compiler
*comp
);
671 bool ppir_schedule_prog(ppir_compiler
*comp
);
672 bool ppir_regalloc_prog(ppir_compiler
*comp
);
673 bool ppir_codegen_prog(ppir_compiler
*comp
);
674 void ppir_liveness_analysis(ppir_compiler
*comp
);