lima/ppir: lower fdot in nir_opt_algebraic
[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_and] = {
149 .name = "and",
150 .slots = (int []) {
151 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_VEC_MUL,
152 PPIR_INSTR_SLOT_END
153 },
154 },
155 [ppir_op_or] = {
156 .name = "or",
157 .slots = (int []) {
158 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_VEC_MUL,
159 PPIR_INSTR_SLOT_END
160 },
161 },
162 [ppir_op_xor] = {
163 .name = "xor",
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_not] = {
170 .name = "not",
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_lt] = {
177 .name = "lt",
178 },
179 [ppir_op_le] = {
180 .name = "le",
181 },
182 [ppir_op_gt] = {
183 .name = "gt",
184 .slots = (int []) {
185 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
186 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
187 PPIR_INSTR_SLOT_END
188 },
189 },
190 [ppir_op_ge] = {
191 .name = "ge",
192 .slots = (int []) {
193 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
194 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
195 PPIR_INSTR_SLOT_END
196 },
197 },
198 [ppir_op_eq] = {
199 .name = "eq",
200 .slots = (int []) {
201 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
202 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
203 PPIR_INSTR_SLOT_END
204 },
205 },
206 [ppir_op_ne] = {
207 .name = "ne",
208 .slots = (int []) {
209 PPIR_INSTR_SLOT_ALU_SCL_MUL, PPIR_INSTR_SLOT_ALU_SCL_ADD,
210 PPIR_INSTR_SLOT_ALU_VEC_MUL, PPIR_INSTR_SLOT_ALU_VEC_ADD,
211 PPIR_INSTR_SLOT_END
212 },
213 },
214 [ppir_op_select] = {
215 .name = "select",
216 .slots = (int []) {
217 PPIR_INSTR_SLOT_ALU_SCL_ADD, PPIR_INSTR_SLOT_ALU_VEC_ADD,
218 PPIR_INSTR_SLOT_END
219 },
220 },
221 [ppir_op_rcp] = {
222 .name = "rcp",
223 .slots = (int []) {
224 PPIR_INSTR_SLOT_ALU_COMBINE, PPIR_INSTR_SLOT_END
225 },
226 },
227 [ppir_op_load_varying] = {
228 .name = "ld_var",
229 .type = ppir_node_type_load,
230 .slots = (int []) {
231 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
232 },
233 },
234 [ppir_op_load_coords] = {
235 .name = "ld_coords",
236 .type = ppir_node_type_load,
237 .slots = (int []) {
238 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
239 },
240 },
241 [ppir_op_load_fragcoord] = {
242 .name = "ld_fragcoord",
243 .type = ppir_node_type_load,
244 .slots = (int []) {
245 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
246 },
247 },
248 [ppir_op_load_pointcoord] = {
249 .name = "ld_pointcoord",
250 .type = ppir_node_type_load,
251 .slots = (int []) {
252 PPIR_INSTR_SLOT_VARYING, PPIR_INSTR_SLOT_END
253 },
254 },
255 [ppir_op_load_uniform] = {
256 .name = "ld_uni",
257 .type = ppir_node_type_load,
258 .slots = (int []) {
259 PPIR_INSTR_SLOT_UNIFORM, PPIR_INSTR_SLOT_END
260 },
261 },
262 [ppir_op_load_texture] = {
263 .name = "ld_tex",
264 .type = ppir_node_type_load_texture,
265 .slots = (int []) {
266 PPIR_INSTR_SLOT_TEXLD, PPIR_INSTR_SLOT_END
267 },
268 },
269 [ppir_op_load_temp] = {
270 .name = "ld_temp",
271 .type = ppir_node_type_load,
272 .slots = (int []) {
273 PPIR_INSTR_SLOT_UNIFORM, PPIR_INSTR_SLOT_END
274 },
275 },
276 [ppir_op_const] = {
277 .name = "const",
278 .type = ppir_node_type_const,
279 },
280 [ppir_op_store_color] = {
281 .name = "st_col",
282 .type = ppir_node_type_store,
283 },
284 [ppir_op_store_temp] = {
285 .name = "st_temp",
286 .type = ppir_node_type_store,
287 .slots = (int []) {
288 PPIR_INSTR_SLOT_STORE_TEMP, PPIR_INSTR_SLOT_END
289 },
290 },
291 [ppir_op_discard] = {
292 .name = "discard",
293 .type = ppir_node_type_discard,
294 .slots = (int []) {
295 PPIR_INSTR_SLOT_BRANCH, PPIR_INSTR_SLOT_END
296 },
297 },
298 [ppir_op_branch] = {
299 .name = "branch",
300 .type = ppir_node_type_branch,
301 .slots = (int []) {
302 PPIR_INSTR_SLOT_BRANCH, PPIR_INSTR_SLOT_END
303 },
304 },
305 };
306
307 void *ppir_node_create(ppir_block *block, ppir_op op, int index, unsigned mask)
308 {
309 ppir_compiler *comp = block->comp;
310 static const int node_size[] = {
311 [ppir_node_type_alu] = sizeof(ppir_alu_node),
312 [ppir_node_type_const] = sizeof(ppir_const_node),
313 [ppir_node_type_load] = sizeof(ppir_load_node),
314 [ppir_node_type_store] = sizeof(ppir_store_node),
315 [ppir_node_type_load_texture] = sizeof(ppir_load_texture_node),
316 [ppir_node_type_discard] = sizeof(ppir_discard_node),
317 [ppir_node_type_branch] = sizeof(ppir_branch_node),
318 };
319
320 ppir_node_type type = ppir_op_infos[op].type;
321 int size = node_size[type];
322 ppir_node *node = rzalloc_size(block, size);
323 if (!node)
324 return NULL;
325
326 list_inithead(&node->succ_list);
327 list_inithead(&node->pred_list);
328
329 if (index >= 0) {
330 if (mask) {
331 /* reg has 4 slots for each componemt write node */
332 while (mask)
333 comp->var_nodes[(index << 2) + comp->reg_base + u_bit_scan(&mask)] = node;
334 snprintf(node->name, sizeof(node->name), "reg%d", index);
335 } else {
336 comp->var_nodes[index] = node;
337 snprintf(node->name, sizeof(node->name), "ssa%d", index);
338 }
339 }
340 else
341 snprintf(node->name, sizeof(node->name), "new");
342
343 node->op = op;
344 node->type = type;
345 node->index = comp->cur_index++;
346 node->block = block;
347
348 return node;
349 }
350
351 void ppir_node_add_dep(ppir_node *succ, ppir_node *pred)
352 {
353 /* don't add dep for two nodes from different block */
354 if (succ->block != pred->block)
355 return;
356
357 /* don't add duplicated dep */
358 ppir_node_foreach_pred(succ, dep) {
359 if (dep->pred == pred)
360 return;
361 }
362
363 ppir_dep *dep = ralloc(succ, ppir_dep);
364 dep->pred = pred;
365 dep->succ = succ;
366 list_addtail(&dep->pred_link, &succ->pred_list);
367 list_addtail(&dep->succ_link, &pred->succ_list);
368 }
369
370 void ppir_node_remove_dep(ppir_dep *dep)
371 {
372 list_del(&dep->succ_link);
373 list_del(&dep->pred_link);
374 ralloc_free(dep);
375 }
376
377 static void _ppir_node_replace_child(ppir_src *src, ppir_node *old_child, ppir_node *new_child)
378 {
379 ppir_dest *od = ppir_node_get_dest(old_child);
380 if (ppir_node_target_equal(src, od)) {
381 ppir_dest *nd = ppir_node_get_dest(new_child);
382 ppir_node_target_assign(src, nd);
383 }
384 }
385
386 void ppir_node_replace_child(ppir_node *parent, ppir_node *old_child, ppir_node *new_child)
387 {
388 switch (parent->type) {
389 case ppir_node_type_alu:
390 {
391 ppir_alu_node *alu = ppir_node_to_alu(parent);
392 for (int i = 0; i < alu->num_src; i++)
393 _ppir_node_replace_child(alu->src + i, old_child, new_child);
394 break;
395 }
396 case ppir_node_type_branch:
397 {
398 ppir_branch_node *branch = ppir_node_to_branch(parent);
399 for (int i = 0; i < 2; i++)
400 _ppir_node_replace_child(branch->src + i, old_child, new_child);
401 break;
402 }
403 case ppir_node_type_load:
404 {
405 ppir_load_node *load = ppir_node_to_load(parent);
406 _ppir_node_replace_child(&load->src, old_child, new_child);
407 break;
408 }
409 case ppir_node_type_load_texture:
410 {
411 ppir_load_texture_node *load_texture = ppir_node_to_load_texture(parent);
412 _ppir_node_replace_child(&load_texture->src_coords, old_child, new_child);
413 break;
414 }
415 case ppir_node_type_store:
416 {
417 ppir_store_node *store = ppir_node_to_store(parent);
418 _ppir_node_replace_child(&store->src, old_child, new_child);
419 break;
420 }
421 default:
422 ppir_debug("unknown node type in %s\n", __func__);
423 break;
424 }
425 }
426
427 void ppir_node_replace_pred(ppir_dep *dep, ppir_node *new_pred)
428 {
429 list_del(&dep->succ_link);
430 dep->pred = new_pred;
431 list_addtail(&dep->succ_link, &new_pred->succ_list);
432 }
433
434 void ppir_node_replace_all_succ(ppir_node *dst, ppir_node *src)
435 {
436 ppir_node_foreach_succ_safe(src, dep) {
437 ppir_node_replace_pred(dep, dst);
438 ppir_node_replace_child(dep->succ, src, dst);
439 }
440 }
441
442 void ppir_node_delete(ppir_node *node)
443 {
444 ppir_node_foreach_succ_safe(node, dep)
445 ppir_node_remove_dep(dep);
446
447 ppir_node_foreach_pred_safe(node, dep)
448 ppir_node_remove_dep(dep);
449
450 list_del(&node->list);
451 ralloc_free(node);
452 }
453
454 static void ppir_node_print_node(ppir_node *node, int space)
455 {
456 for (int i = 0; i < space; i++)
457 printf(" ");
458 printf("%s%s %d %s\n", node->printed && !ppir_node_is_leaf(node) ? "+" : "",
459 ppir_op_infos[node->op].name, node->index, node->name);
460
461 if (!node->printed) {
462 ppir_node_foreach_pred(node, dep) {
463 ppir_node *pred = dep->pred;
464 ppir_node_print_node(pred, space + 2);
465 }
466
467 node->printed = true;
468 }
469 }
470
471 void ppir_node_print_prog(ppir_compiler *comp)
472 {
473 if (!(lima_debug & LIMA_DEBUG_PP))
474 return;
475
476 list_for_each_entry(ppir_block, block, &comp->block_list, list) {
477 list_for_each_entry(ppir_node, node, &block->node_list, list) {
478 node->printed = false;
479 }
480 }
481
482 printf("========prog========\n");
483 list_for_each_entry(ppir_block, block, &comp->block_list, list) {
484 printf("-------block------\n");
485 list_for_each_entry(ppir_node, node, &block->node_list, list) {
486 if (ppir_node_is_root(node))
487 ppir_node_print_node(node, 0);
488 }
489 }
490 printf("====================\n");
491 }