Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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.    (('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),
  64.    (('imul', a, 0), 0),
  65.    (('fmul', a, 1.0), a),
  66.    (('imul', a, 1), 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.    (('fadd', ('fmul', a, ('fadd', 1.0, ('fneg', c))), ('fmul', b, c)), ('flrp', a, b, c), '!options->lower_flrp'),
  80.    (('fadd', a, ('fmul', c, ('fadd', b, ('fneg', a)))), ('flrp', a, b, c), '!options->lower_flrp'),
  81.    (('ffma', a, b, c), ('fadd', ('fmul', a, b), c), 'options->lower_ffma'),
  82.    (('fadd', ('fmul', a, b), c), ('ffma', a, b, c), '!options->lower_ffma'),
  83.    # Comparison simplifications
  84.    (('inot', ('flt', a, b)), ('fge', a, b)),
  85.    (('inot', ('fge', a, b)), ('flt', a, b)),
  86.    (('inot', ('feq', a, b)), ('fne', a, b)),
  87.    (('inot', ('fne', a, b)), ('feq', a, b)),
  88.    (('inot', ('ilt', a, b)), ('ige', a, b)),
  89.    (('inot', ('ige', a, b)), ('ilt', a, b)),
  90.    (('inot', ('ieq', a, b)), ('ine', a, b)),
  91.    (('inot', ('ine', a, b)), ('ieq', a, b)),
  92.    (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
  93.    (('bcsel', ('flt', a, b), a, b), ('fmin', a, b)),
  94.    (('bcsel', ('flt', a, b), b, a), ('fmax', a, b)),
  95.    (('bcsel', ('inot', 'a@bool'), b, c), ('bcsel', a, c, b)),
  96.    (('bcsel', a, ('bcsel', a, b, c), d), ('bcsel', a, b, d)),
  97.    (('fmin', a, a), a),
  98.    (('fmax', a, a), a),
  99.    (('imin', a, a), a),
  100.    (('imax', a, a), a),
  101.    (('umin', a, a), a),
  102.    (('umax', a, a), a),
  103.    (('fmin', ('fmax', a, 0.0), 1.0), ('fsat', a), '!options->lower_fsat'),
  104.    (('fsat', a), ('fmin', ('fmax', a, 0.0), 1.0), 'options->lower_fsat'),
  105.    (('fsat', ('fsat', a)), ('fsat', a)),
  106.    (('fmin', ('fmax', ('fmin', ('fmax', a, 0.0), 1.0), 0.0), 1.0), ('fmin', ('fmax', a, 0.0), 1.0)),
  107.    (('ior', ('flt', a, b), ('flt', a, c)), ('flt', a, ('fmax', b, c))),
  108.    (('ior', ('flt', a, c), ('flt', b, c)), ('flt', ('fmin', a, b), c)),
  109.    (('ior', ('fge', a, b), ('fge', a, c)), ('fge', a, ('fmin', b, c))),
  110.    (('ior', ('fge', a, c), ('fge', b, c)), ('fge', ('fmax', a, b), c)),
  111.    (('slt', a, b), ('b2f', ('flt', a, b)), 'options->lower_scmp'),
  112.    (('sge', a, b), ('b2f', ('fge', a, b)), 'options->lower_scmp'),
  113.    (('seq', a, b), ('b2f', ('feq', a, b)), 'options->lower_scmp'),
  114.    (('sne', a, b), ('b2f', ('fne', a, b)), 'options->lower_scmp'),
  115.    # Emulating booleans
  116.    (('imul', ('b2i', a), ('b2i', b)), ('b2i', ('iand', a, b))),
  117.    (('fmul', ('b2f', a), ('b2f', b)), ('b2f', ('iand', a, b))),
  118.    (('fsat', ('fadd', ('b2f', a), ('b2f', b))), ('b2f', ('ior', a, b))),
  119.    (('iand', 'a@bool', 1.0), ('b2f', a)),
  120.    (('flt', ('fneg', ('b2f', a)), 0), a), # Generated by TGSI KILL_IF.
  121.    (('flt', ('fsub', 0.0, ('b2f', a)), 0), a), # Generated by TGSI KILL_IF.
  122.    # Comparison with the same args.  Note that these are not done for
  123.    # the float versions because NaN always returns false on float
  124.    # inequalities.
  125.    (('ilt', a, a), False),
  126.    (('ige', a, a), True),
  127.    (('ieq', a, a), True),
  128.    (('ine', a, a), False),
  129.    (('ult', a, a), False),
  130.    (('uge', a, a), True),
  131.    # Logical and bit operations
  132.    (('fand', a, 0.0), 0.0),
  133.    (('iand', a, a), a),
  134.    (('iand', a, 0), 0),
  135.    (('ior', a, a), a),
  136.    (('ior', a, 0), a),
  137.    (('fxor', a, a), 0.0),
  138.    (('ixor', a, a), 0),
  139.    (('inot', ('inot', a)), a),
  140.    # DeMorgan's Laws
  141.    (('iand', ('inot', a), ('inot', b)), ('inot', ('ior',  a, b))),
  142.    (('ior',  ('inot', a), ('inot', b)), ('inot', ('iand', a, b))),
  143.    # Shift optimizations
  144.    (('ishl', 0, a), 0),
  145.    (('ishl', a, 0), a),
  146.    (('ishr', 0, a), 0),
  147.    (('ishr', a, 0), a),
  148.    (('ushr', 0, a), 0),
  149.    (('ushr', a, 0), a),
  150.    # Exponential/logarithmic identities
  151.    (('fexp2', ('flog2', a)), a), # 2^lg2(a) = a
  152.    (('flog2', ('fexp2', a)), a), # lg2(2^a) = a
  153.    (('fpow', a, b), ('fexp2', ('fmul', ('flog2', a), b)), 'options->lower_fpow'), # a^b = 2^(lg2(a)*b)
  154.    (('fexp2', ('fmul', ('flog2', a), b)), ('fpow', a, b), '!options->lower_fpow'), # 2^(lg2(a)*b) = a^b
  155.    (('fpow', a, 1.0), a),
  156.    (('fpow', a, 2.0), ('fmul', a, a)),
  157.    (('fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),
  158.    (('fpow', 2.0, a), ('fexp2', a)),
  159.    (('fsqrt', ('fexp2', a)), ('fexp2', ('fmul', 0.5, a))),
  160.    (('frcp', ('fexp2', a)), ('fexp2', ('fneg', a))),
  161.    (('frsq', ('fexp2', a)), ('fexp2', ('fmul', -0.5, a))),
  162.    (('flog2', ('fsqrt', a)), ('fmul', 0.5, ('flog2', a))),
  163.    (('flog2', ('frcp', a)), ('fneg', ('flog2', a))),
  164.    (('flog2', ('frsq', a)), ('fmul', -0.5, ('flog2', a))),
  165.    (('flog2', ('fpow', a, b)), ('fmul', b, ('flog2', a))),
  166.    (('fadd', ('flog2', a), ('flog2', b)), ('flog2', ('fmul', a, b))),
  167.    (('fadd', ('flog2', a), ('fneg', ('flog2', b))), ('flog2', ('fdiv', a, b))),
  168.    (('fmul', ('fexp2', a), ('fexp2', b)), ('fexp2', ('fadd', a, b))),
  169.    # Division and reciprocal
  170.    (('fdiv', 1.0, a), ('frcp', a)),
  171.    (('frcp', ('frcp', a)), a),
  172.    (('frcp', ('fsqrt', a)), ('frsq', a)),
  173.    (('fsqrt', a), ('frcp', ('frsq', a)), 'options->lower_fsqrt'),
  174.    (('frcp', ('frsq', a)), ('fsqrt', a), '!options->lower_fsqrt'),
  175.    # Boolean simplifications
  176.    (('ine', 'a@bool', 0), 'a'),
  177.    (('ieq', 'a@bool', 0), ('inot', 'a')),
  178.    (('bcsel', a, True, False), ('ine', a, 0)),
  179.    (('bcsel', a, False, True), ('ieq', a, 0)),
  180.    (('bcsel', True, b, c), b),
  181.    (('bcsel', False, b, c), c),
  182.    # The result of this should be hit by constant propagation and, in the
  183.    # next round of opt_algebraic, get picked up by one of the above two.
  184.    (('bcsel', '#a', b, c), ('bcsel', ('ine', 'a', 0), b, c)),
  185.  
  186.    (('bcsel', a, b, b), b),
  187.    (('fcsel', a, b, b), b),
  188.  
  189.    # Conversions
  190.    (('i2b', ('b2i', a)), a),
  191.    (('f2i', ('ftrunc', a)), ('f2i', a)),
  192.    (('f2u', ('ftrunc', a)), ('f2u', a)),
  193.  
  194.    # Subtracts
  195.    (('fsub', a, ('fsub', 0.0, b)), ('fadd', a, b)),
  196.    (('isub', a, ('isub', 0, b)), ('iadd', a, b)),
  197.    (('fsub', a, b), ('fadd', a, ('fneg', b)), 'options->lower_sub'),
  198.    (('isub', a, b), ('iadd', a, ('ineg', b)), 'options->lower_sub'),
  199.    (('fneg', a), ('fsub', 0.0, a), 'options->lower_negate'),
  200.    (('ineg', a), ('isub', 0, a), 'options->lower_negate'),
  201.    (('fadd', a, ('fsub', 0.0, b)), ('fsub', a, b)),
  202.    (('iadd', a, ('isub', 0, b)), ('isub', a, b)),
  203.    (('fabs', ('fsub', 0.0, a)), ('fabs', a)),
  204.    (('iabs', ('isub', 0, a)), ('iabs', a)),
  205. ]
  206.  
  207. # Add optimizations to handle the case where the result of a ternary is
  208. # compared to a constant.  This way we can take things like
  209. #
  210. # (a ? 0 : 1) > 0
  211. #
  212. # and turn it into
  213. #
  214. # a ? (0 > 0) : (1 > 0)
  215. #
  216. # which constant folding will eat for lunch.  The resulting ternary will
  217. # further get cleaned up by the boolean reductions above and we will be
  218. # left with just the original variable "a".
  219. for op in ['flt', 'fge', 'feq', 'fne',
  220.            'ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']:
  221.    optimizations += [
  222.       ((op, ('bcsel', 'a', '#b', '#c'), '#d'),
  223.        ('bcsel', 'a', (op, 'b', 'd'), (op, 'c', 'd'))),
  224.       ((op, '#d', ('bcsel', a, '#b', '#c')),
  225.        ('bcsel', 'a', (op, 'd', 'b'), (op, 'd', 'c'))),
  226.    ]
  227.  
  228. # This section contains "late" optimizations that should be run after the
  229. # regular optimizations have finished.  Optimizations should go here if
  230. # they help code generation but do not necessarily produce code that is
  231. # more easily optimizable.
  232. late_optimizations = [
  233.    (('flt', ('fadd', a, b), 0.0), ('flt', a, ('fneg', b))),
  234.    (('fge', ('fadd', a, b), 0.0), ('fge', a, ('fneg', b))),
  235.    (('feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
  236.    (('fne', ('fadd', a, b), 0.0), ('fne', a, ('fneg', b))),
  237. ]
  238.  
  239. print nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render()
  240. print nir_algebraic.AlgebraicPass("nir_opt_algebraic_late",
  241.                                   late_optimizations).render()
  242.