glsl: Optimize (not A) or (not B) into not (A and B).
authorMatt Turner <mattst88@gmail.com>
Wed, 23 Oct 2013 23:40:16 +0000 (16:40 -0700)
committerMatt Turner <mattst88@gmail.com>
Fri, 25 Oct 2013 17:35:13 +0000 (10:35 -0700)
A few Serious Sam 3 shaders affected:

instructions in affected programs:     4384 -> 4344 (-0.91%)

Reviewed-by: Eric Anholt <eric@anholt.net>
src/glsl/opt_algebraic.cpp

index 37b2f02c6e493d82c07dca86864085e2911e8af8..22d2dbfe6e3df7ab997bde9abfae3effabb55bd2 100644 (file)
 #include "ir_visitor.h"
 #include "ir_rvalue_visitor.h"
 #include "ir_optimization.h"
+#include "ir_builder.h"
 #include "glsl_types.h"
 
+using namespace ir_builder;
+
 namespace {
 
 /**
@@ -436,6 +439,15 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
 
         this->progress = true;
         return new(mem_ctx) ir_constant(ir->type, &data);
+      } else if (op_expr[0] && op_expr[0]->operation == ir_unop_logic_not &&
+                 op_expr[1] && op_expr[1]->operation == ir_unop_logic_not) {
+         /* De Morgan's Law:
+          *    (not A) or (not B) === not (A and B)
+          */
+         temp = logic_not(logic_and(op_expr[0]->operands[0],
+                                    op_expr[1]->operands[0]));
+         this->progress = true;
+         return swizzle_if_required(ir, temp);
       }
       break;