101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2015 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include "brw_nir.h"
2501e04c3fSmrg#include "brw_vec4.h"
2601e04c3fSmrg#include "brw_vec4_builder.h"
2701e04c3fSmrg#include "brw_vec4_surface_builder.h"
287ec681f3Smrg#include "brw_eu.h"
2901e04c3fSmrg
3001e04c3fSmrgusing namespace brw;
3101e04c3fSmrgusing namespace brw::surface_access;
3201e04c3fSmrg
3301e04c3fSmrgnamespace brw {
3401e04c3fSmrg
3501e04c3fSmrgvoid
3601e04c3fSmrgvec4_visitor::emit_nir_code()
3701e04c3fSmrg{
3801e04c3fSmrg   if (nir->num_uniforms > 0)
3901e04c3fSmrg      nir_setup_uniforms();
4001e04c3fSmrg
4101e04c3fSmrg   nir_emit_impl(nir_shader_get_entrypoint((nir_shader *)nir));
4201e04c3fSmrg}
4301e04c3fSmrg
4401e04c3fSmrgvoid
4501e04c3fSmrgvec4_visitor::nir_setup_uniforms()
4601e04c3fSmrg{
4701e04c3fSmrg   uniforms = nir->num_uniforms / 16;
4801e04c3fSmrg}
4901e04c3fSmrg
5001e04c3fSmrgvoid
5101e04c3fSmrgvec4_visitor::nir_emit_impl(nir_function_impl *impl)
5201e04c3fSmrg{
5301e04c3fSmrg   nir_locals = ralloc_array(mem_ctx, dst_reg, impl->reg_alloc);
5401e04c3fSmrg   for (unsigned i = 0; i < impl->reg_alloc; i++) {
5501e04c3fSmrg      nir_locals[i] = dst_reg();
5601e04c3fSmrg   }
5701e04c3fSmrg
5801e04c3fSmrg   foreach_list_typed(nir_register, reg, node, &impl->registers) {
5901e04c3fSmrg      unsigned array_elems =
6001e04c3fSmrg         reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
6101e04c3fSmrg      const unsigned num_regs = array_elems * DIV_ROUND_UP(reg->bit_size, 32);
6201e04c3fSmrg      nir_locals[reg->index] = dst_reg(VGRF, alloc.allocate(num_regs));
6301e04c3fSmrg
6401e04c3fSmrg      if (reg->bit_size == 64)
6501e04c3fSmrg         nir_locals[reg->index].type = BRW_REGISTER_TYPE_DF;
6601e04c3fSmrg   }
6701e04c3fSmrg
6801e04c3fSmrg   nir_ssa_values = ralloc_array(mem_ctx, dst_reg, impl->ssa_alloc);
6901e04c3fSmrg
7001e04c3fSmrg   nir_emit_cf_list(&impl->body);
7101e04c3fSmrg}
7201e04c3fSmrg
7301e04c3fSmrgvoid
7401e04c3fSmrgvec4_visitor::nir_emit_cf_list(exec_list *list)
7501e04c3fSmrg{
7601e04c3fSmrg   exec_list_validate(list);
7701e04c3fSmrg   foreach_list_typed(nir_cf_node, node, node, list) {
7801e04c3fSmrg      switch (node->type) {
7901e04c3fSmrg      case nir_cf_node_if:
8001e04c3fSmrg         nir_emit_if(nir_cf_node_as_if(node));
8101e04c3fSmrg         break;
8201e04c3fSmrg
8301e04c3fSmrg      case nir_cf_node_loop:
8401e04c3fSmrg         nir_emit_loop(nir_cf_node_as_loop(node));
8501e04c3fSmrg         break;
8601e04c3fSmrg
8701e04c3fSmrg      case nir_cf_node_block:
8801e04c3fSmrg         nir_emit_block(nir_cf_node_as_block(node));
8901e04c3fSmrg         break;
9001e04c3fSmrg
9101e04c3fSmrg      default:
9201e04c3fSmrg         unreachable("Invalid CFG node block");
9301e04c3fSmrg      }
9401e04c3fSmrg   }
9501e04c3fSmrg}
9601e04c3fSmrg
9701e04c3fSmrgvoid
9801e04c3fSmrgvec4_visitor::nir_emit_if(nir_if *if_stmt)
9901e04c3fSmrg{
10001e04c3fSmrg   /* First, put the condition in f0 */
10101e04c3fSmrg   src_reg condition = get_nir_src(if_stmt->condition, BRW_REGISTER_TYPE_D, 1);
10201e04c3fSmrg   vec4_instruction *inst = emit(MOV(dst_null_d(), condition));
10301e04c3fSmrg   inst->conditional_mod = BRW_CONDITIONAL_NZ;
10401e04c3fSmrg
10501e04c3fSmrg   /* We can just predicate based on the X channel, as the condition only
10601e04c3fSmrg    * goes on its own line */
10701e04c3fSmrg   emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X));
10801e04c3fSmrg
10901e04c3fSmrg   nir_emit_cf_list(&if_stmt->then_list);
11001e04c3fSmrg
11101e04c3fSmrg   /* note: if the else is empty, dead CF elimination will remove it */
11201e04c3fSmrg   emit(BRW_OPCODE_ELSE);
11301e04c3fSmrg
11401e04c3fSmrg   nir_emit_cf_list(&if_stmt->else_list);
11501e04c3fSmrg
11601e04c3fSmrg   emit(BRW_OPCODE_ENDIF);
11701e04c3fSmrg}
11801e04c3fSmrg
11901e04c3fSmrgvoid
12001e04c3fSmrgvec4_visitor::nir_emit_loop(nir_loop *loop)
12101e04c3fSmrg{
12201e04c3fSmrg   emit(BRW_OPCODE_DO);
12301e04c3fSmrg
12401e04c3fSmrg   nir_emit_cf_list(&loop->body);
12501e04c3fSmrg
12601e04c3fSmrg   emit(BRW_OPCODE_WHILE);
12701e04c3fSmrg}
12801e04c3fSmrg
12901e04c3fSmrgvoid
13001e04c3fSmrgvec4_visitor::nir_emit_block(nir_block *block)
13101e04c3fSmrg{
13201e04c3fSmrg   nir_foreach_instr(instr, block) {
13301e04c3fSmrg      nir_emit_instr(instr);
13401e04c3fSmrg   }
13501e04c3fSmrg}
13601e04c3fSmrg
13701e04c3fSmrgvoid
13801e04c3fSmrgvec4_visitor::nir_emit_instr(nir_instr *instr)
13901e04c3fSmrg{
14001e04c3fSmrg   base_ir = instr;
14101e04c3fSmrg
14201e04c3fSmrg   switch (instr->type) {
14301e04c3fSmrg   case nir_instr_type_load_const:
14401e04c3fSmrg      nir_emit_load_const(nir_instr_as_load_const(instr));
14501e04c3fSmrg      break;
14601e04c3fSmrg
14701e04c3fSmrg   case nir_instr_type_intrinsic:
14801e04c3fSmrg      nir_emit_intrinsic(nir_instr_as_intrinsic(instr));
14901e04c3fSmrg      break;
15001e04c3fSmrg
15101e04c3fSmrg   case nir_instr_type_alu:
15201e04c3fSmrg      nir_emit_alu(nir_instr_as_alu(instr));
15301e04c3fSmrg      break;
15401e04c3fSmrg
15501e04c3fSmrg   case nir_instr_type_jump:
15601e04c3fSmrg      nir_emit_jump(nir_instr_as_jump(instr));
15701e04c3fSmrg      break;
15801e04c3fSmrg
15901e04c3fSmrg   case nir_instr_type_tex:
16001e04c3fSmrg      nir_emit_texture(nir_instr_as_tex(instr));
16101e04c3fSmrg      break;
16201e04c3fSmrg
16301e04c3fSmrg   case nir_instr_type_ssa_undef:
16401e04c3fSmrg      nir_emit_undef(nir_instr_as_ssa_undef(instr));
16501e04c3fSmrg      break;
16601e04c3fSmrg
16701e04c3fSmrg   default:
16801e04c3fSmrg      unreachable("VS instruction not yet implemented by NIR->vec4");
16901e04c3fSmrg   }
17001e04c3fSmrg}
17101e04c3fSmrg
17201e04c3fSmrgstatic dst_reg
17301e04c3fSmrgdst_reg_for_nir_reg(vec4_visitor *v, nir_register *nir_reg,
17401e04c3fSmrg                    unsigned base_offset, nir_src *indirect)
17501e04c3fSmrg{
17601e04c3fSmrg   dst_reg reg;
17701e04c3fSmrg
17801e04c3fSmrg   reg = v->nir_locals[nir_reg->index];
17901e04c3fSmrg   if (nir_reg->bit_size == 64)
18001e04c3fSmrg      reg.type = BRW_REGISTER_TYPE_DF;
18101e04c3fSmrg   reg = offset(reg, 8, base_offset);
18201e04c3fSmrg   if (indirect) {
18301e04c3fSmrg      reg.reladdr =
18401e04c3fSmrg         new(v->mem_ctx) src_reg(v->get_nir_src(*indirect,
18501e04c3fSmrg                                                BRW_REGISTER_TYPE_D,
18601e04c3fSmrg                                                1));
18701e04c3fSmrg   }
18801e04c3fSmrg   return reg;
18901e04c3fSmrg}
19001e04c3fSmrg
19101e04c3fSmrgdst_reg
19201e04c3fSmrgvec4_visitor::get_nir_dest(const nir_dest &dest)
19301e04c3fSmrg{
19401e04c3fSmrg   if (dest.is_ssa) {
19501e04c3fSmrg      dst_reg dst =
19601e04c3fSmrg         dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(dest.ssa.bit_size, 32)));
19701e04c3fSmrg      if (dest.ssa.bit_size == 64)
19801e04c3fSmrg         dst.type = BRW_REGISTER_TYPE_DF;
19901e04c3fSmrg      nir_ssa_values[dest.ssa.index] = dst;
20001e04c3fSmrg      return dst;
20101e04c3fSmrg   } else {
20201e04c3fSmrg      return dst_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
20301e04c3fSmrg                                 dest.reg.indirect);
20401e04c3fSmrg   }
20501e04c3fSmrg}
20601e04c3fSmrg
20701e04c3fSmrgdst_reg
20801e04c3fSmrgvec4_visitor::get_nir_dest(const nir_dest &dest, enum brw_reg_type type)
20901e04c3fSmrg{
21001e04c3fSmrg   return retype(get_nir_dest(dest), type);
21101e04c3fSmrg}
21201e04c3fSmrg
21301e04c3fSmrgdst_reg
21401e04c3fSmrgvec4_visitor::get_nir_dest(const nir_dest &dest, nir_alu_type type)
21501e04c3fSmrg{
21601e04c3fSmrg   return get_nir_dest(dest, brw_type_for_nir_type(devinfo, type));
21701e04c3fSmrg}
21801e04c3fSmrg
21901e04c3fSmrgsrc_reg
22001e04c3fSmrgvec4_visitor::get_nir_src(const nir_src &src, enum brw_reg_type type,
22101e04c3fSmrg                          unsigned num_components)
22201e04c3fSmrg{
22301e04c3fSmrg   dst_reg reg;
22401e04c3fSmrg
22501e04c3fSmrg   if (src.is_ssa) {
22601e04c3fSmrg      assert(src.ssa != NULL);
22701e04c3fSmrg      reg = nir_ssa_values[src.ssa->index];
22801e04c3fSmrg   }
22901e04c3fSmrg   else {
23001e04c3fSmrg      reg = dst_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
23101e04c3fSmrg                                src.reg.indirect);
23201e04c3fSmrg   }
23301e04c3fSmrg
23401e04c3fSmrg   reg = retype(reg, type);
23501e04c3fSmrg
23601e04c3fSmrg   src_reg reg_as_src = src_reg(reg);
23701e04c3fSmrg   reg_as_src.swizzle = brw_swizzle_for_size(num_components);
23801e04c3fSmrg   return reg_as_src;
23901e04c3fSmrg}
24001e04c3fSmrg
24101e04c3fSmrgsrc_reg
24201e04c3fSmrgvec4_visitor::get_nir_src(const nir_src &src, nir_alu_type type,
24301e04c3fSmrg                          unsigned num_components)
24401e04c3fSmrg{
24501e04c3fSmrg   return get_nir_src(src, brw_type_for_nir_type(devinfo, type),
24601e04c3fSmrg                      num_components);
24701e04c3fSmrg}
24801e04c3fSmrg
24901e04c3fSmrgsrc_reg
25001e04c3fSmrgvec4_visitor::get_nir_src(const nir_src &src, unsigned num_components)
25101e04c3fSmrg{
25201e04c3fSmrg   /* if type is not specified, default to signed int */
25301e04c3fSmrg   return get_nir_src(src, nir_type_int32, num_components);
25401e04c3fSmrg}
25501e04c3fSmrg
2569f464c52Smayasrc_reg
2579f464c52Smayavec4_visitor::get_nir_src_imm(const nir_src &src)
2589f464c52Smaya{
2599f464c52Smaya   assert(nir_src_num_components(src) == 1);
2609f464c52Smaya   assert(nir_src_bit_size(src) == 32);
2619f464c52Smaya   return nir_src_is_const(src) ? src_reg(brw_imm_d(nir_src_as_int(src))) :
2629f464c52Smaya                                  get_nir_src(src, 1);
2639f464c52Smaya}
2649f464c52Smaya
26501e04c3fSmrgsrc_reg
26601e04c3fSmrgvec4_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
26701e04c3fSmrg{
26801e04c3fSmrg   nir_src *offset_src = nir_get_io_offset_src(instr);
26901e04c3fSmrg
2709f464c52Smaya   if (nir_src_is_const(*offset_src)) {
27101e04c3fSmrg      /* The only constant offset we should find is 0.  brw_nir.c's
27201e04c3fSmrg       * add_const_offset_to_base() will fold other constant offsets
27301e04c3fSmrg       * into instr->const_index[0].
27401e04c3fSmrg       */
2759f464c52Smaya      assert(nir_src_as_uint(*offset_src) == 0);
27601e04c3fSmrg      return src_reg();
27701e04c3fSmrg   }
27801e04c3fSmrg
27901e04c3fSmrg   return get_nir_src(*offset_src, BRW_REGISTER_TYPE_UD, 1);
28001e04c3fSmrg}
28101e04c3fSmrg
28201e04c3fSmrgstatic src_reg
28301e04c3fSmrgsetup_imm_df(const vec4_builder &bld, double v)
28401e04c3fSmrg{
2857ec681f3Smrg   const intel_device_info *devinfo = bld.shader->devinfo;
2867ec681f3Smrg   assert(devinfo->ver == 7);
28701e04c3fSmrg
2887ec681f3Smrg   /* gfx7.5 does not support DF immediates straighforward but the DIM
28901e04c3fSmrg    * instruction allows to set the 64-bit immediate value.
29001e04c3fSmrg    */
29101e04c3fSmrg   if (devinfo->is_haswell) {
29201e04c3fSmrg      const vec4_builder ubld = bld.exec_all();
29301e04c3fSmrg      const dst_reg dst = bld.vgrf(BRW_REGISTER_TYPE_DF);
29401e04c3fSmrg      ubld.DIM(dst, brw_imm_df(v));
29501e04c3fSmrg      return swizzle(src_reg(dst), BRW_SWIZZLE_XXXX);
29601e04c3fSmrg   }
29701e04c3fSmrg
2987ec681f3Smrg   /* gfx7 does not support DF immediates */
29901e04c3fSmrg   union {
30001e04c3fSmrg      double d;
30101e04c3fSmrg      struct {
30201e04c3fSmrg         uint32_t i1;
30301e04c3fSmrg         uint32_t i2;
30401e04c3fSmrg      };
30501e04c3fSmrg   } di;
30601e04c3fSmrg
30701e04c3fSmrg   di.d = v;
30801e04c3fSmrg
30901e04c3fSmrg   /* Write the low 32-bit of the constant to the X:UD channel and the
31001e04c3fSmrg    * high 32-bit to the Y:UD channel to build the constant in a VGRF.
31101e04c3fSmrg    * We have to do this twice (offset 0 and offset 1), since a DF VGRF takes
31201e04c3fSmrg    * two SIMD8 registers in SIMD4x2 execution. Finally, return a swizzle
31301e04c3fSmrg    * XXXX so any access to the VGRF only reads the constant data in these
31401e04c3fSmrg    * channels.
31501e04c3fSmrg    */
31601e04c3fSmrg   const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
31701e04c3fSmrg   for (unsigned n = 0; n < 2; n++) {
31801e04c3fSmrg      const vec4_builder ubld = bld.exec_all().group(4, n);
31901e04c3fSmrg      ubld.MOV(writemask(offset(tmp, 8, n), WRITEMASK_X), brw_imm_ud(di.i1));
32001e04c3fSmrg      ubld.MOV(writemask(offset(tmp, 8, n), WRITEMASK_Y), brw_imm_ud(di.i2));
32101e04c3fSmrg   }
32201e04c3fSmrg
32301e04c3fSmrg   return swizzle(src_reg(retype(tmp, BRW_REGISTER_TYPE_DF)), BRW_SWIZZLE_XXXX);
32401e04c3fSmrg}
32501e04c3fSmrg
32601e04c3fSmrgvoid
32701e04c3fSmrgvec4_visitor::nir_emit_load_const(nir_load_const_instr *instr)
32801e04c3fSmrg{
32901e04c3fSmrg   dst_reg reg;
33001e04c3fSmrg
33101e04c3fSmrg   if (instr->def.bit_size == 64) {
33201e04c3fSmrg      reg = dst_reg(VGRF, alloc.allocate(2));
33301e04c3fSmrg      reg.type = BRW_REGISTER_TYPE_DF;
33401e04c3fSmrg   } else {
33501e04c3fSmrg      reg = dst_reg(VGRF, alloc.allocate(1));
33601e04c3fSmrg      reg.type = BRW_REGISTER_TYPE_D;
33701e04c3fSmrg   }
33801e04c3fSmrg
33901e04c3fSmrg   const vec4_builder ibld = vec4_builder(this).at_end();
34001e04c3fSmrg   unsigned remaining = brw_writemask_for_size(instr->def.num_components);
34101e04c3fSmrg
34201e04c3fSmrg   /* @FIXME: consider emitting vector operations to save some MOVs in
34301e04c3fSmrg    * cases where the components are representable in 8 bits.
34401e04c3fSmrg    * For now, we emit a MOV for each distinct value.
34501e04c3fSmrg    */
34601e04c3fSmrg   for (unsigned i = 0; i < instr->def.num_components; i++) {
34701e04c3fSmrg      unsigned writemask = 1 << i;
34801e04c3fSmrg
34901e04c3fSmrg      if ((remaining & writemask) == 0)
35001e04c3fSmrg         continue;
35101e04c3fSmrg
35201e04c3fSmrg      for (unsigned j = i; j < instr->def.num_components; j++) {
35301e04c3fSmrg         if ((instr->def.bit_size == 32 &&
3549f464c52Smaya              instr->value[i].u32 == instr->value[j].u32) ||
35501e04c3fSmrg             (instr->def.bit_size == 64 &&
3569f464c52Smaya              instr->value[i].f64 == instr->value[j].f64)) {
35701e04c3fSmrg            writemask |= 1 << j;
35801e04c3fSmrg         }
35901e04c3fSmrg      }
36001e04c3fSmrg
36101e04c3fSmrg      reg.writemask = writemask;
36201e04c3fSmrg      if (instr->def.bit_size == 64) {
3639f464c52Smaya         emit(MOV(reg, setup_imm_df(ibld, instr->value[i].f64)));
36401e04c3fSmrg      } else {
3659f464c52Smaya         emit(MOV(reg, brw_imm_d(instr->value[i].i32)));
36601e04c3fSmrg      }
36701e04c3fSmrg
36801e04c3fSmrg      remaining &= ~writemask;
36901e04c3fSmrg   }
37001e04c3fSmrg
37101e04c3fSmrg   /* Set final writemask */
37201e04c3fSmrg   reg.writemask = brw_writemask_for_size(instr->def.num_components);
37301e04c3fSmrg
37401e04c3fSmrg   nir_ssa_values[instr->def.index] = reg;
37501e04c3fSmrg}
37601e04c3fSmrg
3779f464c52Smayasrc_reg
3789f464c52Smayavec4_visitor::get_nir_ssbo_intrinsic_index(nir_intrinsic_instr *instr)
3799f464c52Smaya{
3809f464c52Smaya   /* SSBO stores are weird in that their index is in src[1] */
3819f464c52Smaya   const unsigned src = instr->intrinsic == nir_intrinsic_store_ssbo ? 1 : 0;
3829f464c52Smaya
3839f464c52Smaya   src_reg surf_index;
3849f464c52Smaya   if (nir_src_is_const(instr->src[src])) {
3859f464c52Smaya      unsigned index = prog_data->base.binding_table.ssbo_start +
3869f464c52Smaya                       nir_src_as_uint(instr->src[src]);
3879f464c52Smaya      surf_index = brw_imm_ud(index);
3889f464c52Smaya   } else {
3899f464c52Smaya      surf_index = src_reg(this, glsl_type::uint_type);
3909f464c52Smaya      emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[src], 1),
3919f464c52Smaya               brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
3929f464c52Smaya      surf_index = emit_uniformize(surf_index);
3939f464c52Smaya   }
3949f464c52Smaya
3959f464c52Smaya   return surf_index;
3969f464c52Smaya}
3979f464c52Smaya
39801e04c3fSmrgvoid
39901e04c3fSmrgvec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
40001e04c3fSmrg{
40101e04c3fSmrg   dst_reg dest;
40201e04c3fSmrg   src_reg src;
40301e04c3fSmrg
40401e04c3fSmrg   switch (instr->intrinsic) {
40501e04c3fSmrg
40601e04c3fSmrg   case nir_intrinsic_load_input: {
4077ec681f3Smrg      assert(nir_dest_bit_size(instr->dest) == 32);
40801e04c3fSmrg      /* We set EmitNoIndirectInput for VS */
4099f464c52Smaya      unsigned load_offset = nir_src_as_uint(instr->src[0]);
41001e04c3fSmrg
41101e04c3fSmrg      dest = get_nir_dest(instr->dest);
41201e04c3fSmrg      dest.writemask = brw_writemask_for_size(instr->num_components);
41301e04c3fSmrg
4149f464c52Smaya      src = src_reg(ATTR, instr->const_index[0] + load_offset,
41501e04c3fSmrg                    glsl_type::uvec4_type);
41601e04c3fSmrg      src = retype(src, dest.type);
41701e04c3fSmrg
4187ec681f3Smrg      /* Swizzle source based on component layout qualifier */
4197ec681f3Smrg      src.swizzle = BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr));
4207ec681f3Smrg      emit(MOV(dest, src));
42101e04c3fSmrg      break;
42201e04c3fSmrg   }
42301e04c3fSmrg
42401e04c3fSmrg   case nir_intrinsic_store_output: {
4257ec681f3Smrg      assert(nir_src_bit_size(instr->src[0]) == 32);
4269f464c52Smaya      unsigned store_offset = nir_src_as_uint(instr->src[1]);
4279f464c52Smaya      int varying = instr->const_index[0] + store_offset;
4287ec681f3Smrg      src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F,
4297ec681f3Smrg                        instr->num_components);
43001e04c3fSmrg
43101e04c3fSmrg      unsigned c = nir_intrinsic_component(instr);
43201e04c3fSmrg      output_reg[varying][c] = dst_reg(src);
43301e04c3fSmrg      output_num_components[varying][c] = instr->num_components;
43401e04c3fSmrg      break;
43501e04c3fSmrg   }
43601e04c3fSmrg
4377ec681f3Smrg   case nir_intrinsic_get_ssbo_size: {
4389f464c52Smaya      assert(nir_src_num_components(instr->src[0]) == 1);
4399f464c52Smaya      unsigned ssbo_index = nir_src_is_const(instr->src[0]) ?
4409f464c52Smaya                            nir_src_as_uint(instr->src[0]) : 0;
44101e04c3fSmrg
44201e04c3fSmrg      const unsigned index =
44301e04c3fSmrg         prog_data->base.binding_table.ssbo_start + ssbo_index;
44401e04c3fSmrg      dst_reg result_dst = get_nir_dest(instr->dest);
44501e04c3fSmrg      vec4_instruction *inst = new(mem_ctx)
44601e04c3fSmrg         vec4_instruction(SHADER_OPCODE_GET_BUFFER_SIZE, result_dst);
44701e04c3fSmrg
44801e04c3fSmrg      inst->base_mrf = 2;
44901e04c3fSmrg      inst->mlen = 1; /* always at least one */
45001e04c3fSmrg      inst->src[1] = brw_imm_ud(index);
45101e04c3fSmrg
45201e04c3fSmrg      /* MRF for the first parameter */
45301e04c3fSmrg      src_reg lod = brw_imm_d(0);
45401e04c3fSmrg      int param_base = inst->base_mrf;
45501e04c3fSmrg      int writemask = WRITEMASK_X;
45601e04c3fSmrg      emit(MOV(dst_reg(MRF, param_base, glsl_type::int_type, writemask), lod));
45701e04c3fSmrg
45801e04c3fSmrg      emit(inst);
45901e04c3fSmrg      break;
46001e04c3fSmrg   }
46101e04c3fSmrg
46201e04c3fSmrg   case nir_intrinsic_store_ssbo: {
4637ec681f3Smrg      assert(devinfo->ver == 7);
46401e04c3fSmrg
4659f464c52Smaya      /* brw_nir_lower_mem_access_bit_sizes takes care of this */
4669f464c52Smaya      assert(nir_src_bit_size(instr->src[0]) == 32);
4679f464c52Smaya      assert(nir_intrinsic_write_mask(instr) ==
4689f464c52Smaya             (1u << instr->num_components) - 1);
46901e04c3fSmrg
4709f464c52Smaya      src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
4719f464c52Smaya      src_reg offset_reg = retype(get_nir_src_imm(instr->src[2]),
4729f464c52Smaya                                  BRW_REGISTER_TYPE_UD);
47301e04c3fSmrg
47401e04c3fSmrg      /* Value */
47501e04c3fSmrg      src_reg val_reg = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F, 4);
47601e04c3fSmrg
47701e04c3fSmrg      /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
47801e04c3fSmrg       * writes will use SIMD8 mode. In order to hide this and keep symmetry across
47901e04c3fSmrg       * typed and untyped messages and across hardware platforms, the
48001e04c3fSmrg       * current implementation of the untyped messages will transparently convert
48101e04c3fSmrg       * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
48201e04c3fSmrg       * and enabling only channel X on the SEND instruction.
48301e04c3fSmrg       *
48401e04c3fSmrg       * The above, works well for full vector writes, but not for partial writes
48501e04c3fSmrg       * where we want to write some channels and not others, like when we have
48601e04c3fSmrg       * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
48701e04c3fSmrg       * quite restrictive with regards to the channel enables we can configure in
48801e04c3fSmrg       * the message descriptor (not all combinations are allowed) we cannot simply
48901e04c3fSmrg       * implement these scenarios with a single message while keeping the
49001e04c3fSmrg       * aforementioned symmetry in the implementation. For now we de decided that
49101e04c3fSmrg       * it is better to keep the symmetry to reduce complexity, so in situations
49201e04c3fSmrg       * such as the one described we end up emitting two untyped write messages
49301e04c3fSmrg       * (one for xy and another for w).
49401e04c3fSmrg       *
49501e04c3fSmrg       * The code below packs consecutive channels into a single write message,
49601e04c3fSmrg       * detects gaps in the vector write and if needed, sends a second message
49701e04c3fSmrg       * with the remaining channels. If in the future we decide that we want to
49801e04c3fSmrg       * emit a single message at the expense of losing the symmetry in the
49901e04c3fSmrg       * implementation we can:
50001e04c3fSmrg       *
50101e04c3fSmrg       * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
50201e04c3fSmrg       *    message payload. In this mode we can write up to 8 offsets and dwords
50301e04c3fSmrg       *    to the red channel only (for the two vec4s in the SIMD4x2 execution)
50401e04c3fSmrg       *    and select which of the 8 channels carry data to write by setting the
50501e04c3fSmrg       *    appropriate writemask in the dst register of the SEND instruction.
50601e04c3fSmrg       *    It would require to write a new generator opcode specifically for
50701e04c3fSmrg       *    IvyBridge since we would need to prepare a SIMD8 payload that could
50801e04c3fSmrg       *    use any channel, not just X.
50901e04c3fSmrg       *
51001e04c3fSmrg       * 2) For Haswell+: Simply send a single write message but set the writemask
51101e04c3fSmrg       *    on the dst of the SEND instruction to select the channels we want to
51201e04c3fSmrg       *    write. It would require to modify the current messages to receive
51301e04c3fSmrg       *    and honor the writemask provided.
51401e04c3fSmrg       */
51501e04c3fSmrg      const vec4_builder bld = vec4_builder(this).at_end()
51601e04c3fSmrg                               .annotate(current_annotation, base_ir);
51701e04c3fSmrg
5189f464c52Smaya      emit_untyped_write(bld, surf_index, offset_reg, val_reg,
5199f464c52Smaya                         1 /* dims */, instr->num_components /* size */,
5209f464c52Smaya                         BRW_PREDICATE_NONE);
52101e04c3fSmrg      break;
52201e04c3fSmrg   }
52301e04c3fSmrg
52401e04c3fSmrg   case nir_intrinsic_load_ssbo: {
5257ec681f3Smrg      assert(devinfo->ver == 7);
52601e04c3fSmrg
5279f464c52Smaya      /* brw_nir_lower_mem_access_bit_sizes takes care of this */
5289f464c52Smaya      assert(nir_dest_bit_size(instr->dest) == 32);
52901e04c3fSmrg
5309f464c52Smaya      src_reg surf_index = get_nir_ssbo_intrinsic_index(instr);
5319f464c52Smaya      src_reg offset_reg = retype(get_nir_src_imm(instr->src[1]),
5329f464c52Smaya                                  BRW_REGISTER_TYPE_UD);
53301e04c3fSmrg
53401e04c3fSmrg      /* Read the vector */
53501e04c3fSmrg      const vec4_builder bld = vec4_builder(this).at_end()
53601e04c3fSmrg         .annotate(current_annotation, base_ir);
53701e04c3fSmrg
5389f464c52Smaya      src_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
5399f464c52Smaya                                              1 /* dims */, 4 /* size*/,
5409f464c52Smaya                                              BRW_PREDICATE_NONE);
54101e04c3fSmrg      dst_reg dest = get_nir_dest(instr->dest);
54201e04c3fSmrg      read_result.type = dest.type;
54301e04c3fSmrg      read_result.swizzle = brw_swizzle_for_size(instr->num_components);
54401e04c3fSmrg      emit(MOV(dest, read_result));
54501e04c3fSmrg      break;
54601e04c3fSmrg   }
54701e04c3fSmrg
5487ec681f3Smrg   case nir_intrinsic_ssbo_atomic_add:
54901e04c3fSmrg   case nir_intrinsic_ssbo_atomic_imin:
55001e04c3fSmrg   case nir_intrinsic_ssbo_atomic_umin:
55101e04c3fSmrg   case nir_intrinsic_ssbo_atomic_imax:
55201e04c3fSmrg   case nir_intrinsic_ssbo_atomic_umax:
55301e04c3fSmrg   case nir_intrinsic_ssbo_atomic_and:
55401e04c3fSmrg   case nir_intrinsic_ssbo_atomic_or:
55501e04c3fSmrg   case nir_intrinsic_ssbo_atomic_xor:
55601e04c3fSmrg   case nir_intrinsic_ssbo_atomic_exchange:
55701e04c3fSmrg   case nir_intrinsic_ssbo_atomic_comp_swap:
5587ec681f3Smrg      nir_emit_ssbo_atomic(brw_aop_for_nir_intrinsic(instr), instr);
55901e04c3fSmrg      break;
56001e04c3fSmrg
56101e04c3fSmrg   case nir_intrinsic_load_vertex_id:
56201e04c3fSmrg      unreachable("should be lowered by lower_vertex_id()");
56301e04c3fSmrg
56401e04c3fSmrg   case nir_intrinsic_load_vertex_id_zero_base:
56501e04c3fSmrg   case nir_intrinsic_load_base_vertex:
56601e04c3fSmrg   case nir_intrinsic_load_instance_id:
56701e04c3fSmrg   case nir_intrinsic_load_base_instance:
56801e04c3fSmrg   case nir_intrinsic_load_draw_id:
56901e04c3fSmrg   case nir_intrinsic_load_invocation_id:
57001e04c3fSmrg      unreachable("should be lowered by brw_nir_lower_vs_inputs()");
57101e04c3fSmrg
57201e04c3fSmrg   case nir_intrinsic_load_uniform: {
57301e04c3fSmrg      /* Offsets are in bytes but they should always be multiples of 4 */
57401e04c3fSmrg      assert(nir_intrinsic_base(instr) % 4 == 0);
57501e04c3fSmrg
57601e04c3fSmrg      dest = get_nir_dest(instr->dest);
57701e04c3fSmrg
57801e04c3fSmrg      src = src_reg(dst_reg(UNIFORM, nir_intrinsic_base(instr) / 16));
57901e04c3fSmrg      src.type = dest.type;
58001e04c3fSmrg
58101e04c3fSmrg      /* Uniforms don't actually have to be vec4 aligned.  In the case that
58201e04c3fSmrg       * it isn't, we have to use a swizzle to shift things around.  They
58301e04c3fSmrg       * do still have the std140 alignment requirement that vec2's have to
58401e04c3fSmrg       * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
58501e04c3fSmrg       *
58601e04c3fSmrg       * The swizzle also works in the indirect case as the generator adds
58701e04c3fSmrg       * the swizzle to the offset for us.
58801e04c3fSmrg       */
58901e04c3fSmrg      const int type_size = type_sz(src.type);
59001e04c3fSmrg      unsigned shift = (nir_intrinsic_base(instr) % 16) / type_size;
59101e04c3fSmrg      assert(shift + instr->num_components <= 4);
59201e04c3fSmrg
5939f464c52Smaya      if (nir_src_is_const(instr->src[0])) {
5949f464c52Smaya         const unsigned load_offset = nir_src_as_uint(instr->src[0]);
59501e04c3fSmrg         /* Offsets are in bytes but they should always be multiples of 4 */
5969f464c52Smaya         assert(load_offset % 4 == 0);
59701e04c3fSmrg
59801e04c3fSmrg         src.swizzle = brw_swizzle_for_size(instr->num_components);
59901e04c3fSmrg         dest.writemask = brw_writemask_for_size(instr->num_components);
6009f464c52Smaya         unsigned offset = load_offset + shift * type_size;
60101e04c3fSmrg         src.offset = ROUND_DOWN_TO(offset, 16);
60201e04c3fSmrg         shift = (offset % 16) / type_size;
60301e04c3fSmrg         assert(shift + instr->num_components <= 4);
60401e04c3fSmrg         src.swizzle += BRW_SWIZZLE4(shift, shift, shift, shift);
60501e04c3fSmrg
60601e04c3fSmrg         emit(MOV(dest, src));
60701e04c3fSmrg      } else {
60801e04c3fSmrg         /* Uniform arrays are vec4 aligned, because of std140 alignment
60901e04c3fSmrg          * rules.
61001e04c3fSmrg          */
61101e04c3fSmrg         assert(shift == 0);
61201e04c3fSmrg
61301e04c3fSmrg         src_reg indirect = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_UD, 1);
61401e04c3fSmrg
61501e04c3fSmrg         /* MOV_INDIRECT is going to stomp the whole thing anyway */
61601e04c3fSmrg         dest.writemask = WRITEMASK_XYZW;
61701e04c3fSmrg
61801e04c3fSmrg         emit(SHADER_OPCODE_MOV_INDIRECT, dest, src,
61901e04c3fSmrg              indirect, brw_imm_ud(instr->const_index[1]));
62001e04c3fSmrg      }
62101e04c3fSmrg      break;
62201e04c3fSmrg   }
62301e04c3fSmrg
62401e04c3fSmrg   case nir_intrinsic_load_ubo: {
62501e04c3fSmrg      src_reg surf_index;
62601e04c3fSmrg
62701e04c3fSmrg      dest = get_nir_dest(instr->dest);
62801e04c3fSmrg
6299f464c52Smaya      if (nir_src_is_const(instr->src[0])) {
63001e04c3fSmrg         /* The block index is a constant, so just emit the binding table entry
63101e04c3fSmrg          * as an immediate.
63201e04c3fSmrg          */
63301e04c3fSmrg         const unsigned index = prog_data->base.binding_table.ubo_start +
6349f464c52Smaya                                nir_src_as_uint(instr->src[0]);
63501e04c3fSmrg         surf_index = brw_imm_ud(index);
63601e04c3fSmrg      } else {
63701e04c3fSmrg         /* The block index is not a constant. Evaluate the index expression
63801e04c3fSmrg          * per-channel and add the base UBO index; we have to select a value
63901e04c3fSmrg          * from any live channel.
64001e04c3fSmrg          */
64101e04c3fSmrg         surf_index = src_reg(this, glsl_type::uint_type);
64201e04c3fSmrg         emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int32,
64301e04c3fSmrg                                                   instr->num_components),
64401e04c3fSmrg                  brw_imm_ud(prog_data->base.binding_table.ubo_start)));
64501e04c3fSmrg         surf_index = emit_uniformize(surf_index);
64601e04c3fSmrg      }
64701e04c3fSmrg
6487ec681f3Smrg      src_reg push_reg;
64901e04c3fSmrg      src_reg offset_reg;
6509f464c52Smaya      if (nir_src_is_const(instr->src[1])) {
6519f464c52Smaya         unsigned load_offset = nir_src_as_uint(instr->src[1]);
6527ec681f3Smrg         unsigned aligned_offset = load_offset & ~15;
6537ec681f3Smrg         offset_reg = brw_imm_ud(aligned_offset);
6547ec681f3Smrg
6557ec681f3Smrg         /* See if we've selected this as a push constant candidate */
6567ec681f3Smrg         if (nir_src_is_const(instr->src[0])) {
6577ec681f3Smrg            const unsigned ubo_block = nir_src_as_uint(instr->src[0]);
6587ec681f3Smrg            const unsigned offset_256b = aligned_offset / 32;
6597ec681f3Smrg
6607ec681f3Smrg            for (int i = 0; i < 4; i++) {
6617ec681f3Smrg               const struct brw_ubo_range *range = &prog_data->base.ubo_ranges[i];
6627ec681f3Smrg               if (range->block == ubo_block &&
6637ec681f3Smrg                   offset_256b >= range->start &&
6647ec681f3Smrg                   offset_256b < range->start + range->length) {
6657ec681f3Smrg
6667ec681f3Smrg                  push_reg = src_reg(dst_reg(UNIFORM, UBO_START + i));
6677ec681f3Smrg                  push_reg.type = dest.type;
6687ec681f3Smrg                  push_reg.offset = aligned_offset - 32 * range->start;
6697ec681f3Smrg                  break;
6707ec681f3Smrg               }
6717ec681f3Smrg            }
6727ec681f3Smrg         }
67301e04c3fSmrg      } else {
67401e04c3fSmrg         offset_reg = src_reg(this, glsl_type::uint_type);
67501e04c3fSmrg         emit(MOV(dst_reg(offset_reg),
67601e04c3fSmrg                  get_nir_src(instr->src[1], nir_type_uint32, 1)));
67701e04c3fSmrg      }
67801e04c3fSmrg
67901e04c3fSmrg      src_reg packed_consts;
6807ec681f3Smrg      if (push_reg.file != BAD_FILE) {
6817ec681f3Smrg         packed_consts = push_reg;
6827ec681f3Smrg      } else if (nir_dest_bit_size(instr->dest) == 32) {
68301e04c3fSmrg         packed_consts = src_reg(this, glsl_type::vec4_type);
68401e04c3fSmrg         emit_pull_constant_load_reg(dst_reg(packed_consts),
68501e04c3fSmrg                                     surf_index,
68601e04c3fSmrg                                     offset_reg,
68701e04c3fSmrg                                     NULL, NULL /* before_block/inst */);
6887ec681f3Smrg         prog_data->base.has_ubo_pull = true;
68901e04c3fSmrg      } else {
69001e04c3fSmrg         src_reg temp = src_reg(this, glsl_type::dvec4_type);
69101e04c3fSmrg         src_reg temp_float = retype(temp, BRW_REGISTER_TYPE_F);
69201e04c3fSmrg
69301e04c3fSmrg         emit_pull_constant_load_reg(dst_reg(temp_float),
69401e04c3fSmrg                                     surf_index, offset_reg, NULL, NULL);
69501e04c3fSmrg         if (offset_reg.file == IMM)
69601e04c3fSmrg            offset_reg.ud += 16;
69701e04c3fSmrg         else
69801e04c3fSmrg            emit(ADD(dst_reg(offset_reg), offset_reg, brw_imm_ud(16u)));
69901e04c3fSmrg         emit_pull_constant_load_reg(dst_reg(byte_offset(temp_float, REG_SIZE)),
70001e04c3fSmrg                                     surf_index, offset_reg, NULL, NULL);
7017ec681f3Smrg         prog_data->base.has_ubo_pull = true;
70201e04c3fSmrg
70301e04c3fSmrg         packed_consts = src_reg(this, glsl_type::dvec4_type);
70401e04c3fSmrg         shuffle_64bit_data(dst_reg(packed_consts), temp, false);
70501e04c3fSmrg      }
70601e04c3fSmrg
70701e04c3fSmrg      packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
7089f464c52Smaya      if (nir_src_is_const(instr->src[1])) {
7099f464c52Smaya         unsigned load_offset = nir_src_as_uint(instr->src[1]);
71001e04c3fSmrg         unsigned type_size = type_sz(dest.type);
71101e04c3fSmrg         packed_consts.swizzle +=
7129f464c52Smaya            BRW_SWIZZLE4(load_offset % 16 / type_size,
7139f464c52Smaya                         load_offset % 16 / type_size,
7149f464c52Smaya                         load_offset % 16 / type_size,
7159f464c52Smaya                         load_offset % 16 / type_size);
71601e04c3fSmrg      }
71701e04c3fSmrg
71801e04c3fSmrg      emit(MOV(dest, retype(packed_consts, dest.type)));
71901e04c3fSmrg
72001e04c3fSmrg      break;
72101e04c3fSmrg   }
72201e04c3fSmrg
7237ec681f3Smrg   case nir_intrinsic_scoped_barrier:
7247ec681f3Smrg      assert(nir_intrinsic_execution_scope(instr) == NIR_SCOPE_NONE);
7257ec681f3Smrg      FALLTHROUGH;
72601e04c3fSmrg   case nir_intrinsic_memory_barrier: {
72701e04c3fSmrg      const vec4_builder bld =
72801e04c3fSmrg         vec4_builder(this).at_end().annotate(current_annotation, base_ir);
7297ec681f3Smrg      const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
7307ec681f3Smrg      vec4_instruction *fence =
7317ec681f3Smrg         bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp, brw_vec8_grf(0, 0));
7327ec681f3Smrg      fence->sfid = GFX7_SFID_DATAPORT_DATA_CACHE;
73301e04c3fSmrg      break;
73401e04c3fSmrg   }
73501e04c3fSmrg
73601e04c3fSmrg   case nir_intrinsic_shader_clock: {
73701e04c3fSmrg      /* We cannot do anything if there is an event, so ignore it for now */
73801e04c3fSmrg      const src_reg shader_clock = get_timestamp();
73901e04c3fSmrg      const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
74001e04c3fSmrg
74101e04c3fSmrg      dest = get_nir_dest(instr->dest, type);
74201e04c3fSmrg      emit(MOV(dest, shader_clock));
74301e04c3fSmrg      break;
74401e04c3fSmrg   }
74501e04c3fSmrg
74601e04c3fSmrg   default:
74701e04c3fSmrg      unreachable("Unknown intrinsic");
74801e04c3fSmrg   }
74901e04c3fSmrg}
75001e04c3fSmrg
75101e04c3fSmrgvoid
75201e04c3fSmrgvec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
75301e04c3fSmrg{
75401e04c3fSmrg   dst_reg dest;
75501e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
75601e04c3fSmrg      dest = get_nir_dest(instr->dest);
75701e04c3fSmrg
7589f464c52Smaya   src_reg surface = get_nir_ssbo_intrinsic_index(instr);
75901e04c3fSmrg   src_reg offset = get_nir_src(instr->src[1], 1);
76001e04c3fSmrg   src_reg data1;
76101e04c3fSmrg   if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
76201e04c3fSmrg      data1 = get_nir_src(instr->src[2], 1);
76301e04c3fSmrg   src_reg data2;
76401e04c3fSmrg   if (op == BRW_AOP_CMPWR)
76501e04c3fSmrg      data2 = get_nir_src(instr->src[3], 1);
76601e04c3fSmrg
76701e04c3fSmrg   /* Emit the actual atomic operation operation */
76801e04c3fSmrg   const vec4_builder bld =
76901e04c3fSmrg      vec4_builder(this).at_end().annotate(current_annotation, base_ir);
77001e04c3fSmrg
77101e04c3fSmrg   src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
77201e04c3fSmrg                                               data1, data2,
77301e04c3fSmrg                                               1 /* dims */, 1 /* rsize */,
77401e04c3fSmrg                                               op,
77501e04c3fSmrg                                               BRW_PREDICATE_NONE);
77601e04c3fSmrg   dest.type = atomic_result.type;
77701e04c3fSmrg   bld.MOV(dest, atomic_result);
77801e04c3fSmrg}
77901e04c3fSmrg
78001e04c3fSmrgstatic unsigned
78101e04c3fSmrgbrw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
78201e04c3fSmrg{
78301e04c3fSmrg   return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
78401e04c3fSmrg}
78501e04c3fSmrg
78601e04c3fSmrgbool
78701e04c3fSmrgvec4_visitor::optimize_predicate(nir_alu_instr *instr,
78801e04c3fSmrg                                 enum brw_predicate *predicate)
78901e04c3fSmrg{
79001e04c3fSmrg   if (!instr->src[0].src.is_ssa ||
79101e04c3fSmrg       instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
79201e04c3fSmrg      return false;
79301e04c3fSmrg
79401e04c3fSmrg   nir_alu_instr *cmp_instr =
79501e04c3fSmrg      nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
79601e04c3fSmrg
79701e04c3fSmrg   switch (cmp_instr->op) {
7989f464c52Smaya   case nir_op_b32any_fnequal2:
7999f464c52Smaya   case nir_op_b32any_inequal2:
8009f464c52Smaya   case nir_op_b32any_fnequal3:
8019f464c52Smaya   case nir_op_b32any_inequal3:
8029f464c52Smaya   case nir_op_b32any_fnequal4:
8039f464c52Smaya   case nir_op_b32any_inequal4:
80401e04c3fSmrg      *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
80501e04c3fSmrg      break;
8069f464c52Smaya   case nir_op_b32all_fequal2:
8079f464c52Smaya   case nir_op_b32all_iequal2:
8089f464c52Smaya   case nir_op_b32all_fequal3:
8099f464c52Smaya   case nir_op_b32all_iequal3:
8109f464c52Smaya   case nir_op_b32all_fequal4:
8119f464c52Smaya   case nir_op_b32all_iequal4:
81201e04c3fSmrg      *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
81301e04c3fSmrg      break;
81401e04c3fSmrg   default:
81501e04c3fSmrg      return false;
81601e04c3fSmrg   }
81701e04c3fSmrg
81801e04c3fSmrg   unsigned size_swizzle =
81901e04c3fSmrg      brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
82001e04c3fSmrg
82101e04c3fSmrg   src_reg op[2];
82201e04c3fSmrg   assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
82301e04c3fSmrg   for (unsigned i = 0; i < 2; i++) {
82401e04c3fSmrg      nir_alu_type type = nir_op_infos[cmp_instr->op].input_types[i];
82501e04c3fSmrg      unsigned bit_size = nir_src_bit_size(cmp_instr->src[i].src);
82601e04c3fSmrg      type = (nir_alu_type) (((unsigned) type) | bit_size);
82701e04c3fSmrg      op[i] = get_nir_src(cmp_instr->src[i].src, type, 4);
82801e04c3fSmrg      unsigned base_swizzle =
82901e04c3fSmrg         brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
83001e04c3fSmrg      op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
83101e04c3fSmrg   }
83201e04c3fSmrg
83301e04c3fSmrg   emit(CMP(dst_null_d(), op[0], op[1],
8347ec681f3Smrg            brw_cmod_for_nir_comparison(cmp_instr->op)));
83501e04c3fSmrg
83601e04c3fSmrg   return true;
83701e04c3fSmrg}
83801e04c3fSmrg
83901e04c3fSmrgstatic void
84001e04c3fSmrgemit_find_msb_using_lzd(const vec4_builder &bld,
84101e04c3fSmrg                        const dst_reg &dst,
84201e04c3fSmrg                        const src_reg &src,
84301e04c3fSmrg                        bool is_signed)
84401e04c3fSmrg{
84501e04c3fSmrg   vec4_instruction *inst;
84601e04c3fSmrg   src_reg temp = src;
84701e04c3fSmrg
84801e04c3fSmrg   if (is_signed) {
84901e04c3fSmrg      /* LZD of an absolute value source almost always does the right
85001e04c3fSmrg       * thing.  There are two problem values:
85101e04c3fSmrg       *
85201e04c3fSmrg       * * 0x80000000.  Since abs(0x80000000) == 0x80000000, LZD returns
85301e04c3fSmrg       *   0.  However, findMSB(int(0x80000000)) == 30.
85401e04c3fSmrg       *
85501e04c3fSmrg       * * 0xffffffff.  Since abs(0xffffffff) == 1, LZD returns
85601e04c3fSmrg       *   31.  Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
85701e04c3fSmrg       *
85801e04c3fSmrg       *    For a value of zero or negative one, -1 will be returned.
85901e04c3fSmrg       *
86001e04c3fSmrg       * * Negative powers of two.  LZD(abs(-(1<<x))) returns x, but
86101e04c3fSmrg       *   findMSB(-(1<<x)) should return x-1.
86201e04c3fSmrg       *
86301e04c3fSmrg       * For all negative number cases, including 0x80000000 and
86401e04c3fSmrg       * 0xffffffff, the correct value is obtained from LZD if instead of
86501e04c3fSmrg       * negating the (already negative) value the logical-not is used.  A
86601e04c3fSmrg       * conditonal logical-not can be achieved in two instructions.
86701e04c3fSmrg       */
86801e04c3fSmrg      temp = src_reg(bld.vgrf(BRW_REGISTER_TYPE_D));
86901e04c3fSmrg
87001e04c3fSmrg      bld.ASR(dst_reg(temp), src, brw_imm_d(31));
87101e04c3fSmrg      bld.XOR(dst_reg(temp), temp, src);
87201e04c3fSmrg   }
87301e04c3fSmrg
87401e04c3fSmrg   bld.LZD(retype(dst, BRW_REGISTER_TYPE_UD),
87501e04c3fSmrg           retype(temp, BRW_REGISTER_TYPE_UD));
87601e04c3fSmrg
87701e04c3fSmrg   /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
87801e04c3fSmrg    * from the LSB side. Subtract the result from 31 to convert the MSB count
87901e04c3fSmrg    * into an LSB count.  If no bits are set, LZD will return 32.  31-32 = -1,
88001e04c3fSmrg    * which is exactly what findMSB() is supposed to return.
88101e04c3fSmrg    */
88201e04c3fSmrg   inst = bld.ADD(dst, retype(src_reg(dst), BRW_REGISTER_TYPE_D),
88301e04c3fSmrg                  brw_imm_d(31));
88401e04c3fSmrg   inst->src[0].negate = true;
88501e04c3fSmrg}
88601e04c3fSmrg
88701e04c3fSmrgvoid
8887ec681f3Smrgvec4_visitor::emit_conversion_from_double(dst_reg dst, src_reg src)
88901e04c3fSmrg{
89001e04c3fSmrg   enum opcode op;
89101e04c3fSmrg   switch (dst.type) {
89201e04c3fSmrg   case BRW_REGISTER_TYPE_D:
89301e04c3fSmrg      op = VEC4_OPCODE_DOUBLE_TO_D32;
89401e04c3fSmrg      break;
89501e04c3fSmrg   case BRW_REGISTER_TYPE_UD:
89601e04c3fSmrg      op = VEC4_OPCODE_DOUBLE_TO_U32;
89701e04c3fSmrg      break;
89801e04c3fSmrg   case BRW_REGISTER_TYPE_F:
89901e04c3fSmrg      op = VEC4_OPCODE_DOUBLE_TO_F32;
90001e04c3fSmrg      break;
90101e04c3fSmrg   default:
90201e04c3fSmrg      unreachable("Unknown conversion");
90301e04c3fSmrg   }
90401e04c3fSmrg
90501e04c3fSmrg   dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
90601e04c3fSmrg   emit(MOV(temp, src));
90701e04c3fSmrg   dst_reg temp2 = dst_reg(this, glsl_type::dvec4_type);
90801e04c3fSmrg   emit(op, temp2, src_reg(temp));
90901e04c3fSmrg
91001e04c3fSmrg   emit(VEC4_OPCODE_PICK_LOW_32BIT, retype(temp2, dst.type), src_reg(temp2));
9117ec681f3Smrg   emit(MOV(dst, src_reg(retype(temp2, dst.type))));
91201e04c3fSmrg}
91301e04c3fSmrg
91401e04c3fSmrgvoid
9157ec681f3Smrgvec4_visitor::emit_conversion_to_double(dst_reg dst, src_reg src)
91601e04c3fSmrg{
91701e04c3fSmrg   dst_reg tmp_dst = dst_reg(src_reg(this, glsl_type::dvec4_type));
91801e04c3fSmrg   src_reg tmp_src = retype(src_reg(this, glsl_type::vec4_type), src.type);
91901e04c3fSmrg   emit(MOV(dst_reg(tmp_src), src));
92001e04c3fSmrg   emit(VEC4_OPCODE_TO_DOUBLE, tmp_dst, tmp_src);
9217ec681f3Smrg   emit(MOV(dst, src_reg(tmp_dst)));
92201e04c3fSmrg}
92301e04c3fSmrg
9249f464c52Smaya/**
9257ec681f3Smrg * Try to use an immediate value for a source
9269f464c52Smaya *
9279f464c52Smaya * In cases of flow control, constant propagation is sometimes unable to
9289f464c52Smaya * determine that a register contains a constant value.  To work around this,
9297ec681f3Smrg * try to emit a literal as one of the sources.  If \c try_src0_also is set,
9307ec681f3Smrg * \c op[0] will also be tried for an immediate value.
9317ec681f3Smrg *
9327ec681f3Smrg * If \c op[0] is modified, the operands will be exchanged so that \c op[1]
9337ec681f3Smrg * will always be the immediate value.
9347ec681f3Smrg *
9357ec681f3Smrg * \return The index of the source that was modified, 0 or 1, if successful.
9367ec681f3Smrg * Otherwise, -1.
9377ec681f3Smrg *
9387ec681f3Smrg * \param op - Operands to the instruction
9397ec681f3Smrg * \param try_src0_also - True if \c op[0] should also be a candidate for
9407ec681f3Smrg *                        getting an immediate value.  This should only be set
9417ec681f3Smrg *                        for commutative operations.
9429f464c52Smaya */
9437ec681f3Smrgstatic int
9449f464c52Smayatry_immediate_source(const nir_alu_instr *instr, src_reg *op,
9457ec681f3Smrg                     bool try_src0_also)
9469f464c52Smaya{
9477ec681f3Smrg   unsigned idx;
9489f464c52Smaya
9497ec681f3Smrg   /* MOV should be the only single-source instruction passed to this
9507ec681f3Smrg    * function.  Any other unary instruction with a constant source should
9517ec681f3Smrg    * have been constant-folded away!
9527ec681f3Smrg    */
9537ec681f3Smrg   assert(nir_op_infos[instr->op].num_inputs > 1 ||
9547ec681f3Smrg          instr->op == nir_op_mov);
9557ec681f3Smrg
9567ec681f3Smrg   if (instr->op != nir_op_mov &&
9577ec681f3Smrg       nir_src_bit_size(instr->src[1].src) == 32 &&
9587ec681f3Smrg       nir_src_is_const(instr->src[1].src)) {
9597ec681f3Smrg      idx = 1;
9607ec681f3Smrg   } else if (try_src0_also &&
9617ec681f3Smrg         nir_src_bit_size(instr->src[0].src) == 32 &&
9627ec681f3Smrg         nir_src_is_const(instr->src[0].src)) {
9637ec681f3Smrg      idx = 0;
9647ec681f3Smrg   } else {
9657ec681f3Smrg      return -1;
9667ec681f3Smrg   }
9677ec681f3Smrg
9687ec681f3Smrg   const enum brw_reg_type old_type = op[idx].type;
9699f464c52Smaya
9709f464c52Smaya   switch (old_type) {
9719f464c52Smaya   case BRW_REGISTER_TYPE_D:
9729f464c52Smaya   case BRW_REGISTER_TYPE_UD: {
9737ec681f3Smrg      int first_comp = -1;
9747ec681f3Smrg      int d = 0;
9757ec681f3Smrg
9767ec681f3Smrg      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
9777ec681f3Smrg         if (nir_alu_instr_channel_used(instr, idx, i)) {
9787ec681f3Smrg            if (first_comp < 0) {
9797ec681f3Smrg               first_comp = i;
9807ec681f3Smrg               d = nir_src_comp_as_int(instr->src[idx].src,
9817ec681f3Smrg                                       instr->src[idx].swizzle[i]);
9827ec681f3Smrg            } else if (d != nir_src_comp_as_int(instr->src[idx].src,
9837ec681f3Smrg                                                instr->src[idx].swizzle[i])) {
9847ec681f3Smrg               return -1;
9857ec681f3Smrg            }
9867ec681f3Smrg         }
9877ec681f3Smrg      }
9887ec681f3Smrg
9897ec681f3Smrg      assert(first_comp >= 0);
9909f464c52Smaya
9917ec681f3Smrg      if (op[idx].abs)
9929f464c52Smaya         d = MAX2(-d, d);
9939f464c52Smaya
9947ec681f3Smrg      if (op[idx].negate)
9959f464c52Smaya         d = -d;
9969f464c52Smaya
9977ec681f3Smrg      op[idx] = retype(src_reg(brw_imm_d(d)), old_type);
9989f464c52Smaya      break;
9999f464c52Smaya   }
10009f464c52Smaya
10019f464c52Smaya   case BRW_REGISTER_TYPE_F: {
10027ec681f3Smrg      int first_comp = -1;
10037ec681f3Smrg      float f[NIR_MAX_VEC_COMPONENTS] = { 0.0f };
10047ec681f3Smrg      bool is_scalar = true;
10057ec681f3Smrg
10067ec681f3Smrg      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
10077ec681f3Smrg         if (nir_alu_instr_channel_used(instr, idx, i)) {
10087ec681f3Smrg            f[i] = nir_src_comp_as_float(instr->src[idx].src,
10097ec681f3Smrg                                         instr->src[idx].swizzle[i]);
10107ec681f3Smrg            if (first_comp < 0) {
10117ec681f3Smrg               first_comp = i;
10127ec681f3Smrg            } else if (f[first_comp] != f[i]) {
10137ec681f3Smrg               is_scalar = false;
10147ec681f3Smrg            }
10157ec681f3Smrg         }
10167ec681f3Smrg      }
10179f464c52Smaya
10187ec681f3Smrg      if (is_scalar) {
10197ec681f3Smrg         if (op[idx].abs)
10207ec681f3Smrg            f[first_comp] = fabs(f[first_comp]);
10219f464c52Smaya
10227ec681f3Smrg         if (op[idx].negate)
10237ec681f3Smrg            f[first_comp] = -f[first_comp];
10249f464c52Smaya
10257ec681f3Smrg         op[idx] = src_reg(brw_imm_f(f[first_comp]));
10267ec681f3Smrg         assert(op[idx].type == old_type);
10277ec681f3Smrg      } else {
10287ec681f3Smrg         uint8_t vf_values[4] = { 0, 0, 0, 0 };
10297ec681f3Smrg
10307ec681f3Smrg         for (unsigned i = 0; i < ARRAY_SIZE(vf_values); i++) {
10317ec681f3Smrg
10327ec681f3Smrg            if (op[idx].abs)
10337ec681f3Smrg               f[i] = fabs(f[i]);
10347ec681f3Smrg
10357ec681f3Smrg            if (op[idx].negate)
10367ec681f3Smrg               f[i] = -f[i];
10377ec681f3Smrg
10387ec681f3Smrg            const int vf = brw_float_to_vf(f[i]);
10397ec681f3Smrg            if (vf == -1)
10407ec681f3Smrg               return -1;
10417ec681f3Smrg
10427ec681f3Smrg            vf_values[i] = vf;
10437ec681f3Smrg         }
10447ec681f3Smrg
10457ec681f3Smrg         op[idx] = src_reg(brw_imm_vf4(vf_values[0], vf_values[1],
10467ec681f3Smrg                                       vf_values[2], vf_values[3]));
10477ec681f3Smrg      }
10489f464c52Smaya      break;
10499f464c52Smaya   }
10509f464c52Smaya
10519f464c52Smaya   default:
10529f464c52Smaya      unreachable("Non-32bit type.");
10539f464c52Smaya   }
10547ec681f3Smrg
10557ec681f3Smrg   /* If the instruction has more than one source, the instruction format only
10567ec681f3Smrg    * allows source 1 to be an immediate value.  If the immediate value was
10577ec681f3Smrg    * source 0, then the sources must be exchanged.
10587ec681f3Smrg    */
10597ec681f3Smrg   if (idx == 0 && instr->op != nir_op_mov) {
10607ec681f3Smrg      src_reg tmp = op[0];
10617ec681f3Smrg      op[0] = op[1];
10627ec681f3Smrg      op[1] = tmp;
10637ec681f3Smrg   }
10647ec681f3Smrg
10657ec681f3Smrg   return idx;
10667ec681f3Smrg}
10677ec681f3Smrg
10687ec681f3Smrgvoid
10697ec681f3Smrgvec4_visitor::fix_float_operands(src_reg op[3], nir_alu_instr *instr)
10707ec681f3Smrg{
10717ec681f3Smrg   bool fixed[3] = { false, false, false };
10727ec681f3Smrg
10737ec681f3Smrg   for (unsigned i = 0; i < 2; i++) {
10747ec681f3Smrg      if (!nir_src_is_const(instr->src[i].src))
10757ec681f3Smrg         continue;
10767ec681f3Smrg
10777ec681f3Smrg      for (unsigned j = i + 1; j < 3; j++) {
10787ec681f3Smrg         if (fixed[j])
10797ec681f3Smrg            continue;
10807ec681f3Smrg
10817ec681f3Smrg         if (!nir_src_is_const(instr->src[j].src))
10827ec681f3Smrg            continue;
10837ec681f3Smrg
10847ec681f3Smrg         if (nir_alu_srcs_equal(instr, instr, i, j)) {
10857ec681f3Smrg            if (!fixed[i])
10867ec681f3Smrg               op[i] = fix_3src_operand(op[i]);
10877ec681f3Smrg
10887ec681f3Smrg            op[j] = op[i];
10897ec681f3Smrg
10907ec681f3Smrg            fixed[i] = true;
10917ec681f3Smrg            fixed[j] = true;
10927ec681f3Smrg         } else if (nir_alu_srcs_negative_equal(instr, instr, i, j)) {
10937ec681f3Smrg            if (!fixed[i])
10947ec681f3Smrg               op[i] = fix_3src_operand(op[i]);
10957ec681f3Smrg
10967ec681f3Smrg            op[j] = op[i];
10977ec681f3Smrg            op[j].negate = !op[j].negate;
10987ec681f3Smrg
10997ec681f3Smrg            fixed[i] = true;
11007ec681f3Smrg            fixed[j] = true;
11017ec681f3Smrg         }
11027ec681f3Smrg      }
11037ec681f3Smrg   }
11047ec681f3Smrg
11057ec681f3Smrg   for (unsigned i = 0; i < 3; i++) {
11067ec681f3Smrg      if (!fixed[i])
11077ec681f3Smrg         op[i] = fix_3src_operand(op[i]);
11087ec681f3Smrg   }
11097ec681f3Smrg}
11107ec681f3Smrg
11117ec681f3Smrgstatic bool
11127ec681f3Smrgconst_src_fits_in_16_bits(const nir_src &src, brw_reg_type type)
11137ec681f3Smrg{
11147ec681f3Smrg   assert(nir_src_is_const(src));
11157ec681f3Smrg   if (brw_reg_type_is_unsigned_integer(type)) {
11167ec681f3Smrg      return nir_src_comp_as_uint(src, 0) <= UINT16_MAX;
11177ec681f3Smrg   } else {
11187ec681f3Smrg      const int64_t c = nir_src_comp_as_int(src, 0);
11197ec681f3Smrg      return c <= INT16_MAX && c >= INT16_MIN;
11207ec681f3Smrg   }
11219f464c52Smaya}
11229f464c52Smaya
112301e04c3fSmrgvoid
112401e04c3fSmrgvec4_visitor::nir_emit_alu(nir_alu_instr *instr)
112501e04c3fSmrg{
112601e04c3fSmrg   vec4_instruction *inst;
112701e04c3fSmrg
112801e04c3fSmrg   nir_alu_type dst_type = (nir_alu_type) (nir_op_infos[instr->op].output_type |
112901e04c3fSmrg                                           nir_dest_bit_size(instr->dest.dest));
113001e04c3fSmrg   dst_reg dst = get_nir_dest(instr->dest.dest, dst_type);
113101e04c3fSmrg   dst.writemask = instr->dest.write_mask;
113201e04c3fSmrg
11337ec681f3Smrg   assert(!instr->dest.saturate);
11347ec681f3Smrg
113501e04c3fSmrg   src_reg op[4];
113601e04c3fSmrg   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
11377ec681f3Smrg      /* We don't lower to source modifiers, so they shouldn't exist. */
11387ec681f3Smrg      assert(!instr->src[i].abs);
11397ec681f3Smrg      assert(!instr->src[i].negate);
11407ec681f3Smrg
114101e04c3fSmrg      nir_alu_type src_type = (nir_alu_type)
114201e04c3fSmrg         (nir_op_infos[instr->op].input_types[i] |
114301e04c3fSmrg          nir_src_bit_size(instr->src[i].src));
114401e04c3fSmrg      op[i] = get_nir_src(instr->src[i].src, src_type, 4);
114501e04c3fSmrg      op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
114601e04c3fSmrg   }
114701e04c3fSmrg
11487ec681f3Smrg#ifndef NDEBUG
11497ec681f3Smrg   /* On Gen7 and earlier, no functionality is exposed that should allow 8-bit
11507ec681f3Smrg    * integer types to ever exist.
11517ec681f3Smrg    */
11527ec681f3Smrg   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
11537ec681f3Smrg      assert(type_sz(op[i].type) > 1);
11547ec681f3Smrg#endif
11557ec681f3Smrg
115601e04c3fSmrg   switch (instr->op) {
11577ec681f3Smrg   case nir_op_mov:
11587ec681f3Smrg      try_immediate_source(instr, &op[0], true);
115901e04c3fSmrg      inst = emit(MOV(dst, op[0]));
116001e04c3fSmrg      break;
116101e04c3fSmrg
116201e04c3fSmrg   case nir_op_vec2:
116301e04c3fSmrg   case nir_op_vec3:
116401e04c3fSmrg   case nir_op_vec4:
116501e04c3fSmrg      unreachable("not reached: should be handled by lower_vec_to_movs()");
116601e04c3fSmrg
116701e04c3fSmrg   case nir_op_i2f32:
116801e04c3fSmrg   case nir_op_u2f32:
116901e04c3fSmrg      inst = emit(MOV(dst, op[0]));
117001e04c3fSmrg      break;
117101e04c3fSmrg
117201e04c3fSmrg   case nir_op_f2f32:
117301e04c3fSmrg   case nir_op_f2i32:
117401e04c3fSmrg   case nir_op_f2u32:
117501e04c3fSmrg      if (nir_src_bit_size(instr->src[0].src) == 64)
11767ec681f3Smrg         emit_conversion_from_double(dst, op[0]);
117701e04c3fSmrg      else
117801e04c3fSmrg         inst = emit(MOV(dst, op[0]));
117901e04c3fSmrg      break;
118001e04c3fSmrg
118101e04c3fSmrg   case nir_op_f2f64:
118201e04c3fSmrg   case nir_op_i2f64:
118301e04c3fSmrg   case nir_op_u2f64:
11847ec681f3Smrg      emit_conversion_to_double(dst, op[0]);
11857ec681f3Smrg      break;
11867ec681f3Smrg
11877ec681f3Smrg   case nir_op_fsat:
11887ec681f3Smrg      inst = emit(MOV(dst, op[0]));
11897ec681f3Smrg      inst->saturate = true;
11907ec681f3Smrg      break;
11917ec681f3Smrg
11927ec681f3Smrg   case nir_op_fneg:
11937ec681f3Smrg   case nir_op_ineg:
11947ec681f3Smrg      op[0].negate = true;
11957ec681f3Smrg      inst = emit(MOV(dst, op[0]));
11967ec681f3Smrg      break;
11977ec681f3Smrg
11987ec681f3Smrg   case nir_op_fabs:
11997ec681f3Smrg   case nir_op_iabs:
12007ec681f3Smrg      op[0].negate = false;
12017ec681f3Smrg      op[0].abs = true;
12027ec681f3Smrg      inst = emit(MOV(dst, op[0]));
120301e04c3fSmrg      break;
120401e04c3fSmrg
120501e04c3fSmrg   case nir_op_iadd:
120601e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
12077ec681f3Smrg      FALLTHROUGH;
120801e04c3fSmrg   case nir_op_fadd:
12097ec681f3Smrg      try_immediate_source(instr, op, true);
121001e04c3fSmrg      inst = emit(ADD(dst, op[0], op[1]));
121101e04c3fSmrg      break;
121201e04c3fSmrg
12139f464c52Smaya   case nir_op_uadd_sat:
12149f464c52Smaya      assert(nir_dest_bit_size(instr->dest.dest) < 64);
12159f464c52Smaya      inst = emit(ADD(dst, op[0], op[1]));
12169f464c52Smaya      inst->saturate = true;
12179f464c52Smaya      break;
12189f464c52Smaya
121901e04c3fSmrg   case nir_op_fmul:
12207ec681f3Smrg      try_immediate_source(instr, op, true);
122101e04c3fSmrg      inst = emit(MUL(dst, op[0], op[1]));
122201e04c3fSmrg      break;
122301e04c3fSmrg
122401e04c3fSmrg   case nir_op_imul: {
122501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
12267ec681f3Smrg
12277ec681f3Smrg      /* For integer multiplication, the MUL uses the low 16 bits of one of
12287ec681f3Smrg       * the operands (src0 through SNB, src1 on IVB and later). The MACH
12297ec681f3Smrg       * accumulates in the contribution of the upper 16 bits of that
12307ec681f3Smrg       * operand. If we can determine that one of the args is in the low
12317ec681f3Smrg       * 16 bits, though, we can just emit a single MUL.
12327ec681f3Smrg       */
12337ec681f3Smrg      if (nir_src_is_const(instr->src[0].src) &&
12347ec681f3Smrg          nir_alu_instr_src_read_mask(instr, 0) == 1 &&
12357ec681f3Smrg          const_src_fits_in_16_bits(instr->src[0].src, op[0].type)) {
12367ec681f3Smrg         if (devinfo->ver < 7)
12377ec681f3Smrg            emit(MUL(dst, op[0], op[1]));
12387ec681f3Smrg         else
12397ec681f3Smrg            emit(MUL(dst, op[1], op[0]));
12407ec681f3Smrg      } else if (nir_src_is_const(instr->src[1].src) &&
12417ec681f3Smrg                 nir_alu_instr_src_read_mask(instr, 1) == 1 &&
12427ec681f3Smrg                 const_src_fits_in_16_bits(instr->src[1].src, op[1].type)) {
12437ec681f3Smrg         if (devinfo->ver < 7)
12447ec681f3Smrg            emit(MUL(dst, op[1], op[0]));
12457ec681f3Smrg         else
12467ec681f3Smrg            emit(MUL(dst, op[0], op[1]));
124701e04c3fSmrg      } else {
12487ec681f3Smrg         struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
12497ec681f3Smrg
12507ec681f3Smrg         emit(MUL(acc, op[0], op[1]));
12517ec681f3Smrg         emit(MACH(dst_null_d(), op[0], op[1]));
12527ec681f3Smrg         emit(MOV(dst, src_reg(acc)));
125301e04c3fSmrg      }
125401e04c3fSmrg      break;
125501e04c3fSmrg   }
125601e04c3fSmrg
125701e04c3fSmrg   case nir_op_imul_high:
125801e04c3fSmrg   case nir_op_umul_high: {
125901e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
126001e04c3fSmrg      struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
126101e04c3fSmrg
12627ec681f3Smrg      emit(MUL(acc, op[0], op[1]));
126301e04c3fSmrg      emit(MACH(dst, op[0], op[1]));
126401e04c3fSmrg      break;
126501e04c3fSmrg   }
126601e04c3fSmrg
126701e04c3fSmrg   case nir_op_frcp:
126801e04c3fSmrg      inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
126901e04c3fSmrg      break;
127001e04c3fSmrg
127101e04c3fSmrg   case nir_op_fexp2:
127201e04c3fSmrg      inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
127301e04c3fSmrg      break;
127401e04c3fSmrg
127501e04c3fSmrg   case nir_op_flog2:
127601e04c3fSmrg      inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
127701e04c3fSmrg      break;
127801e04c3fSmrg
127901e04c3fSmrg   case nir_op_fsin:
128001e04c3fSmrg      inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
128101e04c3fSmrg      break;
128201e04c3fSmrg
128301e04c3fSmrg   case nir_op_fcos:
128401e04c3fSmrg      inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
128501e04c3fSmrg      break;
128601e04c3fSmrg
128701e04c3fSmrg   case nir_op_idiv:
128801e04c3fSmrg   case nir_op_udiv:
128901e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
129001e04c3fSmrg      emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
129101e04c3fSmrg      break;
129201e04c3fSmrg
129301e04c3fSmrg   case nir_op_umod:
129401e04c3fSmrg   case nir_op_irem:
129501e04c3fSmrg      /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
129601e04c3fSmrg       * appears that our hardware just does the right thing for signed
129701e04c3fSmrg       * remainder.
129801e04c3fSmrg       */
129901e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
130001e04c3fSmrg      emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
130101e04c3fSmrg      break;
130201e04c3fSmrg
130301e04c3fSmrg   case nir_op_imod: {
130401e04c3fSmrg      /* Get a regular C-style remainder.  If a % b == 0, set the predicate. */
130501e04c3fSmrg      inst = emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
130601e04c3fSmrg
130701e04c3fSmrg      /* Math instructions don't support conditional mod */
130801e04c3fSmrg      inst = emit(MOV(dst_null_d(), src_reg(dst)));
130901e04c3fSmrg      inst->conditional_mod = BRW_CONDITIONAL_NZ;
131001e04c3fSmrg
131101e04c3fSmrg      /* Now, we need to determine if signs of the sources are different.
131201e04c3fSmrg       * When we XOR the sources, the top bit is 0 if they are the same and 1
131301e04c3fSmrg       * if they are different.  We can then use a conditional modifier to
131401e04c3fSmrg       * turn that into a predicate.  This leads us to an XOR.l instruction.
131501e04c3fSmrg       *
131601e04c3fSmrg       * Technically, according to the PRM, you're not allowed to use .l on a
131701e04c3fSmrg       * XOR instruction.  However, emperical experiments and Curro's reading
131801e04c3fSmrg       * of the simulator source both indicate that it's safe.
131901e04c3fSmrg       */
132001e04c3fSmrg      src_reg tmp = src_reg(this, glsl_type::ivec4_type);
132101e04c3fSmrg      inst = emit(XOR(dst_reg(tmp), op[0], op[1]));
132201e04c3fSmrg      inst->predicate = BRW_PREDICATE_NORMAL;
132301e04c3fSmrg      inst->conditional_mod = BRW_CONDITIONAL_L;
132401e04c3fSmrg
132501e04c3fSmrg      /* If the result of the initial remainder operation is non-zero and the
132601e04c3fSmrg       * two sources have different signs, add in a copy of op[1] to get the
132701e04c3fSmrg       * final integer modulus value.
132801e04c3fSmrg       */
132901e04c3fSmrg      inst = emit(ADD(dst, src_reg(dst), op[1]));
133001e04c3fSmrg      inst->predicate = BRW_PREDICATE_NORMAL;
133101e04c3fSmrg      break;
133201e04c3fSmrg   }
133301e04c3fSmrg
133401e04c3fSmrg   case nir_op_ldexp:
133501e04c3fSmrg      unreachable("not reached: should be handled by ldexp_to_arith()");
133601e04c3fSmrg
133701e04c3fSmrg   case nir_op_fsqrt:
133801e04c3fSmrg      inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
133901e04c3fSmrg      break;
134001e04c3fSmrg
134101e04c3fSmrg   case nir_op_frsq:
134201e04c3fSmrg      inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
134301e04c3fSmrg      break;
134401e04c3fSmrg
134501e04c3fSmrg   case nir_op_fpow:
134601e04c3fSmrg      inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
134701e04c3fSmrg      break;
134801e04c3fSmrg
134901e04c3fSmrg   case nir_op_uadd_carry: {
135001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
135101e04c3fSmrg      struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
135201e04c3fSmrg
135301e04c3fSmrg      emit(ADDC(dst_null_ud(), op[0], op[1]));
135401e04c3fSmrg      emit(MOV(dst, src_reg(acc)));
135501e04c3fSmrg      break;
135601e04c3fSmrg   }
135701e04c3fSmrg
135801e04c3fSmrg   case nir_op_usub_borrow: {
135901e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
136001e04c3fSmrg      struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
136101e04c3fSmrg
136201e04c3fSmrg      emit(SUBB(dst_null_ud(), op[0], op[1]));
136301e04c3fSmrg      emit(MOV(dst, src_reg(acc)));
136401e04c3fSmrg      break;
136501e04c3fSmrg   }
136601e04c3fSmrg
136701e04c3fSmrg   case nir_op_ftrunc:
136801e04c3fSmrg      inst = emit(RNDZ(dst, op[0]));
13697ec681f3Smrg      if (devinfo->ver < 6) {
13707ec681f3Smrg         inst->conditional_mod = BRW_CONDITIONAL_R;
13717ec681f3Smrg         inst = emit(ADD(dst, src_reg(dst), brw_imm_f(1.0f)));
13727ec681f3Smrg         inst->predicate = BRW_PREDICATE_NORMAL;
13737ec681f3Smrg         inst = emit(MOV(dst, src_reg(dst))); /* for potential saturation */
13747ec681f3Smrg      }
137501e04c3fSmrg      break;
137601e04c3fSmrg
137701e04c3fSmrg   case nir_op_fceil: {
137801e04c3fSmrg      src_reg tmp = src_reg(this, glsl_type::float_type);
137901e04c3fSmrg      tmp.swizzle =
138001e04c3fSmrg         brw_swizzle_for_size(instr->src[0].src.is_ssa ?
138101e04c3fSmrg                              instr->src[0].src.ssa->num_components :
138201e04c3fSmrg                              instr->src[0].src.reg.reg->num_components);
138301e04c3fSmrg
138401e04c3fSmrg      op[0].negate = !op[0].negate;
138501e04c3fSmrg      emit(RNDD(dst_reg(tmp), op[0]));
138601e04c3fSmrg      tmp.negate = true;
138701e04c3fSmrg      inst = emit(MOV(dst, tmp));
138801e04c3fSmrg      break;
138901e04c3fSmrg   }
139001e04c3fSmrg
139101e04c3fSmrg   case nir_op_ffloor:
139201e04c3fSmrg      inst = emit(RNDD(dst, op[0]));
139301e04c3fSmrg      break;
139401e04c3fSmrg
139501e04c3fSmrg   case nir_op_ffract:
139601e04c3fSmrg      inst = emit(FRC(dst, op[0]));
139701e04c3fSmrg      break;
139801e04c3fSmrg
139901e04c3fSmrg   case nir_op_fround_even:
140001e04c3fSmrg      inst = emit(RNDE(dst, op[0]));
14017ec681f3Smrg      if (devinfo->ver < 6) {
14027ec681f3Smrg         inst->conditional_mod = BRW_CONDITIONAL_R;
14037ec681f3Smrg         inst = emit(ADD(dst, src_reg(dst), brw_imm_f(1.0f)));
14047ec681f3Smrg         inst->predicate = BRW_PREDICATE_NORMAL;
14057ec681f3Smrg         inst = emit(MOV(dst, src_reg(dst))); /* for potential saturation */
14067ec681f3Smrg      }
140701e04c3fSmrg      break;
140801e04c3fSmrg
140901e04c3fSmrg   case nir_op_fquantize2f16: {
141001e04c3fSmrg      /* See also vec4_visitor::emit_pack_half_2x16() */
141101e04c3fSmrg      src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
141201e04c3fSmrg      src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
141301e04c3fSmrg      src_reg zero = src_reg(this, glsl_type::vec4_type);
141401e04c3fSmrg
141501e04c3fSmrg      /* Check for denormal */
141601e04c3fSmrg      src_reg abs_src0 = op[0];
141701e04c3fSmrg      abs_src0.abs = true;
141801e04c3fSmrg      emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
141901e04c3fSmrg               BRW_CONDITIONAL_L));
142001e04c3fSmrg      /* Get the appropriately signed zero */
142101e04c3fSmrg      emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
142201e04c3fSmrg               retype(op[0], BRW_REGISTER_TYPE_UD),
142301e04c3fSmrg               brw_imm_ud(0x80000000)));
142401e04c3fSmrg      /* Do the actual F32 -> F16 -> F32 conversion */
142501e04c3fSmrg      emit(F32TO16(dst_reg(tmp16), op[0]));
142601e04c3fSmrg      emit(F16TO32(dst_reg(tmp32), tmp16));
142701e04c3fSmrg      /* Select that or zero based on normal status */
142801e04c3fSmrg      inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
142901e04c3fSmrg      inst->predicate = BRW_PREDICATE_NORMAL;
143001e04c3fSmrg      break;
143101e04c3fSmrg   }
143201e04c3fSmrg
143301e04c3fSmrg   case nir_op_imin:
143401e04c3fSmrg   case nir_op_umin:
143501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
14367ec681f3Smrg      FALLTHROUGH;
143701e04c3fSmrg   case nir_op_fmin:
14387ec681f3Smrg      try_immediate_source(instr, op, true);
143901e04c3fSmrg      inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
144001e04c3fSmrg      break;
144101e04c3fSmrg
144201e04c3fSmrg   case nir_op_imax:
144301e04c3fSmrg   case nir_op_umax:
144401e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
14457ec681f3Smrg      FALLTHROUGH;
144601e04c3fSmrg   case nir_op_fmax:
14477ec681f3Smrg      try_immediate_source(instr, op, true);
144801e04c3fSmrg      inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
144901e04c3fSmrg      break;
145001e04c3fSmrg
145101e04c3fSmrg   case nir_op_fddx:
145201e04c3fSmrg   case nir_op_fddx_coarse:
145301e04c3fSmrg   case nir_op_fddx_fine:
145401e04c3fSmrg   case nir_op_fddy:
145501e04c3fSmrg   case nir_op_fddy_coarse:
145601e04c3fSmrg   case nir_op_fddy_fine:
145701e04c3fSmrg      unreachable("derivatives are not valid in vertex shaders");
145801e04c3fSmrg
14599f464c52Smaya   case nir_op_ilt32:
14609f464c52Smaya   case nir_op_ult32:
14619f464c52Smaya   case nir_op_ige32:
14629f464c52Smaya   case nir_op_uge32:
14639f464c52Smaya   case nir_op_ieq32:
14649f464c52Smaya   case nir_op_ine32:
146501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
14667ec681f3Smrg      FALLTHROUGH;
14679f464c52Smaya   case nir_op_flt32:
14689f464c52Smaya   case nir_op_fge32:
14699f464c52Smaya   case nir_op_feq32:
14707ec681f3Smrg   case nir_op_fneu32: {
147101e04c3fSmrg      enum brw_conditional_mod conditional_mod =
14727ec681f3Smrg         brw_cmod_for_nir_comparison(instr->op);
147301e04c3fSmrg
147401e04c3fSmrg      if (nir_src_bit_size(instr->src[0].src) < 64) {
14757ec681f3Smrg         /* If the order of the sources is changed due to an immediate value,
14767ec681f3Smrg          * then the condition must also be changed.
14777ec681f3Smrg          */
14787ec681f3Smrg         if (try_immediate_source(instr, op, true) == 0)
14797ec681f3Smrg            conditional_mod = brw_swap_cmod(conditional_mod);
14807ec681f3Smrg
148101e04c3fSmrg         emit(CMP(dst, op[0], op[1], conditional_mod));
148201e04c3fSmrg      } else {
148301e04c3fSmrg         /* Produce a 32-bit boolean result from the DF comparison by selecting
148401e04c3fSmrg          * only the low 32-bit in each DF produced. Do this in a temporary
148501e04c3fSmrg          * so we can then move from there to the result using align16 again
148601e04c3fSmrg          * to honor the original writemask.
148701e04c3fSmrg          */
148801e04c3fSmrg         dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
148901e04c3fSmrg         emit(CMP(temp, op[0], op[1], conditional_mod));
149001e04c3fSmrg         dst_reg result = dst_reg(this, glsl_type::bvec4_type);
149101e04c3fSmrg         emit(VEC4_OPCODE_PICK_LOW_32BIT, result, src_reg(temp));
149201e04c3fSmrg         emit(MOV(dst, src_reg(result)));
149301e04c3fSmrg      }
149401e04c3fSmrg      break;
149501e04c3fSmrg   }
149601e04c3fSmrg
14979f464c52Smaya   case nir_op_b32all_iequal2:
14989f464c52Smaya   case nir_op_b32all_iequal3:
14999f464c52Smaya   case nir_op_b32all_iequal4:
150001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
15017ec681f3Smrg      FALLTHROUGH;
15029f464c52Smaya   case nir_op_b32all_fequal2:
15039f464c52Smaya   case nir_op_b32all_fequal3:
15049f464c52Smaya   case nir_op_b32all_fequal4: {
150501e04c3fSmrg      unsigned swiz =
150601e04c3fSmrg         brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
150701e04c3fSmrg
150801e04c3fSmrg      emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
15097ec681f3Smrg               brw_cmod_for_nir_comparison(instr->op)));
151001e04c3fSmrg      emit(MOV(dst, brw_imm_d(0)));
151101e04c3fSmrg      inst = emit(MOV(dst, brw_imm_d(~0)));
151201e04c3fSmrg      inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
151301e04c3fSmrg      break;
151401e04c3fSmrg   }
151501e04c3fSmrg
15169f464c52Smaya   case nir_op_b32any_inequal2:
15179f464c52Smaya   case nir_op_b32any_inequal3:
15189f464c52Smaya   case nir_op_b32any_inequal4:
151901e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
15207ec681f3Smrg      FALLTHROUGH;
15219f464c52Smaya   case nir_op_b32any_fnequal2:
15229f464c52Smaya   case nir_op_b32any_fnequal3:
15239f464c52Smaya   case nir_op_b32any_fnequal4: {
152401e04c3fSmrg      unsigned swiz =
152501e04c3fSmrg         brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
152601e04c3fSmrg
152701e04c3fSmrg      emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
15287ec681f3Smrg               brw_cmod_for_nir_comparison(instr->op)));
152901e04c3fSmrg
153001e04c3fSmrg      emit(MOV(dst, brw_imm_d(0)));
153101e04c3fSmrg      inst = emit(MOV(dst, brw_imm_d(~0)));
153201e04c3fSmrg      inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
153301e04c3fSmrg      break;
153401e04c3fSmrg   }
153501e04c3fSmrg
153601e04c3fSmrg   case nir_op_inot:
153701e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
153801e04c3fSmrg      emit(NOT(dst, op[0]));
153901e04c3fSmrg      break;
154001e04c3fSmrg
154101e04c3fSmrg   case nir_op_ixor:
154201e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
15437ec681f3Smrg      try_immediate_source(instr, op, true);
154401e04c3fSmrg      emit(XOR(dst, op[0], op[1]));
154501e04c3fSmrg      break;
154601e04c3fSmrg
154701e04c3fSmrg   case nir_op_ior:
154801e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
15497ec681f3Smrg      try_immediate_source(instr, op, true);
155001e04c3fSmrg      emit(OR(dst, op[0], op[1]));
155101e04c3fSmrg      break;
155201e04c3fSmrg
155301e04c3fSmrg   case nir_op_iand:
155401e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
15557ec681f3Smrg      try_immediate_source(instr, op, true);
155601e04c3fSmrg      emit(AND(dst, op[0], op[1]));
155701e04c3fSmrg      break;
155801e04c3fSmrg
15599f464c52Smaya   case nir_op_b2i32:
15609f464c52Smaya   case nir_op_b2f32:
15619f464c52Smaya   case nir_op_b2f64:
156201e04c3fSmrg      if (nir_dest_bit_size(instr->dest.dest) > 32) {
156301e04c3fSmrg         assert(dst.type == BRW_REGISTER_TYPE_DF);
15647ec681f3Smrg         emit_conversion_to_double(dst, negate(op[0]));
156501e04c3fSmrg      } else {
156601e04c3fSmrg         emit(MOV(dst, negate(op[0])));
156701e04c3fSmrg      }
156801e04c3fSmrg      break;
156901e04c3fSmrg
15709f464c52Smaya   case nir_op_f2b32:
157101e04c3fSmrg      if (nir_src_bit_size(instr->src[0].src) == 64) {
157201e04c3fSmrg         /* We use a MOV with conditional_mod to check if the provided value is
157301e04c3fSmrg          * 0.0. We want this to flush denormalized numbers to zero, so we set a
157401e04c3fSmrg          * source modifier on the source operand to trigger this, as source
157501e04c3fSmrg          * modifiers don't affect the result of the testing against 0.0.
157601e04c3fSmrg          */
157701e04c3fSmrg         src_reg value = op[0];
157801e04c3fSmrg         value.abs = true;
157901e04c3fSmrg         vec4_instruction *inst = emit(MOV(dst_null_df(), value));
158001e04c3fSmrg         inst->conditional_mod = BRW_CONDITIONAL_NZ;
158101e04c3fSmrg
158201e04c3fSmrg         src_reg one = src_reg(this, glsl_type::ivec4_type);
158301e04c3fSmrg         emit(MOV(dst_reg(one), brw_imm_d(~0)));
158401e04c3fSmrg         inst = emit(BRW_OPCODE_SEL, dst, one, brw_imm_d(0));
158501e04c3fSmrg         inst->predicate = BRW_PREDICATE_NORMAL;
158601e04c3fSmrg      } else {
158701e04c3fSmrg         emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
158801e04c3fSmrg      }
158901e04c3fSmrg      break;
159001e04c3fSmrg
15919f464c52Smaya   case nir_op_i2b32:
159201e04c3fSmrg      emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
159301e04c3fSmrg      break;
159401e04c3fSmrg
159501e04c3fSmrg   case nir_op_unpack_half_2x16_split_x:
159601e04c3fSmrg   case nir_op_unpack_half_2x16_split_y:
159701e04c3fSmrg   case nir_op_pack_half_2x16_split:
159801e04c3fSmrg      unreachable("not reached: should not occur in vertex shader");
159901e04c3fSmrg
160001e04c3fSmrg   case nir_op_unpack_snorm_2x16:
160101e04c3fSmrg   case nir_op_unpack_unorm_2x16:
160201e04c3fSmrg   case nir_op_pack_snorm_2x16:
160301e04c3fSmrg   case nir_op_pack_unorm_2x16:
160401e04c3fSmrg      unreachable("not reached: should be handled by lower_packing_builtins");
160501e04c3fSmrg
160601e04c3fSmrg   case nir_op_pack_uvec4_to_uint:
160701e04c3fSmrg      unreachable("not reached");
160801e04c3fSmrg
160901e04c3fSmrg   case nir_op_pack_uvec2_to_uint: {
161001e04c3fSmrg      dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
161101e04c3fSmrg      tmp1.writemask = WRITEMASK_X;
161201e04c3fSmrg      op[0].swizzle = BRW_SWIZZLE_YYYY;
161301e04c3fSmrg      emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
161401e04c3fSmrg
161501e04c3fSmrg      dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
161601e04c3fSmrg      tmp2.writemask = WRITEMASK_X;
161701e04c3fSmrg      op[0].swizzle = BRW_SWIZZLE_XXXX;
161801e04c3fSmrg      emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
161901e04c3fSmrg
162001e04c3fSmrg      emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
162101e04c3fSmrg      break;
162201e04c3fSmrg   }
162301e04c3fSmrg
162401e04c3fSmrg   case nir_op_pack_64_2x32_split: {
162501e04c3fSmrg      dst_reg result = dst_reg(this, glsl_type::dvec4_type);
162601e04c3fSmrg      dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
162701e04c3fSmrg      emit(MOV(tmp, retype(op[0], BRW_REGISTER_TYPE_UD)));
162801e04c3fSmrg      emit(VEC4_OPCODE_SET_LOW_32BIT, result, src_reg(tmp));
162901e04c3fSmrg      emit(MOV(tmp, retype(op[1], BRW_REGISTER_TYPE_UD)));
163001e04c3fSmrg      emit(VEC4_OPCODE_SET_HIGH_32BIT, result, src_reg(tmp));
163101e04c3fSmrg      emit(MOV(dst, src_reg(result)));
163201e04c3fSmrg      break;
163301e04c3fSmrg   }
163401e04c3fSmrg
163501e04c3fSmrg   case nir_op_unpack_64_2x32_split_x:
163601e04c3fSmrg   case nir_op_unpack_64_2x32_split_y: {
163701e04c3fSmrg      enum opcode oper = (instr->op == nir_op_unpack_64_2x32_split_x) ?
163801e04c3fSmrg         VEC4_OPCODE_PICK_LOW_32BIT : VEC4_OPCODE_PICK_HIGH_32BIT;
163901e04c3fSmrg      dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
164001e04c3fSmrg      emit(MOV(tmp, op[0]));
164101e04c3fSmrg      dst_reg tmp2 = dst_reg(this, glsl_type::uvec4_type);
164201e04c3fSmrg      emit(oper, tmp2, src_reg(tmp));
164301e04c3fSmrg      emit(MOV(dst, src_reg(tmp2)));
164401e04c3fSmrg      break;
164501e04c3fSmrg   }
164601e04c3fSmrg
164701e04c3fSmrg   case nir_op_unpack_half_2x16:
164801e04c3fSmrg      /* As NIR does not guarantee that we have a correct swizzle outside the
164901e04c3fSmrg       * boundaries of a vector, and the implementation of emit_unpack_half_2x16
165001e04c3fSmrg       * uses the source operand in an operation with WRITEMASK_Y while our
165101e04c3fSmrg       * source operand has only size 1, it accessed incorrect data producing
165201e04c3fSmrg       * regressions in Piglit. We repeat the swizzle of the first component on the
165301e04c3fSmrg       * rest of components to avoid regressions. In the vec4_visitor IR code path
165401e04c3fSmrg       * this is not needed because the operand has already the correct swizzle.
165501e04c3fSmrg       */
165601e04c3fSmrg      op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
165701e04c3fSmrg      emit_unpack_half_2x16(dst, op[0]);
165801e04c3fSmrg      break;
165901e04c3fSmrg
166001e04c3fSmrg   case nir_op_pack_half_2x16:
166101e04c3fSmrg      emit_pack_half_2x16(dst, op[0]);
166201e04c3fSmrg      break;
166301e04c3fSmrg
166401e04c3fSmrg   case nir_op_unpack_unorm_4x8:
166501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
166601e04c3fSmrg      emit_unpack_unorm_4x8(dst, op[0]);
166701e04c3fSmrg      break;
166801e04c3fSmrg
166901e04c3fSmrg   case nir_op_pack_unorm_4x8:
167001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
167101e04c3fSmrg      emit_pack_unorm_4x8(dst, op[0]);
167201e04c3fSmrg      break;
167301e04c3fSmrg
167401e04c3fSmrg   case nir_op_unpack_snorm_4x8:
167501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
167601e04c3fSmrg      emit_unpack_snorm_4x8(dst, op[0]);
167701e04c3fSmrg      break;
167801e04c3fSmrg
167901e04c3fSmrg   case nir_op_pack_snorm_4x8:
168001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
168101e04c3fSmrg      emit_pack_snorm_4x8(dst, op[0]);
168201e04c3fSmrg      break;
168301e04c3fSmrg
168401e04c3fSmrg   case nir_op_bitfield_reverse:
168501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
168601e04c3fSmrg      emit(BFREV(dst, op[0]));
168701e04c3fSmrg      break;
168801e04c3fSmrg
168901e04c3fSmrg   case nir_op_bit_count:
169001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
169101e04c3fSmrg      emit(CBIT(dst, op[0]));
169201e04c3fSmrg      break;
169301e04c3fSmrg
169401e04c3fSmrg   case nir_op_ufind_msb:
169501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
169601e04c3fSmrg      emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst, op[0], false);
169701e04c3fSmrg      break;
169801e04c3fSmrg
169901e04c3fSmrg   case nir_op_ifind_msb: {
170001e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
170101e04c3fSmrg      vec4_builder bld = vec4_builder(this).at_end();
170201e04c3fSmrg      src_reg src(dst);
170301e04c3fSmrg
17047ec681f3Smrg      if (devinfo->ver < 7) {
170501e04c3fSmrg         emit_find_msb_using_lzd(bld, dst, op[0], true);
170601e04c3fSmrg      } else {
170701e04c3fSmrg         emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
170801e04c3fSmrg
170901e04c3fSmrg         /* FBH counts from the MSB side, while GLSL's findMSB() wants the
171001e04c3fSmrg          * count from the LSB side. If FBH didn't return an error
171101e04c3fSmrg          * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
171201e04c3fSmrg          * count into an LSB count.
171301e04c3fSmrg          */
171401e04c3fSmrg         bld.CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
171501e04c3fSmrg
171601e04c3fSmrg         inst = bld.ADD(dst, src, brw_imm_d(31));
171701e04c3fSmrg         inst->predicate = BRW_PREDICATE_NORMAL;
171801e04c3fSmrg         inst->src[0].negate = true;
171901e04c3fSmrg      }
172001e04c3fSmrg      break;
172101e04c3fSmrg   }
172201e04c3fSmrg
172301e04c3fSmrg   case nir_op_find_lsb: {
172401e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
172501e04c3fSmrg      vec4_builder bld = vec4_builder(this).at_end();
172601e04c3fSmrg
17277ec681f3Smrg      if (devinfo->ver < 7) {
172801e04c3fSmrg         dst_reg temp = bld.vgrf(BRW_REGISTER_TYPE_D);
172901e04c3fSmrg
173001e04c3fSmrg         /* (x & -x) generates a value that consists of only the LSB of x.
173101e04c3fSmrg          * For all powers of 2, findMSB(y) == findLSB(y).
173201e04c3fSmrg          */
173301e04c3fSmrg         src_reg src = src_reg(retype(op[0], BRW_REGISTER_TYPE_D));
173401e04c3fSmrg         src_reg negated_src = src;
173501e04c3fSmrg
173601e04c3fSmrg         /* One must be negated, and the other must be non-negated.  It
173701e04c3fSmrg          * doesn't matter which is which.
173801e04c3fSmrg          */
173901e04c3fSmrg         negated_src.negate = true;
174001e04c3fSmrg         src.negate = false;
174101e04c3fSmrg
174201e04c3fSmrg         bld.AND(temp, src, negated_src);
174301e04c3fSmrg         emit_find_msb_using_lzd(bld, dst, src_reg(temp), false);
174401e04c3fSmrg      } else {
174501e04c3fSmrg         bld.FBL(dst, op[0]);
174601e04c3fSmrg      }
174701e04c3fSmrg      break;
174801e04c3fSmrg   }
174901e04c3fSmrg
175001e04c3fSmrg   case nir_op_ubitfield_extract:
175101e04c3fSmrg   case nir_op_ibitfield_extract:
175201e04c3fSmrg      unreachable("should have been lowered");
175301e04c3fSmrg   case nir_op_ubfe:
175401e04c3fSmrg   case nir_op_ibfe:
175501e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
175601e04c3fSmrg      op[0] = fix_3src_operand(op[0]);
175701e04c3fSmrg      op[1] = fix_3src_operand(op[1]);
175801e04c3fSmrg      op[2] = fix_3src_operand(op[2]);
175901e04c3fSmrg
176001e04c3fSmrg      emit(BFE(dst, op[2], op[1], op[0]));
176101e04c3fSmrg      break;
176201e04c3fSmrg
176301e04c3fSmrg   case nir_op_bfm:
176401e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
176501e04c3fSmrg      emit(BFI1(dst, op[0], op[1]));
176601e04c3fSmrg      break;
176701e04c3fSmrg
176801e04c3fSmrg   case nir_op_bfi:
176901e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
177001e04c3fSmrg      op[0] = fix_3src_operand(op[0]);
177101e04c3fSmrg      op[1] = fix_3src_operand(op[1]);
177201e04c3fSmrg      op[2] = fix_3src_operand(op[2]);
177301e04c3fSmrg
177401e04c3fSmrg      emit(BFI2(dst, op[0], op[1], op[2]));
177501e04c3fSmrg      break;
177601e04c3fSmrg
177701e04c3fSmrg   case nir_op_bitfield_insert:
177801e04c3fSmrg      unreachable("not reached: should have been lowered");
177901e04c3fSmrg
178001e04c3fSmrg   case nir_op_fsign:
17817ec681f3Smrg       if (type_sz(op[0].type) < 8) {
178201e04c3fSmrg         /* AND(val, 0x80000000) gives the sign bit.
178301e04c3fSmrg          *
178401e04c3fSmrg          * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
178501e04c3fSmrg          * zero.
178601e04c3fSmrg          */
178701e04c3fSmrg         emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
178801e04c3fSmrg
178901e04c3fSmrg         op[0].type = BRW_REGISTER_TYPE_UD;
179001e04c3fSmrg         dst.type = BRW_REGISTER_TYPE_UD;
179101e04c3fSmrg         emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
179201e04c3fSmrg
179301e04c3fSmrg         inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
179401e04c3fSmrg         inst->predicate = BRW_PREDICATE_NORMAL;
179501e04c3fSmrg         dst.type = BRW_REGISTER_TYPE_F;
179601e04c3fSmrg      } else {
179701e04c3fSmrg         /* For doubles we do the same but we need to consider:
179801e04c3fSmrg          *
179901e04c3fSmrg          * - We use a MOV with conditional_mod instead of a CMP so that we can
180001e04c3fSmrg          *   skip loading a 0.0 immediate. We use a source modifier on the
180101e04c3fSmrg          *   source of the MOV so that we flush denormalized values to 0.
180201e04c3fSmrg          *   Since we want to compare against 0, this won't alter the result.
180301e04c3fSmrg          * - We need to extract the high 32-bit of each DF where the sign
180401e04c3fSmrg          *   is stored.
180501e04c3fSmrg          * - We need to produce a DF result.
180601e04c3fSmrg          */
180701e04c3fSmrg
180801e04c3fSmrg         /* Check for zero */
180901e04c3fSmrg         src_reg value = op[0];
181001e04c3fSmrg         value.abs = true;
181101e04c3fSmrg         inst = emit(MOV(dst_null_df(), value));
181201e04c3fSmrg         inst->conditional_mod = BRW_CONDITIONAL_NZ;
181301e04c3fSmrg
181401e04c3fSmrg         /* AND each high 32-bit channel with 0x80000000u */
181501e04c3fSmrg         dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
181601e04c3fSmrg         emit(VEC4_OPCODE_PICK_HIGH_32BIT, tmp, op[0]);
181701e04c3fSmrg         emit(AND(tmp, src_reg(tmp), brw_imm_ud(0x80000000u)));
181801e04c3fSmrg
181901e04c3fSmrg         /* Add 1.0 to each channel, predicated to skip the cases where the
182001e04c3fSmrg          * channel's value was 0
182101e04c3fSmrg          */
182201e04c3fSmrg         inst = emit(OR(tmp, src_reg(tmp), brw_imm_ud(0x3f800000u)));
182301e04c3fSmrg         inst->predicate = BRW_PREDICATE_NORMAL;
182401e04c3fSmrg
182501e04c3fSmrg         /* Now convert the result from float to double */
182601e04c3fSmrg         emit_conversion_to_double(dst, retype(src_reg(tmp),
18277ec681f3Smrg                                               BRW_REGISTER_TYPE_F));
182801e04c3fSmrg      }
182901e04c3fSmrg      break;
183001e04c3fSmrg
183101e04c3fSmrg   case nir_op_ishl:
183201e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
18337ec681f3Smrg      try_immediate_source(instr, op, false);
183401e04c3fSmrg      emit(SHL(dst, op[0], op[1]));
183501e04c3fSmrg      break;
183601e04c3fSmrg
183701e04c3fSmrg   case nir_op_ishr:
183801e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
18397ec681f3Smrg      try_immediate_source(instr, op, false);
184001e04c3fSmrg      emit(ASR(dst, op[0], op[1]));
184101e04c3fSmrg      break;
184201e04c3fSmrg
184301e04c3fSmrg   case nir_op_ushr:
184401e04c3fSmrg      assert(nir_dest_bit_size(instr->dest.dest) < 64);
18457ec681f3Smrg      try_immediate_source(instr, op, false);
184601e04c3fSmrg      emit(SHR(dst, op[0], op[1]));
184701e04c3fSmrg      break;
184801e04c3fSmrg
184901e04c3fSmrg   case nir_op_ffma:
185001e04c3fSmrg      if (type_sz(dst.type) == 8) {
185101e04c3fSmrg         dst_reg mul_dst = dst_reg(this, glsl_type::dvec4_type);
185201e04c3fSmrg         emit(MUL(mul_dst, op[1], op[0]));
185301e04c3fSmrg         inst = emit(ADD(dst, src_reg(mul_dst), op[2]));
185401e04c3fSmrg      } else {
18557ec681f3Smrg         fix_float_operands(op, instr);
185601e04c3fSmrg         inst = emit(MAD(dst, op[2], op[1], op[0]));
185701e04c3fSmrg      }
185801e04c3fSmrg      break;
185901e04c3fSmrg
186001e04c3fSmrg   case nir_op_flrp:
18617ec681f3Smrg      fix_float_operands(op, instr);
18627ec681f3Smrg      inst = emit(LRP(dst, op[2], op[1], op[0]));
186301e04c3fSmrg      break;
186401e04c3fSmrg
18659f464c52Smaya   case nir_op_b32csel:
186601e04c3fSmrg      enum brw_predicate predicate;
186701e04c3fSmrg      if (!optimize_predicate(instr, &predicate)) {
186801e04c3fSmrg         emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
186901e04c3fSmrg         switch (dst.writemask) {
187001e04c3fSmrg         case WRITEMASK_X:
187101e04c3fSmrg            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
187201e04c3fSmrg            break;
187301e04c3fSmrg         case WRITEMASK_Y:
187401e04c3fSmrg            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
187501e04c3fSmrg            break;
187601e04c3fSmrg         case WRITEMASK_Z:
187701e04c3fSmrg            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
187801e04c3fSmrg            break;
187901e04c3fSmrg         case WRITEMASK_W:
188001e04c3fSmrg            predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
188101e04c3fSmrg            break;
188201e04c3fSmrg         default:
188301e04c3fSmrg            predicate = BRW_PREDICATE_NORMAL;
188401e04c3fSmrg            break;
188501e04c3fSmrg         }
188601e04c3fSmrg      }
188701e04c3fSmrg      inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
188801e04c3fSmrg      inst->predicate = predicate;
188901e04c3fSmrg      break;
189001e04c3fSmrg
18917ec681f3Smrg   case nir_op_fdot2_replicated:
18927ec681f3Smrg      try_immediate_source(instr, op, true);
189301e04c3fSmrg      inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
189401e04c3fSmrg      break;
189501e04c3fSmrg
18967ec681f3Smrg   case nir_op_fdot3_replicated:
18977ec681f3Smrg      try_immediate_source(instr, op, true);
189801e04c3fSmrg      inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
189901e04c3fSmrg      break;
190001e04c3fSmrg
19017ec681f3Smrg   case nir_op_fdot4_replicated:
19027ec681f3Smrg      try_immediate_source(instr, op, true);
190301e04c3fSmrg      inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
190401e04c3fSmrg      break;
190501e04c3fSmrg
190601e04c3fSmrg   case nir_op_fdph_replicated:
19077ec681f3Smrg      try_immediate_source(instr, op, false);
190801e04c3fSmrg      inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
190901e04c3fSmrg      break;
191001e04c3fSmrg
191101e04c3fSmrg   case nir_op_fdiv:
191201e04c3fSmrg      unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
191301e04c3fSmrg
191401e04c3fSmrg   case nir_op_fmod:
191501e04c3fSmrg      unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
191601e04c3fSmrg
191701e04c3fSmrg   case nir_op_fsub:
191801e04c3fSmrg   case nir_op_isub:
191901e04c3fSmrg      unreachable("not reached: should be handled by ir_sub_to_add_neg");
192001e04c3fSmrg
192101e04c3fSmrg   default:
192201e04c3fSmrg      unreachable("Unimplemented ALU operation");
192301e04c3fSmrg   }
192401e04c3fSmrg
192501e04c3fSmrg   /* If we need to do a boolean resolve, replace the result with -(x & 1)
192601e04c3fSmrg    * to sign extend the low bit to 0/~0
192701e04c3fSmrg    */
19287ec681f3Smrg   if (devinfo->ver <= 5 &&
192901e04c3fSmrg       (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
193001e04c3fSmrg       BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
193101e04c3fSmrg      dst_reg masked = dst_reg(this, glsl_type::int_type);
193201e04c3fSmrg      masked.writemask = dst.writemask;
193301e04c3fSmrg      emit(AND(masked, src_reg(dst), brw_imm_d(1)));
193401e04c3fSmrg      src_reg masked_neg = src_reg(masked);
193501e04c3fSmrg      masked_neg.negate = true;
193601e04c3fSmrg      emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
193701e04c3fSmrg   }
193801e04c3fSmrg}
193901e04c3fSmrg
194001e04c3fSmrgvoid
194101e04c3fSmrgvec4_visitor::nir_emit_jump(nir_jump_instr *instr)
194201e04c3fSmrg{
194301e04c3fSmrg   switch (instr->type) {
194401e04c3fSmrg   case nir_jump_break:
194501e04c3fSmrg      emit(BRW_OPCODE_BREAK);
194601e04c3fSmrg      break;
194701e04c3fSmrg
194801e04c3fSmrg   case nir_jump_continue:
194901e04c3fSmrg      emit(BRW_OPCODE_CONTINUE);
195001e04c3fSmrg      break;
195101e04c3fSmrg
195201e04c3fSmrg   case nir_jump_return:
19537ec681f3Smrg      FALLTHROUGH;
195401e04c3fSmrg   default:
195501e04c3fSmrg      unreachable("unknown jump");
195601e04c3fSmrg   }
195701e04c3fSmrg}
195801e04c3fSmrg
195901e04c3fSmrgstatic enum ir_texture_opcode
196001e04c3fSmrgir_texture_opcode_for_nir_texop(nir_texop texop)
196101e04c3fSmrg{
196201e04c3fSmrg   enum ir_texture_opcode op;
196301e04c3fSmrg
196401e04c3fSmrg   switch (texop) {
196501e04c3fSmrg   case nir_texop_lod: op = ir_lod; break;
196601e04c3fSmrg   case nir_texop_query_levels: op = ir_query_levels; break;
196701e04c3fSmrg   case nir_texop_texture_samples: op = ir_texture_samples; break;
196801e04c3fSmrg   case nir_texop_tex: op = ir_tex; break;
196901e04c3fSmrg   case nir_texop_tg4: op = ir_tg4; break;
197001e04c3fSmrg   case nir_texop_txb: op = ir_txb; break;
197101e04c3fSmrg   case nir_texop_txd: op = ir_txd; break;
197201e04c3fSmrg   case nir_texop_txf: op = ir_txf; break;
197301e04c3fSmrg   case nir_texop_txf_ms: op = ir_txf_ms; break;
197401e04c3fSmrg   case nir_texop_txl: op = ir_txl; break;
197501e04c3fSmrg   case nir_texop_txs: op = ir_txs; break;
197601e04c3fSmrg   case nir_texop_samples_identical: op = ir_samples_identical; break;
197701e04c3fSmrg   default:
197801e04c3fSmrg      unreachable("unknown texture opcode");
197901e04c3fSmrg   }
198001e04c3fSmrg
198101e04c3fSmrg   return op;
198201e04c3fSmrg}
198301e04c3fSmrg
198401e04c3fSmrgvoid
198501e04c3fSmrgvec4_visitor::nir_emit_texture(nir_tex_instr *instr)
198601e04c3fSmrg{
198701e04c3fSmrg   unsigned texture = instr->texture_index;
198801e04c3fSmrg   unsigned sampler = instr->sampler_index;
198901e04c3fSmrg   src_reg texture_reg = brw_imm_ud(texture);
199001e04c3fSmrg   src_reg sampler_reg = brw_imm_ud(sampler);
199101e04c3fSmrg   src_reg coordinate;
199201e04c3fSmrg   const glsl_type *coord_type = NULL;
199301e04c3fSmrg   src_reg shadow_comparator;
199401e04c3fSmrg   src_reg offset_value;
199501e04c3fSmrg   src_reg lod, lod2;
199601e04c3fSmrg   src_reg sample_index;
199701e04c3fSmrg   src_reg mcs;
199801e04c3fSmrg
199901e04c3fSmrg   dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
200001e04c3fSmrg
200101e04c3fSmrg   /* The hardware requires a LOD for buffer textures */
200201e04c3fSmrg   if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
200301e04c3fSmrg      lod = brw_imm_d(0);
200401e04c3fSmrg
200501e04c3fSmrg   /* Load the texture operation sources */
200601e04c3fSmrg   uint32_t constant_offset = 0;
200701e04c3fSmrg   for (unsigned i = 0; i < instr->num_srcs; i++) {
200801e04c3fSmrg      switch (instr->src[i].src_type) {
200901e04c3fSmrg      case nir_tex_src_comparator:
201001e04c3fSmrg         shadow_comparator = get_nir_src(instr->src[i].src,
201101e04c3fSmrg                                         BRW_REGISTER_TYPE_F, 1);
201201e04c3fSmrg         break;
201301e04c3fSmrg
201401e04c3fSmrg      case nir_tex_src_coord: {
201501e04c3fSmrg         unsigned src_size = nir_tex_instr_src_size(instr, i);
201601e04c3fSmrg
201701e04c3fSmrg         switch (instr->op) {
201801e04c3fSmrg         case nir_texop_txf:
201901e04c3fSmrg         case nir_texop_txf_ms:
202001e04c3fSmrg         case nir_texop_samples_identical:
202101e04c3fSmrg            coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
202201e04c3fSmrg                                     src_size);
202301e04c3fSmrg            coord_type = glsl_type::ivec(src_size);
202401e04c3fSmrg            break;
202501e04c3fSmrg
202601e04c3fSmrg         default:
202701e04c3fSmrg            coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
202801e04c3fSmrg                                     src_size);
202901e04c3fSmrg            coord_type = glsl_type::vec(src_size);
203001e04c3fSmrg            break;
203101e04c3fSmrg         }
203201e04c3fSmrg         break;
203301e04c3fSmrg      }
203401e04c3fSmrg
203501e04c3fSmrg      case nir_tex_src_ddx:
203601e04c3fSmrg         lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
203701e04c3fSmrg                           nir_tex_instr_src_size(instr, i));
203801e04c3fSmrg         break;
203901e04c3fSmrg
204001e04c3fSmrg      case nir_tex_src_ddy:
204101e04c3fSmrg         lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
204201e04c3fSmrg                           nir_tex_instr_src_size(instr, i));
204301e04c3fSmrg         break;
204401e04c3fSmrg
204501e04c3fSmrg      case nir_tex_src_lod:
204601e04c3fSmrg         switch (instr->op) {
204701e04c3fSmrg         case nir_texop_txs:
204801e04c3fSmrg         case nir_texop_txf:
204901e04c3fSmrg            lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
205001e04c3fSmrg            break;
205101e04c3fSmrg
205201e04c3fSmrg         default:
205301e04c3fSmrg            lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
205401e04c3fSmrg            break;
205501e04c3fSmrg         }
205601e04c3fSmrg         break;
205701e04c3fSmrg
205801e04c3fSmrg      case nir_tex_src_ms_index: {
205901e04c3fSmrg         sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
206001e04c3fSmrg         break;
206101e04c3fSmrg      }
206201e04c3fSmrg
20639f464c52Smaya      case nir_tex_src_offset:
20649f464c52Smaya         if (!brw_texture_offset(instr, i, &constant_offset)) {
206501e04c3fSmrg            offset_value =
206601e04c3fSmrg               get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
206701e04c3fSmrg         }
206801e04c3fSmrg         break;
206901e04c3fSmrg
207001e04c3fSmrg      case nir_tex_src_texture_offset: {
207101e04c3fSmrg         /* Emit code to evaluate the actual indexing expression */
207201e04c3fSmrg         src_reg src = get_nir_src(instr->src[i].src, 1);
207301e04c3fSmrg         src_reg temp(this, glsl_type::uint_type);
207401e04c3fSmrg         emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
207501e04c3fSmrg         texture_reg = emit_uniformize(temp);
207601e04c3fSmrg         break;
207701e04c3fSmrg      }
207801e04c3fSmrg
207901e04c3fSmrg      case nir_tex_src_sampler_offset: {
208001e04c3fSmrg         /* Emit code to evaluate the actual indexing expression */
208101e04c3fSmrg         src_reg src = get_nir_src(instr->src[i].src, 1);
208201e04c3fSmrg         src_reg temp(this, glsl_type::uint_type);
208301e04c3fSmrg         emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
208401e04c3fSmrg         sampler_reg = emit_uniformize(temp);
208501e04c3fSmrg         break;
208601e04c3fSmrg      }
208701e04c3fSmrg
208801e04c3fSmrg      case nir_tex_src_projector:
20897ec681f3Smrg         unreachable("Should be lowered by nir_lower_tex");
209001e04c3fSmrg
209101e04c3fSmrg      case nir_tex_src_bias:
209201e04c3fSmrg         unreachable("LOD bias is not valid for vertex shaders.\n");
209301e04c3fSmrg
209401e04c3fSmrg      default:
209501e04c3fSmrg         unreachable("unknown texture source");
209601e04c3fSmrg      }
209701e04c3fSmrg   }
209801e04c3fSmrg
209901e04c3fSmrg   if (instr->op == nir_texop_txf_ms ||
210001e04c3fSmrg       instr->op == nir_texop_samples_identical) {
210101e04c3fSmrg      assert(coord_type != NULL);
21027ec681f3Smrg      if (devinfo->ver >= 7 &&
210301e04c3fSmrg          key_tex->compressed_multisample_layout_mask & (1 << texture)) {
210401e04c3fSmrg         mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
210501e04c3fSmrg      } else {
210601e04c3fSmrg         mcs = brw_imm_ud(0u);
210701e04c3fSmrg      }
210801e04c3fSmrg   }
210901e04c3fSmrg
211001e04c3fSmrg   /* Stuff the channel select bits in the top of the texture offset */
211101e04c3fSmrg   if (instr->op == nir_texop_tg4) {
211201e04c3fSmrg      if (instr->component == 1 &&
211301e04c3fSmrg          (key_tex->gather_channel_quirk_mask & (1 << texture))) {
211401e04c3fSmrg         /* gather4 sampler is broken for green channel on RG32F --
211501e04c3fSmrg          * we must ask for blue instead.
211601e04c3fSmrg          */
211701e04c3fSmrg         constant_offset |= 2 << 16;
211801e04c3fSmrg      } else {
211901e04c3fSmrg         constant_offset |= instr->component << 16;
212001e04c3fSmrg      }
212101e04c3fSmrg   }
212201e04c3fSmrg
212301e04c3fSmrg   ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
212401e04c3fSmrg
21257ec681f3Smrg   emit_texture(op, dest, nir_tex_instr_dest_size(instr),
21267ec681f3Smrg                coordinate, instr->coord_components,
212701e04c3fSmrg                shadow_comparator,
212801e04c3fSmrg                lod, lod2, sample_index,
212901e04c3fSmrg                constant_offset, offset_value, mcs,
213001e04c3fSmrg                texture, texture_reg, sampler_reg);
213101e04c3fSmrg}
213201e04c3fSmrg
213301e04c3fSmrgvoid
213401e04c3fSmrgvec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
213501e04c3fSmrg{
213601e04c3fSmrg   nir_ssa_values[instr->def.index] =
213701e04c3fSmrg      dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(instr->def.bit_size, 32)));
213801e04c3fSmrg}
213901e04c3fSmrg
214001e04c3fSmrg/* SIMD4x2 64bit data is stored in register space like this:
214101e04c3fSmrg *
214201e04c3fSmrg * r0.0:DF  x0 y0 z0 w0
214301e04c3fSmrg * r1.0:DF  x1 y1 z1 w1
214401e04c3fSmrg *
214501e04c3fSmrg * When we need to write data such as this to memory using 32-bit write
214601e04c3fSmrg * messages we need to shuffle it in this fashion:
214701e04c3fSmrg *
214801e04c3fSmrg * r0.0:DF  x0 y0 x1 y1 (to be written at base offset)
214901e04c3fSmrg * r0.0:DF  z0 w0 z1 w1 (to be written at base offset + 16)
215001e04c3fSmrg *
215101e04c3fSmrg * We need to do the inverse operation when we read using 32-bit messages,
215201e04c3fSmrg * which we can do by applying the same exact shuffling on the 64-bit data
215301e04c3fSmrg * read, only that because the data for each vertex is positioned differently
215401e04c3fSmrg * we need to apply different channel enables.
215501e04c3fSmrg *
215601e04c3fSmrg * This function takes 64bit data and shuffles it as explained above.
215701e04c3fSmrg *
215801e04c3fSmrg * The @for_write parameter is used to specify if the shuffling is being done
215901e04c3fSmrg * for proper SIMD4x2 64-bit data that needs to be shuffled prior to a 32-bit
216001e04c3fSmrg * write message (for_write = true), or instead we are doing the inverse
216101e04c3fSmrg * operation and we have just read 64-bit data using a 32-bit messages that we
216201e04c3fSmrg * need to shuffle to create valid SIMD4x2 64-bit data (for_write = false).
216301e04c3fSmrg *
216401e04c3fSmrg * If @block and @ref are non-NULL, then the shuffling is done after @ref,
216501e04c3fSmrg * otherwise the instructions are emitted normally at the end. The function
216601e04c3fSmrg * returns the last instruction inserted.
216701e04c3fSmrg *
216801e04c3fSmrg * Notice that @src and @dst cannot be the same register.
216901e04c3fSmrg */
217001e04c3fSmrgvec4_instruction *
217101e04c3fSmrgvec4_visitor::shuffle_64bit_data(dst_reg dst, src_reg src, bool for_write,
21727ec681f3Smrg                                 bool for_scratch,
217301e04c3fSmrg                                 bblock_t *block, vec4_instruction *ref)
217401e04c3fSmrg{
217501e04c3fSmrg   assert(type_sz(src.type) == 8);
217601e04c3fSmrg   assert(type_sz(dst.type) == 8);
217701e04c3fSmrg   assert(!regions_overlap(dst, 2 * REG_SIZE, src, 2 * REG_SIZE));
217801e04c3fSmrg   assert(!ref == !block);
217901e04c3fSmrg
21807ec681f3Smrg   opcode mov_op = for_scratch ? VEC4_OPCODE_MOV_FOR_SCRATCH : BRW_OPCODE_MOV;
21817ec681f3Smrg
218201e04c3fSmrg   const vec4_builder bld = !ref ? vec4_builder(this).at_end() :
218301e04c3fSmrg                                   vec4_builder(this).at(block, ref->next);
218401e04c3fSmrg
218501e04c3fSmrg   /* Resolve swizzle in src */
218601e04c3fSmrg   if (src.swizzle != BRW_SWIZZLE_XYZW) {
218701e04c3fSmrg      dst_reg data = dst_reg(this, glsl_type::dvec4_type);
21887ec681f3Smrg      bld.emit(mov_op, data, src);
218901e04c3fSmrg      src = src_reg(data);
219001e04c3fSmrg   }
219101e04c3fSmrg
219201e04c3fSmrg   /* dst+0.XY = src+0.XY */
21937ec681f3Smrg   bld.group(4, 0).emit(mov_op, writemask(dst, WRITEMASK_XY), src);
219401e04c3fSmrg
219501e04c3fSmrg   /* dst+0.ZW = src+1.XY */
21967ec681f3Smrg   bld.group(4, for_write ? 1 : 0)
21977ec681f3Smrg            .emit(mov_op, writemask(dst, WRITEMASK_ZW),
219801e04c3fSmrg                  swizzle(byte_offset(src, REG_SIZE), BRW_SWIZZLE_XYXY));
219901e04c3fSmrg
220001e04c3fSmrg   /* dst+1.XY = src+0.ZW */
22017ec681f3Smrg   bld.group(4, for_write ? 0 : 1)
22027ec681f3Smrg            .emit(mov_op, writemask(byte_offset(dst, REG_SIZE), WRITEMASK_XY),
22037ec681f3Smrg                  swizzle(src, BRW_SWIZZLE_ZWZW));
220401e04c3fSmrg
220501e04c3fSmrg   /* dst+1.ZW = src+1.ZW */
22067ec681f3Smrg   return bld.group(4, 1)
22077ec681f3Smrg            .emit(mov_op, writemask(byte_offset(dst, REG_SIZE), WRITEMASK_ZW),
22087ec681f3Smrg                  byte_offset(src, REG_SIZE));
220901e04c3fSmrg}
221001e04c3fSmrg
221101e04c3fSmrg}
2212