nir/algebraic: Add more extract_[iu](8|16) optimizations
[mesa.git] / src / compiler / nir / nir_search_helpers.h
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 #include "util/bitscan.h"
32
33 static inline bool
34 is_pos_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
35 const uint8_t *swizzle)
36 {
37 nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
38
39 /* only constant srcs: */
40 if (!val)
41 return false;
42
43 for (unsigned i = 0; i < num_components; i++) {
44 switch (nir_op_infos[instr->op].input_types[src]) {
45 case nir_type_int:
46 if (val->i32[swizzle[i]] < 0)
47 return false;
48 if (!util_is_power_of_two_nonzero(val->i32[swizzle[i]]))
49 return false;
50 break;
51 case nir_type_uint:
52 if (!util_is_power_of_two_nonzero(val->u32[swizzle[i]]))
53 return false;
54 break;
55 default:
56 return false;
57 }
58 }
59
60 return true;
61 }
62
63 static inline bool
64 is_neg_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
65 const uint8_t *swizzle)
66 {
67 nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
68
69 /* only constant srcs: */
70 if (!val)
71 return false;
72
73 for (unsigned i = 0; i < num_components; i++) {
74 switch (nir_op_infos[instr->op].input_types[src]) {
75 case nir_type_int:
76 if (val->i32[swizzle[i]] > 0)
77 return false;
78 if (!util_is_power_of_two_nonzero(abs(val->i32[swizzle[i]])))
79 return false;
80 break;
81 default:
82 return false;
83 }
84 }
85
86 return true;
87 }
88
89 static inline bool
90 is_zero_to_one(nir_alu_instr *instr, unsigned src, unsigned num_components,
91 const uint8_t *swizzle)
92 {
93 nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
94
95 if (!val)
96 return false;
97
98 for (unsigned i = 0; i < num_components; i++) {
99 switch (nir_op_infos[instr->op].input_types[src]) {
100 case nir_type_float:
101 if (val->f32[swizzle[i]] < 0.0f || val->f32[swizzle[i]] > 1.0f)
102 return false;
103 break;
104 default:
105 return false;
106 }
107 }
108
109 return true;
110 }
111
112 static inline bool
113 is_not_const(nir_alu_instr *instr, unsigned src, UNUSED unsigned num_components,
114 UNUSED const uint8_t *swizzle)
115 {
116 nir_const_value *val = nir_src_as_const_value(instr->src[src].src);
117
118 if (val)
119 return false;
120
121 return true;
122 }
123
124 static inline bool
125 is_used_more_than_once(nir_alu_instr *instr)
126 {
127 bool zero_if_use = list_empty(&instr->dest.dest.ssa.if_uses);
128 bool zero_use = list_empty(&instr->dest.dest.ssa.uses);
129
130 if (zero_use && zero_if_use)
131 return false;
132 else if (zero_use && list_is_singular(&instr->dest.dest.ssa.if_uses))
133 return false;
134 else if (zero_if_use && list_is_singular(&instr->dest.dest.ssa.uses))
135 return false;
136
137 return true;
138 }
139
140 static inline bool
141 is_used_once(nir_alu_instr *instr)
142 {
143 bool zero_if_use = list_empty(&instr->dest.dest.ssa.if_uses);
144 bool zero_use = list_empty(&instr->dest.dest.ssa.uses);
145
146 if (zero_if_use && zero_use)
147 return false;
148
149 if (!zero_if_use && list_is_singular(&instr->dest.dest.ssa.uses))
150 return false;
151
152 if (!zero_use && list_is_singular(&instr->dest.dest.ssa.if_uses))
153 return false;
154
155 if (!list_is_singular(&instr->dest.dest.ssa.if_uses) &&
156 !list_is_singular(&instr->dest.dest.ssa.uses))
157 return false;
158
159 return true;
160 }
161
162 static inline bool
163 is_not_used_by_if(nir_alu_instr *instr)
164 {
165 return list_empty(&instr->dest.dest.ssa.if_uses);
166 }
167
168 #endif /* _NIR_SEARCH_ */