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