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