glsl: Optimize mul(a, -1) into neg(a).
authorMatt Turner <mattst88@gmail.com>
Wed, 16 Oct 2013 06:42:19 +0000 (23:42 -0700)
committerMatt Turner <mattst88@gmail.com>
Thu, 17 Oct 2013 03:49:43 +0000 (20:49 -0700)
Two extra instructions in some heroesofnewerth shaders, but a win for
everything else.

total instructions in shared programs: 1531352 -> 1530815 (-0.04%)
instructions in affected programs:     121898 -> 121361 (-0.44%)

Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/glsl/opt_algebraic.cpp

index d706a6ad13c44092b5e5498e93f58b2ce6cbd2db..3e5802e1818c5314673dfa308f8e64ddbd05300b 100644 (file)
@@ -84,6 +84,12 @@ is_vec_one(ir_constant *ir)
    return (ir == NULL) ? false : ir->is_one();
 }
 
+static inline bool
+is_vec_negative_one(ir_constant *ir)
+{
+   return (ir == NULL) ? false : ir->is_negative_one();
+}
+
 static inline bool
 is_vec_basis(ir_constant *ir)
 {
@@ -287,6 +293,23 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
         this->progress = true;
         return ir_constant::zero(ir, ir->type);
       }
+      if (is_vec_negative_one(op_const[0])) {
+         this->progress = true;
+         temp = new(mem_ctx) ir_expression(ir_unop_neg,
+                                           ir->operands[1]->type,
+                                           ir->operands[1],
+                                           NULL);
+         return swizzle_if_required(ir, temp);
+      }
+      if (is_vec_negative_one(op_const[1])) {
+         this->progress = true;
+         temp = new(mem_ctx) ir_expression(ir_unop_neg,
+                                           ir->operands[0]->type,
+                                           ir->operands[0],
+                                           NULL);
+         return swizzle_if_required(ir, temp);
+      }
+
 
       /* Reassociate multiplication of constants so that we can do
        * constant folding.