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