aboutsummaryrefslogtreecommitdiffstats
path: root/ir_if_simplification.cpp
diff options
context:
space:
mode:
authorEric Anholt <[email protected]>2010-04-14 17:03:03 -0700
committerEric Anholt <[email protected]>2010-04-14 17:05:13 -0700
commit5ba94206083fcd678febd6cac0231f35c0f1b77a (patch)
tree79311ea6e69d971c8a8c34a7624da4a9f203b2c1 /ir_if_simplification.cpp
parent60be7626b829af7e1d07330b9a88468924ba350e (diff)
Add an ir_if simplification pass.
This is relatively simple at the moment, recognizing only constant values, and not (for example) values that are restricted to a range that make the branching constant. However, it does remove 59 lines from the printout of CorrectParse2.vert.
Diffstat (limited to 'ir_if_simplification.cpp')
-rw-r--r--ir_if_simplification.cpp235
1 files changed, 235 insertions, 0 deletions
diff --git a/ir_if_simplification.cpp b/ir_if_simplification.cpp
new file mode 100644
index 00000000000..5637db510ce
--- /dev/null
+++ b/ir_if_simplification.cpp
@@ -0,0 +1,235 @@
+/*
+ * 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_function_inlining.cpp
+ *
+ * Moves constant branches of if statements out to the surrounding
+ * instruction stream.
+ */
+
+#define NULL 0
+#include "ir.h"
+#include "ir_visitor.h"
+#include "ir_function_inlining.h"
+#include "glsl_types.h"
+
+class ir_if_simplification_visitor : public ir_visitor {
+public:
+ ir_if_simplification_visitor()
+ {
+ /* empty */
+ }
+
+ virtual ~ir_if_simplification_visitor()
+ {
+ /* empty */
+ }
+
+ /**
+ * \name Visit methods
+ *
+ * As typical for the visitor pattern, there must be one \c visit method for
+ * each concrete subclass of \c ir_instruction. Virtual base classes within
+ * the hierarchy should not have \c visit methods.
+ */
+ /*@{*/
+ virtual void visit(ir_variable *);
+ virtual void visit(ir_label *);
+ virtual void visit(ir_loop *);
+ virtual void visit(ir_loop_jump *);
+ virtual void visit(ir_function_signature *);
+ virtual void visit(ir_function *);
+ virtual void visit(ir_expression *);
+ virtual void visit(ir_swizzle *);
+ virtual void visit(ir_dereference *);
+ virtual void visit(ir_assignment *);
+ virtual void visit(ir_constant *);
+ virtual void visit(ir_call *);
+ virtual void visit(ir_return *);
+ virtual void visit(ir_if *);
+ /*@}*/
+};
+
+bool
+do_if_simplification(exec_list *instructions)
+{
+ bool progress = false;
+
+ foreach_iter(exec_list_iterator, iter, *instructions) {
+ ir_instruction *ir = (ir_instruction *)iter.get();
+ ir_if *conditional = ir->as_if();
+
+ if (conditional) {
+ ir_constant *condition_constant;
+
+ condition_constant =
+ conditional->condition->constant_expression_value();
+ if (condition_constant) {
+ /* Move the contents of the one branch of the conditional
+ * that matters out.
+ */
+ if (condition_constant->value.b[0]) {
+ foreach_iter(exec_list_iterator, then_iter,
+ conditional->then_instructions) {
+ ir_instruction *then_ir = (ir_instruction *)then_iter.get();
+ ir->insert_before(then_ir);
+ }
+ } else {
+ foreach_iter(exec_list_iterator, else_iter,
+ conditional->else_instructions) {
+ ir_instruction *else_ir = (ir_instruction *)else_iter.get();
+ ir->insert_before(else_ir);
+ }
+ }
+ ir->remove();
+ progress = true;
+ /* It would be nice to move the iterator back up to the point
+ * that we just spliced in contents.
+ */
+ } else {
+ ir_if_simplification_visitor v;
+ ir->accept(&v);
+ }
+ } else {
+ ir_if_simplification_visitor v;
+ ir->accept(&v);
+ }
+ }
+
+ return progress;
+}
+
+class variable_remap : public exec_node {
+public:
+ variable_remap(const ir_variable *old_var, ir_variable *new_var)
+ : old_var(old_var), new_var(new_var)
+ {
+ /* empty */
+ }
+ const ir_variable *old_var;
+ ir_variable *new_var;
+};
+
+void
+ir_if_simplification_visitor::visit(ir_variable *ir)
+{
+ (void) ir;
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_label *ir)
+{
+ ir->signature->accept(this);
+}
+
+void
+ir_if_simplification_visitor::visit(ir_loop *ir)
+{
+ do_if_simplification(&ir->body_instructions);
+}
+
+void
+ir_if_simplification_visitor::visit(ir_loop_jump *ir)
+{
+ (void) ir;
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_function_signature *ir)
+{
+ do_if_simplification(&ir->body);
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_function *ir)
+{
+ (void) ir;
+}
+
+void
+ir_if_simplification_visitor::visit(ir_expression *ir)
+{
+ unsigned int operand;
+
+ for (operand = 0; operand < ir->get_num_operands(); operand++) {
+ ir->operands[operand]->accept(this);
+ }
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_swizzle *ir)
+{
+ ir->val->accept(this);
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_dereference *ir)
+{
+ if (ir->mode == ir_dereference::ir_reference_array) {
+ ir->selector.array_index->accept(this);
+ }
+ ir->var->accept(this);
+}
+
+void
+ir_if_simplification_visitor::visit(ir_assignment *ir)
+{
+ ir->rhs->accept(this);
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_constant *ir)
+{
+ (void) ir;
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_call *ir)
+{
+ (void) ir;
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_return *ir)
+{
+ (void) ir;
+}
+
+
+void
+ir_if_simplification_visitor::visit(ir_if *ir)
+{
+ ir->condition->accept(this);
+
+ do_if_simplification(&ir->then_instructions);
+ do_if_simplification(&ir->else_instructions);
+}