• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 v3d_opt_copy_propagation.c
26  *
27  * This implements simple copy propagation for VIR without control flow.
28  *
29  * For each temp, it keeps a qreg of which source it was MOVed from, if it
30  * was.  If we see that used later, we can just reuse the source value, since
31  * we know we don't have control flow, and we have SSA for our values so
32  * there's no killing to worry about.
33  */
34 
35 #include "v3d_compiler.h"
36 
37 static bool
is_copy_mov(struct qinst * inst)38 is_copy_mov(struct qinst *inst)
39 {
40         if (!inst)
41                 return false;
42 
43         if (inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU ||
44             (inst->qpu.alu.mul.op != V3D_QPU_M_FMOV &&
45              inst->qpu.alu.mul.op != V3D_QPU_M_MOV)) {
46                 return false;
47         }
48 
49         if (inst->dst.file != QFILE_TEMP)
50                 return false;
51 
52         if (inst->src[0].file != QFILE_TEMP &&
53             inst->src[0].file != QFILE_UNIF) {
54                 return false;
55         }
56 
57         if (inst->qpu.alu.add.output_pack != V3D_QPU_PACK_NONE ||
58             inst->qpu.alu.mul.output_pack != V3D_QPU_PACK_NONE) {
59                 return false;
60         }
61 
62         if (inst->qpu.flags.ac != V3D_QPU_COND_NONE ||
63             inst->qpu.flags.mc != V3D_QPU_COND_NONE) {
64                 return false;
65         }
66 
67         switch (inst->src[0].file) {
68         case QFILE_MAGIC:
69                 /* No copy propagating from R3/R4/R5 -- the MOVs from those
70                  * are there to register allocate values produced into R3/4/5
71                  * to other regs (though hopefully r3/4/5).
72                  */
73                 switch (inst->src[0].index) {
74                 case V3D_QPU_WADDR_R3:
75                 case V3D_QPU_WADDR_R4:
76                 case V3D_QPU_WADDR_R5:
77                         return false;
78                 default:
79                         break;
80                 }
81                 break;
82 
83         case QFILE_REG:
84                 switch (inst->src[0].index) {
85                 case 0:
86                 case 1:
87                 case 2:
88                         /* MOVs from rf0/1/2 are only to track the live
89                          * intervals for W/centroid W/Z.
90                          */
91                         return false;
92                 }
93                 break;
94 
95         default:
96                 break;
97         }
98 
99         return true;
100 }
101 
102 static bool
vir_has_unpack(struct qinst * inst,int chan)103 vir_has_unpack(struct qinst *inst, int chan)
104 {
105         assert(chan == 0 || chan == 1);
106 
107         if (vir_is_add(inst)) {
108                 if (chan == 0)
109                         return inst->qpu.alu.add.a_unpack != V3D_QPU_UNPACK_NONE;
110                 else
111                         return inst->qpu.alu.add.b_unpack != V3D_QPU_UNPACK_NONE;
112         } else {
113                 if (chan == 0)
114                         return inst->qpu.alu.mul.a_unpack != V3D_QPU_UNPACK_NONE;
115                 else
116                         return inst->qpu.alu.mul.b_unpack != V3D_QPU_UNPACK_NONE;
117         }
118 }
119 
120 static bool
try_copy_prop(struct v3d_compile * c,struct qinst * inst,struct qinst ** movs)121 try_copy_prop(struct v3d_compile *c, struct qinst *inst, struct qinst **movs)
122 {
123         bool debug = false;
124         bool progress = false;
125 
126         for (int i = 0; i < vir_get_nsrc(inst); i++) {
127                 if (inst->src[i].file != QFILE_TEMP)
128                         continue;
129 
130                 /* We have two ways of finding MOVs we can copy propagate
131                  * from.  One is if it's an SSA def: then we can reuse it from
132                  * any block in the program, as long as its source is also an
133                  * SSA def.  Alternatively, if it's in the "movs" array
134                  * tracked within the block, then we know the sources for it
135                  * haven't been changed since we saw the instruction within
136                  * our block.
137                  */
138                 struct qinst *mov = movs[inst->src[i].index];
139                 if (!mov) {
140                         if (!is_copy_mov(c->defs[inst->src[i].index]))
141                                 continue;
142                         mov = c->defs[inst->src[i].index];
143 
144                         if (mov->src[0].file == QFILE_TEMP &&
145                             !c->defs[mov->src[0].index])
146                                 continue;
147                 }
148 
149                 if (vir_has_unpack(mov, 0)) {
150                         /* Make sure that the meaning of the unpack
151                          * would be the same between the two
152                          * instructions.
153                          */
154                         if (vir_is_float_input(inst) !=
155                             vir_is_float_input(mov)) {
156                                 continue;
157                         }
158                         /* No composing the unpacks. */
159                         if (vir_has_unpack(inst, i))
160                             continue;
161                 }
162 
163                 if (debug) {
164                         fprintf(stderr, "Copy propagate: ");
165                         vir_dump_inst(c, inst);
166                         fprintf(stderr, "\n");
167                 }
168 
169                 inst->src[i] = mov->src[0];
170                 if (vir_has_unpack(mov, 0)) {
171                         enum v3d_qpu_input_unpack unpack = mov->qpu.alu.mul.a_unpack;
172 
173                         vir_set_unpack(inst, i, unpack);
174                 }
175 
176                 if (debug) {
177                         fprintf(stderr, "to: ");
178                         vir_dump_inst(c, inst);
179                         fprintf(stderr, "\n");
180                 }
181 
182                 progress = true;
183         }
184 
185         return progress;
186 }
187 
188 static void
apply_kills(struct v3d_compile * c,struct qinst ** movs,struct qinst * inst)189 apply_kills(struct v3d_compile *c, struct qinst **movs, struct qinst *inst)
190 {
191         if (inst->dst.file != QFILE_TEMP)
192                 return;
193 
194         for (int i = 0; i < c->num_temps; i++) {
195                 if (movs[i] &&
196                     (movs[i]->dst.index == inst->dst.index ||
197                      (movs[i]->src[0].file == QFILE_TEMP &&
198                       movs[i]->src[0].index == inst->dst.index))) {
199                         movs[i] = NULL;
200                 }
201         }
202 }
203 
204 bool
vir_opt_copy_propagate(struct v3d_compile * c)205 vir_opt_copy_propagate(struct v3d_compile *c)
206 {
207         bool progress = false;
208         struct qinst **movs;
209 
210         movs = ralloc_array(c, struct qinst *, c->num_temps);
211         if (!movs)
212                 return false;
213 
214         vir_for_each_block(block, c) {
215                 /* The MOVs array tracks only available movs within the
216                  * block.
217                  */
218                 memset(movs, 0, sizeof(struct qinst *) * c->num_temps);
219 
220                 vir_for_each_inst(inst, block) {
221                         progress = try_copy_prop(c, inst, movs) || progress;
222 
223                         apply_kills(c, movs, inst);
224 
225                         if (is_copy_mov(inst))
226                                 movs[inst->dst.index] = inst;
227                 }
228         }
229 
230         ralloc_free(movs);
231 
232         return progress;
233 }
234