glsl: Apply the transformation "1/rsq(x) == sqrt(x)" in opt_algebraic.
[mesa.git] / src / glsl / opt_algebraic.cpp
index 20f6159f0e689449352917e311224e5696a51e4e..05a5899898555b89d86de1497e079b0b1e4c4a3a 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 {
+
 /**
  * Visitor class for replacing expressions with ir_constant values.
  */
@@ -68,6 +73,8 @@ public:
    bool progress;
 };
 
+} /* unnamed namespace */
+
 static inline bool
 is_vec_zero(ir_constant *ir)
 {
@@ -80,6 +87,18 @@ 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)
+{
+   return (ir == NULL) ? false : ir->is_basis();
+}
+
 static void
 update_type(ir_expression *ir)
 {
@@ -176,12 +195,11 @@ ir_algebraic_visitor::swizzle_if_required(ir_expression *expr,
 ir_rvalue *
 ir_algebraic_visitor::handle_expression(ir_expression *ir)
 {
-   ir_constant *op_const[2] = {NULL, NULL};
-   ir_expression *op_expr[2] = {NULL, NULL};
-   ir_expression *temp;
+   ir_constant *op_const[4] = {NULL, NULL, NULL, NULL};
+   ir_expression *op_expr[4] = {NULL, NULL, NULL, NULL};
    unsigned int i;
 
-   assert(ir->get_num_operands() <= 2);
+   assert(ir->get_num_operands() <= 4);
    for (i = 0; i < ir->get_num_operands(); i++) {
       if (ir->operands[i]->type->is_matrix())
         return ir;
@@ -191,9 +209,31 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
    }
 
    if (this->mem_ctx == NULL)
-      this->mem_ctx = talloc_parent(ir);
+      this->mem_ctx = ralloc_parent(ir);
 
    switch (ir->operation) {
+   case ir_unop_abs:
+      if (op_expr[0] == NULL)
+        break;
+
+      switch (op_expr[0]->operation) {
+      case ir_unop_abs:
+      case ir_unop_neg:
+         return abs(op_expr[0]->operands[0]);
+      default:
+         break;
+      }
+      break;
+
+   case ir_unop_neg:
+      if (op_expr[0] == NULL)
+        break;
+
+      if (op_expr[0]->operation == ir_unop_neg) {
+         return op_expr[0]->operands[0];
+      }
+      break;
+
    case ir_unop_logic_not: {
       enum ir_expression_operation new_op = ir_unop_logic_not;
 
@@ -217,7 +257,6 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       }
 
       if (new_op != ir_unop_logic_not) {
-        this->progress = true;
         return new(mem_ctx) ir_expression(new_op,
                                           ir->type,
                                           op_expr[0]->operands[0],
@@ -228,123 +267,134 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
    }
 
    case ir_binop_add:
-      if (is_vec_zero(op_const[0])) {
-        this->progress = true;
-        return swizzle_if_required(ir, ir->operands[1]);
-      }
-      if (is_vec_zero(op_const[1])) {
-        this->progress = true;
-        return swizzle_if_required(ir, ir->operands[0]);
-      }
+      if (is_vec_zero(op_const[0]))
+        return ir->operands[1];
+      if (is_vec_zero(op_const[1]))
+        return ir->operands[0];
 
       /* Reassociate addition of constants so that we can do constant
        * folding.
        */
       if (op_const[0] && !op_const[1])
-        reassociate_constant(ir, 0, op_const[0],
-                             ir->operands[1]->as_expression());
+        reassociate_constant(ir, 0, op_const[0], op_expr[1]);
       if (op_const[1] && !op_const[0])
-        reassociate_constant(ir, 1, op_const[1],
-                             ir->operands[0]->as_expression());
+        reassociate_constant(ir, 1, op_const[1], op_expr[0]);
       break;
 
    case ir_binop_sub:
-      if (is_vec_zero(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_zero(op_const[1])) {
-        this->progress = true;
-        return swizzle_if_required(ir, ir->operands[0]);
-      }
+      if (is_vec_zero(op_const[0]))
+        return neg(ir->operands[1]);
+      if (is_vec_zero(op_const[1]))
+        return ir->operands[0];
       break;
 
    case ir_binop_mul:
-      if (is_vec_one(op_const[0])) {
-        this->progress = true;
-        return swizzle_if_required(ir, ir->operands[1]);
-      }
-      if (is_vec_one(op_const[1])) {
-        this->progress = true;
-        return swizzle_if_required(ir, ir->operands[0]);
-      }
+      if (is_vec_one(op_const[0]))
+        return ir->operands[1];
+      if (is_vec_one(op_const[1]))
+        return ir->operands[0];
 
-      if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
-        this->progress = true;
+      if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1]))
         return ir_constant::zero(ir, ir->type);
-      }
+
+      if (is_vec_negative_one(op_const[0]))
+         return neg(ir->operands[1]);
+      if (is_vec_negative_one(op_const[1]))
+         return neg(ir->operands[0]);
+
 
       /* Reassociate multiplication of constants so that we can do
        * constant folding.
        */
       if (op_const[0] && !op_const[1])
-        reassociate_constant(ir, 0, op_const[0],
-                             ir->operands[1]->as_expression());
+        reassociate_constant(ir, 0, op_const[0], op_expr[1]);
       if (op_const[1] && !op_const[0])
-        reassociate_constant(ir, 1, op_const[1],
-                             ir->operands[0]->as_expression());
+        reassociate_constant(ir, 1, op_const[1], op_expr[0]);
 
       break;
 
    case ir_binop_div:
       if (is_vec_one(op_const[0]) && ir->type->base_type == GLSL_TYPE_FLOAT) {
-        this->progress = true;
-        temp = new(mem_ctx) ir_expression(ir_unop_rcp,
+        return new(mem_ctx) ir_expression(ir_unop_rcp,
                                           ir->operands[1]->type,
                                           ir->operands[1],
                                           NULL);
-        return swizzle_if_required(ir, temp);
       }
-      if (is_vec_one(op_const[1])) {
-        this->progress = true;
-        return swizzle_if_required(ir, ir->operands[0]);
+      if (is_vec_one(op_const[1]))
+        return ir->operands[0];
+      break;
+
+   case ir_binop_dot:
+      if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1]))
+        return ir_constant::zero(mem_ctx, ir->type);
+
+      if (is_vec_basis(op_const[0])) {
+        unsigned component = 0;
+        for (unsigned c = 0; c < op_const[0]->type->vector_elements; c++) {
+           if (op_const[0]->value.f[c] == 1.0)
+              component = c;
+        }
+        return new(mem_ctx) ir_swizzle(ir->operands[1], component, 0, 0, 0, 1);
+      }
+      if (is_vec_basis(op_const[1])) {
+        unsigned component = 0;
+        for (unsigned c = 0; c < op_const[1]->type->vector_elements; c++) {
+           if (op_const[1]->value.f[c] == 1.0)
+              component = c;
+        }
+        return new(mem_ctx) ir_swizzle(ir->operands[0], component, 0, 0, 0, 1);
       }
       break;
 
+   case ir_binop_rshift:
+   case ir_binop_lshift:
+      /* 0 >> x == 0 */
+      if (is_vec_zero(op_const[0]))
+         return ir->operands[0];
+      /* x >> 0 == x */
+      if (is_vec_zero(op_const[1]))
+         return ir->operands[0];
+      break;
+
    case ir_binop_logic_and:
-      /* FINISHME: Also simplify (a && a) to (a). */
       if (is_vec_one(op_const[0])) {
-        this->progress = true;
         return ir->operands[1];
       } else if (is_vec_one(op_const[1])) {
-        this->progress = true;
         return ir->operands[0];
       } else if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
-        this->progress = true;
         return ir_constant::zero(mem_ctx, ir->type);
+      } 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) and (not B) === not (A or B)
+          */
+         return logic_not(logic_or(op_expr[0]->operands[0],
+                                   op_expr[1]->operands[0]));
+      } else if (ir->operands[0]->equals(ir->operands[1])) {
+         /* (a && a) == a */
+         return ir->operands[0];
       }
       break;
 
    case ir_binop_logic_xor:
