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