i965: split EU defines to brw_eu_defines.h
[mesa.git] / src / mesa / drivers / dri / i965 / brw_ir_vec4.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2011-2015 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #ifndef BRW_IR_VEC4_H
26 #define BRW_IR_VEC4_H
27
28 #include "brw_shader.h"
29
30 namespace brw {
31
32 class dst_reg;
33
34 class src_reg : public backend_reg
35 {
36 public:
37 DECLARE_RALLOC_CXX_OPERATORS(src_reg)
38
39 void init();
40
41 src_reg(enum brw_reg_file file, int nr, const glsl_type *type);
42 src_reg();
43 src_reg(struct ::brw_reg reg);
44
45 bool equals(const src_reg &r) const;
46
47 src_reg(class vec4_visitor *v, const struct glsl_type *type);
48 src_reg(class vec4_visitor *v, const struct glsl_type *type, int size);
49
50 explicit src_reg(const dst_reg &reg);
51
52 src_reg *reladdr;
53 };
54
55 static inline src_reg
56 retype(src_reg reg, enum brw_reg_type type)
57 {
58 reg.type = type;
59 return reg;
60 }
61
62 namespace detail {
63
64 static inline void
65 add_byte_offset(backend_reg *reg, unsigned bytes)
66 {
67 switch (reg->file) {
68 case BAD_FILE:
69 break;
70 case VGRF:
71 case ATTR:
72 case UNIFORM:
73 reg->offset += bytes;
74 assert(reg->offset % 16 == 0);
75 break;
76 case MRF: {
77 const unsigned suboffset = reg->offset + bytes;
78 reg->nr += suboffset / REG_SIZE;
79 reg->offset = suboffset % REG_SIZE;
80 assert(reg->offset % 16 == 0);
81 break;
82 }
83 case ARF:
84 case FIXED_GRF: {
85 const unsigned suboffset = reg->subnr + bytes;
86 reg->nr += suboffset / REG_SIZE;
87 reg->subnr = suboffset % REG_SIZE;
88 assert(reg->subnr % 16 == 0);
89 break;
90 }
91 default:
92 assert(bytes == 0);
93 }
94 }
95
96 } /* namepace detail */
97
98 static inline src_reg
99 byte_offset(src_reg reg, unsigned bytes)
100 {
101 detail::add_byte_offset(&reg, bytes);
102 return reg;
103 }
104
105 static inline src_reg
106 offset(src_reg reg, unsigned width, unsigned delta)
107 {
108 const unsigned stride = (reg.file == UNIFORM ? 0 : 4);
109 const unsigned num_components = MAX2(width / 4 * stride, 4);
110 return byte_offset(reg, num_components * type_sz(reg.type) * delta);
111 }
112
113 static inline src_reg
114 horiz_offset(src_reg reg, unsigned delta)
115 {
116 return byte_offset(reg, delta * type_sz(reg.type));
117 }
118
119 /**
120 * Reswizzle a given source register.
121 * \sa brw_swizzle().
122 */
123 static inline src_reg
124 swizzle(src_reg reg, unsigned swizzle)
125 {
126 if (reg.file == IMM)
127 reg.ud = brw_swizzle_immediate(reg.type, reg.ud, swizzle);
128 else
129 reg.swizzle = brw_compose_swizzle(swizzle, reg.swizzle);
130
131 return reg;
132 }
133
134 static inline src_reg
135 negate(src_reg reg)
136 {
137 assert(reg.file != IMM);
138 reg.negate = !reg.negate;
139 return reg;
140 }
141
142 static inline bool
143 is_uniform(const src_reg &reg)
144 {
145 return (reg.file == IMM || reg.file == UNIFORM || reg.is_null()) &&
146 (!reg.reladdr || is_uniform(*reg.reladdr));
147 }
148
149 class dst_reg : public backend_reg
150 {
151 public:
152 DECLARE_RALLOC_CXX_OPERATORS(dst_reg)
153
154 void init();
155
156 dst_reg();
157 dst_reg(enum brw_reg_file file, int nr);
158 dst_reg(enum brw_reg_file file, int nr, const glsl_type *type,
159 unsigned writemask);
160 dst_reg(enum brw_reg_file file, int nr, brw_reg_type type,
161 unsigned writemask);
162 dst_reg(struct ::brw_reg reg);
163 dst_reg(class vec4_visitor *v, const struct glsl_type *type);
164
165 explicit dst_reg(const src_reg &reg);
166
167 bool equals(const dst_reg &r) const;
168
169 src_reg *reladdr;
170 };
171
172 static inline dst_reg
173 retype(dst_reg reg, enum brw_reg_type type)
174 {
175 reg.type = type;
176 return reg;
177 }
178
179 static inline dst_reg
180 byte_offset(dst_reg reg, unsigned bytes)
181 {
182 detail::add_byte_offset(&reg, bytes);
183 return reg;
184 }
185
186 static inline dst_reg
187 offset(dst_reg reg, unsigned width, unsigned delta)
188 {
189 const unsigned stride = (reg.file == UNIFORM ? 0 : 4);
190 const unsigned num_components = MAX2(width / 4 * stride, 4);
191 return byte_offset(reg, num_components * type_sz(reg.type) * delta);
192 }
193
194 static inline dst_reg
195 horiz_offset(dst_reg reg, unsigned delta)
196 {
197 return byte_offset(reg, delta * type_sz(reg.type));
198 }
199
200 static inline dst_reg
201 writemask(dst_reg reg, unsigned mask)
202 {
203 assert(reg.file != IMM);
204 assert((reg.writemask & mask) != 0);
205 reg.writemask &= mask;
206 return reg;
207 }
208
209 /**
210 * Return an integer identifying the discrete address space a register is
211 * contained in. A register is by definition fully contained in the single
212 * reg_space it belongs to, so two registers with different reg_space ids are
213 * guaranteed not to overlap. Most register files are a single reg_space of
214 * its own, only the VGRF file is composed of multiple discrete address
215 * spaces, one for each VGRF allocation.
216 */
217 static inline uint32_t
218 reg_space(const backend_reg &r)
219 {
220 return r.file << 16 | (r.file == VGRF ? r.nr : 0);
221 }
222
223 /**
224 * Return the base offset in bytes of a register relative to the start of its
225 * reg_space().
226 */
227 static inline unsigned
228 reg_offset(const backend_reg &r)
229 {
230 return (r.file == VGRF || r.file == IMM ? 0 : r.nr) *
231 (r.file == UNIFORM ? 16 : REG_SIZE) + r.offset +
232 (r.file == ARF || r.file == FIXED_GRF ? r.subnr : 0);
233 }
234
235 /**
236 * Return whether the register region starting at \p r and spanning \p dr
237 * bytes could potentially overlap the register region starting at \p s and
238 * spanning \p ds bytes.
239 */
240 static inline bool
241 regions_overlap(const backend_reg &r, unsigned dr,
242 const backend_reg &s, unsigned ds)
243 {
244 if (r.file == MRF && (r.nr & BRW_MRF_COMPR4)) {
245 /* COMPR4 regions are translated by the hardware during decompression
246 * into two separate half-regions 4 MRFs apart from each other.
247 */
248 backend_reg t0 = r;
249 t0.nr &= ~BRW_MRF_COMPR4;
250 backend_reg t1 = t0;
251 t1.offset += 4 * REG_SIZE;
252 return regions_overlap(t0, dr / 2, s, ds) ||
253 regions_overlap(t1, dr / 2, s, ds);
254
255 } else if (s.file == MRF && (s.nr & BRW_MRF_COMPR4)) {
256 return regions_overlap(s, ds, r, dr);
257
258 } else {
259 return reg_space(r) == reg_space(s) &&
260 !(reg_offset(r) + dr <= reg_offset(s) ||
261 reg_offset(s) + ds <= reg_offset(r));
262 }
263 }
264
265 class vec4_instruction : public backend_instruction {
266 public:
267 DECLARE_RALLOC_CXX_OPERATORS(vec4_instruction)
268
269 vec4_instruction(enum opcode opcode,
270 const dst_reg &dst = dst_reg(),
271 const src_reg &src0 = src_reg(),
272 const src_reg &src1 = src_reg(),
273 const src_reg &src2 = src_reg());
274
275 dst_reg dst;
276 src_reg src[3];
277
278 enum brw_urb_write_flags urb_write_flags;
279
280 unsigned sol_binding; /**< gen6: SOL binding table index */
281 bool sol_final_write; /**< gen6: send commit message */
282 unsigned sol_vertex; /**< gen6: used for setting dst index in SVB header */
283
284 bool is_send_from_grf();
285 unsigned size_read(unsigned arg) const;
286 bool can_reswizzle(const struct gen_device_info *devinfo, int dst_writemask,
287 int swizzle, int swizzle_mask);
288 void reswizzle(int dst_writemask, int swizzle);
289 bool can_do_source_mods(const struct gen_device_info *devinfo);
290 bool can_do_writemask(const struct gen_device_info *devinfo);
291 bool can_change_types() const;
292 bool has_source_and_destination_hazard() const;
293
294 bool is_align1_partial_write()
295 {
296 return opcode == VEC4_OPCODE_SET_LOW_32BIT ||
297 opcode == VEC4_OPCODE_SET_HIGH_32BIT;
298 }
299
300 bool reads_flag()
301 {
302 return predicate || opcode == VS_OPCODE_UNPACK_FLAGS_SIMD4X2;
303 }
304
305 bool reads_flag(unsigned c)
306 {
307 if (opcode == VS_OPCODE_UNPACK_FLAGS_SIMD4X2)
308 return true;
309
310 switch (predicate) {
311 case BRW_PREDICATE_NONE:
312 return false;
313 case BRW_PREDICATE_ALIGN16_REPLICATE_X:
314 return c == 0;
315 case BRW_PREDICATE_ALIGN16_REPLICATE_Y:
316 return c == 1;
317 case BRW_PREDICATE_ALIGN16_REPLICATE_Z:
318 return c == 2;
319 case BRW_PREDICATE_ALIGN16_REPLICATE_W:
320 return c == 3;
321 default:
322 return true;
323 }
324 }
325
326 bool writes_flag()
327 {
328 return (conditional_mod && (opcode != BRW_OPCODE_SEL &&
329 opcode != BRW_OPCODE_IF &&
330 opcode != BRW_OPCODE_WHILE));
331 }
332 };
333
334 /**
335 * Make the execution of \p inst dependent on the evaluation of a possibly
336 * inverted predicate.
337 */
338 inline vec4_instruction *
339 set_predicate_inv(enum brw_predicate pred, bool inverse,
340 vec4_instruction *inst)
341 {
342 inst->predicate = pred;
343 inst->predicate_inverse = inverse;
344 return inst;
345 }
346
347 /**
348 * Make the execution of \p inst dependent on the evaluation of a predicate.
349 */
350 inline vec4_instruction *
351 set_predicate(enum brw_predicate pred, vec4_instruction *inst)
352 {
353 return set_predicate_inv(pred, false, inst);
354 }
355
356 /**
357 * Write the result of evaluating the condition given by \p mod to a flag
358 * register.
359 */
360 inline vec4_instruction *
361 set_condmod(enum brw_conditional_mod mod, vec4_instruction *inst)
362 {
363 inst->conditional_mod = mod;
364 return inst;
365 }
366
367 /**
368 * Clamp the result of \p inst to the saturation range of its destination
369 * datatype.
370 */
371 inline vec4_instruction *
372 set_saturate(bool saturate, vec4_instruction *inst)
373 {
374 inst->saturate = saturate;
375 return inst;
376 }
377
378 /**
379 * Return the number of dataflow registers written by the instruction (either
380 * fully or partially) counted from 'floor(reg_offset(inst->dst) /
381 * register_size)'. The somewhat arbitrary register size unit is 16B for the
382 * UNIFORM and IMM files and 32B for all other files.
383 */
384 inline unsigned
385 regs_written(const vec4_instruction *inst)
386 {
387 assert(inst->dst.file != UNIFORM && inst->dst.file != IMM);
388 return DIV_ROUND_UP(reg_offset(inst->dst) % REG_SIZE + inst->size_written,
389 REG_SIZE);
390 }
391
392 /**
393 * Return the number of dataflow registers read by the instruction (either
394 * fully or partially) counted from 'floor(reg_offset(inst->src[i]) /
395 * register_size)'. The somewhat arbitrary register size unit is 16B for the
396 * UNIFORM and IMM files and 32B for all other files.
397 */
398 inline unsigned
399 regs_read(const vec4_instruction *inst, unsigned i)
400 {
401 const unsigned reg_size =
402 inst->src[i].file == UNIFORM || inst->src[i].file == IMM ? 16 : REG_SIZE;
403 return DIV_ROUND_UP(reg_offset(inst->src[i]) % reg_size + inst->size_read(i),
404 reg_size);
405 }
406
407 } /* namespace brw */
408
409 #endif