nir: Do basic constant reassociation.
authorKenneth Graunke <kenneth@whitecape.org>
Thu, 7 Apr 2016 05:34:39 +0000 (22:34 -0700)
committerKenneth Graunke <kenneth@whitecape.org>
Tue, 12 Apr 2016 01:43:55 +0000 (18:43 -0700)
commit5886cd79a0ed10b887840f6afb980a5a6a47e26e
tree6c994bb93e9255240ab9c766f54084ad9514319e
parent1c7ba7f15646367006d615180e89d5ea34a3d5f6
nir: Do basic constant reassociation.

Many shaders contain expression trees of the form:

    const_1 * (value * const_2)

Reorganizing these to

    (const_1 * const_2) * value

will allow constant folding to combine the constants.  Sometimes, these
constants are 2 and 0.5, so we can remove a multiply altogether.  Other
times, it can create more immediate constants, which can actually hurt.

Finding a good balance here is tricky.  While much more could be done,
this simple patch seems to have a lot of positive benefit while having
a low downside.

shader-db results on Broadwell:

total instructions in shared programs: 8963768 -> 8961369 (-0.03%)
instructions in affected programs: 438318 -> 435919 (-0.55%)
helped: 1502
HURT: 245

total cycles in shared programs: 71527354 -> 71421516 (-0.15%)
cycles in affected programs: 11541788 -> 11435950 (-0.92%)
helped: 3445
HURT: 1224

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eduardo Lima Mitev <elima@igalia.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Matt Turner <mattst88@gmail.com>
src/compiler/nir/nir_opt_algebraic.py