3 # Copyright (C) 2014 Intel Corporation
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 # Jason Ekstrand (jason@jlekstrand.net)
29 # Convenience variables
35 # Written in the form (<search>, <replace>) where <search> is an expression
36 # and <replace> is either an expression or a value. An expression is
37 # defined as a tuple of the form (<op>, <src0>, <src1>, <src2>, <src3>)
38 # where each source is either an expression or a value. A value can be
39 # either a numeric constant or a string representing a variable name.
41 # Variable names are specified as "[#]name[@type]" where "#" inicates that
42 # the given variable will only match constants and the type indicates that
43 # the given variable will only match values from ALU instructions with the
46 # For constants, you have to be careful to make sure that it is the right
47 # type because python is unaware of the source and destination types of the
51 (('fneg', ('fneg', a
)), a
),
52 (('ineg', ('ineg', a
)), a
),
53 (('fabs', ('fabs', a
)), ('fabs', a
)),
54 (('fabs', ('fneg', a
)), ('fabs', a
)),
55 (('iabs', ('iabs', a
)), ('iabs', a
)),
56 (('iabs', ('ineg', a
)), ('iabs', a
)),
57 (('fadd', a
, 0.0), a
),
59 (('usadd_4x8', a
, 0), a
),
60 (('usadd_4x8', a
, ~
0), ~
0),
61 (('fadd', ('fmul', a
, b
), ('fmul', a
, c
)), ('fmul', a
, ('fadd', b
, c
))),
62 (('iadd', ('imul', a
, b
), ('imul', a
, c
)), ('imul', a
, ('iadd', b
, c
))),
63 (('fadd', ('fneg', a
), a
), 0.0),
64 (('iadd', ('ineg', a
), a
), 0),
65 (('fmul', a
, 0.0), 0.0),
67 (('umul_unorm_4x8', a
, 0), 0),
68 (('umul_unorm_4x8', a
, ~
0), a
),
69 (('fmul', a
, 1.0), a
),
71 (('fmul', a
, -1.0), ('fneg', a
)),
72 (('imul', a
, -1), ('ineg', a
)),
73 (('fdiv', a
, b
), ('fmul', a
, ('frcp', b
)), 'options->lower_fdiv'),
74 (('ffma', 0.0, a
, b
), b
),
75 (('ffma', a
, 0.0, b
), b
),
76 (('ffma', a
, b
, 0.0), ('fmul', a
, b
)),
77 (('ffma', a
, 1.0, b
), ('fadd', a
, b
)),
78 (('ffma', 1.0, a
, b
), ('fadd', a
, b
)),
79 (('flrp', a
, b
, 0.0), a
),
80 (('flrp', a
, b
, 1.0), b
),
81 (('flrp', a
, a
, b
), a
),
82 (('flrp', 0.0, a
, b
), ('fmul', a
, b
)),
83 (('flrp', a
, b
, c
), ('fadd', ('fmul', c
, ('fsub', b
, a
)), a
), 'options->lower_flrp'),
84 (('ffract', a
), ('fsub', a
, ('ffloor', a
)), 'options->lower_ffract'),
85 (('fadd', ('fmul', a
, ('fadd', 1.0, ('fneg', c
))), ('fmul', b
, c
)), ('flrp', a
, b
, c
), '!options->lower_flrp'),
86 (('fadd', a
, ('fmul', c
, ('fadd', b
, ('fneg', a
)))), ('flrp', a
, b
, c
), '!options->lower_flrp'),
87 (('ffma', a
, b
, c
), ('fadd', ('fmul', a
, b
), c
), 'options->lower_ffma'),
88 (('fadd', ('fmul', a
, b
), c
), ('ffma', a
, b
, c
), '!options->lower_ffma'),
89 # Comparison simplifications
90 (('inot', ('flt', a
, b
)), ('fge', a
, b
)),
91 (('inot', ('fge', a
, b
)), ('flt', a
, b
)),
92 (('inot', ('feq', a
, b
)), ('fne', a
, b
)),
93 (('inot', ('fne', a
, b
)), ('feq', a
, b
)),
94 (('inot', ('ilt', a
, b
)), ('ige', a
, b
)),
95 (('inot', ('ige', a
, b
)), ('ilt', a
, b
)),
96 (('inot', ('ieq', a
, b
)), ('ine', a
, b
)),
97 (('inot', ('ine', a
, b
)), ('ieq', a
, b
)),
98 (('fge', ('fneg', ('fabs', a
)), 0.0), ('feq', a
, 0.0)),
99 (('bcsel', ('flt', a
, b
), a
, b
), ('fmin', a
, b
)),
100 (('bcsel', ('flt', a
, b
), b
, a
), ('fmax', a
, b
)),
101 (('bcsel', ('inot', 'a@bool'), b
, c
), ('bcsel', a
, c
, b
)),
102 (('bcsel', a
, ('bcsel', a
, b
, c
), d
), ('bcsel', a
, b
, d
)),
109 (('fmin', ('fmax', a
, 0.0), 1.0), ('fsat', a
), '!options->lower_fsat'),
110 (('fmax', ('fmin', a
, 1.0), 0.0), ('fsat', a
), '!options->lower_fsat'),
111 (('fsat', a
), ('fmin', ('fmax', a
, 0.0), 1.0), 'options->lower_fsat'),
112 (('fsat', ('fsat', a
)), ('fsat', a
)),
113 (('fmin', ('fmax', ('fmin', ('fmax', a
, 0.0), 1.0), 0.0), 1.0), ('fmin', ('fmax', a
, 0.0), 1.0)),
114 (('ior', ('flt', a
, b
), ('flt', a
, c
)), ('flt', a
, ('fmax', b
, c
))),
115 (('ior', ('flt', a
, c
), ('flt', b
, c
)), ('flt', ('fmin', a
, b
), c
)),
116 (('ior', ('fge', a
, b
), ('fge', a
, c
)), ('fge', a
, ('fmin', b
, c
))),
117 (('ior', ('fge', a
, c
), ('fge', b
, c
)), ('fge', ('fmax', a
, b
), c
)),
118 (('slt', a
, b
), ('b2f', ('flt', a
, b
)), 'options->lower_scmp'),
119 (('sge', a
, b
), ('b2f', ('fge', a
, b
)), 'options->lower_scmp'),
120 (('seq', a
, b
), ('b2f', ('feq', a
, b
)), 'options->lower_scmp'),
121 (('sne', a
, b
), ('b2f', ('fne', a
, b
)), 'options->lower_scmp'),
122 (('fne', ('fneg', a
), a
), ('fne', a
, 0.0)),
123 (('feq', ('fneg', a
), a
), ('feq', a
, 0.0)),
125 (('imul', ('b2i', a
), ('b2i', b
)), ('b2i', ('iand', a
, b
))),
126 (('fmul', ('b2f', a
), ('b2f', b
)), ('b2f', ('iand', a
, b
))),
127 (('fsat', ('fadd', ('b2f', a
), ('b2f', b
))), ('b2f', ('ior', a
, b
))),
128 (('iand', 'a@bool', 1.0), ('b2f', a
)),
129 (('flt', ('fneg', ('b2f', a
)), 0), a
), # Generated by TGSI KILL_IF.
130 (('flt', ('fsub', 0.0, ('b2f', a
)), 0), a
), # Generated by TGSI KILL_IF.
131 # Comparison with the same args. Note that these are not done for
132 # the float versions because NaN always returns false on float
134 (('ilt', a
, a
), False),
135 (('ige', a
, a
), True),
136 (('ieq', a
, a
), True),
137 (('ine', a
, a
), False),
138 (('ult', a
, a
), False),
139 (('uge', a
, a
), True),
140 # Logical and bit operations
141 (('fand', a
, 0.0), 0.0),
143 (('iand', a
, ~
0), a
),
147 (('fxor', a
, a
), 0.0),
149 (('inot', ('inot', a
)), a
),
151 (('iand', ('inot', a
), ('inot', b
)), ('inot', ('ior', a
, b
))),
152 (('ior', ('inot', a
), ('inot', b
)), ('inot', ('iand', a
, b
))),
153 # Shift optimizations
160 # Exponential/logarithmic identities
161 (('fexp2', ('flog2', a
)), a
), # 2^lg2(a) = a
162 (('flog2', ('fexp2', a
)), a
), # lg2(2^a) = a
163 (('fpow', a
, b
), ('fexp2', ('fmul', ('flog2', a
), b
)), 'options->lower_fpow'), # a^b = 2^(lg2(a)*b)
164 (('fexp2', ('fmul', ('flog2', a
), b
)), ('fpow', a
, b
), '!options->lower_fpow'), # 2^(lg2(a)*b) = a^b
165 (('fpow', a
, 1.0), a
),
166 (('fpow', a
, 2.0), ('fmul', a
, a
)),
167 (('fpow', a
, 4.0), ('fmul', ('fmul', a
, a
), ('fmul', a
, a
))),
168 (('fpow', 2.0, a
), ('fexp2', a
)),
169 (('fpow', ('fpow', a
, 2.2), 0.454545), a
),
170 (('fpow', ('fabs', ('fpow', a
, 2.2)), 0.454545), ('fabs', a
)),
171 (('fsqrt', ('fexp2', a
)), ('fexp2', ('fmul', 0.5, a
))),
172 (('frcp', ('fexp2', a
)), ('fexp2', ('fneg', a
))),
173 (('frsq', ('fexp2', a
)), ('fexp2', ('fmul', -0.5, a
))),
174 (('flog2', ('fsqrt', a
)), ('fmul', 0.5, ('flog2', a
))),
175 (('flog2', ('frcp', a
)), ('fneg', ('flog2', a
))),
176 (('flog2', ('frsq', a
)), ('fmul', -0.5, ('flog2', a
))),
177 (('flog2', ('fpow', a
, b
)), ('fmul', b
, ('flog2', a
))),
178 (('fadd', ('flog2', a
), ('flog2', b
)), ('flog2', ('fmul', a
, b
))),
179 (('fadd', ('flog2', a
), ('fneg', ('flog2', b
))), ('flog2', ('fdiv', a
, b
))),
180 (('fmul', ('fexp2', a
), ('fexp2', b
)), ('fexp2', ('fadd', a
, b
))),
181 # Division and reciprocal
182 (('fdiv', 1.0, a
), ('frcp', a
)),
183 (('frcp', ('frcp', a
)), a
),
184 (('frcp', ('fsqrt', a
)), ('frsq', a
)),
185 (('fsqrt', a
), ('frcp', ('frsq', a
)), 'options->lower_fsqrt'),
186 (('frcp', ('frsq', a
)), ('fsqrt', a
), '!options->lower_fsqrt'),
187 # Boolean simplifications
188 (('ine', 'a@bool', 0), 'a'),
189 (('ieq', 'a@bool', 0), ('inot', 'a')),
190 (('bcsel', a
, True, False), ('ine', a
, 0)),
191 (('bcsel', a
, False, True), ('ieq', a
, 0)),
192 (('bcsel', True, b
, c
), b
),
193 (('bcsel', False, b
, c
), c
),
194 # The result of this should be hit by constant propagation and, in the
195 # next round of opt_algebraic, get picked up by one of the above two.
196 (('bcsel', '#a', b
, c
), ('bcsel', ('ine', 'a', 0), b
, c
)),
198 (('bcsel', a
, b
, b
), b
),
199 (('fcsel', a
, b
, b
), b
),
202 (('i2b', ('b2i', a
)), a
),
203 (('f2i', ('ftrunc', a
)), ('f2i', a
)),
204 (('f2u', ('ftrunc', a
)), ('f2u', a
)),
207 (('fsub', a
, ('fsub', 0.0, b
)), ('fadd', a
, b
)),
208 (('isub', a
, ('isub', 0, b
)), ('iadd', a
, b
)),
209 (('ussub_4x8', a
, 0), a
),
210 (('ussub_4x8', a
, ~
0), 0),
211 (('fsub', a
, b
), ('fadd', a
, ('fneg', b
)), 'options->lower_sub'),
212 (('isub', a
, b
), ('iadd', a
, ('ineg', b
)), 'options->lower_sub'),
213 (('fneg', a
), ('fsub', 0.0, a
), 'options->lower_negate'),
214 (('ineg', a
), ('isub', 0, a
), 'options->lower_negate'),
215 (('fadd', a
, ('fsub', 0.0, b
)), ('fsub', a
, b
)),
216 (('iadd', a
, ('isub', 0, b
)), ('isub', a
, b
)),
217 (('fabs', ('fsub', 0.0, a
)), ('fabs', a
)),
218 (('iabs', ('isub', 0, a
)), ('iabs', a
)),
221 # Add optimizations to handle the case where the result of a ternary is
222 # compared to a constant. This way we can take things like
228 # a ? (0 > 0) : (1 > 0)
230 # which constant folding will eat for lunch. The resulting ternary will
231 # further get cleaned up by the boolean reductions above and we will be
232 # left with just the original variable "a".
233 for op
in ['flt', 'fge', 'feq', 'fne',
234 'ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']:
236 ((op
, ('bcsel', 'a', '#b', '#c'), '#d'),
237 ('bcsel', 'a', (op
, 'b', 'd'), (op
, 'c', 'd'))),
238 ((op
, '#d', ('bcsel', a
, '#b', '#c')),
239 ('bcsel', 'a', (op
, 'd', 'b'), (op
, 'd', 'c'))),
242 # This section contains "late" optimizations that should be run after the
243 # regular optimizations have finished. Optimizations should go here if
244 # they help code generation but do not necessarily produce code that is
245 # more easily optimizable.
246 late_optimizations
= [
247 (('flt', ('fadd', a
, b
), 0.0), ('flt', a
, ('fneg', b
))),
248 (('fge', ('fadd', a
, b
), 0.0), ('fge', a
, ('fneg', b
))),
249 (('feq', ('fadd', a
, b
), 0.0), ('feq', a
, ('fneg', b
))),
250 (('fne', ('fadd', a
, b
), 0.0), ('fne', a
, ('fneg', b
))),
251 (('fdot2', a
, b
), ('fdot_replicated2', a
, b
), 'options->fdot_replicates'),
252 (('fdot3', a
, b
), ('fdot_replicated3', a
, b
), 'options->fdot_replicates'),
253 (('fdot4', a
, b
), ('fdot_replicated4', a
, b
), 'options->fdot_replicates'),
254 (('fdph', a
, b
), ('fdph_replicated', a
, b
), 'options->fdot_replicates'),
257 print nir_algebraic
.AlgebraicPass("nir_opt_algebraic", optimizations
).render()
258 print nir_algebraic
.AlgebraicPass("nir_opt_algebraic_late",
259 late_optimizations
).render()