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 struct list_head pred_link
;
142 struct list_head succ_link
;
145 typedef struct ppir_node
{
146 struct list_head list
;
152 struct ppir_instr
*instr
;
154 struct ppir_block
*block
;
157 struct list_head succ_list
;
158 struct list_head pred_list
;
162 ppir_pipeline_reg_const0
,
163 ppir_pipeline_reg_const1
,
164 ppir_pipeline_reg_sampler
,
165 ppir_pipeline_reg_uniform
,
166 ppir_pipeline_reg_vmul
,
167 ppir_pipeline_reg_fmul
,
168 ppir_pipeline_reg_discard
, /* varying load */
171 typedef struct ppir_reg
{
172 struct list_head list
;
176 /* whether this reg has to start from the x component
177 * of a full physical reg, this is true for reg used
178 * in load/store instr which has no swizzle field
181 /* instr live range */
182 int live_in
, live_out
;
189 ppir_target_pipeline
,
190 ppir_target_register
,
193 typedef struct ppir_src
{
200 ppir_pipeline pipeline
;
204 bool absolute
, negate
;
209 ppir_outmod_clamp_fraction
,
210 ppir_outmod_clamp_positive
,
214 typedef struct ppir_dest
{
220 ppir_pipeline pipeline
;
223 ppir_outmod modifier
;
224 unsigned write_mask
: 4;
232 int shift
: 3; /* Only used for ppir_op_mul */
235 typedef struct ppir_const
{
265 ppir_src src_coords
; /* not to be used after lowering */
268 } ppir_load_texture_node
;
274 enum ppir_instr_slot
{
275 PPIR_INSTR_SLOT_VARYING
,
276 PPIR_INSTR_SLOT_TEXLD
,
277 PPIR_INSTR_SLOT_UNIFORM
,
278 PPIR_INSTR_SLOT_ALU_VEC_MUL
,
279 PPIR_INSTR_SLOT_ALU_SCL_MUL
,
280 PPIR_INSTR_SLOT_ALU_VEC_ADD
,
281 PPIR_INSTR_SLOT_ALU_SCL_ADD
,
282 PPIR_INSTR_SLOT_ALU_COMBINE
,
283 PPIR_INSTR_SLOT_STORE_TEMP
,
284 PPIR_INSTR_SLOT_BRANCH
,
287 PPIR_INSTR_SLOT_ALU_START
= PPIR_INSTR_SLOT_ALU_VEC_MUL
,
288 PPIR_INSTR_SLOT_ALU_END
= PPIR_INSTR_SLOT_ALU_COMBINE
,
291 typedef struct ppir_instr
{
292 struct list_head list
;
295 int seq
; /* command sequence after schedule */
297 ppir_node
*slots
[PPIR_INSTR_SLOT_NUM
];
298 ppir_const constant
[2];
302 struct list_head succ_list
;
303 struct list_head pred_list
;
305 int est
; /* earliest start time */
312 typedef struct ppir_block
{
313 struct list_head list
;
314 struct list_head node_list
;
315 struct list_head instr_list
;
317 struct ppir_block
*successors
[2];
319 struct ppir_compiler
*comp
;
322 int sched_instr_index
;
323 int sched_instr_base
;
326 /* for liveness analysis */
329 BITSET_WORD
*live_in
;
330 BITSET_WORD
*live_out
;
345 struct lima_fs_shader_state
;
347 typedef struct ppir_compiler
{
348 struct list_head block_list
;
349 struct hash_table_u64
*blocks
;
353 struct list_head reg_list
;
355 /* array for searching ssa/reg node */
356 ppir_node
**var_nodes
;
360 struct lima_fs_shader_state
*prog
;
363 int sched_instr_base
;
365 /* for regalloc spilling debug */
373 ppir_block
*discard_block
;
374 ppir_block
*current_block
;
375 ppir_block
*loop_break_block
;
376 ppir_block
*loop_cont_block
;
379 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
);
380 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
);
381 void ppir_node_remove_dep(ppir_dep
*dep
);
382 void ppir_node_delete(ppir_node
*node
);
383 void ppir_node_print_prog(ppir_compiler
*comp
);
384 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
);
385 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
);
386 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
);
387 ppir_dep
*ppir_dep_for_pred(ppir_node
*node
, ppir_node
*pred
);
388 ppir_node
*ppir_node_clone(ppir_block
*block
, ppir_node
*node
);
389 /* Assumes that node successors are in the same block */
390 ppir_node
*ppir_node_insert_mov(ppir_node
*node
);
392 static inline bool ppir_node_is_root(ppir_node
*node
)
394 return list_empty(&node
->succ_list
);
397 static inline bool ppir_node_is_leaf(ppir_node
*node
)
399 return list_empty(&node
->pred_list
);
402 static inline bool ppir_node_has_single_succ(ppir_node
*node
)
404 return list_is_singular(&node
->succ_list
);
407 static inline ppir_node
*ppir_node_first_succ(ppir_node
*node
)
409 return list_first_entry(&node
->succ_list
, ppir_dep
, succ_link
)->succ
;
412 static inline bool ppir_node_has_single_pred(ppir_node
*node
)
414 return list_is_singular(&node
->pred_list
);
417 static inline ppir_node
*ppir_node_first_pred(ppir_node
*node
)
419 return list_first_entry(&node
->pred_list
, ppir_dep
, pred_link
)->pred
;
422 #define ppir_node_foreach_succ(node, dep) \
423 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
424 #define ppir_node_foreach_succ_safe(node, dep) \
425 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
426 #define ppir_node_foreach_pred(node, dep) \
427 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
428 #define ppir_node_foreach_pred_safe(node, dep) \
429 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
431 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
432 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
433 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
434 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
435 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
436 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
437 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
439 static inline ppir_dest
*ppir_node_get_dest(ppir_node
*node
)
441 switch (node
->type
) {
442 case ppir_node_type_alu
:
443 return &ppir_node_to_alu(node
)->dest
;
444 case ppir_node_type_load
:
445 return &ppir_node_to_load(node
)->dest
;
446 case ppir_node_type_const
:
447 return &ppir_node_to_const(node
)->dest
;
448 case ppir_node_type_load_texture
:
449 return &ppir_node_to_load_texture(node
)->dest
;
455 static inline int ppir_node_get_src_num(ppir_node
*node
)
457 switch (node
->type
) {
458 case ppir_node_type_alu
:
459 return ppir_node_to_alu(node
)->num_src
;
460 case ppir_node_type_branch
:
461 return ppir_node_to_branch(node
)->num_src
;
462 case ppir_node_type_load
:
463 return ppir_node_to_load(node
)->num_src
;
464 case ppir_node_type_load_texture
:
465 case ppir_node_type_store
:
474 static inline ppir_src
*ppir_node_get_src(ppir_node
*node
, int idx
)
476 if (idx
< 0 || idx
>= ppir_node_get_src_num(node
))
479 switch (node
->type
) {
480 case ppir_node_type_alu
:
481 return &ppir_node_to_alu(node
)->src
[idx
];
482 case ppir_node_type_branch
:
483 return &ppir_node_to_branch(node
)->src
[idx
];
484 case ppir_node_type_load_texture
:
485 return &ppir_node_to_load_texture(node
)->src_coords
;
486 case ppir_node_type_load
:
487 return &ppir_node_to_load(node
)->src
;
488 case ppir_node_type_store
:
489 return &ppir_node_to_store(node
)->src
;
497 static inline ppir_reg
*ppir_src_get_reg(ppir_src
*src
)
500 case ppir_target_ssa
:
502 case ppir_target_register
:
509 static inline ppir_reg
*ppir_dest_get_reg(ppir_dest
*dest
)
511 switch (dest
->type
) {
512 case ppir_target_ssa
:
514 case ppir_target_register
:
521 static inline ppir_src
*ppir_node_get_src_for_pred(ppir_node
*node
, ppir_node
*pred
)
523 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
524 ppir_src
*src
= ppir_node_get_src(node
, i
);
525 if (src
&& src
->node
== pred
)
532 static inline void ppir_node_target_assign(ppir_src
*src
, ppir_node
*node
)
534 ppir_dest
*dest
= ppir_node_get_dest(node
);
535 src
->type
= dest
->type
;
537 case ppir_target_ssa
:
538 src
->ssa
= &dest
->ssa
;
541 case ppir_target_register
:
542 src
->reg
= dest
->reg
;
543 /* Registers can be assigned from multiple nodes, so don't keep
544 * pointer to the node here
548 case ppir_target_pipeline
:
549 src
->pipeline
= dest
->pipeline
;
555 static inline bool ppir_node_target_equal(ppir_src
*src
, ppir_dest
*dest
)
557 if (src
->type
!= dest
->type
||
558 (src
->type
== ppir_target_ssa
&& src
->ssa
!= &dest
->ssa
) ||
559 (src
->type
== ppir_target_register
&& src
->reg
!= dest
->reg
) ||
560 (src
->type
== ppir_target_pipeline
&& src
->pipeline
!= dest
->pipeline
))
566 static inline int ppir_target_get_src_reg_index(ppir_src
*src
)
569 case ppir_target_ssa
:
571 return src
->ssa
->index
;
573 case ppir_target_register
:
575 return src
->reg
->index
;
577 case ppir_target_pipeline
:
578 if (src
->pipeline
== ppir_pipeline_reg_discard
)
580 return (src
->pipeline
+ 12) * 4;
586 static inline int ppir_target_get_dest_reg_index(ppir_dest
*dest
)
588 switch (dest
->type
) {
589 case ppir_target_ssa
:
590 return dest
->ssa
.index
;
591 case ppir_target_register
:
592 return dest
->reg
->index
;
593 case ppir_target_pipeline
:
594 if (dest
->pipeline
== ppir_pipeline_reg_discard
)
596 return (dest
->pipeline
+ 12) * 4;
602 static inline bool ppir_target_is_scaler(ppir_dest
*dest
)
604 switch (dest
->type
) {
605 case ppir_target_ssa
:
606 return dest
->ssa
.num_components
== 1;
607 case ppir_target_register
:
608 /* only one bit in mask is set */
609 if ((dest
->write_mask
& 0x3) == 0x3 ||
610 (dest
->write_mask
& 0x5) == 0x5 ||
611 (dest
->write_mask
& 0x9) == 0x9 ||
612 (dest
->write_mask
& 0x6) == 0x6 ||
613 (dest
->write_mask
& 0xa) == 0xa ||
614 (dest
->write_mask
& 0xc) == 0xc)
618 case ppir_target_pipeline
:
619 if (dest
->pipeline
== ppir_pipeline_reg_fmul
)
628 ppir_instr
*ppir_instr_create(ppir_block
*block
);
629 bool ppir_instr_insert_node(ppir_instr
*instr
, ppir_node
*node
);
630 void ppir_instr_add_dep(ppir_instr
*succ
, ppir_instr
*pred
);
631 void ppir_instr_print_list(ppir_compiler
*comp
);
632 void ppir_instr_print_dep(ppir_compiler
*comp
);
633 void ppir_instr_insert_mul_node(ppir_node
*add
, ppir_node
*mul
);
635 #define ppir_instr_foreach_succ(instr, dep) \
636 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
637 #define ppir_instr_foreach_succ_safe(instr, dep) \
638 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
639 #define ppir_instr_foreach_pred(instr, dep) \
640 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
641 #define ppir_instr_foreach_pred_safe(instr, dep) \
642 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
644 static inline bool ppir_instr_is_root(ppir_instr
*instr
)
646 return list_empty(&instr
->succ_list
);
649 static inline bool ppir_instr_is_leaf(ppir_instr
*instr
)
651 return list_empty(&instr
->pred_list
);
654 bool ppir_lower_prog(ppir_compiler
*comp
);
655 bool ppir_node_to_instr(ppir_compiler
*comp
);
656 bool ppir_schedule_prog(ppir_compiler
*comp
);
657 bool ppir_regalloc_prog(ppir_compiler
*comp
);
658 bool ppir_codegen_prog(ppir_compiler
*comp
);
659 void ppir_liveness_analysis(ppir_compiler
*comp
);