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 };
46 * Check if a source produces a value of the given type.
48 * Used for satisfying 'a@type' constraints.
51 src_is_type(nir_src src
, nir_alu_type type
)
53 assert(type
!= nir_type_invalid
);
58 /* Turn nir_type_bool32 into nir_type_bool...they're the same thing. */
59 if (nir_alu_type_get_base_type(type
) == nir_type_bool
)
62 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
63 nir_alu_instr
*src_alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
64 nir_alu_type output_type
= nir_op_infos
[src_alu
->op
].output_type
;
66 if (type
== nir_type_bool
) {
67 switch (src_alu
->op
) {
71 return src_is_type(src_alu
->src
[0].src
, nir_type_bool
) &&
72 src_is_type(src_alu
->src
[1].src
, nir_type_bool
);
74 return src_is_type(src_alu
->src
[0].src
, nir_type_bool
);
80 return nir_alu_type_get_base_type(output_type
) == type
;
81 } else if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
82 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
84 if (type
== nir_type_bool
) {
85 return intr
->intrinsic
== nir_intrinsic_load_front_face
||
86 intr
->intrinsic
== nir_intrinsic_load_helper_invocation
;
95 match_value(const nir_search_value
*value
, nir_alu_instr
*instr
, unsigned src
,
96 unsigned num_components
, const uint8_t *swizzle
,
97 struct match_state
*state
)
99 uint8_t new_swizzle
[4];
101 /* If the source is an explicitly sized source, then we need to reset
102 * both the number of components and the swizzle.
104 if (nir_op_infos
[instr
->op
].input_sizes
[src
] != 0) {
105 num_components
= nir_op_infos
[instr
->op
].input_sizes
[src
];
106 swizzle
= identity_swizzle
;
109 for (unsigned i
= 0; i
< num_components
; ++i
)
110 new_swizzle
[i
] = instr
->src
[src
].swizzle
[swizzle
[i
]];
112 /* If the value has a specific bit size and it doesn't match, bail */
113 if (value
->bit_size
&&
114 nir_src_bit_size(instr
->src
[src
].src
) != value
->bit_size
)
117 switch (value
->type
) {
118 case nir_search_value_expression
:
119 if (!instr
->src
[src
].src
.is_ssa
)
122 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
125 return match_expression(nir_search_value_as_expression(value
),
126 nir_instr_as_alu(instr
->src
[src
].src
.ssa
->parent_instr
),
127 num_components
, new_swizzle
, state
);
129 case nir_search_value_variable
: {
130 nir_search_variable
*var
= nir_search_value_as_variable(value
);
131 assert(var
->variable
< NIR_SEARCH_MAX_VARIABLES
);
133 if (state
->variables_seen
& (1 << var
->variable
)) {
134 if (!nir_srcs_equal(state
->variables
[var
->variable
].src
,
135 instr
->src
[src
].src
))
138 assert(!instr
->src
[src
].abs
&& !instr
->src
[src
].negate
);
140 for (unsigned i
= 0; i
< num_components
; ++i
) {
141 if (state
->variables
[var
->variable
].swizzle
[i
] != new_swizzle
[i
])
147 if (var
->is_constant
&&
148 instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
151 if (var
->cond
&& !var
->cond(instr
, src
, num_components
, new_swizzle
))
154 if (var
->type
!= nir_type_invalid
&&
155 !src_is_type(instr
->src
[src
].src
, var
->type
))
158 state
->variables_seen
|= (1 << var
->variable
);
159 state
->variables
[var
->variable
].src
= instr
->src
[src
].src
;
160 state
->variables
[var
->variable
].abs
= false;
161 state
->variables
[var
->variable
].negate
= false;
163 for (unsigned i
= 0; i
< 4; ++i
) {
164 if (i
< num_components
)
165 state
->variables
[var
->variable
].swizzle
[i
] = new_swizzle
[i
];
167 state
->variables
[var
->variable
].swizzle
[i
] = 0;
174 case nir_search_value_constant
: {
175 nir_search_constant
*const_val
= nir_search_value_as_constant(value
);
177 if (!instr
->src
[src
].src
.is_ssa
)
180 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
183 nir_load_const_instr
*load
=
184 nir_instr_as_load_const(instr
->src
[src
].src
.ssa
->parent_instr
);
186 switch (const_val
->type
) {
188 for (unsigned i
= 0; i
< num_components
; ++i
) {
190 switch (load
->def
.bit_size
) {
192 val
= load
->value
.f32
[new_swizzle
[i
]];
195 val
= load
->value
.f64
[new_swizzle
[i
]];
198 unreachable("unknown bit size");
201 if (val
!= const_val
->data
.d
)
207 for (unsigned i
= 0; i
< num_components
; ++i
) {
209 switch (load
->def
.bit_size
) {
211 val
= load
->value
.i32
[new_swizzle
[i
]];
214 val
= load
->value
.i64
[new_swizzle
[i
]];
217 unreachable("unknown bit size");
220 if (val
!= const_val
->data
.i
)
226 case nir_type_bool32
:
227 for (unsigned i
= 0; i
< num_components
; ++i
) {
229 switch (load
->def
.bit_size
) {
231 val
= load
->value
.u32
[new_swizzle
[i
]];
234 val
= load
->value
.u64
[new_swizzle
[i
]];
237 unreachable("unknown bit size");
240 if (val
!= const_val
->data
.u
)
246 unreachable("Invalid alu source type");
251 unreachable("Invalid search value type");
256 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
257 unsigned num_components
, const uint8_t *swizzle
,
258 struct match_state
*state
)
260 if (instr
->op
!= expr
->opcode
)
263 assert(instr
->dest
.dest
.is_ssa
);
265 if (expr
->value
.bit_size
&&
266 instr
->dest
.dest
.ssa
.bit_size
!= expr
->value
.bit_size
)
269 state
->inexact_match
= expr
->inexact
|| state
->inexact_match
;
270 state
->has_exact_alu
= instr
->exact
|| state
->has_exact_alu
;
271 if (state
->inexact_match
&& state
->has_exact_alu
)
274 assert(!instr
->dest
.saturate
);
275 assert(nir_op_infos
[instr
->op
].num_inputs
> 0);
277 /* If we have an explicitly sized destination, we can only handle the
278 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
279 * expression, we don't have the information right now to propagate that
280 * swizzle through. We can only properly propagate swizzles if the
281 * instruction is vectorized.
283 if (nir_op_infos
[instr
->op
].output_size
!= 0) {
284 for (unsigned i
= 0; i
< num_components
; i
++) {
290 /* Stash off the current variables_seen bitmask. This way we can
291 * restore it prior to matching in the commutative case below.
293 unsigned variables_seen_stash
= state
->variables_seen
;
296 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
297 if (!match_value(expr
->srcs
[i
], instr
, i
, num_components
,
307 if (nir_op_infos
[instr
->op
].algebraic_properties
& NIR_OP_IS_COMMUTATIVE
) {
308 assert(nir_op_infos
[instr
->op
].num_inputs
== 2);
310 /* Restore the variables_seen bitmask. If we don't do this, then we
311 * could end up with an erroneous failure due to variables found in the
312 * first match attempt above not matching those in the second.
314 state
->variables_seen
= variables_seen_stash
;
316 if (!match_value(expr
->srcs
[0], instr
, 1, num_components
,
320 return match_value(expr
->srcs
[1], instr
, 0, num_components
,
327 typedef struct bitsize_tree
{
329 struct bitsize_tree
*srcs
[4];
331 unsigned common_size
;
332 bool is_src_sized
[4];
336 unsigned src_size
[4];
339 static bitsize_tree
*
340 build_bitsize_tree(void *mem_ctx
, struct match_state
*state
,
341 const nir_search_value
*value
)
343 bitsize_tree
*tree
= ralloc(mem_ctx
, bitsize_tree
);
345 switch (value
->type
) {
346 case nir_search_value_expression
: {
347 nir_search_expression
*expr
= nir_search_value_as_expression(value
);
348 nir_op_info info
= nir_op_infos
[expr
->opcode
];
349 tree
->num_srcs
= info
.num_inputs
;
350 tree
->common_size
= 0;
351 for (unsigned i
= 0; i
< info
.num_inputs
; i
++) {
352 tree
->is_src_sized
[i
] = !!nir_alu_type_get_type_size(info
.input_types
[i
]);
353 if (tree
->is_src_sized
[i
])
354 tree
->src_size
[i
] = nir_alu_type_get_type_size(info
.input_types
[i
]);
355 tree
->srcs
[i
] = build_bitsize_tree(mem_ctx
, state
, expr
->srcs
[i
]);
357 tree
->is_dest_sized
= !!nir_alu_type_get_type_size(info
.output_type
);
358 if (tree
->is_dest_sized
)
359 tree
->dest_size
= nir_alu_type_get_type_size(info
.output_type
);
363 case nir_search_value_variable
: {
364 nir_search_variable
*var
= nir_search_value_as_variable(value
);
366 tree
->is_dest_sized
= true;
367 tree
->dest_size
= nir_src_bit_size(state
->variables
[var
->variable
].src
);
371 case nir_search_value_constant
: {
373 tree
->is_dest_sized
= false;
374 tree
->common_size
= 0;
379 if (value
->bit_size
) {
380 assert(!tree
->is_dest_sized
|| tree
->dest_size
== value
->bit_size
);
381 tree
->common_size
= value
->bit_size
;
388 bitsize_tree_filter_up(bitsize_tree
*tree
)
390 for (unsigned i
= 0; i
< tree
->num_srcs
; i
++) {
391 unsigned src_size
= bitsize_tree_filter_up(tree
->srcs
[i
]);
395 if (tree
->is_src_sized
[i
]) {
396 assert(src_size
== tree
->src_size
[i
]);
397 } else if (tree
->common_size
!= 0) {
398 assert(src_size
== tree
->common_size
);
399 tree
->src_size
[i
] = src_size
;
401 tree
->common_size
= src_size
;
402 tree
->src_size
[i
] = src_size
;
406 if (tree
->num_srcs
&& tree
->common_size
) {
407 if (tree
->dest_size
== 0)
408 tree
->dest_size
= tree
->common_size
;
409 else if (!tree
->is_dest_sized
)
410 assert(tree
->dest_size
== tree
->common_size
);
412 for (unsigned i
= 0; i
< tree
->num_srcs
; i
++) {
413 if (!tree
->src_size
[i
])
414 tree
->src_size
[i
] = tree
->common_size
;
418 return tree
->dest_size
;
422 bitsize_tree_filter_down(bitsize_tree
*tree
, unsigned size
)
425 assert(tree
->dest_size
== size
);
427 tree
->dest_size
= size
;
429 if (!tree
->is_dest_sized
) {
430 if (tree
->common_size
)
431 assert(tree
->common_size
== size
);
433 tree
->common_size
= size
;
436 for (unsigned i
= 0; i
< tree
->num_srcs
; i
++) {
437 if (!tree
->src_size
[i
]) {
438 assert(tree
->common_size
);
439 tree
->src_size
[i
] = tree
->common_size
;
441 bitsize_tree_filter_down(tree
->srcs
[i
], tree
->src_size
[i
]);
446 construct_value(const nir_search_value
*value
,
447 unsigned num_components
, bitsize_tree
*bitsize
,
448 struct match_state
*state
,
449 nir_instr
*instr
, void *mem_ctx
)
451 switch (value
->type
) {
452 case nir_search_value_expression
: {
453 const nir_search_expression
*expr
= nir_search_value_as_expression(value
);
455 if (nir_op_infos
[expr
->opcode
].output_size
!= 0)
456 num_components
= nir_op_infos
[expr
->opcode
].output_size
;
458 nir_alu_instr
*alu
= nir_alu_instr_create(mem_ctx
, expr
->opcode
);
459 nir_ssa_dest_init(&alu
->instr
, &alu
->dest
.dest
, num_components
,
460 bitsize
->dest_size
, NULL
);
461 alu
->dest
.write_mask
= (1 << num_components
) - 1;
462 alu
->dest
.saturate
= false;
464 /* We have no way of knowing what values in a given search expression
465 * map to a particular replacement value. Therefore, if the
466 * expression we are replacing has any exact values, the entire
467 * replacement should be exact.
469 alu
->exact
= state
->has_exact_alu
;
471 for (unsigned i
= 0; i
< nir_op_infos
[expr
->opcode
].num_inputs
; i
++) {
472 /* If the source is an explicitly sized source, then we need to reset
473 * the number of components to match.
475 if (nir_op_infos
[alu
->op
].input_sizes
[i
] != 0)
476 num_components
= nir_op_infos
[alu
->op
].input_sizes
[i
];
478 alu
->src
[i
] = construct_value(expr
->srcs
[i
],
479 num_components
, bitsize
->srcs
[i
],
480 state
, instr
, mem_ctx
);
483 nir_instr_insert_before(instr
, &alu
->instr
);
486 val
.src
= nir_src_for_ssa(&alu
->dest
.dest
.ssa
);
489 memcpy(val
.swizzle
, identity_swizzle
, sizeof val
.swizzle
);
494 case nir_search_value_variable
: {
495 const nir_search_variable
*var
= nir_search_value_as_variable(value
);
496 assert(state
->variables_seen
& (1 << var
->variable
));
498 nir_alu_src val
= { NIR_SRC_INIT
};
499 nir_alu_src_copy(&val
, &state
->variables
[var
->variable
], mem_ctx
);
501 assert(!var
->is_constant
);
506 case nir_search_value_constant
: {
507 const nir_search_constant
*c
= nir_search_value_as_constant(value
);
508 nir_load_const_instr
*load
=
509 nir_load_const_instr_create(mem_ctx
, 1, bitsize
->dest_size
);
513 load
->def
.name
= ralloc_asprintf(load
, "%f", c
->data
.d
);
514 switch (bitsize
->dest_size
) {
516 load
->value
.f32
[0] = c
->data
.d
;
519 load
->value
.f64
[0] = c
->data
.d
;
522 unreachable("unknown bit size");
527 load
->def
.name
= ralloc_asprintf(load
, "%" PRIi64
, c
->data
.i
);
528 switch (bitsize
->dest_size
) {
530 load
->value
.i32
[0] = c
->data
.i
;
533 load
->value
.i64
[0] = c
->data
.i
;
536 unreachable("unknown bit size");
541 load
->def
.name
= ralloc_asprintf(load
, "%" PRIu64
, c
->data
.u
);
542 switch (bitsize
->dest_size
) {
544 load
->value
.u32
[0] = c
->data
.u
;
547 load
->value
.u64
[0] = c
->data
.u
;
550 unreachable("unknown bit size");
554 case nir_type_bool32
:
555 load
->value
.u32
[0] = c
->data
.u
;
558 unreachable("Invalid alu source type");
561 nir_instr_insert_before(instr
, &load
->instr
);
564 val
.src
= nir_src_for_ssa(&load
->def
);
567 memset(val
.swizzle
, 0, sizeof val
.swizzle
);
573 unreachable("Invalid search value type");
578 nir_replace_instr(nir_alu_instr
*instr
, const nir_search_expression
*search
,
579 const nir_search_value
*replace
, void *mem_ctx
)
581 uint8_t swizzle
[4] = { 0, 0, 0, 0 };
583 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
)
586 assert(instr
->dest
.dest
.is_ssa
);
588 struct match_state state
;
589 state
.inexact_match
= false;
590 state
.has_exact_alu
= false;
591 state
.variables_seen
= 0;
593 if (!match_expression(search
, instr
, instr
->dest
.dest
.ssa
.num_components
,
597 void *bitsize_ctx
= ralloc_context(NULL
);
598 bitsize_tree
*tree
= build_bitsize_tree(bitsize_ctx
, &state
, replace
);
599 bitsize_tree_filter_up(tree
);
600 bitsize_tree_filter_down(tree
, instr
->dest
.dest
.ssa
.bit_size
);
602 /* Inserting a mov may be unnecessary. However, it's much easier to
603 * simply let copy propagation clean this up than to try to go through
604 * and rewrite swizzles ourselves.
606 nir_alu_instr
*mov
= nir_alu_instr_create(mem_ctx
, nir_op_imov
);
607 mov
->dest
.write_mask
= instr
->dest
.write_mask
;
608 nir_ssa_dest_init(&mov
->instr
, &mov
->dest
.dest
,
609 instr
->dest
.dest
.ssa
.num_components
,
610 instr
->dest
.dest
.ssa
.bit_size
, NULL
);
612 mov
->src
[0] = construct_value(replace
,
613 instr
->dest
.dest
.ssa
.num_components
, tree
,
614 &state
, &instr
->instr
, mem_ctx
);
615 nir_instr_insert_before(&instr
->instr
, &mov
->instr
);
617 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
,
618 nir_src_for_ssa(&mov
->dest
.dest
.ssa
));
620 /* We know this one has no more uses because we just rewrote them all,
621 * so we can remove it. The rest of the matched expression, however, we
622 * don't know so much about. We'll just let dead code clean them up.
624 nir_instr_remove(&instr
->instr
);
626 ralloc_free(bitsize_ctx
);