i965: Use MESA_FORMAT_B8G8R8X8_SRGB for RGB visuals
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_cse.cpp
1 /*
2 * Copyright © 2012, 2013, 2014 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_vec4.h"
25 #include "brw_vec4_live_variables.h"
26 #include "brw_cfg.h"
27
28 using namespace brw;
29
30 /** @file brw_vec4_cse.cpp
31 *
32 * Support for local common subexpression elimination.
33 *
34 * See Muchnick's Advanced Compiler Design and Implementation, section
35 * 13.1 (p378).
36 */
37
38 namespace {
39 struct aeb_entry : public exec_node {
40 /** The instruction that generates the expression value. */
41 vec4_instruction *generator;
42
43 /** The temporary where the value is stored. */
44 src_reg tmp;
45 };
46 }
47
48 static bool
49 is_expression(const vec4_instruction *const inst)
50 {
51 switch (inst->opcode) {
52 case BRW_OPCODE_MOV:
53 case BRW_OPCODE_SEL:
54 case BRW_OPCODE_NOT:
55 case BRW_OPCODE_AND:
56 case BRW_OPCODE_OR:
57 case BRW_OPCODE_XOR:
58 case BRW_OPCODE_SHR:
59 case BRW_OPCODE_SHL:
60 case BRW_OPCODE_ASR:
61 case BRW_OPCODE_CMP:
62 case BRW_OPCODE_CMPN:
63 case BRW_OPCODE_ADD:
64 case BRW_OPCODE_MUL:
65 case SHADER_OPCODE_MULH:
66 case BRW_OPCODE_FRC:
67 case BRW_OPCODE_RNDU:
68 case BRW_OPCODE_RNDD:
69 case BRW_OPCODE_RNDE:
70 case BRW_OPCODE_RNDZ:
71 case BRW_OPCODE_LINE:
72 case BRW_OPCODE_PLN:
73 case BRW_OPCODE_MAD:
74 case BRW_OPCODE_LRP:
75 case VEC4_OPCODE_UNPACK_UNIFORM:
76 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
77 case SHADER_OPCODE_BROADCAST:
78 return true;
79 case SHADER_OPCODE_RCP:
80 case SHADER_OPCODE_RSQ:
81 case SHADER_OPCODE_SQRT:
82 case SHADER_OPCODE_EXP2:
83 case SHADER_OPCODE_LOG2:
84 case SHADER_OPCODE_POW:
85 case SHADER_OPCODE_INT_QUOTIENT:
86 case SHADER_OPCODE_INT_REMAINDER:
87 case SHADER_OPCODE_SIN:
88 case SHADER_OPCODE_COS:
89 return inst->mlen == 0;
90 default:
91 return false;
92 }
93 }
94
95 static bool
96 operands_match(const vec4_instruction *a, const vec4_instruction *b)
97 {
98 const src_reg *xs = a->src;
99 const src_reg *ys = b->src;
100
101 if (a->opcode == BRW_OPCODE_MAD) {
102 return xs[0].equals(ys[0]) &&
103 ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
104 (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
105 } else if (!a->is_commutative()) {
106 return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
107 } else {
108 return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
109 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
110 }
111 }
112
113 static bool
114 instructions_match(vec4_instruction *a, vec4_instruction *b)
115 {
116 return a->opcode == b->opcode &&
117 a->saturate == b->saturate &&
118 a->predicate == b->predicate &&
119 a->predicate_inverse == b->predicate_inverse &&
120 a->conditional_mod == b->conditional_mod &&
121 a->flag_subreg == b->flag_subreg &&
122 a->dst.type == b->dst.type &&
123 a->offset == b->offset &&
124 a->mlen == b->mlen &&
125 a->base_mrf == b->base_mrf &&
126 a->header_size == b->header_size &&
127 a->shadow_compare == b->shadow_compare &&
128 a->dst.writemask == b->dst.writemask &&
129 a->force_writemask_all == b->force_writemask_all &&
130 a->regs_written == b->regs_written &&
131 operands_match(a, b);
132 }
133
134 bool
135 vec4_visitor::opt_cse_local(bblock_t *block)
136 {
137 bool progress = false;
138 exec_list aeb;
139
140 void *cse_ctx = ralloc_context(NULL);
141
142 int ip = block->start_ip;
143 foreach_inst_in_block (vec4_instruction, inst, block) {
144 /* Skip some cases. */
145 if (is_expression(inst) && !inst->predicate && inst->mlen == 0 &&
146 ((inst->dst.file != ARF && inst->dst.file != FIXED_GRF) ||
147 inst->dst.is_null()))
148 {
149 bool found = false;
150
151 foreach_in_list_use_after(aeb_entry, entry, &aeb) {
152 /* Match current instruction's expression against those in AEB. */
153 if (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
154 instructions_match(inst, entry->generator)) {
155 found = true;
156 progress = true;
157 break;
158 }
159 }
160
161 if (!found) {
162 if (inst->opcode != BRW_OPCODE_MOV ||
163 (inst->opcode == BRW_OPCODE_MOV &&
164 inst->src[0].file == IMM &&
165 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
166 /* Our first sighting of this expression. Create an entry. */
167 aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
168 entry->tmp = src_reg(); /* file will be BAD_FILE */
169 entry->generator = inst;
170 aeb.push_tail(entry);
171 }
172 } else {
173 /* This is at least our second sighting of this expression.
174 * If we don't have a temporary already, make one.
175 */
176 bool no_existing_temp = entry->tmp.file == BAD_FILE;
177 if (no_existing_temp && !entry->generator->dst.is_null()) {
178 entry->tmp = retype(src_reg(VGRF, alloc.allocate(
179 entry->generator->regs_written),
180 NULL), inst->dst.type);
181
182 for (unsigned i = 0; i < entry->generator->regs_written; ++i) {
183 vec4_instruction *copy = MOV(offset(entry->generator->dst, i),
184 offset(entry->tmp, i));
185 copy->force_writemask_all =
186 entry->generator->force_writemask_all;
187 entry->generator->insert_after(block, copy);
188 }
189
190 entry->generator->dst = dst_reg(entry->tmp);
191 }
192
193 /* dest <- temp */
194 if (!inst->dst.is_null()) {
195 assert(inst->dst.type == entry->tmp.type);
196
197 for (unsigned i = 0; i < inst->regs_written; ++i) {
198 vec4_instruction *copy = MOV(offset(inst->dst, i),
199 offset(entry->tmp, i));
200 copy->force_writemask_all = inst->force_writemask_all;
201 inst->insert_before(block, copy);
202 }
203 }
204
205 /* Set our iterator so that next time through the loop inst->next
206 * will get the instruction in the basic block after the one we've
207 * removed.
208 */
209 vec4_instruction *prev = (vec4_instruction *)inst->prev;
210
211 inst->remove(block);
212 inst = prev;
213 }
214 }
215
216 foreach_in_list_safe(aeb_entry, entry, &aeb) {
217 /* Kill all AEB entries that write a different value to or read from
218 * the flag register if we just wrote it.
219 */
220 if (inst->writes_flag()) {
221 if (entry->generator->reads_flag() ||
222 (entry->generator->writes_flag() &&
223 !instructions_match(inst, entry->generator))) {
224 entry->remove();
225 ralloc_free(entry);
226 continue;
227 }
228 }
229
230 for (int i = 0; i < 3; i++) {
231 src_reg *src = &entry->generator->src[i];
232
233 /* Kill all AEB entries that use the destination we just
234 * overwrote.
235 */
236 if (inst->dst.file == entry->generator->src[i].file &&
237 inst->dst.nr == entry->generator->src[i].nr) {
238 entry->remove();
239 ralloc_free(entry);
240 break;
241 }
242
243 /* Kill any AEB entries using registers that don't get reused any
244 * more -- a sure sign they'll fail operands_match().
245 */
246 if (src->file == VGRF) {
247 if (var_range_end(var_from_reg(alloc, *src), 4) < ip) {
248 entry->remove();
249 ralloc_free(entry);
250 break;
251 }
252 }
253 }
254 }
255
256 ip++;
257 }
258
259 ralloc_free(cse_ctx);
260
261 return progress;
262 }
263
264 bool
265 vec4_visitor::opt_cse()
266 {
267 bool progress = false;
268
269 calculate_live_intervals();
270
271 foreach_block (block, cfg) {
272 progress = opt_cse_local(block) || progress;
273 }
274
275 if (progress)
276 invalidate_live_intervals();
277
278 return progress;
279 }