i965: Put '_default_' in the name of functions that set default state.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_copy_propagation.cpp
1 /*
2 * Copyright © 2012 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 /** @file brw_fs_copy_propagation.cpp
25 *
26 * Support for global copy propagation in two passes: A local pass that does
27 * intra-block copy (and constant) propagation, and a global pass that uses
28 * dataflow analysis on the copies available at the end of each block to re-do
29 * local copy propagation with more copies available.
30 *
31 * See Muchnick's Advanced Compiler Design and Implementation, section
32 * 12.5 (p356).
33 */
34
35 #define ACP_HASH_SIZE 16
36
37 #include "main/bitset.h"
38 #include "brw_fs.h"
39 #include "brw_cfg.h"
40
41 namespace { /* avoid conflict with opt_copy_propagation_elements */
42 struct acp_entry : public exec_node {
43 fs_reg dst;
44 fs_reg src;
45 };
46
47 struct block_data {
48 /**
49 * Which entries in the fs_copy_prop_dataflow acp table are live at the
50 * start of this block. This is the useful output of the analysis, since
51 * it lets us plug those into the local copy propagation on the second
52 * pass.
53 */
54 BITSET_WORD *livein;
55
56 /**
57 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
58 * of this block. This is done in initial setup from the per-block acps
59 * returned by the first local copy prop pass.
60 */
61 BITSET_WORD *liveout;
62
63 /**
64 * Which entries in the fs_copy_prop_dataflow acp table are generated by
65 * instructions in this block which reach the end of the block without
66 * being killed.
67 */
68 BITSET_WORD *copy;
69
70 /**
71 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
72 * course of this block.
73 */
74 BITSET_WORD *kill;
75 };
76
77 class fs_copy_prop_dataflow
78 {
79 public:
80 fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
81 exec_list *out_acp[ACP_HASH_SIZE]);
82
83 void setup_initial_values();
84 void run();
85
86 void dump_block_data() const;
87
88 void *mem_ctx;
89 cfg_t *cfg;
90
91 acp_entry **acp;
92 int num_acp;
93 int bitset_words;
94
95 struct block_data *bd;
96 };
97 } /* anonymous namespace */
98
99 fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
100 exec_list *out_acp[ACP_HASH_SIZE])
101 : mem_ctx(mem_ctx), cfg(cfg)
102 {
103 bd = rzalloc_array(mem_ctx, struct block_data, cfg->num_blocks);
104
105 num_acp = 0;
106 for (int b = 0; b < cfg->num_blocks; b++) {
107 for (int i = 0; i < ACP_HASH_SIZE; i++) {
108 foreach_list(entry_node, &out_acp[b][i]) {
109 num_acp++;
110 }
111 }
112 }
113
114 acp = rzalloc_array(mem_ctx, struct acp_entry *, num_acp);
115
116 bitset_words = BITSET_WORDS(num_acp);
117
118 int next_acp = 0;
119 for (int b = 0; b < cfg->num_blocks; b++) {
120 bd[b].livein = rzalloc_array(bd, BITSET_WORD, bitset_words);
121 bd[b].liveout = rzalloc_array(bd, BITSET_WORD, bitset_words);
122 bd[b].copy = rzalloc_array(bd, BITSET_WORD, bitset_words);
123 bd[b].kill = rzalloc_array(bd, BITSET_WORD, bitset_words);
124
125 for (int i = 0; i < ACP_HASH_SIZE; i++) {
126 foreach_list(entry_node, &out_acp[b][i]) {
127 acp_entry *entry = (acp_entry *)entry_node;
128
129 acp[next_acp] = entry;
130
131 /* opt_copy_propagate_local populates out_acp with copies created
132 * in a block which are still live at the end of the block. This
133 * is exactly what we want in the COPY set.
134 */
135 BITSET_SET(bd[b].copy, next_acp);
136
137 next_acp++;
138 }
139 }
140 }
141
142 assert(next_acp == num_acp);
143
144 setup_initial_values();
145 run();
146 }
147
148 /**
149 * Set up initial values for each of the data flow sets, prior to running
150 * the fixed-point algorithm.
151 */
152 void
153 fs_copy_prop_dataflow::setup_initial_values()
154 {
155 /* Initialize the COPY and KILL sets. */
156 for (int b = 0; b < cfg->num_blocks; b++) {
157 bblock_t *block = cfg->blocks[b];
158
159 for (fs_inst *inst = (fs_inst *)block->start;
160 inst != block->end->next;
161 inst = (fs_inst *)inst->next) {
162 if (inst->dst.file != GRF)
163 continue;
164
165 /* Mark ACP entries which are killed by this instruction. */
166 for (int i = 0; i < num_acp; i++) {
167 if (inst->overwrites_reg(acp[i]->dst) ||
168 inst->overwrites_reg(acp[i]->src)) {
169 BITSET_SET(bd[b].kill, i);
170 }
171 }
172 }
173 }
174
175 /* Populate the initial values for the livein and liveout sets. For the
176 * block at the start of the program, livein = 0 and liveout = copy.
177 * For the others, set liveout to 0 (the empty set) and livein to ~0
178 * (the universal set).
179 */
180 for (int b = 0; b < cfg->num_blocks; b++) {
181 bblock_t *block = cfg->blocks[b];
182 if (block->parents.is_empty()) {
183 for (int i = 0; i < bitset_words; i++) {
184 bd[b].livein[i] = 0u;
185 bd[b].liveout[i] = bd[b].copy[i];
186 }
187 } else {
188 for (int i = 0; i < bitset_words; i++) {
189 bd[b].liveout[i] = 0u;
190 bd[b].livein[i] = ~0u;
191 }
192 }
193 }
194 }
195
196 /**
197 * Walk the set of instructions in the block, marking which entries in the acp
198 * are killed by the block.
199 */
200 void
201 fs_copy_prop_dataflow::run()
202 {
203 bool progress;
204
205 do {
206 progress = false;
207
208 /* Update liveout for all blocks. */
209 for (int b = 0; b < cfg->num_blocks; b++) {
210 if (cfg->blocks[b]->parents.is_empty())
211 continue;
212
213 for (int i = 0; i < bitset_words; i++) {
214 const BITSET_WORD old_liveout = bd[b].liveout[i];
215
216 bd[b].liveout[i] =
217 bd[b].copy[i] | (bd[b].livein[i] & ~bd[b].kill[i]);
218
219 if (old_liveout != bd[b].liveout[i])
220 progress = true;
221 }
222 }
223
224 /* Update livein for all blocks. If a copy is live out of all parent
225 * blocks, it's live coming in to this block.
226 */
227 for (int b = 0; b < cfg->num_blocks; b++) {
228 if (cfg->blocks[b]->parents.is_empty())
229 continue;
230
231 for (int i = 0; i < bitset_words; i++) {
232 const BITSET_WORD old_livein = bd[b].livein[i];
233
234 bd[b].livein[i] = ~0u;
235 foreach_list_typed(bblock_link, link, link, &cfg->blocks[b]->parents) {
236 bblock_t *block = link->block;
237 bd[b].livein[i] &= bd[block->block_num].liveout[i];
238 }
239
240 if (old_livein != bd[b].livein[i])
241 progress = true;
242 }
243 }
244 } while (progress);
245 }
246
247 void
248 fs_copy_prop_dataflow::dump_block_data() const
249 {
250 for (int b = 0; b < cfg->num_blocks; b++) {
251 bblock_t *block = cfg->blocks[b];
252 fprintf(stderr, "Block %d [%d, %d] (parents ", block->block_num,
253 block->start_ip, block->end_ip);
254 foreach_list_typed(bblock_link, link, link, &block->parents) {
255 bblock_t *parent = link->block;
256 fprintf(stderr, "%d ", parent->block_num);
257 }
258 fprintf(stderr, "):\n");
259 fprintf(stderr, " livein = 0x");
260 for (int i = 0; i < bitset_words; i++)
261 fprintf(stderr, "%08x", bd[b].livein[i]);
262 fprintf(stderr, ", liveout = 0x");
263 for (int i = 0; i < bitset_words; i++)
264 fprintf(stderr, "%08x", bd[b].liveout[i]);
265 fprintf(stderr, ",\n copy = 0x");
266 for (int i = 0; i < bitset_words; i++)
267 fprintf(stderr, "%08x", bd[b].copy[i]);
268 fprintf(stderr, ", kill = 0x");
269 for (int i = 0; i < bitset_words; i++)
270 fprintf(stderr, "%08x", bd[b].kill[i]);
271 fprintf(stderr, "\n");
272 }
273 }
274
275 bool
276 fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)
277 {
278 if (entry->src.file == IMM)
279 return false;
280
281 /* Bail if inst is reading more than entry is writing. */
282 if ((inst->regs_read(this, arg) * inst->src[arg].stride *
283 type_sz(inst->src[arg].type)) > type_sz(entry->dst.type))
284 return false;
285
286 if (inst->src[arg].file != entry->dst.file ||
287 inst->src[arg].reg != entry->dst.reg ||
288 inst->src[arg].reg_offset != entry->dst.reg_offset ||
289 inst->src[arg].subreg_offset != entry->dst.subreg_offset) {
290 return false;
291 }
292
293 /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
294 if (inst->conditional_mod &&
295 inst->src[arg].type == BRW_REGISTER_TYPE_UD &&
296 entry->src.negate)
297 return false;
298
299 bool has_source_modifiers = entry->src.abs || entry->src.negate;
300
301 if ((has_source_modifiers || entry->src.file == UNIFORM ||
302 !entry->src.is_contiguous()) &&
303 !can_do_source_mods(inst))
304 return false;
305
306 /* Bail if the result of composing both strides would exceed the
307 * hardware limit.
308 */
309 if (entry->src.stride * inst->src[arg].stride > 4)
310 return false;
311
312 /* Bail if the result of composing both strides cannot be expressed
313 * as another stride. This avoids, for example, trying to transform
314 * this:
315 *
316 * MOV (8) rX<1>UD rY<0;1,0>UD
317 * FOO (8) ... rX<8;8,1>UW
318 *
319 * into this:
320 *
321 * FOO (8) ... rY<0;1,0>UW
322 *
323 * Which would have different semantics.
324 */
325 if (entry->src.stride != 1 &&
326 (inst->src[arg].stride *
327 type_sz(inst->src[arg].type)) % type_sz(entry->src.type) != 0)
328 return false;
329
330 if (has_source_modifiers && entry->dst.type != inst->src[arg].type)
331 return false;
332
333 inst->src[arg].file = entry->src.file;
334 inst->src[arg].reg = entry->src.reg;
335 inst->src[arg].reg_offset = entry->src.reg_offset;
336 inst->src[arg].subreg_offset = entry->src.subreg_offset;
337 inst->src[arg].stride *= entry->src.stride;
338
339 if (!inst->src[arg].abs) {
340 inst->src[arg].abs = entry->src.abs;
341 inst->src[arg].negate ^= entry->src.negate;
342 }
343
344 return true;
345 }
346
347
348 bool
349 fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry)
350 {
351 bool progress = false;
352
353 if (entry->src.file != IMM)
354 return false;
355
356 for (int i = inst->sources - 1; i >= 0; i--) {
357 if (inst->src[i].file != entry->dst.file ||
358 inst->src[i].reg != entry->dst.reg ||
359 inst->src[i].reg_offset != entry->dst.reg_offset ||
360 inst->src[i].subreg_offset != entry->dst.subreg_offset ||
361 inst->src[i].type != entry->dst.type ||
362 inst->src[i].stride > 1)
363 continue;
364
365 /* Don't bother with cases that should have been taken care of by the
366 * GLSL compiler's constant folding pass.
367 */
368 if (inst->src[i].negate || inst->src[i].abs)
369 continue;
370
371 switch (inst->opcode) {
372 case BRW_OPCODE_MOV:
373 inst->src[i] = entry->src;
374 progress = true;
375 break;
376
377 case BRW_OPCODE_BFI1:
378 case BRW_OPCODE_ASR:
379 case BRW_OPCODE_SHL:
380 case BRW_OPCODE_SHR:
381 case BRW_OPCODE_SUBB:
382 if (i == 1) {
383 inst->src[i] = entry->src;
384 progress = true;
385 }
386 break;
387
388 case BRW_OPCODE_MACH:
389 case BRW_OPCODE_MUL:
390 case BRW_OPCODE_ADD:
391 case BRW_OPCODE_OR:
392 case BRW_OPCODE_AND:
393 case BRW_OPCODE_XOR:
394 case BRW_OPCODE_ADDC:
395 if (i == 1) {
396 inst->src[i] = entry->src;
397 progress = true;
398 } else if (i == 0 && inst->src[1].file != IMM) {
399 /* Fit this constant in by commuting the operands.
400 * Exception: we can't do this for 32-bit integer MUL/MACH
401 * because it's asymmetric.
402 */
403 if ((inst->opcode == BRW_OPCODE_MUL ||
404 inst->opcode == BRW_OPCODE_MACH) &&
405 (inst->src[1].type == BRW_REGISTER_TYPE_D ||
406 inst->src[1].type == BRW_REGISTER_TYPE_UD))
407 break;
408 inst->src[0] = inst->src[1];
409 inst->src[1] = entry->src;
410 progress = true;
411 }
412 break;
413
414 case BRW_OPCODE_CMP:
415 case BRW_OPCODE_IF:
416 if (i == 1) {
417 inst->src[i] = entry->src;
418 progress = true;
419 } else if (i == 0 && inst->src[1].file != IMM) {
420 uint32_t new_cmod;
421
422 new_cmod = brw_swap_cmod(inst->conditional_mod);
423 if (new_cmod != ~0u) {
424 /* Fit this constant in by swapping the operands and
425 * flipping the test
426 */
427 inst->src[0] = inst->src[1];
428 inst->src[1] = entry->src;
429 inst->conditional_mod = new_cmod;
430 progress = true;
431 }
432 }
433 break;
434
435 case BRW_OPCODE_SEL:
436 if (i == 1) {
437 inst->src[i] = entry->src;
438 progress = true;
439 } else if (i == 0 && inst->src[1].file != IMM) {
440 inst->src[0] = inst->src[1];
441 inst->src[1] = entry->src;
442
443 /* If this was predicated, flipping operands means
444 * we also need to flip the predicate.
445 */
446 if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
447 inst->predicate_inverse =
448 !inst->predicate_inverse;
449 }
450 progress = true;
451 }
452 break;
453
454 case SHADER_OPCODE_RCP:
455 /* The hardware doesn't do math on immediate values
456 * (because why are you doing that, seriously?), but
457 * the correct answer is to just constant fold it
458 * anyway.
459 */
460 assert(i == 0);
461 if (inst->src[0].imm.f != 0.0f) {
462 inst->opcode = BRW_OPCODE_MOV;
463 inst->src[0] = entry->src;
464 inst->src[0].imm.f = 1.0f / inst->src[0].imm.f;
465 progress = true;
466 }
467 break;
468
469 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
470 inst->src[i] = entry->src;
471 progress = true;
472 break;
473
474 default:
475 break;
476 }
477 }
478
479 return progress;
480 }
481 /* Walks a basic block and does copy propagation on it using the acp
482 * list.
483 */
484 bool
485 fs_visitor::opt_copy_propagate_local(void *copy_prop_ctx, bblock_t *block,
486 exec_list *acp)
487 {
488 bool progress = false;
489
490 for (fs_inst *inst = (fs_inst *)block->start;
491 inst != block->end->next;
492 inst = (fs_inst *)inst->next) {
493
494 /* Try propagating into this instruction. */
495 for (int i = 0; i < inst->sources; i++) {
496 if (inst->src[i].file != GRF)
497 continue;
498
499 foreach_list(entry_node, &acp[inst->src[i].reg % ACP_HASH_SIZE]) {
500 acp_entry *entry = (acp_entry *)entry_node;
501
502 if (try_constant_propagate(inst, entry))
503 progress = true;
504
505 if (try_copy_propagate(inst, i, entry))
506 progress = true;
507 }
508 }
509
510 /* kill the destination from the ACP */
511 if (inst->dst.file == GRF) {
512 foreach_list_safe(entry_node, &acp[inst->dst.reg % ACP_HASH_SIZE]) {
513 acp_entry *entry = (acp_entry *)entry_node;
514
515 if (inst->overwrites_reg(entry->dst)) {
516 entry->remove();
517 }
518 }
519
520 /* Oops, we only have the chaining hash based on the destination, not
521 * the source, so walk across the entire table.
522 */
523 for (int i = 0; i < ACP_HASH_SIZE; i++) {
524 foreach_list_safe(entry_node, &acp[i]) {
525 acp_entry *entry = (acp_entry *)entry_node;
526 if (inst->overwrites_reg(entry->src))
527 entry->remove();
528 }
529 }
530 }
531
532 /* If this instruction's source could potentially be folded into the
533 * operand of another instruction, add it to the ACP.
534 */
535 if (inst->opcode == BRW_OPCODE_MOV &&
536 inst->dst.file == GRF &&
537 ((inst->src[0].file == GRF &&
538 (inst->src[0].reg != inst->dst.reg ||
539 inst->src[0].reg_offset != inst->dst.reg_offset)) ||
540 inst->src[0].file == UNIFORM ||
541 inst->src[0].file == IMM) &&
542 inst->src[0].type == inst->dst.type &&
543 !inst->saturate &&
544 !inst->is_partial_write()) {
545 acp_entry *entry = ralloc(copy_prop_ctx, acp_entry);
546 entry->dst = inst->dst;
547 entry->src = inst->src[0];
548 acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
549 }
550 }
551
552 return progress;
553 }
554
555 bool
556 fs_visitor::opt_copy_propagate()
557 {
558 bool progress = false;
559 void *copy_prop_ctx = ralloc_context(NULL);
560 cfg_t cfg(&instructions);
561 exec_list *out_acp[cfg.num_blocks];
562 for (int i = 0; i < cfg.num_blocks; i++)
563 out_acp[i] = new exec_list [ACP_HASH_SIZE];
564
565 /* First, walk through each block doing local copy propagation and getting
566 * the set of copies available at the end of the block.
567 */
568 for (int b = 0; b < cfg.num_blocks; b++) {
569 bblock_t *block = cfg.blocks[b];
570
571 progress = opt_copy_propagate_local(copy_prop_ctx, block,
572 out_acp[b]) || progress;
573 }
574
575 /* Do dataflow analysis for those available copies. */
576 fs_copy_prop_dataflow dataflow(copy_prop_ctx, &cfg, out_acp);
577
578 /* Next, re-run local copy propagation, this time with the set of copies
579 * provided by the dataflow analysis available at the start of a block.
580 */
581 for (int b = 0; b < cfg.num_blocks; b++) {
582 bblock_t *block = cfg.blocks[b];
583 exec_list in_acp[ACP_HASH_SIZE];
584
585 for (int i = 0; i < dataflow.num_acp; i++) {
586 if (BITSET_TEST(dataflow.bd[b].livein, i)) {
587 struct acp_entry *entry = dataflow.acp[i];
588 in_acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
589 }
590 }
591
592 progress = opt_copy_propagate_local(copy_prop_ctx, block, in_acp) || progress;
593 }
594
595 for (int i = 0; i < cfg.num_blocks; i++)
596 delete [] out_acp[i];
597 ralloc_free(copy_prop_ctx);
598
599 if (progress)
600 invalidate_live_intervals();
601
602 return progress;
603 }