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