2 * Copyright © 2016 Red Hat
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 * Rob Clark <robclark@freedesktop.org>
27 #ifndef _NIR_SEARCH_HELPERS_
28 #define _NIR_SEARCH_HELPERS_
31 #include "util/bitscan.h"
35 is_pos_power_of_two(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
36 const uint8_t *swizzle
)
38 /* only constant srcs: */
39 if (!nir_src_is_const(instr
->src
[src
].src
))
42 for (unsigned i
= 0; i
< num_components
; i
++) {
43 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
45 int64_t val
= nir_src_comp_as_int(instr
->src
[src
].src
, swizzle
[i
]);
46 if (val
<= 0 || !util_is_power_of_two_or_zero64(val
))
51 uint64_t val
= nir_src_comp_as_uint(instr
->src
[src
].src
, swizzle
[i
]);
52 if (val
== 0 || !util_is_power_of_two_or_zero64(val
))
65 is_neg_power_of_two(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
66 const uint8_t *swizzle
)
68 /* only constant srcs: */
69 if (!nir_src_is_const(instr
->src
[src
].src
))
72 for (unsigned i
= 0; i
< num_components
; i
++) {
73 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
75 int64_t val
= nir_src_comp_as_int(instr
->src
[src
].src
, swizzle
[i
]);
76 if (val
>= 0 || !util_is_power_of_two_or_zero64(-val
))
89 is_zero_to_one(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
90 const uint8_t *swizzle
)
92 /* only constant srcs: */
93 if (!nir_src_is_const(instr
->src
[src
].src
))
96 for (unsigned i
= 0; i
< num_components
; i
++) {
97 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
98 case nir_type_float
: {
99 double val
= nir_src_comp_as_float(instr
->src
[src
].src
, swizzle
[i
]);
100 if (isnan(val
) || val
< 0.0f
|| val
> 1.0f
)
113 * Exclusive compare with (0, 1).
115 * This differs from \c is_zero_to_one because that function tests 0 <= src <=
116 * 1 while this function tests 0 < src < 1.
119 is_gt_0_and_lt_1(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
120 const uint8_t *swizzle
)
122 /* only constant srcs: */
123 if (!nir_src_is_const(instr
->src
[src
].src
))
126 for (unsigned i
= 0; i
< num_components
; i
++) {
127 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
128 case nir_type_float
: {
129 double val
= nir_src_comp_as_float(instr
->src
[src
].src
, swizzle
[i
]);
130 if (isnan(val
) || val
<= 0.0f
|| val
>= 1.0f
)
143 is_not_const_zero(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
144 const uint8_t *swizzle
)
146 if (nir_src_as_const_value(instr
->src
[src
].src
) == NULL
)
149 for (unsigned i
= 0; i
< num_components
; i
++) {
150 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
152 if (nir_src_comp_as_float(instr
->src
[src
].src
, swizzle
[i
]) == 0.0)
158 if (nir_src_comp_as_uint(instr
->src
[src
].src
, swizzle
[i
]) == 0)
170 is_not_const(nir_alu_instr
*instr
, unsigned src
, UNUSED
unsigned num_components
,
171 UNUSED
const uint8_t *swizzle
)
173 return !nir_src_is_const(instr
->src
[src
].src
);
177 is_not_fmul(nir_alu_instr
*instr
, unsigned src
,
178 UNUSED
unsigned num_components
, UNUSED
const uint8_t *swizzle
)
180 nir_alu_instr
*src_alu
=
181 nir_src_as_alu_instr(instr
->src
[src
].src
);
186 if (src_alu
->op
== nir_op_fneg
)
187 return is_not_fmul(src_alu
, 0, 0, NULL
);
189 return src_alu
->op
!= nir_op_fmul
;
193 is_used_once(nir_alu_instr
*instr
)
195 bool zero_if_use
= list_empty(&instr
->dest
.dest
.ssa
.if_uses
);
196 bool zero_use
= list_empty(&instr
->dest
.dest
.ssa
.uses
);
198 if (zero_if_use
&& zero_use
)
201 if (!zero_if_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.uses
))
204 if (!zero_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.if_uses
))
207 if (!list_is_singular(&instr
->dest
.dest
.ssa
.if_uses
) &&
208 !list_is_singular(&instr
->dest
.dest
.ssa
.uses
))
215 is_used_by_if(nir_alu_instr
*instr
)
217 return !list_empty(&instr
->dest
.dest
.ssa
.if_uses
);
221 is_not_used_by_if(nir_alu_instr
*instr
)
223 return list_empty(&instr
->dest
.dest
.ssa
.if_uses
);
227 is_used_by_non_fsat(nir_alu_instr
*instr
)
229 nir_foreach_use(src
, &instr
->dest
.dest
.ssa
) {
230 const nir_instr
*const user_instr
= src
->parent_instr
;
232 if (user_instr
->type
!= nir_instr_type_alu
)
235 const nir_alu_instr
*const user_alu
= nir_instr_as_alu(user_instr
);
237 assert(instr
!= user_alu
);
238 if (user_alu
->op
!= nir_op_fsat
)
245 #endif /* _NIR_SEARCH_ */