1/* 2 * Copyright © 2014 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 * Authors: 24 * Jason Ekstrand (jason@jlekstrand.net) 25 * 26 */ 27 28#ifndef _NIR_SEARCH_ 29#define _NIR_SEARCH_ 30 31#include "nir.h" 32 33#define NIR_SEARCH_MAX_VARIABLES 16 34 35struct nir_builder; 36 37typedef enum { 38 nir_search_value_expression, 39 nir_search_value_variable, 40 nir_search_value_constant, 41} nir_search_value_type; 42 43typedef struct { 44 nir_search_value_type type; 45 46 /** 47 * Bit size of the value. It is interpreted as follows: 48 * 49 * For a search expression: 50 * - If bit_size > 0, then the value only matches an SSA value with the 51 * given bit size. 52 * - If bit_size <= 0, then the value matches any size SSA value. 53 * 54 * For a replace expression: 55 * - If bit_size > 0, then the value is constructed with the given bit size. 56 * - If bit_size == 0, then the value is constructed with the same bit size 57 * as the search value. 58 * - If bit_size < 0, then the value is constructed with the same bit size 59 * as variable (-bit_size - 1). 60 */ 61 int bit_size; 62} nir_search_value; 63 64typedef struct { 65 nir_search_value value; 66 67 /** The variable index; Must be less than NIR_SEARCH_MAX_VARIABLES */ 68 unsigned variable; 69 70 /** Indicates that the given variable must be a constant 71 * 72 * This is only allowed in search expressions and indicates that the 73 * given variable is only allowed to match constant values. 74 */ 75 bool is_constant; 76 77 /** Indicates that the given variable must have a certain type 78 * 79 * This is only allowed in search expressions and indicates that the 80 * given variable is only allowed to match values that come from an ALU 81 * instruction with the given output type. A type of nir_type_void 82 * means it can match any type. 83 * 84 * Note: A variable that is both constant and has a non-void type will 85 * never match anything. 86 */ 87 nir_alu_type type; 88 89 /** Optional condition fxn ptr 90 * 91 * This is only allowed in search expressions, and allows additional 92 * constraints to be placed on the match. Typically used for 'is_constant' 93 * variables to require, for example, power-of-two in order for the search 94 * to match. 95 */ 96 bool (*cond)(nir_alu_instr *instr, unsigned src, 97 unsigned num_components, const uint8_t *swizzle); 98} nir_search_variable; 99 100typedef struct { 101 nir_search_value value; 102 103 nir_alu_type type; 104 105 union { 106 uint64_t u; 107 int64_t i; 108 double d; 109 } data; 110} nir_search_constant; 111 112enum nir_search_op { 113 nir_search_op_i2f = nir_last_opcode + 1, 114 nir_search_op_u2f, 115 nir_search_op_f2f, 116 nir_search_op_f2u, 117 nir_search_op_f2i, 118 nir_search_op_u2u, 119 nir_search_op_i2i, 120 nir_search_op_b2f, 121 nir_search_op_b2i, 122 nir_search_op_i2b, 123 nir_search_op_f2b, 124 nir_num_search_ops, 125}; 126 127uint16_t nir_search_op_for_nir_op(nir_op op); 128 129typedef struct { 130 nir_search_value value; 131 132 /* When set on a search expression, the expression will only match an SSA 133 * value that does *not* have the exact bit set. If unset, the exact bit 134 * on the SSA value is ignored. 135 */ 136 bool inexact; 137 138 /* Commutative expression index. This is assigned by opt_algebraic.py when 139 * search structures are constructed and is a unique (to this structure) 140 * index within the commutative operation bitfield used for searching for 141 * all combinations of expressions containing commutative operations. 142 */ 143 int8_t comm_expr_idx; 144 145 /* Number of commutative expressions in this expression including this one 146 * (if it is commutative). 147 */ 148 uint8_t comm_exprs; 149 150 /* One of nir_op or nir_search_op */ 151 uint16_t opcode; 152 const nir_search_value *srcs[4]; 153 154 /** Optional condition fxn ptr 155 * 156 * This allows additional constraints on expression matching, it is 157 * typically used to match an expressions uses such as the number of times 158 * the expression is used, and whether its used by an if. 159 */ 160 bool (*cond)(nir_alu_instr *instr); 161} nir_search_expression; 162 163NIR_DEFINE_CAST(nir_search_value_as_variable, nir_search_value, 164 nir_search_variable, value, 165 type, nir_search_value_variable) 166NIR_DEFINE_CAST(nir_search_value_as_constant, nir_search_value, 167 nir_search_constant, value, 168 type, nir_search_value_constant) 169NIR_DEFINE_CAST(nir_search_value_as_expression, nir_search_value, 170 nir_search_expression, value, 171 type, nir_search_value_expression) 172 173nir_ssa_def * 174nir_replace_instr(struct nir_builder *b, nir_alu_instr *instr, 175 const nir_search_expression *search, 176 const nir_search_value *replace); 177 178#endif /* _NIR_SEARCH_ */ 179