1a822b8903208e034c0d400838a8e974ff4fffc5
[mesa.git] / src / gallium / drivers / lima / ir / pp / node.c
1 /*
2 * Copyright (c) 2017 Lima Project
3 *
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:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
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.
22 *
23 */
24
25 #include "util/u_math.h"
26 #include "util/ralloc.h"
27 #include "util/bitscan.h"
28
29 #include "ppir.h"
30
31 const ppir_op_info ppir_op_infos[] = {
32 [ppir_op_mov] = {
33 .name = "mov",
34 .slots = (int []) {
35 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_SCL_MUL,
36 PPIR_INSTR_SLOT_ALU_VEC_ADD, PPIR_INSTR_SLOT_ALU_VEC_MUL,
37 PPIR_INSTR_SLOT_END
38 },
39 },
40 [ppir_op_abs] = {
41 .name = "abs",
42 },
43 [ppir_op_neg] = {
44 .name = "neg",
45 },
46 [ppir_op_sat] = {
47 .name = "sat",
48 },
49 [ppir_op_mul] = {
50 .name = "mul",
51 .slots = (int []) {
52 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_VEC_MUL,
53 PPIR_INSTR_SLOT_END
54 },
55 },
56 [ppir_op_add] = {
57 .name = "add",
58 .slots = (int []) {
59 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
60 PPIR_INSTR_SLOT_END
61 },
62 },
63 [ppir_op_sum3] = {
64 .name = "sum3",
65 .slots = (int []) {
66 PPIR_INSTR_SLOT_ALU_VEC_ADD, PPIR_INSTR_SLOT_END
67 },
68 },
69 [ppir_op_sum4] = {
70 .name = "sum4",
71 .slots = (int []) {
72 PPIR_INSTR_SLOT_ALU_VEC_ADD, PPIR_INSTR_SLOT_END
73 },
74 },
75 [ppir_op_rsqrt] = {
76 .name = "rsqrt",
77 .slots = (int []) {
78 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
79 },
80 },
81 [ppir_op_log2] = {
82 .name = "log2",
83 .slots = (int []) {
84 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
85 },
86 },
87 [ppir_op_exp2] = {
88 .name = "exp2",
89 .slots = (int []) {
90 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
91 },
92 },
93 [ppir_op_sqrt] = {
94 .name = "sqrt",
95 .slots = (int []) {
96 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
97 },
98 },
99 [ppir_op_sin] = {
100 .name = "sin",
101 .slots = (int []) {
102 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
103 },
104 },
105 [ppir_op_cos] = {
106 .name = "cos",
107 .slots = (int []) {
108 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
109 },
110 },
111 [ppir_op_max] = {
112 .name = "max",
113 .slots = (int []) {
114 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_SCL_MUL,
115 PPIR_INSTR_SLOT_ALU_VEC_ADD, PPIR_INSTR_SLOT_ALU_VEC_MUL,
116 PPIR_INSTR_SLOT_END
117 },
118 },
119 [ppir_op_min] = {
120 .name = "min",
121 .slots = (int []) {
122 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_SCL_MUL,
123 PPIR_INSTR_SLOT_ALU_VEC_ADD, PPIR_INSTR_SLOT_ALU_VEC_MUL,
124 PPIR_INSTR_SLOT_END
125 },
126 },
127 [ppir_op_floor] = {
128 .name = "floor",
129 .slots = (int []) {
130 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
131 PPIR_INSTR_SLOT_END
132 },
133 },
134 [ppir_op_ceil] = {
135 .name = "ceil",
136 .slots = (int []) {
137 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
138 PPIR_INSTR_SLOT_END
139 },
140 },
141 [ppir_op_fract] = {
142 .name = "fract",
143 .slots = (int []) {
144 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
145 PPIR_INSTR_SLOT_END
146 },
147 },
148 [ppir_op_ddx] = {
149 .name = "ddx",
150 .slots = (int []) {
151 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
152 PPIR_INSTR_SLOT_END
153 },
154 },
155 [ppir_op_ddy] = {
156 .name = "ddy",
157 .slots = (int []) {
158 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
159 PPIR_INSTR_SLOT_END
160 },
161 },
162 [ppir_op_and] = {
163 .name = "and",
164 .slots = (int []) {
165 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_VEC_MUL,
166 PPIR_INSTR_SLOT_END
167 },
168 },
169 [ppir_op_or] = {
170 .name = "or",
171 .slots = (int []) {
172 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_VEC_MUL,
173 PPIR_INSTR_SLOT_END
174 },
175 },
176 [ppir_op_xor] = {
177 .name = "xor",
178 .slots = (int []) {
179 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_VEC_MUL,
180 PPIR_INSTR_SLOT_END
181 },
182 },
183 [ppir_op_not] = {
184 .name = "not",
185 .slots = (int []) {
186 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_VEC_MUL,
187 PPIR_INSTR_SLOT_END
188 },
189 },
190 [ppir_op_lt] = {
191 .name = "lt",
192 },
193 [ppir_op_le] = {
194 .name = "le",
195 },
196 [ppir_op_gt] = {
197 .name = "gt",
198 .slots = (int []) {
199 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
200 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
201 PPIR_INSTR_SLOT_END
202 },
203 },
204 [ppir_op_ge] = {
205 .name = "ge",
206 .slots = (int []) {
207 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
208 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
209 PPIR_INSTR_SLOT_END
210 },
211 },
212 [ppir_op_eq] = {
213 .name = "eq",
214 .slots = (int []) {
215 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
216 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
217 PPIR_INSTR_SLOT_END
218 },
219 },
220 [ppir_op_ne] = {
221 .name = "ne",
222 .slots = (int []) {
223 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
224 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
225 PPIR_INSTR_SLOT_END
226 },
227 },
228 [ppir_op_sel_cond] = {
229 /* effectively mov, but must be scheduled only to
230 * PPIR_INSTR_SLOT_ALU_SCL_MUL */
231 .name = "sel_cond",
232 .slots = (int []) {
233 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_END
234 },
235 },
236 [ppir_op_select] = {
237 .name = "select",
238 .slots = (int []) {
239 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
240 PPIR_INSTR_SLOT_END
241 },
242 },
243 [ppir_op_rcp] = {
244 .name = "rcp",
245 .slots = (int []) {
246 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
247 },
248 },
249 [ppir_op_load_varying] = {
250 .name = "ld_var",
251 .type = ppir_node_type_load,
252 .slots = (int []) {
253 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
254 },
255 },
256 [ppir_op_load_coords] = {
257 .name = "ld_coords",
258 .type = ppir_node_type_load,
259 .slots = (int []) {
260 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
261 },
262 },
263 [ppir_op_load_coords_reg] = {
264 .name = "ld_coords_reg",
265 .type = ppir_node_type_load,
266 .slots = (int []) {
267 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
268 },
269 },
270 [ppir_op_load_fragcoord] = {
271 .name = "ld_fragcoord",
272 .type = ppir_node_type_load,
273 .slots = (int []) {
274 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
275 },
276 },
277 [ppir_op_load_pointcoord] = {
278 .name = "ld_pointcoord",
279 .type = ppir_node_type_load,
280 .slots = (int []) {
281 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
282 },
283 },
284 [ppir_op_load_frontface] = {
285 .name = "ld_frontface",
286 .type = ppir_node_type_load,
287 .slots = (int []) {
288 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
289 },
290 },
291 [ppir_op_load_uniform] = {
292 .name = "ld_uni",
293 .type = ppir_node_type_load,
294 .slots = (int []) {
295 PPIR_INSTR_SLOT_UNIFORM, PPIR_INSTR_SLOT_END
296 },
297 },
298 [ppir_op_load_texture] = {
299 .name = "ld_tex",
300 .type = ppir_node_type_load_texture,
301 .slots = (int []) {
302 PPIR_INSTR_SLOT_TEXLD, PPIR_INSTR_SLOT_END
303 },
304 },
305 [ppir_op_load_temp] = {
306 .name = "ld_temp",
307 .type = ppir_node_type_load,
308 .slots = (int []) {
309 PPIR_INSTR_SLOT_UNIFORM, PPIR_INSTR_SLOT_END
310 },
311 },
312 [ppir_op_const] = {
313 .name = "const",
314 .type = ppir_node_type_const,
315 },
316 [ppir_op_store_color] = {
317 .name = "st_col",
318 .type = ppir_node_type_alu,
319 .slots = (int []) {
320 PPIR_INSTR_SLOT_ALU_VEC_ADD, PPIR_INSTR_SLOT_ALU_VEC_MUL,
321 PPIR_INSTR_SLOT_END
322 },
323 },
324 [ppir_op_store_temp] = {
325 .name = "st_temp",
326 .type = ppir_node_type_store,
327 .slots = (int []) {
328 PPIR_INSTR_SLOT_STORE_TEMP, PPIR_INSTR_SLOT_END
329 },
330 },
331 [ppir_op_discard] = {
332 .name = "discard",
333 .type = ppir_node_type_discard,
334 .slots = (int []) {
335 PPIR_INSTR_SLOT_BRANCH, PPIR_INSTR_SLOT_END
336 },
337 },
338 [ppir_op_branch] = {
339 .name = "branch",
340 .type = ppir_node_type_branch,
341 .slots = (int []) {
342 PPIR_INSTR_SLOT_BRANCH, PPIR_INSTR_SLOT_END
343 },
344 },
345 [ppir_op_undef] = {
346 .name = "undef",
347 .type = ppir_node_type_alu,
348 .slots = (int []) {
349 },
350 },
351 [ppir_op_dummy] = {
352 .name = "dummy",
353 .type = ppir_node_type_alu,
354 .slots = (int []) {
355 },
356 },
357 };
358
359 void *ppir_node_create(ppir_block *block, ppir_op op, int index, unsigned mask)
360 {
361 ppir_compiler *comp = block->comp;
362 static const int node_size[] = {
363 [ppir_node_type_alu] = sizeof(ppir_alu_node),
364 [ppir_node_type_const] = sizeof(ppir_const_node),
365 [ppir_node_type_load] = sizeof(ppir_load_node),
366 [ppir_node_type_store] = sizeof(ppir_store_node),
367 [ppir_node_type_load_texture] = sizeof(ppir_load_texture_node),
368 [ppir_node_type_discard] = sizeof(ppir_discard_node),
369 [ppir_node_type_branch] = sizeof(ppir_branch_node),
370 };
371
372 ppir_node_type type = ppir_op_infos[op].type;
373 int size = node_size[type];
374 ppir_node *node = rzalloc_size(block, size);
375 if (!node)
376 return NULL;
377
378 list_inithead(&node->succ_list);
379 list_inithead(&node->pred_list);
380
381 if (index >= 0) {
382 if (mask) {
383 /* reg has 4 slots for each componemt write node */
384 while (mask)
385 comp->var_nodes[(index << 2) + comp->reg_base + u_bit_scan(&mask)] = node;
386 snprintf(node->name, sizeof(node->name), "reg%d", index);
387 } else {
388 comp->var_nodes[index] = node;
389 snprintf(node->name, sizeof(node->name), "ssa%d", index);
390 }
391 }
392 else
393 snprintf(node->name, sizeof(node->name), "new");
394
395 node->op = op;
396 node->type = type;
397 node->index = comp->cur_index++;
398 node->block = block;
399
400 return node;
401 }
402
403 void ppir_node_add_dep(ppir_node *succ, ppir_node *pred,
404 ppir_dep_type type)
405 {
406 /* don't add dep for two nodes from different block */
407 if (succ->block != pred->block)
408 return;
409
410 /* don't add duplicated dep */
411 ppir_node_foreach_pred(succ, dep) {
412 if (dep->pred == pred)
413 return;
414 }
415
416 ppir_dep *dep = ralloc(succ, ppir_dep);
417 dep->pred = pred;
418 dep->succ = succ;
419 dep->type = type;
420 list_addtail(&dep->pred_link, &succ->pred_list);
421 list_addtail(&dep->succ_link, &pred->succ_list);
422 }
423
424 void ppir_node_remove_dep(ppir_dep *dep)
425 {
426 list_del(&dep->succ_link);
427 list_del(&dep->pred_link);
428 ralloc_free(dep);
429 }
430
431 static void _ppir_node_replace_child(ppir_src *src, ppir_node *old_child, ppir_node *new_child)
432 {
433 ppir_dest *od = ppir_node_get_dest(old_child);
434 if (ppir_node_target_equal(src, od)) {
435 ppir_node_target_assign(src, new_child);
436 }
437 }
438
439 void ppir_node_replace_child(ppir_node *parent, ppir_node *old_child, ppir_node *new_child)
440 {
441 switch (parent->type) {
442 case ppir_node_type_alu:
443 {
444 ppir_alu_node *alu = ppir_node_to_alu(parent);
445 for (int i = 0; i < alu->num_src; i++)
446 _ppir_node_replace_child(alu->src + i, old_child, new_child);
447 break;
448 }
449 case ppir_node_type_branch:
450 {
451 ppir_branch_node *branch = ppir_node_to_branch(parent);
452 for (int i = 0; i < 2; i++)
453 _ppir_node_replace_child(branch->src + i, old_child, new_child);
454 break;
455 }
456 case ppir_node_type_load:
457 {
458 ppir_load_node *load = ppir_node_to_load(parent);
459 _ppir_node_replace_child(&load->src, old_child, new_child);
460 break;
461 }
462 case ppir_node_type_load_texture:
463 {
464 ppir_load_texture_node *load_texture = ppir_node_to_load_texture(parent);
465 for (int i = 0; i < load_texture->num_src; i++)
466 _ppir_node_replace_child(ppir_node_get_src(parent, i), old_child, new_child);
467 break;
468 }
469 case ppir_node_type_store:
470 {
471 ppir_store_node *store = ppir_node_to_store(parent);
472 _ppir_node_replace_child(&store->src, old_child, new_child);
473 break;
474 }
475 default:
476 ppir_debug("unknown node type in %s\n", __func__);
477 break;
478 }
479 }
480
481 void ppir_node_replace_pred(ppir_dep *dep, ppir_node *new_pred)
482 {
483 list_del(&dep->succ_link);
484 dep->pred = new_pred;
485 list_addtail(&dep->succ_link, &new_pred->succ_list);
486 }
487
488 ppir_dep *ppir_dep_for_pred(ppir_node *node, ppir_node *pred)
489 {
490 if (!pred)
491 return NULL;
492
493 if (node->block != pred->block)
494 return NULL;
495
496 ppir_node_foreach_pred(node, dep) {
497 if (dep->pred == pred)
498 return dep;
499 }
500 return NULL;
501 }
502
503 void ppir_node_replace_all_succ(ppir_node *dst, ppir_node *src)
504 {
505 ppir_node_foreach_succ_safe(src, dep) {
506 ppir_node_replace_pred(dep, dst);
507 ppir_node_replace_child(dep->succ, src, dst);
508 }
509 }
510
511 void ppir_node_delete(ppir_node *node)
512 {
513 ppir_node_foreach_succ_safe(node, dep)
514 ppir_node_remove_dep(dep);
515
516 ppir_node_foreach_pred_safe(node, dep)
517 ppir_node_remove_dep(dep);
518
519 list_del(&node->list);
520 ralloc_free(node);
521 }
522
523 static void ppir_node_print_dest(ppir_dest *dest)
524 {
525 switch (dest->type) {
526 case ppir_target_ssa:
527 printf("ssa%d", dest->ssa.index);
528 break;
529 case ppir_target_pipeline:
530 printf("pipeline %d", dest->pipeline);
531 break;
532 case ppir_target_register:
533 printf("reg %d", dest->reg->index);
534 break;
535 }
536 }
537
538 static void ppir_node_print_src(ppir_src *src)
539 {
540 switch (src->type) {
541 case ppir_target_ssa: {
542 if (src->node)
543 printf("ssa node %d", src->node->index);
544 else
545 printf("ssa idx %d", src->ssa ? src->ssa->index : -1);
546 break;
547 }
548 case ppir_target_pipeline:
549 if (src->node)
550 printf("pipeline %d node %d", src->pipeline, src->node->index);
551 else
552 printf("pipeline %d", src->pipeline);
553 break;
554 case ppir_target_register:
555 printf("reg %d", src->reg->index);
556 break;
557 }
558 }
559
560 static void ppir_node_print_node(ppir_node *node, int space)
561 {
562 for (int i = 0; i < space; i++)
563 printf(" ");
564
565 printf("%s%d: %s %s: ", node->printed && !ppir_node_is_leaf(node) ? "+" : "",
566 node->index, ppir_op_infos[node->op].name, node->name);
567
568 ppir_dest *dest = ppir_node_get_dest(node);
569 if (dest) {
570 printf("dest: ");
571 ppir_node_print_dest(dest);
572 }
573
574 if (ppir_node_get_src_num(node) > 0) {
575 printf(" src: ");
576 }
577 for (int i = 0; i < ppir_node_get_src_num(node); i++) {
578 ppir_node_print_src(ppir_node_get_src(node, i));
579 if (i != (ppir_node_get_src_num(node) - 1))
580 printf(", ");
581 }
582 printf("\n");
583
584 if (!node->printed) {
585 ppir_node_foreach_pred(node, dep) {
586 ppir_node *pred = dep->pred;
587 ppir_node_print_node(pred, space + 2);
588 }
589
590 node->printed = true;
591 }
592 }
593
594 void ppir_node_print_prog(ppir_compiler *comp)
595 {
596 if (!(lima_debug & LIMA_DEBUG_PP))
597 return;
598
599 list_for_each_entry(ppir_block, block, &comp->block_list, list) {
600 list_for_each_entry(ppir_node, node, &block->node_list, list) {
601 node->printed = false;
602 }
603 }
604
605 printf("========prog========\n");
606 list_for_each_entry(ppir_block, block, &comp->block_list, list) {
607 printf("-------block %3d-------\n", block->index);
608 list_for_each_entry(ppir_node, node, &block->node_list, list) {
609 if (ppir_node_is_root(node))
610 ppir_node_print_node(node, 0);
611 }
612 }
613 printf("====================\n");
614 }
615
616 static ppir_node *ppir_node_clone_const(ppir_block *block, ppir_node *node)
617 {
618 ppir_const_node *cnode = ppir_node_to_const(node);
619 ppir_const_node *new_cnode = ppir_node_create(block, ppir_op_const, -1, 0);
620
621 if (!new_cnode)
622 return NULL;
623
624 list_addtail(&new_cnode->node.list, &block->node_list);
625
626 new_cnode->constant.num = cnode->constant.num;
627 for (int i = 0; i < cnode->constant.num; i++) {
628 new_cnode->constant.value[i] = cnode->constant.value[i];
629 }
630 new_cnode->dest.type = ppir_target_ssa;
631 new_cnode->dest.ssa.num_components = cnode->dest.ssa.num_components;
632 new_cnode->dest.write_mask = cnode->dest.write_mask;
633
634 return &new_cnode->node;
635 }
636
637 static ppir_node *
638 ppir_node_clone_load(ppir_block *block, ppir_node *node)
639 {
640 ppir_load_node *load_node = ppir_node_to_load(node);
641 ppir_load_node *new_lnode = ppir_node_create(block, node->op, -1, 0);
642
643 if (!new_lnode)
644 return NULL;
645
646 list_addtail(&new_lnode->node.list, &block->node_list);
647
648 new_lnode->num_components = load_node->num_components;
649 new_lnode->index = load_node->index;
650
651 ppir_dest *dest = ppir_node_get_dest(node);
652 new_lnode->dest = *dest;
653
654 ppir_src *src = ppir_node_get_src(node, 0);
655 if (src) {
656 new_lnode->num_src = 1;
657 switch (src->type) {
658 case ppir_target_ssa:
659 ppir_node_target_assign(&new_lnode->src, src->node);
660 ppir_node_add_dep(&new_lnode->node, src->node, ppir_dep_src);
661 break;
662 case ppir_target_register:
663 new_lnode->src.type = src->type;
664 new_lnode->src.reg = src->reg;
665 new_lnode->src.node = NULL;
666 break;
667 default:
668 /* Load nodes can't consume pipeline registers */
669 assert(0);
670 }
671 }
672
673 return &new_lnode->node;
674 }
675
676 void
677 ppir_delete_if_orphan(ppir_block *block, ppir_node *node)
678 {
679 ppir_dest *dest = ppir_node_get_dest(node);
680 if (!dest)
681 return;
682
683 ppir_node_foreach_succ_safe(node, dep) {
684 ppir_node *succ = dep->succ;
685 for (int i = 0; i < ppir_node_get_src_num(succ); i++) {
686 ppir_src *src = ppir_node_get_src(succ, i);
687 if (!src)
688 continue;
689 if (ppir_node_target_equal(src, dest))
690 return;
691 }
692 }
693
694 ppir_node_delete(node);
695 }
696
697 ppir_node *ppir_node_clone(ppir_block *block, ppir_node *node)
698 {
699 switch (node->op) {
700 case ppir_op_const:
701 return ppir_node_clone_const(block, node);
702 case ppir_op_load_uniform:
703 case ppir_op_load_varying:
704 case ppir_op_load_temp:
705 case ppir_op_load_coords:
706 case ppir_op_load_coords_reg:
707 return ppir_node_clone_load(block, node);
708 default:
709 return NULL;
710 }
711 }
712
713 ppir_node *ppir_node_insert_mov(ppir_node *node)
714 {
715 ppir_node *move = ppir_node_create(node->block, ppir_op_mov, -1, 0);
716 if (unlikely(!move))
717 return NULL;
718
719 ppir_dest *dest = ppir_node_get_dest(node);
720 ppir_alu_node *alu = ppir_node_to_alu(move);
721 alu->dest = *dest;
722 alu->num_src = 1;
723 ppir_node_target_assign(alu->src, node);
724
725 for (int s = 0; s < 4; s++)
726 alu->src->swizzle[s] = s;
727
728 ppir_node_replace_all_succ(move, node);
729 ppir_node_add_dep(move, node, ppir_dep_src);
730 list_addtail(&move->list, &node->list);
731
732 return move;
733 }
734
735 ppir_node *ppir_node_insert_mov_all_blocks(ppir_node *old)
736 {
737 ppir_node *move = ppir_node_insert_mov(old);
738 ppir_compiler *comp = old->block->comp;
739
740 list_for_each_entry(ppir_block, block, &comp->block_list, list) {
741 if (old->block == block)
742 continue;
743 list_for_each_entry_safe(ppir_node, node, &block->node_list, list) {
744 for (int i = 0; i < ppir_node_get_src_num(node); i++){
745 ppir_src *src = ppir_node_get_src(node, i);
746 if (!src)
747 continue;
748 if (src->node == old)
749 ppir_node_target_assign(src, move);
750 }
751 }
752 }
753
754 return move;
755 }
756 bool ppir_node_has_single_src_succ(ppir_node *node)
757 {
758 if (list_is_singular(&node->succ_list) &&
759 list_first_entry(&node->succ_list,
760 ppir_dep, succ_link)->type == ppir_dep_src)
761 return true;
762
763 int cnt = 0;
764 ppir_node_foreach_succ(node, dep) {
765 if (dep->type != ppir_dep_src)
766 continue;
767 cnt++;
768 }
769
770 return cnt == 1;
771 }