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