1 /*
2 * Copyright © 2016 Red Hat
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #ifndef _NIR_SEARCH_HELPERS_
28 #define _NIR_SEARCH_HELPERS_
29
30 #include "nir.h"
31
32 static inline bool
__is_power_of_two(unsigned int x)33 __is_power_of_two(unsigned int x)
34 {
35 return ((x != 0) && !(x & (x - 1)));
36 }
37
38 static inline bool
is_pos_power_of_two(nir_alu_instr * instr,unsigned src,unsigned num_components,const uint8_t * swizzle)39 is_pos_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
40 const uint8_t *swizzle)
41 {
42 nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
43
44 /* only constant src's: */
45 if (!val)
46 return false;
47
48 for (unsigned i = 0; i < num_components; i++) {
49 switch (nir_op_infos[instr->op].input_types[src]) {
50 case nir_type_int:
51 if (val->i32[swizzle[i]] < 0)
52 return false;
53 if (!__is_power_of_two(val->i32[swizzle[i]]))
54 return false;
55 break;
56 case nir_type_uint:
57 if (!__is_power_of_two(val->u32[swizzle[i]]))
58 return false;
59 break;
60 default:
61 return false;
62 }
63 }
64
65 return true;
66 }
67
68 static inline bool
is_neg_power_of_two(nir_alu_instr * instr,unsigned src,unsigned num_components,const uint8_t * swizzle)69 is_neg_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
70 const uint8_t *swizzle)
71 {
72 nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
73
74 /* only constant src's: */
75 if (!val)
76 return false;
77
78 for (unsigned i = 0; i < num_components; i++) {
79 switch (nir_op_infos[instr->op].input_types[src]) {
80 case nir_type_int:
81 if (val->i32[swizzle[i]] > 0)
82 return false;
83 if (!__is_power_of_two(abs(val->i32[swizzle[i]])))
84 return false;
85 break;
86 default:
87 return false;
88 }
89 }
90
91 return true;
92 }
93
94 static inline bool
is_zero_to_one(nir_alu_instr * instr,unsigned src,unsigned num_components,const uint8_t * swizzle)95 is_zero_to_one(nir_alu_instr *instr, unsigned src, unsigned num_components,
96 const uint8_t *swizzle)
97 {
98 nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
99
100 if (!val)
101 return false;
102
103 for (unsigned i = 0; i < num_components; i++) {
104 switch (nir_op_infos[instr->op].input_types[src]) {
105 case nir_type_float:
106 if (val->f32[swizzle[i]] < 0.0f || val->f32[swizzle[i]] > 1.0f)
107 return false;
108 break;
109 default:
110 return false;
111 }
112 }
113
114 return true;
115 }
116
117 static inline bool
is_used_more_than_once(nir_alu_instr * instr)118 is_used_more_than_once(nir_alu_instr *instr)
119 {
120 bool zero_if_use = list_empty(&instr->dest.dest.ssa.if_uses);
121 bool zero_use = list_empty(&instr->dest.dest.ssa.uses);
122
123 if (zero_use && zero_if_use)
124 return false;
125 else if (zero_use && list_is_singular(&instr->dest.dest.ssa.if_uses))
126 return false;
127 else if (zero_if_use && list_is_singular(&instr->dest.dest.ssa.uses))
128 return false;
129
130 return true;
131 }
132
133 static inline bool
is_used_once(nir_alu_instr * instr)134 is_used_once(nir_alu_instr *instr)
135 {
136 bool zero_if_use = list_empty(&instr->dest.dest.ssa.if_uses);
137 bool zero_use = list_empty(&instr->dest.dest.ssa.uses);
138
139 if (zero_if_use && zero_use)
140 return false;
141
142 if (!zero_if_use && list_is_singular(&instr->dest.dest.ssa.uses))
143 return false;
144
145 if (!zero_use && list_is_singular(&instr->dest.dest.ssa.if_uses))
146 return false;
147
148 if (!list_is_singular(&instr->dest.dest.ssa.if_uses) &&
149 !list_is_singular(&instr->dest.dest.ssa.uses))
150 return false;
151
152 return true;
153 }
154
155 static inline bool
is_not_used_by_if(nir_alu_instr * instr)156 is_not_used_by_if(nir_alu_instr *instr)
157 {
158 return list_empty(&instr->dest.dest.ssa.if_uses);
159 }
160
161 #endif /* _NIR_SEARCH_ */
162