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"
32 #include "nir_range_analysis.h"
36 is_pos_power_of_two(UNUSED
struct hash_table
*ht
, nir_alu_instr
*instr
,
37 unsigned src
, unsigned num_components
,
38 const uint8_t *swizzle
)
40 /* only constant srcs: */
41 if (!nir_src_is_const(instr
->src
[src
].src
))
44 for (unsigned i
= 0; i
< num_components
; i
++) {
45 nir_alu_type type
= nir_op_infos
[instr
->op
].input_types
[src
];
46 switch (nir_alu_type_get_base_type(type
)) {
48 int64_t val
= nir_src_comp_as_int(instr
->src
[src
].src
, swizzle
[i
]);
49 if (val
<= 0 || !util_is_power_of_two_or_zero64(val
))
54 uint64_t val
= nir_src_comp_as_uint(instr
->src
[src
].src
, swizzle
[i
]);
55 if (val
== 0 || !util_is_power_of_two_or_zero64(val
))
68 is_neg_power_of_two(UNUSED
struct hash_table
*ht
, nir_alu_instr
*instr
,
69 unsigned src
, unsigned num_components
,
70 const uint8_t *swizzle
)
72 /* only constant srcs: */
73 if (!nir_src_is_const(instr
->src
[src
].src
))
76 for (unsigned i
= 0; i
< num_components
; i
++) {
77 nir_alu_type type
= nir_op_infos
[instr
->op
].input_types
[src
];
78 switch (nir_alu_type_get_base_type(type
)) {
80 int64_t val
= nir_src_comp_as_int(instr
->src
[src
].src
, swizzle
[i
]);
81 if (val
>= 0 || !util_is_power_of_two_or_zero64(-val
))
93 #define MULTIPLE(test) \
95 is_unsigned_multiple_of_ ## test(UNUSED struct hash_table *ht, nir_alu_instr *instr, \
96 unsigned src, unsigned num_components, \
97 const uint8_t *swizzle) \
99 /* only constant srcs: */ \
100 if (!nir_src_is_const(instr->src[src].src)) \
103 for (unsigned i = 0; i < num_components; i++) { \
104 uint64_t val = nir_src_comp_as_uint(instr->src[src].src, swizzle[i]); \
105 if (val % test != 0) \
120 is_zero_to_one(UNUSED
struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
121 unsigned num_components
,
122 const uint8_t *swizzle
)
124 /* only constant srcs: */
125 if (!nir_src_is_const(instr
->src
[src
].src
))
128 for (unsigned i
= 0; i
< num_components
; i
++) {
129 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
130 case nir_type_float
: {
131 double val
= nir_src_comp_as_float(instr
->src
[src
].src
, swizzle
[i
]);
132 if (isnan(val
) || val
< 0.0f
|| val
> 1.0f
)
145 * Exclusive compare with (0, 1).
147 * This differs from \c is_zero_to_one because that function tests 0 <= src <=
148 * 1 while this function tests 0 < src < 1.
151 is_gt_0_and_lt_1(UNUSED
struct hash_table
*ht
, nir_alu_instr
*instr
,
152 unsigned src
, unsigned num_components
,
153 const uint8_t *swizzle
)
155 /* only constant srcs: */
156 if (!nir_src_is_const(instr
->src
[src
].src
))
159 for (unsigned i
= 0; i
< num_components
; i
++) {
160 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
161 case nir_type_float
: {
162 double val
= nir_src_comp_as_float(instr
->src
[src
].src
, swizzle
[i
]);
163 if (isnan(val
) || val
<= 0.0f
|| val
>= 1.0f
)
176 is_not_const_zero(UNUSED
struct hash_table
*ht
, nir_alu_instr
*instr
,
177 unsigned src
, unsigned num_components
,
178 const uint8_t *swizzle
)
180 if (nir_src_as_const_value(instr
->src
[src
].src
) == NULL
)
183 for (unsigned i
= 0; i
< num_components
; i
++) {
184 nir_alu_type type
= nir_op_infos
[instr
->op
].input_types
[src
];
185 switch (nir_alu_type_get_base_type(type
)) {
187 if (nir_src_comp_as_float(instr
->src
[src
].src
, swizzle
[i
]) == 0.0)
193 if (nir_src_comp_as_uint(instr
->src
[src
].src
, swizzle
[i
]) == 0)
205 is_not_const(UNUSED
struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
206 UNUSED
unsigned num_components
,
207 UNUSED
const uint8_t *swizzle
)
209 return !nir_src_is_const(instr
->src
[src
].src
);
213 is_not_fmul(struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
214 UNUSED
unsigned num_components
, UNUSED
const uint8_t *swizzle
)
216 nir_alu_instr
*src_alu
=
217 nir_src_as_alu_instr(instr
->src
[src
].src
);
222 if (src_alu
->op
== nir_op_fneg
)
223 return is_not_fmul(ht
, src_alu
, 0, 0, NULL
);
225 return src_alu
->op
!= nir_op_fmul
;
229 is_fsign(nir_alu_instr
*instr
, unsigned src
,
230 UNUSED
unsigned num_components
, UNUSED
const uint8_t *swizzle
)
232 nir_alu_instr
*src_alu
=
233 nir_src_as_alu_instr(instr
->src
[src
].src
);
238 if (src_alu
->op
== nir_op_fneg
)
239 src_alu
= nir_src_as_alu_instr(src_alu
->src
[0].src
);
241 return src_alu
!= NULL
&& src_alu
->op
== nir_op_fsign
;
245 is_not_const_and_not_fsign(struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
246 unsigned num_components
, const uint8_t *swizzle
)
248 return is_not_const(ht
, instr
, src
, num_components
, swizzle
) &&
249 !is_fsign(instr
, src
, num_components
, swizzle
);
253 is_used_once(nir_alu_instr
*instr
)
255 bool zero_if_use
= list_is_empty(&instr
->dest
.dest
.ssa
.if_uses
);
256 bool zero_use
= list_is_empty(&instr
->dest
.dest
.ssa
.uses
);
258 if (zero_if_use
&& zero_use
)
261 if (!zero_if_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.uses
))
264 if (!zero_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.if_uses
))
267 if (!list_is_singular(&instr
->dest
.dest
.ssa
.if_uses
) &&
268 !list_is_singular(&instr
->dest
.dest
.ssa
.uses
))
275 is_used_by_if(nir_alu_instr
*instr
)
277 return !list_is_empty(&instr
->dest
.dest
.ssa
.if_uses
);
281 is_not_used_by_if(nir_alu_instr
*instr
)
283 return list_is_empty(&instr
->dest
.dest
.ssa
.if_uses
);
287 is_used_by_non_fsat(nir_alu_instr
*instr
)
289 nir_foreach_use(src
, &instr
->dest
.dest
.ssa
) {
290 const nir_instr
*const user_instr
= src
->parent_instr
;
292 if (user_instr
->type
!= nir_instr_type_alu
)
295 const nir_alu_instr
*const user_alu
= nir_instr_as_alu(user_instr
);
297 assert(instr
!= user_alu
);
298 if (user_alu
->op
!= nir_op_fsat
)
306 * Returns true if a NIR ALU src represents a constant integer
307 * of either 32 or 64 bits, and the higher word (bit-size / 2)
308 * of all its components is zero.
311 is_upper_half_zero(UNUSED
struct hash_table
*ht
,
312 nir_alu_instr
*instr
, unsigned src
,
313 unsigned num_components
, const uint8_t *swizzle
)
315 if (nir_src_as_const_value(instr
->src
[src
].src
) == NULL
)
318 for (unsigned i
= 0; i
< num_components
; i
++) {
319 unsigned half_bit_size
= nir_src_bit_size(instr
->src
[src
].src
) / 2;
320 uint32_t high_bits
= ((1 << half_bit_size
) - 1) << half_bit_size
;
321 if ((nir_src_comp_as_uint(instr
->src
[src
].src
,
322 swizzle
[i
]) & high_bits
) != 0) {
331 * Returns true if a NIR ALU src represents a constant integer
332 * of either 32 or 64 bits, and the lower word (bit-size / 2)
333 * of all its components is zero.
336 is_lower_half_zero(UNUSED
struct hash_table
*ht
,
337 nir_alu_instr
*instr
, unsigned src
,
338 unsigned num_components
, const uint8_t *swizzle
)
340 if (nir_src_as_const_value(instr
->src
[src
].src
) == NULL
)
343 for (unsigned i
= 0; i
< num_components
; i
++) {
345 (1 << (nir_src_bit_size(instr
->src
[src
].src
) / 2)) - 1;
346 if ((nir_src_comp_as_int(instr
->src
[src
].src
, swizzle
[i
]) & low_bits
) != 0)
354 no_signed_wrap(nir_alu_instr
*instr
)
356 return instr
->no_signed_wrap
;
360 no_unsigned_wrap(nir_alu_instr
*instr
)
362 return instr
->no_unsigned_wrap
;
366 is_integral(struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
367 UNUSED
unsigned num_components
, UNUSED
const uint8_t *swizzle
)
369 const struct ssa_result_range r
= nir_analyze_range(ht
, instr
, src
);
371 return r
.is_integral
;
374 #define RELATION(r) \
376 is_ ## r (struct hash_table *ht, nir_alu_instr *instr, unsigned src, \
377 UNUSED unsigned num_components, UNUSED const uint8_t *swizzle) \
379 const struct ssa_result_range v = nir_analyze_range(ht, instr, src); \
380 return v.range == r; \
390 is_not_negative(struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
391 UNUSED
unsigned num_components
, UNUSED
const uint8_t *swizzle
)
393 const struct ssa_result_range v
= nir_analyze_range(ht
, instr
, src
);
394 return v
.range
== ge_zero
|| v
.range
== gt_zero
|| v
.range
== eq_zero
;
398 is_not_positive(struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
399 UNUSED
unsigned num_components
, UNUSED
const uint8_t *swizzle
)
401 const struct ssa_result_range v
= nir_analyze_range(ht
, instr
, src
);
402 return v
.range
== le_zero
|| v
.range
== lt_zero
|| v
.range
== eq_zero
;
406 is_not_zero(struct hash_table
*ht
, nir_alu_instr
*instr
, unsigned src
,
407 UNUSED
unsigned num_components
, UNUSED
const uint8_t *swizzle
)
409 const struct ssa_result_range v
= nir_analyze_range(ht
, instr
, src
);
410 return v
.range
== lt_zero
|| v
.range
== gt_zero
|| v
.range
== ne_zero
;
413 #endif /* _NIR_SEARCH_ */