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