• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "compiler/nir/nir_control_flow.h"
33 #include "compiler/nir/nir_builder.h"
34 #include "main/imports.h"
35 
36 /*
37  * pass to lower GLSL IR to NIR
38  *
39  * This will lower variable dereferences to loads/stores of corresponding
40  * variables in NIR - the variables will be converted to registers in a later
41  * pass.
42  */
43 
44 namespace {
45 
46 class nir_visitor : public ir_visitor
47 {
48 public:
49    nir_visitor(nir_shader *shader);
50    ~nir_visitor();
51 
52    virtual void visit(ir_variable *);
53    virtual void visit(ir_function *);
54    virtual void visit(ir_function_signature *);
55    virtual void visit(ir_loop *);
56    virtual void visit(ir_if *);
57    virtual void visit(ir_discard *);
58    virtual void visit(ir_loop_jump *);
59    virtual void visit(ir_return *);
60    virtual void visit(ir_call *);
61    virtual void visit(ir_assignment *);
62    virtual void visit(ir_emit_vertex *);
63    virtual void visit(ir_end_primitive *);
64    virtual void visit(ir_expression *);
65    virtual void visit(ir_swizzle *);
66    virtual void visit(ir_texture *);
67    virtual void visit(ir_constant *);
68    virtual void visit(ir_dereference_variable *);
69    virtual void visit(ir_dereference_record *);
70    virtual void visit(ir_dereference_array *);
71    virtual void visit(ir_barrier *);
72 
73    void create_function(ir_function_signature *ir);
74 
75 private:
76    void add_instr(nir_instr *instr, unsigned num_components, unsigned bit_size);
77    nir_ssa_def *evaluate_rvalue(ir_rvalue *ir);
78 
79    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def **srcs);
80    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1);
81    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
82                        nir_ssa_def *src2);
83    nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
84                        nir_ssa_def *src2, nir_ssa_def *src3);
85 
86    bool supports_ints;
87 
88    nir_shader *shader;
89    nir_function_impl *impl;
90    nir_builder b;
91    nir_ssa_def *result; /* result of the expression tree last visited */
92 
93    nir_deref_var *evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir);
94 
95    /* the head of the dereference chain we're creating */
96    nir_deref_var *deref_head;
97    /* the tail of the dereference chain we're creating */
98    nir_deref *deref_tail;
99 
100    nir_variable *var; /* variable created by ir_variable visitor */
101 
102    /* whether the IR we're operating on is per-function or global */
103    bool is_global;
104 
105    /* map of ir_variable -> nir_variable */
106    struct hash_table *var_table;
107 
108    /* map of ir_function_signature -> nir_function_overload */
109    struct hash_table *overload_table;
110 };
111 
112 /*
113  * This visitor runs before the main visitor, calling create_function() for
114  * each function so that the main visitor can resolve forward references in
115  * calls.
116  */
117 
118 class nir_function_visitor : public ir_hierarchical_visitor
119 {
120 public:
nir_function_visitor(nir_visitor * v)121    nir_function_visitor(nir_visitor *v) : visitor(v)
122    {
123    }
124    virtual ir_visitor_status visit_enter(ir_function *);
125 
126 private:
127    nir_visitor *visitor;
128 };
129 
130 } /* end of anonymous namespace */
131 
132 static void
nir_remap_attributes(nir_shader * shader)133 nir_remap_attributes(nir_shader *shader)
134 {
135    nir_foreach_variable(var, &shader->inputs) {
136       var->data.location += _mesa_bitcount_64(shader->info->double_inputs_read &
137                                               BITFIELD64_MASK(var->data.location));
138    }
139 
140    /* Once the remap is done, reset double_inputs_read, so later it will have
141     * which location/slots are doubles */
142    shader->info->double_inputs_read = 0;
143 }
144 
145 nir_shader *
glsl_to_nir(const struct gl_shader_program * shader_prog,gl_shader_stage stage,const nir_shader_compiler_options * options)146 glsl_to_nir(const struct gl_shader_program *shader_prog,
147             gl_shader_stage stage,
148             const nir_shader_compiler_options *options)
149 {
150    struct gl_linked_shader *sh = shader_prog->_LinkedShaders[stage];
151 
152    nir_shader *shader = nir_shader_create(NULL, stage, options,
153                                           &sh->Program->info);
154 
155    nir_visitor v1(shader);
156    nir_function_visitor v2(&v1);
157    v2.run(sh->ir);
158    visit_exec_list(sh->ir, &v1);
159 
160    nir_lower_constant_initializers(shader, (nir_variable_mode)~0);
161 
162    /* Remap the locations to slots so those requiring two slots will occupy
163     * two locations. For instance, if we have in the IR code a dvec3 attr0 in
164     * location 0 and vec4 attr1 in location 1, in NIR attr0 will use
165     * locations/slots 0 and 1, and attr1 will use location/slot 2 */
166    if (shader->stage == MESA_SHADER_VERTEX)
167       nir_remap_attributes(shader);
168 
169    shader->info->name = ralloc_asprintf(shader, "GLSL%d", shader_prog->Name);
170    if (shader_prog->Label)
171       shader->info->label = ralloc_strdup(shader, shader_prog->Label);
172    shader->info->clip_distance_array_size = sh->Program->ClipDistanceArraySize;
173    shader->info->cull_distance_array_size = sh->Program->CullDistanceArraySize;
174    shader->info->has_transform_feedback_varyings =
175       shader_prog->TransformFeedback.NumVarying > 0;
176 
177    return shader;
178 }
179 
nir_visitor(nir_shader * shader)180 nir_visitor::nir_visitor(nir_shader *shader)
181 {
182    this->supports_ints = shader->options->native_integers;
183    this->shader = shader;
184    this->is_global = true;
185    this->var_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
186                                              _mesa_key_pointer_equal);
187    this->overload_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
188                                                   _mesa_key_pointer_equal);
189    this->result = NULL;
190    this->impl = NULL;
191    this->var = NULL;
192    this->deref_head = NULL;
193    this->deref_tail = NULL;
194    memset(&this->b, 0, sizeof(this->b));
195 }
196 
~nir_visitor()197 nir_visitor::~nir_visitor()
198 {
199    _mesa_hash_table_destroy(this->var_table, NULL);
200    _mesa_hash_table_destroy(this->overload_table, NULL);
201 }
202 
203 nir_deref_var *
evaluate_deref(nir_instr * mem_ctx,ir_instruction * ir)204 nir_visitor::evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir)
205 {
206    ir->accept(this);
207    ralloc_steal(mem_ctx, this->deref_head);
208    return this->deref_head;
209 }
210 
211 static nir_constant *
constant_copy(ir_constant * ir,void * mem_ctx)212 constant_copy(ir_constant *ir, void *mem_ctx)
213 {
214    if (ir == NULL)
215       return NULL;
216 
217    nir_constant *ret = ralloc(mem_ctx, nir_constant);
218 
219    const unsigned rows = ir->type->vector_elements;
220    const unsigned cols = ir->type->matrix_columns;
221    unsigned i;
222 
223    ret->num_elements = 0;
224    switch (ir->type->base_type) {
225    case GLSL_TYPE_UINT:
226       /* Only float base types can be matrices. */
227       assert(cols == 1);
228 
229       for (unsigned r = 0; r < rows; r++)
230          ret->values[0].u32[r] = ir->value.u[r];
231 
232       break;
233 
234    case GLSL_TYPE_INT:
235       /* Only float base types can be matrices. */
236       assert(cols == 1);
237 
238       for (unsigned r = 0; r < rows; r++)
239          ret->values[0].i32[r] = ir->value.i[r];
240 
241       break;
242 
243    case GLSL_TYPE_FLOAT:
244       for (unsigned c = 0; c < cols; c++) {
245          for (unsigned r = 0; r < rows; r++)
246             ret->values[c].f32[r] = ir->value.f[c * rows + r];
247       }
248       break;
249 
250    case GLSL_TYPE_DOUBLE:
251       for (unsigned c = 0; c < cols; c++) {
252          for (unsigned r = 0; r < rows; r++)
253             ret->values[c].f64[r] = ir->value.d[c * rows + r];
254       }
255       break;
256 
257    case GLSL_TYPE_BOOL:
258       /* Only float base types can be matrices. */
259       assert(cols == 1);
260 
261       for (unsigned r = 0; r < rows; r++)
262          ret->values[0].u32[r] = ir->value.b[r] ? NIR_TRUE : NIR_FALSE;
263 
264       break;
265 
266    case GLSL_TYPE_STRUCT:
267       ret->elements = ralloc_array(mem_ctx, nir_constant *,
268                                    ir->type->length);
269       ret->num_elements = ir->type->length;
270 
271       i = 0;
272       foreach_in_list(ir_constant, field, &ir->components) {
273          ret->elements[i] = constant_copy(field, mem_ctx);
274          i++;
275       }
276       break;
277 
278    case GLSL_TYPE_ARRAY:
279       ret->elements = ralloc_array(mem_ctx, nir_constant *,
280                                    ir->type->length);
281       ret->num_elements = ir->type->length;
282 
283       for (i = 0; i < ir->type->length; i++)
284          ret->elements[i] = constant_copy(ir->array_elements[i], mem_ctx);
285       break;
286 
287    default:
288       unreachable("not reached");
289    }
290 
291    return ret;
292 }
293 
294 void
visit(ir_variable * ir)295 nir_visitor::visit(ir_variable *ir)
296 {
297    nir_variable *var = ralloc(shader, nir_variable);
298    var->type = ir->type;
299    var->name = ralloc_strdup(var, ir->name);
300 
301    var->data.read_only = ir->data.read_only;
302    var->data.centroid = ir->data.centroid;
303    var->data.sample = ir->data.sample;
304    var->data.patch = ir->data.patch;
305    var->data.invariant = ir->data.invariant;
306    var->data.location = ir->data.location;
307    var->data.compact = false;
308 
309    switch(ir->data.mode) {
310    case ir_var_auto:
311    case ir_var_temporary:
312       if (is_global)
313          var->data.mode = nir_var_global;
314       else
315          var->data.mode = nir_var_local;
316       break;
317 
318    case ir_var_function_in:
319    case ir_var_function_out:
320    case ir_var_function_inout:
321    case ir_var_const_in:
322       var->data.mode = nir_var_local;
323       break;
324 
325    case ir_var_shader_in:
326       if (shader->stage == MESA_SHADER_FRAGMENT &&
327           ir->data.location == VARYING_SLOT_FACE) {
328          /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
329          var->data.location = SYSTEM_VALUE_FRONT_FACE;
330          var->data.mode = nir_var_system_value;
331       } else if (shader->stage == MESA_SHADER_GEOMETRY &&
332                  ir->data.location == VARYING_SLOT_PRIMITIVE_ID) {
333          /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
334          var->data.location = SYSTEM_VALUE_PRIMITIVE_ID;
335          var->data.mode = nir_var_system_value;
336       } else {
337          var->data.mode = nir_var_shader_in;
338 
339          if (shader->stage == MESA_SHADER_TESS_EVAL &&
340              (ir->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
341               ir->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)) {
342             var->data.compact = ir->type->without_array()->is_scalar();
343          }
344       }
345 
346       /* Mark all the locations that require two slots */
347       if (glsl_type_is_dual_slot(glsl_without_array(var->type))) {
348          for (uint i = 0; i < glsl_count_attribute_slots(var->type, true); i++) {
349             uint64_t bitfield = BITFIELD64_BIT(var->data.location + i);
350             shader->info->double_inputs_read |= bitfield;
351          }
352       }
353       break;
354 
355    case ir_var_shader_out:
356       var->data.mode = nir_var_shader_out;
357       if (shader->stage == MESA_SHADER_TESS_CTRL &&
358           (ir->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
359            ir->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)) {
360          var->data.compact = ir->type->without_array()->is_scalar();
361       }
362       break;
363 
364    case ir_var_uniform:
365       var->data.mode = nir_var_uniform;
366       break;
367 
368    case ir_var_shader_storage:
369       var->data.mode = nir_var_shader_storage;
370       break;
371 
372    case ir_var_system_value:
373       var->data.mode = nir_var_system_value;
374       break;
375 
376    default:
377       unreachable("not reached");
378    }
379 
380    var->data.interpolation = ir->data.interpolation;
381    var->data.origin_upper_left = ir->data.origin_upper_left;
382    var->data.pixel_center_integer = ir->data.pixel_center_integer;
383    var->data.location_frac = ir->data.location_frac;
384 
385    switch (ir->data.depth_layout) {
386    case ir_depth_layout_none:
387       var->data.depth_layout = nir_depth_layout_none;
388       break;
389    case ir_depth_layout_any:
390       var->data.depth_layout = nir_depth_layout_any;
391       break;
392    case ir_depth_layout_greater:
393       var->data.depth_layout = nir_depth_layout_greater;
394       break;
395    case ir_depth_layout_less:
396       var->data.depth_layout = nir_depth_layout_less;
397       break;
398    case ir_depth_layout_unchanged:
399       var->data.depth_layout = nir_depth_layout_unchanged;
400       break;
401    default:
402       unreachable("not reached");
403    }
404 
405    var->data.index = ir->data.index;
406    var->data.binding = ir->data.binding;
407    var->data.offset = ir->data.offset;
408    var->data.image.read_only = ir->data.image_read_only;
409    var->data.image.write_only = ir->data.image_write_only;
410    var->data.image.coherent = ir->data.image_coherent;
411    var->data.image._volatile = ir->data.image_volatile;
412    var->data.image.restrict_flag = ir->data.image_restrict;
413    var->data.image.format = ir->data.image_format;
414    var->data.fb_fetch_output = ir->data.fb_fetch_output;
415 
416    var->num_state_slots = ir->get_num_state_slots();
417    if (var->num_state_slots > 0) {
418       var->state_slots = ralloc_array(var, nir_state_slot,
419                                       var->num_state_slots);
420 
421       ir_state_slot *state_slots = ir->get_state_slots();
422       for (unsigned i = 0; i < var->num_state_slots; i++) {
423          for (unsigned j = 0; j < 5; j++)
424             var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
425          var->state_slots[i].swizzle = state_slots[i].swizzle;
426       }
427    } else {
428       var->state_slots = NULL;
429    }
430 
431    var->constant_initializer = constant_copy(ir->constant_initializer, var);
432 
433    var->interface_type = ir->get_interface_type();
434 
435    if (var->data.mode == nir_var_local)
436       nir_function_impl_add_variable(impl, var);
437    else
438       nir_shader_add_variable(shader, var);
439 
440    _mesa_hash_table_insert(var_table, ir, var);
441    this->var = var;
442 }
443 
444 ir_visitor_status
visit_enter(ir_function * ir)445 nir_function_visitor::visit_enter(ir_function *ir)
446 {
447    foreach_in_list(ir_function_signature, sig, &ir->signatures) {
448       visitor->create_function(sig);
449    }
450    return visit_continue_with_parent;
451 }
452 
453 void
create_function(ir_function_signature * ir)454 nir_visitor::create_function(ir_function_signature *ir)
455 {
456    if (ir->is_intrinsic())
457       return;
458 
459    nir_function *func = nir_function_create(shader, ir->function_name());
460 
461    assert(ir->parameters.is_empty());
462    assert(ir->return_type == glsl_type::void_type);
463 
464    _mesa_hash_table_insert(this->overload_table, ir, func);
465 }
466 
467 void
visit(ir_function * ir)468 nir_visitor::visit(ir_function *ir)
469 {
470    foreach_in_list(ir_function_signature, sig, &ir->signatures)
471       sig->accept(this);
472 }
473 
474 void
visit(ir_function_signature * ir)475 nir_visitor::visit(ir_function_signature *ir)
476 {
477    if (ir->is_intrinsic())
478       return;
479 
480    struct hash_entry *entry =
481       _mesa_hash_table_search(this->overload_table, ir);
482 
483    assert(entry);
484    nir_function *func = (nir_function *) entry->data;
485 
486    if (ir->is_defined) {
487       nir_function_impl *impl = nir_function_impl_create(func);
488       this->impl = impl;
489 
490       assert(strcmp(func->name, "main") == 0);
491       assert(ir->parameters.is_empty());
492       assert(func->return_type == glsl_type::void_type);
493 
494       this->is_global = false;
495 
496       nir_builder_init(&b, impl);
497       b.cursor = nir_after_cf_list(&impl->body);
498       visit_exec_list(&ir->body, this);
499 
500       this->is_global = true;
501    } else {
502       func->impl = NULL;
503    }
504 }
505 
506 void
visit(ir_loop * ir)507 nir_visitor::visit(ir_loop *ir)
508 {
509    nir_loop *loop = nir_loop_create(this->shader);
510    nir_builder_cf_insert(&b, &loop->cf_node);
511 
512    b.cursor = nir_after_cf_list(&loop->body);
513    visit_exec_list(&ir->body_instructions, this);
514    b.cursor = nir_after_cf_node(&loop->cf_node);
515 }
516 
517 void
visit(ir_if * ir)518 nir_visitor::visit(ir_if *ir)
519 {
520    nir_src condition =
521       nir_src_for_ssa(evaluate_rvalue(ir->condition));
522 
523    nir_if *if_stmt = nir_if_create(this->shader);
524    if_stmt->condition = condition;
525    nir_builder_cf_insert(&b, &if_stmt->cf_node);
526 
527    b.cursor = nir_after_cf_list(&if_stmt->then_list);
528    visit_exec_list(&ir->then_instructions, this);
529 
530    b.cursor = nir_after_cf_list(&if_stmt->else_list);
531    visit_exec_list(&ir->else_instructions, this);
532 
533    b.cursor = nir_after_cf_node(&if_stmt->cf_node);
534 }
535 
536 void
visit(ir_discard * ir)537 nir_visitor::visit(ir_discard *ir)
538 {
539    /*
540     * discards aren't treated as control flow, because before we lower them
541     * they can appear anywhere in the shader and the stuff after them may still
542     * be executed (yay, crazy GLSL rules!). However, after lowering, all the
543     * discards will be immediately followed by a return.
544     */
545 
546    nir_intrinsic_instr *discard;
547    if (ir->condition) {
548       discard = nir_intrinsic_instr_create(this->shader,
549                                            nir_intrinsic_discard_if);
550       discard->src[0] =
551          nir_src_for_ssa(evaluate_rvalue(ir->condition));
552    } else {
553       discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
554    }
555 
556    nir_builder_instr_insert(&b, &discard->instr);
557 }
558 
559 void
visit(ir_emit_vertex * ir)560 nir_visitor::visit(ir_emit_vertex *ir)
561 {
562    nir_intrinsic_instr *instr =
563       nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
564    nir_intrinsic_set_stream_id(instr, ir->stream_id());
565    nir_builder_instr_insert(&b, &instr->instr);
566 }
567 
568 void
visit(ir_end_primitive * ir)569 nir_visitor::visit(ir_end_primitive *ir)
570 {
571    nir_intrinsic_instr *instr =
572       nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
573    nir_intrinsic_set_stream_id(instr, ir->stream_id());
574    nir_builder_instr_insert(&b, &instr->instr);
575 }
576 
577 void
visit(ir_loop_jump * ir)578 nir_visitor::visit(ir_loop_jump *ir)
579 {
580    nir_jump_type type;
581    switch (ir->mode) {
582    case ir_loop_jump::jump_break:
583       type = nir_jump_break;
584       break;
585    case ir_loop_jump::jump_continue:
586       type = nir_jump_continue;
587       break;
588    default:
589       unreachable("not reached");
590    }
591 
592    nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
593    nir_builder_instr_insert(&b, &instr->instr);
594 }
595 
596 void
visit(ir_return * ir)597 nir_visitor::visit(ir_return *ir)
598 {
599    if (ir->value != NULL) {
600       nir_intrinsic_instr *copy =
601          nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
602 
603       copy->variables[0] = nir_deref_var_create(copy, this->impl->return_var);
604       copy->variables[1] = evaluate_deref(&copy->instr, ir->value);
605    }
606 
607    nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
608    nir_builder_instr_insert(&b, &instr->instr);
609 }
610 
611 void
visit(ir_call * ir)612 nir_visitor::visit(ir_call *ir)
613 {
614    if (ir->callee->is_intrinsic()) {
615       nir_intrinsic_op op;
616 
617       switch (ir->callee->intrinsic_id) {
618       case ir_intrinsic_atomic_counter_read:
619          op = nir_intrinsic_atomic_counter_read_var;
620          break;
621       case ir_intrinsic_atomic_counter_increment:
622          op = nir_intrinsic_atomic_counter_inc_var;
623          break;
624       case ir_intrinsic_atomic_counter_predecrement:
625          op = nir_intrinsic_atomic_counter_dec_var;
626          break;
627       case ir_intrinsic_atomic_counter_add:
628          op = nir_intrinsic_atomic_counter_add_var;
629          break;
630       case ir_intrinsic_atomic_counter_and:
631          op = nir_intrinsic_atomic_counter_and_var;
632          break;
633       case ir_intrinsic_atomic_counter_or:
634          op = nir_intrinsic_atomic_counter_or_var;
635          break;
636       case ir_intrinsic_atomic_counter_xor:
637          op = nir_intrinsic_atomic_counter_xor_var;
638          break;
639       case ir_intrinsic_atomic_counter_min:
640          op = nir_intrinsic_atomic_counter_min_var;
641          break;
642       case ir_intrinsic_atomic_counter_max:
643          op = nir_intrinsic_atomic_counter_max_var;
644          break;
645       case ir_intrinsic_atomic_counter_exchange:
646          op = nir_intrinsic_atomic_counter_exchange_var;
647          break;
648       case ir_intrinsic_atomic_counter_comp_swap:
649          op = nir_intrinsic_atomic_counter_comp_swap_var;
650          break;
651       case ir_intrinsic_image_load:
652          op = nir_intrinsic_image_load;
653          break;
654       case ir_intrinsic_image_store:
655          op = nir_intrinsic_image_store;
656          break;
657       case ir_intrinsic_image_atomic_add:
658          op = nir_intrinsic_image_atomic_add;
659          break;
660       case ir_intrinsic_image_atomic_min:
661          op = nir_intrinsic_image_atomic_min;
662          break;
663       case ir_intrinsic_image_atomic_max:
664          op = nir_intrinsic_image_atomic_max;
665          break;
666       case ir_intrinsic_image_atomic_and:
667          op = nir_intrinsic_image_atomic_and;
668          break;
669       case ir_intrinsic_image_atomic_or:
670          op = nir_intrinsic_image_atomic_or;
671          break;
672       case ir_intrinsic_image_atomic_xor:
673          op = nir_intrinsic_image_atomic_xor;
674          break;
675       case ir_intrinsic_image_atomic_exchange:
676          op = nir_intrinsic_image_atomic_exchange;
677          break;
678       case ir_intrinsic_image_atomic_comp_swap:
679          op = nir_intrinsic_image_atomic_comp_swap;
680          break;
681       case ir_intrinsic_memory_barrier:
682          op = nir_intrinsic_memory_barrier;
683          break;
684       case ir_intrinsic_image_size:
685          op = nir_intrinsic_image_size;
686          break;
687       case ir_intrinsic_image_samples:
688          op = nir_intrinsic_image_samples;
689          break;
690       case ir_intrinsic_ssbo_store:
691          op = nir_intrinsic_store_ssbo;
692          break;
693       case ir_intrinsic_ssbo_load:
694          op = nir_intrinsic_load_ssbo;
695          break;
696       case ir_intrinsic_ssbo_atomic_add:
697          op = nir_intrinsic_ssbo_atomic_add;
698          break;
699       case ir_intrinsic_ssbo_atomic_and:
700          op = nir_intrinsic_ssbo_atomic_and;
701          break;
702       case ir_intrinsic_ssbo_atomic_or:
703          op = nir_intrinsic_ssbo_atomic_or;
704          break;
705       case ir_intrinsic_ssbo_atomic_xor:
706          op = nir_intrinsic_ssbo_atomic_xor;
707          break;
708       case ir_intrinsic_ssbo_atomic_min:
709          assert(ir->return_deref);
710          if (ir->return_deref->type == glsl_type::int_type)
711             op = nir_intrinsic_ssbo_atomic_imin;
712          else if (ir->return_deref->type == glsl_type::uint_type)
713             op = nir_intrinsic_ssbo_atomic_umin;
714          else
715             unreachable("Invalid type");
716          break;
717       case ir_intrinsic_ssbo_atomic_max:
718          assert(ir->return_deref);
719          if (ir->return_deref->type == glsl_type::int_type)
720             op = nir_intrinsic_ssbo_atomic_imax;
721          else if (ir->return_deref->type == glsl_type::uint_type)
722             op = nir_intrinsic_ssbo_atomic_umax;
723          else
724             unreachable("Invalid type");
725          break;
726       case ir_intrinsic_ssbo_atomic_exchange:
727          op = nir_intrinsic_ssbo_atomic_exchange;
728          break;
729       case ir_intrinsic_ssbo_atomic_comp_swap:
730          op = nir_intrinsic_ssbo_atomic_comp_swap;
731          break;
732       case ir_intrinsic_shader_clock:
733          op = nir_intrinsic_shader_clock;
734          break;
735       case ir_intrinsic_group_memory_barrier:
736          op = nir_intrinsic_group_memory_barrier;
737          break;
738       case ir_intrinsic_memory_barrier_atomic_counter:
739          op = nir_intrinsic_memory_barrier_atomic_counter;
740          break;
741       case ir_intrinsic_memory_barrier_buffer:
742          op = nir_intrinsic_memory_barrier_buffer;
743          break;
744       case ir_intrinsic_memory_barrier_image:
745          op = nir_intrinsic_memory_barrier_image;
746          break;
747       case ir_intrinsic_memory_barrier_shared:
748          op = nir_intrinsic_memory_barrier_shared;
749          break;
750       case ir_intrinsic_shared_load:
751          op = nir_intrinsic_load_shared;
752          break;
753       case ir_intrinsic_shared_store:
754          op = nir_intrinsic_store_shared;
755          break;
756       case ir_intrinsic_shared_atomic_add:
757          op = nir_intrinsic_shared_atomic_add;
758          break;
759       case ir_intrinsic_shared_atomic_and:
760          op = nir_intrinsic_shared_atomic_and;
761          break;
762       case ir_intrinsic_shared_atomic_or:
763          op = nir_intrinsic_shared_atomic_or;
764          break;
765       case ir_intrinsic_shared_atomic_xor:
766          op = nir_intrinsic_shared_atomic_xor;
767          break;
768       case ir_intrinsic_shared_atomic_min:
769          assert(ir->return_deref);
770          if (ir->return_deref->type == glsl_type::int_type)
771             op = nir_intrinsic_shared_atomic_imin;
772          else if (ir->return_deref->type == glsl_type::uint_type)
773             op = nir_intrinsic_shared_atomic_umin;
774          else
775             unreachable("Invalid type");
776          break;
777       case ir_intrinsic_shared_atomic_max:
778          assert(ir->return_deref);
779          if (ir->return_deref->type == glsl_type::int_type)
780             op = nir_intrinsic_shared_atomic_imax;
781          else if (ir->return_deref->type == glsl_type::uint_type)
782             op = nir_intrinsic_shared_atomic_umax;
783          else
784             unreachable("Invalid type");
785          break;
786       case ir_intrinsic_shared_atomic_exchange:
787          op = nir_intrinsic_shared_atomic_exchange;
788          break;
789       case ir_intrinsic_shared_atomic_comp_swap:
790          op = nir_intrinsic_shared_atomic_comp_swap;
791          break;
792       default:
793          unreachable("not reached");
794       }
795 
796       nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
797       nir_dest *dest = &instr->dest;
798 
799       switch (op) {
800       case nir_intrinsic_atomic_counter_read_var:
801       case nir_intrinsic_atomic_counter_inc_var:
802       case nir_intrinsic_atomic_counter_dec_var:
803       case nir_intrinsic_atomic_counter_add_var:
804       case nir_intrinsic_atomic_counter_min_var:
805       case nir_intrinsic_atomic_counter_max_var:
806       case nir_intrinsic_atomic_counter_and_var:
807       case nir_intrinsic_atomic_counter_or_var:
808       case nir_intrinsic_atomic_counter_xor_var:
809       case nir_intrinsic_atomic_counter_exchange_var:
810       case nir_intrinsic_atomic_counter_comp_swap_var: {
811          /* Set the counter variable dereference. */
812          exec_node *param = ir->actual_parameters.get_head();
813          ir_dereference *counter = (ir_dereference *)param;
814 
815          instr->variables[0] = evaluate_deref(&instr->instr, counter);
816          param = param->get_next();
817 
818          /* Set the intrinsic destination. */
819          if (ir->return_deref) {
820             nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
821          }
822 
823          /* Set the intrinsic parameters. */
824          if (!param->is_tail_sentinel()) {
825             instr->src[0] =
826                nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
827             param = param->get_next();
828          }
829 
830          if (!param->is_tail_sentinel()) {
831             instr->src[1] =
832                nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
833             param = param->get_next();
834          }
835 
836          nir_builder_instr_insert(&b, &instr->instr);
837          break;
838       }
839       case nir_intrinsic_image_load:
840       case nir_intrinsic_image_store:
841       case nir_intrinsic_image_atomic_add:
842       case nir_intrinsic_image_atomic_min:
843       case nir_intrinsic_image_atomic_max:
844       case nir_intrinsic_image_atomic_and:
845       case nir_intrinsic_image_atomic_or:
846       case nir_intrinsic_image_atomic_xor:
847       case nir_intrinsic_image_atomic_exchange:
848       case nir_intrinsic_image_atomic_comp_swap:
849       case nir_intrinsic_image_samples:
850       case nir_intrinsic_image_size: {
851          nir_ssa_undef_instr *instr_undef =
852             nir_ssa_undef_instr_create(shader, 1, 32);
853          nir_builder_instr_insert(&b, &instr_undef->instr);
854 
855          /* Set the image variable dereference. */
856          exec_node *param = ir->actual_parameters.get_head();
857          ir_dereference *image = (ir_dereference *)param;
858          const glsl_type *type =
859             image->variable_referenced()->type->without_array();
860 
861          instr->variables[0] = evaluate_deref(&instr->instr, image);
862          param = param->get_next();
863 
864          /* Set the intrinsic destination. */
865          if (ir->return_deref) {
866             const nir_intrinsic_info *info =
867                     &nir_intrinsic_infos[instr->intrinsic];
868             nir_ssa_dest_init(&instr->instr, &instr->dest,
869                               info->dest_components, 32, NULL);
870          }
871 
872          if (op == nir_intrinsic_image_size ||
873              op == nir_intrinsic_image_samples) {
874             nir_builder_instr_insert(&b, &instr->instr);
875             break;
876          }
877 
878          /* Set the address argument, extending the coordinate vector to four
879           * components.
880           */
881          nir_ssa_def *src_addr =
882             evaluate_rvalue((ir_dereference *)param);
883          nir_ssa_def *srcs[4];
884 
885          for (int i = 0; i < 4; i++) {
886             if (i < type->coordinate_components())
887                srcs[i] = nir_channel(&b, src_addr, i);
888             else
889                srcs[i] = &instr_undef->def;
890          }
891 
892          instr->src[0] = nir_src_for_ssa(nir_vec(&b, srcs, 4));
893          param = param->get_next();
894 
895          /* Set the sample argument, which is undefined for single-sample
896           * images.
897           */
898          if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
899             instr->src[1] =
900                nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
901             param = param->get_next();
902          } else {
903             instr->src[1] = nir_src_for_ssa(&instr_undef->def);
904          }
905 
906          /* Set the intrinsic parameters. */
907          if (!param->is_tail_sentinel()) {
908             instr->src[2] =
909                nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
910             param = param->get_next();
911          }
912 
913          if (!param->is_tail_sentinel()) {
914             instr->src[3] =
915                nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
916             param = param->get_next();
917          }
918          nir_builder_instr_insert(&b, &instr->instr);
919          break;
920       }
921       case nir_intrinsic_memory_barrier:
922       case nir_intrinsic_group_memory_barrier:
923       case nir_intrinsic_memory_barrier_atomic_counter:
924       case nir_intrinsic_memory_barrier_buffer:
925       case nir_intrinsic_memory_barrier_image:
926       case nir_intrinsic_memory_barrier_shared:
927          nir_builder_instr_insert(&b, &instr->instr);
928          break;
929       case nir_intrinsic_shader_clock:
930          nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
931          nir_builder_instr_insert(&b, &instr->instr);
932          break;
933       case nir_intrinsic_store_ssbo: {
934          exec_node *param = ir->actual_parameters.get_head();
935          ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
936 
937          param = param->get_next();
938          ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
939 
940          param = param->get_next();
941          ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
942 
943          param = param->get_next();
944          ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
945          assert(write_mask);
946 
947          instr->src[0] = nir_src_for_ssa(evaluate_rvalue(val));
948          instr->src[1] = nir_src_for_ssa(evaluate_rvalue(block));
949          instr->src[2] = nir_src_for_ssa(evaluate_rvalue(offset));
950          nir_intrinsic_set_write_mask(instr, write_mask->value.u[0]);
951          instr->num_components = val->type->vector_elements;
952 
953          nir_builder_instr_insert(&b, &instr->instr);
954          break;
955       }
956       case nir_intrinsic_load_ssbo: {
957          exec_node *param = ir->actual_parameters.get_head();
958          ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
959 
960          param = param->get_next();
961          ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
962 
963          instr->src[0] = nir_src_for_ssa(evaluate_rvalue(block));
964          instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
965 
966          const glsl_type *type = ir->return_deref->var->type;
967          instr->num_components = type->vector_elements;
968 
969          /* Setup destination register */
970          unsigned bit_size = glsl_get_bit_size(type);
971          nir_ssa_dest_init(&instr->instr, &instr->dest,
972                            type->vector_elements, bit_size, NULL);
973 
974          /* Insert the created nir instruction now since in the case of boolean
975           * result we will need to emit another instruction after it
976           */
977          nir_builder_instr_insert(&b, &instr->instr);
978 
979          /*
980           * In SSBO/UBO's, a true boolean value is any non-zero value, but we
981           * consider a true boolean to be ~0. Fix this up with a != 0
982           * comparison.
983           */
984          if (type->base_type == GLSL_TYPE_BOOL) {
985             nir_alu_instr *load_ssbo_compare =
986                nir_alu_instr_create(shader, nir_op_ine);
987             load_ssbo_compare->src[0].src.is_ssa = true;
988             load_ssbo_compare->src[0].src.ssa = &instr->dest.ssa;
989             load_ssbo_compare->src[1].src =
990                nir_src_for_ssa(nir_imm_int(&b, 0));
991             for (unsigned i = 0; i < type->vector_elements; i++)
992                load_ssbo_compare->src[1].swizzle[i] = 0;
993             nir_ssa_dest_init(&load_ssbo_compare->instr,
994                               &load_ssbo_compare->dest.dest,
995                               type->vector_elements, bit_size, NULL);
996             load_ssbo_compare->dest.write_mask = (1 << type->vector_elements) - 1;
997             nir_builder_instr_insert(&b, &load_ssbo_compare->instr);
998             dest = &load_ssbo_compare->dest.dest;
999          }
1000          break;
1001       }
1002       case nir_intrinsic_ssbo_atomic_add:
1003       case nir_intrinsic_ssbo_atomic_imin:
1004       case nir_intrinsic_ssbo_atomic_umin:
1005       case nir_intrinsic_ssbo_atomic_imax:
1006       case nir_intrinsic_ssbo_atomic_umax:
1007       case nir_intrinsic_ssbo_atomic_and:
1008       case nir_intrinsic_ssbo_atomic_or:
1009       case nir_intrinsic_ssbo_atomic_xor:
1010       case nir_intrinsic_ssbo_atomic_exchange:
1011       case nir_intrinsic_ssbo_atomic_comp_swap: {
1012          int param_count = ir->actual_parameters.length();
1013          assert(param_count == 3 || param_count == 4);
1014 
1015          /* Block index */
1016          exec_node *param = ir->actual_parameters.get_head();
1017          ir_instruction *inst = (ir_instruction *) param;
1018          instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1019 
1020          /* Offset */
1021          param = param->get_next();
1022          inst = (ir_instruction *) param;
1023          instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1024 
1025          /* data1 parameter (this is always present) */
1026          param = param->get_next();
1027          inst = (ir_instruction *) param;
1028          instr->src[2] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1029 
1030          /* data2 parameter (only with atomic_comp_swap) */
1031          if (param_count == 4) {
1032             assert(op == nir_intrinsic_ssbo_atomic_comp_swap);
1033             param = param->get_next();
1034             inst = (ir_instruction *) param;
1035             instr->src[3] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1036          }
1037 
1038          /* Atomic result */
1039          assert(ir->return_deref);
1040          nir_ssa_dest_init(&instr->instr, &instr->dest,
1041                            ir->return_deref->type->vector_elements, 32, NULL);
1042          nir_builder_instr_insert(&b, &instr->instr);
1043          break;
1044       }
1045       case nir_intrinsic_load_shared: {
1046          exec_node *param = ir->actual_parameters.get_head();
1047          ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1048 
1049          nir_intrinsic_set_base(instr, 0);
1050          instr->src[0] = nir_src_for_ssa(evaluate_rvalue(offset));
1051 
1052          const glsl_type *type = ir->return_deref->var->type;
1053          instr->num_components = type->vector_elements;
1054 
1055          /* Setup destination register */
1056          unsigned bit_size = glsl_get_bit_size(type);
1057          nir_ssa_dest_init(&instr->instr, &instr->dest,
1058                            type->vector_elements, bit_size, NULL);
1059 
1060          nir_builder_instr_insert(&b, &instr->instr);
1061          break;
1062       }
1063       case nir_intrinsic_store_shared: {
1064          exec_node *param = ir->actual_parameters.get_head();
1065          ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1066 
1067          param = param->get_next();
1068          ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
1069 
1070          param = param->get_next();
1071          ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
1072          assert(write_mask);
1073 
1074          nir_intrinsic_set_base(instr, 0);
1075          instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
1076 
1077          nir_intrinsic_set_write_mask(instr, write_mask->value.u[0]);
1078 
1079          instr->src[0] = nir_src_for_ssa(evaluate_rvalue(val));
1080          instr->num_components = val->type->vector_elements;
1081 
1082          nir_builder_instr_insert(&b, &instr->instr);
1083          break;
1084       }
1085       case nir_intrinsic_shared_atomic_add:
1086       case nir_intrinsic_shared_atomic_imin:
1087       case nir_intrinsic_shared_atomic_umin:
1088       case nir_intrinsic_shared_atomic_imax:
1089       case nir_intrinsic_shared_atomic_umax:
1090       case nir_intrinsic_shared_atomic_and:
1091       case nir_intrinsic_shared_atomic_or:
1092       case nir_intrinsic_shared_atomic_xor:
1093       case nir_intrinsic_shared_atomic_exchange:
1094       case nir_intrinsic_shared_atomic_comp_swap: {
1095          int param_count = ir->actual_parameters.length();
1096          assert(param_count == 2 || param_count == 3);
1097 
1098          /* Offset */
1099          exec_node *param = ir->actual_parameters.get_head();
1100          ir_instruction *inst = (ir_instruction *) param;
1101          instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1102 
1103          /* data1 parameter (this is always present) */
1104          param = param->get_next();
1105          inst = (ir_instruction *) param;
1106          instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1107 
1108          /* data2 parameter (only with atomic_comp_swap) */
1109          if (param_count == 3) {
1110             assert(op == nir_intrinsic_shared_atomic_comp_swap);
1111             param = param->get_next();
1112             inst = (ir_instruction *) param;
1113             instr->src[2] =
1114                nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1115          }
1116 
1117          /* Atomic result */
1118          assert(ir->return_deref);
1119          unsigned bit_size = glsl_get_bit_size(ir->return_deref->type);
1120          nir_ssa_dest_init(&instr->instr, &instr->dest,
1121                            ir->return_deref->type->vector_elements,
1122                            bit_size, NULL);
1123          nir_builder_instr_insert(&b, &instr->instr);
1124          break;
1125       }
1126       default:
1127          unreachable("not reached");
1128       }
1129 
1130       if (ir->return_deref) {
1131          nir_intrinsic_instr *store_instr =
1132             nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
1133          store_instr->num_components = ir->return_deref->type->vector_elements;
1134          nir_intrinsic_set_write_mask(store_instr,
1135                                       (1 << store_instr->num_components) - 1);
1136 
1137          store_instr->variables[0] =
1138             evaluate_deref(&store_instr->instr, ir->return_deref);
1139          store_instr->src[0] = nir_src_for_ssa(&dest->ssa);
1140 
1141          nir_builder_instr_insert(&b, &store_instr->instr);
1142       }
1143 
1144       return;
1145    }
1146 
1147    struct hash_entry *entry =
1148       _mesa_hash_table_search(this->overload_table, ir->callee);
1149    assert(entry);
1150    nir_function *callee = (nir_function *) entry->data;
1151 
1152    nir_call_instr *instr = nir_call_instr_create(this->shader, callee);
1153 
1154    unsigned i = 0;
1155    foreach_in_list(ir_dereference, param, &ir->actual_parameters) {
1156       instr->params[i] = evaluate_deref(&instr->instr, param);
1157       i++;
1158    }
1159 
1160    instr->return_deref = evaluate_deref(&instr->instr, ir->return_deref);
1161    nir_builder_instr_insert(&b, &instr->instr);
1162 }
1163 
1164 void
visit(ir_assignment * ir)1165 nir_visitor::visit(ir_assignment *ir)
1166 {
1167    unsigned num_components = ir->lhs->type->vector_elements;
1168 
1169    b.exact = ir->lhs->variable_referenced()->data.invariant ||
1170              ir->lhs->variable_referenced()->data.precise;
1171 
1172    if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
1173        (ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
1174       /* We're doing a plain-as-can-be copy, so emit a copy_var */
1175       nir_intrinsic_instr *copy =
1176          nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
1177 
1178       copy->variables[0] = evaluate_deref(&copy->instr, ir->lhs);
1179       copy->variables[1] = evaluate_deref(&copy->instr, ir->rhs);
1180 
1181       if (ir->condition) {
1182          nir_if *if_stmt = nir_if_create(this->shader);
1183          if_stmt->condition = nir_src_for_ssa(evaluate_rvalue(ir->condition));
1184          nir_builder_cf_insert(&b, &if_stmt->cf_node);
1185          nir_instr_insert_after_cf_list(&if_stmt->then_list, &copy->instr);
1186          b.cursor = nir_after_cf_node(&if_stmt->cf_node);
1187       } else {
1188          nir_builder_instr_insert(&b, &copy->instr);
1189       }
1190       return;
1191    }
1192 
1193    assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
1194 
1195    ir->lhs->accept(this);
1196    nir_deref_var *lhs_deref = this->deref_head;
1197    nir_ssa_def *src = evaluate_rvalue(ir->rhs);
1198 
1199    if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
1200       /* GLSL IR will give us the input to the write-masked assignment in a
1201        * single packed vector.  So, for example, if the writemask is xzw, then
1202        * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1203        * from the load.
1204        */
1205       unsigned swiz[4];
1206       unsigned component = 0;
1207       for (unsigned i = 0; i < 4; i++) {
1208          swiz[i] = ir->write_mask & (1 << i) ? component++ : 0;
1209       }
1210       src = nir_swizzle(&b, src, swiz, num_components, !supports_ints);
1211    }
1212 
1213    nir_intrinsic_instr *store =
1214       nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
1215    store->num_components = ir->lhs->type->vector_elements;
1216    nir_intrinsic_set_write_mask(store, ir->write_mask);
1217    store->variables[0] = nir_deref_var_clone(lhs_deref, store);
1218    store->src[0] = nir_src_for_ssa(src);
1219 
1220    if (ir->condition) {
1221       nir_if *if_stmt = nir_if_create(this->shader);
1222       if_stmt->condition = nir_src_for_ssa(evaluate_rvalue(ir->condition));
1223       nir_builder_cf_insert(&b, &if_stmt->cf_node);
1224       nir_instr_insert_after_cf_list(&if_stmt->then_list, &store->instr);
1225       b.cursor = nir_after_cf_node(&if_stmt->cf_node);
1226    } else {
1227       nir_builder_instr_insert(&b, &store->instr);
1228    }
1229 }
1230 
1231 /*
1232  * Given an instruction, returns a pointer to its destination or NULL if there
1233  * is no destination.
1234  *
1235  * Note that this only handles instructions we generate at this level.
1236  */
1237 static nir_dest *
get_instr_dest(nir_instr * instr)1238 get_instr_dest(nir_instr *instr)
1239 {
1240    nir_alu_instr *alu_instr;
1241    nir_intrinsic_instr *intrinsic_instr;
1242    nir_tex_instr *tex_instr;
1243 
1244    switch (instr->type) {
1245       case nir_instr_type_alu:
1246          alu_instr = nir_instr_as_alu(instr);
1247          return &alu_instr->dest.dest;
1248 
1249       case nir_instr_type_intrinsic:
1250          intrinsic_instr = nir_instr_as_intrinsic(instr);
1251          if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
1252             return &intrinsic_instr->dest;
1253          else
1254             return NULL;
1255 
1256       case nir_instr_type_tex:
1257          tex_instr = nir_instr_as_tex(instr);
1258          return &tex_instr->dest;
1259 
1260       default:
1261          unreachable("not reached");
1262    }
1263 
1264    return NULL;
1265 }
1266 
1267 void
add_instr(nir_instr * instr,unsigned num_components,unsigned bit_size)1268 nir_visitor::add_instr(nir_instr *instr, unsigned num_components,
1269                        unsigned bit_size)
1270 {
1271    nir_dest *dest = get_instr_dest(instr);
1272 
1273    if (dest)
1274       nir_ssa_dest_init(instr, dest, num_components, bit_size, NULL);
1275 
1276    nir_builder_instr_insert(&b, instr);
1277 
1278    if (dest) {
1279       assert(dest->is_ssa);
1280       this->result = &dest->ssa;
1281    }
1282 }
1283 
1284 nir_ssa_def *
evaluate_rvalue(ir_rvalue * ir)1285 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
1286 {
1287    ir->accept(this);
1288    if (ir->as_dereference() || ir->as_constant()) {
1289       /*
1290        * A dereference is being used on the right hand side, which means we
1291        * must emit a variable load.
1292        */
1293 
1294       nir_intrinsic_instr *load_instr =
1295          nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
1296       load_instr->num_components = ir->type->vector_elements;
1297       load_instr->variables[0] = this->deref_head;
1298       ralloc_steal(load_instr, load_instr->variables[0]);
1299       unsigned bit_size = glsl_get_bit_size(ir->type);
1300       add_instr(&load_instr->instr, ir->type->vector_elements, bit_size);
1301    }
1302 
1303    return this->result;
1304 }
1305 
1306 static bool
type_is_float(glsl_base_type type)1307 type_is_float(glsl_base_type type)
1308 {
1309    return type == GLSL_TYPE_FLOAT || type == GLSL_TYPE_DOUBLE;
1310 }
1311 
1312 void
visit(ir_expression * ir)1313 nir_visitor::visit(ir_expression *ir)
1314 {
1315    /* Some special cases */
1316    switch (ir->operation) {
1317    case ir_binop_ubo_load: {
1318       nir_intrinsic_instr *load =
1319          nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_ubo);
1320       unsigned bit_size = glsl_get_bit_size(ir->type);
1321       load->num_components = ir->type->vector_elements;
1322       load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
1323       load->src[1] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
1324       add_instr(&load->instr, ir->type->vector_elements, bit_size);
1325 
1326       /*
1327        * In UBO's, a true boolean value is any non-zero value, but we consider
1328        * a true boolean to be ~0. Fix this up with a != 0 comparison.
1329        */
1330 
1331       if (ir->type->base_type == GLSL_TYPE_BOOL)
1332          this->result = nir_ine(&b, &load->dest.ssa, nir_imm_int(&b, 0));
1333 
1334       return;
1335    }
1336 
1337    case ir_unop_interpolate_at_centroid:
1338    case ir_binop_interpolate_at_offset:
1339    case ir_binop_interpolate_at_sample: {
1340       ir_dereference *deref = ir->operands[0]->as_dereference();
1341       ir_swizzle *swizzle = NULL;
1342       if (!deref) {
1343          /* the api does not allow a swizzle here, but the varying packing code
1344           * may have pushed one into here.
1345           */
1346          swizzle = ir->operands[0]->as_swizzle();
1347          assert(swizzle);
1348          deref = swizzle->val->as_dereference();
1349          assert(deref);
1350       }
1351 
1352       deref->accept(this);
1353 
1354       nir_intrinsic_op op;
1355       if (this->deref_head->var->data.mode == nir_var_shader_in) {
1356          switch (ir->operation) {
1357          case ir_unop_interpolate_at_centroid:
1358             op = nir_intrinsic_interp_var_at_centroid;
1359             break;
1360          case ir_binop_interpolate_at_offset:
1361             op = nir_intrinsic_interp_var_at_offset;
1362             break;
1363          case ir_binop_interpolate_at_sample:
1364             op = nir_intrinsic_interp_var_at_sample;
1365             break;
1366          default:
1367             unreachable("Invalid interpolation intrinsic");
1368          }
1369       } else {
1370          /* This case can happen if the vertex shader does not write the
1371           * given varying.  In this case, the linker will lower it to a
1372           * global variable.  Since interpolating a variable makes no
1373           * sense, we'll just turn it into a load which will probably
1374           * eventually end up as an SSA definition.
1375           */
1376          assert(this->deref_head->var->data.mode == nir_var_global);
1377          op = nir_intrinsic_load_var;
1378       }
1379 
1380       nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
1381       intrin->num_components = deref->type->vector_elements;
1382       intrin->variables[0] = this->deref_head;
1383       ralloc_steal(intrin, intrin->variables[0]);
1384 
1385       if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
1386           intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
1387          intrin->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
1388 
1389       unsigned bit_size =  glsl_get_bit_size(deref->type);
1390       add_instr(&intrin->instr, deref->type->vector_elements, bit_size);
1391 
1392       if (swizzle) {
1393          unsigned swiz[4] = {
1394             swizzle->mask.x, swizzle->mask.y, swizzle->mask.z, swizzle->mask.w
1395          };
1396 
1397          result = nir_swizzle(&b, result, swiz,
1398                               swizzle->type->vector_elements, false);
1399       }
1400 
1401       return;
1402    }
1403 
1404    default:
1405       break;
1406    }
1407 
1408    nir_ssa_def *srcs[4];
1409    for (unsigned i = 0; i < ir->get_num_operands(); i++)
1410       srcs[i] = evaluate_rvalue(ir->operands[i]);
1411 
1412    glsl_base_type types[4];
1413    for (unsigned i = 0; i < ir->get_num_operands(); i++)
1414       if (supports_ints)
1415          types[i] = ir->operands[i]->type->base_type;
1416       else
1417          types[i] = GLSL_TYPE_FLOAT;
1418 
1419    glsl_base_type out_type;
1420    if (supports_ints)
1421       out_type = ir->type->base_type;
1422    else
1423       out_type = GLSL_TYPE_FLOAT;
1424 
1425    switch (ir->operation) {
1426    case ir_unop_bit_not: result = nir_inot(&b, srcs[0]); break;
1427    case ir_unop_logic_not:
1428       result = supports_ints ? nir_inot(&b, srcs[0]) : nir_fnot(&b, srcs[0]);
1429       break;
1430    case ir_unop_neg:
1431       result = type_is_float(types[0]) ? nir_fneg(&b, srcs[0])
1432                                        : nir_ineg(&b, srcs[0]);
1433       break;
1434    case ir_unop_abs:
1435       result = type_is_float(types[0]) ? nir_fabs(&b, srcs[0])
1436                                        : nir_iabs(&b, srcs[0]);
1437       break;
1438    case ir_unop_saturate:
1439       assert(type_is_float(types[0]));
1440       result = nir_fsat(&b, srcs[0]);
1441       break;
1442    case ir_unop_sign:
1443       result = type_is_float(types[0]) ? nir_fsign(&b, srcs[0])
1444                                        : nir_isign(&b, srcs[0]);
1445       break;
1446    case ir_unop_rcp:  result = nir_frcp(&b, srcs[0]);  break;
1447    case ir_unop_rsq:  result = nir_frsq(&b, srcs[0]);  break;
1448    case ir_unop_sqrt: result = nir_fsqrt(&b, srcs[0]); break;
1449    case ir_unop_exp:  unreachable("ir_unop_exp should have been lowered");
1450    case ir_unop_log:  unreachable("ir_unop_log should have been lowered");
1451    case ir_unop_exp2: result = nir_fexp2(&b, srcs[0]); break;
1452    case ir_unop_log2: result = nir_flog2(&b, srcs[0]); break;
1453    case ir_unop_i2f:
1454       result = supports_ints ? nir_i2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1455       break;
1456    case ir_unop_u2f:
1457       result = supports_ints ? nir_u2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1458       break;
1459    case ir_unop_b2f:
1460       result = supports_ints ? nir_b2f(&b, srcs[0]) : nir_fmov(&b, srcs[0]);
1461       break;
1462    case ir_unop_f2i:  result = nir_f2i(&b, srcs[0]);   break;
1463    case ir_unop_f2u:  result = nir_f2u(&b, srcs[0]);   break;
1464    case ir_unop_f2b:  result = nir_f2b(&b, srcs[0]);   break;
1465    case ir_unop_i2b:  result = nir_i2b(&b, srcs[0]);   break;
1466    case ir_unop_b2i:  result = nir_b2i(&b, srcs[0]);   break;
1467    case ir_unop_d2f:  result = nir_d2f(&b, srcs[0]);   break;
1468    case ir_unop_f2d:  result = nir_f2d(&b, srcs[0]);   break;
1469    case ir_unop_d2i:  result = nir_d2i(&b, srcs[0]);   break;
1470    case ir_unop_d2u:  result = nir_d2u(&b, srcs[0]);   break;
1471    case ir_unop_d2b:  result = nir_d2b(&b, srcs[0]);   break;
1472    case ir_unop_i2d:
1473       assert(supports_ints);
1474       result = nir_i2d(&b, srcs[0]);
1475       break;
1476    case ir_unop_u2d:
1477       assert(supports_ints);
1478       result = nir_u2d(&b, srcs[0]);
1479       break;
1480    case ir_unop_i2u:
1481    case ir_unop_u2i:
1482    case ir_unop_bitcast_i2f:
1483    case ir_unop_bitcast_f2i:
1484    case ir_unop_bitcast_u2f:
1485    case ir_unop_bitcast_f2u:
1486    case ir_unop_subroutine_to_int:
1487       /* no-op */
1488       result = nir_imov(&b, srcs[0]);
1489       break;
1490    case ir_unop_trunc: result = nir_ftrunc(&b, srcs[0]); break;
1491    case ir_unop_ceil:  result = nir_fceil(&b, srcs[0]); break;
1492    case ir_unop_floor: result = nir_ffloor(&b, srcs[0]); break;
1493    case ir_unop_fract: result = nir_ffract(&b, srcs[0]); break;
1494    case ir_unop_round_even: result = nir_fround_even(&b, srcs[0]); break;
1495    case ir_unop_sin:   result = nir_fsin(&b, srcs[0]); break;
1496    case ir_unop_cos:   result = nir_fcos(&b, srcs[0]); break;
1497    case ir_unop_dFdx:        result = nir_fddx(&b, srcs[0]); break;
1498    case ir_unop_dFdy:        result = nir_fddy(&b, srcs[0]); break;
1499    case ir_unop_dFdx_fine:   result = nir_fddx_fine(&b, srcs[0]); break;
1500    case ir_unop_dFdy_fine:   result = nir_fddy_fine(&b, srcs[0]); break;
1501    case ir_unop_dFdx_coarse: result = nir_fddx_coarse(&b, srcs[0]); break;
1502    case ir_unop_dFdy_coarse: result = nir_fddy_coarse(&b, srcs[0]); break;
1503    case ir_unop_pack_snorm_2x16:
1504       result = nir_pack_snorm_2x16(&b, srcs[0]);
1505       break;
1506    case ir_unop_pack_snorm_4x8:
1507       result = nir_pack_snorm_4x8(&b, srcs[0]);
1508       break;
1509    case ir_unop_pack_unorm_2x16:
1510       result = nir_pack_unorm_2x16(&b, srcs[0]);
1511       break;
1512    case ir_unop_pack_unorm_4x8:
1513       result = nir_pack_unorm_4x8(&b, srcs[0]);
1514       break;
1515    case ir_unop_pack_half_2x16:
1516       result = nir_pack_half_2x16(&b, srcs[0]);
1517       break;
1518    case ir_unop_unpack_snorm_2x16:
1519       result = nir_unpack_snorm_2x16(&b, srcs[0]);
1520       break;
1521    case ir_unop_unpack_snorm_4x8:
1522       result = nir_unpack_snorm_4x8(&b, srcs[0]);
1523       break;
1524    case ir_unop_unpack_unorm_2x16:
1525       result = nir_unpack_unorm_2x16(&b, srcs[0]);
1526       break;
1527    case ir_unop_unpack_unorm_4x8:
1528       result = nir_unpack_unorm_4x8(&b, srcs[0]);
1529       break;
1530    case ir_unop_unpack_half_2x16:
1531       result = nir_unpack_half_2x16(&b, srcs[0]);
1532       break;
1533    case ir_unop_pack_double_2x32:
1534       result = nir_pack_double_2x32(&b, srcs[0]);
1535       break;
1536    case ir_unop_unpack_double_2x32:
1537       result = nir_unpack_double_2x32(&b, srcs[0]);
1538       break;
1539    case ir_unop_bitfield_reverse:
1540       result = nir_bitfield_reverse(&b, srcs[0]);
1541       break;
1542    case ir_unop_bit_count:
1543       result = nir_bit_count(&b, srcs[0]);
1544       break;
1545    case ir_unop_find_msb:
1546       switch (types[0]) {
1547       case GLSL_TYPE_UINT:
1548          result = nir_ufind_msb(&b, srcs[0]);
1549          break;
1550       case GLSL_TYPE_INT:
1551          result = nir_ifind_msb(&b, srcs[0]);
1552          break;
1553       default:
1554          unreachable("Invalid type for findMSB()");
1555       }
1556       break;
1557    case ir_unop_find_lsb:
1558       result = nir_find_lsb(&b, srcs[0]);
1559       break;
1560 
1561    case ir_unop_noise:
1562       switch (ir->type->vector_elements) {
1563       case 1:
1564          switch (ir->operands[0]->type->vector_elements) {
1565             case 1: result = nir_fnoise1_1(&b, srcs[0]); break;
1566             case 2: result = nir_fnoise1_2(&b, srcs[0]); break;
1567             case 3: result = nir_fnoise1_3(&b, srcs[0]); break;
1568             case 4: result = nir_fnoise1_4(&b, srcs[0]); break;
1569             default: unreachable("not reached");
1570          }
1571          break;
1572       case 2:
1573          switch (ir->operands[0]->type->vector_elements) {
1574             case 1: result = nir_fnoise2_1(&b, srcs[0]); break;
1575             case 2: result = nir_fnoise2_2(&b, srcs[0]); break;
1576             case 3: result = nir_fnoise2_3(&b, srcs[0]); break;
1577             case 4: result = nir_fnoise2_4(&b, srcs[0]); break;
1578             default: unreachable("not reached");
1579          }
1580          break;
1581       case 3:
1582          switch (ir->operands[0]->type->vector_elements) {
1583             case 1: result = nir_fnoise3_1(&b, srcs[0]); break;
1584             case 2: result = nir_fnoise3_2(&b, srcs[0]); break;
1585             case 3: result = nir_fnoise3_3(&b, srcs[0]); break;
1586             case 4: result = nir_fnoise3_4(&b, srcs[0]); break;
1587             default: unreachable("not reached");
1588          }
1589          break;
1590       case 4:
1591          switch (ir->operands[0]->type->vector_elements) {
1592             case 1: result = nir_fnoise4_1(&b, srcs[0]); break;
1593             case 2: result = nir_fnoise4_2(&b, srcs[0]); break;
1594             case 3: result = nir_fnoise4_3(&b, srcs[0]); break;
1595             case 4: result = nir_fnoise4_4(&b, srcs[0]); break;
1596             default: unreachable("not reached");
1597          }
1598          break;
1599       default:
1600          unreachable("not reached");
1601       }
1602       break;
1603    case ir_unop_get_buffer_size: {
1604       nir_intrinsic_instr *load = nir_intrinsic_instr_create(
1605          this->shader,
1606          nir_intrinsic_get_buffer_size);
1607       load->num_components = ir->type->vector_elements;
1608       load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
1609       unsigned bit_size = glsl_get_bit_size(ir->type);
1610       add_instr(&load->instr, ir->type->vector_elements, bit_size);
1611       return;
1612    }
1613 
1614    case ir_binop_add:
1615       result = type_is_float(out_type) ? nir_fadd(&b, srcs[0], srcs[1])
1616                                        : nir_iadd(&b, srcs[0], srcs[1]);
1617       break;
1618    case ir_binop_sub:
1619       result = type_is_float(out_type) ? nir_fsub(&b, srcs[0], srcs[1])
1620                                        : nir_isub(&b, srcs[0], srcs[1]);
1621       break;
1622    case ir_binop_mul:
1623       result = type_is_float(out_type) ? nir_fmul(&b, srcs[0], srcs[1])
1624                                        : nir_imul(&b, srcs[0], srcs[1]);
1625       break;
1626    case ir_binop_div:
1627       if (type_is_float(out_type))
1628          result = nir_fdiv(&b, srcs[0], srcs[1]);
1629       else if (out_type == GLSL_TYPE_INT)
1630          result = nir_idiv(&b, srcs[0], srcs[1]);
1631       else
1632          result = nir_udiv(&b, srcs[0], srcs[1]);
1633       break;
1634    case ir_binop_mod:
1635       result = type_is_float(out_type) ? nir_fmod(&b, srcs[0], srcs[1])
1636                                        : nir_umod(&b, srcs[0], srcs[1]);
1637       break;
1638    case ir_binop_min:
1639       if (type_is_float(out_type))
1640          result = nir_fmin(&b, srcs[0], srcs[1]);
1641       else if (out_type == GLSL_TYPE_INT)
1642          result = nir_imin(&b, srcs[0], srcs[1]);
1643       else
1644          result = nir_umin(&b, srcs[0], srcs[1]);
1645       break;
1646    case ir_binop_max:
1647       if (type_is_float(out_type))
1648          result = nir_fmax(&b, srcs[0], srcs[1]);
1649       else if (out_type == GLSL_TYPE_INT)
1650          result = nir_imax(&b, srcs[0], srcs[1]);
1651       else
1652          result = nir_umax(&b, srcs[0], srcs[1]);
1653       break;
1654    case ir_binop_pow: result = nir_fpow(&b, srcs[0], srcs[1]); break;
1655    case ir_binop_bit_and: result = nir_iand(&b, srcs[0], srcs[1]); break;
1656    case ir_binop_bit_or: result = nir_ior(&b, srcs[0], srcs[1]); break;
1657    case ir_binop_bit_xor: result = nir_ixor(&b, srcs[0], srcs[1]); break;
1658    case ir_binop_logic_and:
1659       result = supports_ints ? nir_iand(&b, srcs[0], srcs[1])
1660                              : nir_fand(&b, srcs[0], srcs[1]);
1661       break;
1662    case ir_binop_logic_or:
1663       result = supports_ints ? nir_ior(&b, srcs[0], srcs[1])
1664                              : nir_for(&b, srcs[0], srcs[1]);
1665       break;
1666    case ir_binop_logic_xor:
1667       result = supports_ints ? nir_ixor(&b, srcs[0], srcs[1])
1668                              : nir_fxor(&b, srcs[0], srcs[1]);
1669       break;
1670    case ir_binop_lshift: result = nir_ishl(&b, srcs[0], srcs[1]); break;
1671    case ir_binop_rshift:
1672       result = (out_type == GLSL_TYPE_INT) ? nir_ishr(&b, srcs[0], srcs[1])
1673                                            : nir_ushr(&b, srcs[0], srcs[1]);
1674       break;
1675    case ir_binop_imul_high:
1676       result = (out_type == GLSL_TYPE_INT) ? nir_imul_high(&b, srcs[0], srcs[1])
1677                                            : nir_umul_high(&b, srcs[0], srcs[1]);
1678       break;
1679    case ir_binop_carry:  result = nir_uadd_carry(&b, srcs[0], srcs[1]);  break;
1680    case ir_binop_borrow: result = nir_usub_borrow(&b, srcs[0], srcs[1]); break;
1681    case ir_binop_less:
1682       if (supports_ints) {
1683          if (type_is_float(types[0]))
1684             result = nir_flt(&b, srcs[0], srcs[1]);
1685          else if (types[0] == GLSL_TYPE_INT)
1686             result = nir_ilt(&b, srcs[0], srcs[1]);
1687          else
1688             result = nir_ult(&b, srcs[0], srcs[1]);
1689       } else {
1690          result = nir_slt(&b, srcs[0], srcs[1]);
1691       }
1692       break;
1693    case ir_binop_greater:
1694       if (supports_ints) {
1695          if (type_is_float(types[0]))
1696             result = nir_flt(&b, srcs[1], srcs[0]);
1697          else if (types[0] == GLSL_TYPE_INT)
1698             result = nir_ilt(&b, srcs[1], srcs[0]);
1699          else
1700             result = nir_ult(&b, srcs[1], srcs[0]);
1701       } else {
1702          result = nir_slt(&b, srcs[1], srcs[0]);
1703       }
1704       break;
1705    case ir_binop_lequal:
1706       if (supports_ints) {
1707          if (type_is_float(types[0]))
1708             result = nir_fge(&b, srcs[1], srcs[0]);
1709          else if (types[0] == GLSL_TYPE_INT)
1710             result = nir_ige(&b, srcs[1], srcs[0]);
1711          else
1712             result = nir_uge(&b, srcs[1], srcs[0]);
1713       } else {
1714          result = nir_slt(&b, srcs[1], srcs[0]);
1715       }
1716       break;
1717    case ir_binop_gequal:
1718       if (supports_ints) {
1719          if (type_is_float(types[0]))
1720             result = nir_fge(&b, srcs[0], srcs[1]);
1721          else if (types[0] == GLSL_TYPE_INT)
1722             result = nir_ige(&b, srcs[0], srcs[1]);
1723          else
1724             result = nir_uge(&b, srcs[0], srcs[1]);
1725       } else {
1726          result = nir_slt(&b, srcs[0], srcs[1]);
1727       }
1728       break;
1729    case ir_binop_equal:
1730       if (supports_ints) {
1731          if (type_is_float(types[0]))
1732             result = nir_feq(&b, srcs[0], srcs[1]);
1733          else
1734             result = nir_ieq(&b, srcs[0], srcs[1]);
1735       } else {
1736          result = nir_seq(&b, srcs[0], srcs[1]);
1737       }
1738       break;
1739    case ir_binop_nequal:
1740       if (supports_ints) {
1741          if (type_is_float(types[0]))
1742             result = nir_fne(&b, srcs[0], srcs[1]);
1743          else
1744             result = nir_ine(&b, srcs[0], srcs[1]);
1745       } else {
1746          result = nir_sne(&b, srcs[0], srcs[1]);
1747       }
1748       break;
1749    case ir_binop_all_equal:
1750       if (supports_ints) {
1751          if (type_is_float(types[0])) {
1752             switch (ir->operands[0]->type->vector_elements) {
1753                case 1: result = nir_feq(&b, srcs[0], srcs[1]); break;
1754                case 2: result = nir_ball_fequal2(&b, srcs[0], srcs[1]); break;
1755                case 3: result = nir_ball_fequal3(&b, srcs[0], srcs[1]); break;
1756                case 4: result = nir_ball_fequal4(&b, srcs[0], srcs[1]); break;
1757                default:
1758                   unreachable("not reached");
1759             }
1760          } else {
1761             switch (ir->operands[0]->type->vector_elements) {
1762                case 1: result = nir_ieq(&b, srcs[0], srcs[1]); break;
1763                case 2: result = nir_ball_iequal2(&b, srcs[0], srcs[1]); break;
1764                case 3: result = nir_ball_iequal3(&b, srcs[0], srcs[1]); break;
1765                case 4: result = nir_ball_iequal4(&b, srcs[0], srcs[1]); break;
1766                default:
1767                   unreachable("not reached");
1768             }
1769          }
1770       } else {
1771          switch (ir->operands[0]->type->vector_elements) {
1772             case 1: result = nir_seq(&b, srcs[0], srcs[1]); break;
1773             case 2: result = nir_fall_equal2(&b, srcs[0], srcs[1]); break;
1774             case 3: result = nir_fall_equal3(&b, srcs[0], srcs[1]); break;
1775             case 4: result = nir_fall_equal4(&b, srcs[0], srcs[1]); break;
1776             default:
1777                unreachable("not reached");
1778          }
1779       }
1780       break;
1781    case ir_binop_any_nequal:
1782       if (supports_ints) {
1783          if (type_is_float(types[0])) {
1784             switch (ir->operands[0]->type->vector_elements) {
1785                case 1: result = nir_fne(&b, srcs[0], srcs[1]); break;
1786                case 2: result = nir_bany_fnequal2(&b, srcs[0], srcs[1]); break;
1787                case 3: result = nir_bany_fnequal3(&b, srcs[0], srcs[1]); break;
1788                case 4: result = nir_bany_fnequal4(&b, srcs[0], srcs[1]); break;
1789                default:
1790                   unreachable("not reached");
1791             }
1792          } else {
1793             switch (ir->operands[0]->type->vector_elements) {
1794                case 1: result = nir_ine(&b, srcs[0], srcs[1]); break;
1795                case 2: result = nir_bany_inequal2(&b, srcs[0], srcs[1]); break;
1796                case 3: result = nir_bany_inequal3(&b, srcs[0], srcs[1]); break;
1797                case 4: result = nir_bany_inequal4(&b, srcs[0], srcs[1]); break;
1798                default:
1799                   unreachable("not reached");
1800             }
1801          }
1802       } else {
1803          switch (ir->operands[0]->type->vector_elements) {
1804             case 1: result = nir_sne(&b, srcs[0], srcs[1]); break;
1805             case 2: result = nir_fany_nequal2(&b, srcs[0], srcs[1]); break;
1806             case 3: result = nir_fany_nequal3(&b, srcs[0], srcs[1]); break;
1807             case 4: result = nir_fany_nequal4(&b, srcs[0], srcs[1]); break;
1808             default:
1809                unreachable("not reached");
1810          }
1811       }
1812       break;
1813    case ir_binop_dot:
1814       switch (ir->operands[0]->type->vector_elements) {
1815          case 2: result = nir_fdot2(&b, srcs[0], srcs[1]); break;
1816          case 3: result = nir_fdot3(&b, srcs[0], srcs[1]); break;
1817          case 4: result = nir_fdot4(&b, srcs[0], srcs[1]); break;
1818          default:
1819             unreachable("not reached");
1820       }
1821       break;
1822 
1823    case ir_binop_ldexp: result = nir_ldexp(&b, srcs[0], srcs[1]); break;
1824    case ir_triop_fma:
1825       result = nir_ffma(&b, srcs[0], srcs[1], srcs[2]);
1826       break;
1827    case ir_triop_lrp:
1828       result = nir_flrp(&b, srcs[0], srcs[1], srcs[2]);
1829       break;
1830    case ir_triop_csel:
1831       if (supports_ints)
1832          result = nir_bcsel(&b, srcs[0], srcs[1], srcs[2]);
1833       else
1834          result = nir_fcsel(&b, srcs[0], srcs[1], srcs[2]);
1835       break;
1836    case ir_triop_bitfield_extract:
1837       result = (out_type == GLSL_TYPE_INT) ?
1838          nir_ibitfield_extract(&b, srcs[0], srcs[1], srcs[2]) :
1839          nir_ubitfield_extract(&b, srcs[0], srcs[1], srcs[2]);
1840       break;
1841    case ir_quadop_bitfield_insert:
1842       result = nir_bitfield_insert(&b, srcs[0], srcs[1], srcs[2], srcs[3]);
1843       break;
1844    case ir_quadop_vector:
1845       result = nir_vec(&b, srcs, ir->type->vector_elements);
1846       break;
1847 
1848    default:
1849       unreachable("not reached");
1850    }
1851 }
1852 
1853 void
visit(ir_swizzle * ir)1854 nir_visitor::visit(ir_swizzle *ir)
1855 {
1856    unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
1857    result = nir_swizzle(&b, evaluate_rvalue(ir->val), swizzle,
1858                         ir->type->vector_elements, !supports_ints);
1859 }
1860 
1861 void
visit(ir_texture * ir)1862 nir_visitor::visit(ir_texture *ir)
1863 {
1864    unsigned num_srcs;
1865    nir_texop op;
1866    switch (ir->op) {
1867    case ir_tex:
1868       op = nir_texop_tex;
1869       num_srcs = 1; /* coordinate */
1870       break;
1871 
1872    case ir_txb:
1873    case ir_txl:
1874       op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
1875       num_srcs = 2; /* coordinate, bias/lod */
1876       break;
1877 
1878    case ir_txd:
1879       op = nir_texop_txd; /* coordinate, dPdx, dPdy */
1880       num_srcs = 3;
1881       break;
1882 
1883    case ir_txf:
1884       op = nir_texop_txf;
1885       if (ir->lod_info.lod != NULL)
1886          num_srcs = 2; /* coordinate, lod */
1887       else
1888          num_srcs = 1; /* coordinate */
1889       break;
1890 
1891    case ir_txf_ms:
1892       op = nir_texop_txf_ms;
1893       num_srcs = 2; /* coordinate, sample_index */
1894       break;
1895 
1896    case ir_txs:
1897       op = nir_texop_txs;
1898       if (ir->lod_info.lod != NULL)
1899          num_srcs = 1; /* lod */
1900       else
1901          num_srcs = 0;
1902       break;
1903 
1904    case ir_lod:
1905       op = nir_texop_lod;
1906       num_srcs = 1; /* coordinate */
1907       break;
1908 
1909    case ir_tg4:
1910       op = nir_texop_tg4;
1911       num_srcs = 1; /* coordinate */
1912       break;
1913 
1914    case ir_query_levels:
1915       op = nir_texop_query_levels;
1916       num_srcs = 0;
1917       break;
1918 
1919    case ir_texture_samples:
1920       op = nir_texop_texture_samples;
1921       num_srcs = 0;
1922       break;
1923 
1924    case ir_samples_identical:
1925       op = nir_texop_samples_identical;
1926       num_srcs = 1; /* coordinate */
1927       break;
1928 
1929    default:
1930       unreachable("not reached");
1931    }
1932 
1933    if (ir->projector != NULL)
1934       num_srcs++;
1935    if (ir->shadow_comparator != NULL)
1936       num_srcs++;
1937    if (ir->offset != NULL)
1938       num_srcs++;
1939 
1940    nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
1941 
1942    instr->op = op;
1943    instr->sampler_dim =
1944       (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
1945    instr->is_array = ir->sampler->type->sampler_array;
1946    instr->is_shadow = ir->sampler->type->sampler_shadow;
1947    if (instr->is_shadow)
1948       instr->is_new_style_shadow = (ir->type->vector_elements == 1);
1949    switch (ir->type->base_type) {
1950    case GLSL_TYPE_FLOAT:
1951       instr->dest_type = nir_type_float;
1952       break;
1953    case GLSL_TYPE_INT:
1954       instr->dest_type = nir_type_int;
1955       break;
1956    case GLSL_TYPE_BOOL:
1957    case GLSL_TYPE_UINT:
1958       instr->dest_type = nir_type_uint;
1959       break;
1960    default:
1961       unreachable("not reached");
1962    }
1963 
1964    instr->texture = evaluate_deref(&instr->instr, ir->sampler);
1965 
1966    unsigned src_number = 0;
1967 
1968    if (ir->coordinate != NULL) {
1969       instr->coord_components = ir->coordinate->type->vector_elements;
1970       instr->src[src_number].src =
1971          nir_src_for_ssa(evaluate_rvalue(ir->coordinate));
1972       instr->src[src_number].src_type = nir_tex_src_coord;
1973       src_number++;
1974    }
1975 
1976    if (ir->projector != NULL) {
1977       instr->src[src_number].src =
1978          nir_src_for_ssa(evaluate_rvalue(ir->projector));
1979       instr->src[src_number].src_type = nir_tex_src_projector;
1980       src_number++;
1981    }
1982 
1983    if (ir->shadow_comparator != NULL) {
1984       instr->src[src_number].src =
1985          nir_src_for_ssa(evaluate_rvalue(ir->shadow_comparator));
1986       instr->src[src_number].src_type = nir_tex_src_comparator;
1987       src_number++;
1988    }
1989 
1990    if (ir->offset != NULL) {
1991       /* we don't support multiple offsets yet */
1992       assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
1993 
1994       instr->src[src_number].src =
1995          nir_src_for_ssa(evaluate_rvalue(ir->offset));
1996       instr->src[src_number].src_type = nir_tex_src_offset;
1997       src_number++;
1998    }
1999 
2000    switch (ir->op) {
2001    case ir_txb:
2002       instr->src[src_number].src =
2003          nir_src_for_ssa(evaluate_rvalue(ir->lod_info.bias));
2004       instr->src[src_number].src_type = nir_tex_src_bias;
2005       src_number++;
2006       break;
2007 
2008    case ir_txl:
2009    case ir_txf:
2010    case ir_txs:
2011       if (ir->lod_info.lod != NULL) {
2012          instr->src[src_number].src =
2013             nir_src_for_ssa(evaluate_rvalue(ir->lod_info.lod));
2014          instr->src[src_number].src_type = nir_tex_src_lod;
2015          src_number++;
2016       }
2017       break;
2018 
2019    case ir_txd:
2020       instr->src[src_number].src =
2021          nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdx));
2022       instr->src[src_number].src_type = nir_tex_src_ddx;
2023       src_number++;
2024       instr->src[src_number].src =
2025          nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdy));
2026       instr->src[src_number].src_type = nir_tex_src_ddy;
2027       src_number++;
2028       break;
2029 
2030    case ir_txf_ms:
2031       instr->src[src_number].src =
2032          nir_src_for_ssa(evaluate_rvalue(ir->lod_info.sample_index));
2033       instr->src[src_number].src_type = nir_tex_src_ms_index;
2034       src_number++;
2035       break;
2036 
2037    case ir_tg4:
2038       instr->component = ir->lod_info.component->as_constant()->value.u[0];
2039       break;
2040 
2041    default:
2042       break;
2043    }
2044 
2045    assert(src_number == num_srcs);
2046 
2047    unsigned bit_size = glsl_get_bit_size(ir->type);
2048    add_instr(&instr->instr, nir_tex_instr_dest_size(instr), bit_size);
2049 }
2050 
2051 void
visit(ir_constant * ir)2052 nir_visitor::visit(ir_constant *ir)
2053 {
2054    /*
2055     * We don't know if this variable is an array or struct that gets
2056     * dereferenced, so do the safe thing an make it a variable with a
2057     * constant initializer and return a dereference.
2058     */
2059 
2060    nir_variable *var =
2061       nir_local_variable_create(this->impl, ir->type, "const_temp");
2062    var->data.read_only = true;
2063    var->constant_initializer = constant_copy(ir, var);
2064 
2065    this->deref_head = nir_deref_var_create(this->shader, var);
2066    this->deref_tail = &this->deref_head->deref;
2067 }
2068 
2069 void
visit(ir_dereference_variable * ir)2070 nir_visitor::visit(ir_dereference_variable *ir)
2071 {
2072    struct hash_entry *entry =
2073       _mesa_hash_table_search(this->var_table, ir->var);
2074    assert(entry);
2075    nir_variable *var = (nir_variable *) entry->data;
2076 
2077    nir_deref_var *deref = nir_deref_var_create(this->shader, var);
2078    this->deref_head = deref;
2079    this->deref_tail = &deref->deref;
2080 }
2081 
2082 void
visit(ir_dereference_record * ir)2083 nir_visitor::visit(ir_dereference_record *ir)
2084 {
2085    ir->record->accept(this);
2086 
2087    int field_index = this->deref_tail->type->field_index(ir->field);
2088    assert(field_index >= 0);
2089 
2090    nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
2091    deref->deref.type = ir->type;
2092    this->deref_tail->child = &deref->deref;
2093    this->deref_tail = &deref->deref;
2094 }
2095 
2096 void
visit(ir_dereference_array * ir)2097 nir_visitor::visit(ir_dereference_array *ir)
2098 {
2099    nir_deref_array *deref = nir_deref_array_create(this->shader);
2100    deref->deref.type = ir->type;
2101 
2102    ir_constant *const_index = ir->array_index->as_constant();
2103    if (const_index != NULL) {
2104       deref->deref_array_type = nir_deref_array_type_direct;
2105       deref->base_offset = const_index->value.u[0];
2106    } else {
2107       deref->deref_array_type = nir_deref_array_type_indirect;
2108       deref->indirect =
2109          nir_src_for_ssa(evaluate_rvalue(ir->array_index));
2110    }
2111 
2112    ir->array->accept(this);
2113 
2114    this->deref_tail->child = &deref->deref;
2115    ralloc_steal(this->deref_tail, deref);
2116    this->deref_tail = &deref->deref;
2117 }
2118 
2119 void
visit(ir_barrier *)2120 nir_visitor::visit(ir_barrier *)
2121 {
2122    nir_intrinsic_instr *instr =
2123       nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
2124    nir_builder_instr_insert(&b, &instr->instr);
2125 }
2126