OSDN Git Service

i965: Move is_math/is_tex/is_control_flow() to backend_instruction.
[android-x86/external-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 "glsl/ir_print_visitor.h"
27
28 extern "C" {
29 #include "main/macros.h"
30 #include "main/shaderobj.h"
31 #include "program/prog_print.h"
32 #include "program/prog_parameter.h"
33 }
34
35 #define MAX_INSTRUCTION (1 << 30)
36
37 using namespace brw;
38
39 namespace brw {
40
41 /**
42  * Common helper for constructing swizzles.  When only a subset of
43  * channels of a vec4 are used, we don't want to reference the other
44  * channels, as that will tell optimization passes that those other
45  * channels are used.
46  */
47 unsigned
48 swizzle_for_size(int size)
49 {
50    static const unsigned size_swizzles[4] = {
51       BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
52       BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
53       BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
54       BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
55    };
56
57    assert((size >= 1) && (size <= 4));
58    return size_swizzles[size - 1];
59 }
60
61 void
62 src_reg::init()
63 {
64    memset(this, 0, sizeof(*this));
65
66    this->file = BAD_FILE;
67 }
68
69 src_reg::src_reg(register_file file, int reg, const glsl_type *type)
70 {
71    init();
72
73    this->file = file;
74    this->reg = reg;
75    if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
76       this->swizzle = swizzle_for_size(type->vector_elements);
77    else
78       this->swizzle = SWIZZLE_XYZW;
79 }
80
81 /** Generic unset register constructor. */
82 src_reg::src_reg()
83 {
84    init();
85 }
86
87 src_reg::src_reg(float f)
88 {
89    init();
90
91    this->file = IMM;
92    this->type = BRW_REGISTER_TYPE_F;
93    this->imm.f = f;
94 }
95
96 src_reg::src_reg(uint32_t u)
97 {
98    init();
99
100    this->file = IMM;
101    this->type = BRW_REGISTER_TYPE_UD;
102    this->imm.u = u;
103 }
104
105 src_reg::src_reg(int32_t i)
106 {
107    init();
108
109    this->file = IMM;
110    this->type = BRW_REGISTER_TYPE_D;
111    this->imm.i = i;
112 }
113
114 src_reg::src_reg(dst_reg reg)
115 {
116    init();
117
118    this->file = reg.file;
119    this->reg = reg.reg;
120    this->reg_offset = reg.reg_offset;
121    this->type = reg.type;
122    this->reladdr = reg.reladdr;
123    this->fixed_hw_reg = reg.fixed_hw_reg;
124
125    int swizzles[4];
126    int next_chan = 0;
127    int last = 0;
128
129    for (int i = 0; i < 4; i++) {
130       if (!(reg.writemask & (1 << i)))
131          continue;
132
133       swizzles[next_chan++] = last = i;
134    }
135
136    for (; next_chan < 4; next_chan++) {
137       swizzles[next_chan] = last;
138    }
139
140    this->swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
141                                 swizzles[2], swizzles[3]);
142 }
143
144 void
145 dst_reg::init()
146 {
147    memset(this, 0, sizeof(*this));
148    this->file = BAD_FILE;
149    this->writemask = WRITEMASK_XYZW;
150 }
151
152 dst_reg::dst_reg()
153 {
154    init();
155 }
156
157 dst_reg::dst_reg(register_file file, int reg)
158 {
159    init();
160
161    this->file = file;
162    this->reg = reg;
163 }
164
165 dst_reg::dst_reg(register_file file, int reg, const glsl_type *type,
166                  int writemask)
167 {
168    init();
169
170    this->file = file;
171    this->reg = reg;
172    this->type = brw_type_for_base_type(type);
173    this->writemask = writemask;
174 }
175
176 dst_reg::dst_reg(struct brw_reg reg)
177 {
178    init();
179
180    this->file = HW_REG;
181    this->fixed_hw_reg = reg;
182 }
183
184 dst_reg::dst_reg(src_reg reg)
185 {
186    init();
187
188    this->file = reg.file;
189    this->reg = reg.reg;
190    this->reg_offset = reg.reg_offset;
191    this->type = reg.type;
192    /* How should we do writemasking when converting from a src_reg?  It seems
193     * pretty obvious that for src.xxxx the caller wants to write to src.x, but
194     * what about for src.wx?  Just special-case src.xxxx for now.
195     */
196    if (reg.swizzle == BRW_SWIZZLE_XXXX)
197       this->writemask = WRITEMASK_X;
198    else
199       this->writemask = WRITEMASK_XYZW;
200    this->reladdr = reg.reladdr;
201    this->fixed_hw_reg = reg.fixed_hw_reg;
202 }
203
204 bool
205 vec4_instruction::is_send_from_grf()
206 {
207    switch (opcode) {
208    case SHADER_OPCODE_SHADER_TIME_ADD:
209    case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
210       return true;
211    default:
212       return false;
213    }
214 }
215
216 bool
217 vec4_visitor::can_do_source_mods(vec4_instruction *inst)
218 {
219    if (intel->gen == 6 && inst->is_math())
220       return false;
221
222    if (inst->is_send_from_grf())
223       return false;
224
225    return true;
226 }
227
228 /**
229  * Returns how many MRFs an opcode will write over.
230  *
231  * Note that this is not the 0 or 1 implied writes in an actual gen
232  * instruction -- the generate_* functions generate additional MOVs
233  * for setup.
234  */
235 int
236 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
237 {
238    if (inst->mlen == 0)
239       return 0;
240
241    switch (inst->opcode) {
242    case SHADER_OPCODE_RCP:
243    case SHADER_OPCODE_RSQ:
244    case SHADER_OPCODE_SQRT:
245    case SHADER_OPCODE_EXP2:
246    case SHADER_OPCODE_LOG2:
247    case SHADER_OPCODE_SIN:
248    case SHADER_OPCODE_COS:
249       return 1;
250    case SHADER_OPCODE_POW:
251       return 2;
252    case VS_OPCODE_URB_WRITE:
253       return 1;
254    case VS_OPCODE_PULL_CONSTANT_LOAD:
255       return 2;
256    case VS_OPCODE_SCRATCH_READ:
257       return 2;
258    case VS_OPCODE_SCRATCH_WRITE:
259       return 3;
260    case SHADER_OPCODE_SHADER_TIME_ADD:
261       return 0;
262    default:
263       assert(!"not reached");
264       return inst->mlen;
265    }
266 }
267
268 bool
269 src_reg::equals(src_reg *r)
270 {
271    return (file == r->file &&
272            reg == r->reg &&
273            reg_offset == r->reg_offset &&
274            type == r->type &&
275            negate == r->negate &&
276            abs == r->abs &&
277            swizzle == r->swizzle &&
278            !reladdr && !r->reladdr &&
279            memcmp(&fixed_hw_reg, &r->fixed_hw_reg,
280                   sizeof(fixed_hw_reg)) == 0 &&
281            imm.u == r->imm.u);
282 }
283
284 /**
285  * Must be called after calculate_live_intervales() to remove unused
286  * writes to registers -- register allocation will fail otherwise
287  * because something deffed but not used won't be considered to
288  * interfere with other regs.
289  */
290 bool
291 vec4_visitor::dead_code_eliminate()
292 {
293    bool progress = false;
294    int pc = 0;
295
296    calculate_live_intervals();
297
298    foreach_list_safe(node, &this->instructions) {
299       vec4_instruction *inst = (vec4_instruction *)node;
300
301       if (inst->dst.file == GRF && this->virtual_grf_use[inst->dst.reg] <= pc) {
302          inst->remove();
303          progress = true;
304       }
305
306       pc++;
307    }
308
309    if (progress)
310       live_intervals_valid = false;
311
312    return progress;
313 }
314
315 void
316 vec4_visitor::split_uniform_registers()
317 {
318    /* Prior to this, uniforms have been in an array sized according to
319     * the number of vector uniforms present, sparsely filled (so an
320     * aggregate results in reg indices being skipped over).  Now we're
321     * going to cut those aggregates up so each .reg index is one
322     * vector.  The goal is to make elimination of unused uniform
323     * components easier later.
324     */
325    foreach_list(node, &this->instructions) {
326       vec4_instruction *inst = (vec4_instruction *)node;
327
328       for (int i = 0 ; i < 3; i++) {
329          if (inst->src[i].file != UNIFORM)
330             continue;
331
332          assert(!inst->src[i].reladdr);
333
334          inst->src[i].reg += inst->src[i].reg_offset;
335          inst->src[i].reg_offset = 0;
336       }
337    }
338
339    /* Update that everything is now vector-sized. */
340    for (int i = 0; i < this->uniforms; i++) {
341       this->uniform_size[i] = 1;
342    }
343 }
344
345 void
346 vec4_visitor::pack_uniform_registers()
347 {
348    bool uniform_used[this->uniforms];
349    int new_loc[this->uniforms];
350    int new_chan[this->uniforms];
351
352    memset(uniform_used, 0, sizeof(uniform_used));
353    memset(new_loc, 0, sizeof(new_loc));
354    memset(new_chan, 0, sizeof(new_chan));
355
356    /* Find which uniform vectors are actually used by the program.  We
357     * expect unused vector elements when we've moved array access out
358     * to pull constants, and from some GLSL code generators like wine.
359     */
360    foreach_list(node, &this->instructions) {
361       vec4_instruction *inst = (vec4_instruction *)node;
362
363       for (int i = 0 ; i < 3; i++) {
364          if (inst->src[i].file != UNIFORM)
365             continue;
366
367          uniform_used[inst->src[i].reg] = true;
368       }
369    }
370
371    int new_uniform_count = 0;
372
373    /* Now, figure out a packing of the live uniform vectors into our
374     * push constants.
375     */
376    for (int src = 0; src < uniforms; src++) {
377       int size = this->uniform_vector_size[src];
378
379       if (!uniform_used[src]) {
380          this->uniform_vector_size[src] = 0;
381          continue;
382       }
383
384       int dst;
385       /* Find the lowest place we can slot this uniform in. */
386       for (dst = 0; dst < src; dst++) {
387          if (this->uniform_vector_size[dst] + size <= 4)
388             break;
389       }
390
391       if (src == dst) {
392          new_loc[src] = dst;
393          new_chan[src] = 0;
394       } else {
395          new_loc[src] = dst;
396          new_chan[src] = this->uniform_vector_size[dst];
397
398          /* Move the references to the data */
399          for (int j = 0; j < size; j++) {
400             prog_data->param[dst * 4 + new_chan[src] + j] =
401                prog_data->param[src * 4 + j];
402          }
403
404          this->uniform_vector_size[dst] += size;
405          this->uniform_vector_size[src] = 0;
406       }
407
408       new_uniform_count = MAX2(new_uniform_count, dst + 1);
409    }
410
411    this->uniforms = new_uniform_count;
412
413    /* Now, update the instructions for our repacked uniforms. */
414    foreach_list(node, &this->instructions) {
415       vec4_instruction *inst = (vec4_instruction *)node;
416
417       for (int i = 0 ; i < 3; i++) {
418          int src = inst->src[i].reg;
419
420          if (inst->src[i].file != UNIFORM)
421             continue;
422
423          inst->src[i].reg = new_loc[src];
424
425          int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src];
426          int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src];
427          int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src];
428          int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src];
429          inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw);
430       }
431    }
432 }
433
434 bool
435 src_reg::is_zero() const
436 {
437    if (file != IMM)
438       return false;
439
440    if (type == BRW_REGISTER_TYPE_F) {
441       return imm.f == 0.0;
442    } else {
443       return imm.i == 0;
444    }
445 }
446
447 bool
448 src_reg::is_one() const
449 {
450    if (file != IMM)
451       return false;
452
453    if (type == BRW_REGISTER_TYPE_F) {
454       return imm.f == 1.0;
455    } else {
456       return imm.i == 1;
457    }
458 }
459
460 /**
461  * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
462  *
463  * While GLSL IR also performs this optimization, we end up with it in
464  * our instruction stream for a couple of reasons.  One is that we
465  * sometimes generate silly instructions, for example in array access
466  * where we'll generate "ADD offset, index, base" even if base is 0.
467  * The other is that GLSL IR's constant propagation doesn't track the
468  * components of aggregates, so some VS patterns (initialize matrix to
469  * 0, accumulate in vertex blending factors) end up breaking down to
470  * instructions involving 0.
471  */
472 bool
473 vec4_visitor::opt_algebraic()
474 {
475    bool progress = false;
476
477    foreach_list(node, &this->instructions) {
478       vec4_instruction *inst = (vec4_instruction *)node;
479
480       switch (inst->opcode) {
481       case BRW_OPCODE_ADD:
482          if (inst->src[1].is_zero()) {
483             inst->opcode = BRW_OPCODE_MOV;
484             inst->src[1] = src_reg();
485             progress = true;
486          }
487          break;
488
489       case BRW_OPCODE_MUL:
490          if (inst->src[1].is_zero()) {
491             inst->opcode = BRW_OPCODE_MOV;
492             switch (inst->src[0].type) {
493             case BRW_REGISTER_TYPE_F:
494                inst->src[0] = src_reg(0.0f);
495                break;
496             case BRW_REGISTER_TYPE_D:
497                inst->src[0] = src_reg(0);
498                break;
499             case BRW_REGISTER_TYPE_UD:
500                inst->src[0] = src_reg(0u);
501                break;
502             default:
503                assert(!"not reached");
504                inst->src[0] = src_reg(0.0f);
505                break;
506             }
507             inst->src[1] = src_reg();
508             progress = true;
509          } else if (inst->src[1].is_one()) {
510             inst->opcode = BRW_OPCODE_MOV;
511             inst->src[1] = src_reg();
512             progress = true;
513          }
514          break;
515       default:
516          break;
517       }
518    }
519
520    if (progress)
521       this->live_intervals_valid = false;
522
523    return progress;
524 }
525
526 /**
527  * Only a limited number of hardware registers may be used for push
528  * constants, so this turns access to the overflowed constants into
529  * pull constants.
530  */
531 void
532 vec4_visitor::move_push_constants_to_pull_constants()
533 {
534    int pull_constant_loc[this->uniforms];
535
536    /* Only allow 32 registers (256 uniform components) as push constants,
537     * which is the limit on gen6.
538     */
539    int max_uniform_components = 32 * 8;
540    if (this->uniforms * 4 <= max_uniform_components)
541       return;
542
543    /* Make some sort of choice as to which uniforms get sent to pull
544     * constants.  We could potentially do something clever here like
545     * look for the most infrequently used uniform vec4s, but leave
546     * that for later.
547     */
548    for (int i = 0; i < this->uniforms * 4; i += 4) {
549       pull_constant_loc[i / 4] = -1;
550
551       if (i >= max_uniform_components) {
552          const float **values = &prog_data->param[i];
553
554          /* Try to find an existing copy of this uniform in the pull
555           * constants if it was part of an array access already.
556           */
557          for (unsigned int j = 0; j < prog_data->nr_pull_params; j += 4) {
558             int matches;
559
560             for (matches = 0; matches < 4; matches++) {
561                if (prog_data->pull_param[j + matches] != values[matches])
562                   break;
563             }
564
565             if (matches == 4) {
566                pull_constant_loc[i / 4] = j / 4;
567                break;
568             }
569          }
570
571          if (pull_constant_loc[i / 4] == -1) {
572             assert(prog_data->nr_pull_params % 4 == 0);
573             pull_constant_loc[i / 4] = prog_data->nr_pull_params / 4;
574
575             for (int j = 0; j < 4; j++) {
576                prog_data->pull_param[prog_data->nr_pull_params++] = values[j];
577             }
578          }
579       }
580    }
581
582    /* Now actually rewrite usage of the things we've moved to pull
583     * constants.
584     */
585    foreach_list_safe(node, &this->instructions) {
586       vec4_instruction *inst = (vec4_instruction *)node;
587
588       for (int i = 0 ; i < 3; i++) {
589          if (inst->src[i].file != UNIFORM ||
590              pull_constant_loc[inst->src[i].reg] == -1)
591             continue;
592
593          int uniform = inst->src[i].reg;
594
595          dst_reg temp = dst_reg(this, glsl_type::vec4_type);
596
597          emit_pull_constant_load(inst, temp, inst->src[i],
598                                  pull_constant_loc[uniform]);
599
600          inst->src[i].file = temp.file;
601          inst->src[i].reg = temp.reg;
602          inst->src[i].reg_offset = temp.reg_offset;
603          inst->src[i].reladdr = NULL;
604       }
605    }
606
607    /* Repack push constants to remove the now-unused ones. */
608    pack_uniform_registers();
609 }
610
611 /**
612  * Sets the dependency control fields on instructions after register
613  * allocation and before the generator is run.
614  *
615  * When you have a sequence of instructions like:
616  *
617  * DP4 temp.x vertex uniform[0]
618  * DP4 temp.y vertex uniform[0]
619  * DP4 temp.z vertex uniform[0]
620  * DP4 temp.w vertex uniform[0]
621  *
622  * The hardware doesn't know that it can actually run the later instructions
623  * while the previous ones are in flight, producing stalls.  However, we have
624  * manual fields we can set in the instructions that let it do so.
625  */
626 void
627 vec4_visitor::opt_set_dependency_control()
628 {
629    vec4_instruction *last_grf_write[BRW_MAX_GRF];
630    uint8_t grf_channels_written[BRW_MAX_GRF];
631    vec4_instruction *last_mrf_write[BRW_MAX_GRF];
632    uint8_t mrf_channels_written[BRW_MAX_GRF];
633
634    cfg_t cfg(this);
635
636    assert(prog_data->total_grf ||
637           !"Must be called after register allocation");
638
639    for (int i = 0; i < cfg.num_blocks; i++) {
640       bblock_t *bblock = cfg.blocks[i];
641       vec4_instruction *inst;
642
643       memset(last_grf_write, 0, sizeof(last_grf_write));
644       memset(last_mrf_write, 0, sizeof(last_mrf_write));
645
646       for (inst = (vec4_instruction *)bblock->start;
647            inst != (vec4_instruction *)bblock->end->next;
648            inst = (vec4_instruction *)inst->next) {
649          /* If we read from a register that we were doing dependency control
650           * on, don't do dependency control across the read.
651           */
652          for (int i = 0; i < 3; i++) {
653             int reg = inst->src[i].reg + inst->src[i].reg_offset;
654             if (inst->src[i].file == GRF) {
655                last_grf_write[reg] = NULL;
656             } else if (inst->src[i].file == HW_REG) {
657                memset(last_grf_write, 0, sizeof(last_grf_write));
658                break;
659             }
660             assert(inst->src[i].file != MRF);
661          }
662
663          /* In the presence of send messages, totally interrupt dependency
664           * control.  They're long enough that the chance of dependency
665           * control around them just doesn't matter.
666           */
667          if (inst->mlen) {
668             memset(last_grf_write, 0, sizeof(last_grf_write));
669             memset(last_mrf_write, 0, sizeof(last_mrf_write));
670             continue;
671          }
672
673          /* It looks like setting dependency control on a predicated
674           * instruction hangs the GPU.
675           */
676          if (inst->predicate) {
677             memset(last_grf_write, 0, sizeof(last_grf_write));
678             memset(last_mrf_write, 0, sizeof(last_mrf_write));
679             continue;
680          }
681
682          /* Now, see if we can do dependency control for this instruction
683           * against a previous one writing to its destination.
684           */
685          int reg = inst->dst.reg + inst->dst.reg_offset;
686          if (inst->dst.file == GRF) {
687             if (last_grf_write[reg] &&
688                 !(inst->dst.writemask & grf_channels_written[reg])) {
689                last_grf_write[reg]->no_dd_clear = true;
690                inst->no_dd_check = true;
691             } else {
692                grf_channels_written[reg] = 0;
693             }
694
695             last_grf_write[reg] = inst;
696             grf_channels_written[reg] |= inst->dst.writemask;
697          } else if (inst->dst.file == MRF) {
698             if (last_mrf_write[reg] &&
699                 !(inst->dst.writemask & mrf_channels_written[reg])) {
700                last_mrf_write[reg]->no_dd_clear = true;
701                inst->no_dd_check = true;
702             } else {
703                mrf_channels_written[reg] = 0;
704             }
705
706             last_mrf_write[reg] = inst;
707             mrf_channels_written[reg] |= inst->dst.writemask;
708          } else if (inst->dst.reg == HW_REG) {
709             if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
710                memset(last_grf_write, 0, sizeof(last_grf_write));
711             if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
712                memset(last_mrf_write, 0, sizeof(last_mrf_write));
713          }
714       }
715    }
716 }
717
718 bool
719 vec4_instruction::can_reswizzle_dst(int dst_writemask,
720                                     int swizzle,
721                                     int swizzle_mask)
722 {
723    /* If this instruction sets anything not referenced by swizzle, then we'd
724     * totally break it when we reswizzle.
725     */
726    if (dst.writemask & ~swizzle_mask)
727       return false;
728
729    switch (opcode) {
730    case BRW_OPCODE_DP4:
731    case BRW_OPCODE_DP3:
732    case BRW_OPCODE_DP2:
733       return true;
734    default:
735       /* Check if there happens to be no reswizzling required. */
736       for (int c = 0; c < 4; c++) {
737          int bit = 1 << BRW_GET_SWZ(swizzle, c);
738          /* Skip components of the swizzle not used by the dst. */
739          if (!(dst_writemask & (1 << c)))
740             continue;
741
742          /* We don't do the reswizzling yet, so just sanity check that we
743           * don't have to.
744           */
745          if (bit != (1 << c))
746             return false;
747       }
748       return true;
749    }
750 }
751
752 /**
753  * For any channels in the swizzle's source that were populated by this
754  * instruction, rewrite the instruction to put the appropriate result directly
755  * in those channels.
756  *
757  * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
758  */
759 void
760 vec4_instruction::reswizzle_dst(int dst_writemask, int swizzle)
761 {
762    int new_writemask = 0;
763
764    switch (opcode) {
765    case BRW_OPCODE_DP4:
766    case BRW_OPCODE_DP3:
767    case BRW_OPCODE_DP2:
768       for (int c = 0; c < 4; c++) {
769          int bit = 1 << BRW_GET_SWZ(swizzle, c);
770          /* Skip components of the swizzle not used by the dst. */
771          if (!(dst_writemask & (1 << c)))
772             continue;
773          /* If we were populating this component, then populate the
774           * corresponding channel of the new dst.
775           */
776          if (dst.writemask & bit)
777             new_writemask |= (1 << c);
778       }
779       dst.writemask = new_writemask;
780       break;
781    default:
782       for (int c = 0; c < 4; c++) {
783          /* Skip components of the swizzle not used by the dst. */
784          if (!(dst_writemask & (1 << c)))
785             continue;
786
787          /* We don't do the reswizzling yet, so just sanity check that we
788           * don't have to.
789           */
790          assert((1 << BRW_GET_SWZ(swizzle, c)) == (1 << c));
791       }
792       break;
793    }
794 }
795
796 /*
797  * Tries to reduce extra MOV instructions by taking temporary GRFs that get
798  * just written and then MOVed into another reg and making the original write
799  * of the GRF write directly to the final destination instead.
800  */
801 bool
802 vec4_visitor::opt_register_coalesce()
803 {
804    bool progress = false;
805    int next_ip = 0;
806
807    calculate_live_intervals();
808
809    foreach_list_safe(node, &this->instructions) {
810       vec4_instruction *inst = (vec4_instruction *)node;
811
812       int ip = next_ip;
813       next_ip++;
814
815       if (inst->opcode != BRW_OPCODE_MOV ||
816           (inst->dst.file != GRF && inst->dst.file != MRF) ||
817           inst->predicate ||
818           inst->src[0].file != GRF ||
819           inst->dst.type != inst->src[0].type ||
820           inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
821          continue;
822
823       bool to_mrf = (inst->dst.file == MRF);
824
825       /* Can't coalesce this GRF if someone else was going to
826        * read it later.
827        */
828       if (this->virtual_grf_use[inst->src[0].reg] > ip)
829          continue;
830
831       /* We need to check interference with the final destination between this
832        * instruction and the earliest instruction involved in writing the GRF
833        * we're eliminating.  To do that, keep track of which of our source
834        * channels we've seen initialized.
835        */
836       bool chans_needed[4] = {false, false, false, false};
837       int chans_remaining = 0;
838       int swizzle_mask = 0;
839       for (int i = 0; i < 4; i++) {
840          int chan = BRW_GET_SWZ(inst->src[0].swizzle, i);
841
842          if (!(inst->dst.writemask & (1 << i)))
843             continue;
844
845          swizzle_mask |= (1 << chan);
846
847          if (!chans_needed[chan]) {
848             chans_needed[chan] = true;
849             chans_remaining++;
850          }
851       }
852
853       /* Now walk up the instruction stream trying to see if we can rewrite
854        * everything writing to the temporary to write into the destination
855        * instead.
856        */
857       vec4_instruction *scan_inst;
858       for (scan_inst = (vec4_instruction *)inst->prev;
859            scan_inst->prev != NULL;
860            scan_inst = (vec4_instruction *)scan_inst->prev) {
861          if (scan_inst->dst.file == GRF &&
862              scan_inst->dst.reg == inst->src[0].reg &&
863              scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
864             /* Found something writing to the reg we want to coalesce away. */
865             if (to_mrf) {
866                /* SEND instructions can't have MRF as a destination. */
867                if (scan_inst->mlen)
868                   break;
869
870                if (intel->gen == 6) {
871                   /* gen6 math instructions must have the destination be
872                    * GRF, so no compute-to-MRF for them.
873                    */
874                   if (scan_inst->is_math()) {
875                      break;
876                   }
877                }
878             }
879
880             /* If we can't handle the swizzle, bail. */
881             if (!scan_inst->can_reswizzle_dst(inst->dst.writemask,
882                                               inst->src[0].swizzle,
883                                               swizzle_mask)) {
884                break;
885             }
886
887             /* Mark which channels we found unconditional writes for. */
888             if (!scan_inst->predicate) {
889                for (int i = 0; i < 4; i++) {
890                   if (scan_inst->dst.writemask & (1 << i) &&
891                       chans_needed[i]) {
892                      chans_needed[i] = false;
893                      chans_remaining--;
894                   }
895                }
896             }
897
898             if (chans_remaining == 0)
899                break;
900          }
901
902          /* We don't handle flow control here.  Most computation of values
903           * that could be coalesced happens just before their use.
904           */
905          if (scan_inst->opcode == BRW_OPCODE_DO ||
906              scan_inst->opcode == BRW_OPCODE_WHILE ||
907              scan_inst->opcode == BRW_OPCODE_ELSE ||
908              scan_inst->opcode == BRW_OPCODE_ENDIF) {
909             break;
910          }
911
912          /* You can't read from an MRF, so if someone else reads our MRF's
913           * source GRF that we wanted to rewrite, that stops us.  If it's a
914           * GRF we're trying to coalesce to, we don't actually handle
915           * rewriting sources so bail in that case as well.
916           */
917          bool interfered = false;
918          for (int i = 0; i < 3; i++) {
919             if (scan_inst->src[i].file == GRF &&
920                 scan_inst->src[i].reg == inst->src[0].reg &&
921                 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
922                interfered = true;
923             }
924          }
925          if (interfered)
926             break;
927
928          /* If somebody else writes our destination here, we can't coalesce
929           * before that.
930           */
931          if (scan_inst->dst.file == inst->dst.file &&
932              scan_inst->dst.reg == inst->dst.reg) {
933             break;
934          }
935
936          /* Check for reads of the register we're trying to coalesce into.  We
937           * can't go rewriting instructions above that to put some other value
938           * in the register instead.
939           */
940          if (to_mrf && scan_inst->mlen > 0) {
941             if (inst->dst.reg >= scan_inst->base_mrf &&
942                 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
943                break;
944             }
945          } else {
946             for (int i = 0; i < 3; i++) {
947                if (scan_inst->src[i].file == inst->dst.file &&
948                    scan_inst->src[i].reg == inst->dst.reg &&
949                    scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
950                   interfered = true;
951                }
952             }
953             if (interfered)
954                break;
955          }
956       }
957
958       if (chans_remaining == 0) {
959          /* If we've made it here, we have an MOV we want to coalesce out, and
960           * a scan_inst pointing to the earliest instruction involved in
961           * computing the value.  Now go rewrite the instruction stream
962           * between the two.
963           */
964
965          while (scan_inst != inst) {
966             if (scan_inst->dst.file == GRF &&
967                 scan_inst->dst.reg == inst->src[0].reg &&
968                 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
969                scan_inst->reswizzle_dst(inst->dst.writemask,
970                                         inst->src[0].swizzle);
971                scan_inst->dst.file = inst->dst.file;
972                scan_inst->dst.reg = inst->dst.reg;
973                scan_inst->dst.reg_offset = inst->dst.reg_offset;
974                scan_inst->saturate |= inst->saturate;
975             }
976             scan_inst = (vec4_instruction *)scan_inst->next;
977          }
978          inst->remove();
979          progress = true;
980       }
981    }
982
983    if (progress)
984       live_intervals_valid = false;
985
986    return progress;
987 }
988
989 /**
990  * Splits virtual GRFs requesting more than one contiguous physical register.
991  *
992  * We initially create large virtual GRFs for temporary structures, arrays,
993  * and matrices, so that the dereference visitor functions can add reg_offsets
994  * to work their way down to the actual member being accessed.  But when it
995  * comes to optimization, we'd like to treat each register as individual
996  * storage if possible.
997  *
998  * So far, the only thing that might prevent splitting is a send message from
999  * a GRF on IVB.
1000  */
1001 void
1002 vec4_visitor::split_virtual_grfs()
1003 {
1004    int num_vars = this->virtual_grf_count;
1005    int new_virtual_grf[num_vars];
1006    bool split_grf[num_vars];
1007
1008    memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1009
1010    /* Try to split anything > 0 sized. */
1011    for (int i = 0; i < num_vars; i++) {
1012       split_grf[i] = this->virtual_grf_sizes[i] != 1;
1013    }
1014
1015    /* Check that the instructions are compatible with the registers we're trying
1016     * to split.
1017     */
1018    foreach_list(node, &this->instructions) {
1019       vec4_instruction *inst = (vec4_instruction *)node;
1020
1021       /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1022        * contiguous.  Assume that the GRF for the SEND is always in src[0].
1023        */
1024       if (inst->is_send_from_grf()) {
1025          split_grf[inst->src[0].reg] = false;
1026       }
1027    }
1028
1029    /* Allocate new space for split regs.  Note that the virtual
1030     * numbers will be contiguous.
1031     */
1032    for (int i = 0; i < num_vars; i++) {
1033       if (!split_grf[i])
1034          continue;
1035
1036       new_virtual_grf[i] = virtual_grf_alloc(1);
1037       for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
1038          int reg = virtual_grf_alloc(1);
1039          assert(reg == new_virtual_grf[i] + j - 1);
1040          (void) reg;
1041       }
1042       this->virtual_grf_sizes[i] = 1;
1043    }
1044
1045    foreach_list(node, &this->instructions) {
1046       vec4_instruction *inst = (vec4_instruction *)node;
1047
1048       if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1049           inst->dst.reg_offset != 0) {
1050          inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1051                           inst->dst.reg_offset - 1);
1052          inst->dst.reg_offset = 0;
1053       }
1054       for (int i = 0; i < 3; i++) {
1055          if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1056              inst->src[i].reg_offset != 0) {
1057             inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1058                                 inst->src[i].reg_offset - 1);
1059             inst->src[i].reg_offset = 0;
1060          }
1061       }
1062    }
1063    this->live_intervals_valid = false;
1064 }
1065
1066 void
1067 vec4_visitor::dump_instruction(vec4_instruction *inst)
1068 {
1069    printf("%s ", brw_instruction_name(inst->opcode));
1070
1071    switch (inst->dst.file) {
1072    case GRF:
1073       printf("vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1074       break;
1075    case MRF:
1076       printf("m%d", inst->dst.reg);
1077       break;
1078    case BAD_FILE:
1079       printf("(null)");
1080       break;
1081    default:
1082       printf("???");
1083       break;
1084    }
1085    if (inst->dst.writemask != WRITEMASK_XYZW) {
1086       printf(".");
1087       if (inst->dst.writemask & 1)
1088          printf("x");
1089       if (inst->dst.writemask & 2)
1090          printf("y");
1091       if (inst->dst.writemask & 4)
1092          printf("z");
1093       if (inst->dst.writemask & 8)
1094          printf("w");
1095    }
1096    printf(", ");
1097
1098    for (int i = 0; i < 3; i++) {
1099       switch (inst->src[i].file) {
1100       case GRF:
1101          printf("vgrf%d", inst->src[i].reg);
1102          break;
1103       case ATTR:
1104          printf("attr%d", inst->src[i].reg);
1105          break;
1106       case UNIFORM:
1107          printf("u%d", inst->src[i].reg);
1108          break;
1109       case IMM:
1110          switch (inst->src[i].type) {
1111          case BRW_REGISTER_TYPE_F:
1112             printf("%fF", inst->src[i].imm.f);
1113             break;
1114          case BRW_REGISTER_TYPE_D:
1115             printf("%dD", inst->src[i].imm.i);
1116             break;
1117          case BRW_REGISTER_TYPE_UD:
1118             printf("%uU", inst->src[i].imm.u);
1119             break;
1120          default:
1121             printf("???");
1122             break;
1123          }
1124          break;
1125       case BAD_FILE:
1126          printf("(null)");
1127          break;
1128       default:
1129          printf("???");
1130          break;
1131       }
1132
1133       if (inst->src[i].reg_offset)
1134          printf(".%d", inst->src[i].reg_offset);
1135
1136       static const char *chans[4] = {"x", "y", "z", "w"};
1137       printf(".");
1138       for (int c = 0; c < 4; c++) {
1139          printf("%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1140       }
1141
1142       if (i < 3)
1143          printf(", ");
1144    }
1145
1146    printf("\n");
1147 }
1148
1149 void
1150 vec4_visitor::dump_instructions()
1151 {
1152    int ip = 0;
1153    foreach_list_safe(node, &this->instructions) {
1154       vec4_instruction *inst = (vec4_instruction *)node;
1155       printf("%d: ", ip++);
1156       dump_instruction(inst);
1157    }
1158 }
1159
1160 /**
1161  * Replace each register of type ATTR in this->instructions with a reference
1162  * to a fixed HW register.
1163  */
1164 void
1165 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map)
1166 {
1167    foreach_list(node, &this->instructions) {
1168       vec4_instruction *inst = (vec4_instruction *)node;
1169
1170       /* We have to support ATTR as a destination for GL_FIXED fixup. */
1171       if (inst->dst.file == ATTR) {
1172          int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1173
1174          /* All attributes used in the shader need to have been assigned a
1175           * hardware register by the caller
1176           */
1177          assert(grf != 0);
1178
1179          struct brw_reg reg = brw_vec8_grf(grf, 0);
1180          reg.type = inst->dst.type;
1181          reg.dw1.bits.writemask = inst->dst.writemask;
1182
1183          inst->dst.file = HW_REG;
1184          inst->dst.fixed_hw_reg = reg;
1185       }
1186
1187       for (int i = 0; i < 3; i++) {
1188          if (inst->src[i].file != ATTR)
1189             continue;
1190
1191          int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1192
1193          /* All attributes used in the shader need to have been assigned a
1194           * hardware register by the caller
1195           */
1196          assert(grf != 0);
1197
1198          struct brw_reg reg = brw_vec8_grf(grf, 0);
1199          reg.dw1.bits.swizzle = inst->src[i].swizzle;
1200          reg.type = inst->src[i].type;
1201          if (inst->src[i].abs)
1202             reg = brw_abs(reg);
1203          if (inst->src[i].negate)
1204             reg = negate(reg);
1205
1206          inst->src[i].file = HW_REG;
1207          inst->src[i].fixed_hw_reg = reg;
1208       }
1209    }
1210 }
1211
1212 int
1213 vec4_vs_visitor::setup_attributes(int payload_reg)
1214 {
1215    int nr_attributes;
1216    int attribute_map[VERT_ATTRIB_MAX + 1];
1217    memset(attribute_map, 0, sizeof(attribute_map));
1218
1219    nr_attributes = 0;
1220    for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1221       if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1222          attribute_map[i] = payload_reg + nr_attributes;
1223          nr_attributes++;
1224       }
1225    }
1226
1227    /* VertexID is stored by the VF as the last vertex element, but we
1228     * don't represent it with a flag in inputs_read, so we call it
1229     * VERT_ATTRIB_MAX.
1230     */
1231    if (vs_prog_data->uses_vertexid) {
1232       attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1233       nr_attributes++;
1234    }
1235
1236    lower_attributes_to_hw_regs(attribute_map);
1237
1238    /* The BSpec says we always have to read at least one thing from
1239     * the VF, and it appears that the hardware wedges otherwise.
1240     */
1241    if (nr_attributes == 0)
1242       nr_attributes = 1;
1243
1244    prog_data->urb_read_length = (nr_attributes + 1) / 2;
1245
1246    unsigned vue_entries =
1247       MAX2(nr_attributes, prog_data->vue_map.num_slots);
1248
1249    if (intel->gen == 6)
1250       prog_data->urb_entry_size = ALIGN(vue_entries, 8) / 8;
1251    else
1252       prog_data->urb_entry_size = ALIGN(vue_entries, 4) / 4;
1253
1254    return payload_reg + nr_attributes;
1255 }
1256
1257 int
1258 vec4_visitor::setup_uniforms(int reg)
1259 {
1260    /* The pre-gen6 VS requires that some push constants get loaded no
1261     * matter what, or the GPU would hang.
1262     */
1263    if (intel->gen < 6 && this->uniforms == 0) {
1264       this->uniform_vector_size[this->uniforms] = 1;
1265
1266       for (unsigned int i = 0; i < 4; i++) {
1267          unsigned int slot = this->uniforms * 4 + i;
1268          static float zero = 0.0;
1269          prog_data->param[slot] = &zero;
1270       }
1271
1272       this->uniforms++;
1273       reg++;
1274    } else {
1275       reg += ALIGN(uniforms, 2) / 2;
1276    }
1277
1278    prog_data->nr_params = this->uniforms * 4;
1279
1280    prog_data->curb_read_length = reg - 1;
1281
1282    return reg;
1283 }
1284
1285 void
1286 vec4_visitor::setup_payload(void)
1287 {
1288    int reg = 0;
1289
1290    /* The payload always contains important data in g0, which contains
1291     * the URB handles that are passed on to the URB write at the end
1292     * of the thread.  So, we always start push constants at g1.
1293     */
1294    reg++;
1295
1296    reg = setup_uniforms(reg);
1297
1298    reg = setup_attributes(reg);
1299
1300    this->first_non_payload_grf = reg;
1301 }
1302
1303 src_reg
1304 vec4_visitor::get_timestamp()
1305 {
1306    assert(intel->gen >= 7);
1307
1308    src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1309                                 BRW_ARF_TIMESTAMP,
1310                                 0,
1311                                 BRW_REGISTER_TYPE_UD,
1312                                 BRW_VERTICAL_STRIDE_0,
1313                                 BRW_WIDTH_4,
1314                                 BRW_HORIZONTAL_STRIDE_4,
1315                                 BRW_SWIZZLE_XYZW,
1316                                 WRITEMASK_XYZW));
1317
1318    dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1319
1320    vec4_instruction *mov = emit(MOV(dst, ts));
1321    /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1322     * even if it's not enabled in the dispatch.
1323     */
1324    mov->force_writemask_all = true;
1325
1326    return src_reg(dst);
1327 }
1328
1329 void
1330 vec4_visitor::emit_shader_time_begin()
1331 {
1332    current_annotation = "shader time start";
1333    shader_start_time = get_timestamp();
1334 }
1335
1336 void
1337 vec4_visitor::emit_shader_time_end()
1338 {
1339    current_annotation = "shader time end";
1340    src_reg shader_end_time = get_timestamp();
1341
1342
1343    /* Check that there weren't any timestamp reset events (assuming these
1344     * were the only two timestamp reads that happened).
1345     */
1346    src_reg reset_end = shader_end_time;
1347    reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1348    vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1349    test->conditional_mod = BRW_CONDITIONAL_Z;
1350
1351    emit(IF(BRW_PREDICATE_NORMAL));
1352
1353    /* Take the current timestamp and get the delta. */
1354    shader_start_time.negate = true;
1355    dst_reg diff = dst_reg(this, glsl_type::uint_type);
1356    emit(ADD(diff, shader_start_time, shader_end_time));
1357
1358    /* If there were no instructions between the two timestamp gets, the diff
1359     * is 2 cycles.  Remove that overhead, so I can forget about that when
1360     * trying to determine the time taken for single instructions.
1361     */
1362    emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1363
1364    emit_shader_time_write(ST_VS, src_reg(diff));
1365    emit_shader_time_write(ST_VS_WRITTEN, src_reg(1u));
1366    emit(BRW_OPCODE_ELSE);
1367    emit_shader_time_write(ST_VS_RESET, src_reg(1u));
1368    emit(BRW_OPCODE_ENDIF);
1369 }
1370
1371 void
1372 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type,
1373                                      src_reg value)
1374 {
1375    int shader_time_index =
1376       brw_get_shader_time_index(brw, shader_prog, prog, type);
1377
1378    dst_reg dst =
1379       dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1380
1381    dst_reg offset = dst;
1382    dst_reg time = dst;
1383    time.reg_offset++;
1384
1385    offset.type = BRW_REGISTER_TYPE_UD;
1386    emit(MOV(offset, src_reg(shader_time_index * SHADER_TIME_STRIDE)));
1387
1388    time.type = BRW_REGISTER_TYPE_UD;
1389    emit(MOV(time, src_reg(value)));
1390
1391    emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1392 }
1393
1394 bool
1395 vec4_visitor::run()
1396 {
1397    sanity_param_count = prog->Parameters->NumParameters;
1398
1399    if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1400       emit_shader_time_begin();
1401
1402    emit_prolog();
1403
1404    /* Generate VS IR for main().  (the visitor only descends into
1405     * functions called "main").
1406     */
1407    if (shader) {
1408       visit_instructions(shader->ir);
1409    } else {
1410       emit_program_code();
1411    }
1412    base_ir = NULL;
1413
1414    if (key->userclip_active && !key->uses_clip_distance)
1415       setup_uniform_clipplane_values();
1416
1417    emit_thread_end();
1418
1419    /* Before any optimization, push array accesses out to scratch
1420     * space where we need them to be.  This pass may allocate new
1421     * virtual GRFs, so we want to do it early.  It also makes sure
1422     * that we have reladdr computations available for CSE, since we'll
1423     * often do repeated subexpressions for those.
1424     */
1425    if (shader) {
1426       move_grf_array_access_to_scratch();
1427       move_uniform_array_access_to_pull_constants();
1428    } else {
1429       /* The ARB_vertex_program frontend emits pull constant loads directly
1430        * rather than using reladdr, so we don't need to walk through all the
1431        * instructions looking for things to move.  There isn't anything.
1432        *
1433        * We do still need to split things to vec4 size.
1434        */
1435       split_uniform_registers();
1436    }
1437    pack_uniform_registers();
1438    move_push_constants_to_pull_constants();
1439    split_virtual_grfs();
1440
1441    bool progress;
1442    do {
1443       progress = false;
1444       progress = dead_code_eliminate() || progress;
1445       progress = opt_copy_propagation() || progress;
1446       progress = opt_algebraic() || progress;
1447       progress = opt_register_coalesce() || progress;
1448    } while (progress);
1449
1450
1451    if (failed)
1452       return false;
1453
1454    setup_payload();
1455
1456    if (false) {
1457       /* Debug of register spilling: Go spill everything. */
1458       const int grf_count = virtual_grf_count;
1459       float spill_costs[virtual_grf_count];
1460       bool no_spill[virtual_grf_count];
1461       evaluate_spill_costs(spill_costs, no_spill);
1462       for (int i = 0; i < grf_count; i++) {
1463          if (no_spill[i])
1464             continue;
1465          spill_reg(i);
1466       }
1467    }
1468
1469    while (!reg_allocate()) {
1470       if (failed)
1471          break;
1472    }
1473
1474    opt_set_dependency_control();
1475
1476    /* If any state parameters were appended, then ParameterValues could have
1477     * been realloced, in which case the driver uniform storage set up by
1478     * _mesa_associate_uniform_storage() would point to freed memory.  Make
1479     * sure that didn't happen.
1480     */
1481    assert(sanity_param_count == prog->Parameters->NumParameters);
1482
1483    return !failed;
1484 }
1485
1486 } /* namespace brw */
1487
1488 extern "C" {
1489
1490 /**
1491  * Compile a vertex shader.
1492  *
1493  * Returns the final assembly and the program's size.
1494  */
1495 const unsigned *
1496 brw_vs_emit(struct brw_context *brw,
1497             struct gl_shader_program *prog,
1498             struct brw_vs_compile *c,
1499             struct brw_vs_prog_data *prog_data,
1500             void *mem_ctx,
1501             unsigned *final_assembly_size)
1502 {
1503    struct intel_context *intel = &brw->intel;
1504    bool start_busy = false;
1505    float start_time = 0;
1506
1507    if (unlikely(intel->perf_debug)) {
1508       start_busy = (intel->batch.last_bo &&
1509                     drm_intel_bo_busy(intel->batch.last_bo));
1510       start_time = get_time();
1511    }
1512
1513    struct brw_shader *shader = NULL;
1514    if (prog)
1515       shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
1516
1517    if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
1518       if (prog) {
1519          printf("GLSL IR for native vertex shader %d:\n", prog->Name);
1520          _mesa_print_ir(shader->ir, NULL);
1521          printf("\n\n");
1522       } else {
1523          printf("ARB_vertex_program %d for native vertex shader\n",
1524                 c->vp->program.Base.Id);
1525          _mesa_print_program(&c->vp->program.Base);
1526       }
1527    }
1528
1529    vec4_vs_visitor v(brw, c, prog_data, prog, shader, mem_ctx);
1530    if (!v.run()) {
1531       if (prog) {
1532          prog->LinkStatus = false;
1533          ralloc_strcat(&prog->InfoLog, v.fail_msg);
1534       }
1535
1536       _mesa_problem(NULL, "Failed to compile vertex shader: %s\n",
1537                     v.fail_msg);
1538
1539       return NULL;
1540    }
1541
1542    vec4_generator g(brw, prog, &c->vp->program.Base, mem_ctx,
1543                     INTEL_DEBUG & DEBUG_VS);
1544    const unsigned *generated =g.generate_assembly(&v.instructions,
1545                                                   final_assembly_size);
1546
1547    if (unlikely(intel->perf_debug) && shader) {
1548       if (shader->compiled_once) {
1549          brw_vs_debug_recompile(brw, prog, &c->key);
1550       }
1551       if (start_busy && !drm_intel_bo_busy(intel->batch.last_bo)) {
1552          perf_debug("VS compile took %.03f ms and stalled the GPU\n",
1553                     (get_time() - start_time) * 1000);
1554       }
1555       shader->compiled_once = true;
1556    }
1557
1558    return generated;
1559 }
1560
1561 } /* extern "C" */