9d567f88165e8b717127a9fb816add4136f43b56
[mesa.git] / src / compiler / nir / nir_search.h
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 #include "util/u_dynarray.h"
33
34 #define NIR_SEARCH_MAX_VARIABLES 16
35
36 struct nir_builder;
37
38 typedef enum {
39 nir_search_value_expression,
40 nir_search_value_variable,
41 nir_search_value_constant,
42 } nir_search_value_type;
43
44 typedef struct {
45 nir_search_value_type type;
46
47 /**
48 * Bit size of the value. It is interpreted as follows:
49 *
50 * For a search expression:
51 * - If bit_size > 0, then the value only matches an SSA value with the
52 * given bit size.
53 * - If bit_size <= 0, then the value matches any size SSA value.
54 *
55 * For a replace expression:
56 * - If bit_size > 0, then the value is constructed with the given bit size.
57 * - If bit_size == 0, then the value is constructed with the same bit size
58 * as the search value.
59 * - If bit_size < 0, then the value is constructed with the same bit size
60 * as variable (-bit_size - 1).
61 */
62 int bit_size;
63 } nir_search_value;
64
65 typedef struct {
66 nir_search_value value;
67
68 /** The variable index; Must be less than NIR_SEARCH_MAX_VARIABLES */
69 unsigned variable;
70
71 /** Indicates that the given variable must be a constant
72 *
73 * This is only allowed in search expressions and indicates that the
74 * given variable is only allowed to match constant values.
75 */
76 bool is_constant;
77
78 /** Indicates that the given variable must have a certain type
79 *
80 * This is only allowed in search expressions and indicates that the
81 * given variable is only allowed to match values that come from an ALU
82 * instruction with the given output type. A type of nir_type_void
83 * means it can match any type.
84 *
85 * Note: A variable that is both constant and has a non-void type will
86 * never match anything.
87 */
88 nir_alu_type type;
89
90 /** Optional condition fxn ptr
91 *
92 * This is only allowed in search expressions, and allows additional
93 * constraints to be placed on the match. Typically used for 'is_constant'
94 * variables to require, for example, power-of-two in order for the search
95 * to match.
96 */
97 bool (*cond)(struct hash_table *range_ht, nir_alu_instr *instr, unsigned src,
98 unsigned num_components, const uint8_t *swizzle);
99
100 /** Swizzle (for replace only) */
101 uint8_t swizzle[NIR_MAX_VEC_COMPONENTS];
102 } nir_search_variable;
103
104 typedef struct {
105 nir_search_value value;
106
107 nir_alu_type type;
108
109 union {
110 uint64_t u;
111 int64_t i;
112 double d;
113 } data;
114 } nir_search_constant;
115
116 enum nir_search_op {
117 nir_search_op_i2f = nir_last_opcode + 1,
118 nir_search_op_u2f,
119 nir_search_op_f2f,
120 nir_search_op_f2u,
121 nir_search_op_f2i,
122 nir_search_op_u2u,
123 nir_search_op_i2i,
124 nir_search_op_b2f,
125 nir_search_op_b2i,
126 nir_search_op_i2b,
127 nir_search_op_f2b,
128 nir_num_search_ops,
129 };
130
131 uint16_t nir_search_op_for_nir_op(nir_op op);
132
133 typedef struct {
134 nir_search_value value;
135
136 /* When set on a search expression, the expression will only match an SSA
137 * value that does *not* have the exact bit set. If unset, the exact bit
138 * on the SSA value is ignored.
139 */
140 bool inexact;
141
142 /** In a replacement, requests that the instruction be marked exact. */
143 bool exact;
144
145 /* Commutative expression index. This is assigned by opt_algebraic.py when
146 * search structures are constructed and is a unique (to this structure)
147 * index within the commutative operation bitfield used for searching for
148 * all combinations of expressions containing commutative operations.
149 */
150 int8_t comm_expr_idx;
151
152 /* Number of commutative expressions in this expression including this one
153 * (if it is commutative).
154 */
155 uint8_t comm_exprs;
156
157 /* One of nir_op or nir_search_op */
158 uint16_t opcode;
159 const nir_search_value *srcs[4];
160
161 /** Optional condition fxn ptr
162 *
163 * This allows additional constraints on expression matching, it is
164 * typically used to match an expressions uses such as the number of times
165 * the expression is used, and whether its used by an if.
166 */
167 bool (*cond)(nir_alu_instr *instr);
168 } nir_search_expression;
169
170 struct per_op_table {
171 const uint16_t *filter;
172 unsigned num_filtered_states;
173 const uint16_t *table;
174 };
175
176 struct transform {
177 const nir_search_expression *search;
178 const nir_search_value *replace;
179 unsigned condition_offset;
180 };
181
182 /* Note: these must match the start states created in
183 * TreeAutomaton._build_table()
184 */
185
186 /* WILDCARD_STATE = 0 is set by zeroing the state array */
187 static const uint16_t CONST_STATE = 1;
188
189 NIR_DEFINE_CAST(nir_search_value_as_variable, nir_search_value,
190 nir_search_variable, value,
191 type, nir_search_value_variable)
192 NIR_DEFINE_CAST(nir_search_value_as_constant, nir_search_value,
193 nir_search_constant, value,
194 type, nir_search_value_constant)
195 NIR_DEFINE_CAST(nir_search_value_as_expression, nir_search_value,
196 nir_search_expression, value,
197 type, nir_search_value_expression)
198
199 nir_ssa_def *
200 nir_replace_instr(struct nir_builder *b, nir_alu_instr *instr,
201 struct hash_table *range_ht,
202 struct util_dynarray *states,
203 const struct per_op_table *pass_op_table,
204 const nir_search_expression *search,
205 const nir_search_value *replace);
206 bool
207 nir_algebraic_impl(nir_function_impl *impl,
208 const bool *condition_flags,
209 const struct transform **transforms,
210 const uint16_t *transform_counts,
211 const struct per_op_table *pass_op_table);
212
213 #endif /* _NIR_SEARCH_ */