glsl/opt_cpe: Kill when the assignment isn't something we recognize.
[mesa.git] / src / glsl / opt_copy_propagation_elements.cpp
1 /*
2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file opt_copy_propagation_elements.cpp
26 *
27 * Replaces usage of recently-copied components of variables with the
28 * previous copy of the variable.
29 *
30 * This pass can be compared with opt_copy_propagation, which operands
31 * on arbitrary whole-variable copies. However, in order to handle
32 * the copy propagation of swizzled variables or writemasked writes,
33 * we want to track things on a channel-wise basis. I found that
34 * trying to mix the swizzled/writemasked support here with the
35 * whole-variable stuff in opt_copy_propagation.cpp just made a mess,
36 * so this is separate despite the ACP handling being somewhat
37 * similar.
38 *
39 * This should reduce the number of MOV instructions in the generated
40 * programs unless copy propagation is also done on the LIR, and may
41 * help anyway by triggering other optimizations that live in the HIR.
42 */
43
44 #include "ir.h"
45 #include "ir_rvalue_visitor.h"
46 #include "ir_basic_block.h"
47 #include "ir_optimization.h"
48 #include "glsl_types.h"
49
50 static bool debug = false;
51
52 class acp_entry : public exec_node
53 {
54 public:
55 acp_entry(ir_variable *lhs, ir_variable *rhs, int write_mask, int swizzle[4])
56 {
57 this->lhs = lhs;
58 this->rhs = rhs;
59 this->write_mask = write_mask;
60 memcpy(this->swizzle, swizzle, sizeof(this->swizzle));
61 }
62
63 acp_entry(acp_entry *a)
64 {
65 this->lhs = a->lhs;
66 this->rhs = a->rhs;
67 this->write_mask = a->write_mask;
68 memcpy(this->swizzle, a->swizzle, sizeof(this->swizzle));
69 }
70
71 ir_variable *lhs;
72 ir_variable *rhs;
73 unsigned int write_mask;
74 int swizzle[4];
75 };
76
77
78 class kill_entry : public exec_node
79 {
80 public:
81 kill_entry(ir_variable *var, int write_mask)
82 {
83 this->var = var;
84 this->write_mask = write_mask;
85 }
86
87 ir_variable *var;
88 unsigned int write_mask;
89 };
90
91 class ir_copy_propagation_elements_visitor : public ir_rvalue_visitor {
92 public:
93 ir_copy_propagation_elements_visitor()
94 {
95 this->progress = false;
96 this->mem_ctx = ralloc_context(NULL);
97 this->shader_mem_ctx = NULL;
98 this->acp = new(mem_ctx) exec_list;
99 this->kills = new(mem_ctx) exec_list;
100 }
101 ~ir_copy_propagation_elements_visitor()
102 {
103 ralloc_free(mem_ctx);
104 }
105
106 virtual ir_visitor_status visit_enter(class ir_loop *);
107 virtual ir_visitor_status visit_enter(class ir_function_signature *);
108 virtual ir_visitor_status visit_leave(class ir_assignment *);
109 virtual ir_visitor_status visit_enter(class ir_call *);
110 virtual ir_visitor_status visit_enter(class ir_if *);
111
112 void handle_rvalue(ir_rvalue **rvalue);
113
114 void add_copy(ir_assignment *ir);
115 void kill(kill_entry *k);
116 void handle_if_block(exec_list *instructions);
117
118 /** List of acp_entry: The available copies to propagate */
119 exec_list *acp;
120 /**
121 * List of kill_entry: The variables whose values were killed in this
122 * block.
123 */
124 exec_list *kills;
125
126 bool progress;
127
128 bool killed_all;
129
130 /* Context for our local data structures. */
131 void *mem_ctx;
132 /* Context for allocating new shader nodes. */
133 void *shader_mem_ctx;
134 };
135
136 ir_visitor_status
137 ir_copy_propagation_elements_visitor::visit_enter(ir_function_signature *ir)
138 {
139 /* Treat entry into a function signature as a completely separate
140 * block. Any instructions at global scope will be shuffled into
141 * main() at link time, so they're irrelevant to us.
142 */
143 exec_list *orig_acp = this->acp;
144 exec_list *orig_kills = this->kills;
145 bool orig_killed_all = this->killed_all;
146
147 this->acp = new(mem_ctx) exec_list;
148 this->kills = new(mem_ctx) exec_list;
149 this->killed_all = false;
150
151 visit_list_elements(this, &ir->body);
152
153 this->kills = orig_kills;
154 this->acp = orig_acp;
155 this->killed_all = orig_killed_all;
156
157 return visit_continue_with_parent;
158 }
159
160 ir_visitor_status
161 ir_copy_propagation_elements_visitor::visit_leave(ir_assignment *ir)
162 {
163 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
164 ir_variable *var = ir->lhs->variable_referenced();
165
166 if (var->type->is_scalar() || var->type->is_vector()) {
167 kill_entry *k;
168
169 if (lhs)
170 k = new(mem_ctx) kill_entry(var, ir->write_mask);
171 else
172 k = new(mem_ctx) kill_entry(var, ~0);
173
174 kill(k);
175 }
176
177 add_copy(ir);
178
179 return visit_continue;
180 }
181
182 /**
183 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
184 *
185 * This is where the actual copy propagation occurs. Note that the
186 * rewriting of ir_dereference means that the ir_dereference instance
187 * must not be shared by multiple IR operations!
188 */
189 void
190 ir_copy_propagation_elements_visitor::handle_rvalue(ir_rvalue **ir)
191 {
192 int swizzle_chan[4];
193 ir_dereference_variable *deref_var;
194 ir_variable *source[4] = {NULL, NULL, NULL, NULL};
195 int source_chan[4];
196 int chans;
197
198 if (!*ir)
199 return;
200
201 ir_swizzle *swizzle = (*ir)->as_swizzle();
202 if (swizzle) {
203 deref_var = swizzle->val->as_dereference_variable();
204 if (!deref_var)
205 return;
206
207 swizzle_chan[0] = swizzle->mask.x;
208 swizzle_chan[1] = swizzle->mask.y;
209 swizzle_chan[2] = swizzle->mask.z;
210 swizzle_chan[3] = swizzle->mask.w;
211 chans = swizzle->type->vector_elements;
212 } else {
213 deref_var = (*ir)->as_dereference_variable();
214 if (!deref_var)
215 return;
216
217 swizzle_chan[0] = 0;
218 swizzle_chan[1] = 1;
219 swizzle_chan[2] = 2;
220 swizzle_chan[3] = 3;
221 chans = deref_var->type->vector_elements;
222 }
223
224 if (this->in_assignee)
225 return;
226
227 ir_variable *var = deref_var->var;
228
229 /* Try to find ACP entries covering swizzle_chan[], hoping they're
230 * the same source variable.
231 */
232 foreach_iter(exec_list_iterator, iter, *this->acp) {
233 acp_entry *entry = (acp_entry *)iter.get();
234
235 if (var == entry->lhs) {
236 for (int c = 0; c < chans; c++) {
237 if (entry->write_mask & (1 << swizzle_chan[c])) {
238 source[c] = entry->rhs;
239 source_chan[c] = entry->swizzle[swizzle_chan[c]];
240 }
241 }
242 }
243 }
244
245 /* Make sure all channels are copying from the same source variable. */
246 if (!source[0])
247 return;
248 for (int c = 1; c < chans; c++) {
249 if (source[c] != source[0])
250 return;
251 }
252
253 if (!shader_mem_ctx)
254 shader_mem_ctx = ralloc_parent(deref_var);
255
256 if (debug) {
257 printf("Copy propagation from:\n");
258 (*ir)->print();
259 }
260
261 deref_var = new(shader_mem_ctx) ir_dereference_variable(source[0]);
262 *ir = new(shader_mem_ctx) ir_swizzle(deref_var,
263 source_chan[0],
264 source_chan[1],
265 source_chan[2],
266 source_chan[3],
267 chans);
268
269 if (debug) {
270 printf("to:\n");
271 (*ir)->print();
272 printf("\n");
273 }
274 }
275
276
277 ir_visitor_status
278 ir_copy_propagation_elements_visitor::visit_enter(ir_call *ir)
279 {
280 /* Do copy propagation on call parameters, but skip any out params */
281 exec_list_iterator sig_param_iter = ir->get_callee()->parameters.iterator();
282 foreach_iter(exec_list_iterator, iter, ir->actual_parameters) {
283 ir_variable *sig_param = (ir_variable *)sig_param_iter.get();
284 ir_instruction *ir = (ir_instruction *)iter.get();
285 if (sig_param->mode != ir_var_out && sig_param->mode != ir_var_inout) {
286 ir->accept(this);
287 }
288 sig_param_iter.next();
289 }
290
291 /* Since we're unlinked, we don't (necessarily) know the side effects of
292 * this call. So kill all copies.
293 */
294 acp->make_empty();
295 this->killed_all = true;
296
297 return visit_continue_with_parent;
298 }
299
300 void
301 ir_copy_propagation_elements_visitor::handle_if_block(exec_list *instructions)
302 {
303 exec_list *orig_acp = this->acp;
304 exec_list *orig_kills = this->kills;
305 bool orig_killed_all = this->killed_all;
306
307 this->acp = new(mem_ctx) exec_list;
308 this->kills = new(mem_ctx) exec_list;
309 this->killed_all = false;
310
311 /* Populate the initial acp with a copy of the original */
312 foreach_iter(exec_list_iterator, iter, *orig_acp) {
313 acp_entry *a = (acp_entry *)iter.get();
314 this->acp->push_tail(new(this->mem_ctx) acp_entry(a));
315 }
316
317 visit_list_elements(this, instructions);
318
319 if (this->killed_all) {
320 orig_acp->make_empty();
321 }
322
323 exec_list *new_kills = this->kills;
324 this->kills = orig_kills;
325 this->acp = orig_acp;
326 this->killed_all = this->killed_all || orig_killed_all;
327
328 /* Move the new kills into the parent block's list, removing them
329 * from the parent's ACP list in the process.
330 */
331 foreach_list_safe(node, new_kills) {
332 kill_entry *k = (kill_entry *)node;
333 kill(k);
334 }
335 }
336
337 ir_visitor_status
338 ir_copy_propagation_elements_visitor::visit_enter(ir_if *ir)
339 {
340 ir->condition->accept(this);
341
342 handle_if_block(&ir->then_instructions);
343 handle_if_block(&ir->else_instructions);
344
345 /* handle_if_block() already descended into the children. */
346 return visit_continue_with_parent;
347 }
348
349 ir_visitor_status
350 ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
351 {
352 exec_list *orig_acp = this->acp;
353 exec_list *orig_kills = this->kills;
354 bool orig_killed_all = this->killed_all;
355
356 /* FINISHME: For now, the initial acp for loops is totally empty.
357 * We could go through once, then go through again with the acp
358 * cloned minus the killed entries after the first run through.
359 */
360 this->acp = new(mem_ctx) exec_list;
361 this->kills = new(mem_ctx) exec_list;
362 this->killed_all = false;
363
364 visit_list_elements(this, &ir->body_instructions);
365
366 if (this->killed_all) {
367 orig_acp->make_empty();
368 }
369
370 exec_list *new_kills = this->kills;
371 this->kills = orig_kills;
372 this->acp = orig_acp;
373 this->killed_all = this->killed_all || orig_killed_all;
374
375 foreach_list_safe(node, new_kills) {
376 kill_entry *k = (kill_entry *)node;
377 kill(k);
378 }
379
380 /* already descended into the children. */
381 return visit_continue_with_parent;
382 }
383
384 /* Remove any entries currently in the ACP for this kill. */
385 void
386 ir_copy_propagation_elements_visitor::kill(kill_entry *k)
387 {
388 foreach_list_safe(node, acp) {
389 acp_entry *entry = (acp_entry *)node;
390
391 if (entry->lhs == k->var) {
392 entry->write_mask = entry->write_mask & ~k->write_mask;
393 if (entry->write_mask == 0)
394 entry->remove();
395 }
396 if (entry->rhs == k->var) {
397 entry->remove();
398 }
399 }
400
401 /* If we were on a list, remove ourselves before inserting */
402 if (k->next)
403 k->remove();
404
405 this->kills->push_tail(k);
406 }
407
408 /**
409 * Adds directly-copied channels between vector variables to the available
410 * copy propagation list.
411 */
412 void
413 ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
414 {
415 acp_entry *entry;
416 int orig_swizzle[4] = {0, 1, 2, 3};
417 int swizzle[4];
418
419 if (ir->condition)
420 return;
421
422 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
423 if (!lhs || !(lhs->type->is_scalar() || lhs->type->is_vector()))
424 return;
425
426 ir_dereference_variable *rhs = ir->rhs->as_dereference_variable();
427 if (!rhs) {
428 ir_swizzle *swiz = ir->rhs->as_swizzle();
429 if (!swiz)
430 return;
431
432 rhs = swiz->val->as_dereference_variable();
433 if (!rhs)
434 return;
435
436 orig_swizzle[0] = swiz->mask.x;
437 orig_swizzle[1] = swiz->mask.y;
438 orig_swizzle[2] = swiz->mask.z;
439 orig_swizzle[3] = swiz->mask.w;
440 }
441
442 /* Move the swizzle channels out to the positions they match in the
443 * destination. We don't want to have to rewrite the swizzle[]
444 * array every time we clear a bit of the write_mask.
445 */
446 int j = 0;
447 for (int i = 0; i < 4; i++) {
448 if (ir->write_mask & (1 << i))
449 swizzle[i] = orig_swizzle[j++];
450 }
451
452 entry = new(this->mem_ctx) acp_entry(lhs->var, rhs->var, ir->write_mask,
453 swizzle);
454 this->acp->push_tail(entry);
455 }
456
457 bool
458 do_copy_propagation_elements(exec_list *instructions)
459 {
460 ir_copy_propagation_elements_visitor v;
461
462 visit_list_elements(&v, instructions);
463
464 return v.progress;
465 }