egl/wayland: add missing newline between functions
[mesa.git] / src / compiler / glsl / opt_algebraic.cpp
index ce5f26559a77788eb1f92f65443bd857ec2b0f07..7cef4fc6ef934f12b306a64d129ddc97f48ae386 100644 (file)
@@ -34,6 +34,7 @@
 #include "ir_optimization.h"
 #include "ir_builder.h"
 #include "compiler/glsl_types.h"
+#include "main/mtypes.h"
 
 using namespace ir_builder;
 
@@ -506,6 +507,18 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       if (is_vec_zero(op_const[1]))
         return ir->operands[0];
 
+      /* Replace (x + (-x)) with constant 0 */
+      for (int i = 0; i < 2; i++) {
+         if (op_expr[i]) {
+            if (op_expr[i]->operation == ir_unop_neg) {
+               ir_rvalue *other = ir->operands[(i + 1) % 2];
+               if (other && op_expr[i]->operands[0]->equals(other)) {
+                  return ir_constant::zero(ir, ir->type);
+               }
+            }
+         }
+      }
+
       /* Reassociate addition of constants so that we can do constant
        * folding.
        */
@@ -565,7 +578,8 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
                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 ||
+               if (!x_operand->type->is_float_16_32_64() ||
+                   x_operand->type != y_operand->type ||
                    x_operand->type != a_operand->type)
                   continue;
 
@@ -708,6 +722,12 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
          if (!is_vec_zero(zero))
             continue;
 
+         /* We are allowed to add scalars with a vector or matrix. In that
+          * case lets just exit early.
+          */
+         if (add->operands[0]->type != add->operands[1]->type)
+            continue;
+
          /* Depending of the zero position we want to optimize
           * (0 cmp x+y) into (-x cmp y) or (x+y cmp 0) into (x cmp -y)
           */
@@ -964,6 +984,9 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
          ir_constant *one;
 
          switch (ir->type->base_type) {
+         case GLSL_TYPE_FLOAT16:
+            one = new(mem_ctx) ir_constant(float16_t::one(), op2_components);
+            break;
          case GLSL_TYPE_FLOAT:
             one = new(mem_ctx) ir_constant(1.0f, op2_components);
             break;