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)
29 #include "nir_search.h"
34 unsigned variables_seen
;
35 nir_alu_src variables
[NIR_SEARCH_MAX_VARIABLES
];
39 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
40 unsigned num_components
, const uint8_t *swizzle
,
41 struct match_state
*state
);
43 static const uint8_t identity_swizzle
[] = { 0, 1, 2, 3 };
45 static bool alu_instr_is_bool(nir_alu_instr
*instr
);
48 src_is_bool(nir_src src
)
52 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
54 return alu_instr_is_bool(nir_instr_as_alu(src
.ssa
->parent_instr
));
58 alu_instr_is_bool(nir_alu_instr
*instr
)
64 return src_is_bool(instr
->src
[0].src
) && src_is_bool(instr
->src
[1].src
);
66 return src_is_bool(instr
->src
[0].src
);
68 return (nir_alu_type_get_base_type(nir_op_infos
[instr
->op
].output_type
)
74 * Check if a source produces a value of the given type.
76 * Used for satisfying 'a@type' constraints.
79 src_is_type(nir_src src
, nir_alu_type type
)
81 assert(type
!= nir_type_invalid
);
86 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
87 nir_alu_instr
*src_alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
88 nir_alu_type output_type
= nir_op_infos
[src_alu
->op
].output_type
;
90 return nir_alu_type_get_base_type(output_type
) == type
||
91 (nir_alu_type_get_base_type(type
) == nir_type_bool
&&
92 alu_instr_is_bool(src_alu
));
100 match_value(const nir_search_value
*value
, nir_alu_instr
*instr
, unsigned src
,
101 unsigned num_components
, const uint8_t *swizzle
,
102 struct match_state
*state
)
104 uint8_t new_swizzle
[4];
106 /* If the source is an explicitly sized source, then we need to reset
107 * both the number of components and the swizzle.
109 if (nir_op_infos
[instr
->op
].input_sizes
[src
] != 0) {
110 num_components
= nir_op_infos
[instr
->op
].input_sizes
[src
];
111 swizzle
= identity_swizzle
;
114 for (unsigned i
= 0; i
< num_components
; ++i
)
115 new_swizzle
[i
] = instr
->src
[src
].swizzle
[swizzle
[i
]];
117 /* If the value has a specific bit size and it doesn't match, bail */
118 if (value
->bit_size
&&
119 nir_src_bit_size(instr
->src
[src
].src
) != value
->bit_size
)
122 switch (value
->type
) {
123 case nir_search_value_expression
:
124 if (!instr
->src
[src
].src
.is_ssa
)
127 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
130 return match_expression(nir_search_value_as_expression(value
),
131 nir_instr_as_alu(instr
->src
[src
].src
.ssa
->parent_instr
),
132 num_components
, new_swizzle
, state
);
134 case nir_search_value_variable
: {
135 nir_search_variable
*var
= nir_search_value_as_variable(value
);
136 assert(var
->variable
< NIR_SEARCH_MAX_VARIABLES
);
138 if (state
->variables_seen
& (1 << var
->variable
)) {
139 if (!nir_srcs_equal(state
->variables
[var
->variable
].src
,
140 instr
->src
[src
].src
))
143 assert(!instr
->src
[src
].abs
&& !instr
->src
[src
].negate
);
145 for (unsigned i
= 0; i
< num_components
; ++i
) {
146 if (state
->variables
[var
->variable
].swizzle
[i
] != new_swizzle
[i
])
152 if (var
->is_constant
&&
153 instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
156 if (var
->cond
&& !var
->cond(instr
, src
, num_components
, new_swizzle
))
159 if (var
->type
!= nir_type_invalid
&&
160 !src_is_type(instr
->src
[src
].src
, var
->type
))
163 state
->variables_seen
|= (1 << var
->variable
);
164 state
->variables
[var
->variable
].src
= instr
->src
[src
].src
;
165 state
->variables
[var
->variable
].abs
= false;
166 state
->variables
[var
->variable
].negate
= false;
168 for (unsigned i
= 0; i
< 4; ++i
) {
169 if (i
< num_components
)
170 state
->variables
[var
->variable
].swizzle
[i
] = new_swizzle
[i
];
172 state
->variables
[var
->variable
].swizzle
[i
] = 0;
179 case nir_search_value_constant
: {
180 nir_search_constant
*const_val
= nir_search_value_as_constant(value
);
182 if (!instr
->src
[src
].src
.is_ssa
)
185 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
188 nir_load_const_instr
*load
=
189 nir_instr_as_load_const(instr
->src
[src
].src
.ssa
->parent_instr
);
191 switch (const_val
->type
) {
193 for (unsigned i
= 0; i
< num_components
; ++i
) {
195 switch (load
->def
.bit_size
) {
197 val
= load
->value
.f32
[new_swizzle
[i
]];
200 val
= load
->value
.f64
[new_swizzle
[i
]];
203 unreachable("unknown bit size");
206 if (val
!= const_val
->data
.d
)
212 for (unsigned i
= 0; i
< num_components
; ++i
) {
214 switch (load
->def
.bit_size
) {
216 val
= load
->value
.i32
[new_swizzle
[i
]];
219 val
= load
->value
.i64
[new_swizzle
[i
]];
222 unreachable("unknown bit size");
225 if (val
!= const_val
->data
.i
)
231 case nir_type_bool32
:
232 for (unsigned i
= 0; i
< num_components
; ++i
) {
234 switch (load
->def
.bit_size
) {
236 val
= load
->value
.u32
[new_swizzle
[i
]];
239 val
= load
->value
.u64
[new_swizzle
[i
]];
242 unreachable("unknown bit size");
245 if (val
!= const_val
->data
.u
)
251 unreachable("Invalid alu source type");
256 unreachable("Invalid search value type");
261 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
262 unsigned num_components
, const uint8_t *swizzle
,
263 struct match_state
*state
)
265 if (instr
->op
!= expr
->opcode
)
268 assert(instr
->dest
.dest
.is_ssa
);
270 if (expr
->value
.bit_size
&&
271 instr
->dest
.dest
.ssa
.bit_size
!= expr
->value
.bit_size
)
274 state
->inexact_match
= expr
->inexact
|| state
->inexact_match
;
275 state
->has_exact_alu
= instr
->exact
|| state
->has_exact_alu
;
276 if (state
->inexact_match
&& state
->has_exact_alu
)
279 assert(!instr
->dest
.saturate
);
280 assert(nir_op_infos
[instr
->op
].num_inputs
> 0);
282 /* If we have an explicitly sized destination, we can only handle the
283 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
284 * expression, we don't have the information right now to propagate that
285 * swizzle through. We can only properly propagate swizzles if the
286 * instruction is vectorized.
288 if (nir_op_infos
[instr
->op
].output_size
!= 0) {
289 for (unsigned i
= 0; i
< num_components
; i
++) {
295 /* Stash off the current variables_seen bitmask. This way we can
296 * restore it prior to matching in the commutative case below.
298 unsigned variables_seen_stash
= state
->variables_seen
;
301 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
302 if (!match_value(expr
->srcs
[i
], instr
, i
, num_components
,
312 if (nir_op_infos
[instr
->op
].algebraic_properties
& NIR_OP_IS_COMMUTATIVE
) {
313 assert(nir_op_infos
[instr
->op
].num_inputs
== 2);
315 /* Restore the variables_seen bitmask. If we don't do this, then we
316 * could end up with an erroneous failure due to variables found in the
317 * first match attempt above not matching those in the second.
319 state
->variables_seen
= variables_seen_stash
;
321 if (!match_value(expr
->srcs
[0], instr
, 1, num_components
,
325 return match_value(expr
->srcs
[1], instr
, 0, num_components
,
332 typedef struct bitsize_tree
{
334 struct bitsize_tree
*srcs
[4];
336 unsigned common_size
;
337 bool is_src_sized
[4];
341 unsigned src_size
[4];
344 static bitsize_tree
*
345 build_bitsize_tree(void *mem_ctx
, struct match_state
*state
,
346 const nir_search_value
*value
)
348 bitsize_tree
*tree
= ralloc(mem_ctx
, bitsize_tree
);
350 switch (value
->type
) {
351 case nir_search_value_expression
: {
352 nir_search_expression
*expr
= nir_search_value_as_expression(value
);
353 nir_op_info info
= nir_op_infos
[expr
->opcode
];
354 tree
->num_srcs
= info
.num_inputs
;
355 tree
->common_size
= 0;
356 for (unsigned i
= 0; i
< info
.num_inputs
; i
++) {
357 tree
->is_src_sized
[i
] = !!nir_alu_type_get_type_size(info
.input_types
[i
]);
358 if (tree
->is_src_sized
[i
])
359 tree
->src_size
[i
] = nir_alu_type_get_type_size(info
.input_types
[i
]);
360 tree
->srcs
[i
] = build_bitsize_tree(mem_ctx
, state
, expr
->srcs
[i
]);
362 tree
->is_dest_sized
= !!nir_alu_type_get_type_size(info
.output_type
);
363 if (tree
->is_dest_sized
)
364 tree
->dest_size
= nir_alu_type_get_type_size(info
.output_type
);
368 case nir_search_value_variable
: {
369 nir_search_variable
*var
= nir_search_value_as_variable(value
);
371 tree
->is_dest_sized
= true;
372 tree
->dest_size
= nir_src_bit_size(state
->variables
[var
->variable
].src
);
376 case nir_search_value_constant
: {
378 tree
->is_dest_sized
= false;
379 tree
->common_size
= 0;
384 if (value
->bit_size
) {
385 assert(!tree
->is_dest_sized
|| tree
->dest_size
== value
->bit_size
);
386 tree
->common_size
= value
->bit_size
;
393 bitsize_tree_filter_up(bitsize_tree
*tree
)
395 for (unsigned i
= 0; i
< tree
->num_srcs
; i
++) {
396 unsigned src_size
= bitsize_tree_filter_up(tree
->srcs
[i
]);
400 if (tree
->is_src_sized
[i
]) {
401 assert(src_size
== tree
->src_size
[i
]);
402 } else if (tree
->common_size
!= 0) {
403 assert(src_size
== tree
->common_size
);
404 tree
->src_size
[i
] = src_size
;
406 tree
->common_size
= src_size
;
407 tree
->src_size
[i
] = src_size
;
411 if (tree
->num_srcs
&& tree
->common_size
) {
412 if (tree
->dest_size
== 0)
413 tree
->dest_size
= tree
->common_size
;
414 else if (!tree
->is_dest_sized
)
415 assert(tree
->dest_size
== tree
->common_size
);
417 for (unsigned i
= 0; i
< tree
->num_srcs
; i
++) {
418 if (!tree
->src_size
[i
])
419 tree
->src_size
[i
] = tree
->common_size
;
423 return tree
->dest_size
;
427 bitsize_tree_filter_down(bitsize_tree
*tree
, unsigned size
)
430 assert(tree
->dest_size
== size
);
432 tree
->dest_size
= size
;
434 if (!tree
->is_dest_sized
) {
435 if (tree
->common_size
)
436 assert(tree
->common_size
== size
);
438 tree
->common_size
= size
;
441 for (unsigned i
= 0; i
< tree
->num_srcs
; i
++) {
442 if (!tree
->src_size
[i
]) {
443 assert(tree
->common_size
);
444 tree
->src_size
[i
] = tree
->common_size
;
446 bitsize_tree_filter_down(tree
->srcs
[i
], tree
->src_size
[i
]);
451 construct_value(const nir_search_value
*value
,
452 unsigned num_components
, bitsize_tree
*bitsize
,
453 struct match_state
*state
,
454 nir_instr
*instr
, void *mem_ctx
)
456 switch (value
->type
) {
457 case nir_search_value_expression
: {
458 const nir_search_expression
*expr
= nir_search_value_as_expression(value
);
460 if (nir_op_infos
[expr
->opcode
].output_size
!= 0)
461 num_components
= nir_op_infos
[expr
->opcode
].output_size
;
463 nir_alu_instr
*alu
= nir_alu_instr_create(mem_ctx
, expr
->opcode
);
464 nir_ssa_dest_init(&alu
->instr
, &alu
->dest
.dest
, num_components
,
465 bitsize
->dest_size
, NULL
);
466 alu
->dest
.write_mask
= (1 << num_components
) - 1;
467 alu
->dest
.saturate
= false;
469 /* We have no way of knowing what values in a given search expression
470 * map to a particular replacement value. Therefore, if the
471 * expression we are replacing has any exact values, the entire
472 * replacement should be exact.
474 alu
->exact
= state
->has_exact_alu
;
476 for (unsigned i
= 0; i
< nir_op_infos
[expr
->opcode
].num_inputs
; i
++) {
477 /* If the source is an explicitly sized source, then we need to reset
478 * the number of components to match.
480 if (nir_op_infos
[alu
->op
].input_sizes
[i
] != 0)
481 num_components
= nir_op_infos
[alu
->op
].input_sizes
[i
];
483 alu
->src
[i
] = construct_value(expr
->srcs
[i
],
484 num_components
, bitsize
->srcs
[i
],
485 state
, instr
, mem_ctx
);
488 nir_instr_insert_before(instr
, &alu
->instr
);
491 val
.src
= nir_src_for_ssa(&alu
->dest
.dest
.ssa
);
494 memcpy(val
.swizzle
, identity_swizzle
, sizeof val
.swizzle
);
499 case nir_search_value_variable
: {
500 const nir_search_variable
*var
= nir_search_value_as_variable(value
);
501 assert(state
->variables_seen
& (1 << var
->variable
));
503 nir_alu_src val
= { NIR_SRC_INIT
};
504 nir_alu_src_copy(&val
, &state
->variables
[var
->variable
], mem_ctx
);
506 assert(!var
->is_constant
);
511 case nir_search_value_constant
: {
512 const nir_search_constant
*c
= nir_search_value_as_constant(value
);
513 nir_load_const_instr
*load
=
514 nir_load_const_instr_create(mem_ctx
, 1, bitsize
->dest_size
);
518 load
->def
.name
= ralloc_asprintf(load
, "%f", c
->data
.d
);
519 switch (bitsize
->dest_size
) {
521 load
->value
.f32
[0] = c
->data
.d
;
524 load
->value
.f64
[0] = c
->data
.d
;
527 unreachable("unknown bit size");
532 load
->def
.name
= ralloc_asprintf(load
, "%" PRIi64
, c
->data
.i
);
533 switch (bitsize
->dest_size
) {
535 load
->value
.i32
[0] = c
->data
.i
;
538 load
->value
.i64
[0] = c
->data
.i
;
541 unreachable("unknown bit size");
546 load
->def
.name
= ralloc_asprintf(load
, "%" PRIu64
, c
->data
.u
);
547 switch (bitsize
->dest_size
) {
549 load
->value
.u32
[0] = c
->data
.u
;
552 load
->value
.u64
[0] = c
->data
.u
;
555 unreachable("unknown bit size");
559 case nir_type_bool32
:
560 load
->value
.u32
[0] = c
->data
.u
;
563 unreachable("Invalid alu source type");
566 nir_instr_insert_before(instr
, &load
->instr
);
569 val
.src
= nir_src_for_ssa(&load
->def
);
572 memset(val
.swizzle
, 0, sizeof val
.swizzle
);
578 unreachable("Invalid search value type");
583 nir_replace_instr(nir_alu_instr
*instr
, const nir_search_expression
*search
,
584 const nir_search_value
*replace
, void *mem_ctx
)
586 uint8_t swizzle
[4] = { 0, 0, 0, 0 };
588 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
)
591 assert(instr
->dest
.dest
.is_ssa
);
593 struct match_state state
;
594 state
.inexact_match
= false;
595 state
.has_exact_alu
= false;
596 state
.variables_seen
= 0;
598 if (!match_expression(search
, instr
, instr
->dest
.dest
.ssa
.num_components
,
602 void *bitsize_ctx
= ralloc_context(NULL
);
603 bitsize_tree
*tree
= build_bitsize_tree(bitsize_ctx
, &state
, replace
);
604 bitsize_tree_filter_up(tree
);
605 bitsize_tree_filter_down(tree
, instr
->dest
.dest
.ssa
.bit_size
);
607 /* Inserting a mov may be unnecessary. However, it's much easier to
608 * simply let copy propagation clean this up than to try to go through
609 * and rewrite swizzles ourselves.
611 nir_alu_instr
*mov
= nir_alu_instr_create(mem_ctx
, nir_op_imov
);
612 mov
->dest
.write_mask
= instr
->dest
.write_mask
;
613 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
,
614 instr
->dest
.dest
.ssa
.num_components
,
615 instr
->dest
.dest
.ssa
.bit_size
, NULL
);
617 mov
->src
[0] = construct_value(replace
,
618 instr
->dest
.dest
.ssa
.num_components
, tree
,
619 &state
, &instr
->instr
, mem_ctx
);
620 nir_instr_insert_before(&instr
->instr
, &mov
->instr
);
622 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
,
623 nir_src_for_ssa(&mov
->dest
.dest
.ssa
));
625 /* We know this one has no more uses because we just rewrote them all,
626 * so we can remove it. The rest of the matched expression, however, we
627 * don't know so much about. We'll just let dead code clean them up.
629 nir_instr_remove(&instr
->instr
);
631 ralloc_free(bitsize_ctx
);