1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2017 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg#include "nir.h" 25b8e80941Smrg#include "nir_builder.h" 26b8e80941Smrg 27b8e80941Smrg/** 28b8e80941Smrg * \file nir_opt_intrinsics.c 29b8e80941Smrg */ 30b8e80941Smrg 31b8e80941Smrgstatic bool 32b8e80941Smrgopt_intrinsics_impl(nir_function_impl *impl, 33b8e80941Smrg const struct nir_shader_compiler_options *options) 34b8e80941Smrg{ 35b8e80941Smrg nir_builder b; 36b8e80941Smrg nir_builder_init(&b, impl); 37b8e80941Smrg bool progress = false; 38b8e80941Smrg 39b8e80941Smrg nir_foreach_block(block, impl) { 40b8e80941Smrg nir_foreach_instr_safe(instr, block) { 41b8e80941Smrg if (instr->type != nir_instr_type_intrinsic) 42b8e80941Smrg continue; 43b8e80941Smrg 44b8e80941Smrg nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); 45b8e80941Smrg nir_ssa_def *replacement = NULL; 46b8e80941Smrg b.cursor = nir_before_instr(instr); 47b8e80941Smrg 48b8e80941Smrg switch (intrin->intrinsic) { 49b8e80941Smrg case nir_intrinsic_vote_any: 50b8e80941Smrg case nir_intrinsic_vote_all: 51b8e80941Smrg if (nir_src_is_const(intrin->src[0])) 52b8e80941Smrg replacement = nir_ssa_for_src(&b, intrin->src[0], 1); 53b8e80941Smrg break; 54b8e80941Smrg case nir_intrinsic_vote_feq: 55b8e80941Smrg case nir_intrinsic_vote_ieq: 56b8e80941Smrg if (nir_src_is_const(intrin->src[0])) 57b8e80941Smrg replacement = nir_imm_true(&b); 58b8e80941Smrg break; 59b8e80941Smrg case nir_intrinsic_load_sample_mask_in: 60b8e80941Smrg /* Transform: 61b8e80941Smrg * gl_SampleMaskIn == 0 ---> gl_HelperInvocation 62b8e80941Smrg * gl_SampleMaskIn != 0 ---> !gl_HelperInvocation 63b8e80941Smrg */ 64b8e80941Smrg if (!options->optimize_sample_mask_in) 65b8e80941Smrg continue; 66b8e80941Smrg 67b8e80941Smrg nir_foreach_use_safe(use_src, &intrin->dest.ssa) { 68b8e80941Smrg if (use_src->parent_instr->type == nir_instr_type_alu) { 69b8e80941Smrg nir_alu_instr *alu = nir_instr_as_alu(use_src->parent_instr); 70b8e80941Smrg 71b8e80941Smrg if (alu->op == nir_op_ieq || 72b8e80941Smrg alu->op == nir_op_ine) { 73b8e80941Smrg /* Check for 0 in either operand. */ 74b8e80941Smrg nir_const_value *const_val = 75b8e80941Smrg nir_src_as_const_value(alu->src[0].src); 76b8e80941Smrg if (!const_val) 77b8e80941Smrg const_val = nir_src_as_const_value(alu->src[1].src); 78b8e80941Smrg if (!const_val || const_val->i32 != 0) 79b8e80941Smrg continue; 80b8e80941Smrg 81b8e80941Smrg nir_ssa_def *new_expr = nir_load_helper_invocation(&b, 1); 82b8e80941Smrg 83b8e80941Smrg if (alu->op == nir_op_ine) 84b8e80941Smrg new_expr = nir_inot(&b, new_expr); 85b8e80941Smrg 86b8e80941Smrg nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, 87b8e80941Smrg nir_src_for_ssa(new_expr)); 88b8e80941Smrg nir_instr_remove(&alu->instr); 89b8e80941Smrg continue; 90b8e80941Smrg } 91b8e80941Smrg } 92b8e80941Smrg } 93b8e80941Smrg continue; 94b8e80941Smrg default: 95b8e80941Smrg break; 96b8e80941Smrg } 97b8e80941Smrg 98b8e80941Smrg if (!replacement) 99b8e80941Smrg continue; 100b8e80941Smrg 101b8e80941Smrg nir_ssa_def_rewrite_uses(&intrin->dest.ssa, 102b8e80941Smrg nir_src_for_ssa(replacement)); 103b8e80941Smrg nir_instr_remove(instr); 104b8e80941Smrg progress = true; 105b8e80941Smrg } 106b8e80941Smrg } 107b8e80941Smrg 108b8e80941Smrg return progress; 109b8e80941Smrg} 110b8e80941Smrg 111b8e80941Smrgbool 112b8e80941Smrgnir_opt_intrinsics(nir_shader *shader) 113b8e80941Smrg{ 114b8e80941Smrg bool progress = false; 115b8e80941Smrg 116b8e80941Smrg nir_foreach_function(function, shader) { 117b8e80941Smrg if (!function->impl) 118b8e80941Smrg continue; 119b8e80941Smrg 120b8e80941Smrg if (opt_intrinsics_impl(function->impl, shader->options)) { 121b8e80941Smrg progress = true; 122b8e80941Smrg nir_metadata_preserve(function->impl, nir_metadata_block_index | 123b8e80941Smrg nir_metadata_dominance); 124b8e80941Smrg } 125b8e80941Smrg } 126b8e80941Smrg 127b8e80941Smrg return progress; 128b8e80941Smrg} 129