[Mesa-dev] [PATCH 13/23] glsl: Rewrite (-abs(x) < 0) and (-abs(x) < abs(x)) as (x != 0)

Ian Romanick idr at freedesktop.org
Fri Mar 20 13:58:13 PDT 2015


From: Ian Romanick <ian.d.romanick at intel.com>

Shader-db results:

GM45 (0x2A42):
total instructions in shared programs: 3545713 -> 3545712 (-0.00%)
instructions in affected programs:     141 -> 140 (-0.71%)
helped:                                1

Iron Lake (0x0046):
total instructions in shared programs: 4976069 -> 4976067 (-0.00%)
instructions in affected programs:     271 -> 269 (-0.74%)
helped:                                2

Sandy Bridge (0x0116):
total instructions in shared programs: 6803294 -> 6803288 (-0.00%)
instructions in affected programs:     5277 -> 5271 (-0.11%)
helped:                                6

Sandy Bridge (0x0116) NIR:
total instructions in shared programs: 6817199 -> 6817171 (-0.00%)
instructions in affected programs:     4980 -> 4952 (-0.56%)
helped:                                8

Ivy Bridge (0x0166):
total instructions in shared programs: 6278324 -> 6278318 (-0.00%)
instructions in affected programs:     4814 -> 4808 (-0.12%)
helped:                                6

Ivy Bridge (0x0166) NIR:
total instructions in shared programs: 6324632 -> 6324604 (-0.00%)
instructions in affected programs:     4556 -> 4528 (-0.61%)
helped:                                8

Haswell (0x0426):
total instructions in shared programs: 5763785 -> 5763779 (-0.00%)
instructions in affected programs:     4382 -> 4376 (-0.14%)
helped:                                6

Haswell (0x0426) NIR:
total instructions in shared programs: 5800362 -> 5800334 (-0.00%)
instructions in affected programs:     4090 -> 4062 (-0.68%)
helped:                                8

Broadwell (0x162E):
total instructions in shared programs: 6811811 -> 6811805 (-0.00%)
instructions in affected programs:     4356 -> 4350 (-0.14%)
helped:                                6

Broadwell (0x162E) NIR:
total instructions in shared programs: 7015134 -> 7015106 (-0.00%)
instructions in affected programs:     4070 -> 4042 (-0.69%)
helped:                                8

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/glsl/opt_algebraic.cpp | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 228bb6c..deedc9c 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -683,27 +683,32 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       }
       break;
 
-   case ir_binop_gequal: {
+   case ir_binop_gequal:
+   case ir_binop_less: {
       /* Thanks to limitations of DX9 hardware, the HLSL compiler generates
-       * expressions like (-abs(x) >= 0) instead of (x == 0).  If a backend
-       * needs that, it can generate it from the actual expression.  This
-       * saves memory and makes it easier for other optimization passes to see
-       * what's happening.
+       * expressions like (-abs(x) >= 0) instead of (x == 0) and (-abs(x) < 0)
+       * instead of (x != 0).  If a backend needs that, it can generate it
+       * from the actual expression.  This saves memory and makes it easier
+       * for other optimization passes to see what's happening.
        */
+      const enum ir_expression_operation replacement =
+         ir->operation == ir_binop_gequal ? ir_binop_equal : ir_binop_nequal;
+
       if (op_expr[0] != NULL && op_expr[0]->operation == ir_unop_neg &&
           op_const[1] != NULL && op_const[1]->is_zero()) {
          ir_expression *const subexpr =
             op_expr[0]->operands[0]->as_expression();
 
          if (subexpr != NULL && subexpr->operation == ir_unop_abs) {
-            return new(mem_ctx) ir_expression(ir_binop_equal,
+            return new(mem_ctx) ir_expression(replacement,
                                               subexpr->operands[0],
                                               op_const[1]);
          }
       }
 
       /* In some cases the HLSL compiler generates expressions like (-abs(x)
-       * >= abs(x)) instead of (x == 0).
+       * >= abs(x)) instead of (x == 0) and (-abs(x) < abs(x)) instead of (x
+       * != 0).
        *
        * This form seems most common in vertex shaders.
        */
@@ -717,7 +722,7 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
             ir_rvalue *const right_inner = op_expr[1]->operands[0]->as_rvalue();
 
             if (left_inner->equals(right_inner)) {
-               return new(mem_ctx) ir_expression(ir_binop_equal,
+               return new(mem_ctx) ir_expression(replacement,
                                                  left_inner,
                                                  ir_constant::zero(mem_ctx, left_inner->type));
             }
@@ -725,7 +730,6 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       }
    }
       /* FALLTHROUGH */
-   case ir_binop_less:
    case ir_binop_lequal:
    case ir_binop_greater:
    case ir_binop_equal:
-- 
2.1.0



More information about the mesa-dev mailing list