glsl: move to compiler/
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_vector_splitting.cpp
index 00d5c202485701bf9840843a6a34bca464c9e330..5fe24debeecfa135a355cf14f472a824bbcff212 100644 (file)
  * behavior we want for the results of texture lookups, but probably not for
  */
 
-extern "C" {
-#include "main/core.h"
-#include "intel_context.h"
-}
-#include "../glsl/ir.h"
-#include "../glsl/ir_visitor.h"
-#include "../glsl/ir_print_visitor.h"
-#include "../glsl/ir_rvalue_visitor.h"
-#include "../glsl/glsl_types.h"
+#include "main/imports.h"
+#include "compiler/glsl/ir.h"
+#include "compiler/glsl/ir_rvalue_visitor.h"
+#include "compiler/glsl_types.h"
+#include "util/hash_table.h"
 
 static bool debug = false;
 
@@ -56,7 +52,6 @@ public:
    {
       this->var = var;
       this->whole_vector_access = 0;
-      this->declaration = false;
       this->mem_ctx = NULL;
    }
 
@@ -65,11 +60,9 @@ public:
    /** Number of times the variable is referenced, including assignments. */
    unsigned whole_vector_access;
 
-   bool declaration; /* If the variable had a decl in the instruction stream */
-
    ir_variable *components[4];
 
-   /** talloc_parent(this->var) -- the shader's talloc context. */
+   /** ralloc_parent(this->var) -- the shader's ralloc context. */
    void *mem_ctx;
 };
 
@@ -77,13 +70,14 @@ class ir_vector_reference_visitor : public ir_hierarchical_visitor {
 public:
    ir_vector_reference_visitor(void)
    {
-      this->mem_ctx = talloc_new(NULL);
-      this->variable_list.make_empty();
+      this->mem_ctx = ralloc_context(NULL);
+      this->ht = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
+                                         _mesa_key_pointer_equal);
    }
 
    ~ir_vector_reference_visitor(void)
    {
-      talloc_free(mem_ctx);
+      ralloc_free(mem_ctx);
    }
 
    virtual ir_visitor_status visit(ir_variable *);
@@ -95,7 +89,7 @@ public:
    variable_entry *get_variable_entry(ir_variable *var);
 
    /* List of variable_entry */
-   exec_list variable_list;
+   struct hash_table *ht;
 
    void *mem_ctx;
 };
@@ -108,13 +102,18 @@ ir_vector_reference_visitor::get_variable_entry(ir_variable *var)
    if (!var->type->is_vector())
       return NULL;
 
-   switch (var->mode) {
+   switch (var->data.mode) {
    case ir_var_uniform:
-   case ir_var_in:
-   case ir_var_out:
-   case ir_var_inout:
+   case ir_var_shader_storage:
+   case ir_var_shader_shared:
+   case ir_var_shader_in:
+   case ir_var_shader_out:
+   case ir_var_system_value:
+   case ir_var_function_in:
+   case ir_var_function_out:
+   case ir_var_function_inout:
       /* Can't split varyings or uniforms.  Function in/outs won't get split
-       * either, so don't care about the ambiguity.
+       * either.
        */
       return NULL;
    case ir_var_auto:
@@ -122,14 +121,12 @@ ir_vector_reference_visitor::get_variable_entry(ir_variable *var)
       break;
    }
 
-   foreach_iter(exec_list_iterator, iter, this->variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
-      if (entry->var == var)
-        return entry;
-   }
+   struct hash_entry *hte = _mesa_hash_table_search(ht, var);
+   if (hte)
+      return (struct variable_entry *) hte->data;
 
    variable_entry *entry = new(mem_ctx) variable_entry(var);
-   this->variable_list.push_tail(entry);
+   _mesa_hash_table_insert(ht, var, entry);
    return entry;
 }
 
