1/* 2 * Copyright © 2017 Intel Corporation 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/** 28 * \file nir_opt_intrinsics.c 29 */ 30 31static bool 32opt_intrinsics_impl(nir_function_impl *impl, 33 const struct nir_shader_compiler_options *options) 34{ 35 nir_builder b; 36 nir_builder_init(&b, impl); 37 bool progress = false; 38 39 nir_foreach_block(block, impl) { 40 nir_foreach_instr_safe(instr, block) { 41 if (instr->type != nir_instr_type_intrinsic) 42 continue; 43 44 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 45 nir_ssa_def *replacement = NULL; 46 b.cursor = nir_before_instr(instr); 47 48 switch (intrin->intrinsic) { 49 case nir_intrinsic_vote_any: 50 case nir_intrinsic_vote_all: 51 if (nir_src_is_const(intrin->src[0])) 52 replacement = nir_ssa_for_src(&b, intrin->src[0], 1); 53 break; 54 case nir_intrinsic_vote_feq: 55 case nir_intrinsic_vote_ieq: 56 if (nir_src_is_const(intrin->src[0])) 57 replacement = nir_imm_true(&b); 58 break; 59 case nir_intrinsic_load_sample_mask_in: 60 /* Transform: 61 * gl_SampleMaskIn == 0 ---> gl_HelperInvocation 62 * gl_SampleMaskIn != 0 ---> !gl_HelperInvocation 63 */ 64 if (!options->optimize_sample_mask_in) 65 continue; 66 67 nir_foreach_use_safe(use_src, &intrin->dest.ssa) { 68 if (use_src->parent_instr->type == nir_instr_type_alu) { 69 nir_alu_instr *alu = nir_instr_as_alu(use_src->parent_instr); 70 71 if (alu->op == nir_op_ieq || 72 alu->op == nir_op_ine) { 73 /* Check for 0 in either operand. */ 74 nir_const_value *const_val = 75 nir_src_as_const_value(alu->src[0].src); 76 if (!const_val) 77 const_val = nir_src_as_const_value(alu->src[1].src); 78 if (!const_val || const_val->i32 != 0) 79 continue; 80 81 nir_ssa_def *new_expr = nir_load_helper_invocation(&b, 1); 82 83 if (alu->op == nir_op_ine) 84 new_expr = nir_inot(&b, new_expr); 85 86 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, 87 nir_src_for_ssa(new_expr)); 88 nir_instr_remove(&alu->instr); 89 continue; 90 } 91 } 92 } 93 continue; 94 default: 95 break; 96 } 97 98 if (!replacement) 99 continue; 100 101 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, 102 nir_src_for_ssa(replacement)); 103 nir_instr_remove(instr); 104 progress = true; 105 } 106 } 107 108 return progress; 109} 110 111bool 112nir_opt_intrinsics(nir_shader *shader) 113{ 114 bool progress = false; 115 116 nir_foreach_function(function, shader) { 117 if (!function->impl) 118 continue; 119 120 if (opt_intrinsics_impl(function->impl, shader->options)) { 121 progress = true; 122 nir_metadata_preserve(function->impl, nir_metadata_block_index | 123 nir_metadata_dominance); 124 } 125 } 126 127 return progress; 128} 129