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