glsl: Convert piles of foreach_iter to foreach_list_safe.
[mesa.git] / src / glsl / opt_structure_splitting.cpp
index ff3ec936eb8b93849c10b08ee5d185e4be8ba392..1ec537b132bd28d1eff9a2fea85d1341ee505bb6 100644 (file)
@@ -22,7 +22,7 @@
  */
 
 /**
- * \file ir_structure_splitting.cpp
+ * \file opt_structure_splitting.cpp
  *
  * If a structure is only ever referenced by its components, then
  * split those components out to individual variables so they can be
 
 #include "ir.h"
 #include "ir_visitor.h"
-#include "ir_print_visitor.h"
 #include "ir_rvalue_visitor.h"
 #include "glsl_types.h"
 
+namespace {
+
 static bool debug = false;
 
-// XXX using variable_entry2 here to avoid collision (MSVC multiply-defined
-// function) with the variable_entry class seen in ir_variable_refcount.h
-// Perhaps we can use the one in ir_variable_refcount.h and make this class
-// here go away?
-class variable_entry2 : public exec_node
+class variable_entry : public exec_node
 {
 public:
-   variable_entry2(ir_variable *var)
+   variable_entry(ir_variable *var)
    {
       this->var = var;
       this->whole_structure_access = 0;
@@ -61,11 +58,15 @@ public:
    /** Number of times the variable is referenced, including assignments. */
    unsigned whole_structure_access;
 
-   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;
 
-   /** talloc_parent(this->var) -- the shader's talloc context. */
+   /** ralloc_parent(this->var) -- the shader's ralloc context. */
    void *mem_ctx;
 };
 
@@ -74,13 +75,13 @@ class ir_structure_reference_visitor : public ir_hierarchical_visitor {
 public:
    ir_structure_reference_visitor(void)
    {
-      this->mem_ctx = talloc_new(NULL);
+      this->mem_ctx = ralloc_context(NULL);
       this->variable_list.make_empty();
    }
 
    ~ir_structure_reference_visitor(void)
    {
-      talloc_free(mem_ctx);
+      ralloc_free(mem_ctx);
    }
 
    virtual ir_visitor_status visit(ir_variable *);
@@ -89,7 +90,7 @@ public:
    virtual ir_visitor_status visit_enter(ir_assignment *);
    virtual ir_visitor_status visit_enter(ir_function_signature *);
 
-   variable_entry2 *get_variable_entry2(ir_variable *var);
+   variable_entry *get_variable_entry(ir_variable *var);
 
    /* List of variable_entry */
    exec_list variable_list;
@@ -97,21 +98,22 @@ public:
    void *mem_ctx;
 };
 
