From 07cd30ca293d1eb6980f69f330f9d182652cf902 Mon Sep 17 00:00:00 2001 From: Samuel Pitoiset Date: Thu, 1 Jul 2021 08:41:04 +0200 Subject: [PATCH] nir/opt_algebraic: optimize fmax(-fmin(b, a), b) -> fmax(b, -a) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Found with Cyberpunk 2077. fossils-db (GFX10.3): Totals from 128 (2.34% of 5465) affected shaders: CodeSize: 769720 -> 767656 (-0.27%); split: -0.27%, +0.00% Instrs: 145748 -> 145229 (-0.36%) Signed-off-by: Samuel Pitoiset Reviewed-by: Daniel Schürmann Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 449f114da6b..48129c9ce45 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -652,6 +652,11 @@ optimizations.extend([ # fmin(0.0, b)) while the right one is "b", so this optimization is inexact. (('~fmin', ('fsat', a), '#b(is_zero_to_one)'), ('fsat', ('fmin', a, b))), + # max(-min(b, a), b) -> max(b, -a) + # min(-max(b, a), b) -> min(-b, -a) + (('fmax', ('fneg', ('fmin', b, a)), b), ('fmax', b, ('fneg', a))), + (('fmin', ('fneg', ('fmax', b, a)), b), ('fmin', ('fneg', b), ('fneg', a))), + # If a in [0,b] then b-a is also in [0,b]. Since b in [0,1], max(b-a, 0) = # fsat(b-a). #