glsl: Pass in options to do_algebraic().
[mesa.git] / src / glsl / opt_algebraic.cpp
index 22d2dbfe6e3df7ab997bde9abfae3effabb55bd2..d57c3e8bde415ed294fa47857f5155c2a8a7159e 100644 (file)
@@ -45,10 +45,13 @@ namespace {
 
 class ir_algebraic_visitor : public ir_rvalue_visitor {
 public:
-   ir_algebraic_visitor()
+   ir_algebraic_visitor(bool native_integers,
+                        const struct gl_shader_compiler_options *options)
+      : options(options)
    {
       this->progress = false;
       this->mem_ctx = NULL;
+      this->native_integers = native_integers;
    }
 
    virtual ~ir_algebraic_visitor()
@@ -68,8 +71,10 @@ public:
    ir_rvalue *swizzle_if_required(ir_expression *expr,
                                  ir_rvalue *operand);
 
+   const struct gl_shader_compiler_options *options;
    void *mem_ctx;
 
+   bool native_integers;
    bool progress;
 };
 
@@ -87,6 +92,12 @@ is_vec_one(ir_constant *ir)
    return (ir == NULL) ? false : ir->is_one();
 }
 
+static inline bool
+is_vec_two(ir_constant *ir)
+{
+   return (ir == NULL) ? false : ir->is_value(2.0, 2);
+}
+
 static inline bool
 is_vec_negative_one(ir_constant *ir)
 {
@@ -197,7 +208,6 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
 {
    ir_constant *op_const[4] = {NULL, NULL, NULL, NULL};
    ir_expression *op_expr[4] = {NULL, NULL, NULL, NULL};
-   ir_expression *temp;
    unsigned int i;
 
    assert(ir->get_num_operands() <= 4);
@@ -213,6 +223,11 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       this->mem_ctx = ralloc_parent(ir);
 
    switch (ir->operation) {
+   case ir_unop_bit_not:
+      if (op_expr[0] && op_expr[0]->operation == ir_unop_bit_not)
+         return op_expr[0]->operands[0];
+      break;
+
    case ir_unop_abs:
       if (op_expr[0] == NULL)
         break;
@@ -220,12 +235,7 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       switch (op_expr[0]->operation) {
       case ir_unop_abs:
       case ir_unop_neg:
-         this->progress = true;
-         temp = new(mem_ctx) ir_expression(ir_unop_abs,
-                                           ir->type,
-                                           op_expr[0]->operands[0],
-                                           NULL);
-         return swizzle_if_required(ir, temp);
+         return abs(op_expr[0]->operands[0]);
       default:
          break;
       }
@@ -236,8 +246,43 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
         break;
 
       if (op_expr[0]->operation == ir_unop_neg) {
-         this->progress = true;
-         return swizzle_if_required(ir, op_expr[0]->operands[0]);
+         return op_expr[0]->operands[0];
+      }
+      break;
+
+   case ir_unop_exp:
+      if (op_expr[0] == NULL)
+        break;
+
+      if (op_expr[0]->operation == ir_unop_log) {
+         return op_expr[0]->operands[0];
+      }
+      break;
+
+   case ir_unop_log:
+      if (op_expr[0] == NULL)
+        break;
+
+      if (op_expr[0]->operation == ir_unop_exp) {
+         return op_expr[0]->operands[0];
+      }
+      break;
+
+   case ir_unop_exp2:
+      if (op_expr[0] == NULL)
+        break;
+
+      if (op_expr[0]->operation == ir_unop_log2) {
+         return op_expr[0]->operands[0];
+      }
+      break;
+
+   case ir_unop_log2:
+      if (op_expr[0] == NULL)
+        break;
+
+      if (op_expr[0]->operation == ir_unop_exp2) {
+         return op_expr[0]->operands[0];
       }
       break;
 
@@ -264,7 +309,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],
@@ -275,14 +319,10 @@ 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.
@@ -291,53 +331,81 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
         reassociate_constant(ir, 0, op_const[0], op_expr[1]);
       if (op_const[1] && !op_const[0])
         reassociate_constant(ir, 1, op_const[1], op_expr[0]);
+
+      /* Replace (-x + y) * a + x and commutative variations with lrp(x, y, a).
+       *
+       * (-x + y) * a + x
+       * (x * -a) + (y * a) + x
+       * x + (x * -a) + (y * a)
+       * x * (1 - a) + y * a
+       * lrp(x, y, a)
+       */
+      for (int mul_pos = 0; mul_pos < 2; mul_pos++) {
+         ir_expression *mul = op_expr[mul_pos];
+
+         if (!mul || mul->operation != ir_binop_mul)
+            continue;
+
+         /* Multiply found on one of the operands. Now check for an
+          * inner addition operation.
+          */
+         for (int inner_add_pos = 0; inner_add_pos < 2; inner_add_pos++) {
+            ir_expression *inner_add =
+               mul->operands[inner_add_pos]->as_expression();
+
+            if (!inner_add || inner_add->operation != ir_binop_add)
+               continue;
+
+            /* Inner addition found on one of the operands. Now check for
+             * one of the operands of the inner addition to be the negative
+             * of x_operand.
+             */
+            for (int neg_pos = 0; neg_pos < 2; neg_pos++) {
+               ir_expression *neg =
+                  inner_add->operands[neg_pos]->as_expression();
+
+               if (!neg || neg->operation != ir_unop_neg)
+                  continue;
+
+               ir_rvalue *x_operand = ir->operands[1 - mul_pos];
+
+               if (!neg->operands[0]->equals(x_operand))
+                  continue;
+
+               ir_rvalue *y_operand = inner_add->operands[1 - neg_pos];
+               ir_rvalue *a_operand = mul->operands[1 - inner_add_pos];
+
+               if (x_operand->type != y_operand->type ||
+                   x_operand->type != a_operand->type)
+                  continue;
+
+               return lrp(x_operand, y_operand, a_operand);
+            }
+         }
+      }
+
       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])) {
-         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);
-      }
+
+      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
@@ -352,26 +420,20 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
 
    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])) {
-        this->progress = true;
+      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])) {
-        this->progress = true;
         unsigned component = 0;
         for (unsigned c = 0; c < op_const[0]->type->vector_elements; c++) {
            if (op_const[0]->value.f[c] == 1.0)
@@ -380,7 +442,6 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
         return new(mem_ctx) ir_swizzle(ir->operands[1], component, 0, 0, 0, 1);
       }
       if (is_vec_basis(op_const[1])) {
-        this->progress = true;
         unsigned component = 0;
         for (unsigned c = 0; c < op_const[1]->type->vector_elements; c++) {
            if (op_const[1]->value.f[c] == 1.0)
@@ -390,46 +451,77 @@ 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 */
+      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;
@@ -437,54 +529,97 @@ 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)
           */
-         temp = logic_not(logic_and(op_expr[0]->operands[0],
+         return logic_not(logic_and(op_expr[0]->operands[0],
                                     op_expr[1]->operands[0]));
-         this->progress = true;
-         return swizzle_if_required(ir, temp);
+      } else if (ir->operands[0]->equals(ir->operands[1])) {
+         /* (a || a) == a */
+         return ir->operands[0];
       }
       break;
 
+   case ir_binop_pow:
+      /* 1^x == 1 */
+      if (is_vec_one(op_const[0]))
+         return op_const[0];
+
+      /* x^1 == x */
+      if (is_vec_one(op_const[1]))
+         return ir->operands[0];
+
+      /* pow(2,x) == exp2(x) */
+      if (is_vec_two(op_const[0]))
+         return expr(ir_unop_exp2, ir->operands[1]);
+
+      if (is_vec_two(op_const[1])) {
+         ir_variable *x = new(ir) ir_variable(ir->operands[1]->type, "x",
+                                              ir_var_temporary);
+         base_ir->insert_before(x);
+         base_ir->insert_before(assign(x, ir->operands[0]));
+         return mul(x, x);
+      }
+
+      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_fma:
+      /* Operands are op0 * op1 + op2. */
+      if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
+         return ir->operands[2];
+      } else if (is_vec_zero(op_const[2])) {
+         return mul(ir->operands[0], ir->operands[1]);
+      } else if (is_vec_one(op_const[0])) {
+         return add(ir->operands[1], ir->operands[2]);
+      } else if (is_vec_one(op_const[1])) {
+         return add(ir->operands[0], ir->operands[2]);
+      }
+      break;
+
    case ir_triop_lrp:
       /* Operands are (x, y, a). */
       if (is_vec_zero(op_const[2])) {
-         this->progress = true;
-         return swizzle_if_required(ir, ir->operands[0]);
+         return ir->operands[0];
       } else if (is_vec_one(op_const[2])) {
-         this->progress = true;
-         return swizzle_if_required(ir, ir->operands[1]);
+         return ir->operands[1];
+      } else if (ir->operands[0]->equals(ir->operands[1])) {
+         return ir->operands[0];
+      } else if (is_vec_zero(op_const[0])) {
+         return mul(ir->operands[1], ir->operands[2]);
+      } else if (is_vec_zero(op_const[1])) {
+         unsigned op2_components = ir->operands[2]->type->vector_elements;
+         ir_constant *one = new(mem_ctx) ir_constant(1.0f, op2_components);
+         return mul(ir->operands[0], add(one, neg(ir->operands[2])));
       }
       break;
 
+   case ir_triop_csel:
+      if (is_vec_one(op_const[0]))
+        return ir->operands[1];
+      if (is_vec_zero(op_const[0]))
+        return ir->operands[2];
+      break;
+
    default:
       break;
    }
@@ -502,13 +637,24 @@ 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
-do_algebraic(exec_list *instructions)
+do_algebraic(exec_list *instructions, bool native_integers,
+             const struct gl_shader_compiler_options *options)
 {
-   ir_algebraic_visitor v;
+   ir_algebraic_visitor v(native_integers, options);
 
    visit_list_elements(&v, instructions);