1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2022 Imagination Technologies Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 5bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 6bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights 7bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 9bf215546Sopenharmony_ci * 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 THE 18bf215546Sopenharmony_ci * 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 FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifndef ROGUE_NIR_HELPERS_H 25bf215546Sopenharmony_ci#define ROGUE_NIR_HELPERS_H 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include <assert.h> 28bf215546Sopenharmony_ci#include <stddef.h> 29bf215546Sopenharmony_ci#include <stdint.h> 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ci#include "nir/nir.h" 32bf215546Sopenharmony_ci#include "util/bitscan.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci/** 35bf215546Sopenharmony_ci * \file rogue_nir.c 36bf215546Sopenharmony_ci * 37bf215546Sopenharmony_ci * \brief Contains various NIR helper functions. 38bf215546Sopenharmony_ci */ 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cistatic inline unsigned nir_alu_dest_regindex(const nir_alu_instr *alu) 41bf215546Sopenharmony_ci{ 42bf215546Sopenharmony_ci assert(!alu->dest.dest.is_ssa); 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci return alu->dest.dest.reg.reg->index; 45bf215546Sopenharmony_ci} 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_cistatic inline unsigned nir_alu_dest_comp(const nir_alu_instr *alu) 48bf215546Sopenharmony_ci{ 49bf215546Sopenharmony_ci assert(!alu->dest.dest.is_ssa); 50bf215546Sopenharmony_ci assert(util_is_power_of_two_nonzero(alu->dest.write_mask)); 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci return ffs(alu->dest.write_mask) - 1; 53bf215546Sopenharmony_ci} 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_cistatic inline unsigned nir_alu_src_regindex(const nir_alu_instr *alu, 56bf215546Sopenharmony_ci size_t src) 57bf215546Sopenharmony_ci{ 58bf215546Sopenharmony_ci assert(src < nir_op_infos[alu->op].num_inputs); 59bf215546Sopenharmony_ci assert(!alu->src[src].src.is_ssa); 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci return alu->src[src].src.reg.reg->index; 62bf215546Sopenharmony_ci} 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_cistatic inline uint32_t nir_alu_src_const(const nir_alu_instr *alu, size_t src) 65bf215546Sopenharmony_ci{ 66bf215546Sopenharmony_ci assert(src < nir_op_infos[alu->op].num_inputs); 67bf215546Sopenharmony_ci assert(alu->src[src].src.is_ssa); 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci nir_const_value *const_value = nir_src_as_const_value(alu->src[src].src); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci return nir_const_value_as_uint(*const_value, 32); 72bf215546Sopenharmony_ci} 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_cistatic inline bool nir_alu_src_is_const(const nir_alu_instr *alu, size_t src) 75bf215546Sopenharmony_ci{ 76bf215546Sopenharmony_ci assert(src < nir_op_infos[alu->op].num_inputs); 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci if (!alu->src[src].src.is_ssa) 79bf215546Sopenharmony_ci return false; 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_ci assert(alu->src[src].src.ssa->parent_instr); 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci return (alu->src[src].src.ssa->parent_instr->type == 84bf215546Sopenharmony_ci nir_instr_type_load_const); 85bf215546Sopenharmony_ci} 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_cistatic inline unsigned nir_intr_dest_regindex(const nir_intrinsic_instr *intr) 88bf215546Sopenharmony_ci{ 89bf215546Sopenharmony_ci assert(!intr->dest.is_ssa); 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci return intr->dest.reg.reg->index; 92bf215546Sopenharmony_ci} 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_cistatic inline unsigned nir_intr_src_regindex(const nir_intrinsic_instr *intr, 95bf215546Sopenharmony_ci size_t src) 96bf215546Sopenharmony_ci{ 97bf215546Sopenharmony_ci assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs); 98bf215546Sopenharmony_ci assert(!intr->src[src].is_ssa); 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci return intr->src[src].reg.reg->index; 101bf215546Sopenharmony_ci} 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_cistatic inline uint32_t nir_intr_src_const(const nir_intrinsic_instr *intr, 104bf215546Sopenharmony_ci size_t src) 105bf215546Sopenharmony_ci{ 106bf215546Sopenharmony_ci assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs); 107bf215546Sopenharmony_ci assert(intr->src[src].is_ssa); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci nir_const_value *const_value = nir_src_as_const_value(intr->src[src]); 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci return nir_const_value_as_uint(*const_value, 32); 112bf215546Sopenharmony_ci} 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_cistatic inline uint32_t nir_intr_src_comp_const(const nir_intrinsic_instr *intr, 115bf215546Sopenharmony_ci size_t src, 116bf215546Sopenharmony_ci size_t comp) 117bf215546Sopenharmony_ci{ 118bf215546Sopenharmony_ci assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs); 119bf215546Sopenharmony_ci assert(intr->src[src].is_ssa); 120bf215546Sopenharmony_ci assert(comp < nir_src_num_components(intr->src[src])); 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci return nir_src_comp_as_uint(intr->src[src], comp); 123bf215546Sopenharmony_ci} 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_cistatic inline bool nir_intr_src_is_const(const nir_intrinsic_instr *intr, 126bf215546Sopenharmony_ci size_t src) 127bf215546Sopenharmony_ci{ 128bf215546Sopenharmony_ci assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs); 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci if (!intr->src[src].is_ssa) 131bf215546Sopenharmony_ci return false; 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci assert(intr->src[src].ssa->parent_instr); 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci return (intr->src[src].ssa->parent_instr->type == nir_instr_type_load_const); 136bf215546Sopenharmony_ci} 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_cistatic inline size_t nir_count_variables_with_modes(const nir_shader *nir, 139bf215546Sopenharmony_ci nir_variable_mode mode) 140bf215546Sopenharmony_ci{ 141bf215546Sopenharmony_ci size_t count = 0; 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci nir_foreach_variable_with_modes (var, nir, mode) 144bf215546Sopenharmony_ci ++count; 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci return count; 147bf215546Sopenharmony_ci} 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_ci#endif /* ROGUE_NIR_HELPERS_H */ 150