radeon: use _mesa_get_current_tex_object() in radeonSetTexBuffer2()
[mesa.git] / src / glsl / opt_array_splitting.cpp
index 1e278c16ae18d09410c887f030f561d1f6a9d7b4..f37d0902201e4a2a49bd250423c5ad22e8d6aca2 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,18 +50,27 @@ 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;
+      if (var->type->is_array())
+        this->size = var->type->length;
+      else
+        this->size = var->type->matrix_columns;
    }
 
    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;
 
@@ -69,6 +79,7 @@ public:
 };
 
 } /* namespace */
+
 using namespace opt_array_splitting;
 
 /**
@@ -94,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);
 
@@ -103,26 +115,28 @@ 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())
+   if (!(var->type->is_array() || var->type->is_matrix()))
       return NULL;
 
    /* If the array hasn't been sized yet, we can't split it.  After
     * linking, this should be resolved.
     */
-   if (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_list(n, &this->variable_list) {
+      variable_entry *entry = (variable_entry *) n;
       if (entry->var == var)
         return entry;
    }
@@ -149,12 +163,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;
 }
@@ -168,9 +183,23 @@ 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;
 }
 
@@ -195,16 +224,16 @@ 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_list_safe(n, &variable_list) {
+      variable_entry *entry = (variable_entry *) n;
 
       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();
       }
    }
@@ -212,7 +241,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)
@@ -231,7 +263,6 @@ public:
    variable_entry *get_splitting_entry(ir_variable *var);
 
    exec_list *variable_list;
-   void *mem_ctx;
 };
 
 variable_entry *
@@ -239,11 +270,8 @@ ir_array_splitting_visitor::get_splitting_entry(ir_variable *var)
 {
    assert(var);
 
-   if (!var->type->is_array())
-      return NULL;
-
-   foreach_iter(exec_list_iterator, iter, *this->variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
+   foreach_list(n, this->variable_list) {
+      variable_entry *entry = (variable_entry *) n;
       if (entry->var == var) {
         return entry;
       }
@@ -271,7 +299,7 @@ ir_array_splitting_visitor::split_deref(ir_dereference **deref)
    ir_constant *constant = deref_array->array_index->as_constant();
    assert(constant);
 
-   if (constant->value.i[0] < (int)var->type->length) {
+   if (constant->value.i[0] < (int)entry->size) {
       *deref = new(entry->mem_ctx)
         ir_dereference_variable(entry->components[constant->value.i[0]]);
    } else {
@@ -340,24 +368,28 @@ 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_list(n, &refs.variable_list) {
+      variable_entry *entry = (variable_entry *) n;
       const struct glsl_type *type = entry->var->type;
+      const struct glsl_type *subtype;
+
+      if (type->is_matrix())
+        subtype = type->column_type();
+      else
+        subtype = type->fields.array;
 
       entry->mem_ctx = ralloc_parent(entry->var);
 
       entry->components = ralloc_array(mem_ctx,
                                       ir_variable *,
-                                      type->length);
+                                      entry->size);
 
-      for (unsigned int i = 0; i < type->length; i++) {
+      for (unsigned int i = 0; i < entry->size; i++) {
         const char *name = ralloc_asprintf(mem_ctx, "%s_%d",
                                            entry->var->name, i);
 
         entry->components[i] =
-           new(entry->mem_ctx) ir_variable(type->fields.array,
-                                           name,
-                                           ir_var_temporary);
+           new(entry->mem_ctx) ir_variable(subtype, name, ir_var_temporary);
         entry->var->insert_before(entry->components[i]);
       }