glsl: Use typed foreach_in_list_safe instead of foreach_list_safe.
[mesa.git] / src / glsl / opt_array_splitting.cpp
index b9c70dc9006d632ff7cfec453ee7903b3059f4ab..ebb076b223ebe1d1cfd84a535200582760ae78de 100644 (file)
 #include "ir.h"
 #include "ir_visitor.h"
 #include "ir_rvalue_visitor.h"
-#include "ir_print_visitor.h"
 #include "glsl_types.h"
 
 static bool debug = false;
 
+namespace {
+
 namespace opt_array_splitting {
 
 class variable_entry : public exec_node
@@ -49,7 +50,7 @@ public:
    variable_entry(ir_variable *var)
    {
       this->var = var;
-      this->whole_array_access = 0;
+      this->split = true;
       this->declaration = false;
       this->components = NULL;
       this->mem_ctx = NULL;
@@ -62,10 +63,14 @@ public:
    ir_variable *var; /* The key: the variable's pointer. */
    unsigned size; /* array length or matrix columns */
 
-   /** Number of times the variable is referenced, including assignments. */
-   unsigned whole_array_access;
+   /** Whether this array should be split or not. */
+   bool split;
 
-   bool declaration; /* If the variable had a decl in the instruction stream */
+   /* If the variable had a decl we can work with in the instruction
+    * stream.  We can't do splitting on function arguments, which
+    * don't get this variable set.
+    */
+   bool declaration;
 
    ir_variable **components;
 
@@ -74,6 +79,7 @@ public:
 };
 
 } /* namespace */
+
 using namespace opt_array_splitting;
 
 /**
@@ -99,6 +105,7 @@ public:
    virtual ir_visitor_status visit(ir_variable *);
    virtual ir_visitor_status visit(ir_dereference_variable *);
    virtual ir_visitor_status visit_enter(ir_dereference_array *);
+   virtual ir_visitor_status visit_enter(ir_function_signature *);
 
    variable_entry *get_variable_entry(ir_variable *var);
 
@@ -108,13 +115,15 @@ public:
    void *mem_ctx;
 };
 
+} /* namespace */
+
 variable_entry *
 ir_array_reference_visitor::get_variable_entry(ir_variable *var)
 {
    assert(var);
 
-   if (var->mode != ir_var_auto &&
-       var->mode != ir_var_temporary)
+   if (var->data.mode != ir_var_auto &&
+       var->data.mode != ir_var_temporary)
       return NULL;
 
    if (!(var->type->is_array() || var->type->is_matrix()))
@@ -123,11 +132,10 @@ ir_array_reference_visitor::get_variable_entry(ir_variable *var)
    /* If the array hasn't been sized yet, we can't split it.  After
     * linking, this should be resolved.
     */
-   if (var->type->is_array() && var->type->length == 0)
+   if (var->type->is_unsized_array())
       return NULL;
 
-   foreach_iter(exec_list_iterator, iter, this->variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
+   foreach_in_list(variable_entry, entry, &this->variable_list) {
       if (entry->var == var)
         return entry;
    }
@@ -154,12 +162,13 @@ ir_array_reference_visitor::visit(ir_dereference_variable *ir)
 {
    variable_entry *entry = this->get_variable_entry(ir->var);
 
-   /* If we made it to here, then the dereference of this array didn't
-    * have a constant index (see the visit_continue_with_parent
-    * below), so we can't split the variable.
+   /* If we made it to here without seeing an ir_dereference_array,
+    * then the dereference of this array didn't have a constant index
+    * (see the visit_continue_with_parent below), so we can't split
+    * the variable.
     */
    if (entry)
-      entry->whole_array_access++;
+      entry->split = false;
 
    return visit_continue;
 }
@@ -173,12 +182,26 @@ ir_array_reference_visitor::visit_enter(ir_dereference_array *ir)
 
    variable_entry *entry = this->get_variable_entry(deref->var);
 
+   /* If the access to the array has a variable index, we wouldn't
+    * know which split variable this dereference should go to.
+    */
    if (entry && !ir->array_index->as_constant())
-      entry->whole_array_access++;
+      entry->split = false;
 
    return visit_continue_with_parent;
 }
 
+ir_visitor_status
+ir_array_reference_visitor::visit_enter(ir_function_signature *ir)
+{
+   /* We don't have logic for array-splitting function arguments,
+    * so just look at the body instructions and not the parameter
+    * declarations.
+    */
+   visit_list_elements(this, &ir->body);
+   return visit_continue_with_parent;
+}
+
 bool
 ir_array_reference_visitor::get_split_list(exec_list *instructions,
                                           bool linked)
@@ -189,8 +212,8 @@ ir_array_reference_visitor::get_split_list(exec_list *instructions,
     * declarations, which need to be matched by name across shaders.
     */
    if (!linked) {
-      foreach_list(node, instructions) {
-        ir_variable *var = ((ir_instruction *)node)->as_variable();
+      foreach_in_list(ir_instruction, node, instructions) {
+        ir_variable *var = node->as_variable();
         if (var) {
            variable_entry *entry = get_variable_entry(var);
            if (entry)
@@ -200,16 +223,14 @@ ir_array_reference_visitor::get_split_list(exec_list *instructions,
    }
 
    /* Trim out variables we found that we can't split. */
-   foreach_iter(exec_list_iterator, iter, variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
-
+   foreach_in_list_safe(variable_entry, entry, &variable_list) {
       if (debug) {
-        printf("array %s@%p: decl %d, whole_access %d\n",
+        printf("array %s@%p: decl %d, split %d\n",
                entry->var->name, (void *) entry->var, entry->declaration,
-               entry->whole_array_access);
+               entry->split);
       }
 
-      if (!entry->declaration || entry->whole_array_access) {
+      if (!(entry->declaration && entry->split)) {
         entry->remove();
       }
    }
@@ -217,7 +238,10 @@ ir_array_reference_visitor::get_split_list(exec_list *instructions,
    return !variable_list.is_empty();
 }
 
-/** This is the class that does the actual work of splitting. */
+/**
+ * This class rewrites the dereferences of arrays that have been split
+ * to use the newly created ir_variables for each component.
+ */
 class ir_array_splitting_visitor : public ir_rvalue_visitor {
 public:
    ir_array_splitting_visitor(exec_list *vars)
@@ -243,8 +267,7 @@ ir_array_splitting_visitor::get_splitting_entry(ir_variable *var)
 {
    assert(var);
 
-   foreach_iter(exec_list_iterator, iter, *this->variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
+   foreach_in_list(variable_entry, entry, this->variable_list) {
       if (entry->var == var) {
         return entry;
       }
@@ -341,8 +364,7 @@ optimize_split_arrays(exec_list *instructions, bool linked)
    /* Replace the decls of the arrays to be split with their split
     * components.
     */
-   foreach_iter(exec_list_iterator, iter, refs.variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
+   foreach_in_list(variable_entry, entry, &refs.variable_list) {
       const struct glsl_type *type = entry->var->type;
       const struct glsl_type *subtype;
 
@@ -373,7 +395,7 @@ optimize_split_arrays(exec_list *instructions, bool linked)
    visit_list_elements(&split, instructions);
 
    if (debug)
-      _mesa_print_ir(instructions, NULL);
+      _mesa_print_ir(stdout, instructions, NULL);
 
    ralloc_free(mem_ctx);