brw_ir_fs.h revision 01e04c3f
1/* -*- c++ -*- */
2/*
3 * Copyright © 2010-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_FS_H
26#define BRW_IR_FS_H
27
28#include "brw_shader.h"
29
30class fs_inst;
31
32class fs_reg : public backend_reg {
33public:
34   DECLARE_RALLOC_CXX_OPERATORS(fs_reg)
35
36   void init();
37
38   fs_reg();
39   fs_reg(struct ::brw_reg reg);
40   fs_reg(enum brw_reg_file file, int nr);
41   fs_reg(enum brw_reg_file file, int nr, enum brw_reg_type type);
42
43   bool equals(const fs_reg &r) const;
44   bool negative_equals(const fs_reg &r) const;
45   bool is_contiguous() const;
46
47   /**
48    * Return the size in bytes of a single logical component of the
49    * register assuming the given execution width.
50    */
51   unsigned component_size(unsigned width) const;
52
53   /** Register region horizontal stride */
54   uint8_t stride;
55};
56
57static inline fs_reg
58negate(fs_reg reg)
59{
60   assert(reg.file != IMM);
61   reg.negate = !reg.negate;
62   return reg;
63}
64
65static inline fs_reg
66retype(fs_reg reg, enum brw_reg_type type)
67{
68   reg.type = type;
69   return reg;
70}
71
72static inline fs_reg
73byte_offset(fs_reg reg, unsigned delta)
74{
75   switch (reg.file) {
76   case BAD_FILE:
77      break;
78   case VGRF:
79   case ATTR:
80   case UNIFORM:
81      reg.offset += delta;
82      break;
83   case MRF: {
84      const unsigned suboffset = reg.offset + delta;
85      reg.nr += suboffset / REG_SIZE;
86      reg.offset = suboffset % REG_SIZE;
87      break;
88   }
89   case ARF:
90   case FIXED_GRF: {
91      const unsigned suboffset = reg.subnr + delta;
92      reg.nr += suboffset / REG_SIZE;
93      reg.subnr = suboffset % REG_SIZE;
94      break;
95   }
96   case IMM:
97   default:
98      assert(delta == 0);
99   }
100   return reg;
101}
102
103static inline fs_reg
104horiz_offset(const fs_reg &reg, unsigned delta)
105{
106   switch (reg.file) {
107   case BAD_FILE:
108   case UNIFORM:
109   case IMM:
110      /* These only have a single component that is implicitly splatted.  A
111       * horizontal offset should be a harmless no-op.
112       * XXX - Handle vector immediates correctly.
113       */
114      return reg;
115   case VGRF:
116   case MRF:
117   case ATTR:
118      return byte_offset(reg, delta * reg.stride * type_sz(reg.type));
119   case ARF:
120   case FIXED_GRF:
121      if (reg.is_null()) {
122         return reg;
123      } else {
124         const unsigned stride = reg.hstride ? 1 << (reg.hstride - 1) : 0;
125         return byte_offset(reg, delta * stride * type_sz(reg.type));
126      }
127   }
128   unreachable("Invalid register file");
129}
130
131static inline fs_reg
132offset(fs_reg reg, unsigned width, unsigned delta)
133{
134   switch (reg.file) {
135   case BAD_FILE:
136      break;
137   case ARF:
138   case FIXED_GRF:
139   case MRF:
140   case VGRF:
141   case ATTR:
142   case UNIFORM:
143      return byte_offset(reg, delta * reg.component_size(width));
144   case IMM:
145      assert(delta == 0);
146   }
147   return reg;
148}
149
150/**
151 * Get the scalar channel of \p reg given by \p idx and replicate it to all
152 * channels of the result.
153 */
154static inline fs_reg
155component(fs_reg reg, unsigned idx)
156{
157   reg = horiz_offset(reg, idx);
158   reg.stride = 0;
159   return reg;
160}
161
162/**
163 * Return an integer identifying the discrete address space a register is
164 * contained in.  A register is by definition fully contained in the single
165 * reg_space it belongs to, so two registers with different reg_space ids are
166 * guaranteed not to overlap.  Most register files are a single reg_space of
167 * its own, only the VGRF file is composed of multiple discrete address
168 * spaces, one for each VGRF allocation.
169 */
170static inline uint32_t
171reg_space(const fs_reg &r)
172{
173   return r.file << 16 | (r.file == VGRF ? r.nr : 0);
174}
175
176/**
177 * Return the base offset in bytes of a register relative to the start of its
178 * reg_space().
179 */
180static inline unsigned
181reg_offset(const fs_reg &r)
182{
183   return (r.file == VGRF || r.file == IMM ? 0 : r.nr) *
184          (r.file == UNIFORM ? 4 : REG_SIZE) + r.offset +
185          (r.file == ARF || r.file == FIXED_GRF ? r.subnr : 0);
186}
187
188/**
189 * Return the amount of padding in bytes left unused between individual
190 * components of register \p r due to a (horizontal) stride value greater than
191 * one, or zero if components are tightly packed in the register file.
192 */
193static inline unsigned
194reg_padding(const fs_reg &r)
195{
196   const unsigned stride = ((r.file != ARF && r.file != FIXED_GRF) ? r.stride :
197                            r.hstride == 0 ? 0 :
198                            1 << (r.hstride - 1));
199   return (MAX2(1, stride) - 1) * type_sz(r.type);
200}
201
202/**
203 * Return whether the register region starting at \p r and spanning \p dr
204 * bytes could potentially overlap the register region starting at \p s and
205 * spanning \p ds bytes.
206 */
207static inline bool
208regions_overlap(const fs_reg &r, unsigned dr, const fs_reg &s, unsigned ds)
209{
210   if (r.file == MRF && (r.nr & BRW_MRF_COMPR4)) {
211      fs_reg t = r;
212      t.nr &= ~BRW_MRF_COMPR4;
213      /* COMPR4 regions are translated by the hardware during decompression
214       * into two separate half-regions 4 MRFs apart from each other.
215       */
216      return regions_overlap(t, dr / 2, s, ds) ||
217             regions_overlap(byte_offset(t, 4 * REG_SIZE), dr / 2, s, ds);
218
219   } else if (s.file == MRF && (s.nr & BRW_MRF_COMPR4)) {
220      return regions_overlap(s, ds, r, dr);
221
222   } else {
223      return reg_space(r) == reg_space(s) &&
224             !(reg_offset(r) + dr <= reg_offset(s) ||
225               reg_offset(s) + ds <= reg_offset(r));
226   }
227}
228
229/**
230 * Check that the register region given by r [r.offset, r.offset + dr[
231 * is fully contained inside the register region given by s
232 * [s.offset, s.offset + ds[.
233 */
234static inline bool
235region_contained_in(const fs_reg &r, unsigned dr, const fs_reg &s, unsigned ds)
236{
237   return reg_space(r) == reg_space(s) &&
238          reg_offset(r) >= reg_offset(s) &&
239          reg_offset(r) + dr <= reg_offset(s) + ds;
240}
241
242/**
243 * Return whether the given register region is n-periodic, i.e. whether the
244 * original region remains invariant after shifting it by \p n scalar
245 * channels.
246 */
247static inline bool
248is_periodic(const fs_reg &reg, unsigned n)
249{
250   if (reg.file == BAD_FILE || reg.is_null()) {
251      return true;
252
253   } else if (reg.file == IMM) {
254      const unsigned period = (reg.type == BRW_REGISTER_TYPE_UV ||
255                               reg.type == BRW_REGISTER_TYPE_V ? 8 :
256                               reg.type == BRW_REGISTER_TYPE_VF ? 4 :
257                               1);
258      return n % period == 0;
259
260   } else if (reg.file == ARF || reg.file == FIXED_GRF) {
261      const unsigned period = (reg.hstride == 0 && reg.vstride == 0 ? 1 :
262                               reg.vstride == 0 ? 1 << reg.width :
263                               ~0);
264      return n % period == 0;
265
266   } else {
267      return reg.stride == 0;
268   }
269}
270
271static inline bool
272is_uniform(const fs_reg &reg)
273{
274   return is_periodic(reg, 1);
275}
276
277/**
278 * Get the specified 8-component quarter of a register.
279 * XXX - Maybe come up with a less misleading name for this (e.g. quarter())?
280 */
281static inline fs_reg
282half(const fs_reg &reg, unsigned idx)
283{
284   assert(idx < 2);
285   return horiz_offset(reg, 8 * idx);
286}
287
288/**
289 * Reinterpret each channel of register \p reg as a vector of values of the
290 * given smaller type and take the i-th subcomponent from each.
291 */
292static inline fs_reg
293subscript(fs_reg reg, brw_reg_type type, unsigned i)
294{
295   assert((i + 1) * type_sz(type) <= type_sz(reg.type));
296
297   if (reg.file == ARF || reg.file == FIXED_GRF) {
298      /* The stride is encoded inconsistently for fixed GRF and ARF registers
299       * as the log2 of the actual vertical and horizontal strides.
300       */
301      const int delta = _mesa_logbase2(type_sz(reg.type)) -
302                        _mesa_logbase2(type_sz(type));
303      reg.hstride += (reg.hstride ? delta : 0);
304      reg.vstride += (reg.vstride ? delta : 0);
305
306   } else if (reg.file == IMM) {
307      assert(reg.type == type);
308
309   } else {
310      reg.stride *= type_sz(reg.type) / type_sz(type);
311   }
312
313   return byte_offset(retype(reg, type), i * type_sz(type));
314}
315
316static inline fs_reg
317horiz_stride(fs_reg reg, unsigned s)
318{
319   reg.stride *= s;
320   return reg;
321}
322
323static const fs_reg reg_undef;
324
325class fs_inst : public backend_instruction {
326   fs_inst &operator=(const fs_inst &);
327
328   void init(enum opcode opcode, uint8_t exec_width, const fs_reg &dst,
329             const fs_reg *src, unsigned sources);
330
331public:
332   DECLARE_RALLOC_CXX_OPERATORS(fs_inst)
333
334   fs_inst();
335   fs_inst(enum opcode opcode, uint8_t exec_size);
336   fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst);
337   fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
338           const fs_reg &src0);
339   fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
340           const fs_reg &src0, const fs_reg &src1);
341   fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
342           const fs_reg &src0, const fs_reg &src1, const fs_reg &src2);
343   fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
344           const fs_reg src[], unsigned sources);
345   fs_inst(const fs_inst &that);
346   ~fs_inst();
347
348   void resize_sources(uint8_t num_sources);
349
350   bool equals(fs_inst *inst) const;
351   bool is_send_from_grf() const;
352   bool is_partial_write() const;
353   bool is_copy_payload(const brw::simple_allocator &grf_alloc) const;
354   unsigned components_read(unsigned i) const;
355   unsigned size_read(int arg) const;
356   bool can_do_source_mods(const struct gen_device_info *devinfo);
357   bool can_do_cmod();
358   bool can_change_types() const;
359   bool has_source_and_destination_hazard() const;
360
361   /**
362    * Return the subset of flag registers read by the instruction as a bitset
363    * with byte granularity.
364    */
365   unsigned flags_read(const gen_device_info *devinfo) const;
366
367   /**
368    * Return the subset of flag registers updated by the instruction (either
369    * partially or fully) as a bitset with byte granularity.
370    */
371   unsigned flags_written() const;
372
373   fs_reg dst;
374   fs_reg *src;
375
376   uint8_t sources; /**< Number of fs_reg sources. */
377
378   bool last_rt:1;
379   bool pi_noperspective:1;   /**< Pixel interpolator noperspective flag */
380};
381
382/**
383 * Make the execution of \p inst dependent on the evaluation of a possibly
384 * inverted predicate.
385 */
386static inline fs_inst *
387set_predicate_inv(enum brw_predicate pred, bool inverse,
388                  fs_inst *inst)
389{
390   inst->predicate = pred;
391   inst->predicate_inverse = inverse;
392   return inst;
393}
394
395/**
396 * Make the execution of \p inst dependent on the evaluation of a predicate.
397 */
398static inline fs_inst *
399set_predicate(enum brw_predicate pred, fs_inst *inst)
400{
401   return set_predicate_inv(pred, false, inst);
402}
403
404/**
405 * Write the result of evaluating the condition given by \p mod to a flag
406 * register.
407 */
408static inline fs_inst *
409set_condmod(enum brw_conditional_mod mod, fs_inst *inst)
410{
411   inst->conditional_mod = mod;
412   return inst;
413}
414
415/**
416 * Clamp the result of \p inst to the saturation range of its destination
417 * datatype.
418 */
419static inline fs_inst *
420set_saturate(bool saturate, fs_inst *inst)
421{
422   inst->saturate = saturate;
423   return inst;
424}
425
426/**
427 * Return the number of dataflow registers written by the instruction (either
428 * fully or partially) counted from 'floor(reg_offset(inst->dst) /
429 * register_size)'.  The somewhat arbitrary register size unit is 4B for the
430 * UNIFORM and IMM files and 32B for all other files.
431 */
432inline unsigned
433regs_written(const fs_inst *inst)
434{
435   assert(inst->dst.file != UNIFORM && inst->dst.file != IMM);
436   return DIV_ROUND_UP(reg_offset(inst->dst) % REG_SIZE +
437                       inst->size_written -
438                       MIN2(inst->size_written, reg_padding(inst->dst)),
439                       REG_SIZE);
440}
441
442/**
443 * Return the number of dataflow registers read by the instruction (either
444 * fully or partially) counted from 'floor(reg_offset(inst->src[i]) /
445 * register_size)'.  The somewhat arbitrary register size unit is 4B for the
446 * UNIFORM and IMM files and 32B for all other files.
447 */
448inline unsigned
449regs_read(const fs_inst *inst, unsigned i)
450{
451   const unsigned reg_size =
452      inst->src[i].file == UNIFORM || inst->src[i].file == IMM ? 4 : REG_SIZE;
453   return DIV_ROUND_UP(reg_offset(inst->src[i]) % reg_size +
454                       inst->size_read(i) -
455                       MIN2(inst->size_read(i), reg_padding(inst->src[i])),
456                       reg_size);
457}
458
459static inline enum brw_reg_type
460get_exec_type(const fs_inst *inst)
461{
462   brw_reg_type exec_type = BRW_REGISTER_TYPE_B;
463
464   for (int i = 0; i < inst->sources; i++) {
465      if (inst->src[i].file != BAD_FILE) {
466         const brw_reg_type t = get_exec_type(inst->src[i].type);
467         if (type_sz(t) > type_sz(exec_type))
468            exec_type = t;
469         else if (type_sz(t) == type_sz(exec_type) &&
470                  brw_reg_type_is_floating_point(t))
471            exec_type = t;
472      }
473   }
474
475   if (exec_type == BRW_REGISTER_TYPE_B)
476      exec_type = inst->dst.type;
477
478   assert(exec_type != BRW_REGISTER_TYPE_B);
479
480   return exec_type;
481}
482
483static inline unsigned
484get_exec_type_size(const fs_inst *inst)
485{
486   return type_sz(get_exec_type(inst));
487}
488
489/**
490 * Return whether the following regioning restriction applies to the specified
491 * instruction.  From the Cherryview PRM Vol 7. "Register Region
492 * Restrictions":
493 *
494 * "When source or destination datatype is 64b or operation is integer DWord
495 *  multiply, regioning in Align1 must follow these rules:
496 *
497 *  1. Source and Destination horizontal stride must be aligned to the same qword.
498 *  2. Regioning must ensure Src.Vstride = Src.Width * Src.Hstride.
499 *  3. Source and Destination offset must be the same, except the case of
500 *     scalar source."
501 */
502static inline bool
503has_dst_aligned_region_restriction(const gen_device_info *devinfo,
504                                   const fs_inst *inst)
505{
506   const brw_reg_type exec_type = get_exec_type(inst);
507   const bool is_int_multiply = !brw_reg_type_is_floating_point(exec_type) &&
508         (inst->opcode == BRW_OPCODE_MUL || inst->opcode == BRW_OPCODE_MAD);
509
510   if (type_sz(inst->dst.type) > 4 || type_sz(exec_type) > 4 ||
511       (type_sz(exec_type) == 4 && is_int_multiply))
512      return devinfo->is_cherryview || gen_device_info_is_9lp(devinfo);
513   else
514      return false;
515}
516
517#endif
518