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