i965: Perform basic optimizations on the BROADCAST opcode.
[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 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 VEC4_OPCODE_UNPACK_UNIFORM:
75 case SHADER_OPCODE_BROADCAST:
76 return true;
77 case SHADER_OPCODE_RCP:
78 case SHADER_OPCODE_RSQ:
79 case SHADER_OPCODE_SQRT:
80 case SHADER_OPCODE_EXP2:
81 case SHADER_OPCODE_LOG2:
82 case SHADER_OPCODE_POW:
83 case SHADER_OPCODE_INT_QUOTIENT:
84 case SHADER_OPCODE_INT_REMAINDER:
85 case SHADER_OPCODE_SIN:
86 case SHADER_OPCODE_COS:
87 return inst->mlen == 0;
88 default:
89 return false;
90 }
91 }
92
93 static bool
94 operands_match(const vec4_instruction *a, const vec4_instruction *b)
95 {
96 const src_reg *xs = a->src;
97 const src_reg *ys = b->src;
98
99 if (a->opcode == BRW_OPCODE_MAD) {
100 return xs[0].equals(ys[0]) &&
101 ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
102 (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
103 } else if (!a->is_commutative()) {
104 return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
105 } else {
106 return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
107 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
108 }
109 }
110
111 static bool
112 instructions_match(vec4_instruction *a, vec4_instruction *b)
113 {
114 return a->opcode == b->opcode &&
115 a->saturate == b->saturate &&
116 a->conditional_mod == b->conditional_mod &&
117 a->dst.type == b->dst.type &&
118 a->dst.writemask == b->dst.writemask &&
119 a->force_writemask_all == b->force_writemask_all &&
120 a->regs_written == b->regs_written &&
121 operands_match(a, b);
122 }
123
124 bool
125 vec4_visitor::opt_cse_local(bblock_t *block)
126 {
127 bool progress = false;
128 exec_list aeb;
129
130 void *cse_ctx = ralloc_context(NULL);
131
132 int ip = block->start_ip;
133 foreach_inst_in_block (vec4_instruction, inst, block) {
134 /* Skip some cases. */
135 if (is_expression(inst) && !inst->predicate && inst->mlen == 0 &&
136 (inst->dst.file != HW_REG || inst->dst.is_null()))
137 {
138 bool found = false;
139
140 foreach_in_list_use_after(aeb_entry, entry, &aeb) {
141 /* Match current instruction's expression against those in AEB. */
142 if (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
143 instructions_match(inst, entry->generator)) {
144 found = true;
145 progress = true;
146 break;
147 }
148 }
149
150 if (!found) {
151 if (inst->opcode != BRW_OPCODE_MOV ||
152 (inst->opcode == BRW_OPCODE_MOV &&
153 inst->src[0].file == IMM &&
154 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
155 /* Our first sighting of this expression. Create an entry. */
156 aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
157 entry->tmp = src_reg(); /* file will be BAD_FILE */
158 entry->generator = inst;
159 aeb.push_tail(entry);
160 }
161 } else {
162 /* This is at least our second sighting of this expression.
163 * If we don't have a temporary already, make one.
164 */
165 bool no_existing_temp = entry->tmp.file == BAD_FILE;
166 if (no_existing_temp && !entry->generator->dst.is_null()) {
167 entry->tmp = retype(src_reg(GRF, alloc.allocate(
168 entry->generator->regs_written),
169 NULL), inst->dst.type);
170
171 for (unsigned i = 0; i < entry->generator->regs_written; ++i) {
172 vec4_instruction *copy = MOV(offset(entry->generator->dst, i),
173 offset(entry->tmp, i));
174 copy->force_writemask_all =
175 entry->generator->force_writemask_all;
176 entry->generator->insert_after(block, copy);
177 }
178
179 entry->generator->dst = dst_reg(entry->tmp);
180 }
181
182 /* dest <- temp */
183 if (!inst->dst.is_null()) {
184 assert(inst->dst.type == entry->tmp.type);
185
186 for (unsigned i = 0; i < inst->regs_written; ++i) {
187 vec4_instruction *copy = MOV(offset(inst->dst, i),
188 offset(entry->tmp, i));
189 copy->force_writemask_all = inst->force_writemask_all;
190 inst->insert_before(block, copy);
191 }
192 }
193
194 /* Set our iterator so that next time through the loop inst->next
195 * will get the instruction in the basic block after the one we've
196 * removed.
197 */
198 vec4_instruction *prev = (vec4_instruction *)inst->prev;
199
200 inst->remove(block);
201 inst = prev;
202 }
203 }
204
205 foreach_in_list_safe(aeb_entry, entry, &aeb) {
206 /* Kill all AEB entries that write a different value to or read from
207 * the flag register if we just wrote it.
208 */
209 if (inst->writes_flag()) {
210 if (entry->generator->reads_flag() ||
211 (entry->generator->writes_flag() &&
212 !instructions_match(inst, entry->generator))) {
213 entry->remove();
214 ralloc_free(entry);
215 continue;
216 }
217 }
218
219 for (int i = 0; i < 3; i++) {
220 src_reg *src = &entry->generator->src[i];
221
222 /* Kill all AEB entries that use the destination we just
223 * overwrote.
224 */
225 if (inst->dst.file == entry->generator->src[i].file &&
226 inst->dst.reg == entry->generator->src[i].reg) {
227 entry->remove();
228 ralloc_free(entry);
229 break;
230 }
231
232 /* Kill any AEB entries using registers that don't get reused any
233 * more -- a sure sign they'll fail operands_match().
234 */
235 if (src->file == GRF) {
236 if (var_range_end(var_from_reg(alloc, *src), 4) < ip) {
237 entry->remove();
238 ralloc_free(entry);
239 break;
240 }
241 }
242 }
243 }
244
245 ip++;
246 }
247
248 ralloc_free(cse_ctx);
249
250 return progress;
251 }
252
253 bool
254 vec4_visitor::opt_cse()
255 {
256 bool progress = false;
257
258 calculate_live_intervals();
259
260 foreach_block (block, cfg) {
261 progress = opt_cse_local(block) || progress;
262 }
263
264 if (progress)
265 invalidate_live_intervals();
266
267 return progress;
268 }