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