static bool loop_is_dead(nir_loop *loop) { nir_block *before = nir_cf_node_as_block(nir_cf_node_prev(&loop->cf_node)); nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node)); if (!exec_list_is_empty(&after->instr_list) && nir_block_first_instr(after)->type == nir_instr_type_phi) return false; if (!nir_foreach_block_in_cf_node(&loop->cf_node, block_has_no_side_effects, NULL)) return false; nir_function_impl *impl = nir_cf_node_get_function(&loop->cf_node); nir_metadata_require(impl, nir_metadata_live_variables | nir_metadata_dominance); for (nir_block *cur = after->imm_dom; cur != before; cur = cur->imm_dom) { nir_foreach_instr(cur, instr) { if (!nir_foreach_ssa_def(instr, def_not_live_out, after)) return false; } } return true; }
static void opt_constant_if(nir_if *if_stmt, bool condition) { /* First, we need to remove any phi nodes after the if by rewriting uses to * point to the correct source. */ nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&if_stmt->cf_node)); nir_block *last_block = nir_cf_node_as_block(condition ? nir_if_last_then_node(if_stmt) : nir_if_last_else_node(if_stmt)); nir_foreach_instr_safe(after, instr) { if (instr->type != nir_instr_type_phi) break; nir_phi_instr *phi = nir_instr_as_phi(instr); nir_ssa_def *def = NULL; nir_foreach_phi_src(phi, phi_src) { if (phi_src->pred != last_block) continue; assert(phi_src->src.is_ssa); def = phi_src->src.ssa; } assert(def); assert(phi->dest.is_ssa); nir_ssa_def_rewrite_uses(&phi->dest.ssa, nir_src_for_ssa(def)); nir_instr_remove(instr); } /* The control flow list we're about to paste in may include a jump at the * end, and in that case we have to delete the rest of the control flow * list after the if since it's unreachable and the validator will balk if * we don't. */ if (!exec_list_is_empty(&last_block->instr_list)) { nir_instr *last_instr = nir_block_last_instr(last_block); if (last_instr->type == nir_instr_type_jump) remove_after_cf_node(&if_stmt->cf_node); } /* Finally, actually paste in the then or else branch and delete the if. */ struct exec_list *cf_list = condition ? &if_stmt->then_list : &if_stmt->else_list; nir_cf_list list; nir_cf_extract(&list, nir_before_cf_list(cf_list), nir_after_cf_list(cf_list)); nir_cf_reinsert(&list, nir_after_cf_node(&if_stmt->cf_node)); nir_cf_node_remove(&if_stmt->cf_node); }
static bool dead_cf_list(struct exec_list *list, bool *list_ends_in_jump) { bool progress = false; *list_ends_in_jump = false; nir_cf_node *prev = NULL; foreach_list_typed(nir_cf_node, cur, node, list) { switch (cur->type) { case nir_cf_node_block: { nir_block *block = nir_cf_node_as_block(cur); if (dead_cf_block(block)) { /* We just deleted the if or loop after this block, so we may have * deleted the block before or after it -- which one is an * implementation detail. Therefore, to recover the place we were * at, we have to use the previous cf_node. */ if (prev) { cur = nir_cf_node_next(prev); } else { cur = exec_node_data(nir_cf_node, exec_list_get_head(list), node); } block = nir_cf_node_as_block(cur); progress = true; } if (ends_in_jump(block)) { *list_ends_in_jump = true; if (!exec_node_is_tail_sentinel(cur->node.next)) { remove_after_cf_node(cur); return true; } } break; } case nir_cf_node_if: { nir_if *if_stmt = nir_cf_node_as_if(cur); bool then_ends_in_jump, else_ends_in_jump; progress |= dead_cf_list(&if_stmt->then_list, &then_ends_in_jump); progress |= dead_cf_list(&if_stmt->else_list, &else_ends_in_jump); if (then_ends_in_jump && else_ends_in_jump) { *list_ends_in_jump = true; nir_block *next = nir_cf_node_as_block(nir_cf_node_next(cur)); if (!exec_list_is_empty(&next->instr_list) || !exec_node_is_tail_sentinel(next->cf_node.node.next)) { remove_after_cf_node(cur); return true; } } break; } case nir_cf_node_loop: { nir_loop *loop = nir_cf_node_as_loop(cur); bool dummy; progress |= dead_cf_list(&loop->body, &dummy); break; } default: unreachable("unknown cf node type"); } prev = cur; } return progress; }
static void emit_indirect_load_store(nir_builder *b, nir_intrinsic_instr *orig_instr, nir_deref_var *deref, nir_deref *arr_parent, int start, int end, nir_ssa_def **dest, nir_ssa_def *src) { assert(arr_parent->child && arr_parent->child->deref_type == nir_deref_type_array); nir_deref_array *arr = nir_deref_as_array(arr_parent->child); assert(arr->deref_array_type == nir_deref_array_type_indirect); assert(arr->indirect.is_ssa); assert(start < end); if (start == end - 1) { /* Base case. Just emit the load/store op */ nir_deref_array direct = *arr; direct.deref_array_type = nir_deref_array_type_direct; direct.base_offset += start; direct.indirect = NIR_SRC_INIT; arr_parent->child = &direct.deref; emit_load_store(b, orig_instr, deref, &arr->deref, dest, src); arr_parent->child = &arr->deref; } else { int mid = start + (end - start) / 2; nir_ssa_def *then_dest, *else_dest; nir_if *if_stmt = nir_if_create(b->shader); if_stmt->condition = nir_src_for_ssa(nir_ilt(b, arr->indirect.ssa, nir_imm_int(b, mid))); nir_cf_node_insert(b->cursor, &if_stmt->cf_node); b->cursor = nir_after_cf_list(&if_stmt->then_list); emit_indirect_load_store(b, orig_instr, deref, arr_parent, start, mid, &then_dest, src); b->cursor = nir_after_cf_list(&if_stmt->else_list); emit_indirect_load_store(b, orig_instr, deref, arr_parent, mid, end, &else_dest, src); b->cursor = nir_after_cf_node(&if_stmt->cf_node); if (src == NULL) { /* We're a load. We need to insert a phi node */ nir_phi_instr *phi = nir_phi_instr_create(b->shader); unsigned bit_size = then_dest->bit_size; nir_ssa_dest_init(&phi->instr, &phi->dest, then_dest->num_components, bit_size, NULL); nir_phi_src *src0 = ralloc(phi, nir_phi_src); src0->pred = nir_cf_node_as_block(nir_if_last_then_node(if_stmt)); src0->src = nir_src_for_ssa(then_dest); exec_list_push_tail(&phi->srcs, &src0->node); nir_phi_src *src1 = ralloc(phi, nir_phi_src); src1->pred = nir_cf_node_as_block(nir_if_last_else_node(if_stmt)); src1->src = nir_src_for_ssa(else_dest); exec_list_push_tail(&phi->srcs, &src1->node); nir_builder_instr_insert(b, &phi->instr); *dest = &phi->dest.ssa; } } }