faf5280103ee7e50cfa20d60fca8eb4fc2cef5cf
[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 };
35
36 static const struct qir_op_info qir_op_info[] = {
37 [QOP_MOV] = { "mov", 1, 1 },
38 [QOP_FMOV] = { "fmov", 1, 1 },
39 [QOP_MMOV] = { "mmov", 1, 1 },
40 [QOP_FADD] = { "fadd", 1, 2 },
41 [QOP_FSUB] = { "fsub", 1, 2 },
42 [QOP_FMUL] = { "fmul", 1, 2 },
43 [QOP_MUL24] = { "mul24", 1, 2 },
44 [QOP_V8MULD] = {"v8muld", 1, 2 },
45 [QOP_V8MIN] = {"v8min", 1, 2 },
46 [QOP_V8MAX] = {"v8max", 1, 2 },
47 [QOP_V8ADDS] = {"v8adds", 1, 2 },
48 [QOP_V8SUBS] = {"v8subs", 1, 2 },
49 [QOP_FMIN] = { "fmin", 1, 2 },
50 [QOP_FMAX] = { "fmax", 1, 2 },
51 [QOP_FMINABS] = { "fminabs", 1, 2 },
52 [QOP_FMAXABS] = { "fmaxabs", 1, 2 },
53 [QOP_FTOI] = { "ftoi", 1, 1 },
54 [QOP_ITOF] = { "itof", 1, 1 },
55 [QOP_ADD] = { "add", 1, 2 },
56 [QOP_SUB] = { "sub", 1, 2 },
57 [QOP_SHR] = { "shr", 1, 2 },
58 [QOP_ASR] = { "asr", 1, 2 },
59 [QOP_SHL] = { "shl", 1, 2 },
60 [QOP_MIN] = { "min", 1, 2 },
61 [QOP_MAX] = { "max", 1, 2 },
62 [QOP_AND] = { "and", 1, 2 },
63 [QOP_OR] = { "or", 1, 2 },
64 [QOP_XOR] = { "xor", 1, 2 },
65 [QOP_NOT] = { "not", 1, 1 },
66
67 [QOP_RCP] = { "rcp", 1, 1 },
68 [QOP_RSQ] = { "rsq", 1, 1 },
69 [QOP_EXP2] = { "exp2", 1, 2 },
70 [QOP_LOG2] = { "log2", 1, 2 },
71 [QOP_TLB_COLOR_READ] = { "tlb_color_read", 1, 0 },
72 [QOP_MS_MASK] = { "ms_mask", 0, 1, true },
73 [QOP_VARY_ADD_C] = { "vary_add_c", 1, 1 },
74
75 [QOP_FRAG_Z] = { "frag_z", 1, 0 },
76 [QOP_FRAG_W] = { "frag_w", 1, 0 },
77
78 [QOP_TEX_S] = { "tex_s", 0, 2 },
79 [QOP_TEX_T] = { "tex_t", 0, 2 },
80 [QOP_TEX_R] = { "tex_r", 0, 2 },
81 [QOP_TEX_B] = { "tex_b", 0, 2 },
82 [QOP_TEX_DIRECT] = { "tex_direct", 0, 2 },
83 [QOP_TEX_RESULT] = { "tex_result", 1, 0, true },
84 };
85
86 static const char *
87 qir_get_op_name(enum qop qop)
88 {
89 if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
90 return qir_op_info[qop].name;
91 else
92 return "???";
93 }
94
95 int
96 qir_get_op_nsrc(enum qop qop)
97 {
98 if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
99 return qir_op_info[qop].nsrc;
100 else
101 abort();
102 }
103
104 /**
105 * Returns whether the instruction has any side effects that must be
106 * preserved.
107 */
108 bool
109 qir_has_side_effects(struct vc4_compile *c, struct qinst *inst)
110 {
111 switch (inst->dst.file) {
112 case QFILE_TLB_Z_WRITE:
113 case QFILE_TLB_COLOR_WRITE:
114 case QFILE_TLB_COLOR_WRITE_MS:
115 case QFILE_TLB_STENCIL_SETUP:
116 return true;
117 default:
118 break;
119 }
120
121 return qir_op_info[inst->op].has_side_effects;
122 }
123
124 bool
125 qir_has_side_effect_reads(struct vc4_compile *c, struct qinst *inst)
126 {
127 /* We can dead-code eliminate varyings, because we only tell the VS
128 * about the live ones at the end. But we have to preserve the
129 * point/line coordinates reads, because they're generated by
130 * fixed-function hardware.
131 */
132 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
133 if (inst->src[i].file == QFILE_VARY &&
134 c->input_slots[inst->src[i].index].slot == 0xff) {
135 return true;
136 }
137
138 if (inst->src[i].file == QFILE_VPM)
139 return true;
140 }
141
142 if (inst->dst.file == QFILE_VPM)
143 return true;
144
145 return false;
146 }
147
148 bool
149 qir_is_mul(struct qinst *inst)
150 {
151 switch (inst->op) {
152 case QOP_MMOV:
153 case QOP_FMUL:
154 case QOP_MUL24:
155 case QOP_V8MULD:
156 case QOP_V8MIN:
157 case QOP_V8MAX:
158 case QOP_V8ADDS:
159 case QOP_V8SUBS:
160 return true;
161 default:
162 return false;
163 }
164 }
165
166 bool
167 qir_is_float_input(struct qinst *inst)
168 {
169 switch (inst->op) {
170 case QOP_FMOV:
171 case QOP_FMUL:
172 case QOP_FADD:
173 case QOP_FSUB:
174 case QOP_FMIN:
175 case QOP_FMAX:
176 case QOP_FMINABS:
177 case QOP_FMAXABS:
178 case QOP_FTOI:
179 return true;
180 default:
181 return false;
182 }
183 }
184
185 bool
186 qir_is_raw_mov(struct qinst *inst)
187 {
188 return ((inst->op == QOP_MOV ||
189 inst->op == QOP_FMOV ||
190 inst->op == QOP_MMOV) &&
191 inst->cond == QPU_COND_ALWAYS &&
192 !inst->dst.pack &&
193 !inst->src[0].pack);
194 }
195
196 bool
197 qir_is_tex(struct qinst *inst)
198 {
199 return inst->op >= QOP_TEX_S && inst->op <= QOP_TEX_DIRECT;
200 }
201
202 bool
203 qir_depends_on_flags(struct qinst *inst)
204 {
205 return (inst->cond != QPU_COND_ALWAYS &&
206 inst->cond != QPU_COND_NEVER);
207 }
208
209 bool
210 qir_writes_r4(struct qinst *inst)
211 {
212 switch (inst->op) {
213 case QOP_TEX_RESULT:
214 case QOP_TLB_COLOR_READ:
215 case QOP_RCP:
216 case QOP_RSQ:
217 case QOP_EXP2:
218 case QOP_LOG2:
219 return true;
220 default:
221 return false;
222 }
223 }
224
225 static void
226 qir_print_reg(struct vc4_compile *c, struct qreg reg, bool write)
227 {
228 static const char *files[] = {
229 [QFILE_TEMP] = "t",
230 [QFILE_VARY] = "v",
231 [QFILE_UNIF] = "u",
232 [QFILE_TLB_COLOR_WRITE] = "tlb_c",
233 [QFILE_TLB_COLOR_WRITE_MS] = "tlb_c_ms",
234 [QFILE_TLB_Z_WRITE] = "tlb_z",
235 [QFILE_TLB_STENCIL_SETUP] = "tlb_stencil",
236 [QFILE_FRAG_X] = "frag_x",
237 [QFILE_FRAG_Y] = "frag_y",
238 [QFILE_FRAG_REV_FLAG] = "frag_rev_flag",
239 };
240
241 switch (reg.file) {
242
243 case QFILE_NULL:
244 fprintf(stderr, "null");
245 break;
246
247 case QFILE_SMALL_IMM:
248 if ((int)reg.index >= -16 && (int)reg.index <= 15)
249 fprintf(stderr, "%d", reg.index);
250 else
251 fprintf(stderr, "%f", uif(reg.index));
252 break;
253
254 case QFILE_VPM:
255 if (write) {
256 fprintf(stderr, "vpm");
257 } else {
258 fprintf(stderr, "vpm%d.%d",
259 reg.index / 4, reg.index % 4);
260 }
261 break;
262
263 case QFILE_TLB_COLOR_WRITE:
264 case QFILE_TLB_COLOR_WRITE_MS:
265 case QFILE_TLB_Z_WRITE:
266 case QFILE_TLB_STENCIL_SETUP:
267 fprintf(stderr, "%s", files[reg.file]);
268 break;
269
270 default:
271 fprintf(stderr, "%s%d", files[reg.file], reg.index);
272 break;
273 }
274
275 if (reg.file == QFILE_UNIF &&
276 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT) {
277 fprintf(stderr, " (0x%08x / %f)",
278 c->uniform_data[reg.index],
279 uif(c->uniform_data[reg.index]));
280 }
281 }
282
283 void
284 qir_dump_inst(struct vc4_compile *c, struct qinst *inst)
285 {
286 fprintf(stderr, "%s", qir_get_op_name(inst->op));
287 vc4_qpu_disasm_cond(stderr, inst->cond);
288 if (inst->sf)
289 fprintf(stderr, ".sf");
290 fprintf(stderr, " ");
291
292 qir_print_reg(c, inst->dst, true);
293 if (inst->dst.pack) {
294 if (inst->dst.pack) {
295 if (qir_is_mul(inst))
296 vc4_qpu_disasm_pack_mul(stderr, inst->dst.pack);
297 else
298 vc4_qpu_disasm_pack_a(stderr, inst->dst.pack);
299 }
300 }
301 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
302 fprintf(stderr, ", ");
303 qir_print_reg(c, inst->src[i], false);
304 vc4_qpu_disasm_unpack(stderr, inst->src[i].pack);
305 }
306 }
307
308 void
309 qir_dump(struct vc4_compile *c)
310 {
311 list_for_each_entry(struct qinst, inst, &c->instructions, link) {
312 qir_dump_inst(c, inst);
313 fprintf(stderr, "\n");
314 }
315 }
316
317 struct qreg
318 qir_get_temp(struct vc4_compile *c)
319 {
320 struct qreg reg;
321
322 reg.file = QFILE_TEMP;
323 reg.index = c->num_temps++;
324 reg.pack = 0;
325
326 if (c->num_temps > c->defs_array_size) {
327 uint32_t old_size = c->defs_array_size;
328 c->defs_array_size = MAX2(old_size * 2, 16);
329 c->defs = reralloc(c, c->defs, struct qinst *,
330 c->defs_array_size);
331 memset(&c->defs[old_size], 0,
332 sizeof(c->defs[0]) * (c->defs_array_size - old_size));
333 }
334
335 return reg;
336 }
337
338 struct qinst *
339 qir_inst(enum qop op, struct qreg dst, struct qreg src0, struct qreg src1)
340 {
341 struct qinst *inst = CALLOC_STRUCT(qinst);
342
343 inst->op = op;
344 inst->dst = dst;
345 inst->src = calloc(2, sizeof(inst->src[0]));
346 inst->src[0] = src0;
347 inst->src[1] = src1;
348 inst->cond = QPU_COND_ALWAYS;
349
350 return inst;
351 }
352
353 struct qinst *
354 qir_inst4(enum qop op, struct qreg dst,
355 struct qreg a,
356 struct qreg b,
357 struct qreg c,
358 struct qreg d)
359 {
360 struct qinst *inst = CALLOC_STRUCT(qinst);
361
362 inst->op = op;
363 inst->dst = dst;
364 inst->src = calloc(4, sizeof(*inst->src));
365 inst->src[0] = a;
366 inst->src[1] = b;
367 inst->src[2] = c;
368 inst->src[3] = d;
369
370 return inst;
371 }
372
373 void
374 qir_emit(struct vc4_compile *c, struct qinst *inst)
375 {
376 if (inst->dst.file == QFILE_TEMP)
377 c->defs[inst->dst.index] = inst;
378
379 qir_emit_nodef(c, inst);
380 }
381
382 bool
383 qir_reg_equals(struct qreg a, struct qreg b)
384 {
385 return a.file == b.file && a.index == b.index;
386 }
387
388 struct vc4_compile *
389 qir_compile_init(void)
390 {
391 struct vc4_compile *c = rzalloc(NULL, struct vc4_compile);
392
393 list_inithead(&c->instructions);
394
395 c->output_position_index = -1;
396 c->output_color_index = -1;
397 c->output_point_size_index = -1;
398 c->output_sample_mask_index = -1;
399
400 c->def_ht = _mesa_hash_table_create(c, _mesa_hash_pointer,
401 _mesa_key_pointer_equal);
402
403 return c;
404 }
405
406 void
407 qir_remove_instruction(struct vc4_compile *c, struct qinst *qinst)
408 {
409 if (qinst->dst.file == QFILE_TEMP)
410 c->defs[qinst->dst.index] = NULL;
411
412 list_del(&qinst->link);
413 free(qinst->src);
414 free(qinst);
415 }
416
417 struct qreg
418 qir_follow_movs(struct vc4_compile *c, struct qreg reg)
419 {
420 int pack = reg.pack;
421
422 while (reg.file == QFILE_TEMP &&
423 c->defs[reg.index] &&
424 (c->defs[reg.index]->op == QOP_MOV ||
425 c->defs[reg.index]->op == QOP_FMOV ||
426 c->defs[reg.index]->op == QOP_MMOV)&&
427 !c->defs[reg.index]->dst.pack &&
428 !c->defs[reg.index]->src[0].pack) {
429 reg = c->defs[reg.index]->src[0];
430 }
431
432 reg.pack = pack;
433 return reg;
434 }
435
436 void
437 qir_compile_destroy(struct vc4_compile *c)
438 {
439 while (!list_empty(&c->instructions)) {
440 struct qinst *qinst =
441 (struct qinst *)c->instructions.next;
442 qir_remove_instruction(c, qinst);
443 }
444
445 ralloc_free(c);
446 }
447
448 const char *
449 qir_get_stage_name(enum qstage stage)
450 {
451 static const char *names[] = {
452 [QSTAGE_FRAG] = "FS",
453 [QSTAGE_VERT] = "VS",
454 [QSTAGE_COORD] = "CS",
455 };
456
457 return names[stage];
458 }
459
460 struct qreg
461 qir_uniform(struct vc4_compile *c,
462 enum quniform_contents contents,
463 uint32_t data)
464 {
465 for (int i = 0; i < c->num_uniforms; i++) {
466 if (c->uniform_contents[i] == contents &&
467 c->uniform_data[i] == data) {
468 return qir_reg(QFILE_UNIF, i);
469 }
470 }
471
472 uint32_t uniform = c->num_uniforms++;
473
474 if (uniform >= c->uniform_array_size) {
475 c->uniform_array_size = MAX2(MAX2(16, uniform + 1),
476 c->uniform_array_size * 2);
477
478 c->uniform_data = reralloc(c, c->uniform_data,
479 uint32_t,
480 c->uniform_array_size);
481 c->uniform_contents = reralloc(c, c->uniform_contents,
482 enum quniform_contents,
483 c->uniform_array_size);
484 }
485
486 c->uniform_contents[uniform] = contents;
487 c->uniform_data[uniform] = data;
488
489 return qir_reg(QFILE_UNIF, uniform);
490 }
491
492 void
493 qir_SF(struct vc4_compile *c, struct qreg src)
494 {
495 struct qinst *last_inst = NULL;
496 if (!list_empty(&c->instructions))
497 last_inst = (struct qinst *)c->instructions.prev;
498
499 /* We don't have any way to guess which kind of MOV is implied. */
500 assert(!src.pack);
501
502 if (src.file != QFILE_TEMP ||
503 !c->defs[src.index] ||
504 last_inst != c->defs[src.index]) {
505 last_inst = qir_MOV_dest(c, qir_reg(QFILE_NULL, 0), src);
506 last_inst = (struct qinst *)c->instructions.prev;
507 }
508 last_inst->sf = true;
509 }
510
511 #define OPTPASS(func) \
512 do { \
513 bool stage_progress = func(c); \
514 if (stage_progress) { \
515 progress = true; \
516 if (print_opt_debug) { \
517 fprintf(stderr, \
518 "QIR opt pass %2d: %s progress\n", \
519 pass, #func); \
520 } \
521 } \
522 } while (0)
523
524 void
525 qir_optimize(struct vc4_compile *c)
526 {
527 bool print_opt_debug = false;
528 int pass = 1;
529
530 while (true) {
531 bool progress = false;
532
533 OPTPASS(qir_opt_algebraic);
534 OPTPASS(qir_opt_constant_folding);
535 OPTPASS(qir_opt_copy_propagation);
536 OPTPASS(qir_opt_dead_code);
537 OPTPASS(qir_opt_small_immediates);
538 OPTPASS(qir_opt_vpm);
539
540 if (!progress)
541 break;
542
543 pass++;
544 }
545 }