Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nir_opt_algebraic.py 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. #
  2. # Copyright (C) 2014 Intel Corporation
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a
  5. # copy of this software and associated documentation files (the "Software"),
  6. # to deal in the Software without restriction, including without limitation
  7. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. # and/or sell copies of the Software, and to permit persons to whom the
  9. # Software is furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice (including the next
  12. # paragraph) shall be included in all copies or substantial portions of the
  13. # Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. # IN THE SOFTWARE.
  22. #
  23. # Authors:
  24. # Jason Ekstrand (jason@jlekstrand.net)
  25. from __future__ import print_function
  26. from collections import OrderedDict
  27. import nir_algebraic
  28. import itertools
  29. # Convenience variables
  30. a = 'a'
  31. b = 'b'
  32. c = 'c'
  33. d = 'd'
  34. # Written in the form (<search>, <replace>) where <search> is an expression
  35. # and <replace> is either an expression or a value. An expression is
  36. # defined as a tuple of the form ([~]<op>, <src0>, <src1>, <src2>, <src3>)
  37. # where each source is either an expression or a value. A value can be
  38. # either a numeric constant or a string representing a variable name.
  39. #
  40. # If the opcode in a search expression is prefixed by a '~' character, this
  41. # indicates that the operation is inexact. Such operations will only get
  42. # applied to SSA values that do not have the exact bit set. This should be
  43. # used by by any optimizations that are not bit-for-bit exact. It should not,
  44. # however, be used for backend-requested lowering operations as those need to
  45. # happen regardless of precision.
  46. #
  47. # Variable names are specified as "[#]name[@type][(cond)]" where "#" inicates
  48. # that the given variable will only match constants and the type indicates that
  49. # the given variable will only match values from ALU instructions with the
  50. # given output type, and (cond) specifies an additional condition function
  51. # (see nir_search_helpers.h).
  52. #
  53. # For constants, you have to be careful to make sure that it is the right
  54. # type because python is unaware of the source and destination types of the
  55. # opcodes.
  56. #
  57. # All expression types can have a bit-size specified. For opcodes, this
  58. # looks like "op@32", for variables it is "a@32" or "a@uint32" to specify a
  59. # type and size, and for literals, you can write "2.0@32". In the search half
  60. # of the expression this indicates that it should only match that particular
  61. # bit-size. In the replace half of the expression this indicates that the
  62. # constructed value should have that bit-size.
  63. optimizations = [
  64. (('imul', a, '#b@32(is_pos_power_of_two)'), ('ishl', a, ('find_lsb', b))),
  65. (('imul', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('ishl', a, ('find_lsb', ('iabs', b))))),
  66. (('udiv', a, 1), a),
  67. (('idiv', a, 1), a),
  68. (('umod', a, 1), 0),
  69. (('imod', a, 1), 0),
  70. (('udiv', a, '#b@32(is_pos_power_of_two)'), ('ushr', a, ('find_lsb', b))),
  71. (('idiv', a, '#b@32(is_pos_power_of_two)'), ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', b))), 'options->lower_idiv'),
  72. (('idiv', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', ('iabs', b))))), 'options->lower_idiv'),
  73. (('umod', a, '#b(is_pos_power_of_two)'), ('iand', a, ('isub', b, 1))),
  74. (('fneg', ('fneg', a)), a),
  75. (('ineg', ('ineg', a)), a),
  76. (('fabs', ('fabs', a)), ('fabs', a)),
  77. (('fabs', ('fneg', a)), ('fabs', a)),
  78. (('fabs', ('u2f32', a)), ('u2f32', a)),
  79. (('iabs', ('iabs', a)), ('iabs', a)),
  80. (('iabs', ('ineg', a)), ('iabs', a)),
  81. (('~fadd', a, 0.0), a),
  82. (('iadd', a, 0), a),
  83. (('usadd_4x8', a, 0), a),
  84. (('usadd_4x8', a, ~0), ~0),
  85. (('~fadd', ('fmul', a, b), ('fmul', a, c)), ('fmul', a, ('fadd', b, c))),
  86. (('iadd', ('imul', a, b), ('imul', a, c)), ('imul', a, ('iadd', b, c))),
  87. (('~fadd', ('fneg', a), a), 0.0),
  88. (('iadd', ('ineg', a), a), 0),
  89. (('iadd', ('ineg', a), ('iadd', a, b)), b),
  90. (('iadd', a, ('iadd', ('ineg', a), b)), b),
  91. (('~fadd', ('fneg', a), ('fadd', a, b)), b),
  92. (('~fadd', a, ('fadd', ('fneg', a), b)), b),
  93. (('~fmul', a, 0.0), 0.0),
  94. (('imul', a, 0), 0),
  95. (('umul_unorm_4x8', a, 0), 0),
  96. (('umul_unorm_4x8', a, ~0), a),
  97. (('fmul', a, 1.0), a),
  98. (('imul', a, 1), a),
  99. (('fmul', a, -1.0), ('fneg', a)),
  100. (('imul', a, -1), ('ineg', a)),
  101. # If a < 0: fsign(a)*a*a => -1*a*a => -a*a => abs(a)*a
  102. # If a > 0: fsign(a)*a*a => 1*a*a => a*a => abs(a)*a
  103. # If a == 0: fsign(a)*a*a => 0*0*0 => abs(0)*0
  104. (('fmul', ('fsign', a), ('fmul', a, a)), ('fmul', ('fabs', a), a)),
  105. (('fmul', ('fmul', ('fsign', a), a), a), ('fmul', ('fabs', a), a)),
  106. (('~ffma', 0.0, a, b), b),
  107. (('~ffma', a, 0.0, b), b),
  108. (('~ffma', a, b, 0.0), ('fmul', a, b)),
  109. (('ffma', a, 1.0, b), ('fadd', a, b)),
  110. (('ffma', 1.0, a, b), ('fadd', a, b)),
  111. (('~flrp', a, b, 0.0), a),
  112. (('~flrp', a, b, 1.0), b),
  113. (('~flrp', a, a, b), a),
  114. (('~flrp', 0.0, a, b), ('fmul', a, b)),
  115. (('~flrp', a, b, ('b2f', c)), ('bcsel', c, b, a), 'options->lower_flrp32'),
  116. (('~flrp', a, 0.0, c), ('fadd', ('fmul', ('fneg', a), c), a)),
  117. (('flrp@32', a, b, c), ('fadd', ('fmul', c, ('fsub', b, a)), a), 'options->lower_flrp32'),
  118. (('flrp@64', a, b, c), ('fadd', ('fmul', c, ('fsub', b, a)), a), 'options->lower_flrp64'),
  119. (('ffract', a), ('fsub', a, ('ffloor', a)), 'options->lower_ffract'),
  120. (('~fadd', ('fmul', a, ('fadd', 1.0, ('fneg', ('b2f', c)))), ('fmul', b, ('b2f', c))), ('bcsel', c, b, a), 'options->lower_flrp32'),
  121. (('~fadd@32', ('fmul', a, ('fadd', 1.0, ('fneg', c ))), ('fmul', b, c )), ('flrp', a, b, c), '!options->lower_flrp32'),
  122. (('~fadd@64', ('fmul', a, ('fadd', 1.0, ('fneg', c ))), ('fmul', b, c )), ('flrp', a, b, c), '!options->lower_flrp64'),
  123. (('~fadd', a, ('fmul', ('b2f', c), ('fadd', b, ('fneg', a)))), ('bcsel', c, b, a), 'options->lower_flrp32'),
  124. (('~fadd@32', a, ('fmul', c , ('fadd', b, ('fneg', a)))), ('flrp', a, b, c), '!options->lower_flrp32'),
  125. (('~fadd@64', a, ('fmul', c , ('fadd', b, ('fneg', a)))), ('flrp', a, b, c), '!options->lower_flrp64'),
  126. (('ffma', a, b, c), ('fadd', ('fmul', a, b), c), 'options->lower_ffma'),
  127. (('~fadd', ('fmul', a, b), c), ('ffma', a, b, c), 'options->fuse_ffma'),
  128. (('fdot4', ('vec4', a, b, c, 1.0), d), ('fdph', ('vec3', a, b, c), d)),
  129. (('fdot4', ('vec4', a, 0.0, 0.0, 0.0), b), ('fmul', a, b)),
  130. (('fdot4', ('vec4', a, b, 0.0, 0.0), c), ('fdot2', ('vec2', a, b), c)),
  131. (('fdot4', ('vec4', a, b, c, 0.0), d), ('fdot3', ('vec3', a, b, c), d)),
  132. (('fdot3', ('vec3', a, 0.0, 0.0), b), ('fmul', a, b)),
  133. (('fdot3', ('vec3', a, b, 0.0), c), ('fdot2', ('vec2', a, b), c)),
  134. # (a * #b + #c) << #d
  135. # ((a * #b) << #d) + (#c << #d)
  136. # (a * (#b << #d)) + (#c << #d)
  137. (('ishl', ('iadd', ('imul', a, '#b'), '#c'), '#d'),
  138. ('iadd', ('imul', a, ('ishl', b, d)), ('ishl', c, d))),
  139. # (a * #b) << #c
  140. # a * (#b << #c)
  141. (('ishl', ('imul', a, '#b'), '#c'), ('imul', a, ('ishl', b, c))),
  142. # Comparison simplifications
  143. (('~inot', ('flt', a, b)), ('fge', a, b)),
  144. (('~inot', ('fge', a, b)), ('flt', a, b)),
  145. (('~inot', ('feq', a, b)), ('fne', a, b)),
  146. (('~inot', ('fne', a, b)), ('feq', a, b)),
  147. (('inot', ('ilt', a, b)), ('ige', a, b)),
  148. (('inot', ('ult', a, b)), ('uge', a, b)),
  149. (('inot', ('ige', a, b)), ('ilt', a, b)),
  150. (('inot', ('uge', a, b)), ('ult', a, b)),
  151. (('inot', ('ieq', a, b)), ('ine', a, b)),
  152. (('inot', ('ine', a, b)), ('ieq', a, b)),
  153. # 0.0 >= b2f(a)
  154. # b2f(a) <= 0.0
  155. # b2f(a) == 0.0 because b2f(a) can only be 0 or 1
  156. # inot(a)
  157. (('fge', 0.0, ('b2f', a)), ('inot', a)),
  158. (('fge', ('fneg', ('b2f', a)), 0.0), ('inot', a)),
  159. (('fne', ('fadd', ('b2f', a), ('b2f', b)), 0.0), ('ior', a, b)),
  160. (('fne', ('fmax', ('b2f', a), ('b2f', b)), 0.0), ('ior', a, b)),
  161. (('fne', ('bcsel', a, 1.0, ('b2f', b)) , 0.0), ('ior', a, b)),
  162. (('fne', ('b2f', a), ('fneg', ('b2f', b))), ('ior', a, b)),
  163. (('fne', ('fmul', ('b2f', a), ('b2f', b)), 0.0), ('iand', a, b)),
  164. (('fne', ('fmin', ('b2f', a), ('b2f', b)), 0.0), ('iand', a, b)),
  165. (('fne', ('bcsel', a, ('b2f', b), 0.0) , 0.0), ('iand', a, b)),
  166. (('fne', ('fadd', ('b2f', a), ('fneg', ('b2f', b))), 0.0), ('ixor', a, b)),
  167. (('fne', ('b2f', a) , ('b2f', b) ), ('ixor', a, b)),
  168. (('fne', ('fneg', ('b2f', a)), ('fneg', ('b2f', b))), ('ixor', a, b)),
  169. (('feq', ('fadd', ('b2f', a), ('b2f', b)), 0.0), ('inot', ('ior', a, b))),
  170. (('feq', ('fmax', ('b2f', a), ('b2f', b)), 0.0), ('inot', ('ior', a, b))),
  171. (('feq', ('bcsel', a, 1.0, ('b2f', b)) , 0.0), ('inot', ('ior', a, b))),
  172. (('feq', ('b2f', a), ('fneg', ('b2f', b))), ('inot', ('ior', a, b))),
  173. (('feq', ('fmul', ('b2f', a), ('b2f', b)), 0.0), ('inot', ('iand', a, b))),
  174. (('feq', ('fmin', ('b2f', a), ('b2f', b)), 0.0), ('inot', ('iand', a, b))),
  175. (('feq', ('bcsel', a, ('b2f', b), 0.0) , 0.0), ('inot', ('iand', a, b))),
  176. (('feq', ('fadd', ('b2f', a), ('fneg', ('b2f', b))), 0.0), ('ieq', a, b)),
  177. (('feq', ('b2f', a) , ('b2f', b) ), ('ieq', a, b)),
  178. (('feq', ('fneg', ('b2f', a)), ('fneg', ('b2f', b))), ('ieq', a, b)),
  179. # -(b2f(a) + b2f(b)) < 0
  180. # 0 < b2f(a) + b2f(b)
  181. # 0 != b2f(a) + b2f(b) b2f must be 0 or 1, so the sum is non-negative
  182. # a || b
  183. (('flt', ('fneg', ('fadd', ('b2f', a), ('b2f', b))), 0.0), ('ior', a, b)),
  184. (('flt', 0.0, ('fadd', ('b2f', a), ('b2f', b))), ('ior', a, b)),
  185. # -(b2f(a) + b2f(b)) >= 0
  186. # 0 >= b2f(a) + b2f(b)
  187. # 0 == b2f(a) + b2f(b) b2f must be 0 or 1, so the sum is non-negative
  188. # !(a || b)
  189. (('fge', ('fneg', ('fadd', ('b2f', a), ('b2f', b))), 0.0), ('inot', ('ior', a, b))),
  190. (('fge', 0.0, ('fadd', ('b2f', a), ('b2f', b))), ('inot', ('ior', a, b))),
  191. # Some optimizations (below) convert things like (a < b || c < b) into
  192. # (min(a, c) < b). However, this interfers with the previous optimizations
  193. # that try to remove comparisons with negated sums of b2f. This just
  194. # breaks that apart.
  195. (('flt', ('fmin', c, ('fneg', ('fadd', ('b2f', a), ('b2f', b)))), 0.0),
  196. ('ior', ('flt', c, 0.0), ('ior', a, b))),
  197. (('~flt', ('fadd', a, b), a), ('flt', b, 0.0)),
  198. (('~fge', ('fadd', a, b), a), ('fge', b, 0.0)),
  199. (('~feq', ('fadd', a, b), a), ('feq', b, 0.0)),
  200. (('~fne', ('fadd', a, b), a), ('fne', b, 0.0)),
  201. # Cannot remove the addition from ilt or ige due to overflow.
  202. (('ieq', ('iadd', a, b), a), ('ieq', b, 0)),
  203. (('ine', ('iadd', a, b), a), ('ine', b, 0)),
  204. # fmin(-b2f(a), b) >= 0.0
  205. # -b2f(a) >= 0.0 && b >= 0.0
  206. # -b2f(a) == 0.0 && b >= 0.0 -b2f can only be 0 or -1, never >0
  207. # b2f(a) == 0.0 && b >= 0.0
  208. # a == False && b >= 0.0
  209. # !a && b >= 0.0
  210. #
  211. # The fge in the second replacement is not a typo. I leave the proof that
  212. # "fmin(-b2f(a), b) >= 0 <=> fmin(-b2f(a), b) == 0" as an exercise for the
  213. # reader.
  214. (('fge', ('fmin', ('fneg', ('b2f', a)), b), 0.0), ('iand', ('inot', a), ('fge', b, 0.0))),
  215. (('feq', ('fmin', ('fneg', ('b2f', a)), b), 0.0), ('iand', ('inot', a), ('fge', b, 0.0))),
  216. (('feq', ('b2f', a), 0.0), ('inot', a)),
  217. (('fne', ('b2f', a), 0.0), a),
  218. (('ieq', ('b2i', a), 0), ('inot', a)),
  219. (('ine', ('b2i', a), 0), a),
  220. (('fne', ('u2f32', a), 0.0), ('ine', a, 0)),
  221. (('feq', ('u2f32', a), 0.0), ('ieq', a, 0)),
  222. (('fge', ('u2f32', a), 0.0), True),
  223. (('fge', 0.0, ('u2f32', a)), ('uge', 0, a)), # ieq instead?
  224. (('flt', ('u2f32', a), 0.0), False),
  225. (('flt', 0.0, ('u2f32', a)), ('ult', 0, a)), # ine instead?
  226. (('fne', ('i2f32', a), 0.0), ('ine', a, 0)),
  227. (('feq', ('i2f32', a), 0.0), ('ieq', a, 0)),
  228. (('fge', ('i2f32', a), 0.0), ('ige', a, 0)),
  229. (('fge', 0.0, ('i2f32', a)), ('ige', 0, a)),
  230. (('flt', ('i2f32', a), 0.0), ('ilt', a, 0)),
  231. (('flt', 0.0, ('i2f32', a)), ('ilt', 0, a)),
  232. # 0.0 < fabs(a)
  233. # fabs(a) > 0.0
  234. # fabs(a) != 0.0 because fabs(a) must be >= 0
  235. # a != 0.0
  236. (('~flt', 0.0, ('fabs', a)), ('fne', a, 0.0)),
  237. # -fabs(a) < 0.0
  238. # fabs(a) > 0.0
  239. (('~flt', ('fneg', ('fabs', a)), 0.0), ('fne', a, 0.0)),
  240. # 0.0 >= fabs(a)
  241. # 0.0 == fabs(a) because fabs(a) must be >= 0
  242. # 0.0 == a
  243. (('fge', 0.0, ('fabs', a)), ('feq', a, 0.0)),
  244. # -fabs(a) >= 0.0
  245. # 0.0 >= fabs(a)
  246. (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
  247. (('fmax', ('b2f(is_used_once)', a), ('b2f', b)), ('b2f', ('ior', a, b))),
  248. (('fmax', ('fneg(is_used_once)', ('b2f(is_used_once)', a)), ('fneg', ('b2f', b))), ('fneg', ('b2f', ('ior', a, b)))),
  249. (('fmin', ('b2f(is_used_once)', a), ('b2f', b)), ('b2f', ('iand', a, b))),
  250. (('fmin', ('fneg(is_used_once)', ('b2f(is_used_once)', a)), ('fneg', ('b2f', b))), ('fneg', ('b2f', ('iand', a, b)))),
  251. # fmin(b2f(a), b)
  252. # bcsel(a, fmin(b2f(a), b), fmin(b2f(a), b))
  253. # bcsel(a, fmin(b2f(True), b), fmin(b2f(False), b))
  254. # bcsel(a, fmin(1.0, b), fmin(0.0, b))
  255. #
  256. # Since b is a constant, constant folding will eliminate the fmin and the
  257. # fmax. If b is > 1.0, the bcsel will be replaced with a b2f.
  258. (('fmin', ('b2f', a), '#b'), ('bcsel', a, ('fmin', b, 1.0), ('fmin', b, 0.0))),
  259. (('flt', ('fadd(is_used_once)', a, ('fneg', b)), 0.0), ('flt', a, b)),
  260. (('fge', ('fneg', ('fabs', a)), 0.0), ('feq', a, 0.0)),
  261. (('~bcsel', ('flt', b, a), b, a), ('fmin', a, b)),
  262. (('~bcsel', ('flt', a, b), b, a), ('fmax', a, b)),
  263. (('~bcsel', ('fge', a, b), b, a), ('fmin', a, b)),
  264. (('~bcsel', ('fge', b, a), b, a), ('fmax', a, b)),
  265. (('bcsel', ('inot', a), b, c), ('bcsel', a, c, b)),
  266. (('bcsel', a, ('bcsel', a, b, c), d), ('bcsel', a, b, d)),
  267. (('bcsel', a, b, ('bcsel', a, c, d)), ('bcsel', a, b, d)),
  268. (('bcsel', a, ('bcsel', b, c, d), ('bcsel(is_used_once)', b, c, 'e')), ('bcsel', b, c, ('bcsel', a, d, 'e'))),
  269. (('bcsel', a, ('bcsel(is_used_once)', b, c, d), ('bcsel', b, c, 'e')), ('bcsel', b, c, ('bcsel', a, d, 'e'))),
  270. (('bcsel', a, ('bcsel', b, c, d), ('bcsel(is_used_once)', b, 'e', d)), ('bcsel', b, ('bcsel', a, c, 'e'), d)),
  271. (('bcsel', a, ('bcsel(is_used_once)', b, c, d), ('bcsel', b, 'e', d)), ('bcsel', b, ('bcsel', a, c, 'e'), d)),
  272. (('bcsel', a, True, 'b@bool'), ('ior', a, b)),
  273. (('fmin', a, a), a),
  274. (('fmax', a, a), a),
  275. (('imin', a, a), a),
  276. (('imax', a, a), a),
  277. (('umin', a, a), a),
  278. (('umax', a, a), a),
  279. (('fmax', ('fmax', a, b), b), ('fmax', a, b)),
  280. (('umax', ('umax', a, b), b), ('umax', a, b)),
  281. (('imax', ('imax', a, b), b), ('imax', a, b)),
  282. (('fmin', ('fmin', a, b), b), ('fmin', a, b)),
  283. (('umin', ('umin', a, b), b), ('umin', a, b)),
  284. (('imin', ('imin', a, b), b), ('imin', a, b)),
  285. (('fmax', a, ('fneg', a)), ('fabs', a)),
  286. (('imax', a, ('ineg', a)), ('iabs', a)),
  287. (('fmin', a, ('fneg', a)), ('fneg', ('fabs', a))),
  288. (('imin', a, ('ineg', a)), ('ineg', ('iabs', a))),
  289. (('fmin', a, ('fneg', ('fabs', a))), ('fneg', ('fabs', a))),
  290. (('imin', a, ('ineg', ('iabs', a))), ('ineg', ('iabs', a))),
  291. (('fmin', a, ('fabs', a)), a),
  292. (('imin', a, ('iabs', a)), a),
  293. (('fmax', a, ('fneg', ('fabs', a))), a),
  294. (('imax', a, ('ineg', ('iabs', a))), a),
  295. (('fmax', a, ('fabs', a)), ('fabs', a)),
  296. (('imax', a, ('iabs', a)), ('iabs', a)),
  297. (('fmax', a, ('fneg', a)), ('fabs', a)),
  298. (('imax', a, ('ineg', a)), ('iabs', a)),
  299. (('~fmin', ('fmax', a, 0.0), 1.0), ('fsat', a), '!options->lower_fsat'),
  300. (('~fmax', ('fmin', a, 1.0), 0.0), ('fsat', a), '!options->lower_fsat'),
  301. (('fsat', ('fsign', a)), ('b2f', ('flt', 0.0, a))),
  302. (('fsat', a), ('fmin', ('fmax', a, 0.0), 1.0), 'options->lower_fsat'),
  303. (('fsat', ('fsat', a)), ('fsat', a)),
  304. (('fmin', ('fmax', ('fmin', ('fmax', a, b), c), b), c), ('fmin', ('fmax', a, b), c)),
  305. (('imin', ('imax', ('imin', ('imax', a, b), c), b), c), ('imin', ('imax', a, b), c)),
  306. (('umin', ('umax', ('umin', ('umax', a, b), c), b), c), ('umin', ('umax', a, b), c)),
  307. (('fmax', ('fsat', a), '#b@32(is_zero_to_one)'), ('fsat', ('fmax', a, b))),
  308. (('fmin', ('fsat', a), '#b@32(is_zero_to_one)'), ('fsat', ('fmin', a, b))),
  309. (('extract_u8', ('imin', ('imax', a, 0), 0xff), 0), ('imin', ('imax', a, 0), 0xff)),
  310. (('~ior', ('flt(is_used_once)', a, b), ('flt', a, c)), ('flt', a, ('fmax', b, c))),
  311. (('~ior', ('flt(is_used_once)', a, c), ('flt', b, c)), ('flt', ('fmin', a, b), c)),
  312. (('~ior', ('fge(is_used_once)', a, b), ('fge', a, c)), ('fge', a, ('fmin', b, c))),
  313. (('~ior', ('fge(is_used_once)', a, c), ('fge', b, c)), ('fge', ('fmax', a, b), c)),
  314. (('~ior', ('flt', a, '#b'), ('flt', a, '#c')), ('flt', a, ('fmax', b, c))),
  315. (('~ior', ('flt', '#a', c), ('flt', '#b', c)), ('flt', ('fmin', a, b), c)),
  316. (('~ior', ('fge', a, '#b'), ('fge', a, '#c')), ('fge', a, ('fmin', b, c))),
  317. (('~ior', ('fge', '#a', c), ('fge', '#b', c)), ('fge', ('fmax', a, b), c)),
  318. (('~iand', ('flt(is_used_once)', a, b), ('flt', a, c)), ('flt', a, ('fmin', b, c))),
  319. (('~iand', ('flt(is_used_once)', a, c), ('flt', b, c)), ('flt', ('fmax', a, b), c)),
  320. (('~iand', ('fge(is_used_once)', a, b), ('fge', a, c)), ('fge', a, ('fmax', b, c))),
  321. (('~iand', ('fge(is_used_once)', a, c), ('fge', b, c)), ('fge', ('fmin', a, b), c)),
  322. (('~iand', ('flt', a, '#b'), ('flt', a, '#c')), ('flt', a, ('fmin', b, c))),
  323. (('~iand', ('flt', '#a', c), ('flt', '#b', c)), ('flt', ('fmax', a, b), c)),
  324. (('~iand', ('fge', a, '#b'), ('fge', a, '#c')), ('fge', a, ('fmax', b, c))),
  325. (('~iand', ('fge', '#a', c), ('fge', '#b', c)), ('fge', ('fmin', a, b), c)),
  326. (('ior', ('ilt(is_used_once)', a, b), ('ilt', a, c)), ('ilt', a, ('imax', b, c))),
  327. (('ior', ('ilt(is_used_once)', a, c), ('ilt', b, c)), ('ilt', ('imin', a, b), c)),
  328. (('ior', ('ige(is_used_once)', a, b), ('ige', a, c)), ('ige', a, ('imin', b, c))),
  329. (('ior', ('ige(is_used_once)', a, c), ('ige', b, c)), ('ige', ('imax', a, b), c)),
  330. (('ior', ('ult(is_used_once)', a, b), ('ult', a, c)), ('ult', a, ('umax', b, c))),
  331. (('ior', ('ult(is_used_once)', a, c), ('ult', b, c)), ('ult', ('umin', a, b), c)),
  332. (('ior', ('uge(is_used_once)', a, b), ('uge', a, c)), ('uge', a, ('umin', b, c))),
  333. (('ior', ('uge(is_used_once)', a, c), ('uge', b, c)), ('uge', ('umax', a, b), c)),
  334. (('iand', ('ilt(is_used_once)', a, b), ('ilt', a, c)), ('ilt', a, ('imin', b, c))),
  335. (('iand', ('ilt(is_used_once)', a, c), ('ilt', b, c)), ('ilt', ('imax', a, b), c)),
  336. (('iand', ('ige(is_used_once)', a, b), ('ige', a, c)), ('ige', a, ('imax', b, c))),
  337. (('iand', ('ige(is_used_once)', a, c), ('ige', b, c)), ('ige', ('imin', a, b), c)),
  338. (('iand', ('ult(is_used_once)', a, b), ('ult', a, c)), ('ult', a, ('umin', b, c))),
  339. (('iand', ('ult(is_used_once)', a, c), ('ult', b, c)), ('ult', ('umax', a, b), c)),
  340. (('iand', ('uge(is_used_once)', a, b), ('uge', a, c)), ('uge', a, ('umax', b, c))),
  341. (('iand', ('uge(is_used_once)', a, c), ('uge', b, c)), ('uge', ('umin', a, b), c)),
  342. (('ior', 'a@bool', ('ieq', a, False)), True),
  343. (('ior', 'a@bool', ('inot', a)), True),
  344. (('iand', ('ieq', 'a@32', 0), ('ieq', 'b@32', 0)), ('ieq', ('ior', 'a@32', 'b@32'), 0)),
  345. # These patterns can result when (a < b || a < c) => (a < min(b, c))
  346. # transformations occur before constant propagation and loop-unrolling.
  347. (('~flt', a, ('fmax', b, a)), ('flt', a, b)),
  348. (('~flt', ('fmin', a, b), a), ('flt', b, a)),
  349. (('~fge', a, ('fmin', b, a)), True),
  350. (('~fge', ('fmax', a, b), a), True),
  351. (('~flt', a, ('fmin', b, a)), False),
  352. (('~flt', ('fmax', a, b), a), False),
  353. (('~fge', a, ('fmax', b, a)), ('fge', a, b)),
  354. (('~fge', ('fmin', a, b), a), ('fge', b, a)),
  355. (('ilt', a, ('imax', b, a)), ('ilt', a, b)),
  356. (('ilt', ('imin', a, b), a), ('ilt', b, a)),
  357. (('ige', a, ('imin', b, a)), True),
  358. (('ige', ('imax', a, b), a), True),
  359. (('ult', a, ('umax', b, a)), ('ult', a, b)),
  360. (('ult', ('umin', a, b), a), ('ult', b, a)),
  361. (('uge', a, ('umin', b, a)), True),
  362. (('uge', ('umax', a, b), a), True),
  363. (('ilt', a, ('imin', b, a)), False),
  364. (('ilt', ('imax', a, b), a), False),
  365. (('ige', a, ('imax', b, a)), ('ige', a, b)),
  366. (('ige', ('imin', a, b), a), ('ige', b, a)),
  367. (('ult', a, ('umin', b, a)), False),
  368. (('ult', ('umax', a, b), a), False),
  369. (('uge', a, ('umax', b, a)), ('uge', a, b)),
  370. (('uge', ('umin', a, b), a), ('uge', b, a)),
  371. (('ilt', '#a', ('imax', '#b', c)), ('ior', ('ilt', a, b), ('ilt', a, c))),
  372. (('ilt', ('imin', '#a', b), '#c'), ('ior', ('ilt', a, c), ('ilt', b, c))),
  373. (('ige', '#a', ('imin', '#b', c)), ('ior', ('ige', a, b), ('ige', a, c))),
  374. (('ige', ('imax', '#a', b), '#c'), ('ior', ('ige', a, c), ('ige', b, c))),
  375. (('ult', '#a', ('umax', '#b', c)), ('ior', ('ult', a, b), ('ult', a, c))),
  376. (('ult', ('umin', '#a', b), '#c'), ('ior', ('ult', a, c), ('ult', b, c))),
  377. (('uge', '#a', ('umin', '#b', c)), ('ior', ('uge', a, b), ('uge', a, c))),
  378. (('uge', ('umax', '#a', b), '#c'), ('ior', ('uge', a, c), ('uge', b, c))),
  379. (('ilt', '#a', ('imin', '#b', c)), ('iand', ('ilt', a, b), ('ilt', a, c))),
  380. (('ilt', ('imax', '#a', b), '#c'), ('iand', ('ilt', a, c), ('ilt', b, c))),
  381. (('ige', '#a', ('imax', '#b', c)), ('iand', ('ige', a, b), ('ige', a, c))),
  382. (('ige', ('imin', '#a', b), '#c'), ('iand', ('ige', a, c), ('ige', b, c))),
  383. (('ult', '#a', ('umin', '#b', c)), ('iand', ('ult', a, b), ('ult', a, c))),
  384. (('ult', ('umax', '#a', b), '#c'), ('iand', ('ult', a, c), ('ult', b, c))),
  385. (('uge', '#a', ('umax', '#b', c)), ('iand', ('uge', a, b), ('uge', a, c))),
  386. (('uge', ('umin', '#a', b), '#c'), ('iand', ('uge', a, c), ('uge', b, c))),
  387. (('fabs', ('slt', a, b)), ('slt', a, b)),
  388. (('fabs', ('sge', a, b)), ('sge', a, b)),
  389. (('fabs', ('seq', a, b)), ('seq', a, b)),
  390. (('fabs', ('sne', a, b)), ('sne', a, b)),
  391. (('slt', a, b), ('b2f', ('flt', a, b)), 'options->lower_scmp'),
  392. (('sge', a, b), ('b2f', ('fge', a, b)), 'options->lower_scmp'),
  393. (('seq', a, b), ('b2f', ('feq', a, b)), 'options->lower_scmp'),
  394. (('sne', a, b), ('b2f', ('fne', a, b)), 'options->lower_scmp'),
  395. (('fne', ('fneg', a), a), ('fne', a, 0.0)),
  396. (('feq', ('fneg', a), a), ('feq', a, 0.0)),
  397. # Emulating booleans
  398. (('imul', ('b2i', a), ('b2i', b)), ('b2i', ('iand', a, b))),
  399. (('fmul', ('b2f', a), ('b2f', b)), ('b2f', ('iand', a, b))),
  400. (('fsat', ('fadd', ('b2f', a), ('b2f', b))), ('b2f', ('ior', a, b))),
  401. (('iand', 'a@bool', 1.0), ('b2f', a), '!options->lower_b2f'),
  402. # True/False are ~0 and 0 in NIR. b2i of True is 1, and -1 is ~0 (True).
  403. (('ineg', ('b2i@32', a)), a),
  404. (('flt', ('fneg', ('b2f', a)), 0), a), # Generated by TGSI KILL_IF.
  405. (('flt', ('fsub', 0.0, ('b2f', a)), 0), a), # Generated by TGSI KILL_IF.
  406. # Comparison with the same args. Note that these are not done for
  407. # the float versions because NaN always returns false on float
  408. # inequalities.
  409. (('ilt', a, a), False),
  410. (('ige', a, a), True),
  411. (('ieq', a, a), True),
  412. (('ine', a, a), False),
  413. (('ult', a, a), False),
  414. (('uge', a, a), True),
  415. # Logical and bit operations
  416. (('fand', a, 0.0), 0.0),
  417. (('iand', a, a), a),
  418. (('iand', a, ~0), a),
  419. (('iand', a, 0), 0),
  420. (('ior', a, a), a),
  421. (('ior', a, 0), a),
  422. (('ior', a, True), True),
  423. (('fxor', a, a), 0.0),
  424. (('ixor', a, a), 0),
  425. (('ixor', a, 0), a),
  426. (('inot', ('inot', a)), a),
  427. (('ior', ('iand', a, b), b), b),
  428. (('ior', ('ior', a, b), b), ('ior', a, b)),
  429. (('iand', ('ior', a, b), b), b),
  430. (('iand', ('iand', a, b), b), ('iand', a, b)),
  431. # DeMorgan's Laws
  432. (('iand', ('inot', a), ('inot', b)), ('inot', ('ior', a, b))),
  433. (('ior', ('inot', a), ('inot', b)), ('inot', ('iand', a, b))),
  434. # Shift optimizations
  435. (('ishl', 0, a), 0),
  436. (('ishl', a, 0), a),
  437. (('ishr', 0, a), 0),
  438. (('ishr', a, 0), a),
  439. (('ushr', 0, a), 0),
  440. (('ushr', a, 0), a),
  441. (('iand', 0xff, ('ushr@32', a, 24)), ('ushr', a, 24)),
  442. (('iand', 0xffff, ('ushr@32', a, 16)), ('ushr', a, 16)),
  443. # Exponential/logarithmic identities
  444. (('~fexp2', ('flog2', a)), a), # 2^lg2(a) = a
  445. (('~flog2', ('fexp2', a)), a), # lg2(2^a) = a
  446. (('fpow', a, b), ('fexp2', ('fmul', ('flog2', a), b)), 'options->lower_fpow'), # a^b = 2^(lg2(a)*b)
  447. (('~fexp2', ('fmul', ('flog2', a), b)), ('fpow', a, b), '!options->lower_fpow'), # 2^(lg2(a)*b) = a^b
  448. (('~fexp2', ('fadd', ('fmul', ('flog2', a), b), ('fmul', ('flog2', c), d))),
  449. ('~fmul', ('fpow', a, b), ('fpow', c, d)), '!options->lower_fpow'), # 2^(lg2(a) * b + lg2(c) + d) = a^b * c^d
  450. (('~fexp2', ('fmul', ('flog2', a), 2.0)), ('fmul', a, a)),
  451. (('~fexp2', ('fmul', ('flog2', a), 4.0)), ('fmul', ('fmul', a, a), ('fmul', a, a))),
  452. (('~fpow', a, 1.0), a),
  453. (('~fpow', a, 2.0), ('fmul', a, a)),
  454. (('~fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),
  455. (('~fpow', 2.0, a), ('fexp2', a)),
  456. (('~fpow', ('fpow', a, 2.2), 0.454545), a),
  457. (('~fpow', ('fabs', ('fpow', a, 2.2)), 0.454545), ('fabs', a)),
  458. (('~fsqrt', ('fexp2', a)), ('fexp2', ('fmul', 0.5, a))),
  459. (('~frcp', ('fexp2', a)), ('fexp2', ('fneg', a))),
  460. (('~frsq', ('fexp2', a)), ('fexp2', ('fmul', -0.5, a))),
  461. (('~flog2', ('fsqrt', a)), ('fmul', 0.5, ('flog2', a))),
  462. (('~flog2', ('frcp', a)), ('fneg', ('flog2', a))),
  463. (('~flog2', ('frsq', a)), ('fmul', -0.5, ('flog2', a))),
  464. (('~flog2', ('fpow', a, b)), ('fmul', b, ('flog2', a))),
  465. (('~fmul', ('fexp2(is_used_once)', a), ('fexp2(is_used_once)', b)), ('fexp2', ('fadd', a, b))),
  466. # Division and reciprocal
  467. (('~fdiv', 1.0, a), ('frcp', a)),
  468. (('fdiv', a, b), ('fmul', a, ('frcp', b)), 'options->lower_fdiv'),
  469. (('~frcp', ('frcp', a)), a),
  470. (('~frcp', ('fsqrt', a)), ('frsq', a)),
  471. (('fsqrt', a), ('frcp', ('frsq', a)), 'options->lower_fsqrt'),
  472. (('~frcp', ('frsq', a)), ('fsqrt', a), '!options->lower_fsqrt'),
  473. # Boolean simplifications
  474. (('ieq', 'a@bool', True), a),
  475. (('ine(is_not_used_by_if)', 'a@bool', True), ('inot', a)),
  476. (('ine', 'a@bool', False), a),
  477. (('ieq(is_not_used_by_if)', 'a@bool', False), ('inot', 'a')),
  478. (('bcsel', a, True, False), a),
  479. (('bcsel', a, False, True), ('inot', a)),
  480. (('bcsel@32', a, 1.0, 0.0), ('b2f', a)),
  481. (('bcsel@32', a, 0.0, 1.0), ('b2f', ('inot', a))),
  482. (('bcsel@32', a, -1.0, -0.0), ('fneg', ('b2f', a))),
  483. (('bcsel@32', a, -0.0, -1.0), ('fneg', ('b2f', ('inot', a)))),
  484. (('bcsel', True, b, c), b),
  485. (('bcsel', False, b, c), c),
  486. (('bcsel', a, ('b2f(is_used_once)', b), ('b2f', c)), ('b2f', ('bcsel', a, b, c))),
  487. # The result of this should be hit by constant propagation and, in the
  488. # next round of opt_algebraic, get picked up by one of the above two.
  489. (('bcsel', '#a', b, c), ('bcsel', ('ine', 'a', 0), b, c)),
  490. (('bcsel', a, b, b), b),
  491. (('fcsel', a, b, b), b),
  492. # Conversions
  493. (('i2b', ('b2i', a)), a),
  494. (('i2b', 'a@bool'), a),
  495. (('f2i32', ('ftrunc', a)), ('f2i32', a)),
  496. (('f2u32', ('ftrunc', a)), ('f2u32', a)),
  497. (('i2b', ('ineg', a)), ('i2b', a)),
  498. (('i2b', ('iabs', a)), ('i2b', a)),
  499. (('fabs', ('b2f', a)), ('b2f', a)),
  500. (('iabs', ('b2i', a)), ('b2i', a)),
  501. (('inot', ('f2b', a)), ('feq', a, 0.0)),
  502. # Ironically, mark these as imprecise because removing the conversions may
  503. # preserve more precision than doing the conversions (e.g.,
  504. # uint(float(0x81818181u)) == 0x81818200).
  505. (('~f2i32', ('i2f32', 'a@32')), a),
  506. (('~f2i32', ('u2f32', 'a@32')), a),
  507. (('~f2u32', ('i2f32', 'a@32')), a),
  508. (('~f2u32', ('u2f32', 'a@32')), a),
  509. # Packing and then unpacking does nothing
  510. (('unpack_64_2x32_split_x', ('pack_64_2x32_split', a, b)), a),
  511. (('unpack_64_2x32_split_y', ('pack_64_2x32_split', a, b)), b),
  512. (('pack_64_2x32_split', ('unpack_64_2x32_split_x', a),
  513. ('unpack_64_2x32_split_y', a)), a),
  514. # Byte extraction
  515. (('ushr', ('ishl', 'a@32', 24), 24), ('extract_u8', a, 0), '!options->lower_extract_byte'),
  516. (('ushr', ('ishl', 'a@32', 16), 24), ('extract_u8', a, 1), '!options->lower_extract_byte'),
  517. (('ushr', ('ishl', 'a@32', 8), 24), ('extract_u8', a, 2), '!options->lower_extract_byte'),
  518. (('ushr', 'a@32', 24), ('extract_u8', a, 3), '!options->lower_extract_byte'),
  519. (('ishr', ('ishl', 'a@32', 24), 24), ('extract_i8', a, 0), '!options->lower_extract_byte'),
  520. (('ishr', ('ishl', 'a@32', 16), 24), ('extract_i8', a, 1), '!options->lower_extract_byte'),
  521. (('ishr', ('ishl', 'a@32', 8), 24), ('extract_i8', a, 2), '!options->lower_extract_byte'),
  522. (('ishr', 'a@32', 24), ('extract_i8', a, 3), '!options->lower_extract_byte'),
  523. (('iand', 0xff, ('ushr', a, 16)), ('extract_u8', a, 2), '!options->lower_extract_byte'),
  524. (('iand', 0xff, ('ushr', a, 8)), ('extract_u8', a, 1), '!options->lower_extract_byte'),
  525. (('iand', 0xff, a), ('extract_u8', a, 0), '!options->lower_extract_byte'),
  526. # Word extraction
  527. (('ushr', ('ishl', 'a@32', 16), 16), ('extract_u16', a, 0), '!options->lower_extract_word'),
  528. (('ushr', 'a@32', 16), ('extract_u16', a, 1), '!options->lower_extract_word'),
  529. (('ishr', ('ishl', 'a@32', 16), 16), ('extract_i16', a, 0), '!options->lower_extract_word'),
  530. (('ishr', 'a@32', 16), ('extract_i16', a, 1), '!options->lower_extract_word'),
  531. (('iand', 0xffff, a), ('extract_u16', a, 0), '!options->lower_extract_word'),
  532. # Subtracts
  533. (('~fsub', a, ('fsub', 0.0, b)), ('fadd', a, b)),
  534. (('isub', a, ('isub', 0, b)), ('iadd', a, b)),
  535. (('ussub_4x8', a, 0), a),
  536. (('ussub_4x8', a, ~0), 0),
  537. (('fsub', a, b), ('fadd', a, ('fneg', b)), 'options->lower_sub'),
  538. (('isub', a, b), ('iadd', a, ('ineg', b)), 'options->lower_sub'),
  539. (('fneg', a), ('fsub', 0.0, a), 'options->lower_negate'),
  540. (('ineg', a), ('isub', 0, a), 'options->lower_negate'),
  541. (('~fadd', a, ('fsub', 0.0, b)), ('fsub', a, b)),
  542. (('iadd', a, ('isub', 0, b)), ('isub', a, b)),
  543. (('fabs', ('fsub', 0.0, a)), ('fabs', a)),
  544. (('iabs', ('isub', 0, a)), ('iabs', a)),
  545. # Propagate negation up multiplication chains
  546. (('fmul', ('fneg', a), b), ('fneg', ('fmul', a, b))),
  547. (('imul', ('ineg', a), b), ('ineg', ('imul', a, b))),
  548. # Propagate constants up multiplication chains
  549. (('~fmul(is_used_once)', ('fmul(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('fmul', ('fmul', a, c), b)),
  550. (('imul(is_used_once)', ('imul(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('imul', ('imul', a, c), b)),
  551. (('~fadd(is_used_once)', ('fadd(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('fadd', ('fadd', a, c), b)),
  552. (('iadd(is_used_once)', ('iadd(is_used_once)', 'a(is_not_const)', 'b(is_not_const)'), '#c'), ('iadd', ('iadd', a, c), b)),
  553. # Reassociate constants in add/mul chains so they can be folded together.
  554. # For now, we mostly only handle cases where the constants are separated by
  555. # a single non-constant. We could do better eventually.
  556. (('~fmul', '#a', ('fmul', b, '#c')), ('fmul', ('fmul', a, c), b)),
  557. (('imul', '#a', ('imul', b, '#c')), ('imul', ('imul', a, c), b)),
  558. (('~fadd', '#a', ('fadd', b, '#c')), ('fadd', ('fadd', a, c), b)),
  559. (('~fadd', '#a', ('fneg', ('fadd', b, '#c'))), ('fadd', ('fadd', a, ('fneg', c)), ('fneg', b))),
  560. (('iadd', '#a', ('iadd', b, '#c')), ('iadd', ('iadd', a, c), b)),
  561. # By definition...
  562. (('bcsel', ('ige', ('find_lsb', a), 0), ('find_lsb', a), -1), ('find_lsb', a)),
  563. (('bcsel', ('ige', ('ifind_msb', a), 0), ('ifind_msb', a), -1), ('ifind_msb', a)),
  564. (('bcsel', ('ige', ('ufind_msb', a), 0), ('ufind_msb', a), -1), ('ufind_msb', a)),
  565. (('bcsel', ('ine', a, 0), ('find_lsb', a), -1), ('find_lsb', a)),
  566. (('bcsel', ('ine', a, 0), ('ifind_msb', a), -1), ('ifind_msb', a)),
  567. (('bcsel', ('ine', a, 0), ('ufind_msb', a), -1), ('ufind_msb', a)),
  568. (('bcsel', ('ine', a, -1), ('ifind_msb', a), -1), ('ifind_msb', a)),
  569. # Misc. lowering
  570. (('fmod@32', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod32'),
  571. (('fmod@64', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod64'),
  572. (('frem', a, b), ('fsub', a, ('fmul', b, ('ftrunc', ('fdiv', a, b)))), 'options->lower_fmod32'),
  573. (('uadd_carry@32', a, b), ('b2i', ('ult', ('iadd', a, b), a)), 'options->lower_uadd_carry'),
  574. (('usub_borrow@32', a, b), ('b2i', ('ult', a, b)), 'options->lower_usub_borrow'),
  575. (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
  576. ('bcsel', ('ilt', 31, 'bits'), 'insert',
  577. ('bfi', ('bfm', 'bits', 'offset'), 'insert', 'base')),
  578. 'options->lower_bitfield_insert'),
  579. # Alternative lowering that doesn't rely on bfi.
  580. (('bitfield_insert', 'base', 'insert', 'offset', 'bits'),
  581. ('bcsel', ('ilt', 31, 'bits'),
  582. 'insert',
  583. ('ior',
  584. ('iand', 'base', ('inot', ('bfm', 'bits', 'offset'))),
  585. ('iand', ('ishl', 'insert', 'offset'), ('bfm', 'bits', 'offset')))),
  586. 'options->lower_bitfield_insert_to_shifts'),
  587. # bfm lowering -- note that the NIR opcode is undefined if either arg is 32.
  588. (('bfm', 'bits', 'offset'),
  589. ('ishl', ('isub', ('ishl', 1, 'bits'), 1), 'offset'),
  590. 'options->lower_bfm'),
  591. (('ibitfield_extract', 'value', 'offset', 'bits'),
  592. ('bcsel', ('ilt', 31, 'bits'), 'value',
  593. ('ibfe', 'value', 'offset', 'bits')),
  594. 'options->lower_bitfield_extract'),
  595. (('ubitfield_extract', 'value', 'offset', 'bits'),
  596. ('bcsel', ('ult', 31, 'bits'), 'value',
  597. ('ubfe', 'value', 'offset', 'bits')),
  598. 'options->lower_bitfield_extract'),
  599. (('ibitfield_extract', 'value', 'offset', 'bits'),
  600. ('bcsel', ('ieq', 0, 'bits'),
  601. 0,
  602. ('ishr',
  603. ('ishl', 'value', ('isub', ('isub', 32, 'bits'), 'offset')),
  604. ('isub', 32, 'bits'))),
  605. 'options->lower_bitfield_extract_to_shifts'),
  606. (('ubitfield_extract', 'value', 'offset', 'bits'),
  607. ('iand',
  608. ('ushr', 'value', 'offset'),
  609. ('bcsel', ('ieq', 'bits', 32),
  610. 0xffffffff,
  611. ('bfm', 'bits', 0))),
  612. 'options->lower_bitfield_extract_to_shifts'),
  613. (('ifind_msb', 'value'),
  614. ('ufind_msb', ('bcsel', ('ilt', 'value', 0), ('inot', 'value'), 'value')),
  615. 'options->lower_ifind_msb'),
  616. (('find_lsb', 'value'),
  617. ('ufind_msb', ('iand', 'value', ('ineg', 'value'))),
  618. 'options->lower_find_lsb'),
  619. (('extract_i8', a, 'b@32'),
  620. ('ishr', ('ishl', a, ('imul', ('isub', 3, b), 8)), 24),
  621. 'options->lower_extract_byte'),
  622. (('extract_u8', a, 'b@32'),
  623. ('iand', ('ushr', a, ('imul', b, 8)), 0xff),
  624. 'options->lower_extract_byte'),
  625. (('extract_i16', a, 'b@32'),
  626. ('ishr', ('ishl', a, ('imul', ('isub', 1, b), 16)), 16),
  627. 'options->lower_extract_word'),
  628. (('extract_u16', a, 'b@32'),
  629. ('iand', ('ushr', a, ('imul', b, 16)), 0xffff),
  630. 'options->lower_extract_word'),
  631. (('pack_unorm_2x16', 'v'),
  632. ('pack_uvec2_to_uint',
  633. ('f2u32', ('fround_even', ('fmul', ('fsat', 'v'), 65535.0)))),
  634. 'options->lower_pack_unorm_2x16'),
  635. (('pack_unorm_4x8', 'v'),
  636. ('pack_uvec4_to_uint',
  637. ('f2u32', ('fround_even', ('fmul', ('fsat', 'v'), 255.0)))),
  638. 'options->lower_pack_unorm_4x8'),
  639. (('pack_snorm_2x16', 'v'),
  640. ('pack_uvec2_to_uint',
  641. ('f2i32', ('fround_even', ('fmul', ('fmin', 1.0, ('fmax', -1.0, 'v')), 32767.0)))),
  642. 'options->lower_pack_snorm_2x16'),
  643. (('pack_snorm_4x8', 'v'),
  644. ('pack_uvec4_to_uint',
  645. ('f2i32', ('fround_even', ('fmul', ('fmin', 1.0, ('fmax', -1.0, 'v')), 127.0)))),
  646. 'options->lower_pack_snorm_4x8'),
  647. (('unpack_unorm_2x16', 'v'),
  648. ('fdiv', ('u2f32', ('vec2', ('extract_u16', 'v', 0),
  649. ('extract_u16', 'v', 1))),
  650. 65535.0),
  651. 'options->lower_unpack_unorm_2x16'),
  652. (('unpack_unorm_4x8', 'v'),
  653. ('fdiv', ('u2f32', ('vec4', ('extract_u8', 'v', 0),
  654. ('extract_u8', 'v', 1),
  655. ('extract_u8', 'v', 2),
  656. ('extract_u8', 'v', 3))),
  657. 255.0),
  658. 'options->lower_unpack_unorm_4x8'),
  659. (('unpack_snorm_2x16', 'v'),
  660. ('fmin', 1.0, ('fmax', -1.0, ('fdiv', ('i2f32', ('vec2', ('extract_i16', 'v', 0),
  661. ('extract_i16', 'v', 1))),
  662. 32767.0))),
  663. 'options->lower_unpack_snorm_2x16'),
  664. (('unpack_snorm_4x8', 'v'),
  665. ('fmin', 1.0, ('fmax', -1.0, ('fdiv', ('i2f32', ('vec4', ('extract_i8', 'v', 0),
  666. ('extract_i8', 'v', 1),
  667. ('extract_i8', 'v', 2),
  668. ('extract_i8', 'v', 3))),
  669. 127.0))),
  670. 'options->lower_unpack_snorm_4x8'),
  671. ]
  672. invert = OrderedDict([('feq', 'fne'), ('fne', 'feq'), ('fge', 'flt'), ('flt', 'fge')])
  673. for left, right in itertools.combinations_with_replacement(invert.keys(), 2):
  674. optimizations.append((('inot', ('ior(is_used_once)', (left, a, b), (right, c, d))),
  675. ('iand', (invert[left], a, b), (invert[right], c, d))))
  676. optimizations.append((('inot', ('iand(is_used_once)', (left, a, b), (right, c, d))),
  677. ('ior', (invert[left], a, b), (invert[right], c, d))))
  678. # Optimize x2yN(b2x(x)) -> b2y
  679. optimizations.append((('f2b', ('b2f', a)), a))
  680. optimizations.append((('i2b', ('b2i', a)), a))
  681. for x, y in itertools.product(['f', 'u', 'i'], ['f', 'u', 'i']):
  682. if x != 'f' and y != 'f' and x != y:
  683. continue
  684. b2x = 'b2f' if x == 'f' else 'b2i'
  685. b2y = 'b2f' if y == 'f' else 'b2i'
  686. for N in [8, 16, 32, 64]:
  687. if y == 'f' and N == 8:
  688. continue
  689. x2yN = '{}2{}{}'.format(x, y, N)
  690. optimizations.append(((x2yN, (b2x, a)), (b2y, a)))
  691. def fexp2i(exp, bits):
  692. # We assume that exp is already in the right range.
  693. if bits == 32:
  694. return ('ishl', ('iadd', exp, 127), 23)
  695. elif bits == 64:
  696. return ('pack_64_2x32_split', 0, ('ishl', ('iadd', exp, 1023), 20))
  697. else:
  698. assert False
  699. def ldexp(f, exp, bits):
  700. # First, we clamp exp to a reasonable range. The maximum possible range
  701. # for a normal exponent is [-126, 127] and, throwing in denormals, you get
  702. # a maximum range of [-149, 127]. This means that we can potentially have
  703. # a swing of +-276. If you start with FLT_MAX, you actually have to do
  704. # ldexp(FLT_MAX, -278) to get it to flush all the way to zero. The GLSL
  705. # spec, on the other hand, only requires that we handle an exponent value
  706. # in the range [-126, 128]. This implementation is *mostly* correct; it
  707. # handles a range on exp of [-252, 254] which allows you to create any
  708. # value (including denorms if the hardware supports it) and to adjust the
  709. # exponent of any normal value to anything you want.
  710. if bits == 32:
  711. exp = ('imin', ('imax', exp, -252), 254)
  712. elif bits == 64:
  713. exp = ('imin', ('imax', exp, -2044), 2046)
  714. else:
  715. assert False
  716. # Now we compute two powers of 2, one for exp/2 and one for exp-exp/2.
  717. # (We use ishr which isn't the same for -1, but the -1 case still works
  718. # since we use exp-exp/2 as the second exponent.) While the spec
  719. # technically defines ldexp as f * 2.0^exp, simply multiplying once doesn't
  720. # work with denormals and doesn't allow for the full swing in exponents
  721. # that you can get with normalized values. Instead, we create two powers
  722. # of two and multiply by them each in turn. That way the effective range
  723. # of our exponent is doubled.
  724. pow2_1 = fexp2i(('ishr', exp, 1), bits)
  725. pow2_2 = fexp2i(('isub', exp, ('ishr', exp, 1)), bits)
  726. return ('fmul', ('fmul', f, pow2_1), pow2_2)
  727. optimizations += [
  728. (('ldexp@32', 'x', 'exp'), ldexp('x', 'exp', 32), 'options->lower_ldexp'),
  729. (('ldexp@64', 'x', 'exp'), ldexp('x', 'exp', 64), 'options->lower_ldexp'),
  730. ]
  731. # Unreal Engine 4 demo applications open-codes bitfieldReverse()
  732. def bitfield_reverse(u):
  733. step1 = ('ior', ('ishl', u, 16), ('ushr', u, 16))
  734. step2 = ('ior', ('ishl', ('iand', step1, 0x00ff00ff), 8), ('ushr', ('iand', step1, 0xff00ff00), 8))
  735. step3 = ('ior', ('ishl', ('iand', step2, 0x0f0f0f0f), 4), ('ushr', ('iand', step2, 0xf0f0f0f0), 4))
  736. step4 = ('ior', ('ishl', ('iand', step3, 0x33333333), 2), ('ushr', ('iand', step3, 0xcccccccc), 2))
  737. step5 = ('ior', ('ishl', ('iand', step4, 0x55555555), 1), ('ushr', ('iand', step4, 0xaaaaaaaa), 1))
  738. return step5
  739. optimizations += [(bitfield_reverse('x@32'), ('bitfield_reverse', 'x'))]
  740. # For any float comparison operation, "cmp", if you have "a == a && a cmp b"
  741. # then the "a == a" is redundant because it's equivalent to "a is not NaN"
  742. # and, if a is a NaN then the second comparison will fail anyway.
  743. for op in ['flt', 'fge', 'feq']:
  744. optimizations += [
  745. (('iand', ('feq', a, a), (op, a, b)), (op, a, b)),
  746. (('iand', ('feq', a, a), (op, b, a)), (op, b, a)),
  747. ]
  748. # Add optimizations to handle the case where the result of a ternary is
  749. # compared to a constant. This way we can take things like
  750. #
  751. # (a ? 0 : 1) > 0
  752. #
  753. # and turn it into
  754. #
  755. # a ? (0 > 0) : (1 > 0)
  756. #
  757. # which constant folding will eat for lunch. The resulting ternary will
  758. # further get cleaned up by the boolean reductions above and we will be
  759. # left with just the original variable "a".
  760. for op in ['flt', 'fge', 'feq', 'fne',
  761. 'ilt', 'ige', 'ieq', 'ine', 'ult', 'uge']:
  762. optimizations += [
  763. ((op, ('bcsel', 'a', '#b', '#c'), '#d'),
  764. ('bcsel', 'a', (op, 'b', 'd'), (op, 'c', 'd'))),
  765. ((op, '#d', ('bcsel', a, '#b', '#c')),
  766. ('bcsel', 'a', (op, 'd', 'b'), (op, 'd', 'c'))),
  767. ]
  768. # For example, this converts things like
  769. #
  770. # 1 + mix(0, a - 1, condition)
  771. #
  772. # into
  773. #
  774. # mix(1, (a-1)+1, condition)
  775. #
  776. # Other optimizations will rearrange the constants.
  777. for op in ['fadd', 'fmul', 'iadd', 'imul']:
  778. optimizations += [
  779. ((op, ('bcsel(is_used_once)', a, '#b', c), '#d'), ('bcsel', a, (op, b, d), (op, c, d)))
  780. ]
  781. # This section contains "late" optimizations that should be run before
  782. # creating ffmas and calling regular optimizations for the final time.
  783. # Optimizations should go here if they help code generation and conflict
  784. # with the regular optimizations.
  785. before_ffma_optimizations = [
  786. # Propagate constants down multiplication chains
  787. (('~fmul(is_used_once)', ('fmul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fmul', ('fmul', a, c), b)),
  788. (('imul(is_used_once)', ('imul(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('imul', ('imul', a, c), b)),
  789. (('~fadd(is_used_once)', ('fadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('fadd', ('fadd', a, c), b)),
  790. (('iadd(is_used_once)', ('iadd(is_used_once)', 'a(is_not_const)', '#b'), 'c(is_not_const)'), ('iadd', ('iadd', a, c), b)),
  791. (('~fadd', ('fmul', a, b), ('fmul', a, c)), ('fmul', a, ('fadd', b, c))),
  792. (('iadd', ('imul', a, b), ('imul', a, c)), ('imul', a, ('iadd', b, c))),
  793. (('~fadd', ('fneg', a), a), 0.0),
  794. (('iadd', ('ineg', a), a), 0),
  795. (('iadd', ('ineg', a), ('iadd', a, b)), b),
  796. (('iadd', a, ('iadd', ('ineg', a), b)), b),
  797. (('~fadd', ('fneg', a), ('fadd', a, b)), b),
  798. (('~fadd', a, ('fadd', ('fneg', a), b)), b),
  799. ]
  800. # This section contains "late" optimizations that should be run after the
  801. # regular optimizations have finished. Optimizations should go here if
  802. # they help code generation but do not necessarily produce code that is
  803. # more easily optimizable.
  804. late_optimizations = [
  805. # Most of these optimizations aren't quite safe when you get infinity or
  806. # Nan involved but the first one should be fine.
  807. (('flt', ('fadd', a, b), 0.0), ('flt', a, ('fneg', b))),
  808. (('flt', ('fneg', ('fadd', a, b)), 0.0), ('flt', ('fneg', a), b)),
  809. (('~fge', ('fadd', a, b), 0.0), ('fge', a, ('fneg', b))),
  810. (('~fge', ('fneg', ('fadd', a, b)), 0.0), ('fge', ('fneg', a), b)),
  811. (('~feq', ('fadd', a, b), 0.0), ('feq', a, ('fneg', b))),
  812. (('~fne', ('fadd', a, b), 0.0), ('fne', a, ('fneg', b))),
  813. (('~fge', ('fmin(is_used_once)', ('fadd(is_used_once)', a, b), ('fadd', c, d)), 0.0), ('iand', ('fge', a, ('fneg', b)), ('fge', c, ('fneg', d)))),
  814. (('fdot2', a, b), ('fdot_replicated2', a, b), 'options->fdot_replicates'),
  815. (('fdot3', a, b), ('fdot_replicated3', a, b), 'options->fdot_replicates'),
  816. (('fdot4', a, b), ('fdot_replicated4', a, b), 'options->fdot_replicates'),
  817. (('fdph', a, b), ('fdph_replicated', a, b), 'options->fdot_replicates'),
  818. (('b2f(is_used_more_than_once)', ('inot', a)), ('bcsel', a, 0.0, 1.0)),
  819. (('fneg(is_used_more_than_once)', ('b2f', ('inot', a))), ('bcsel', a, -0.0, -1.0)),
  820. # we do these late so that we don't get in the way of creating ffmas
  821. (('fmin', ('fadd(is_used_once)', '#c', a), ('fadd(is_used_once)', '#c', b)), ('fadd', c, ('fmin', a, b))),
  822. (('fmax', ('fadd(is_used_once)', '#c', a), ('fadd(is_used_once)', '#c', b)), ('fadd', c, ('fmax', a, b))),
  823. # Lowered for backends without a dedicated b2f instruction
  824. (('b2f@32', a), ('iand', a, 1.0), 'options->lower_b2f'),
  825. ]
  826. print(nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render())
  827. print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_before_ffma",
  828. before_ffma_optimizations).render())
  829. print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_late",
  830. late_optimizations).render())