glsl2: Add a pass to simplify if statements returning from both sides.
authorEric Anholt <eric@anholt.net>
Wed, 7 Jul 2010 01:09:39 +0000 (18:09 -0700)
committerEric Anholt <eric@anholt.net>
Wed, 7 Jul 2010 16:10:48 +0000 (09:10 -0700)
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.)

src/glsl/Makefile
src/glsl/ir_if_return.cpp [new file with mode: 0644]
src/glsl/ir_optimization.h
src/mesa/shader/ir_to_mesa.cpp

index d2a687aa335d71fc7455930ca33e0a0edc0fea92..ddc9d82d61fb5e8cfea05521e327efde7a99adee 100644 (file)
@@ -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 (file)
index 0000000..f68dcfb
--- /dev/null
@@ -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;
+}
index 93010dadbeebc2643b6bf740c9e5484e88828dfa..b03c0644cf0ac371457b092569671141e1559e87 100644 (file)
@@ -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);
index daf09e9e652de7baba7855ae7f5e7da409949e1a..2ffff60065d87956a6afa925da491a1e007d3763 100644 (file)
@@ -1821,6 +1821,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_if_return(shader->ir) || progress;
 
         progress = do_vec_index_to_swizzle(shader->ir) || progress;
         /* Do this one after the previous to let the easier pass handle