intel: Add a batch flush between front-buffer downsample and X protocol.
[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 Muchnik'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(block_node, &cfg->blocks[b]->parents) {
236 bblock_link *link = (bblock_link *)block_node;
237 bblock_t *block = link->block;
238 bd[b].livein[i] &= bd[block->block_num].liveout[i];
239 }
240
241 if (old_livein != bd[b].livein[i])
242 progress = true;
243 }
244 }
245 } while (progress);
246 }
247
248 void
249 fs_copy_prop_dataflow::dump_block_data() const
250 {
251 for (int b = 0; b < cfg->num_blocks; b++) {
252 bblock_t *block = cfg->blocks[b];
253 printf("Block %d [%d, %d] (parents ", block->block_num,
254 block->start_ip, block->end_ip);
255 foreach_list(block_node, &block->parents) {
256 bblock_t *parent = ((bblock_link *) block_node)->block;
257 printf("%d ", parent->block_num);
258 }
259 printf("):\n");
260 printf(" livein = 0x");
261 for (int i = 0; i < bitset_words; i++)
262 printf("%08x", bd[b].livein[i]);
263 printf(", liveout = 0x");
264 for (int i = 0; i < bitset_words; i++)
265 printf("%08x", bd[b].liveout[i]);
266 printf(",\n copy = 0x");
267 for (int i = 0; i < bitset_words; i++)
268 printf("%08x", bd[b].copy[i]);
269 printf(", kill = 0x");
270 for (int i = 0; i < bitset_words; i++)
271 printf("%08x", bd[b].kill[i]);
272 printf("\n");
273 }
274 }
275
276 bool
277 fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)
278 {
279 if (entry->src.file == IMM)
280 return false;
281
282 if (inst->src[arg].file != entry->dst.file ||
283 inst->src[arg].reg != entry->dst.reg ||
284 inst->src[arg].reg_offset != entry->dst.reg_offset) {
285 return false;
286 }
287
288 /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
289 if (inst->conditional_mod &&
290 inst->src[arg].type == BRW_REGISTER_TYPE_UD &&
291 entry->src.negate)
292 return false;
293
294 bool has_source_modifiers = entry->src.abs || entry->src.negate;
295
296 if ((has_source_modifiers || entry->src.file == UNIFORM ||
297 entry->src.smear != -1) && !can_do_source_mods(inst))
298 return false;
299
300 if (has_source_modifiers && entry->dst.type != inst->src[arg].type)
301 return false;
302
303 inst->src[arg].file = entry->src.file;
304 inst->src[arg].reg = entry->src.reg;
305 inst->src[arg].reg_offset = entry->src.reg_offset;
306 if (entry->src.smear != -1)
307 inst->src[arg].smear = entry->src.smear;
308
309 if (!inst->src[arg].abs) {
310 inst->src[arg].abs = entry->src.abs;
311 inst->src[arg].negate ^= entry->src.negate;
312 }
313
314 return true;
315 }
316
317
318 bool
319 fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry)
320 {
321 bool progress = false;
322
323 if (entry->src.file != IMM)
324 return false;
325
326 for (int i = 2; i >= 0; i--) {
327 if (inst->src[i].file != entry->dst.file ||
328 inst->src[i].reg != entry->dst.reg ||
329 inst->src[i].reg_offset != entry->dst.reg_offset)
330 continue;
331
332 /* Don't bother with cases that should have been taken care of by the
333 * GLSL compiler's constant folding pass.
334 */
335 if (inst->src[i].negate || inst->src[i].abs)
336 continue;
337
338 switch (inst->opcode) {
339 case BRW_OPCODE_MOV:
340 inst->src[i] = entry->src;
341 progress = true;
342 break;
343
344 case BRW_OPCODE_MACH:
345 case BRW_OPCODE_MUL:
346 case BRW_OPCODE_ADD:
347 if (i == 1) {
348 inst->src[i] = entry->src;
349 progress = true;
350 } else if (i == 0 && inst->src[1].file != IMM) {
351 /* Fit this constant in by commuting the operands.
352 * Exception: we can't do this for 32-bit integer MUL/MACH
353 * because it's asymmetric.
354 */
355 if ((inst->opcode == BRW_OPCODE_MUL ||
356 inst->opcode == BRW_OPCODE_MACH) &&
357 (inst->src[1].type == BRW_REGISTER_TYPE_D ||
358 inst->src[1].type == BRW_REGISTER_TYPE_UD))
359 break;
360 inst->src[0] = inst->src[1];
361 inst->src[1] = entry->src;
362 progress = true;
363 }
364 break;
365
366 case BRW_OPCODE_CMP:
367 case BRW_OPCODE_IF:
368 if (i == 1) {
369 inst->src[i] = entry->src;
370 progress = true;
371 } else if (i == 0 && inst->src[1].file != IMM) {
372 uint32_t new_cmod;
373
374 new_cmod = brw_swap_cmod(inst->conditional_mod);
375 if (new_cmod != ~0u) {
376 /* Fit this constant in by swapping the operands and
377 * flipping the test
378 */
379 inst->src[0] = inst->src[1];
380 inst->src[1] = entry->src;
381 inst->conditional_mod = new_cmod;
382 progress = true;
383 }
384 }
385 break;
386
387 case BRW_OPCODE_SEL:
388 if (i == 1) {
389 inst->src[i] = entry->src;
390 progress = true;
391 } else if (i == 0 && inst->src[1].file != IMM) {
392 inst->src[0] = inst->src[1];
393 inst->src[1] = entry->src;
394
395 /* If this was predicated, flipping operands means
396 * we also need to flip the predicate.
397 */
398 if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
399 inst->predicate_inverse =
400 !inst->predicate_inverse;
401 }
402 progress = true;
403 }
404 break;
405
406 case SHADER_OPCODE_RCP:
407 /* The hardware doesn't do math on immediate values
408 * (because why are you doing that, seriously?), but
409 * the correct answer is to just constant fold it
410 * anyway.
411 */
412 assert(i == 0);
413 if (inst->src[0].imm.f != 0.0f) {
414 inst->opcode = BRW_OPCODE_MOV;
415 inst->src[0] = entry->src;
416 inst->src[0].imm.f = 1.0f / inst->src[0].imm.f;
417 progress = true;
418 }
419 break;
420
421 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
422 inst->src[i] = entry->src;
423 progress = true;
424 break;
425
426 default:
427 break;
428 }
429 }
430
431 return progress;
432 }
433 /* Walks a basic block and does copy propagation on it using the acp
434 * list.
435 */
436 bool
437 fs_visitor::opt_copy_propagate_local(void *mem_ctx, bblock_t *block,
438 exec_list *acp)
439 {
440 bool progress = false;
441
442 for (fs_inst *inst = (fs_inst *)block->start;
443 inst != block->end->next;
444 inst = (fs_inst *)inst->next) {
445
446 /* Try propagating into this instruction. */
447 for (int i = 0; i < 3; i++) {
448 if (inst->src[i].file != GRF)
449 continue;
450
451 foreach_list(entry_node, &acp[inst->src[i].reg % ACP_HASH_SIZE]) {
452 acp_entry *entry = (acp_entry *)entry_node;
453
454 if (try_constant_propagate(inst, entry))
455 progress = true;
456
457 if (try_copy_propagate(inst, i, entry))
458 progress = true;
459 }
460 }
461
462 /* kill the destination from the ACP */
463 if (inst->dst.file == GRF) {
464 foreach_list_safe(entry_node, &acp[inst->dst.reg % ACP_HASH_SIZE]) {
465 acp_entry *entry = (acp_entry *)entry_node;
466
467 if (inst->overwrites_reg(entry->dst)) {
468 entry->remove();
469 }
470 }
471
472 /* Oops, we only have the chaining hash based on the destination, not
473 * the source, so walk across the entire table.
474 */
475 for (int i = 0; i < ACP_HASH_SIZE; i++) {
476 foreach_list_safe(entry_node, &acp[i]) {
477 acp_entry *entry = (acp_entry *)entry_node;
478 if (inst->overwrites_reg(entry->src))
479 entry->remove();
480 }
481 }
482 }
483
484 /* If this instruction's source could potentially be folded into the
485 * operand of another instruction, add it to the ACP.
486 */
487 if (inst->opcode == BRW_OPCODE_MOV &&
488 inst->dst.file == GRF &&
489 ((inst->src[0].file == GRF &&
490 (inst->src[0].reg != inst->dst.reg ||
491 inst->src[0].reg_offset != inst->dst.reg_offset)) ||
492 inst->src[0].file == UNIFORM ||
493 inst->src[0].file == IMM) &&
494 inst->src[0].type == inst->dst.type &&
495 !inst->saturate &&
496 !inst->is_partial_write()) {
497 acp_entry *entry = ralloc(mem_ctx, acp_entry);
498 entry->dst = inst->dst;
499 entry->src = inst->src[0];
500 acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
501 }
502 }
503
504 return progress;
505 }
506
507 bool
508 fs_visitor::opt_copy_propagate()
509 {
510 bool progress = false;
511 void *mem_ctx = ralloc_context(this->mem_ctx);
512 cfg_t cfg(this);
513 exec_list *out_acp[cfg.num_blocks];
514 for (int i = 0; i < cfg.num_blocks; i++)
515 out_acp[i] = new exec_list [ACP_HASH_SIZE];
516
517 /* First, walk through each block doing local copy propagation and getting
518 * the set of copies available at the end of the block.
519 */
520 for (int b = 0; b < cfg.num_blocks; b++) {
521 bblock_t *block = cfg.blocks[b];
522
523 progress = opt_copy_propagate_local(mem_ctx, block,
524 out_acp[b]) || progress;
525 }
526
527 /* Do dataflow analysis for those available copies. */
528 fs_copy_prop_dataflow dataflow(mem_ctx, &cfg, out_acp);
529
530 /* Next, re-run local copy propagation, this time with the set of copies
531 * provided by the dataflow analysis available at the start of a block.
532 */
533 for (int b = 0; b < cfg.num_blocks; b++) {
534 bblock_t *block = cfg.blocks[b];
535 exec_list in_acp[ACP_HASH_SIZE];
536
537 for (int i = 0; i < dataflow.num_acp; i++) {
538 if (BITSET_TEST(dataflow.bd[b].livein, i)) {
539 struct acp_entry *entry = dataflow.acp[i];
540 in_acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
541 }
542 }
543
544 progress = opt_copy_propagate_local(mem_ctx, block, in_acp) || progress;
545 }
546
547 for (int i = 0; i < cfg.num_blocks; i++)
548 delete [] out_acp[i];
549 ralloc_free(mem_ctx);
550
551 if (progress)
552 live_intervals_valid = false;
553
554 return progress;
555 }