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_fragcoord
,
103 ppir_op_load_pointcoord
,
104 ppir_op_load_frontface
,
105 ppir_op_load_texture
,
123 ppir_node_type_const
,
125 ppir_node_type_store
,
126 ppir_node_type_load_texture
,
127 ppir_node_type_discard
,
128 ppir_node_type_branch
,
137 extern const ppir_op_info ppir_op_infos
[];
141 ppir_dep_write_after_read
,
148 struct list_head pred_link
;
149 struct list_head succ_link
;
152 typedef struct ppir_node
{
153 struct list_head list
;
159 struct ppir_instr
*instr
;
161 struct ppir_block
*block
;
164 struct list_head succ_list
;
165 struct list_head pred_list
;
169 ppir_pipeline_reg_const0
,
170 ppir_pipeline_reg_const1
,
171 ppir_pipeline_reg_sampler
,
172 ppir_pipeline_reg_uniform
,
173 ppir_pipeline_reg_vmul
,
174 ppir_pipeline_reg_fmul
,
175 ppir_pipeline_reg_discard
, /* varying load */
178 typedef struct ppir_reg
{
179 struct list_head list
;
183 /* whether this reg has to start from the x component
184 * of a full physical reg, this is true for reg used
185 * in load/store instr which has no swizzle field
188 /* instr live range */
189 int live_in
, live_out
;
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_coords
; /* not to be used after lowering */
275 } ppir_load_texture_node
;
281 enum ppir_instr_slot
{
282 PPIR_INSTR_SLOT_VARYING
,
283 PPIR_INSTR_SLOT_TEXLD
,
284 PPIR_INSTR_SLOT_UNIFORM
,
285 PPIR_INSTR_SLOT_ALU_VEC_MUL
,
286 PPIR_INSTR_SLOT_ALU_SCL_MUL
,
287 PPIR_INSTR_SLOT_ALU_VEC_ADD
,
288 PPIR_INSTR_SLOT_ALU_SCL_ADD
,
289 PPIR_INSTR_SLOT_ALU_COMBINE
,
290 PPIR_INSTR_SLOT_STORE_TEMP
,
291 PPIR_INSTR_SLOT_BRANCH
,
294 PPIR_INSTR_SLOT_ALU_START
= PPIR_INSTR_SLOT_ALU_VEC_MUL
,
295 PPIR_INSTR_SLOT_ALU_END
= PPIR_INSTR_SLOT_ALU_COMBINE
,
298 typedef struct ppir_instr
{
299 struct list_head list
;
302 int seq
; /* command sequence after schedule */
304 ppir_node
*slots
[PPIR_INSTR_SLOT_NUM
];
305 ppir_const constant
[2];
309 struct list_head succ_list
;
310 struct list_head pred_list
;
312 int est
; /* earliest start time */
319 typedef struct ppir_block
{
320 struct list_head list
;
321 struct list_head node_list
;
322 struct list_head instr_list
;
324 struct ppir_block
*successors
[2];
326 struct ppir_compiler
*comp
;
329 int sched_instr_index
;
330 int sched_instr_base
;
333 /* for liveness analysis */
336 BITSET_WORD
*live_in
;
337 BITSET_WORD
*live_out
;
352 struct lima_fs_shader_state
;
354 typedef struct ppir_compiler
{
355 struct list_head block_list
;
356 struct hash_table_u64
*blocks
;
360 struct list_head reg_list
;
362 /* array for searching ssa/reg node */
363 ppir_node
**var_nodes
;
367 struct lima_fs_shader_state
*prog
;
370 int sched_instr_base
;
372 /* for regalloc spilling debug */
380 ppir_block
*discard_block
;
381 ppir_block
*current_block
;
382 ppir_block
*loop_break_block
;
383 ppir_block
*loop_cont_block
;
386 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
);
387 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
, ppir_dep_type type
);
388 void ppir_node_remove_dep(ppir_dep
*dep
);
389 void ppir_node_delete(ppir_node
*node
);
390 void ppir_node_print_prog(ppir_compiler
*comp
);
391 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
);
392 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
);
393 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
);
394 ppir_dep
*ppir_dep_for_pred(ppir_node
*node
, ppir_node
*pred
);
395 ppir_node
*ppir_node_clone(ppir_block
*block
, ppir_node
*node
);
396 /* Assumes that node successors are in the same block */
397 ppir_node
*ppir_node_insert_mov(ppir_node
*node
);
399 static inline bool ppir_node_is_root(ppir_node
*node
)
401 return list_empty(&node
->succ_list
);
404 static inline bool ppir_node_is_leaf(ppir_node
*node
)
406 return list_empty(&node
->pred_list
);
409 static inline bool ppir_node_has_single_succ(ppir_node
*node
)
411 return list_is_singular(&node
->succ_list
);
414 bool ppir_node_has_single_src_succ(ppir_node
*node
);
416 static inline ppir_node
*ppir_node_first_succ(ppir_node
*node
)
418 return list_first_entry(&node
->succ_list
, ppir_dep
, succ_link
)->succ
;
421 static inline bool ppir_node_has_single_pred(ppir_node
*node
)
423 return list_is_singular(&node
->pred_list
);
426 static inline ppir_node
*ppir_node_first_pred(ppir_node
*node
)
428 return list_first_entry(&node
->pred_list
, ppir_dep
, pred_link
)->pred
;
431 #define ppir_node_foreach_succ(node, dep) \
432 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
433 #define ppir_node_foreach_succ_safe(node, dep) \
434 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
435 #define ppir_node_foreach_pred(node, dep) \
436 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
437 #define ppir_node_foreach_pred_safe(node, dep) \
438 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
440 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
441 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
442 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
443 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
444 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
445 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
446 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
448 static inline ppir_dest
*ppir_node_get_dest(ppir_node
*node
)
450 switch (node
->type
) {
451 case ppir_node_type_alu
:
452 return &ppir_node_to_alu(node
)->dest
;
453 case ppir_node_type_load
:
454 return &ppir_node_to_load(node
)->dest
;
455 case ppir_node_type_const
:
456 return &ppir_node_to_const(node
)->dest
;
457 case ppir_node_type_load_texture
:
458 return &ppir_node_to_load_texture(node
)->dest
;
464 static inline int ppir_node_get_src_num(ppir_node
*node
)
466 switch (node
->type
) {
467 case ppir_node_type_alu
:
468 return ppir_node_to_alu(node
)->num_src
;
469 case ppir_node_type_branch
:
470 return ppir_node_to_branch(node
)->num_src
;
471 case ppir_node_type_load
:
472 return ppir_node_to_load(node
)->num_src
;
473 case ppir_node_type_load_texture
:
474 case ppir_node_type_store
:
483 static inline ppir_src
*ppir_node_get_src(ppir_node
*node
, int idx
)
485 if (idx
< 0 || idx
>= ppir_node_get_src_num(node
))
488 switch (node
->type
) {
489 case ppir_node_type_alu
:
490 return &ppir_node_to_alu(node
)->src
[idx
];
491 case ppir_node_type_branch
:
492 return &ppir_node_to_branch(node
)->src
[idx
];
493 case ppir_node_type_load_texture
:
494 return &ppir_node_to_load_texture(node
)->src_coords
;
495 case ppir_node_type_load
:
496 return &ppir_node_to_load(node
)->src
;
497 case ppir_node_type_store
:
498 return &ppir_node_to_store(node
)->src
;
506 static inline ppir_reg
*ppir_src_get_reg(ppir_src
*src
)
509 case ppir_target_ssa
:
511 case ppir_target_register
:
518 static inline ppir_reg
*ppir_dest_get_reg(ppir_dest
*dest
)
520 switch (dest
->type
) {
521 case ppir_target_ssa
:
523 case ppir_target_register
:
530 static inline ppir_src
*ppir_node_get_src_for_pred(ppir_node
*node
, ppir_node
*pred
)
532 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
533 ppir_src
*src
= ppir_node_get_src(node
, i
);
534 if (src
&& src
->node
== pred
)
541 static inline void ppir_node_target_assign(ppir_src
*src
, ppir_node
*node
)
543 ppir_dest
*dest
= ppir_node_get_dest(node
);
544 src
->type
= dest
->type
;
546 case ppir_target_ssa
:
547 src
->ssa
= &dest
->ssa
;
550 case ppir_target_register
:
551 src
->reg
= dest
->reg
;
552 /* Registers can be assigned from multiple nodes, so don't keep
553 * pointer to the node here
557 case ppir_target_pipeline
:
558 src
->pipeline
= dest
->pipeline
;
564 static inline bool ppir_node_target_equal(ppir_src
*src
, ppir_dest
*dest
)
566 if (src
->type
!= dest
->type
||
567 (src
->type
== ppir_target_ssa
&& src
->ssa
!= &dest
->ssa
) ||
568 (src
->type
== ppir_target_register
&& src
->reg
!= dest
->reg
) ||
569 (src
->type
== ppir_target_pipeline
&& src
->pipeline
!= dest
->pipeline
))
575 static inline int ppir_target_get_src_reg_index(ppir_src
*src
)
578 case ppir_target_ssa
:
580 return src
->ssa
->index
;
582 case ppir_target_register
:
584 return src
->reg
->index
;
586 case ppir_target_pipeline
:
587 if (src
->pipeline
== ppir_pipeline_reg_discard
)
589 return (src
->pipeline
+ 12) * 4;
595 static inline int ppir_target_get_dest_reg_index(ppir_dest
*dest
)
597 switch (dest
->type
) {
598 case ppir_target_ssa
:
599 return dest
->ssa
.index
;
600 case ppir_target_register
:
601 return dest
->reg
->index
;
602 case ppir_target_pipeline
:
603 if (dest
->pipeline
== ppir_pipeline_reg_discard
)
605 return (dest
->pipeline
+ 12) * 4;
611 static inline bool ppir_target_is_scaler(ppir_dest
*dest
)
613 switch (dest
->type
) {
614 case ppir_target_ssa
:
615 return dest
->ssa
.num_components
== 1;
616 case ppir_target_register
:
617 /* only one bit in mask is set */
618 if ((dest
->write_mask
& 0x3) == 0x3 ||
619 (dest
->write_mask
& 0x5) == 0x5 ||
620 (dest
->write_mask
& 0x9) == 0x9 ||
621 (dest
->write_mask
& 0x6) == 0x6 ||
622 (dest
->write_mask
& 0xa) == 0xa ||
623 (dest
->write_mask
& 0xc) == 0xc)
627 case ppir_target_pipeline
:
628 if (dest
->pipeline
== ppir_pipeline_reg_fmul
)
637 ppir_instr
*ppir_instr_create(ppir_block
*block
);
638 bool ppir_instr_insert_node(ppir_instr
*instr
, ppir_node
*node
);
639 void ppir_instr_add_dep(ppir_instr
*succ
, ppir_instr
*pred
);
640 void ppir_instr_print_list(ppir_compiler
*comp
);
641 void ppir_instr_print_dep(ppir_compiler
*comp
);
642 void ppir_instr_insert_mul_node(ppir_node
*add
, ppir_node
*mul
);
644 #define ppir_instr_foreach_succ(instr, dep) \
645 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
646 #define ppir_instr_foreach_succ_safe(instr, dep) \
647 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
648 #define ppir_instr_foreach_pred(instr, dep) \
649 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
650 #define ppir_instr_foreach_pred_safe(instr, dep) \
651 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
653 static inline bool ppir_instr_is_root(ppir_instr
*instr
)
655 return list_empty(&instr
->succ_list
);
658 static inline bool ppir_instr_is_leaf(ppir_instr
*instr
)
660 return list_empty(&instr
->pred_list
);
663 bool ppir_lower_prog(ppir_compiler
*comp
);
664 bool ppir_node_to_instr(ppir_compiler
*comp
);
665 bool ppir_schedule_prog(ppir_compiler
*comp
);
666 bool ppir_regalloc_prog(ppir_compiler
*comp
);
667 bool ppir_codegen_prog(ppir_compiler
*comp
);
668 void ppir_liveness_analysis(ppir_compiler
*comp
);