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