@@ -137,10 +134,8 @@ ir_vector_reference_visitor::get_variable_entry(ir_variable *var)
 ir_visitor_status
 ir_vector_reference_visitor::visit(ir_variable *ir)
 {
-   variable_entry *entry = this->get_variable_entry(ir);
-
-   if (entry)
-      entry->declaration = true;
+   /* Make sure splitting looks at splitting this variable */
+   (void)this->get_variable_entry(ir);
 
    return visit_continue;
 }
@@ -179,7 +174,7 @@ ir_vector_reference_visitor::visit_enter(ir_assignment *ir)
       return visit_continue_with_parent;
    }
    if (ir->lhs->as_dereference_variable() &&
-       is_power_of_two(ir->write_mask) &&
+       _mesa_is_pow_two(ir->write_mask) &&
        !ir->condition) {
       /* If we're writing just a channel, then channel-splitting the LHS is OK.
        */
@@ -201,21 +196,20 @@ ir_vector_reference_visitor::visit_enter(ir_function_signature *ir)
 
 class ir_vector_splitting_visitor : public ir_rvalue_visitor {
 public:
-   ir_vector_splitting_visitor(exec_list *vars)
+   ir_vector_splitting_visitor(struct hash_table *vars)
    {
-      this->variable_list = vars;
+      this->ht = vars;
    }
 
    virtual ir_visitor_status visit_leave(ir_assignment *);
 
    void handle_rvalue(ir_rvalue **rvalue);
-   struct variable_entry *get_splitting_entry(ir_variable *var);
+   variable_entry *get_splitting_entry(ir_variable *var);
 
-   exec_list *variable_list;
-   void *mem_ctx;
+   struct hash_table *ht;
 };
 
-struct variable_entry *
+variable_entry *
 ir_vector_splitting_visitor::get_splitting_entry(ir_variable *var)
 {
    assert(var);
@@ -223,14 +217,8 @@ ir_vector_splitting_visitor::get_splitting_entry(ir_variable *var)
    if (!var->type->is_vector())
       return NULL;
 
-   foreach_iter(exec_list_iterator, iter, *this->variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
-      if (entry->var == var) {
-        return entry;
-      }
-   }
-
-   return NULL;
+   struct hash_entry *hte = _mesa_hash_table_search(ht, var);
+   return hte ? (struct variable_entry *) hte->data : NULL;
 }
 
 void
@@ -264,37 +252,43 @@ ir_vector_splitting_visitor::visit_leave(ir_assignment *ir)
    variable_entry *rhs = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
 
    if (lhs_deref && rhs_deref && (lhs || rhs) && !ir->condition) {
+      unsigned int rhs_chan = 0;
+
       /* Straight assignment of vector variables. */
-      for (unsigned int i = 0; i < ir->rhs->type->vector_elements; i++) {
+      for (unsigned int i = 0; i < ir->lhs->type->vector_elements; i++) {
         ir_dereference *new_lhs;
         ir_rvalue *new_rhs;
         void *mem_ctx = lhs ? lhs->mem_ctx : rhs->mem_ctx;
         unsigned int writemask;
 
+        if (!(ir->write_mask & (1 << i)))
+           continue;
+
         if (lhs) {
            new_lhs = new(mem_ctx) ir_dereference_variable(lhs->components[i]);
-           writemask = (ir->write_mask >> i) & 1;
+           writemask = 1;
         } else {
            new_lhs = ir->lhs->clone(mem_ctx, NULL);
-           writemask = ir->write_mask & (1 << i);
+           writemask = 1 << i;
         }
 
         if (rhs) {
-           new_rhs = new(mem_ctx) ir_dereference_variable(rhs->components[i]);
-           /* If we're writing into a writemask, smear it out to that channel. */
-           if (!lhs)
-              new_rhs = new(mem_ctx) ir_swizzle(new_rhs, i, i, i, i, i + 1);
+           new_rhs =
+              new(mem_ctx) ir_dereference_variable(rhs->components[rhs_chan]);
         } else {
            new_rhs = new(mem_ctx) ir_swizzle(ir->rhs->clone(mem_ctx, NULL),
-                                             i, i, i, i, 1);
+                                             rhs_chan, 0, 0, 0, 1);
         }
 
         ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
                                                      new_rhs,
                                                      NULL, writemask));
+
+        rhs_chan++;
       }
       ir->remove();
    } else if (lhs) {
+      void *mem_ctx = lhs->mem_ctx;
       int elem = -1;
 
       switch (ir->write_mask) {
@@ -311,16 +305,14 @@ ir_vector_splitting_visitor::visit_leave(ir_assignment *ir)
         elem = 3;
         break;
       default:
-        ir->print();
-        assert(!"not reached: non-channelwise dereference of LHS.");
+        ir->fprint(stderr);
+        unreachable("not reached: non-channelwise dereference of LHS.");
       }
 
       ir->lhs = new(mem_ctx) ir_dereference_variable(lhs->components[elem]);
       ir->write_mask = (1 << 0);
 
       handle_rvalue(&ir->rhs);
-      ir->rhs = new(mem_ctx) ir_swizzle(ir->rhs,
-                                       elem, elem, elem, elem, 1);
    } else {
       handle_rvalue(&ir->rhs);
    }
