freedreno/a4xx: format updates
[mesa.git] / src / gallium / drivers / vc4 / vc4_qir.c
1 /*
2 * Copyright © 2014 Broadcom
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, sublicense,
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 next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 NONINFRINGEMENT. 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "util/u_memory.h"
25 #include "util/ralloc.h"
26
27 #include "vc4_qir.h"
28 #include "vc4_qpu.h"
29
30 struct qir_op_info {
31 const char *name;
32 uint8_t ndst, nsrc;
33 bool has_side_effects;
34 bool multi_instruction;
35 };
36
37 static const struct qir_op_info qir_op_info[] = {
38 [QOP_MOV] = { "mov", 1, 1 },
39 [QOP_FADD] = { "fadd", 1, 2 },
40 [QOP_FSUB] = { "fsub", 1, 2 },
41 [QOP_FMUL] = { "fmul", 1, 2 },
42 [QOP_MUL24] = { "mul24", 1, 2 },
43 [QOP_FMIN] = { "fmin", 1, 2 },
44 [QOP_FMAX] = { "fmax", 1, 2 },
45 [QOP_FMINABS] = { "fminabs", 1, 2 },
46 [QOP_FMAXABS] = { "fmaxabs", 1, 2 },
47 [QOP_FTOI] = { "ftoi", 1, 1 },
48 [QOP_ITOF] = { "itof", 1, 1 },
49 [QOP_ADD] = { "add", 1, 2 },
50 [QOP_SUB] = { "sub", 1, 2 },
51 [QOP_SHR] = { "shr", 1, 2 },
52 [QOP_ASR] = { "asr", 1, 2 },
53 [QOP_SHL] = { "shl", 1, 2 },
54 [QOP_MIN] = { "min", 1, 2 },
55 [QOP_MAX] = { "max", 1, 2 },
56 [QOP_AND] = { "and", 1, 2 },
57 [QOP_OR] = { "or", 1, 2 },
58 [QOP_XOR] = { "xor", 1, 2 },
59 [QOP_NOT] = { "not", 1, 1 },
60
61 [QOP_SEL_X_0_NS] = { "fsel_x_0_ns", 1, 1, false, true },
62 [QOP_SEL_X_0_NC] = { "fsel_x_0_nc", 1, 1, false, true },
63 [QOP_SEL_X_0_ZS] = { "fsel_x_0_zs", 1, 1, false, true },
64 [QOP_SEL_X_0_ZC] = { "fsel_x_0_zc", 1, 1, false, true },
65 [QOP_SEL_X_Y_NS] = { "fsel_x_y_ns", 1, 2, false, true },
66 [QOP_SEL_X_Y_NC] = { "fsel_x_y_nc", 1, 2, false, true },
67 [QOP_SEL_X_Y_ZS] = { "fsel_x_y_zs", 1, 2, false, true },
68 [QOP_SEL_X_Y_ZC] = { "fsel_x_y_zc", 1, 2, false, true },
69
70 [QOP_RCP] = { "rcp", 1, 1, false, true },
71 [QOP_RSQ] = { "rsq", 1, 1, false, true },
72 [QOP_EXP2] = { "exp2", 1, 2, false, true },
73 [QOP_LOG2] = { "log2", 1, 2, false, true },
74 [QOP_PACK_8888_F] = { "pack_8888_f", 1, 1, false, true },
75 [QOP_PACK_8A_F] = { "pack_8a_f", 1, 2, false, true },
76 [QOP_PACK_8B_F] = { "pack_8b_f", 1, 2, false, true },
77 [QOP_PACK_8C_F] = { "pack_8c_f", 1, 2, false, true },
78 [QOP_PACK_8D_F] = { "pack_8d_f", 1, 2, false, true },
79 [QOP_PACK_SCALED] = { "pack_scaled", 1, 2, false, true },
80 [QOP_TLB_DISCARD_SETUP] = { "discard", 0, 1, true },
81 [QOP_TLB_STENCIL_SETUP] = { "tlb_stencil_setup", 0, 1, true },
82 [QOP_TLB_Z_WRITE] = { "tlb_z", 0, 1, true },
83 [QOP_TLB_COLOR_WRITE] = { "tlb_color", 0, 1, true },
84 [QOP_TLB_COLOR_READ] = { "tlb_color_read", 1, 0 },
85 [QOP_VARY_ADD_C] = { "vary_add_c", 1, 1 },
86
87 [QOP_FRAG_X] = { "frag_x", 1, 0 },
88 [QOP_FRAG_Y] = { "frag_y", 1, 0 },
89 [QOP_FRAG_Z] = { "frag_z", 1, 0 },
90 [QOP_FRAG_W] = { "frag_w", 1, 0 },
91 [QOP_FRAG_REV_FLAG] = { "frag_rev_flag", 1, 0 },
92
93 [QOP_TEX_S] = { "tex_s", 0, 2 },
94 [QOP_TEX_T] = { "tex_t", 0, 2 },
95 [QOP_TEX_R] = { "tex_r", 0, 2 },
96 [QOP_TEX_B] = { "tex_b", 0, 2 },
97 [QOP_TEX_DIRECT] = { "tex_direct", 0, 2 },
98 [QOP_TEX_RESULT] = { "tex_result", 1, 0, true },
99 [QOP_UNPACK_8A_F] = { "unpack_8a_f", 1, 1 },
100 [QOP_UNPACK_8B_F] = { "unpack_8b_f", 1, 1 },
101 [QOP_UNPACK_8C_F] = { "unpack_8c_f", 1, 1 },
102 [QOP_UNPACK_8D_F] = { "unpack_8d_f", 1, 1 },
103 [QOP_UNPACK_16A_F] = { "unpack_16a_f", 1, 1 },
104 [QOP_UNPACK_16B_F] = { "unpack_16b_f", 1, 1 },
105 [QOP_UNPACK_8A_I] = { "unpack_8a_i", 1, 1 },
106 [QOP_UNPACK_8B_I] = { "unpack_8b_i", 1, 1 },
107 [QOP_UNPACK_8C_I] = { "unpack_8c_i", 1, 1 },
108 [QOP_UNPACK_8D_I] = { "unpack_8d_i", 1, 1 },
109 [QOP_UNPACK_16A_I] = { "unpack_16a_i", 1, 1 },
110 [QOP_UNPACK_16B_I] = { "unpack_16b_i", 1, 1 },
111 };
112
113 static const char *
114 qir_get_op_name(enum qop qop)
115 {
116 if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
117 return qir_op_info[qop].name;
118 else
119 return "???";
120 }
121
122 int
123 qir_get_op_nsrc(enum qop qop)
124 {
125 if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
126 return qir_op_info[qop].nsrc;
127 else
128 abort();
129 }
130
131 /**
132 * Returns whether the instruction has any side effects that must be
133 * preserved.
134 */
135 bool
136 qir_has_side_effects(struct vc4_compile *c, struct qinst *inst)
137 {
138 return qir_op_info[inst->op].has_side_effects;
139 }
140
141 bool
142 qir_has_side_effect_reads(struct vc4_compile *c, struct qinst *inst)
143 {
144 /* We can dead-code eliminate varyings, because we only tell the VS
145 * about the live ones at the end. But we have to preserve the
146 * point/line coordinates reads, because they're generated by
147 * fixed-function hardware.
148 */
149 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
150 if (inst->src[i].file == QFILE_VARY &&
151 c->input_semantics[inst->src[i].index].semantic == 0xff) {
152 return true;
153 }
154
155 if (inst->src[i].file == QFILE_VPM)
156 return true;
157 }
158
159 if (inst->dst.file == QFILE_VPM)
160 return true;
161
162 return false;
163 }
164
165 bool
166 qir_is_multi_instruction(struct qinst *inst)
167 {
168 return qir_op_info[inst->op].multi_instruction;
169 }
170
171 bool
172 qir_is_tex(struct qinst *inst)
173 {
174 return inst->op >= QOP_TEX_S && inst->op <= QOP_TEX_DIRECT;
175 }
176
177 bool
178 qir_depends_on_flags(struct qinst *inst)
179 {
180 switch (inst->op) {
181 case QOP_SEL_X_0_NS:
182 case QOP_SEL_X_0_NC:
183 case QOP_SEL_X_0_ZS:
184 case QOP_SEL_X_0_ZC:
185 case QOP_SEL_X_Y_NS:
186 case QOP_SEL_X_Y_NC:
187 case QOP_SEL_X_Y_ZS:
188 case QOP_SEL_X_Y_ZC:
189 return true;
190 default:
191 return false;
192 }
193 }
194
195 bool
196 qir_src_needs_a_file(struct qinst *inst)
197 {
198 switch (inst->op) {
199 case QOP_UNPACK_8A_F:
200 case QOP_UNPACK_8B_F:
201 case QOP_UNPACK_8C_F:
202 case QOP_UNPACK_8D_F:
203 case QOP_UNPACK_16A_F:
204 case QOP_UNPACK_16B_F:
205 case QOP_UNPACK_8A_I:
206 case QOP_UNPACK_8B_I:
207 case QOP_UNPACK_8C_I:
208 case QOP_UNPACK_8D_I:
209 case QOP_UNPACK_16A_I:
210 case QOP_UNPACK_16B_I:
211 return true;
212 default:
213 return false;
214 }
215 }
216
217 bool
218 qir_writes_r4(struct qinst *inst)
219 {
220 switch (inst->op) {
221 case QOP_TEX_RESULT:
222 case QOP_TLB_COLOR_READ:
223 case QOP_RCP:
224 case QOP_RSQ:
225 case QOP_EXP2:
226 case QOP_LOG2:
227 return true;
228 default:
229 return false;
230 }
231 }
232
233 static void
234 qir_print_reg(struct vc4_compile *c, struct qreg reg, bool write)
235 {
236 static const char *files[] = {
237 [QFILE_TEMP] = "t",
238 [QFILE_VARY] = "v",
239 [QFILE_UNIF] = "u",
240 };
241
242 if (reg.file == QFILE_NULL) {
243 fprintf(stderr, "null");
244 } else if (reg.file == QFILE_SMALL_IMM) {
245 if ((int)reg.index >= -16 && (int)reg.index <= 15)
246 fprintf(stderr, "%d", reg.index);
247 else
248 fprintf(stderr, "%f", uif(reg.index));
249 } else if (reg.file == QFILE_VPM) {
250 if (write) {
251 fprintf(stderr, "vpm");
252 } else {
253 fprintf(stderr, "vpm%d.%d",
254 reg.index / 4, reg.index % 4);
255 }
256 } else {
257 fprintf(stderr, "%s%d", files[reg.file], reg.index);
258 }
259
260 if (reg.file == QFILE_UNIF &&
261 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT) {
262 fprintf(stderr, " (0x%08x / %f)",
263 c->uniform_data[reg.index],
264 uif(c->uniform_data[reg.index]));
265 }
266 }
267
268 void
269 qir_dump_inst(struct vc4_compile *c, struct qinst *inst)
270 {
271 fprintf(stderr, "%s%s ",
272 qir_get_op_name(inst->op),
273 inst->sf ? ".sf" : "");
274
275 qir_print_reg(c, inst->dst, true);
276 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
277 fprintf(stderr, ", ");
278 qir_print_reg(c, inst->src[i], false);
279 }
280 }
281
282 void
283 qir_dump(struct vc4_compile *c)
284 {
285 list_for_each_entry(struct qinst, inst, &c->instructions, link) {
286 qir_dump_inst(c, inst);
287 fprintf(stderr, "\n");
288 }
289 }
290
291 struct qreg
292 qir_get_temp(struct vc4_compile *c)
293 {
294 struct qreg reg;
295
296 reg.file = QFILE_TEMP;
297 reg.index = c->num_temps++;
298
299 if (c->num_temps > c->defs_array_size) {
300 uint32_t old_size = c->defs_array_size;
301 c->defs_array_size = MAX2(old_size * 2, 16);
302 c->defs = reralloc(c, c->defs, struct qinst *,
303 c->defs_array_size);
304 memset(&c->defs[old_size], 0,
305 sizeof(c->defs[0]) * (c->defs_array_size - old_size));
306 }
307
308 return reg;
309 }
310
311 struct qinst *
312 qir_inst(enum qop op, struct qreg dst, struct qreg src0, struct qreg src1)
313 {
314 struct qinst *inst = CALLOC_STRUCT(qinst);
315
316 inst->op = op;
317 inst->dst = dst;
318 inst->src = calloc(2, sizeof(inst->src[0]));
319 inst->src[0] = src0;
320 inst->src[1] = src1;
321
322 return inst;
323 }
324
325 struct qinst *
326 qir_inst4(enum qop op, struct qreg dst,
327 struct qreg a,
328 struct qreg b,
329 struct qreg c,
330 struct qreg d)
331 {
332 struct qinst *inst = CALLOC_STRUCT(qinst);
333
334 inst->op = op;
335 inst->dst = dst;
336 inst->src = calloc(4, sizeof(*inst->src));
337 inst->src[0] = a;
338 inst->src[1] = b;
339 inst->src[2] = c;
340 inst->src[3] = d;
341
342 return inst;
343 }
344
345 void
346 qir_emit(struct vc4_compile *c, struct qinst *inst)
347 {
348 if (inst->dst.file == QFILE_TEMP)
349 c->defs[inst->dst.index] = inst;
350
351 list_addtail(&inst->link, &c->instructions);
352 }
353
354 bool
355 qir_reg_equals(struct qreg a, struct qreg b)
356 {
357 return a.file == b.file && a.index == b.index;
358 }
359
360 struct vc4_compile *
361 qir_compile_init(void)
362 {
363 struct vc4_compile *c = rzalloc(NULL, struct vc4_compile);
364
365 list_inithead(&c->instructions);
366
367 c->output_position_index = -1;
368 c->output_clipvertex_index = -1;
369 c->output_color_index = -1;
370 c->output_point_size_index = -1;
371
372 c->def_ht = _mesa_hash_table_create(c, _mesa_hash_pointer,
373 _mesa_key_pointer_equal);
374
375 return c;
376 }
377
378 void
379 qir_remove_instruction(struct vc4_compile *c, struct qinst *qinst)
380 {
381 if (qinst->dst.file == QFILE_TEMP)
382 c->defs[qinst->dst.index] = NULL;
383
384 list_del(&qinst->link);
385 free(qinst->src);
386 free(qinst);
387 }
388
389 struct qreg
390 qir_follow_movs(struct vc4_compile *c, struct qreg reg)
391 {
392 while (reg.file == QFILE_TEMP && c->defs[reg.index]->op == QOP_MOV)
393 reg = c->defs[reg.index]->src[0];
394
395 return reg;
396 }
397
398 void
399 qir_compile_destroy(struct vc4_compile *c)
400 {
401 while (!list_empty(&c->instructions)) {
402 struct qinst *qinst =
403 (struct qinst *)c->instructions.next;
404 qir_remove_instruction(c, qinst);
405 }
406
407 ralloc_free(c);
408 }
409
410 const char *
411 qir_get_stage_name(enum qstage stage)
412 {
413 static const char *names[] = {
414 [QSTAGE_FRAG] = "FS",
415 [QSTAGE_VERT] = "VS",
416 [QSTAGE_COORD] = "CS",
417 };
418
419 return names[stage];
420 }
421
422 struct qreg
423 qir_uniform(struct vc4_compile *c,
424 enum quniform_contents contents,
425 uint32_t data)
426 {
427 for (int i = 0; i < c->num_uniforms; i++) {
428 if (c->uniform_contents[i] == contents &&
429 c->uniform_data[i] == data) {
430 return (struct qreg) { QFILE_UNIF, i };
431 }
432 }
433
434 uint32_t uniform = c->num_uniforms++;
435 struct qreg u = { QFILE_UNIF, uniform };
436
437 if (uniform >= c->uniform_array_size) {
438 c->uniform_array_size = MAX2(MAX2(16, uniform + 1),
439 c->uniform_array_size * 2);
440
441 c->uniform_data = reralloc(c, c->uniform_data,
442 uint32_t,
443 c->uniform_array_size);
444 c->uniform_contents = reralloc(c, c->uniform_contents,
445 enum quniform_contents,
446 c->uniform_array_size);
447 }
448
449 c->uniform_contents[uniform] = contents;
450 c->uniform_data[uniform] = data;
451
452 return u;
453 }
454
455 void
456 qir_SF(struct vc4_compile *c, struct qreg src)
457 {
458 struct qinst *last_inst = NULL;
459 if (!list_empty(&c->instructions))
460 last_inst = (struct qinst *)c->instructions.prev;
461
462 if (!last_inst ||
463 last_inst->dst.file != src.file ||
464 last_inst->dst.index != src.index ||
465 qir_is_multi_instruction(last_inst)) {
466 src = qir_MOV(c, src);
467 last_inst = (struct qinst *)c->instructions.prev;
468 }
469 last_inst->sf = true;
470 }
471
472 #define OPTPASS(func) \
473 do { \
474 bool stage_progress = func(c); \
475 if (stage_progress) { \
476 progress = true; \
477 if (print_opt_debug) { \
478 fprintf(stderr, \
479 "QIR opt pass %2d: %s progress\n", \
480 pass, #func); \
481 } \
482 } \
483 } while (0)
484
485 void
486 qir_optimize(struct vc4_compile *c)
487 {
488 bool print_opt_debug = false;
489 int pass = 1;
490
491 while (true) {
492 bool progress = false;
493
494 OPTPASS(qir_opt_algebraic);
495 OPTPASS(qir_opt_cse);
496 OPTPASS(qir_opt_constant_folding);
497 OPTPASS(qir_opt_copy_propagation);
498 OPTPASS(qir_opt_dead_code);
499 OPTPASS(qir_opt_small_immediates);
500 OPTPASS(qir_opt_vpm_writes);
501
502 if (!progress)
503 break;
504
505 pass++;
506 }
507 }