summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Anholt <[email protected]>2010-07-26 22:50:29 -0700
committerEric Anholt <[email protected]>2010-07-27 09:43:52 -0700
commit832aad989e3d319a8aaac046aa49df25da134d82 (patch)
treea0646404b655dbaac4ef112074ad985d02b4dea7
parent5533c6e38030ff6e26375a1a4e4bfa9ab2204d4c (diff)
glsl2: Add optimization pass for algebraic simplifications.
This cleans up the assembly output of almost all the non-logic tests glsl-algebraic-*. glsl-algebraic-pow-two needs love (basically, flattening to a temporary and squaring it).
-rw-r--r--src/glsl/Makefile1
-rw-r--r--src/glsl/ir.h8
-rw-r--r--src/glsl/ir_algebraic.cpp366
-rw-r--r--src/glsl/ir_optimization.h3
-rw-r--r--src/glsl/main.cpp1
-rw-r--r--src/mesa/program/ir_to_mesa.cpp1
6 files changed, 379 insertions, 1 deletions
diff --git a/src/glsl/Makefile b/src/glsl/Makefile
index 462d49e8840..4c85af8906d 100644
--- a/src/glsl/Makefile
+++ b/src/glsl/Makefile
@@ -30,6 +30,7 @@ CXX_SOURCES = \
glsl_parser_extras.cpp \
glsl_types.cpp \
hir_field_selection.cpp \
+ ir_algebraic.cpp \
ir_basic_block.cpp \
ir_clone.cpp \
ir_constant_expression.cpp \
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index e0f3683a7ab..7e8363106da 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -613,7 +613,15 @@ enum ir_expression_operation {
ir_binop_greater,
ir_binop_lequal,
ir_binop_gequal,
+ /**
+ * Returns single boolean for whether all components of operands[0]
+ * equal the components of operands[1].
+ */
ir_binop_equal,
+ /**
+ * Returns single boolean for whether any component of operands[0]
+ * is not equal to the corresponding component of operands[1].
+ */
ir_binop_nequal,
/*@}*/
diff --git a/src/glsl/ir_algebraic.cpp b/src/glsl/ir_algebraic.cpp
new file mode 100644
index 00000000000..5b065b086e5
--- /dev/null
+++ b/src/glsl/ir_algebraic.cpp
@@ -0,0 +1,366 @@
+/*
+ * 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.
+ */
+
+/**
+ * \file ir_algebraic.cpp
+ *
+ * Takes advantage of association, commutivity, and other algebraic
+ * properties to simplify expressions.
+ */
+
+#include "ir.h"
+#include "ir_visitor.h"
+#include "ir_optimization.h"
+#include "glsl_types.h"
+
+/**
+ * Visitor class for replacing expressions with ir_constant values.
+ */
+
+class ir_algebraic_visitor : public ir_hierarchical_visitor {
+public:
+ ir_algebraic_visitor()
+ {
+ this->progress = false;
+ }
+
+ virtual ~ir_algebraic_visitor()
+ {
+ }
+
+ virtual ir_visitor_status visit_leave(ir_assignment *);
+ virtual ir_visitor_status visit_leave(ir_call *);
+ virtual ir_visitor_status visit_leave(ir_dereference_array *);
+ virtual ir_visitor_status visit_leave(ir_expression *);
+ virtual ir_visitor_status visit_leave(ir_if *);
+ virtual ir_visitor_status visit_leave(ir_return *);
+ virtual ir_visitor_status visit_leave(ir_swizzle *);
+ virtual ir_visitor_status visit_leave(ir_texture *);
+
+ ir_rvalue *handle_expression(ir_rvalue *in_ir);
+
+ bool progress;
+};
+
+static bool
+is_vec_zero(ir_constant *ir)
+{
+ int c;
+
+ if (!ir)
+ return false;
+ if (!ir->type->is_scalar() &&
+ !ir->type->is_vector())
+ return false;
+
+ for (c = 0; c < ir->type->vector_elements; c++) {
+ switch (ir->type->base_type) {
+ case GLSL_TYPE_FLOAT:
+ if (ir->value.f[c] != 0.0)
+ return false;
+ break;
+ case GLSL_TYPE_INT:
+ if (ir->value.i[c] != 0)
+ return false;
+ break;
+ case GLSL_TYPE_UINT:
+ if (ir->value.u[c] != 0)
+ return false;
+ break;
+ case GLSL_TYPE_BOOL:
+ if (ir->value.b[c] != false)
+ return false;
+ break;
+ default:
+ assert(!"bad base type");
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static bool
+is_vec_one(ir_constant *ir)
+{
+ int c;
+
+ if (!ir)
+ return false;
+ if (!ir->type->is_scalar() &&
+ !ir->type->is_vector())
+ return false;
+
+ for (c = 0; c < ir->type->vector_elements; c++) {
+ switch (ir->type->base_type) {
+ case GLSL_TYPE_FLOAT:
+ if (ir->value.f[c] != 1.0)
+ return false;
+ break;
+ case GLSL_TYPE_INT:
+ if (ir->value.i[c] != 1)
+ return false;
+ break;
+ case GLSL_TYPE_UINT:
+ if (ir->value.u[c] != 1)
+ return false;
+ break;
+ case GLSL_TYPE_BOOL:
+ if (ir->value.b[c] != true)
+ return false;
+ break;
+ default:
+ assert(!"bad base type");
+ return false;
+ }
+ }
+
+ return true;
+}
+
+ir_rvalue *
+ir_algebraic_visitor::handle_expression(ir_rvalue *in_ir)
+{
+ ir_expression *ir = (ir_expression *)in_ir;
+ ir_constant *op_const[2] = {NULL, NULL};
+ ir_expression *op_expr[2] = {NULL, NULL};
+ unsigned int i;
+
+ if (!in_ir)
+ return NULL;
+
+ if (in_ir->ir_type != ir_type_expression)
+ return in_ir;
+
+ for (i = 0; i < ir->get_num_operands(); i++) {
+ if (ir->operands[i]->type->is_matrix())
+ return in_ir;
+
+ op_const[i] = ir->operands[i]->constant_expression_value();
+ op_expr[i] = ir->operands[i]->as_expression();
+ }
+
+ switch (ir->operation) {
+ case ir_unop_logic_not:
+ if (op_expr[0] && op_expr[0]->operation == ir_binop_equal) {
+ this->progress = true;
+ return new(ir) ir_expression(ir_binop_nequal,
+ ir->type,
+ op_expr[0]->operands[0],
+ op_expr[0]->operands[1]);
+ }
+ if (op_expr[0] && op_expr[0]->operation == ir_binop_nequal) {
+ this->progress = true;
+ return new(ir) ir_expression(ir_binop_equal,
+ ir->type,
+ op_expr[0]->operands[0],
+ op_expr[0]->operands[1]);
+ }
+ break;
+
+ case ir_binop_add:
+ if (is_vec_zero(op_const[0])) {
+ this->progress = true;
+ return ir->operands[1];
+ }
+ if (is_vec_zero(op_const[1])) {
+ this->progress = true;
+ return ir->operands[0];
+ }
+ break;
+
+ case ir_binop_sub:
+ if (is_vec_zero(op_const[0])) {
+ this->progress = true;
+ return new(ir) ir_expression(ir_unop_neg,
+ ir->type,
+ ir->operands[1],
+ NULL);
+ }
+ if (is_vec_zero(op_const[1])) {
+ this->progress = true;
+ return ir->operands[0];
+ }
+ break;
+
+ case ir_binop_mul:
+ if (is_vec_one(op_const[0])) {
+ this->progress = true;
+ return ir->operands[1];
+ }
+ if (is_vec_one(op_const[1])) {
+ this->progress = true;
+ return ir->operands[0];
+ }
+
+ if (is_vec_zero(op_const[0]) ||
+ is_vec_zero(op_const[1])) {
+ ir_constant_data zero_data;
+ memset(&zero_data, 0, sizeof(zero_data));
+
+ this->progress = true;
+ return new(ir) ir_constant(ir->type, &zero_data);
+ }
+ break;
+
+ case ir_binop_div:
+ if (is_vec_one(op_const[0]) && ir->type->base_type == GLSL_TYPE_FLOAT) {
+ this->progress = true;
+ return new(ir) ir_expression(ir_unop_rcp,
+ ir->type,
+ ir->operands[1],
+ NULL);
+ }
+ if (is_vec_one(op_const[1])) {
+ this->progress = true;
+ return ir->operands[0];
+ }
+ break;
+
+ case ir_unop_rcp:
+ if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp) {
+ this->progress = true;
+ 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.
+ */
+
+ /* 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;
+ return new(ir) ir_expression(ir_unop_rsq,
+ ir->type,
+ op_expr[0]->operands[0],
+ NULL);
+ }
+
+ break;
+
+ default:
+ break;
+ }
+
+ return in_ir;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_expression *ir)
+{
+ unsigned int operand;
+
+ for (operand = 0; operand < ir->get_num_operands(); operand++) {
+ ir->operands[operand] = handle_expression(ir->operands[operand]);
+ }
+
+ return visit_continue;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_texture *ir)
+{
+ ir->coordinate = handle_expression(ir->coordinate);
+ ir->projector = handle_expression(ir->projector);
+ ir->shadow_comparitor = handle_expression(ir->shadow_comparitor);
+
+ switch (ir->op) {
+ case ir_tex:
+ break;
+ case ir_txb:
+ ir->lod_info.bias = handle_expression(ir->lod_info.bias);
+ break;
+ case ir_txf:
+ case ir_txl:
+ ir->lod_info.lod = handle_expression(ir->lod_info.lod);
+ break;
+ case ir_txd:
+ ir->lod_info.grad.dPdx = handle_expression(ir->lod_info.grad.dPdx);
+ ir->lod_info.grad.dPdy = handle_expression(ir->lod_info.grad.dPdy);
+ break;
+ }
+
+ return visit_continue;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_swizzle *ir)
+{
+ ir->val = handle_expression(ir->val);
+ return visit_continue;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_dereference_array *ir)
+{
+ ir->array_index = handle_expression(ir->array_index);
+ return visit_continue;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_assignment *ir)
+{
+ ir->rhs = handle_expression(ir->rhs);
+ ir->condition = handle_expression(ir->condition);
+ return visit_continue;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_call *ir)
+{
+ foreach_iter(exec_list_iterator, iter, *ir) {
+ ir_rvalue *param = (ir_rvalue *)iter.get();
+ ir_rvalue *new_param = handle_expression(param);
+
+ if (new_param != param) {
+ param->replace_with(new_param);
+ }
+ }
+ return visit_continue;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_return *ir)
+{
+ ir->value = handle_expression(ir->value);;
+ return visit_continue;
+}
+
+ir_visitor_status
+ir_algebraic_visitor::visit_leave(ir_if *ir)
+{
+ ir->condition = handle_expression(ir->condition);
+ return visit_continue;
+}
+
+
+bool
+do_algebraic(exec_list *instructions)
+{
+ ir_algebraic_visitor v;
+
+ visit_list_elements(&v, instructions);
+
+ return v.progress;
+}
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h
index 06cb4d22ca8..4f39565e5f1 100644
--- a/src/glsl/ir_optimization.h
+++ b/src/glsl/ir_optimization.h
@@ -23,11 +23,12 @@
/**
- * \file ir_dead_code.h
+ * \file ir_optimization.h
*
* Prototypes for optimization passes to be called by the compiler and drivers.
*/
+bool do_algebraic(exec_list *instructions);
bool do_constant_folding(exec_list *instructions);
bool do_constant_variable(exec_list *instructions);
bool do_constant_variable_unlinked(exec_list *instructions);
diff --git a/src/glsl/main.cpp b/src/glsl/main.cpp
index 5c0f6475e0b..b62902278c4 100644
--- a/src/glsl/main.cpp
+++ b/src/glsl/main.cpp
@@ -165,6 +165,7 @@ compile_shader(struct gl_shader *shader)
progress = do_dead_code_unlinked(state, shader->ir) || progress;
progress = do_constant_variable_unlinked(shader->ir) || progress;
progress = do_constant_folding(shader->ir) || progress;
+ progress = do_algebraic(shader->ir) || progress;
progress = do_vec_index_to_swizzle(shader->ir) || progress;
progress = do_vec_index_to_cond_assign(shader->ir) || progress;
progress = do_swizzle_swizzle(shader->ir) || progress;
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index d06b83261c6..2fd0507c2fb 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -2240,6 +2240,7 @@ _mesa_glsl_compile_shader(GLcontext *ctx, struct gl_shader *shader)
progress = do_dead_code_unlinked(state, shader->ir) || progress;
progress = do_constant_variable_unlinked(shader->ir) || progress;
progress = do_constant_folding(shader->ir) || progress;
+ progress = do_algebraic(shader->ir) || progress;
progress = do_if_return(shader->ir) || progress;
if (ctx->Shader.EmitNoIfs)
progress = do_if_to_cond_assign(shader->ir) || progress;