OSDN Git Service

vc4: Add a thread switch QIR instruction.
[android-x86/external-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, 1 },
70         [QOP_LOG2] = { "log2", 1, 1 },
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, true },
79         [QOP_TEX_T] = { "tex_t", 0, 2, true },
80         [QOP_TEX_R] = { "tex_r", 0, 2, true },
81         [QOP_TEX_B] = { "tex_b", 0, 2, true },
82         [QOP_TEX_DIRECT] = { "tex_direct", 0, 2, true },
83         [QOP_TEX_RESULT] = { "tex_result", 1, 0, true },
84
85         [QOP_THRSW] = { "thrsw", 0, 0, true },
86
87         [QOP_LOAD_IMM] = { "load_imm", 0, 1 },
88         [QOP_LOAD_IMM_U2] = { "load_imm_u2", 0, 1 },
89         [QOP_LOAD_IMM_I2] = { "load_imm_i2", 0, 1 },
90
91         [QOP_ROT_MUL] = { "rot_mul", 0, 2 },
92
93         [QOP_BRANCH] = { "branch", 0, 0, true },
94         [QOP_UNIFORMS_RESET] = { "uniforms_reset", 0, 2, true },
95 };
96
97 static const char *
98 qir_get_op_name(enum qop qop)
99 {
100         if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
101                 return qir_op_info[qop].name;
102         else
103                 return "???";
104 }
105
106 int
107 qir_get_op_nsrc(enum qop qop)
108 {
109         if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
110                 return qir_op_info[qop].nsrc;
111         else
112                 abort();
113 }
114
115 /**
116  * Returns whether the instruction has any side effects that must be
117  * preserved.
118  */
119 bool
120 qir_has_side_effects(struct vc4_compile *c, struct qinst *inst)
121 {
122         switch (inst->dst.file) {
123         case QFILE_TLB_Z_WRITE:
124         case QFILE_TLB_COLOR_WRITE:
125         case QFILE_TLB_COLOR_WRITE_MS:
126         case QFILE_TLB_STENCIL_SETUP:
127                 return true;
128         default:
129                 break;
130         }
131
132         return qir_op_info[inst->op].has_side_effects;
133 }
134
135 bool
136 qir_has_side_effect_reads(struct vc4_compile *c, struct qinst *inst)
137 {
138         /* We can dead-code eliminate varyings, because we only tell the VS
139          * about the live ones at the end.  But we have to preserve the
140          * point/line coordinates reads, because they're generated by
141          * fixed-function hardware.
142          */
143         for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
144                 if (inst->src[i].file == QFILE_VARY &&
145                     c->input_slots[inst->src[i].index].slot == 0xff) {
146                         return true;
147                 }
148
149                 if (inst->src[i].file == QFILE_VPM)
150                         return true;
151         }
152
153         if (inst->dst.file == QFILE_VPM)
154                 return true;
155
156         return false;
157 }
158
159 bool
160 qir_is_mul(struct qinst *inst)
161 {
162         switch (inst->op) {
163         case QOP_MMOV:
164         case QOP_FMUL:
165         case QOP_MUL24:
166         case QOP_V8MULD:
167         case QOP_V8MIN:
168         case QOP_V8MAX:
169         case QOP_V8ADDS:
170         case QOP_V8SUBS:
171         case QOP_ROT_MUL:
172                 return true;
173         default:
174                 return false;
175         }
176 }
177
178 bool
179 qir_is_float_input(struct qinst *inst)
180 {
181         switch (inst->op) {
182         case QOP_FMOV:
183         case QOP_FMUL:
184         case QOP_FADD:
185         case QOP_FSUB:
186         case QOP_FMIN:
187         case QOP_FMAX:
188         case QOP_FMINABS:
189         case QOP_FMAXABS:
190         case QOP_FTOI:
191                 return true;
192         default:
193                 return false;
194         }
195 }
196
197 bool
198 qir_is_raw_mov(struct qinst *inst)
199 {
200         return ((inst->op == QOP_MOV ||
201                  inst->op == QOP_FMOV ||
202                  inst->op == QOP_MMOV) &&
203                 inst->cond == QPU_COND_ALWAYS &&
204                 !inst->dst.pack &&
205                 !inst->src[0].pack);
206 }
207
208 bool
209 qir_is_tex(struct qinst *inst)
210 {
211         return inst->op >= QOP_TEX_S && inst->op <= QOP_TEX_DIRECT;
212 }
213
214 bool
215 qir_depends_on_flags(struct qinst *inst)
216 {
217         if (inst->op == QOP_BRANCH) {
218                 return inst->cond != QPU_COND_BRANCH_ALWAYS;
219         } else {
220                 return (inst->cond != QPU_COND_ALWAYS &&
221                         inst->cond != QPU_COND_NEVER);
222         }
223 }
224
225 bool
226 qir_writes_r4(struct qinst *inst)
227 {
228         switch (inst->op) {
229         case QOP_TEX_RESULT:
230         case QOP_TLB_COLOR_READ:
231         case QOP_RCP:
232         case QOP_RSQ:
233         case QOP_EXP2:
234         case QOP_LOG2:
235                 return true;
236         default:
237                 return false;
238         }
239 }
240
241 uint8_t
242 qir_channels_written(struct qinst *inst)
243 {
244         if (qir_is_mul(inst)) {
245                 switch (inst->dst.pack) {
246                 case QPU_PACK_MUL_NOP:
247                 case QPU_PACK_MUL_8888:
248                         return 0xf;
249                 case QPU_PACK_MUL_8A:
250                         return 0x1;
251                 case QPU_PACK_MUL_8B:
252                         return 0x2;
253                 case QPU_PACK_MUL_8C:
254                         return 0x4;
255                 case QPU_PACK_MUL_8D:
256                         return 0x8;
257                 }
258         } else {
259                 switch (inst->dst.pack) {
260                 case QPU_PACK_A_NOP:
261                 case QPU_PACK_A_8888:
262                 case QPU_PACK_A_8888_SAT:
263                 case QPU_PACK_A_32_SAT:
264                         return 0xf;
265                 case QPU_PACK_A_8A:
266                 case QPU_PACK_A_8A_SAT:
267                         return 0x1;
268                 case QPU_PACK_A_8B:
269                 case QPU_PACK_A_8B_SAT:
270                         return 0x2;
271                 case QPU_PACK_A_8C:
272                 case QPU_PACK_A_8C_SAT:
273                         return 0x4;
274                 case QPU_PACK_A_8D:
275                 case QPU_PACK_A_8D_SAT:
276                         return 0x8;
277                 case QPU_PACK_A_16A:
278                 case QPU_PACK_A_16A_SAT:
279                         return 0x3;
280                 case QPU_PACK_A_16B:
281                 case QPU_PACK_A_16B_SAT:
282                         return 0xc;
283                 }
284         }
285         unreachable("Bad pack field");
286 }
287
288 static void
289 qir_print_reg(struct vc4_compile *c, struct qreg reg, bool write)
290 {
291         static const char *files[] = {
292                 [QFILE_TEMP] = "t",
293                 [QFILE_VARY] = "v",
294                 [QFILE_UNIF] = "u",
295                 [QFILE_TLB_COLOR_WRITE] = "tlb_c",
296                 [QFILE_TLB_COLOR_WRITE_MS] = "tlb_c_ms",
297                 [QFILE_TLB_Z_WRITE] = "tlb_z",
298                 [QFILE_TLB_STENCIL_SETUP] = "tlb_stencil",
299                 [QFILE_FRAG_X] = "frag_x",
300                 [QFILE_FRAG_Y] = "frag_y",
301                 [QFILE_FRAG_REV_FLAG] = "frag_rev_flag",
302                 [QFILE_QPU_ELEMENT] = "elem",
303         };
304
305         switch (reg.file) {
306
307         case QFILE_NULL:
308                 fprintf(stderr, "null");
309                 break;
310
311         case QFILE_LOAD_IMM:
312                 fprintf(stderr, "0x%08x (%f)", reg.index, uif(reg.index));
313                 break;
314
315         case QFILE_SMALL_IMM:
316                 if ((int)reg.index >= -16 && (int)reg.index <= 15)
317                         fprintf(stderr, "%d", reg.index);
318                 else
319                         fprintf(stderr, "%f", uif(reg.index));
320                 break;
321
322         case QFILE_VPM:
323                 if (write) {
324                         fprintf(stderr, "vpm");
325                 } else {
326                         fprintf(stderr, "vpm%d.%d",
327                                 reg.index / 4, reg.index % 4);
328                 }
329                 break;
330
331         case QFILE_TLB_COLOR_WRITE:
332         case QFILE_TLB_COLOR_WRITE_MS:
333         case QFILE_TLB_Z_WRITE:
334         case QFILE_TLB_STENCIL_SETUP:
335                 fprintf(stderr, "%s", files[reg.file]);
336                 break;
337
338         default:
339                 fprintf(stderr, "%s%d", files[reg.file], reg.index);
340                 break;
341         }
342
343         if (reg.file == QFILE_UNIF &&
344             c->uniform_contents[reg.index] == QUNIFORM_CONSTANT) {
345                 fprintf(stderr, " (0x%08x / %f)",
346                         c->uniform_data[reg.index],
347                         uif(c->uniform_data[reg.index]));
348         }
349 }
350
351 void
352 qir_dump_inst(struct vc4_compile *c, struct qinst *inst)
353 {
354         fprintf(stderr, "%s", qir_get_op_name(inst->op));
355         if (inst->op == QOP_BRANCH)
356                 vc4_qpu_disasm_cond_branch(stderr, inst->cond);
357         else
358                 vc4_qpu_disasm_cond(stderr, inst->cond);
359         if (inst->sf)
360                 fprintf(stderr, ".sf");
361         fprintf(stderr, " ");
362
363         if (inst->op != QOP_BRANCH) {
364                 qir_print_reg(c, inst->dst, true);
365                 if (inst->dst.pack) {
366                         if (inst->dst.pack) {
367                                 if (qir_is_mul(inst))
368                                         vc4_qpu_disasm_pack_mul(stderr, inst->dst.pack);
369                                 else
370                                         vc4_qpu_disasm_pack_a(stderr, inst->dst.pack);
371                         }
372                 }
373         }
374
375         for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
376                 fprintf(stderr, ", ");
377                 qir_print_reg(c, inst->src[i], false);
378                 vc4_qpu_disasm_unpack(stderr, inst->src[i].pack);
379         }
380 }
381
382 void
383 qir_dump(struct vc4_compile *c)
384 {
385         int ip = 0;
386         int pressure = 0;
387
388         qir_for_each_block(block, c) {
389                 fprintf(stderr, "BLOCK %d:\n", block->index);
390                 qir_for_each_inst(inst, block) {
391                         if (c->temp_start) {
392                                 bool first = true;
393
394                                 fprintf(stderr, "%3d ", pressure);
395
396                                 for (int i = 0; i < c->num_temps; i++) {
397                                         if (c->temp_start[i] != ip)
398                                                 continue;
399
400                                         if (first) {
401                                                 first = false;
402                                         } else {
403                                                 fprintf(stderr, ", ");
404                                         }
405                                         fprintf(stderr, "S%4d", i);
406                                         pressure++;
407                                 }
408
409                                 if (first)
410                                         fprintf(stderr, "      ");
411                                 else
412                                         fprintf(stderr, " ");
413                         }
414
415                         if (c->temp_end) {
416                                 bool first = true;
417
418                                 for (int i = 0; i < c->num_temps; i++) {
419                                         if (c->temp_end[i] != ip)
420                                                 continue;
421
422                                         if (first) {
423                                                 first = false;
424                                         } else {
425                                                 fprintf(stderr, ", ");
426                                         }
427                                         fprintf(stderr, "E%4d", i);
428                                         pressure--;
429                                 }
430
431                                 if (first)
432                                         fprintf(stderr, "      ");
433                                 else
434                                         fprintf(stderr, " ");
435                         }
436
437                         qir_dump_inst(c, inst);
438                         fprintf(stderr, "\n");
439                         ip++;
440                 }
441                 if (block->successors[1]) {
442                         fprintf(stderr, "-> BLOCK %d, %d\n",
443                                 block->successors[0]->index,
444                                 block->successors[1]->index);
445                 } else if (block->successors[0]) {
446                         fprintf(stderr, "-> BLOCK %d\n",
447                                 block->successors[0]->index);
448                 }
449         }
450 }
451
452 struct qreg
453 qir_get_temp(struct vc4_compile *c)
454 {
455         struct qreg reg;
456
457         reg.file = QFILE_TEMP;
458         reg.index = c->num_temps++;
459         reg.pack = 0;
460
461         if (c->num_temps > c->defs_array_size) {
462                 uint32_t old_size = c->defs_array_size;
463                 c->defs_array_size = MAX2(old_size * 2, 16);
464                 c->defs = reralloc(c, c->defs, struct qinst *,
465                                    c->defs_array_size);
466                 memset(&c->defs[old_size], 0,
467                        sizeof(c->defs[0]) * (c->defs_array_size - old_size));
468         }
469
470         return reg;
471 }
472
473 struct qinst *
474 qir_inst(enum qop op, struct qreg dst, struct qreg src0, struct qreg src1)
475 {
476         struct qinst *inst = CALLOC_STRUCT(qinst);
477
478         inst->op = op;
479         inst->dst = dst;
480         inst->src = calloc(2, sizeof(inst->src[0]));
481         inst->src[0] = src0;
482         inst->src[1] = src1;
483         inst->cond = QPU_COND_ALWAYS;
484
485         return inst;
486 }
487
488 struct qinst *
489 qir_inst4(enum qop op, struct qreg dst,
490           struct qreg a,
491           struct qreg b,
492           struct qreg c,
493           struct qreg d)
494 {
495         struct qinst *inst = CALLOC_STRUCT(qinst);
496
497         inst->op = op;
498         inst->dst = dst;
499         inst->src = calloc(4, sizeof(*inst->src));
500         inst->src[0] = a;
501         inst->src[1] = b;
502         inst->src[2] = c;
503         inst->src[3] = d;
504
505         return inst;
506 }
507
508 static void
509 qir_emit(struct vc4_compile *c, struct qinst *inst)
510 {
511         list_addtail(&inst->link, &c->cur_block->instructions);
512 }
513
514 /* Updates inst to write to a new temporary, emits it, and notes the def. */
515 struct qreg
516 qir_emit_def(struct vc4_compile *c, struct qinst *inst)
517 {
518         assert(inst->dst.file == QFILE_NULL);
519
520         inst->dst = qir_get_temp(c);
521
522         if (inst->dst.file == QFILE_TEMP)
523                 c->defs[inst->dst.index] = inst;
524
525         qir_emit(c, inst);
526
527         return inst->dst;
528 }
529
530 struct qinst *
531 qir_emit_nondef(struct vc4_compile *c, struct qinst *inst)
532 {
533         if (inst->dst.file == QFILE_TEMP)
534                 c->defs[inst->dst.index] = NULL;
535
536         qir_emit(c, inst);
537
538         return inst;
539 }
540
541 bool
542 qir_reg_equals(struct qreg a, struct qreg b)
543 {
544         return a.file == b.file && a.index == b.index && a.pack == b.pack;
545 }
546
547 struct qblock *
548 qir_new_block(struct vc4_compile *c)
549 {
550         struct qblock *block = rzalloc(c, struct qblock);
551
552         list_inithead(&block->instructions);
553         list_inithead(&block->qpu_inst_list);
554
555         block->predecessors = _mesa_set_create(block,
556                                                _mesa_hash_pointer,
557                                                _mesa_key_pointer_equal);
558
559         block->index = c->next_block_index++;
560
561         return block;
562 }
563
564 void
565 qir_set_emit_block(struct vc4_compile *c, struct qblock *block)
566 {
567         c->cur_block = block;
568         list_addtail(&block->link, &c->blocks);
569 }
570
571 struct qblock *
572 qir_entry_block(struct vc4_compile *c)
573 {
574         return list_first_entry(&c->blocks, struct qblock, link);
575 }
576
577 struct qblock *
578 qir_exit_block(struct vc4_compile *c)
579 {
580         return list_last_entry(&c->blocks, struct qblock, link);
581 }
582
583 void
584 qir_link_blocks(struct qblock *predecessor, struct qblock *successor)
585 {
586         _mesa_set_add(successor->predecessors, predecessor);
587         if (predecessor->successors[0]) {
588                 assert(!predecessor->successors[1]);
589                 predecessor->successors[1] = successor;
590         } else {
591                 predecessor->successors[0] = successor;
592         }
593 }
594
595 struct vc4_compile *
596 qir_compile_init(void)
597 {
598         struct vc4_compile *c = rzalloc(NULL, struct vc4_compile);
599
600         list_inithead(&c->blocks);
601         qir_set_emit_block(c, qir_new_block(c));
602
603         c->output_position_index = -1;
604         c->output_color_index = -1;
605         c->output_point_size_index = -1;
606         c->output_sample_mask_index = -1;
607
608         c->def_ht = _mesa_hash_table_create(c, _mesa_hash_pointer,
609                                             _mesa_key_pointer_equal);
610
611         return c;
612 }
613
614 void
615 qir_remove_instruction(struct vc4_compile *c, struct qinst *qinst)
616 {
617         if (qinst->dst.file == QFILE_TEMP)
618                 c->defs[qinst->dst.index] = NULL;
619
620         list_del(&qinst->link);
621         free(qinst->src);
622         free(qinst);
623 }
624
625 struct qreg
626 qir_follow_movs(struct vc4_compile *c, struct qreg reg)
627 {
628         int pack = reg.pack;
629
630         while (reg.file == QFILE_TEMP &&
631                c->defs[reg.index] &&
632                (c->defs[reg.index]->op == QOP_MOV ||
633                 c->defs[reg.index]->op == QOP_FMOV ||
634                 c->defs[reg.index]->op == QOP_MMOV)&&
635                !c->defs[reg.index]->dst.pack &&
636                !c->defs[reg.index]->src[0].pack) {
637                 reg = c->defs[reg.index]->src[0];
638         }
639
640         reg.pack = pack;
641         return reg;
642 }
643
644 void
645 qir_compile_destroy(struct vc4_compile *c)
646 {
647         qir_for_each_block(block, c) {
648                 while (!list_empty(&block->instructions)) {
649                         struct qinst *qinst =
650                                 list_first_entry(&block->instructions,
651                                                  struct qinst, link);
652                         qir_remove_instruction(c, qinst);
653                 }
654         }
655
656         ralloc_free(c);
657 }
658
659 const char *
660 qir_get_stage_name(enum qstage stage)
661 {
662         static const char *names[] = {
663                 [QSTAGE_FRAG] = "FS",
664                 [QSTAGE_VERT] = "VS",
665                 [QSTAGE_COORD] = "CS",
666         };
667
668         return names[stage];
669 }
670
671 struct qreg
672 qir_uniform(struct vc4_compile *c,
673             enum quniform_contents contents,
674             uint32_t data)
675 {
676         for (int i = 0; i < c->num_uniforms; i++) {
677                 if (c->uniform_contents[i] == contents &&
678                     c->uniform_data[i] == data) {
679                         return qir_reg(QFILE_UNIF, i);
680                 }
681         }
682
683         uint32_t uniform = c->num_uniforms++;
684
685         if (uniform >= c->uniform_array_size) {
686                 c->uniform_array_size = MAX2(MAX2(16, uniform + 1),
687                                              c->uniform_array_size * 2);
688
689                 c->uniform_data = reralloc(c, c->uniform_data,
690                                            uint32_t,
691                                            c->uniform_array_size);
692                 c->uniform_contents = reralloc(c, c->uniform_contents,
693                                                enum quniform_contents,
694                                                c->uniform_array_size);
695         }
696
697         c->uniform_contents[uniform] = contents;
698         c->uniform_data[uniform] = data;
699
700         return qir_reg(QFILE_UNIF, uniform);
701 }
702
703 void
704 qir_SF(struct vc4_compile *c, struct qreg src)
705 {
706         struct qinst *last_inst = NULL;
707
708         if (!list_empty(&c->cur_block->instructions))
709                 last_inst = (struct qinst *)c->cur_block->instructions.prev;
710
711         /* We don't have any way to guess which kind of MOV is implied. */
712         assert(!src.pack);
713
714         if (src.file != QFILE_TEMP ||
715             !c->defs[src.index] ||
716             last_inst != c->defs[src.index]) {
717                 last_inst = qir_MOV_dest(c, qir_reg(QFILE_NULL, 0), src);
718                 last_inst = (struct qinst *)c->cur_block->instructions.prev;
719         }
720         last_inst->sf = true;
721 }
722
723 #define OPTPASS(func)                                                   \
724         do {                                                            \
725                 bool stage_progress = func(c);                          \
726                 if (stage_progress) {                                   \
727                         progress = true;                                \
728                         if (print_opt_debug) {                          \
729                                 fprintf(stderr,                         \
730                                         "QIR opt pass %2d: %s progress\n", \
731                                         pass, #func);                   \
732                         }                                               \
733                         qir_validate(c);                                \
734                 }                                                       \
735         } while (0)
736
737 void
738 qir_optimize(struct vc4_compile *c)
739 {
740         bool print_opt_debug = false;
741         int pass = 1;
742
743         while (true) {
744                 bool progress = false;
745
746                 OPTPASS(qir_opt_algebraic);
747                 OPTPASS(qir_opt_constant_folding);
748                 OPTPASS(qir_opt_copy_propagation);
749                 OPTPASS(qir_opt_peephole_sf);
750                 OPTPASS(qir_opt_dead_code);
751                 OPTPASS(qir_opt_small_immediates);
752                 OPTPASS(qir_opt_vpm);
753
754                 if (!progress)
755                         break;
756
757                 pass++;
758         }
759 }