nir: When faced with a csel on !condition, just flip the arguments.
[mesa.git] / src / glsl / nir / nir_opt_algebraic.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2014 Intel Corporation
4 #
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:
11 #
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
14 # Software.
15 #
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
22 # IN THE SOFTWARE.
23 #
24 # Authors:
25 # Jason Ekstrand (jason@jlekstrand.net)
26
27 import nir_algebraic
28
29 # Convenience variables
30 a = 'a'
31 b = 'b'
32 c = 'c'
33 d = 'd'
34
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.
40 #
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
44 # given output type.
45 #
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
48 # opcodes.
49
50 optimizations = [
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),
58 (('iadd', a, 0), a),
59 (('fmul', a, 0.0), 0.0),
60 (('imul', a, 0), 0),
61 (('fmul', a, 1.0), a),
62 (('imul', a, 1), a),
63 (('fmul', a, -1.0), ('fneg', a)),
64 (('imul', a, -1), ('ineg', a)),
65 (('ffma', 0.0, a, b), b),
66 (('ffma', a, 0.0, b), b),
67 (('ffma', a, b, 0.0), ('fmul', a, b)),
68 (('ffma', a, 1.0, b), ('fadd', a, b)),
69 (('ffma', 1.0, a, b), ('fadd', a, b)),
70 (('flrp', a, b, 0.0), a),
71 (('flrp', a, b, 1.0), b),
72 (('flrp', a, a, b), a),
73 (('flrp', 0.0, a, b), ('fmul', a, b)),
74 (('ffma', a, b, c), ('fadd', ('fmul', a, b), c), 'options->lower_ffma'),
75 (('fadd', ('fmul', a, b), c), ('ffma', a, b, c), '!options->lower_ffma'),
76 # Comparison simplifications
77 (('inot', ('flt', a, b)), ('fge', a, b)),
78 (('inot', ('fge', a, b)), ('flt', a, b)),
79 (('inot', ('ilt', a, b)), ('ige', a, b)),
80 (('inot', ('ige', a, b)), ('ilt', a, b)),
81 (('flt', ('fadd', a, b), 0.0), ('flt', a, ('fneg', b))),
82 (('fge', ('fadd', a, b), 0.0), ('fge', a, ('fneg', b))),
83 (('feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
84 (('fne', ('fadd', a, b), 0.0), ('fne', a, ('fneg', b))),
85 (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
86 (('bcsel', ('flt', a, b), a, b), ('fmin', a, b)),
87 (('bcsel', ('flt', a, b), b, a), ('fmax', a, b)),
88 (('bcsel', ('inot', 'a@bool'), b, c), ('bcsel', a, c, b)),
89 (('fmin', ('fmax', a, 0.0), 1.0), ('fsat', a), '!options->lower_fsat'),
90 (('fsat', a), ('fmin', ('fmax', a, 0.0), 1.0), 'options->lower_fsat'),
91 (('fsat', ('fsat', a)), ('fsat', a)),
92 (('fmin', ('fmax', ('fmin', ('fmax', a, 0.0), 1.0), 0.0), 1.0), ('fmin', ('fmax', a, 0.0), 1.0)),
93 # Comparison with the same args. Note that these are not done for
94 # the float versions because NaN always returns false on float
95 # inequalities.
96 (('ilt', a, a), False),
97 (('ige', a, a), True),
98 (('ieq', a, a), True),
99 (('ine', a, a), False),
100 (('ult', a, a), False),
101 (('uge', a, a), True),
102 # Logical and bit operations
103 (('fand', a, 0.0), 0.0),
104 (('iand', a, a), a),
105 (('iand', a, 0), 0),
106 (('ior', a, a), a),
107 (('ior', a, 0), a),
108 (('fxor', a, a), 0.0),
109 (('ixor', a, a), 0),
110 (('inot', ('inot', a)), a),
111 # DeMorgan's Laws
112 (('iand', ('inot', a), ('inot', b)), ('inot', ('ior', a, b))),
113 (('ior', ('inot', a), ('inot', b)), ('inot', ('iand', a, b))),
114 # Shift optimizations
115 (('ishl', 0, a), 0),
116 (('ishl', a, 0), a),
117 (('ishr', 0, a), 0),
118 (('ishr', a, 0), a),
119 (('ushr', 0, a), 0),
120 (('ushr', a, 0), 0),
121 # Exponential/logarithmic identities
122 (('fexp2', ('flog2', a)), a), # 2^lg2(a) = a
123 (('fexp', ('flog', a)), a), # e^ln(a) = a
124 (('flog2', ('fexp2', a)), a), # lg2(2^a) = a
125 (('flog', ('fexp', a)), a), # ln(e^a) = a
126 (('fpow', a, b), ('fexp2', ('fmul', ('flog2', a), b)), 'options->lower_fpow'), # a^b = 2^(lg2(a)*b)
127 (('fexp2', ('fmul', ('flog2', a), b)), ('fpow', a, b), '!options->lower_fpow'), # 2^(lg2(a)*b) = a^b
128 (('fexp', ('fmul', ('flog', a), b)), ('fpow', a, b), '!options->lower_fpow'), # e^(ln(a)*b) = a^b
129 (('fpow', a, 1.0), a),
130 (('fpow', a, 2.0), ('fmul', a, a)),
131 (('fpow', 2.0, a), ('fexp2', a)),
132 # Division and reciprocal
133 (('fdiv', 1.0, a), ('frcp', a)),
134 (('frcp', ('frcp', a)), a),
135 (('frcp', ('fsqrt', a)), ('frsq', a)),
136 (('fsqrt', a), ('frcp', ('frsq', a)), 'options->lower_fsqrt'),
137 (('frcp', ('frsq', a)), ('fsqrt', a), '!options->lower_fsqrt'),
138 # Boolean simplifications
139 (('ine', 'a@bool', 0), 'a'),
140 (('ieq', 'a@bool', 0), ('inot', 'a')),
141 (('bcsel', a, True, False), ('ine', a, 0)),
142 (('bcsel', a, False, True), ('ieq', a, 0)),
143 (('bcsel', True, b, c), b),
144 (('bcsel', False, b, c), c),
145 # The result of this should be hit by constant propagation and, in the
146 # next round of opt_algebraic, get picked up by one of the above two.
147 (('bcsel', '#a', b, c), ('bcsel', ('ine', 'a', 0), b, c)),
148
149 (('bcsel', a, b, b), b),
150 (('fcsel', a, b, b), b),
151
152 # Subtracts
153 (('fsub', 0.0, ('fsub', 0.0, a)), a),
154 (('isub', 0, ('isub', 0, a)), a),
155 (('fneg', a), ('fsub', 0.0, a), 'options->lower_negate'),
156 (('ineg', a), ('isub', 0, a), 'options->lower_negate'),
157 (('fadd', a, ('fsub', 0.0, b)), ('fsub', a, b)),
158 (('iadd', a, ('isub', 0, b)), ('isub', a, b)),
159 (('fabs', ('fsub', 0.0, a)), ('fabs', a)),
160 (('iabs', ('isub', 0, a)), ('iabs', a)),
161
162 # This one may not be exact
163 (('feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
164 ]
165
166 # Add optimizations to handle the case where the result of a ternary is
167 # compared to a constant. This way we can take things like
168 #
169 # (a ? 0 : 1) > 0
170 #
171 # and turn it into
172 #
173 # a ? (0 > 0) : (1 > 0)
174 #
175 # which constant folding will eat for lunch. The resulting ternary will
176 # further get cleaned up by the boolean reductions above and we will be
177 # left with just the original variable "a".
178 for op in ['flt', 'fge', 'feq', 'fne',
179 'ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']:
180 optimizations += [
181 ((op, ('bcsel', 'a', '#b', '#c'), '#d'),
182 ('bcsel', 'a', (op, 'b', 'd'), (op, 'c', 'd'))),
183 ((op, '#d', ('bcsel', a, '#b', '#c')),
184 ('bcsel', 'a', (op, 'd', 'b'), (op, 'd', 'c'))),
185 ]
186
187 print nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render()