2 * Copyright (c) 2017 Lima Project
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "util/bitscan.h"
26 #include "util/ralloc.h"
30 static bool ppir_lower_const(ppir_block
*block
, ppir_node
*node
)
32 if (ppir_node_is_root(node
)) {
33 ppir_node_delete(node
);
37 assert(ppir_node_has_single_succ(node
));
39 ppir_node
*succ
= ppir_node_first_succ(node
);
40 ppir_dest
*dest
= ppir_node_get_dest(node
);
43 case ppir_node_type_alu
:
44 case ppir_node_type_branch
:
45 /* ALU and branch can consume consts directly */
46 dest
->type
= ppir_target_pipeline
;
47 /* Reg will be updated in node_to_instr later */
48 dest
->pipeline
= ppir_pipeline_reg_const0
;
50 /* single succ can still have multiple references to this node */
51 for (int i
= 0; i
< ppir_node_get_src_num(succ
); i
++) {
52 ppir_src
*src
= ppir_node_get_src(succ
, i
);
53 if (src
&& src
->node
== node
) {
54 src
->type
= ppir_target_pipeline
;
55 src
->pipeline
= ppir_pipeline_reg_const0
;
60 /* Create a move for everyone else */
64 ppir_node
*move
= ppir_node_insert_mov(node
);
68 ppir_debug("lower const create move %d for %d\n",
69 move
->index
, node
->index
);
71 /* Need to be careful with changing src/dst type here:
72 * it has to be done *after* successors have their children
73 * replaced, otherwise ppir_node_replace_child() won't find
74 * matching src/dst and as result won't work
76 ppir_src
*mov_src
= ppir_node_get_src(move
, 0);
77 mov_src
->type
= dest
->type
= ppir_target_pipeline
;
78 mov_src
->pipeline
= dest
->pipeline
= ppir_pipeline_reg_const0
;
83 static bool ppir_lower_swap_args(ppir_block
*block
, ppir_node
*node
)
85 /* swapped op must be the next op */
88 assert(node
->type
== ppir_node_type_alu
);
89 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
90 assert(alu
->num_src
== 2);
92 ppir_src tmp
= alu
->src
[0];
93 alu
->src
[0] = alu
->src
[1];
98 static bool ppir_lower_load(ppir_block
*block
, ppir_node
*node
)
100 ppir_dest
*dest
= ppir_node_get_dest(node
);
101 if (ppir_node_is_root(node
) && dest
->type
== ppir_target_ssa
) {
102 ppir_node_delete(node
);
106 /* load can have multiple successors in case if we duplicated load node
107 * that has load node in source
109 if ((ppir_node_has_single_src_succ(node
) || ppir_node_is_root(node
)) &&
110 dest
->type
!= ppir_target_register
) {
111 ppir_node
*succ
= ppir_node_first_succ(node
);
112 switch (succ
->type
) {
113 case ppir_node_type_alu
:
114 case ppir_node_type_branch
: {
115 /* single succ can still have multiple references to this node */
116 for (int i
= 0; i
< ppir_node_get_src_num(succ
); i
++) {
117 ppir_src
*src
= ppir_node_get_src(succ
, i
);
118 if (src
&& src
->node
== node
) {
119 /* Can consume uniforms directly */
120 src
->type
= dest
->type
= ppir_target_pipeline
;
121 src
->pipeline
= dest
->pipeline
= ppir_pipeline_reg_uniform
;
127 /* Create mov for everyone else */
132 ppir_node
*move
= ppir_node_insert_mov(node
);
136 ppir_src
*mov_src
= ppir_node_get_src(move
, 0);
137 mov_src
->type
= dest
->type
= ppir_target_pipeline
;
138 mov_src
->pipeline
= dest
->pipeline
= ppir_pipeline_reg_uniform
;
143 static bool ppir_lower_ddxy(ppir_block
*block
, ppir_node
*node
)
145 assert(node
->type
== ppir_node_type_alu
);
146 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
148 alu
->src
[1] = alu
->src
[0];
149 if (node
->op
== ppir_op_ddx
)
150 alu
->src
[1].negate
= !alu
->src
[1].negate
;
151 else if (node
->op
== ppir_op_ddy
)
152 alu
->src
[0].negate
= !alu
->src
[0].negate
;
161 static bool ppir_lower_texture(ppir_block
*block
, ppir_node
*node
)
163 ppir_dest
*dest
= ppir_node_get_dest(node
);
165 if (ppir_node_has_single_succ(node
)) {
166 ppir_node
*succ
= ppir_node_first_succ(node
);
167 dest
->type
= ppir_target_pipeline
;
168 dest
->pipeline
= ppir_pipeline_reg_sampler
;
170 for (int i
= 0; i
< ppir_node_get_src_num(succ
); i
++) {
171 ppir_src
*src
= ppir_node_get_src(succ
, i
);
172 if (src
&& src
->node
== node
) {
173 src
->type
= ppir_target_pipeline
;
174 src
->pipeline
= ppir_pipeline_reg_sampler
;
180 /* Create move node as fallback */
181 ppir_node
*move
= ppir_node_insert_mov(node
);
185 ppir_debug("lower texture create move %d for %d\n",
186 move
->index
, node
->index
);
188 ppir_src
*mov_src
= ppir_node_get_src(move
, 0);
189 mov_src
->type
= dest
->type
= ppir_target_pipeline
;
190 mov_src
->pipeline
= dest
->pipeline
= ppir_pipeline_reg_sampler
;
195 /* Check if the select condition and ensure it can be inserted to
196 * the scalar mul slot */
197 static bool ppir_lower_select(ppir_block
*block
, ppir_node
*node
)
199 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
200 ppir_src
*src0
= &alu
->src
[0];
201 ppir_src
*src1
= &alu
->src
[1];
202 ppir_src
*src2
= &alu
->src
[2];
204 /* If the condition is already an alu scalar whose only successor
205 * is the select node, just turn it into pipeline output. */
206 /* The (src2->node == cond) case is a tricky exception.
207 * The reason is that we must force cond to output to ^fmul -- but
208 * then it no longer writes to a register and it is impossible to
209 * reference ^fmul in src2. So in that exceptional case, also fall
210 * back to the mov. */
211 ppir_node
*cond
= src0
->node
;
213 cond
->type
== ppir_node_type_alu
&&
214 ppir_node_has_single_succ(cond
) &&
215 ppir_target_is_scalar(ppir_node_get_dest(cond
)) &&
216 ppir_node_schedulable_slot(cond
, PPIR_INSTR_SLOT_ALU_SCL_MUL
) &&
217 src2
->node
!= cond
) {
219 ppir_dest
*cond_dest
= ppir_node_get_dest(cond
);
220 cond_dest
->type
= ppir_target_pipeline
;
221 cond_dest
->pipeline
= ppir_pipeline_reg_fmul
;
223 ppir_node_target_assign(src0
, cond
);
225 /* src1 could also be a reference from the same node as
226 * the condition, so update it in that case. */
227 if (src1
->node
&& src1
->node
== cond
)
228 ppir_node_target_assign(src1
, cond
);
233 /* If the condition can't be used for any reason, insert a mov
234 * so that the condition can end up in ^fmul */
235 ppir_node
*move
= ppir_node_create(block
, ppir_op_mov
, -1, 0);
238 list_addtail(&move
->list
, &node
->list
);
240 ppir_alu_node
*move_alu
= ppir_node_to_alu(move
);
241 ppir_src
*move_src
= move_alu
->src
;
242 move_src
->type
= src0
->type
;
243 move_src
->ssa
= src0
->ssa
;
244 move_src
->swizzle
[0] = src0
->swizzle
[0];
245 move_alu
->num_src
= 1;
247 ppir_dest
*move_dest
= &move_alu
->dest
;
248 move_dest
->type
= ppir_target_pipeline
;
249 move_dest
->pipeline
= ppir_pipeline_reg_fmul
;
250 move_dest
->write_mask
= 1;
252 ppir_node
*pred
= src0
->node
;
253 ppir_dep
*dep
= ppir_dep_for_pred(node
, pred
);
255 ppir_node_replace_pred(dep
, move
);
257 ppir_node_add_dep(node
, move
, ppir_dep_src
);
259 /* pred can be a register */
261 ppir_node_add_dep(move
, pred
, ppir_dep_src
);
263 ppir_node_target_assign(src0
, move
);
265 /* src1 could also be a reference from the same node as
266 * the condition, so update it in that case. */
267 if (src1
->node
&& src1
->node
== pred
)
268 ppir_node_target_assign(src1
, move
);
273 static bool ppir_lower_trunc(ppir_block
*block
, ppir_node
*node
)
275 /* Turn it into a mov with a round to integer output modifier */
276 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
277 ppir_dest
*move_dest
= &alu
->dest
;
278 move_dest
->modifier
= ppir_outmod_round
;
279 node
->op
= ppir_op_mov
;
284 static bool ppir_lower_abs(ppir_block
*block
, ppir_node
*node
)
286 /* Turn it into a mov and set the absolute modifier */
287 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
289 assert(alu
->num_src
== 1);
291 alu
->src
[0].absolute
= true;
292 alu
->src
[0].negate
= false;
293 node
->op
= ppir_op_mov
;
298 static bool ppir_lower_neg(ppir_block
*block
, ppir_node
*node
)
300 /* Turn it into a mov and set the negate modifier */
301 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
303 assert(alu
->num_src
== 1);
305 alu
->src
[0].negate
= !alu
->src
[0].negate
;
306 node
->op
= ppir_op_mov
;
311 static bool ppir_lower_sat(ppir_block
*block
, ppir_node
*node
)
313 /* Turn it into a mov with the saturate output modifier */
314 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
316 assert(alu
->num_src
== 1);
318 ppir_dest
*move_dest
= &alu
->dest
;
319 move_dest
->modifier
= ppir_outmod_clamp_fraction
;
320 node
->op
= ppir_op_mov
;
325 static bool ppir_lower_branch(ppir_block
*block
, ppir_node
*node
)
327 ppir_branch_node
*branch
= ppir_node_to_branch(node
);
329 /* Unconditional branch */
330 if (branch
->num_src
== 0)
333 ppir_const_node
*zero
= ppir_node_create(block
, ppir_op_const
, -1, 0);
338 zero
->constant
.value
[0].f
= 0;
339 zero
->constant
.num
= 1;
340 zero
->dest
.type
= ppir_target_pipeline
;
341 zero
->dest
.pipeline
= ppir_pipeline_reg_const0
;
342 zero
->dest
.ssa
.num_components
= 1;
343 zero
->dest
.write_mask
= 0x01;
345 /* For now we're just comparing branch condition with 0,
346 * in future we should look whether it's possible to move
347 * comparision node into branch itself and use current
348 * way as a fallback for complex conditions.
350 ppir_node_target_assign(&branch
->src
[1], &zero
->node
);
353 branch
->cond_eq
= true;
355 branch
->cond_gt
= true;
356 branch
->cond_lt
= true;
361 ppir_node_add_dep(&branch
->node
, &zero
->node
, ppir_dep_src
);
362 list_addtail(&zero
->node
.list
, &node
->list
);
367 static bool (*ppir_lower_funcs
[ppir_op_num
])(ppir_block
*, ppir_node
*) = {
368 [ppir_op_abs
] = ppir_lower_abs
,
369 [ppir_op_neg
] = ppir_lower_neg
,
370 [ppir_op_const
] = ppir_lower_const
,
371 [ppir_op_ddx
] = ppir_lower_ddxy
,
372 [ppir_op_ddy
] = ppir_lower_ddxy
,
373 [ppir_op_lt
] = ppir_lower_swap_args
,
374 [ppir_op_le
] = ppir_lower_swap_args
,
375 [ppir_op_load_texture
] = ppir_lower_texture
,
376 [ppir_op_select
] = ppir_lower_select
,
377 [ppir_op_trunc
] = ppir_lower_trunc
,
378 [ppir_op_sat
] = ppir_lower_sat
,
379 [ppir_op_branch
] = ppir_lower_branch
,
380 [ppir_op_load_uniform
] = ppir_lower_load
,
381 [ppir_op_load_temp
] = ppir_lower_load
,
384 bool ppir_lower_prog(ppir_compiler
*comp
)
386 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
387 list_for_each_entry_safe(ppir_node
, node
, &block
->node_list
, list
) {
388 if (ppir_lower_funcs
[node
->op
] &&
389 !ppir_lower_funcs
[node
->op
](block
, node
))