-/** Pushes an SSA def onto the def stack for the given node
- *
- * Each node is potentially associated with a stack of SSA definitions.
- * This stack is used for determining what SSA definition reaches a given
- * point in the program for variable renaming. The stack is always kept in
- * dominance-order with at most one SSA def per block. If the SSA
- * definition on the top of the stack is in the same block as the one being
- * pushed, the top element is replaced.
- */
-static void
-def_stack_push(struct deref_node *node, nir_ssa_def *def,
- struct lower_variables_state *state)
-{
- if (node->def_stack == NULL) {
- node->def_stack = ralloc_array(state->dead_ctx, nir_ssa_def *,
- state->impl->num_blocks);
- node->def_stack_tail = node->def_stack - 1;
- }
-
- if (node->def_stack_tail >= node->def_stack) {
- nir_ssa_def *top_def = *node->def_stack_tail;
-
- if (def->parent_instr->block == top_def->parent_instr->block) {
- /* They're in the same block, just replace the top */
- *node->def_stack_tail = def;
- return;
- }
- }
-
- *(++node->def_stack_tail) = def;
-}
-
-/* Pop the top of the def stack if it's in the given block */
-static void
-def_stack_pop_if_in_block(struct deref_node *node, nir_block *block)
-{
- /* If we're popping, then we have presumably pushed at some time in the
- * past so this should exist.
- */
- assert(node->def_stack != NULL);
-
- /* The stack is already empty. Do nothing. */
- if (node->def_stack_tail < node->def_stack)
- return;
-
- nir_ssa_def *def = *node->def_stack_tail;
- if (def->parent_instr->block == block)
- node->def_stack_tail--;
-}
-
-/** Retrieves the SSA definition on the top of the stack for the given
- * node, if one exists. If the stack is empty, then we return the constant
- * initializer (if it exists) or an SSA undef.
- */
-static nir_ssa_def *
-get_ssa_def_for_block(struct deref_node *node, nir_block *block,
- struct lower_variables_state *state)
-{
- /* If we have something on the stack, go ahead and return it. We're
- * assuming that the top of the stack dominates the given block.
- */
- if (node->def_stack && node->def_stack_tail >= node->def_stack)
- return *node->def_stack_tail;
-
- /* If we got here then we don't have a definition that dominates the
- * given block. This means that we need to add an undef and use that.
- */
- nir_ssa_undef_instr *undef =
- nir_ssa_undef_instr_create(state->shader,
- glsl_get_vector_elements(node->type));
- nir_instr_insert_before_cf_list(&state->impl->body, &undef->instr);
- def_stack_push(node, &undef->def, state);
- return &undef->def;
-}
-
-/* Given a block and one of its predecessors, this function fills in the
- * souces of the phi nodes to take SSA defs from the given predecessor.
- * This function must be called exactly once per block/predecessor pair.
- */
-static void
-add_phi_sources(nir_block *block, nir_block *pred,
- struct lower_variables_state *state)
-{
- nir_foreach_instr(block, instr) {
- if (instr->type != nir_instr_type_phi)
- break;
-
- nir_phi_instr *phi = nir_instr_as_phi(instr);
-
- struct hash_entry *entry =
- _mesa_hash_table_search(state->phi_table, phi);
- if (!entry)
- continue;
-
- struct deref_node *node = entry->data;
-
- nir_phi_src *src = ralloc(phi, nir_phi_src);
- src->pred = pred;
- src->src.parent_instr = &phi->instr;
- src->src.is_ssa = true;
- src->src.ssa = get_ssa_def_for_block(node, pred, state);
-
- list_addtail(&src->src.use_link, &src->src.ssa->uses);
-
- exec_list_push_tail(&phi->srcs, &src->node);
- }
-}
-