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 is_commutative_binop(nir_op op
)
43 case nir_op_imul_high
:
44 case nir_op_umul_high
:
68 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
69 unsigned num_components
, const uint8_t *swizzle
,
70 struct match_state
*state
);
72 static const uint8_t identity_swizzle
[] = { 0, 1, 2, 3 };
75 match_value(const nir_search_value
*value
, nir_alu_instr
*instr
, unsigned src
,
76 unsigned num_components
, const uint8_t *swizzle
,
77 struct match_state
*state
)
79 uint8_t new_swizzle
[4];
81 for (int i
= 0; i
< num_components
; ++i
)
82 new_swizzle
[i
] = instr
->src
[src
].swizzle
[swizzle
[i
]];
84 switch (value
->type
) {
85 case nir_search_value_expression
:
86 if (!instr
->src
[src
].src
.is_ssa
)
89 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
92 return match_expression(nir_search_value_as_expression(value
),
93 nir_instr_as_alu(instr
->src
[src
].src
.ssa
->parent_instr
),
94 num_components
, new_swizzle
, state
);
96 case nir_search_value_variable
: {
97 nir_search_variable
*var
= nir_search_value_as_variable(value
);
99 if (state
->variables_seen
& (1 << var
->variable
)) {
100 if (!nir_srcs_equal(state
->variables
[var
->variable
].src
,
101 instr
->src
[src
].src
))
104 assert(!instr
->src
[src
].abs
&& !instr
->src
[src
].negate
);
106 for (int i
= 0; i
< num_components
; ++i
) {
107 if (state
->variables
[var
->variable
].swizzle
[i
] != new_swizzle
[i
])
113 state
->variables_seen
|= (1 << var
->variable
);
114 state
->variables
[var
->variable
].src
= instr
->src
[src
].src
;
115 state
->variables
[var
->variable
].abs
= false;
116 state
->variables
[var
->variable
].negate
= false;
118 for (int i
= 0; i
< 4; ++i
) {
119 if (i
< num_components
)
120 state
->variables
[var
->variable
].swizzle
[i
] = new_swizzle
[i
];
122 state
->variables
[var
->variable
].swizzle
[i
] = 0;
129 case nir_search_value_constant
: {
130 nir_search_constant
*const_val
= nir_search_value_as_constant(value
);
132 if (!instr
->src
[src
].src
.is_ssa
)
135 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
138 nir_load_const_instr
*load
=
139 nir_instr_as_load_const(instr
->src
[src
].src
.ssa
->parent_instr
);
141 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
143 for (unsigned i
= 0; i
< num_components
; ++i
) {
144 if (load
->value
.f
[new_swizzle
[i
]] != const_val
->data
.f
)
149 case nir_type_unsigned
:
151 for (unsigned i
= 0; i
< num_components
; ++i
) {
152 if (load
->value
.i
[new_swizzle
[i
]] != const_val
->data
.i
)
157 unreachable("Invalid alu source type");
162 unreachable("Invalid search value type");
167 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
168 unsigned num_components
, const uint8_t *swizzle
,
169 struct match_state
*state
)
171 if (instr
->op
!= expr
->opcode
)
174 assert(!instr
->dest
.saturate
);
175 assert(nir_op_infos
[instr
->op
].num_inputs
> 0);
177 /* If we have an explicitly sized destination, we can only handle the
178 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
179 * expression, we don't have the information right now to propagate that
180 * swizzle through. We can only properly propagate swizzles if the
181 * instruction is vectorized.
183 if (nir_op_infos
[instr
->op
].output_size
!= 0) {
184 for (unsigned i
= 0; i
< num_components
; i
++) {
191 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
192 /* If the source is an explicitly sized source, then we need to reset
193 * both the number of components and the swizzle.
195 if (nir_op_infos
[instr
->op
].input_sizes
[i
] != 0) {
196 num_components
= nir_op_infos
[instr
->op
].input_sizes
[i
];
197 swizzle
= identity_swizzle
;
200 if (!match_value(expr
->srcs
[i
], instr
, i
, num_components
,
210 if (is_commutative_binop(instr
->op
)) {
211 if (!match_value(expr
->srcs
[0], instr
, 1, num_components
,
215 return match_value(expr
->srcs
[1], instr
, 0, num_components
,
223 construct_value(const nir_search_value
*value
, nir_alu_type type
,
224 unsigned num_components
, struct match_state
*state
,
225 nir_instr
*instr
, void *mem_ctx
)
227 switch (value
->type
) {
228 case nir_search_value_expression
: {
229 const nir_search_expression
*expr
= nir_search_value_as_expression(value
);
231 if (nir_op_infos
[expr
->opcode
].output_size
!= 0)
232 num_components
= nir_op_infos
[expr
->opcode
].output_size
;
234 nir_alu_instr
*alu
= nir_alu_instr_create(mem_ctx
, expr
->opcode
);
235 alu
->dest
.dest
.is_ssa
= true;
236 nir_ssa_def_init(&alu
->instr
, &alu
->dest
.dest
.ssa
, num_components
, NULL
);
237 alu
->dest
.write_mask
= (1 << num_components
) - 1;
238 alu
->dest
.saturate
= false;
240 for (unsigned i
= 0; i
< nir_op_infos
[expr
->opcode
].num_inputs
; i
++) {
241 /* If the source is an explicitly sized source, then we need to reset
242 * the number of components to match.
244 if (nir_op_infos
[alu
->op
].input_sizes
[i
] != 0)
245 num_components
= nir_op_infos
[alu
->op
].input_sizes
[i
];
247 alu
->src
[i
] = construct_value(expr
->srcs
[i
],
248 nir_op_infos
[alu
->op
].input_types
[i
],
250 state
, instr
, mem_ctx
);
253 nir_instr_insert_before(instr
, &alu
->instr
);
257 .src
.ssa
= &alu
->dest
.dest
.ssa
,
260 .swizzle
= { 0, 1, 2, 3 }
266 case nir_search_value_variable
: {
267 const nir_search_variable
*var
= nir_search_value_as_variable(value
);
268 assert(state
->variables_seen
& (1 << var
->variable
));
270 nir_alu_src val
= state
->variables
[var
->variable
];
271 val
.src
= nir_src_copy(val
.src
, mem_ctx
);
276 case nir_search_value_constant
: {
277 const nir_search_constant
*c
= nir_search_value_as_constant(value
);
278 nir_load_const_instr
*load
= nir_load_const_instr_create(mem_ctx
, 1);
282 load
->def
.name
= ralloc_asprintf(mem_ctx
, "%f", c
->data
.f
);
283 load
->value
.f
[0] = c
->data
.f
;
286 load
->def
.name
= ralloc_asprintf(mem_ctx
, "%d", c
->data
.i
);
287 load
->value
.i
[0] = c
->data
.i
;
289 case nir_type_unsigned
:
291 load
->value
.u
[0] = c
->data
.u
;
294 unreachable("Invalid alu source type");
297 nir_instr_insert_before(instr
, &load
->instr
);
301 .src
.ssa
= &load
->def
,
304 .swizzle
= { 0, 0, 0, 0 } /* Splatted scalar */
311 unreachable("Invalid search value type");
316 nir_replace_instr(nir_alu_instr
*instr
, const nir_search_expression
*search
,
317 const nir_search_value
*replace
, void *mem_ctx
)
319 uint8_t swizzle
[4] = { 0, 0, 0, 0 };
321 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
)
324 assert(instr
->dest
.dest
.is_ssa
);
326 struct match_state state
;
327 state
.variables_seen
= 0;
329 if (!match_expression(search
, instr
, instr
->dest
.dest
.ssa
.num_components
,
333 /* Inserting a mov may be unnecessary. However, it's much easier to
334 * simply let copy propagation clean this up than to try to go through
335 * and rewrite swizzles ourselves.
337 nir_alu_instr
*mov
= nir_alu_instr_create(mem_ctx
, nir_op_imov
);
338 mov
->dest
.write_mask
= instr
->dest
.write_mask
;
339 mov
->dest
.dest
.is_ssa
= true;
340 nir_ssa_def_init(&mov
->instr
, &mov
->dest
.dest
.ssa
,
341 instr
->dest
.dest
.ssa
.num_components
, NULL
);
343 mov
->src
[0] = construct_value(replace
, nir_op_infos
[instr
->op
].output_type
,
344 instr
->dest
.dest
.ssa
.num_components
, &state
,
345 &instr
->instr
, mem_ctx
);
346 nir_instr_insert_before(&instr
->instr
, &mov
->instr
);
348 nir_src replace_src
= {
350 .ssa
= &mov
->dest
.dest
.ssa
,
353 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, replace_src
, mem_ctx
);
355 /* We know this one has no more uses because we just rewrote them all,
356 * so we can remove it. The rest of the matched expression, however, we
357 * don't know so much about. We'll just let dead code clean them up.
359 nir_instr_remove(&instr
->instr
);