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)
28 #include "nir_search.h"
31 unsigned variables_seen
;
32 nir_alu_src variables
[NIR_SEARCH_MAX_VARIABLES
];
36 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
37 unsigned num_components
, const uint8_t *swizzle
,
38 struct match_state
*state
);
40 static const uint8_t identity_swizzle
[] = { 0, 1, 2, 3 };
43 match_value(const nir_search_value
*value
, nir_alu_instr
*instr
, unsigned src
,
44 unsigned num_components
, const uint8_t *swizzle
,
45 struct match_state
*state
)
47 uint8_t new_swizzle
[4];
49 for (int i
= 0; i
< num_components
; ++i
)
50 new_swizzle
[i
] = instr
->src
[src
].swizzle
[swizzle
[i
]];
52 switch (value
->type
) {
53 case nir_search_value_expression
:
54 if (!instr
->src
[src
].src
.is_ssa
)
57 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
60 return match_expression(nir_search_value_as_expression(value
),
61 nir_instr_as_alu(instr
->src
[src
].src
.ssa
->parent_instr
),
62 num_components
, new_swizzle
, state
);
64 case nir_search_value_variable
: {
65 nir_search_variable
*var
= nir_search_value_as_variable(value
);
67 if (state
->variables_seen
& (1 << var
->variable
)) {
68 if (!nir_srcs_equal(state
->variables
[var
->variable
].src
,
72 assert(!instr
->src
[src
].abs
&& !instr
->src
[src
].negate
);
74 for (int i
= 0; i
< num_components
; ++i
) {
75 if (state
->variables
[var
->variable
].swizzle
[i
] != new_swizzle
[i
])
81 state
->variables_seen
|= (1 << var
->variable
);
82 state
->variables
[var
->variable
].src
= instr
->src
[src
].src
;
83 state
->variables
[var
->variable
].abs
= false;
84 state
->variables
[var
->variable
].negate
= false;
86 for (int i
= 0; i
< 4; ++i
) {
87 if (i
< num_components
)
88 state
->variables
[var
->variable
].swizzle
[i
] = new_swizzle
[i
];
90 state
->variables
[var
->variable
].swizzle
[i
] = 0;
97 case nir_search_value_constant
: {
98 nir_search_constant
*const_val
= nir_search_value_as_constant(value
);
100 if (!instr
->src
[src
].src
.is_ssa
)
103 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
106 nir_load_const_instr
*load
=
107 nir_instr_as_load_const(instr
->src
[src
].src
.ssa
->parent_instr
);
109 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
111 for (unsigned i
= 0; i
< num_components
; ++i
) {
112 if (load
->value
.f
[new_swizzle
[i
]] != const_val
->data
.f
)
117 case nir_type_unsigned
:
119 for (unsigned i
= 0; i
< num_components
; ++i
) {
120 if (load
->value
.i
[new_swizzle
[i
]] != const_val
->data
.i
)
125 unreachable("Invalid alu source type");
130 unreachable("Invalid search value type");
135 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
136 unsigned num_components
, const uint8_t *swizzle
,
137 struct match_state
*state
)
139 if (instr
->op
!= expr
->opcode
)
142 assert(!instr
->dest
.saturate
);
143 assert(nir_op_infos
[instr
->op
].num_inputs
> 0);
145 /* If we have an explicitly sized destination, we can only handle the
146 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
147 * expression, we don't have the information right now to propagate that
148 * swizzle through. We can only properly propagate swizzles if the
149 * instruction is vectorized.
151 if (nir_op_infos
[instr
->op
].output_size
!= 0) {
152 for (unsigned i
= 0; i
< num_components
; i
++) {
159 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
160 /* If the source is an explicitly sized source, then we need to reset
161 * both the number of components and the swizzle.
163 if (nir_op_infos
[instr
->op
].input_sizes
[i
] != 0) {
164 num_components
= nir_op_infos
[instr
->op
].input_sizes
[i
];
165 swizzle
= identity_swizzle
;
168 if (!match_value(expr
->srcs
[i
], instr
, i
, num_components
,
178 if (nir_op_infos
[instr
->op
].num_inputs
== 2 &&
179 (nir_op_infos
[instr
->op
].algebraic_properties
& NIR_OP_IS_COMMUTATIVE
)) {
180 if (!match_value(expr
->srcs
[0], instr
, 1, num_components
,
184 return match_value(expr
->srcs
[1], instr
, 0, num_components
,
192 construct_value(const nir_search_value
*value
, nir_alu_type type
,
193 unsigned num_components
, struct match_state
*state
,
194 nir_instr
*instr
, void *mem_ctx
)
196 switch (value
->type
) {
197 case nir_search_value_expression
: {
198 const nir_search_expression
*expr
= nir_search_value_as_expression(value
);
200 if (nir_op_infos
[expr
->opcode
].output_size
!= 0)
201 num_components
= nir_op_infos
[expr
->opcode
].output_size
;
203 nir_alu_instr
*alu
= nir_alu_instr_create(mem_ctx
, expr
->opcode
);
204 alu
->dest
.dest
.is_ssa
= true;
205 nir_ssa_def_init(&alu
->instr
, &alu
->dest
.dest
.ssa
, num_components
, NULL
);
206 alu
->dest
.write_mask
= (1 << num_components
) - 1;
207 alu
->dest
.saturate
= false;
209 for (unsigned i
= 0; i
< nir_op_infos
[expr
->opcode
].num_inputs
; i
++) {
210 /* If the source is an explicitly sized source, then we need to reset
211 * the number of components to match.
213 if (nir_op_infos
[alu
->op
].input_sizes
[i
] != 0)
214 num_components
= nir_op_infos
[alu
->op
].input_sizes
[i
];
216 alu
->src
[i
] = construct_value(expr
->srcs
[i
],
217 nir_op_infos
[alu
->op
].input_types
[i
],
219 state
, instr
, mem_ctx
);
222 nir_instr_insert_before(instr
, &alu
->instr
);
226 .src
.ssa
= &alu
->dest
.dest
.ssa
,
229 .swizzle
= { 0, 1, 2, 3 }
235 case nir_search_value_variable
: {
236 const nir_search_variable
*var
= nir_search_value_as_variable(value
);
237 assert(state
->variables_seen
& (1 << var
->variable
));
239 nir_alu_src val
= state
->variables
[var
->variable
];
240 val
.src
= nir_src_copy(val
.src
, mem_ctx
);
245 case nir_search_value_constant
: {
246 const nir_search_constant
*c
= nir_search_value_as_constant(value
);
247 nir_load_const_instr
*load
= nir_load_const_instr_create(mem_ctx
, 1);
251 load
->def
.name
= ralloc_asprintf(mem_ctx
, "%f", c
->data
.f
);
252 load
->value
.f
[0] = c
->data
.f
;
255 load
->def
.name
= ralloc_asprintf(mem_ctx
, "%d", c
->data
.i
);
256 load
->value
.i
[0] = c
->data
.i
;
258 case nir_type_unsigned
:
260 load
->value
.u
[0] = c
->data
.u
;
263 unreachable("Invalid alu source type");
266 nir_instr_insert_before(instr
, &load
->instr
);
270 .src
.ssa
= &load
->def
,
273 .swizzle
= { 0, 0, 0, 0 } /* Splatted scalar */
280 unreachable("Invalid search value type");
285 nir_replace_instr(nir_alu_instr
*instr
, const nir_search_expression
*search
,
286 const nir_search_value
*replace
, void *mem_ctx
)
288 uint8_t swizzle
[4] = { 0, 0, 0, 0 };
290 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
)
293 assert(instr
->dest
.dest
.is_ssa
);
295 struct match_state state
;
296 state
.variables_seen
= 0;
298 if (!match_expression(search
, instr
, instr
->dest
.dest
.ssa
.num_components
,
302 /* Inserting a mov may be unnecessary. However, it's much easier to
303 * simply let copy propagation clean this up than to try to go through
304 * and rewrite swizzles ourselves.
306 nir_alu_instr
*mov
= nir_alu_instr_create(mem_ctx
, nir_op_imov
);
307 mov
->dest
.write_mask
= instr
->dest
.write_mask
;
308 mov
->dest
.dest
.is_ssa
= true;
309 nir_ssa_def_init(&mov
->instr
, &mov
->dest
.dest
.ssa
,
310 instr
->dest
.dest
.ssa
.num_components
, NULL
);
312 mov
->src
[0] = construct_value(replace
, nir_op_infos
[instr
->op
].output_type
,
313 instr
->dest
.dest
.ssa
.num_components
, &state
,
314 &instr
->instr
, mem_ctx
);
315 nir_instr_insert_before(&instr
->instr
, &mov
->instr
);
317 nir_src replace_src
= {
319 .ssa
= &mov
->dest
.dest
.ssa
,
322 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, replace_src
, mem_ctx
);
324 /* We know this one has no more uses because we just rewrote them all,
325 * so we can remove it. The rest of the matched expression, however, we
326 * don't know so much about. We'll just let dead code clean them up.
328 nir_instr_remove(&instr
->instr
);