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