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