nir: Add variants of some of the comparison simplifications.
[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. For
40 # constants, you have to be careful to make sure that it is the right type
41 # because python is unaware of the source and destination types of the
42 # opcodes.
43
44 optimizations = [
45 (('fneg', ('fneg', a)), a),
46 (('ineg', ('ineg', a)), a),
47 (('fabs', ('fabs', a)), ('fabs', a)),
48 (('fabs', ('fneg', a)), ('fabs', a)),
49 (('iabs', ('iabs', a)), ('iabs', a)),
50 (('iabs', ('ineg', a)), ('iabs', a)),
51 (('fadd', a, 0.0), a),
52 (('iadd', a, 0), a),
53 (('fmul', a, 0.0), 0.0),
54 (('imul', a, 0), 0),
55 (('fmul', a, 1.0), a),
56 (('imul', a, 1), a),
57 (('fmul', a, -1.0), ('fneg', a)),
58 (('imul', a, -1), ('ineg', a)),
59 (('ffma', 0.0, a, b), b),
60 (('ffma', a, 0.0, b), b),
61 (('ffma', a, b, 0.0), ('fmul', a, b)),
62 (('ffma', a, 1.0, b), ('fadd', a, b)),
63 (('ffma', 1.0, a, b), ('fadd', a, b)),
64 (('flrp', a, b, 0.0), a),
65 (('flrp', a, b, 1.0), b),
66 (('flrp', a, a, b), a),
67 (('flrp', 0.0, a, b), ('fmul', a, b)),
68 (('fadd', ('fmul', a, b), c), ('ffma', a, b, c)),
69 # Comparison simplifications
70 (('inot', ('flt', a, b)), ('fge', a, b)),
71 (('inot', ('fge', a, b)), ('flt', a, b)),
72 (('inot', ('ilt', a, b)), ('ige', a, b)),
73 (('inot', ('ige', a, b)), ('ilt', a, b)),
74 (('ine', ('flt', a, b), 0), ('flt', a, b)),
75 (('ine', ('fge', a, b), 0), ('fge', a, b)),
76 (('ine', ('ilt', a, b), 0), ('ilt', a, b)),
77 (('ine', ('ige', a, b), 0), ('ige', a, b)),
78 (('flt', ('fadd', a, b), 0.0), ('flt', a, ('fneg', b))),
79 (('fge', ('fadd', a, b), 0.0), ('fge', a, ('fneg', b))),
80 (('feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
81 (('fne', ('fadd', a, b), 0.0), ('fne', a, ('fneg', b))),
82 (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
83 (('fmin', ('fmax', a, 1.0), 0.0), ('fsat', a)),
84 # Logical and bit operations
85 (('fand', a, 0.0), 0.0),
86 (('iand', a, a), a),
87 (('iand', a, 0), 0),
88 (('ior', a, a), a),
89 (('ior', a, 0), a),
90 (('fxor', a, a), 0.0),
91 (('ixor', a, a), 0),
92 (('inot', ('inot', a)), a),
93 # DeMorgan's Laws
94 (('iand', ('inot', a), ('inot', b)), ('inot', ('ior', a, b))),
95 (('ior', ('inot', a), ('inot', b)), ('inot', ('iand', a, b))),
96 # Shift optimizations
97 (('ishl', 0, a), 0),
98 (('ishl', a, 0), a),
99 (('ishr', 0, a), 0),
100 (('ishr', a, 0), a),
101 (('ushr', 0, a), 0),
102 (('ushr', a, 0), 0),
103 # Exponential/logarithmic identities
104 (('fexp2', ('flog2', a)), a), # 2^lg2(a) = a
105 (('fexp', ('flog', a)), a), # e^ln(a) = a
106 (('flog2', ('fexp2', a)), a), # lg2(2^a) = a
107 (('flog', ('fexp', a)), a), # ln(e^a) = a
108 (('fexp2', ('fmul', ('flog2', a), b)), ('fpow', a, b)), # 2^(lg2(a)*b) = a^b
109 (('fexp', ('fmul', ('flog', a), b)), ('fpow', a, b)), # e^(ln(a)*b) = a^b
110 (('fpow', a, 1.0), a),
111 (('fpow', a, 2.0), ('fmul', a, a)),
112 (('fpow', 2.0, a), ('fexp2', a)),
113 # Division and reciprocal
114 (('fdiv', 1.0, a), ('frcp', a)),
115 (('frcp', ('frcp', a)), a),
116 (('frcp', ('fsqrt', a)), ('frsq', a)),
117 (('frcp', ('frsq', a)), ('fsqrt', a)),
118
119 # This one may not be exact
120 (('feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
121 ]
122
123 print nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render()