Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2014 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21.  * IN THE SOFTWARE.
  22.  *
  23.  * Authors:
  24.  *    Connor Abbott (cwabbott0@gmail.com)
  25.  *
  26.  */
  27.  
  28. #include "glsl_to_nir.h"
  29. #include "ir_visitor.h"
  30. #include "ir_hierarchical_visitor.h"
  31. #include "ir.h"
  32.  
  33. /*
  34.  * pass to lower GLSL IR to NIR
  35.  *
  36.  * This will lower variable dereferences to loads/stores of corresponding
  37.  * variables in NIR - the variables will be converted to registers in a later
  38.  * pass.
  39.  */
  40.  
  41. namespace {
  42.  
  43. class nir_visitor : public ir_visitor
  44. {
  45. public:
  46.    nir_visitor(nir_shader *shader, gl_shader_stage stage);
  47.    ~nir_visitor();
  48.  
  49.    virtual void visit(ir_variable *);
  50.    virtual void visit(ir_function *);
  51.    virtual void visit(ir_function_signature *);
  52.    virtual void visit(ir_loop *);
  53.    virtual void visit(ir_if *);
  54.    virtual void visit(ir_discard *);
  55.    virtual void visit(ir_loop_jump *);
  56.    virtual void visit(ir_return *);
  57.    virtual void visit(ir_call *);
  58.    virtual void visit(ir_assignment *);
  59.    virtual void visit(ir_emit_vertex *);
  60.    virtual void visit(ir_end_primitive *);
  61.    virtual void visit(ir_expression *);
  62.    virtual void visit(ir_swizzle *);
  63.    virtual void visit(ir_texture *);
  64.    virtual void visit(ir_constant *);
  65.    virtual void visit(ir_dereference_variable *);
  66.    virtual void visit(ir_dereference_record *);
  67.    virtual void visit(ir_dereference_array *);
  68.  
  69.    void create_function(ir_function *ir);
  70.  
  71. private:
  72.    void create_overload(ir_function_signature *ir, nir_function *function);
  73.    void add_instr(nir_instr *instr, unsigned num_components);
  74.    nir_src evaluate_rvalue(ir_rvalue *ir);
  75.  
  76.    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src *srcs);
  77.    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1);
  78.    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1,
  79.                        nir_src src2);
  80.    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1,
  81.                        nir_src src2, nir_src src3);
  82.  
  83.    bool supports_ints;
  84.  
  85.    nir_shader *shader;
  86.    gl_shader_stage stage;
  87.    nir_function_impl *impl;
  88.    exec_list *cf_node_list;
  89.    nir_instr *result; /* result of the expression tree last visited */
  90.  
  91.    nir_deref_var *evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir);
  92.  
  93.    /* the head of the dereference chain we're creating */
  94.    nir_deref_var *deref_head;
  95.    /* the tail of the dereference chain we're creating */
  96.    nir_deref *deref_tail;
  97.  
  98.    nir_variable *var; /* variable created by ir_variable visitor */
  99.  
  100.    /* whether the IR we're operating on is per-function or global */
  101.    bool is_global;
  102.  
  103.    /* map of ir_variable -> nir_variable */
  104.    struct hash_table *var_table;
  105.  
  106.    /* map of ir_function_signature -> nir_function_overload */
  107.    struct hash_table *overload_table;
  108. };
  109.  
  110. /*
  111.  * This visitor runs before the main visitor, calling create_function() for
  112.  * each function so that the main visitor can resolve forward references in
  113.  * calls.
  114.  */
  115.  
  116. class nir_function_visitor : public ir_hierarchical_visitor
  117. {
  118. public:
  119.    nir_function_visitor(nir_visitor *v) : visitor(v)
  120.    {
  121.    }
  122.    virtual ir_visitor_status visit_enter(ir_function *);
  123.  
  124. private:
  125.    nir_visitor *visitor;
  126. };
  127.  
  128. }; /* end of anonymous namespace */
  129.  
  130. nir_shader *
  131. glsl_to_nir(struct gl_shader *sh, const nir_shader_compiler_options *options)
  132. {
  133.    nir_shader *shader = nir_shader_create(NULL, options);
  134.  
  135.    nir_visitor v1(shader, sh->Stage);
  136.    nir_function_visitor v2(&v1);
  137.    v2.run(sh->ir);
  138.    visit_exec_list(sh->ir, &v1);
  139.  
  140.    return shader;
  141. }
  142.  
  143. nir_visitor::nir_visitor(nir_shader *shader, gl_shader_stage stage)
  144. {
  145.    this->supports_ints = shader->options->native_integers;
  146.    this->shader = shader;
  147.    this->stage = stage;
  148.    this->is_global = true;
  149.    this->var_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
  150.                                              _mesa_key_pointer_equal);
  151.    this->overload_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
  152.                                                   _mesa_key_pointer_equal);
  153. }
  154.  
  155. nir_visitor::~nir_visitor()
  156. {
  157.    _mesa_hash_table_destroy(this->var_table, NULL);
  158.    _mesa_hash_table_destroy(this->overload_table, NULL);
  159. }
  160.  
  161. nir_deref_var *
  162. nir_visitor::evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir)
  163. {
  164.    ir->accept(this);
  165.    ralloc_steal(mem_ctx, this->deref_head);
  166.    return this->deref_head;
  167. }
  168.  
  169. static nir_constant *
  170. constant_copy(ir_constant *ir, void *mem_ctx)
  171. {
  172.    if (ir == NULL)
  173.       return NULL;
  174.  
  175.    nir_constant *ret = ralloc(mem_ctx, nir_constant);
  176.  
  177.    unsigned total_elems = ir->type->components();
  178.    unsigned i;
  179.    switch (ir->type->base_type) {
  180.    case GLSL_TYPE_UINT:
  181.       for (i = 0; i < total_elems; i++)
  182.          ret->value.u[i] = ir->value.u[i];
  183.       break;
  184.  
  185.    case GLSL_TYPE_INT:
  186.       for (i = 0; i < total_elems; i++)
  187.          ret->value.i[i] = ir->value.i[i];
  188.       break;
  189.  
  190.    case GLSL_TYPE_FLOAT:
  191.       for (i = 0; i < total_elems; i++)
  192.          ret->value.f[i] = ir->value.f[i];
  193.       break;
  194.  
  195.    case GLSL_TYPE_BOOL:
  196.       for (i = 0; i < total_elems; i++)
  197.          ret->value.b[i] = ir->value.b[i];
  198.       break;
  199.  
  200.    case GLSL_TYPE_STRUCT:
  201.       ret->elements = ralloc_array(mem_ctx, nir_constant *,
  202.                                    ir->type->length);
  203.       i = 0;
  204.       foreach_in_list(ir_constant, field, &ir->components) {
  205.          ret->elements[i] = constant_copy(field, mem_ctx);
  206.          i++;
  207.       }
  208.       break;
  209.  
  210.    case GLSL_TYPE_ARRAY:
  211.       ret->elements = ralloc_array(mem_ctx, nir_constant *,
  212.                                    ir->type->length);
  213.  
  214.       for (i = 0; i < ir->type->length; i++)
  215.          ret->elements[i] = constant_copy(ir->array_elements[i], mem_ctx);
  216.       break;
  217.  
  218.    default:
  219.       unreachable("not reached");
  220.    }
  221.  
  222.    return ret;
  223. }
  224.  
  225. void
  226. nir_visitor::visit(ir_variable *ir)
  227. {
  228.    nir_variable *var = ralloc(shader, nir_variable);
  229.    var->type = ir->type;
  230.    var->name = ralloc_strdup(var, ir->name);
  231.  
  232.    if (ir->is_interface_instance() && ir->get_max_ifc_array_access() != NULL) {
  233.       unsigned size = ir->get_interface_type()->length;
  234.       var->max_ifc_array_access = ralloc_array(var, unsigned, size);
  235.       memcpy(var->max_ifc_array_access, ir->get_max_ifc_array_access(),
  236.              size * sizeof(unsigned));
  237.    } else {
  238.       var->max_ifc_array_access = NULL;
  239.    }
  240.  
  241.    var->data.read_only = ir->data.read_only;
  242.    var->data.centroid = ir->data.centroid;
  243.    var->data.sample = ir->data.sample;
  244.    var->data.invariant = ir->data.invariant;
  245.    var->data.location = ir->data.location;
  246.  
  247.    switch(ir->data.mode) {
  248.    case ir_var_auto:
  249.    case ir_var_temporary:
  250.       if (is_global)
  251.          var->data.mode = nir_var_global;
  252.       else
  253.          var->data.mode = nir_var_local;
  254.       break;
  255.  
  256.    case ir_var_function_in:
  257.    case ir_var_function_out:
  258.    case ir_var_function_inout:
  259.    case ir_var_const_in:
  260.       var->data.mode = nir_var_local;
  261.       break;
  262.  
  263.    case ir_var_shader_in:
  264.       if (stage == MESA_SHADER_FRAGMENT &&
  265.           ir->data.location == VARYING_SLOT_FACE) {
  266.          /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
  267.          var->data.location = SYSTEM_VALUE_FRONT_FACE;
  268.          var->data.mode = nir_var_system_value;
  269.       } else {
  270.          var->data.mode = nir_var_shader_in;
  271.       }
  272.       break;
  273.  
  274.    case ir_var_shader_out:
  275.       var->data.mode = nir_var_shader_out;
  276.       break;
  277.  
  278.    case ir_var_uniform:
  279.       var->data.mode = nir_var_uniform;
  280.       break;
  281.  
  282.  
  283.    case ir_var_system_value:
  284.       var->data.mode = nir_var_system_value;
  285.       break;
  286.  
  287.    default:
  288.       unreachable("not reached");
  289.    }
  290.  
  291.    var->data.interpolation = ir->data.interpolation;
  292.    var->data.origin_upper_left = ir->data.origin_upper_left;
  293.    var->data.pixel_center_integer = ir->data.pixel_center_integer;
  294.    var->data.explicit_location = ir->data.explicit_location;
  295.    var->data.explicit_index = ir->data.explicit_index;
  296.    var->data.explicit_binding = ir->data.explicit_binding;
  297.    var->data.has_initializer = ir->data.has_initializer;
  298.    var->data.is_unmatched_generic_inout = ir->data.is_unmatched_generic_inout;
  299.    var->data.location_frac = ir->data.location_frac;
  300.    var->data.from_named_ifc_block_array = ir->data.from_named_ifc_block_array;
  301.    var->data.from_named_ifc_block_nonarray = ir->data.from_named_ifc_block_nonarray;
  302.  
  303.    switch (ir->data.depth_layout) {
  304.    case ir_depth_layout_none:
  305.       var->data.depth_layout = nir_depth_layout_none;
  306.       break;
  307.    case ir_depth_layout_any:
  308.       var->data.depth_layout = nir_depth_layout_any;
  309.       break;
  310.    case ir_depth_layout_greater:
  311.       var->data.depth_layout = nir_depth_layout_greater;
  312.       break;
  313.    case ir_depth_layout_less:
  314.       var->data.depth_layout = nir_depth_layout_less;
  315.       break;
  316.    case ir_depth_layout_unchanged:
  317.       var->data.depth_layout = nir_depth_layout_unchanged;
  318.       break;
  319.    default:
  320.       unreachable("not reached");
  321.    }
  322.  
  323.    var->data.index = ir->data.index;
  324.    var->data.binding = ir->data.binding;
  325.    /* XXX Get rid of buffer_index */
  326.    var->data.atomic.buffer_index = ir->data.binding;
  327.    var->data.atomic.offset = ir->data.atomic.offset;
  328.    var->data.image.read_only = ir->data.image_read_only;
  329.    var->data.image.write_only = ir->data.image_write_only;
  330.    var->data.image.coherent = ir->data.image_coherent;
  331.    var->data.image._volatile = ir->data.image_volatile;
  332.    var->data.image.restrict_flag = ir->data.image_restrict;
  333.    var->data.image.format = ir->data.image_format;
  334.    var->data.max_array_access = ir->data.max_array_access;
  335.  
  336.    var->num_state_slots = ir->get_num_state_slots();
  337.    if (var->num_state_slots > 0) {
  338.       var->state_slots = ralloc_array(var, nir_state_slot,
  339.                                       var->num_state_slots);
  340.  
  341.       ir_state_slot *state_slots = ir->get_state_slots();
  342.       for (unsigned i = 0; i < var->num_state_slots; i++) {
  343.          for (unsigned j = 0; j < 5; j++)
  344.             var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
  345.          var->state_slots[i].swizzle = state_slots[i].swizzle;
  346.       }
  347.    } else {
  348.       var->state_slots = NULL;
  349.    }
  350.  
  351.    var->constant_initializer = constant_copy(ir->constant_initializer, var);
  352.  
  353.    var->interface_type = ir->get_interface_type();
  354.  
  355.    switch (var->data.mode) {
  356.    case nir_var_local:
  357.       exec_list_push_tail(&impl->locals, &var->node);
  358.       break;
  359.  
  360.    case nir_var_global:
  361.       exec_list_push_tail(&shader->globals, &var->node);
  362.       break;
  363.  
  364.    case nir_var_shader_in:
  365.       exec_list_push_tail(&shader->inputs, &var->node);
  366.       break;
  367.  
  368.    case nir_var_shader_out:
  369.       exec_list_push_tail(&shader->outputs, &var->node);
  370.       break;
  371.  
  372.    case nir_var_uniform:
  373.       exec_list_push_tail(&shader->uniforms, &var->node);
  374.       break;
  375.  
  376.    case nir_var_system_value:
  377.       exec_list_push_tail(&shader->system_values, &var->node);
  378.       break;
  379.  
  380.    default:
  381.       unreachable("not reached");
  382.    }
  383.  
  384.    _mesa_hash_table_insert(var_table, ir, var);
  385.    this->var = var;
  386. }
  387.  
  388. ir_visitor_status
  389. nir_function_visitor::visit_enter(ir_function *ir)
  390. {
  391.    visitor->create_function(ir);
  392.    return visit_continue_with_parent;
  393. }
  394.  
  395.  
  396. void
  397. nir_visitor::create_function(ir_function *ir)
  398. {
  399.    nir_function *func = nir_function_create(this->shader, ir->name);
  400.    foreach_in_list(ir_function_signature, sig, &ir->signatures) {
  401.       create_overload(sig, func);
  402.    }
  403. }
  404.  
  405.  
  406.  
  407. void
  408. nir_visitor::create_overload(ir_function_signature *ir, nir_function *function)
  409. {
  410.    if (ir->is_intrinsic)
  411.       return;
  412.  
  413.    nir_function_overload *overload = nir_function_overload_create(function);
  414.  
  415.    unsigned num_params = ir->parameters.length();
  416.    overload->num_params = num_params;
  417.    overload->params = ralloc_array(shader, nir_parameter, num_params);
  418.  
  419.    unsigned i = 0;
  420.    foreach_in_list(ir_variable, param, &ir->parameters) {
  421.       switch (param->data.mode) {
  422.       case ir_var_function_in:
  423.          overload->params[i].param_type = nir_parameter_in;
  424.          break;
  425.  
  426.       case ir_var_function_out:
  427.          overload->params[i].param_type = nir_parameter_out;
  428.          break;
  429.  
  430.       case ir_var_function_inout:
  431.          overload->params[i].param_type = nir_parameter_inout;
  432.          break;
  433.  
  434.       default:
  435.          unreachable("not reached");
  436.       }
  437.  
  438.       overload->params[i].type = param->type;
  439.       i++;
  440.    }
  441.  
  442.    overload->return_type = ir->return_type;
  443.  
  444.    _mesa_hash_table_insert(this->overload_table, ir, overload);
  445. }
  446.  
  447. void
  448. nir_visitor::visit(ir_function *ir)
  449. {
  450.    foreach_in_list(ir_function_signature, sig, &ir->signatures)
  451.       sig->accept(this);
  452. }
  453.  
  454. void
  455. nir_visitor::visit(ir_function_signature *ir)
  456. {
  457.    if (ir->is_intrinsic)
  458.       return;
  459.  
  460.    struct hash_entry *entry =
  461.       _mesa_hash_table_search(this->overload_table, ir);
  462.  
  463.    assert(entry);
  464.    nir_function_overload *overload = (nir_function_overload *) entry->data;
  465.  
  466.    if (ir->is_defined) {
  467.       nir_function_impl *impl = nir_function_impl_create(overload);
  468.       this->impl = impl;
  469.  
  470.       unsigned num_params = overload->num_params;
  471.       impl->num_params = num_params;
  472.       impl->params = ralloc_array(this->shader, nir_variable *, num_params);
  473.       unsigned i = 0;
  474.       foreach_in_list(ir_variable, param, &ir->parameters) {
  475.          param->accept(this);
  476.          impl->params[i] = this->var;
  477.          i++;
  478.       }
  479.  
  480.       if (overload->return_type == glsl_type::void_type) {
  481.          impl->return_var = NULL;
  482.       } else {
  483.          impl->return_var = ralloc(this->shader, nir_variable);
  484.          impl->return_var->name = ralloc_strdup(impl->return_var,
  485.                                                 "return_var");
  486.          impl->return_var->type = overload->return_type;
  487.       }
  488.  
  489.       this->is_global = false;
  490.  
  491.       this->cf_node_list = &impl->body;
  492.       visit_exec_list(&ir->body, this);
  493.  
  494.       this->is_global = true;
  495.    } else {
  496.       overload->impl = NULL;
  497.    }
  498. }
  499.  
  500. void
  501. nir_visitor::visit(ir_loop *ir)
  502. {
  503.    exec_list *old_list = this->cf_node_list;
  504.  
  505.    nir_loop *loop = nir_loop_create(this->shader);
  506.    nir_cf_node_insert_end(old_list, &loop->cf_node);
  507.    this->cf_node_list = &loop->body;
  508.    visit_exec_list(&ir->body_instructions, this);
  509.  
  510.    this->cf_node_list = old_list;
  511. }
  512.  
  513. void
  514. nir_visitor::visit(ir_if *ir)
  515. {
  516.    nir_src condition = evaluate_rvalue(ir->condition);
  517.  
  518.    exec_list *old_list = this->cf_node_list;
  519.  
  520.    nir_if *if_stmt = nir_if_create(this->shader);
  521.    if_stmt->condition = condition;
  522.    nir_cf_node_insert_end(old_list, &if_stmt->cf_node);
  523.  
  524.    this->cf_node_list = &if_stmt->then_list;
  525.    visit_exec_list(&ir->then_instructions, this);
  526.  
  527.    this->cf_node_list = &if_stmt->else_list;
  528.    visit_exec_list(&ir->else_instructions, this);
  529.  
  530.    this->cf_node_list = old_list;
  531. }
  532.  
  533. void
  534. nir_visitor::visit(ir_discard *ir)
  535. {
  536.    /*
  537.     * discards aren't treated as control flow, because before we lower them
  538.     * they can appear anywhere in the shader and the stuff after them may still
  539.     * be executed (yay, crazy GLSL rules!). However, after lowering, all the
  540.     * discards will be immediately followed by a return.
  541.     */
  542.  
  543.    nir_intrinsic_instr *discard;
  544.    if (ir->condition) {
  545.       discard = nir_intrinsic_instr_create(this->shader,
  546.                                            nir_intrinsic_discard_if);
  547.       discard->src[0] = evaluate_rvalue(ir->condition);
  548.    } else {
  549.       discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
  550.    }
  551.    nir_instr_insert_after_cf_list(this->cf_node_list, &discard->instr);
  552. }
  553.  
  554. void
  555. nir_visitor::visit(ir_emit_vertex *ir)
  556. {
  557.    nir_intrinsic_instr *instr =
  558.       nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
  559.    instr->const_index[0] = ir->stream_id();
  560.    nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
  561. }
  562.  
  563. void
  564. nir_visitor::visit(ir_end_primitive *ir)
  565. {
  566.    nir_intrinsic_instr *instr =
  567.       nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
  568.    instr->const_index[0] = ir->stream_id();
  569.    nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
  570. }
  571.  
  572. void
  573. nir_visitor::visit(ir_loop_jump *ir)
  574. {
  575.    nir_jump_type type;
  576.    switch (ir->mode) {
  577.    case ir_loop_jump::jump_break:
  578.       type = nir_jump_break;
  579.       break;
  580.    case ir_loop_jump::jump_continue:
  581.       type = nir_jump_continue;
  582.       break;
  583.    default:
  584.       unreachable("not reached");
  585.    }
  586.  
  587.    nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
  588.    nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
  589. }
  590.  
  591. void
  592. nir_visitor::visit(ir_return *ir)
  593. {
  594.    if (ir->value != NULL) {
  595.       nir_intrinsic_instr *copy =
  596.          nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
  597.  
  598.       copy->variables[0] = nir_deref_var_create(copy, this->impl->return_var);
  599.       copy->variables[1] = evaluate_deref(&copy->instr, ir->value);
  600.    }
  601.  
  602.    nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
  603.    nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
  604. }
  605.  
  606. void
  607. nir_visitor::visit(ir_call *ir)
  608. {
  609.    if (ir->callee->is_intrinsic) {
  610.       nir_intrinsic_op op;
  611.       if (strcmp(ir->callee_name(), "__intrinsic_atomic_read") == 0) {
  612.          op = nir_intrinsic_atomic_counter_read_var;
  613.       } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_increment") == 0) {
  614.          op = nir_intrinsic_atomic_counter_inc_var;
  615.       } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
  616.          op = nir_intrinsic_atomic_counter_dec_var;
  617.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_load") == 0) {
  618.          op = nir_intrinsic_image_load;
  619.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_store") == 0) {
  620.          op = nir_intrinsic_image_store;
  621.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_add") == 0) {
  622.          op = nir_intrinsic_image_atomic_add;
  623.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_min") == 0) {
  624.          op = nir_intrinsic_image_atomic_min;
  625.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_max") == 0) {
  626.          op = nir_intrinsic_image_atomic_max;
  627.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_and") == 0) {
  628.          op = nir_intrinsic_image_atomic_and;
  629.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_or") == 0) {
  630.          op = nir_intrinsic_image_atomic_or;
  631.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
  632.          op = nir_intrinsic_image_atomic_xor;
  633.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
  634.          op = nir_intrinsic_image_atomic_exchange;
  635.       } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
  636.          op = nir_intrinsic_image_atomic_comp_swap;
  637.       } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier") == 0) {
  638.          op = nir_intrinsic_memory_barrier;
  639.       } else {
  640.          unreachable("not reached");
  641.       }
  642.  
  643.       nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
  644.  
  645.       switch (op) {
  646.       case nir_intrinsic_atomic_counter_read_var:
  647.       case nir_intrinsic_atomic_counter_inc_var:
  648.       case nir_intrinsic_atomic_counter_dec_var: {
  649.          ir_dereference *param =
  650.             (ir_dereference *) ir->actual_parameters.get_head();
  651.          instr->variables[0] = evaluate_deref(&instr->instr, param);
  652.          nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
  653.          break;
  654.       }
  655.       case nir_intrinsic_image_load:
  656.       case nir_intrinsic_image_store:
  657.       case nir_intrinsic_image_atomic_add:
  658.       case nir_intrinsic_image_atomic_min:
  659.       case nir_intrinsic_image_atomic_max:
  660.       case nir_intrinsic_image_atomic_and:
  661.       case nir_intrinsic_image_atomic_or:
  662.       case nir_intrinsic_image_atomic_xor:
  663.       case nir_intrinsic_image_atomic_exchange:
  664.       case nir_intrinsic_image_atomic_comp_swap: {
  665.          nir_ssa_undef_instr *instr_undef =
  666.             nir_ssa_undef_instr_create(shader, 1);
  667.          nir_instr_insert_after_cf_list(this->cf_node_list,
  668.                                         &instr_undef->instr);
  669.  
  670.          /* Set the image variable dereference. */
  671.          exec_node *param = ir->actual_parameters.get_head();
  672.          ir_dereference *image = (ir_dereference *)param;
  673.          const glsl_type *type =
  674.             image->variable_referenced()->type->without_array();
  675.  
  676.          instr->variables[0] = evaluate_deref(&instr->instr, image);
  677.          param = param->get_next();
  678.  
  679.          /* Set the address argument, extending the coordinate vector to four
  680.           * components.
  681.           */
  682.          const nir_src src_addr = evaluate_rvalue((ir_dereference *)param);
  683.          nir_alu_instr *instr_addr = nir_alu_instr_create(shader, nir_op_vec4);
  684.          nir_ssa_dest_init(&instr_addr->instr, &instr_addr->dest.dest, 4, NULL);
  685.  
  686.          for (int i = 0; i < 4; i++) {
  687.             if (i < type->coordinate_components()) {
  688.                instr_addr->src[i].src = src_addr;
  689.                instr_addr->src[i].swizzle[0] = i;
  690.             } else {
  691.                instr_addr->src[i].src = nir_src_for_ssa(&instr_undef->def);
  692.             }
  693.          }
  694.  
  695.          nir_instr_insert_after_cf_list(cf_node_list, &instr_addr->instr);
  696.          instr->src[0] = nir_src_for_ssa(&instr_addr->dest.dest.ssa);
  697.          param = param->get_next();
  698.  
  699.          /* Set the sample argument, which is undefined for single-sample
  700.           * images.
  701.           */
  702.          if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
  703.             instr->src[1] = evaluate_rvalue((ir_dereference *)param);
  704.             param = param->get_next();
  705.          } else {
  706.             instr->src[1] = nir_src_for_ssa(&instr_undef->def);
  707.          }
  708.  
  709.          /* Set the intrinsic parameters. */
  710.          if (!param->is_tail_sentinel()) {
  711.             instr->src[2] = evaluate_rvalue((ir_dereference *)param);
  712.             param = param->get_next();
  713.          }
  714.  
  715.          if (!param->is_tail_sentinel()) {
  716.             instr->src[3] = evaluate_rvalue((ir_dereference *)param);
  717.             param = param->get_next();
  718.          }
  719.  
  720.          /* Set the intrinsic destination. */
  721.          if (ir->return_deref)
  722.             nir_ssa_dest_init(&instr->instr, &instr->dest,
  723.                               ir->return_deref->type->vector_elements, NULL);
  724.          break;
  725.       }
  726.       case nir_intrinsic_memory_barrier:
  727.          break;
  728.       default:
  729.          unreachable("not reached");
  730.       }
  731.  
  732.       nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
  733.  
  734.       if (ir->return_deref) {
  735.          nir_intrinsic_instr *store_instr =
  736.             nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
  737.          store_instr->num_components = ir->return_deref->type->vector_elements;
  738.  
  739.          store_instr->variables[0] =
  740.             evaluate_deref(&store_instr->instr, ir->return_deref);
  741.          store_instr->src[0] = nir_src_for_ssa(&instr->dest.ssa);
  742.  
  743.          nir_instr_insert_after_cf_list(this->cf_node_list,
  744.                                         &store_instr->instr);
  745.       }
  746.  
  747.       return;
  748.    }
  749.  
  750.    struct hash_entry *entry =
  751.       _mesa_hash_table_search(this->overload_table, ir->callee);
  752.    assert(entry);
  753.    nir_function_overload *callee = (nir_function_overload *) entry->data;
  754.  
  755.    nir_call_instr *instr = nir_call_instr_create(this->shader, callee);
  756.  
  757.    unsigned i = 0;
  758.    foreach_in_list(ir_dereference, param, &ir->actual_parameters) {
  759.       instr->params[i] = evaluate_deref(&instr->instr, param);
  760.       i++;
  761.    }
  762.  
  763.    instr->return_deref = evaluate_deref(&instr->instr, ir->return_deref);
  764.    nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
  765. }
  766.  
  767. void
  768. nir_visitor::visit(ir_assignment *ir)
  769. {
  770.    unsigned num_components = ir->lhs->type->vector_elements;
  771.  
  772.    if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
  773.        (ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
  774.       /* We're doing a plain-as-can-be copy, so emit a copy_var */
  775.       nir_intrinsic_instr *copy =
  776.          nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
  777.  
  778.       copy->variables[0] = evaluate_deref(&copy->instr, ir->lhs);
  779.       copy->variables[1] = evaluate_deref(&copy->instr, ir->rhs);
  780.  
  781.       if (ir->condition) {
  782.          nir_if *if_stmt = nir_if_create(this->shader);
  783.          if_stmt->condition = evaluate_rvalue(ir->condition);
  784.          nir_cf_node_insert_end(this->cf_node_list, &if_stmt->cf_node);
  785.          nir_instr_insert_after_cf_list(&if_stmt->then_list, &copy->instr);
  786.       } else {
  787.          nir_instr_insert_after_cf_list(this->cf_node_list, &copy->instr);
  788.       }
  789.       return;
  790.    }
  791.  
  792.    assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
  793.  
  794.    ir->lhs->accept(this);
  795.    nir_deref_var *lhs_deref = this->deref_head;
  796.    nir_src src = evaluate_rvalue(ir->rhs);
  797.  
  798.    if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
  799.       /*
  800.        * We have no good way to update only part of a variable, so just load
  801.        * the LHS and do a vec operation to combine the old with the new, and
  802.        * then store it
  803.        * back into the LHS. Copy propagation should get rid of the mess.
  804.        */
  805.  
  806.       nir_intrinsic_instr *load =
  807.          nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
  808.       load->num_components = ir->lhs->type->vector_elements;
  809.       nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
  810.       load->variables[0] = lhs_deref;
  811.       ralloc_steal(load, load->variables[0]);
  812.       nir_instr_insert_after_cf_list(this->cf_node_list, &load->instr);
  813.  
  814.       nir_op vec_op;
  815.       switch (ir->lhs->type->vector_elements) {
  816.          case 1: vec_op = nir_op_imov; break;
  817.          case 2: vec_op = nir_op_vec2; break;
  818.          case 3: vec_op = nir_op_vec3; break;
  819.          case 4: vec_op = nir_op_vec4; break;
  820.          default: unreachable("Invalid number of components"); break;
  821.       }
  822.       nir_alu_instr *vec = nir_alu_instr_create(this->shader, vec_op);
  823.       nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components, NULL);
  824.       vec->dest.write_mask = (1 << num_components) - 1;
  825.  
  826.       unsigned component = 0;
  827.       for (unsigned i = 0; i < ir->lhs->type->vector_elements; i++) {
  828.          if (ir->write_mask & (1 << i)) {
  829.             vec->src[i].src = src;
  830.  
  831.             /* GLSL IR will give us the input to the write-masked assignment
  832.              * in a single packed vector.  So, for example, if the
  833.              * writemask is xzw, then we have to swizzle x -> x, y -> z,
  834.              * and z -> w and get the y component from the load.
  835.              */
  836.             vec->src[i].swizzle[0] = component++;
  837.          } else {
  838.             vec->src[i].src.is_ssa = true;
  839.             vec->src[i].src.ssa = &load->dest.ssa;
  840.             vec->src[i].swizzle[0] = i;
  841.          }
  842.       }
  843.  
  844.       nir_instr_insert_after_cf_list(this->cf_node_list, &vec->instr);
  845.  
  846.       src.is_ssa = true;
  847.       src.ssa = &vec->dest.dest.ssa;
  848.    }
  849.  
  850.    nir_intrinsic_instr *store =
  851.       nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
  852.    store->num_components = ir->lhs->type->vector_elements;
  853.    nir_deref *store_deref = nir_copy_deref(store, &lhs_deref->deref);
  854.    store->variables[0] = nir_deref_as_var(store_deref);
  855.    store->src[0] = src;
  856.  
  857.    if (ir->condition) {
  858.       nir_if *if_stmt = nir_if_create(this->shader);
  859.       if_stmt->condition = evaluate_rvalue(ir->condition);
  860.       nir_cf_node_insert_end(this->cf_node_list, &if_stmt->cf_node);
  861.       nir_instr_insert_after_cf_list(&if_stmt->then_list, &store->instr);
  862.    } else {
  863.       nir_instr_insert_after_cf_list(this->cf_node_list, &store->instr);
  864.    }
  865. }
  866.  
  867. /*
  868.  * Given an instruction, returns a pointer to its destination or NULL if there
  869.  * is no destination.
  870.  *
  871.  * Note that this only handles instructions we generate at this level.
  872.  */
  873. static nir_dest *
  874. get_instr_dest(nir_instr *instr)
  875. {
  876.    nir_alu_instr *alu_instr;
  877.    nir_intrinsic_instr *intrinsic_instr;
  878.    nir_tex_instr *tex_instr;
  879.  
  880.    switch (instr->type) {
  881.       case nir_instr_type_alu:
  882.          alu_instr = nir_instr_as_alu(instr);
  883.          return &alu_instr->dest.dest;
  884.  
  885.       case nir_instr_type_intrinsic:
  886.          intrinsic_instr = nir_instr_as_intrinsic(instr);
  887.          if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
  888.             return &intrinsic_instr->dest;
  889.          else
  890.             return NULL;
  891.  
  892.       case nir_instr_type_tex:
  893.          tex_instr = nir_instr_as_tex(instr);
  894.          return &tex_instr->dest;
  895.  
  896.       default:
  897.          unreachable("not reached");
  898.    }
  899.  
  900.    return NULL;
  901. }
  902.  
  903. void
  904. nir_visitor::add_instr(nir_instr *instr, unsigned num_components)
  905. {
  906.    nir_dest *dest = get_instr_dest(instr);
  907.  
  908.    nir_ssa_dest_init(instr, dest, num_components, NULL);
  909.  
  910.    nir_instr_insert_after_cf_list(this->cf_node_list, instr);
  911.    this->result = instr;
  912. }
  913.  
  914. nir_src
  915. nir_visitor::evaluate_rvalue(ir_rvalue* ir)
  916. {
  917.    ir->accept(this);
  918.    if (ir->as_dereference() || ir->as_constant()) {
  919.       /*
  920.        * A dereference is being used on the right hand side, which means we
  921.        * must emit a variable load.
  922.        */
  923.  
  924.       nir_intrinsic_instr *load_instr =
  925.          nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
  926.       load_instr->num_components = ir->type->vector_elements;
  927.       load_instr->variables[0] = this->deref_head;
  928.       ralloc_steal(load_instr, load_instr->variables[0]);
  929.       add_instr(&load_instr->instr, ir->type->vector_elements);
  930.    }
  931.  
  932.    nir_dest *dest = get_instr_dest(this->result);
  933.  
  934.    assert(dest->is_ssa);
  935.    nir_src src = NIR_SRC_INIT;
  936.    src.is_ssa = true;
  937.    src.ssa = &dest->ssa;
  938.  
  939.    return src;
  940. }
  941.  
  942. nir_alu_instr *
  943. nir_visitor::emit(nir_op op, unsigned dest_size, nir_src *srcs)
  944. {
  945.    nir_alu_instr *instr = nir_alu_instr_create(this->shader, op);
  946.    for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
  947.       instr->src[i].src = srcs[i];
  948.    instr->dest.write_mask = (1 << dest_size) - 1;
  949.    add_instr(&instr->instr, dest_size);
  950.    return instr;
  951. }
  952.  
  953. nir_alu_instr *
  954. nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1)
  955. {
  956.    assert(nir_op_infos[op].num_inputs == 1);
  957.    return emit(op, dest_size, &src1);
  958. }
  959.  
  960. nir_alu_instr *
  961. nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
  962.                   nir_src src2)
  963. {
  964.    assert(nir_op_infos[op].num_inputs == 2);
  965.    nir_src srcs[] = { src1, src2 };
  966.    return emit(op, dest_size, srcs);
  967. }
  968.  
  969. nir_alu_instr *
  970. nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
  971.                   nir_src src2, nir_src src3)
  972. {
  973.    assert(nir_op_infos[op].num_inputs == 3);
  974.    nir_src srcs[] = { src1, src2, src3 };
  975.    return emit(op, dest_size, srcs);
  976. }
  977.  
  978. void
  979. nir_visitor::visit(ir_expression *ir)
  980. {
  981.    /* Some special cases */
  982.    switch (ir->operation) {
  983.    case ir_binop_ubo_load: {
  984.       ir_constant *const_index = ir->operands[1]->as_constant();
  985.  
  986.       nir_intrinsic_op op;
  987.       if (const_index) {
  988.          op = nir_intrinsic_load_ubo;
  989.       } else {
  990.          op = nir_intrinsic_load_ubo_indirect;
  991.       }
  992.       nir_intrinsic_instr *load = nir_intrinsic_instr_create(this->shader, op);
  993.       load->num_components = ir->type->vector_elements;
  994.       load->const_index[0] = const_index ? const_index->value.u[0] : 0; /* base offset */
  995.       load->const_index[1] = 1; /* number of vec4's */
  996.       load->src[0] = evaluate_rvalue(ir->operands[0]);
  997.       if (!const_index)
  998.          load->src[1] = evaluate_rvalue(ir->operands[1]);
  999.       add_instr(&load->instr, ir->type->vector_elements);
  1000.  
  1001.       /*
  1002.        * In UBO's, a true boolean value is any non-zero value, but we consider
  1003.        * a true boolean to be ~0. Fix this up with a != 0 comparison.
  1004.        */
  1005.  
  1006.       if (ir->type->base_type == GLSL_TYPE_BOOL) {
  1007.          nir_load_const_instr *const_zero = nir_load_const_instr_create(shader, 1);
  1008.          const_zero->value.u[0] = 0;
  1009.          nir_instr_insert_after_cf_list(this->cf_node_list, &const_zero->instr);
  1010.  
  1011.          nir_alu_instr *compare = nir_alu_instr_create(shader, nir_op_ine);
  1012.          compare->src[0].src.is_ssa = true;
  1013.          compare->src[0].src.ssa = &load->dest.ssa;
  1014.          compare->src[1].src.is_ssa = true;
  1015.          compare->src[1].src.ssa = &const_zero->def;
  1016.          for (unsigned i = 0; i < ir->type->vector_elements; i++)
  1017.             compare->src[1].swizzle[i] = 0;
  1018.          compare->dest.write_mask = (1 << ir->type->vector_elements) - 1;
  1019.  
  1020.          add_instr(&compare->instr, ir->type->vector_elements);
  1021.       }
  1022.  
  1023.       return;
  1024.    }
  1025.  
  1026.    case ir_unop_interpolate_at_centroid:
  1027.    case ir_binop_interpolate_at_offset:
  1028.    case ir_binop_interpolate_at_sample: {
  1029.       ir_dereference *deref = ir->operands[0]->as_dereference();
  1030.       ir_swizzle *swizzle = NULL;
  1031.       if (!deref) {
  1032.          /* the api does not allow a swizzle here, but the varying packing code
  1033.           * may have pushed one into here.
  1034.           */
  1035.          swizzle = ir->operands[0]->as_swizzle();
  1036.          assert(swizzle);
  1037.          deref = swizzle->val->as_dereference();
  1038.          assert(deref);
  1039.       }
  1040.  
  1041.       deref->accept(this);
  1042.  
  1043.       nir_intrinsic_op op;
  1044.       if (this->deref_head->var->data.mode == nir_var_shader_in) {
  1045.          switch (ir->operation) {
  1046.          case ir_unop_interpolate_at_centroid:
  1047.             op = nir_intrinsic_interp_var_at_centroid;
  1048.             break;
  1049.          case ir_binop_interpolate_at_offset:
  1050.             op = nir_intrinsic_interp_var_at_offset;
  1051.             break;
  1052.          case ir_binop_interpolate_at_sample:
  1053.             op = nir_intrinsic_interp_var_at_sample;
  1054.             break;
  1055.          default:
  1056.             unreachable("Invalid interpolation intrinsic");
  1057.          }
  1058.       } else {
  1059.          /* This case can happen if the vertex shader does not write the
  1060.           * given varying.  In this case, the linker will lower it to a
  1061.           * global variable.  Since interpolating a variable makes no
  1062.           * sense, we'll just turn it into a load which will probably
  1063.           * eventually end up as an SSA definition.
  1064.           */
  1065.          assert(this->deref_head->var->data.mode == nir_var_global);
  1066.          op = nir_intrinsic_load_var;
  1067.       }
  1068.  
  1069.       nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
  1070.       intrin->num_components = deref->type->vector_elements;
  1071.       intrin->variables[0] = this->deref_head;
  1072.       ralloc_steal(intrin, intrin->variables[0]);
  1073.  
  1074.       if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
  1075.           intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
  1076.          intrin->src[0] = evaluate_rvalue(ir->operands[1]);
  1077.  
  1078.       add_instr(&intrin->instr, deref->type->vector_elements);
  1079.  
  1080.       if (swizzle) {
  1081.          nir_alu_instr *mov = nir_alu_instr_create(shader, nir_op_imov);
  1082.          mov->dest.write_mask = (1 << swizzle->type->vector_elements) - 1;
  1083.          mov->src[0].src.is_ssa = true;
  1084.          mov->src[0].src.ssa = &intrin->dest.ssa;
  1085.  
  1086.          mov->src[0].swizzle[0] = swizzle->mask.x;
  1087.          mov->src[0].swizzle[1] = swizzle->mask.y;
  1088.          mov->src[0].swizzle[2] = swizzle->mask.z;
  1089.          mov->src[0].swizzle[3] = swizzle->mask.w;
  1090.          for (unsigned i = deref->type->vector_elements; i < 4; i++)
  1091.             mov->src[0].swizzle[i] = 0;
  1092.  
  1093.          add_instr(&mov->instr, swizzle->type->vector_elements);
  1094.       }
  1095.  
  1096.       return;
  1097.    }
  1098.  
  1099.    default:
  1100.       break;
  1101.    }
  1102.  
  1103.    nir_src srcs[4];
  1104.    for (unsigned i = 0; i < ir->get_num_operands(); i++)
  1105.       srcs[i] = evaluate_rvalue(ir->operands[i]);
  1106.  
  1107.    glsl_base_type types[4];
  1108.    for (unsigned i = 0; i < ir->get_num_operands(); i++)
  1109.       if (supports_ints)
  1110.          types[i] = ir->operands[i]->type->base_type;
  1111.       else
  1112.          types[i] = GLSL_TYPE_FLOAT;
  1113.  
  1114.    glsl_base_type out_type;
  1115.    if (supports_ints)
  1116.       out_type = ir->type->base_type;
  1117.    else
  1118.       out_type = GLSL_TYPE_FLOAT;
  1119.  
  1120.    unsigned dest_size = ir->type->vector_elements;
  1121.  
  1122.    nir_alu_instr *instr;
  1123.    nir_op op;
  1124.  
  1125.    switch (ir->operation) {
  1126.    case ir_unop_bit_not: emit(nir_op_inot, dest_size, srcs); break;
  1127.    case ir_unop_logic_not:
  1128.       emit(supports_ints ? nir_op_inot : nir_op_fnot, dest_size, srcs);
  1129.       break;
  1130.    case ir_unop_neg:
  1131.       instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fneg : nir_op_ineg,
  1132.                    dest_size, srcs);
  1133.       break;
  1134.    case ir_unop_abs:
  1135.       instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fabs : nir_op_iabs,
  1136.                    dest_size, srcs);
  1137.       break;
  1138.    case ir_unop_saturate:
  1139.       assert(types[0] == GLSL_TYPE_FLOAT);
  1140.       instr = emit(nir_op_fsat, dest_size, srcs);
  1141.       break;
  1142.    case ir_unop_sign:
  1143.       emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fsign : nir_op_isign,
  1144.            dest_size, srcs);
  1145.       break;
  1146.    case ir_unop_rcp:  emit(nir_op_frcp, dest_size, srcs);  break;
  1147.    case ir_unop_rsq:  emit(nir_op_frsq, dest_size, srcs);  break;
  1148.    case ir_unop_sqrt: emit(nir_op_fsqrt, dest_size, srcs); break;
  1149.    case ir_unop_exp:  unreachable("ir_unop_exp should have been lowered");
  1150.    case ir_unop_log:  unreachable("ir_unop_log should have been lowered");
  1151.    case ir_unop_exp2: emit(nir_op_fexp2, dest_size, srcs); break;
  1152.    case ir_unop_log2: emit(nir_op_flog2, dest_size, srcs); break;
  1153.    case ir_unop_i2f:
  1154.       emit(supports_ints ? nir_op_i2f : nir_op_fmov, dest_size, srcs);
  1155.       break;
  1156.    case ir_unop_u2f:
  1157.       emit(supports_ints ? nir_op_u2f : nir_op_fmov, dest_size, srcs);
  1158.       break;
  1159.    case ir_unop_b2f:
  1160.       emit(supports_ints ? nir_op_b2f : nir_op_fmov, dest_size, srcs);
  1161.       break;
  1162.    case ir_unop_f2i:  emit(nir_op_f2i, dest_size, srcs);   break;
  1163.    case ir_unop_f2u:  emit(nir_op_f2u, dest_size, srcs);   break;
  1164.    case ir_unop_f2b:  emit(nir_op_f2b, dest_size, srcs);   break;
  1165.    case ir_unop_i2b:  emit(nir_op_i2b, dest_size, srcs);   break;
  1166.    case ir_unop_b2i:  emit(nir_op_b2i, dest_size, srcs);   break;
  1167.    case ir_unop_i2u:
  1168.    case ir_unop_u2i:
  1169.    case ir_unop_bitcast_i2f:
  1170.    case ir_unop_bitcast_f2i:
  1171.    case ir_unop_bitcast_u2f:
  1172.    case ir_unop_bitcast_f2u:
  1173.       /* no-op */
  1174.       emit(nir_op_imov, dest_size, srcs);
  1175.       break;
  1176.    case ir_unop_any:
  1177.       switch (ir->operands[0]->type->vector_elements) {
  1178.       case 2:
  1179.          emit(supports_ints ? nir_op_bany2 : nir_op_fany2,
  1180.               dest_size, srcs);
  1181.          break;
  1182.       case 3:
  1183.          emit(supports_ints ? nir_op_bany3 : nir_op_fany3,
  1184.               dest_size, srcs);
  1185.          break;
  1186.       case 4:
  1187.          emit(supports_ints ? nir_op_bany4 : nir_op_fany4,
  1188.               dest_size, srcs);
  1189.          break;
  1190.       default:
  1191.          unreachable("not reached");
  1192.       }
  1193.       break;
  1194.    case ir_unop_trunc: emit(nir_op_ftrunc, dest_size, srcs); break;
  1195.    case ir_unop_ceil:  emit(nir_op_fceil,  dest_size, srcs); break;
  1196.    case ir_unop_floor: emit(nir_op_ffloor, dest_size, srcs); break;
  1197.    case ir_unop_fract: emit(nir_op_ffract, dest_size, srcs); break;
  1198.    case ir_unop_round_even: emit(nir_op_fround_even, dest_size, srcs); break;
  1199.    case ir_unop_sin:   emit(nir_op_fsin,   dest_size, srcs); break;
  1200.    case ir_unop_cos:   emit(nir_op_fcos,   dest_size, srcs); break;
  1201.    case ir_unop_dFdx:        emit(nir_op_fddx,        dest_size, srcs); break;
  1202.    case ir_unop_dFdy:        emit(nir_op_fddy,        dest_size, srcs); break;
  1203.    case ir_unop_dFdx_fine:   emit(nir_op_fddx_fine,   dest_size, srcs); break;
  1204.    case ir_unop_dFdy_fine:   emit(nir_op_fddy_fine,   dest_size, srcs); break;
  1205.    case ir_unop_dFdx_coarse: emit(nir_op_fddx_coarse, dest_size, srcs); break;
  1206.    case ir_unop_dFdy_coarse: emit(nir_op_fddy_coarse, dest_size, srcs); break;
  1207.    case ir_unop_pack_snorm_2x16:
  1208.       emit(nir_op_pack_snorm_2x16, dest_size, srcs);
  1209.       break;
  1210.    case ir_unop_pack_snorm_4x8:
  1211.       emit(nir_op_pack_snorm_4x8, dest_size, srcs);
  1212.       break;
  1213.    case ir_unop_pack_unorm_2x16:
  1214.       emit(nir_op_pack_unorm_2x16, dest_size, srcs);
  1215.       break;
  1216.    case ir_unop_pack_unorm_4x8:
  1217.       emit(nir_op_pack_unorm_4x8, dest_size, srcs);
  1218.       break;
  1219.    case ir_unop_pack_half_2x16:
  1220.       emit(nir_op_pack_half_2x16, dest_size, srcs);
  1221.       break;
  1222.    case ir_unop_unpack_snorm_2x16:
  1223.       emit(nir_op_unpack_snorm_2x16, dest_size, srcs);
  1224.       break;
  1225.    case ir_unop_unpack_snorm_4x8:
  1226.       emit(nir_op_unpack_snorm_4x8, dest_size, srcs);
  1227.       break;
  1228.    case ir_unop_unpack_unorm_2x16:
  1229.       emit(nir_op_unpack_unorm_2x16, dest_size, srcs);
  1230.       break;
  1231.    case ir_unop_unpack_unorm_4x8:
  1232.       emit(nir_op_unpack_unorm_4x8, dest_size, srcs);
  1233.       break;
  1234.    case ir_unop_unpack_half_2x16:
  1235.       emit(nir_op_unpack_half_2x16, dest_size, srcs);
  1236.       break;
  1237.    case ir_unop_unpack_half_2x16_split_x:
  1238.       emit(nir_op_unpack_half_2x16_split_x, dest_size, srcs);
  1239.       break;
  1240.    case ir_unop_unpack_half_2x16_split_y:
  1241.       emit(nir_op_unpack_half_2x16_split_y, dest_size, srcs);
  1242.       break;
  1243.    case ir_unop_bitfield_reverse:
  1244.       emit(nir_op_bitfield_reverse, dest_size, srcs);
  1245.       break;
  1246.    case ir_unop_bit_count:
  1247.       emit(nir_op_bit_count, dest_size, srcs);
  1248.       break;
  1249.    case ir_unop_find_msb:
  1250.       switch (types[0]) {
  1251.       case GLSL_TYPE_UINT:
  1252.          emit(nir_op_ufind_msb, dest_size, srcs);
  1253.          break;
  1254.       case GLSL_TYPE_INT:
  1255.          emit(nir_op_ifind_msb, dest_size, srcs);
  1256.          break;
  1257.       default:
  1258.          unreachable("Invalid type for findMSB()");
  1259.       }
  1260.       break;
  1261.    case ir_unop_find_lsb:
  1262.       emit(nir_op_find_lsb,  dest_size, srcs);
  1263.       break;
  1264.  
  1265.    case ir_unop_noise:
  1266.       switch (ir->type->vector_elements) {
  1267.       case 1:
  1268.          switch (ir->operands[0]->type->vector_elements) {
  1269.             case 1: emit(nir_op_fnoise1_1, dest_size, srcs); break;
  1270.             case 2: emit(nir_op_fnoise1_2, dest_size, srcs); break;
  1271.             case 3: emit(nir_op_fnoise1_3, dest_size, srcs); break;
  1272.             case 4: emit(nir_op_fnoise1_4, dest_size, srcs); break;
  1273.             default: unreachable("not reached");
  1274.          }
  1275.          break;
  1276.       case 2:
  1277.          switch (ir->operands[0]->type->vector_elements) {
  1278.             case 1: emit(nir_op_fnoise2_1, dest_size, srcs); break;
  1279.             case 2: emit(nir_op_fnoise2_2, dest_size, srcs); break;
  1280.             case 3: emit(nir_op_fnoise2_3, dest_size, srcs); break;
  1281.             case 4: emit(nir_op_fnoise2_4, dest_size, srcs); break;
  1282.             default: unreachable("not reached");
  1283.          }
  1284.          break;
  1285.       case 3:
  1286.          switch (ir->operands[0]->type->vector_elements) {
  1287.             case 1: emit(nir_op_fnoise3_1, dest_size, srcs); break;
  1288.             case 2: emit(nir_op_fnoise3_2, dest_size, srcs); break;
  1289.             case 3: emit(nir_op_fnoise3_3, dest_size, srcs); break;
  1290.             case 4: emit(nir_op_fnoise3_4, dest_size, srcs); break;
  1291.             default: unreachable("not reached");
  1292.          }
  1293.          break;
  1294.       case 4:
  1295.          switch (ir->operands[0]->type->vector_elements) {
  1296.             case 1: emit(nir_op_fnoise4_1, dest_size, srcs); break;
  1297.             case 2: emit(nir_op_fnoise4_2, dest_size, srcs); break;
  1298.             case 3: emit(nir_op_fnoise4_3, dest_size, srcs); break;
  1299.             case 4: emit(nir_op_fnoise4_4, dest_size, srcs); break;
  1300.             default: unreachable("not reached");
  1301.          }
  1302.          break;
  1303.       default:
  1304.          unreachable("not reached");
  1305.       }
  1306.       break;
  1307.    case ir_binop_add:
  1308.    case ir_binop_sub:
  1309.    case ir_binop_mul:
  1310.    case ir_binop_div:
  1311.    case ir_binop_mod:
  1312.    case ir_binop_min:
  1313.    case ir_binop_max:
  1314.    case ir_binop_pow:
  1315.    case ir_binop_bit_and:
  1316.    case ir_binop_bit_or:
  1317.    case ir_binop_bit_xor:
  1318.    case ir_binop_logic_and:
  1319.    case ir_binop_logic_or:
  1320.    case ir_binop_logic_xor:
  1321.    case ir_binop_lshift:
  1322.    case ir_binop_rshift:
  1323.       switch (ir->operation) {
  1324.       case ir_binop_add:
  1325.          if (out_type == GLSL_TYPE_FLOAT)
  1326.             op = nir_op_fadd;
  1327.          else
  1328.             op = nir_op_iadd;
  1329.          break;
  1330.       case ir_binop_sub:
  1331.          if (out_type == GLSL_TYPE_FLOAT)
  1332.             op = nir_op_fsub;
  1333.          else
  1334.             op = nir_op_isub;
  1335.          break;
  1336.       case ir_binop_mul:
  1337.          if (out_type == GLSL_TYPE_FLOAT)
  1338.             op = nir_op_fmul;
  1339.          else
  1340.             op = nir_op_imul;
  1341.          break;
  1342.       case ir_binop_div:
  1343.          if (out_type == GLSL_TYPE_FLOAT)
  1344.             op = nir_op_fdiv;
  1345.          else if (out_type == GLSL_TYPE_INT)
  1346.             op = nir_op_idiv;
  1347.          else
  1348.             op = nir_op_udiv;
  1349.          break;
  1350.       case ir_binop_mod:
  1351.          if (out_type == GLSL_TYPE_FLOAT)
  1352.             op = nir_op_fmod;
  1353.          else
  1354.             op = nir_op_umod;
  1355.          break;
  1356.       case ir_binop_min:
  1357.          if (out_type == GLSL_TYPE_FLOAT)
  1358.             op = nir_op_fmin;
  1359.          else if (out_type == GLSL_TYPE_INT)
  1360.             op = nir_op_imin;
  1361.          else
  1362.             op = nir_op_umin;
  1363.          break;
  1364.       case ir_binop_max:
  1365.          if (out_type == GLSL_TYPE_FLOAT)
  1366.             op = nir_op_fmax;
  1367.          else if (out_type == GLSL_TYPE_INT)
  1368.             op = nir_op_imax;
  1369.          else
  1370.             op = nir_op_umax;
  1371.          break;
  1372.       case ir_binop_bit_and:
  1373.          op = nir_op_iand;
  1374.          break;
  1375.       case ir_binop_bit_or:
  1376.          op = nir_op_ior;
  1377.          break;
  1378.       case ir_binop_bit_xor:
  1379.          op = nir_op_ixor;
  1380.          break;
  1381.       case ir_binop_logic_and:
  1382.          if (supports_ints)
  1383.             op = nir_op_iand;
  1384.          else
  1385.             op = nir_op_fand;
  1386.          break;
  1387.       case ir_binop_logic_or:
  1388.          if (supports_ints)
  1389.             op = nir_op_ior;
  1390.          else
  1391.             op = nir_op_for;
  1392.          break;
  1393.       case ir_binop_logic_xor:
  1394.          if (supports_ints)
  1395.             op = nir_op_ixor;
  1396.          else
  1397.             op = nir_op_fxor;
  1398.          break;
  1399.       case ir_binop_lshift:
  1400.          op = nir_op_ishl;
  1401.          break;
  1402.       case ir_binop_rshift:
  1403.          if (out_type == GLSL_TYPE_INT)
  1404.             op = nir_op_ishr;
  1405.          else
  1406.             op = nir_op_ushr;
  1407.          break;
  1408.       case ir_binop_pow:
  1409.          op = nir_op_fpow;
  1410.          break;
  1411.  
  1412.       default:
  1413.          unreachable("not reached");
  1414.       }
  1415.  
  1416.       instr = emit(op, dest_size, srcs);
  1417.  
  1418.       if (ir->operands[0]->type->vector_elements != 1 &&
  1419.           ir->operands[1]->type->vector_elements == 1) {
  1420.          for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
  1421.               i++) {
  1422.             instr->src[1].swizzle[i] = 0;
  1423.          }
  1424.       }
  1425.  
  1426.       if (ir->operands[1]->type->vector_elements != 1 &&
  1427.           ir->operands[0]->type->vector_elements == 1) {
  1428.          for (unsigned i = 0; i < ir->operands[1]->type->vector_elements;
  1429.               i++) {
  1430.             instr->src[0].swizzle[i] = 0;
  1431.          }
  1432.       }
  1433.  
  1434.       break;
  1435.    case ir_binop_imul_high:
  1436.       emit(out_type == GLSL_TYPE_UINT ? nir_op_umul_high : nir_op_imul_high,
  1437.            dest_size, srcs);
  1438.       break;
  1439.    case ir_binop_carry:  emit(nir_op_uadd_carry, dest_size, srcs);  break;
  1440.    case ir_binop_borrow: emit(nir_op_usub_borrow, dest_size, srcs); break;
  1441.    case ir_binop_less:
  1442.       if (supports_ints) {
  1443.          if (types[0] == GLSL_TYPE_FLOAT)
  1444.             emit(nir_op_flt, dest_size, srcs);
  1445.          else if (types[0] == GLSL_TYPE_INT)
  1446.             emit(nir_op_ilt, dest_size, srcs);
  1447.          else
  1448.             emit(nir_op_ult, dest_size, srcs);
  1449.       } else {
  1450.          emit(nir_op_slt, dest_size, srcs);
  1451.       }
  1452.       break;
  1453.    case ir_binop_greater:
  1454.       if (supports_ints) {
  1455.          if (types[0] == GLSL_TYPE_FLOAT)
  1456.             emit(nir_op_flt, dest_size, srcs[1], srcs[0]);
  1457.          else if (types[0] == GLSL_TYPE_INT)
  1458.             emit(nir_op_ilt, dest_size, srcs[1], srcs[0]);
  1459.          else
  1460.             emit(nir_op_ult, dest_size, srcs[1], srcs[0]);
  1461.       } else {
  1462.          emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
  1463.       }
  1464.       break;
  1465.    case ir_binop_lequal:
  1466.       if (supports_ints) {
  1467.          if (types[0] == GLSL_TYPE_FLOAT)
  1468.             emit(nir_op_fge, dest_size, srcs[1], srcs[0]);
  1469.          else if (types[0] == GLSL_TYPE_INT)
  1470.             emit(nir_op_ige, dest_size, srcs[1], srcs[0]);
  1471.          else
  1472.             emit(nir_op_uge, dest_size, srcs[1], srcs[0]);
  1473.       } else {
  1474.          emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
  1475.       }
  1476.       break;
  1477.    case ir_binop_gequal:
  1478.       if (supports_ints) {
  1479.          if (types[0] == GLSL_TYPE_FLOAT)
  1480.             emit(nir_op_fge, dest_size, srcs);
  1481.          else if (types[0] == GLSL_TYPE_INT)
  1482.             emit(nir_op_ige, dest_size, srcs);
  1483.          else
  1484.             emit(nir_op_uge, dest_size, srcs);
  1485.       } else {
  1486.          emit(nir_op_slt, dest_size, srcs);
  1487.       }
  1488.       break;
  1489.    case ir_binop_equal:
  1490.       if (supports_ints) {
  1491.          if (types[0] == GLSL_TYPE_FLOAT)
  1492.             emit(nir_op_feq, dest_size, srcs);
  1493.          else
  1494.             emit(nir_op_ieq, dest_size, srcs);
  1495.       } else {
  1496.          emit(nir_op_seq, dest_size, srcs);
  1497.       }
  1498.       break;
  1499.    case ir_binop_nequal:
  1500.       if (supports_ints) {
  1501.          if (types[0] == GLSL_TYPE_FLOAT)
  1502.             emit(nir_op_fne, dest_size, srcs);
  1503.          else
  1504.             emit(nir_op_ine, dest_size, srcs);
  1505.       } else {
  1506.          emit(nir_op_sne, dest_size, srcs);
  1507.       }
  1508.       break;
  1509.    case ir_binop_all_equal:
  1510.       if (supports_ints) {
  1511.          if (types[0] == GLSL_TYPE_FLOAT) {
  1512.             switch (ir->operands[0]->type->vector_elements) {
  1513.                case 1: emit(nir_op_feq, dest_size, srcs); break;
  1514.                case 2: emit(nir_op_ball_fequal2, dest_size, srcs); break;
  1515.                case 3: emit(nir_op_ball_fequal3, dest_size, srcs); break;
  1516.                case 4: emit(nir_op_ball_fequal4, dest_size, srcs); break;
  1517.                default:
  1518.                   unreachable("not reached");
  1519.             }
  1520.          } else {
  1521.             switch (ir->operands[0]->type->vector_elements) {
  1522.                case 1: emit(nir_op_ieq, dest_size, srcs); break;
  1523.                case 2: emit(nir_op_ball_iequal2, dest_size, srcs); break;
  1524.                case 3: emit(nir_op_ball_iequal3, dest_size, srcs); break;
  1525.                case 4: emit(nir_op_ball_iequal4, dest_size, srcs); break;
  1526.                default:
  1527.                   unreachable("not reached");
  1528.             }
  1529.          }
  1530.       } else {
  1531.          switch (ir->operands[0]->type->vector_elements) {
  1532.             case 1: emit(nir_op_seq, dest_size, srcs); break;
  1533.             case 2: emit(nir_op_fall_equal2, dest_size, srcs); break;
  1534.             case 3: emit(nir_op_fall_equal3, dest_size, srcs); break;
  1535.             case 4: emit(nir_op_fall_equal4, dest_size, srcs); break;
  1536.             default:
  1537.                unreachable("not reached");
  1538.          }
  1539.       }
  1540.       break;
  1541.    case ir_binop_any_nequal:
  1542.       if (supports_ints) {
  1543.          if (types[0] == GLSL_TYPE_FLOAT) {
  1544.             switch (ir->operands[0]->type->vector_elements) {
  1545.                case 1: emit(nir_op_fne, dest_size, srcs); break;
  1546.                case 2: emit(nir_op_bany_fnequal2, dest_size, srcs); break;
  1547.                case 3: emit(nir_op_bany_fnequal3, dest_size, srcs); break;
  1548.                case 4: emit(nir_op_bany_fnequal4, dest_size, srcs); break;
  1549.                default:
  1550.                   unreachable("not reached");
  1551.             }
  1552.          } else {
  1553.             switch (ir->operands[0]->type->vector_elements) {
  1554.                case 1: emit(nir_op_ine, dest_size, srcs); break;
  1555.                case 2: emit(nir_op_bany_inequal2, dest_size, srcs); break;
  1556.                case 3: emit(nir_op_bany_inequal3, dest_size, srcs); break;
  1557.                case 4: emit(nir_op_bany_inequal4, dest_size, srcs); break;
  1558.                default:
  1559.                   unreachable("not reached");
  1560.             }
  1561.          }
  1562.       } else {
  1563.          switch (ir->operands[0]->type->vector_elements) {
  1564.             case 1: emit(nir_op_sne, dest_size, srcs); break;
  1565.             case 2: emit(nir_op_fany_nequal2, dest_size, srcs); break;
  1566.             case 3: emit(nir_op_fany_nequal3, dest_size, srcs); break;
  1567.             case 4: emit(nir_op_fany_nequal4, dest_size, srcs); break;
  1568.             default:
  1569.                unreachable("not reached");
  1570.          }
  1571.       }
  1572.       break;
  1573.    case ir_binop_dot:
  1574.       switch (ir->operands[0]->type->vector_elements) {
  1575.          case 2: emit(nir_op_fdot2, dest_size, srcs); break;
  1576.          case 3: emit(nir_op_fdot3, dest_size, srcs); break;
  1577.          case 4: emit(nir_op_fdot4, dest_size, srcs); break;
  1578.          default:
  1579.             unreachable("not reached");
  1580.       }
  1581.       break;
  1582.  
  1583.    case ir_binop_pack_half_2x16_split:
  1584.          emit(nir_op_pack_half_2x16_split, dest_size, srcs);
  1585.          break;
  1586.    case ir_binop_bfm:   emit(nir_op_bfm, dest_size, srcs);   break;
  1587.    case ir_binop_ldexp: emit(nir_op_ldexp, dest_size, srcs); break;
  1588.    case ir_triop_fma:   emit(nir_op_ffma, dest_size, srcs);  break;
  1589.    case ir_triop_lrp:
  1590.       instr = emit(nir_op_flrp, dest_size, srcs);
  1591.       if (ir->operands[0]->type->vector_elements != 1 &&
  1592.           ir->operands[2]->type->vector_elements == 1) {
  1593.          for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
  1594.               i++) {
  1595.             instr->src[2].swizzle[i] = 0;
  1596.          }
  1597.       }
  1598.       break;
  1599.    case ir_triop_csel:
  1600.       if (supports_ints)
  1601.          emit(nir_op_bcsel, dest_size, srcs);
  1602.       else
  1603.          emit(nir_op_fcsel, dest_size, srcs);
  1604.       break;
  1605.    case ir_triop_bfi:
  1606.       instr = emit(nir_op_bfi, dest_size, srcs);
  1607.       for (unsigned i = 0; i < ir->operands[1]->type->vector_elements; i++) {
  1608.          instr->src[0].swizzle[i] = 0;
  1609.       }
  1610.       break;
  1611.    case ir_triop_bitfield_extract:
  1612.       instr = emit(out_type == GLSL_TYPE_INT ? nir_op_ibitfield_extract :
  1613.                    nir_op_ubitfield_extract, dest_size, srcs);
  1614.       for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
  1615.          instr->src[1].swizzle[i] = 0;
  1616.          instr->src[2].swizzle[i] = 0;
  1617.       }
  1618.       break;
  1619.    case ir_quadop_bitfield_insert:
  1620.       instr = emit(nir_op_bitfield_insert, dest_size, srcs);
  1621.       for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
  1622.          instr->src[2].swizzle[i] = 0;
  1623.          instr->src[3].swizzle[i] = 0;
  1624.       }
  1625.       break;
  1626.    case ir_quadop_vector:
  1627.       switch (ir->type->vector_elements) {
  1628.          case 2: emit(nir_op_vec2, dest_size, srcs); break;
  1629.          case 3: emit(nir_op_vec3, dest_size, srcs); break;
  1630.          case 4: emit(nir_op_vec4, dest_size, srcs); break;
  1631.          default: unreachable("not reached");
  1632.       }
  1633.       break;
  1634.  
  1635.    default:
  1636.       unreachable("not reached");
  1637.    }
  1638. }
  1639.  
  1640. void
  1641. nir_visitor::visit(ir_swizzle *ir)
  1642. {
  1643.    nir_alu_instr *instr = emit(supports_ints ? nir_op_imov : nir_op_fmov,
  1644.                                ir->type->vector_elements,
  1645.                                evaluate_rvalue(ir->val));
  1646.  
  1647.    unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
  1648.    for (unsigned i = 0; i < ir->type->vector_elements; i++)
  1649.       instr->src[0].swizzle[i] = swizzle[i];
  1650. }
  1651.  
  1652. void
  1653. nir_visitor::visit(ir_texture *ir)
  1654. {
  1655.    unsigned num_srcs;
  1656.    nir_texop op;
  1657.    switch (ir->op) {
  1658.    case ir_tex:
  1659.       op = nir_texop_tex;
  1660.       num_srcs = 1; /* coordinate */
  1661.       break;
  1662.  
  1663.    case ir_txb:
  1664.    case ir_txl:
  1665.       op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
  1666.       num_srcs = 2; /* coordinate, bias/lod */
  1667.       break;
  1668.  
  1669.    case ir_txd:
  1670.       op = nir_texop_txd; /* coordinate, dPdx, dPdy */
  1671.       num_srcs = 3;
  1672.       break;
  1673.  
  1674.    case ir_txf:
  1675.       op = nir_texop_txf;
  1676.       if (ir->lod_info.lod != NULL)
  1677.          num_srcs = 2; /* coordinate, lod */
  1678.       else
  1679.          num_srcs = 1; /* coordinate */
  1680.       break;
  1681.  
  1682.    case ir_txf_ms:
  1683.       op = nir_texop_txf_ms;
  1684.       num_srcs = 2; /* coordinate, sample_index */
  1685.       break;
  1686.  
  1687.    case ir_txs:
  1688.       op = nir_texop_txs;
  1689.       if (ir->lod_info.lod != NULL)
  1690.          num_srcs = 1; /* lod */
  1691.       else
  1692.          num_srcs = 0;
  1693.       break;
  1694.  
  1695.    case ir_lod:
  1696.       op = nir_texop_lod;
  1697.       num_srcs = 1; /* coordinate */
  1698.       break;
  1699.  
  1700.    case ir_tg4:
  1701.       op = nir_texop_tg4;
  1702.       num_srcs = 1; /* coordinate */
  1703.       break;
  1704.  
  1705.    case ir_query_levels:
  1706.       op = nir_texop_query_levels;
  1707.       num_srcs = 0;
  1708.       break;
  1709.  
  1710.    default:
  1711.       unreachable("not reached");
  1712.    }
  1713.  
  1714.    if (ir->projector != NULL)
  1715.       num_srcs++;
  1716.    if (ir->shadow_comparitor != NULL)
  1717.       num_srcs++;
  1718.    if (ir->offset != NULL && ir->offset->as_constant() == NULL)
  1719.       num_srcs++;
  1720.  
  1721.    nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
  1722.  
  1723.    instr->op = op;
  1724.    instr->sampler_dim =
  1725.       (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
  1726.    instr->is_array = ir->sampler->type->sampler_array;
  1727.    instr->is_shadow = ir->sampler->type->sampler_shadow;
  1728.    if (instr->is_shadow)
  1729.       instr->is_new_style_shadow = (ir->type->vector_elements == 1);
  1730.    switch (ir->type->base_type) {
  1731.    case GLSL_TYPE_FLOAT:
  1732.       instr->dest_type = nir_type_float;
  1733.       break;
  1734.    case GLSL_TYPE_INT:
  1735.       instr->dest_type = nir_type_int;
  1736.       break;
  1737.    case GLSL_TYPE_UINT:
  1738.       instr->dest_type = nir_type_unsigned;
  1739.       break;
  1740.    default:
  1741.       unreachable("not reached");
  1742.    }
  1743.  
  1744.    instr->sampler = evaluate_deref(&instr->instr, ir->sampler);
  1745.  
  1746.    unsigned src_number = 0;
  1747.  
  1748.    if (ir->coordinate != NULL) {
  1749.       instr->coord_components = ir->coordinate->type->vector_elements;
  1750.       instr->src[src_number].src = evaluate_rvalue(ir->coordinate);
  1751.       instr->src[src_number].src_type = nir_tex_src_coord;
  1752.       src_number++;
  1753.    }
  1754.  
  1755.    if (ir->projector != NULL) {
  1756.       instr->src[src_number].src = evaluate_rvalue(ir->projector);
  1757.       instr->src[src_number].src_type = nir_tex_src_projector;
  1758.       src_number++;
  1759.    }
  1760.  
  1761.    if (ir->shadow_comparitor != NULL) {
  1762.       instr->src[src_number].src = evaluate_rvalue(ir->shadow_comparitor);
  1763.       instr->src[src_number].src_type = nir_tex_src_comparitor;
  1764.       src_number++;
  1765.    }
  1766.  
  1767.    if (ir->offset != NULL) {
  1768.       /* we don't support multiple offsets yet */
  1769.       assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
  1770.  
  1771.       ir_constant *const_offset = ir->offset->as_constant();
  1772.       if (const_offset != NULL) {
  1773.          for (unsigned i = 0; i < const_offset->type->vector_elements; i++)
  1774.             instr->const_offset[i] = const_offset->value.i[i];
  1775.       } else {
  1776.          instr->src[src_number].src = evaluate_rvalue(ir->offset);
  1777.          instr->src[src_number].src_type = nir_tex_src_offset;
  1778.          src_number++;
  1779.       }
  1780.    }
  1781.  
  1782.    switch (ir->op) {
  1783.    case ir_txb:
  1784.       instr->src[src_number].src = evaluate_rvalue(ir->lod_info.bias);
  1785.       instr->src[src_number].src_type = nir_tex_src_bias;
  1786.       src_number++;
  1787.       break;
  1788.  
  1789.    case ir_txl:
  1790.    case ir_txf:
  1791.    case ir_txs:
  1792.       if (ir->lod_info.lod != NULL) {
  1793.          instr->src[src_number].src = evaluate_rvalue(ir->lod_info.lod);
  1794.          instr->src[src_number].src_type = nir_tex_src_lod;
  1795.          src_number++;
  1796.       }
  1797.       break;
  1798.  
  1799.    case ir_txd:
  1800.       instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdx);
  1801.       instr->src[src_number].src_type = nir_tex_src_ddx;
  1802.       src_number++;
  1803.       instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdy);
  1804.       instr->src[src_number].src_type = nir_tex_src_ddy;
  1805.       src_number++;
  1806.       break;
  1807.  
  1808.    case ir_txf_ms:
  1809.       instr->src[src_number].src = evaluate_rvalue(ir->lod_info.sample_index);
  1810.       instr->src[src_number].src_type = nir_tex_src_ms_index;
  1811.       src_number++;
  1812.       break;
  1813.  
  1814.    case ir_tg4:
  1815.       instr->component = ir->lod_info.component->as_constant()->value.u[0];
  1816.       break;
  1817.  
  1818.    default:
  1819.       break;
  1820.    }
  1821.  
  1822.    assert(src_number == num_srcs);
  1823.  
  1824.    add_instr(&instr->instr, nir_tex_instr_dest_size(instr));
  1825. }
  1826.  
  1827. void
  1828. nir_visitor::visit(ir_constant *ir)
  1829. {
  1830.    /*
  1831.     * We don't know if this variable is an an array or struct that gets
  1832.     * dereferenced, so do the safe thing an make it a variable with a
  1833.     * constant initializer and return a dereference.
  1834.     */
  1835.  
  1836.    nir_variable *var = ralloc(this->shader, nir_variable);
  1837.    var->name = ralloc_strdup(var, "const_temp");
  1838.    var->type = ir->type;
  1839.    var->data.mode = nir_var_local;
  1840.    var->data.read_only = true;
  1841.    var->constant_initializer = constant_copy(ir, var);
  1842.    exec_list_push_tail(&this->impl->locals, &var->node);
  1843.  
  1844.    this->deref_head = nir_deref_var_create(this->shader, var);
  1845.    this->deref_tail = &this->deref_head->deref;
  1846. }
  1847.  
  1848. void
  1849. nir_visitor::visit(ir_dereference_variable *ir)
  1850. {
  1851.    struct hash_entry *entry =
  1852.       _mesa_hash_table_search(this->var_table, ir->var);
  1853.    assert(entry);
  1854.    nir_variable *var = (nir_variable *) entry->data;
  1855.  
  1856.    nir_deref_var *deref = nir_deref_var_create(this->shader, var);
  1857.    this->deref_head = deref;
  1858.    this->deref_tail = &deref->deref;
  1859. }
  1860.  
  1861. void
  1862. nir_visitor::visit(ir_dereference_record *ir)
  1863. {
  1864.    ir->record->accept(this);
  1865.  
  1866.    int field_index = this->deref_tail->type->field_index(ir->field);
  1867.    assert(field_index >= 0);
  1868.  
  1869.    nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
  1870.    deref->deref.type = ir->type;
  1871.    this->deref_tail->child = &deref->deref;
  1872.    this->deref_tail = &deref->deref;
  1873. }
  1874.  
  1875. void
  1876. nir_visitor::visit(ir_dereference_array *ir)
  1877. {
  1878.    nir_deref_array *deref = nir_deref_array_create(this->shader);
  1879.    deref->deref.type = ir->type;
  1880.  
  1881.    ir_constant *const_index = ir->array_index->as_constant();
  1882.    if (const_index != NULL) {
  1883.       deref->deref_array_type = nir_deref_array_type_direct;
  1884.       deref->base_offset = const_index->value.u[0];
  1885.    } else {
  1886.       deref->deref_array_type = nir_deref_array_type_indirect;
  1887.       deref->indirect = evaluate_rvalue(ir->array_index);
  1888.    }
  1889.  
  1890.    ir->array->accept(this);
  1891.  
  1892.    this->deref_tail->child = &deref->deref;
  1893.    ralloc_steal(this->deref_tail, deref);
  1894.    this->deref_tail = &deref->deref;
  1895. }
  1896.