1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2011 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#include "brw_vec4.h"
25b8e80941Smrg#include "brw_fs.h"
26b8e80941Smrg#include "brw_cfg.h"
27b8e80941Smrg#include "brw_nir.h"
28b8e80941Smrg#include "brw_vec4_builder.h"
29b8e80941Smrg#include "brw_vec4_live_variables.h"
30b8e80941Smrg#include "brw_vec4_vs.h"
31b8e80941Smrg#include "brw_dead_control_flow.h"
32b8e80941Smrg#include "dev/gen_debug.h"
33b8e80941Smrg#include "program/prog_parameter.h"
34b8e80941Smrg#include "util/u_math.h"
35b8e80941Smrg
36b8e80941Smrg#define MAX_INSTRUCTION (1 << 30)
37b8e80941Smrg
38b8e80941Smrgusing namespace brw;
39b8e80941Smrg
40b8e80941Smrgnamespace brw {
41b8e80941Smrg
42b8e80941Smrgvoid
43b8e80941Smrgsrc_reg::init()
44b8e80941Smrg{
45b8e80941Smrg   memset((void*)this, 0, sizeof(*this));
46b8e80941Smrg   this->file = BAD_FILE;
47b8e80941Smrg   this->type = BRW_REGISTER_TYPE_UD;
48b8e80941Smrg}
49b8e80941Smrg
50b8e80941Smrgsrc_reg::src_reg(enum brw_reg_file file, int nr, const glsl_type *type)
51b8e80941Smrg{
52b8e80941Smrg   init();
53b8e80941Smrg
54b8e80941Smrg   this->file = file;
55b8e80941Smrg   this->nr = nr;
56b8e80941Smrg   if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
57b8e80941Smrg      this->swizzle = brw_swizzle_for_size(type->vector_elements);
58b8e80941Smrg   else
59b8e80941Smrg      this->swizzle = BRW_SWIZZLE_XYZW;
60b8e80941Smrg   if (type)
61b8e80941Smrg      this->type = brw_type_for_base_type(type);
62b8e80941Smrg}
63b8e80941Smrg
64b8e80941Smrg/** Generic unset register constructor. */
65b8e80941Smrgsrc_reg::src_reg()
66b8e80941Smrg{
67b8e80941Smrg   init();
68b8e80941Smrg}
69b8e80941Smrg
70b8e80941Smrgsrc_reg::src_reg(struct ::brw_reg reg) :
71b8e80941Smrg   backend_reg(reg)
72b8e80941Smrg{
73b8e80941Smrg   this->offset = 0;
74b8e80941Smrg   this->reladdr = NULL;
75b8e80941Smrg}
76b8e80941Smrg
77b8e80941Smrgsrc_reg::src_reg(const dst_reg &reg) :
78b8e80941Smrg   backend_reg(reg)
79b8e80941Smrg{
80b8e80941Smrg   this->reladdr = reg.reladdr;
81b8e80941Smrg   this->swizzle = brw_swizzle_for_mask(reg.writemask);
82b8e80941Smrg}
83b8e80941Smrg
84b8e80941Smrgvoid
85b8e80941Smrgdst_reg::init()
86b8e80941Smrg{
87b8e80941Smrg   memset((void*)this, 0, sizeof(*this));
88b8e80941Smrg   this->file = BAD_FILE;
89b8e80941Smrg   this->type = BRW_REGISTER_TYPE_UD;
90b8e80941Smrg   this->writemask = WRITEMASK_XYZW;
91b8e80941Smrg}
92b8e80941Smrg
93b8e80941Smrgdst_reg::dst_reg()
94b8e80941Smrg{
95b8e80941Smrg   init();
96b8e80941Smrg}
97b8e80941Smrg
98b8e80941Smrgdst_reg::dst_reg(enum brw_reg_file file, int nr)
99b8e80941Smrg{
100b8e80941Smrg   init();
101b8e80941Smrg
102b8e80941Smrg   this->file = file;
103b8e80941Smrg   this->nr = nr;
104b8e80941Smrg}
105b8e80941Smrg
106b8e80941Smrgdst_reg::dst_reg(enum brw_reg_file file, int nr, const glsl_type *type,
107b8e80941Smrg                 unsigned writemask)
108b8e80941Smrg{
109b8e80941Smrg   init();
110b8e80941Smrg
111b8e80941Smrg   this->file = file;
112b8e80941Smrg   this->nr = nr;
113b8e80941Smrg   this->type = brw_type_for_base_type(type);
114b8e80941Smrg   this->writemask = writemask;
115b8e80941Smrg}
116b8e80941Smrg
117b8e80941Smrgdst_reg::dst_reg(enum brw_reg_file file, int nr, brw_reg_type type,
118b8e80941Smrg                 unsigned writemask)
119b8e80941Smrg{
120b8e80941Smrg   init();
121b8e80941Smrg
122b8e80941Smrg   this->file = file;
123b8e80941Smrg   this->nr = nr;
124b8e80941Smrg   this->type = type;
125b8e80941Smrg   this->writemask = writemask;
126b8e80941Smrg}
127b8e80941Smrg
128b8e80941Smrgdst_reg::dst_reg(struct ::brw_reg reg) :
129b8e80941Smrg   backend_reg(reg)
130b8e80941Smrg{
131b8e80941Smrg   this->offset = 0;
132b8e80941Smrg   this->reladdr = NULL;
133b8e80941Smrg}
134b8e80941Smrg
135b8e80941Smrgdst_reg::dst_reg(const src_reg &reg) :
136b8e80941Smrg   backend_reg(reg)
137b8e80941Smrg{
138b8e80941Smrg   this->writemask = brw_mask_for_swizzle(reg.swizzle);
139b8e80941Smrg   this->reladdr = reg.reladdr;
140b8e80941Smrg}
141b8e80941Smrg
142b8e80941Smrgbool
143b8e80941Smrgdst_reg::equals(const dst_reg &r) const
144b8e80941Smrg{
145b8e80941Smrg   return (this->backend_reg::equals(r) &&
146b8e80941Smrg           (reladdr == r.reladdr ||
147b8e80941Smrg            (reladdr && r.reladdr && reladdr->equals(*r.reladdr))));
148b8e80941Smrg}
149b8e80941Smrg
150b8e80941Smrgbool
151b8e80941Smrgvec4_instruction::is_send_from_grf()
152b8e80941Smrg{
153b8e80941Smrg   switch (opcode) {
154b8e80941Smrg   case SHADER_OPCODE_SHADER_TIME_ADD:
155b8e80941Smrg   case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
156b8e80941Smrg   case VEC4_OPCODE_UNTYPED_ATOMIC:
157b8e80941Smrg   case VEC4_OPCODE_UNTYPED_SURFACE_READ:
158b8e80941Smrg   case VEC4_OPCODE_UNTYPED_SURFACE_WRITE:
159b8e80941Smrg   case VEC4_OPCODE_URB_READ:
160b8e80941Smrg   case TCS_OPCODE_URB_WRITE:
161b8e80941Smrg   case TCS_OPCODE_RELEASE_INPUT:
162b8e80941Smrg   case SHADER_OPCODE_BARRIER:
163b8e80941Smrg      return true;
164b8e80941Smrg   default:
165b8e80941Smrg      return false;
166b8e80941Smrg   }
167b8e80941Smrg}
168b8e80941Smrg
169b8e80941Smrg/**
170b8e80941Smrg * Returns true if this instruction's sources and destinations cannot
171b8e80941Smrg * safely be the same register.
172b8e80941Smrg *
173b8e80941Smrg * In most cases, a register can be written over safely by the same
174b8e80941Smrg * instruction that is its last use.  For a single instruction, the
175b8e80941Smrg * sources are dereferenced before writing of the destination starts
176b8e80941Smrg * (naturally).
177b8e80941Smrg *
178b8e80941Smrg * However, there are a few cases where this can be problematic:
179b8e80941Smrg *
180b8e80941Smrg * - Virtual opcodes that translate to multiple instructions in the
181b8e80941Smrg *   code generator: if src == dst and one instruction writes the
182b8e80941Smrg *   destination before a later instruction reads the source, then
183b8e80941Smrg *   src will have been clobbered.
184b8e80941Smrg *
185b8e80941Smrg * The register allocator uses this information to set up conflicts between
186b8e80941Smrg * GRF sources and the destination.
187b8e80941Smrg */
188b8e80941Smrgbool
189b8e80941Smrgvec4_instruction::has_source_and_destination_hazard() const
190b8e80941Smrg{
191b8e80941Smrg   switch (opcode) {
192b8e80941Smrg   case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
193b8e80941Smrg   case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
194b8e80941Smrg   case TES_OPCODE_ADD_INDIRECT_URB_OFFSET:
195b8e80941Smrg      return true;
196b8e80941Smrg   default:
197b8e80941Smrg      /* 8-wide compressed DF operations are executed as two 4-wide operations,
198b8e80941Smrg       * so we have a src/dst hazard if the first half of the instruction
199b8e80941Smrg       * overwrites the source of the second half. Prevent this by marking
200b8e80941Smrg       * compressed instructions as having src/dst hazards, so the register
201b8e80941Smrg       * allocator assigns safe register regions for dst and srcs.
202b8e80941Smrg       */
203b8e80941Smrg      return size_written > REG_SIZE;
204b8e80941Smrg   }
205b8e80941Smrg}
206b8e80941Smrg
207b8e80941Smrgunsigned
208b8e80941Smrgvec4_instruction::size_read(unsigned arg) const
209b8e80941Smrg{
210b8e80941Smrg   switch (opcode) {
211b8e80941Smrg   case SHADER_OPCODE_SHADER_TIME_ADD:
212b8e80941Smrg   case VEC4_OPCODE_UNTYPED_ATOMIC:
213b8e80941Smrg   case VEC4_OPCODE_UNTYPED_SURFACE_READ:
214b8e80941Smrg   case VEC4_OPCODE_UNTYPED_SURFACE_WRITE:
215b8e80941Smrg   case TCS_OPCODE_URB_WRITE:
216b8e80941Smrg      if (arg == 0)
217b8e80941Smrg         return mlen * REG_SIZE;
218b8e80941Smrg      break;
219b8e80941Smrg   case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
220b8e80941Smrg      if (arg == 1)
221b8e80941Smrg         return mlen * REG_SIZE;
222b8e80941Smrg      break;
223b8e80941Smrg   default:
224b8e80941Smrg      break;
225b8e80941Smrg   }
226b8e80941Smrg
227b8e80941Smrg   switch (src[arg].file) {
228b8e80941Smrg   case BAD_FILE:
229b8e80941Smrg      return 0;
230b8e80941Smrg   case IMM:
231b8e80941Smrg   case UNIFORM:
232b8e80941Smrg      return 4 * type_sz(src[arg].type);
233b8e80941Smrg   default:
234b8e80941Smrg      /* XXX - Represent actual vertical stride. */
235b8e80941Smrg      return exec_size * type_sz(src[arg].type);
236b8e80941Smrg   }
237b8e80941Smrg}
238b8e80941Smrg
239b8e80941Smrgbool
240b8e80941Smrgvec4_instruction::can_do_source_mods(const struct gen_device_info *devinfo)
241b8e80941Smrg{
242b8e80941Smrg   if (devinfo->gen == 6 && is_math())
243b8e80941Smrg      return false;
244b8e80941Smrg
245b8e80941Smrg   if (is_send_from_grf())
246b8e80941Smrg      return false;
247b8e80941Smrg
248b8e80941Smrg   if (!backend_instruction::can_do_source_mods())
249b8e80941Smrg      return false;
250b8e80941Smrg
251b8e80941Smrg   return true;
252b8e80941Smrg}
253b8e80941Smrg
254b8e80941Smrgbool
255b8e80941Smrgvec4_instruction::can_do_cmod()
256b8e80941Smrg{
257b8e80941Smrg   if (!backend_instruction::can_do_cmod())
258b8e80941Smrg      return false;
259b8e80941Smrg
260b8e80941Smrg   /* The accumulator result appears to get used for the conditional modifier
261b8e80941Smrg    * generation.  When negating a UD value, there is a 33rd bit generated for
262b8e80941Smrg    * the sign in the accumulator value, so now you can't check, for example,
263b8e80941Smrg    * equality with a 32-bit value.  See piglit fs-op-neg-uvec4.
264b8e80941Smrg    */
265b8e80941Smrg   for (unsigned i = 0; i < 3; i++) {
266b8e80941Smrg      if (src[i].file != BAD_FILE &&
267b8e80941Smrg          type_is_unsigned_int(src[i].type) && src[i].negate)
268b8e80941Smrg         return false;
269b8e80941Smrg   }
270b8e80941Smrg
271b8e80941Smrg   return true;
272b8e80941Smrg}
273b8e80941Smrg
274b8e80941Smrgbool
275b8e80941Smrgvec4_instruction::can_do_writemask(const struct gen_device_info *devinfo)
276b8e80941Smrg{
277b8e80941Smrg   switch (opcode) {
278b8e80941Smrg   case SHADER_OPCODE_GEN4_SCRATCH_READ:
279b8e80941Smrg   case VEC4_OPCODE_DOUBLE_TO_F32:
280b8e80941Smrg   case VEC4_OPCODE_DOUBLE_TO_D32:
281b8e80941Smrg   case VEC4_OPCODE_DOUBLE_TO_U32:
282b8e80941Smrg   case VEC4_OPCODE_TO_DOUBLE:
283b8e80941Smrg   case VEC4_OPCODE_PICK_LOW_32BIT:
284b8e80941Smrg   case VEC4_OPCODE_PICK_HIGH_32BIT:
285b8e80941Smrg   case VEC4_OPCODE_SET_LOW_32BIT:
286b8e80941Smrg   case VEC4_OPCODE_SET_HIGH_32BIT:
287b8e80941Smrg   case VS_OPCODE_PULL_CONSTANT_LOAD:
288b8e80941Smrg   case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
289b8e80941Smrg   case VS_OPCODE_SET_SIMD4X2_HEADER_GEN9:
290b8e80941Smrg   case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
291b8e80941Smrg   case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
292b8e80941Smrg   case TES_OPCODE_CREATE_INPUT_READ_HEADER:
293b8e80941Smrg   case TES_OPCODE_ADD_INDIRECT_URB_OFFSET:
294b8e80941Smrg   case VEC4_OPCODE_URB_READ:
295b8e80941Smrg   case SHADER_OPCODE_MOV_INDIRECT:
296b8e80941Smrg      return false;
297b8e80941Smrg   default:
298b8e80941Smrg      /* The MATH instruction on Gen6 only executes in align1 mode, which does
299b8e80941Smrg       * not support writemasking.
300b8e80941Smrg       */
301b8e80941Smrg      if (devinfo->gen == 6 && is_math())
302b8e80941Smrg         return false;
303b8e80941Smrg
304b8e80941Smrg      if (is_tex())
305b8e80941Smrg         return false;
306b8e80941Smrg
307b8e80941Smrg      return true;
308b8e80941Smrg   }
309b8e80941Smrg}
310b8e80941Smrg
311b8e80941Smrgbool
312b8e80941Smrgvec4_instruction::can_change_types() const
313b8e80941Smrg{
314b8e80941Smrg   return dst.type == src[0].type &&
315b8e80941Smrg          !src[0].abs && !src[0].negate && !saturate &&
316b8e80941Smrg          (opcode == BRW_OPCODE_MOV ||
317b8e80941Smrg           (opcode == BRW_OPCODE_SEL &&
318b8e80941Smrg            dst.type == src[1].type &&
319b8e80941Smrg            predicate != BRW_PREDICATE_NONE &&
320b8e80941Smrg            !src[1].abs && !src[1].negate));
321b8e80941Smrg}
322b8e80941Smrg
323b8e80941Smrg/**
324b8e80941Smrg * Returns how many MRFs an opcode will write over.
325b8e80941Smrg *
326b8e80941Smrg * Note that this is not the 0 or 1 implied writes in an actual gen
327b8e80941Smrg * instruction -- the generate_* functions generate additional MOVs
328b8e80941Smrg * for setup.
329b8e80941Smrg */
330b8e80941Smrgint
331b8e80941Smrgvec4_visitor::implied_mrf_writes(vec4_instruction *inst)
332b8e80941Smrg{
333b8e80941Smrg   if (inst->mlen == 0 || inst->is_send_from_grf())
334b8e80941Smrg      return 0;
335b8e80941Smrg
336b8e80941Smrg   switch (inst->opcode) {
337b8e80941Smrg   case SHADER_OPCODE_RCP:
338b8e80941Smrg   case SHADER_OPCODE_RSQ:
339b8e80941Smrg   case SHADER_OPCODE_SQRT:
340b8e80941Smrg   case SHADER_OPCODE_EXP2:
341b8e80941Smrg   case SHADER_OPCODE_LOG2:
342b8e80941Smrg   case SHADER_OPCODE_SIN:
343b8e80941Smrg   case SHADER_OPCODE_COS:
344b8e80941Smrg      return 1;
345b8e80941Smrg   case SHADER_OPCODE_INT_QUOTIENT:
346b8e80941Smrg   case SHADER_OPCODE_INT_REMAINDER:
347b8e80941Smrg   case SHADER_OPCODE_POW:
348b8e80941Smrg   case TCS_OPCODE_THREAD_END:
349b8e80941Smrg      return 2;
350b8e80941Smrg   case VS_OPCODE_URB_WRITE:
351b8e80941Smrg      return 1;
352b8e80941Smrg   case VS_OPCODE_PULL_CONSTANT_LOAD:
353b8e80941Smrg      return 2;
354b8e80941Smrg   case SHADER_OPCODE_GEN4_SCRATCH_READ:
355b8e80941Smrg      return 2;
356b8e80941Smrg   case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
357b8e80941Smrg      return 3;
358b8e80941Smrg   case GS_OPCODE_URB_WRITE:
359b8e80941Smrg   case GS_OPCODE_URB_WRITE_ALLOCATE:
360b8e80941Smrg   case GS_OPCODE_THREAD_END:
361b8e80941Smrg      return 0;
362b8e80941Smrg   case GS_OPCODE_FF_SYNC:
363b8e80941Smrg      return 1;
364b8e80941Smrg   case TCS_OPCODE_URB_WRITE:
365b8e80941Smrg      return 0;
366b8e80941Smrg   case SHADER_OPCODE_SHADER_TIME_ADD:
367b8e80941Smrg      return 0;
368b8e80941Smrg   case SHADER_OPCODE_TEX:
369b8e80941Smrg   case SHADER_OPCODE_TXL:
370b8e80941Smrg   case SHADER_OPCODE_TXD:
371b8e80941Smrg   case SHADER_OPCODE_TXF:
372b8e80941Smrg   case SHADER_OPCODE_TXF_CMS:
373b8e80941Smrg   case SHADER_OPCODE_TXF_CMS_W:
374b8e80941Smrg   case SHADER_OPCODE_TXF_MCS:
375b8e80941Smrg   case SHADER_OPCODE_TXS:
376b8e80941Smrg   case SHADER_OPCODE_TG4:
377b8e80941Smrg   case SHADER_OPCODE_TG4_OFFSET:
378b8e80941Smrg   case SHADER_OPCODE_SAMPLEINFO:
379b8e80941Smrg   case SHADER_OPCODE_GET_BUFFER_SIZE:
380b8e80941Smrg      return inst->header_size;
381b8e80941Smrg   default:
382b8e80941Smrg      unreachable("not reached");
383b8e80941Smrg   }
384b8e80941Smrg}
385b8e80941Smrg
386b8e80941Smrgbool
387b8e80941Smrgsrc_reg::equals(const src_reg &r) const
388b8e80941Smrg{
389b8e80941Smrg   return (this->backend_reg::equals(r) &&
390b8e80941Smrg	   !reladdr && !r.reladdr);
391b8e80941Smrg}
392b8e80941Smrg
393b8e80941Smrgbool
394b8e80941Smrgsrc_reg::negative_equals(const src_reg &r) const
395b8e80941Smrg{
396b8e80941Smrg   return this->backend_reg::negative_equals(r) &&
397b8e80941Smrg          !reladdr && !r.reladdr;
398b8e80941Smrg}
399b8e80941Smrg
400b8e80941Smrgbool
401b8e80941Smrgvec4_visitor::opt_vector_float()
402b8e80941Smrg{
403b8e80941Smrg   bool progress = false;
404b8e80941Smrg
405b8e80941Smrg   foreach_block(block, cfg) {
406b8e80941Smrg      unsigned last_reg = ~0u, last_offset = ~0u;
407b8e80941Smrg      enum brw_reg_file last_reg_file = BAD_FILE;
408b8e80941Smrg
409b8e80941Smrg      uint8_t imm[4] = { 0 };
410b8e80941Smrg      int inst_count = 0;
411b8e80941Smrg      vec4_instruction *imm_inst[4];
412b8e80941Smrg      unsigned writemask = 0;
413b8e80941Smrg      enum brw_reg_type dest_type = BRW_REGISTER_TYPE_F;
414b8e80941Smrg
415b8e80941Smrg      foreach_inst_in_block_safe(vec4_instruction, inst, block) {
416b8e80941Smrg         int vf = -1;
417b8e80941Smrg         enum brw_reg_type need_type = BRW_REGISTER_TYPE_LAST;
418b8e80941Smrg
419b8e80941Smrg         /* Look for unconditional MOVs from an immediate with a partial
420b8e80941Smrg          * writemask.  Skip type-conversion MOVs other than integer 0,
421b8e80941Smrg          * where the type doesn't matter.  See if the immediate can be
422b8e80941Smrg          * represented as a VF.
423b8e80941Smrg          */
424b8e80941Smrg         if (inst->opcode == BRW_OPCODE_MOV &&
425b8e80941Smrg             inst->src[0].file == IMM &&
426b8e80941Smrg             inst->predicate == BRW_PREDICATE_NONE &&
427b8e80941Smrg             inst->dst.writemask != WRITEMASK_XYZW &&
428b8e80941Smrg             type_sz(inst->src[0].type) < 8 &&
429b8e80941Smrg             (inst->src[0].type == inst->dst.type || inst->src[0].d == 0)) {
430b8e80941Smrg
431b8e80941Smrg            vf = brw_float_to_vf(inst->src[0].d);
432b8e80941Smrg            need_type = BRW_REGISTER_TYPE_D;
433b8e80941Smrg
434b8e80941Smrg            if (vf == -1) {
435b8e80941Smrg               vf = brw_float_to_vf(inst->src[0].f);
436b8e80941Smrg               need_type = BRW_REGISTER_TYPE_F;
437b8e80941Smrg            }
438b8e80941Smrg         } else {
439b8e80941Smrg            last_reg = ~0u;
440b8e80941Smrg         }
441b8e80941Smrg
442b8e80941Smrg         /* If this wasn't a MOV, or the destination register doesn't match,
443b8e80941Smrg          * or we have to switch destination types, then this breaks our
444b8e80941Smrg          * sequence.  Combine anything we've accumulated so far.
445b8e80941Smrg          */
446b8e80941Smrg         if (last_reg != inst->dst.nr ||
447b8e80941Smrg             last_offset != inst->dst.offset ||
448b8e80941Smrg             last_reg_file != inst->dst.file ||
449b8e80941Smrg             (vf > 0 && dest_type != need_type)) {
450b8e80941Smrg
451b8e80941Smrg            if (inst_count > 1) {
452b8e80941Smrg               unsigned vf;
453b8e80941Smrg               memcpy(&vf, imm, sizeof(vf));
454b8e80941Smrg               vec4_instruction *mov = MOV(imm_inst[0]->dst, brw_imm_vf(vf));
455b8e80941Smrg               mov->dst.type = dest_type;
456b8e80941Smrg               mov->dst.writemask = writemask;
457b8e80941Smrg               inst->insert_before(block, mov);
458b8e80941Smrg
459b8e80941Smrg               for (int i = 0; i < inst_count; i++) {
460b8e80941Smrg                  imm_inst[i]->remove(block);
461b8e80941Smrg               }
462b8e80941Smrg
463b8e80941Smrg               progress = true;
464b8e80941Smrg            }
465b8e80941Smrg
466b8e80941Smrg            inst_count = 0;
467b8e80941Smrg            last_reg = ~0u;;
468b8e80941Smrg            writemask = 0;
469b8e80941Smrg            dest_type = BRW_REGISTER_TYPE_F;
470b8e80941Smrg
471b8e80941Smrg            for (int i = 0; i < 4; i++) {
472b8e80941Smrg               imm[i] = 0;
473b8e80941Smrg            }
474b8e80941Smrg         }
475b8e80941Smrg
476b8e80941Smrg         /* Record this instruction's value (if it was representable). */
477b8e80941Smrg         if (vf != -1) {
478b8e80941Smrg            if ((inst->dst.writemask & WRITEMASK_X) != 0)
479b8e80941Smrg               imm[0] = vf;
480b8e80941Smrg            if ((inst->dst.writemask & WRITEMASK_Y) != 0)
481b8e80941Smrg               imm[1] = vf;
482b8e80941Smrg            if ((inst->dst.writemask & WRITEMASK_Z) != 0)
483b8e80941Smrg               imm[2] = vf;
484b8e80941Smrg            if ((inst->dst.writemask & WRITEMASK_W) != 0)
485b8e80941Smrg               imm[3] = vf;
486b8e80941Smrg
487b8e80941Smrg            writemask |= inst->dst.writemask;
488b8e80941Smrg            imm_inst[inst_count++] = inst;
489b8e80941Smrg
490b8e80941Smrg            last_reg = inst->dst.nr;
491b8e80941Smrg            last_offset = inst->dst.offset;
492b8e80941Smrg            last_reg_file = inst->dst.file;
493b8e80941Smrg            if (vf > 0)
494b8e80941Smrg               dest_type = need_type;
495b8e80941Smrg         }
496b8e80941Smrg      }
497b8e80941Smrg   }
498b8e80941Smrg
499b8e80941Smrg   if (progress)
500b8e80941Smrg      invalidate_live_intervals();
501b8e80941Smrg
502b8e80941Smrg   return progress;
503b8e80941Smrg}
504b8e80941Smrg
505b8e80941Smrg/* Replaces unused channels of a swizzle with channels that are used.
506b8e80941Smrg *
507b8e80941Smrg * For instance, this pass transforms
508b8e80941Smrg *
509b8e80941Smrg *    mov vgrf4.yz, vgrf5.wxzy
510b8e80941Smrg *
511b8e80941Smrg * into
512b8e80941Smrg *
513b8e80941Smrg *    mov vgrf4.yz, vgrf5.xxzx
514b8e80941Smrg *
515b8e80941Smrg * This eliminates false uses of some channels, letting dead code elimination
516b8e80941Smrg * remove the instructions that wrote them.
517b8e80941Smrg */
518b8e80941Smrgbool
519b8e80941Smrgvec4_visitor::opt_reduce_swizzle()
520b8e80941Smrg{
521b8e80941Smrg   bool progress = false;
522b8e80941Smrg
523b8e80941Smrg   foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
524b8e80941Smrg      if (inst->dst.file == BAD_FILE ||
525b8e80941Smrg          inst->dst.file == ARF ||
526b8e80941Smrg          inst->dst.file == FIXED_GRF ||
527b8e80941Smrg          inst->is_send_from_grf())
528b8e80941Smrg         continue;
529b8e80941Smrg
530b8e80941Smrg      unsigned swizzle;
531b8e80941Smrg
532b8e80941Smrg      /* Determine which channels of the sources are read. */
533b8e80941Smrg      switch (inst->opcode) {
534b8e80941Smrg      case VEC4_OPCODE_PACK_BYTES:
535b8e80941Smrg      case BRW_OPCODE_DP4:
536b8e80941Smrg      case BRW_OPCODE_DPH: /* FINISHME: DPH reads only three channels of src0,
537b8e80941Smrg                            *           but all four of src1.
538b8e80941Smrg                            */
539b8e80941Smrg         swizzle = brw_swizzle_for_size(4);
540b8e80941Smrg         break;
541b8e80941Smrg      case BRW_OPCODE_DP3:
542b8e80941Smrg         swizzle = brw_swizzle_for_size(3);
543b8e80941Smrg         break;
544b8e80941Smrg      case BRW_OPCODE_DP2:
545b8e80941Smrg         swizzle = brw_swizzle_for_size(2);
546b8e80941Smrg         break;
547b8e80941Smrg
548b8e80941Smrg      case VEC4_OPCODE_TO_DOUBLE:
549b8e80941Smrg      case VEC4_OPCODE_DOUBLE_TO_F32:
550b8e80941Smrg      case VEC4_OPCODE_DOUBLE_TO_D32:
551b8e80941Smrg      case VEC4_OPCODE_DOUBLE_TO_U32:
552b8e80941Smrg      case VEC4_OPCODE_PICK_LOW_32BIT:
553b8e80941Smrg      case VEC4_OPCODE_PICK_HIGH_32BIT:
554b8e80941Smrg      case VEC4_OPCODE_SET_LOW_32BIT:
555b8e80941Smrg      case VEC4_OPCODE_SET_HIGH_32BIT:
556b8e80941Smrg         swizzle = brw_swizzle_for_size(4);
557b8e80941Smrg         break;
558b8e80941Smrg
559b8e80941Smrg      default:
560b8e80941Smrg         swizzle = brw_swizzle_for_mask(inst->dst.writemask);
561b8e80941Smrg         break;
562b8e80941Smrg      }
563b8e80941Smrg
564b8e80941Smrg      /* Update sources' swizzles. */
565b8e80941Smrg      for (int i = 0; i < 3; i++) {
566b8e80941Smrg         if (inst->src[i].file != VGRF &&
567b8e80941Smrg             inst->src[i].file != ATTR &&
568b8e80941Smrg             inst->src[i].file != UNIFORM)
569b8e80941Smrg            continue;
570b8e80941Smrg
571b8e80941Smrg         const unsigned new_swizzle =
572b8e80941Smrg            brw_compose_swizzle(swizzle, inst->src[i].swizzle);
573b8e80941Smrg         if (inst->src[i].swizzle != new_swizzle) {
574b8e80941Smrg            inst->src[i].swizzle = new_swizzle;
575b8e80941Smrg            progress = true;
576b8e80941Smrg         }
577b8e80941Smrg      }
578b8e80941Smrg   }
579b8e80941Smrg
580b8e80941Smrg   if (progress)
581b8e80941Smrg      invalidate_live_intervals();
582b8e80941Smrg
583b8e80941Smrg   return progress;
584b8e80941Smrg}
585b8e80941Smrg
586b8e80941Smrgvoid
587b8e80941Smrgvec4_visitor::split_uniform_registers()
588b8e80941Smrg{
589b8e80941Smrg   /* Prior to this, uniforms have been in an array sized according to
590b8e80941Smrg    * the number of vector uniforms present, sparsely filled (so an
591b8e80941Smrg    * aggregate results in reg indices being skipped over).  Now we're
592b8e80941Smrg    * going to cut those aggregates up so each .nr index is one
593b8e80941Smrg    * vector.  The goal is to make elimination of unused uniform
594b8e80941Smrg    * components easier later.
595b8e80941Smrg    */
596b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
597b8e80941Smrg      for (int i = 0 ; i < 3; i++) {
598b8e80941Smrg	 if (inst->src[i].file != UNIFORM)
599b8e80941Smrg	    continue;
600b8e80941Smrg
601b8e80941Smrg	 assert(!inst->src[i].reladdr);
602b8e80941Smrg
603b8e80941Smrg         inst->src[i].nr += inst->src[i].offset / 16;
604b8e80941Smrg	 inst->src[i].offset %= 16;
605b8e80941Smrg      }
606b8e80941Smrg   }
607b8e80941Smrg}
608b8e80941Smrg
609b8e80941Smrg/* This function returns the register number where we placed the uniform */
610b8e80941Smrgstatic int
611b8e80941Smrgset_push_constant_loc(const int nr_uniforms, int *new_uniform_count,
612b8e80941Smrg                      const int src, const int size, const int channel_size,
613b8e80941Smrg                      int *new_loc, int *new_chan,
614b8e80941Smrg                      int *new_chans_used)
615b8e80941Smrg{
616b8e80941Smrg   int dst;
617b8e80941Smrg   /* Find the lowest place we can slot this uniform in. */
618b8e80941Smrg   for (dst = 0; dst < nr_uniforms; dst++) {
619b8e80941Smrg      if (ALIGN(new_chans_used[dst], channel_size) + size <= 4)
620b8e80941Smrg         break;
621b8e80941Smrg   }
622b8e80941Smrg
623b8e80941Smrg   assert(dst < nr_uniforms);
624b8e80941Smrg
625b8e80941Smrg   new_loc[src] = dst;
626b8e80941Smrg   new_chan[src] = ALIGN(new_chans_used[dst], channel_size);
627b8e80941Smrg   new_chans_used[dst] = ALIGN(new_chans_used[dst], channel_size) + size;
628b8e80941Smrg
629b8e80941Smrg   *new_uniform_count = MAX2(*new_uniform_count, dst + 1);
630b8e80941Smrg   return dst;
631b8e80941Smrg}
632b8e80941Smrg
633b8e80941Smrgvoid
634b8e80941Smrgvec4_visitor::pack_uniform_registers()
635b8e80941Smrg{
636b8e80941Smrg   uint8_t chans_used[this->uniforms];
637b8e80941Smrg   int new_loc[this->uniforms];
638b8e80941Smrg   int new_chan[this->uniforms];
639b8e80941Smrg   bool is_aligned_to_dvec4[this->uniforms];
640b8e80941Smrg   int new_chans_used[this->uniforms];
641b8e80941Smrg   int channel_sizes[this->uniforms];
642b8e80941Smrg
643b8e80941Smrg   memset(chans_used, 0, sizeof(chans_used));
644b8e80941Smrg   memset(new_loc, 0, sizeof(new_loc));
645b8e80941Smrg   memset(new_chan, 0, sizeof(new_chan));
646b8e80941Smrg   memset(new_chans_used, 0, sizeof(new_chans_used));
647b8e80941Smrg   memset(is_aligned_to_dvec4, 0, sizeof(is_aligned_to_dvec4));
648b8e80941Smrg   memset(channel_sizes, 0, sizeof(channel_sizes));
649b8e80941Smrg
650b8e80941Smrg   /* Find which uniform vectors are actually used by the program.  We
651b8e80941Smrg    * expect unused vector elements when we've moved array access out
652b8e80941Smrg    * to pull constants, and from some GLSL code generators like wine.
653b8e80941Smrg    */
654b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
655b8e80941Smrg      unsigned readmask;
656b8e80941Smrg      switch (inst->opcode) {
657b8e80941Smrg      case VEC4_OPCODE_PACK_BYTES:
658b8e80941Smrg      case BRW_OPCODE_DP4:
659b8e80941Smrg      case BRW_OPCODE_DPH:
660b8e80941Smrg         readmask = 0xf;
661b8e80941Smrg         break;
662b8e80941Smrg      case BRW_OPCODE_DP3:
663b8e80941Smrg         readmask = 0x7;
664b8e80941Smrg         break;
665b8e80941Smrg      case BRW_OPCODE_DP2:
666b8e80941Smrg         readmask = 0x3;
667b8e80941Smrg         break;
668b8e80941Smrg      default:
669b8e80941Smrg         readmask = inst->dst.writemask;
670b8e80941Smrg         break;
671b8e80941Smrg      }
672b8e80941Smrg
673b8e80941Smrg      for (int i = 0 ; i < 3; i++) {
674b8e80941Smrg         if (inst->src[i].file != UNIFORM)
675b8e80941Smrg            continue;
676b8e80941Smrg
677b8e80941Smrg         assert(type_sz(inst->src[i].type) % 4 == 0);
678b8e80941Smrg         int channel_size = type_sz(inst->src[i].type) / 4;
679b8e80941Smrg
680b8e80941Smrg         int reg = inst->src[i].nr;
681b8e80941Smrg         for (int c = 0; c < 4; c++) {
682b8e80941Smrg            if (!(readmask & (1 << c)))
683b8e80941Smrg               continue;
684b8e80941Smrg
685b8e80941Smrg            unsigned channel = BRW_GET_SWZ(inst->src[i].swizzle, c) + 1;
686b8e80941Smrg            unsigned used = MAX2(chans_used[reg], channel * channel_size);
687b8e80941Smrg            if (used <= 4) {
688b8e80941Smrg               chans_used[reg] = used;
689b8e80941Smrg               channel_sizes[reg] = MAX2(channel_sizes[reg], channel_size);
690b8e80941Smrg            } else {
691b8e80941Smrg               is_aligned_to_dvec4[reg] = true;
692b8e80941Smrg               is_aligned_to_dvec4[reg + 1] = true;
693b8e80941Smrg               chans_used[reg + 1] = used - 4;
694b8e80941Smrg               channel_sizes[reg + 1] = MAX2(channel_sizes[reg + 1], channel_size);
695b8e80941Smrg            }
696b8e80941Smrg         }
697b8e80941Smrg      }
698b8e80941Smrg
699b8e80941Smrg      if (inst->opcode == SHADER_OPCODE_MOV_INDIRECT &&
700b8e80941Smrg          inst->src[0].file == UNIFORM) {
701b8e80941Smrg         assert(inst->src[2].file == BRW_IMMEDIATE_VALUE);
702b8e80941Smrg         assert(inst->src[0].subnr == 0);
703b8e80941Smrg
704b8e80941Smrg         unsigned bytes_read = inst->src[2].ud;
705b8e80941Smrg         assert(bytes_read % 4 == 0);
706b8e80941Smrg         unsigned vec4s_read = DIV_ROUND_UP(bytes_read, 16);
707b8e80941Smrg
708b8e80941Smrg         /* We just mark every register touched by a MOV_INDIRECT as being
709b8e80941Smrg          * fully used.  This ensures that it doesn't broken up piecewise by
710b8e80941Smrg          * the next part of our packing algorithm.
711b8e80941Smrg          */
712b8e80941Smrg         int reg = inst->src[0].nr;
713b8e80941Smrg         int channel_size = type_sz(inst->src[0].type) / 4;
714b8e80941Smrg         for (unsigned i = 0; i < vec4s_read; i++) {
715b8e80941Smrg            chans_used[reg + i] = 4;
716b8e80941Smrg            channel_sizes[reg + i] = MAX2(channel_sizes[reg + i], channel_size);
717b8e80941Smrg         }
718b8e80941Smrg      }
719b8e80941Smrg   }
720b8e80941Smrg
721b8e80941Smrg   int new_uniform_count = 0;
722b8e80941Smrg
723b8e80941Smrg   /* As the uniforms are going to be reordered, take the data from a temporary
724b8e80941Smrg    * copy of the original param[].
725b8e80941Smrg    */
726b8e80941Smrg   uint32_t *param = ralloc_array(NULL, uint32_t, stage_prog_data->nr_params);
727b8e80941Smrg   memcpy(param, stage_prog_data->param,
728b8e80941Smrg          sizeof(uint32_t) * stage_prog_data->nr_params);
729b8e80941Smrg
730b8e80941Smrg   /* Now, figure out a packing of the live uniform vectors into our
731b8e80941Smrg    * push constants. Start with dvec{3,4} because they are aligned to
732b8e80941Smrg    * dvec4 size (2 vec4).
733b8e80941Smrg    */
734b8e80941Smrg   for (int src = 0; src < uniforms; src++) {
735b8e80941Smrg      int size = chans_used[src];
736b8e80941Smrg
737b8e80941Smrg      if (size == 0 || !is_aligned_to_dvec4[src])
738b8e80941Smrg         continue;
739b8e80941Smrg
740b8e80941Smrg      /* dvec3 are aligned to dvec4 size, apply the alignment of the size
741b8e80941Smrg       * to 4 to avoid moving last component of a dvec3 to the available
742b8e80941Smrg       * location at the end of a previous dvec3. These available locations
743b8e80941Smrg       * could be filled by smaller variables in next loop.
744b8e80941Smrg       */
745b8e80941Smrg      size = ALIGN(size, 4);
746b8e80941Smrg      int dst = set_push_constant_loc(uniforms, &new_uniform_count,
747b8e80941Smrg                                      src, size, channel_sizes[src],
748b8e80941Smrg                                      new_loc, new_chan,
749b8e80941Smrg                                      new_chans_used);
750b8e80941Smrg      /* Move the references to the data */
751b8e80941Smrg      for (int j = 0; j < size; j++) {
752b8e80941Smrg         stage_prog_data->param[dst * 4 + new_chan[src] + j] =
753b8e80941Smrg            param[src * 4 + j];
754b8e80941Smrg      }
755b8e80941Smrg   }
756b8e80941Smrg
757b8e80941Smrg   /* Continue with the rest of data, which is aligned to vec4. */
758b8e80941Smrg   for (int src = 0; src < uniforms; src++) {
759b8e80941Smrg      int size = chans_used[src];
760b8e80941Smrg
761b8e80941Smrg      if (size == 0 || is_aligned_to_dvec4[src])
762b8e80941Smrg         continue;
763b8e80941Smrg
764b8e80941Smrg      int dst = set_push_constant_loc(uniforms, &new_uniform_count,
765b8e80941Smrg                                      src, size, channel_sizes[src],
766b8e80941Smrg                                      new_loc, new_chan,
767b8e80941Smrg                                      new_chans_used);
768b8e80941Smrg      /* Move the references to the data */
769b8e80941Smrg      for (int j = 0; j < size; j++) {
770b8e80941Smrg         stage_prog_data->param[dst * 4 + new_chan[src] + j] =
771b8e80941Smrg            param[src * 4 + j];
772b8e80941Smrg      }
773b8e80941Smrg   }
774b8e80941Smrg
775b8e80941Smrg   ralloc_free(param);
776b8e80941Smrg   this->uniforms = new_uniform_count;
777b8e80941Smrg
778b8e80941Smrg   /* Now, update the instructions for our repacked uniforms. */
779b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
780b8e80941Smrg      for (int i = 0 ; i < 3; i++) {
781b8e80941Smrg         int src = inst->src[i].nr;
782b8e80941Smrg
783b8e80941Smrg         if (inst->src[i].file != UNIFORM)
784b8e80941Smrg            continue;
785b8e80941Smrg
786b8e80941Smrg         int chan = new_chan[src] / channel_sizes[src];
787b8e80941Smrg         inst->src[i].nr = new_loc[src];
788b8e80941Smrg         inst->src[i].swizzle += BRW_SWIZZLE4(chan, chan, chan, chan);
789b8e80941Smrg      }
790b8e80941Smrg   }
791b8e80941Smrg}
792b8e80941Smrg
793b8e80941Smrg/**
794b8e80941Smrg * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
795b8e80941Smrg *
796b8e80941Smrg * While GLSL IR also performs this optimization, we end up with it in
797b8e80941Smrg * our instruction stream for a couple of reasons.  One is that we
798b8e80941Smrg * sometimes generate silly instructions, for example in array access
799b8e80941Smrg * where we'll generate "ADD offset, index, base" even if base is 0.
800b8e80941Smrg * The other is that GLSL IR's constant propagation doesn't track the
801b8e80941Smrg * components of aggregates, so some VS patterns (initialize matrix to
802b8e80941Smrg * 0, accumulate in vertex blending factors) end up breaking down to
803b8e80941Smrg * instructions involving 0.
804b8e80941Smrg */
805b8e80941Smrgbool
806b8e80941Smrgvec4_visitor::opt_algebraic()
807b8e80941Smrg{
808b8e80941Smrg   bool progress = false;
809b8e80941Smrg
810b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
811b8e80941Smrg      switch (inst->opcode) {
812b8e80941Smrg      case BRW_OPCODE_MOV:
813b8e80941Smrg         if (inst->src[0].file != IMM)
814b8e80941Smrg            break;
815b8e80941Smrg
816b8e80941Smrg         if (inst->saturate) {
817b8e80941Smrg            /* Full mixed-type saturates don't happen.  However, we can end up
818b8e80941Smrg             * with things like:
819b8e80941Smrg             *
820b8e80941Smrg             *    mov.sat(8) g21<1>DF       -1F
821b8e80941Smrg             *
822b8e80941Smrg             * Other mixed-size-but-same-base-type cases may also be possible.
823b8e80941Smrg             */
824b8e80941Smrg            if (inst->dst.type != inst->src[0].type &&
825b8e80941Smrg                inst->dst.type != BRW_REGISTER_TYPE_DF &&
826b8e80941Smrg                inst->src[0].type != BRW_REGISTER_TYPE_F)
827b8e80941Smrg               assert(!"unimplemented: saturate mixed types");
828b8e80941Smrg
829b8e80941Smrg            if (brw_saturate_immediate(inst->src[0].type,
830b8e80941Smrg                                       &inst->src[0].as_brw_reg())) {
831b8e80941Smrg               inst->saturate = false;
832b8e80941Smrg               progress = true;
833b8e80941Smrg            }
834b8e80941Smrg         }
835b8e80941Smrg         break;
836b8e80941Smrg
837b8e80941Smrg      case BRW_OPCODE_OR:
838b8e80941Smrg         if (inst->src[1].is_zero()) {
839b8e80941Smrg            inst->opcode = BRW_OPCODE_MOV;
840b8e80941Smrg            inst->src[1] = src_reg();
841b8e80941Smrg            progress = true;
842b8e80941Smrg         }
843b8e80941Smrg         break;
844b8e80941Smrg
845b8e80941Smrg      case VEC4_OPCODE_UNPACK_UNIFORM:
846b8e80941Smrg         if (inst->src[0].file != UNIFORM) {
847b8e80941Smrg            inst->opcode = BRW_OPCODE_MOV;
848b8e80941Smrg            progress = true;
849b8e80941Smrg         }
850b8e80941Smrg         break;
851b8e80941Smrg
852b8e80941Smrg      case BRW_OPCODE_ADD:
853b8e80941Smrg	 if (inst->src[1].is_zero()) {
854b8e80941Smrg	    inst->opcode = BRW_OPCODE_MOV;
855b8e80941Smrg	    inst->src[1] = src_reg();
856b8e80941Smrg	    progress = true;
857b8e80941Smrg	 }
858b8e80941Smrg	 break;
859b8e80941Smrg
860b8e80941Smrg      case BRW_OPCODE_MUL:
861b8e80941Smrg	 if (inst->src[1].is_zero()) {
862b8e80941Smrg	    inst->opcode = BRW_OPCODE_MOV;
863b8e80941Smrg	    switch (inst->src[0].type) {
864b8e80941Smrg	    case BRW_REGISTER_TYPE_F:
865b8e80941Smrg	       inst->src[0] = brw_imm_f(0.0f);
866b8e80941Smrg	       break;
867b8e80941Smrg	    case BRW_REGISTER_TYPE_D:
868b8e80941Smrg	       inst->src[0] = brw_imm_d(0);
869b8e80941Smrg	       break;
870b8e80941Smrg	    case BRW_REGISTER_TYPE_UD:
871b8e80941Smrg	       inst->src[0] = brw_imm_ud(0u);
872b8e80941Smrg	       break;
873b8e80941Smrg	    default:
874b8e80941Smrg	       unreachable("not reached");
875b8e80941Smrg	    }
876b8e80941Smrg	    inst->src[1] = src_reg();
877b8e80941Smrg	    progress = true;
878b8e80941Smrg	 } else if (inst->src[1].is_one()) {
879b8e80941Smrg	    inst->opcode = BRW_OPCODE_MOV;
880b8e80941Smrg	    inst->src[1] = src_reg();
881b8e80941Smrg	    progress = true;
882b8e80941Smrg         } else if (inst->src[1].is_negative_one()) {
883b8e80941Smrg            inst->opcode = BRW_OPCODE_MOV;
884b8e80941Smrg            inst->src[0].negate = !inst->src[0].negate;
885b8e80941Smrg            inst->src[1] = src_reg();
886b8e80941Smrg            progress = true;
887b8e80941Smrg	 }
888b8e80941Smrg	 break;
889b8e80941Smrg      case SHADER_OPCODE_BROADCAST:
890b8e80941Smrg         if (is_uniform(inst->src[0]) ||
891b8e80941Smrg             inst->src[1].is_zero()) {
892b8e80941Smrg            inst->opcode = BRW_OPCODE_MOV;
893b8e80941Smrg            inst->src[1] = src_reg();
894b8e80941Smrg            inst->force_writemask_all = true;
895b8e80941Smrg            progress = true;
896b8e80941Smrg         }
897b8e80941Smrg         break;
898b8e80941Smrg
899b8e80941Smrg      default:
900b8e80941Smrg	 break;
901b8e80941Smrg      }
902b8e80941Smrg   }
903b8e80941Smrg
904b8e80941Smrg   if (progress)
905b8e80941Smrg      invalidate_live_intervals();
906b8e80941Smrg
907b8e80941Smrg   return progress;
908b8e80941Smrg}
909b8e80941Smrg
910b8e80941Smrg/**
911b8e80941Smrg * Only a limited number of hardware registers may be used for push
912b8e80941Smrg * constants, so this turns access to the overflowed constants into
913b8e80941Smrg * pull constants.
914b8e80941Smrg */
915b8e80941Smrgvoid
916b8e80941Smrgvec4_visitor::move_push_constants_to_pull_constants()
917b8e80941Smrg{
918b8e80941Smrg   int pull_constant_loc[this->uniforms];
919b8e80941Smrg
920b8e80941Smrg   /* Only allow 32 registers (256 uniform components) as push constants,
921b8e80941Smrg    * which is the limit on gen6.
922b8e80941Smrg    *
923b8e80941Smrg    * If changing this value, note the limitation about total_regs in
924b8e80941Smrg    * brw_curbe.c.
925b8e80941Smrg    */
926b8e80941Smrg   int max_uniform_components = 32 * 8;
927b8e80941Smrg   if (this->uniforms * 4 <= max_uniform_components)
928b8e80941Smrg      return;
929b8e80941Smrg
930b8e80941Smrg   /* Make some sort of choice as to which uniforms get sent to pull
931b8e80941Smrg    * constants.  We could potentially do something clever here like
932b8e80941Smrg    * look for the most infrequently used uniform vec4s, but leave
933b8e80941Smrg    * that for later.
934b8e80941Smrg    */
935b8e80941Smrg   for (int i = 0; i < this->uniforms * 4; i += 4) {
936b8e80941Smrg      pull_constant_loc[i / 4] = -1;
937b8e80941Smrg
938b8e80941Smrg      if (i >= max_uniform_components) {
939b8e80941Smrg         uint32_t *values = &stage_prog_data->param[i];
940b8e80941Smrg
941b8e80941Smrg         /* Try to find an existing copy of this uniform in the pull
942b8e80941Smrg          * constants if it was part of an array access already.
943b8e80941Smrg          */
944b8e80941Smrg         for (unsigned int j = 0; j < stage_prog_data->nr_pull_params; j += 4) {
945b8e80941Smrg            int matches;
946b8e80941Smrg
947b8e80941Smrg            for (matches = 0; matches < 4; matches++) {
948b8e80941Smrg               if (stage_prog_data->pull_param[j + matches] != values[matches])
949b8e80941Smrg                  break;
950b8e80941Smrg            }
951b8e80941Smrg
952b8e80941Smrg            if (matches == 4) {
953b8e80941Smrg               pull_constant_loc[i / 4] = j / 4;
954b8e80941Smrg               break;
955b8e80941Smrg            }
956b8e80941Smrg         }
957b8e80941Smrg
958b8e80941Smrg         if (pull_constant_loc[i / 4] == -1) {
959b8e80941Smrg            assert(stage_prog_data->nr_pull_params % 4 == 0);
960b8e80941Smrg            pull_constant_loc[i / 4] = stage_prog_data->nr_pull_params / 4;
961b8e80941Smrg
962b8e80941Smrg            for (int j = 0; j < 4; j++) {
963b8e80941Smrg               stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
964b8e80941Smrg                  values[j];
965b8e80941Smrg            }
966b8e80941Smrg         }
967b8e80941Smrg      }
968b8e80941Smrg   }
969b8e80941Smrg
970b8e80941Smrg   /* Now actually rewrite usage of the things we've moved to pull
971b8e80941Smrg    * constants.
972b8e80941Smrg    */
973b8e80941Smrg   foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
974b8e80941Smrg      for (int i = 0 ; i < 3; i++) {
975b8e80941Smrg         if (inst->src[i].file != UNIFORM ||
976b8e80941Smrg             pull_constant_loc[inst->src[i].nr] == -1)
977b8e80941Smrg            continue;
978b8e80941Smrg
979b8e80941Smrg         int uniform = inst->src[i].nr;
980b8e80941Smrg
981b8e80941Smrg         const glsl_type *temp_type = type_sz(inst->src[i].type) == 8 ?
982b8e80941Smrg            glsl_type::dvec4_type : glsl_type::vec4_type;
983b8e80941Smrg         dst_reg temp = dst_reg(this, temp_type);
984b8e80941Smrg
985b8e80941Smrg         emit_pull_constant_load(block, inst, temp, inst->src[i],
986b8e80941Smrg                                 pull_constant_loc[uniform], src_reg());
987b8e80941Smrg
988b8e80941Smrg         inst->src[i].file = temp.file;
989b8e80941Smrg         inst->src[i].nr = temp.nr;
990b8e80941Smrg         inst->src[i].offset %= 16;
991b8e80941Smrg         inst->src[i].reladdr = NULL;
992b8e80941Smrg      }
993b8e80941Smrg   }
994b8e80941Smrg
995b8e80941Smrg   /* Repack push constants to remove the now-unused ones. */
996b8e80941Smrg   pack_uniform_registers();
997b8e80941Smrg}
998b8e80941Smrg
999b8e80941Smrg/* Conditions for which we want to avoid setting the dependency control bits */
1000b8e80941Smrgbool
1001b8e80941Smrgvec4_visitor::is_dep_ctrl_unsafe(const vec4_instruction *inst)
1002b8e80941Smrg{
1003b8e80941Smrg#define IS_DWORD(reg) \
1004b8e80941Smrg   (reg.type == BRW_REGISTER_TYPE_UD || \
1005b8e80941Smrg    reg.type == BRW_REGISTER_TYPE_D)
1006b8e80941Smrg
1007b8e80941Smrg#define IS_64BIT(reg) (reg.file != BAD_FILE && type_sz(reg.type) == 8)
1008b8e80941Smrg
1009b8e80941Smrg   /* From the Cherryview and Broadwell PRMs:
1010b8e80941Smrg    *
1011b8e80941Smrg    * "When source or destination datatype is 64b or operation is integer DWord
1012b8e80941Smrg    * multiply, DepCtrl must not be used."
1013b8e80941Smrg    *
1014b8e80941Smrg    * SKL PRMs don't include this restriction, however, gen7 seems to be
1015b8e80941Smrg    * affected, at least by the 64b restriction, since DepCtrl with double
1016b8e80941Smrg    * precision instructions seems to produce GPU hangs in some cases.
1017b8e80941Smrg    */
1018b8e80941Smrg   if (devinfo->gen == 8 || gen_device_info_is_9lp(devinfo)) {
1019b8e80941Smrg      if (inst->opcode == BRW_OPCODE_MUL &&
1020b8e80941Smrg         IS_DWORD(inst->src[0]) &&
1021b8e80941Smrg         IS_DWORD(inst->src[1]))
1022b8e80941Smrg         return true;
1023b8e80941Smrg   }
1024b8e80941Smrg
1025b8e80941Smrg   if (devinfo->gen >= 7 && devinfo->gen <= 8) {
1026b8e80941Smrg      if (IS_64BIT(inst->dst) || IS_64BIT(inst->src[0]) ||
1027b8e80941Smrg          IS_64BIT(inst->src[1]) || IS_64BIT(inst->src[2]))
1028b8e80941Smrg      return true;
1029b8e80941Smrg   }
1030b8e80941Smrg
1031b8e80941Smrg#undef IS_64BIT
1032b8e80941Smrg#undef IS_DWORD
1033b8e80941Smrg
1034b8e80941Smrg   if (devinfo->gen >= 8) {
1035b8e80941Smrg      if (inst->opcode == BRW_OPCODE_F32TO16)
1036b8e80941Smrg         return true;
1037b8e80941Smrg   }
1038b8e80941Smrg
1039b8e80941Smrg   /*
1040b8e80941Smrg    * mlen:
1041b8e80941Smrg    * In the presence of send messages, totally interrupt dependency
1042b8e80941Smrg    * control. They're long enough that the chance of dependency
1043b8e80941Smrg    * control around them just doesn't matter.
1044b8e80941Smrg    *
1045b8e80941Smrg    * predicate:
1046b8e80941Smrg    * From the Ivy Bridge PRM, volume 4 part 3.7, page 80:
1047b8e80941Smrg    * When a sequence of NoDDChk and NoDDClr are used, the last instruction that
1048b8e80941Smrg    * completes the scoreboard clear must have a non-zero execution mask. This
1049b8e80941Smrg    * means, if any kind of predication can change the execution mask or channel
1050b8e80941Smrg    * enable of the last instruction, the optimization must be avoided. This is
1051b8e80941Smrg    * to avoid instructions being shot down the pipeline when no writes are
1052b8e80941Smrg    * required.
1053b8e80941Smrg    *
1054b8e80941Smrg    * math:
1055b8e80941Smrg    * Dependency control does not work well over math instructions.
1056b8e80941Smrg    * NB: Discovered empirically
1057b8e80941Smrg    */
1058b8e80941Smrg   return (inst->mlen || inst->predicate || inst->is_math());
1059b8e80941Smrg}
1060b8e80941Smrg
1061b8e80941Smrg/**
1062b8e80941Smrg * Sets the dependency control fields on instructions after register
1063b8e80941Smrg * allocation and before the generator is run.
1064b8e80941Smrg *
1065b8e80941Smrg * When you have a sequence of instructions like:
1066b8e80941Smrg *
1067b8e80941Smrg * DP4 temp.x vertex uniform[0]
1068b8e80941Smrg * DP4 temp.y vertex uniform[0]
1069b8e80941Smrg * DP4 temp.z vertex uniform[0]
1070b8e80941Smrg * DP4 temp.w vertex uniform[0]
1071b8e80941Smrg *
1072b8e80941Smrg * The hardware doesn't know that it can actually run the later instructions
1073b8e80941Smrg * while the previous ones are in flight, producing stalls.  However, we have
1074b8e80941Smrg * manual fields we can set in the instructions that let it do so.
1075b8e80941Smrg */
1076b8e80941Smrgvoid
1077b8e80941Smrgvec4_visitor::opt_set_dependency_control()
1078b8e80941Smrg{
1079b8e80941Smrg   vec4_instruction *last_grf_write[BRW_MAX_GRF];
1080b8e80941Smrg   uint8_t grf_channels_written[BRW_MAX_GRF];
1081b8e80941Smrg   vec4_instruction *last_mrf_write[BRW_MAX_GRF];
1082b8e80941Smrg   uint8_t mrf_channels_written[BRW_MAX_GRF];
1083b8e80941Smrg
1084b8e80941Smrg   assert(prog_data->total_grf ||
1085b8e80941Smrg          !"Must be called after register allocation");
1086b8e80941Smrg
1087b8e80941Smrg   foreach_block (block, cfg) {
1088b8e80941Smrg      memset(last_grf_write, 0, sizeof(last_grf_write));
1089b8e80941Smrg      memset(last_mrf_write, 0, sizeof(last_mrf_write));
1090b8e80941Smrg
1091b8e80941Smrg      foreach_inst_in_block (vec4_instruction, inst, block) {
1092b8e80941Smrg         /* If we read from a register that we were doing dependency control
1093b8e80941Smrg          * on, don't do dependency control across the read.
1094b8e80941Smrg          */
1095b8e80941Smrg         for (int i = 0; i < 3; i++) {
1096b8e80941Smrg            int reg = inst->src[i].nr + inst->src[i].offset / REG_SIZE;
1097b8e80941Smrg            if (inst->src[i].file == VGRF) {
1098b8e80941Smrg               last_grf_write[reg] = NULL;
1099b8e80941Smrg            } else if (inst->src[i].file == FIXED_GRF) {
1100b8e80941Smrg               memset(last_grf_write, 0, sizeof(last_grf_write));
1101b8e80941Smrg               break;
1102b8e80941Smrg            }
1103b8e80941Smrg            assert(inst->src[i].file != MRF);
1104b8e80941Smrg         }
1105b8e80941Smrg
1106b8e80941Smrg         if (is_dep_ctrl_unsafe(inst)) {
1107b8e80941Smrg            memset(last_grf_write, 0, sizeof(last_grf_write));
1108b8e80941Smrg            memset(last_mrf_write, 0, sizeof(last_mrf_write));
1109b8e80941Smrg            continue;
1110b8e80941Smrg         }
1111b8e80941Smrg
1112b8e80941Smrg         /* Now, see if we can do dependency control for this instruction
1113b8e80941Smrg          * against a previous one writing to its destination.
1114b8e80941Smrg          */
1115b8e80941Smrg         int reg = inst->dst.nr + inst->dst.offset / REG_SIZE;
1116b8e80941Smrg         if (inst->dst.file == VGRF || inst->dst.file == FIXED_GRF) {
1117b8e80941Smrg            if (last_grf_write[reg] &&
1118b8e80941Smrg                last_grf_write[reg]->dst.offset == inst->dst.offset &&
1119b8e80941Smrg                !(inst->dst.writemask & grf_channels_written[reg])) {
1120b8e80941Smrg               last_grf_write[reg]->no_dd_clear = true;
1121b8e80941Smrg               inst->no_dd_check = true;
1122b8e80941Smrg            } else {
1123b8e80941Smrg               grf_channels_written[reg] = 0;
1124b8e80941Smrg            }
1125b8e80941Smrg
1126b8e80941Smrg            last_grf_write[reg] = inst;
1127b8e80941Smrg            grf_channels_written[reg] |= inst->dst.writemask;
1128b8e80941Smrg         } else if (inst->dst.file == MRF) {
1129b8e80941Smrg            if (last_mrf_write[reg] &&
1130b8e80941Smrg                last_mrf_write[reg]->dst.offset == inst->dst.offset &&
1131b8e80941Smrg                !(inst->dst.writemask & mrf_channels_written[reg])) {
1132b8e80941Smrg               last_mrf_write[reg]->no_dd_clear = true;
1133b8e80941Smrg               inst->no_dd_check = true;
1134b8e80941Smrg            } else {
1135b8e80941Smrg               mrf_channels_written[reg] = 0;
1136b8e80941Smrg            }
1137b8e80941Smrg
1138b8e80941Smrg            last_mrf_write[reg] = inst;
1139b8e80941Smrg            mrf_channels_written[reg] |= inst->dst.writemask;
1140b8e80941Smrg         }
1141b8e80941Smrg      }
1142b8e80941Smrg   }
1143b8e80941Smrg}
1144b8e80941Smrg
1145b8e80941Smrgbool
1146b8e80941Smrgvec4_instruction::can_reswizzle(const struct gen_device_info *devinfo,
1147b8e80941Smrg                                int dst_writemask,
1148b8e80941Smrg                                int swizzle,
1149b8e80941Smrg                                int swizzle_mask)
1150b8e80941Smrg{
1151b8e80941Smrg   /* Gen6 MATH instructions can not execute in align16 mode, so swizzles
1152b8e80941Smrg    * are not allowed.
1153b8e80941Smrg    */
1154b8e80941Smrg   if (devinfo->gen == 6 && is_math() && swizzle != BRW_SWIZZLE_XYZW)
1155b8e80941Smrg      return false;
1156b8e80941Smrg
1157b8e80941Smrg   /* If we write to the flag register changing the swizzle would change
1158b8e80941Smrg    * what channels are written to the flag register.
1159b8e80941Smrg    */
1160b8e80941Smrg   if (writes_flag())
1161b8e80941Smrg      return false;
1162b8e80941Smrg
1163b8e80941Smrg   /* We can't swizzle implicit accumulator access.  We'd have to
1164b8e80941Smrg    * reswizzle the producer of the accumulator value in addition
1165b8e80941Smrg    * to the consumer (i.e. both MUL and MACH).  Just skip this.
1166b8e80941Smrg    */
1167b8e80941Smrg   if (reads_accumulator_implicitly())
1168b8e80941Smrg      return false;
1169b8e80941Smrg
1170b8e80941Smrg   if (!can_do_writemask(devinfo) && dst_writemask != WRITEMASK_XYZW)
1171b8e80941Smrg      return false;
1172b8e80941Smrg
1173b8e80941Smrg   /* If this instruction sets anything not referenced by swizzle, then we'd
1174b8e80941Smrg    * totally break it when we reswizzle.
1175b8e80941Smrg    */
1176b8e80941Smrg   if (dst.writemask & ~swizzle_mask)
1177b8e80941Smrg      return false;
1178b8e80941Smrg
1179b8e80941Smrg   if (mlen > 0)
1180b8e80941Smrg      return false;
1181b8e80941Smrg
1182b8e80941Smrg   for (int i = 0; i < 3; i++) {
1183b8e80941Smrg      if (src[i].is_accumulator())
1184b8e80941Smrg         return false;
1185b8e80941Smrg   }
1186b8e80941Smrg
1187b8e80941Smrg   return true;
1188b8e80941Smrg}
1189b8e80941Smrg
1190b8e80941Smrg/**
1191b8e80941Smrg * For any channels in the swizzle's source that were populated by this
1192b8e80941Smrg * instruction, rewrite the instruction to put the appropriate result directly
1193b8e80941Smrg * in those channels.
1194b8e80941Smrg *
1195b8e80941Smrg * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
1196b8e80941Smrg */
1197b8e80941Smrgvoid
1198b8e80941Smrgvec4_instruction::reswizzle(int dst_writemask, int swizzle)
1199b8e80941Smrg{
1200b8e80941Smrg   /* Destination write mask doesn't correspond to source swizzle for the dot
1201b8e80941Smrg    * product and pack_bytes instructions.
1202b8e80941Smrg    */
1203b8e80941Smrg   if (opcode != BRW_OPCODE_DP4 && opcode != BRW_OPCODE_DPH &&
1204b8e80941Smrg       opcode != BRW_OPCODE_DP3 && opcode != BRW_OPCODE_DP2 &&
1205b8e80941Smrg       opcode != VEC4_OPCODE_PACK_BYTES) {
1206b8e80941Smrg      for (int i = 0; i < 3; i++) {
1207b8e80941Smrg         if (src[i].file == BAD_FILE)
1208b8e80941Smrg            continue;
1209b8e80941Smrg
1210b8e80941Smrg         if (src[i].file == IMM) {
1211b8e80941Smrg            assert(src[i].type != BRW_REGISTER_TYPE_V &&
1212b8e80941Smrg                   src[i].type != BRW_REGISTER_TYPE_UV);
1213b8e80941Smrg
1214b8e80941Smrg            /* Vector immediate types need to be reswizzled. */
1215b8e80941Smrg            if (src[i].type == BRW_REGISTER_TYPE_VF) {
1216b8e80941Smrg               const unsigned imm[] = {
1217b8e80941Smrg                  (src[i].ud >>  0) & 0x0ff,
1218b8e80941Smrg                  (src[i].ud >>  8) & 0x0ff,
1219b8e80941Smrg                  (src[i].ud >> 16) & 0x0ff,
1220b8e80941Smrg                  (src[i].ud >> 24) & 0x0ff,
1221b8e80941Smrg               };
1222b8e80941Smrg
1223b8e80941Smrg               src[i] = brw_imm_vf4(imm[BRW_GET_SWZ(swizzle, 0)],
1224b8e80941Smrg                                    imm[BRW_GET_SWZ(swizzle, 1)],
1225b8e80941Smrg                                    imm[BRW_GET_SWZ(swizzle, 2)],
1226b8e80941Smrg                                    imm[BRW_GET_SWZ(swizzle, 3)]);
1227b8e80941Smrg            }
1228b8e80941Smrg
1229b8e80941Smrg            continue;
1230b8e80941Smrg         }
1231b8e80941Smrg
1232b8e80941Smrg         src[i].swizzle = brw_compose_swizzle(swizzle, src[i].swizzle);
1233b8e80941Smrg      }
1234b8e80941Smrg   }
1235b8e80941Smrg
1236b8e80941Smrg   /* Apply the specified swizzle and writemask to the original mask of
1237b8e80941Smrg    * written components.
1238b8e80941Smrg    */
1239b8e80941Smrg   dst.writemask = dst_writemask &
1240b8e80941Smrg                   brw_apply_swizzle_to_mask(swizzle, dst.writemask);
1241b8e80941Smrg}
1242b8e80941Smrg
1243b8e80941Smrg/*
1244b8e80941Smrg * Tries to reduce extra MOV instructions by taking temporary GRFs that get
1245b8e80941Smrg * just written and then MOVed into another reg and making the original write
1246b8e80941Smrg * of the GRF write directly to the final destination instead.
1247b8e80941Smrg */
1248b8e80941Smrgbool
1249b8e80941Smrgvec4_visitor::opt_register_coalesce()
1250b8e80941Smrg{
1251b8e80941Smrg   bool progress = false;
1252b8e80941Smrg   int next_ip = 0;
1253b8e80941Smrg
1254b8e80941Smrg   calculate_live_intervals();
1255b8e80941Smrg
1256b8e80941Smrg   foreach_block_and_inst_safe (block, vec4_instruction, inst, cfg) {
1257b8e80941Smrg      int ip = next_ip;
1258b8e80941Smrg      next_ip++;
1259b8e80941Smrg
1260b8e80941Smrg      if (inst->opcode != BRW_OPCODE_MOV ||
1261b8e80941Smrg          (inst->dst.file != VGRF && inst->dst.file != MRF) ||
1262b8e80941Smrg	  inst->predicate ||
1263b8e80941Smrg	  inst->src[0].file != VGRF ||
1264b8e80941Smrg	  inst->dst.type != inst->src[0].type ||
1265b8e80941Smrg	  inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
1266b8e80941Smrg	 continue;
1267b8e80941Smrg
1268b8e80941Smrg      /* Remove no-op MOVs */
1269b8e80941Smrg      if (inst->dst.file == inst->src[0].file &&
1270b8e80941Smrg          inst->dst.nr == inst->src[0].nr &&
1271b8e80941Smrg          inst->dst.offset == inst->src[0].offset) {
1272b8e80941Smrg         bool is_nop_mov = true;
1273b8e80941Smrg
1274b8e80941Smrg         for (unsigned c = 0; c < 4; c++) {
1275b8e80941Smrg            if ((inst->dst.writemask & (1 << c)) == 0)
1276b8e80941Smrg               continue;
1277b8e80941Smrg
1278b8e80941Smrg            if (BRW_GET_SWZ(inst->src[0].swizzle, c) != c) {
1279b8e80941Smrg               is_nop_mov = false;
1280b8e80941Smrg               break;
1281b8e80941Smrg            }
1282b8e80941Smrg         }
1283b8e80941Smrg
1284b8e80941Smrg         if (is_nop_mov) {
1285b8e80941Smrg            inst->remove(block);
1286b8e80941Smrg            progress = true;
1287b8e80941Smrg            continue;
1288b8e80941Smrg         }
1289b8e80941Smrg      }
1290b8e80941Smrg
1291b8e80941Smrg      bool to_mrf = (inst->dst.file == MRF);
1292b8e80941Smrg
1293b8e80941Smrg      /* Can't coalesce this GRF if someone else was going to
1294b8e80941Smrg       * read it later.
1295b8e80941Smrg       */
1296b8e80941Smrg      if (var_range_end(var_from_reg(alloc, dst_reg(inst->src[0])), 8) > ip)
1297b8e80941Smrg	 continue;
1298b8e80941Smrg
1299b8e80941Smrg      /* We need to check interference with the final destination between this
1300b8e80941Smrg       * instruction and the earliest instruction involved in writing the GRF
1301b8e80941Smrg       * we're eliminating.  To do that, keep track of which of our source
1302b8e80941Smrg       * channels we've seen initialized.
1303b8e80941Smrg       */
1304b8e80941Smrg      const unsigned chans_needed =
1305b8e80941Smrg         brw_apply_inv_swizzle_to_mask(inst->src[0].swizzle,
1306b8e80941Smrg                                       inst->dst.writemask);
1307b8e80941Smrg      unsigned chans_remaining = chans_needed;
1308b8e80941Smrg
1309b8e80941Smrg      /* Now walk up the instruction stream trying to see if we can rewrite
1310b8e80941Smrg       * everything writing to the temporary to write into the destination
1311b8e80941Smrg       * instead.
1312b8e80941Smrg       */
1313b8e80941Smrg      vec4_instruction *_scan_inst = (vec4_instruction *)inst->prev;
1314b8e80941Smrg      foreach_inst_in_block_reverse_starting_from(vec4_instruction, scan_inst,
1315b8e80941Smrg                                                  inst) {
1316b8e80941Smrg         _scan_inst = scan_inst;
1317b8e80941Smrg
1318b8e80941Smrg         if (regions_overlap(inst->src[0], inst->size_read(0),
1319b8e80941Smrg                             scan_inst->dst, scan_inst->size_written)) {
1320b8e80941Smrg            /* Found something writing to the reg we want to coalesce away. */
1321b8e80941Smrg            if (to_mrf) {
1322b8e80941Smrg               /* SEND instructions can't have MRF as a destination. */
1323b8e80941Smrg               if (scan_inst->mlen)
1324b8e80941Smrg                  break;
1325b8e80941Smrg
1326b8e80941Smrg               if (devinfo->gen == 6) {
1327b8e80941Smrg                  /* gen6 math instructions must have the destination be
1328b8e80941Smrg                   * VGRF, so no compute-to-MRF for them.
1329b8e80941Smrg                   */
1330b8e80941Smrg                  if (scan_inst->is_math()) {
1331b8e80941Smrg                     break;
1332b8e80941Smrg                  }
1333b8e80941Smrg               }
1334b8e80941Smrg            }
1335b8e80941Smrg
1336b8e80941Smrg            /* VS_OPCODE_UNPACK_FLAGS_SIMD4X2 generates a bunch of mov(1)
1337b8e80941Smrg             * instructions, and this optimization pass is not capable of
1338b8e80941Smrg             * handling that.  Bail on these instructions and hope that some
1339b8e80941Smrg             * later optimization pass can do the right thing after they are
1340b8e80941Smrg             * expanded.
1341b8e80941Smrg             */
1342b8e80941Smrg            if (scan_inst->opcode == VS_OPCODE_UNPACK_FLAGS_SIMD4X2)
1343b8e80941Smrg               break;
1344b8e80941Smrg
1345b8e80941Smrg            /* This doesn't handle saturation on the instruction we
1346b8e80941Smrg             * want to coalesce away if the register types do not match.
1347b8e80941Smrg             * But if scan_inst is a non type-converting 'mov', we can fix
1348b8e80941Smrg             * the types later.
1349b8e80941Smrg             */
1350b8e80941Smrg            if (inst->saturate &&
1351b8e80941Smrg                inst->dst.type != scan_inst->dst.type &&
1352b8e80941Smrg                !(scan_inst->opcode == BRW_OPCODE_MOV &&
1353b8e80941Smrg                  scan_inst->dst.type == scan_inst->src[0].type))
1354b8e80941Smrg               break;
1355b8e80941Smrg
1356b8e80941Smrg            /* Only allow coalescing between registers of the same type size.
1357b8e80941Smrg             * Otherwise we would need to make the pass aware of the fact that
1358b8e80941Smrg             * channel sizes are different for single and double precision.
1359b8e80941Smrg             */
1360b8e80941Smrg            if (type_sz(inst->src[0].type) != type_sz(scan_inst->src[0].type))
1361b8e80941Smrg               break;
1362b8e80941Smrg
1363b8e80941Smrg            /* Check that scan_inst writes the same amount of data as the
1364b8e80941Smrg             * instruction, otherwise coalescing would lead to writing a
1365b8e80941Smrg             * different (larger or smaller) region of the destination
1366b8e80941Smrg             */
1367b8e80941Smrg            if (scan_inst->size_written != inst->size_written)
1368b8e80941Smrg               break;
1369b8e80941Smrg
1370b8e80941Smrg            /* If we can't handle the swizzle, bail. */
1371b8e80941Smrg            if (!scan_inst->can_reswizzle(devinfo, inst->dst.writemask,
1372b8e80941Smrg                                          inst->src[0].swizzle,
1373b8e80941Smrg                                          chans_needed)) {
1374b8e80941Smrg               break;
1375b8e80941Smrg            }
1376b8e80941Smrg
1377b8e80941Smrg            /* This only handles coalescing writes of 8 channels (1 register
1378b8e80941Smrg             * for single-precision and 2 registers for double-precision)
1379b8e80941Smrg             * starting at the source offset of the copy instruction.
1380b8e80941Smrg             */
1381b8e80941Smrg            if (DIV_ROUND_UP(scan_inst->size_written,
1382b8e80941Smrg                             type_sz(scan_inst->dst.type)) > 8 ||
1383b8e80941Smrg                scan_inst->dst.offset != inst->src[0].offset)
1384b8e80941Smrg               break;
1385b8e80941Smrg
1386b8e80941Smrg	    /* Mark which channels we found unconditional writes for. */
1387b8e80941Smrg	    if (!scan_inst->predicate)
1388b8e80941Smrg               chans_remaining &= ~scan_inst->dst.writemask;
1389b8e80941Smrg
1390b8e80941Smrg	    if (chans_remaining == 0)
1391b8e80941Smrg	       break;
1392b8e80941Smrg	 }
1393b8e80941Smrg
1394b8e80941Smrg         /* You can't read from an MRF, so if someone else reads our MRF's
1395b8e80941Smrg          * source GRF that we wanted to rewrite, that stops us.  If it's a
1396b8e80941Smrg          * GRF we're trying to coalesce to, we don't actually handle
1397b8e80941Smrg          * rewriting sources so bail in that case as well.
1398b8e80941Smrg          */
1399b8e80941Smrg	 bool interfered = false;
1400b8e80941Smrg	 for (int i = 0; i < 3; i++) {
1401b8e80941Smrg            if (regions_overlap(inst->src[0], inst->size_read(0),
1402b8e80941Smrg                                scan_inst->src[i], scan_inst->size_read(i)))
1403b8e80941Smrg	       interfered = true;
1404b8e80941Smrg	 }
1405b8e80941Smrg	 if (interfered)
1406b8e80941Smrg	    break;
1407b8e80941Smrg
1408b8e80941Smrg         /* If somebody else writes the same channels of our destination here,
1409b8e80941Smrg          * we can't coalesce before that.
1410b8e80941Smrg          */
1411b8e80941Smrg         if (regions_overlap(inst->dst, inst->size_written,
1412b8e80941Smrg                             scan_inst->dst, scan_inst->size_written) &&
1413b8e80941Smrg             (inst->dst.writemask & scan_inst->dst.writemask) != 0) {
1414b8e80941Smrg            break;
1415b8e80941Smrg         }
1416b8e80941Smrg
1417b8e80941Smrg         /* Check for reads of the register we're trying to coalesce into.  We
1418b8e80941Smrg          * can't go rewriting instructions above that to put some other value
1419b8e80941Smrg          * in the register instead.
1420b8e80941Smrg          */
1421b8e80941Smrg         if (to_mrf && scan_inst->mlen > 0) {
1422b8e80941Smrg            unsigned start = scan_inst->base_mrf;
1423b8e80941Smrg            unsigned end = scan_inst->base_mrf + scan_inst->mlen;
1424b8e80941Smrg
1425b8e80941Smrg            if (inst->dst.nr >= start && inst->dst.nr < end) {
1426b8e80941Smrg               break;
1427b8e80941Smrg            }
1428b8e80941Smrg         } else {
1429b8e80941Smrg            for (int i = 0; i < 3; i++) {
1430b8e80941Smrg               if (regions_overlap(inst->dst, inst->size_written,
1431b8e80941Smrg                                   scan_inst->src[i], scan_inst->size_read(i)))
1432b8e80941Smrg                  interfered = true;
1433b8e80941Smrg            }
1434b8e80941Smrg            if (interfered)
1435b8e80941Smrg               break;
1436b8e80941Smrg         }
1437b8e80941Smrg      }
1438b8e80941Smrg
1439b8e80941Smrg      if (chans_remaining == 0) {
1440b8e80941Smrg	 /* If we've made it here, we have an MOV we want to coalesce out, and
1441b8e80941Smrg	  * a scan_inst pointing to the earliest instruction involved in
1442b8e80941Smrg	  * computing the value.  Now go rewrite the instruction stream
1443b8e80941Smrg	  * between the two.
1444b8e80941Smrg	  */
1445b8e80941Smrg         vec4_instruction *scan_inst = _scan_inst;
1446b8e80941Smrg	 while (scan_inst != inst) {
1447b8e80941Smrg	    if (scan_inst->dst.file == VGRF &&
1448b8e80941Smrg                scan_inst->dst.nr == inst->src[0].nr &&
1449b8e80941Smrg		scan_inst->dst.offset == inst->src[0].offset) {
1450b8e80941Smrg               scan_inst->reswizzle(inst->dst.writemask,
1451b8e80941Smrg                                    inst->src[0].swizzle);
1452b8e80941Smrg	       scan_inst->dst.file = inst->dst.file;
1453b8e80941Smrg               scan_inst->dst.nr = inst->dst.nr;
1454b8e80941Smrg	       scan_inst->dst.offset = inst->dst.offset;
1455b8e80941Smrg               if (inst->saturate &&
1456b8e80941Smrg                   inst->dst.type != scan_inst->dst.type) {
1457b8e80941Smrg                  /* If we have reached this point, scan_inst is a non
1458b8e80941Smrg                   * type-converting 'mov' and we can modify its register types
1459b8e80941Smrg                   * to match the ones in inst. Otherwise, we could have an
1460b8e80941Smrg                   * incorrect saturation result.
1461b8e80941Smrg                   */
1462b8e80941Smrg                  scan_inst->dst.type = inst->dst.type;
1463b8e80941Smrg                  scan_inst->src[0].type = inst->src[0].type;
1464b8e80941Smrg               }
1465b8e80941Smrg	       scan_inst->saturate |= inst->saturate;
1466b8e80941Smrg	    }
1467b8e80941Smrg	    scan_inst = (vec4_instruction *)scan_inst->next;
1468b8e80941Smrg	 }
1469b8e80941Smrg	 inst->remove(block);
1470b8e80941Smrg	 progress = true;
1471b8e80941Smrg      }
1472b8e80941Smrg   }
1473b8e80941Smrg
1474b8e80941Smrg   if (progress)
1475b8e80941Smrg      invalidate_live_intervals();
1476b8e80941Smrg
1477b8e80941Smrg   return progress;
1478b8e80941Smrg}
1479b8e80941Smrg
1480b8e80941Smrg/**
1481b8e80941Smrg * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
1482b8e80941Smrg * flow.  We could probably do better here with some form of divergence
1483b8e80941Smrg * analysis.
1484b8e80941Smrg */
1485b8e80941Smrgbool
1486b8e80941Smrgvec4_visitor::eliminate_find_live_channel()
1487b8e80941Smrg{
1488b8e80941Smrg   bool progress = false;
1489b8e80941Smrg   unsigned depth = 0;
1490b8e80941Smrg
1491b8e80941Smrg   if (!brw_stage_has_packed_dispatch(devinfo, stage, stage_prog_data)) {
1492b8e80941Smrg      /* The optimization below assumes that channel zero is live on thread
1493b8e80941Smrg       * dispatch, which may not be the case if the fixed function dispatches
1494b8e80941Smrg       * threads sparsely.
1495b8e80941Smrg       */
1496b8e80941Smrg      return false;
1497b8e80941Smrg   }
1498b8e80941Smrg
1499b8e80941Smrg   foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1500b8e80941Smrg      switch (inst->opcode) {
1501b8e80941Smrg      case BRW_OPCODE_IF:
1502b8e80941Smrg      case BRW_OPCODE_DO:
1503b8e80941Smrg         depth++;
1504b8e80941Smrg         break;
1505b8e80941Smrg
1506b8e80941Smrg      case BRW_OPCODE_ENDIF:
1507b8e80941Smrg      case BRW_OPCODE_WHILE:
1508b8e80941Smrg         depth--;
1509b8e80941Smrg         break;
1510b8e80941Smrg
1511b8e80941Smrg      case SHADER_OPCODE_FIND_LIVE_CHANNEL:
1512b8e80941Smrg         if (depth == 0) {
1513b8e80941Smrg            inst->opcode = BRW_OPCODE_MOV;
1514b8e80941Smrg            inst->src[0] = brw_imm_d(0);
1515b8e80941Smrg            inst->force_writemask_all = true;
1516b8e80941Smrg            progress = true;
1517b8e80941Smrg         }
1518b8e80941Smrg         break;
1519b8e80941Smrg
1520b8e80941Smrg      default:
1521b8e80941Smrg         break;
1522b8e80941Smrg      }
1523b8e80941Smrg   }
1524b8e80941Smrg
1525b8e80941Smrg   return progress;
1526b8e80941Smrg}
1527b8e80941Smrg
1528b8e80941Smrg/**
1529b8e80941Smrg * Splits virtual GRFs requesting more than one contiguous physical register.
1530b8e80941Smrg *
1531b8e80941Smrg * We initially create large virtual GRFs for temporary structures, arrays,
1532b8e80941Smrg * and matrices, so that the visitor functions can add offsets to work their
1533b8e80941Smrg * way down to the actual member being accessed.  But when it comes to
1534b8e80941Smrg * optimization, we'd like to treat each register as individual storage if
1535b8e80941Smrg * possible.
1536b8e80941Smrg *
1537b8e80941Smrg * So far, the only thing that might prevent splitting is a send message from
1538b8e80941Smrg * a GRF on IVB.
1539b8e80941Smrg */
1540b8e80941Smrgvoid
1541b8e80941Smrgvec4_visitor::split_virtual_grfs()
1542b8e80941Smrg{
1543b8e80941Smrg   int num_vars = this->alloc.count;
1544b8e80941Smrg   int new_virtual_grf[num_vars];
1545b8e80941Smrg   bool split_grf[num_vars];
1546b8e80941Smrg
1547b8e80941Smrg   memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1548b8e80941Smrg
1549b8e80941Smrg   /* Try to split anything > 0 sized. */
1550b8e80941Smrg   for (int i = 0; i < num_vars; i++) {
1551b8e80941Smrg      split_grf[i] = this->alloc.sizes[i] != 1;
1552b8e80941Smrg   }
1553b8e80941Smrg
1554b8e80941Smrg   /* Check that the instructions are compatible with the registers we're trying
1555b8e80941Smrg    * to split.
1556b8e80941Smrg    */
1557b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1558b8e80941Smrg      if (inst->dst.file == VGRF && regs_written(inst) > 1)
1559b8e80941Smrg         split_grf[inst->dst.nr] = false;
1560b8e80941Smrg
1561b8e80941Smrg      for (int i = 0; i < 3; i++) {
1562b8e80941Smrg         if (inst->src[i].file == VGRF && regs_read(inst, i) > 1)
1563b8e80941Smrg            split_grf[inst->src[i].nr] = false;
1564b8e80941Smrg      }
1565b8e80941Smrg   }
1566b8e80941Smrg
1567b8e80941Smrg   /* Allocate new space for split regs.  Note that the virtual
1568b8e80941Smrg    * numbers will be contiguous.
1569b8e80941Smrg    */
1570b8e80941Smrg   for (int i = 0; i < num_vars; i++) {
1571b8e80941Smrg      if (!split_grf[i])
1572b8e80941Smrg         continue;
1573b8e80941Smrg
1574b8e80941Smrg      new_virtual_grf[i] = alloc.allocate(1);
1575b8e80941Smrg      for (unsigned j = 2; j < this->alloc.sizes[i]; j++) {
1576b8e80941Smrg         unsigned reg = alloc.allocate(1);
1577b8e80941Smrg         assert(reg == new_virtual_grf[i] + j - 1);
1578b8e80941Smrg         (void) reg;
1579b8e80941Smrg      }
1580b8e80941Smrg      this->alloc.sizes[i] = 1;
1581b8e80941Smrg   }
1582b8e80941Smrg
1583b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1584b8e80941Smrg      if (inst->dst.file == VGRF && split_grf[inst->dst.nr] &&
1585b8e80941Smrg          inst->dst.offset / REG_SIZE != 0) {
1586b8e80941Smrg         inst->dst.nr = (new_virtual_grf[inst->dst.nr] +
1587b8e80941Smrg                         inst->dst.offset / REG_SIZE - 1);
1588b8e80941Smrg         inst->dst.offset %= REG_SIZE;
1589b8e80941Smrg      }
1590b8e80941Smrg      for (int i = 0; i < 3; i++) {
1591b8e80941Smrg         if (inst->src[i].file == VGRF && split_grf[inst->src[i].nr] &&
1592b8e80941Smrg             inst->src[i].offset / REG_SIZE != 0) {
1593b8e80941Smrg            inst->src[i].nr = (new_virtual_grf[inst->src[i].nr] +
1594b8e80941Smrg                                inst->src[i].offset / REG_SIZE - 1);
1595b8e80941Smrg            inst->src[i].offset %= REG_SIZE;
1596b8e80941Smrg         }
1597b8e80941Smrg      }
1598b8e80941Smrg   }
1599b8e80941Smrg   invalidate_live_intervals();
1600b8e80941Smrg}
1601b8e80941Smrg
1602b8e80941Smrgvoid
1603b8e80941Smrgvec4_visitor::dump_instruction(backend_instruction *be_inst)
1604b8e80941Smrg{
1605b8e80941Smrg   dump_instruction(be_inst, stderr);
1606b8e80941Smrg}
1607b8e80941Smrg
1608b8e80941Smrgvoid
1609b8e80941Smrgvec4_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
1610b8e80941Smrg{
1611b8e80941Smrg   vec4_instruction *inst = (vec4_instruction *)be_inst;
1612b8e80941Smrg
1613b8e80941Smrg   if (inst->predicate) {
1614b8e80941Smrg      fprintf(file, "(%cf%d.%d%s) ",
1615b8e80941Smrg              inst->predicate_inverse ? '-' : '+',
1616b8e80941Smrg              inst->flag_subreg / 2,
1617b8e80941Smrg              inst->flag_subreg % 2,
1618b8e80941Smrg              pred_ctrl_align16[inst->predicate]);
1619b8e80941Smrg   }
1620b8e80941Smrg
1621b8e80941Smrg   fprintf(file, "%s(%d)", brw_instruction_name(devinfo, inst->opcode),
1622b8e80941Smrg           inst->exec_size);
1623b8e80941Smrg   if (inst->saturate)
1624b8e80941Smrg      fprintf(file, ".sat");
1625b8e80941Smrg   if (inst->conditional_mod) {
1626b8e80941Smrg      fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
1627b8e80941Smrg      if (!inst->predicate &&
1628b8e80941Smrg          (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
1629b8e80941Smrg                                inst->opcode != BRW_OPCODE_CSEL &&
1630b8e80941Smrg                                inst->opcode != BRW_OPCODE_IF &&
1631b8e80941Smrg                                inst->opcode != BRW_OPCODE_WHILE))) {
1632b8e80941Smrg         fprintf(file, ".f%d.%d", inst->flag_subreg / 2, inst->flag_subreg % 2);
1633b8e80941Smrg      }
1634b8e80941Smrg   }
1635b8e80941Smrg   fprintf(file, " ");
1636b8e80941Smrg
1637b8e80941Smrg   switch (inst->dst.file) {
1638b8e80941Smrg   case VGRF:
1639b8e80941Smrg      fprintf(file, "vgrf%d", inst->dst.nr);
1640b8e80941Smrg      break;
1641b8e80941Smrg   case FIXED_GRF:
1642b8e80941Smrg      fprintf(file, "g%d", inst->dst.nr);
1643b8e80941Smrg      break;
1644b8e80941Smrg   case MRF:
1645b8e80941Smrg      fprintf(file, "m%d", inst->dst.nr);
1646b8e80941Smrg      break;
1647b8e80941Smrg   case ARF:
1648b8e80941Smrg      switch (inst->dst.nr) {
1649b8e80941Smrg      case BRW_ARF_NULL:
1650b8e80941Smrg         fprintf(file, "null");
1651b8e80941Smrg         break;
1652b8e80941Smrg      case BRW_ARF_ADDRESS:
1653b8e80941Smrg         fprintf(file, "a0.%d", inst->dst.subnr);
1654b8e80941Smrg         break;
1655b8e80941Smrg      case BRW_ARF_ACCUMULATOR:
1656b8e80941Smrg         fprintf(file, "acc%d", inst->dst.subnr);
1657b8e80941Smrg         break;
1658b8e80941Smrg      case BRW_ARF_FLAG:
1659b8e80941Smrg         fprintf(file, "f%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1660b8e80941Smrg         break;
1661b8e80941Smrg      default:
1662b8e80941Smrg         fprintf(file, "arf%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1663b8e80941Smrg         break;
1664b8e80941Smrg      }
1665b8e80941Smrg      break;
1666b8e80941Smrg   case BAD_FILE:
1667b8e80941Smrg      fprintf(file, "(null)");
1668b8e80941Smrg      break;
1669b8e80941Smrg   case IMM:
1670b8e80941Smrg   case ATTR:
1671b8e80941Smrg   case UNIFORM:
1672b8e80941Smrg      unreachable("not reached");
1673b8e80941Smrg   }
1674b8e80941Smrg   if (inst->dst.offset ||
1675b8e80941Smrg       (inst->dst.file == VGRF &&
1676b8e80941Smrg        alloc.sizes[inst->dst.nr] * REG_SIZE != inst->size_written)) {
1677b8e80941Smrg      const unsigned reg_size = (inst->dst.file == UNIFORM ? 16 : REG_SIZE);
1678b8e80941Smrg      fprintf(file, "+%d.%d", inst->dst.offset / reg_size,
1679b8e80941Smrg              inst->dst.offset % reg_size);
1680b8e80941Smrg   }
1681b8e80941Smrg   if (inst->dst.writemask != WRITEMASK_XYZW) {
1682b8e80941Smrg      fprintf(file, ".");
1683b8e80941Smrg      if (inst->dst.writemask & 1)
1684b8e80941Smrg         fprintf(file, "x");
1685b8e80941Smrg      if (inst->dst.writemask & 2)
1686b8e80941Smrg         fprintf(file, "y");
1687b8e80941Smrg      if (inst->dst.writemask & 4)
1688b8e80941Smrg         fprintf(file, "z");
1689b8e80941Smrg      if (inst->dst.writemask & 8)
1690b8e80941Smrg         fprintf(file, "w");
1691b8e80941Smrg   }
1692b8e80941Smrg   fprintf(file, ":%s", brw_reg_type_to_letters(inst->dst.type));
1693b8e80941Smrg
1694b8e80941Smrg   if (inst->src[0].file != BAD_FILE)
1695b8e80941Smrg      fprintf(file, ", ");
1696b8e80941Smrg
1697b8e80941Smrg   for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1698b8e80941Smrg      if (inst->src[i].negate)
1699b8e80941Smrg         fprintf(file, "-");
1700b8e80941Smrg      if (inst->src[i].abs)
1701b8e80941Smrg         fprintf(file, "|");
1702b8e80941Smrg      switch (inst->src[i].file) {
1703b8e80941Smrg      case VGRF:
1704b8e80941Smrg         fprintf(file, "vgrf%d", inst->src[i].nr);
1705b8e80941Smrg         break;
1706b8e80941Smrg      case FIXED_GRF:
1707b8e80941Smrg         fprintf(file, "g%d.%d", inst->src[i].nr, inst->src[i].subnr);
1708b8e80941Smrg         break;
1709b8e80941Smrg      case ATTR:
1710b8e80941Smrg         fprintf(file, "attr%d", inst->src[i].nr);
1711b8e80941Smrg         break;
1712b8e80941Smrg      case UNIFORM:
1713b8e80941Smrg         fprintf(file, "u%d", inst->src[i].nr);
1714b8e80941Smrg         break;
1715b8e80941Smrg      case IMM:
1716b8e80941Smrg         switch (inst->src[i].type) {
1717b8e80941Smrg         case BRW_REGISTER_TYPE_F:
1718b8e80941Smrg            fprintf(file, "%fF", inst->src[i].f);
1719b8e80941Smrg            break;
1720b8e80941Smrg         case BRW_REGISTER_TYPE_DF:
1721b8e80941Smrg            fprintf(file, "%fDF", inst->src[i].df);
1722b8e80941Smrg            break;
1723b8e80941Smrg         case BRW_REGISTER_TYPE_D:
1724b8e80941Smrg            fprintf(file, "%dD", inst->src[i].d);
1725b8e80941Smrg            break;
1726b8e80941Smrg         case BRW_REGISTER_TYPE_UD:
1727b8e80941Smrg            fprintf(file, "%uU", inst->src[i].ud);
1728b8e80941Smrg            break;
1729b8e80941Smrg         case BRW_REGISTER_TYPE_VF:
1730b8e80941Smrg            fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
1731b8e80941Smrg                    brw_vf_to_float((inst->src[i].ud >>  0) & 0xff),
1732b8e80941Smrg                    brw_vf_to_float((inst->src[i].ud >>  8) & 0xff),
1733b8e80941Smrg                    brw_vf_to_float((inst->src[i].ud >> 16) & 0xff),
1734b8e80941Smrg                    brw_vf_to_float((inst->src[i].ud >> 24) & 0xff));
1735b8e80941Smrg            break;
1736b8e80941Smrg         default:
1737b8e80941Smrg            fprintf(file, "???");
1738b8e80941Smrg            break;
1739b8e80941Smrg         }
1740b8e80941Smrg         break;
1741b8e80941Smrg      case ARF:
1742b8e80941Smrg         switch (inst->src[i].nr) {
1743b8e80941Smrg         case BRW_ARF_NULL:
1744b8e80941Smrg            fprintf(file, "null");
1745b8e80941Smrg            break;
1746b8e80941Smrg         case BRW_ARF_ADDRESS:
1747b8e80941Smrg            fprintf(file, "a0.%d", inst->src[i].subnr);
1748b8e80941Smrg            break;
1749b8e80941Smrg         case BRW_ARF_ACCUMULATOR:
1750b8e80941Smrg            fprintf(file, "acc%d", inst->src[i].subnr);
1751b8e80941Smrg            break;
1752b8e80941Smrg         case BRW_ARF_FLAG:
1753b8e80941Smrg            fprintf(file, "f%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1754b8e80941Smrg            break;
1755b8e80941Smrg         default:
1756b8e80941Smrg            fprintf(file, "arf%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1757b8e80941Smrg            break;
1758b8e80941Smrg         }
1759b8e80941Smrg         break;
1760b8e80941Smrg      case BAD_FILE:
1761b8e80941Smrg         fprintf(file, "(null)");
1762b8e80941Smrg         break;
1763b8e80941Smrg      case MRF:
1764b8e80941Smrg         unreachable("not reached");
1765b8e80941Smrg      }
1766b8e80941Smrg
1767b8e80941Smrg      if (inst->src[i].offset ||
1768b8e80941Smrg          (inst->src[i].file == VGRF &&
1769b8e80941Smrg           alloc.sizes[inst->src[i].nr] * REG_SIZE != inst->size_read(i))) {
1770b8e80941Smrg         const unsigned reg_size = (inst->src[i].file == UNIFORM ? 16 : REG_SIZE);
1771b8e80941Smrg         fprintf(file, "+%d.%d", inst->src[i].offset / reg_size,
1772b8e80941Smrg                 inst->src[i].offset % reg_size);
1773b8e80941Smrg      }
1774b8e80941Smrg
1775b8e80941Smrg      if (inst->src[i].file != IMM) {
1776b8e80941Smrg         static const char *chans[4] = {"x", "y", "z", "w"};
1777b8e80941Smrg         fprintf(file, ".");
1778b8e80941Smrg         for (int c = 0; c < 4; c++) {
1779b8e80941Smrg            fprintf(file, "%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1780b8e80941Smrg         }
1781b8e80941Smrg      }
1782b8e80941Smrg
1783b8e80941Smrg      if (inst->src[i].abs)
1784b8e80941Smrg         fprintf(file, "|");
1785b8e80941Smrg
1786b8e80941Smrg      if (inst->src[i].file != IMM) {
1787b8e80941Smrg         fprintf(file, ":%s", brw_reg_type_to_letters(inst->src[i].type));
1788b8e80941Smrg      }
1789b8e80941Smrg
1790b8e80941Smrg      if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1791b8e80941Smrg         fprintf(file, ", ");
1792b8e80941Smrg   }
1793b8e80941Smrg
1794b8e80941Smrg   if (inst->force_writemask_all)
1795b8e80941Smrg      fprintf(file, " NoMask");
1796b8e80941Smrg
1797b8e80941Smrg   if (inst->exec_size != 8)
1798b8e80941Smrg      fprintf(file, " group%d", inst->group);
1799b8e80941Smrg
1800b8e80941Smrg   fprintf(file, "\n");
1801b8e80941Smrg}
1802b8e80941Smrg
1803b8e80941Smrg
1804b8e80941Smrgint
1805b8e80941Smrgvec4_vs_visitor::setup_attributes(int payload_reg)
1806b8e80941Smrg{
1807b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1808b8e80941Smrg      for (int i = 0; i < 3; i++) {
1809b8e80941Smrg         if (inst->src[i].file == ATTR) {
1810b8e80941Smrg            assert(inst->src[i].offset % REG_SIZE == 0);
1811b8e80941Smrg            int grf = payload_reg + inst->src[i].nr +
1812b8e80941Smrg                      inst->src[i].offset / REG_SIZE;
1813b8e80941Smrg
1814b8e80941Smrg            struct brw_reg reg = brw_vec8_grf(grf, 0);
1815b8e80941Smrg            reg.swizzle = inst->src[i].swizzle;
1816b8e80941Smrg            reg.type = inst->src[i].type;
1817b8e80941Smrg            reg.abs = inst->src[i].abs;
1818b8e80941Smrg            reg.negate = inst->src[i].negate;
1819b8e80941Smrg            inst->src[i] = reg;
1820b8e80941Smrg         }
1821b8e80941Smrg      }
1822b8e80941Smrg   }
1823b8e80941Smrg
1824b8e80941Smrg   return payload_reg + vs_prog_data->nr_attribute_slots;
1825b8e80941Smrg}
1826b8e80941Smrg
1827b8e80941Smrgint
1828b8e80941Smrgvec4_visitor::setup_uniforms(int reg)
1829b8e80941Smrg{
1830b8e80941Smrg   prog_data->base.dispatch_grf_start_reg = reg;
1831b8e80941Smrg
1832b8e80941Smrg   /* The pre-gen6 VS requires that some push constants get loaded no
1833b8e80941Smrg    * matter what, or the GPU would hang.
1834b8e80941Smrg    */
1835b8e80941Smrg   if (devinfo->gen < 6 && this->uniforms == 0) {
1836b8e80941Smrg      brw_stage_prog_data_add_params(stage_prog_data, 4);
1837b8e80941Smrg      for (unsigned int i = 0; i < 4; i++) {
1838b8e80941Smrg	 unsigned int slot = this->uniforms * 4 + i;
1839b8e80941Smrg	 stage_prog_data->param[slot] = BRW_PARAM_BUILTIN_ZERO;
1840b8e80941Smrg      }
1841b8e80941Smrg
1842b8e80941Smrg      this->uniforms++;
1843b8e80941Smrg      reg++;
1844b8e80941Smrg   } else {
1845b8e80941Smrg      reg += ALIGN(uniforms, 2) / 2;
1846b8e80941Smrg   }
1847b8e80941Smrg
1848b8e80941Smrg   for (int i = 0; i < 4; i++)
1849b8e80941Smrg      reg += stage_prog_data->ubo_ranges[i].length;
1850b8e80941Smrg
1851b8e80941Smrg   stage_prog_data->nr_params = this->uniforms * 4;
1852b8e80941Smrg
1853b8e80941Smrg   prog_data->base.curb_read_length =
1854b8e80941Smrg      reg - prog_data->base.dispatch_grf_start_reg;
1855b8e80941Smrg
1856b8e80941Smrg   return reg;
1857b8e80941Smrg}
1858b8e80941Smrg
1859b8e80941Smrgvoid
1860b8e80941Smrgvec4_vs_visitor::setup_payload(void)
1861b8e80941Smrg{
1862b8e80941Smrg   int reg = 0;
1863b8e80941Smrg
1864b8e80941Smrg   /* The payload always contains important data in g0, which contains
1865b8e80941Smrg    * the URB handles that are passed on to the URB write at the end
1866b8e80941Smrg    * of the thread.  So, we always start push constants at g1.
1867b8e80941Smrg    */
1868b8e80941Smrg   reg++;
1869b8e80941Smrg
1870b8e80941Smrg   reg = setup_uniforms(reg);
1871b8e80941Smrg
1872b8e80941Smrg   reg = setup_attributes(reg);
1873b8e80941Smrg
1874b8e80941Smrg   this->first_non_payload_grf = reg;
1875b8e80941Smrg}
1876b8e80941Smrg
1877b8e80941Smrgbool
1878b8e80941Smrgvec4_visitor::lower_minmax()
1879b8e80941Smrg{
1880b8e80941Smrg   assert(devinfo->gen < 6);
1881b8e80941Smrg
1882b8e80941Smrg   bool progress = false;
1883b8e80941Smrg
1884b8e80941Smrg   foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1885b8e80941Smrg      const vec4_builder ibld(this, block, inst);
1886b8e80941Smrg
1887b8e80941Smrg      if (inst->opcode == BRW_OPCODE_SEL &&
1888b8e80941Smrg          inst->predicate == BRW_PREDICATE_NONE) {
1889b8e80941Smrg         /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
1890b8e80941Smrg          *        the original SEL.L/GE instruction
1891b8e80941Smrg          */
1892b8e80941Smrg         ibld.CMP(ibld.null_reg_d(), inst->src[0], inst->src[1],
1893b8e80941Smrg                  inst->conditional_mod);
1894b8e80941Smrg         inst->predicate = BRW_PREDICATE_NORMAL;
1895b8e80941Smrg         inst->conditional_mod = BRW_CONDITIONAL_NONE;
1896b8e80941Smrg
1897b8e80941Smrg         progress = true;
1898b8e80941Smrg      }
1899b8e80941Smrg   }
1900b8e80941Smrg
1901b8e80941Smrg   if (progress)
1902b8e80941Smrg      invalidate_live_intervals();
1903b8e80941Smrg
1904b8e80941Smrg   return progress;
1905b8e80941Smrg}
1906b8e80941Smrg
1907b8e80941Smrgsrc_reg
1908b8e80941Smrgvec4_visitor::get_timestamp()
1909b8e80941Smrg{
1910b8e80941Smrg   assert(devinfo->gen >= 7);
1911b8e80941Smrg
1912b8e80941Smrg   src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1913b8e80941Smrg                                BRW_ARF_TIMESTAMP,
1914b8e80941Smrg                                0,
1915b8e80941Smrg                                0,
1916b8e80941Smrg                                0,
1917b8e80941Smrg                                BRW_REGISTER_TYPE_UD,
1918b8e80941Smrg                                BRW_VERTICAL_STRIDE_0,
1919b8e80941Smrg                                BRW_WIDTH_4,
1920b8e80941Smrg                                BRW_HORIZONTAL_STRIDE_4,
1921b8e80941Smrg                                BRW_SWIZZLE_XYZW,
1922b8e80941Smrg                                WRITEMASK_XYZW));
1923b8e80941Smrg
1924b8e80941Smrg   dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1925b8e80941Smrg
1926b8e80941Smrg   vec4_instruction *mov = emit(MOV(dst, ts));
1927b8e80941Smrg   /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1928b8e80941Smrg    * even if it's not enabled in the dispatch.
1929b8e80941Smrg    */
1930b8e80941Smrg   mov->force_writemask_all = true;
1931b8e80941Smrg
1932b8e80941Smrg   return src_reg(dst);
1933b8e80941Smrg}
1934b8e80941Smrg
1935b8e80941Smrgvoid
1936b8e80941Smrgvec4_visitor::emit_shader_time_begin()
1937b8e80941Smrg{
1938b8e80941Smrg   current_annotation = "shader time start";
1939b8e80941Smrg   shader_start_time = get_timestamp();
1940b8e80941Smrg}
1941b8e80941Smrg
1942b8e80941Smrgvoid
1943b8e80941Smrgvec4_visitor::emit_shader_time_end()
1944b8e80941Smrg{
1945b8e80941Smrg   current_annotation = "shader time end";
1946b8e80941Smrg   src_reg shader_end_time = get_timestamp();
1947b8e80941Smrg
1948b8e80941Smrg
1949b8e80941Smrg   /* Check that there weren't any timestamp reset events (assuming these
1950b8e80941Smrg    * were the only two timestamp reads that happened).
1951b8e80941Smrg    */
1952b8e80941Smrg   src_reg reset_end = shader_end_time;
1953b8e80941Smrg   reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1954b8e80941Smrg   vec4_instruction *test = emit(AND(dst_null_ud(), reset_end, brw_imm_ud(1u)));
1955b8e80941Smrg   test->conditional_mod = BRW_CONDITIONAL_Z;
1956b8e80941Smrg
1957b8e80941Smrg   emit(IF(BRW_PREDICATE_NORMAL));
1958b8e80941Smrg
1959b8e80941Smrg   /* Take the current timestamp and get the delta. */
1960b8e80941Smrg   shader_start_time.negate = true;
1961b8e80941Smrg   dst_reg diff = dst_reg(this, glsl_type::uint_type);
1962b8e80941Smrg   emit(ADD(diff, shader_start_time, shader_end_time));
1963b8e80941Smrg
1964b8e80941Smrg   /* If there were no instructions between the two timestamp gets, the diff
1965b8e80941Smrg    * is 2 cycles.  Remove that overhead, so I can forget about that when
1966b8e80941Smrg    * trying to determine the time taken for single instructions.
1967b8e80941Smrg    */
1968b8e80941Smrg   emit(ADD(diff, src_reg(diff), brw_imm_ud(-2u)));
1969b8e80941Smrg
1970b8e80941Smrg   emit_shader_time_write(0, src_reg(diff));
1971b8e80941Smrg   emit_shader_time_write(1, brw_imm_ud(1u));
1972b8e80941Smrg   emit(BRW_OPCODE_ELSE);
1973b8e80941Smrg   emit_shader_time_write(2, brw_imm_ud(1u));
1974b8e80941Smrg   emit(BRW_OPCODE_ENDIF);
1975b8e80941Smrg}
1976b8e80941Smrg
1977b8e80941Smrgvoid
1978b8e80941Smrgvec4_visitor::emit_shader_time_write(int shader_time_subindex, src_reg value)
1979b8e80941Smrg{
1980b8e80941Smrg   dst_reg dst =
1981b8e80941Smrg      dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1982b8e80941Smrg
1983b8e80941Smrg   dst_reg offset = dst;
1984b8e80941Smrg   dst_reg time = dst;
1985b8e80941Smrg   time.offset += REG_SIZE;
1986b8e80941Smrg
1987b8e80941Smrg   offset.type = BRW_REGISTER_TYPE_UD;
1988b8e80941Smrg   int index = shader_time_index * 3 + shader_time_subindex;
1989b8e80941Smrg   emit(MOV(offset, brw_imm_d(index * BRW_SHADER_TIME_STRIDE)));
1990b8e80941Smrg
1991b8e80941Smrg   time.type = BRW_REGISTER_TYPE_UD;
1992b8e80941Smrg   emit(MOV(time, value));
1993b8e80941Smrg
1994b8e80941Smrg   vec4_instruction *inst =
1995b8e80941Smrg      emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1996b8e80941Smrg   inst->mlen = 2;
1997b8e80941Smrg}
1998b8e80941Smrg
1999b8e80941Smrgstatic bool
2000b8e80941Smrgis_align1_df(vec4_instruction *inst)
2001b8e80941Smrg{
2002b8e80941Smrg   switch (inst->opcode) {
2003b8e80941Smrg   case VEC4_OPCODE_DOUBLE_TO_F32:
2004b8e80941Smrg   case VEC4_OPCODE_DOUBLE_TO_D32:
2005b8e80941Smrg   case VEC4_OPCODE_DOUBLE_TO_U32:
2006b8e80941Smrg   case VEC4_OPCODE_TO_DOUBLE:
2007b8e80941Smrg   case VEC4_OPCODE_PICK_LOW_32BIT:
2008b8e80941Smrg   case VEC4_OPCODE_PICK_HIGH_32BIT:
2009b8e80941Smrg   case VEC4_OPCODE_SET_LOW_32BIT:
2010b8e80941Smrg   case VEC4_OPCODE_SET_HIGH_32BIT:
2011b8e80941Smrg      return true;
2012b8e80941Smrg   default:
2013b8e80941Smrg      return false;
2014b8e80941Smrg   }
2015b8e80941Smrg}
2016b8e80941Smrg
2017b8e80941Smrg/**
2018b8e80941Smrg * Three source instruction must have a GRF/MRF destination register.
2019b8e80941Smrg * ARF NULL is not allowed.  Fix that up by allocating a temporary GRF.
2020b8e80941Smrg */
2021b8e80941Smrgvoid
2022b8e80941Smrgvec4_visitor::fixup_3src_null_dest()
2023b8e80941Smrg{
2024b8e80941Smrg   bool progress = false;
2025b8e80941Smrg
2026b8e80941Smrg   foreach_block_and_inst_safe (block, vec4_instruction, inst, cfg) {
2027b8e80941Smrg      if (inst->is_3src(devinfo) && inst->dst.is_null()) {
2028b8e80941Smrg         const unsigned size_written = type_sz(inst->dst.type);
2029b8e80941Smrg         const unsigned num_regs = DIV_ROUND_UP(size_written, REG_SIZE);
2030b8e80941Smrg
2031b8e80941Smrg         inst->dst = retype(dst_reg(VGRF, alloc.allocate(num_regs)),
2032b8e80941Smrg                            inst->dst.type);
2033b8e80941Smrg         progress = true;
2034b8e80941Smrg      }
2035b8e80941Smrg   }
2036b8e80941Smrg
2037b8e80941Smrg   if (progress)
2038b8e80941Smrg      invalidate_live_intervals();
2039b8e80941Smrg}
2040b8e80941Smrg
2041b8e80941Smrgvoid
2042b8e80941Smrgvec4_visitor::convert_to_hw_regs()
2043b8e80941Smrg{
2044b8e80941Smrg   foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
2045b8e80941Smrg      for (int i = 0; i < 3; i++) {
2046b8e80941Smrg         class src_reg &src = inst->src[i];
2047b8e80941Smrg         struct brw_reg reg;
2048b8e80941Smrg         switch (src.file) {
2049b8e80941Smrg         case VGRF: {
2050b8e80941Smrg            reg = byte_offset(brw_vecn_grf(4, src.nr, 0), src.offset);
2051b8e80941Smrg            reg.type = src.type;
2052b8e80941Smrg            reg.abs = src.abs;
2053b8e80941Smrg            reg.negate = src.negate;
2054b8e80941Smrg            break;
2055b8e80941Smrg         }
2056b8e80941Smrg
2057b8e80941Smrg         case UNIFORM: {
2058b8e80941Smrg            reg = stride(byte_offset(brw_vec4_grf(
2059b8e80941Smrg                                        prog_data->base.dispatch_grf_start_reg +
2060b8e80941Smrg                                        src.nr / 2, src.nr % 2 * 4),
2061b8e80941Smrg                                     src.offset),
2062b8e80941Smrg                         0, 4, 1);
2063b8e80941Smrg            reg.type = src.type;
2064b8e80941Smrg            reg.abs = src.abs;
2065b8e80941Smrg            reg.negate = src.negate;
2066b8e80941Smrg
2067b8e80941Smrg            /* This should have been moved to pull constants. */
2068b8e80941Smrg            assert(!src.reladdr);
2069b8e80941Smrg            break;
2070b8e80941Smrg         }
2071b8e80941Smrg
2072b8e80941Smrg         case FIXED_GRF:
2073b8e80941Smrg            if (type_sz(src.type) == 8) {
2074b8e80941Smrg               reg = src.as_brw_reg();
2075b8e80941Smrg               break;
2076b8e80941Smrg            }
2077b8e80941Smrg            /* fallthrough */
2078b8e80941Smrg         case ARF:
2079b8e80941Smrg         case IMM:
2080b8e80941Smrg            continue;
2081b8e80941Smrg
2082b8e80941Smrg         case BAD_FILE:
2083b8e80941Smrg            /* Probably unused. */
2084b8e80941Smrg            reg = brw_null_reg();
2085b8e80941Smrg            reg = retype(reg, src.type);
2086b8e80941Smrg            break;
2087b8e80941Smrg
2088b8e80941Smrg         case MRF:
2089b8e80941Smrg         case ATTR:
2090b8e80941Smrg            unreachable("not reached");
2091b8e80941Smrg         }
2092b8e80941Smrg
2093b8e80941Smrg         apply_logical_swizzle(&reg, inst, i);
2094b8e80941Smrg         src = reg;
2095b8e80941Smrg
2096b8e80941Smrg         /* From IVB PRM, vol4, part3, "General Restrictions on Regioning
2097b8e80941Smrg          * Parameters":
2098b8e80941Smrg          *
2099b8e80941Smrg          *   "If ExecSize = Width and HorzStride ≠ 0, VertStride must be set
2100b8e80941Smrg          *    to Width * HorzStride."
2101b8e80941Smrg          *
2102b8e80941Smrg          * We can break this rule with DF sources on DF align1
2103b8e80941Smrg          * instructions, because the exec_size would be 4 and width is 4.
2104b8e80941Smrg          * As we know we are not accessing to next GRF, it is safe to
2105b8e80941Smrg          * set vstride to the formula given by the rule itself.
2106b8e80941Smrg          */
2107b8e80941Smrg         if (is_align1_df(inst) && (cvt(inst->exec_size) - 1) == src.width)
2108b8e80941Smrg            src.vstride = src.width + src.hstride;
2109b8e80941Smrg      }
2110b8e80941Smrg
2111b8e80941Smrg      if (inst->is_3src(devinfo)) {
2112b8e80941Smrg         /* 3-src instructions with scalar sources support arbitrary subnr,
2113b8e80941Smrg          * but don't actually use swizzles.  Convert swizzle into subnr.
2114b8e80941Smrg          * Skip this for double-precision instructions: RepCtrl=1 is not
2115b8e80941Smrg          * allowed for them and needs special handling.
2116b8e80941Smrg          */
2117b8e80941Smrg         for (int i = 0; i < 3; i++) {
2118b8e80941Smrg            if (inst->src[i].vstride == BRW_VERTICAL_STRIDE_0 &&
2119b8e80941Smrg                type_sz(inst->src[i].type) < 8) {
2120b8e80941Smrg               assert(brw_is_single_value_swizzle(inst->src[i].swizzle));
2121b8e80941Smrg               inst->src[i].subnr += 4 * BRW_GET_SWZ(inst->src[i].swizzle, 0);
2122b8e80941Smrg            }
2123b8e80941Smrg         }
2124b8e80941Smrg      }
2125b8e80941Smrg
2126b8e80941Smrg      dst_reg &dst = inst->dst;
2127b8e80941Smrg      struct brw_reg reg;
2128b8e80941Smrg
2129b8e80941Smrg      switch (inst->dst.file) {
2130b8e80941Smrg      case VGRF:
2131b8e80941Smrg         reg = byte_offset(brw_vec8_grf(dst.nr, 0), dst.offset);
2132b8e80941Smrg         reg.type = dst.type;
2133b8e80941Smrg         reg.writemask = dst.writemask;
2134b8e80941Smrg         break;
2135b8e80941Smrg
2136b8e80941Smrg      case MRF:
2137b8e80941Smrg         reg = byte_offset(brw_message_reg(dst.nr), dst.offset);
2138b8e80941Smrg         assert((reg.nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
2139b8e80941Smrg         reg.type = dst.type;
2140b8e80941Smrg         reg.writemask = dst.writemask;
2141b8e80941Smrg         break;
2142b8e80941Smrg
2143b8e80941Smrg      case ARF:
2144b8e80941Smrg      case FIXED_GRF:
2145b8e80941Smrg         reg = dst.as_brw_reg();
2146b8e80941Smrg         break;
2147b8e80941Smrg
2148b8e80941Smrg      case BAD_FILE:
2149b8e80941Smrg         reg = brw_null_reg();
2150b8e80941Smrg         reg = retype(reg, dst.type);
2151b8e80941Smrg         break;
2152b8e80941Smrg
2153b8e80941Smrg      case IMM:
2154b8e80941Smrg      case ATTR:
2155b8e80941Smrg      case UNIFORM:
2156b8e80941Smrg         unreachable("not reached");
2157b8e80941Smrg      }
2158b8e80941Smrg
2159b8e80941Smrg      dst = reg;
2160b8e80941Smrg   }
2161b8e80941Smrg}
2162b8e80941Smrg
2163b8e80941Smrgstatic bool
2164b8e80941Smrgstage_uses_interleaved_attributes(unsigned stage,
2165b8e80941Smrg                                  enum shader_dispatch_mode dispatch_mode)
2166b8e80941Smrg{
2167b8e80941Smrg   switch (stage) {
2168b8e80941Smrg   case MESA_SHADER_TESS_EVAL:
2169b8e80941Smrg      return true;
2170b8e80941Smrg   case MESA_SHADER_GEOMETRY:
2171b8e80941Smrg      return dispatch_mode != DISPATCH_MODE_4X2_DUAL_OBJECT;
2172b8e80941Smrg   default:
2173b8e80941Smrg      return false;
2174b8e80941Smrg   }
2175b8e80941Smrg}
2176b8e80941Smrg
2177b8e80941Smrg/**
2178b8e80941Smrg * Get the closest native SIMD width supported by the hardware for instruction
2179b8e80941Smrg * \p inst.  The instruction will be left untouched by
2180b8e80941Smrg * vec4_visitor::lower_simd_width() if the returned value matches the
2181b8e80941Smrg * instruction's original execution size.
2182b8e80941Smrg */
2183b8e80941Smrgstatic unsigned
2184b8e80941Smrgget_lowered_simd_width(const struct gen_device_info *devinfo,
2185b8e80941Smrg                       enum shader_dispatch_mode dispatch_mode,
2186b8e80941Smrg                       unsigned stage, const vec4_instruction *inst)
2187b8e80941Smrg{
2188b8e80941Smrg   /* Do not split some instructions that require special handling */
2189b8e80941Smrg   switch (inst->opcode) {
2190b8e80941Smrg   case SHADER_OPCODE_GEN4_SCRATCH_READ:
2191b8e80941Smrg   case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
2192b8e80941Smrg      return inst->exec_size;
2193b8e80941Smrg   default:
2194b8e80941Smrg      break;
2195b8e80941Smrg   }
2196b8e80941Smrg
2197b8e80941Smrg   unsigned lowered_width = MIN2(16, inst->exec_size);
2198b8e80941Smrg
2199b8e80941Smrg   /* We need to split some cases of double-precision instructions that write
2200b8e80941Smrg    * 2 registers. We only need to care about this in gen7 because that is the
2201b8e80941Smrg    * only hardware that implements fp64 in Align16.
2202b8e80941Smrg    */
2203b8e80941Smrg   if (devinfo->gen == 7 && inst->size_written > REG_SIZE) {
2204b8e80941Smrg      /* Align16 8-wide double-precision SEL does not work well. Verified
2205b8e80941Smrg       * empirically.
2206b8e80941Smrg       */
2207b8e80941Smrg      if (inst->opcode == BRW_OPCODE_SEL && type_sz(inst->dst.type) == 8)
2208b8e80941Smrg         lowered_width = MIN2(lowered_width, 4);
2209b8e80941Smrg
2210b8e80941Smrg      /* HSW PRM, 3D Media GPGPU Engine, Region Alignment Rules for Direct
2211b8e80941Smrg       * Register Addressing:
2212b8e80941Smrg       *
2213b8e80941Smrg       *    "When destination spans two registers, the source MUST span two
2214b8e80941Smrg       *     registers."
2215b8e80941Smrg       */
2216b8e80941Smrg      for (unsigned i = 0; i < 3; i++) {
2217b8e80941Smrg         if (inst->src[i].file == BAD_FILE)
2218b8e80941Smrg            continue;
2219b8e80941Smrg         if (inst->size_read(i) <= REG_SIZE)
2220b8e80941Smrg            lowered_width = MIN2(lowered_width, 4);
2221b8e80941Smrg
2222b8e80941Smrg         /* Interleaved attribute setups use a vertical stride of 0, which
2223b8e80941Smrg          * makes them hit the associated instruction decompression bug in gen7.
2224b8e80941Smrg          * Split them to prevent this.
2225b8e80941Smrg          */
2226b8e80941Smrg         if (inst->src[i].file == ATTR &&
2227b8e80941Smrg             stage_uses_interleaved_attributes(stage, dispatch_mode))
2228b8e80941Smrg            lowered_width = MIN2(lowered_width, 4);
2229b8e80941Smrg      }
2230b8e80941Smrg   }
2231b8e80941Smrg
2232b8e80941Smrg   /* IvyBridge can manage a maximum of 4 DFs per SIMD4x2 instruction, since
2233b8e80941Smrg    * it doesn't support compression in Align16 mode, no matter if it has
2234b8e80941Smrg    * force_writemask_all enabled or disabled (the latter is affected by the
2235b8e80941Smrg    * compressed instruction bug in gen7, which is another reason to enforce
2236b8e80941Smrg    * this limit).
2237b8e80941Smrg    */
2238b8e80941Smrg   if (devinfo->gen == 7 && !devinfo->is_haswell &&
2239b8e80941Smrg       (get_exec_type_size(inst) == 8 || type_sz(inst->dst.type) == 8))
2240b8e80941Smrg      lowered_width = MIN2(lowered_width, 4);
2241b8e80941Smrg
2242b8e80941Smrg   return lowered_width;
2243b8e80941Smrg}
2244b8e80941Smrg
2245b8e80941Smrgstatic bool
2246b8e80941Smrgdst_src_regions_overlap(vec4_instruction *inst)
2247b8e80941Smrg{
2248b8e80941Smrg   if (inst->size_written == 0)
2249b8e80941Smrg      return false;
2250b8e80941Smrg
2251b8e80941Smrg   unsigned dst_start = inst->dst.offset;
2252b8e80941Smrg   unsigned dst_end = dst_start + inst->size_written - 1;
2253b8e80941Smrg   for (int i = 0; i < 3; i++) {
2254b8e80941Smrg      if (inst->src[i].file == BAD_FILE)
2255b8e80941Smrg         continue;
2256b8e80941Smrg
2257b8e80941Smrg      if (inst->dst.file != inst->src[i].file ||
2258b8e80941Smrg          inst->dst.nr != inst->src[i].nr)
2259b8e80941Smrg         continue;
2260b8e80941Smrg
2261b8e80941Smrg      unsigned src_start = inst->src[i].offset;
2262b8e80941Smrg      unsigned src_end = src_start + inst->size_read(i) - 1;
2263b8e80941Smrg
2264b8e80941Smrg      if ((dst_start >= src_start && dst_start <= src_end) ||
2265b8e80941Smrg          (dst_end >= src_start && dst_end <= src_end) ||
2266b8e80941Smrg          (dst_start <= src_start && dst_end >= src_end)) {
2267b8e80941Smrg         return true;
2268b8e80941Smrg      }
2269b8e80941Smrg   }
2270b8e80941Smrg
2271b8e80941Smrg   return false;
2272b8e80941Smrg}
2273b8e80941Smrg
2274b8e80941Smrgbool
2275b8e80941Smrgvec4_visitor::lower_simd_width()
2276b8e80941Smrg{
2277b8e80941Smrg   bool progress = false;
2278b8e80941Smrg
2279b8e80941Smrg   foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
2280b8e80941Smrg      const unsigned lowered_width =
2281b8e80941Smrg         get_lowered_simd_width(devinfo, prog_data->dispatch_mode, stage, inst);
2282b8e80941Smrg      assert(lowered_width <= inst->exec_size);
2283b8e80941Smrg      if (lowered_width == inst->exec_size)
2284b8e80941Smrg         continue;
2285b8e80941Smrg
2286b8e80941Smrg      /* We need to deal with source / destination overlaps when splitting.
2287b8e80941Smrg       * The hardware supports reading from and writing to the same register
2288b8e80941Smrg       * in the same instruction, but we need to be careful that each split
2289b8e80941Smrg       * instruction we produce does not corrupt the source of the next.
2290b8e80941Smrg       *
2291b8e80941Smrg       * The easiest way to handle this is to make the split instructions write
2292b8e80941Smrg       * to temporaries if there is an src/dst overlap and then move from the
2293b8e80941Smrg       * temporaries to the original destination. We also need to consider
2294b8e80941Smrg       * instructions that do partial writes via align1 opcodes, in which case
2295b8e80941Smrg       * we need to make sure that the we initialize the temporary with the
2296b8e80941Smrg       * value of the instruction's dst.
2297b8e80941Smrg       */
2298b8e80941Smrg      bool needs_temp = dst_src_regions_overlap(inst);
2299b8e80941Smrg      for (unsigned n = 0; n < inst->exec_size / lowered_width; n++)  {
2300b8e80941Smrg         unsigned channel_offset = lowered_width * n;
2301b8e80941Smrg
2302b8e80941Smrg         unsigned size_written = lowered_width * type_sz(inst->dst.type);
2303b8e80941Smrg
2304b8e80941Smrg         /* Create the split instruction from the original so that we copy all
2305b8e80941Smrg          * relevant instruction fields, then set the width and calculate the
2306b8e80941Smrg          * new dst/src regions.
2307b8e80941Smrg          */
2308b8e80941Smrg         vec4_instruction *linst = new(mem_ctx) vec4_instruction(*inst);
2309b8e80941Smrg         linst->exec_size = lowered_width;
2310b8e80941Smrg         linst->group = channel_offset;
2311b8e80941Smrg         linst->size_written = size_written;
2312b8e80941Smrg
2313b8e80941Smrg         /* Compute split dst region */
2314b8e80941Smrg         dst_reg dst;
2315b8e80941Smrg         if (needs_temp) {
2316b8e80941Smrg            unsigned num_regs = DIV_ROUND_UP(size_written, REG_SIZE);
2317b8e80941Smrg            dst = retype(dst_reg(VGRF, alloc.allocate(num_regs)),
2318b8e80941Smrg                         inst->dst.type);
2319b8e80941Smrg            if (inst->is_align1_partial_write()) {
2320b8e80941Smrg               vec4_instruction *copy = MOV(dst, src_reg(inst->dst));
2321b8e80941Smrg               copy->exec_size = lowered_width;
2322b8e80941Smrg               copy->group = channel_offset;
2323b8e80941Smrg               copy->size_written = size_written;
2324b8e80941Smrg               inst->insert_before(block, copy);
2325b8e80941Smrg            }
2326b8e80941Smrg         } else {
2327b8e80941Smrg            dst = horiz_offset(inst->dst, channel_offset);
2328b8e80941Smrg         }
2329b8e80941Smrg         linst->dst = dst;
2330b8e80941Smrg
2331b8e80941Smrg         /* Compute split source regions */
2332b8e80941Smrg         for (int i = 0; i < 3; i++) {
2333b8e80941Smrg            if (linst->src[i].file == BAD_FILE)
2334b8e80941Smrg               continue;
2335b8e80941Smrg
2336b8e80941Smrg            bool is_interleaved_attr =
2337b8e80941Smrg               linst->src[i].file == ATTR &&
2338b8e80941Smrg               stage_uses_interleaved_attributes(stage,
2339b8e80941Smrg                                                 prog_data->dispatch_mode);
2340b8e80941Smrg
2341b8e80941Smrg            if (!is_uniform(linst->src[i]) && !is_interleaved_attr)
2342b8e80941Smrg               linst->src[i] = horiz_offset(linst->src[i], channel_offset);
2343b8e80941Smrg         }
2344b8e80941Smrg
2345b8e80941Smrg         inst->insert_before(block, linst);
2346b8e80941Smrg
2347b8e80941Smrg         /* If we used a temporary to store the result of the split
2348b8e80941Smrg          * instruction, copy the result to the original destination
2349b8e80941Smrg          */
2350b8e80941Smrg         if (needs_temp) {
2351b8e80941Smrg            vec4_instruction *mov =
2352b8e80941Smrg               MOV(offset(inst->dst, lowered_width, n), src_reg(dst));
2353b8e80941Smrg            mov->exec_size = lowered_width;
2354b8e80941Smrg            mov->group = channel_offset;
2355b8e80941Smrg            mov->size_written = size_written;
2356b8e80941Smrg            mov->predicate = inst->predicate;
2357b8e80941Smrg            inst->insert_before(block, mov);
2358b8e80941Smrg         }
2359b8e80941Smrg      }
2360b8e80941Smrg
2361b8e80941Smrg      inst->remove(block);
2362b8e80941Smrg      progress = true;
2363b8e80941Smrg   }
2364b8e80941Smrg
2365b8e80941Smrg   if (progress)
2366b8e80941Smrg      invalidate_live_intervals();
2367b8e80941Smrg
2368b8e80941Smrg   return progress;
2369b8e80941Smrg}
2370b8e80941Smrg
2371b8e80941Smrgstatic brw_predicate
2372b8e80941Smrgscalarize_predicate(brw_predicate predicate, unsigned writemask)
2373b8e80941Smrg{
2374b8e80941Smrg   if (predicate != BRW_PREDICATE_NORMAL)
2375b8e80941Smrg      return predicate;
2376b8e80941Smrg
2377b8e80941Smrg   switch (writemask) {
2378b8e80941Smrg   case WRITEMASK_X:
2379b8e80941Smrg      return BRW_PREDICATE_ALIGN16_REPLICATE_X;
2380b8e80941Smrg   case WRITEMASK_Y:
2381b8e80941Smrg      return BRW_PREDICATE_ALIGN16_REPLICATE_Y;
2382b8e80941Smrg   case WRITEMASK_Z:
2383b8e80941Smrg      return BRW_PREDICATE_ALIGN16_REPLICATE_Z;
2384b8e80941Smrg   case WRITEMASK_W:
2385b8e80941Smrg      return BRW_PREDICATE_ALIGN16_REPLICATE_W;
2386b8e80941Smrg   default:
2387b8e80941Smrg      unreachable("invalid writemask");
2388b8e80941Smrg   }
2389b8e80941Smrg}
2390b8e80941Smrg
2391b8e80941Smrg/* Gen7 has a hardware decompression bug that we can exploit to represent
2392b8e80941Smrg * handful of additional swizzles natively.
2393b8e80941Smrg */
2394b8e80941Smrgstatic bool
2395b8e80941Smrgis_gen7_supported_64bit_swizzle(vec4_instruction *inst, unsigned arg)
2396b8e80941Smrg{
2397b8e80941Smrg   switch (inst->src[arg].swizzle) {
2398b8e80941Smrg   case BRW_SWIZZLE_XXXX:
2399b8e80941Smrg   case BRW_SWIZZLE_YYYY:
2400b8e80941Smrg   case BRW_SWIZZLE_ZZZZ:
2401b8e80941Smrg   case BRW_SWIZZLE_WWWW:
2402b8e80941Smrg   case BRW_SWIZZLE_XYXY:
2403b8e80941Smrg   case BRW_SWIZZLE_YXYX:
2404b8e80941Smrg   case BRW_SWIZZLE_ZWZW:
2405b8e80941Smrg   case BRW_SWIZZLE_WZWZ:
2406b8e80941Smrg      return true;
2407b8e80941Smrg   default:
2408b8e80941Smrg      return false;
2409b8e80941Smrg   }
2410b8e80941Smrg}
2411b8e80941Smrg
2412b8e80941Smrg/* 64-bit sources use regions with a width of 2. These 2 elements in each row
2413b8e80941Smrg * can be addressed using 32-bit swizzles (which is what the hardware supports)
2414b8e80941Smrg * but it also means that the swizzle we apply on the first two components of a
2415b8e80941Smrg * dvec4 is coupled with the swizzle we use for the last 2. In other words,
2416b8e80941Smrg * only some specific swizzle combinations can be natively supported.
2417b8e80941Smrg *
2418b8e80941Smrg * FIXME: we can go an step further and implement even more swizzle
2419b8e80941Smrg *        variations using only partial scalarization.
2420b8e80941Smrg *
2421b8e80941Smrg * For more details see:
2422b8e80941Smrg * https://bugs.freedesktop.org/show_bug.cgi?id=92760#c82
2423b8e80941Smrg */
2424b8e80941Smrgbool
2425b8e80941Smrgvec4_visitor::is_supported_64bit_region(vec4_instruction *inst, unsigned arg)
2426b8e80941Smrg{
2427b8e80941Smrg   const src_reg &src = inst->src[arg];
2428b8e80941Smrg   assert(type_sz(src.type) == 8);
2429b8e80941Smrg
2430b8e80941Smrg   /* Uniform regions have a vstride=0. Because we use 2-wide rows with
2431b8e80941Smrg    * 64-bit regions it means that we cannot access components Z/W, so
2432b8e80941Smrg    * return false for any such case. Interleaved attributes will also be
2433b8e80941Smrg    * mapped to GRF registers with a vstride of 0, so apply the same
2434b8e80941Smrg    * treatment.
2435b8e80941Smrg    */
2436b8e80941Smrg   if ((is_uniform(src) ||
2437b8e80941Smrg        (stage_uses_interleaved_attributes(stage, prog_data->dispatch_mode) &&
2438b8e80941Smrg         src.file == ATTR)) &&
2439b8e80941Smrg       (brw_mask_for_swizzle(src.swizzle) & 12))
2440b8e80941Smrg      return false;
2441b8e80941Smrg
2442b8e80941Smrg   switch (src.swizzle) {
2443b8e80941Smrg   case BRW_SWIZZLE_XYZW:
2444b8e80941Smrg   case BRW_SWIZZLE_XXZZ:
2445b8e80941Smrg   case BRW_SWIZZLE_YYWW:
2446b8e80941Smrg   case BRW_SWIZZLE_YXWZ:
2447b8e80941Smrg      return true;
2448b8e80941Smrg   default:
2449b8e80941Smrg      return devinfo->gen == 7 && is_gen7_supported_64bit_swizzle(inst, arg);
2450b8e80941Smrg   }
2451b8e80941Smrg}
2452b8e80941Smrg
2453b8e80941Smrgbool
2454b8e80941Smrgvec4_visitor::scalarize_df()
2455b8e80941Smrg{
2456b8e80941Smrg   bool progress = false;
2457b8e80941Smrg
2458b8e80941Smrg   foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
2459b8e80941Smrg      /* Skip DF instructions that operate in Align1 mode */
2460b8e80941Smrg      if (is_align1_df(inst))
2461b8e80941Smrg         continue;
2462b8e80941Smrg
2463b8e80941Smrg      /* Check if this is a double-precision instruction */
2464b8e80941Smrg      bool is_double = type_sz(inst->dst.type) == 8;
2465b8e80941Smrg      for (int arg = 0; !is_double && arg < 3; arg++) {
2466b8e80941Smrg         is_double = inst->src[arg].file != BAD_FILE &&
2467b8e80941Smrg                     type_sz(inst->src[arg].type) == 8;
2468b8e80941Smrg      }
2469b8e80941Smrg
2470b8e80941Smrg      if (!is_double)
2471b8e80941Smrg         continue;
2472b8e80941Smrg
2473b8e80941Smrg      /* Skip the lowering for specific regioning scenarios that we can
2474b8e80941Smrg       * support natively.
2475b8e80941Smrg       */
2476b8e80941Smrg      bool skip_lowering = true;
2477b8e80941Smrg
2478b8e80941Smrg      /* XY and ZW writemasks operate in 32-bit, which means that they don't
2479b8e80941Smrg       * have a native 64-bit representation and they should always be split.
2480b8e80941Smrg       */
2481b8e80941Smrg      if (inst->dst.writemask == WRITEMASK_XY ||
2482b8e80941Smrg          inst->dst.writemask == WRITEMASK_ZW) {
2483b8e80941Smrg         skip_lowering = false;
2484b8e80941Smrg      } else {
2485b8e80941Smrg         for (unsigned i = 0; i < 3; i++) {
2486b8e80941Smrg            if (inst->src[i].file == BAD_FILE || type_sz(inst->src[i].type) < 8)
2487b8e80941Smrg               continue;
2488b8e80941Smrg            skip_lowering = skip_lowering && is_supported_64bit_region(inst, i);
2489b8e80941Smrg         }
2490b8e80941Smrg      }
2491b8e80941Smrg
2492b8e80941Smrg      if (skip_lowering)
2493b8e80941Smrg         continue;
2494b8e80941Smrg
2495b8e80941Smrg      /* Generate scalar instructions for each enabled channel */
2496b8e80941Smrg      for (unsigned chan = 0; chan < 4; chan++) {
2497b8e80941Smrg         unsigned chan_mask = 1 << chan;
2498b8e80941Smrg         if (!(inst->dst.writemask & chan_mask))
2499b8e80941Smrg            continue;
2500b8e80941Smrg
2501b8e80941Smrg         vec4_instruction *scalar_inst = new(mem_ctx) vec4_instruction(*inst);
2502b8e80941Smrg
2503b8e80941Smrg         for (unsigned i = 0; i < 3; i++) {
2504b8e80941Smrg            unsigned swz = BRW_GET_SWZ(inst->src[i].swizzle, chan);
2505b8e80941Smrg            scalar_inst->src[i].swizzle = BRW_SWIZZLE4(swz, swz, swz, swz);
2506b8e80941Smrg         }
2507b8e80941Smrg
2508b8e80941Smrg         scalar_inst->dst.writemask = chan_mask;
2509b8e80941Smrg
2510b8e80941Smrg         if (inst->predicate != BRW_PREDICATE_NONE) {
2511b8e80941Smrg            scalar_inst->predicate =
2512b8e80941Smrg               scalarize_predicate(inst->predicate, chan_mask);
2513b8e80941Smrg         }
2514b8e80941Smrg
2515b8e80941Smrg         inst->insert_before(block, scalar_inst);
2516b8e80941Smrg      }
2517b8e80941Smrg
2518b8e80941Smrg      inst->remove(block);
2519b8e80941Smrg      progress = true;
2520b8e80941Smrg   }
2521b8e80941Smrg
2522b8e80941Smrg   if (progress)
2523b8e80941Smrg      invalidate_live_intervals();
2524b8e80941Smrg
2525b8e80941Smrg   return progress;
2526b8e80941Smrg}
2527b8e80941Smrg
2528b8e80941Smrgbool
2529b8e80941Smrgvec4_visitor::lower_64bit_mad_to_mul_add()
2530b8e80941Smrg{
2531b8e80941Smrg   bool progress = false;
2532b8e80941Smrg
2533b8e80941Smrg   foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
2534b8e80941Smrg      if (inst->opcode != BRW_OPCODE_MAD)
2535b8e80941Smrg         continue;
2536b8e80941Smrg
2537b8e80941Smrg      if (type_sz(inst->dst.type) != 8)
2538b8e80941Smrg         continue;
2539b8e80941Smrg
2540b8e80941Smrg      dst_reg mul_dst = dst_reg(this, glsl_type::dvec4_type);
2541b8e80941Smrg
2542b8e80941Smrg      /* Use the copy constructor so we copy all relevant instruction fields
2543b8e80941Smrg       * from the original mad into the add and mul instructions
2544b8e80941Smrg       */
2545b8e80941Smrg      vec4_instruction *mul = new(mem_ctx) vec4_instruction(*inst);
2546b8e80941Smrg      mul->opcode = BRW_OPCODE_MUL;
2547b8e80941Smrg      mul->dst = mul_dst;
2548b8e80941Smrg      mul->src[0] = inst->src[1];
2549b8e80941Smrg      mul->src[1] = inst->src[2];
2550b8e80941Smrg      mul->src[2].file = BAD_FILE;
2551b8e80941Smrg
2552b8e80941Smrg      vec4_instruction *add = new(mem_ctx) vec4_instruction(*inst);
2553b8e80941Smrg      add->opcode = BRW_OPCODE_ADD;
2554b8e80941Smrg      add->src[0] = src_reg(mul_dst);
2555b8e80941Smrg      add->src[1] = inst->src[0];
2556b8e80941Smrg      add->src[2].file = BAD_FILE;
2557b8e80941Smrg
2558b8e80941Smrg      inst->insert_before(block, mul);
2559b8e80941Smrg      inst->insert_before(block, add);
2560b8e80941Smrg      inst->remove(block);
2561b8e80941Smrg
2562b8e80941Smrg      progress = true;
2563b8e80941Smrg   }
2564b8e80941Smrg
2565b8e80941Smrg   if (progress)
2566b8e80941Smrg      invalidate_live_intervals();
2567b8e80941Smrg
2568b8e80941Smrg   return progress;
2569b8e80941Smrg}
2570b8e80941Smrg
2571b8e80941Smrg/* The align16 hardware can only do 32-bit swizzle channels, so we need to
2572b8e80941Smrg * translate the logical 64-bit swizzle channels that we use in the Vec4 IR
2573b8e80941Smrg * to 32-bit swizzle channels in hardware registers.
2574b8e80941Smrg *
2575b8e80941Smrg * @inst and @arg identify the original vec4 IR source operand we need to
2576b8e80941Smrg * translate the swizzle for and @hw_reg is the hardware register where we
2577b8e80941Smrg * will write the hardware swizzle to use.
2578b8e80941Smrg *
2579b8e80941Smrg * This pass assumes that Align16/DF instructions have been fully scalarized
2580b8e80941Smrg * previously so there is just one 64-bit swizzle channel to deal with for any
2581b8e80941Smrg * given Vec4 IR source.
2582b8e80941Smrg */
2583b8e80941Smrgvoid
2584b8e80941Smrgvec4_visitor::apply_logical_swizzle(struct brw_reg *hw_reg,
2585b8e80941Smrg                                    vec4_instruction *inst, int arg)
2586b8e80941Smrg{
2587b8e80941Smrg   src_reg reg = inst->src[arg];
2588b8e80941Smrg
2589b8e80941Smrg   if (reg.file == BAD_FILE || reg.file == BRW_IMMEDIATE_VALUE)
2590b8e80941Smrg      return;
2591b8e80941Smrg
2592b8e80941Smrg   /* If this is not a 64-bit operand or this is a scalar instruction we don't
2593b8e80941Smrg    * need to do anything about the swizzles.
2594b8e80941Smrg    */
2595b8e80941Smrg   if(type_sz(reg.type) < 8 || is_align1_df(inst)) {
2596b8e80941Smrg      hw_reg->swizzle = reg.swizzle;
2597b8e80941Smrg      return;
2598b8e80941Smrg   }
2599b8e80941Smrg
2600b8e80941Smrg   /* Take the 64-bit logical swizzle channel and translate it to 32-bit */
2601b8e80941Smrg   assert(brw_is_single_value_swizzle(reg.swizzle) ||
2602b8e80941Smrg          is_supported_64bit_region(inst, arg));
2603b8e80941Smrg
2604b8e80941Smrg   /* Apply the region <2, 2, 1> for GRF or <0, 2, 1> for uniforms, as align16
2605b8e80941Smrg    * HW can only do 32-bit swizzle channels.
2606b8e80941Smrg    */
2607b8e80941Smrg   hw_reg->width = BRW_WIDTH_2;
2608b8e80941Smrg
2609b8e80941Smrg   if (is_supported_64bit_region(inst, arg) &&
2610b8e80941Smrg       !is_gen7_supported_64bit_swizzle(inst, arg)) {
2611b8e80941Smrg      /* Supported 64-bit swizzles are those such that their first two
2612b8e80941Smrg       * components, when expanded to 32-bit swizzles, match the semantics
2613b8e80941Smrg       * of the original 64-bit swizzle with 2-wide row regioning.
2614b8e80941Smrg       */
2615b8e80941Smrg      unsigned swizzle0 = BRW_GET_SWZ(reg.swizzle, 0);
2616b8e80941Smrg      unsigned swizzle1 = BRW_GET_SWZ(reg.swizzle, 1);
2617b8e80941Smrg      hw_reg->swizzle = BRW_SWIZZLE4(swizzle0 * 2, swizzle0 * 2 + 1,
2618b8e80941Smrg                                     swizzle1 * 2, swizzle1 * 2 + 1);
2619b8e80941Smrg   } else {
2620b8e80941Smrg      /* If we got here then we have one of the following:
2621b8e80941Smrg       *
2622b8e80941Smrg       * 1. An unsupported swizzle, which should be single-value thanks to the
2623b8e80941Smrg       *    scalarization pass.
2624b8e80941Smrg       *
2625b8e80941Smrg       * 2. A gen7 supported swizzle. These can be single-value or double-value
2626b8e80941Smrg       *    swizzles. If the latter, they are never cross-dvec2 channels. For
2627b8e80941Smrg       *    these we always need to activate the gen7 vstride=0 exploit.
2628b8e80941Smrg       */
2629b8e80941Smrg      unsigned swizzle0 = BRW_GET_SWZ(reg.swizzle, 0);
2630b8e80941Smrg      unsigned swizzle1 = BRW_GET_SWZ(reg.swizzle, 1);
2631b8e80941Smrg      assert((swizzle0 < 2) == (swizzle1 < 2));
2632b8e80941Smrg
2633b8e80941Smrg      /* To gain access to Z/W components we need to select the second half
2634b8e80941Smrg       * of the register and then use a X/Y swizzle to select Z/W respectively.
2635b8e80941Smrg       */
2636b8e80941Smrg      if (swizzle0 >= 2) {
2637b8e80941Smrg         *hw_reg = suboffset(*hw_reg, 2);
2638b8e80941Smrg         swizzle0 -= 2;
2639b8e80941Smrg         swizzle1 -= 2;
2640b8e80941Smrg      }
2641b8e80941Smrg
2642b8e80941Smrg      /* All gen7-specific supported swizzles require the vstride=0 exploit */
2643b8e80941Smrg      if (devinfo->gen == 7 && is_gen7_supported_64bit_swizzle(inst, arg))
2644b8e80941Smrg         hw_reg->vstride = BRW_VERTICAL_STRIDE_0;
2645b8e80941Smrg
2646b8e80941Smrg      /* Any 64-bit source with an offset at 16B is intended to address the
2647b8e80941Smrg       * second half of a register and needs a vertical stride of 0 so we:
2648b8e80941Smrg       *
2649b8e80941Smrg       * 1. Don't violate register region restrictions.
2650b8e80941Smrg       * 2. Activate the gen7 instruction decompresion bug exploit when
2651b8e80941Smrg       *    execsize > 4
2652b8e80941Smrg       */
2653b8e80941Smrg      if (hw_reg->subnr % REG_SIZE == 16) {
2654b8e80941Smrg         assert(devinfo->gen == 7);
2655b8e80941Smrg         hw_reg->vstride = BRW_VERTICAL_STRIDE_0;
2656b8e80941Smrg      }
2657b8e80941Smrg
2658b8e80941Smrg      hw_reg->swizzle = BRW_SWIZZLE4(swizzle0 * 2, swizzle0 * 2 + 1,
2659b8e80941Smrg                                     swizzle1 * 2, swizzle1 * 2 + 1);
2660b8e80941Smrg   }
2661b8e80941Smrg}
2662b8e80941Smrg
2663b8e80941Smrgbool
2664b8e80941Smrgvec4_visitor::run()
2665b8e80941Smrg{
2666b8e80941Smrg   if (shader_time_index >= 0)
2667b8e80941Smrg      emit_shader_time_begin();
2668b8e80941Smrg
2669b8e80941Smrg   emit_prolog();
2670b8e80941Smrg
2671b8e80941Smrg   emit_nir_code();
2672b8e80941Smrg   if (failed)
2673b8e80941Smrg      return false;
2674b8e80941Smrg   base_ir = NULL;
2675b8e80941Smrg
2676b8e80941Smrg   emit_thread_end();
2677b8e80941Smrg
2678b8e80941Smrg   calculate_cfg();
2679b8e80941Smrg
2680b8e80941Smrg   /* Before any optimization, push array accesses out to scratch
2681b8e80941Smrg    * space where we need them to be.  This pass may allocate new
2682b8e80941Smrg    * virtual GRFs, so we want to do it early.  It also makes sure
2683b8e80941Smrg    * that we have reladdr computations available for CSE, since we'll
2684b8e80941Smrg    * often do repeated subexpressions for those.
2685b8e80941Smrg    */
2686b8e80941Smrg   move_grf_array_access_to_scratch();
2687b8e80941Smrg   move_uniform_array_access_to_pull_constants();
2688b8e80941Smrg
2689b8e80941Smrg   pack_uniform_registers();
2690b8e80941Smrg   move_push_constants_to_pull_constants();
2691b8e80941Smrg   split_virtual_grfs();
2692b8e80941Smrg
2693b8e80941Smrg#define OPT(pass, args...) ({                                          \
2694b8e80941Smrg      pass_num++;                                                      \
2695b8e80941Smrg      bool this_progress = pass(args);                                 \
2696b8e80941Smrg                                                                       \
2697b8e80941Smrg      if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) {  \
2698b8e80941Smrg         char filename[64];                                            \
2699b8e80941Smrg         snprintf(filename, 64, "%s-%s-%02d-%02d-" #pass,              \
2700b8e80941Smrg                  stage_abbrev, nir->info.name, iteration, pass_num); \
2701b8e80941Smrg                                                                       \
2702b8e80941Smrg         backend_shader::dump_instructions(filename);                  \
2703b8e80941Smrg      }                                                                \
2704b8e80941Smrg                                                                       \
2705b8e80941Smrg      progress = progress || this_progress;                            \
2706b8e80941Smrg      this_progress;                                                   \
2707b8e80941Smrg   })
2708b8e80941Smrg
2709b8e80941Smrg
2710b8e80941Smrg   if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
2711b8e80941Smrg      char filename[64];
2712b8e80941Smrg      snprintf(filename, 64, "%s-%s-00-00-start",
2713b8e80941Smrg               stage_abbrev, nir->info.name);
2714b8e80941Smrg
2715b8e80941Smrg      backend_shader::dump_instructions(filename);
2716b8e80941Smrg   }
2717b8e80941Smrg
2718b8e80941Smrg   bool progress;
2719b8e80941Smrg   int iteration = 0;
2720b8e80941Smrg   int pass_num = 0;
2721b8e80941Smrg   do {
2722b8e80941Smrg      progress = false;
2723b8e80941Smrg      pass_num = 0;
2724b8e80941Smrg      iteration++;
2725b8e80941Smrg
2726b8e80941Smrg      OPT(opt_predicated_break, this);
2727b8e80941Smrg      OPT(opt_reduce_swizzle);
2728b8e80941Smrg      OPT(dead_code_eliminate);
2729b8e80941Smrg      OPT(dead_control_flow_eliminate, this);
2730b8e80941Smrg      OPT(opt_copy_propagation);
2731b8e80941Smrg      OPT(opt_cmod_propagation);
2732b8e80941Smrg      OPT(opt_cse);
2733b8e80941Smrg      OPT(opt_algebraic);
2734b8e80941Smrg      OPT(opt_register_coalesce);
2735b8e80941Smrg      OPT(eliminate_find_live_channel);
2736b8e80941Smrg   } while (progress);
2737b8e80941Smrg
2738b8e80941Smrg   pass_num = 0;
2739b8e80941Smrg
2740b8e80941Smrg   if (OPT(opt_vector_float)) {
2741b8e80941Smrg      OPT(opt_cse);
2742b8e80941Smrg      OPT(opt_copy_propagation, false);
2743b8e80941Smrg      OPT(opt_copy_propagation, true);
2744b8e80941Smrg      OPT(dead_code_eliminate);
2745b8e80941Smrg   }
2746b8e80941Smrg
2747b8e80941Smrg   if (devinfo->gen <= 5 && OPT(lower_minmax)) {
2748b8e80941Smrg      OPT(opt_cmod_propagation);
2749b8e80941Smrg      OPT(opt_cse);
2750b8e80941Smrg      OPT(opt_copy_propagation);
2751b8e80941Smrg      OPT(dead_code_eliminate);
2752b8e80941Smrg   }
2753b8e80941Smrg
2754b8e80941Smrg   if (OPT(lower_simd_width)) {
2755b8e80941Smrg      OPT(opt_copy_propagation);
2756b8e80941Smrg      OPT(dead_code_eliminate);
2757b8e80941Smrg   }
2758b8e80941Smrg
2759b8e80941Smrg   if (failed)
2760b8e80941Smrg      return false;
2761b8e80941Smrg
2762b8e80941Smrg   OPT(lower_64bit_mad_to_mul_add);
2763b8e80941Smrg
2764b8e80941Smrg   /* Run this before payload setup because tesselation shaders
2765b8e80941Smrg    * rely on it to prevent cross dvec2 regioning on DF attributes
2766b8e80941Smrg    * that are setup so that XY are on the second half of register and
2767b8e80941Smrg    * ZW are in the first half of the next.
2768b8e80941Smrg    */
2769b8e80941Smrg   OPT(scalarize_df);
2770b8e80941Smrg
2771b8e80941Smrg   setup_payload();
2772b8e80941Smrg
2773b8e80941Smrg   if (unlikely(INTEL_DEBUG & DEBUG_SPILL_VEC4)) {
2774b8e80941Smrg      /* Debug of register spilling: Go spill everything. */
2775b8e80941Smrg      const int grf_count = alloc.count;
2776b8e80941Smrg      float spill_costs[alloc.count];
2777b8e80941Smrg      bool no_spill[alloc.count];
2778b8e80941Smrg      evaluate_spill_costs(spill_costs, no_spill);
2779b8e80941Smrg      for (int i = 0; i < grf_count; i++) {
2780b8e80941Smrg         if (no_spill[i])
2781b8e80941Smrg            continue;
2782b8e80941Smrg         spill_reg(i);
2783b8e80941Smrg      }
2784b8e80941Smrg
2785b8e80941Smrg      /* We want to run this after spilling because 64-bit (un)spills need to
2786b8e80941Smrg       * emit code to shuffle 64-bit data for the 32-bit scratch read/write
2787b8e80941Smrg       * messages that can produce unsupported 64-bit swizzle regions.
2788b8e80941Smrg       */
2789b8e80941Smrg      OPT(scalarize_df);
2790b8e80941Smrg   }
2791b8e80941Smrg
2792b8e80941Smrg   fixup_3src_null_dest();
2793b8e80941Smrg
2794b8e80941Smrg   bool allocated_without_spills = reg_allocate();
2795b8e80941Smrg
2796b8e80941Smrg   if (!allocated_without_spills) {
2797b8e80941Smrg      compiler->shader_perf_log(log_data,
2798b8e80941Smrg                                "%s shader triggered register spilling.  "
2799b8e80941Smrg                                "Try reducing the number of live vec4 values "
2800b8e80941Smrg                                "to improve performance.\n",
2801b8e80941Smrg                                stage_name);
2802b8e80941Smrg
2803b8e80941Smrg      while (!reg_allocate()) {
2804b8e80941Smrg         if (failed)
2805b8e80941Smrg            return false;
2806b8e80941Smrg      }
2807b8e80941Smrg
2808b8e80941Smrg      /* We want to run this after spilling because 64-bit (un)spills need to
2809b8e80941Smrg       * emit code to shuffle 64-bit data for the 32-bit scratch read/write
2810b8e80941Smrg       * messages that can produce unsupported 64-bit swizzle regions.
2811b8e80941Smrg       */
2812b8e80941Smrg      OPT(scalarize_df);
2813b8e80941Smrg   }
2814b8e80941Smrg
2815b8e80941Smrg   opt_schedule_instructions();
2816b8e80941Smrg
2817b8e80941Smrg   opt_set_dependency_control();
2818b8e80941Smrg
2819b8e80941Smrg   convert_to_hw_regs();
2820b8e80941Smrg
2821b8e80941Smrg   if (last_scratch > 0) {
2822b8e80941Smrg      prog_data->base.total_scratch =
2823b8e80941Smrg         brw_get_scratch_size(last_scratch * REG_SIZE);
2824b8e80941Smrg   }
2825b8e80941Smrg
2826b8e80941Smrg   return !failed;
2827b8e80941Smrg}
2828b8e80941Smrg
2829b8e80941Smrg} /* namespace brw */
2830b8e80941Smrg
2831b8e80941Smrgextern "C" {
2832b8e80941Smrg
2833b8e80941Smrg/**
2834b8e80941Smrg * Compile a vertex shader.
2835b8e80941Smrg *
2836b8e80941Smrg * Returns the final assembly and the program's size.
2837b8e80941Smrg */
2838b8e80941Smrgconst unsigned *
2839b8e80941Smrgbrw_compile_vs(const struct brw_compiler *compiler, void *log_data,
2840b8e80941Smrg               void *mem_ctx,
2841b8e80941Smrg               const struct brw_vs_prog_key *key,
2842b8e80941Smrg               struct brw_vs_prog_data *prog_data,
2843b8e80941Smrg               nir_shader *shader,
2844b8e80941Smrg               int shader_time_index,
2845b8e80941Smrg               char **error_str)
2846b8e80941Smrg{
2847b8e80941Smrg   const bool is_scalar = compiler->scalar_stage[MESA_SHADER_VERTEX];
2848b8e80941Smrg   shader = brw_nir_apply_sampler_key(shader, compiler, &key->tex, is_scalar);
2849b8e80941Smrg
2850b8e80941Smrg   const unsigned *assembly = NULL;
2851b8e80941Smrg
2852b8e80941Smrg   if (prog_data->base.vue_map.varying_to_slot[VARYING_SLOT_EDGE] != -1) {
2853b8e80941Smrg      /* If the output VUE map contains VARYING_SLOT_EDGE then we need to copy
2854b8e80941Smrg       * the edge flag from VERT_ATTRIB_EDGEFLAG.  This will be done
2855b8e80941Smrg       * automatically by brw_vec4_visitor::emit_urb_slot but we need to
2856b8e80941Smrg       * ensure that prog_data->inputs_read is accurate.
2857b8e80941Smrg       *
2858b8e80941Smrg       * In order to make late NIR passes aware of the change, we actually
2859b8e80941Smrg       * whack shader->info.inputs_read instead.  This is safe because we just
2860b8e80941Smrg       * made a copy of the shader.
2861b8e80941Smrg       */
2862b8e80941Smrg      assert(!is_scalar);
2863b8e80941Smrg      assert(key->copy_edgeflag);
2864b8e80941Smrg      shader->info.inputs_read |= VERT_BIT_EDGEFLAG;
2865b8e80941Smrg   }
2866b8e80941Smrg
2867b8e80941Smrg   prog_data->inputs_read = shader->info.inputs_read;
2868b8e80941Smrg   prog_data->double_inputs_read = shader->info.vs.double_inputs;
2869b8e80941Smrg
2870b8e80941Smrg   brw_nir_lower_vs_inputs(shader, key->gl_attrib_wa_flags);
2871b8e80941Smrg   brw_nir_lower_vue_outputs(shader);
2872b8e80941Smrg   shader = brw_postprocess_nir(shader, compiler, is_scalar);
2873b8e80941Smrg
2874b8e80941Smrg   prog_data->base.clip_distance_mask =
2875b8e80941Smrg      ((1 << shader->info.clip_distance_array_size) - 1);
2876b8e80941Smrg   prog_data->base.cull_distance_mask =
2877b8e80941Smrg      ((1 << shader->info.cull_distance_array_size) - 1) <<
2878b8e80941Smrg      shader->info.clip_distance_array_size;
2879b8e80941Smrg
2880b8e80941Smrg   unsigned nr_attribute_slots = util_bitcount64(prog_data->inputs_read);
2881b8e80941Smrg
2882b8e80941Smrg   /* gl_VertexID and gl_InstanceID are system values, but arrive via an
2883b8e80941Smrg    * incoming vertex attribute.  So, add an extra slot.
2884b8e80941Smrg    */
2885b8e80941Smrg   if (shader->info.system_values_read &
2886b8e80941Smrg       (BITFIELD64_BIT(SYSTEM_VALUE_FIRST_VERTEX) |
2887b8e80941Smrg        BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
2888b8e80941Smrg        BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
2889b8e80941Smrg        BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID))) {
2890b8e80941Smrg      nr_attribute_slots++;
2891b8e80941Smrg   }
2892b8e80941Smrg
2893b8e80941Smrg   /* gl_DrawID and IsIndexedDraw share its very own vec4 */
2894b8e80941Smrg   if (shader->info.system_values_read &
2895b8e80941Smrg       (BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID) |
2896b8e80941Smrg        BITFIELD64_BIT(SYSTEM_VALUE_IS_INDEXED_DRAW))) {
2897b8e80941Smrg      nr_attribute_slots++;
2898b8e80941Smrg   }
2899b8e80941Smrg
2900b8e80941Smrg   if (shader->info.system_values_read &
2901b8e80941Smrg       BITFIELD64_BIT(SYSTEM_VALUE_IS_INDEXED_DRAW))
2902b8e80941Smrg      prog_data->uses_is_indexed_draw = true;
2903b8e80941Smrg
2904b8e80941Smrg   if (shader->info.system_values_read &
2905b8e80941Smrg       BITFIELD64_BIT(SYSTEM_VALUE_FIRST_VERTEX))
2906b8e80941Smrg      prog_data->uses_firstvertex = true;
2907b8e80941Smrg
2908b8e80941Smrg   if (shader->info.system_values_read &
2909b8e80941Smrg       BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE))
2910b8e80941Smrg      prog_data->uses_baseinstance = true;
2911b8e80941Smrg
2912b8e80941Smrg   if (shader->info.system_values_read &
2913b8e80941Smrg       BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE))
2914b8e80941Smrg      prog_data->uses_vertexid = true;
2915b8e80941Smrg
2916b8e80941Smrg   if (shader->info.system_values_read &
2917b8e80941Smrg       BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID))
2918b8e80941Smrg      prog_data->uses_instanceid = true;
2919b8e80941Smrg
2920b8e80941Smrg   if (shader->info.system_values_read &
2921b8e80941Smrg       BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID))
2922b8e80941Smrg          prog_data->uses_drawid = true;
2923b8e80941Smrg
2924b8e80941Smrg   /* The 3DSTATE_VS documentation lists the lower bound on "Vertex URB Entry
2925b8e80941Smrg    * Read Length" as 1 in vec4 mode, and 0 in SIMD8 mode.  Empirically, in
2926b8e80941Smrg    * vec4 mode, the hardware appears to wedge unless we read something.
2927b8e80941Smrg    */
2928b8e80941Smrg   if (is_scalar)
2929b8e80941Smrg      prog_data->base.urb_read_length =
2930b8e80941Smrg         DIV_ROUND_UP(nr_attribute_slots, 2);
2931b8e80941Smrg   else
2932b8e80941Smrg      prog_data->base.urb_read_length =
2933b8e80941Smrg         DIV_ROUND_UP(MAX2(nr_attribute_slots, 1), 2);
2934b8e80941Smrg
2935b8e80941Smrg   prog_data->nr_attribute_slots = nr_attribute_slots;
2936b8e80941Smrg
2937b8e80941Smrg   /* Since vertex shaders reuse the same VUE entry for inputs and outputs
2938b8e80941Smrg    * (overwriting the original contents), we need to make sure the size is
2939b8e80941Smrg    * the larger of the two.
2940b8e80941Smrg    */
2941b8e80941Smrg   const unsigned vue_entries =
2942b8e80941Smrg      MAX2(nr_attribute_slots, (unsigned)prog_data->base.vue_map.num_slots);
2943b8e80941Smrg
2944b8e80941Smrg   if (compiler->devinfo->gen == 6) {
2945b8e80941Smrg      prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 8);
2946b8e80941Smrg   } else {
2947b8e80941Smrg      prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 4);
2948b8e80941Smrg      /* On Cannonlake software shall not program an allocation size that
2949b8e80941Smrg       * specifies a size that is a multiple of 3 64B (512-bit) cachelines.
2950b8e80941Smrg       */
2951b8e80941Smrg      if (compiler->devinfo->gen == 10 &&
2952b8e80941Smrg          prog_data->base.urb_entry_size % 3 == 0)
2953b8e80941Smrg         prog_data->base.urb_entry_size++;
2954b8e80941Smrg   }
2955b8e80941Smrg
2956b8e80941Smrg   if (INTEL_DEBUG & DEBUG_VS) {
2957b8e80941Smrg      fprintf(stderr, "VS Output ");
2958b8e80941Smrg      brw_print_vue_map(stderr, &prog_data->base.vue_map);
2959b8e80941Smrg   }
2960b8e80941Smrg
2961b8e80941Smrg   if (is_scalar) {
2962b8e80941Smrg      prog_data->base.dispatch_mode = DISPATCH_MODE_SIMD8;
2963b8e80941Smrg
2964b8e80941Smrg      fs_visitor v(compiler, log_data, mem_ctx, key, &prog_data->base.base,
2965b8e80941Smrg                   NULL, /* prog; Only used for TEXTURE_RECTANGLE on gen < 8 */
2966b8e80941Smrg                   shader, 8, shader_time_index);
2967b8e80941Smrg      if (!v.run_vs()) {
2968b8e80941Smrg         if (error_str)
2969b8e80941Smrg            *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
2970b8e80941Smrg
2971b8e80941Smrg         return NULL;
2972b8e80941Smrg      }
2973b8e80941Smrg
2974b8e80941Smrg      prog_data->base.base.dispatch_grf_start_reg = v.payload.num_regs;
2975b8e80941Smrg
2976b8e80941Smrg      fs_generator g(compiler, log_data, mem_ctx,
2977b8e80941Smrg                     &prog_data->base.base, v.promoted_constants,
2978b8e80941Smrg                     v.runtime_check_aads_emit, MESA_SHADER_VERTEX);
2979b8e80941Smrg      if (INTEL_DEBUG & DEBUG_VS) {
2980b8e80941Smrg         const char *debug_name =
2981b8e80941Smrg            ralloc_asprintf(mem_ctx, "%s vertex shader %s",
2982b8e80941Smrg                            shader->info.label ? shader->info.label :
2983b8e80941Smrg                               "unnamed",
2984b8e80941Smrg                            shader->info.name);
2985b8e80941Smrg
2986b8e80941Smrg         g.enable_debug(debug_name);
2987b8e80941Smrg      }
2988b8e80941Smrg      g.generate_code(v.cfg, 8);
2989b8e80941Smrg      assembly = g.get_assembly();
2990b8e80941Smrg   }
2991b8e80941Smrg
2992b8e80941Smrg   if (!assembly) {
2993b8e80941Smrg      prog_data->base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
2994b8e80941Smrg
2995b8e80941Smrg      vec4_vs_visitor v(compiler, log_data, key, prog_data,
2996b8e80941Smrg                        shader, mem_ctx, shader_time_index);
2997b8e80941Smrg      if (!v.run()) {
2998b8e80941Smrg         if (error_str)
2999b8e80941Smrg            *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
3000b8e80941Smrg
3001b8e80941Smrg         return NULL;
3002b8e80941Smrg      }
3003b8e80941Smrg
3004b8e80941Smrg      assembly = brw_vec4_generate_assembly(compiler, log_data, mem_ctx,
3005b8e80941Smrg                                            shader, &prog_data->base, v.cfg);
3006b8e80941Smrg   }
3007b8e80941Smrg
3008b8e80941Smrg   return assembly;
3009b8e80941Smrg}
3010b8e80941Smrg
3011b8e80941Smrg} /* extern "C" */
3012