-      /* FINISHME: Also simplify (a ^^ a) to (false). */
       if (is_vec_zero(op_const[0])) {
-        this->progress = true;
         return ir->operands[1];
       } else if (is_vec_zero(op_const[1])) {
-        this->progress = true;
         return ir->operands[0];
       } else if (is_vec_one(op_const[0])) {
-        this->progress = true;
-        return new(mem_ctx) ir_expression(ir_unop_logic_not, ir->type,
-                                          ir->operands[1], NULL);
+        return logic_not(ir->operands[1]);
       } else if (is_vec_one(op_const[1])) {
-        this->progress = true;
-        return new(mem_ctx) ir_expression(ir_unop_logic_not, ir->type,
-                                          ir->operands[0], NULL);
+        return logic_not(ir->operands[0]);
+      } else if (ir->operands[0]->equals(ir->operands[1])) {
+         /* (a ^^ a) == false */
+        return ir_constant::zero(mem_ctx, ir->type);
       }
       break;
 
    case ir_binop_logic_or:
-      /* FINISHME: Also simplify (a || a) to (a). */
       if (is_vec_zero(op_const[0])) {
-        this->progress = true;
         return ir->operands[1];
       } else if (is_vec_zero(op_const[1])) {
-        this->progress = true;
         return ir->operands[0];
       } else if (is_vec_one(op_const[0]) || is_vec_one(op_const[1])) {
         ir_constant_data data;
@@ -352,34 +402,46 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
         for (unsigned i = 0; i < 16; i++)
            data.b[i] = true;
 
-        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)
+          */
+         return logic_not(logic_and(op_expr[0]->operands[0],
+                                    op_expr[1]->operands[0]));
+      } else if (ir->operands[0]->equals(ir->operands[1])) {
+         /* (a || a) == a */
+         return ir->operands[0];
       }
       break;
 
    case ir_unop_rcp:
