2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
33 #define NIR_SEARCH_MAX_VARIABLES 16
38 nir_search_value_expression
,
39 nir_search_value_variable
,
40 nir_search_value_constant
,
41 } nir_search_value_type
;
44 nir_search_value_type type
;
47 * Bit size of the value. It is interpreted as follows:
49 * For a search expression:
50 * - If bit_size > 0, then the value only matches an SSA value with the
52 * - If bit_size <= 0, then the value matches any size SSA value.
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).
65 nir_search_value value
;
67 /** The variable index; Must be less than NIR_SEARCH_MAX_VARIABLES */
70 /** Indicates that the given variable must be a constant
72 * This is only allowed in search expressions and indicates that the
73 * given variable is only allowed to match constant values.
77 /** Indicates that the given variable must have a certain type
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.
84 * Note: A variable that is both constant and has a non-void type will
85 * never match anything.
89 /** Optional condition fxn ptr
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
96 bool (*cond
)(struct hash_table
*range_ht
, nir_alu_instr
*instr
, unsigned src
,
97 unsigned num_components
, const uint8_t *swizzle
);
99 /** Swizzle (for replace only) */
100 uint8_t swizzle
[NIR_MAX_VEC_COMPONENTS
];
101 } nir_search_variable
;
104 nir_search_value value
;
113 } nir_search_constant
;
116 nir_search_op_i2f
= nir_last_opcode
+ 1,
130 uint16_t nir_search_op_for_nir_op(nir_op op
);
133 nir_search_value value
;
135 /* When set on a search expression, the expression will only match an SSA
136 * value that does *not* have the exact bit set. If unset, the exact bit
137 * on the SSA value is ignored.
141 /** In a replacement, requests that the instruction be marked exact. */
144 /* Commutative expression index. This is assigned by opt_algebraic.py when
145 * search structures are constructed and is a unique (to this structure)
146 * index within the commutative operation bitfield used for searching for
147 * all combinations of expressions containing commutative operations.
149 int8_t comm_expr_idx
;
151 /* Number of commutative expressions in this expression including this one
152 * (if it is commutative).
156 /* One of nir_op or nir_search_op */
158 const nir_search_value
*srcs
[4];
160 /** Optional condition fxn ptr
162 * This allows additional constraints on expression matching, it is
163 * typically used to match an expressions uses such as the number of times
164 * the expression is used, and whether its used by an if.
166 bool (*cond
)(nir_alu_instr
*instr
);
167 } nir_search_expression
;
169 struct per_op_table
{
170 const uint16_t *filter
;
171 unsigned num_filtered_states
;
172 const uint16_t *table
;
176 const nir_search_expression
*search
;
177 const nir_search_value
*replace
;
178 unsigned condition_offset
;
181 /* Note: these must match the start states created in
182 * TreeAutomaton._build_table()
185 /* WILDCARD_STATE = 0 is set by zeroing the state array */
186 static const uint16_t CONST_STATE
= 1;
188 NIR_DEFINE_CAST(nir_search_value_as_variable
, nir_search_value
,
189 nir_search_variable
, value
,
190 type
, nir_search_value_variable
)
191 NIR_DEFINE_CAST(nir_search_value_as_constant
, nir_search_value
,
192 nir_search_constant
, value
,
193 type
, nir_search_value_constant
)
194 NIR_DEFINE_CAST(nir_search_value_as_expression
, nir_search_value
,
195 nir_search_expression
, value
,
196 type
, nir_search_value_expression
)
199 nir_replace_instr(struct nir_builder
*b
, nir_alu_instr
*instr
,
200 struct hash_table
*range_ht
,
201 const nir_search_expression
*search
,
202 const nir_search_value
*replace
);
204 nir_algebraic_impl(nir_function_impl
*impl
,
205 const bool *condition_flags
,
206 const struct transform
**transforms
,
207 const uint16_t *transform_counts
,
208 const struct per_op_table
*pass_op_table
);
210 #endif /* _NIR_SEARCH_ */