2010-07-26 22:50:29 -07:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-11-17 10:43:10 -08:00
|
|
|
* \file opt_algebraic.cpp
|
2010-07-26 22:50:29 -07:00
|
|
|
*
|
|
|
|
* Takes advantage of association, commutivity, and other algebraic
|
|
|
|
* properties to simplify expressions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ir.h"
|
|
|
|
#include "ir_visitor.h"
|
2010-08-13 20:39:24 -07:00
|
|
|
#include "ir_rvalue_visitor.h"
|
2010-07-26 22:50:29 -07:00
|
|
|
#include "ir_optimization.h"
|
2013-10-23 16:40:16 -07:00
|
|
|
#include "ir_builder.h"
|
2010-07-26 22:50:29 -07:00
|
|
|
#include "glsl_types.h"
|
|
|
|
|
2013-10-23 16:40:16 -07:00
|
|
|
using namespace ir_builder;
|
|
|
|
|
2012-05-29 16:18:37 -07:00
|
|
|
namespace {
|
|
|
|
|
2010-07-26 22:50:29 -07:00
|
|
|
/**
|
|
|
|
* Visitor class for replacing expressions with ir_constant values.
|
|
|
|
*/
|
|
|
|
|
2010-08-13 20:39:24 -07:00
|
|
|
class ir_algebraic_visitor : public ir_rvalue_visitor {
|
2010-07-26 22:50:29 -07:00
|
|
|
public:
|
2014-02-28 17:49:20 -08:00
|
|
|
ir_algebraic_visitor(bool native_integers,
|
|
|
|
const struct gl_shader_compiler_options *options)
|
|
|
|
: options(options)
|
2010-07-26 22:50:29 -07:00
|
|
|
{
|
|
|
|
this->progress = false;
|
2010-08-17 13:27:44 -07:00
|
|
|
this->mem_ctx = NULL;
|
2014-04-06 23:28:02 -07:00
|
|
|
this->native_integers = native_integers;
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ir_algebraic_visitor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-13 20:39:24 -07:00
|
|
|
ir_rvalue *handle_expression(ir_expression *ir);
|
|
|
|
void handle_rvalue(ir_rvalue **rvalue);
|
2010-07-26 23:56:19 -07:00
|
|
|
bool reassociate_constant(ir_expression *ir1,
|
|
|
|
int const_index,
|
|
|
|
ir_constant *constant,
|
|
|
|
ir_expression *ir2);
|
|
|
|
void reassociate_operands(ir_expression *ir1,
|
|
|
|
int op1,
|
|
|
|
ir_expression *ir2,
|
|
|
|
int op2);
|
2010-08-17 13:24:50 -07:00
|
|
|
ir_rvalue *swizzle_if_required(ir_expression *expr,
|
|
|
|
ir_rvalue *operand);
|
2010-08-17 13:27:44 -07:00
|
|
|
|
2014-02-28 17:49:20 -08:00
|
|
|
const struct gl_shader_compiler_options *options;
|
2010-08-17 13:27:44 -07:00
|
|
|
void *mem_ctx;
|
2010-08-17 13:24:50 -07:00
|
|
|
|
2014-04-06 23:28:02 -07:00
|
|
|
bool native_integers;
|
2010-07-26 22:50:29 -07:00
|
|
|
bool progress;
|
|
|
|
};
|
|
|
|
|
2012-05-29 16:18:37 -07:00
|
|
|
} /* unnamed namespace */
|
|
|
|
|
2010-11-12 10:19:08 -08:00
|
|
|
static inline bool
|
2010-07-26 22:50:29 -07:00
|
|
|
is_vec_zero(ir_constant *ir)
|
|
|
|
{
|
2010-11-12 10:19:08 -08:00
|
|
|
return (ir == NULL) ? false : ir->is_zero();
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
|
|
|
|
2010-11-12 10:19:08 -08:00
|
|
|
static inline bool
|
2010-07-26 22:50:29 -07:00
|
|
|
is_vec_one(ir_constant *ir)
|
|
|
|
{
|
2010-11-12 10:19:08 -08:00
|
|
|
return (ir == NULL) ? false : ir->is_one();
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
|
|
|
|
2014-01-05 22:57:01 -08:00
|
|
|
static inline bool
|
|
|
|
is_vec_two(ir_constant *ir)
|
|
|
|
{
|
|
|
|
return (ir == NULL) ? false : ir->is_value(2.0, 2);
|
|
|
|
}
|
|
|
|
|
2013-10-15 23:42:19 -07:00
|
|
|
static inline bool
|
|
|
|
is_vec_negative_one(ir_constant *ir)
|
|
|
|
{
|
|
|
|
return (ir == NULL) ? false : ir->is_negative_one();
|
|
|
|
}
|
|
|
|
|
2014-06-19 22:17:20 -07:00
|
|
|
static inline bool
|
|
|
|
is_valid_vec_const(ir_constant *ir)
|
|
|
|
{
|
|
|
|
if (ir == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ir->type->is_scalar() && !ir->type->is_vector())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
is_less_than_one(ir_constant *ir)
|
|
|
|
{
|
|
|
|
if (!is_valid_vec_const(ir))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned component = 0;
|
|
|
|
for (int c = 0; c < ir->type->vector_elements; c++) {
|
|
|
|
if (ir->get_float_component(c) < 1.0f)
|
|
|
|
component++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (component == ir->type->vector_elements);
|
|
|
|
}
|
|
|
|
|
2014-07-08 14:12:50 +03:00
|
|
|
static inline bool
|
|
|
|
is_greater_than_zero(ir_constant *ir)
|
|
|
|
{
|
|
|
|
if (!is_valid_vec_const(ir))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned component = 0;
|
|
|
|
for (int c = 0; c < ir->type->vector_elements; c++) {
|
|
|
|
if (ir->get_float_component(c) > 0.0f)
|
|
|
|
component++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (component == ir->type->vector_elements);
|
|
|
|
}
|
|
|
|
|
2010-07-26 23:56:19 -07:00
|
|
|
static void
|
|
|
|
update_type(ir_expression *ir)
|
|
|
|
{
|
|
|
|
if (ir->operands[0]->type->is_vector())
|
|
|
|
ir->type = ir->operands[0]->type;
|
|
|
|
else
|
|
|
|
ir->type = ir->operands[1]->type;
|
|
|
|
}
|
|
|
|
|
2014-03-02 08:59:50 -08:00
|
|
|
/* Recognize (v.x + v.y) + (v.z + v.w) as dot(v, 1.0) */
|
|
|
|
static ir_expression *
|
|
|
|
try_replace_with_dot(ir_expression *expr0, ir_expression *expr1, void *mem_ctx)
|
|
|
|
{
|
|
|
|
if (expr0 && expr0->operation == ir_binop_add &&
|
2014-06-24 19:12:24 -07:00
|
|
|
expr0->type->is_float() &&
|
|
|
|
expr1 && expr1->operation == ir_binop_add &&
|
|
|
|
expr1->type->is_float()) {
|
2014-03-02 08:59:50 -08:00
|
|
|
ir_swizzle *x = expr0->operands[0]->as_swizzle();
|
|
|
|
ir_swizzle *y = expr0->operands[1]->as_swizzle();
|
|
|
|
ir_swizzle *z = expr1->operands[0]->as_swizzle();
|
|
|
|
ir_swizzle *w = expr1->operands[1]->as_swizzle();
|
|
|
|
|
|
|
|
if (!x || x->mask.num_components != 1 ||
|
|
|
|
!y || y->mask.num_components != 1 ||
|
|
|
|
!z || z->mask.num_components != 1 ||
|
|
|
|
!w || w->mask.num_components != 1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool swiz_seen[4] = {false, false, false, false};
|
|
|
|
swiz_seen[x->mask.x] = true;
|
|
|
|
swiz_seen[y->mask.x] = true;
|
|
|
|
swiz_seen[z->mask.x] = true;
|
|
|
|
swiz_seen[w->mask.x] = true;
|
|
|
|
|
|
|
|
if (!swiz_seen[0] || !swiz_seen[1] ||
|
|
|
|
!swiz_seen[2] || !swiz_seen[3]) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x->val->equals(y->val) &&
|
|
|
|
x->val->equals(z->val) &&
|
|
|
|
x->val->equals(w->val)) {
|
|
|
|
return dot(x->val, new(mem_ctx) ir_constant(1.0f, 4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-26 23:56:19 -07:00
|
|
|
void
|
|
|
|
ir_algebraic_visitor::reassociate_operands(ir_expression *ir1,
|
|
|
|
int op1,
|
|
|
|
ir_expression *ir2,
|
|
|
|
int op2)
|
|
|
|
{
|
|
|
|
ir_rvalue *temp = ir2->operands[op2];
|
|
|
|
ir2->operands[op2] = ir1->operands[op1];
|
|
|
|
ir1->operands[op1] = temp;
|
|
|
|
|
|
|
|
/* Update the type of ir2. The type of ir1 won't have changed --
|
|
|
|
* base types matched, and at least one of the operands of the 2
|
|
|
|
* binops is still a vector if any of them were.
|
|
|
|
*/
|
|
|
|
update_type(ir2);
|
|
|
|
|
|
|
|
this->progress = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reassociates a constant down a tree of adds or multiplies.
|
|
|
|
*
|
|
|
|
* Consider (2 * (a * (b * 0.5))). We want to send up with a * b.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
ir_algebraic_visitor::reassociate_constant(ir_expression *ir1, int const_index,
|
|
|
|
ir_constant *constant,
|
|
|
|
ir_expression *ir2)
|
|
|
|
{
|
|
|
|
if (!ir2 || ir1->operation != ir2->operation)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Don't want to even think about matrices. */
|
|
|
|
if (ir1->operands[0]->type->is_matrix() ||
|
2010-11-30 13:24:44 -08:00
|
|
|
ir1->operands[1]->type->is_matrix() ||
|
|
|
|
ir2->operands[0]->type->is_matrix() ||
|
2010-07-26 23:56:19 -07:00
|
|
|
ir2->operands[1]->type->is_matrix())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ir_constant *ir2_const[2];
|
|
|
|
ir2_const[0] = ir2->operands[0]->constant_expression_value();
|
|
|
|
ir2_const[1] = ir2->operands[1]->constant_expression_value();
|
|
|
|
|
|
|
|
if (ir2_const[0] && ir2_const[1])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ir2_const[0]) {
|
|
|
|
reassociate_operands(ir1, const_index, ir2, 1);
|
|
|
|
return true;
|
|
|
|
} else if (ir2_const[1]) {
|
|
|
|
reassociate_operands(ir1, const_index, ir2, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reassociate_constant(ir1, const_index, constant,
|
|
|
|
ir2->operands[0]->as_expression())) {
|
|
|
|
update_type(ir2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reassociate_constant(ir1, const_index, constant,
|
|
|
|
ir2->operands[1]->as_expression())) {
|
|
|
|
update_type(ir2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-17 13:24:50 -07:00
|
|
|
/* When eliminating an expression and just returning one of its operands,
|
|
|
|
* we may need to swizzle that operand out to a vector if the expression was
|
|
|
|
* vector type.
|
|
|
|
*/
|
|
|
|
ir_rvalue *
|
|
|
|
ir_algebraic_visitor::swizzle_if_required(ir_expression *expr,
|
|
|
|
ir_rvalue *operand)
|
|
|
|
{
|
|
|
|
if (expr->type->is_vector() && operand->type->is_scalar()) {
|
|
|
|
return new(mem_ctx) ir_swizzle(operand, 0, 0, 0, 0,
|
|
|
|
expr->type->vector_elements);
|
|
|
|
} else
|
|
|
|
return operand;
|
|
|
|
}
|
|
|
|
|
2010-07-26 22:50:29 -07:00
|
|
|
ir_rvalue *
|
2010-08-13 20:39:24 -07:00
|
|
|
ir_algebraic_visitor::handle_expression(ir_expression *ir)
|
2010-07-26 22:50:29 -07:00
|
|
|
{
|
2013-04-09 17:45:12 -07:00
|
|
|
ir_constant *op_const[4] = {NULL, NULL, NULL, NULL};
|
|
|
|
ir_expression *op_expr[4] = {NULL, NULL, NULL, NULL};
|
2010-07-26 22:50:29 -07:00
|
|
|
unsigned int i;
|
|
|
|
|
2013-04-09 17:45:12 -07:00
|
|
|
assert(ir->get_num_operands() <= 4);
|
2010-07-26 22:50:29 -07:00
|
|
|
for (i = 0; i < ir->get_num_operands(); i++) {
|
|
|
|
if (ir->operands[i]->type->is_matrix())
|
2010-08-13 20:39:24 -07:00
|
|
|
return ir;
|
2010-07-26 22:50:29 -07:00
|
|
|
|
|
|
|
op_const[i] = ir->operands[i]->constant_expression_value();
|
|
|
|
op_expr[i] = ir->operands[i]->as_expression();
|
|
|
|
}
|
|
|
|
|
2010-08-17 13:27:44 -07:00
|
|
|
if (this->mem_ctx == NULL)
|
2011-01-21 14:32:31 -08:00
|
|
|
this->mem_ctx = ralloc_parent(ir);
|
2010-08-17 13:27:44 -07:00
|
|
|
|
2010-07-26 22:50:29 -07:00
|
|
|
switch (ir->operation) {
|
2014-01-18 10:36:28 -08:00
|
|
|
case ir_unop_bit_not:
|
|
|
|
if (op_expr[0] && op_expr[0]->operation == ir_unop_bit_not)
|
|
|
|
return op_expr[0]->operands[0];
|
|
|
|
break;
|
|
|
|
|
2013-10-16 16:56:44 -07:00
|
|
|
case ir_unop_abs:
|
|
|
|
if (op_expr[0] == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (op_expr[0]->operation) {
|
|
|
|
case ir_unop_abs:
|
|
|
|
case ir_unop_neg:
|
2013-10-24 15:03:45 -07:00
|
|
|
return abs(op_expr[0]->operands[0]);
|
2013-10-16 16:56:44 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-10-16 16:56:45 -07:00
|
|
|
case ir_unop_neg:
|
|
|
|
if (op_expr[0] == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (op_expr[0]->operation == ir_unop_neg) {
|
2013-10-24 14:55:50 -07:00
|
|
|
return op_expr[0]->operands[0];
|
2013-10-16 16:56:45 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-18 10:47:19 -08:00
|
|
|
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];
|
|
|
|
}
|
2014-09-07 00:41:41 -07:00
|
|
|
|
|
|
|
if (!options->EmitNoPow && op_expr[0]->operation == ir_binop_mul) {
|
|
|
|
for (int log2_pos = 0; log2_pos < 2; log2_pos++) {
|
|
|
|
ir_expression *log2_expr =
|
|
|
|
op_expr[0]->operands[log2_pos]->as_expression();
|
|
|
|
|
|
|
|
if (log2_expr && log2_expr->operation == ir_unop_log2) {
|
|
|
|
return new(mem_ctx) ir_expression(ir_binop_pow,
|
|
|
|
ir->type,
|
|
|
|
log2_expr->operands[0],
|
|
|
|
op_expr[0]->operands[1 - log2_pos]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-18 10:47:19 -08:00
|
|
|
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;
|
|
|
|
|
2010-08-09 09:54:47 -07:00
|
|
|
case ir_unop_logic_not: {
|
|
|
|
enum ir_expression_operation new_op = ir_unop_logic_not;
|
|
|
|
|
|
|
|
if (op_expr[0] == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (op_expr[0]->operation) {
|
2010-08-09 10:46:38 -07:00
|
|
|
case ir_binop_less: new_op = ir_binop_gequal; break;
|
|
|
|
case ir_binop_greater: new_op = ir_binop_lequal; break;
|
|
|
|
case ir_binop_lequal: new_op = ir_binop_greater; break;
|
|
|
|
case ir_binop_gequal: new_op = ir_binop_less; break;
|
2010-08-09 09:54:47 -07:00
|
|
|
case ir_binop_equal: new_op = ir_binop_nequal; break;
|
|
|
|
case ir_binop_nequal: new_op = ir_binop_equal; break;
|
2010-09-08 01:31:39 +02:00
|
|
|
case ir_binop_all_equal: new_op = ir_binop_any_nequal; break;
|
|
|
|
case ir_binop_any_nequal: new_op = ir_binop_all_equal; break;
|
2010-08-09 09:54:47 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* The default case handler is here to silence a warning from GCC.
|
|
|
|
*/
|
|
|
|
break;
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
2010-08-09 09:54:47 -07:00
|
|
|
|
|
|
|
if (new_op != ir_unop_logic_not) {
|
2010-08-17 13:27:44 -07:00
|
|
|
return new(mem_ctx) ir_expression(new_op,
|
|
|
|
ir->type,
|
|
|
|
op_expr[0]->operands[0],
|
|
|
|
op_expr[0]->operands[1]);
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
2010-08-09 09:54:47 -07:00
|
|
|
|
2010-07-26 22:50:29 -07:00
|
|
|
break;
|
2010-08-09 09:54:47 -07:00
|
|
|
}
|
2010-07-26 22:50:29 -07:00
|
|
|
|
|
|
|
case ir_binop_add:
|
2013-10-24 14:55:50 -07:00
|
|
|
if (is_vec_zero(op_const[0]))
|
|
|
|
return ir->operands[1];
|
|
|
|
if (is_vec_zero(op_const[1]))
|
|
|
|
return ir->operands[0];
|
2010-07-26 23:56:19 -07:00
|
|
|
|
|
|
|
/* Reassociate addition of constants so that we can do constant
|
|
|
|
* folding.
|
|
|
|
*/
|
|
|
|
if (op_const[0] && !op_const[1])
|
2013-10-16 16:56:43 -07:00
|
|
|
reassociate_constant(ir, 0, op_const[0], op_expr[1]);
|
2010-07-26 23:56:19 -07:00
|
|
|
if (op_const[1] && !op_const[0])
|
2013-10-16 16:56:43 -07:00
|
|
|
reassociate_constant(ir, 1, op_const[1], op_expr[0]);
|
2013-11-04 10:23:24 -08:00
|
|
|
|
2014-03-02 08:59:50 -08:00
|
|
|
/* Recognize (v.x + v.y) + (v.z + v.w) as dot(v, 1.0) */
|
|
|
|
if (options->OptimizeForAOS) {
|
|
|
|
ir_expression *expr = try_replace_with_dot(op_expr[0], op_expr[1],
|
|
|
|
mem_ctx);
|
|
|
|
if (expr)
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2013-11-04 10:23:24 -08:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 17:49:20 -08:00
|
|
|
|
2010-07-26 22:50:29 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ir_binop_sub:
|
2013-10-24 15:03:45 -07:00
|
|
|
if (is_vec_zero(op_const[0]))
|
|
|
|
return neg(ir->operands[1]);
|
2013-10-24 14:55:50 -07:00
|
|
|
if (is_vec_zero(op_const[1]))
|
|
|
|
return ir->operands[0];
|
2010-07-26 22:50:29 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ir_binop_mul:
|
2013-10-24 14:55:50 -07:00
|
|
|
if (is_vec_one(op_const[0]))
|
|
|
|
return ir->operands[1];
|
|
|
|
if (is_vec_one(op_const[1]))
|
|
|
|
return ir->operands[0];
|
2010-07-26 22:50:29 -07:00
|
|
|
|
2013-10-24 14:55:50 -07:00
|
|
|
if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1]))
|
2010-07-28 12:20:38 -07:00
|
|
|
return ir_constant::zero(ir, ir->type);
|
2013-10-24 14:55:50 -07:00
|
|
|
|
2013-10-24 15:03:45 -07:00
|
|
|
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]);
|
2013-10-15 23:42:19 -07:00
|
|
|
|
2010-07-26 23:56:19 -07:00
|
|
|
|
|
|
|
/* Reassociate multiplication of constants so that we can do
|
|
|
|
* constant folding.
|
|
|
|
*/
|
|
|
|
if (op_const[0] && !op_const[1])
|
2013-10-16 16:56:43 -07:00
|
|
|
reassociate_constant(ir, 0, op_const[0], op_expr[1]);
|
2010-07-26 23:56:19 -07:00
|
|
|
if (op_const[1] && !op_const[0])
|
2013-10-16 16:56:43 -07:00
|
|
|
reassociate_constant(ir, 1, op_const[1], op_expr[0]);
|
2010-07-26 23:56:19 -07:00
|
|
|
|
2015-01-28 10:09:14 -08:00
|
|
|
/* Optimizes
|
|
|
|
*
|
|
|
|
* (mul (floor (add (abs x) 0.5) (sign x)))
|
|
|
|
*
|
|
|
|
* into
|
|
|
|
*
|
|
|
|
* (trunc (add x (mul (sign x) 0.5)))
|
|
|
|
*/
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
ir_expression *sign_expr = ir->operands[i]->as_expression();
|
|
|
|
ir_expression *floor_expr = ir->operands[1 - i]->as_expression();
|
|
|
|
|
|
|
|
if (!sign_expr || sign_expr->operation != ir_unop_sign ||
|
|
|
|
!floor_expr || floor_expr->operation != ir_unop_floor)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ir_expression *add_expr = floor_expr->operands[0]->as_expression();
|
|
|
|
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
ir_expression *abs_expr = add_expr->operands[j]->as_expression();
|
|
|
|
if (!abs_expr || abs_expr->operation != ir_unop_abs)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ir_constant *point_five = add_expr->operands[1 - j]->as_constant();
|
|
|
|
if (!point_five->is_value(0.5, 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (abs_expr->operands[0]->equals(sign_expr->operands[0])) {
|
|
|
|
return trunc(add(abs_expr->operands[0],
|
|
|
|
mul(sign_expr, point_five)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-26 22:50:29 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ir_binop_div:
|
|
|
|
if (is_vec_one(op_const[0]) && ir->type->base_type == GLSL_TYPE_FLOAT) {
|
2013-10-24 14:55:50 -07:00
|
|
|
return new(mem_ctx) ir_expression(ir_unop_rcp,
|
2010-08-17 13:24:50 -07:00
|
|
|
ir->operands[1]->type,
|
2010-08-17 13:27:44 -07:00
|
|
|
ir->operands[1],
|
|
|
|
NULL);
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
2013-10-24 14:55:50 -07:00
|
|
|
if (is_vec_one(op_const[1]))
|
|
|
|
return ir->operands[0];
|
2010-07-26 22:50:29 -07:00
|
|
|
break;
|
|
|
|
|
2012-06-04 16:17:12 -04:00
|
|
|
case ir_binop_dot:
|
2013-10-24 14:55:50 -07:00
|
|
|
if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1]))
|
2012-06-04 16:17:12 -04:00
|
|
|
return ir_constant::zero(mem_ctx, ir->type);
|
2013-10-24 14:55:50 -07:00
|
|
|
|
2014-10-17 20:32:58 -07:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (!op_const[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned components[4] = { 0 }, count = 0;
|
|
|
|
|
|
|
|
for (unsigned c = 0; c < op_const[i]->type->vector_elements; c++) {
|
|
|
|
if (op_const[i]->value.f[c] == 0.0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
components[count] = c;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No channels had zero values; bail. */
|
|
|
|
if (count >= op_const[i]->type->vector_elements)
|
|
|
|
break;
|
|
|
|
|
2014-10-31 10:33:17 -07:00
|
|
|
ir_expression_operation op = count == 1 ?
|
|
|
|
ir_binop_mul : ir_binop_dot;
|
|
|
|
|
2014-10-17 20:32:58 -07:00
|
|
|
/* Swizzle both operands to remove the channels that were zero. */
|
|
|
|
return new(mem_ctx)
|
2014-10-31 10:33:17 -07:00
|
|
|
ir_expression(op, glsl_type::float_type,
|
2014-10-17 20:32:58 -07:00
|
|
|
new(mem_ctx) ir_swizzle(ir->operands[0],
|
|
|
|
components, count),
|
|
|
|
new(mem_ctx) ir_swizzle(ir->operands[1],
|
|
|
|
components, count));
|
|
|
|
}
|
2012-06-04 16:17:12 -04:00
|
|
|
break;
|
|
|
|
|
2014-03-29 12:31:24 -07:00
|
|
|
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;
|
|
|
|
|
2014-08-22 14:30:38 -07:00
|
|
|
case ir_binop_all_equal:
|
|
|
|
case ir_binop_any_nequal:
|
|
|
|
if (ir->operands[0]->type->is_scalar() &&
|
|
|
|
ir->operands[1]->type->is_scalar())
|
|
|
|
return new(mem_ctx) ir_expression(ir->operation == ir_binop_all_equal
|
|
|
|
? ir_binop_equal : ir_binop_nequal,
|
|
|
|
ir->operands[0],
|
|
|
|
ir->operands[1]);
|
|
|
|
break;
|
|
|
|
|
2013-10-21 17:01:49 -07:00
|
|
|
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;
|
|
|
|
|
2010-09-01 10:13:21 -07:00
|
|
|
case ir_binop_logic_and:
|
|
|
|
if (is_vec_one(op_const[0])) {
|
|
|
|
return ir->operands[1];
|
|
|
|
} else if (is_vec_one(op_const[1])) {
|
|
|
|
return ir->operands[0];
|
|
|
|
} else if (is_vec_zero(op_const[0]) || is_vec_zero(op_const[1])) {
|
|
|
|
return ir_constant::zero(mem_ctx, ir->type);
|
2013-10-23 16:42:34 -07:00
|
|
|
} 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)
|
|
|
|
*/
|
2013-10-24 14:55:50 -07:00
|
|
|
return logic_not(logic_or(op_expr[0]->operands[0],
|
2013-10-23 16:42:34 -07:00
|
|
|
op_expr[1]->operands[0]));
|
2013-10-31 09:32:42 -07:00
|
|
|
} else if (ir->operands[0]->equals(ir->operands[1])) {
|
|
|
|
/* (a && a) == a */
|
|
|
|
return ir->operands[0];
|
2010-09-01 10:13:21 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ir_binop_logic_xor:
|
|
|
|
if (is_vec_zero(op_const[0])) {
|
|
|
|
return ir->operands[1];
|
|
|
|
} else if (is_vec_zero(op_const[1])) {
|
|
|
|
return ir->operands[0];
|
|
|
|
} else if (is_vec_one(op_const[0])) {
|
2013-10-24 15:03:45 -07:00
|
|
|
return logic_not(ir->operands[1]);
|
2010-09-01 10:13:21 -07:00
|
|
|
} else if (is_vec_one(op_const[1])) {
|
2013-10-24 15:03:45 -07:00
|
|
|
return logic_not(ir->operands[0]);
|
2013-11-07 12:10:25 -08:00
|
|
|
} else if (ir->operands[0]->equals(ir->operands[1])) {
|
|
|
|
/* (a ^^ a) == false */
|
|
|
|
return ir_constant::zero(mem_ctx, ir->type);
|
2010-09-01 10:13:21 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ir_binop_logic_or:
|
|
|
|
if (is_vec_zero(op_const[0])) {
|
|
|
|
return ir->operands[1];
|
|
|
|
} else if (is_vec_zero(op_const[1])) {
|
|
|
|
return ir->operands[0];
|
|
|
|
} else if (is_vec_one(op_const[0]) || is_vec_one(op_const[1])) {
|
|
|
|
ir_constant_data data;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 16; i++)
|
|
|
|
data.b[i] = true;
|
|
|
|
|
|
|
|
return new(mem_ctx) ir_constant(ir->type, &data);
|
2013-10-23 16:40:16 -07:00
|
|
|
} 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)
|
|
|
|
*/
|
2013-10-24 14:55:50 -07:00
|
|
|
return logic_not(logic_and(op_expr[0]->operands[0],
|
2013-10-23 16:40:16 -07:00
|
|
|
op_expr[1]->operands[0]));
|
2013-10-31 00:10:32 -07:00
|
|
|
} else if (ir->operands[0]->equals(ir->operands[1])) {
|
|
|
|
/* (a || a) == a */
|
|
|
|
return ir->operands[0];
|
2010-09-01 10:13:21 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-05 22:19:42 -08:00
|
|
|
case ir_binop_pow:
|
|
|
|
/* 1^x == 1 */
|
|
|
|
if (is_vec_one(op_const[0]))
|
|
|
|
return op_const[0];
|
2014-01-05 22:57:01 -08:00
|
|
|
|
2014-01-18 10:57:29 -08:00
|
|
|
/* x^1 == x */
|
|
|
|
if (is_vec_one(op_const[1]))
|
|
|
|
return ir->operands[0];
|
|
|
|
|
2014-01-05 22:57:01 -08:00
|
|
|
/* pow(2,x) == exp2(x) */
|
|
|
|
if (is_vec_two(op_const[0]))
|
|
|
|
return expr(ir_unop_exp2, ir->operands[1]);
|
|
|
|
|
2014-02-28 13:33:19 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-05 22:19:42 -08:00
|
|
|
break;
|
|
|
|
|
2014-06-19 22:15:14 -07:00
|
|
|
case ir_binop_min:
|
|
|
|
case ir_binop_max:
|
2014-12-08 13:31:29 +02:00
|
|
|
if (ir->type->base_type != GLSL_TYPE_FLOAT || options->EmitNoSat)
|
2014-06-19 22:15:14 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Replace min(max) operations and its commutative combinations with
|
|
|
|
* a saturate operation
|
|
|
|
*/
|
|
|
|
for (int op = 0; op < 2; op++) {
|
|
|
|
ir_expression *minmax = op_expr[op];
|
|
|
|
ir_constant *outer_const = op_const[1 - op];
|
|
|
|
ir_expression_operation op_cond = (ir->operation == ir_binop_max) ?
|
|
|
|
ir_binop_min : ir_binop_max;
|
|
|
|
|
|
|
|
if (!minmax || !outer_const || (minmax->operation != op_cond))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Found a min(max) combination. Now try to see if its operands
|
|
|
|
* meet our conditions that we can do just a single saturate operation
|
|
|
|
*/
|
|
|
|
for (int minmax_op = 0; minmax_op < 2; minmax_op++) {
|
|
|
|
ir_rvalue *inner_val_a = minmax->operands[minmax_op];
|
|
|
|
ir_rvalue *inner_val_b = minmax->operands[1 - minmax_op];
|
|
|
|
|
|
|
|
if (!inner_val_a || !inner_val_b)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Found a {min|max} ({max|min} (x, 0.0), 1.0) operation and its variations */
|
|
|
|
if ((outer_const->is_one() && inner_val_a->is_zero()) ||
|
|
|
|
(inner_val_a->is_one() && outer_const->is_zero()))
|
|
|
|
return saturate(inner_val_b);
|
2014-06-19 22:17:20 -07:00
|
|
|
|
|
|
|
/* Found a {min|max} ({max|min} (x, 0.0), b) where b < 1.0
|
|
|
|
* and its variations
|
|
|
|
*/
|
|
|
|
if (is_less_than_one(outer_const) && inner_val_b->is_zero())
|
|
|
|
return expr(ir_binop_min, saturate(inner_val_a), outer_const);
|
|
|
|
|
|
|
|
if (!inner_val_b->as_constant())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (is_less_than_one(inner_val_b->as_constant()) && outer_const->is_zero())
|
|
|
|
return expr(ir_binop_min, saturate(inner_val_a), inner_val_b);
|
2014-07-08 14:12:50 +03:00
|
|
|
|
|
|
|
/* Found a {min|max} ({max|min} (x, b), 1.0), where b > 0.0
|
|
|
|
* and its variations
|
|
|
|
*/
|
|
|
|
if (outer_const->is_one() && is_greater_than_zero(inner_val_b->as_constant()))
|
|
|
|
return expr(ir_binop_max, saturate(inner_val_a), inner_val_b);
|
|
|
|
if (inner_val_b->as_constant()->is_one() && is_greater_than_zero(outer_const))
|
|
|
|
return expr(ir_binop_max, saturate(inner_val_a), outer_const);
|
2014-06-19 22:15:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2010-07-26 22:50:29 -07:00
|
|
|
case ir_unop_rcp:
|
2013-10-24 14:55:50 -07:00
|
|
|
if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp)
|
2010-07-26 22:50:29 -07:00
|
|
|
return op_expr[0]->operands[0];
|
|
|
|
|
2015-02-10 12:31:37 -08:00
|
|
|
if (op_expr[0] && (op_expr[0]->operation == ir_unop_exp2 ||
|
|
|
|
op_expr[0]->operation == ir_unop_exp)) {
|
|
|
|
return new(mem_ctx) ir_expression(op_expr[0]->operation, ir->type,
|
|
|
|
neg(op_expr[0]->operands[0]));
|
|
|
|
}
|
|
|
|
|
2013-11-07 12:15:13 -08:00
|
|
|
/* 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.
|
2010-07-26 22:50:29 -07:00
|
|
|
*/
|
2013-11-07 12:15:13 -08:00
|
|
|
if (op_expr[0] && op_expr[0]->operation == ir_unop_rsq)
|
|
|
|
return sqrt(op_expr[0]->operands[0]);
|
2010-07-26 22:50:29 -07:00
|
|
|
|
|
|
|
/* As far as we know, all backends are OK with rsq. */
|
|
|
|
if (op_expr[0] && op_expr[0]->operation == ir_unop_sqrt) {
|
2013-10-24 15:03:45 -07:00
|
|
|
return rsq(op_expr[0]->operands[0]);
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-01-18 11:06:16 -08:00
|
|
|
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;
|
|
|
|
|
2013-02-15 17:51:46 -08:00
|
|
|
case ir_triop_lrp:
|
|
|
|
/* Operands are (x, y, a). */
|
|
|
|
if (is_vec_zero(op_const[2])) {
|
2013-10-24 14:55:50 -07:00
|
|
|
return ir->operands[0];
|
2013-02-15 17:51:46 -08:00
|
|
|
} else if (is_vec_one(op_const[2])) {
|
2013-10-24 14:55:50 -07:00
|
|
|
return ir->operands[1];
|
2014-01-18 11:00:51 -08:00
|
|
|
} else if (ir->operands[0]->equals(ir->operands[1])) {
|
|
|
|
return ir->operands[0];
|
2014-02-24 15:00:44 -08:00
|
|
|
} else if (is_vec_zero(op_const[0])) {
|
|
|
|
return mul(ir->operands[1], ir->operands[2]);
|
2014-02-24 15:00:45 -08:00
|
|
|
} else if (is_vec_zero(op_const[1])) {
|
glsl: Fix broken LRP algebraic optimization.
opt_algebraic was translating lrp(x, 0, a) into add(x, -mul(x, a)).
Unfortunately, this references "x" twice, which is invalid in the IR,
leading to assertion failures in the validator.
Normally, cloning IR solves this. However, "x" could actually be an
arbitrary expression tree, so copying it could result in huge piles
of wasted computation. This is why we avoid reusing subexpressions.
Instead, transform it into mul(x, add(1.0, -a)), which is equivalent
but doesn't need two references to "x".
Fixes a regression since d5fa8a95621169, which isn't in any stable
branches. Fixes 18 shaders in shader-db (bastion and yofrankie).
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
2014-02-28 23:15:49 -08:00
|
|
|
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])));
|
2013-02-15 17:51:46 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-01 12:29:12 -07:00
|
|
|
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;
|
|
|
|
|
2010-07-26 22:50:29 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-13 20:39:24 -07:00
|
|
|
return ir;
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
|
|
|
|
2010-08-13 20:39:24 -07:00
|
|
|
void
|
|
|
|
ir_algebraic_visitor::handle_rvalue(ir_rvalue **rvalue)
|
2010-07-26 22:50:29 -07:00
|
|
|
{
|
2010-08-13 20:39:24 -07:00
|
|
|
if (!*rvalue)
|
|
|
|
return;
|
2010-07-26 22:50:29 -07:00
|
|
|
|
2010-08-13 20:39:24 -07:00
|
|
|
ir_expression *expr = (*rvalue)->as_expression();
|
2010-11-16 12:01:42 -08:00
|
|
|
if (!expr || expr->operation == ir_quadop_vector)
|
2010-08-13 20:39:24 -07:00
|
|
|
return;
|
2010-07-26 22:50:29 -07:00
|
|
|
|
2013-10-24 14:55:50 -07:00
|
|
|
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;
|
2010-07-26 22:50:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-02-28 17:49:20 -08:00
|
|
|
do_algebraic(exec_list *instructions, bool native_integers,
|
|
|
|
const struct gl_shader_compiler_options *options)
|
2010-07-26 22:50:29 -07:00
|
|
|
{
|
2014-02-28 17:49:20 -08:00
|
|
|
ir_algebraic_visitor v(native_integers, options);
|
2010-07-26 22:50:29 -07:00
|
|
|
|
|
|
|
visit_list_elements(&v, instructions);
|
|
|
|
|
|
|
|
return v.progress;
|
|
|
|
}
|