glsl: Add bit_xor builder
[mesa.git] / src / compiler / 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 "compiler/glsl_types.h"
49
50 static bool debug = false;
51
52 namespace {
53
54 class acp_entry : public exec_node
55 {
56 public:
57 acp_entry(ir_variable *lhs, ir_variable *rhs, int write_mask, int swizzle[4])
58 {
59 this->lhs = lhs;
60 this->rhs = rhs;
61 this->write_mask = write_mask;
62 memcpy(this->swizzle, swizzle, sizeof(this->swizzle));
63 }
64
65 acp_entry(acp_entry *a)
66 {
67 this->lhs = a->lhs;
68 this->rhs = a->rhs;
69 this->write_mask = a->write_mask;
70 memcpy(this->swizzle, a->swizzle, sizeof(this->swizzle));
71 }
72
73 ir_variable *lhs;
74 ir_variable *rhs;
75 unsigned int write_mask;
76 int swizzle[4];
77 };
78
79
80 class kill_entry : public exec_node
81 {
82 public:
83 kill_entry(ir_variable *var, int write_mask)
84 {
85 this->var = var;
86 this->write_mask = write_mask;
87 }
88
89 ir_variable *var;
90 unsigned int write_mask;
91 };
92
93 class ir_copy_propagation_elements_visitor : public ir_rvalue_visitor {
94 public:
95 ir_copy_propagation_elements_visitor()
96 {
97 this->progress = false;
98 this->killed_all = false;
99 this->mem_ctx = ralloc_context(NULL);
100 this->shader_mem_ctx = NULL;
101 this->acp = new(mem_ctx) exec_list;
102 this->kills = new(mem_ctx) exec_list;
103 }
104 ~ir_copy_propagation_elements_visitor()
105 {
106 ralloc_free(mem_ctx);
107 }
108
109 void handle_loop(ir_loop *, bool keep_acp);
110 virtual ir_visitor_status visit_enter(class ir_loop *);
111 virtual ir_visitor_status visit_enter(class ir_function_signature *);
112 virtual ir_visitor_status visit_leave(class ir_assignment *);
113 virtual ir_visitor_status visit_enter(class ir_call *);
114 virtual ir_visitor_status visit_enter(class ir_if *);
115 virtual ir_visitor_status visit_leave(class ir_swizzle *);
116
117 void handle_rvalue(ir_rvalue **rvalue);
118
119 void add_copy(ir_assignment *ir);
120 void kill(kill_entry *k);
121 void handle_if_block(exec_list *instructions);
122
123 /** List of acp_entry: The available copies to propagate */
124 exec_list *acp;
125 /**
126 * List of kill_entry: The variables whose values were killed in this
127 * block.
128 */
129 exec_list *kills;
130
131 bool progress;
132
133 bool killed_all;
134
135 /* Context for our local data structures. */
136 void *mem_ctx;
137 /* Context for allocating new shader nodes. */
138 void *shader_mem_ctx;
139 };
140
141 } /* unnamed namespace */
142
143 ir_visitor_status
144 ir_copy_propagation_elements_visitor::visit_enter(ir_function_signature *ir)
145 {
146 /* Treat entry into a function signature as a completely separate
147 * block. Any instructions at global scope will be shuffled into
148 * main() at link time, so they're irrelevant to us.
149 */
150 exec_list *orig_acp = this->acp;
151 exec_list *orig_kills = this->kills;
152 bool orig_killed_all = this->killed_all;
153
154 this->acp = new(mem_ctx) exec_list;
155 this->kills = new(mem_ctx) exec_list;
156 this->killed_all = false;
157
158 visit_list_elements(this, &ir->body);
159
160 ralloc_free(this->acp);
161 ralloc_free(this->kills);
162
163 this->kills = orig_kills;
164 this->acp = orig_acp;
165 this->killed_all = orig_killed_all;
166
167 return visit_continue_with_parent;
168 }
169
170 ir_visitor_status
171 ir_copy_propagation_elements_visitor::visit_leave(ir_assignment *ir)
172 {
173 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
174 ir_variable *var = ir->lhs->variable_referenced();
175
176 if (var->type->is_scalar() || var->type->is_vector()) {
177 kill_entry *k;
178
179 if (lhs)
180 k = new(this->kills) kill_entry(var, ir->write_mask);
181 else
182 k = new(this->kills) kill_entry(var, ~0);
183
184 kill(k);
185 }
186
187 add_copy(ir);
188
189 return visit_continue;
190 }
191
192 ir_visitor_status
193 ir_copy_propagation_elements_visitor::visit_leave(ir_swizzle *)
194 {
195 /* Don't visit the values of swizzles since they are handled while
196 * visiting the swizzle itself.
197 */
198 return visit_continue;
199 }
200
201 /**
202 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
203 *
204 * This is where the actual copy propagation occurs. Note that the
205 * rewriting of ir_dereference means that the ir_dereference instance
206 * must not be shared by multiple IR operations!
207 */
208 void
209 ir_copy_propagation_elements_visitor::handle_rvalue(ir_rvalue **ir)
210 {
211 int swizzle_chan[4];
212 ir_dereference_variable *deref_var;
213 ir_variable *source[4] = {NULL, NULL, NULL, NULL};
214 int source_chan[4] = {0, 0, 0, 0};
215 int chans;
216 bool noop_swizzle = true;
217
218 if (!*ir)
219 return;
220
221 ir_swizzle *swizzle = (*ir)->as_swizzle();
222 if (swizzle) {
223 deref_var = swizzle->val->as_dereference_variable();
224 if (!deref_var)
225 return;
226
227 swizzle_chan[0] = swizzle->mask.x;
228 swizzle_chan[1] = swizzle->mask.y;
229 swizzle_chan[2] = swizzle->mask.z;
230 swizzle_chan[3] = swizzle->mask.w;
231 chans = swizzle->type->vector_elements;
232 } else {
233 deref_var = (*ir)->as_dereference_variable();
234 if (!deref_var)
235 return;
236
237 swizzle_chan[0] = 0;
238 swizzle_chan[1] = 1;
239 swizzle_chan[2] = 2;
240 swizzle_chan[3] = 3;
241 chans = deref_var->type->vector_elements;
242 }
243
244 if (this->in_assignee)
245 return;
246
247 ir_variable *var = deref_var->var;
248
249 /* Try to find ACP entries covering swizzle_chan[], hoping they're
250 * the same source variable.
251 */
252 foreach_in_list(acp_entry, entry, this->acp) {
253 if (var == entry->lhs) {
254 for (int c = 0; c < chans; c++) {
255 if (entry->write_mask & (1 << swizzle_chan[c])) {
256 source[c] = entry->rhs;
257 source_chan[c] = entry->swizzle[swizzle_chan[c]];
258
259 if (source_chan[c] != swizzle_chan[c])
260 noop_swizzle = false;
261 }
262 }
263 }
264 }
265
266 /* Make sure all channels are copying from the same source variable. */
267 if (!source[0])
268 return;
269 for (int c = 1; c < chans; c++) {
270 if (source[c] != source[0])
271 return;
272 }
273
274 if (!shader_mem_ctx)
275 shader_mem_ctx = ralloc_parent(deref_var);
276
277 /* Don't pointlessly replace the rvalue with itself (or a noop swizzle
278 * of itself, which would just be deleted by opt_noop_swizzle).
279 */
280 if (source[0] == var && noop_swizzle)
281 return;
282
283 if (debug) {
284 printf("Copy propagation from:\n");
285 (*ir)->print();
286 }
287
288 deref_var = new(shader_mem_ctx) ir_dereference_variable(source[0]);
289 *ir = new(shader_mem_ctx) ir_swizzle(deref_var,
290 source_chan[0],
291 source_chan[1],
292 source_chan[2],
293 source_chan[3],
294 chans);
295 progress = true;
296
297 if (debug) {
298 printf("to:\n");
299 (*ir)->print();
300 printf("\n");
301 }
302 }
303
304
305 ir_visitor_status
306 ir_copy_propagation_elements_visitor::visit_enter(ir_call *ir)
307 {
308 /* Do copy propagation on call parameters, but skip any out params */
309 foreach_two_lists(formal_node, &ir->callee->parameters,
310 actual_node, &ir->actual_parameters) {
311 ir_variable *sig_param = (ir_variable *) formal_node;
312 ir_rvalue *ir = (ir_rvalue *) actual_node;
313 if (sig_param->data.mode != ir_var_function_out
314 && sig_param->data.mode != ir_var_function_inout) {
315 ir->accept(this);
316 }
317 }
318
319 /* Since we're unlinked, we don't (necessarily) know the side effects of
320 * this call. So kill all copies.
321 */
322 acp->make_empty();
323 this->killed_all = true;
324
325 return visit_continue_with_parent;
326 }
327
328 void
329 ir_copy_propagation_elements_visitor::handle_if_block(exec_list *instructions)
330 {
331 exec_list *orig_acp = this->acp;
332 exec_list *orig_kills = this->kills;
333 bool orig_killed_all = this->killed_all;
334
335 this->acp = new(mem_ctx) exec_list;
336 this->kills = new(mem_ctx) exec_list;
337 this->killed_all = false;
338
339 /* Populate the initial acp with a copy of the original */
340 foreach_in_list(acp_entry, a, orig_acp) {
341 this->acp->push_tail(new(this->acp) acp_entry(a));
342 }
343
344 visit_list_elements(this, instructions);
345
346 if (this->killed_all) {
347 orig_acp->make_empty();
348 }
349
350 exec_list *new_kills = this->kills;
351 this->kills = orig_kills;
352 ralloc_free(this->acp);
353 this->acp = orig_acp;
354 this->killed_all = this->killed_all || orig_killed_all;
355
356 /* Move the new kills into the parent block's list, removing them
357 * from the parent's ACP list in the process.
358 */
359 foreach_in_list_safe(kill_entry, k, new_kills) {
360 kill(k);
361 }
362
363 ralloc_free(new_kills);
364 }
365
366 ir_visitor_status
367 ir_copy_propagation_elements_visitor::visit_enter(ir_if *ir)
368 {
369 ir->condition->accept(this);
370
371 handle_if_block(&ir->then_instructions);
372 handle_if_block(&ir->else_instructions);
373
374 /* handle_if_block() already descended into the children. */
375 return visit_continue_with_parent;
376 }
377
378 void
379 ir_copy_propagation_elements_visitor::handle_loop(ir_loop *ir, bool keep_acp)
380 {
381 exec_list *orig_acp = this->acp;
382 exec_list *orig_kills = this->kills;
383 bool orig_killed_all = this->killed_all;
384
385 /* FINISHME: For now, the initial acp for loops is totally empty.
386 * We could go through once, then go through again with the acp
387 * cloned minus the killed entries after the first run through.
388 */
389 this->acp = new(mem_ctx) exec_list;
390 this->kills = new(mem_ctx) exec_list;
391 this->killed_all = false;
392
393 if (keep_acp) {
394 /* Populate the initial acp with a copy of the original */
395 foreach_in_list(acp_entry, a, orig_acp) {
396 this->acp->push_tail(new(this->acp) acp_entry(a));
397 }
398 }
399
400 visit_list_elements(this, &ir->body_instructions);
401
402 if (this->killed_all) {
403 orig_acp->make_empty();
404 }
405
406 exec_list *new_kills = this->kills;
407 this->kills = orig_kills;
408 ralloc_free(this->acp);
409 this->acp = orig_acp;
410 this->killed_all = this->killed_all || orig_killed_all;
411
412 foreach_in_list_safe(kill_entry, k, new_kills) {
413 kill(k);
414 }
415
416 ralloc_free(new_kills);
417 }
418
419 ir_visitor_status
420 ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
421 {
422 handle_loop(ir, false);
423 handle_loop(ir, true);
424
425 /* already descended into the children. */
426 return visit_continue_with_parent;
427 }
428
429 /* Remove any entries currently in the ACP for this kill. */
430 void
431 ir_copy_propagation_elements_visitor::kill(kill_entry *k)
432 {
433 foreach_in_list_safe(acp_entry, entry, acp) {
434 if (entry->lhs == k->var) {
435 entry->write_mask = entry->write_mask & ~k->write_mask;
436 if (entry->write_mask == 0) {
437 entry->remove();
438 continue;
439 }
440 }
441 if (entry->rhs == k->var) {
442 entry->remove();
443 }
444 }
445
446 /* If we were on a list, remove ourselves before inserting */
447 if (k->next)
448 k->remove();
449
450 ralloc_steal(this->kills, k);
451 this->kills->push_tail(k);
452 }
453
454 /**
455 * Adds directly-copied channels between vector variables to the available
456 * copy propagation list.
457 */
458 void
459 ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
460 {
461 acp_entry *entry;
462 int orig_swizzle[4] = {0, 1, 2, 3};
463 int swizzle[4];
464
465 if (ir->condition)
466 return;
467
468 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
469 if (!lhs || !(lhs->type->is_scalar() || lhs->type->is_vector()))
470 return;
471
472 ir_dereference_variable *rhs = ir->rhs->as_dereference_variable();
473 if (!rhs) {
474 ir_swizzle *swiz = ir->rhs->as_swizzle();
475 if (!swiz)
476 return;
477
478 rhs = swiz->val->as_dereference_variable();
479 if (!rhs)
480 return;
481
482 orig_swizzle[0] = swiz->mask.x;
483 orig_swizzle[1] = swiz->mask.y;
484 orig_swizzle[2] = swiz->mask.z;
485 orig_swizzle[3] = swiz->mask.w;
486 }
487
488 /* Move the swizzle channels out to the positions they match in the
489 * destination. We don't want to have to rewrite the swizzle[]
490 * array every time we clear a bit of the write_mask.
491 */
492 int j = 0;
493 for (int i = 0; i < 4; i++) {
494 if (ir->write_mask & (1 << i))
495 swizzle[i] = orig_swizzle[j++];
496 }
497
498 int write_mask = ir->write_mask;
499 if (lhs->var == rhs->var) {
500 /* If this is a copy from the variable to itself, then we need
501 * to be sure not to include the updated channels from this
502 * instruction in the set of new source channels to be
503 * copy-propagated from.
504 */
505 for (int i = 0; i < 4; i++) {
506 if (ir->write_mask & (1 << orig_swizzle[i]))
507 write_mask &= ~(1 << i);
508 }
509 }
510
511 if (lhs->var->data.precise != rhs->var->data.precise)
512 return;
513
514 entry = new(this->mem_ctx) acp_entry(lhs->var, rhs->var, write_mask,
515 swizzle);
516 this->acp->push_tail(entry);
517 }
518
519 bool
520 do_copy_propagation_elements(exec_list *instructions)
521 {
522 ir_copy_propagation_elements_visitor v;
523
524 visit_list_elements(&v, instructions);
525
526 return v.progress;
527 }