memset(&empty_src, 0, sizeof(empty_src));
nir_instr_rewrite_src(&instr->instr, &instr->src[1].src, empty_src);
nir_instr_rewrite_src(&instr->instr, &instr->src[2].src, empty_src);
- instr->op = nir_op_imov;
+ instr->op = nir_op_mov;
return true;
}
static bool
opt_undef_vecN(nir_builder *b, nir_alu_instr *alu)
{
- if (alu->op != nir_op_vec2 &&
- alu->op != nir_op_vec3 &&
- alu->op != nir_op_vec4 &&
- alu->op != nir_op_fmov &&
- alu->op != nir_op_imov)
+ if (!nir_op_is_vec(alu->op))
return false;
assert(alu->dest.dest.is_ssa);
return true;
}
+static uint32_t
+nir_get_undef_mask(nir_ssa_def *def)
+{
+ nir_instr *instr = def->parent_instr;
+
+ if (instr->type == nir_instr_type_ssa_undef)
+ return BITSET_MASK(def->num_components);
+
+ if (instr->type != nir_instr_type_alu)
+ return 0;
+
+ nir_alu_instr *alu = nir_instr_as_alu(instr);
+ unsigned undef = 0;
+
+ if (nir_op_is_vec(alu->op)) {
+ for (int i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
+ if (alu->src[i].src.is_ssa &&
+ alu->src[i].src.ssa->parent_instr->type ==
+ nir_instr_type_ssa_undef) {
+ undef |= BITSET_MASK(nir_ssa_alu_instr_src_components(alu, i)) << i;
+ }
+ }
+ }
+
+ return undef;
+}
+
/**
- * Remove any store intrinsics whose value is undefined (the existing
- * value is a fine representation of "undefined").
+ * Remove any store intrinsic writemask channels whose value is undefined (the
+ * existing value is a fine representation of "undefined").
*/
static bool
opt_undef_store(nir_intrinsic_instr *intrin)
case nir_intrinsic_store_per_vertex_output:
case nir_intrinsic_store_ssbo:
case nir_intrinsic_store_shared:
+ case nir_intrinsic_store_global:
+ case nir_intrinsic_store_scratch:
arg_index = 0;
break;
default:
return false;
}
- if (!intrin->src[arg_index].is_ssa ||
- intrin->src[arg_index].ssa->parent_instr->type != nir_instr_type_ssa_undef)
+ if (!intrin->src[arg_index].is_ssa)
return false;
- nir_instr_remove(&intrin->instr);
+ nir_ssa_def *def = intrin->src[arg_index].ssa;
+
+ unsigned write_mask = nir_intrinsic_write_mask(intrin);
+ unsigned undef_mask = nir_get_undef_mask(def);
+
+ if (!(write_mask & undef_mask))
+ return false;
+
+ write_mask &= ~undef_mask;
+ if (!write_mask)
+ nir_instr_remove(&intrin->instr);
+ else
+ nir_intrinsic_set_write_mask(intrin, write_mask);
return true;
}
-bool
-nir_opt_undef(nir_shader *shader)
+static bool
+nir_opt_undef_instr(nir_builder *b, nir_instr *instr, void *data)
{
- nir_builder b;
- bool progress = false;
-
- nir_foreach_function(function, shader) {
- if (function->impl) {
- nir_builder_init(&b, function->impl);
- nir_foreach_block(block, function->impl) {
- nir_foreach_instr_safe(instr, block) {
- if (instr->type == nir_instr_type_alu) {
- nir_alu_instr *alu = nir_instr_as_alu(instr);
-
- progress = opt_undef_csel(alu) || progress;
- progress = opt_undef_vecN(&b, alu) || progress;
- } else if (instr->type == nir_instr_type_intrinsic) {
- nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
- progress = opt_undef_store(intrin) || progress;
- }
- }
- }
-
- if (progress)
- nir_metadata_preserve(function->impl,
- nir_metadata_block_index |
- nir_metadata_dominance);
- }
+ if (instr->type == nir_instr_type_alu) {
+ nir_alu_instr *alu = nir_instr_as_alu(instr);
+ return opt_undef_csel(alu) || opt_undef_vecN(b, alu);
+ } else if (instr->type == nir_instr_type_intrinsic) {
+ nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+ return opt_undef_store(intrin);
}
- return progress;
+ return false;
+}
+
+bool
+nir_opt_undef(nir_shader *shader)
+{
+ return nir_shader_instructions_pass(shader,
+ nir_opt_undef_instr,
+ nir_metadata_block_index |
+ nir_metadata_dominance,
+ NULL);
}