nir: Add floating point atomic add instrinsics
[mesa.git] / src / compiler / nir / nir_lower_vars_to_ssa.c
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 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_builder.h"
30 #include "nir_deref.h"
31 #include "nir_phi_builder.h"
32 #include "nir_vla.h"
33
34
35 struct deref_node {
36 struct deref_node *parent;
37 const struct glsl_type *type;
38
39 bool lower_to_ssa;
40
41 /* Only valid for things that end up in the direct list.
42 * Note that multiple nir_deref_instrs may correspond to this node, but
43 * they will all be equivalent, so any is as good as the other.
44 */
45 nir_deref_path path;
46 struct exec_node direct_derefs_link;
47
48 struct set *loads;
49 struct set *stores;
50 struct set *copies;
51
52 struct nir_phi_builder_value *pb_value;
53
54 /* True if this node is fully direct. If set, it must be in the children
55 * array of its parent.
56 */
57 bool is_direct;
58
59 struct deref_node *wildcard;
60 struct deref_node *indirect;
61 struct deref_node *children[0];
62 };
63
64 struct lower_variables_state {
65 nir_shader *shader;
66 void *dead_ctx;
67 nir_function_impl *impl;
68
69 /* A hash table mapping variables to deref_node data */
70 struct hash_table *deref_var_nodes;
71
72 /* A hash table mapping fully-qualified direct dereferences, i.e.
73 * dereferences with no indirect or wildcard array dereferences, to
74 * deref_node data.
75 *
76 * At the moment, we only lower loads, stores, and copies that can be
77 * trivially lowered to loads and stores, i.e. copies with no indirects
78 * and no wildcards. If a part of a variable that is being loaded from
79 * and/or stored into is also involved in a copy operation with
80 * wildcards, then we lower that copy operation to loads and stores, but
81 * otherwise we leave copies with wildcards alone. Since the only derefs
82 * used in these loads, stores, and trivial copies are ones with no
83 * wildcards and no indirects, these are precisely the derefs that we
84 * can actually consider lowering.
85 */
86 struct exec_list direct_deref_nodes;
87
88 /* Controls whether get_deref_node will add variables to the
89 * direct_deref_nodes table. This is turned on when we are initially
90 * scanning for load/store instructions. It is then turned off so we
91 * don't accidentally change the direct_deref_nodes table while we're
92 * iterating throug it.
93 */
94 bool add_to_direct_deref_nodes;
95
96 struct nir_phi_builder *phi_builder;
97 };
98
99 static struct deref_node *
100 deref_node_create(struct deref_node *parent,
101 const struct glsl_type *type,
102 bool is_direct, void *mem_ctx)
103 {
104 size_t size = sizeof(struct deref_node) +
105 glsl_get_length(type) * sizeof(struct deref_node *);
106
107 struct deref_node *node = rzalloc_size(mem_ctx, size);
108 node->type = type;
109 node->parent = parent;
110 exec_node_init(&node->direct_derefs_link);
111 node->is_direct = is_direct;
112
113 return node;
114 }
115
116 /* Returns the deref node associated with the given variable. This will be
117 * the root of the tree representing all of the derefs of the given variable.
118 */
119 static struct deref_node *
120 get_deref_node_for_var(nir_variable *var, struct lower_variables_state *state)
121 {
122 struct deref_node *node;
123
124 struct hash_entry *var_entry =
125 _mesa_hash_table_search(state->deref_var_nodes, var);
126
127 if (var_entry) {
128 return var_entry->data;
129 } else {
130 node = deref_node_create(NULL, var->type, true, state->dead_ctx);
131 _mesa_hash_table_insert(state->deref_var_nodes, var, node);
132 return node;
133 }
134 }
135
136 /* Gets the deref_node for the given deref chain and creates it if it
137 * doesn't yet exist. If the deref is fully-qualified and direct and
138 * state->add_to_direct_deref_nodes is true, it will be added to the hash
139 * table of of fully-qualified direct derefs.
140 */
141 static struct deref_node *
142 get_deref_node_recur(nir_deref_instr *deref,
143 struct lower_variables_state *state)
144 {
145 if (deref->deref_type == nir_deref_type_var)
146 return get_deref_node_for_var(deref->var, state);
147
148 struct deref_node *parent =
149 get_deref_node_recur(nir_deref_instr_parent(deref), state);
150
151 switch (deref->deref_type) {
152 case nir_deref_type_struct:
153 assert(glsl_type_is_struct(parent->type));
154 assert(deref->strct.index < glsl_get_length(parent->type));
155
156 if (parent->children[deref->strct.index] == NULL) {
157 parent->children[deref->strct.index] =
158 deref_node_create(parent, deref->type, parent->is_direct,
159 state->dead_ctx);
160 }
161
162 return parent->children[deref->strct.index];
163
164 case nir_deref_type_array: {
165 nir_const_value *const_index = nir_src_as_const_value(deref->arr.index);
166 if (const_index) {
167 uint32_t index = const_index->u32[0];
168 /* This is possible if a loop unrolls and generates an
169 * out-of-bounds offset. We need to handle this at least
170 * somewhat gracefully.
171 */
172 if (index >= glsl_get_length(parent->type))
173 return NULL;
174
175 if (parent->children[index] == NULL) {
176 parent->children[index] =
177 deref_node_create(parent, deref->type, parent->is_direct,
178 state->dead_ctx);
179 }
180
181 return parent->children[index];
182 } else {
183 if (parent->indirect == NULL) {
184 parent->indirect =
185 deref_node_create(parent, deref->type, false, state->dead_ctx);
186 }
187
188 return parent->indirect;
189 }
190 break;
191 }
192
193 case nir_deref_type_array_wildcard:
194 if (parent->wildcard == NULL) {
195 parent->wildcard =
196 deref_node_create(parent, deref->type, false, state->dead_ctx);
197 }
198
199 return parent->wildcard;
200
201 default:
202 unreachable("Invalid deref type");
203 }
204 }
205
206 static struct deref_node *
207 get_deref_node(nir_deref_instr *deref, struct lower_variables_state *state)
208 {
209 /* This pass only works on local variables. Just ignore any derefs with
210 * a non-local mode.
211 */
212 if (deref->mode != nir_var_local)
213 return NULL;
214
215 struct deref_node *node = get_deref_node_recur(deref, state);
216 if (!node)
217 return NULL;
218
219 /* Insert the node in the direct derefs list. We only do this if it's not
220 * already in the list and we only bother for deref nodes which are used
221 * directly in a load or store.
222 */
223 if (node->is_direct && state->add_to_direct_deref_nodes &&
224 node->direct_derefs_link.next == NULL) {
225 nir_deref_path_init(&node->path, deref, state->dead_ctx);
226 assert(deref->var != NULL);
227 exec_list_push_tail(&state->direct_deref_nodes,
228 &node->direct_derefs_link);
229 }
230
231 return node;
232 }
233
234 /* \sa foreach_deref_node_match */
235 static void
236 foreach_deref_node_worker(struct deref_node *node, nir_deref_instr **path,
237 void (* cb)(struct deref_node *node,
238 struct lower_variables_state *state),
239 struct lower_variables_state *state)
240 {
241 if (*path == NULL) {
242 cb(node, state);
243 return;
244 }
245
246 switch ((*path)->deref_type) {
247 case nir_deref_type_struct:
248 if (node->children[(*path)->strct.index]) {
249 foreach_deref_node_worker(node->children[(*path)->strct.index],
250 path + 1, cb, state);
251 }
252 return;
253
254 case nir_deref_type_array: {
255 nir_const_value *const_index = nir_src_as_const_value((*path)->arr.index);
256 assert(const_index);
257 uint32_t index = const_index->u32[0];
258
259 if (node->children[index]) {
260 foreach_deref_node_worker(node->children[index],
261 path + 1, cb, state);
262 }
263
264 if (node->wildcard) {
265 foreach_deref_node_worker(node->wildcard,
266 path + 1, cb, state);
267 }
268 return;
269 }
270
271 default:
272 unreachable("Unsupported deref type");
273 }
274 }
275
276 /* Walks over every "matching" deref_node and calls the callback. A node
277 * is considered to "match" if either refers to that deref or matches up t
278 * a wildcard. In other words, the following would match a[6].foo[3].bar:
279 *
280 * a[6].foo[3].bar
281 * a[*].foo[3].bar
282 * a[6].foo[*].bar
283 * a[*].foo[*].bar
284 *
285 * The given deref must be a full-length and fully qualified (no wildcards
286 * or indirects) deref chain.
287 */
288 static void
289 foreach_deref_node_match(nir_deref_path *path,
290 void (* cb)(struct deref_node *node,
291 struct lower_variables_state *state),
292 struct lower_variables_state *state)
293 {
294 assert(path->path[0]->deref_type == nir_deref_type_var);
295 struct deref_node *node = get_deref_node_for_var(path->path[0]->var, state);
296
297 if (node == NULL)
298 return;
299
300 foreach_deref_node_worker(node, &path->path[1], cb, state);
301 }
302
303 /* \sa deref_may_be_aliased */
304 static bool
305 path_may_be_aliased_node(struct deref_node *node, nir_deref_instr **path,
306 struct lower_variables_state *state)
307 {
308 if (*path == NULL)
309 return false;
310
311 switch ((*path)->deref_type) {
312 case nir_deref_type_struct:
313 if (node->children[(*path)->strct.index]) {
314 return path_may_be_aliased_node(node->children[(*path)->strct.index],
315 path + 1, state);
316 } else {
317 return false;
318 }
319
320 case nir_deref_type_array: {
321 nir_const_value *const_index = nir_src_as_const_value((*path)->arr.index);
322 if (!const_index)
323 return true;
324
325 uint32_t index = const_index->u32[0];
326
327 /* If there is an indirect at this level, we're aliased. */
328 if (node->indirect)
329 return true;
330
331 if (node->children[index] &&
332 path_may_be_aliased_node(node->children[index],
333 path + 1, state))
334 return true;
335
336 if (node->wildcard &&
337 path_may_be_aliased_node(node->wildcard, path + 1, state))
338 return true;
339
340 return false;
341 }
342
343 default:
344 unreachable("Unsupported deref type");
345 }
346 }
347
348 /* Returns true if there are no indirects that can ever touch this deref.
349 *
350 * For example, if the given deref is a[6].foo, then any uses of a[i].foo
351 * would cause this to return false, but a[i].bar would not affect it
352 * because it's a different structure member. A var_copy involving of
353 * a[*].bar also doesn't affect it because that can be lowered to entirely
354 * direct load/stores.
355 *
356 * We only support asking this question about fully-qualified derefs.
357 * Obviously, it's pointless to ask this about indirects, but we also
358 * rule-out wildcards. Handling Wildcard dereferences would involve
359 * checking each array index to make sure that there aren't any indirect
360 * references.
361 */
362 static bool
363 path_may_be_aliased(nir_deref_path *path,
364 struct lower_variables_state *state)
365 {
366 assert(path->path[0]->deref_type == nir_deref_type_var);
367 nir_variable *var = path->path[0]->var;
368
369 return path_may_be_aliased_node(get_deref_node_for_var(var, state),
370 &path->path[1], state);
371 }
372
373 static void
374 register_load_instr(nir_intrinsic_instr *load_instr,
375 struct lower_variables_state *state)
376 {
377 nir_deref_instr *deref = nir_src_as_deref(load_instr->src[0]);
378 struct deref_node *node = get_deref_node(deref, state);
379 if (node == NULL)
380 return;
381
382 if (node->loads == NULL)
383 node->loads = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer,
384 _mesa_key_pointer_equal);
385
386 _mesa_set_add(node->loads, load_instr);
387 }
388
389 static void
390 register_store_instr(nir_intrinsic_instr *store_instr,
391 struct lower_variables_state *state)
392 {
393 nir_deref_instr *deref = nir_src_as_deref(store_instr->src[0]);
394 struct deref_node *node = get_deref_node(deref, state);
395 if (node == NULL)
396 return;
397
398 if (node->stores == NULL)
399 node->stores = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer,
400 _mesa_key_pointer_equal);
401
402 _mesa_set_add(node->stores, store_instr);
403 }
404
405 static void
406 register_copy_instr(nir_intrinsic_instr *copy_instr,
407 struct lower_variables_state *state)
408 {
409 for (unsigned idx = 0; idx < 2; idx++) {
410 nir_deref_instr *deref = nir_src_as_deref(copy_instr->src[idx]);
411 struct deref_node *node = get_deref_node(deref, state);
412 if (node == NULL)
413 continue;
414
415 if (node->copies == NULL)
416 node->copies = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer,
417 _mesa_key_pointer_equal);
418
419 _mesa_set_add(node->copies, copy_instr);
420 }
421 }
422
423 static void
424 register_variable_uses(nir_function_impl *impl,
425 struct lower_variables_state *state)
426 {
427 nir_foreach_block(block, impl) {
428 nir_foreach_instr_safe(instr, block) {
429 if (instr->type != nir_instr_type_intrinsic)
430 continue;
431
432 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
433
434 switch (intrin->intrinsic) {
435 case nir_intrinsic_load_deref:
436 register_load_instr(intrin, state);
437 break;
438
439 case nir_intrinsic_store_deref:
440 register_store_instr(intrin, state);
441 break;
442
443 case nir_intrinsic_copy_deref:
444 register_copy_instr(intrin, state);
445 break;
446
447 default:
448 continue;
449 }
450 }
451 }
452 }
453
454 /* Walks over all of the copy instructions to or from the given deref_node
455 * and lowers them to load/store intrinsics.
456 */
457 static void
458 lower_copies_to_load_store(struct deref_node *node,
459 struct lower_variables_state *state)
460 {
461 if (!node->copies)
462 return;
463
464 nir_builder b;
465 nir_builder_init(&b, state->impl);
466
467 struct set_entry *copy_entry;
468 set_foreach(node->copies, copy_entry) {
469 nir_intrinsic_instr *copy = (void *)copy_entry->key;
470
471 nir_lower_deref_copy_instr(&b, copy);
472
473 for (unsigned i = 0; i < 2; ++i) {
474 nir_deref_instr *arg_deref = nir_src_as_deref(copy->src[i]);
475 struct deref_node *arg_node = get_deref_node(arg_deref, state);
476
477 /* Only bother removing copy entries for other nodes */
478 if (arg_node == NULL || arg_node == node)
479 continue;
480
481 struct set_entry *arg_entry = _mesa_set_search(arg_node->copies, copy);
482 assert(arg_entry);
483 _mesa_set_remove(arg_node->copies, arg_entry);
484 }
485
486 nir_instr_remove(&copy->instr);
487 }
488
489 node->copies = NULL;
490 }
491
492 /* Performs variable renaming
493 *
494 * This algorithm is very similar to the one outlined in "Efficiently
495 * Computing Static Single Assignment Form and the Control Dependence
496 * Graph" by Cytron et al. The primary difference is that we only put one
497 * SSA def on the stack per block.
498 */
499 static bool
500 rename_variables(struct lower_variables_state *state)
501 {
502 nir_builder b;
503 nir_builder_init(&b, state->impl);
504
505 nir_foreach_block(block, state->impl) {
506 nir_foreach_instr_safe(instr, block) {
507 if (instr->type != nir_instr_type_intrinsic)
508 continue;
509
510 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
511
512 switch (intrin->intrinsic) {
513 case nir_intrinsic_load_deref: {
514 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
515 if (deref->mode != nir_var_local)
516 continue;
517
518 struct deref_node *node = get_deref_node(deref, state);
519 if (node == NULL) {
520 /* If we hit this path then we are referencing an invalid
521 * value. Most likely, we unrolled something and are
522 * reading past the end of some array. In any case, this
523 * should result in an undefined value.
524 */
525 nir_ssa_undef_instr *undef =
526 nir_ssa_undef_instr_create(state->shader,
527 intrin->num_components,
528 intrin->dest.ssa.bit_size);
529
530 nir_instr_insert_before(&intrin->instr, &undef->instr);
531 nir_instr_remove(&intrin->instr);
532
533 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
534 nir_src_for_ssa(&undef->def));
535 continue;
536 }
537
538 if (!node->lower_to_ssa)
539 continue;
540
541 nir_alu_instr *mov = nir_alu_instr_create(state->shader,
542 nir_op_imov);
543 mov->src[0].src = nir_src_for_ssa(
544 nir_phi_builder_value_get_block_def(node->pb_value, block));
545 for (unsigned i = intrin->num_components; i < 4; i++)
546 mov->src[0].swizzle[i] = 0;
547
548 assert(intrin->dest.is_ssa);
549
550 mov->dest.write_mask = (1 << intrin->num_components) - 1;
551 nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
552 intrin->num_components,
553 intrin->dest.ssa.bit_size, NULL);
554
555 nir_instr_insert_before(&intrin->instr, &mov->instr);
556 nir_instr_remove(&intrin->instr);
557
558 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
559 nir_src_for_ssa(&mov->dest.dest.ssa));
560 break;
561 }
562
563 case nir_intrinsic_store_deref: {
564 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
565 if (deref->mode != nir_var_local)
566 continue;
567
568 struct deref_node *node = get_deref_node(deref, state);
569
570 assert(intrin->src[1].is_ssa);
571 nir_ssa_def *value = intrin->src[1].ssa;
572
573 if (node == NULL) {
574 /* Probably an out-of-bounds array store. That should be a
575 * no-op. */
576 nir_instr_remove(&intrin->instr);
577 continue;
578 }
579
580 if (!node->lower_to_ssa)
581 continue;
582
583 assert(intrin->num_components ==
584 glsl_get_vector_elements(node->type));
585
586 nir_ssa_def *new_def;
587 b.cursor = nir_before_instr(&intrin->instr);
588
589 unsigned wrmask = nir_intrinsic_write_mask(intrin);
590 if (wrmask == (1 << intrin->num_components) - 1) {
591 /* Whole variable store - just copy the source. Note that
592 * intrin->num_components and value->num_components
593 * may differ.
594 */
595 unsigned swiz[4];
596 for (unsigned i = 0; i < 4; i++)
597 swiz[i] = i < intrin->num_components ? i : 0;
598
599 new_def = nir_swizzle(&b, value, swiz,
600 intrin->num_components, false);
601 } else {
602 nir_ssa_def *old_def =
603 nir_phi_builder_value_get_block_def(node->pb_value, block);
604 /* For writemasked store_var intrinsics, we combine the newly
605 * written values with the existing contents of unwritten
606 * channels, creating a new SSA value for the whole vector.
607 */
608 nir_ssa_def *srcs[4];
609 for (unsigned i = 0; i < intrin->num_components; i++) {
610 if (wrmask & (1 << i)) {
611 srcs[i] = nir_channel(&b, value, i);
612 } else {
613 srcs[i] = nir_channel(&b, old_def, i);
614 }
615 }
616 new_def = nir_vec(&b, srcs, intrin->num_components);
617 }
618
619 assert(new_def->num_components == intrin->num_components);
620
621 nir_phi_builder_value_set_block_def(node->pb_value, block, new_def);
622 nir_instr_remove(&intrin->instr);
623 break;
624 }
625
626 default:
627 break;
628 }
629 }
630 }
631
632 return true;
633 }
634
635 /** Implements a pass to lower variable uses to SSA values
636 *
637 * This path walks the list of instructions and tries to lower as many
638 * local variable load/store operations to SSA defs and uses as it can.
639 * The process involves four passes:
640 *
641 * 1) Iterate over all of the instructions and mark where each local
642 * variable deref is used in a load, store, or copy. While we're at
643 * it, we keep track of all of the fully-qualified (no wildcards) and
644 * fully-direct references we see and store them in the
645 * direct_deref_nodes hash table.
646 *
647 * 2) Walk over the list of fully-qualified direct derefs generated in
648 * the previous pass. For each deref, we determine if it can ever be
649 * aliased, i.e. if there is an indirect reference anywhere that may
650 * refer to it. If it cannot be aliased, we mark it for lowering to an
651 * SSA value. At this point, we lower any var_copy instructions that
652 * use the given deref to load/store operations.
653 *
654 * 3) Walk over the list of derefs we plan to lower to SSA values and
655 * insert phi nodes as needed.
656 *
657 * 4) Perform "variable renaming" by replacing the load/store instructions
658 * with SSA definitions and SSA uses.
659 */
660 static bool
661 nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
662 {
663 struct lower_variables_state state;
664
665 state.shader = impl->function->shader;
666 state.dead_ctx = ralloc_context(state.shader);
667 state.impl = impl;
668
669 state.deref_var_nodes = _mesa_hash_table_create(state.dead_ctx,
670 _mesa_hash_pointer,
671 _mesa_key_pointer_equal);
672 exec_list_make_empty(&state.direct_deref_nodes);
673
674 /* Build the initial deref structures and direct_deref_nodes table */
675 state.add_to_direct_deref_nodes = true;
676
677 register_variable_uses(impl, &state);
678
679 bool progress = false;
680
681 nir_metadata_require(impl, nir_metadata_block_index);
682
683 /* We're about to iterate through direct_deref_nodes. Don't modify it. */
684 state.add_to_direct_deref_nodes = false;
685
686 foreach_list_typed_safe(struct deref_node, node, direct_derefs_link,
687 &state.direct_deref_nodes) {
688 nir_deref_path *path = &node->path;
689
690 assert(path->path[0]->deref_type == nir_deref_type_var);
691 nir_variable *var = path->path[0]->var;
692
693 /* We don't build deref nodes for non-local variables */
694 assert(var->data.mode == nir_var_local);
695
696 if (path_may_be_aliased(path, &state)) {
697 exec_node_remove(&node->direct_derefs_link);
698 continue;
699 }
700
701 node->lower_to_ssa = true;
702 progress = true;
703
704 foreach_deref_node_match(path, lower_copies_to_load_store, &state);
705 }
706
707 if (!progress)
708 return false;
709
710 nir_metadata_require(impl, nir_metadata_dominance);
711
712 /* We may have lowered some copy instructions to load/store
713 * instructions. The uses from the copy instructions hav already been
714 * removed but we need to rescan to ensure that the uses from the newly
715 * added load/store instructions are registered. We need this
716 * information for phi node insertion below.
717 */
718 register_variable_uses(impl, &state);
719
720 state.phi_builder = nir_phi_builder_create(state.impl);
721
722 NIR_VLA(BITSET_WORD, store_blocks, BITSET_WORDS(state.impl->num_blocks));
723 foreach_list_typed(struct deref_node, node, direct_derefs_link,
724 &state.direct_deref_nodes) {
725 if (!node->lower_to_ssa)
726 continue;
727
728 memset(store_blocks, 0,
729 BITSET_WORDS(state.impl->num_blocks) * sizeof(*store_blocks));
730
731 assert(node->path.path[0]->var->constant_initializer == NULL);
732
733 if (node->stores) {
734 struct set_entry *store_entry;
735 set_foreach(node->stores, store_entry) {
736 nir_intrinsic_instr *store =
737 (nir_intrinsic_instr *)store_entry->key;
738 BITSET_SET(store_blocks, store->instr.block->index);
739 }
740 }
741
742 node->pb_value =
743 nir_phi_builder_add_value(state.phi_builder,
744 glsl_get_vector_elements(node->type),
745 glsl_get_bit_size(node->type),
746 store_blocks);
747 }
748
749 rename_variables(&state);
750
751 nir_phi_builder_finish(state.phi_builder);
752
753 nir_metadata_preserve(impl, nir_metadata_block_index |
754 nir_metadata_dominance);
755
756 ralloc_free(state.dead_ctx);
757
758 return progress;
759 }
760
761 bool
762 nir_lower_vars_to_ssa(nir_shader *shader)
763 {
764 bool progress = false;
765
766 nir_foreach_function(function, shader) {
767 if (function->impl)
768 progress |= nir_lower_vars_to_ssa_impl(function->impl);
769 }
770
771 return progress;
772 }