nir/algebraic: Fail build when too many commutative expressions are used
[mesa.git] / src / compiler / nir / nir_opt_algebraic.py
1 #
2 # Copyright (C) 2014 Intel Corporation
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 # Jason Ekstrand (jason@jlekstrand.net)
25
26 from __future__ import print_function
27
28 from collections import OrderedDict
29 import nir_algebraic
30 from nir_opcodes import type_sizes
31 import itertools
32
33 # Convenience variables
34 a = 'a'
35 b = 'b'
36 c = 'c'
37 d = 'd'
38 e = 'e'
39
40 # Written in the form (<search>, <replace>) where <search> is an expression
41 # and <replace> is either an expression or a value. An expression is
42 # defined as a tuple of the form ([~]<op>, <src0>, <src1>, <src2>, <src3>)
43 # where each source is either an expression or a value. A value can be
44 # either a numeric constant or a string representing a variable name.
45 #
46 # If the opcode in a search expression is prefixed by a '~' character, this
47 # indicates that the operation is inexact. Such operations will only get
48 # applied to SSA values that do not have the exact bit set. This should be
49 # used by by any optimizations that are not bit-for-bit exact. It should not,
50 # however, be used for backend-requested lowering operations as those need to
51 # happen regardless of precision.
52 #
53 # Variable names are specified as "[#]name[@type][(cond)]" where "#" inicates
54 # that the given variable will only match constants and the type indicates that
55 # the given variable will only match values from ALU instructions with the
56 # given output type, and (cond) specifies an additional condition function
57 # (see nir_search_helpers.h).
58 #
59 # For constants, you have to be careful to make sure that it is the right
60 # type because python is unaware of the source and destination types of the
61 # opcodes.
62 #
63 # All expression types can have a bit-size specified. For opcodes, this
64 # looks like "op@32", for variables it is "a@32" or "a@uint32" to specify a
65 # type and size. In the search half of the expression this indicates that it
66 # should only match that particular bit-size. In the replace half of the
67 # expression this indicates that the constructed value should have that
68 # bit-size.
69 #
70 # A special condition "many-comm-expr" can be used with expressions to note
71 # that the expression and its subexpressions have more commutative expressions
72 # than nir_replace_instr can handle. If this special condition is needed with
73 # another condition, the two can be separated by a comma (e.g.,
74 # "(many-comm-expr,is_used_once)").
75
76 optimizations = [
77
78 (('imul', a, '#b@32(is_pos_power_of_two)'), ('ishl', a, ('find_lsb', b)), '!options->lower_bitshift'),
79 (('imul', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('ishl', a, ('find_lsb', ('iabs', b)))), '!options->lower_bitshift'),
80 (('ishl', a, '#b@32'), ('imul', a, ('ishl', 1, b)), 'options->lower_bitshift'),
81
82 (('unpack_64_2x32_split_x', ('imul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
83 (('unpack_64_2x32_split_x', ('umul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
84 (('imul_2x32_64', a, b), ('pack_64_2x32_split', ('imul', a, b), ('imul_high', a, b)), 'options->lower_mul_2x32_64'),
85 (('umul_2x32_64', a, b), ('pack_64_2x32_split', ('imul', a, b), ('umul_high', a, b)), 'options->lower_mul_2x32_64'),
86 (('udiv', a, 1), a),
87 (('idiv', a, 1), a),
88 (('umod', a, 1), 0),
89 (('imod', a, 1), 0),
90 (('udiv', a, '#b@32(is_pos_power_of_two)'), ('ushr', a, ('find_lsb', b)), '!options->lower_bitshift'),
91 (('idiv', a, '#b@32(is_pos_power_of_two)'), ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', b))), 'options->lower_idiv'),
92 (('idiv', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', ('iabs', b))))), 'options->lower_idiv'),
93 (('umod', a, '#b(is_pos_power_of_two)'), ('iand', a, ('isub', b, 1))),
94
95 (('fneg', ('fneg', a)), a),
96 (('ineg', ('ineg', a)), a),
97 (('fabs', ('fabs', a)), ('fabs', a)),
98 (('fabs', ('fneg', a)), ('fabs', a)),
99 (('fabs', ('u2f', a)), ('u2f', a)),
100 (('iabs', ('iabs', a)), ('iabs', a)),
101 (('iabs', ('ineg', a)), ('iabs', a)),
102 (('f2b', ('fneg', a)), ('f2b', a)),
103 (('i2b', ('ineg', a)), ('i2b', a)),
104 (('~fadd', a, 0.0), a),
105 (('iadd', a, 0), a),
106 (('usadd_4x8', a, 0), a),
107 (('usadd_4x8', a, ~0), ~0),
108 (('~fadd', ('fmul', a, b), ('fmul', a, c)), ('fmul', a, ('fadd', b, c))),
109 (('iadd', ('imul', a, b), ('imul', a, c)), ('imul', a, ('iadd', b, c))),
110 (('~fadd', ('fneg', a), a), 0.0),
111 (('iadd', ('ineg', a), a), 0),
112 (('iadd', ('ineg', a), ('iadd', a, b)), b),
113 (('iadd', a, ('iadd', ('ineg', a), b)), b),
114 (('~fadd', ('fneg', a), ('fadd', a, b)), b),
115 (('~fadd', a, ('fadd', ('fneg', a), b)), b),
116 (('fadd', ('fsat', a), ('fsat', ('fneg', a))), ('fsat', ('fabs', a))),
117 (('~fmul', a, 0.0), 0.0),
118 (('imul', a, 0), 0),
119 (('umul_unorm_4x8', a, 0), 0),
120 (('umul_unorm_4x8', a, ~0), a),
121 (('fmul', a, 1.0), a),
122 (('imul', a, 1), a),
123 (('fmul', a, -1.0), ('fneg', a)),
124 (('imul', a, -1), ('ineg', a)),
125 # If a < 0: fsign(a)*a*a => -1*a*a => -a*a => abs(a)*a
126 # If a > 0: fsign(a)*a*a => 1*a*a => a*a => abs(a)*a
127 # If a == 0: fsign(a)*a*a => 0*0*0 => abs(0)*0
128 (('fmul', ('fsign', a), ('fmul', a, a)), ('fmul', ('fabs', a), a)),
129 (('fmul', ('fmul', ('fsign', a), a), a), ('fmul', ('fabs', a), a)),
130 (('~ffma', 0.0, a, b), b),
131 (('~ffma', a, b, 0.0), ('fmul', a, b)),
132 (('ffma', 1.0, a, b), ('fadd', a, b)),
133 (('ffma', -1.0, a, b), ('fadd', ('fneg', a), b)),
134 (('~flrp', a, b, 0.0), a),
135 (('~flrp', a, b, 1.0), b),
136 (('~flrp', a, a, b), a),
137 (('~flrp', 0.0, a, b), ('fmul', a, b)),
138
139 # flrp(a, a + b, c) => a + flrp(0, b, c) => a + (b * c)
140 (('~flrp', a, ('fadd(is_used_once)', a, b), c), ('fadd', ('fmul', b, c), a)),
141 (('~flrp@32', a, ('fadd', a, b), c), ('fadd', ('fmul', b, c), a), 'options->lower_flrp32'),
142 (('~flrp@64', a, ('fadd', a, b), c), ('fadd', ('fmul', b, c), a), 'options->lower_flrp64'),
143
144 (('~flrp@32', ('fadd', a, b), ('fadd', a, c), d), ('fadd', ('flrp', b, c, d), a), 'options->lower_flrp32'),
145 (('~flrp@64', ('fadd', a, b), ('fadd', a, c), d), ('fadd', ('flrp', b, c, d), a), 'options->lower_flrp64'),
146
147 (('~flrp@32', a, ('fmul(is_used_once)', a, b), c), ('fmul', ('flrp', 1.0, b, c), a), 'options->lower_flrp32'),
148 (('~flrp@64', a, ('fmul(is_used_once)', a, b), c), ('fmul', ('flrp', 1.0, b, c), a), 'options->lower_flrp64'),
149
150 (('~flrp', ('fmul(is_used_once)', a, b), ('fmul(is_used_once)', a, c), d), ('fmul', ('flrp', b, c, d), a)),
151
152 (('~flrp', a, b, ('b2f', 'c@1')), ('bcsel', c, b, a), 'options->lower_flrp32'),
153 (('~flrp', a, 0.0, c), ('fadd', ('fmul', ('fneg', a), c), a)),
154 (('ftrunc', a), ('bcsel', ('flt', a, 0.0), ('fneg', ('ffloor', ('fabs', a))), ('ffloor', ('fabs', a))), 'options->lower_ftrunc'),
155 (('ffloor', a), ('fsub', a, ('ffract', a)), 'options->lower_ffloor'),
156 (('fadd', a, ('fneg', ('ffract', a))), ('ffloor', a), '!options->lower_ffloor'),
157 (('ffract', a), ('fsub', a, ('ffloor', a)), 'options->lower_ffract'),
158 (('fceil', a), ('fneg', ('ffloor', ('fneg', a))), 'options->lower_fceil'),
159 (('~fadd', ('fmul', a, ('fadd', 1.0, ('fneg', ('b2f', 'c@1')))), ('fmul', b, ('b2f', c))), ('bcsel', c, b, a), 'options->lower_flrp32'),
160 (('~fadd@32', ('fmul', a, ('fadd', 1.0, ('fneg', c ) )), ('fmul', b, c )), ('flrp', a, b, c), '!options->lower_flrp32'),
161 (('~fadd@64', ('fmul', a, ('fadd', 1.0, ('fneg', c ) )), ('fmul', b, c )), ('flrp', a, b, c), '!options->lower_flrp64'),
162 # These are the same as the previous three rules, but it depends on
163 # 1-fsat(x) <=> fsat(1-x). See below.
164 (('~fadd@32', ('fmul', a, ('fsat', ('fadd', 1.0, ('fneg', c )))), ('fmul', b, ('fsat', c))), ('flrp', a, b, ('fsat', c)), '!options->lower_flrp32'),
165 (('~fadd@64', ('fmul', a, ('fsat', ('fadd', 1.0, ('fneg', c )))), ('fmul', b, ('fsat', c))), ('flrp', a, b, ('fsat', c)), '!options->lower_flrp64'),
166
167 (('~fadd', a, ('fmul', ('b2f', 'c@1'), ('fadd', b, ('fneg', a)))), ('bcsel', c, b, a), 'options->lower_flrp32'),
168 (('~fadd@32', a, ('fmul', c , ('fadd', b, ('fneg', a)))), ('flrp', a, b, c), '!options->lower_flrp32'),
169 (('~fadd@64', a, ('fmul', c , ('fadd', b, ('fneg', a)))), ('flrp', a, b, c), '!options->lower_flrp64'),
170 (('ffma', a, b, c), ('fadd', ('fmul', a, b), c), 'options->lower_ffma'),
171 (('~fadd', ('fmul', a, b), c), ('ffma', a, b, c), 'options->fuse_ffma'),
172
173 (('~fmul', ('fadd', ('iand', ('ineg', ('b2i32', 'a@bool')), ('fmul', b, c)), '#d'), '#e'),
174 ('bcsel', a, ('fmul', ('fadd', ('fmul', b, c), d), e), ('fmul', d, e))),
175
176 (('fdot4', ('vec4', a, b, c, 1.0), d), ('fdph', ('vec3', a, b, c), d)),
177 (('fdot4', ('vec4', a, 0.0, 0.0, 0.0), b), ('fmul', a, b)),
178 (('fdot4', ('vec4', a, b, 0.0, 0.0), c), ('fdot2', ('vec2', a, b), c)),
179 (('fdot4', ('vec4', a, b, c, 0.0), d), ('fdot3', ('vec3', a, b, c), d)),
180
181 (('fdot3', ('vec3', a, 0.0, 0.0), b), ('fmul', a, b)),
182 (('fdot3', ('vec3', a, b, 0.0), c), ('fdot2', ('vec2', a, b), c)),
183
184 # If x >= 0 and x <= 1: fsat(1 - x) == 1 - fsat(x) trivially
185 # If x < 0: 1 - fsat(x) => 1 - 0 => 1 and fsat(1 - x) => fsat(> 1) => 1
186 # If x > 1: 1 - fsat(x) => 1 - 1 => 0 and fsat(1 - x) => fsat(< 0) => 0
187 (('~fadd', ('fneg(is_used_once)', ('fsat(is_used_once)', 'a(is_not_fmul)')), 1.0), ('fsat', ('fadd', 1.0, ('fneg', a)))),
188
189 # (a * #b + #c) << #d
190 # ((a * #b) << #d) + (#c << #d)
191 # (a * (#b << #d)) + (#c << #d)
192 (('ishl', ('iadd', ('imul', a, '#b'), '#c'), '#d'),
193 ('iadd', ('imul', a, ('ishl', b, d)), ('ishl', c, d))),
194
195 # (a * #b) << #c
196 # a * (#b << #c)
197 (('ishl', ('imul', a, '#b'), '#c'), ('imul', a, ('ishl', b, c))),
198
199 # Comparison simplifications
200 (('~inot', ('flt', a, b)), ('fge', a, b)),
201 (('~inot', ('fge', a, b)), ('flt', a, b)),
202 (('~inot', ('feq', a, b)), ('fne', a, b)),
203 (('~inot', ('fne', a, b)), ('feq', a, b)),
204 (('inot', ('ilt', a, b)), ('ige', a, b)),
205 (('inot', ('ult', a, b)), ('uge', a, b)),
206 (('inot', ('ige', a, b)), ('ilt', a, b)),
207 (('inot', ('uge', a, b)), ('ult', a, b)),
208 (('inot', ('ieq', a, b)), ('ine', a, b)),
209 (('inot', ('ine', a, b)), ('ieq', a, b)),
210
211 # This helps some shaders because, after some optimizations, they end up
212 # with patterns like (-a < -b) || (b < a). In an ideal world, this sort of
213 # matching would be handled by CSE.
214 (('flt', ('fneg', a), ('fneg', b)), ('flt', b, a)),
215 (('fge', ('fneg', a), ('fneg', b)), ('fge', b, a)),
216 (('feq', ('fneg', a), ('fneg', b)), ('feq', b, a)),
217 (('fne', ('fneg', a), ('fneg', b)), ('fne', b, a)),
218 (('flt', ('fneg', a), -1.0), ('flt', 1.0, a)),
219 (('flt', -1.0, ('fneg', a)), ('flt', a, 1.0)),
220 (('fge', ('fneg', a), -1.0), ('fge', 1.0, a)),
221 (('fge', -1.0, ('fneg', a)), ('fge', a, 1.0)),
222 (('fne', ('fneg', a), -1.0), ('fne', 1.0, a)),
223 (('feq', -1.0, ('fneg', a)), ('feq', a, 1.0)),
224
225 (('flt', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('flt', a, b)),
226 (('flt', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('flt', b, a)),
227 (('fge', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fge', a, b)),
228 (('fge', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('fge', b, a)),
229 (('feq', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('feq', a, b)),
230 (('fne', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fne', a, b)),
231
232 (('fge', ('fsat(is_used_once)', a), 1.0), ('fge', a, 1.0)),
233 (('flt', ('fsat(is_used_once)', a), 1.0), ('flt', a, 1.0)),
234 (('fge', 0.0, ('fsat(is_used_once)', a)), ('fge', 0.0, a)),
235 (('flt', 0.0, ('fsat(is_used_once)', a)), ('flt', 0.0, a)),
236
237 # 0.0 >= b2f(a)
238 # b2f(a) <= 0.0
239 # b2f(a) == 0.0 because b2f(a) can only be 0 or 1
240 # inot(a)
241 (('fge', 0.0, ('b2f', 'a@1')), ('inot', a)),
242
243 (('fge', ('fneg', ('b2f', 'a@1')), 0.0), ('inot', a)),
244
245 (('fne', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('ior', a, b)),
246 (('fne', ('fmax', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('ior', a, b)),
247 (('fne', ('bcsel', a, 1.0, ('b2f', 'b@1')) , 0.0), ('ior', a, b)),
248 (('fne', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), ('ior', a, b)),
249 (('fne', ('fmul', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('iand', a, b)),
250 (('fne', ('fmin', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('iand', a, b)),
251 (('fne', ('bcsel', a, ('b2f', 'b@1'), 0.0) , 0.0), ('iand', a, b)),
252 (('fne', ('fadd', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), 0.0), ('ixor', a, b)),
253 (('fne', ('b2f', 'a@1') , ('b2f', 'b@1') ), ('ixor', a, b)),
254 (('fne', ('fneg', ('b2f', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('ixor', a, b)),
255 (('feq', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('ior', a, b))),
256 (('feq', ('fmax', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('ior', a, b))),
257 (('feq', ('bcsel', a, 1.0, ('b2f', 'b@1')) , 0.0), ('inot', ('ior', a, b))),
258 (('feq', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), ('inot', ('ior', a, b))),
259 (('feq', ('fmul', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('iand', a, b))),
260 (('feq', ('fmin', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('iand', a, b))),
261 (('feq', ('bcsel', a, ('b2f', 'b@1'), 0.0) , 0.0), ('inot', ('iand', a, b))),
262 (('feq', ('fadd', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), 0.0), ('ieq', a, b)),
263 (('feq', ('b2f', 'a@1') , ('b2f', 'b@1') ), ('ieq', a, b)),
264 (('feq', ('fneg', ('b2f', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('ieq', a, b)),
265
266 # -(b2f(a) + b2f(b)) < 0
267 # 0 < b2f(a) + b2f(b)
268 # 0 != b2f(a) + b2f(b) b2f must be 0 or 1, so the sum is non-negative
269 # a || b
270 (('flt', ('fneg', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), 0.0), ('ior', a, b)),
271 (('flt', 0.0, ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), ('ior', a, b)),
272
273 # -(b2f(a) + b2f(b)) >= 0
274 # 0 >= b2f(a) + b2f(b)
275 # 0 == b2f(a) + b2f(b) b2f must be 0 or 1, so the sum is non-negative
276 # !(a || b)
277 (('fge', ('fneg', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), 0.0), ('inot', ('ior', a, b))),
278 (('fge', 0.0, ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), ('inot', ('ior', a, b))),
279
280 (('flt', a, ('fneg', a)), ('flt', a, 0.0)),
281 (('fge', a, ('fneg', a)), ('fge', a, 0.0)),
282
283 # Some optimizations (below) convert things like (a < b || c < b) into
284 # (min(a, c) < b). However, this interfers with the previous optimizations
285 # that try to remove comparisons with negated sums of b2f. This just
286 # breaks that apart.
287 (('flt', ('fmin', c, ('fneg', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1')))), 0.0),
288 ('ior', ('flt', c, 0.0), ('ior', a, b))),
289
290 (('~flt', ('fadd', a, b), a), ('flt', b, 0.0)),
291 (('~fge', ('fadd', a, b), a), ('fge', b, 0.0)),
292 (('~feq', ('fadd', a, b), a), ('feq', b, 0.0)),
293 (('~fne', ('fadd', a, b), a), ('fne', b, 0.0)),
294
295 # Cannot remove the addition from ilt or ige due to overflow.
296 (('ieq', ('iadd', a, b), a), ('ieq', b, 0)),
297 (('ine', ('iadd', a, b), a), ('ine', b, 0)),
298
299 # fmin(-b2f(a), b) >= 0.0
300 # -b2f(a) >= 0.0 && b >= 0.0
301 # -b2f(a) == 0.0 && b >= 0.0 -b2f can only be 0 or -1, never >0
302 # b2f(a) == 0.0 && b >= 0.0
303 # a == False && b >= 0.0
304 # !a && b >= 0.0
305 #
306 # The fge in the second replacement is not a typo. I leave the proof that
307 # "fmin(-b2f(a), b) >= 0 <=> fmin(-b2f(a), b) == 0" as an exercise for the
308 # reader.
309 (('fge', ('fmin', ('fneg', ('b2f', 'a@1')), 'b@1'), 0.0), ('iand', ('inot', a), ('fge', b, 0.0))),
310 (('feq', ('fmin', ('fneg', ('b2f', 'a@1')), 'b@1'), 0.0), ('iand', ('inot', a), ('fge', b, 0.0))),
311
312 (('feq', ('b2f', 'a@1'), 0.0), ('inot', a)),
313 (('fne', ('b2f', 'a@1'), 0.0), a),
314 (('ieq', ('b2i', 'a@1'), 0), ('inot', a)),
315 (('ine', ('b2i', 'a@1'), 0), a),
316
317 (('fne', ('u2f', a), 0.0), ('ine', a, 0)),
318 (('feq', ('u2f', a), 0.0), ('ieq', a, 0)),
319 (('fge', ('u2f', a), 0.0), True),
320 (('fge', 0.0, ('u2f', a)), ('uge', 0, a)), # ieq instead?
321 (('flt', ('u2f', a), 0.0), False),
322 (('flt', 0.0, ('u2f', a)), ('ult', 0, a)), # ine instead?
323 (('fne', ('i2f', a), 0.0), ('ine', a, 0)),
324 (('feq', ('i2f', a), 0.0), ('ieq', a, 0)),
325 (('fge', ('i2f', a), 0.0), ('ige', a, 0)),
326 (('fge', 0.0, ('i2f', a)), ('ige', 0, a)),
327 (('flt', ('i2f', a), 0.0), ('ilt', a, 0)),
328 (('flt', 0.0, ('i2f', a)), ('ilt', 0, a)),
329
330 # 0.0 < fabs(a)
331 # fabs(a) > 0.0
332 # fabs(a) != 0.0 because fabs(a) must be >= 0
333 # a != 0.0
334 (('~flt', 0.0, ('fabs', a)), ('fne', a, 0.0)),
335
336 # -fabs(a) < 0.0
337 # fabs(a) > 0.0
338 (('~flt', ('fneg', ('fabs', a)), 0.0), ('fne', a, 0.0)),
339
340 # 0.0 >= fabs(a)
341 # 0.0 == fabs(a) because fabs(a) must be >= 0
342 # 0.0 == a
343 (('fge', 0.0, ('fabs', a)), ('feq', a, 0.0)),
344
345 # -fabs(a) >= 0.0
346 # 0.0 >= fabs(a)
347 (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
348
349 (('fmax', ('b2f(is_used_once)', 'a@1'), ('b2f', 'b@1')), ('b2f', ('ior', a, b))),
350 (('fmax', ('fneg(is_used_once)', ('b2f(is_used_once)', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('fneg', ('b2f', ('ior', a, b)))),
351 (('fmin', ('b2f(is_used_once)', 'a@1'), ('b2f', 'b@1')), ('b2f', ('iand', a, b))),
352 (('fmin', ('fneg(is_used_once)', ('b2f(is_used_once)', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('fneg', ('b2f', ('iand', a, b)))),
353
354 # fmin(b2f(a), b)
355 # bcsel(a, fmin(b2f(a), b), fmin(b2f(a), b))
356 # bcsel(a, fmin(b2f(True), b), fmin(b2f(False), b))
357 # bcsel(a, fmin(1.0, b), fmin(0.0, b))
358 #
359 # Since b is a constant, constant folding will eliminate the fmin and the
360 # fmax. If b is > 1.0, the bcsel will be replaced with a b2f.
361 (('fmin', ('b2f', 'a@1'), '#b'), ('bcsel', a, ('fmin', b, 1.0), ('fmin', b, 0.0))),
362
363 (('flt', ('fadd(is_used_once)', a, ('fneg', b)), 0.0), ('flt', a, b)),
364
365 (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
366 (('~bcsel', ('flt', b, a), b, a), ('fmin', a, b)),
367 (('~bcsel', ('flt', a, b), b, a), ('fmax', a, b)),
368 (('~bcsel', ('fge', a, b), b, a), ('fmin', a, b)),
369 (('~bcsel', ('fge', b, a), b, a), ('fmax', a, b)),
370 (('bcsel', ('i2b', a), b, c), ('bcsel', ('ine', a, 0), b, c)),
371 (('bcsel', ('inot', a), b, c), ('bcsel', a, c, b)),
372 (('bcsel', a, ('bcsel', a, b, c), d), ('bcsel', a, b, d)),
373 (('bcsel', a, b, ('bcsel', a, c, d)), ('bcsel', a, b, d)),
374 (('bcsel', a, ('bcsel', b, c, d), ('bcsel(is_used_once)', b, c, 'e')), ('bcsel', b, c, ('bcsel', a, d, 'e'))),
375 (('bcsel', a, ('bcsel(is_used_once)', b, c, d), ('bcsel', b, c, 'e')), ('bcsel', b, c, ('bcsel', a, d, 'e'))),
376 (('bcsel', a, ('bcsel', b, c, d), ('bcsel(is_used_once)', b, 'e', d)), ('bcsel', b, ('bcsel', a, c, 'e'), d)),
377 (('bcsel', a, ('bcsel(is_used_once)', b, c, d), ('bcsel', b, 'e', d)), ('bcsel', b, ('bcsel', a, c, 'e'), d)),
378 (('bcsel', a, True, b), ('ior', a, b)),
379 (('bcsel', a, a, b), ('ior', a, b)),
380 (('bcsel', a, b, False), ('iand', a, b)),
381 (('bcsel', a, b, a), ('iand', a, b)),
382 (('fmin', a, a), a),
383 (('fmax', a, a), a),
384 (('imin', a, a), a),
385 (('imax', a, a), a),
386 (('umin', a, a), a),
387 (('umax', a, a), a),
388 (('fmax', ('fmax', a, b), b), ('fmax', a, b)),
389 (('umax', ('umax', a, b), b), ('umax', a, b)),
390 (('imax', ('imax', a, b), b), ('imax', a, b)),
391 (('fmin', ('fmin', a, b), b), ('fmin', a, b)),
392 (('umin', ('umin', a, b), b), ('umin', a, b)),
393 (('imin', ('imin', a, b), b), ('imin', a, b)),
394 (('fmax', a, ('fneg', a)), ('fabs', a)),
395 (('imax', a, ('ineg', a)), ('iabs', a)),
396 (('fmin', a, ('fneg', a)), ('fneg', ('fabs', a))),
397 (('imin', a, ('ineg', a)), ('ineg', ('iabs', a))),
398 (('fmin', a, ('fneg', ('fabs', a))), ('fneg', ('fabs', a))),
399 (('imin', a, ('ineg', ('iabs', a))), ('ineg', ('iabs', a))),
400 (('fmin', a, ('fabs', a)), a),
401 (('imin', a, ('iabs', a)), a),
402 (('fmax', a, ('fneg', ('fabs', a))), a),
403 (('imax', a, ('ineg', ('iabs', a))), a),
404 (('fmax', a, ('fabs', a)), ('fabs', a)),
405 (('imax', a, ('iabs', a)), ('iabs', a)),
406 (('fmax', a, ('fneg', a)), ('fabs', a)),
407 (('imax', a, ('ineg', a)), ('iabs', a)),
408 (('~fmax', ('fabs', a), 0.0), ('fabs', a)),
409 (('~fmin', ('fmax', a, 0.0), 1.0), ('fsat', a), '!options->lower_fsat'),
410 (('~fmax', ('fmin', a, 1.0), 0.0), ('fsat', a), '!options->lower_fsat'),
411 (('~fmin', ('fmax', a, -1.0), 0.0), ('fneg', ('fsat', ('fneg', a))), '!options->lower_negate && !options->lower_fsat'),
412 (('~fmax', ('fmin', a, 0.0), -1.0), ('fneg', ('fsat', ('fneg', a))), '!options->lower_negate && !options->lower_fsat'),
413 (('fsat', ('fsign', a)), ('b2f', ('flt', 0.0, a))),
414 (('fsat', ('b2f', a)), ('b2f', a)),
415 (('fsat', a), ('fmin', ('fmax', a, 0.0), 1.0), 'options->lower_fsat'),
416 (('fsat', ('fsat', a)), ('fsat', a)),
417 (('fsat', ('fneg(is_used_once)', ('fadd(is_used_once)', a, b))), ('fsat', ('fadd', ('fneg', a), ('fneg', b))), '!options->lower_negate && !options->lower_fsat'),
418 (('fsat', ('fneg(is_used_once)', ('fmul(is_used_once)', a, b))), ('fsat', ('fmul', ('fneg', a), b)), '!options->lower_negate && !options->lower_fsat'),
419 (('fsat', ('fabs(is_used_once)', ('fmul(is_used_once)', a, b))), ('fsat', ('fmul', ('fabs', a), ('fabs', b))), '!options->lower_fsat'),
420 (('fmin', ('fmax', ('fmin', ('fmax', a, b), c), b), c), ('fmin', ('fmax', a, b), c)),
421 (('imin', ('imax', ('imin', ('imax', a, b), c), b), c), ('imin', ('imax', a, b), c)),
422 (('umin', ('umax', ('umin', ('umax', a, b), c), b), c), ('umin', ('umax', a, b), c)),
423 (('fmax', ('fsat', a), '#b@32(is_zero_to_one)'), ('fsat', ('fmax', a, b))),
424 (('fmin', ('fsat', a), '#b@32(is_zero_to_one)'), ('fsat', ('fmin', a, b))),
425 (('extract_u8', ('imin', ('imax', a, 0), 0xff), 0), ('imin', ('imax', a, 0), 0xff)),
426 (('~ior', ('flt(is_used_once)', a, b), ('flt', a, c)), ('flt', a, ('fmax', b, c))),
427 (('~ior', ('flt(is_used_once)', a, c), ('flt', b, c)), ('flt', ('fmin', a, b), c)),
428 (('~ior', ('fge(is_used_once)', a, b), ('fge', a, c)), ('fge', a, ('fmin', b, c))),
429 (('~ior', ('fge(is_used_once)', a, c), ('fge', b, c)), ('fge', ('fmax', a, b), c)),
430 (('~ior', ('flt', a, '#b'), ('flt', a, '#c')), ('flt', a, ('fmax', b, c))),
431 (('~ior', ('flt', '#a', c), ('flt', '#b', c)), ('flt', ('fmin', a, b), c)),
432 (('~ior', ('fge', a, '#b'), ('fge', a, '#c')), ('fge', a, ('fmin', b, c))),
433 (('~ior', ('fge', '#a', c), ('fge', '#b', c)), ('fge', ('fmax', a, b), c)),
434 (('~iand', ('flt(is_used_once)', a, b), ('flt', a, c)), ('flt', a, ('fmin', b, c))),
435 (('~iand', ('flt(is_used_once)', a, c), ('flt', b, c)), ('flt', ('fmax', a, b), c)),
436 (('~iand', ('fge(is_used_once)', a, b), ('fge', a, c)), ('fge', a, ('fmax', b, c))),
437 (('~iand', ('fge(is_used_once)', a, c), ('fge', b, c)), ('fge', ('fmin', a, b), c)),
438 (('~iand', ('flt', a, '#b'), ('flt', a, '#c')), ('flt', a, ('fmin', b, c))),
439 (('~iand', ('flt', '#a', c), ('flt', '#b', c)), ('flt', ('fmax', a, b), c)),
440 (('~iand', ('fge', a, '#b'), ('fge', a, '#c')), ('fge', a, ('fmax', b, c))),
441 (('~iand', ('fge', '#a', c), ('fge', '#b', c)), ('fge', ('fmin', a, b), c)),
442
443 (('ior', ('ilt(is_used_once)', a, b), ('ilt', a, c)), ('ilt', a, ('imax', b, c))),
444 (('ior', ('ilt(is_used_once)', a, c), ('ilt', b, c)), ('ilt', ('imin', a, b), c)),
445 (('ior', ('ige(is_used_once)', a, b), ('ige', a, c)), ('ige', a, ('imin', b, c))),
446 (('ior', ('ige(is_used_once)', a, c), ('ige', b, c)), ('ige', ('imax', a, b), c)),
447 (('ior', ('ult(is_used_once)', a, b), ('ult', a, c)), ('ult', a, ('umax', b, c))),
448 (('ior', ('ult(is_used_once)', a, c), ('ult', b, c)), ('ult', ('umin', a, b), c)),
449 (('ior', ('uge(is_used_once)', a, b), ('uge', a, c)), ('uge', a, ('umin', b, c))),
450 (('ior', ('uge(is_used_once)', a, c), ('uge', b, c)), ('uge', ('umax', a, b), c)),
451 (('iand', ('ilt(is_used_once)', a, b), ('ilt', a, c)), ('ilt', a, ('imin', b, c))),
452 (('iand', ('ilt(is_used_once)', a, c), ('ilt', b, c)), ('ilt', ('imax', a, b), c)),
453 (('iand', ('ige(is_used_once)', a, b), ('ige', a, c)), ('ige', a, ('imax', b, c))),
454 (('iand', ('ige(is_used_once)', a, c), ('ige', b, c)), ('ige', ('imin', a, b), c)),
455 (('iand', ('ult(is_used_once)', a, b), ('ult', a, c)), ('ult', a, ('umin', b, c))),
456 (('iand', ('ult(is_used_once)', a, c), ('ult', b, c)), ('ult', ('umax', a, b), c)),
457 (('iand', ('uge(is_used_once)', a, b), ('uge', a, c)), ('uge', a, ('umax', b, c))),
458 (('iand', ('uge(is_used_once)', a, c), ('uge', b, c)), ('uge', ('umin', a, b), c)),
459
460 # Common pattern like 'if (i == 0 || i == 1 || ...)'
461 (('ior', ('ieq', a, 0), ('ieq', a, 1)), ('uge', 1, a)),
462 (('ior', ('uge', 1, a), ('ieq', a, 2)), ('uge', 2, a)),
463 (('ior', ('uge', 2, a), ('ieq', a, 3)), ('uge', 3, a)),
464
465 # The (i2f32, ...) part is an open-coded fsign. When that is combined with
466 # the bcsel, it's basically copysign(1.0, a). There is no copysign in NIR,
467 # so emit an open-coded version of that.
468 (('bcsel@32', ('feq', a, 0.0), 1.0, ('i2f32', ('iadd', ('b2i32', ('flt', 0.0, 'a@32')), ('ineg', ('b2i32', ('flt', 'a@32', 0.0)))))),
469 ('ior', 0x3f800000, ('iand', a, 0x80000000))),
470
471 (('ior', a, ('ieq', a, False)), True),
472 (('ior', a, ('inot', a)), -1),
473
474 (('ine', ('ineg', ('b2i32', 'a@1')), ('ineg', ('b2i32', 'b@1'))), ('ine', a, b)),
475 (('b2i32', ('ine', 'a@1', 'b@1')), ('b2i32', ('ixor', a, b))),
476
477 (('iand', ('ieq', 'a@32', 0), ('ieq', 'b@32', 0)), ('ieq', ('ior', 'a@32', 'b@32'), 0)),
478
479 # These patterns can result when (a < b || a < c) => (a < min(b, c))
480 # transformations occur before constant propagation and loop-unrolling.
481 (('~flt', a, ('fmax', b, a)), ('flt', a, b)),
482 (('~flt', ('fmin', a, b), a), ('flt', b, a)),
483 (('~fge', a, ('fmin', b, a)), True),
484 (('~fge', ('fmax', a, b), a), True),
485 (('~flt', a, ('fmin', b, a)), False),
486 (('~flt', ('fmax', a, b), a), False),
487 (('~fge', a, ('fmax', b, a)), ('fge', a, b)),
488 (('~fge', ('fmin', a, b), a), ('fge', b, a)),
489
490 (('ilt', a, ('imax', b, a)), ('ilt', a, b)),
491 (('ilt', ('imin', a, b), a), ('ilt', b, a)),
492 (('ige', a, ('imin', b, a)), True),
493 (('ige', ('imax', a, b), a), True),
494 (('ult', a, ('umax', b, a)), ('ult', a, b)),
495 (('ult', ('umin', a, b), a), ('ult', b, a)),
496 (('uge', a, ('umin', b, a)), True),
497 (('uge', ('umax', a, b), a), True),
498 (('ilt', a, ('imin', b, a)), False),
499 (('ilt', ('imax', a, b), a), False),
500 (('ige', a, ('imax', b, a)), ('ige', a, b)),
501 (('ige', ('imin', a, b), a), ('ige', b, a)),
502 (('ult', a, ('umin', b, a)), False),
503 (('ult', ('umax', a, b), a), False),
504 (('uge', a, ('umax', b, a)), ('uge', a, b)),
505 (('uge', ('umin', a, b), a), ('uge', b, a)),
506 (('ult', a, ('iand', b, a)), False),
507 (('ult', ('ior', a, b), a), False),
508 (('uge', a, ('iand', b, a)), True),
509 (('uge', ('ior', a, b), a), True),
510
511 (('ilt', '#a', ('imax', '#b', c)), ('ior', ('ilt', a, b), ('ilt', a, c))),
512 (('ilt', ('imin', '#a', b), '#c'), ('ior', ('ilt', a, c), ('ilt', b, c))),
513 (('ige', '#a', ('imin', '#b', c)), ('ior', ('ige', a, b), ('ige', a, c))),
514 (('ige', ('imax', '#a', b), '#c'), ('ior', ('ige', a, c), ('ige', b, c))),
515 (('ult', '#a', ('umax', '#b', c)), ('ior', ('ult', a, b), ('ult', a, c))),
516 (('ult', ('umin', '#a', b), '#c'), ('ior', ('ult', a, c), ('ult', b, c))),
517 (('uge', '#a', ('umin', '#b', c)), ('ior', ('uge', a, b), ('uge', a, c))),
518 (('uge', ('umax', '#a', b), '#c'), ('ior', ('uge', a, c), ('uge', b, c))),
519 (('ilt', '#a', ('imin', '#b', c)), ('iand', ('ilt', a, b), ('ilt', a, c))),
520 (('ilt', ('imax', '#a', b), '#c'), ('iand', ('ilt', a, c), ('ilt', b, c))),
521 (('ige', '#a', ('imax', '#b', c)), ('iand', ('ige', a, b), ('ige', a, c))),
522 (('ige', ('imin', '#a', b), '#c'), ('iand', ('ige', a, c), ('ige', b, c))),
523 (('ult', '#a', ('umin', '#b', c)), ('iand', ('ult', a, b), ('ult', a, c))),
524 (('ult', ('umax', '#a', b), '#c'), ('iand', ('ult', a, c), ('ult', b, c))),
525 (('uge', '#a', ('umax', '#b', c)), ('iand', ('uge', a, b), ('uge', a, c))),
526 (('uge', ('umin', '#a', b), '#c'), ('iand', ('uge', a, c), ('uge', b, c))),
527
528 # Thanks to sign extension, the ishr(a, b) is negative if and only if a is
529 # negative.
530 (('bcsel', ('ilt', a, 0), ('ineg', ('ishr', a, b)), ('ishr', a, b)),
531 ('iabs', ('ishr', a, b))),
532 (('iabs', ('ishr', ('iabs', a), b)), ('ishr', ('iabs', a), b)),
533
534 (('fabs', ('slt', a, b)), ('slt', a, b)),
535 (('fabs', ('sge', a, b)), ('sge', a, b)),
536 (('fabs', ('seq', a, b)), ('seq', a, b)),
537 (('fabs', ('sne', a, b)), ('sne', a, b)),
538 (('slt', a, b), ('b2f', ('flt', a, b)), 'options->lower_scmp'),
539 (('sge', a, b), ('b2f', ('fge', a, b)), 'options->lower_scmp'),
540 (('seq', a, b), ('b2f', ('feq', a, b)), 'options->lower_scmp'),
541 (('sne', a, b), ('b2f', ('fne', a, b)), 'options->lower_scmp'),
542 (('fne', ('fneg', a), a), ('fne', a, 0.0)),
543 (('feq', ('fneg', a), a), ('feq', a, 0.0)),
544 # Emulating booleans
545 (('imul', ('b2i', 'a@1'), ('b2i', 'b@1')), ('b2i', ('iand', a, b))),
546 (('fmul', ('b2f', 'a@1'), ('b2f', 'b@1')), ('b2f', ('iand', a, b))),
547 (('fsat', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), ('b2f', ('ior', a, b))),
548 (('iand', 'a@bool32', 1.0), ('b2f', a)),
549 # True/False are ~0 and 0 in NIR. b2i of True is 1, and -1 is ~0 (True).
550 (('ineg', ('b2i32', 'a@32')), a),
551 (('flt', ('fneg', ('b2f', 'a@1')), 0), a), # Generated by TGSI KILL_IF.
552 (('flt', ('fsub', 0.0, ('b2f', 'a@1')), 0), a), # Generated by TGSI KILL_IF.
553 # Comparison with the same args. Note that these are not done for
554 # the float versions because NaN always returns false on float
555 # inequalities.
556 (('ilt', a, a), False),
557 (('ige', a, a), True),
558 (('ieq', a, a), True),
559 (('ine', a, a), False),
560 (('ult', a, a), False),
561 (('uge', a, a), True),
562 # Logical and bit operations
563 (('iand', a, a), a),
564 (('iand', a, ~0), a),
565 (('iand', a, 0), 0),
566 (('ior', a, a), a),
567 (('ior', a, 0), a),
568 (('ior', a, True), True),
569 (('ixor', a, a), 0),
570 (('ixor', a, 0), a),
571 (('inot', ('inot', a)), a),
572 (('ior', ('iand', a, b), b), b),
573 (('ior', ('ior', a, b), b), ('ior', a, b)),
574 (('iand', ('ior', a, b), b), b),
575 (('iand', ('iand', a, b), b), ('iand', a, b)),
576 # DeMorgan's Laws
577 (('iand', ('inot', a), ('inot', b)), ('inot', ('ior', a, b))),
578 (('ior', ('inot', a), ('inot', b)), ('inot', ('iand', a, b))),
579 # Shift optimizations
580 (('ishl', 0, a), 0),
581 (('ishl', a, 0), a),
582 (('ishr', 0, a), 0),
583 (('ishr', a, 0), a),
584 (('ushr', 0, a), 0),
585 (('ushr', a, 0), a),
586 (('iand', 0xff, ('ushr@32', a, 24)), ('ushr', a, 24)),
587 (('iand', 0xffff, ('ushr@32', a, 16)), ('ushr', a, 16)),
588 # Exponential/logarithmic identities
589 (('~fexp2', ('flog2', a)), a), # 2^lg2(a) = a
590 (('~flog2', ('fexp2', a)), a), # lg2(2^a) = a
591 (('fpow', a, b), ('fexp2', ('fmul', ('flog2', a), b)), 'options->lower_fpow'), # a^b = 2^(lg2(a)*b)
592 (('~fexp2', ('fmul', ('flog2', a), b)), ('fpow', a, b), '!options->lower_fpow'), # 2^(lg2(a)*b) = a^b
593 (('~fexp2', ('fadd', ('fmul', ('flog2', a), b), ('fmul', ('flog2', c), d))),
594 ('~fmul', ('fpow', a, b), ('fpow', c, d)), '!options->lower_fpow'), # 2^(lg2(a) * b + lg2(c) + d) = a^b * c^d
595 (('~fexp2', ('fmul', ('flog2', a), 2.0)), ('fmul', a, a)),
596 (('~fexp2', ('fmul', ('flog2', a), 4.0)), ('fmul', ('fmul', a, a), ('fmul', a, a))),
597 (('~fpow', a, 1.0), a),
598 (('~fpow', a, 2.0), ('fmul', a, a)),
599 (('~fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),
600 (('~fpow', 2.0, a), ('fexp2', a)),
601 (('~fpow', ('fpow', a, 2.2), 0.454545), a),
602 (('~fpow', ('fabs', ('fpow', a, 2.2)), 0.454545), ('fabs', a)),
603 (('~fsqrt', ('fexp2', a)), ('fexp2', ('fmul', 0.5, a))),
604 (('~frcp', ('fexp2', a)), ('fexp2', ('fneg', a))),
605 (('~frsq', ('fexp2', a)), ('fexp2', ('fmul', -0.5, a))),
606 (('~flog2', ('fsqrt', a)), ('fmul', 0.5, ('flog2', a))),
607 (('~flog2', ('frcp', a)), ('fneg', ('flog2', a))),
608 (('~flog2', ('frsq', a)), ('fmul', -0.5, ('flog2', a))),
609 (('~flog2', ('fpow', a, b)), ('fmul', b, ('flog2', a))),
610 (('~fmul', ('fexp2(is_used_once)', a), ('fexp2(is_used_once)', b)), ('fexp2', ('fadd', a, b))),
611 (('bcsel', ('flt', a, 0.0), 0.0, ('fsqrt', a)), ('fsqrt', ('fmax', a, 0.0))),
612 # Division and reciprocal
613 (('~fdiv', 1.0, a), ('frcp', a)),
614 (('fdiv', a, b), ('fmul', a, ('frcp', b)), 'options->lower_fdiv'),
615 (('~frcp', ('frcp', a)), a),
616 (('~frcp', ('fsqrt', a)), ('frsq', a)),
617 (('fsqrt', a), ('frcp', ('frsq', a)), 'options->lower_fsqrt'),
618 (('~frcp', ('frsq', a)), ('fsqrt', a), '!options->lower_fsqrt'),
619 # Boolean simplifications
620 (('i2b32(is_used_by_if)', a), ('ine32', a, 0)),
621 (('i2b1(is_used_by_if)', a), ('ine', a, 0)),
622 (('ieq', a, True), a),
623 (('ine(is_not_used_by_if)', a, True), ('inot', a)),
624 (('ine', a, False), a),
625 (('ieq(is_not_used_by_if)', a, False), ('inot', 'a')),
626 (('bcsel', a, True, False), a),
627 (('bcsel', a, False, True), ('inot', a)),
628 (('bcsel@32', a, 1.0, 0.0), ('b2f', a)),
629 (('bcsel@32', a, 0.0, 1.0), ('b2f', ('inot', a))),
630 (('bcsel@32', a, -1.0, -0.0), ('fneg', ('b2f', a))),
631 (('bcsel@32', a, -0.0, -1.0), ('fneg', ('b2f', ('inot', a)))),
632 (('bcsel', True, b, c), b),
633 (('bcsel', False, b, c), c),
634 (('bcsel', a, ('b2f(is_used_once)', 'b@32'), ('b2f', 'c@32')), ('b2f', ('bcsel', a, b, c))),
635
636 (('bcsel', a, b, b), b),
637 (('fcsel', a, b, b), b),
638
639 # D3D Boolean emulation
640 (('bcsel', a, -1, 0), ('ineg', ('b2i', 'a@1'))),
641 (('bcsel', a, 0, -1), ('ineg', ('b2i', ('inot', a)))),
642 (('iand', ('ineg', ('b2i', 'a@1')), ('ineg', ('b2i', 'b@1'))),
643 ('ineg', ('b2i', ('iand', a, b)))),
644 (('ior', ('ineg', ('b2i','a@1')), ('ineg', ('b2i', 'b@1'))),
645 ('ineg', ('b2i', ('ior', a, b)))),
646 (('ieq', ('ineg', ('b2i', 'a@1')), 0), ('inot', a)),
647 (('ieq', ('ineg', ('b2i', 'a@1')), -1), a),
648 (('ine', ('ineg', ('b2i', 'a@1')), 0), a),
649 (('ine', ('ineg', ('b2i', 'a@1')), -1), ('inot', a)),
650 (('iand', ('ineg', ('b2i', a)), 1.0), ('b2f', a)),
651
652 # SM5 32-bit shifts are defined to use the 5 least significant bits
653 (('ishl', 'a@32', ('iand', 31, b)), ('ishl', a, b)),
654 (('ishr', 'a@32', ('iand', 31, b)), ('ishr', a, b)),
655 (('ushr', 'a@32', ('iand', 31, b)), ('ushr', a, b)),
656
657 # Conversions
658 (('i2b32', ('b2i', 'a@32')), a),
659 (('f2i', ('ftrunc', a)), ('f2i', a)),
660 (('f2u', ('ftrunc', a)), ('f2u', a)),
661 (('i2b', ('ineg', a)), ('i2b', a)),
662 (('i2b', ('iabs', a)), ('i2b', a)),
663 (('fabs', ('b2f', a)), ('b2f', a)),
664 (('iabs', ('b2i', a)), ('b2i', a)),
665 (('inot', ('f2b1', a)), ('feq', a, 0.0)),
666
667 # Ironically, mark these as imprecise because removing the conversions may
668 # preserve more precision than doing the conversions (e.g.,
669 # uint(float(0x81818181u)) == 0x81818200).
670 (('~f2i32', ('i2f', 'a@32')), a),
671 (('~f2i32', ('u2f', 'a@32')), a),
672 (('~f2u32', ('i2f', 'a@32')), a),
673 (('~f2u32', ('u2f', 'a@32')), a),
674
675 # Section 5.4.1 (Conversion and Scalar Constructors) of the GLSL 4.60 spec
676 # says:
677 #
678 # It is undefined to convert a negative floating-point value to an
679 # uint.
680 #
681 # Assuming that (uint)some_float behaves like (uint)(int)some_float allows
682 # some optimizations in the i965 backend to proceed.
683 (('ige', ('f2u', a), b), ('ige', ('f2i', a), b)),
684 (('ige', b, ('f2u', a)), ('ige', b, ('f2i', a))),
685 (('ilt', ('f2u', a), b), ('ilt', ('f2i', a), b)),
686 (('ilt', b, ('f2u', a)), ('ilt', b, ('f2i', a))),
687
688 (('~fmin', ('fabs', a), 1.0), ('fsat', ('fabs', a)), '!options->lower_fsat'),
689
690 # The result of the multiply must be in [-1, 0], so the result of the ffma
691 # must be in [0, 1].
692 (('flt', ('fadd', ('fmul', ('fsat', a), ('fneg', ('fsat', a))), 1.0), 0.0), False),
693 (('flt', ('fadd', ('fneg', ('fmul', ('fsat', a), ('fsat', a))), 1.0), 0.0), False),
694 (('fmax', ('fadd', ('fmul', ('fsat', a), ('fneg', ('fsat', a))), 1.0), 0.0), ('fadd', ('fmul', ('fsat', a), ('fneg', ('fsat', a))), 1.0)),
695 (('fmax', ('fadd', ('fneg', ('fmul', ('fsat', a), ('fsat', a))), 1.0), 0.0), ('fadd', ('fneg', ('fmul', ('fsat', a), ('fsat', a))), 1.0)),
696
697 # Packing and then unpacking does nothing
698 (('unpack_64_2x32_split_x', ('pack_64_2x32_split', a, b)), a),
699 (('unpack_64_2x32_split_y', ('pack_64_2x32_split', a, b)), b),
700 (('pack_64_2x32_split', ('unpack_64_2x32_split_x', a),
701 ('unpack_64_2x32_split_y', a)), a),
702
703 # Byte extraction
704 (('ushr', 'a@16', 8), ('extract_u8', a, 1), '!options->lower_extract_byte'),
705 (('ushr', 'a@32', 24), ('extract_u8', a, 3), '!options->lower_extract_byte'),
706 (('ushr', 'a@64', 56), ('extract_u8', a, 7), '!options->lower_extract_byte'),
707 (('ishr', 'a@16', 8), ('extract_i8', a, 1), '!options->lower_extract_byte'),
708 (('ishr', 'a@32', 24), ('extract_i8', a, 3), '!options->lower_extract_byte'),
709 (('ishr', 'a@64', 56), ('extract_i8', a, 7), '!options->lower_extract_byte'),
710 (('iand', 0xff, a), ('extract_u8', a, 0), '!options->lower_extract_byte')
711 ]
712
713 # After the ('extract_u8', a, 0) pattern, above, triggers, there will be
714 # patterns like those below.
715 for op in ('ushr', 'ishr'):
716 optimizations.extend([(('extract_u8', (op, 'a@16', 8), 0), ('extract_u8', a, 1))])
717 optimizations.extend([(('extract_u8', (op, 'a@32', 8 * i), 0), ('extract_u8', a, i)) for i in range(1, 4)])
718 optimizations.extend([(('extract_u8', (op, 'a@64', 8 * i), 0), ('extract_u8', a, i)) for i in range(1, 8)])
719
720 optimizations.extend([(('extract_u8', ('extract_u16', a, 1), 0), ('extract_u8', a, 2))])
721
722 # After the ('extract_[iu]8', a, 3) patterns, above, trigger, there will be
723 # patterns like those below.
724 for op in ('extract_u8', 'extract_i8'):
725 optimizations.extend([((op, ('ishl', 'a@16', 8), 1), (op, a, 0))])
726 optimizations.extend([((op, ('ishl', 'a@32', 24 - 8 * i), 3), (op, a, i)) for i in range(2, -1, -1)])
727 optimizations.extend([((op, ('ishl', 'a@64', 56 - 8 * i), 7), (op, a, i)) for i in range(6, -1, -1)])
728
729 optimizations.extend([
730 # Word extraction
731 (('ushr', ('ishl', 'a@32', 16), 16), ('extract_u16', a, 0), '!options->lower_extract_word'),
732 (('ushr', 'a@32', 16), ('extract_u16', a, 1), '!options->lower_extract_word'),
733 (('ishr', ('ishl', 'a@32', 16), 16), ('extract_i16', a, 0), '!options->lower_extract_word'),
734 (('ishr', 'a@32', 16), ('extract_i16', a, 1), '!options->lower_extract_word'),
735 (('iand', 0xffff, a), ('extract_u16', a, 0), '!options->lower_extract_word'),
736
737 # Subtracts
738 (('~fsub', a, ('fsub', 0.0, b)), ('fadd', a, b)),
739 (('isub', a, ('isub', 0, b)), ('iadd', a, b)),
740 (('ussub_4x8', a, 0), a),
741 (('ussub_4x8', a, ~0), 0),
742 (('fsub', a, b), ('fadd', a, ('fneg', b)), 'options->lower_sub'),
743 (('isub', a, b), ('iadd', a, ('ineg', b)), 'options->lower_sub'),
744 (('fneg', a), ('fsub', 0.0, a), 'options->lower_negate'),
745 (('ineg', a), ('isub', 0, a), 'options->lower_negate'),
746 (('~fadd', a, ('fsub', 0.0, b)), ('fsub', a, b)),
747 (('iadd', a, ('isub', 0, b)), ('isub', a, b)),
748 (('fabs', ('fsub', 0.0, a)), ('fabs', a)),
749 (('iabs', ('isub', 0, a)), ('iabs', a)),
750
751 # Propagate negation up multiplication chains
752 (('fmul(is_used_by_non_fsat)', ('fneg', a), b), ('fneg', ('fmul', a, b))),
753 (('imul', ('ineg', a), b), ('ineg', ('imul', a, b))),
754
755 # Propagate constants up multiplication chains
756 (('~fmul(is_used_once)', ('fmul(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('fmul', ('fmul', a, c), b)),
757 (('imul(is_used_once)', ('imul(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('imul', ('imul', a, c), b)),
758 (('~fadd(is_used_once)', ('fadd(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('fadd', ('fadd', a, c), b)),
759 (('iadd(is_used_once)', ('iadd(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('iadd', ('iadd', a, c), b)),
760
761 # Reassociate constants in add/mul chains so they can be folded together.
762 # For now, we mostly only handle cases where the constants are separated by
763 # a single non-constant. We could do better eventually.
764 (('~fmul', '#a', ('fmul', 'b(is_not_const)', '#c')), ('fmul', ('fmul', a, c), b)),
765 (('imul', '#a', ('imul', 'b(is_not_const)', '#c')), ('imul', ('imul', a, c), b)),
766 (('~fadd', '#a', ('fadd', 'b(is_not_const)', '#c')), ('fadd', ('fadd', a, c), b)),
767 (('~fadd', '#a', ('fneg', ('fadd', 'b(is_not_const)', '#c'))), ('fadd', ('fadd', a, ('fneg', c)), ('fneg', b))),
768 (('iadd', '#a', ('iadd', 'b(is_not_const)', '#c')), ('iadd', ('iadd', a, c), b)),
769
770 # Drop mul-div by the same value when there's no wrapping.
771 (('idiv', ('imul(no_signed_wrap)', a, b), b), a),
772
773 # By definition...
774 (('bcsel', ('ige', ('find_lsb', a), 0), ('find_lsb', a), -1), ('find_lsb', a)),
775 (('bcsel', ('ige', ('ifind_msb', a), 0), ('ifind_msb', a), -1), ('ifind_msb', a)),
776 (('bcsel', ('ige', ('ufind_msb', a), 0), ('ufind_msb', a), -1), ('ufind_msb', a)),
777
778 (('bcsel', ('ine', a, 0), ('find_lsb', a), -1), ('find_lsb', a)),
779 (('bcsel', ('ine', a, 0), ('ifind_msb', a), -1), ('ifind_msb', a)),
780 (('bcsel', ('ine', a, 0), ('ufind_msb', a), -1), ('ufind_msb', a)),
781
782 (('bcsel', ('ine', a, -1), ('ifind_msb', a), -1), ('ifind_msb', a)),
783
784 # Misc. lowering
785 (('fmod@16', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod'),
786 (('fmod@32', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod'),
787 (('frem', a, b), ('fsub', a, ('fmul', b, ('ftrunc', ('fdiv', a, b)))), 'options->lower_fmod'),
788 (('uadd_carry@32', a, b), ('b2i', ('ult', ('iadd', a, b), a)), 'options->lower_uadd_carry'),
789 (('usub_borrow@32', a, b), ('b2i', ('ult', a, b)), 'options->lower_usub_borrow'),
790
791 (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
792 ('bcsel', ('ult', 31, 'bits'), 'insert',
793 ('bfi', ('bfm', 'bits', 'offset'), 'insert', 'base')),
794 'options->lower_bitfield_insert'),
795 (('ihadd', a, b), ('iadd', ('iand', a, b), ('ishr', ('ixor', a, b), 1)), 'options->lower_hadd'),
796 (('uhadd', a, b), ('iadd', ('iand', a, b), ('ushr', ('ixor', a, b), 1)), 'options->lower_hadd'),
797 (('irhadd', a, b), ('isub', ('ior', a, b), ('ishr', ('ixor', a, b), 1)), 'options->lower_hadd'),
798 (('urhadd', a, b), ('isub', ('ior', a, b), ('ushr', ('ixor', a, b), 1)), 'options->lower_hadd'),
799 (('uadd_sat', a, b), ('bcsel', ('ult', ('iadd', a, b), a), -1, ('iadd', a, b)), 'options->lower_add_sat'),
800 (('usub_sat', a, b), ('bcsel', ('ult', a, b), 0, ('isub', a, b)), 'options->lower_add_sat'),
801
802 # Alternative lowering that doesn't rely on bfi.
803 (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
804 ('bcsel', ('ult', 31, 'bits'),
805 'insert',
806 (('ior',
807 ('iand', 'base', ('inot', ('ishl', ('isub', ('ishl', 1, 'bits'), 1), 'offset'))),
808 ('iand', ('ishl', 'insert', 'offset'), ('ishl', ('isub', ('ishl', 1, 'bits'), 1), 'offset'))))),
809 'options->lower_bitfield_insert_to_shifts'),
810
811 # Alternative lowering that uses bitfield_select.
812 (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
813 ('bcsel', ('ult', 31, 'bits'), 'insert',
814 ('bitfield_select', ('bfm', 'bits', 'offset'), ('ishl', 'insert', 'offset'), 'base')),
815 'options->lower_bitfield_insert_to_bitfield_select'),
816
817 (('ibitfield_extract', 'value', 'offset', 'bits'),
818 ('bcsel', ('ult', 31, 'bits'), 'value',
819 ('ibfe', 'value', 'offset', 'bits')),
820 'options->lower_bitfield_extract'),
821
822 (('ubitfield_extract', 'value', 'offset', 'bits'),
823 ('bcsel', ('ult', 31, 'bits'), 'value',
824 ('ubfe', 'value', 'offset', 'bits')),
825 'options->lower_bitfield_extract'),
826
827 # Note that these opcodes are defined to only use the five least significant bits of 'offset' and 'bits'
828 (('ubfe', 'value', 'offset', ('iand', 31, 'bits')), ('ubfe', 'value', 'offset', 'bits')),
829 (('ubfe', 'value', ('iand', 31, 'offset'), 'bits'), ('ubfe', 'value', 'offset', 'bits')),
830 (('ibfe', 'value', 'offset', ('iand', 31, 'bits')), ('ibfe', 'value', 'offset', 'bits')),
831 (('ibfe', 'value', ('iand', 31, 'offset'), 'bits'), ('ibfe', 'value', 'offset', 'bits')),
832 (('bfm', 'bits', ('iand', 31, 'offset')), ('bfm', 'bits', 'offset')),
833 (('bfm', ('iand', 31, 'bits'), 'offset'), ('bfm', 'bits', 'offset')),
834
835 (('ibitfield_extract', 'value', 'offset', 'bits'),
836 ('bcsel', ('ieq', 0, 'bits'),
837 0,
838 ('ishr',
839 ('ishl', 'value', ('isub', ('isub', 32, 'bits'), 'offset')),
840 ('isub', 32, 'bits'))),
841 'options->lower_bitfield_extract_to_shifts'),
842
843 (('ubitfield_extract', 'value', 'offset', 'bits'),
844 ('iand',
845 ('ushr', 'value', 'offset'),
846 ('bcsel', ('ieq', 'bits', 32),
847 0xffffffff,
848 ('isub', ('ishl', 1, 'bits'), 1))),
849 'options->lower_bitfield_extract_to_shifts'),
850
851 (('ifind_msb', 'value'),
852 ('ufind_msb', ('bcsel', ('ilt', 'value', 0), ('inot', 'value'), 'value')),
853 'options->lower_ifind_msb'),
854
855 (('find_lsb', 'value'),
856 ('ufind_msb', ('iand', 'value', ('ineg', 'value'))),
857 'options->lower_find_lsb'),
858
859 (('extract_i8', a, 'b@32'),
860 ('ishr', ('ishl', a, ('imul', ('isub', 3, b), 8)), 24),
861 'options->lower_extract_byte'),
862
863 (('extract_u8', a, 'b@32'),
864 ('iand', ('ushr', a, ('imul', b, 8)), 0xff),
865 'options->lower_extract_byte'),
866
867 (('extract_i16', a, 'b@32'),
868 ('ishr', ('ishl', a, ('imul', ('isub', 1, b), 16)), 16),
869 'options->lower_extract_word'),
870
871 (('extract_u16', a, 'b@32'),
872 ('iand', ('ushr', a, ('imul', b, 16)), 0xffff),
873 'options->lower_extract_word'),
874
875 (('pack_unorm_2x16', 'v'),
876 ('pack_uvec2_to_uint',
877 ('f2u32', ('fround_even', ('fmul', ('fsat', 'v'), 65535.0)))),
878 'options->lower_pack_unorm_2x16'),
879
880 (('pack_unorm_4x8', 'v'),
881 ('pack_uvec4_to_uint',
882 ('f2u32', ('fround_even', ('fmul', ('fsat', 'v'), 255.0)))),
883 'options->lower_pack_unorm_4x8'),
884
885 (('pack_snorm_2x16', 'v'),
886 ('pack_uvec2_to_uint',
887 ('f2i32', ('fround_even', ('fmul', ('fmin', 1.0, ('fmax', -1.0, 'v')), 32767.0)))),
888 'options->lower_pack_snorm_2x16'),
889
890 (('pack_snorm_4x8', 'v'),
891 ('pack_uvec4_to_uint',
892 ('f2i32', ('fround_even', ('fmul', ('fmin', 1.0, ('fmax', -1.0, 'v')), 127.0)))),
893 'options->lower_pack_snorm_4x8'),
894
895 (('unpack_unorm_2x16', 'v'),
896 ('fdiv', ('u2f32', ('vec2', ('extract_u16', 'v', 0),
897 ('extract_u16', 'v', 1))),
898 65535.0),
899 'options->lower_unpack_unorm_2x16'),
900
901 (('unpack_unorm_4x8', 'v'),
902 ('fdiv', ('u2f32', ('vec4', ('extract_u8', 'v', 0),
903 ('extract_u8', 'v', 1),
904 ('extract_u8', 'v', 2),
905 ('extract_u8', 'v', 3))),
906 255.0),
907 'options->lower_unpack_unorm_4x8'),
908
909 (('unpack_snorm_2x16', 'v'),
910 ('fmin', 1.0, ('fmax', -1.0, ('fdiv', ('i2f', ('vec2', ('extract_i16', 'v', 0),
911 ('extract_i16', 'v', 1))),
912 32767.0))),
913 'options->lower_unpack_snorm_2x16'),
914
915 (('unpack_snorm_4x8', 'v'),
916 ('fmin', 1.0, ('fmax', -1.0, ('fdiv', ('i2f', ('vec4', ('extract_i8', 'v', 0),
917 ('extract_i8', 'v', 1),
918 ('extract_i8', 'v', 2),
919 ('extract_i8', 'v', 3))),
920 127.0))),
921 'options->lower_unpack_snorm_4x8'),
922
923 (('isign', a), ('imin', ('imax', a, -1), 1), 'options->lower_isign'),
924 (('fsign', a), ('fsub', ('b2f', ('flt', 0.0, a)), ('b2f', ('flt', a, 0.0))), 'options->lower_fsign'),
925 ])
926
927 # bit_size dependent lowerings
928 for bit_size in [8, 16, 32, 64]:
929 # convenience constants
930 intmax = (1 << (bit_size - 1)) - 1
931 intmin = 1 << (bit_size - 1)
932
933 optimizations += [
934 (('iadd_sat@' + str(bit_size), a, b),
935 ('bcsel', ('ige', b, 1), ('bcsel', ('ilt', ('iadd', a, b), a), intmax, ('iadd', a, b)),
936 ('bcsel', ('ilt', a, ('iadd', a, b)), intmin, ('iadd', a, b))), 'options->lower_add_sat'),
937 (('isub_sat@' + str(bit_size), a, b),
938 ('bcsel', ('ilt', b, 0), ('bcsel', ('ilt', ('isub', a, b), a), intmax, ('isub', a, b)),
939 ('bcsel', ('ilt', a, ('isub', a, b)), intmin, ('isub', a, b))), 'options->lower_add_sat'),
940 ]
941
942 invert = OrderedDict([('feq', 'fne'), ('fne', 'feq'), ('fge', 'flt'), ('flt', 'fge')])
943
944 for left, right in itertools.combinations_with_replacement(invert.keys(), 2):
945 optimizations.append((('inot', ('ior(is_used_once)', (left, a, b), (right, c, d))),
946 ('iand', (invert[left], a, b), (invert[right], c, d))))
947 optimizations.append((('inot', ('iand(is_used_once)', (left, a, b), (right, c, d))),
948 ('ior', (invert[left], a, b), (invert[right], c, d))))
949
950 # Optimize x2bN(b2x(x)) -> x
951 for size in type_sizes('bool'):
952 aN = 'a@' + str(size)
953 f2bN = 'f2b' + str(size)
954 i2bN = 'i2b' + str(size)
955 optimizations.append(((f2bN, ('b2f', aN)), a))
956 optimizations.append(((i2bN, ('b2i', aN)), a))
957
958 # Optimize x2yN(b2x(x)) -> b2y
959 for x, y in itertools.product(['f', 'u', 'i'], ['f', 'u', 'i']):
960 if x != 'f' and y != 'f' and x != y:
961 continue
962
963 b2x = 'b2f' if x == 'f' else 'b2i'
964 b2y = 'b2f' if y == 'f' else 'b2i'
965 x2yN = '{}2{}'.format(x, y)
966 optimizations.append(((x2yN, (b2x, a)), (b2y, a)))
967
968 # Optimize away x2xN(a@N)
969 for t in ['int', 'uint', 'float']:
970 for N in type_sizes(t):
971 x2xN = '{0}2{0}{1}'.format(t[0], N)
972 aN = 'a@{0}'.format(N)
973 optimizations.append(((x2xN, aN), a))
974
975 # Optimize x2xN(y2yM(a@P)) -> y2yN(a) for integers
976 # In particular, we can optimize away everything except upcast of downcast and
977 # upcasts where the type differs from the other cast
978 for N, M in itertools.product(type_sizes('uint'), type_sizes('uint')):
979 if N < M:
980 # The outer cast is a down-cast. It doesn't matter what the size of the
981 # argument of the inner cast is because we'll never been in the upcast
982 # of downcast case. Regardless of types, we'll always end up with y2yN
983 # in the end.
984 for x, y in itertools.product(['i', 'u'], ['i', 'u']):
985 x2xN = '{0}2{0}{1}'.format(x, N)
986 y2yM = '{0}2{0}{1}'.format(y, M)
987 y2yN = '{0}2{0}{1}'.format(y, N)
988 optimizations.append(((x2xN, (y2yM, a)), (y2yN, a)))
989 elif N > M:
990 # If the outer cast is an up-cast, we have to be more careful about the
991 # size of the argument of the inner cast and with types. In this case,
992 # the type is always the type of type up-cast which is given by the
993 # outer cast.
994 for P in type_sizes('uint'):
995 # We can't optimize away up-cast of down-cast.
996 if M < P:
997 continue
998
999 # Because we're doing down-cast of down-cast, the types always have
1000 # to match between the two casts
1001 for x in ['i', 'u']:
1002 x2xN = '{0}2{0}{1}'.format(x, N)
1003 x2xM = '{0}2{0}{1}'.format(x, M)
1004 aP = 'a@{0}'.format(P)
1005 optimizations.append(((x2xN, (x2xM, aP)), (x2xN, a)))
1006 else:
1007 # The N == M case is handled by other optimizations
1008 pass
1009
1010 def fexp2i(exp, bits):
1011 # We assume that exp is already in the right range.
1012 if bits == 16:
1013 return ('i2i16', ('ishl', ('iadd', exp, 15), 10))
1014 elif bits == 32:
1015 return ('ishl', ('iadd', exp, 127), 23)
1016 elif bits == 64:
1017 return ('pack_64_2x32_split', 0, ('ishl', ('iadd', exp, 1023), 20))
1018 else:
1019 assert False
1020
1021 def ldexp(f, exp, bits):
1022 # First, we clamp exp to a reasonable range. The maximum possible range
1023 # for a normal exponent is [-126, 127] and, throwing in denormals, you get
1024 # a maximum range of [-149, 127]. This means that we can potentially have
1025 # a swing of +-276. If you start with FLT_MAX, you actually have to do
1026 # ldexp(FLT_MAX, -278) to get it to flush all the way to zero. The GLSL
1027 # spec, on the other hand, only requires that we handle an exponent value
1028 # in the range [-126, 128]. This implementation is *mostly* correct; it
1029 # handles a range on exp of [-252, 254] which allows you to create any
1030 # value (including denorms if the hardware supports it) and to adjust the
1031 # exponent of any normal value to anything you want.
1032 if bits == 16:
1033 exp = ('imin', ('imax', exp, -28), 30)
1034 elif bits == 32:
1035 exp = ('imin', ('imax', exp, -252), 254)
1036 elif bits == 64:
1037 exp = ('imin', ('imax', exp, -2044), 2046)
1038 else:
1039 assert False
1040
1041 # Now we compute two powers of 2, one for exp/2 and one for exp-exp/2.
1042 # (We use ishr which isn't the same for -1, but the -1 case still works
1043 # since we use exp-exp/2 as the second exponent.) While the spec
1044 # technically defines ldexp as f * 2.0^exp, simply multiplying once doesn't
1045 # work with denormals and doesn't allow for the full swing in exponents
1046 # that you can get with normalized values. Instead, we create two powers
1047 # of two and multiply by them each in turn. That way the effective range
1048 # of our exponent is doubled.
1049 pow2_1 = fexp2i(('ishr', exp, 1), bits)
1050 pow2_2 = fexp2i(('isub', exp, ('ishr', exp, 1)), bits)
1051 return ('fmul', ('fmul', f, pow2_1), pow2_2)
1052
1053 optimizations += [
1054 (('ldexp@16', 'x', 'exp'), ldexp('x', 'exp', 16), 'options->lower_ldexp'),
1055 (('ldexp@32', 'x', 'exp'), ldexp('x', 'exp', 32), 'options->lower_ldexp'),
1056 (('ldexp@64', 'x', 'exp'), ldexp('x', 'exp', 64), 'options->lower_ldexp'),
1057 ]
1058
1059 # Unreal Engine 4 demo applications open-codes bitfieldReverse()
1060 def bitfield_reverse(u):
1061 step1 = ('ior', ('ishl', u, 16), ('ushr', u, 16))
1062 step2 = ('ior', ('ishl', ('iand', step1, 0x00ff00ff), 8), ('ushr', ('iand', step1, 0xff00ff00), 8))
1063 step3 = ('ior', ('ishl', ('iand', step2, 0x0f0f0f0f), 4), ('ushr', ('iand', step2, 0xf0f0f0f0), 4))
1064 step4 = ('ior', ('ishl', ('iand', step3, 0x33333333), 2), ('ushr', ('iand', step3, 0xcccccccc), 2))
1065 step5 = ('ior(many-comm-expr)', ('ishl', ('iand', step4, 0x55555555), 1), ('ushr', ('iand', step4, 0xaaaaaaaa), 1))
1066
1067 return step5
1068
1069 optimizations += [(bitfield_reverse('x@32'), ('bitfield_reverse', 'x'))]
1070
1071 # For any float comparison operation, "cmp", if you have "a == a && a cmp b"
1072 # then the "a == a" is redundant because it's equivalent to "a is not NaN"
1073 # and, if a is a NaN then the second comparison will fail anyway.
1074 for op in ['flt', 'fge', 'feq']:
1075 optimizations += [
1076 (('iand', ('feq', a, a), (op, a, b)), (op, a, b)),
1077 (('iand', ('feq', a, a), (op, b, a)), (op, b, a)),
1078 ]
1079
1080 # Add optimizations to handle the case where the result of a ternary is
1081 # compared to a constant. This way we can take things like
1082 #
1083 # (a ? 0 : 1) > 0
1084 #
1085 # and turn it into
1086 #
1087 # a ? (0 > 0) : (1 > 0)
1088 #
1089 # which constant folding will eat for lunch. The resulting ternary will
1090 # further get cleaned up by the boolean reductions above and we will be
1091 # left with just the original variable "a".
1092 for op in ['flt', 'fge', 'feq', 'fne',
1093 'ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']:
1094 optimizations += [
1095 ((op, ('bcsel', 'a', '#b', '#c'), '#d'),
1096 ('bcsel', 'a', (op, 'b', 'd'), (op, 'c', 'd'))),
1097 ((op, '#d', ('bcsel', a, '#b', '#c')),
1098 ('bcsel', 'a', (op, 'd', 'b'), (op, 'd', 'c'))),
1099 ]
1100
1101
1102 # For example, this converts things like
1103 #
1104 # 1 + mix(0, a - 1, condition)
1105 #
1106 # into
1107 #
1108 # mix(1, (a-1)+1, condition)
1109 #
1110 # Other optimizations will rearrange the constants.
1111 for op in ['fadd', 'fmul', 'iadd', 'imul']:
1112 optimizations += [
1113 ((op, ('bcsel(is_used_once)', a, '#b', c), '#d'), ('bcsel', a, (op, b, d), (op, c, d)))
1114 ]
1115
1116 # For derivatives in compute shaders, GLSL_NV_compute_shader_derivatives
1117 # states:
1118 #
1119 # If neither layout qualifier is specified, derivatives in compute shaders
1120 # return zero, which is consistent with the handling of built-in texture
1121 # functions like texture() in GLSL 4.50 compute shaders.
1122 for op in ['fddx', 'fddx_fine', 'fddx_coarse',
1123 'fddy', 'fddy_fine', 'fddy_coarse']:
1124 optimizations += [
1125 ((op, 'a'), 0.0, 'info->stage == MESA_SHADER_COMPUTE && info->cs.derivative_group == DERIVATIVE_GROUP_NONE')
1126 ]
1127
1128 # Some optimizations for ir3-specific instructions.
1129 optimizations += [
1130 # 'al * bl': If either 'al' or 'bl' is zero, return zero.
1131 (('umul_low', '#a(is_lower_half_zero)', 'b'), (0)),
1132 # '(ah * bl) << 16 + c': If either 'ah' or 'bl' is zero, return 'c'.
1133 (('imadsh_mix16', '#a@32(is_lower_half_zero)', 'b@32', 'c@32'), ('c')),
1134 (('imadsh_mix16', 'a@32', '#b@32(is_upper_half_zero)', 'c@32'), ('c')),
1135 ]
1136
1137 # This section contains "late" optimizations that should be run before
1138 # creating ffmas and calling regular optimizations for the final time.
1139 # Optimizations should go here if they help code generation and conflict
1140 # with the regular optimizations.
1141 before_ffma_optimizations = [
1142 # Propagate constants down multiplication chains
1143 (('~fmul(is_used_once)', ('fmul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fmul', ('fmul', a, c), b)),
1144 (('imul(is_used_once)', ('imul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('imul', ('imul', a, c), b)),
1145 (('~fadd(is_used_once)', ('fadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fadd', ('fadd', a, c), b)),
1146 (('iadd(is_used_once)', ('iadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('iadd', ('iadd', a, c), b)),
1147
1148 (('~fadd', ('fmul', a, b), ('fmul', a, c)), ('fmul', a, ('fadd', b, c))),
1149 (('iadd', ('imul', a, b), ('imul', a, c)), ('imul', a, ('iadd', b, c))),
1150 (('~fadd', ('fneg', a), a), 0.0),
1151 (('iadd', ('ineg', a), a), 0),
1152 (('iadd', ('ineg', a), ('iadd', a, b)), b),
1153 (('iadd', a, ('iadd', ('ineg', a), b)), b),
1154 (('~fadd', ('fneg', a), ('fadd', a, b)), b),
1155 (('~fadd', a, ('fadd', ('fneg', a), b)), b),
1156
1157 (('~flrp@32', ('fadd(is_used_once)', a, -1.0), ('fadd(is_used_once)', a, 1.0), d), ('fadd', ('flrp', -1.0, 1.0, d), a)),
1158 (('~flrp@32', ('fadd(is_used_once)', a, 1.0), ('fadd(is_used_once)', a, -1.0), d), ('fadd', ('flrp', 1.0, -1.0, d), a)),
1159 (('~flrp@32', ('fadd(is_used_once)', a, '#b'), ('fadd(is_used_once)', a, '#c'), d), ('fadd', ('fmul', d, ('fadd', c, ('fneg', b))), ('fadd', a, b))),
1160 ]
1161
1162 # This section contains "late" optimizations that should be run after the
1163 # regular optimizations have finished. Optimizations should go here if
1164 # they help code generation but do not necessarily produce code that is
1165 # more easily optimizable.
1166 late_optimizations = [
1167 # Most of these optimizations aren't quite safe when you get infinity or
1168 # Nan involved but the first one should be fine.
1169 (('flt', ('fadd', a, b), 0.0), ('flt', a, ('fneg', b))),
1170 (('flt', ('fneg', ('fadd', a, b)), 0.0), ('flt', ('fneg', a), b)),
1171 (('~fge', ('fadd', a, b), 0.0), ('fge', a, ('fneg', b))),
1172 (('~fge', ('fneg', ('fadd', a, b)), 0.0), ('fge', ('fneg', a), b)),
1173 (('~feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
1174 (('~fne', ('fadd', a, b), 0.0), ('fne', a, ('fneg', b))),
1175
1176 # nir_lower_to_source_mods will collapse this, but its existence during the
1177 # optimization loop can prevent other optimizations.
1178 (('fneg', ('fneg', a)), a),
1179
1180 # These are duplicated from the main optimizations table. The late
1181 # patterns that rearrange expressions like x - .5 < 0 to x < .5 can create
1182 # new patterns like these. The patterns that compare with zero are removed
1183 # because they are unlikely to be created in by anything in
1184 # late_optimizations.
1185 (('flt', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('flt', a, b)),
1186 (('flt', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('flt', b, a)),
1187 (('fge', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fge', a, b)),
1188 (('fge', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('fge', b, a)),
1189 (('feq', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('feq', a, b)),
1190 (('fne', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fne', a, b)),
1191
1192 (('fge', ('fsat(is_used_once)', a), 1.0), ('fge', a, 1.0)),
1193 (('flt', ('fsat(is_used_once)', a), 1.0), ('flt', a, 1.0)),
1194
1195 (('~fge', ('fmin(is_used_once)', ('fadd(is_used_once)', a, b), ('fadd', c, d)), 0.0), ('iand', ('fge', a, ('fneg', b)), ('fge', c, ('fneg', d)))),
1196
1197 (('flt', ('fneg', a), ('fneg', b)), ('flt', b, a)),
1198 (('fge', ('fneg', a), ('fneg', b)), ('fge', b, a)),
1199 (('feq', ('fneg', a), ('fneg', b)), ('feq', b, a)),
1200 (('fne', ('fneg', a), ('fneg', b)), ('fne', b, a)),
1201 (('flt', ('fneg', a), -1.0), ('flt', 1.0, a)),
1202 (('flt', -1.0, ('fneg', a)), ('flt', a, 1.0)),
1203 (('fge', ('fneg', a), -1.0), ('fge', 1.0, a)),
1204 (('fge', -1.0, ('fneg', a)), ('fge', a, 1.0)),
1205 (('fne', ('fneg', a), -1.0), ('fne', 1.0, a)),
1206 (('feq', -1.0, ('fneg', a)), ('feq', a, 1.0)),
1207
1208 (('ior', a, a), a),
1209 (('iand', a, a), a),
1210
1211 (('~fadd', ('fneg(is_used_once)', ('fsat(is_used_once)', 'a(is_not_fmul)')), 1.0), ('fsat', ('fadd', 1.0, ('fneg', a)))),
1212
1213 (('fdot2', a, b), ('fdot_replicated2', a, b), 'options->fdot_replicates'),
1214 (('fdot3', a, b), ('fdot_replicated3', a, b), 'options->fdot_replicates'),
1215 (('fdot4', a, b), ('fdot_replicated4', a, b), 'options->fdot_replicates'),
1216 (('fdph', a, b), ('fdph_replicated', a, b), 'options->fdot_replicates'),
1217
1218 (('~flrp@32', ('fadd(is_used_once)', a, b), ('fadd(is_used_once)', a, c), d), ('fadd', ('flrp', b, c, d), a)),
1219 (('~flrp@64', ('fadd(is_used_once)', a, b), ('fadd(is_used_once)', a, c), d), ('fadd', ('flrp', b, c, d), a)),
1220
1221 (('~fadd@32', 1.0, ('fmul(is_used_once)', c , ('fadd', b, -1.0 ))), ('fadd', ('fadd', 1.0, ('fneg', c)), ('fmul', b, c)), 'options->lower_flrp32'),
1222 (('~fadd@64', 1.0, ('fmul(is_used_once)', c , ('fadd', b, -1.0 ))), ('fadd', ('fadd', 1.0, ('fneg', c)), ('fmul', b, c)), 'options->lower_flrp64'),
1223
1224 # we do these late so that we don't get in the way of creating ffmas
1225 (('fmin', ('fadd(is_used_once)', '#c', a), ('fadd(is_used_once)', '#c', b)), ('fadd', c, ('fmin', a, b))),
1226 (('fmax', ('fadd(is_used_once)', '#c', a), ('fadd(is_used_once)', '#c', b)), ('fadd', c, ('fmax', a, b))),
1227
1228 (('bcsel', a, 0, ('b2f32', ('inot', 'b@bool'))), ('b2f32', ('inot', ('ior', a, b)))),
1229 ]
1230
1231 print(nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render())
1232 print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_before_ffma",
1233 before_ffma_optimizations).render())
1234 print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_late",
1235 late_optimizations).render())