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 bool ends_in_jump(nir_block *block) { if (exec_list_is_empty(&block->instr_list)) return false; nir_instr *instr = nir_block_last_instr(block); return instr->type == nir_instr_type_jump; }
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); }
bool nir_opt_dce_impl(nir_function_impl *impl) { struct exec_list *worklist = ralloc(NULL, struct exec_list); exec_list_make_empty(worklist); nir_foreach_block(impl, init_block_cb, worklist); while (!exec_list_is_empty(worklist)) { nir_instr *instr = worklist_pop(worklist); nir_foreach_src(instr, mark_live_cb, worklist); } ralloc_free(worklist); bool progress = false; nir_foreach_block(impl, delete_block_cb, &progress); if (progress) nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance); return progress; }
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; }