[Mesa-dev] [PATCH v3 09/11] glsl: Optimize logic operation A || (A && B)

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


From: Thomas Helland <thomashelland90 at gmail.com>

Let's cut the needless A && B here.

v1 -> v2: Correct the comments, return A instead of A && B
v2 -> v3: Rebase on top of this series

Signed-off-by: Thomas Helland <thomashelland90 at gmail.com>
---
This was originally posted here:
http://lists.freedesktop.org/archives/mesa-dev/2014-July/063129.html
v2 of the patch got no responses, so I'm reposting it here.

This used to give some effect on a clean shader-db with
some extra shaders from TF2 and portal;

helped: shaders/tf2/2042.shader_test fs16:        23 -> 21 (-8.70%)
helped: shaders/tf2/2042.shader_test fs8:         23 -> 21 (-8.70%)
helped: shaders/tf2/4624.shader_test fs16:        21 -> 19 (-9.52%)
helped: shaders/tf2/4624.shader_test fs8:         21 -> 19 (-9.52%)
helped: shaders/tf2/763.shader_test fs16:         23 -> 21 (-8.70%)
helped: shaders/tf2/763.shader_test fs8:          23 -> 21 (-8.70%)

HURT:   shaders/orbital_explorer.shader_test vs:  1049 -> 1052 (0.29%)

total instructions in shared programs: 758979 -> 758970 (-0.00%)
instructions in affected programs:     1183 -> 1174 (-0.76%)
GAINED:                                0
LOST:                                  0

However, current master gets this optimized, probably in i965 backend;
the previously affected shaders now get the same amount of
instructions with master as they did with this patch,
and this patch has no effect.
It does, however, cause 5 extra crashes in a piglit-quick run.
I have not investigated this any further.
---
 src/glsl/opt_algebraic.cpp | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index c673495..4008fe7 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -647,7 +647,17 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
             data.b[i] = true;
 
          return new(mem_ctx) ir_constant(ir->type, &data);
-      }
+      } else if (op_expr[0] && op_expr[0]->operation == ir_binop_logic_and &&
+                 (op_expr[0]->operands[0]->equals(op_expr[1]) ||
+                  op_expr[0]->operands[1]->equals(op_expr[1]))) {
+         /* (A && B) || A == A or (B && A) || A == A */
+         return ir->operands[1];
+      } else if (op_expr[1] && op_expr[1]->operation == ir_binop_logic_and &&
+                 (op_expr[1]->operands[0]->equals(op_expr[0]) ||
+                  op_expr[1]->operands[1]->equals(op_expr[0]))) {
+         /* A || (A && B) == A or A || (B && A) == A */
+         return ir->operands[0];
+       }
       break;
 
    case ir_binop_pow:
-- 
2.0.3



More information about the mesa-dev mailing list