diff options
Diffstat (limited to 'mesalib/src/glsl/opt_algebraic.cpp')
-rw-r--r-- | mesalib/src/glsl/opt_algebraic.cpp | 166 |
1 files changed, 69 insertions, 97 deletions
diff --git a/mesalib/src/glsl/opt_algebraic.cpp b/mesalib/src/glsl/opt_algebraic.cpp index 37b2f02c6..a07e153ae 100644 --- a/mesalib/src/glsl/opt_algebraic.cpp +++ b/mesalib/src/glsl/opt_algebraic.cpp @@ -32,8 +32,11 @@ #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 { /** @@ -194,7 +197,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); @@ -217,12 +219,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; } @@ -233,8 +230,7 @@ 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; @@ -261,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], @@ -272,14 +267,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,50 +282,25 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) 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 @@ -349,26 +315,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) @@ -377,7 +337,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) @@ -387,46 +346,52 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) } 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])); } 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]); } 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; @@ -434,16 +399,20 @@ 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])); } 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 -> @@ -452,12 +421,7 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) /* 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; @@ -465,11 +429,9 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir) 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]; } break; @@ -490,7 +452,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 |