#include "ir_optimization.h"
#include "ir_builder.h"
#include "compiler/glsl_types.h"
+#include "main/mtypes.h"
using namespace ir_builder;
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.
*/
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;
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)
*/
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;