1c96ef4795f987f6128faf4177fbce30f1289921
[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_R4_UNPACK_A] = { "r4_unpack_a", 1, 1 },
100 [QOP_R4_UNPACK_B] = { "r4_unpack_b", 1, 1 },
101 [QOP_R4_UNPACK_C] = { "r4_unpack_c", 1, 1 },
102 [QOP_R4_UNPACK_D] = { "r4_unpack_d", 1, 1 },
103 [QOP_UNPACK_8A_F] = { "unpack_8a_f", 1, 1 },
104 [QOP_UNPACK_8B_F] = { "unpack_8b_f", 1, 1 },
105 [QOP_UNPACK_8C_F] = { "unpack_8c_f", 1, 1 },
106 [QOP_UNPACK_8D_F] = { "unpack_8d_f", 1, 1 },
107 [QOP_UNPACK_16A_F] = { "unpack_16a_f", 1, 1 },
108 [QOP_UNPACK_16B_F] = { "unpack_16b_f", 1, 1 },
109 [QOP_UNPACK_8A_I] = { "unpack_8a_i", 1, 1 },
110 [QOP_UNPACK_8B_I] = { "unpack_8b_i", 1, 1 },
111 [QOP_UNPACK_8C_I] = { "unpack_8c_i", 1, 1 },
112 [QOP_UNPACK_8D_I] = { "unpack_8d_i", 1, 1 },
113 [QOP_UNPACK_16A_I] = { "unpack_16a_i", 1, 1 },
114 [QOP_UNPACK_16B_I] = { "unpack_16b_i", 1, 1 },
115 };
116
117 static const char *
118 qir_get_op_name(enum qop qop)
119 {
120 if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
121 return qir_op_info[qop].name;
122 else
123 return "???";
124 }
125
126 int
127 qir_get_op_nsrc(enum qop qop)
128 {
129 if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
130 return qir_op_info[qop].nsrc;
131 else
132 abort();
133 }
134
135 /**
136 * Returns whether the instruction has any side effects that must be
137 * preserved.
138 */
139 bool
140 qir_has_side_effects(struct vc4_compile *c, struct qinst *inst)
141 {
142 return qir_op_info[inst->op].has_side_effects;
143 }
144
145 bool
146 qir_has_side_effect_reads(struct vc4_compile *c, struct qinst *inst)
147 {
148 /* We can dead-code eliminate varyings, because we only tell the VS
149 * about the live ones at the end. But we have to preserve the
150 * point/line coordinates reads, because they're generated by
151 * fixed-function hardware.
152 */
153 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
154 if (inst->src[i].file == QFILE_VARY &&
155 c->input_semantics[inst->src[i].index].semantic == 0xff) {
156 return true;
157 }
158
159 if (inst->src[i].file == QFILE_VPM)
160 return true;
161 }
162
163 if (inst->dst.file == QFILE_VPM)
164 return true;
165
166 return false;
167 }
168
169 bool
170 qir_is_multi_instruction(struct qinst *inst)
171 {
172 return qir_op_info[inst->op].multi_instruction;
173 }
174
175 bool
176 qir_is_tex(struct qinst *inst)
177 {
178 return inst->op >= QOP_TEX_S && inst->op <= QOP_TEX_DIRECT;
179 }
180
181 bool
182 qir_depends_on_flags(struct qinst *inst)
183 {
184 switch (inst->op) {
185 case QOP_SEL_X_0_NS:
186 case QOP_SEL_X_0_NC:
187 case QOP_SEL_X_0_ZS:
188 case QOP_SEL_X_0_ZC:
189 case QOP_SEL_X_Y_NS:
190 case QOP_SEL_X_Y_NC:
191 case QOP_SEL_X_Y_ZS:
192 case QOP_SEL_X_Y_ZC:
193 return true;
194 default:
195 return false;
196 }
197 }
198
199 bool
200 qir_src_needs_a_file(struct qinst *inst)
201 {
202 switch (inst->op) {
203 case QOP_UNPACK_8A_F:
204 case QOP_UNPACK_8B_F:
205 case QOP_UNPACK_8C_F:
206 case QOP_UNPACK_8D_F:
207 case QOP_UNPACK_16A_F:
208 case QOP_UNPACK_16B_F:
209 case QOP_UNPACK_8A_I:
210 case QOP_UNPACK_8B_I:
211 case QOP_UNPACK_8C_I:
212 case QOP_UNPACK_8D_I:
213 case QOP_UNPACK_16A_I:
214 case QOP_UNPACK_16B_I:
215 return true;
216 default:
217 return false;
218 }
219 }
220
221 bool
222 qir_writes_r4(struct qinst *inst)
223 {
224 switch (inst->op) {
225 case QOP_TEX_RESULT:
226 case QOP_TLB_COLOR_READ:
227 case QOP_RCP:
228 case QOP_RSQ:
229 case QOP_EXP2:
230 case QOP_LOG2:
231 return true;
232 default:
233 return false;
234 }
235 }
236
237 bool
238 qir_reads_r4(struct qinst *inst)
239 {
240 switch (inst->op) {
241 case QOP_R4_UNPACK_A:
242 case QOP_R4_UNPACK_B:
243 case QOP_R4_UNPACK_C:
244 case QOP_R4_UNPACK_D:
245 return true;
246 default:
247 return false;
248 }
249 }
250
251 static void
252 qir_print_reg(struct vc4_compile *c, struct qreg reg, bool write)
253 {
254 static const char *files[] = {
255 [QFILE_TEMP] = "t",
256 [QFILE_VARY] = "v",
257 [QFILE_UNIF] = "u",
258 };
259
260 if (reg.file == QFILE_NULL) {
261 fprintf(stderr, "null");
262 } else if (reg.file == QFILE_SMALL_IMM) {
263 if ((int)reg.index >= -16 && (int)reg.index <= 15)
264 fprintf(stderr, "%d", reg.index);
265 else
266 fprintf(stderr, "%f", uif(reg.index));
267 } else if (reg.file == QFILE_VPM) {
268 if (write) {
269 fprintf(stderr, "vpm");
270 } else {
271 fprintf(stderr, "vpm%d.%d",
272 reg.index / 4, reg.index % 4);
273 }
274 } else {
275 fprintf(stderr, "%s%d", files[reg.file], reg.index);
276 }
277
278 if (reg.file == QFILE_UNIF &&
279 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT) {
280 fprintf(stderr, " (0x%08x / %f)",
281 c->uniform_data[reg.index],
282 uif(c->uniform_data[reg.index]));
283 }
284 }
285
286 void
287 qir_dump_inst(struct vc4_compile *c, struct qinst *inst)
288 {
289 fprintf(stderr, "%s%s ",
290 qir_get_op_name(inst->op),
291 inst->sf ? ".sf" : "");
292
293 qir_print_reg(c, inst->dst, true);
294 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
295 fprintf(stderr, ", ");
296 qir_print_reg(c, inst->src[i], false);
297 }
298 }
299
300 void
301 qir_dump(struct vc4_compile *c)
302 {
303 list_for_each_entry(struct qinst, inst, &c->instructions, link) {
304 qir_dump_inst(c, inst);
305 fprintf(stderr, "\n");
306 }
307 }
308
309 struct qreg
310 qir_get_temp(struct vc4_compile *c)
311 {
312 struct qreg reg;
313
314 reg.file = QFILE_TEMP;
315 reg.index = c->num_temps++;
316
317 if (c->num_temps > c->defs_array_size) {
318 uint32_t old_size = c->defs_array_size;
319 c->defs_array_size = MAX2(old_size * 2, 16);
320 c->defs = reralloc(c, c->defs, struct qinst *,
321 c->defs_array_size);
322 memset(&c->defs[old_size], 0,
323 sizeof(c->defs[0]) * (c->defs_array_size - old_size));
324 }
325
326 return reg;
327 }
328
329 struct qinst *
330 qir_inst(enum qop op, struct qreg dst, struct qreg src0, struct qreg src1)
331 {
332 struct qinst *inst = CALLOC_STRUCT(qinst);
333
334 inst->op = op;
335 inst->dst = dst;
336 inst->src = calloc(2, sizeof(inst->src[0]));
337 inst->src[0] = src0;
338 inst->src[1] = src1;
339
340 return inst;
341 }
342
343 struct qinst *
344 qir_inst4(enum qop op, struct qreg dst,
345 struct qreg a,
346 struct qreg b,
347 struct qreg c,
348 struct qreg d)
349 {
350 struct qinst *inst = CALLOC_STRUCT(qinst);
351
352 inst->op = op;
353 inst->dst = dst;
354 inst->src = calloc(4, sizeof(*inst->src));
355 inst->src[0] = a;
356 inst->src[1] = b;
357 inst->src[2] = c;
358 inst->src[3] = d;
359
360 return inst;
361 }
362
363 void
364 qir_emit(struct vc4_compile *c, struct qinst *inst)
365 {
366 if (inst->dst.file == QFILE_TEMP)
367 c->defs[inst->dst.index] = inst;
368
369 list_addtail(&inst->link, &c->instructions);
370 }
371
372 bool
373 qir_reg_equals(struct qreg a, struct qreg b)
374 {
375 return a.file == b.file && a.index == b.index;
376 }
377
378 struct vc4_compile *
379 qir_compile_init(void)
380 {
381 struct vc4_compile *c = rzalloc(NULL, struct vc4_compile);
382
383 list_inithead(&c->instructions);
384
385 c->output_position_index = -1;
386 c->output_clipvertex_index = -1;
387 c->output_color_index = -1;
388 c->output_point_size_index = -1;
389
390 c->def_ht = _mesa_hash_table_create(c, _mesa_hash_pointer,
391 _mesa_key_pointer_equal);
392
393 return c;
394 }
395
396 void
397 qir_remove_instruction(struct vc4_compile *c, struct qinst *qinst)
398 {
399 if (qinst->dst.file == QFILE_TEMP)
400 c->defs[qinst->dst.index] = NULL;
401
402 list_del(&qinst->link);
403 free(qinst->src);
404 free(qinst);
405 }
406
407 struct qreg
408 qir_follow_movs(struct vc4_compile *c, struct qreg reg)
409 {
410 while (reg.file == QFILE_TEMP && c->defs[reg.index]->op == QOP_MOV)
411 reg = c->defs[reg.index]->src[0];
412
413 return reg;
414 }
415
416 void
417 qir_compile_destroy(struct vc4_compile *c)
418 {
419 while (!list_empty(&c->instructions)) {
420 struct qinst *qinst =
421 (struct qinst *)c->instructions.next;
422 qir_remove_instruction(c, qinst);
423 }
424
425 ralloc_free(c);
426 }
427
428 const char *
429 qir_get_stage_name(enum qstage stage)
430 {
431 static const char *names[] = {
432 [QSTAGE_FRAG] = "FS",
433 [QSTAGE_VERT] = "VS",
434 [QSTAGE_COORD] = "CS",
435 };
436
437 return names[stage];
438 }
439
440 struct qreg
441 qir_uniform(struct vc4_compile *c,
442 enum quniform_contents contents,
443 uint32_t data)
444 {
445 for (int i = 0; i < c->num_uniforms; i++) {
446 if (c->uniform_contents[i] == contents &&
447 c->uniform_data[i] == data) {
448 return (struct qreg) { QFILE_UNIF, i };
449 }
450 }
451
452 uint32_t uniform = c->num_uniforms++;
453 struct qreg u = { QFILE_UNIF, uniform };
454
455 if (uniform >= c->uniform_array_size) {
456 c->uniform_array_size = MAX2(MAX2(16, uniform + 1),
457 c->uniform_array_size * 2);
458
459 c->uniform_data = reralloc(c, c->uniform_data,
460 uint32_t,
461 c->uniform_array_size);
462 c->uniform_contents = reralloc(c, c->uniform_contents,
463 enum quniform_contents,
464 c->uniform_array_size);
465 }
466
467 c->uniform_contents[uniform] = contents;
468 c->uniform_data[uniform] = data;
469
470 return u;
471 }
472
473 void
474 qir_SF(struct vc4_compile *c, struct qreg src)
475 {
476 struct qinst *last_inst = NULL;
477 if (!list_empty(&c->instructions))
478 last_inst = (struct qinst *)c->instructions.prev;
479
480 if (!last_inst ||
481 last_inst->dst.file != src.file ||
482 last_inst->dst.index != src.index ||
483 qir_is_multi_instruction(last_inst)) {
484 src = qir_MOV(c, src);
485 last_inst = (struct qinst *)c->instructions.prev;
486 }
487 last_inst->sf = true;
488 }
489
490 #define OPTPASS(func) \
491 do { \
492 bool stage_progress = func(c); \
493 if (stage_progress) { \
494 progress = true; \
495 if (print_opt_debug) { \
496 fprintf(stderr, \
497 "QIR opt pass %2d: %s progress\n", \
498 pass, #func); \
499 } \
500 } \
501 } while (0)
502
503 void
504 qir_optimize(struct vc4_compile *c)
505 {
506 bool print_opt_debug = false;
507 int pass = 1;
508
509 while (true) {
510 bool progress = false;
511
512 OPTPASS(qir_opt_algebraic);
513 OPTPASS(qir_opt_cse);
514 OPTPASS(qir_opt_constant_folding);
515 OPTPASS(qir_opt_copy_propagation);
516 OPTPASS(qir_opt_dead_code);
517 OPTPASS(qir_opt_small_immediates);
518 OPTPASS(qir_opt_vpm_writes);
519
520 if (!progress)
521 break;
522
523 pass++;
524 }
525 }