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 };
42 static bool alu_instr_is_bool(nir_alu_instr
*instr
);
45 src_is_bool(nir_src src
)
49 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
51 return alu_instr_is_bool((nir_alu_instr
*)src
.ssa
->parent_instr
);
55 alu_instr_is_bool(nir_alu_instr
*instr
)
61 return src_is_bool(instr
->src
[0].src
) && src_is_bool(instr
->src
[1].src
);
63 return src_is_bool(instr
->src
[0].src
);
65 return nir_op_infos
[instr
->op
].output_type
== nir_type_bool
;
70 match_value(const nir_search_value
*value
, nir_alu_instr
*instr
, unsigned src
,
71 unsigned num_components
, const uint8_t *swizzle
,
72 struct match_state
*state
)
74 uint8_t new_swizzle
[4];
76 /* If the source is an explicitly sized source, then we need to reset
77 * both the number of components and the swizzle.
79 if (nir_op_infos
[instr
->op
].input_sizes
[src
] != 0) {
80 num_components
= nir_op_infos
[instr
->op
].input_sizes
[src
];
81 swizzle
= identity_swizzle
;
84 for (int i
= 0; i
< num_components
; ++i
)
85 new_swizzle
[i
] = instr
->src
[src
].swizzle
[swizzle
[i
]];
87 switch (value
->type
) {
88 case nir_search_value_expression
:
89 if (!instr
->src
[src
].src
.is_ssa
)
92 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
95 return match_expression(nir_search_value_as_expression(value
),
96 nir_instr_as_alu(instr
->src
[src
].src
.ssa
->parent_instr
),
97 num_components
, new_swizzle
, state
);
99 case nir_search_value_variable
: {
100 nir_search_variable
*var
= nir_search_value_as_variable(value
);
102 if (state
->variables_seen
& (1 << var
->variable
)) {
103 if (!nir_srcs_equal(state
->variables
[var
->variable
].src
,
104 instr
->src
[src
].src
))
107 assert(!instr
->src
[src
].abs
&& !instr
->src
[src
].negate
);
109 for (int i
= 0; i
< num_components
; ++i
) {
110 if (state
->variables
[var
->variable
].swizzle
[i
] != new_swizzle
[i
])
116 if (var
->is_constant
&&
117 instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
120 if (var
->type
!= nir_type_invalid
) {
121 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
124 nir_alu_instr
*src_alu
=
125 nir_instr_as_alu(instr
->src
[src
].src
.ssa
->parent_instr
);
127 if (nir_op_infos
[src_alu
->op
].output_type
!= var
->type
&&
128 !(var
->type
== nir_type_bool
&& alu_instr_is_bool(src_alu
)))
132 state
->variables_seen
|= (1 << var
->variable
);
133 state
->variables
[var
->variable
].src
= instr
->src
[src
].src
;
134 state
->variables
[var
->variable
].abs
= false;
135 state
->variables
[var
->variable
].negate
= false;
137 for (int i
= 0; i
< 4; ++i
) {
138 if (i
< num_components
)
139 state
->variables
[var
->variable
].swizzle
[i
] = new_swizzle
[i
];
141 state
->variables
[var
->variable
].swizzle
[i
] = 0;
148 case nir_search_value_constant
: {
149 nir_search_constant
*const_val
= nir_search_value_as_constant(value
);
151 if (!instr
->src
[src
].src
.is_ssa
)
154 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
157 nir_load_const_instr
*load
=
158 nir_instr_as_load_const(instr
->src
[src
].src
.ssa
->parent_instr
);
160 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
162 for (unsigned i
= 0; i
< num_components
; ++i
) {
163 if (load
->value
.f
[new_swizzle
[i
]] != const_val
->data
.f
)
168 case nir_type_unsigned
:
170 for (unsigned i
= 0; i
< num_components
; ++i
) {
171 if (load
->value
.i
[new_swizzle
[i
]] != const_val
->data
.i
)
176 unreachable("Invalid alu source type");
181 unreachable("Invalid search value type");
186 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
187 unsigned num_components
, const uint8_t *swizzle
,
188 struct match_state
*state
)
190 if (instr
->op
!= expr
->opcode
)
193 assert(!instr
->dest
.saturate
);
194 assert(nir_op_infos
[instr
->op
].num_inputs
> 0);
196 /* If we have an explicitly sized destination, we can only handle the
197 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
198 * expression, we don't have the information right now to propagate that
199 * swizzle through. We can only properly propagate swizzles if the
200 * instruction is vectorized.
202 if (nir_op_infos
[instr
->op
].output_size
!= 0) {
203 for (unsigned i
= 0; i
< num_components
; i
++) {
210 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
211 if (!match_value(expr
->srcs
[i
], instr
, i
, num_components
,
221 if (nir_op_infos
[instr
->op
].algebraic_properties
& NIR_OP_IS_COMMUTATIVE
) {
222 assert(nir_op_infos
[instr
->op
].num_inputs
== 2);
223 if (!match_value(expr
->srcs
[0], instr
, 1, num_components
,
227 return match_value(expr
->srcs
[1], instr
, 0, num_components
,
235 construct_value(const nir_search_value
*value
, nir_alu_type type
,
236 unsigned num_components
, struct match_state
*state
,
237 nir_instr
*instr
, void *mem_ctx
)
239 switch (value
->type
) {
240 case nir_search_value_expression
: {
241 const nir_search_expression
*expr
= nir_search_value_as_expression(value
);
243 if (nir_op_infos
[expr
->opcode
].output_size
!= 0)
244 num_components
= nir_op_infos
[expr
->opcode
].output_size
;
246 nir_alu_instr
*alu
= nir_alu_instr_create(mem_ctx
, expr
->opcode
);
247 nir_ssa_dest_init(&alu
->instr
, &alu
->dest
.dest
, num_components
, NULL
);
248 alu
->dest
.write_mask
= (1 << num_components
) - 1;
249 alu
->dest
.saturate
= false;
251 for (unsigned i
= 0; i
< nir_op_infos
[expr
->opcode
].num_inputs
; i
++) {
252 /* If the source is an explicitly sized source, then we need to reset
253 * the number of components to match.
255 if (nir_op_infos
[alu
->op
].input_sizes
[i
] != 0)
256 num_components
= nir_op_infos
[alu
->op
].input_sizes
[i
];
258 alu
->src
[i
] = construct_value(expr
->srcs
[i
],
259 nir_op_infos
[alu
->op
].input_types
[i
],
261 state
, instr
, mem_ctx
);
264 nir_instr_insert_before(instr
, &alu
->instr
);
267 val
.src
= nir_src_for_ssa(&alu
->dest
.dest
.ssa
);
270 memcpy(val
.swizzle
, identity_swizzle
, sizeof val
.swizzle
);
275 case nir_search_value_variable
: {
276 const nir_search_variable
*var
= nir_search_value_as_variable(value
);
277 assert(state
->variables_seen
& (1 << var
->variable
));
279 nir_alu_src val
= { NIR_SRC_INIT
};
280 nir_alu_src_copy(&val
, &state
->variables
[var
->variable
], mem_ctx
);
282 assert(!var
->is_constant
);
287 case nir_search_value_constant
: {
288 const nir_search_constant
*c
= nir_search_value_as_constant(value
);
289 nir_load_const_instr
*load
= nir_load_const_instr_create(mem_ctx
, 1);
293 load
->def
.name
= ralloc_asprintf(mem_ctx
, "%f", c
->data
.f
);
294 load
->value
.f
[0] = c
->data
.f
;
297 load
->def
.name
= ralloc_asprintf(mem_ctx
, "%d", c
->data
.i
);
298 load
->value
.i
[0] = c
->data
.i
;
300 case nir_type_unsigned
:
302 load
->value
.u
[0] = c
->data
.u
;
305 unreachable("Invalid alu source type");
308 nir_instr_insert_before(instr
, &load
->instr
);
311 val
.src
= nir_src_for_ssa(&load
->def
);
314 memset(val
.swizzle
, 0, sizeof val
.swizzle
);
320 unreachable("Invalid search value type");
325 nir_replace_instr(nir_alu_instr
*instr
, const nir_search_expression
*search
,
326 const nir_search_value
*replace
, void *mem_ctx
)
328 uint8_t swizzle
[4] = { 0, 0, 0, 0 };
330 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
)
333 assert(instr
->dest
.dest
.is_ssa
);
335 struct match_state state
;
336 state
.variables_seen
= 0;
338 if (!match_expression(search
, instr
, instr
->dest
.dest
.ssa
.num_components
,
342 /* Inserting a mov may be unnecessary. However, it's much easier to
343 * simply let copy propagation clean this up than to try to go through
344 * and rewrite swizzles ourselves.
346 nir_alu_instr
*mov
= nir_alu_instr_create(mem_ctx
, nir_op_imov
);
347 mov
->dest
.write_mask
= instr
->dest
.write_mask
;
348 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
,
349 instr
->dest
.dest
.ssa
.num_components
, NULL
);
351 mov
->src
[0] = construct_value(replace
, nir_op_infos
[instr
->op
].output_type
,
352 instr
->dest
.dest
.ssa
.num_components
, &state
,
353 &instr
->instr
, mem_ctx
);
354 nir_instr_insert_before(&instr
->instr
, &mov
->instr
);
356 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
,
357 nir_src_for_ssa(&mov
->dest
.dest
.ssa
), mem_ctx
);
359 /* We know this one has no more uses because we just rewrote them all,
360 * so we can remove it. The rest of the matched expression, however, we
361 * don't know so much about. We'll just let dead code clean them up.
363 nir_instr_remove(&instr
->instr
);