- nir_foreach_function(shader, function) {
- if (function->impl) {
- nir_foreach_block(function->impl, add_var_use_block, live);
+ nir_foreach_function(function, shader) {
+ if (!function->impl)
+ continue;
+
+ nir_foreach_block(block, function->impl) {
+ nir_foreach_instr_safe(instr, block) {
+ switch (instr->type) {
+ case nir_instr_type_deref: {
+ nir_deref_instr *deref = nir_instr_as_deref(instr);
+ if (deref->deref_type == nir_deref_type_cast &&
+ !nir_deref_instr_parent(deref))
+ continue;
+
+ nir_variable_mode parent_mode;
+ if (deref->deref_type == nir_deref_type_var)
+ parent_mode = deref->var->data.mode;
+ else
+ parent_mode = nir_deref_instr_parent(deref)->mode;
+
+ /* If the parent mode is 0, then it references a dead variable.
+ * Flag this deref as dead and remove it.
+ */
+ if (parent_mode == 0) {
+ deref->mode = 0;
+ nir_instr_remove(&deref->instr);
+ }
+ break;
+ }
+
+ case nir_instr_type_intrinsic: {
+ nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+ if (intrin->intrinsic != nir_intrinsic_copy_deref &&
+ intrin->intrinsic != nir_intrinsic_store_deref)
+ break;
+
+ if (nir_src_as_deref(intrin->src[0])->mode == 0)
+ nir_instr_remove(instr);
+ break;
+ }
+
+ default:
+ break; /* Nothing to do */
+ }
+ }