vc4: Split optimizing VPM writes from VPM reads.
[mesa.git] / src / gallium / drivers / vc4 / vc4_opt_coalesce_ff_writes.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 /**
25 * @file vc4_opt_coalesce_ff_writes.c
26 *
27 * This modifies instructions that generate the value consumed by a VPM write
28 * to write directly into the VPM.
29 */
30
31 #include "vc4_qir.h"
32
33 bool
34 qir_opt_coalesce_ff_writes(struct vc4_compile *c)
35 {
36 if (c->stage == QSTAGE_FRAG)
37 return false;
38
39 /* For now, only do this pass when we don't have control flow. */
40 struct qblock *block = qir_entry_block(c);
41 if (block != qir_exit_block(c))
42 return false;
43
44 bool progress = false;
45 struct qinst *vpm_writes[64] = { 0 };
46 uint32_t use_count[c->num_temps];
47 uint32_t vpm_write_count = 0;
48 memset(&use_count, 0, sizeof(use_count));
49
50 qir_for_each_inst_inorder(inst, c) {
51 switch (inst->dst.file) {
52 case QFILE_VPM:
53 vpm_writes[vpm_write_count++] = inst;
54 break;
55 default:
56 break;
57 }
58
59 for (int i = 0; i < qir_get_nsrc(inst); i++) {
60 if (inst->src[i].file == QFILE_TEMP) {
61 uint32_t temp = inst->src[i].index;
62 use_count[temp]++;
63 }
64 }
65 }
66
67 for (int i = 0; i < vpm_write_count; i++) {
68 if (!qir_is_raw_mov(vpm_writes[i]) ||
69 vpm_writes[i]->src[0].file != QFILE_TEMP) {
70 continue;
71 }
72
73 uint32_t temp = vpm_writes[i]->src[0].index;
74 if (use_count[temp] != 1)
75 continue;
76
77 struct qinst *inst = c->defs[temp];
78 if (!inst)
79 continue;
80
81 if (qir_depends_on_flags(inst) || inst->sf)
82 continue;
83
84 if (qir_has_side_effects(c, inst) ||
85 qir_has_side_effect_reads(c, inst)) {
86 continue;
87 }
88
89 /* Move the generating instruction to the end of the program
90 * to maintain the order of the VPM writes.
91 */
92 assert(!vpm_writes[i]->sf);
93 list_del(&inst->link);
94 list_addtail(&inst->link, &vpm_writes[i]->link);
95 qir_remove_instruction(c, vpm_writes[i]);
96
97 c->defs[inst->dst.index] = NULL;
98 inst->dst.file = QFILE_VPM;
99 inst->dst.index = 0;
100
101 progress = true;
102 }
103
104 return progress;
105 }