nir/search: Use the nir_imm_* helpers from nir_builder
[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
33 #define NIR_SEARCH_MAX_VARIABLES 16
34
35 struct nir_builder;
36
37 typedef enum {
38 nir_search_value_expression,
39 nir_search_value_variable,
40 nir_search_value_constant,
41 } nir_search_value_type;
42
43 typedef struct {
44 nir_search_value_type type;
45
46 unsigned bit_size;
47 } nir_search_value;
48
49 typedef struct {
50 nir_search_value value;
51
52 /** The variable index; Must be less than NIR_SEARCH_MAX_VARIABLES */
53 unsigned variable;
54
55 /** Indicates that the given variable must be a constant
56 *
57 * This is only allowed in search expressions and indicates that the
58 * given variable is only allowed to match constant values.
59 */
60 bool is_constant;
61
62 /** Indicates that the given variable must have a certain type
63 *
64 * This is only allowed in search expressions and indicates that the
65 * given variable is only allowed to match values that come from an ALU
66 * instruction with the given output type. A type of nir_type_void
67 * means it can match any type.
68 *
69 * Note: A variable that is both constant and has a non-void type will
70 * never match anything.
71 */
72 nir_alu_type type;
73
74 /** Optional condition fxn ptr
75 *
76 * This is only allowed in search expressions, and allows additional
77 * constraints to be placed on the match. Typically used for 'is_constant'
78 * variables to require, for example, power-of-two in order for the search
79 * to match.
80 */
81 bool (*cond)(nir_alu_instr *instr, unsigned src,
82 unsigned num_components, const uint8_t *swizzle);
83 } nir_search_variable;
84
85 typedef struct {
86 nir_search_value value;
87
88 nir_alu_type type;
89
90 union {
91 uint64_t u;
92 int64_t i;
93 double d;
94 } data;
95 } nir_search_constant;
96
97 typedef struct {
98 nir_search_value value;
99
100 /* When set on a search expression, the expression will only match an SSA
101 * value that does *not* have the exact bit set. If unset, the exact bit
102 * on the SSA value is ignored.
103 */
104 bool inexact;
105
106 nir_op opcode;
107 const nir_search_value *srcs[4];
108
109 /** Optional condition fxn ptr
110 *
111 * This allows additional constraints on expression matching, it is
112 * typically used to match an expressions uses such as the number of times
113 * the expression is used, and whether its used by an if.
114 */
115 bool (*cond)(nir_alu_instr *instr);
116 } nir_search_expression;
117
118 NIR_DEFINE_CAST(nir_search_value_as_variable, nir_search_value,
119 nir_search_variable, value,
120 type, nir_search_value_variable)
121 NIR_DEFINE_CAST(nir_search_value_as_constant, nir_search_value,
122 nir_search_constant, value,
123 type, nir_search_value_constant)
124 NIR_DEFINE_CAST(nir_search_value_as_expression, nir_search_value,
125 nir_search_expression, value,
126 type, nir_search_value_expression)
127
128 nir_ssa_def *
129 nir_replace_instr(struct nir_builder *b, nir_alu_instr *instr,
130 const nir_search_expression *search,
131 const nir_search_value *replace);
132
133 #endif /* _NIR_SEARCH_ */