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 (('fadd', ('fmul', a
, b
), ('fmul', a
, c
)), ('fmul', a
, ('fadd', b
, c
))),
60 (('iadd', ('imul', a
, b
), ('imul', a
, c
)), ('imul', a
, ('iadd', b
, c
))),
61 (('fadd', ('fneg', a
), a
), 0.0),
62 (('iadd', ('ineg', a
), a
), 0),
63 (('fmul', a
, 0.0), 0.0),
65 (('fmul', a
, 1.0), a
),
67 (('fmul', a
, -1.0), ('fneg', a
)),
68 (('imul', a
, -1), ('ineg', a
)),
69 (('ffma', 0.0, a
, b
), b
),
70 (('ffma', a
, 0.0, b
), b
),
71 (('ffma', a
, b
, 0.0), ('fmul', a
, b
)),
72 (('ffma', a
, 1.0, b
), ('fadd', a
, b
)),
73 (('ffma', 1.0, a
, b
), ('fadd', a
, b
)),
74 (('flrp', a
, b
, 0.0), a
),
75 (('flrp', a
, b
, 1.0), b
),
76 (('flrp', a
, a
, b
), a
),
77 (('flrp', 0.0, a
, b
), ('fmul', a
, b
)),
78 (('flrp', a
, b
, c
), ('fadd', ('fmul', c
, ('fsub', b
, a
)), a
), 'options->lower_flrp'),
79 (('ffma', a
, b
, c
), ('fadd', ('fmul', a
, b
), c
), 'options->lower_ffma'),
80 (('fadd', ('fmul', a
, b
), c
), ('ffma', a
, b
, c
), '!options->lower_ffma'),
81 # Comparison simplifications
82 (('inot', ('flt', a
, b
)), ('fge', a
, b
)),
83 (('inot', ('fge', a
, b
)), ('flt', a
, b
)),
84 (('inot', ('ilt', a
, b
)), ('ige', a
, b
)),
85 (('inot', ('ige', a
, b
)), ('ilt', a
, b
)),
86 (('flt', ('fadd', a
, b
), 0.0), ('flt', a
, ('fneg', b
))),
87 (('fge', ('fadd', a
, b
), 0.0), ('fge', a
, ('fneg', b
))),
88 (('feq', ('fadd', a
, b
), 0.0), ('feq', a
, ('fneg', b
))),
89 (('fne', ('fadd', a
, b
), 0.0), ('fne', a
, ('fneg', b
))),
90 (('fge', ('fneg', ('fabs', a
)), 0.0), ('feq', a
, 0.0)),
91 (('bcsel', ('flt', a
, b
), a
, b
), ('fmin', a
, b
)),
92 (('bcsel', ('flt', a
, b
), b
, a
), ('fmax', a
, b
)),
93 (('bcsel', ('inot', 'a@bool'), b
, c
), ('bcsel', a
, c
, b
)),
94 (('bcsel', a
, ('bcsel', a
, b
, c
), d
), ('bcsel', a
, b
, d
)),
95 (('fmin', ('fmax', a
, 0.0), 1.0), ('fsat', a
), '!options->lower_fsat'),
96 (('fsat', a
), ('fmin', ('fmax', a
, 0.0), 1.0), 'options->lower_fsat'),
97 (('fsat', ('fsat', a
)), ('fsat', a
)),
98 (('fmin', ('fmax', ('fmin', ('fmax', a
, 0.0), 1.0), 0.0), 1.0), ('fmin', ('fmax', a
, 0.0), 1.0)),
100 (('fmul', ('b2f', a
), ('b2f', b
)), ('b2f', ('iand', a
, b
))),
101 (('fsat', ('fadd', ('b2f', a
), ('b2f', b
))), ('b2f', ('ior', a
, b
))),
102 (('iand', 'a@bool', 1.0), ('b2f', a
)),
103 # Comparison with the same args. Note that these are not done for
104 # the float versions because NaN always returns false on float
106 (('ilt', a
, a
), False),
107 (('ige', a
, a
), True),
108 (('ieq', a
, a
), True),
109 (('ine', a
, a
), False),
110 (('ult', a
, a
), False),
111 (('uge', a
, a
), True),
112 # Logical and bit operations
113 (('fand', a
, 0.0), 0.0),
118 (('fxor', a
, a
), 0.0),
120 (('inot', ('inot', a
)), a
),
122 (('iand', ('inot', a
), ('inot', b
)), ('inot', ('ior', a
, b
))),
123 (('ior', ('inot', a
), ('inot', b
)), ('inot', ('iand', a
, b
))),
124 # Shift optimizations
131 # Exponential/logarithmic identities
132 (('fexp2', ('flog2', a
)), a
), # 2^lg2(a) = a
133 (('fexp', ('flog', a
)), a
), # e^ln(a) = a
134 (('flog2', ('fexp2', a
)), a
), # lg2(2^a) = a
135 (('flog', ('fexp', a
)), a
), # ln(e^a) = a
136 (('fpow', a
, b
), ('fexp2', ('fmul', ('flog2', a
), b
)), 'options->lower_fpow'), # a^b = 2^(lg2(a)*b)
137 (('fexp2', ('fmul', ('flog2', a
), b
)), ('fpow', a
, b
), '!options->lower_fpow'), # 2^(lg2(a)*b) = a^b
138 (('fexp', ('fmul', ('flog', a
), b
)), ('fpow', a
, b
), '!options->lower_fpow'), # e^(ln(a)*b) = a^b
139 (('fpow', a
, 1.0), a
),
140 (('fpow', a
, 2.0), ('fmul', a
, a
)),
141 (('fpow', 2.0, a
), ('fexp2', a
)),
142 # Division and reciprocal
143 (('fdiv', 1.0, a
), ('frcp', a
)),
144 (('frcp', ('frcp', a
)), a
),
145 (('frcp', ('fsqrt', a
)), ('frsq', a
)),
146 (('fsqrt', a
), ('frcp', ('frsq', a
)), 'options->lower_fsqrt'),
147 (('frcp', ('frsq', a
)), ('fsqrt', a
), '!options->lower_fsqrt'),
148 # Boolean simplifications
149 (('ine', 'a@bool', 0), 'a'),
150 (('ieq', 'a@bool', 0), ('inot', 'a')),
151 (('bcsel', a
, True, False), ('ine', a
, 0)),
152 (('bcsel', a
, False, True), ('ieq', a
, 0)),
153 (('bcsel', True, b
, c
), b
),
154 (('bcsel', False, b
, c
), c
),
155 # The result of this should be hit by constant propagation and, in the
156 # next round of opt_algebraic, get picked up by one of the above two.
157 (('bcsel', '#a', b
, c
), ('bcsel', ('ine', 'a', 0), b
, c
)),
159 (('bcsel', a
, b
, b
), b
),
160 (('fcsel', a
, b
, b
), b
),
163 (('fsub', a
, ('fsub', 0.0, b
)), ('fadd', a
, b
)),
164 (('isub', a
, ('isub', 0, b
)), ('iadd', a
, b
)),
165 (('fsub', a
, b
), ('fadd', a
, ('fneg', b
)), '!options->lower_negate'),
166 (('isub', a
, b
), ('iadd', a
, ('ineg', b
)), '!options->lower_negate'),
167 (('fneg', a
), ('fsub', 0.0, a
), 'options->lower_negate'),
168 (('ineg', a
), ('isub', 0, a
), 'options->lower_negate'),
169 (('fadd', a
, ('fsub', 0.0, b
)), ('fsub', a
, b
)),
170 (('iadd', a
, ('isub', 0, b
)), ('isub', a
, b
)),
171 (('fabs', ('fsub', 0.0, a
)), ('fabs', a
)),
172 (('iabs', ('isub', 0, a
)), ('iabs', a
)),
174 # This one may not be exact
175 (('feq', ('fadd', a
, b
), 0.0), ('feq', a
, ('fneg', b
))),
178 # Add optimizations to handle the case where the result of a ternary is
179 # compared to a constant. This way we can take things like
185 # a ? (0 > 0) : (1 > 0)
187 # which constant folding will eat for lunch. The resulting ternary will
188 # further get cleaned up by the boolean reductions above and we will be
189 # left with just the original variable "a".
190 for op
in ['flt', 'fge', 'feq', 'fne',
191 'ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']:
193 ((op
, ('bcsel', 'a', '#b', '#c'), '#d'),
194 ('bcsel', 'a', (op
, 'b', 'd'), (op
, 'c', 'd'))),
195 ((op
, '#d', ('bcsel', a
, '#b', '#c')),
196 ('bcsel', 'a', (op
, 'd', 'b'), (op
, 'd', 'c'))),
199 print nir_algebraic
.AlgebraicPass("nir_opt_algebraic", optimizations
).render()