i965/fs: Loop from 0 to inst->sources, not 0 to 3.
[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 == BRW_OPCODE_MOV) {
50 return inst->dst.equals(inst->src[0]);
51 }
52
53 return false;
54 }
55
56 static bool
57 is_coalesce_candidate(const fs_inst *inst, const int *virtual_grf_sizes)
58 {
59 if (inst->opcode != BRW_OPCODE_MOV ||
60 inst->is_partial_write() ||
61 inst->saturate ||
62 inst->src[0].file != GRF ||
63 inst->src[0].negate ||
64 inst->src[0].abs ||
65 !inst->src[0].is_contiguous() ||
66 inst->dst.file != GRF ||
67 inst->dst.type != inst->src[0].type) {
68 return false;
69 }
70
71 if (virtual_grf_sizes[inst->src[0].reg] >
72 virtual_grf_sizes[inst->dst.reg])
73 return false;
74
75 return true;
76 }
77
78 static bool
79 can_coalesce_vars(brw::fs_live_variables *live_intervals,
80 const exec_list *instructions, const fs_inst *inst,
81 int var_to, int var_from)
82 {
83 if (!live_intervals->vars_interfere(var_from, var_to))
84 return true;
85
86 /* We know that the live ranges of A (var_from) and B (var_to)
87 * interfere because of the ->vars_interfere() call above. If the end
88 * of B's live range is after the end of A's range, then we know two
89 * things:
90 * - the start of B's live range must be in A's live range (since we
91 * already know the two ranges interfere, this is the only remaining
92 * possibility)
93 * - the interference isn't of the form we're looking for (where B is
94 * entirely inside A)
95 */
96 if (live_intervals->end[var_to] > live_intervals->end[var_from])
97 return false;
98
99 int scan_ip = -1;
100
101 foreach_list(n, instructions) {
102 fs_inst *scan_inst = (fs_inst *)n;
103 scan_ip++;
104
105 if (scan_inst->is_control_flow())
106 return false;
107
108 if (scan_ip <= live_intervals->start[var_to])
109 continue;
110
111 if (scan_ip > live_intervals->end[var_to])
112 break;
113
114 if (scan_inst->dst.equals(inst->dst) ||
115 scan_inst->dst.equals(inst->src[0]))
116 return false;
117 }
118
119 return true;
120 }
121
122 bool
123 fs_visitor::register_coalesce()
124 {
125 bool progress = false;
126
127 calculate_live_intervals();
128
129 int src_size = 0;
130 int channels_remaining = 0;
131 int reg_from = -1, reg_to = -1;
132 int reg_to_offset[MAX_SAMPLER_MESSAGE_SIZE];
133 fs_inst *mov[MAX_SAMPLER_MESSAGE_SIZE];
134 int var_to[MAX_SAMPLER_MESSAGE_SIZE];
135 int var_from[MAX_SAMPLER_MESSAGE_SIZE];
136
137 foreach_list(node, &this->instructions) {
138 fs_inst *inst = (fs_inst *)node;
139
140 if (!is_coalesce_candidate(inst, virtual_grf_sizes))
141 continue;
142
143 if (is_nop_mov(inst)) {
144 inst->opcode = BRW_OPCODE_NOP;
145 progress = true;
146 continue;
147 }
148
149 if (reg_from != inst->src[0].reg) {
150 reg_from = inst->src[0].reg;
151
152 src_size = virtual_grf_sizes[inst->src[0].reg];
153 assert(src_size <= MAX_SAMPLER_MESSAGE_SIZE);
154
155 channels_remaining = src_size;
156 memset(mov, 0, sizeof(mov));
157
158 reg_to = inst->dst.reg;
159 }
160
161 if (reg_to != inst->dst.reg)
162 continue;
163
164 const int offset = inst->src[0].reg_offset;
165 reg_to_offset[offset] = inst->dst.reg_offset;
166 mov[offset] = inst;
167 channels_remaining--;
168
169 if (channels_remaining)
170 continue;
171
172 bool can_coalesce = true;
173 for (int i = 0; i < src_size; i++) {
174 var_to[i] = live_intervals->var_from_vgrf[reg_to] + reg_to_offset[i];
175 var_from[i] = live_intervals->var_from_vgrf[reg_from] + i;
176
177 if (!can_coalesce_vars(live_intervals, &instructions, inst,
178 var_to[i], var_from[i])) {
179 can_coalesce = false;
180 reg_from = -1;
181 break;
182 }
183 }
184
185 if (!can_coalesce)
186 continue;
187
188 progress = true;
189
190 for (int i = 0; i < src_size; i++) {
191 if (mov[i]) {
192 mov[i]->opcode = BRW_OPCODE_NOP;
193 mov[i]->conditional_mod = BRW_CONDITIONAL_NONE;
194 mov[i]->dst = reg_undef;
195 mov[i]->src[0] = reg_undef;
196 mov[i]->src[1] = reg_undef;
197 mov[i]->src[2] = reg_undef;
198 }
199 }
200
201 foreach_list(node, &this->instructions) {
202 fs_inst *scan_inst = (fs_inst *)node;
203
204 for (int i = 0; i < src_size; i++) {
205 if (mov[i]) {
206 if (scan_inst->dst.file == GRF &&
207 scan_inst->dst.reg == reg_from &&
208 scan_inst->dst.reg_offset == i) {
209 scan_inst->dst.reg = reg_to;
210 scan_inst->dst.reg_offset = reg_to_offset[i];
211 }
212 for (int j = 0; j < scan_inst->sources; j++) {
213 if (scan_inst->src[j].file == GRF &&
214 scan_inst->src[j].reg == reg_from &&
215 scan_inst->src[j].reg_offset == i) {
216 scan_inst->src[j].reg = reg_to;
217 scan_inst->src[j].reg_offset = reg_to_offset[i];
218 }
219 }
220 }
221 }
222 }
223
224 for (int i = 0; i < src_size; i++) {
225 live_intervals->start[var_to[i]] =
226 MIN2(live_intervals->start[var_to[i]],
227 live_intervals->start[var_from[i]]);
228 live_intervals->end[var_to[i]] =
229 MAX2(live_intervals->end[var_to[i]],
230 live_intervals->end[var_from[i]]);
231 }
232 reg_from = -1;
233 }
234
235 if (progress) {
236 foreach_list_safe(node, &this->instructions) {
237 fs_inst *inst = (fs_inst *)node;
238
239 if (inst->opcode == BRW_OPCODE_NOP) {
240 inst->remove();
241 }
242 }
243
244 invalidate_live_intervals();
245 }
246
247 return progress;
248 }