[Mesa-dev] [PATCH 10/11] glsl: Optimize X + neg(X) == 0 and the other way around

thomashelland90 at gmail.com thomashelland90 at gmail.com
Thu Aug 7 13:51:11 PDT 2014


From: Thomas Helland <thomashelland90 at gmail.com>

Since we lower A - B to A + neg(B) on some architectures
lets add a optimization for this pattern.
This yields some gains in a shader-db run with extra shaders
added from Team Fortress 2 and Portal.

helped: shaders/tf2/1684.shader_test fs16:           62 -> 60 (-3.23%)
helped: shaders/tf2/1684.shader_test fs8:            62 -> 60 (-3.23%)
helped: shaders/tf2/5861.shader_test fs16:           68 -> 66 (-2.94%)
helped: shaders/tf2/5861.shader_test fs8:            68 -> 66 (-2.94%)
helped: shaders/tf2/5986.shader_test fs16:           68 -> 66 (-2.94%)
helped: shaders/tf2/5986.shader_test fs8:            68 -> 66 (-2.94%)
helped: shaders/tf2/6926.shader_test fs16:           62 -> 60 (-3.23%)
helped: shaders/tf2/6926.shader_test fs8:            62 -> 60 (-3.23%)

total instructions in shared programs: 731050 -> 731034 (-0.00%)
instructions in affected programs:     520 -> 504 (-3.08%)
GAINED:                                0
LOST:                                  0

Signed-off-by: Thomas Helland <thomashelland90 at gmail.com>
---
 src/glsl/opt_algebraic.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 4008fe7..21bf332 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -364,6 +364,15 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       if (is_vec_zero(op_const[1]))
 	 return ir->operands[0];
 
+      /* X + neg(X) == 0 */
+      if (op_expr[1] && op_expr[1]->operation == ir_unop_neg &&
+          ir->operands[0]->equals(op_expr[1]->operands[0]))
+         return ir_constant::zero(ir, ir->type);
+      /* neg(X) + X == 0 */
+      if (op_expr[0] && op_expr[0]->operation == ir_unop_neg &&
+          ir->operands[1]->equals(op_expr[0]->operands[0]))
+         return ir_constant::zero(ir, ir->type);
+
       /* Reassociate addition of constants so that we can do constant
        * folding.
        */
-- 
2.0.3



More information about the mesa-dev mailing list