i965: Use typed foreach_in_list_safe instead of foreach_list_safe.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_register_coalesce.cpp
1 /*
2 * Copyright © 2012 Intel Corporation
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 /** @file brw_fs_register_coalesce.cpp
25 *
26 * Implements register coalescing: Checks if the two registers involved in a
27 * raw move don't interfere, in which case they can both be stored in the same
28 * place and the MOV removed.
29 *
30 * To do this, all uses of the source of the MOV in the shader are replaced
31 * with the destination of the MOV. For example:
32 *
33 * add vgrf3:F, vgrf1:F, vgrf2:F
34 * mov vgrf4:F, vgrf3:F
35 * mul vgrf5:F, vgrf5:F, vgrf4:F
36 *
37 * becomes
38 *
39 * add vgrf4:F, vgrf1:F, vgrf2:F
40 * mul vgrf5:F, vgrf5:F, vgrf4:F
41 */
42
43 #include "brw_fs.h"
44 #include "brw_fs_live_variables.h"
45
46 static bool
47 is_nop_mov(const fs_inst *inst)
48 {
49 if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {
50 fs_reg dst = inst->dst;
51 for (int i = 0; i < inst->sources; i++) {
52 dst.reg_offset = i;
53 if (!dst.equals(inst->src[i])) {
54 return false;
55 }
56 }
57 return true;
58 } else if (inst->opcode == BRW_OPCODE_MOV) {
59 return inst->dst.equals(inst->src[0]);
60 }
61
62 return false;
63 }
64
65 static bool
66 is_copy_payload(const fs_inst *inst, int src_size)
67 {
68 if (src_size != inst->sources)
69 return false;
70
71 const int reg = inst->src[0].reg;
72 if (inst->src[0].reg_offset != 0)
73 return false;
74
75 for (int i = 1; i < inst->sources; i++) {
76 if (inst->src[i].reg != reg ||
77 inst->src[i].reg_offset != i) {
78 return false;
79 }
80 }
81 return true;
82 }
83
84 static bool
85 is_coalesce_candidate(const fs_inst *inst, const int *virtual_grf_sizes)
86 {
87 if ((inst->opcode != BRW_OPCODE_MOV &&
88 inst->opcode != SHADER_OPCODE_LOAD_PAYLOAD) ||
89 inst->is_partial_write() ||
90 inst->saturate ||
91 inst->src[0].file != GRF ||
92 inst->src[0].negate ||
93 inst->src[0].abs ||
94 !inst->src[0].is_contiguous() ||
95 inst->dst.file != GRF ||
96 inst->dst.type != inst->src[0].type) {
97 return false;
98 }
99
100 if (virtual_grf_sizes[inst->src[0].reg] >
101 virtual_grf_sizes[inst->dst.reg])
102 return false;
103
104 if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {
105 if (!is_copy_payload(inst, virtual_grf_sizes[inst->src[0].reg])) {
106 return false;
107 }
108 }
109
110 return true;
111 }
112
113 static bool
114 can_coalesce_vars(brw::fs_live_variables *live_intervals,
115 const exec_list *instructions, const fs_inst *inst,
116 int var_to, int var_from)
117 {
118 if (!live_intervals->vars_interfere(var_from, var_to))
119 return true;
120
121 /* We know that the live ranges of A (var_from) and B (var_to)
122 * interfere because of the ->vars_interfere() call above. If the end
123 * of B's live range is after the end of A's range, then we know two
124 * things:
125 * - the start of B's live range must be in A's live range (since we
126 * already know the two ranges interfere, this is the only remaining
127 * possibility)
128 * - the interference isn't of the form we're looking for (where B is
129 * entirely inside A)
130 */
131 if (live_intervals->end[var_to] > live_intervals->end[var_from])
132 return false;
133
134 int scan_ip = -1;
135
136 foreach_in_list(fs_inst, scan_inst, instructions) {
137 scan_ip++;
138
139 if (scan_inst->is_control_flow())
140 return false;
141
142 if (scan_ip <= live_intervals->start[var_to])
143 continue;
144
145 if (scan_ip > live_intervals->end[var_to])
146 break;
147
148 if (scan_inst->dst.equals(inst->dst) ||
149 scan_inst->dst.equals(inst->src[0]))
150 return false;
151 }
152
153 return true;
154 }
155
156 bool
157 fs_visitor::register_coalesce()
158 {
159 bool progress = false;
160
161 calculate_live_intervals();
162
163 int src_size = 0;
164 int channels_remaining = 0;
165 int reg_from = -1, reg_to = -1;
166 int reg_to_offset[MAX_SAMPLER_MESSAGE_SIZE];
167 fs_inst *mov[MAX_SAMPLER_MESSAGE_SIZE];
168 int var_to[MAX_SAMPLER_MESSAGE_SIZE];
169 int var_from[MAX_SAMPLER_MESSAGE_SIZE];
170
171 foreach_in_list(fs_inst, inst, &instructions) {
172 if (!is_coalesce_candidate(inst, virtual_grf_sizes))
173 continue;
174
175 if (is_nop_mov(inst)) {
176 inst->opcode = BRW_OPCODE_NOP;
177 progress = true;
178 continue;
179 }
180
181 if (reg_from != inst->src[0].reg) {
182 reg_from = inst->src[0].reg;
183
184 src_size = virtual_grf_sizes[inst->src[0].reg];
185 assert(src_size <= MAX_SAMPLER_MESSAGE_SIZE);
186
187 channels_remaining = src_size;
188 memset(mov, 0, sizeof(mov));
189
190 reg_to = inst->dst.reg;
191 }
192
193 if (reg_to != inst->dst.reg)
194 continue;
195
196 if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {
197 for (int i = 0; i < src_size; i++) {
198 reg_to_offset[i] = i;
199 }
200 mov[0] = inst;
201 channels_remaining -= inst->sources;
202 } else {
203 const int offset = inst->src[0].reg_offset;
204 reg_to_offset[offset] = inst->dst.reg_offset;
205 mov[offset] = inst;
206 channels_remaining--;
207 }
208
209 if (channels_remaining)
210 continue;
211
212 bool can_coalesce = true;
213 for (int i = 0; i < src_size; i++) {
214 var_to[i] = live_intervals->var_from_vgrf[reg_to] + reg_to_offset[i];
215 var_from[i] = live_intervals->var_from_vgrf[reg_from] + i;
216
217 if (!can_coalesce_vars(live_intervals, &instructions, inst,
218 var_to[i], var_from[i])) {
219 can_coalesce = false;
220 reg_from = -1;
221 break;
222 }
223 }
224
225 if (!can_coalesce)
226 continue;
227
228 progress = true;
229 bool was_load_payload = inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD;
230
231 for (int i = 0; i < src_size; i++) {
232 if (mov[i]) {
233 mov[i]->opcode = BRW_OPCODE_NOP;
234 mov[i]->conditional_mod = BRW_CONDITIONAL_NONE;
235 mov[i]->dst = reg_undef;
236 for (int j = 0; j < mov[i]->sources; j++) {
237 mov[i]->src[j] = reg_undef;
238 }
239 }
240 }
241
242 foreach_in_list(fs_inst, scan_inst, &instructions) {
243 for (int i = 0; i < src_size; i++) {
244 if (mov[i] || was_load_payload) {
245 if (scan_inst->dst.file == GRF &&
246 scan_inst->dst.reg == reg_from &&
247 scan_inst->dst.reg_offset == i) {
248 scan_inst->dst.reg = reg_to;
249 scan_inst->dst.reg_offset = reg_to_offset[i];
250 }
251 for (int j = 0; j < scan_inst->sources; j++) {
252 if (scan_inst->src[j].file == GRF &&
253 scan_inst->src[j].reg == reg_from &&
254 scan_inst->src[j].reg_offset == i) {
255 scan_inst->src[j].reg = reg_to;
256 scan_inst->src[j].reg_offset = reg_to_offset[i];
257 }
258 }
259 }
260 }
261 }
262
263 for (int i = 0; i < src_size; i++) {
264 live_intervals->start[var_to[i]] =
265 MIN2(live_intervals->start[var_to[i]],
266 live_intervals->start[var_from[i]]);
267 live_intervals->end[var_to[i]] =
268 MAX2(live_intervals->end[var_to[i]],
269 live_intervals->end[var_from[i]]);
270 }
271 reg_from = -1;
272 }
273
274 if (progress) {
275 foreach_in_list_safe(fs_inst, inst, &instructions) {
276 if (inst->opcode == BRW_OPCODE_NOP) {
277 inst->remove();
278 }
279 }
280
281 invalidate_live_intervals();
282 }
283
284 return progress;
285 }