summaryrefslogtreecommitdiff
path: root/src/glsl
diff options
context:
space:
mode:
authorEric Anholt <eric@anholt.net>2010-07-06 18:09:39 -0700
committerEric Anholt <eric@anholt.net>2010-07-07 09:10:48 -0700
commitd674ebcee0d2731e50d6530502cefcebc39dcdb6 (patch)
tree74feecda4fdc277c972e01267289a66cd45415f6 /src/glsl
parent6de882334ac7f3d32d04261adfed1397e075ffd5 (diff)
glsl2: Add a pass to simplify if statements returning from both sides.
This allows function inlining making the following tests work even without function calls implemented: glsl-fs-functions-2 glsl-fs-functions-3 glsl-vs-functions glsl-vs-functions-2 glsl-vs-functions-3 glsl-vs-vec4-indexing-5 (Note that those tests were designed to trigger actual function calls, and this defeats them. However, those testcases ended up catching the bug in the previous commit.)
Diffstat (limited to 'src/glsl')
-rw-r--r--src/glsl/Makefile1
-rw-r--r--src/glsl/ir_if_return.cpp123
-rw-r--r--src/glsl/ir_optimization.h1
3 files changed, 125 insertions, 0 deletions
diff --git a/src/glsl/Makefile b/src/glsl/Makefile
index d2a687aa33..ddc9d82d61 100644
--- a/src/glsl/Makefile
+++ b/src/glsl/Makefile
@@ -40,6 +40,7 @@ CXX_SOURCES = \
ir_function_inlining.cpp \
ir_hierarchical_visitor.cpp \
ir_hv_accept.cpp \
+ ir_if_return.cpp \
ir_if_simplification.cpp \
ir_mod_to_fract.cpp \
ir_print_visitor.cpp \
diff --git a/src/glsl/ir_if_return.cpp b/src/glsl/ir_if_return.cpp
new file mode 100644
index 0000000000..f68dcfb501
--- /dev/null
+++ b/src/glsl/ir_if_return.cpp
@@ -0,0 +1,123 @@
+/*
+ * 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_if_return.cpp
+ *
+ * If a function includes an if statement that returns from both
+ * branches, then make the branches write the return val to a temp and
+ * return the temp after the if statement.
+ *
+ * This allows inlinining in the common case of short functions that
+ * return one of two values based on a condition. This helps on
+ * hardware with no branching support, and may even be a useful
+ * transform on hardware supporting control flow by masked returns
+ * with normal returns.
+ */
+
+#include "ir.h"
+
+class ir_if_return_visitor : public ir_hierarchical_visitor {
+public:
+ ir_if_return_visitor()
+ {
+ this->progress = false;
+ }
+
+ ir_visitor_status visit_enter(ir_if *);
+
+ bool progress;
+};
+
+bool
+do_if_return(exec_list *instructions)
+{
+ ir_if_return_visitor v;
+
+ visit_list_elements(&v, instructions);
+
+ return v.progress;
+}
+
+
+ir_visitor_status
+ir_if_return_visitor::visit_enter(ir_if *ir)
+{
+ ir_return *then_return = NULL;
+ ir_return *else_return = NULL;
+
+ /* Try to find a return statement on both sides. */
+ foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
+ ir_instruction *then_ir = (ir_instruction *)then_iter.get();
+ then_return = then_ir->as_return();
+ if (then_return)
+ break;
+ }
+ if (!then_return)
+ return visit_continue;
+
+ foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
+ ir_instruction *else_ir = (ir_instruction *)else_iter.get();
+ else_return = else_ir->as_return();
+ if (else_return)
+ break;
+ }
+ if (!else_return)
+ return visit_continue;
+
+ /* Trim off any trailing instructions after the return statements
+ * on both sides.
+ */
+ while (then_return->get_next()->get_next())
+ ((ir_instruction *)then_return->get_next())->remove();
+ while (else_return->get_next()->get_next())
+ ((ir_instruction *)else_return->get_next())->remove();
+
+ this->progress = true;
+
+ if (!then_return->value) {
+ then_return->remove();
+ else_return->remove();
+ ir->insert_after(new(ir) ir_return(NULL));
+ } else {
+ ir_assignment *assign;
+ ir_variable *new_var = new(ir) ir_variable(then_return->value->type,
+ "if_return_tmp");
+ ir->insert_before(new_var);
+
+ assign = new(ir) ir_assignment(new(ir) ir_dereference_variable(new_var),
+ then_return->value, NULL);
+ then_return->insert_before(assign);
+ then_return->remove();
+
+ assign = new(ir) ir_assignment(new(ir) ir_dereference_variable(new_var),
+ else_return->value, NULL);
+ else_return->insert_before(assign);
+ else_return->remove();
+
+ ir_dereference_variable *deref = new(ir) ir_dereference_variable(new_var);
+ ir->insert_after(new(ir) ir_return(deref));
+ }
+
+ return visit_continue;
+}
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h
index 93010dadbe..b03c0644cf 100644
--- a/src/glsl/ir_optimization.h
+++ b/src/glsl/ir_optimization.h
@@ -39,6 +39,7 @@ bool do_dead_code_unlinked(struct _mesa_glsl_parse_state *state,
exec_list *instructions);
bool do_div_to_mul_rcp(exec_list *instructions);
bool do_function_inlining(exec_list *instructions);
+bool do_if_return(exec_list *instructions);
bool do_if_simplification(exec_list *instructions);
bool do_mod_to_fract(exec_list *instructions);
bool do_swizzle_swizzle(exec_list *instructions);