-variable_entry2 *
-ir_structure_reference_visitor::get_variable_entry2(ir_variable *var)
+variable_entry *
+ir_structure_reference_visitor::get_variable_entry(ir_variable *var)
 {
    assert(var);
 
-   if (!var->type->is_record() || var->mode == ir_var_uniform)
+   if (!var->type->is_record() || var->data.mode == ir_var_uniform
+       || var->data.mode == ir_var_shader_in || var->data.mode == ir_var_shader_out)
       return NULL;
 
-   foreach_iter(exec_list_iterator, iter, this->variable_list) {
-      variable_entry2 *entry = (variable_entry2 *)iter.get();
+   foreach_list(n, &this->variable_list) {
+      variable_entry *entry = (variable_entry *) n;
       if (entry->var == var)
         return entry;
    }
 
-   variable_entry2 *entry = new(mem_ctx) variable_entry2(var);
+   variable_entry *entry = new(mem_ctx) variable_entry(var);
    this->variable_list.push_tail(entry);
    return entry;
 }
@@ -120,7 +122,7 @@ ir_structure_reference_visitor::get_variable_entry2(ir_variable *var)
 ir_visitor_status
 ir_structure_reference_visitor::visit(ir_variable *ir)
 {
-   variable_entry2 *entry = this->get_variable_entry2(ir);
+   variable_entry *entry = this->get_variable_entry(ir);
 
    if (entry)
       entry->declaration = true;
@@ -132,7 +134,7 @@ ir_visitor_status
 ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
 {
    ir_variable *const var = ir->variable_referenced();
-   variable_entry2 *entry = this->get_variable_entry2(var);
+   variable_entry *entry = this->get_variable_entry(var);
 
    if (entry)
       entry->whole_structure_access++;
@@ -151,6 +153,12 @@ ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
 ir_visitor_status
 ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
 {
+   /* If there are no structure references yet, no need to bother with
+    * processing the expression tree.
+    */
+   if (this->variable_list.is_empty())
+      return visit_continue_with_parent;
+
    if (ir->lhs->as_dereference_variable() &&
        ir->rhs->as_dereference_variable() &&
        !ir->condition) {
@@ -165,8 +173,9 @@ ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
 ir_visitor_status
 ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
 {
-   /* We don't want to descend into the function parameters and
-    * dead-code eliminate them, so just accept the body here.
+   /* We don't have logic for structure-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;
@@ -187,13 +196,12 @@ public:
 
    void split_deref(ir_dereference **deref);
    void handle_rvalue(ir_rvalue **rvalue);
-   variable_entry2 *get_splitting_entry(ir_variable *var);
+   variable_entry *get_splitting_entry(ir_variable *var);
 
    exec_list *variable_list;
-   void *mem_ctx;
 };
 
-variable_entry2 *
+variable_entry *
 ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
 {
    assert(var);
@@ -201,8 +209,8 @@ ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
    if (!var->type->is_record())
       return NULL;
 
-   foreach_iter(exec_list_iterator, iter, *this->variable_list) {
-      variable_entry2 *entry = (variable_entry2 *)iter.get();
+   foreach_list(n, this->variable_list) {
+      variable_entry *entry = (variable_entry *) n;
       if (entry->var == var) {
         return entry;
       }
@@ -222,7 +230,7 @@ ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
    if (!deref_var)
       return;
 
-   variable_entry2 *entry = get_splitting_entry(deref_var->var);
+   variable_entry *entry = get_splitting_entry(deref_var->var);
    if (!entry)
       return;
 
@@ -257,8 +265,8 @@ ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
 {
    ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
    ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
-   variable_entry2 *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
-   variable_entry2 *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
+   variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
+   variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
    const glsl_type *type = ir->rhs->type;
 
    if ((lhs_entry || rhs_entry) && !ir->condition) {
@@ -297,6 +305,8 @@ ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
    return visit_continue;
 }
 
+} /* unnamed namespace */
+
 bool
 do_structure_splitting(exec_list *instructions)
 {
@@ -305,8 +315,8 @@ do_structure_splitting(exec_list *instructions)
    visit_list_elements(&refs, instructions);
 
    /* Trim out variables we can't split. */
-   foreach_iter(exec_list_iterator, iter, refs.variable_list) {
-      variable_entry2 *entry = (variable_entry2 *)iter.get();
+   foreach_list_safe(n, &refs.variable_list) {
+      variable_entry *entry = (variable_entry *) n;
 
       if (debug) {
         printf("structure %s@%p: decl %d, whole_access %d\n",
@@ -322,23 +332,23 @@ do_structure_splitting(exec_list *instructions)
    if (refs.variable_list.is_empty())
       return false;
 
-   void *mem_ctx = talloc_new(NULL);
+   void *mem_ctx = ralloc_context(NULL);
 
    /* Replace the decls of the structures to be split with their split
     * components.
     */
-   foreach_iter(exec_list_iterator, iter, refs.variable_list) {
-      variable_entry2 *entry = (variable_entry2 *)iter.get();
+   foreach_list_safe(n, &refs.variable_list) {
+      variable_entry *entry = (variable_entry *) n;
       const struct glsl_type *type = entry->var->type;
 
-      entry->mem_ctx = talloc_parent(entry->var);
+      entry->mem_ctx = ralloc_parent(entry->var);
 
-      entry->components = talloc_array(mem_ctx,
+      entry->components = ralloc_array(mem_ctx,
                                       ir_variable *,
                                       type->length);
 
       for (unsigned int i = 0; i < entry->var->type->length; i++) {
-        const char *name = talloc_asprintf(mem_ctx, "%s_%s",
+        const char *name = ralloc_asprintf(mem_ctx, "%s_%s",
                                            entry->var->name,
                                            type->fields.structure[i].name);
 
@@ -355,7 +365,7 @@ do_structure_splitting(exec_list *instructions)
    ir_structure_splitting_visitor split(&refs.variable_list);
    visit_list_elements(&split, instructions);
 
-   talloc_free(mem_ctx);
+   ralloc_free(mem_ctx);
 
    return true;
 }