8958e62180273ce6637a4a9c54d7c4a390051b03
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_cse.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 #include "brw_fs.h"
25 #include "brw_cfg.h"
26
27 /** @file brw_fs_cse.cpp
28 *
29 * Support for local common subexpression elimination.
30 *
31 * See Muchnick's Advanced Compiler Design and Implementation, section
32 * 13.1 (p378).
33 */
34
35 namespace {
36 struct aeb_entry : public exec_node {
37 /** The instruction that generates the expression value. */
38 fs_inst *generator;
39
40 /** The temporary where the value is stored. */
41 fs_reg tmp;
42 };
43 }
44
45 static bool
46 is_copy_payload(const fs_inst *inst)
47 {
48 const int reg = inst->src[0].reg;
49 if (inst->src[0].reg_offset != 0)
50 return false;
51
52 for (int i = 1; i < inst->sources; i++) {
53 if (inst->src[i].reg != reg ||
54 inst->src[i].reg_offset != i) {
55 return false;
56 }
57 }
58 return true;
59 }
60
61 static bool
62 is_expression(const fs_inst *const inst)
63 {
64 switch (inst->opcode) {
65 case BRW_OPCODE_MOV:
66 case BRW_OPCODE_SEL:
67 case BRW_OPCODE_NOT:
68 case BRW_OPCODE_AND:
69 case BRW_OPCODE_OR:
70 case BRW_OPCODE_XOR:
71 case BRW_OPCODE_SHR:
72 case BRW_OPCODE_SHL:
73 case BRW_OPCODE_ASR:
74 case BRW_OPCODE_CMP:
75 case BRW_OPCODE_CMPN:
76 case BRW_OPCODE_ADD:
77 case BRW_OPCODE_MUL:
78 case BRW_OPCODE_FRC:
79 case BRW_OPCODE_RNDU:
80 case BRW_OPCODE_RNDD:
81 case BRW_OPCODE_RNDE:
82 case BRW_OPCODE_RNDZ:
83 case BRW_OPCODE_LINE:
84 case BRW_OPCODE_PLN:
85 case BRW_OPCODE_MAD:
86 case BRW_OPCODE_LRP:
87 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
88 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
89 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
90 case FS_OPCODE_CINTERP:
91 case FS_OPCODE_LINTERP:
92 case SHADER_OPCODE_BROADCAST:
93 return true;
94 case SHADER_OPCODE_RCP:
95 case SHADER_OPCODE_RSQ:
96 case SHADER_OPCODE_SQRT:
97 case SHADER_OPCODE_EXP2:
98 case SHADER_OPCODE_LOG2:
99 case SHADER_OPCODE_POW:
100 case SHADER_OPCODE_INT_QUOTIENT:
101 case SHADER_OPCODE_INT_REMAINDER:
102 case SHADER_OPCODE_SIN:
103 case SHADER_OPCODE_COS:
104 return inst->mlen < 2;
105 case SHADER_OPCODE_LOAD_PAYLOAD:
106 return !is_copy_payload(inst);
107 default:
108 return inst->is_send_from_grf() && !inst->has_side_effects();
109 }
110 }
111
112 static bool
113 operands_match(const fs_inst *a, const fs_inst *b, bool *negate)
114 {
115 fs_reg *xs = a->src;
116 fs_reg *ys = b->src;
117
118 if (a->opcode == BRW_OPCODE_MAD) {
119 return xs[0].equals(ys[0]) &&
120 ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
121 (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
122 } else if (a->opcode == BRW_OPCODE_MUL && a->dst.type == BRW_REGISTER_TYPE_F) {
123 bool xs0_negate = xs[0].negate;
124 bool xs1_negate = xs[1].file == IMM ? xs[1].fixed_hw_reg.dw1.f < 0.0f
125 : xs[1].negate;
126 bool ys0_negate = ys[0].negate;
127 bool ys1_negate = ys[1].file == IMM ? ys[1].fixed_hw_reg.dw1.f < 0.0f
128 : ys[1].negate;
129 float xs1_imm = xs[1].fixed_hw_reg.dw1.f;
130 float ys1_imm = ys[1].fixed_hw_reg.dw1.f;
131
132 xs[0].negate = false;
133 xs[1].negate = false;
134 ys[0].negate = false;
135 ys[1].negate = false;
136 xs[1].fixed_hw_reg.dw1.f = fabsf(xs[1].fixed_hw_reg.dw1.f);
137 ys[1].fixed_hw_reg.dw1.f = fabsf(ys[1].fixed_hw_reg.dw1.f);
138
139 bool ret = (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
140 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
141
142 xs[0].negate = xs0_negate;
143 xs[1].negate = xs[1].file == IMM ? false : xs1_negate;
144 ys[0].negate = ys0_negate;
145 ys[1].negate = ys[1].file == IMM ? false : ys1_negate;
146 xs[1].fixed_hw_reg.dw1.f = xs1_imm;
147 ys[1].fixed_hw_reg.dw1.f = ys1_imm;
148
149 *negate = (xs0_negate != xs1_negate) != (ys0_negate != ys1_negate);
150 return ret;
151 } else if (!a->is_commutative()) {
152 bool match = true;
153 for (int i = 0; i < a->sources; i++) {
154 if (!xs[i].equals(ys[i])) {
155 match = false;
156 break;
157 }
158 }
159 return match;
160 } else {
161 return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
162 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
163 }
164 }
165
166 static bool
167 instructions_match(fs_inst *a, fs_inst *b, bool *negate)
168 {
169 return a->opcode == b->opcode &&
170 a->saturate == b->saturate &&
171 a->predicate == b->predicate &&
172 a->predicate_inverse == b->predicate_inverse &&
173 a->conditional_mod == b->conditional_mod &&
174 a->dst.type == b->dst.type &&
175 a->sources == b->sources &&
176 (a->is_tex() ? (a->offset == b->offset &&
177 a->mlen == b->mlen &&
178 a->regs_written == b->regs_written &&
179 a->base_mrf == b->base_mrf &&
180 a->eot == b->eot &&
181 a->header_present == b->header_present &&
182 a->shadow_compare == b->shadow_compare)
183 : true) &&
184 operands_match(a, b, negate);
185 }
186
187 bool
188 fs_visitor::opt_cse_local(bblock_t *block)
189 {
190 bool progress = false;
191 exec_list aeb;
192
193 void *cse_ctx = ralloc_context(NULL);
194
195 int ip = block->start_ip;
196 foreach_inst_in_block(fs_inst, inst, block) {
197 /* Skip some cases. */
198 if (is_expression(inst) && !inst->is_partial_write() &&
199 (inst->dst.file != HW_REG || inst->dst.is_null()))
200 {
201 bool found = false;
202 bool negate = false;
203
204 foreach_in_list_use_after(aeb_entry, entry, &aeb) {
205 /* Match current instruction's expression against those in AEB. */
206 if (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
207 instructions_match(inst, entry->generator, &negate)) {
208 found = true;
209 progress = true;
210 break;
211 }
212 }
213
214 if (!found) {
215 if (inst->opcode != BRW_OPCODE_MOV ||
216 (inst->opcode == BRW_OPCODE_MOV &&
217 inst->src[0].file == IMM &&
218 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
219 /* Our first sighting of this expression. Create an entry. */
220 aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
221 entry->tmp = reg_undef;
222 entry->generator = inst;
223 aeb.push_tail(entry);
224 }
225 } else {
226 /* This is at least our second sighting of this expression.
227 * If we don't have a temporary already, make one.
228 */
229 bool no_existing_temp = entry->tmp.file == BAD_FILE;
230 if (no_existing_temp && !entry->generator->dst.is_null()) {
231 int written = entry->generator->regs_written;
232 int dst_width = entry->generator->dst.width / 8;
233 assert(written % dst_width == 0);
234
235 fs_reg orig_dst = entry->generator->dst;
236 fs_reg tmp = fs_reg(GRF, alloc.allocate(written),
237 orig_dst.type, orig_dst.width);
238 entry->tmp = tmp;
239 entry->generator->dst = tmp;
240
241 fs_inst *copy;
242 if (written > dst_width) {
243 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written / dst_width);
244 for (int i = 0; i < written / dst_width; i++)
245 sources[i] = offset(tmp, i);
246 copy = LOAD_PAYLOAD(orig_dst, sources, written / dst_width);
247 } else {
248 copy = MOV(orig_dst, tmp);
249 copy->force_writemask_all =
250 entry->generator->force_writemask_all;
251 }
252 entry->generator->insert_after(block, copy);
253 }
254
255 /* dest <- temp */
256 if (!inst->dst.is_null()) {
257 int written = inst->regs_written;
258 int dst_width = inst->dst.width / 8;
259 assert(written == entry->generator->regs_written);
260 assert(dst_width == entry->generator->dst.width / 8);
261 assert(inst->dst.type == entry->tmp.type);
262 fs_reg dst = inst->dst;
263 fs_reg tmp = entry->tmp;
264 fs_inst *copy;
265 if (written > dst_width) {
266 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written / dst_width);
267 for (int i = 0; i < written / dst_width; i++)
268 sources[i] = offset(tmp, i);
269 copy = LOAD_PAYLOAD(dst, sources, written / dst_width);
270 } else {
271 copy = MOV(dst, tmp);
272 copy->force_writemask_all = inst->force_writemask_all;
273 copy->src[0].negate = negate;
274 }
275 inst->insert_before(block, copy);
276 }
277
278 /* Set our iterator so that next time through the loop inst->next
279 * will get the instruction in the basic block after the one we've
280 * removed.
281 */
282 fs_inst *prev = (fs_inst *)inst->prev;
283
284 inst->remove(block);
285 inst = prev;
286 }
287 }
288
289 foreach_in_list_safe(aeb_entry, entry, &aeb) {
290 /* Kill all AEB entries that write a different value to or read from
291 * the flag register if we just wrote it.
292 */
293 if (inst->writes_flag()) {
294 bool negate; /* dummy */
295 if (entry->generator->reads_flag() ||
296 (entry->generator->writes_flag() &&
297 !instructions_match(inst, entry->generator, &negate))) {
298 entry->remove();
299 ralloc_free(entry);
300 continue;
301 }
302 }
303
304 for (int i = 0; i < entry->generator->sources; i++) {
305 fs_reg *src_reg = &entry->generator->src[i];
306
307 /* Kill all AEB entries that use the destination we just
308 * overwrote.
309 */
310 if (inst->overwrites_reg(entry->generator->src[i])) {
311 entry->remove();
312 ralloc_free(entry);
313 break;
314 }
315
316 /* Kill any AEB entries using registers that don't get reused any
317 * more -- a sure sign they'll fail operands_match().
318 */
319 if (src_reg->file == GRF && virtual_grf_end[src_reg->reg] < ip) {
320 entry->remove();
321 ralloc_free(entry);
322 break;
323 }
324 }
325 }
326
327 ip++;
328 }
329
330 ralloc_free(cse_ctx);
331
332 return progress;
333 }
334
335 bool
336 fs_visitor::opt_cse()
337 {
338 bool progress = false;
339
340 calculate_live_intervals();
341
342 foreach_block (block, cfg) {
343 progress = opt_cse_local(block) || progress;
344 }
345
346 if (progress)
347 invalidate_live_intervals();
348
349 return progress;
350 }