@@ -330,62 +322,66 @@ ir_vector_splitting_visitor::visit_leave(ir_assignment *ir)
    return visit_continue;
 }
 
-extern "C" {
 bool
 brw_do_vector_splitting(exec_list *instructions)
 {
+   struct hash_entry *hte;
+
    ir_vector_reference_visitor refs;
 
    visit_list_elements(&refs, instructions);
 
    /* Trim out variables we can't split. */
-   foreach_iter(exec_list_iterator, iter, refs.variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
-
+   hash_table_foreach(refs.ht, hte) {
+      struct variable_entry *entry = (struct variable_entry *) hte->data;
       if (debug) {
-        printf("vector %s@%p: decl %d, whole_access %d\n",
-               entry->var->name, (void *) entry->var, entry->declaration,
-               entry->whole_vector_access);
+        fprintf(stderr, "vector %s@%p: whole_access %d\n",
+                 entry->var->name, (void *) entry->var,
+                 entry->whole_vector_access);
       }
 
-      if (!entry->declaration || entry->whole_vector_access) {
-        entry->remove();
+      if (entry->whole_vector_access) {
+         _mesa_hash_table_remove(refs.ht, hte);
       }
    }
 
-   if (refs.variable_list.is_empty())
+   if (refs.ht->entries == 0)
       return false;
 
-   void *mem_ctx = talloc_new(NULL);
+   void *mem_ctx = ralloc_context(NULL);
 
    /* Replace the decls of the vectors to be split with their split
     * components.
     */
-   foreach_iter(exec_list_iterator, iter, refs.variable_list) {
-      variable_entry *entry = (variable_entry *)iter.get();
+   hash_table_foreach(refs.ht, hte) {
+      struct variable_entry *entry = (struct variable_entry *) hte->data;
       const struct glsl_type *type;
       type = glsl_type::get_instance(entry->var->type->base_type, 1, 1);
 
-      entry->mem_ctx = talloc_parent(entry->var);
+      entry->mem_ctx = ralloc_parent(entry->var);
 
       for (unsigned int i = 0; i < entry->var->type->vector_elements; i++) {
-        const char *name = talloc_asprintf(mem_ctx, "%s_%c",
-                                           entry->var->name,
-                                           "xyzw"[i]);
+         char *const name = ir_variable::temporaries_allocate_names
+            ? ralloc_asprintf(mem_ctx, "%s_%c",
+                              entry->var->name,
+                              "xyzw"[i])
+            : NULL;
 
         entry->components[i] = new(entry->mem_ctx) ir_variable(type, name,
                                                                ir_var_temporary);
+
+         ralloc_free(name);
+
         entry->var->insert_before(entry->components[i]);
       }
 
       entry->var->remove();
    }
 
-   ir_vector_splitting_visitor split(&refs.variable_list);
+   ir_vector_splitting_visitor split(refs.ht);
    visit_list_elements(&split, instructions);
 
-   talloc_free(mem_ctx);
+   ralloc_free(mem_ctx);
 
    return true;
 }
-}