-      if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp) {
-        this->progress = true;
+      if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp)
         return op_expr[0]->operands[0];
-      }
 
-      /* FINISHME: We should do rcp(rsq(x)) -> sqrt(x) for some
-       * backends, except that some backends will have done sqrt ->
-       * rcp(rsq(x)) and we don't want to undo it for them.
+      /* While ir_to_mesa.cpp will lower sqrt(x) to rcp(rsq(x)), it does so at
+       * its IR level, so we can always apply this transformation.
        */
+      if (op_expr[0] && op_expr[0]->operation == ir_unop_rsq)
+         return sqrt(op_expr[0]->operands[0]);
 
       /* As far as we know, all backends are OK with rsq. */
       if (op_expr[0] && op_expr[0]->operation == ir_unop_sqrt) {
-        this->progress = true;
-        temp = new(mem_ctx) ir_expression(ir_unop_rsq,
-                                          op_expr[0]->operands[0]->type,
-                                          op_expr[0]->operands[0],
-                                          NULL);
-        return swizzle_if_required(ir, temp);
+        return rsq(op_expr[0]->operands[0]);
       }
 
       break;
 
+   case ir_triop_lrp:
+      /* Operands are (x, y, a). */
+      if (is_vec_zero(op_const[2])) {
+         return ir->operands[0];
+      } else if (is_vec_one(op_const[2])) {
+         return ir->operands[1];
+      }
+      break;
+
    default:
       break;
    }
@@ -397,7 +459,17 @@ ir_algebraic_visitor::handle_rvalue(ir_rvalue **rvalue)
    if (!expr || expr->operation == ir_quadop_vector)
       return;
 
-   *rvalue = handle_expression(expr);
+   ir_rvalue *new_rvalue = handle_expression(expr);
+   if (new_rvalue == *rvalue)
+      return;
+
+   /* If the expr used to be some vec OP scalar returning a vector, and the
+    * optimization gave us back a scalar, we still need to turn it into a
+    * vector.
+    */
+   *rvalue = swizzle_if_required(expr, new_rvalue);
+
+   this->progress = true;
 }
 
 bool