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