1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2015 Broadcom 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "nir.h" 25bf215546Sopenharmony_ci#include "nir_builder.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci/** @file nir_opt_undef.c 28bf215546Sopenharmony_ci * 29bf215546Sopenharmony_ci * Handles optimization of operations involving ssa_undef. 30bf215546Sopenharmony_ci */ 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci/** 33bf215546Sopenharmony_ci * Turn conditional selects between an undef and some other value into a move 34bf215546Sopenharmony_ci * of that other value (on the assumption that the condition's going to be 35bf215546Sopenharmony_ci * choosing the defined value). This reduces work after if flattening when 36bf215546Sopenharmony_ci * each side of the if is defining a variable. 37bf215546Sopenharmony_ci */ 38bf215546Sopenharmony_cistatic bool 39bf215546Sopenharmony_ciopt_undef_csel(nir_alu_instr *instr) 40bf215546Sopenharmony_ci{ 41bf215546Sopenharmony_ci if (!nir_op_is_selection(instr->op)) 42bf215546Sopenharmony_ci return false; 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci assert(instr->dest.dest.is_ssa); 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci for (int i = 1; i <= 2; i++) { 47bf215546Sopenharmony_ci if (!instr->src[i].src.is_ssa) 48bf215546Sopenharmony_ci continue; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci nir_instr *parent = instr->src[i].src.ssa->parent_instr; 51bf215546Sopenharmony_ci if (parent->type != nir_instr_type_ssa_undef) 52bf215546Sopenharmony_ci continue; 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci /* We can't just use nir_alu_src_copy, because we need the def/use 55bf215546Sopenharmony_ci * updated. 56bf215546Sopenharmony_ci */ 57bf215546Sopenharmony_ci nir_instr_rewrite_src(&instr->instr, &instr->src[0].src, 58bf215546Sopenharmony_ci instr->src[i == 1 ? 2 : 1].src); 59bf215546Sopenharmony_ci nir_alu_src_copy(&instr->src[0], &instr->src[i == 1 ? 2 : 1]); 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci nir_src empty_src; 62bf215546Sopenharmony_ci memset(&empty_src, 0, sizeof(empty_src)); 63bf215546Sopenharmony_ci nir_instr_rewrite_src(&instr->instr, &instr->src[1].src, empty_src); 64bf215546Sopenharmony_ci nir_instr_rewrite_src(&instr->instr, &instr->src[2].src, empty_src); 65bf215546Sopenharmony_ci instr->op = nir_op_mov; 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci return true; 68bf215546Sopenharmony_ci } 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci return false; 71bf215546Sopenharmony_ci} 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci/** 74bf215546Sopenharmony_ci * Replace vecN(undef, undef, ...) with a single undef. 75bf215546Sopenharmony_ci */ 76bf215546Sopenharmony_cistatic bool 77bf215546Sopenharmony_ciopt_undef_vecN(nir_builder *b, nir_alu_instr *alu) 78bf215546Sopenharmony_ci{ 79bf215546Sopenharmony_ci if (!nir_op_is_vec(alu->op)) 80bf215546Sopenharmony_ci return false; 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci assert(alu->dest.dest.is_ssa); 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) { 85bf215546Sopenharmony_ci if (!alu->src[i].src.is_ssa || 86bf215546Sopenharmony_ci alu->src[i].src.ssa->parent_instr->type != nir_instr_type_ssa_undef) 87bf215546Sopenharmony_ci return false; 88bf215546Sopenharmony_ci } 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci b->cursor = nir_before_instr(&alu->instr); 91bf215546Sopenharmony_ci nir_ssa_def *undef = nir_ssa_undef(b, alu->dest.dest.ssa.num_components, 92bf215546Sopenharmony_ci nir_dest_bit_size(alu->dest.dest)); 93bf215546Sopenharmony_ci nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, undef); 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci return true; 96bf215546Sopenharmony_ci} 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_cistatic uint32_t 99bf215546Sopenharmony_cinir_get_undef_mask(nir_ssa_def *def) 100bf215546Sopenharmony_ci{ 101bf215546Sopenharmony_ci nir_instr *instr = def->parent_instr; 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci if (instr->type == nir_instr_type_ssa_undef) 104bf215546Sopenharmony_ci return BITSET_MASK(def->num_components); 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci if (instr->type != nir_instr_type_alu) 107bf215546Sopenharmony_ci return 0; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci nir_alu_instr *alu = nir_instr_as_alu(instr); 110bf215546Sopenharmony_ci unsigned undef = 0; 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci if (nir_op_is_vec(alu->op)) { 113bf215546Sopenharmony_ci for (int i = 0; i < nir_op_infos[alu->op].num_inputs; i++) { 114bf215546Sopenharmony_ci if (alu->src[i].src.is_ssa && 115bf215546Sopenharmony_ci alu->src[i].src.ssa->parent_instr->type == 116bf215546Sopenharmony_ci nir_instr_type_ssa_undef) { 117bf215546Sopenharmony_ci undef |= BITSET_MASK(nir_ssa_alu_instr_src_components(alu, i)) << i; 118bf215546Sopenharmony_ci } 119bf215546Sopenharmony_ci } 120bf215546Sopenharmony_ci } 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci return undef; 123bf215546Sopenharmony_ci} 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_ci/** 126bf215546Sopenharmony_ci * Remove any store intrinsic writemask channels whose value is undefined (the 127bf215546Sopenharmony_ci * existing value is a fine representation of "undefined"). 128bf215546Sopenharmony_ci */ 129bf215546Sopenharmony_cistatic bool 130bf215546Sopenharmony_ciopt_undef_store(nir_intrinsic_instr *intrin) 131bf215546Sopenharmony_ci{ 132bf215546Sopenharmony_ci int arg_index; 133bf215546Sopenharmony_ci switch (intrin->intrinsic) { 134bf215546Sopenharmony_ci case nir_intrinsic_store_deref: 135bf215546Sopenharmony_ci arg_index = 1; 136bf215546Sopenharmony_ci break; 137bf215546Sopenharmony_ci case nir_intrinsic_store_output: 138bf215546Sopenharmony_ci case nir_intrinsic_store_per_vertex_output: 139bf215546Sopenharmony_ci case nir_intrinsic_store_per_primitive_output: 140bf215546Sopenharmony_ci case nir_intrinsic_store_ssbo: 141bf215546Sopenharmony_ci case nir_intrinsic_store_shared: 142bf215546Sopenharmony_ci case nir_intrinsic_store_global: 143bf215546Sopenharmony_ci case nir_intrinsic_store_scratch: 144bf215546Sopenharmony_ci arg_index = 0; 145bf215546Sopenharmony_ci break; 146bf215546Sopenharmony_ci default: 147bf215546Sopenharmony_ci return false; 148bf215546Sopenharmony_ci } 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci if (!intrin->src[arg_index].is_ssa) 151bf215546Sopenharmony_ci return false; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci nir_ssa_def *def = intrin->src[arg_index].ssa; 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci unsigned write_mask = nir_intrinsic_write_mask(intrin); 156bf215546Sopenharmony_ci unsigned undef_mask = nir_get_undef_mask(def); 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci if (!(write_mask & undef_mask)) 159bf215546Sopenharmony_ci return false; 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci write_mask &= ~undef_mask; 162bf215546Sopenharmony_ci if (!write_mask) 163bf215546Sopenharmony_ci nir_instr_remove(&intrin->instr); 164bf215546Sopenharmony_ci else 165bf215546Sopenharmony_ci nir_intrinsic_set_write_mask(intrin, write_mask); 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci return true; 168bf215546Sopenharmony_ci} 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_cistatic bool 171bf215546Sopenharmony_cinir_opt_undef_instr(nir_builder *b, nir_instr *instr, void *data) 172bf215546Sopenharmony_ci{ 173bf215546Sopenharmony_ci if (instr->type == nir_instr_type_alu) { 174bf215546Sopenharmony_ci nir_alu_instr *alu = nir_instr_as_alu(instr); 175bf215546Sopenharmony_ci return opt_undef_csel(alu) || opt_undef_vecN(b, alu); 176bf215546Sopenharmony_ci } else if (instr->type == nir_instr_type_intrinsic) { 177bf215546Sopenharmony_ci nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 178bf215546Sopenharmony_ci return opt_undef_store(intrin); 179bf215546Sopenharmony_ci } 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci return false; 182bf215546Sopenharmony_ci} 183bf215546Sopenharmony_ci 184bf215546Sopenharmony_cibool 185bf215546Sopenharmony_cinir_opt_undef(nir_shader *shader) 186bf215546Sopenharmony_ci{ 187bf215546Sopenharmony_ci return nir_shader_instructions_pass(shader, 188bf215546Sopenharmony_ci nir_opt_undef_instr, 189bf215546Sopenharmony_ci nir_metadata_block_index | 190bf215546Sopenharmony_ci nir_metadata_dominance, 191bf215546Sopenharmony_ci NULL); 192bf215546Sopenharmony_ci} 193