vc4: Use the intrinsic's first_component for vattr VPM index.
[mesa.git] / src / gallium / drivers / vc4 / vc4_opt_vpm.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_vpm.c
26 *
27 * This modifies instructions that:
28 * 1. exclusively consume a value read from the VPM to directly read the VPM if
29 * other operands allow it.
30 * 2. generate the value consumed by a VPM write to write directly into the VPM.
31 */
32
33 #include "vc4_qir.h"
34
35 bool
36 qir_opt_vpm(struct vc4_compile *c)
37 {
38 if (c->stage == QSTAGE_FRAG)
39 return false;
40
41 /* For now, only do this pass when we don't have control flow. */
42 struct qblock *block = qir_entry_block(c);
43 if (block != qir_exit_block(c))
44 return false;
45
46 bool progress = false;
47 struct qinst *vpm_writes[64] = { 0 };
48 uint32_t use_count[c->num_temps];
49 uint32_t vpm_write_count = 0;
50 memset(&use_count, 0, sizeof(use_count));
51
52 qir_for_each_inst_inorder(inst, c) {
53 switch (inst->dst.file) {
54 case QFILE_VPM:
55 vpm_writes[vpm_write_count++] = inst;
56 break;
57 default:
58 break;
59 }
60
61 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
62 if (inst->src[i].file == QFILE_TEMP) {
63 uint32_t temp = inst->src[i].index;
64 use_count[temp]++;
65 }
66 }
67 }
68
69 /* For instructions reading from a temporary that contains a VPM read
70 * result, try to move the instruction up in place of the VPM read.
71 */
72 qir_for_each_inst_inorder(inst, c) {
73 if (!inst)
74 continue;
75
76 if (qir_depends_on_flags(inst) || inst->sf)
77 continue;
78
79 if (qir_has_side_effects(c, inst) ||
80 qir_has_side_effect_reads(c, inst) ||
81 qir_is_tex(inst))
82 continue;
83
84 for (int j = 0; j < qir_get_op_nsrc(inst->op); j++) {
85 if (inst->src[j].file != QFILE_TEMP ||
86 inst->src[j].pack)
87 continue;
88
89 uint32_t temp = inst->src[j].index;
90
91 /* Since VPM reads pull from a FIFO, we only get to
92 * read each VPM entry once (unless we reset the read
93 * pointer). That means we can't copy-propagate a VPM
94 * read to multiple locations.
95 */
96 if (use_count[temp] != 1)
97 continue;
98
99 struct qinst *mov = c->defs[temp];
100 if (!mov ||
101 (mov->op != QOP_MOV &&
102 mov->op != QOP_FMOV &&
103 mov->op != QOP_MMOV) ||
104 mov->src[0].file != QFILE_VPM) {
105 continue;
106 }
107
108 uint32_t temps = 0;
109 for (int k = 0; k < qir_get_op_nsrc(inst->op); k++) {
110 if (inst->src[k].file == QFILE_TEMP)
111 temps++;
112 }
113
114 /* The instruction is safe to reorder if its other
115 * sources are independent of previous instructions
116 */
117 if (temps == 1) {
118 inst->src[j] = mov->src[0];
119
120 list_del(&inst->link);
121 list_addtail(&inst->link, &mov->link);
122 qir_remove_instruction(c, mov);
123
124 progress = true;
125 break;
126 }
127 }
128 }
129
130 for (int i = 0; i < vpm_write_count; i++) {
131 if (!qir_is_raw_mov(vpm_writes[i]) ||
132 vpm_writes[i]->src[0].file != QFILE_TEMP) {
133 continue;
134 }
135
136 uint32_t temp = vpm_writes[i]->src[0].index;
137 if (use_count[temp] != 1)
138 continue;
139
140 struct qinst *inst = c->defs[temp];
141 if (!inst)
142 continue;
143
144 if (qir_depends_on_flags(inst) || inst->sf)
145 continue;
146
147 if (qir_has_side_effects(c, inst) ||
148 qir_has_side_effect_reads(c, inst)) {
149 continue;
150 }
151
152 /* Move the generating instruction to the end of the program
153 * to maintain the order of the VPM writes.
154 */
155 assert(!vpm_writes[i]->sf);
156 list_del(&inst->link);
157 list_addtail(&inst->link, &vpm_writes[i]->link);
158 qir_remove_instruction(c, vpm_writes[i]);
159
160 c->defs[inst->dst.index] = NULL;
161 inst->dst.file = QFILE_VPM;
162 inst->dst.index = 0;
163
164 progress = true;
165 }
166
167 return progress;
168 }