nir/algebraic: Mark other comparison exact when removing a == a
[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 from math import pi
33
34 # Convenience variables
35 a = 'a'
36 b = 'b'
37 c = 'c'
38 d = 'd'
39 e = 'e'
40
41 # Written in the form (<search>, <replace>) where <search> is an expression
42 # and <replace> is either an expression or a value. An expression is
43 # defined as a tuple of the form ([~]<op>, <src0>, <src1>, <src2>, <src3>)
44 # where each source is either an expression or a value. A value can be
45 # either a numeric constant or a string representing a variable name.
46 #
47 # If the opcode in a search expression is prefixed by a '~' character, this
48 # indicates that the operation is inexact. Such operations will only get
49 # applied to SSA values that do not have the exact bit set. This should be
50 # used by by any optimizations that are not bit-for-bit exact. It should not,
51 # however, be used for backend-requested lowering operations as those need to
52 # happen regardless of precision.
53 #
54 # Variable names are specified as "[#]name[@type][(cond)][.swiz]" where:
55 # "#" indicates that the given variable will only match constants,
56 # type indicates that the given variable will only match values from ALU
57 # instructions with the given output type,
58 # (cond) specifies an additional condition function (see nir_search_helpers.h),
59 # swiz is a swizzle applied to the variable (only in the <replace> expression)
60 #
61 # For constants, you have to be careful to make sure that it is the right
62 # type because python is unaware of the source and destination types of the
63 # opcodes.
64 #
65 # All expression types can have a bit-size specified. For opcodes, this
66 # looks like "op@32", for variables it is "a@32" or "a@uint32" to specify a
67 # type and size. In the search half of the expression this indicates that it
68 # should only match that particular bit-size. In the replace half of the
69 # expression this indicates that the constructed value should have that
70 # bit-size.
71 #
72 # If the opcode in a replacement expression is prefixed by a '!' character,
73 # this indicated that the new expression will be marked exact.
74 #
75 # A special condition "many-comm-expr" can be used with expressions to note
76 # that the expression and its subexpressions have more commutative expressions
77 # than nir_replace_instr can handle. If this special condition is needed with
78 # another condition, the two can be separated by a comma (e.g.,
79 # "(many-comm-expr,is_used_once)").
80
81 # based on https://web.archive.org/web/20180105155939/http://forum.devmaster.net/t/fast-and-accurate-sine-cosine/9648
82 def lowered_sincos(c):
83 x = ('fsub', ('fmul', 2.0, ('ffract', ('fadd', ('fmul', 0.5 / pi, a), c))), 1.0)
84 x = ('fmul', ('fsub', x, ('fmul', x, ('fabs', x))), 4.0)
85 return ('ffma', ('ffma', x, ('fabs', x), ('fneg', x)), 0.225, x)
86
87 optimizations = [
88
89 (('imul', a, '#b@32(is_pos_power_of_two)'), ('ishl', a, ('find_lsb', b)), '!options->lower_bitops'),
90 (('imul', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('ishl', a, ('find_lsb', ('iabs', b)))), '!options->lower_bitops'),
91 (('ishl', a, '#b@32'), ('imul', a, ('ishl', 1, b)), 'options->lower_bitops'),
92
93 (('unpack_64_2x32_split_x', ('imul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
94 (('unpack_64_2x32_split_x', ('umul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
95 (('imul_2x32_64', a, b), ('pack_64_2x32_split', ('imul', a, b), ('imul_high', a, b)), 'options->lower_mul_2x32_64'),
96 (('umul_2x32_64', a, b), ('pack_64_2x32_split', ('imul', a, b), ('umul_high', a, b)), 'options->lower_mul_2x32_64'),
97 (('udiv', a, 1), a),
98 (('idiv', a, 1), a),
99 (('umod', a, 1), 0),
100 (('imod', a, 1), 0),
101 (('udiv', a, '#b@32(is_pos_power_of_two)'), ('ushr', a, ('find_lsb', b)), '!options->lower_bitops'),
102 (('idiv', a, '#b@32(is_pos_power_of_two)'), ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', b))), 'options->lower_idiv'),
103 (('idiv', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', ('iabs', b))))), 'options->lower_idiv'),
104 (('umod', a, '#b(is_pos_power_of_two)'), ('iand', a, ('isub', b, 1))),
105
106 (('~fneg', ('fneg', a)), a),
107 (('ineg', ('ineg', a)), a),
108 (('fabs', ('fabs', a)), ('fabs', a)),
109 (('fabs', ('fneg', a)), ('fabs', a)),
110 (('fabs', ('u2f', a)), ('u2f', a)),
111 (('iabs', ('iabs', a)), ('iabs', a)),
112 (('iabs', ('ineg', a)), ('iabs', a)),
113 (('f2b', ('fneg', a)), ('f2b', a)),
114 (('i2b', ('ineg', a)), ('i2b', a)),
115 (('~fadd', a, 0.0), a),
116 (('iadd', a, 0), a),
117 (('usadd_4x8', a, 0), a),
118 (('usadd_4x8', a, ~0), ~0),
119 (('~fadd', ('fmul', a, b), ('fmul', a, c)), ('fmul', a, ('fadd', b, c))),
120 (('iadd', ('imul', a, b), ('imul', a, c)), ('imul', a, ('iadd', b, c))),
121 (('~fadd', ('fneg', a), a), 0.0),
122 (('iadd', ('ineg', a), a), 0),
123 (('iadd', ('ineg', a), ('iadd', a, b)), b),
124 (('iadd', a, ('iadd', ('ineg', a), b)), b),
125 (('~fadd', ('fneg', a), ('fadd', a, b)), b),
126 (('~fadd', a, ('fadd', ('fneg', a), b)), b),
127 (('fadd', ('fsat', a), ('fsat', ('fneg', a))), ('fsat', ('fabs', a))),
128 (('~fmul', a, 0.0), 0.0),
129 (('imul', a, 0), 0),
130 (('umul_unorm_4x8', a, 0), 0),
131 (('umul_unorm_4x8', a, ~0), a),
132 (('~fmul', a, 1.0), a),
133 (('imul', a, 1), a),
134 (('fmul', a, -1.0), ('fneg', a)),
135 (('imul', a, -1), ('ineg', a)),
136 # If a < 0: fsign(a)*a*a => -1*a*a => -a*a => abs(a)*a
137 # If a > 0: fsign(a)*a*a => 1*a*a => a*a => abs(a)*a
138 # If a == 0: fsign(a)*a*a => 0*0*0 => abs(0)*0
139 (('fmul', ('fsign', a), ('fmul', a, a)), ('fmul', ('fabs', a), a)),
140 (('fmul', ('fmul', ('fsign', a), a), a), ('fmul', ('fabs', a), a)),
141 (('~ffma', 0.0, a, b), b),
142 (('~ffma', a, b, 0.0), ('fmul', a, b)),
143 (('ffma', 1.0, a, b), ('fadd', a, b)),
144 (('ffma', -1.0, a, b), ('fadd', ('fneg', a), b)),
145 (('~flrp', a, b, 0.0), a),
146 (('~flrp', a, b, 1.0), b),
147 (('~flrp', a, a, b), a),
148 (('~flrp', 0.0, a, b), ('fmul', a, b)),
149
150 # flrp(a, a + b, c) => a + flrp(0, b, c) => a + (b * c)
151 (('~flrp', a, ('fadd(is_used_once)', a, b), c), ('fadd', ('fmul', b, c), a)),
152 (('~flrp@32', a, ('fadd', a, b), c), ('fadd', ('fmul', b, c), a), 'options->lower_flrp32'),
153 (('~flrp@64', a, ('fadd', a, b), c), ('fadd', ('fmul', b, c), a), 'options->lower_flrp64'),
154
155 (('~flrp@32', ('fadd', a, b), ('fadd', a, c), d), ('fadd', ('flrp', b, c, d), a), 'options->lower_flrp32'),
156 (('~flrp@64', ('fadd', a, b), ('fadd', a, c), d), ('fadd', ('flrp', b, c, d), a), 'options->lower_flrp64'),
157
158 (('~flrp@32', a, ('fmul(is_used_once)', a, b), c), ('fmul', ('flrp', 1.0, b, c), a), 'options->lower_flrp32'),
159 (('~flrp@64', a, ('fmul(is_used_once)', a, b), c), ('fmul', ('flrp', 1.0, b, c), a), 'options->lower_flrp64'),
160
161 (('~flrp', ('fmul(is_used_once)', a, b), ('fmul(is_used_once)', a, c), d), ('fmul', ('flrp', b, c, d), a)),
162
163 (('~flrp', a, b, ('b2f', 'c@1')), ('bcsel', c, b, a), 'options->lower_flrp32'),
164 (('~flrp', a, 0.0, c), ('fadd', ('fmul', ('fneg', a), c), a)),
165 (('ftrunc', a), ('bcsel', ('flt', a, 0.0), ('fneg', ('ffloor', ('fabs', a))), ('ffloor', ('fabs', a))), 'options->lower_ftrunc'),
166 (('ffloor', a), ('fsub', a, ('ffract', a)), 'options->lower_ffloor'),
167 (('fadd', a, ('fneg', ('ffract', a))), ('ffloor', a), '!options->lower_ffloor'),
168 (('ffract', a), ('fsub', a, ('ffloor', a)), 'options->lower_ffract'),
169 (('fceil', a), ('fneg', ('ffloor', ('fneg', a))), 'options->lower_fceil'),
170 (('~fadd', ('fmul', a, ('fadd', 1.0, ('fneg', ('b2f', 'c@1')))), ('fmul', b, ('b2f', c))), ('bcsel', c, b, a), 'options->lower_flrp32'),
171 (('~fadd@32', ('fmul', a, ('fadd', 1.0, ('fneg', c ) )), ('fmul', b, c )), ('flrp', a, b, c), '!options->lower_flrp32'),
172 (('~fadd@64', ('fmul', a, ('fadd', 1.0, ('fneg', c ) )), ('fmul', b, c )), ('flrp', a, b, c), '!options->lower_flrp64'),
173 # These are the same as the previous three rules, but it depends on
174 # 1-fsat(x) <=> fsat(1-x). See below.
175 (('~fadd@32', ('fmul', a, ('fsat', ('fadd', 1.0, ('fneg', c )))), ('fmul', b, ('fsat', c))), ('flrp', a, b, ('fsat', c)), '!options->lower_flrp32'),
176 (('~fadd@64', ('fmul', a, ('fsat', ('fadd', 1.0, ('fneg', c )))), ('fmul', b, ('fsat', c))), ('flrp', a, b, ('fsat', c)), '!options->lower_flrp64'),
177
178 (('~fadd', a, ('fmul', ('b2f', 'c@1'), ('fadd', b, ('fneg', a)))), ('bcsel', c, b, a), 'options->lower_flrp32'),
179 (('~fadd@32', a, ('fmul', c , ('fadd', b, ('fneg', a)))), ('flrp', a, b, c), '!options->lower_flrp32'),
180 (('~fadd@64', a, ('fmul', c , ('fadd', b, ('fneg', a)))), ('flrp', a, b, c), '!options->lower_flrp64'),
181 (('ffma', a, b, c), ('fadd', ('fmul', a, b), c), 'options->lower_ffma'),
182 (('~fadd', ('fmul', a, b), c), ('ffma', a, b, c), 'options->fuse_ffma'),
183
184 (('~fmul', ('fadd', ('iand', ('ineg', ('b2i32', 'a@bool')), ('fmul', b, c)), '#d'), '#e'),
185 ('bcsel', a, ('fmul', ('fadd', ('fmul', b, c), d), e), ('fmul', d, e))),
186
187 (('fdph', a, b), ('fdot4', ('vec4', 'a.x', 'a.y', 'a.z', 1.0), b), 'options->lower_fdph'),
188
189 (('fdot4', ('vec4', a, b, c, 1.0), d), ('fdph', ('vec3', a, b, c), d), '!options->lower_fdph'),
190 (('fdot4', ('vec4', a, 0.0, 0.0, 0.0), b), ('fmul', a, b)),
191 (('fdot4', ('vec4', a, b, 0.0, 0.0), c), ('fdot2', ('vec2', a, b), c)),
192 (('fdot4', ('vec4', a, b, c, 0.0), d), ('fdot3', ('vec3', a, b, c), d)),
193
194 (('fdot3', ('vec3', a, 0.0, 0.0), b), ('fmul', a, b)),
195 (('fdot3', ('vec3', a, b, 0.0), c), ('fdot2', ('vec2', a, b), c)),
196
197 (('fdot2', ('vec2', a, 0.0), b), ('fmul', a, b)),
198 (('fdot2', a, 1.0), ('fadd', 'a.x', 'a.y')),
199
200 # Lower fdot to fsum when it is available
201 (('fdot2', a, b), ('fsum2', ('fmul', a, b)), 'options->lower_fdot'),
202 (('fdot3', a, b), ('fsum3', ('fmul', a, b)), 'options->lower_fdot'),
203 (('fdot4', a, b), ('fsum4', ('fmul', a, b)), 'options->lower_fdot'),
204 (('fsum2', a), ('fadd', 'a.x', 'a.y'), 'options->lower_fdot'),
205
206 # If x >= 0 and x <= 1: fsat(1 - x) == 1 - fsat(x) trivially
207 # If x < 0: 1 - fsat(x) => 1 - 0 => 1 and fsat(1 - x) => fsat(> 1) => 1
208 # If x > 1: 1 - fsat(x) => 1 - 1 => 0 and fsat(1 - x) => fsat(< 0) => 0
209 (('~fadd', ('fneg(is_used_once)', ('fsat(is_used_once)', 'a(is_not_fmul)')), 1.0), ('fsat', ('fadd', 1.0, ('fneg', a)))),
210
211 # 1 - ((1 - a) * (1 - b))
212 # 1 - (1 - a - b + a*b)
213 # 1 - 1 + a + b - a*b
214 # a + b - a*b
215 # a + b*(1 - a)
216 # b*(1 - a) + 1*a
217 # flrp(b, 1, a)
218 (('~fadd@32', 1.0, ('fneg', ('fmul', ('fadd', 1.0, ('fneg', a)), ('fadd', 1.0, ('fneg', b))))),
219 ('flrp', b, 1.0, a), '!options->lower_flrp32'),
220
221 # (a * #b + #c) << #d
222 # ((a * #b) << #d) + (#c << #d)
223 # (a * (#b << #d)) + (#c << #d)
224 (('ishl', ('iadd', ('imul', a, '#b'), '#c'), '#d'),
225 ('iadd', ('imul', a, ('ishl', b, d)), ('ishl', c, d))),
226
227 # (a * #b) << #c
228 # a * (#b << #c)
229 (('ishl', ('imul', a, '#b'), '#c'), ('imul', a, ('ishl', b, c))),
230 ]
231
232 # Care must be taken here. Shifts in NIR uses only the lower log2(bitsize)
233 # bits of the second source. These replacements must correctly handle the
234 # case where (b % bitsize) + (c % bitsize) >= bitsize.
235 for s in [8, 16, 32, 64]:
236 mask = (1 << s) - 1
237
238 ishl = "ishl@{}".format(s)
239 ishr = "ishr@{}".format(s)
240 ushr = "ushr@{}".format(s)
241
242 in_bounds = ('ult', ('iadd', ('iand', b, mask), ('iand', c, mask)), s)
243
244 optimizations.extend([
245 ((ishl, (ishl, a, '#b'), '#c'), ('bcsel', in_bounds, (ishl, a, ('iadd', b, c)), 0)),
246 ((ushr, (ushr, a, '#b'), '#c'), ('bcsel', in_bounds, (ushr, a, ('iadd', b, c)), 0)),
247
248 # To get get -1 for large shifts of negative values, ishr must instead
249 # clamp the shift count to the maximum value.
250 ((ishr, (ishr, a, '#b'), '#c'),
251 (ishr, a, ('imin', ('iadd', ('iand', b, mask), ('iand', c, mask)), s - 1))),
252 ])
253
254 optimizations.extend([
255 # This is common for address calculations. Reassociating may enable the
256 # 'a<<c' to be CSE'd. It also helps architectures that have an ISHLADD
257 # instruction or a constant offset field for in load / store instructions.
258 (('ishl', ('iadd', a, '#b'), '#c'), ('iadd', ('ishl', a, c), ('ishl', b, c))),
259
260 # Comparison simplifications
261 (('~inot', ('flt', a, b)), ('fge', a, b)),
262 (('~inot', ('fge', a, b)), ('flt', a, b)),
263 (('inot', ('feq', a, b)), ('fne', a, b)),
264 (('inot', ('fne', a, b)), ('feq', a, b)),
265 (('inot', ('ilt', a, b)), ('ige', a, b)),
266 (('inot', ('ult', a, b)), ('uge', a, b)),
267 (('inot', ('ige', a, b)), ('ilt', a, b)),
268 (('inot', ('uge', a, b)), ('ult', a, b)),
269 (('inot', ('ieq', a, b)), ('ine', a, b)),
270 (('inot', ('ine', a, b)), ('ieq', a, b)),
271
272 (('iand', ('feq', a, b), ('fne', a, b)), False),
273 (('iand', ('flt', a, b), ('flt', b, a)), False),
274 (('iand', ('ieq', a, b), ('ine', a, b)), False),
275 (('iand', ('ilt', a, b), ('ilt', b, a)), False),
276 (('iand', ('ult', a, b), ('ult', b, a)), False),
277
278 # This helps some shaders because, after some optimizations, they end up
279 # with patterns like (-a < -b) || (b < a). In an ideal world, this sort of
280 # matching would be handled by CSE.
281 (('flt', ('fneg', a), ('fneg', b)), ('flt', b, a)),
282 (('fge', ('fneg', a), ('fneg', b)), ('fge', b, a)),
283 (('feq', ('fneg', a), ('fneg', b)), ('feq', b, a)),
284 (('fne', ('fneg', a), ('fneg', b)), ('fne', b, a)),
285 (('flt', ('fneg', a), -1.0), ('flt', 1.0, a)),
286 (('flt', -1.0, ('fneg', a)), ('flt', a, 1.0)),
287 (('fge', ('fneg', a), -1.0), ('fge', 1.0, a)),
288 (('fge', -1.0, ('fneg', a)), ('fge', a, 1.0)),
289 (('fne', ('fneg', a), -1.0), ('fne', 1.0, a)),
290 (('feq', -1.0, ('fneg', a)), ('feq', a, 1.0)),
291
292 (('flt', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('flt', a, b)),
293 (('flt', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('flt', b, a)),
294 (('fge', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fge', a, b)),
295 (('fge', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('fge', b, a)),
296 (('feq', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('feq', a, b)),
297 (('fne', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fne', a, b)),
298
299 (('fge', ('fsat(is_used_once)', a), 1.0), ('fge', a, 1.0)),
300 (('flt', ('fsat(is_used_once)', a), 1.0), ('flt', a, 1.0)),
301 (('fge', 0.0, ('fsat(is_used_once)', a)), ('fge', 0.0, a)),
302 (('flt', 0.0, ('fsat(is_used_once)', a)), ('flt', 0.0, a)),
303
304 # 0.0 >= b2f(a)
305 # b2f(a) <= 0.0
306 # b2f(a) == 0.0 because b2f(a) can only be 0 or 1
307 # inot(a)
308 (('fge', 0.0, ('b2f', 'a@1')), ('inot', a)),
309
310 (('fge', ('fneg', ('b2f', 'a@1')), 0.0), ('inot', a)),
311
312 (('fne', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('ior', a, b)),
313 (('fne', ('fmax', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('ior', a, b)),
314 (('fne', ('bcsel', a, 1.0, ('b2f', 'b@1')) , 0.0), ('ior', a, b)),
315 (('fne', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), ('ior', a, b)),
316 (('fne', ('fmul', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('iand', a, b)),
317 (('fne', ('fmin', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('iand', a, b)),
318 (('fne', ('bcsel', a, ('b2f', 'b@1'), 0.0) , 0.0), ('iand', a, b)),
319 (('fne', ('fadd', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), 0.0), ('ixor', a, b)),
320 (('fne', ('b2f', 'a@1') , ('b2f', 'b@1') ), ('ixor', a, b)),
321 (('fne', ('fneg', ('b2f', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('ixor', a, b)),
322 (('feq', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('ior', a, b))),
323 (('feq', ('fmax', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('ior', a, b))),
324 (('feq', ('bcsel', a, 1.0, ('b2f', 'b@1')) , 0.0), ('inot', ('ior', a, b))),
325 (('feq', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), ('inot', ('ior', a, b))),
326 (('feq', ('fmul', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('iand', a, b))),
327 (('feq', ('fmin', ('b2f', 'a@1'), ('b2f', 'b@1')), 0.0), ('inot', ('iand', a, b))),
328 (('feq', ('bcsel', a, ('b2f', 'b@1'), 0.0) , 0.0), ('inot', ('iand', a, b))),
329 (('feq', ('fadd', ('b2f', 'a@1'), ('fneg', ('b2f', 'b@1'))), 0.0), ('ieq', a, b)),
330 (('feq', ('b2f', 'a@1') , ('b2f', 'b@1') ), ('ieq', a, b)),
331 (('feq', ('fneg', ('b2f', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('ieq', a, b)),
332
333 # -(b2f(a) + b2f(b)) < 0
334 # 0 < b2f(a) + b2f(b)
335 # 0 != b2f(a) + b2f(b) b2f must be 0 or 1, so the sum is non-negative
336 # a || b
337 (('flt', ('fneg', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), 0.0), ('ior', a, b)),
338 (('flt', 0.0, ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), ('ior', a, b)),
339
340 # -(b2f(a) + b2f(b)) >= 0
341 # 0 >= b2f(a) + b2f(b)
342 # 0 == b2f(a) + b2f(b) b2f must be 0 or 1, so the sum is non-negative
343 # !(a || b)
344 (('fge', ('fneg', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), 0.0), ('inot', ('ior', a, b))),
345 (('fge', 0.0, ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), ('inot', ('ior', a, b))),
346
347 (('flt', a, ('fneg', a)), ('flt', a, 0.0)),
348 (('fge', a, ('fneg', a)), ('fge', a, 0.0)),
349
350 # Some optimizations (below) convert things like (a < b || c < b) into
351 # (min(a, c) < b). However, this interfers with the previous optimizations
352 # that try to remove comparisons with negated sums of b2f. This just
353 # breaks that apart.
354 (('flt', ('fmin', c, ('fneg', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1')))), 0.0),
355 ('ior', ('flt', c, 0.0), ('ior', a, b))),
356
357 (('~flt', ('fadd', a, b), a), ('flt', b, 0.0)),
358 (('~fge', ('fadd', a, b), a), ('fge', b, 0.0)),
359 (('~feq', ('fadd', a, b), a), ('feq', b, 0.0)),
360 (('~fne', ('fadd', a, b), a), ('fne', b, 0.0)),
361 (('~flt', ('fadd(is_used_once)', a, '#b'), '#c'), ('flt', a, ('fadd', c, ('fneg', b)))),
362 (('~flt', ('fneg(is_used_once)', ('fadd(is_used_once)', a, '#b')), '#c'), ('flt', ('fneg', ('fadd', c, b)), a)),
363 (('~fge', ('fadd(is_used_once)', a, '#b'), '#c'), ('fge', a, ('fadd', c, ('fneg', b)))),
364 (('~fge', ('fneg(is_used_once)', ('fadd(is_used_once)', a, '#b')), '#c'), ('fge', ('fneg', ('fadd', c, b)), a)),
365 (('~feq', ('fadd(is_used_once)', a, '#b'), '#c'), ('feq', a, ('fadd', c, ('fneg', b)))),
366 (('~feq', ('fneg(is_used_once)', ('fadd(is_used_once)', a, '#b')), '#c'), ('feq', ('fneg', ('fadd', c, b)), a)),
367 (('~fne', ('fadd(is_used_once)', a, '#b'), '#c'), ('fne', a, ('fadd', c, ('fneg', b)))),
368 (('~fne', ('fneg(is_used_once)', ('fadd(is_used_once)', a, '#b')), '#c'), ('fne', ('fneg', ('fadd', c, b)), a)),
369
370 # Cannot remove the addition from ilt or ige due to overflow.
371 (('ieq', ('iadd', a, b), a), ('ieq', b, 0)),
372 (('ine', ('iadd', a, b), a), ('ine', b, 0)),
373
374 # fmin(-b2f(a), b) >= 0.0
375 # -b2f(a) >= 0.0 && b >= 0.0
376 # -b2f(a) == 0.0 && b >= 0.0 -b2f can only be 0 or -1, never >0
377 # b2f(a) == 0.0 && b >= 0.0
378 # a == False && b >= 0.0
379 # !a && b >= 0.0
380 #
381 # The fge in the second replacement is not a typo. I leave the proof that
382 # "fmin(-b2f(a), b) >= 0 <=> fmin(-b2f(a), b) == 0" as an exercise for the
383 # reader.
384 (('fge', ('fmin', ('fneg', ('b2f', 'a@1')), 'b@1'), 0.0), ('iand', ('inot', a), ('fge', b, 0.0))),
385 (('feq', ('fmin', ('fneg', ('b2f', 'a@1')), 'b@1'), 0.0), ('iand', ('inot', a), ('fge', b, 0.0))),
386
387 (('feq', ('b2f', 'a@1'), 0.0), ('inot', a)),
388 (('~fne', ('b2f', 'a@1'), 0.0), a),
389 (('ieq', ('b2i', 'a@1'), 0), ('inot', a)),
390 (('ine', ('b2i', 'a@1'), 0), a),
391
392 (('fne', ('u2f', a), 0.0), ('ine', a, 0)),
393 (('feq', ('u2f', a), 0.0), ('ieq', a, 0)),
394 (('fge', ('u2f', a), 0.0), True),
395 (('fge', 0.0, ('u2f', a)), ('uge', 0, a)), # ieq instead?
396 (('flt', ('u2f', a), 0.0), False),
397 (('flt', 0.0, ('u2f', a)), ('ult', 0, a)), # ine instead?
398 (('fne', ('i2f', a), 0.0), ('ine', a, 0)),
399 (('feq', ('i2f', a), 0.0), ('ieq', a, 0)),
400 (('fge', ('i2f', a), 0.0), ('ige', a, 0)),
401 (('fge', 0.0, ('i2f', a)), ('ige', 0, a)),
402 (('flt', ('i2f', a), 0.0), ('ilt', a, 0)),
403 (('flt', 0.0, ('i2f', a)), ('ilt', 0, a)),
404
405 # 0.0 < fabs(a)
406 # fabs(a) > 0.0
407 # fabs(a) != 0.0 because fabs(a) must be >= 0
408 # a != 0.0
409 (('~flt', 0.0, ('fabs', a)), ('fne', a, 0.0)),
410
411 # -fabs(a) < 0.0
412 # fabs(a) > 0.0
413 (('~flt', ('fneg', ('fabs', a)), 0.0), ('fne', a, 0.0)),
414
415 # 0.0 >= fabs(a)
416 # 0.0 == fabs(a) because fabs(a) must be >= 0
417 # 0.0 == a
418 (('fge', 0.0, ('fabs', a)), ('feq', a, 0.0)),
419
420 # -fabs(a) >= 0.0
421 # 0.0 >= fabs(a)
422 (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
423
424 # (a >= 0.0) && (a <= 1.0) -> fsat(a) == a
425 (('iand', ('fge', a, 0.0), ('fge', 1.0, a)), ('feq', a, ('fsat', a)), '!options->lower_fsat'),
426
427 # (a < 0.0) || (a > 1.0)
428 # !(!(a < 0.0) && !(a > 1.0))
429 # !((a >= 0.0) && (a <= 1.0))
430 # !(a == fsat(a))
431 # a != fsat(a)
432 (('ior', ('flt', a, 0.0), ('flt', 1.0, a)), ('fne', a, ('fsat', a)), '!options->lower_fsat'),
433
434 (('fmax', ('b2f(is_used_once)', 'a@1'), ('b2f', 'b@1')), ('b2f', ('ior', a, b))),
435 (('fmax', ('fneg(is_used_once)', ('b2f(is_used_once)', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('fneg', ('b2f', ('ior', a, b)))),
436 (('fmin', ('b2f(is_used_once)', 'a@1'), ('b2f', 'b@1')), ('b2f', ('iand', a, b))),
437 (('fmin', ('fneg(is_used_once)', ('b2f(is_used_once)', 'a@1')), ('fneg', ('b2f', 'b@1'))), ('fneg', ('b2f', ('iand', a, b)))),
438
439 # fmin(b2f(a), b)
440 # bcsel(a, fmin(b2f(a), b), fmin(b2f(a), b))
441 # bcsel(a, fmin(b2f(True), b), fmin(b2f(False), b))
442 # bcsel(a, fmin(1.0, b), fmin(0.0, b))
443 #
444 # Since b is a constant, constant folding will eliminate the fmin and the
445 # fmax. If b is > 1.0, the bcsel will be replaced with a b2f.
446 (('fmin', ('b2f', 'a@1'), '#b'), ('bcsel', a, ('fmin', b, 1.0), ('fmin', b, 0.0))),
447
448 (('flt', ('fadd(is_used_once)', a, ('fneg', b)), 0.0), ('flt', a, b)),
449
450 (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
451 (('~bcsel', ('flt', b, a), b, a), ('fmin', a, b)),
452 (('~bcsel', ('flt', a, b), b, a), ('fmax', a, b)),
453 (('~bcsel', ('fge', a, b), b, a), ('fmin', a, b)),
454 (('~bcsel', ('fge', b, a), b, a), ('fmax', a, b)),
455 (('bcsel', ('i2b', a), b, c), ('bcsel', ('ine', a, 0), b, c)),
456 (('bcsel', ('inot', a), b, c), ('bcsel', a, c, b)),
457 (('bcsel', a, ('bcsel', a, b, c), d), ('bcsel', a, b, d)),
458 (('bcsel', a, b, ('bcsel', a, c, d)), ('bcsel', a, b, d)),
459 (('bcsel', a, ('bcsel', b, c, d), ('bcsel(is_used_once)', b, c, 'e')), ('bcsel', b, c, ('bcsel', a, d, 'e'))),
460 (('bcsel', a, ('bcsel(is_used_once)', b, c, d), ('bcsel', b, c, 'e')), ('bcsel', b, c, ('bcsel', a, d, 'e'))),
461 (('bcsel', a, ('bcsel', b, c, d), ('bcsel(is_used_once)', b, 'e', d)), ('bcsel', b, ('bcsel', a, c, 'e'), d)),
462 (('bcsel', a, ('bcsel(is_used_once)', b, c, d), ('bcsel', b, 'e', d)), ('bcsel', b, ('bcsel', a, c, 'e'), d)),
463 (('bcsel', a, True, b), ('ior', a, b)),
464 (('bcsel', a, a, b), ('ior', a, b)),
465 (('bcsel', a, b, False), ('iand', a, b)),
466 (('bcsel', a, b, a), ('iand', a, b)),
467 (('~fmin', a, a), a),
468 (('~fmax', a, a), a),
469 (('imin', a, a), a),
470 (('imax', a, a), a),
471 (('umin', a, a), a),
472 (('umax', a, a), a),
473 (('fmax', ('fmax', a, b), b), ('fmax', a, b)),
474 (('umax', ('umax', a, b), b), ('umax', a, b)),
475 (('imax', ('imax', a, b), b), ('imax', a, b)),
476 (('fmin', ('fmin', a, b), b), ('fmin', a, b)),
477 (('umin', ('umin', a, b), b), ('umin', a, b)),
478 (('imin', ('imin', a, b), b), ('imin', a, b)),
479 (('fmax', a, ('fneg', a)), ('fabs', a)),
480 (('imax', a, ('ineg', a)), ('iabs', a)),
481 (('fmin', a, ('fneg', a)), ('fneg', ('fabs', a))),
482 (('imin', a, ('ineg', a)), ('ineg', ('iabs', a))),
483 (('fmin', a, ('fneg', ('fabs', a))), ('fneg', ('fabs', a))),
484 (('imin', a, ('ineg', ('iabs', a))), ('ineg', ('iabs', a))),
485 (('~fmin', a, ('fabs', a)), a),
486 (('imin', a, ('iabs', a)), a),
487 (('~fmax', a, ('fneg', ('fabs', a))), a),
488 (('imax', a, ('ineg', ('iabs', a))), a),
489 (('fmax', a, ('fabs', a)), ('fabs', a)),
490 (('imax', a, ('iabs', a)), ('iabs', a)),
491 (('fmax', a, ('fneg', a)), ('fabs', a)),
492 (('imax', a, ('ineg', a)), ('iabs', a)),
493 (('~fmax', ('fabs', a), 0.0), ('fabs', a)),
494 (('~fmin', ('fmax', a, 0.0), 1.0), ('fsat', a), '!options->lower_fsat'),
495 (('~fmax', ('fmin', a, 1.0), 0.0), ('fsat', a), '!options->lower_fsat'),
496 (('~fmin', ('fmax', a, -1.0), 0.0), ('fneg', ('fsat', ('fneg', a))), '!options->lower_fsat'),
497 (('~fmax', ('fmin', a, 0.0), -1.0), ('fneg', ('fsat', ('fneg', a))), '!options->lower_fsat'),
498 (('fsat', ('fsign', a)), ('b2f', ('flt', 0.0, a))),
499 (('fsat', ('b2f', a)), ('b2f', a)),
500 (('fsat', a), ('fmin', ('fmax', a, 0.0), 1.0), 'options->lower_fsat'),
501 (('fsat', ('fsat', a)), ('fsat', a)),
502 (('fsat', ('fneg(is_used_once)', ('fadd(is_used_once)', a, b))), ('fsat', ('fadd', ('fneg', a), ('fneg', b))), '!options->lower_fsat'),
503 (('fsat', ('fneg(is_used_once)', ('fmul(is_used_once)', a, b))), ('fsat', ('fmul', ('fneg', a), b)), '!options->lower_fsat'),
504 (('fsat', ('fabs(is_used_once)', ('fmul(is_used_once)', a, b))), ('fsat', ('fmul', ('fabs', a), ('fabs', b))), '!options->lower_fsat'),
505 (('fmin', ('fmax', ('fmin', ('fmax', a, b), c), b), c), ('fmin', ('fmax', a, b), c)),
506 (('imin', ('imax', ('imin', ('imax', a, b), c), b), c), ('imin', ('imax', a, b), c)),
507 (('umin', ('umax', ('umin', ('umax', a, b), c), b), c), ('umin', ('umax', a, b), c)),
508 (('fmax', ('fsat', a), '#b@32(is_zero_to_one)'), ('fsat', ('fmax', a, b))),
509 (('fmin', ('fsat', a), '#b@32(is_zero_to_one)'), ('fsat', ('fmin', a, b))),
510 (('extract_u8', ('imin', ('imax', a, 0), 0xff), 0), ('imin', ('imax', a, 0), 0xff)),
511 (('~ior', ('flt(is_used_once)', a, b), ('flt', a, c)), ('flt', a, ('fmax', b, c))),
512 (('~ior', ('flt(is_used_once)', a, c), ('flt', b, c)), ('flt', ('fmin', a, b), c)),
513 (('~ior', ('fge(is_used_once)', a, b), ('fge', a, c)), ('fge', a, ('fmin', b, c))),
514 (('~ior', ('fge(is_used_once)', a, c), ('fge', b, c)), ('fge', ('fmax', a, b), c)),
515 (('~ior', ('flt', a, '#b'), ('flt', a, '#c')), ('flt', a, ('fmax', b, c))),
516 (('~ior', ('flt', '#a', c), ('flt', '#b', c)), ('flt', ('fmin', a, b), c)),
517 (('~ior', ('fge', a, '#b'), ('fge', a, '#c')), ('fge', a, ('fmin', b, c))),
518 (('~ior', ('fge', '#a', c), ('fge', '#b', c)), ('fge', ('fmax', a, b), c)),
519 (('~iand', ('flt(is_used_once)', a, b), ('flt', a, c)), ('flt', a, ('fmin', b, c))),
520 (('~iand', ('flt(is_used_once)', a, c), ('flt', b, c)), ('flt', ('fmax', a, b), c)),
521 (('~iand', ('fge(is_used_once)', a, b), ('fge', a, c)), ('fge', a, ('fmax', b, c))),
522 (('~iand', ('fge(is_used_once)', a, c), ('fge', b, c)), ('fge', ('fmin', a, b), c)),
523 (('~iand', ('flt', a, '#b'), ('flt', a, '#c')), ('flt', a, ('fmin', b, c))),
524 (('~iand', ('flt', '#a', c), ('flt', '#b', c)), ('flt', ('fmax', a, b), c)),
525 (('~iand', ('fge', a, '#b'), ('fge', a, '#c')), ('fge', a, ('fmax', b, c))),
526 (('~iand', ('fge', '#a', c), ('fge', '#b', c)), ('fge', ('fmin', a, b), c)),
527
528 (('ior', ('ilt(is_used_once)', a, b), ('ilt', a, c)), ('ilt', a, ('imax', b, c))),
529 (('ior', ('ilt(is_used_once)', a, c), ('ilt', b, c)), ('ilt', ('imin', a, b), c)),
530 (('ior', ('ige(is_used_once)', a, b), ('ige', a, c)), ('ige', a, ('imin', b, c))),
531 (('ior', ('ige(is_used_once)', a, c), ('ige', b, c)), ('ige', ('imax', a, b), c)),
532 (('ior', ('ult(is_used_once)', a, b), ('ult', a, c)), ('ult', a, ('umax', b, c))),
533 (('ior', ('ult(is_used_once)', a, c), ('ult', b, c)), ('ult', ('umin', a, b), c)),
534 (('ior', ('uge(is_used_once)', a, b), ('uge', a, c)), ('uge', a, ('umin', b, c))),
535 (('ior', ('uge(is_used_once)', a, c), ('uge', b, c)), ('uge', ('umax', a, b), c)),
536 (('iand', ('ilt(is_used_once)', a, b), ('ilt', a, c)), ('ilt', a, ('imin', b, c))),
537 (('iand', ('ilt(is_used_once)', a, c), ('ilt', b, c)), ('ilt', ('imax', a, b), c)),
538 (('iand', ('ige(is_used_once)', a, b), ('ige', a, c)), ('ige', a, ('imax', b, c))),
539 (('iand', ('ige(is_used_once)', a, c), ('ige', b, c)), ('ige', ('imin', a, b), c)),
540 (('iand', ('ult(is_used_once)', a, b), ('ult', a, c)), ('ult', a, ('umin', b, c))),
541 (('iand', ('ult(is_used_once)', a, c), ('ult', b, c)), ('ult', ('umax', a, b), c)),
542 (('iand', ('uge(is_used_once)', a, b), ('uge', a, c)), ('uge', a, ('umax', b, c))),
543 (('iand', ('uge(is_used_once)', a, c), ('uge', b, c)), ('uge', ('umin', a, b), c)),
544
545 # These derive from the previous patterns with the application of b < 0 <=>
546 # 0 < -b. The transformation should be applied if either comparison is
547 # used once as this ensures that the number of comparisons will not
548 # increase. The sources to the ior and iand are not symmetric, so the
549 # rules have to be duplicated to get this behavior.
550 (('~ior', ('flt(is_used_once)', 0.0, 'a@32'), ('flt', 'b@32', 0.0)), ('flt', 0.0, ('fmax', a, ('fneg', b)))),
551 (('~ior', ('flt', 0.0, 'a@32'), ('flt(is_used_once)', 'b@32', 0.0)), ('flt', 0.0, ('fmax', a, ('fneg', b)))),
552 (('~ior', ('fge(is_used_once)', 0.0, 'a@32'), ('fge', 'b@32', 0.0)), ('fge', 0.0, ('fmin', a, ('fneg', b)))),
553 (('~ior', ('fge', 0.0, 'a@32'), ('fge(is_used_once)', 'b@32', 0.0)), ('fge', 0.0, ('fmin', a, ('fneg', b)))),
554 (('~iand', ('flt(is_used_once)', 0.0, 'a@32'), ('flt', 'b@32', 0.0)), ('flt', 0.0, ('fmin', a, ('fneg', b)))),
555 (('~iand', ('flt', 0.0, 'a@32'), ('flt(is_used_once)', 'b@32', 0.0)), ('flt', 0.0, ('fmin', a, ('fneg', b)))),
556 (('~iand', ('fge(is_used_once)', 0.0, 'a@32'), ('fge', 'b@32', 0.0)), ('fge', 0.0, ('fmax', a, ('fneg', b)))),
557 (('~iand', ('fge', 0.0, 'a@32'), ('fge(is_used_once)', 'b@32', 0.0)), ('fge', 0.0, ('fmax', a, ('fneg', b)))),
558
559 # Common pattern like 'if (i == 0 || i == 1 || ...)'
560 (('ior', ('ieq', a, 0), ('ieq', a, 1)), ('uge', 1, a)),
561 (('ior', ('uge', 1, a), ('ieq', a, 2)), ('uge', 2, a)),
562 (('ior', ('uge', 2, a), ('ieq', a, 3)), ('uge', 3, a)),
563
564 # The (i2f32, ...) part is an open-coded fsign. When that is combined with
565 # the bcsel, it's basically copysign(1.0, a). There is no copysign in NIR,
566 # so emit an open-coded version of that.
567 (('bcsel@32', ('feq', a, 0.0), 1.0, ('i2f32', ('iadd', ('b2i32', ('flt', 0.0, 'a@32')), ('ineg', ('b2i32', ('flt', 'a@32', 0.0)))))),
568 ('ior', 0x3f800000, ('iand', a, 0x80000000))),
569
570 (('ior', a, ('ieq', a, False)), True),
571 (('ior', a, ('inot', a)), -1),
572
573 (('ine', ('ineg', ('b2i32', 'a@1')), ('ineg', ('b2i32', 'b@1'))), ('ine', a, b)),
574 (('b2i32', ('ine', 'a@1', 'b@1')), ('b2i32', ('ixor', a, b))),
575
576 (('iand', ('ieq', 'a@32', 0), ('ieq', 'b@32', 0)), ('ieq', ('ior', 'a@32', 'b@32'), 0), '!options->lower_bitops'),
577
578 # These patterns can result when (a < b || a < c) => (a < min(b, c))
579 # transformations occur before constant propagation and loop-unrolling.
580 (('~flt', a, ('fmax', b, a)), ('flt', a, b)),
581 (('~flt', ('fmin', a, b), a), ('flt', b, a)),
582 (('~fge', a, ('fmin', b, a)), True),
583 (('~fge', ('fmax', a, b), a), True),
584 (('~flt', a, ('fmin', b, a)), False),
585 (('~flt', ('fmax', a, b), a), False),
586 (('~fge', a, ('fmax', b, a)), ('fge', a, b)),
587 (('~fge', ('fmin', a, b), a), ('fge', b, a)),
588
589 (('ilt', a, ('imax', b, a)), ('ilt', a, b)),
590 (('ilt', ('imin', a, b), a), ('ilt', b, a)),
591 (('ige', a, ('imin', b, a)), True),
592 (('ige', ('imax', a, b), a), True),
593 (('ult', a, ('umax', b, a)), ('ult', a, b)),
594 (('ult', ('umin', a, b), a), ('ult', b, a)),
595 (('uge', a, ('umin', b, a)), True),
596 (('uge', ('umax', a, b), a), True),
597 (('ilt', a, ('imin', b, a)), False),
598 (('ilt', ('imax', a, b), a), False),
599 (('ige', a, ('imax', b, a)), ('ige', a, b)),
600 (('ige', ('imin', a, b), a), ('ige', b, a)),
601 (('ult', a, ('umin', b, a)), False),
602 (('ult', ('umax', a, b), a), False),
603 (('uge', a, ('umax', b, a)), ('uge', a, b)),
604 (('uge', ('umin', a, b), a), ('uge', b, a)),
605 (('ult', a, ('iand', b, a)), False),
606 (('ult', ('ior', a, b), a), False),
607 (('uge', a, ('iand', b, a)), True),
608 (('uge', ('ior', a, b), a), True),
609
610 (('ilt', '#a', ('imax', '#b', c)), ('ior', ('ilt', a, b), ('ilt', a, c))),
611 (('ilt', ('imin', '#a', b), '#c'), ('ior', ('ilt', a, c), ('ilt', b, c))),
612 (('ige', '#a', ('imin', '#b', c)), ('ior', ('ige', a, b), ('ige', a, c))),
613 (('ige', ('imax', '#a', b), '#c'), ('ior', ('ige', a, c), ('ige', b, c))),
614 (('ult', '#a', ('umax', '#b', c)), ('ior', ('ult', a, b), ('ult', a, c))),
615 (('ult', ('umin', '#a', b), '#c'), ('ior', ('ult', a, c), ('ult', b, c))),
616 (('uge', '#a', ('umin', '#b', c)), ('ior', ('uge', a, b), ('uge', a, c))),
617 (('uge', ('umax', '#a', b), '#c'), ('ior', ('uge', a, c), ('uge', b, c))),
618 (('ilt', '#a', ('imin', '#b', c)), ('iand', ('ilt', a, b), ('ilt', a, c))),
619 (('ilt', ('imax', '#a', b), '#c'), ('iand', ('ilt', a, c), ('ilt', b, c))),
620 (('ige', '#a', ('imax', '#b', c)), ('iand', ('ige', a, b), ('ige', a, c))),
621 (('ige', ('imin', '#a', b), '#c'), ('iand', ('ige', a, c), ('ige', b, c))),
622 (('ult', '#a', ('umin', '#b', c)), ('iand', ('ult', a, b), ('ult', a, c))),
623 (('ult', ('umax', '#a', b), '#c'), ('iand', ('ult', a, c), ('ult', b, c))),
624 (('uge', '#a', ('umax', '#b', c)), ('iand', ('uge', a, b), ('uge', a, c))),
625 (('uge', ('umin', '#a', b), '#c'), ('iand', ('uge', a, c), ('uge', b, c))),
626
627 # Thanks to sign extension, the ishr(a, b) is negative if and only if a is
628 # negative.
629 (('bcsel', ('ilt', a, 0), ('ineg', ('ishr', a, b)), ('ishr', a, b)),
630 ('iabs', ('ishr', a, b))),
631 (('iabs', ('ishr', ('iabs', a), b)), ('ishr', ('iabs', a), b)),
632
633 (('fabs', ('slt', a, b)), ('slt', a, b)),
634 (('fabs', ('sge', a, b)), ('sge', a, b)),
635 (('fabs', ('seq', a, b)), ('seq', a, b)),
636 (('fabs', ('sne', a, b)), ('sne', a, b)),
637 (('slt', a, b), ('b2f', ('flt', a, b)), 'options->lower_scmp'),
638 (('sge', a, b), ('b2f', ('fge', a, b)), 'options->lower_scmp'),
639 (('seq', a, b), ('b2f', ('feq', a, b)), 'options->lower_scmp'),
640 (('sne', a, b), ('b2f', ('fne', a, b)), 'options->lower_scmp'),
641 (('seq', ('seq', a, b), 1.0), ('seq', a, b)),
642 (('seq', ('sne', a, b), 1.0), ('sne', a, b)),
643 (('seq', ('slt', a, b), 1.0), ('slt', a, b)),
644 (('seq', ('sge', a, b), 1.0), ('sge', a, b)),
645 (('sne', ('seq', a, b), 0.0), ('seq', a, b)),
646 (('sne', ('sne', a, b), 0.0), ('sne', a, b)),
647 (('sne', ('slt', a, b), 0.0), ('slt', a, b)),
648 (('sne', ('sge', a, b), 0.0), ('sge', a, b)),
649 (('seq', ('seq', a, b), 0.0), ('sne', a, b)),
650 (('seq', ('sne', a, b), 0.0), ('seq', a, b)),
651 (('seq', ('slt', a, b), 0.0), ('sge', a, b)),
652 (('seq', ('sge', a, b), 0.0), ('slt', a, b)),
653 (('sne', ('seq', a, b), 1.0), ('sne', a, b)),
654 (('sne', ('sne', a, b), 1.0), ('seq', a, b)),
655 (('sne', ('slt', a, b), 1.0), ('sge', a, b)),
656 (('sne', ('sge', a, b), 1.0), ('slt', a, b)),
657 (('fall_equal2', a, b), ('fmin', ('seq', 'a.x', 'b.x'), ('seq', 'a.y', 'b.y')), 'options->lower_vector_cmp'),
658 (('fall_equal3', a, b), ('seq', ('fany_nequal3', a, b), 0.0), 'options->lower_vector_cmp'),
659 (('fall_equal4', a, b), ('seq', ('fany_nequal4', a, b), 0.0), 'options->lower_vector_cmp'),
660 (('fany_nequal2', a, b), ('fmax', ('sne', 'a.x', 'b.x'), ('sne', 'a.y', 'b.y')), 'options->lower_vector_cmp'),
661 (('fany_nequal3', a, b), ('fsat', ('fdot3', ('sne', a, b), ('sne', a, b))), 'options->lower_vector_cmp'),
662 (('fany_nequal4', a, b), ('fsat', ('fdot4', ('sne', a, b), ('sne', a, b))), 'options->lower_vector_cmp'),
663 (('fne', ('fneg', a), a), ('fne', a, 0.0)),
664 (('feq', ('fneg', a), a), ('feq', a, 0.0)),
665 # Emulating booleans
666 (('imul', ('b2i', 'a@1'), ('b2i', 'b@1')), ('b2i', ('iand', a, b))),
667 (('fmul', ('b2f', 'a@1'), ('b2f', 'b@1')), ('b2f', ('iand', a, b))),
668 (('fsat', ('fadd', ('b2f', 'a@1'), ('b2f', 'b@1'))), ('b2f', ('ior', a, b))),
669 (('iand', 'a@bool32', 1.0), ('b2f', a)),
670 # True/False are ~0 and 0 in NIR. b2i of True is 1, and -1 is ~0 (True).
671 (('ineg', ('b2i32', 'a@32')), a),
672 (('flt', ('fneg', ('b2f', 'a@1')), 0), a), # Generated by TGSI KILL_IF.
673 # Comparison with the same args. Note that these are not done for
674 # the float versions because NaN always returns false on float
675 # inequalities.
676 (('ilt', a, a), False),
677 (('ige', a, a), True),
678 (('ieq', a, a), True),
679 (('ine', a, a), False),
680 (('ult', a, a), False),
681 (('uge', a, a), True),
682 # Logical and bit operations
683 (('iand', a, a), a),
684 (('iand', a, ~0), a),
685 (('iand', a, 0), 0),
686 (('ior', a, a), a),
687 (('ior', a, 0), a),
688 (('ior', a, True), True),
689 (('ixor', a, a), 0),
690 (('ixor', a, 0), a),
691 (('inot', ('inot', a)), a),
692 (('ior', ('iand', a, b), b), b),
693 (('ior', ('ior', a, b), b), ('ior', a, b)),
694 (('iand', ('ior', a, b), b), b),
695 (('iand', ('iand', a, b), b), ('iand', a, b)),
696 # DeMorgan's Laws
697 (('iand', ('inot', a), ('inot', b)), ('inot', ('ior', a, b))),
698 (('ior', ('inot', a), ('inot', b)), ('inot', ('iand', a, b))),
699 # Shift optimizations
700 (('ishl', 0, a), 0),
701 (('ishl', a, 0), a),
702 (('ishr', 0, a), 0),
703 (('ishr', a, 0), a),
704 (('ushr', 0, a), 0),
705 (('ushr', a, 0), a),
706 (('iand', 0xff, ('ushr@32', a, 24)), ('ushr', a, 24)),
707 (('iand', 0xffff, ('ushr@32', a, 16)), ('ushr', a, 16)),
708 (('ior', ('ishl@16', a, b), ('ushr@16', a, ('iadd', 16, ('ineg', b)))), ('urol', a, b), '!options->lower_rotate'),
709 (('ior', ('ishl@16', a, b), ('ushr@16', a, ('isub', 16, b))), ('urol', a, b), '!options->lower_rotate'),
710 (('ior', ('ishl@32', a, b), ('ushr@32', a, ('iadd', 32, ('ineg', b)))), ('urol', a, b), '!options->lower_rotate'),
711 (('ior', ('ishl@32', a, b), ('ushr@32', a, ('isub', 32, b))), ('urol', a, b), '!options->lower_rotate'),
712 (('ior', ('ushr@16', a, b), ('ishl@16', a, ('iadd', 16, ('ineg', b)))), ('uror', a, b), '!options->lower_rotate'),
713 (('ior', ('ushr@16', a, b), ('ishl@16', a, ('isub', 16, b))), ('uror', a, b), '!options->lower_rotate'),
714 (('ior', ('ushr@32', a, b), ('ishl@32', a, ('iadd', 32, ('ineg', b)))), ('uror', a, b), '!options->lower_rotate'),
715 (('ior', ('ushr@32', a, b), ('ishl@32', a, ('isub', 32, b))), ('uror', a, b), '!options->lower_rotate'),
716 (('urol@16', a, b), ('ior', ('ishl', a, b), ('ushr', a, ('isub', 16, b))), 'options->lower_rotate'),
717 (('urol@32', a, b), ('ior', ('ishl', a, b), ('ushr', a, ('isub', 32, b))), 'options->lower_rotate'),
718 (('uror@16', a, b), ('ior', ('ushr', a, b), ('ishl', a, ('isub', 16, b))), 'options->lower_rotate'),
719 (('uror@32', a, b), ('ior', ('ushr', a, b), ('ishl', a, ('isub', 32, b))), 'options->lower_rotate'),
720 # Exponential/logarithmic identities
721 (('~fexp2', ('flog2', a)), a), # 2^lg2(a) = a
722 (('~flog2', ('fexp2', a)), a), # lg2(2^a) = a
723 (('fpow', a, b), ('fexp2', ('fmul', ('flog2', a), b)), 'options->lower_fpow'), # a^b = 2^(lg2(a)*b)
724 (('~fexp2', ('fmul', ('flog2', a), b)), ('fpow', a, b), '!options->lower_fpow'), # 2^(lg2(a)*b) = a^b
725 (('~fexp2', ('fadd', ('fmul', ('flog2', a), b), ('fmul', ('flog2', c), d))),
726 ('~fmul', ('fpow', a, b), ('fpow', c, d)), '!options->lower_fpow'), # 2^(lg2(a) * b + lg2(c) + d) = a^b * c^d
727 (('~fexp2', ('fmul', ('flog2', a), 2.0)), ('fmul', a, a)),
728 (('~fexp2', ('fmul', ('flog2', a), 4.0)), ('fmul', ('fmul', a, a), ('fmul', a, a))),
729 (('~fpow', a, 1.0), a),
730 (('~fpow', a, 2.0), ('fmul', a, a)),
731 (('~fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),
732 (('~fpow', 2.0, a), ('fexp2', a)),
733 (('~fpow', ('fpow', a, 2.2), 0.454545), a),
734 (('~fpow', ('fabs', ('fpow', a, 2.2)), 0.454545), ('fabs', a)),
735 (('~fsqrt', ('fexp2', a)), ('fexp2', ('fmul', 0.5, a))),
736 (('~frcp', ('fexp2', a)), ('fexp2', ('fneg', a))),
737 (('~frsq', ('fexp2', a)), ('fexp2', ('fmul', -0.5, a))),
738 (('~flog2', ('fsqrt', a)), ('fmul', 0.5, ('flog2', a))),
739 (('~flog2', ('frcp', a)), ('fneg', ('flog2', a))),
740 (('~flog2', ('frsq', a)), ('fmul', -0.5, ('flog2', a))),
741 (('~flog2', ('fpow', a, b)), ('fmul', b, ('flog2', a))),
742 (('~fmul', ('fexp2(is_used_once)', a), ('fexp2(is_used_once)', b)), ('fexp2', ('fadd', a, b))),
743 (('bcsel', ('flt', a, 0.0), 0.0, ('fsqrt', a)), ('fsqrt', ('fmax', a, 0.0))),
744 # Division and reciprocal
745 (('~fdiv', 1.0, a), ('frcp', a)),
746 (('fdiv', a, b), ('fmul', a, ('frcp', b)), 'options->lower_fdiv'),
747 (('~frcp', ('frcp', a)), a),
748 (('~frcp', ('fsqrt', a)), ('frsq', a)),
749 (('fsqrt', a), ('frcp', ('frsq', a)), 'options->lower_fsqrt'),
750 (('~frcp', ('frsq', a)), ('fsqrt', a), '!options->lower_fsqrt'),
751 # Trig
752 (('fsin', a), lowered_sincos(0.5), 'options->lower_sincos'),
753 (('fcos', a), lowered_sincos(0.75), 'options->lower_sincos'),
754 # Boolean simplifications
755 (('i2b32(is_used_by_if)', a), ('ine32', a, 0)),
756 (('i2b1(is_used_by_if)', a), ('ine', a, 0)),
757 (('ieq', a, True), a),
758 (('ine(is_not_used_by_if)', a, True), ('inot', a)),
759 (('ine', a, False), a),
760 (('ieq(is_not_used_by_if)', a, False), ('inot', 'a')),
761 (('bcsel', a, True, False), a),
762 (('bcsel', a, False, True), ('inot', a)),
763 (('bcsel@32', a, 1.0, 0.0), ('b2f', a)),
764 (('bcsel@32', a, 0.0, 1.0), ('b2f', ('inot', a))),
765 (('bcsel@32', a, -1.0, -0.0), ('fneg', ('b2f', a))),
766 (('bcsel@32', a, -0.0, -1.0), ('fneg', ('b2f', ('inot', a)))),
767 (('bcsel', True, b, c), b),
768 (('bcsel', False, b, c), c),
769 (('bcsel', a, ('b2f(is_used_once)', 'b@32'), ('b2f', 'c@32')), ('b2f', ('bcsel', a, b, c))),
770
771 (('bcsel', a, b, b), b),
772 (('~fcsel', a, b, b), b),
773
774 # D3D Boolean emulation
775 (('bcsel', a, -1, 0), ('ineg', ('b2i', 'a@1'))),
776 (('bcsel', a, 0, -1), ('ineg', ('b2i', ('inot', a)))),
777 (('iand', ('ineg', ('b2i', 'a@1')), ('ineg', ('b2i', 'b@1'))),
778 ('ineg', ('b2i', ('iand', a, b)))),
779 (('ior', ('ineg', ('b2i','a@1')), ('ineg', ('b2i', 'b@1'))),
780 ('ineg', ('b2i', ('ior', a, b)))),
781 (('ieq', ('ineg', ('b2i', 'a@1')), 0), ('inot', a)),
782 (('ieq', ('ineg', ('b2i', 'a@1')), -1), a),
783 (('ine', ('ineg', ('b2i', 'a@1')), 0), a),
784 (('ine', ('ineg', ('b2i', 'a@1')), -1), ('inot', a)),
785 (('iand', ('ineg', ('b2i', a)), 1.0), ('b2f', a)),
786 (('iand', ('ineg', ('b2i', a)), 1), ('b2i', a)),
787
788 # SM5 32-bit shifts are defined to use the 5 least significant bits
789 (('ishl', 'a@32', ('iand', 31, b)), ('ishl', a, b)),
790 (('ishr', 'a@32', ('iand', 31, b)), ('ishr', a, b)),
791 (('ushr', 'a@32', ('iand', 31, b)), ('ushr', a, b)),
792
793 # Conversions
794 (('i2b32', ('b2i', 'a@32')), a),
795 (('f2i', ('ftrunc', a)), ('f2i', a)),
796 (('f2u', ('ftrunc', a)), ('f2u', a)),
797 (('i2b', ('ineg', a)), ('i2b', a)),
798 (('i2b', ('iabs', a)), ('i2b', a)),
799 (('inot', ('f2b1', a)), ('feq', a, 0.0)),
800
801 # Ironically, mark these as imprecise because removing the conversions may
802 # preserve more precision than doing the conversions (e.g.,
803 # uint(float(0x81818181u)) == 0x81818200).
804 (('~f2i32', ('i2f', 'a@32')), a),
805 (('~f2i32', ('u2f', 'a@32')), a),
806 (('~f2u32', ('i2f', 'a@32')), a),
807 (('~f2u32', ('u2f', 'a@32')), a),
808
809 (('ffloor', 'a(is_integral)'), a),
810 (('fceil', 'a(is_integral)'), a),
811 (('ftrunc', 'a(is_integral)'), a),
812 # fract(x) = x - floor(x), so fract(NaN) = NaN
813 (('~ffract', 'a(is_integral)'), 0.0),
814 (('fabs', 'a(is_not_negative)'), a),
815 (('iabs', 'a(is_not_negative)'), a),
816 (('fsat', 'a(is_not_positive)'), 0.0),
817
818 # Section 5.4.1 (Conversion and Scalar Constructors) of the GLSL 4.60 spec
819 # says:
820 #
821 # It is undefined to convert a negative floating-point value to an
822 # uint.
823 #
824 # Assuming that (uint)some_float behaves like (uint)(int)some_float allows
825 # some optimizations in the i965 backend to proceed.
826 (('ige', ('f2u', a), b), ('ige', ('f2i', a), b)),
827 (('ige', b, ('f2u', a)), ('ige', b, ('f2i', a))),
828 (('ilt', ('f2u', a), b), ('ilt', ('f2i', a), b)),
829 (('ilt', b, ('f2u', a)), ('ilt', b, ('f2i', a))),
830
831 (('~fmin', 'a(is_not_negative)', 1.0), ('fsat', a), '!options->lower_fsat'),
832
833 # The result of the multiply must be in [-1, 0], so the result of the ffma
834 # must be in [0, 1].
835 (('flt', ('fadd', ('fmul', ('fsat', a), ('fneg', ('fsat', a))), 1.0), 0.0), False),
836 (('flt', ('fadd', ('fneg', ('fmul', ('fsat', a), ('fsat', a))), 1.0), 0.0), False),
837 (('fmax', ('fadd', ('fmul', ('fsat', a), ('fneg', ('fsat', a))), 1.0), 0.0), ('fadd', ('fmul', ('fsat', a), ('fneg', ('fsat', a))), 1.0)),
838 (('fmax', ('fadd', ('fneg', ('fmul', ('fsat', a), ('fsat', a))), 1.0), 0.0), ('fadd', ('fneg', ('fmul', ('fsat', a), ('fsat', a))), 1.0)),
839
840 (('fne', 'a(is_not_zero)', 0.0), True),
841 (('feq', 'a(is_not_zero)', 0.0), False),
842
843 # In this chart, + means value > 0 and - means value < 0.
844 #
845 # + >= + -> unknown 0 >= + -> false - >= + -> false
846 # + >= 0 -> true 0 >= 0 -> true - >= 0 -> false
847 # + >= - -> true 0 >= - -> true - >= - -> unknown
848 #
849 # Using grouping conceptually similar to a Karnaugh map...
850 #
851 # (+ >= 0, + >= -, 0 >= 0, 0 >= -) == (is_not_negative >= is_not_positive) -> true
852 # (0 >= +, - >= +) == (is_not_positive >= gt_zero) -> false
853 # (- >= +, - >= 0) == (lt_zero >= is_not_negative) -> false
854 #
855 # The flt / ilt cases just invert the expected result.
856 #
857 # The results expecting true, must be marked imprecise. The results
858 # expecting false are fine because NaN compared >= or < anything is false.
859
860 (('~fge', 'a(is_not_negative)', 'b(is_not_positive)'), True),
861 (('fge', 'a(is_not_positive)', 'b(is_gt_zero)'), False),
862 (('fge', 'a(is_lt_zero)', 'b(is_not_negative)'), False),
863
864 (('flt', 'a(is_not_negative)', 'b(is_not_positive)'), False),
865 (('~flt', 'a(is_not_positive)', 'b(is_gt_zero)'), True),
866 (('~flt', 'a(is_lt_zero)', 'b(is_not_negative)'), True),
867
868 (('ine', 'a(is_not_zero)', 0), True),
869 (('ieq', 'a(is_not_zero)', 0), False),
870
871 (('ige', 'a(is_not_negative)', 'b(is_not_positive)'), True),
872 (('ige', 'a(is_not_positive)', 'b(is_gt_zero)'), False),
873 (('ige', 'a(is_lt_zero)', 'b(is_not_negative)'), False),
874
875 (('ilt', 'a(is_not_negative)', 'b(is_not_positive)'), False),
876 (('ilt', 'a(is_not_positive)', 'b(is_gt_zero)'), True),
877 (('ilt', 'a(is_lt_zero)', 'b(is_not_negative)'), True),
878
879 (('ult', 0, 'a(is_gt_zero)'), True),
880
881 # Packing and then unpacking does nothing
882 (('unpack_64_2x32_split_x', ('pack_64_2x32_split', a, b)), a),
883 (('unpack_64_2x32_split_y', ('pack_64_2x32_split', a, b)), b),
884 (('pack_64_2x32_split', ('unpack_64_2x32_split_x', a),
885 ('unpack_64_2x32_split_y', a)), a),
886
887 # Comparing two halves of an unpack separately. While this optimization
888 # should be correct for non-constant values, it's less obvious that it's
889 # useful in that case. For constant values, the pack will fold and we're
890 # guaranteed to reduce the whole tree to one instruction.
891 (('iand', ('ieq', ('unpack_32_2x16_split_x', a), '#b'),
892 ('ieq', ('unpack_32_2x16_split_y', a), '#c')),
893 ('ieq', a, ('pack_32_2x16_split', b, c))),
894
895 # Byte extraction
896 (('ushr', 'a@16', 8), ('extract_u8', a, 1), '!options->lower_extract_byte'),
897 (('ushr', 'a@32', 24), ('extract_u8', a, 3), '!options->lower_extract_byte'),
898 (('ushr', 'a@64', 56), ('extract_u8', a, 7), '!options->lower_extract_byte'),
899 (('ishr', 'a@16', 8), ('extract_i8', a, 1), '!options->lower_extract_byte'),
900 (('ishr', 'a@32', 24), ('extract_i8', a, 3), '!options->lower_extract_byte'),
901 (('ishr', 'a@64', 56), ('extract_i8', a, 7), '!options->lower_extract_byte'),
902 (('iand', 0xff, a), ('extract_u8', a, 0), '!options->lower_extract_byte'),
903
904 # Useless masking before unpacking
905 (('unpack_half_2x16_split_x', ('iand', a, 0xffff)), ('unpack_half_2x16_split_x', a)),
906 (('unpack_32_2x16_split_x', ('iand', a, 0xffff)), ('unpack_32_2x16_split_x', a)),
907 (('unpack_64_2x32_split_x', ('iand', a, 0xffffffff)), ('unpack_64_2x32_split_x', a)),
908 (('unpack_half_2x16_split_y', ('iand', a, 0xffff0000)), ('unpack_half_2x16_split_y', a)),
909 (('unpack_32_2x16_split_y', ('iand', a, 0xffff0000)), ('unpack_32_2x16_split_y', a)),
910 (('unpack_64_2x32_split_y', ('iand', a, 0xffffffff00000000)), ('unpack_64_2x32_split_y', a)),
911 ])
912
913 # After the ('extract_u8', a, 0) pattern, above, triggers, there will be
914 # patterns like those below.
915 for op in ('ushr', 'ishr'):
916 optimizations.extend([(('extract_u8', (op, 'a@16', 8), 0), ('extract_u8', a, 1))])
917 optimizations.extend([(('extract_u8', (op, 'a@32', 8 * i), 0), ('extract_u8', a, i)) for i in range(1, 4)])
918 optimizations.extend([(('extract_u8', (op, 'a@64', 8 * i), 0), ('extract_u8', a, i)) for i in range(1, 8)])
919
920 optimizations.extend([(('extract_u8', ('extract_u16', a, 1), 0), ('extract_u8', a, 2))])
921
922 # After the ('extract_[iu]8', a, 3) patterns, above, trigger, there will be
923 # patterns like those below.
924 for op in ('extract_u8', 'extract_i8'):
925 optimizations.extend([((op, ('ishl', 'a@16', 8), 1), (op, a, 0))])
926 optimizations.extend([((op, ('ishl', 'a@32', 24 - 8 * i), 3), (op, a, i)) for i in range(2, -1, -1)])
927 optimizations.extend([((op, ('ishl', 'a@64', 56 - 8 * i), 7), (op, a, i)) for i in range(6, -1, -1)])
928
929 optimizations.extend([
930 # Word extraction
931 (('ushr', ('ishl', 'a@32', 16), 16), ('extract_u16', a, 0), '!options->lower_extract_word'),
932 (('ushr', 'a@32', 16), ('extract_u16', a, 1), '!options->lower_extract_word'),
933 (('ishr', ('ishl', 'a@32', 16), 16), ('extract_i16', a, 0), '!options->lower_extract_word'),
934 (('ishr', 'a@32', 16), ('extract_i16', a, 1), '!options->lower_extract_word'),
935 (('iand', 0xffff, a), ('extract_u16', a, 0), '!options->lower_extract_word'),
936
937 # Subtracts
938 (('ussub_4x8', a, 0), a),
939 (('ussub_4x8', a, ~0), 0),
940 # Lower all Subtractions first - they can get recombined later
941 (('fsub', a, b), ('fadd', a, ('fneg', b))),
942 (('isub', a, b), ('iadd', a, ('ineg', b))),
943
944 # Propagate negation up multiplication chains
945 (('fmul(is_used_by_non_fsat)', ('fneg', a), b), ('fneg', ('fmul', a, b))),
946 (('imul', ('ineg', a), b), ('ineg', ('imul', a, b))),
947
948 # Propagate constants up multiplication chains
949 (('~fmul(is_used_once)', ('fmul(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('fmul', ('fmul', a, c), b)),
950 (('imul(is_used_once)', ('imul(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('imul', ('imul', a, c), b)),
951 (('~fadd(is_used_once)', ('fadd(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('fadd', ('fadd', a, c), b)),
952 (('iadd(is_used_once)', ('iadd(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('iadd', ('iadd', a, c), b)),
953
954 # Reassociate constants in add/mul chains so they can be folded together.
955 # For now, we mostly only handle cases where the constants are separated by
956 # a single non-constant. We could do better eventually.
957 (('~fmul', '#a', ('fmul', 'b(is_not_const)', '#c')), ('fmul', ('fmul', a, c), b)),
958 (('imul', '#a', ('imul', 'b(is_not_const)', '#c')), ('imul', ('imul', a, c), b)),
959 (('~fadd', '#a', ('fadd', 'b(is_not_const)', '#c')), ('fadd', ('fadd', a, c), b)),
960 (('~fadd', '#a', ('fneg', ('fadd', 'b(is_not_const)', '#c'))), ('fadd', ('fadd', a, ('fneg', c)), ('fneg', b))),
961 (('iadd', '#a', ('iadd', 'b(is_not_const)', '#c')), ('iadd', ('iadd', a, c), b)),
962
963 # Drop mul-div by the same value when there's no wrapping.
964 (('idiv', ('imul(no_signed_wrap)', a, b), b), a),
965
966 # By definition...
967 (('bcsel', ('ige', ('find_lsb', a), 0), ('find_lsb', a), -1), ('find_lsb', a)),
968 (('bcsel', ('ige', ('ifind_msb', a), 0), ('ifind_msb', a), -1), ('ifind_msb', a)),
969 (('bcsel', ('ige', ('ufind_msb', a), 0), ('ufind_msb', a), -1), ('ufind_msb', a)),
970
971 (('bcsel', ('ine', a, 0), ('find_lsb', a), -1), ('find_lsb', a)),
972 (('bcsel', ('ine', a, 0), ('ifind_msb', a), -1), ('ifind_msb', a)),
973 (('bcsel', ('ine', a, 0), ('ufind_msb', a), -1), ('ufind_msb', a)),
974
975 (('bcsel', ('ine', a, -1), ('ifind_msb', a), -1), ('ifind_msb', a)),
976
977 # Misc. lowering
978 (('fmod@16', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod'),
979 (('fmod@32', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod'),
980 (('frem', a, b), ('fsub', a, ('fmul', b, ('ftrunc', ('fdiv', a, b)))), 'options->lower_fmod'),
981 (('uadd_carry@32', a, b), ('b2i', ('ult', ('iadd', a, b), a)), 'options->lower_uadd_carry'),
982 (('usub_borrow@32', a, b), ('b2i', ('ult', a, b)), 'options->lower_usub_borrow'),
983
984 (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
985 ('bcsel', ('ult', 31, 'bits'), 'insert',
986 ('bfi', ('bfm', 'bits', 'offset'), 'insert', 'base')),
987 'options->lower_bitfield_insert'),
988 (('ihadd', a, b), ('iadd', ('iand', a, b), ('ishr', ('ixor', a, b), 1)), 'options->lower_hadd'),
989 (('uhadd', a, b), ('iadd', ('iand', a, b), ('ushr', ('ixor', a, b), 1)), 'options->lower_hadd'),
990 (('irhadd', a, b), ('isub', ('ior', a, b), ('ishr', ('ixor', a, b), 1)), 'options->lower_hadd'),
991 (('urhadd', a, b), ('isub', ('ior', a, b), ('ushr', ('ixor', a, b), 1)), 'options->lower_hadd'),
992 (('uadd_sat', a, b), ('bcsel', ('ult', ('iadd', a, b), a), -1, ('iadd', a, b)), 'options->lower_add_sat'),
993 (('usub_sat', a, b), ('bcsel', ('ult', a, b), 0, ('isub', a, b)), 'options->lower_add_sat'),
994
995 # Alternative lowering that doesn't rely on bfi.
996 (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
997 ('bcsel', ('ult', 31, 'bits'),
998 'insert',
999 (('ior',
1000 ('iand', 'base', ('inot', ('ishl', ('isub', ('ishl', 1, 'bits'), 1), 'offset'))),
1001 ('iand', ('ishl', 'insert', 'offset'), ('ishl', ('isub', ('ishl', 1, 'bits'), 1), 'offset'))))),
1002 'options->lower_bitfield_insert_to_shifts'),
1003
1004 # Alternative lowering that uses bitfield_select.
1005 (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
1006 ('bcsel', ('ult', 31, 'bits'), 'insert',
1007 ('bitfield_select', ('bfm', 'bits', 'offset'), ('ishl', 'insert', 'offset'), 'base')),
1008 'options->lower_bitfield_insert_to_bitfield_select'),
1009
1010 (('ibitfield_extract', 'value', 'offset', 'bits'),
1011 ('bcsel', ('ult', 31, 'bits'), 'value',
1012 ('ibfe', 'value', 'offset', 'bits')),
1013 'options->lower_bitfield_extract'),
1014
1015 (('ubitfield_extract', 'value', 'offset', 'bits'),
1016 ('bcsel', ('ult', 31, 'bits'), 'value',
1017 ('ubfe', 'value', 'offset', 'bits')),
1018 'options->lower_bitfield_extract'),
1019
1020 # Note that these opcodes are defined to only use the five least significant bits of 'offset' and 'bits'
1021 (('ubfe', 'value', 'offset', ('iand', 31, 'bits')), ('ubfe', 'value', 'offset', 'bits')),
1022 (('ubfe', 'value', ('iand', 31, 'offset'), 'bits'), ('ubfe', 'value', 'offset', 'bits')),
1023 (('ibfe', 'value', 'offset', ('iand', 31, 'bits')), ('ibfe', 'value', 'offset', 'bits')),
1024 (('ibfe', 'value', ('iand', 31, 'offset'), 'bits'), ('ibfe', 'value', 'offset', 'bits')),
1025 (('bfm', 'bits', ('iand', 31, 'offset')), ('bfm', 'bits', 'offset')),
1026 (('bfm', ('iand', 31, 'bits'), 'offset'), ('bfm', 'bits', 'offset')),
1027
1028 (('ibitfield_extract', 'value', 'offset', 'bits'),
1029 ('bcsel', ('ieq', 0, 'bits'),
1030 0,
1031 ('ishr',
1032 ('ishl', 'value', ('isub', ('isub', 32, 'bits'), 'offset')),
1033 ('isub', 32, 'bits'))),
1034 'options->lower_bitfield_extract_to_shifts'),
1035
1036 (('ubitfield_extract', 'value', 'offset', 'bits'),
1037 ('iand',
1038 ('ushr', 'value', 'offset'),
1039 ('bcsel', ('ieq', 'bits', 32),
1040 0xffffffff,
1041 ('isub', ('ishl', 1, 'bits'), 1))),
1042 'options->lower_bitfield_extract_to_shifts'),
1043
1044 (('ifind_msb', 'value'),
1045 ('ufind_msb', ('bcsel', ('ilt', 'value', 0), ('inot', 'value'), 'value')),
1046 'options->lower_ifind_msb'),
1047
1048 (('find_lsb', 'value'),
1049 ('ufind_msb', ('iand', 'value', ('ineg', 'value'))),
1050 'options->lower_find_lsb'),
1051
1052 (('extract_i8', a, 'b@32'),
1053 ('ishr', ('ishl', a, ('imul', ('isub', 3, b), 8)), 24),
1054 'options->lower_extract_byte'),
1055
1056 (('extract_u8', a, 'b@32'),
1057 ('iand', ('ushr', a, ('imul', b, 8)), 0xff),
1058 'options->lower_extract_byte'),
1059
1060 (('extract_i16', a, 'b@32'),
1061 ('ishr', ('ishl', a, ('imul', ('isub', 1, b), 16)), 16),
1062 'options->lower_extract_word'),
1063
1064 (('extract_u16', a, 'b@32'),
1065 ('iand', ('ushr', a, ('imul', b, 16)), 0xffff),
1066 'options->lower_extract_word'),
1067
1068 (('pack_unorm_2x16', 'v'),
1069 ('pack_uvec2_to_uint',
1070 ('f2u32', ('fround_even', ('fmul', ('fsat', 'v'), 65535.0)))),
1071 'options->lower_pack_unorm_2x16'),
1072
1073 (('pack_unorm_4x8', 'v'),
1074 ('pack_uvec4_to_uint',
1075 ('f2u32', ('fround_even', ('fmul', ('fsat', 'v'), 255.0)))),
1076 'options->lower_pack_unorm_4x8'),
1077
1078 (('pack_snorm_2x16', 'v'),
1079 ('pack_uvec2_to_uint',
1080 ('f2i32', ('fround_even', ('fmul', ('fmin', 1.0, ('fmax', -1.0, 'v')), 32767.0)))),
1081 'options->lower_pack_snorm_2x16'),
1082
1083 (('pack_snorm_4x8', 'v'),
1084 ('pack_uvec4_to_uint',
1085 ('f2i32', ('fround_even', ('fmul', ('fmin', 1.0, ('fmax', -1.0, 'v')), 127.0)))),
1086 'options->lower_pack_snorm_4x8'),
1087
1088 (('unpack_unorm_2x16', 'v'),
1089 ('fdiv', ('u2f32', ('vec2', ('extract_u16', 'v', 0),
1090 ('extract_u16', 'v', 1))),
1091 65535.0),
1092 'options->lower_unpack_unorm_2x16'),
1093
1094 (('unpack_unorm_4x8', 'v'),
1095 ('fdiv', ('u2f32', ('vec4', ('extract_u8', 'v', 0),
1096 ('extract_u8', 'v', 1),
1097 ('extract_u8', 'v', 2),
1098 ('extract_u8', 'v', 3))),
1099 255.0),
1100 'options->lower_unpack_unorm_4x8'),
1101
1102 (('unpack_snorm_2x16', 'v'),
1103 ('fmin', 1.0, ('fmax', -1.0, ('fdiv', ('i2f', ('vec2', ('extract_i16', 'v', 0),
1104 ('extract_i16', 'v', 1))),
1105 32767.0))),
1106 'options->lower_unpack_snorm_2x16'),
1107
1108 (('unpack_snorm_4x8', 'v'),
1109 ('fmin', 1.0, ('fmax', -1.0, ('fdiv', ('i2f', ('vec4', ('extract_i8', 'v', 0),
1110 ('extract_i8', 'v', 1),
1111 ('extract_i8', 'v', 2),
1112 ('extract_i8', 'v', 3))),
1113 127.0))),
1114 'options->lower_unpack_snorm_4x8'),
1115
1116 (('isign', a), ('imin', ('imax', a, -1), 1), 'options->lower_isign'),
1117 (('fsign', a), ('fsub', ('b2f', ('flt', 0.0, a)), ('b2f', ('flt', a, 0.0))), 'options->lower_fsign'),
1118
1119 # Address/offset calculations:
1120 # Drivers supporting imul24 should use the nir_lower_amul() pass, this
1121 # rule converts everyone else to imul:
1122 (('amul', a, b), ('imul', a, b), '!options->has_imul24'),
1123
1124 (('imad24_ir3', a, b, 0), ('imul24', a, b)),
1125 (('imad24_ir3', a, 0, c), (c)),
1126 (('imad24_ir3', a, 1, c), ('iadd', a, c)),
1127
1128 # if first two srcs are const, crack apart the imad so constant folding
1129 # can clean up the imul:
1130 # TODO ffma should probably get a similar rule:
1131 (('imad24_ir3', '#a', '#b', c), ('iadd', ('imul', a, b), c)),
1132
1133 # These will turn 24b address/offset calc back into 32b shifts, but
1134 # it should be safe to get back some of the bits of precision that we
1135 # already decided were no necessary:
1136 (('imul24', a, '#b@32(is_pos_power_of_two)'), ('ishl', a, ('find_lsb', b)), '!options->lower_bitops'),
1137 (('imul24', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('ishl', a, ('find_lsb', ('iabs', b)))), '!options->lower_bitops'),
1138 (('imul24', a, 0), (0)),
1139 ])
1140
1141 # bit_size dependent lowerings
1142 for bit_size in [8, 16, 32, 64]:
1143 # convenience constants
1144 intmax = (1 << (bit_size - 1)) - 1
1145 intmin = 1 << (bit_size - 1)
1146
1147 optimizations += [
1148 (('iadd_sat@' + str(bit_size), a, b),
1149 ('bcsel', ('ige', b, 1), ('bcsel', ('ilt', ('iadd', a, b), a), intmax, ('iadd', a, b)),
1150 ('bcsel', ('ilt', a, ('iadd', a, b)), intmin, ('iadd', a, b))), 'options->lower_add_sat'),
1151 (('isub_sat@' + str(bit_size), a, b),
1152 ('bcsel', ('ilt', b, 0), ('bcsel', ('ilt', ('isub', a, b), a), intmax, ('isub', a, b)),
1153 ('bcsel', ('ilt', a, ('isub', a, b)), intmin, ('isub', a, b))), 'options->lower_add_sat'),
1154 ]
1155
1156 invert = OrderedDict([('feq', 'fne'), ('fne', 'feq'), ('fge', 'flt'), ('flt', 'fge')])
1157
1158 for left, right in itertools.combinations_with_replacement(invert.keys(), 2):
1159 optimizations.append((('inot', ('ior(is_used_once)', (left, a, b), (right, c, d))),
1160 ('iand', (invert[left], a, b), (invert[right], c, d))))
1161 optimizations.append((('inot', ('iand(is_used_once)', (left, a, b), (right, c, d))),
1162 ('ior', (invert[left], a, b), (invert[right], c, d))))
1163
1164 # Optimize x2bN(b2x(x)) -> x
1165 for size in type_sizes('bool'):
1166 aN = 'a@' + str(size)
1167 f2bN = 'f2b' + str(size)
1168 i2bN = 'i2b' + str(size)
1169 optimizations.append(((f2bN, ('b2f', aN)), a))
1170 optimizations.append(((i2bN, ('b2i', aN)), a))
1171
1172 # Optimize x2yN(b2x(x)) -> b2y
1173 for x, y in itertools.product(['f', 'u', 'i'], ['f', 'u', 'i']):
1174 if x != 'f' and y != 'f' and x != y:
1175 continue
1176
1177 b2x = 'b2f' if x == 'f' else 'b2i'
1178 b2y = 'b2f' if y == 'f' else 'b2i'
1179 x2yN = '{}2{}'.format(x, y)
1180 optimizations.append(((x2yN, (b2x, a)), (b2y, a)))
1181
1182 # Optimize away x2xN(a@N)
1183 for t in ['int', 'uint', 'float']:
1184 for N in type_sizes(t):
1185 x2xN = '{0}2{0}{1}'.format(t[0], N)
1186 aN = 'a@{0}'.format(N)
1187 optimizations.append(((x2xN, aN), a))
1188
1189 # Optimize x2xN(y2yM(a@P)) -> y2yN(a) for integers
1190 # In particular, we can optimize away everything except upcast of downcast and
1191 # upcasts where the type differs from the other cast
1192 for N, M in itertools.product(type_sizes('uint'), type_sizes('uint')):
1193 if N < M:
1194 # The outer cast is a down-cast. It doesn't matter what the size of the
1195 # argument of the inner cast is because we'll never been in the upcast
1196 # of downcast case. Regardless of types, we'll always end up with y2yN
1197 # in the end.
1198 for x, y in itertools.product(['i', 'u'], ['i', 'u']):
1199 x2xN = '{0}2{0}{1}'.format(x, N)
1200 y2yM = '{0}2{0}{1}'.format(y, M)
1201 y2yN = '{0}2{0}{1}'.format(y, N)
1202 optimizations.append(((x2xN, (y2yM, a)), (y2yN, a)))
1203 elif N > M:
1204 # If the outer cast is an up-cast, we have to be more careful about the
1205 # size of the argument of the inner cast and with types. In this case,
1206 # the type is always the type of type up-cast which is given by the
1207 # outer cast.
1208 for P in type_sizes('uint'):
1209 # We can't optimize away up-cast of down-cast.
1210 if M < P:
1211 continue
1212
1213 # Because we're doing down-cast of down-cast, the types always have
1214 # to match between the two casts
1215 for x in ['i', 'u']:
1216 x2xN = '{0}2{0}{1}'.format(x, N)
1217 x2xM = '{0}2{0}{1}'.format(x, M)
1218 aP = 'a@{0}'.format(P)
1219 optimizations.append(((x2xN, (x2xM, aP)), (x2xN, a)))
1220 else:
1221 # The N == M case is handled by other optimizations
1222 pass
1223
1224 # Optimize comparisons with up-casts
1225 for t in ['int', 'uint', 'float']:
1226 for N, M in itertools.product(type_sizes(t), repeat=2):
1227 if N == 1 or N >= M:
1228 continue
1229
1230 x2xM = '{0}2{0}{1}'.format(t[0], M)
1231 x2xN = '{0}2{0}{1}'.format(t[0], N)
1232 aN = 'a@' + str(N)
1233 bN = 'b@' + str(N)
1234 xeq = 'feq' if t == 'float' else 'ieq'
1235 xne = 'fne' if t == 'float' else 'ine'
1236 xge = '{0}ge'.format(t[0])
1237 xlt = '{0}lt'.format(t[0])
1238
1239 # Up-casts are lossless so for correctly signed comparisons of
1240 # up-casted values we can do the comparison at the largest of the two
1241 # original sizes and drop one or both of the casts. (We have
1242 # optimizations to drop the no-op casts which this may generate.)
1243 for P in type_sizes(t):
1244 if P == 1 or P > N:
1245 continue
1246
1247 bP = 'b@' + str(P)
1248 optimizations += [
1249 ((xeq, (x2xM, aN), (x2xM, bP)), (xeq, a, (x2xN, b))),
1250 ((xne, (x2xM, aN), (x2xM, bP)), (xne, a, (x2xN, b))),
1251 ((xge, (x2xM, aN), (x2xM, bP)), (xge, a, (x2xN, b))),
1252 ((xlt, (x2xM, aN), (x2xM, bP)), (xlt, a, (x2xN, b))),
1253 ((xge, (x2xM, bP), (x2xM, aN)), (xge, (x2xN, b), a)),
1254 ((xlt, (x2xM, bP), (x2xM, aN)), (xlt, (x2xN, b), a)),
1255 ]
1256
1257 # The next bit doesn't work on floats because the range checks would
1258 # get way too complicated.
1259 if t in ['int', 'uint']:
1260 if t == 'int':
1261 xN_min = -(1 << (N - 1))
1262 xN_max = (1 << (N - 1)) - 1
1263 elif t == 'uint':
1264 xN_min = 0
1265 xN_max = (1 << N) - 1
1266 else:
1267 assert False
1268
1269 # If we're up-casting and comparing to a constant, we can unfold
1270 # the comparison into a comparison with the shrunk down constant
1271 # and a check that the constant fits in the smaller bit size.
1272 optimizations += [
1273 ((xeq, (x2xM, aN), '#b'),
1274 ('iand', (xeq, a, (x2xN, b)), (xeq, (x2xM, (x2xN, b)), b))),
1275 ((xne, (x2xM, aN), '#b'),
1276 ('ior', (xne, a, (x2xN, b)), (xne, (x2xM, (x2xN, b)), b))),
1277 ((xlt, (x2xM, aN), '#b'),
1278 ('iand', (xlt, xN_min, b),
1279 ('ior', (xlt, xN_max, b), (xlt, a, (x2xN, b))))),
1280 ((xlt, '#a', (x2xM, bN)),
1281 ('iand', (xlt, a, xN_max),
1282 ('ior', (xlt, a, xN_min), (xlt, (x2xN, a), b)))),
1283 ((xge, (x2xM, aN), '#b'),
1284 ('iand', (xge, xN_max, b),
1285 ('ior', (xge, xN_min, b), (xge, a, (x2xN, b))))),
1286 ((xge, '#a', (x2xM, bN)),
1287 ('iand', (xge, a, xN_min),
1288 ('ior', (xge, a, xN_max), (xge, (x2xN, a), b)))),
1289 ]
1290
1291 def fexp2i(exp, bits):
1292 # We assume that exp is already in the right range.
1293 if bits == 16:
1294 return ('i2i16', ('ishl', ('iadd', exp, 15), 10))
1295 elif bits == 32:
1296 return ('ishl', ('iadd', exp, 127), 23)
1297 elif bits == 64:
1298 return ('pack_64_2x32_split', 0, ('ishl', ('iadd', exp, 1023), 20))
1299 else:
1300 assert False
1301
1302 def ldexp(f, exp, bits):
1303 # First, we clamp exp to a reasonable range. The maximum possible range
1304 # for a normal exponent is [-126, 127] and, throwing in denormals, you get
1305 # a maximum range of [-149, 127]. This means that we can potentially have
1306 # a swing of +-276. If you start with FLT_MAX, you actually have to do
1307 # ldexp(FLT_MAX, -278) to get it to flush all the way to zero. The GLSL
1308 # spec, on the other hand, only requires that we handle an exponent value
1309 # in the range [-126, 128]. This implementation is *mostly* correct; it
1310 # handles a range on exp of [-252, 254] which allows you to create any
1311 # value (including denorms if the hardware supports it) and to adjust the
1312 # exponent of any normal value to anything you want.
1313 if bits == 16:
1314 exp = ('imin', ('imax', exp, -28), 30)
1315 elif bits == 32:
1316 exp = ('imin', ('imax', exp, -252), 254)
1317 elif bits == 64:
1318 exp = ('imin', ('imax', exp, -2044), 2046)
1319 else:
1320 assert False
1321
1322 # Now we compute two powers of 2, one for exp/2 and one for exp-exp/2.
1323 # (We use ishr which isn't the same for -1, but the -1 case still works
1324 # since we use exp-exp/2 as the second exponent.) While the spec
1325 # technically defines ldexp as f * 2.0^exp, simply multiplying once doesn't
1326 # work with denormals and doesn't allow for the full swing in exponents
1327 # that you can get with normalized values. Instead, we create two powers
1328 # of two and multiply by them each in turn. That way the effective range
1329 # of our exponent is doubled.
1330 pow2_1 = fexp2i(('ishr', exp, 1), bits)
1331 pow2_2 = fexp2i(('isub', exp, ('ishr', exp, 1)), bits)
1332 return ('fmul', ('fmul', f, pow2_1), pow2_2)
1333
1334 optimizations += [
1335 (('ldexp@16', 'x', 'exp'), ldexp('x', 'exp', 16), 'options->lower_ldexp'),
1336 (('ldexp@32', 'x', 'exp'), ldexp('x', 'exp', 32), 'options->lower_ldexp'),
1337 (('ldexp@64', 'x', 'exp'), ldexp('x', 'exp', 64), 'options->lower_ldexp'),
1338 ]
1339
1340 # Unreal Engine 4 demo applications open-codes bitfieldReverse()
1341 def bitfield_reverse(u):
1342 step1 = ('ior', ('ishl', u, 16), ('ushr', u, 16))
1343 step2 = ('ior', ('ishl', ('iand', step1, 0x00ff00ff), 8), ('ushr', ('iand', step1, 0xff00ff00), 8))
1344 step3 = ('ior', ('ishl', ('iand', step2, 0x0f0f0f0f), 4), ('ushr', ('iand', step2, 0xf0f0f0f0), 4))
1345 step4 = ('ior', ('ishl', ('iand', step3, 0x33333333), 2), ('ushr', ('iand', step3, 0xcccccccc), 2))
1346 step5 = ('ior(many-comm-expr)', ('ishl', ('iand', step4, 0x55555555), 1), ('ushr', ('iand', step4, 0xaaaaaaaa), 1))
1347
1348 return step5
1349
1350 optimizations += [(bitfield_reverse('x@32'), ('bitfield_reverse', 'x'), '!options->lower_bitfield_reverse')]
1351
1352 # For any float comparison operation, "cmp", if you have "a == a && a cmp b"
1353 # then the "a == a" is redundant because it's equivalent to "a is not NaN"
1354 # and, if a is a NaN then the second comparison will fail anyway.
1355 for op in ['flt', 'fge', 'feq']:
1356 optimizations += [
1357 (('iand', ('feq', a, a), (op, a, b)), ('!' + op, a, b)),
1358 (('iand', ('feq', a, a), (op, b, a)), ('!' + op, b, a)),
1359 ]
1360
1361 # Add optimizations to handle the case where the result of a ternary is
1362 # compared to a constant. This way we can take things like
1363 #
1364 # (a ? 0 : 1) > 0
1365 #
1366 # and turn it into
1367 #
1368 # a ? (0 > 0) : (1 > 0)
1369 #
1370 # which constant folding will eat for lunch. The resulting ternary will
1371 # further get cleaned up by the boolean reductions above and we will be
1372 # left with just the original variable "a".
1373 for op in ['flt', 'fge', 'feq', 'fne',
1374 'ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']:
1375 optimizations += [
1376 ((op, ('bcsel', 'a', '#b', '#c'), '#d'),
1377 ('bcsel', 'a', (op, 'b', 'd'), (op, 'c', 'd'))),
1378 ((op, '#d', ('bcsel', a, '#b', '#c')),
1379 ('bcsel', 'a', (op, 'd', 'b'), (op, 'd', 'c'))),
1380 ]
1381
1382
1383 # For example, this converts things like
1384 #
1385 # 1 + mix(0, a - 1, condition)
1386 #
1387 # into
1388 #
1389 # mix(1, (a-1)+1, condition)
1390 #
1391 # Other optimizations will rearrange the constants.
1392 for op in ['fadd', 'fmul', 'iadd', 'imul']:
1393 optimizations += [
1394 ((op, ('bcsel(is_used_once)', a, '#b', c), '#d'), ('bcsel', a, (op, b, d), (op, c, d)))
1395 ]
1396
1397 # For derivatives in compute shaders, GLSL_NV_compute_shader_derivatives
1398 # states:
1399 #
1400 # If neither layout qualifier is specified, derivatives in compute shaders
1401 # return zero, which is consistent with the handling of built-in texture
1402 # functions like texture() in GLSL 4.50 compute shaders.
1403 for op in ['fddx', 'fddx_fine', 'fddx_coarse',
1404 'fddy', 'fddy_fine', 'fddy_coarse']:
1405 optimizations += [
1406 ((op, 'a'), 0.0, 'info->stage == MESA_SHADER_COMPUTE && info->cs.derivative_group == DERIVATIVE_GROUP_NONE')
1407 ]
1408
1409 # Some optimizations for ir3-specific instructions.
1410 optimizations += [
1411 # 'al * bl': If either 'al' or 'bl' is zero, return zero.
1412 (('umul_low', '#a(is_lower_half_zero)', 'b'), (0)),
1413 # '(ah * bl) << 16 + c': If either 'ah' or 'bl' is zero, return 'c'.
1414 (('imadsh_mix16', '#a@32(is_lower_half_zero)', 'b@32', 'c@32'), ('c')),
1415 (('imadsh_mix16', 'a@32', '#b@32(is_upper_half_zero)', 'c@32'), ('c')),
1416 ]
1417
1418 # This section contains "late" optimizations that should be run before
1419 # creating ffmas and calling regular optimizations for the final time.
1420 # Optimizations should go here if they help code generation and conflict
1421 # with the regular optimizations.
1422 before_ffma_optimizations = [
1423 # Propagate constants down multiplication chains
1424 (('~fmul(is_used_once)', ('fmul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fmul', ('fmul', a, c), b)),
1425 (('imul(is_used_once)', ('imul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('imul', ('imul', a, c), b)),
1426 (('~fadd(is_used_once)', ('fadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fadd', ('fadd', a, c), b)),
1427 (('iadd(is_used_once)', ('iadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('iadd', ('iadd', a, c), b)),
1428
1429 (('~fadd', ('fmul', a, b), ('fmul', a, c)), ('fmul', a, ('fadd', b, c))),
1430 (('iadd', ('imul', a, b), ('imul', a, c)), ('imul', a, ('iadd', b, c))),
1431 (('~fadd', ('fneg', a), a), 0.0),
1432 (('iadd', ('ineg', a), a), 0),
1433 (('iadd', ('ineg', a), ('iadd', a, b)), b),
1434 (('iadd', a, ('iadd', ('ineg', a), b)), b),
1435 (('~fadd', ('fneg', a), ('fadd', a, b)), b),
1436 (('~fadd', a, ('fadd', ('fneg', a), b)), b),
1437
1438 (('~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)),
1439 (('~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)),
1440 (('~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))),
1441 ]
1442
1443 # This section contains "late" optimizations that should be run after the
1444 # regular optimizations have finished. Optimizations should go here if
1445 # they help code generation but do not necessarily produce code that is
1446 # more easily optimizable.
1447 late_optimizations = [
1448 # Most of these optimizations aren't quite safe when you get infinity or
1449 # Nan involved but the first one should be fine.
1450 (('flt', ('fadd', a, b), 0.0), ('flt', a, ('fneg', b))),
1451 (('flt', ('fneg', ('fadd', a, b)), 0.0), ('flt', ('fneg', a), b)),
1452 (('~fge', ('fadd', a, b), 0.0), ('fge', a, ('fneg', b))),
1453 (('~fge', ('fneg', ('fadd', a, b)), 0.0), ('fge', ('fneg', a), b)),
1454 (('~feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
1455 (('~fne', ('fadd', a, b), 0.0), ('fne', a, ('fneg', b))),
1456
1457 # nir_lower_to_source_mods will collapse this, but its existence during the
1458 # optimization loop can prevent other optimizations.
1459 (('fneg', ('fneg', a)), a),
1460
1461 # Subtractions get lowered during optimization, so we need to recombine them
1462 (('fadd', 'a', ('fneg', 'b')), ('fsub', 'a', 'b'), '!options->lower_sub'),
1463 (('iadd', 'a', ('ineg', 'b')), ('isub', 'a', 'b'), '!options->lower_sub'),
1464 (('fneg', a), ('fsub', 0.0, a), 'options->lower_negate'),
1465 (('ineg', a), ('isub', 0, a), 'options->lower_negate'),
1466
1467 # These are duplicated from the main optimizations table. The late
1468 # patterns that rearrange expressions like x - .5 < 0 to x < .5 can create
1469 # new patterns like these. The patterns that compare with zero are removed
1470 # because they are unlikely to be created in by anything in
1471 # late_optimizations.
1472 (('flt', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('flt', a, b)),
1473 (('flt', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('flt', b, a)),
1474 (('fge', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fge', a, b)),
1475 (('fge', '#b(is_gt_0_and_lt_1)', ('fsat(is_used_once)', a)), ('fge', b, a)),
1476 (('feq', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('feq', a, b)),
1477 (('fne', ('fsat(is_used_once)', a), '#b(is_gt_0_and_lt_1)'), ('fne', a, b)),
1478
1479 (('fge', ('fsat(is_used_once)', a), 1.0), ('fge', a, 1.0)),
1480 (('flt', ('fsat(is_used_once)', a), 1.0), ('flt', a, 1.0)),
1481
1482 (('~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)))),
1483
1484 (('flt', ('fneg', a), ('fneg', b)), ('flt', b, a)),
1485 (('fge', ('fneg', a), ('fneg', b)), ('fge', b, a)),
1486 (('feq', ('fneg', a), ('fneg', b)), ('feq', b, a)),
1487 (('fne', ('fneg', a), ('fneg', b)), ('fne', b, a)),
1488 (('flt', ('fneg', a), -1.0), ('flt', 1.0, a)),
1489 (('flt', -1.0, ('fneg', a)), ('flt', a, 1.0)),
1490 (('fge', ('fneg', a), -1.0), ('fge', 1.0, a)),
1491 (('fge', -1.0, ('fneg', a)), ('fge', a, 1.0)),
1492 (('fne', ('fneg', a), -1.0), ('fne', 1.0, a)),
1493 (('feq', -1.0, ('fneg', a)), ('feq', a, 1.0)),
1494
1495 (('ior', a, a), a),
1496 (('iand', a, a), a),
1497
1498 (('~fadd', ('fneg(is_used_once)', ('fsat(is_used_once)', 'a(is_not_fmul)')), 1.0), ('fsat', ('fadd', 1.0, ('fneg', a)))),
1499
1500 (('fdot2', a, b), ('fdot_replicated2', a, b), 'options->fdot_replicates'),
1501 (('fdot3', a, b), ('fdot_replicated3', a, b), 'options->fdot_replicates'),
1502 (('fdot4', a, b), ('fdot_replicated4', a, b), 'options->fdot_replicates'),
1503 (('fdph', a, b), ('fdph_replicated', a, b), 'options->fdot_replicates'),
1504
1505 (('~flrp@32', ('fadd(is_used_once)', a, b), ('fadd(is_used_once)', a, c), d), ('fadd', ('flrp', b, c, d), a)),
1506 (('~flrp@64', ('fadd(is_used_once)', a, b), ('fadd(is_used_once)', a, c), d), ('fadd', ('flrp', b, c, d), a)),
1507
1508 (('~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'),
1509 (('~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'),
1510
1511 # A similar operation could apply to any ffma(#a, b, #(-a/2)), but this
1512 # particular operation is common for expanding values stored in a texture
1513 # from [0,1] to [-1,1].
1514 (('~ffma@32', a, 2.0, -1.0), ('flrp', -1.0, 1.0, a ), '!options->lower_flrp32'),
1515 (('~ffma@32', a, -2.0, -1.0), ('flrp', -1.0, 1.0, ('fneg', a)), '!options->lower_flrp32'),
1516 (('~ffma@32', a, -2.0, 1.0), ('flrp', 1.0, -1.0, a ), '!options->lower_flrp32'),
1517 (('~ffma@32', a, 2.0, 1.0), ('flrp', 1.0, -1.0, ('fneg', a)), '!options->lower_flrp32'),
1518 (('~fadd@32', ('fmul(is_used_once)', 2.0, a), -1.0), ('flrp', -1.0, 1.0, a ), '!options->lower_flrp32'),
1519 (('~fadd@32', ('fmul(is_used_once)', -2.0, a), -1.0), ('flrp', -1.0, 1.0, ('fneg', a)), '!options->lower_flrp32'),
1520 (('~fadd@32', ('fmul(is_used_once)', -2.0, a), 1.0), ('flrp', 1.0, -1.0, a ), '!options->lower_flrp32'),
1521 (('~fadd@32', ('fmul(is_used_once)', 2.0, a), 1.0), ('flrp', 1.0, -1.0, ('fneg', a)), '!options->lower_flrp32'),
1522
1523 # flrp(a, b, a)
1524 # a*(1-a) + b*a
1525 # a + -a*a + a*b (1)
1526 # a + a*(b - a)
1527 # Option 1: ffma(a, (b-a), a)
1528 #
1529 # Alternately, after (1):
1530 # a*(1+b) + -a*a
1531 # a*((1+b) + -a)
1532 #
1533 # Let b=1
1534 #
1535 # Option 2: ffma(a, 2, -(a*a))
1536 # Option 3: ffma(a, 2, (-a)*a)
1537 # Option 4: ffma(a, -a, (2*a)
1538 # Option 5: a * (2 - a)
1539 #
1540 # There are a lot of other possible combinations.
1541 (('~ffma@32', ('fadd', b, ('fneg', a)), a, a), ('flrp', a, b, a), '!options->lower_flrp32'),
1542 (('~ffma@32', a, 2.0, ('fneg', ('fmul', a, a))), ('flrp', a, 1.0, a), '!options->lower_flrp32'),
1543 (('~ffma@32', a, 2.0, ('fmul', ('fneg', a), a)), ('flrp', a, 1.0, a), '!options->lower_flrp32'),
1544 (('~ffma@32', a, ('fneg', a), ('fmul', 2.0, a)), ('flrp', a, 1.0, a), '!options->lower_flrp32'),
1545 (('~fmul@32', a, ('fadd', 2.0, ('fneg', a))), ('flrp', a, 1.0, a), '!options->lower_flrp32'),
1546
1547 # we do these late so that we don't get in the way of creating ffmas
1548 (('fmin', ('fadd(is_used_once)', '#c', a), ('fadd(is_used_once)', '#c', b)), ('fadd', c, ('fmin', a, b))),
1549 (('fmax', ('fadd(is_used_once)', '#c', a), ('fadd(is_used_once)', '#c', b)), ('fadd', c, ('fmax', a, b))),
1550
1551 (('bcsel', a, 0, ('b2f32', ('inot', 'b@bool'))), ('b2f32', ('inot', ('ior', a, b)))),
1552
1553 # Things that look like DPH in the source shader may get expanded to
1554 # something that looks like dot(v1.xyz, v2.xyz) + v1.w by the time it gets
1555 # to NIR. After FFMA is generated, this can look like:
1556 #
1557 # fadd(ffma(v1.z, v2.z, ffma(v1.y, v2.y, fmul(v1.x, v2.x))), v1.w)
1558 #
1559 # Reassociate the last addition into the first multiplication.
1560 #
1561 # Some shaders do not use 'invariant' in vertex and (possibly) geometry
1562 # shader stages on some outputs that are intended to be invariant. For
1563 # various reasons, this optimization may not be fully applied in all
1564 # shaders used for different rendering passes of the same geometry. This
1565 # can result in Z-fighting artifacts (at best). For now, disable this
1566 # optimization in these stages. See bugzilla #111490. In tessellation
1567 # stages applications seem to use 'precise' when necessary, so allow the
1568 # optimization in those stages.
1569 (('~fadd', ('ffma(is_used_once)', a, b, ('ffma', c, d, ('fmul', 'e(is_not_const_and_not_fsign)', 'f(is_not_const_and_not_fsign)'))), 'g(is_not_const)'),
1570 ('ffma', a, b, ('ffma', c, d, ('ffma', e, 'f', 'g'))), '(info->stage != MESA_SHADER_VERTEX && info->stage != MESA_SHADER_GEOMETRY) && !options->intel_vec4'),
1571 (('~fadd', ('ffma(is_used_once)', a, b, ('fmul', 'c(is_not_const_and_not_fsign)', 'd(is_not_const_and_not_fsign)') ), 'e(is_not_const)'),
1572 ('ffma', a, b, ('ffma', c, d, e)), '(info->stage != MESA_SHADER_VERTEX && info->stage != MESA_SHADER_GEOMETRY) && !options->intel_vec4'),
1573 ]
1574
1575 print(nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render())
1576 print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_before_ffma",
1577 before_ffma_optimizations).render())
1578 print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_late",
1579 late_optimizations).render())