glsl: Optimize (x + y cmp 0) into (x cmp -y).
authorMatt Turner <mattst88@gmail.com>
Sat, 29 Mar 2014 19:31:24 +0000 (12:31 -0700)
committerMatt Turner <mattst88@gmail.com>
Sat, 5 Apr 2014 16:47:37 +0000 (09:47 -0700)
Cuts a small handful of instructions in Serious Sam 3:

instructions in affected programs:     4692 -> 4666 (-0.55%)

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/glsl/opt_algebraic.cpp

index 8494bd9ec539f21ac2a912fea225a60f86829de5..2db877d5b8bd2d5ce2d6ac2ef9ebf5ecb58a3f39 100644 (file)
@@ -445,6 +445,28 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       }
       break;
 
+   case ir_binop_less:
+   case ir_binop_lequal:
+   case ir_binop_greater:
+   case ir_binop_gequal:
+   case ir_binop_equal:
+   case ir_binop_nequal:
+      for (int add_pos = 0; add_pos < 2; add_pos++) {
+         ir_expression *add = op_expr[add_pos];
+
+         if (!add || add->operation != ir_binop_add)
+            continue;
+
+         ir_constant *zero = op_const[1 - add_pos];
+         if (!is_vec_zero(zero))
+            continue;
+
+         return new(mem_ctx) ir_expression(ir->operation,
+                                           add->operands[0],
+                                           neg(add->operands[1]));
+      }
+      break;
+
    case ir_binop_rshift:
    case ir_binop_lshift:
       /* 0 >> x == 0 */