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 #pragma once
26
27 #include "elk_shader.h"
28
29 namespace elk {
30
31 class dst_reg;
32
33 class src_reg : public elk_backend_reg
34 {
35 public:
36 DECLARE_RALLOC_CXX_OPERATORS(src_reg)
37
38 void init();
39
40 src_reg(enum elk_reg_file file, int nr, const glsl_type *type);
41 src_reg();
42 src_reg(struct ::elk_reg reg);
43
44 bool equals(const src_reg &r) const;
45 bool negative_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 ®);
51
52 src_reg *reladdr;
53 };
54
55 static inline src_reg
retype(src_reg reg,enum elk_reg_type type)56 retype(src_reg reg, enum elk_reg_type type)
57 {
58 reg.type = type;
59 return reg;
60 }
61
62 namespace detail {
63
64 static inline void
add_byte_offset(elk_backend_reg * reg,unsigned bytes)65 add_byte_offset(elk_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 } /* namespace detail */
97
98 static inline src_reg
byte_offset(src_reg reg,unsigned bytes)99 byte_offset(src_reg reg, unsigned bytes)
100 {
101 detail::add_byte_offset(®, bytes);
102 return reg;
103 }
104
105 static inline src_reg
offset(src_reg reg,unsigned width,unsigned delta)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
horiz_offset(src_reg reg,unsigned delta)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 elk_swizzle().
122 */
123 static inline src_reg
swizzle(src_reg reg,unsigned swizzle)124 swizzle(src_reg reg, unsigned swizzle)
125 {
126 if (reg.file == IMM)
127 reg.ud = elk_swizzle_immediate(reg.type, reg.ud, swizzle);
128 else
129 reg.swizzle = elk_compose_swizzle(swizzle, reg.swizzle);
130
131 return reg;
132 }
133
134 static inline src_reg
negate(src_reg 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
is_uniform(const src_reg & reg)143 is_uniform(const src_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 elk_backend_reg
150 {
151 public:
152 DECLARE_RALLOC_CXX_OPERATORS(dst_reg)
153
154 void init();
155
156 dst_reg();
157 dst_reg(enum elk_reg_file file, int nr);
158 dst_reg(enum elk_reg_file file, int nr, const glsl_type *type,
159 unsigned writemask);
160 dst_reg(enum elk_reg_file file, int nr, elk_reg_type type,
161 unsigned writemask);
162 dst_reg(struct ::elk_reg reg);
163 dst_reg(class vec4_visitor *v, const struct glsl_type *type);
164
165 explicit dst_reg(const src_reg ®);
166
167 bool equals(const dst_reg &r) const;
168
169 src_reg *reladdr;
170 };
171
172 static inline dst_reg
retype(dst_reg reg,enum elk_reg_type type)173 retype(dst_reg reg, enum elk_reg_type type)
174 {
175 reg.type = type;
176 return reg;
177 }
178
179 static inline dst_reg
byte_offset(dst_reg reg,unsigned bytes)180 byte_offset(dst_reg reg, unsigned bytes)
181 {
182 detail::add_byte_offset(®, bytes);
183 return reg;
184 }
185
186 static inline dst_reg
offset(dst_reg reg,unsigned width,unsigned delta)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
horiz_offset(const dst_reg & reg,unsigned delta)195 horiz_offset(const dst_reg ®, unsigned delta)
196 {
197 if (is_uniform(src_reg(reg)))
198 return reg;
199 else
200 return byte_offset(reg, delta * type_sz(reg.type));
201 }
202
203 static inline dst_reg
writemask(dst_reg reg,unsigned mask)204 writemask(dst_reg reg, unsigned mask)
205 {
206 assert(reg.file != IMM);
207 assert((reg.writemask & mask) != 0);
208 reg.writemask &= mask;
209 return reg;
210 }
211
212 /**
213 * Return an integer identifying the discrete address space a register is
214 * contained in. A register is by definition fully contained in the single
215 * reg_space it belongs to, so two registers with different reg_space ids are
216 * guaranteed not to overlap. Most register files are a single reg_space of
217 * its own, only the VGRF file is composed of multiple discrete address
218 * spaces, one for each VGRF allocation.
219 */
220 static inline uint32_t
reg_space(const elk_backend_reg & r)221 reg_space(const elk_backend_reg &r)
222 {
223 return r.file << 16 | (r.file == VGRF ? r.nr : 0);
224 }
225
226 /**
227 * Return the base offset in bytes of a register relative to the start of its
228 * reg_space().
229 */
230 static inline unsigned
reg_offset(const elk_backend_reg & r)231 reg_offset(const elk_backend_reg &r)
232 {
233 return (r.file == VGRF || r.file == IMM ? 0 : r.nr) *
234 (r.file == UNIFORM ? 16 : REG_SIZE) + r.offset +
235 (r.file == ARF || r.file == FIXED_GRF ? r.subnr : 0);
236 }
237
238 /**
239 * Return whether the register region starting at \p r and spanning \p dr
240 * bytes could potentially overlap the register region starting at \p s and
241 * spanning \p ds bytes.
242 */
243 static inline bool
regions_overlap(const elk_backend_reg & r,unsigned dr,const elk_backend_reg & s,unsigned ds)244 regions_overlap(const elk_backend_reg &r, unsigned dr,
245 const elk_backend_reg &s, unsigned ds)
246 {
247 if (r.file == MRF && (r.nr & ELK_MRF_COMPR4)) {
248 /* COMPR4 regions are translated by the hardware during decompression
249 * into two separate half-regions 4 MRFs apart from each other.
250 */
251 elk_backend_reg t0 = r;
252 t0.nr &= ~ELK_MRF_COMPR4;
253 elk_backend_reg t1 = t0;
254 t1.offset += 4 * REG_SIZE;
255 return regions_overlap(t0, dr / 2, s, ds) ||
256 regions_overlap(t1, dr / 2, s, ds);
257
258 } else if (s.file == MRF && (s.nr & ELK_MRF_COMPR4)) {
259 return regions_overlap(s, ds, r, dr);
260
261 } else {
262 return reg_space(r) == reg_space(s) &&
263 !(reg_offset(r) + dr <= reg_offset(s) ||
264 reg_offset(s) + ds <= reg_offset(r));
265 }
266 }
267
268 class vec4_instruction : public elk_backend_instruction {
269 public:
270 DECLARE_RALLOC_CXX_OPERATORS(vec4_instruction)
271
272 vec4_instruction(enum elk_opcode opcode,
273 const dst_reg &dst = dst_reg(),
274 const src_reg &src0 = src_reg(),
275 const src_reg &src1 = src_reg(),
276 const src_reg &src2 = src_reg());
277
278 dst_reg dst;
279 src_reg src[3];
280
281 enum elk_urb_write_flags urb_write_flags;
282
283 unsigned sol_binding; /**< gfx6: SOL binding table index */
284 bool sol_final_write; /**< gfx6: send commit message */
285 unsigned sol_vertex; /**< gfx6: used for setting dst index in SVB header */
286
287 bool is_send_from_grf() const;
288 unsigned size_read(unsigned arg) const;
289 bool can_reswizzle(const struct intel_device_info *devinfo,
290 int dst_writemask,
291 int swizzle, int swizzle_mask);
292 void reswizzle(int dst_writemask, int swizzle);
293 bool can_do_source_mods(const struct intel_device_info *devinfo);
294 bool can_do_cmod();
295 bool can_do_writemask(const struct intel_device_info *devinfo);
296 bool can_change_types() const;
297 bool has_source_and_destination_hazard() const;
298 unsigned implied_mrf_writes() const;
299
is_align1_partial_write()300 bool is_align1_partial_write()
301 {
302 return opcode == ELK_VEC4_OPCODE_SET_LOW_32BIT ||
303 opcode == ELK_VEC4_OPCODE_SET_HIGH_32BIT;
304 }
305
reads_flag()306 bool reads_flag() const
307 {
308 return predicate || opcode == ELK_VS_OPCODE_UNPACK_FLAGS_SIMD4X2;
309 }
310
reads_flag(unsigned c)311 bool reads_flag(unsigned c)
312 {
313 if (opcode == ELK_VS_OPCODE_UNPACK_FLAGS_SIMD4X2)
314 return true;
315
316 switch (predicate) {
317 case ELK_PREDICATE_NONE:
318 return false;
319 case ELK_PREDICATE_ALIGN16_REPLICATE_X:
320 return c == 0;
321 case ELK_PREDICATE_ALIGN16_REPLICATE_Y:
322 return c == 1;
323 case ELK_PREDICATE_ALIGN16_REPLICATE_Z:
324 return c == 2;
325 case ELK_PREDICATE_ALIGN16_REPLICATE_W:
326 return c == 3;
327 default:
328 return true;
329 }
330 }
331
writes_flag(const intel_device_info * devinfo)332 bool writes_flag(const intel_device_info *devinfo) const
333 {
334 return (conditional_mod && ((opcode != ELK_OPCODE_SEL || devinfo->ver <= 5) &&
335 opcode != ELK_OPCODE_CSEL &&
336 opcode != ELK_OPCODE_IF &&
337 opcode != ELK_OPCODE_WHILE));
338 }
339
reads_g0_implicitly()340 bool reads_g0_implicitly() const
341 {
342 switch (opcode) {
343 case ELK_SHADER_OPCODE_TEX:
344 case ELK_SHADER_OPCODE_TXL:
345 case ELK_SHADER_OPCODE_TXD:
346 case ELK_SHADER_OPCODE_TXF:
347 case ELK_SHADER_OPCODE_TXF_CMS_W:
348 case ELK_SHADER_OPCODE_TXF_CMS:
349 case ELK_SHADER_OPCODE_TXF_MCS:
350 case ELK_SHADER_OPCODE_TXS:
351 case ELK_SHADER_OPCODE_TG4:
352 case ELK_SHADER_OPCODE_TG4_OFFSET:
353 case ELK_SHADER_OPCODE_SAMPLEINFO:
354 case ELK_VS_OPCODE_PULL_CONSTANT_LOAD:
355 case ELK_GS_OPCODE_SET_PRIMITIVE_ID:
356 case ELK_GS_OPCODE_GET_INSTANCE_ID:
357 case ELK_SHADER_OPCODE_GFX4_SCRATCH_READ:
358 case ELK_SHADER_OPCODE_GFX4_SCRATCH_WRITE:
359 return true;
360 default:
361 return false;
362 }
363 }
364 };
365
366 /**
367 * Make the execution of \p inst dependent on the evaluation of a possibly
368 * inverted predicate.
369 */
370 inline vec4_instruction *
set_predicate_inv(enum elk_predicate pred,bool inverse,vec4_instruction * inst)371 set_predicate_inv(enum elk_predicate pred, bool inverse,
372 vec4_instruction *inst)
373 {
374 inst->predicate = pred;
375 inst->predicate_inverse = inverse;
376 return inst;
377 }
378
379 /**
380 * Make the execution of \p inst dependent on the evaluation of a predicate.
381 */
382 inline vec4_instruction *
set_predicate(enum elk_predicate pred,vec4_instruction * inst)383 set_predicate(enum elk_predicate pred, vec4_instruction *inst)
384 {
385 return set_predicate_inv(pred, false, inst);
386 }
387
388 /**
389 * Write the result of evaluating the condition given by \p mod to a flag
390 * register.
391 */
392 inline vec4_instruction *
set_condmod(enum elk_conditional_mod mod,vec4_instruction * inst)393 set_condmod(enum elk_conditional_mod mod, vec4_instruction *inst)
394 {
395 inst->conditional_mod = mod;
396 return inst;
397 }
398
399 /**
400 * Clamp the result of \p inst to the saturation range of its destination
401 * datatype.
402 */
403 inline vec4_instruction *
set_saturate(bool saturate,vec4_instruction * inst)404 set_saturate(bool saturate, vec4_instruction *inst)
405 {
406 inst->saturate = saturate;
407 return inst;
408 }
409
410 /**
411 * Return the number of dataflow registers written by the instruction (either
412 * fully or partially) counted from 'floor(reg_offset(inst->dst) /
413 * register_size)'. The somewhat arbitrary register size unit is 16B for the
414 * UNIFORM and IMM files and 32B for all other files.
415 */
416 inline unsigned
regs_written(const vec4_instruction * inst)417 regs_written(const vec4_instruction *inst)
418 {
419 assert(inst->dst.file != UNIFORM && inst->dst.file != IMM);
420 return DIV_ROUND_UP(reg_offset(inst->dst) % REG_SIZE + inst->size_written,
421 REG_SIZE);
422 }
423
424 /**
425 * Return the number of dataflow registers read by the instruction (either
426 * fully or partially) counted from 'floor(reg_offset(inst->src[i]) /
427 * register_size)'. The somewhat arbitrary register size unit is 16B for the
428 * UNIFORM and IMM files and 32B for all other files.
429 */
430 inline unsigned
regs_read(const vec4_instruction * inst,unsigned i)431 regs_read(const vec4_instruction *inst, unsigned i)
432 {
433 const unsigned reg_size =
434 inst->src[i].file == UNIFORM || inst->src[i].file == IMM ? 16 : REG_SIZE;
435 return DIV_ROUND_UP(reg_offset(inst->src[i]) % reg_size + inst->size_read(i),
436 reg_size);
437 }
438
439 static inline enum elk_reg_type
get_exec_type(const vec4_instruction * inst)440 get_exec_type(const vec4_instruction *inst)
441 {
442 enum elk_reg_type exec_type = ELK_REGISTER_TYPE_B;
443
444 for (int i = 0; i < 3; i++) {
445 if (inst->src[i].file != BAD_FILE) {
446 const elk_reg_type t = get_exec_type(elk_reg_type(inst->src[i].type));
447 if (type_sz(t) > type_sz(exec_type))
448 exec_type = t;
449 else if (type_sz(t) == type_sz(exec_type) &&
450 elk_reg_type_is_floating_point(t))
451 exec_type = t;
452 }
453 }
454
455 if (exec_type == ELK_REGISTER_TYPE_B)
456 exec_type = inst->dst.type;
457
458 /* TODO: We need to handle half-float conversions. */
459 assert(exec_type != ELK_REGISTER_TYPE_HF ||
460 inst->dst.type == ELK_REGISTER_TYPE_HF);
461 assert(exec_type != ELK_REGISTER_TYPE_B);
462
463 return exec_type;
464 }
465
466 static inline unsigned
get_exec_type_size(const vec4_instruction * inst)467 get_exec_type_size(const vec4_instruction *inst)
468 {
469 return type_sz(get_exec_type(inst));
470 }
471
472 } /* namespace elk */
473