2015-08-05 00:25:24 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2015 Broadcom
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nir.h"
|
2016-04-20 09:42:25 +01:00
|
|
|
#include "nir_builder.h"
|
2015-08-05 00:25:24 +01:00
|
|
|
|
|
|
|
/** @file nir_opt_undef.c
|
|
|
|
*
|
2016-04-20 09:15:53 +01:00
|
|
|
* Handles optimization of operations involving ssa_undef.
|
2015-08-05 00:25:24 +01:00
|
|
|
*/
|
|
|
|
|
2016-04-20 09:15:53 +01:00
|
|
|
/**
|
|
|
|
* Turn conditional selects between an undef and some other value into a move
|
|
|
|
* of that other value (on the assumption that the condition's going to be
|
|
|
|
* choosing the defined value). This reduces work after if flattening when
|
|
|
|
* each side of the if is defining a variable.
|
|
|
|
*/
|
2015-08-05 00:25:24 +01:00
|
|
|
static bool
|
2016-04-20 09:15:53 +01:00
|
|
|
opt_undef_csel(nir_alu_instr *instr)
|
2015-08-05 00:25:24 +01:00
|
|
|
{
|
2018-10-19 17:14:47 +01:00
|
|
|
if (instr->op != nir_op_bcsel && instr->op != nir_op_fcsel)
|
2015-08-05 00:25:24 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(instr->dest.dest.is_ssa);
|
|
|
|
|
|
|
|
for (int i = 1; i <= 2; i++) {
|
|
|
|
if (!instr->src[i].src.is_ssa)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nir_instr *parent = instr->src[i].src.ssa->parent_instr;
|
|
|
|
if (parent->type != nir_instr_type_ssa_undef)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We can't just use nir_alu_src_copy, because we need the def/use
|
|
|
|
* updated.
|
|
|
|
*/
|
|
|
|
nir_instr_rewrite_src(&instr->instr, &instr->src[0].src,
|
|
|
|
instr->src[i == 1 ? 2 : 1].src);
|
|
|
|
nir_alu_src_copy(&instr->src[0], &instr->src[i == 1 ? 2 : 1],
|
2021-07-07 19:04:49 +01:00
|
|
|
instr);
|
2015-08-05 00:25:24 +01:00
|
|
|
|
|
|
|
nir_src empty_src;
|
|
|
|
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);
|
2019-05-06 17:45:46 +01:00
|
|
|
instr->op = nir_op_mov;
|
2015-08-05 00:25:24 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:42:25 +01:00
|
|
|
/**
|
|
|
|
* Replace vecN(undef, undef, ...) with a single undef.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
opt_undef_vecN(nir_builder *b, nir_alu_instr *alu)
|
|
|
|
{
|
2020-03-30 18:09:03 +01:00
|
|
|
if (!nir_op_is_vec(alu->op))
|
2016-04-20 09:42:25 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(alu->dest.dest.is_ssa);
|
|
|
|
|
2016-12-15 23:17:33 +00:00
|
|
|
for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
|
2016-04-20 09:42:25 +01:00
|
|
|
if (!alu->src[i].src.is_ssa ||
|
|
|
|
alu->src[i].src.ssa->parent_instr->type != nir_instr_type_ssa_undef)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
b->cursor = nir_before_instr(&alu->instr);
|
2016-12-15 23:17:33 +00:00
|
|
|
nir_ssa_def *undef = nir_ssa_undef(b, alu->dest.dest.ssa.num_components,
|
|
|
|
nir_dest_bit_size(alu->dest.dest));
|
2021-03-03 06:13:38 +00:00
|
|
|
nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, undef);
|
2016-04-20 09:42:25 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 22:24:15 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:38:57 +01:00
|
|
|
/**
|
2020-07-23 22:24:15 +01:00
|
|
|
* Remove any store intrinsic writemask channels whose value is undefined (the
|
|
|
|
* existing value is a fine representation of "undefined").
|
2016-04-20 10:38:57 +01:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
opt_undef_store(nir_intrinsic_instr *intrin)
|
|
|
|
{
|
2018-04-03 00:44:24 +01:00
|
|
|
int arg_index;
|
2016-04-20 10:38:57 +01:00
|
|
|
switch (intrin->intrinsic) {
|
2018-04-03 00:44:24 +01:00
|
|
|
case nir_intrinsic_store_deref:
|
|
|
|
arg_index = 1;
|
|
|
|
break;
|
2016-04-20 10:38:57 +01:00
|
|
|
case nir_intrinsic_store_output:
|
|
|
|
case nir_intrinsic_store_per_vertex_output:
|
2021-05-03 20:04:01 +01:00
|
|
|
case nir_intrinsic_store_per_primitive_output:
|
2016-04-20 10:38:57 +01:00
|
|
|
case nir_intrinsic_store_ssbo:
|
|
|
|
case nir_intrinsic_store_shared:
|
2020-07-23 21:39:56 +01:00
|
|
|
case nir_intrinsic_store_global:
|
|
|
|
case nir_intrinsic_store_scratch:
|
2018-04-03 00:44:24 +01:00
|
|
|
arg_index = 0;
|
2016-04-20 10:38:57 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-23 22:24:15 +01:00
|
|
|
if (!intrin->src[arg_index].is_ssa)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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))
|
2016-04-20 10:38:57 +01:00
|
|
|
return false;
|
|
|
|
|
2020-07-23 22:24:15 +01:00
|
|
|
write_mask &= ~undef_mask;
|
|
|
|
if (!write_mask)
|
|
|
|
nir_instr_remove(&intrin->instr);
|
|
|
|
else
|
|
|
|
nir_intrinsic_set_write_mask(intrin, write_mask);
|
2016-04-20 10:38:57 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-20 19:56:04 +01:00
|
|
|
static bool
|
|
|
|
nir_opt_undef_instr(nir_builder *b, nir_instr *instr, void *data)
|
2015-08-05 00:25:24 +01:00
|
|
|
{
|
2020-08-20 19:56:04 +01:00
|
|
|
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);
|
2015-08-05 00:25:24 +01:00
|
|
|
}
|
|
|
|
|
2020-08-20 19:56:04 +01:00
|
|
|
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);
|
2015-08-05 00:25:24 +01:00
|
|
|
}
|