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"
102 ppir_op_load_uniform
,
103 ppir_op_load_varying
,
105 ppir_op_load_fragcoord
,
106 ppir_op_load_pointcoord
,
107 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
;
175 /* whether this reg has to start from the x component
176 * of a full physical reg, this is true for reg used
177 * in load/store instr which has no swizzle field
180 /* instr live range */
181 int live_in
, live_out
;
187 ppir_target_pipeline
,
188 ppir_target_register
,
191 typedef struct ppir_src
{
197 ppir_pipeline pipeline
;
201 bool absolute
, negate
;
206 ppir_outmod_clamp_fraction
,
207 ppir_outmod_clamp_positive
,
211 typedef struct ppir_dest
{
217 ppir_pipeline pipeline
;
220 ppir_outmod modifier
;
221 unsigned write_mask
: 4;
229 int shift
: 3; /* Only used for ppir_op_mul */
232 typedef struct ppir_const
{
261 ppir_src src_coords
; /* not to be used after lowering */
264 } ppir_load_texture_node
;
270 enum ppir_instr_slot
{
271 PPIR_INSTR_SLOT_VARYING
,
272 PPIR_INSTR_SLOT_TEXLD
,
273 PPIR_INSTR_SLOT_UNIFORM
,
274 PPIR_INSTR_SLOT_ALU_VEC_MUL
,
275 PPIR_INSTR_SLOT_ALU_SCL_MUL
,
276 PPIR_INSTR_SLOT_ALU_VEC_ADD
,
277 PPIR_INSTR_SLOT_ALU_SCL_ADD
,
278 PPIR_INSTR_SLOT_ALU_COMBINE
,
279 PPIR_INSTR_SLOT_STORE_TEMP
,
280 PPIR_INSTR_SLOT_BRANCH
,
283 PPIR_INSTR_SLOT_ALU_START
= PPIR_INSTR_SLOT_ALU_VEC_MUL
,
284 PPIR_INSTR_SLOT_ALU_END
= PPIR_INSTR_SLOT_ALU_COMBINE
,
287 typedef struct ppir_instr
{
288 struct list_head list
;
291 int seq
; /* command sequence after schedule */
293 ppir_node
*slots
[PPIR_INSTR_SLOT_NUM
];
294 ppir_const constant
[2];
298 struct list_head succ_list
;
299 struct list_head pred_list
;
301 int est
; /* earliest start time */
308 typedef struct ppir_block
{
309 struct list_head list
;
310 struct list_head node_list
;
311 struct list_head instr_list
;
312 struct ppir_compiler
*comp
;
315 int sched_instr_index
;
316 int sched_instr_base
;
329 struct lima_fs_shader_state
;
331 typedef struct ppir_compiler
{
332 struct list_head block_list
;
336 struct list_head reg_list
;
338 /* array for searching ssa/reg node */
339 ppir_node
**var_nodes
;
343 struct lima_fs_shader_state
*prog
;
346 int sched_instr_base
;
348 /* for regalloc spilling debug */
351 ppir_block
*discard_block
;
354 void *ppir_node_create(ppir_block
*block
, ppir_op op
, int index
, unsigned mask
);
355 void ppir_node_add_dep(ppir_node
*succ
, ppir_node
*pred
);
356 void ppir_node_remove_dep(ppir_dep
*dep
);
357 void ppir_node_delete(ppir_node
*node
);
358 void ppir_node_print_prog(ppir_compiler
*comp
);
359 void ppir_node_replace_child(ppir_node
*parent
, ppir_node
*old_child
, ppir_node
*new_child
);
360 void ppir_node_replace_all_succ(ppir_node
*dst
, ppir_node
*src
);
361 void ppir_node_replace_pred(ppir_dep
*dep
, ppir_node
*new_pred
);
363 static inline bool ppir_node_is_root(ppir_node
*node
)
365 return list_empty(&node
->succ_list
);
368 static inline bool ppir_node_is_leaf(ppir_node
*node
)
370 return list_empty(&node
->pred_list
);
373 static inline bool ppir_node_has_single_succ(ppir_node
*node
)
375 return list_is_singular(&node
->succ_list
);
378 static inline ppir_node
*ppir_node_first_succ(ppir_node
*node
)
380 return list_first_entry(&node
->succ_list
, ppir_dep
, succ_link
)->succ
;
383 static inline bool ppir_node_has_single_pred(ppir_node
*node
)
385 return list_is_singular(&node
->pred_list
);
388 static inline ppir_node
*ppir_node_first_pred(ppir_node
*node
)
390 return list_first_entry(&node
->pred_list
, ppir_dep
, pred_link
)->pred
;
393 #define ppir_node_foreach_succ(node, dep) \
394 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
395 #define ppir_node_foreach_succ_safe(node, dep) \
396 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
397 #define ppir_node_foreach_pred(node, dep) \
398 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
399 #define ppir_node_foreach_pred_safe(node, dep) \
400 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
402 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
403 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
404 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
405 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
406 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
407 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
408 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
410 static inline ppir_dest
*ppir_node_get_dest(ppir_node
*node
)
412 switch (node
->type
) {
413 case ppir_node_type_alu
:
414 return &ppir_node_to_alu(node
)->dest
;
415 case ppir_node_type_load
:
416 return &ppir_node_to_load(node
)->dest
;
417 case ppir_node_type_const
:
418 return &ppir_node_to_const(node
)->dest
;
419 case ppir_node_type_load_texture
:
420 return &ppir_node_to_load_texture(node
)->dest
;
426 static inline void ppir_node_target_assign(ppir_src
*src
, ppir_dest
*dest
)
428 src
->type
= dest
->type
;
430 case ppir_target_ssa
:
431 src
->ssa
= &dest
->ssa
;
433 case ppir_target_register
:
434 src
->reg
= dest
->reg
;
436 case ppir_target_pipeline
:
437 src
->pipeline
= dest
->pipeline
;
442 static inline bool ppir_node_target_equal(ppir_src
*src
, ppir_dest
*dest
)
444 if (src
->type
!= dest
->type
||
445 (src
->type
== ppir_target_ssa
&& src
->ssa
!= &dest
->ssa
) ||
446 (src
->type
== ppir_target_register
&& src
->reg
!= dest
->reg
) ||
447 (src
->type
== ppir_target_pipeline
&& src
->pipeline
!= dest
->pipeline
))
453 static inline int ppir_target_get_src_reg_index(ppir_src
*src
)
456 case ppir_target_ssa
:
457 return src
->ssa
->index
;
458 case ppir_target_register
:
459 return src
->reg
->index
;
460 case ppir_target_pipeline
:
461 if (src
->pipeline
== ppir_pipeline_reg_discard
)
463 return (src
->pipeline
+ 12) * 4;
469 static inline int ppir_target_get_dest_reg_index(ppir_dest
*dest
)
471 switch (dest
->type
) {
472 case ppir_target_ssa
:
473 return dest
->ssa
.index
;
474 case ppir_target_register
:
475 return dest
->reg
->index
;
476 case ppir_target_pipeline
:
477 if (dest
->pipeline
== ppir_pipeline_reg_discard
)
479 return (dest
->pipeline
+ 12) * 4;
485 static inline bool ppir_target_is_scaler(ppir_dest
*dest
)
487 switch (dest
->type
) {
488 case ppir_target_ssa
:
489 return dest
->ssa
.num_components
== 1;
490 case ppir_target_register
:
491 /* only one bit in mask is set */
492 if ((dest
->write_mask
& 0x3) == 0x3 ||
493 (dest
->write_mask
& 0x5) == 0x5 ||
494 (dest
->write_mask
& 0x9) == 0x9 ||
495 (dest
->write_mask
& 0x6) == 0x6 ||
496 (dest
->write_mask
& 0xa) == 0xa ||
497 (dest
->write_mask
& 0xc) == 0xc)
501 case ppir_target_pipeline
:
502 if (dest
->pipeline
== ppir_pipeline_reg_fmul
)
511 ppir_instr
*ppir_instr_create(ppir_block
*block
);
512 bool ppir_instr_insert_node(ppir_instr
*instr
, ppir_node
*node
);
513 void ppir_instr_add_dep(ppir_instr
*succ
, ppir_instr
*pred
);
514 void ppir_instr_print_list(ppir_compiler
*comp
);
515 void ppir_instr_print_dep(ppir_compiler
*comp
);
516 void ppir_instr_insert_mul_node(ppir_node
*add
, ppir_node
*mul
);
518 #define ppir_instr_foreach_succ(instr, dep) \
519 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
520 #define ppir_instr_foreach_succ_safe(instr, dep) \
521 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
522 #define ppir_instr_foreach_pred(instr, dep) \
523 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
524 #define ppir_instr_foreach_pred_safe(instr, dep) \
525 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
527 static inline bool ppir_instr_is_root(ppir_instr
*instr
)
529 return list_empty(&instr
->succ_list
);
532 static inline bool ppir_instr_is_leaf(ppir_instr
*instr
)
534 return list_empty(&instr
->pred_list
);
537 bool ppir_lower_prog(ppir_compiler
*comp
);
538 bool ppir_node_to_instr(ppir_compiler
*comp
);
539 bool ppir_schedule_prog(ppir_compiler
*comp
);
540 bool ppir_regalloc_prog(ppir_compiler
*comp
);
541 bool ppir_codegen_prog(ppir_compiler
*comp
);