i965/vec4: Don't iterate between blocks with inst->next/prev.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4.cpp
1 /*
2 * Copyright © 2011 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 #include "brw_vs.h"
27 #include "brw_dead_control_flow.h"
28
29 extern "C" {
30 #include "main/macros.h"
31 #include "main/shaderobj.h"
32 #include "program/prog_print.h"
33 #include "program/prog_parameter.h"
34 }
35
36 #define MAX_INSTRUCTION (1 << 30)
37
38 using namespace brw;
39
40 namespace brw {
41
42 /**
43 * Common helper for constructing swizzles. When only a subset of
44 * channels of a vec4 are used, we don't want to reference the other
45 * channels, as that will tell optimization passes that those other
46 * channels are used.
47 */
48 unsigned
49 swizzle_for_size(int size)
50 {
51 static const unsigned size_swizzles[4] = {
52 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
53 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
54 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
55 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
56 };
57
58 assert((size >= 1) && (size <= 4));
59 return size_swizzles[size - 1];
60 }
61
62 void
63 src_reg::init()
64 {
65 memset(this, 0, sizeof(*this));
66
67 this->file = BAD_FILE;
68 }
69
70 src_reg::src_reg(register_file file, int reg, const glsl_type *type)
71 {
72 init();
73
74 this->file = file;
75 this->reg = reg;
76 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
77 this->swizzle = swizzle_for_size(type->vector_elements);
78 else
79 this->swizzle = BRW_SWIZZLE_XYZW;
80 }
81
82 /** Generic unset register constructor. */
83 src_reg::src_reg()
84 {
85 init();
86 }
87
88 src_reg::src_reg(float f)
89 {
90 init();
91
92 this->file = IMM;
93 this->type = BRW_REGISTER_TYPE_F;
94 this->fixed_hw_reg.dw1.f = f;
95 }
96
97 src_reg::src_reg(uint32_t u)
98 {
99 init();
100
101 this->file = IMM;
102 this->type = BRW_REGISTER_TYPE_UD;
103 this->fixed_hw_reg.dw1.ud = u;
104 }
105
106 src_reg::src_reg(int32_t i)
107 {
108 init();
109
110 this->file = IMM;
111 this->type = BRW_REGISTER_TYPE_D;
112 this->fixed_hw_reg.dw1.d = i;
113 }
114
115 src_reg::src_reg(struct brw_reg reg)
116 {
117 init();
118
119 this->file = HW_REG;
120 this->fixed_hw_reg = reg;
121 this->type = reg.type;
122 }
123
124 src_reg::src_reg(dst_reg reg)
125 {
126 init();
127
128 this->file = reg.file;
129 this->reg = reg.reg;
130 this->reg_offset = reg.reg_offset;
131 this->type = reg.type;
132 this->reladdr = reg.reladdr;
133 this->fixed_hw_reg = reg.fixed_hw_reg;
134
135 int swizzles[4];
136 int next_chan = 0;
137 int last = 0;
138
139 for (int i = 0; i < 4; i++) {
140 if (!(reg.writemask & (1 << i)))
141 continue;
142
143 swizzles[next_chan++] = last = i;
144 }
145
146 for (; next_chan < 4; next_chan++) {
147 swizzles[next_chan] = last;
148 }
149
150 this->swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
151 swizzles[2], swizzles[3]);
152 }
153
154 void
155 dst_reg::init()
156 {
157 memset(this, 0, sizeof(*this));
158 this->file = BAD_FILE;
159 this->writemask = WRITEMASK_XYZW;
160 }
161
162 dst_reg::dst_reg()
163 {
164 init();
165 }
166
167 dst_reg::dst_reg(register_file file, int reg)
168 {
169 init();
170
171 this->file = file;
172 this->reg = reg;
173 }
174
175 dst_reg::dst_reg(register_file file, int reg, const glsl_type *type,
176 int writemask)
177 {
178 init();
179
180 this->file = file;
181 this->reg = reg;
182 this->type = brw_type_for_base_type(type);
183 this->writemask = writemask;
184 }
185
186 dst_reg::dst_reg(struct brw_reg reg)
187 {
188 init();
189
190 this->file = HW_REG;
191 this->fixed_hw_reg = reg;
192 this->type = reg.type;
193 }
194
195 dst_reg::dst_reg(src_reg reg)
196 {
197 init();
198
199 this->file = reg.file;
200 this->reg = reg.reg;
201 this->reg_offset = reg.reg_offset;
202 this->type = reg.type;
203 /* How should we do writemasking when converting from a src_reg? It seems
204 * pretty obvious that for src.xxxx the caller wants to write to src.x, but
205 * what about for src.wx? Just special-case src.xxxx for now.
206 */
207 if (reg.swizzle == BRW_SWIZZLE_XXXX)
208 this->writemask = WRITEMASK_X;
209 else
210 this->writemask = WRITEMASK_XYZW;
211 this->reladdr = reg.reladdr;
212 this->fixed_hw_reg = reg.fixed_hw_reg;
213 }
214
215 bool
216 vec4_instruction::is_send_from_grf()
217 {
218 switch (opcode) {
219 case SHADER_OPCODE_SHADER_TIME_ADD:
220 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
221 return true;
222 default:
223 return false;
224 }
225 }
226
227 bool
228 vec4_instruction::can_do_source_mods(struct brw_context *brw)
229 {
230 if (brw->gen == 6 && is_math())
231 return false;
232
233 if (is_send_from_grf())
234 return false;
235
236 if (!backend_instruction::can_do_source_mods())
237 return false;
238
239 return true;
240 }
241
242 /**
243 * Returns how many MRFs an opcode will write over.
244 *
245 * Note that this is not the 0 or 1 implied writes in an actual gen
246 * instruction -- the generate_* functions generate additional MOVs
247 * for setup.
248 */
249 int
250 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
251 {
252 if (inst->mlen == 0)
253 return 0;
254
255 switch (inst->opcode) {
256 case SHADER_OPCODE_RCP:
257 case SHADER_OPCODE_RSQ:
258 case SHADER_OPCODE_SQRT:
259 case SHADER_OPCODE_EXP2:
260 case SHADER_OPCODE_LOG2:
261 case SHADER_OPCODE_SIN:
262 case SHADER_OPCODE_COS:
263 return 1;
264 case SHADER_OPCODE_INT_QUOTIENT:
265 case SHADER_OPCODE_INT_REMAINDER:
266 case SHADER_OPCODE_POW:
267 return 2;
268 case VS_OPCODE_URB_WRITE:
269 return 1;
270 case VS_OPCODE_PULL_CONSTANT_LOAD:
271 return 2;
272 case SHADER_OPCODE_GEN4_SCRATCH_READ:
273 return 2;
274 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
275 return 3;
276 case GS_OPCODE_URB_WRITE:
277 case GS_OPCODE_URB_WRITE_ALLOCATE:
278 case GS_OPCODE_THREAD_END:
279 return 0;
280 case GS_OPCODE_FF_SYNC:
281 return 1;
282 case SHADER_OPCODE_SHADER_TIME_ADD:
283 return 0;
284 case SHADER_OPCODE_TEX:
285 case SHADER_OPCODE_TXL:
286 case SHADER_OPCODE_TXD:
287 case SHADER_OPCODE_TXF:
288 case SHADER_OPCODE_TXF_CMS:
289 case SHADER_OPCODE_TXF_MCS:
290 case SHADER_OPCODE_TXS:
291 case SHADER_OPCODE_TG4:
292 case SHADER_OPCODE_TG4_OFFSET:
293 return inst->header_present ? 1 : 0;
294 case SHADER_OPCODE_UNTYPED_ATOMIC:
295 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
296 return 0;
297 default:
298 unreachable("not reached");
299 }
300 }
301
302 bool
303 src_reg::equals(const src_reg &r) const
304 {
305 return (file == r.file &&
306 reg == r.reg &&
307 reg_offset == r.reg_offset &&
308 type == r.type &&
309 negate == r.negate &&
310 abs == r.abs &&
311 swizzle == r.swizzle &&
312 !reladdr && !r.reladdr &&
313 memcmp(&fixed_hw_reg, &r.fixed_hw_reg,
314 sizeof(fixed_hw_reg)) == 0);
315 }
316
317 /* Replaces unused channels of a swizzle with channels that are used.
318 *
319 * For instance, this pass transforms
320 *
321 * mov vgrf4.yz, vgrf5.wxzy
322 *
323 * into
324 *
325 * mov vgrf4.yz, vgrf5.xxzx
326 *
327 * This eliminates false uses of some channels, letting dead code elimination
328 * remove the instructions that wrote them.
329 */
330 bool
331 vec4_visitor::opt_reduce_swizzle()
332 {
333 bool progress = false;
334
335 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
336 if (inst->dst.file == BAD_FILE || inst->dst.file == HW_REG)
337 continue;
338
339 int swizzle[4];
340
341 /* Determine which channels of the sources are read. */
342 switch (inst->opcode) {
343 case BRW_OPCODE_DP4:
344 case BRW_OPCODE_DPH: /* FINISHME: DPH reads only three channels of src0,
345 * but all four of src1.
346 */
347 swizzle[0] = 0;
348 swizzle[1] = 1;
349 swizzle[2] = 2;
350 swizzle[3] = 3;
351 break;
352 case BRW_OPCODE_DP3:
353 swizzle[0] = 0;
354 swizzle[1] = 1;
355 swizzle[2] = 2;
356 swizzle[3] = -1;
357 break;
358 case BRW_OPCODE_DP2:
359 swizzle[0] = 0;
360 swizzle[1] = 1;
361 swizzle[2] = -1;
362 swizzle[3] = -1;
363 break;
364 default:
365 swizzle[0] = inst->dst.writemask & WRITEMASK_X ? 0 : -1;
366 swizzle[1] = inst->dst.writemask & WRITEMASK_Y ? 1 : -1;
367 swizzle[2] = inst->dst.writemask & WRITEMASK_Z ? 2 : -1;
368 swizzle[3] = inst->dst.writemask & WRITEMASK_W ? 3 : -1;
369 break;
370 }
371
372 /* Resolve unread channels (-1) by assigning them the swizzle of the
373 * first channel that is used.
374 */
375 int first_used_channel = 0;
376 for (int i = 0; i < 4; i++) {
377 if (swizzle[i] != -1) {
378 first_used_channel = swizzle[i];
379 break;
380 }
381 }
382 for (int i = 0; i < 4; i++) {
383 if (swizzle[i] == -1) {
384 swizzle[i] = first_used_channel;
385 }
386 }
387
388 /* Update sources' swizzles. */
389 for (int i = 0; i < 3; i++) {
390 if (inst->src[i].file != GRF &&
391 inst->src[i].file != ATTR &&
392 inst->src[i].file != UNIFORM)
393 continue;
394
395 int swiz[4];
396 for (int j = 0; j < 4; j++) {
397 swiz[j] = BRW_GET_SWZ(inst->src[i].swizzle, swizzle[j]);
398 }
399
400 unsigned new_swizzle = BRW_SWIZZLE4(swiz[0], swiz[1], swiz[2], swiz[3]);
401 if (inst->src[i].swizzle != new_swizzle) {
402 inst->src[i].swizzle = new_swizzle;
403 progress = true;
404 }
405 }
406 }
407
408 if (progress)
409 invalidate_live_intervals();
410
411 return progress;
412 }
413
414 static bool
415 try_eliminate_instruction(vec4_instruction *inst, int new_writemask,
416 const struct brw_context *brw)
417 {
418 if (inst->has_side_effects())
419 return false;
420
421 if (new_writemask == 0) {
422 /* Don't dead code eliminate instructions that write to the
423 * accumulator as a side-effect. Instead just set the destination
424 * to the null register to free it.
425 */
426 if (inst->writes_accumulator || inst->writes_flag()) {
427 inst->dst = dst_reg(retype(brw_null_reg(), inst->dst.type));
428 } else {
429 inst->opcode = BRW_OPCODE_NOP;
430 }
431
432 return true;
433 } else if (inst->dst.writemask != new_writemask) {
434 switch (inst->opcode) {
435 case SHADER_OPCODE_TXF_CMS:
436 case SHADER_OPCODE_GEN4_SCRATCH_READ:
437 case VS_OPCODE_PULL_CONSTANT_LOAD:
438 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
439 break;
440 default:
441 /* Do not set a writemask on Gen6 for math instructions, those are
442 * executed using align1 mode that does not support a destination mask.
443 */
444 if (!(brw->gen == 6 && inst->is_math()) && !inst->is_tex()) {
445 inst->dst.writemask = new_writemask;
446 return true;
447 }
448 }
449 }
450
451 return false;
452 }
453
454 /**
455 * Must be called after calculate_live_intervals() to remove unused
456 * writes to registers -- register allocation will fail otherwise
457 * because something deffed but not used won't be considered to
458 * interfere with other regs.
459 */
460 bool
461 vec4_visitor::dead_code_eliminate()
462 {
463 bool progress = false;
464 int pc = -1;
465
466 calculate_live_intervals();
467
468 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
469 pc++;
470
471 bool inst_writes_flag = false;
472 if (inst->dst.file != GRF) {
473 if (inst->dst.is_null() && inst->writes_flag()) {
474 inst_writes_flag = true;
475 } else {
476 continue;
477 }
478 }
479
480 if (inst->dst.file == GRF) {
481 int write_mask = inst->dst.writemask;
482
483 for (int c = 0; c < 4; c++) {
484 if (write_mask & (1 << c)) {
485 assert(this->virtual_grf_end[inst->dst.reg * 4 + c] >= pc);
486 if (this->virtual_grf_end[inst->dst.reg * 4 + c] == pc) {
487 write_mask &= ~(1 << c);
488 }
489 }
490 }
491
492 progress = try_eliminate_instruction(inst, write_mask, brw) ||
493 progress;
494 }
495
496 if (inst->predicate || inst->prev == NULL)
497 continue;
498
499 int dead_channels;
500 if (inst_writes_flag) {
501 /* Arbitrarily chosen, other than not being an xyzw writemask. */
502 #define FLAG_WRITEMASK (1 << 5)
503 dead_channels = inst->reads_flag() ? 0 : FLAG_WRITEMASK;
504 } else {
505 dead_channels = inst->dst.writemask;
506
507 for (int i = 0; i < 3; i++) {
508 if (inst->src[i].file != GRF ||
509 inst->src[i].reg != inst->dst.reg)
510 continue;
511
512 for (int j = 0; j < 4; j++) {
513 int swiz = BRW_GET_SWZ(inst->src[i].swizzle, j);
514 dead_channels &= ~(1 << swiz);
515 }
516 }
517 }
518
519 foreach_inst_in_block_reverse_starting_from(vec4_instruction, scan_inst,
520 inst, block) {
521 if (dead_channels == 0)
522 break;
523
524 if (inst_writes_flag) {
525 if (scan_inst->dst.is_null() && scan_inst->writes_flag()) {
526 scan_inst->opcode = BRW_OPCODE_NOP;
527 progress = true;
528 continue;
529 } else if (scan_inst->reads_flag()) {
530 break;
531 }
532 }
533
534 if (inst->dst.file == scan_inst->dst.file &&
535 inst->dst.reg == scan_inst->dst.reg &&
536 inst->dst.reg_offset == scan_inst->dst.reg_offset) {
537 int new_writemask = scan_inst->dst.writemask & ~dead_channels;
538
539 progress = try_eliminate_instruction(scan_inst, new_writemask, brw) ||
540 progress;
541 }
542
543 for (int i = 0; i < 3; i++) {
544 if (scan_inst->src[i].file != inst->dst.file ||
545 scan_inst->src[i].reg != inst->dst.reg)
546 continue;
547
548 for (int j = 0; j < 4; j++) {
549 int swiz = BRW_GET_SWZ(scan_inst->src[i].swizzle, j);
550 dead_channels &= ~(1 << swiz);
551 }
552 }
553 }
554 }
555
556 if (progress) {
557 foreach_block_and_inst_safe (block, backend_instruction, inst, cfg) {
558 if (inst->opcode == BRW_OPCODE_NOP) {
559 inst->remove(block);
560 }
561 }
562
563 invalidate_live_intervals();
564 }
565
566 return progress;
567 }
568
569 void
570 vec4_visitor::split_uniform_registers()
571 {
572 /* Prior to this, uniforms have been in an array sized according to
573 * the number of vector uniforms present, sparsely filled (so an
574 * aggregate results in reg indices being skipped over). Now we're
575 * going to cut those aggregates up so each .reg index is one
576 * vector. The goal is to make elimination of unused uniform
577 * components easier later.
578 */
579 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
580 for (int i = 0 ; i < 3; i++) {
581 if (inst->src[i].file != UNIFORM)
582 continue;
583
584 assert(!inst->src[i].reladdr);
585
586 inst->src[i].reg += inst->src[i].reg_offset;
587 inst->src[i].reg_offset = 0;
588 }
589 }
590
591 /* Update that everything is now vector-sized. */
592 for (int i = 0; i < this->uniforms; i++) {
593 this->uniform_size[i] = 1;
594 }
595 }
596
597 void
598 vec4_visitor::pack_uniform_registers()
599 {
600 bool uniform_used[this->uniforms];
601 int new_loc[this->uniforms];
602 int new_chan[this->uniforms];
603
604 memset(uniform_used, 0, sizeof(uniform_used));
605 memset(new_loc, 0, sizeof(new_loc));
606 memset(new_chan, 0, sizeof(new_chan));
607
608 /* Find which uniform vectors are actually used by the program. We
609 * expect unused vector elements when we've moved array access out
610 * to pull constants, and from some GLSL code generators like wine.
611 */
612 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
613 for (int i = 0 ; i < 3; i++) {
614 if (inst->src[i].file != UNIFORM)
615 continue;
616
617 uniform_used[inst->src[i].reg] = true;
618 }
619 }
620
621 int new_uniform_count = 0;
622
623 /* Now, figure out a packing of the live uniform vectors into our
624 * push constants.
625 */
626 for (int src = 0; src < uniforms; src++) {
627 assert(src < uniform_array_size);
628 int size = this->uniform_vector_size[src];
629
630 if (!uniform_used[src]) {
631 this->uniform_vector_size[src] = 0;
632 continue;
633 }
634
635 int dst;
636 /* Find the lowest place we can slot this uniform in. */
637 for (dst = 0; dst < src; dst++) {
638 if (this->uniform_vector_size[dst] + size <= 4)
639 break;
640 }
641
642 if (src == dst) {
643 new_loc[src] = dst;
644 new_chan[src] = 0;
645 } else {
646 new_loc[src] = dst;
647 new_chan[src] = this->uniform_vector_size[dst];
648
649 /* Move the references to the data */
650 for (int j = 0; j < size; j++) {
651 stage_prog_data->param[dst * 4 + new_chan[src] + j] =
652 stage_prog_data->param[src * 4 + j];
653 }
654
655 this->uniform_vector_size[dst] += size;
656 this->uniform_vector_size[src] = 0;
657 }
658
659 new_uniform_count = MAX2(new_uniform_count, dst + 1);
660 }
661
662 this->uniforms = new_uniform_count;
663
664 /* Now, update the instructions for our repacked uniforms. */
665 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
666 for (int i = 0 ; i < 3; i++) {
667 int src = inst->src[i].reg;
668
669 if (inst->src[i].file != UNIFORM)
670 continue;
671
672 inst->src[i].reg = new_loc[src];
673
674 int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src];
675 int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src];
676 int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src];
677 int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src];
678 inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw);
679 }
680 }
681 }
682
683 /**
684 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
685 *
686 * While GLSL IR also performs this optimization, we end up with it in
687 * our instruction stream for a couple of reasons. One is that we
688 * sometimes generate silly instructions, for example in array access
689 * where we'll generate "ADD offset, index, base" even if base is 0.
690 * The other is that GLSL IR's constant propagation doesn't track the
691 * components of aggregates, so some VS patterns (initialize matrix to
692 * 0, accumulate in vertex blending factors) end up breaking down to
693 * instructions involving 0.
694 */
695 bool
696 vec4_visitor::opt_algebraic()
697 {
698 bool progress = false;
699
700 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
701 switch (inst->opcode) {
702 case BRW_OPCODE_ADD:
703 if (inst->src[1].is_zero()) {
704 inst->opcode = BRW_OPCODE_MOV;
705 inst->src[1] = src_reg();
706 progress = true;
707 }
708 break;
709
710 case BRW_OPCODE_MUL:
711 if (inst->src[1].is_zero()) {
712 inst->opcode = BRW_OPCODE_MOV;
713 switch (inst->src[0].type) {
714 case BRW_REGISTER_TYPE_F:
715 inst->src[0] = src_reg(0.0f);
716 break;
717 case BRW_REGISTER_TYPE_D:
718 inst->src[0] = src_reg(0);
719 break;
720 case BRW_REGISTER_TYPE_UD:
721 inst->src[0] = src_reg(0u);
722 break;
723 default:
724 unreachable("not reached");
725 }
726 inst->src[1] = src_reg();
727 progress = true;
728 } else if (inst->src[1].is_one()) {
729 inst->opcode = BRW_OPCODE_MOV;
730 inst->src[1] = src_reg();
731 progress = true;
732 }
733 break;
734 default:
735 break;
736 }
737 }
738
739 if (progress)
740 invalidate_live_intervals();
741
742 return progress;
743 }
744
745 /**
746 * Only a limited number of hardware registers may be used for push
747 * constants, so this turns access to the overflowed constants into
748 * pull constants.
749 */
750 void
751 vec4_visitor::move_push_constants_to_pull_constants()
752 {
753 int pull_constant_loc[this->uniforms];
754
755 /* Only allow 32 registers (256 uniform components) as push constants,
756 * which is the limit on gen6.
757 *
758 * If changing this value, note the limitation about total_regs in
759 * brw_curbe.c.
760 */
761 int max_uniform_components = 32 * 8;
762 if (this->uniforms * 4 <= max_uniform_components)
763 return;
764
765 /* Make some sort of choice as to which uniforms get sent to pull
766 * constants. We could potentially do something clever here like
767 * look for the most infrequently used uniform vec4s, but leave
768 * that for later.
769 */
770 for (int i = 0; i < this->uniforms * 4; i += 4) {
771 pull_constant_loc[i / 4] = -1;
772
773 if (i >= max_uniform_components) {
774 const gl_constant_value **values = &stage_prog_data->param[i];
775
776 /* Try to find an existing copy of this uniform in the pull
777 * constants if it was part of an array access already.
778 */
779 for (unsigned int j = 0; j < stage_prog_data->nr_pull_params; j += 4) {
780 int matches;
781
782 for (matches = 0; matches < 4; matches++) {
783 if (stage_prog_data->pull_param[j + matches] != values[matches])
784 break;
785 }
786
787 if (matches == 4) {
788 pull_constant_loc[i / 4] = j / 4;
789 break;
790 }
791 }
792
793 if (pull_constant_loc[i / 4] == -1) {
794 assert(stage_prog_data->nr_pull_params % 4 == 0);
795 pull_constant_loc[i / 4] = stage_prog_data->nr_pull_params / 4;
796
797 for (int j = 0; j < 4; j++) {
798 stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
799 values[j];
800 }
801 }
802 }
803 }
804
805 /* Now actually rewrite usage of the things we've moved to pull
806 * constants.
807 */
808 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
809 for (int i = 0 ; i < 3; i++) {
810 if (inst->src[i].file != UNIFORM ||
811 pull_constant_loc[inst->src[i].reg] == -1)
812 continue;
813
814 int uniform = inst->src[i].reg;
815
816 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
817
818 emit_pull_constant_load(block, inst, temp, inst->src[i],
819 pull_constant_loc[uniform]);
820
821 inst->src[i].file = temp.file;
822 inst->src[i].reg = temp.reg;
823 inst->src[i].reg_offset = temp.reg_offset;
824 inst->src[i].reladdr = NULL;
825 }
826 }
827
828 /* Repack push constants to remove the now-unused ones. */
829 pack_uniform_registers();
830 }
831
832 /**
833 * Sets the dependency control fields on instructions after register
834 * allocation and before the generator is run.
835 *
836 * When you have a sequence of instructions like:
837 *
838 * DP4 temp.x vertex uniform[0]
839 * DP4 temp.y vertex uniform[0]
840 * DP4 temp.z vertex uniform[0]
841 * DP4 temp.w vertex uniform[0]
842 *
843 * The hardware doesn't know that it can actually run the later instructions
844 * while the previous ones are in flight, producing stalls. However, we have
845 * manual fields we can set in the instructions that let it do so.
846 */
847 void
848 vec4_visitor::opt_set_dependency_control()
849 {
850 vec4_instruction *last_grf_write[BRW_MAX_GRF];
851 uint8_t grf_channels_written[BRW_MAX_GRF];
852 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
853 uint8_t mrf_channels_written[BRW_MAX_GRF];
854
855 assert(prog_data->total_grf ||
856 !"Must be called after register allocation");
857
858 foreach_block (block, cfg) {
859 memset(last_grf_write, 0, sizeof(last_grf_write));
860 memset(last_mrf_write, 0, sizeof(last_mrf_write));
861
862 foreach_inst_in_block (vec4_instruction, inst, block) {
863 /* If we read from a register that we were doing dependency control
864 * on, don't do dependency control across the read.
865 */
866 for (int i = 0; i < 3; i++) {
867 int reg = inst->src[i].reg + inst->src[i].reg_offset;
868 if (inst->src[i].file == GRF) {
869 last_grf_write[reg] = NULL;
870 } else if (inst->src[i].file == HW_REG) {
871 memset(last_grf_write, 0, sizeof(last_grf_write));
872 break;
873 }
874 assert(inst->src[i].file != MRF);
875 }
876
877 /* In the presence of send messages, totally interrupt dependency
878 * control. They're long enough that the chance of dependency
879 * control around them just doesn't matter.
880 */
881 if (inst->mlen) {
882 memset(last_grf_write, 0, sizeof(last_grf_write));
883 memset(last_mrf_write, 0, sizeof(last_mrf_write));
884 continue;
885 }
886
887 /* It looks like setting dependency control on a predicated
888 * instruction hangs the GPU.
889 */
890 if (inst->predicate) {
891 memset(last_grf_write, 0, sizeof(last_grf_write));
892 memset(last_mrf_write, 0, sizeof(last_mrf_write));
893 continue;
894 }
895
896 /* Dependency control does not work well over math instructions.
897 */
898 if (inst->is_math()) {
899 memset(last_grf_write, 0, sizeof(last_grf_write));
900 memset(last_mrf_write, 0, sizeof(last_mrf_write));
901 continue;
902 }
903
904 /* Now, see if we can do dependency control for this instruction
905 * against a previous one writing to its destination.
906 */
907 int reg = inst->dst.reg + inst->dst.reg_offset;
908 if (inst->dst.file == GRF) {
909 if (last_grf_write[reg] &&
910 !(inst->dst.writemask & grf_channels_written[reg])) {
911 last_grf_write[reg]->no_dd_clear = true;
912 inst->no_dd_check = true;
913 } else {
914 grf_channels_written[reg] = 0;
915 }
916
917 last_grf_write[reg] = inst;
918 grf_channels_written[reg] |= inst->dst.writemask;
919 } else if (inst->dst.file == MRF) {
920 if (last_mrf_write[reg] &&
921 !(inst->dst.writemask & mrf_channels_written[reg])) {
922 last_mrf_write[reg]->no_dd_clear = true;
923 inst->no_dd_check = true;
924 } else {
925 mrf_channels_written[reg] = 0;
926 }
927
928 last_mrf_write[reg] = inst;
929 mrf_channels_written[reg] |= inst->dst.writemask;
930 } else if (inst->dst.reg == HW_REG) {
931 if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
932 memset(last_grf_write, 0, sizeof(last_grf_write));
933 if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
934 memset(last_mrf_write, 0, sizeof(last_mrf_write));
935 }
936 }
937 }
938 }
939
940 bool
941 vec4_instruction::can_reswizzle(int dst_writemask,
942 int swizzle,
943 int swizzle_mask)
944 {
945 /* If this instruction sets anything not referenced by swizzle, then we'd
946 * totally break it when we reswizzle.
947 */
948 if (dst.writemask & ~swizzle_mask)
949 return false;
950
951 if (mlen > 0)
952 return false;
953
954 return true;
955 }
956
957 /**
958 * For any channels in the swizzle's source that were populated by this
959 * instruction, rewrite the instruction to put the appropriate result directly
960 * in those channels.
961 *
962 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
963 */
964 void
965 vec4_instruction::reswizzle(int dst_writemask, int swizzle)
966 {
967 int new_writemask = 0;
968 int new_swizzle[4] = { 0 };
969
970 /* Dot product instructions write a single result into all channels. */
971 if (opcode != BRW_OPCODE_DP4 && opcode != BRW_OPCODE_DPH &&
972 opcode != BRW_OPCODE_DP3 && opcode != BRW_OPCODE_DP2) {
973 for (int i = 0; i < 3; i++) {
974 if (src[i].file == BAD_FILE || src[i].file == IMM)
975 continue;
976
977 for (int c = 0; c < 4; c++) {
978 new_swizzle[c] = BRW_GET_SWZ(src[i].swizzle, BRW_GET_SWZ(swizzle, c));
979 }
980
981 src[i].swizzle = BRW_SWIZZLE4(new_swizzle[0], new_swizzle[1],
982 new_swizzle[2], new_swizzle[3]);
983 }
984 }
985
986 for (int c = 0; c < 4; c++) {
987 int bit = 1 << BRW_GET_SWZ(swizzle, c);
988 /* Skip components of the swizzle not used by the dst. */
989 if (!(dst_writemask & (1 << c)))
990 continue;
991 /* If we were populating this component, then populate the
992 * corresponding channel of the new dst.
993 */
994 if (dst.writemask & bit)
995 new_writemask |= (1 << c);
996 }
997 dst.writemask = new_writemask;
998 }
999
1000 /*
1001 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
1002 * just written and then MOVed into another reg and making the original write
1003 * of the GRF write directly to the final destination instead.
1004 */
1005 bool
1006 vec4_visitor::opt_register_coalesce()
1007 {
1008 bool progress = false;
1009 int next_ip = 0;
1010
1011 calculate_live_intervals();
1012
1013 foreach_block_and_inst_safe (block, vec4_instruction, inst, cfg) {
1014 int ip = next_ip;
1015 next_ip++;
1016
1017 if (inst->opcode != BRW_OPCODE_MOV ||
1018 (inst->dst.file != GRF && inst->dst.file != MRF) ||
1019 inst->predicate ||
1020 inst->src[0].file != GRF ||
1021 inst->dst.type != inst->src[0].type ||
1022 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
1023 continue;
1024
1025 bool to_mrf = (inst->dst.file == MRF);
1026
1027 /* Can't coalesce this GRF if someone else was going to
1028 * read it later.
1029 */
1030 if (this->virtual_grf_end[inst->src[0].reg * 4 + 0] > ip ||
1031 this->virtual_grf_end[inst->src[0].reg * 4 + 1] > ip ||
1032 this->virtual_grf_end[inst->src[0].reg * 4 + 2] > ip ||
1033 this->virtual_grf_end[inst->src[0].reg * 4 + 3] > ip)
1034 continue;
1035
1036 /* We need to check interference with the final destination between this
1037 * instruction and the earliest instruction involved in writing the GRF
1038 * we're eliminating. To do that, keep track of which of our source
1039 * channels we've seen initialized.
1040 */
1041 bool chans_needed[4] = {false, false, false, false};
1042 int chans_remaining = 0;
1043 int swizzle_mask = 0;
1044 for (int i = 0; i < 4; i++) {
1045 int chan = BRW_GET_SWZ(inst->src[0].swizzle, i);
1046
1047 if (!(inst->dst.writemask & (1 << i)))
1048 continue;
1049
1050 swizzle_mask |= (1 << chan);
1051
1052 if (!chans_needed[chan]) {
1053 chans_needed[chan] = true;
1054 chans_remaining++;
1055 }
1056 }
1057
1058 /* Now walk up the instruction stream trying to see if we can rewrite
1059 * everything writing to the temporary to write into the destination
1060 * instead.
1061 */
1062 vec4_instruction *_scan_inst = (vec4_instruction *)inst->prev;
1063 foreach_inst_in_block_reverse_starting_from(vec4_instruction, scan_inst,
1064 inst, block) {
1065 _scan_inst = scan_inst;
1066
1067 if (scan_inst->dst.file == GRF &&
1068 scan_inst->dst.reg == inst->src[0].reg &&
1069 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
1070 /* Found something writing to the reg we want to coalesce away. */
1071 if (to_mrf) {
1072 /* SEND instructions can't have MRF as a destination. */
1073 if (scan_inst->mlen)
1074 break;
1075
1076 if (brw->gen == 6) {
1077 /* gen6 math instructions must have the destination be
1078 * GRF, so no compute-to-MRF for them.
1079 */
1080 if (scan_inst->is_math()) {
1081 break;
1082 }
1083 }
1084 }
1085
1086 /* If we can't handle the swizzle, bail. */
1087 if (!scan_inst->can_reswizzle(inst->dst.writemask,
1088 inst->src[0].swizzle,
1089 swizzle_mask)) {
1090 break;
1091 }
1092
1093 /* Mark which channels we found unconditional writes for. */
1094 if (!scan_inst->predicate) {
1095 for (int i = 0; i < 4; i++) {
1096 if (scan_inst->dst.writemask & (1 << i) &&
1097 chans_needed[i]) {
1098 chans_needed[i] = false;
1099 chans_remaining--;
1100 }
1101 }
1102 }
1103
1104 if (chans_remaining == 0)
1105 break;
1106 }
1107
1108 /* You can't read from an MRF, so if someone else reads our MRF's
1109 * source GRF that we wanted to rewrite, that stops us. If it's a
1110 * GRF we're trying to coalesce to, we don't actually handle
1111 * rewriting sources so bail in that case as well.
1112 */
1113 bool interfered = false;
1114 for (int i = 0; i < 3; i++) {
1115 if (scan_inst->src[i].file == GRF &&
1116 scan_inst->src[i].reg == inst->src[0].reg &&
1117 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
1118 interfered = true;
1119 }
1120 }
1121 if (interfered)
1122 break;
1123
1124 /* If somebody else writes our destination here, we can't coalesce
1125 * before that.
1126 */
1127 if (scan_inst->dst.file == inst->dst.file &&
1128 scan_inst->dst.reg == inst->dst.reg) {
1129 break;
1130 }
1131
1132 /* Check for reads of the register we're trying to coalesce into. We
1133 * can't go rewriting instructions above that to put some other value
1134 * in the register instead.
1135 */
1136 if (to_mrf && scan_inst->mlen > 0) {
1137 if (inst->dst.reg >= scan_inst->base_mrf &&
1138 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
1139 break;
1140 }
1141 } else {
1142 for (int i = 0; i < 3; i++) {
1143 if (scan_inst->src[i].file == inst->dst.file &&
1144 scan_inst->src[i].reg == inst->dst.reg &&
1145 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
1146 interfered = true;
1147 }
1148 }
1149 if (interfered)
1150 break;
1151 }
1152 }
1153
1154 if (chans_remaining == 0) {
1155 /* If we've made it here, we have an MOV we want to coalesce out, and
1156 * a scan_inst pointing to the earliest instruction involved in
1157 * computing the value. Now go rewrite the instruction stream
1158 * between the two.
1159 */
1160 vec4_instruction *scan_inst = _scan_inst;
1161 while (scan_inst != inst) {
1162 if (scan_inst->dst.file == GRF &&
1163 scan_inst->dst.reg == inst->src[0].reg &&
1164 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
1165 scan_inst->reswizzle(inst->dst.writemask,
1166 inst->src[0].swizzle);
1167 scan_inst->dst.file = inst->dst.file;
1168 scan_inst->dst.reg = inst->dst.reg;
1169 scan_inst->dst.reg_offset = inst->dst.reg_offset;
1170 scan_inst->saturate |= inst->saturate;
1171 }
1172 scan_inst = (vec4_instruction *)scan_inst->next;
1173 }
1174 inst->remove(block);
1175 progress = true;
1176 }
1177 }
1178
1179 if (progress)
1180 invalidate_live_intervals();
1181
1182 return progress;
1183 }
1184
1185 /**
1186 * Splits virtual GRFs requesting more than one contiguous physical register.
1187 *
1188 * We initially create large virtual GRFs for temporary structures, arrays,
1189 * and matrices, so that the dereference visitor functions can add reg_offsets
1190 * to work their way down to the actual member being accessed. But when it
1191 * comes to optimization, we'd like to treat each register as individual
1192 * storage if possible.
1193 *
1194 * So far, the only thing that might prevent splitting is a send message from
1195 * a GRF on IVB.
1196 */
1197 void
1198 vec4_visitor::split_virtual_grfs()
1199 {
1200 int num_vars = this->virtual_grf_count;
1201 int new_virtual_grf[num_vars];
1202 bool split_grf[num_vars];
1203
1204 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1205
1206 /* Try to split anything > 0 sized. */
1207 for (int i = 0; i < num_vars; i++) {
1208 split_grf[i] = this->virtual_grf_sizes[i] != 1;
1209 }
1210
1211 /* Check that the instructions are compatible with the registers we're trying
1212 * to split.
1213 */
1214 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1215 /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1216 * contiguous.
1217 */
1218 if (inst->is_send_from_grf()) {
1219 for (int i = 0; i < 3; i++) {
1220 if (inst->src[i].file == GRF) {
1221 split_grf[inst->src[i].reg] = false;
1222 }
1223 }
1224 }
1225 }
1226
1227 /* Allocate new space for split regs. Note that the virtual
1228 * numbers will be contiguous.
1229 */
1230 for (int i = 0; i < num_vars; i++) {
1231 if (!split_grf[i])
1232 continue;
1233
1234 new_virtual_grf[i] = virtual_grf_alloc(1);
1235 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
1236 int reg = virtual_grf_alloc(1);
1237 assert(reg == new_virtual_grf[i] + j - 1);
1238 (void) reg;
1239 }
1240 this->virtual_grf_sizes[i] = 1;
1241 }
1242
1243 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1244 if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1245 inst->dst.reg_offset != 0) {
1246 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1247 inst->dst.reg_offset - 1);
1248 inst->dst.reg_offset = 0;
1249 }
1250 for (int i = 0; i < 3; i++) {
1251 if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1252 inst->src[i].reg_offset != 0) {
1253 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1254 inst->src[i].reg_offset - 1);
1255 inst->src[i].reg_offset = 0;
1256 }
1257 }
1258 }
1259 invalidate_live_intervals();
1260 }
1261
1262 void
1263 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1264 {
1265 dump_instruction(be_inst, stderr);
1266 }
1267
1268 void
1269 vec4_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
1270 {
1271 vec4_instruction *inst = (vec4_instruction *)be_inst;
1272
1273 if (inst->predicate) {
1274 fprintf(file, "(%cf0) ",
1275 inst->predicate_inverse ? '-' : '+');
1276 }
1277
1278 fprintf(file, "%s", brw_instruction_name(inst->opcode));
1279 if (inst->conditional_mod) {
1280 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
1281 }
1282 fprintf(file, " ");
1283
1284 switch (inst->dst.file) {
1285 case GRF:
1286 fprintf(file, "vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1287 break;
1288 case MRF:
1289 fprintf(file, "m%d", inst->dst.reg);
1290 break;
1291 case HW_REG:
1292 if (inst->dst.fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1293 switch (inst->dst.fixed_hw_reg.nr) {
1294 case BRW_ARF_NULL:
1295 fprintf(file, "null");
1296 break;
1297 case BRW_ARF_ADDRESS:
1298 fprintf(file, "a0.%d", inst->dst.fixed_hw_reg.subnr);
1299 break;
1300 case BRW_ARF_ACCUMULATOR:
1301 fprintf(file, "acc%d", inst->dst.fixed_hw_reg.subnr);
1302 break;
1303 case BRW_ARF_FLAG:
1304 fprintf(file, "f%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1305 inst->dst.fixed_hw_reg.subnr);
1306 break;
1307 default:
1308 fprintf(file, "arf%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1309 inst->dst.fixed_hw_reg.subnr);
1310 break;
1311 }
1312 } else {
1313 fprintf(file, "hw_reg%d", inst->dst.fixed_hw_reg.nr);
1314 }
1315 if (inst->dst.fixed_hw_reg.subnr)
1316 fprintf(file, "+%d", inst->dst.fixed_hw_reg.subnr);
1317 break;
1318 case BAD_FILE:
1319 fprintf(file, "(null)");
1320 break;
1321 default:
1322 fprintf(file, "???");
1323 break;
1324 }
1325 if (inst->dst.writemask != WRITEMASK_XYZW) {
1326 fprintf(file, ".");
1327 if (inst->dst.writemask & 1)
1328 fprintf(file, "x");
1329 if (inst->dst.writemask & 2)
1330 fprintf(file, "y");
1331 if (inst->dst.writemask & 4)
1332 fprintf(file, "z");
1333 if (inst->dst.writemask & 8)
1334 fprintf(file, "w");
1335 }
1336 fprintf(file, ":%s", brw_reg_type_letters(inst->dst.type));
1337
1338 if (inst->src[0].file != BAD_FILE)
1339 fprintf(file, ", ");
1340
1341 for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1342 if (inst->src[i].negate)
1343 fprintf(file, "-");
1344 if (inst->src[i].abs)
1345 fprintf(file, "|");
1346 switch (inst->src[i].file) {
1347 case GRF:
1348 fprintf(file, "vgrf%d", inst->src[i].reg);
1349 break;
1350 case ATTR:
1351 fprintf(file, "attr%d", inst->src[i].reg);
1352 break;
1353 case UNIFORM:
1354 fprintf(file, "u%d", inst->src[i].reg);
1355 break;
1356 case IMM:
1357 switch (inst->src[i].type) {
1358 case BRW_REGISTER_TYPE_F:
1359 fprintf(file, "%fF", inst->src[i].fixed_hw_reg.dw1.f);
1360 break;
1361 case BRW_REGISTER_TYPE_D:
1362 fprintf(file, "%dD", inst->src[i].fixed_hw_reg.dw1.d);
1363 break;
1364 case BRW_REGISTER_TYPE_UD:
1365 fprintf(file, "%uU", inst->src[i].fixed_hw_reg.dw1.ud);
1366 break;
1367 default:
1368 fprintf(file, "???");
1369 break;
1370 }
1371 break;
1372 case HW_REG:
1373 if (inst->src[i].fixed_hw_reg.negate)
1374 fprintf(file, "-");
1375 if (inst->src[i].fixed_hw_reg.abs)
1376 fprintf(file, "|");
1377 if (inst->src[i].fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1378 switch (inst->src[i].fixed_hw_reg.nr) {
1379 case BRW_ARF_NULL:
1380 fprintf(file, "null");
1381 break;
1382 case BRW_ARF_ADDRESS:
1383 fprintf(file, "a0.%d", inst->src[i].fixed_hw_reg.subnr);
1384 break;
1385 case BRW_ARF_ACCUMULATOR:
1386 fprintf(file, "acc%d", inst->src[i].fixed_hw_reg.subnr);
1387 break;
1388 case BRW_ARF_FLAG:
1389 fprintf(file, "f%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1390 inst->src[i].fixed_hw_reg.subnr);
1391 break;
1392 default:
1393 fprintf(file, "arf%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1394 inst->src[i].fixed_hw_reg.subnr);
1395 break;
1396 }
1397 } else {
1398 fprintf(file, "hw_reg%d", inst->src[i].fixed_hw_reg.nr);
1399 }
1400 if (inst->src[i].fixed_hw_reg.subnr)
1401 fprintf(file, "+%d", inst->src[i].fixed_hw_reg.subnr);
1402 if (inst->src[i].fixed_hw_reg.abs)
1403 fprintf(file, "|");
1404 break;
1405 case BAD_FILE:
1406 fprintf(file, "(null)");
1407 break;
1408 default:
1409 fprintf(file, "???");
1410 break;
1411 }
1412
1413 /* Don't print .0; and only VGRFs have reg_offsets and sizes */
1414 if (inst->src[i].reg_offset != 0 &&
1415 inst->src[i].file == GRF &&
1416 virtual_grf_sizes[inst->src[i].reg] != 1)
1417 fprintf(file, ".%d", inst->src[i].reg_offset);
1418
1419 if (inst->src[i].file != IMM) {
1420 static const char *chans[4] = {"x", "y", "z", "w"};
1421 fprintf(file, ".");
1422 for (int c = 0; c < 4; c++) {
1423 fprintf(file, "%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1424 }
1425 }
1426
1427 if (inst->src[i].abs)
1428 fprintf(file, "|");
1429
1430 if (inst->src[i].file != IMM) {
1431 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
1432 }
1433
1434 if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1435 fprintf(file, ", ");
1436 }
1437
1438 fprintf(file, "\n");
1439 }
1440
1441
1442 static inline struct brw_reg
1443 attribute_to_hw_reg(int attr, bool interleaved)
1444 {
1445 if (interleaved)
1446 return stride(brw_vec4_grf(attr / 2, (attr % 2) * 4), 0, 4, 1);
1447 else
1448 return brw_vec8_grf(attr, 0);
1449 }
1450
1451
1452 /**
1453 * Replace each register of type ATTR in this->instructions with a reference
1454 * to a fixed HW register.
1455 *
1456 * If interleaved is true, then each attribute takes up half a register, with
1457 * register N containing attribute 2*N in its first half and attribute 2*N+1
1458 * in its second half (this corresponds to the payload setup used by geometry
1459 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1460 * false, then each attribute takes up a whole register, with register N
1461 * containing attribute N (this corresponds to the payload setup used by
1462 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1463 */
1464 void
1465 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map,
1466 bool interleaved)
1467 {
1468 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1469 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1470 if (inst->dst.file == ATTR) {
1471 int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1472
1473 /* All attributes used in the shader need to have been assigned a
1474 * hardware register by the caller
1475 */
1476 assert(grf != 0);
1477
1478 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1479 reg.type = inst->dst.type;
1480 reg.dw1.bits.writemask = inst->dst.writemask;
1481
1482 inst->dst.file = HW_REG;
1483 inst->dst.fixed_hw_reg = reg;
1484 }
1485
1486 for (int i = 0; i < 3; i++) {
1487 if (inst->src[i].file != ATTR)
1488 continue;
1489
1490 int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1491
1492 /* All attributes used in the shader need to have been assigned a
1493 * hardware register by the caller
1494 */
1495 assert(grf != 0);
1496
1497 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1498 reg.dw1.bits.swizzle = inst->src[i].swizzle;
1499 reg.type = inst->src[i].type;
1500 if (inst->src[i].abs)
1501 reg = brw_abs(reg);
1502 if (inst->src[i].negate)
1503 reg = negate(reg);
1504
1505 inst->src[i].file = HW_REG;
1506 inst->src[i].fixed_hw_reg = reg;
1507 }
1508 }
1509 }
1510
1511 int
1512 vec4_vs_visitor::setup_attributes(int payload_reg)
1513 {
1514 int nr_attributes;
1515 int attribute_map[VERT_ATTRIB_MAX + 1];
1516 memset(attribute_map, 0, sizeof(attribute_map));
1517
1518 nr_attributes = 0;
1519 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1520 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1521 attribute_map[i] = payload_reg + nr_attributes;
1522 nr_attributes++;
1523 }
1524 }
1525
1526 /* VertexID is stored by the VF as the last vertex element, but we
1527 * don't represent it with a flag in inputs_read, so we call it
1528 * VERT_ATTRIB_MAX.
1529 */
1530 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid) {
1531 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1532 nr_attributes++;
1533 }
1534
1535 lower_attributes_to_hw_regs(attribute_map, false /* interleaved */);
1536
1537 /* The BSpec says we always have to read at least one thing from
1538 * the VF, and it appears that the hardware wedges otherwise.
1539 */
1540 if (nr_attributes == 0)
1541 nr_attributes = 1;
1542
1543 prog_data->urb_read_length = (nr_attributes + 1) / 2;
1544
1545 unsigned vue_entries =
1546 MAX2(nr_attributes, prog_data->vue_map.num_slots);
1547
1548 if (brw->gen == 6)
1549 prog_data->urb_entry_size = ALIGN(vue_entries, 8) / 8;
1550 else
1551 prog_data->urb_entry_size = ALIGN(vue_entries, 4) / 4;
1552
1553 return payload_reg + nr_attributes;
1554 }
1555
1556 int
1557 vec4_visitor::setup_uniforms(int reg)
1558 {
1559 prog_data->base.dispatch_grf_start_reg = reg;
1560
1561 /* The pre-gen6 VS requires that some push constants get loaded no
1562 * matter what, or the GPU would hang.
1563 */
1564 if (brw->gen < 6 && this->uniforms == 0) {
1565 assert(this->uniforms < this->uniform_array_size);
1566 this->uniform_vector_size[this->uniforms] = 1;
1567
1568 stage_prog_data->param =
1569 reralloc(NULL, stage_prog_data->param, const gl_constant_value *, 4);
1570 for (unsigned int i = 0; i < 4; i++) {
1571 unsigned int slot = this->uniforms * 4 + i;
1572 static gl_constant_value zero = { 0.0 };
1573 stage_prog_data->param[slot] = &zero;
1574 }
1575
1576 this->uniforms++;
1577 reg++;
1578 } else {
1579 reg += ALIGN(uniforms, 2) / 2;
1580 }
1581
1582 stage_prog_data->nr_params = this->uniforms * 4;
1583
1584 prog_data->base.curb_read_length =
1585 reg - prog_data->base.dispatch_grf_start_reg;
1586
1587 return reg;
1588 }
1589
1590 void
1591 vec4_vs_visitor::setup_payload(void)
1592 {
1593 int reg = 0;
1594
1595 /* The payload always contains important data in g0, which contains
1596 * the URB handles that are passed on to the URB write at the end
1597 * of the thread. So, we always start push constants at g1.
1598 */
1599 reg++;
1600
1601 reg = setup_uniforms(reg);
1602
1603 reg = setup_attributes(reg);
1604
1605 this->first_non_payload_grf = reg;
1606 }
1607
1608 void
1609 vec4_visitor::assign_binding_table_offsets()
1610 {
1611 assign_common_binding_table_offsets(0);
1612 }
1613
1614 src_reg
1615 vec4_visitor::get_timestamp()
1616 {
1617 assert(brw->gen >= 7);
1618
1619 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1620 BRW_ARF_TIMESTAMP,
1621 0,
1622 BRW_REGISTER_TYPE_UD,
1623 BRW_VERTICAL_STRIDE_0,
1624 BRW_WIDTH_4,
1625 BRW_HORIZONTAL_STRIDE_4,
1626 BRW_SWIZZLE_XYZW,
1627 WRITEMASK_XYZW));
1628
1629 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1630
1631 vec4_instruction *mov = emit(MOV(dst, ts));
1632 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1633 * even if it's not enabled in the dispatch.
1634 */
1635 mov->force_writemask_all = true;
1636
1637 return src_reg(dst);
1638 }
1639
1640 void
1641 vec4_visitor::emit_shader_time_begin()
1642 {
1643 current_annotation = "shader time start";
1644 shader_start_time = get_timestamp();
1645 }
1646
1647 void
1648 vec4_visitor::emit_shader_time_end()
1649 {
1650 current_annotation = "shader time end";
1651 src_reg shader_end_time = get_timestamp();
1652
1653
1654 /* Check that there weren't any timestamp reset events (assuming these
1655 * were the only two timestamp reads that happened).
1656 */
1657 src_reg reset_end = shader_end_time;
1658 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1659 vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1660 test->conditional_mod = BRW_CONDITIONAL_Z;
1661
1662 emit(IF(BRW_PREDICATE_NORMAL));
1663
1664 /* Take the current timestamp and get the delta. */
1665 shader_start_time.negate = true;
1666 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1667 emit(ADD(diff, shader_start_time, shader_end_time));
1668
1669 /* If there were no instructions between the two timestamp gets, the diff
1670 * is 2 cycles. Remove that overhead, so I can forget about that when
1671 * trying to determine the time taken for single instructions.
1672 */
1673 emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1674
1675 emit_shader_time_write(st_base, src_reg(diff));
1676 emit_shader_time_write(st_written, src_reg(1u));
1677 emit(BRW_OPCODE_ELSE);
1678 emit_shader_time_write(st_reset, src_reg(1u));
1679 emit(BRW_OPCODE_ENDIF);
1680 }
1681
1682 void
1683 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type,
1684 src_reg value)
1685 {
1686 int shader_time_index =
1687 brw_get_shader_time_index(brw, shader_prog, prog, type);
1688
1689 dst_reg dst =
1690 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1691
1692 dst_reg offset = dst;
1693 dst_reg time = dst;
1694 time.reg_offset++;
1695
1696 offset.type = BRW_REGISTER_TYPE_UD;
1697 emit(MOV(offset, src_reg(shader_time_index * SHADER_TIME_STRIDE)));
1698
1699 time.type = BRW_REGISTER_TYPE_UD;
1700 emit(MOV(time, src_reg(value)));
1701
1702 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1703 }
1704
1705 bool
1706 vec4_visitor::run()
1707 {
1708 sanity_param_count = prog->Parameters->NumParameters;
1709
1710 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1711 emit_shader_time_begin();
1712
1713 assign_binding_table_offsets();
1714
1715 emit_prolog();
1716
1717 /* Generate VS IR for main(). (the visitor only descends into
1718 * functions called "main").
1719 */
1720 if (shader) {
1721 visit_instructions(shader->base.ir);
1722 } else {
1723 emit_program_code();
1724 }
1725 base_ir = NULL;
1726
1727 if (key->userclip_active && !prog->UsesClipDistanceOut)
1728 setup_uniform_clipplane_values();
1729
1730 emit_thread_end();
1731
1732 calculate_cfg();
1733
1734 /* Before any optimization, push array accesses out to scratch
1735 * space where we need them to be. This pass may allocate new
1736 * virtual GRFs, so we want to do it early. It also makes sure
1737 * that we have reladdr computations available for CSE, since we'll
1738 * often do repeated subexpressions for those.
1739 */
1740 if (shader) {
1741 move_grf_array_access_to_scratch();
1742 move_uniform_array_access_to_pull_constants();
1743 } else {
1744 /* The ARB_vertex_program frontend emits pull constant loads directly
1745 * rather than using reladdr, so we don't need to walk through all the
1746 * instructions looking for things to move. There isn't anything.
1747 *
1748 * We do still need to split things to vec4 size.
1749 */
1750 split_uniform_registers();
1751 }
1752 pack_uniform_registers();
1753 move_push_constants_to_pull_constants();
1754 split_virtual_grfs();
1755
1756 const char *stage_name = stage == MESA_SHADER_GEOMETRY ? "gs" : "vs";
1757
1758 #define OPT(pass, args...) do { \
1759 pass_num++; \
1760 bool this_progress = pass(args); \
1761 \
1762 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
1763 char filename[64]; \
1764 snprintf(filename, 64, "%s-%04d-%02d-%02d-" #pass, \
1765 stage_name, shader_prog ? shader_prog->Name : 0, iteration, pass_num); \
1766 \
1767 backend_visitor::dump_instructions(filename); \
1768 } \
1769 \
1770 progress = progress || this_progress; \
1771 } while (false)
1772
1773
1774 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
1775 char filename[64];
1776 snprintf(filename, 64, "%s-%04d-00-start",
1777 stage_name, shader_prog ? shader_prog->Name : 0);
1778
1779 backend_visitor::dump_instructions(filename);
1780 }
1781
1782 bool progress;
1783 int iteration = 0;
1784 do {
1785 progress = false;
1786 iteration++;
1787 int pass_num = 0;
1788
1789 OPT(opt_reduce_swizzle);
1790 OPT(dead_code_eliminate);
1791 OPT(dead_control_flow_eliminate, this);
1792 OPT(opt_copy_propagation);
1793 OPT(opt_algebraic);
1794 OPT(opt_cse);
1795 OPT(opt_register_coalesce);
1796 } while (progress);
1797
1798
1799 if (failed)
1800 return false;
1801
1802 setup_payload();
1803
1804 if (false) {
1805 /* Debug of register spilling: Go spill everything. */
1806 const int grf_count = virtual_grf_count;
1807 float spill_costs[virtual_grf_count];
1808 bool no_spill[virtual_grf_count];
1809 evaluate_spill_costs(spill_costs, no_spill);
1810 for (int i = 0; i < grf_count; i++) {
1811 if (no_spill[i])
1812 continue;
1813 spill_reg(i);
1814 }
1815 }
1816
1817 while (!reg_allocate()) {
1818 if (failed)
1819 return false;
1820 }
1821
1822 opt_schedule_instructions();
1823
1824 opt_set_dependency_control();
1825
1826 /* If any state parameters were appended, then ParameterValues could have
1827 * been realloced, in which case the driver uniform storage set up by
1828 * _mesa_associate_uniform_storage() would point to freed memory. Make
1829 * sure that didn't happen.
1830 */
1831 assert(sanity_param_count == prog->Parameters->NumParameters);
1832
1833 return !failed;
1834 }
1835
1836 } /* namespace brw */
1837
1838 extern "C" {
1839
1840 /**
1841 * Compile a vertex shader.
1842 *
1843 * Returns the final assembly and the program's size.
1844 */
1845 const unsigned *
1846 brw_vs_emit(struct brw_context *brw,
1847 struct gl_shader_program *prog,
1848 struct brw_vs_compile *c,
1849 struct brw_vs_prog_data *prog_data,
1850 void *mem_ctx,
1851 unsigned *final_assembly_size)
1852 {
1853 bool start_busy = false;
1854 double start_time = 0;
1855
1856 if (unlikely(brw->perf_debug)) {
1857 start_busy = (brw->batch.last_bo &&
1858 drm_intel_bo_busy(brw->batch.last_bo));
1859 start_time = get_time();
1860 }
1861
1862 struct brw_shader *shader = NULL;
1863 if (prog)
1864 shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
1865
1866 if (unlikely(INTEL_DEBUG & DEBUG_VS))
1867 brw_dump_ir(brw, "vertex", prog, &shader->base, &c->vp->program.Base);
1868
1869 vec4_vs_visitor v(brw, c, prog_data, prog, mem_ctx);
1870 if (!v.run()) {
1871 if (prog) {
1872 prog->LinkStatus = false;
1873 ralloc_strcat(&prog->InfoLog, v.fail_msg);
1874 }
1875
1876 _mesa_problem(NULL, "Failed to compile vertex shader: %s\n",
1877 v.fail_msg);
1878
1879 return NULL;
1880 }
1881
1882 const unsigned *assembly = NULL;
1883 vec4_generator g(brw, prog, &c->vp->program.Base, &prog_data->base,
1884 mem_ctx, INTEL_DEBUG & DEBUG_VS);
1885 assembly = g.generate_assembly(v.cfg, final_assembly_size);
1886
1887 if (unlikely(brw->perf_debug) && shader) {
1888 if (shader->compiled_once) {
1889 brw_vs_debug_recompile(brw, prog, &c->key);
1890 }
1891 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
1892 perf_debug("VS compile took %.03f ms and stalled the GPU\n",
1893 (get_time() - start_time) * 1000);
1894 }
1895 shader->compiled_once = true;
1896 }
1897
1898 return assembly;
1899 }
1900
1901
1902 void
1903 brw_vec4_setup_prog_key_for_precompile(struct gl_context *ctx,
1904 struct brw_vec4_prog_key *key,
1905 GLuint id, struct gl_program *prog)
1906 {
1907 key->program_string_id = id;
1908 key->clamp_vertex_color = ctx->API == API_OPENGL_COMPAT;
1909
1910 unsigned sampler_count = _mesa_fls(prog->SamplersUsed);
1911 for (unsigned i = 0; i < sampler_count; i++) {
1912 if (prog->ShadowSamplers & (1 << i)) {
1913 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
1914 key->tex.swizzles[i] =
1915 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
1916 } else {
1917 /* Color sampler: assume no swizzling. */
1918 key->tex.swizzles[i] = SWIZZLE_XYZW;
1919 }
1920 }
1921 }
1922
1923 } /* extern "C" */