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_
33 __is_power_of_two(unsigned int x
)
35 return ((x
!= 0) && !(x
& (x
- 1)));
39 is_pos_power_of_two(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
40 const uint8_t *swizzle
)
42 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[src
].src
);
44 /* only constant srcs: */
48 for (unsigned i
= 0; i
< num_components
; i
++) {
49 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
51 if (val
->i32
[swizzle
[i
]] < 0)
53 if (!__is_power_of_two(val
->i32
[swizzle
[i
]]))
57 if (!__is_power_of_two(val
->u32
[swizzle
[i
]]))
69 is_neg_power_of_two(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
70 const uint8_t *swizzle
)
72 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[src
].src
);
74 /* only constant srcs: */
78 for (unsigned i
= 0; i
< num_components
; i
++) {
79 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
81 if (val
->i32
[swizzle
[i
]] > 0)
83 if (!__is_power_of_two(abs(val
->i32
[swizzle
[i
]])))
95 is_zero_to_one(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
96 const uint8_t *swizzle
)
98 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[src
].src
);
103 for (unsigned i
= 0; i
< num_components
; i
++) {
104 switch (nir_op_infos
[instr
->op
].input_types
[src
]) {
106 if (val
->f32
[swizzle
[i
]] < 0.0f
|| val
->f32
[swizzle
[i
]] > 1.0f
)
118 is_not_const(nir_alu_instr
*instr
, unsigned src
, unsigned num_components
,
119 const uint8_t *swizzle
)
121 nir_const_value
*val
= nir_src_as_const_value(instr
->src
[src
].src
);
130 is_used_more_than_once(nir_alu_instr
*instr
)
132 bool zero_if_use
= list_empty(&instr
->dest
.dest
.ssa
.if_uses
);
133 bool zero_use
= list_empty(&instr
->dest
.dest
.ssa
.uses
);
135 if (zero_use
&& zero_if_use
)
137 else if (zero_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.if_uses
))
139 else if (zero_if_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.uses
))
146 is_used_once(nir_alu_instr
*instr
)
148 bool zero_if_use
= list_empty(&instr
->dest
.dest
.ssa
.if_uses
);
149 bool zero_use
= list_empty(&instr
->dest
.dest
.ssa
.uses
);
151 if (zero_if_use
&& zero_use
)
154 if (!zero_if_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.uses
))
157 if (!zero_use
&& list_is_singular(&instr
->dest
.dest
.ssa
.if_uses
))
160 if (!list_is_singular(&instr
->dest
.dest
.ssa
.if_uses
) &&
161 !list_is_singular(&instr
->dest
.dest
.ssa
.uses
))
168 is_not_used_by_if(nir_alu_instr
*instr
)
170 return list_empty(&instr
->dest
.dest
.ssa
.if_uses
);
174 is_not_used_by_conditional(nir_alu_instr
*instr
)
176 if (!is_not_used_by_if(instr
))
179 nir_foreach_use(use
, &instr
->dest
.dest
.ssa
) {
180 if (use
->parent_instr
->type
== nir_instr_type_alu
&&
181 nir_instr_as_alu(use
->parent_instr
)->op
== nir_op_bcsel
)
188 #endif /* _NIR_SEARCH_ */