glsl: separate copy propagation state
[mesa.git] / src / compiler / glsl / opt_copy_propagation_elements.cpp
index a6791801943fab0b39b44df4ad02e8f1eeb74065..63f7e1ff25bead282c2cef6958febdf94f95b959 100644 (file)
 #include "ir_basic_block.h"
 #include "ir_optimization.h"
 #include "compiler/glsl_types.h"
+#include "util/hash_table.h"
 
 static bool debug = false;
 
 namespace {
 
+class acp_entry;
+
+/* Class that refers to acp_entry in another exec_list. Used
+ * when making removals based on rhs.
+ */
+class acp_ref : public exec_node
+{
+public:
+   acp_ref(acp_entry *e)
+   {
+      entry = e;
+   }
+   acp_entry *entry;
+};
+
 class acp_entry : public exec_node
 {
 public:
+   /* override operator new from exec_node */
+   DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(acp_entry)
+
    acp_entry(ir_variable *lhs, ir_variable *rhs, int write_mask, int swizzle[4])
+      : rhs_node(this)
    {
       this->lhs = lhs;
       this->rhs = rhs;
@@ -62,24 +82,134 @@ public:
       memcpy(this->swizzle, swizzle, sizeof(this->swizzle));
    }
 
-   acp_entry(acp_entry *a)
-   {
-      this->lhs = a->lhs;
-      this->rhs = a->rhs;
-      this->write_mask = a->write_mask;
-      memcpy(this->swizzle, a->swizzle, sizeof(this->swizzle));
-   }
-
    ir_variable *lhs;
    ir_variable *rhs;
    unsigned int write_mask;
    int swizzle[4];
+   acp_ref rhs_node;
 };
 
+class copy_propagation_state {
+public:
+   DECLARE_RZALLOC_CXX_OPERATORS(copy_propagation_state);
+
+   copy_propagation_state(void *mem_ctx, void *lin_ctx)
+   {
+      /* Use 'this' as context for the tables, no explicit destruction
+       * needed later.
+       */
+      lhs_ht = _mesa_hash_table_create(this, _mesa_hash_pointer,
+                                       _mesa_key_pointer_equal);
+      rhs_ht = _mesa_hash_table_create(this, _mesa_hash_pointer,
+                                       _mesa_key_pointer_equal);
+      this->mem_ctx = mem_ctx;
+      this->lin_ctx = lin_ctx;
+   }
+
+   copy_propagation_state* clone()
+   {
+      return new (ralloc_parent(this)) copy_propagation_state(this);
+   }
+
+   void erase_all()
+   {
+      _mesa_hash_table_clear(lhs_ht, NULL);
+      _mesa_hash_table_clear(rhs_ht, NULL);
+   }
+
+   void erase(ir_variable *var, unsigned write_mask)
+   {
+      /* removal of lhs entries */
+      hash_entry *ht_entry = _mesa_hash_table_search(lhs_ht, var);
+      if (ht_entry) {
+         exec_list *lhs_list = (exec_list *) ht_entry->data;
+         foreach_in_list_safe(acp_entry, entry, lhs_list) {
+            entry->write_mask = entry->write_mask & ~write_mask;
+            if (entry->write_mask == 0) {
+               entry->remove();
+               continue;
+            }
+         }
+      }
+
+      /* removal of rhs entries */
+      ht_entry = _mesa_hash_table_search(rhs_ht, var);
+      if (ht_entry) {
+         exec_list *rhs_list = (exec_list *) ht_entry->data;
+         acp_ref *ref;
+
+         while ((ref = (acp_ref *) rhs_list->pop_head()) != NULL) {
+            acp_entry *entry = ref->entry;
+
+            /* If entry is still in a list (not already removed by lhs entry
+             * removal above), remove it.
+             */
+            if (entry->prev || entry->next)
+               entry->remove();
+         }
+      }
+   }
+
+   exec_list *read(ir_variable *var)
+   {
+      hash_entry *ht_entry = _mesa_hash_table_search(lhs_ht, var);
+      if (ht_entry)
+         return (exec_list *) ht_entry->data;
+      return NULL;
+   }
+
+   void write(ir_variable *lhs, ir_variable *rhs, unsigned write_mask, int swizzle[4])
+   {
+      acp_entry *entry = new(this->lin_ctx) acp_entry(lhs, rhs, write_mask, swizzle);
+
+      /* lhs hash, hash of lhs -> acp_entry lists */
+      hash_entry *ht_entry = _mesa_hash_table_search(lhs_ht, lhs);
+      if (ht_entry) {
+         exec_list *lhs_list = (exec_list *) ht_entry->data;
+         lhs_list->push_tail(entry);
+      } else {
+         exec_list *lhs_list = new(mem_ctx) exec_list;
+         lhs_list->push_tail(entry);
+         _mesa_hash_table_insert(lhs_ht, lhs, lhs_list);
+      }
+
+      /* rhs hash, hash of rhs -> acp_entry pointers to lhs lists */
+      ht_entry = _mesa_hash_table_search(rhs_ht, rhs);
+      if (ht_entry) {
+         exec_list *rhs_list = (exec_list *) ht_entry->data;
+         rhs_list->push_tail(&entry->rhs_node);
+      } else {
+         exec_list *rhs_list = new(mem_ctx) exec_list;
+         rhs_list->push_tail(&entry->rhs_node);
+         _mesa_hash_table_insert(rhs_ht, rhs, rhs_list);
+      }
+   }
+
+private:
+   explicit copy_propagation_state(copy_propagation_state *original)
+   {
+      lhs_ht = _mesa_hash_table_clone(original->lhs_ht, this);
+      rhs_ht = _mesa_hash_table_clone(original->rhs_ht, this);
+      lin_ctx = original->lin_ctx;
+      mem_ctx = original->mem_ctx;
+   }
+
+   /** Hash of acp_entry: The available copies to propagate */
+   hash_table *lhs_ht;
+
+   /** Reverse index of the lhs_ht, to optimize finding uses of a certain variable. */
+   hash_table *rhs_ht;
+
+   void *mem_ctx;
+   void *lin_ctx;
+};
 
 class kill_entry : public exec_node
 {
 public:
+   /* override operator new from exec_node */
+   DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(kill_entry)
+
    kill_entry(ir_variable *var, int write_mask)
    {
       this->var = var;
@@ -97,15 +227,17 @@ public:
       this->progress = false;
       this->killed_all = false;
       this->mem_ctx = ralloc_context(NULL);
+      this->lin_ctx = linear_alloc_parent(this->mem_ctx, 0);
       this->shader_mem_ctx = NULL;
-      this->acp = new(mem_ctx) exec_list;
       this->kills = new(mem_ctx) exec_list;
+      this->state = new(mem_ctx) copy_propagation_state(mem_ctx, lin_ctx);
    }
    ~ir_copy_propagation_elements_visitor()
    {
       ralloc_free(mem_ctx);
    }
 
+   void handle_loop(ir_loop *, bool keep_acp);
    virtual ir_visitor_status visit_enter(class ir_loop *);
    virtual ir_visitor_status visit_enter(class ir_function_signature *);
    virtual ir_visitor_status visit_leave(class ir_assignment *);
@@ -119,8 +251,8 @@ public:
    void kill(kill_entry *k);
    void handle_if_block(exec_list *instructions);
 
-   /** List of acp_entry: The available copies to propagate */
-   exec_list *acp;
+   copy_propagation_state *state;
+
    /**
     * List of kill_entry: The variables whose values were killed in this
     * block.
@@ -133,6 +265,7 @@ public:
 
    /* Context for our local data structures. */
    void *mem_ctx;
+   void *lin_ctx;
    /* Context for allocating new shader nodes. */
    void *shader_mem_ctx;
 };
@@ -146,21 +279,22 @@ ir_copy_propagation_elements_visitor::visit_enter(ir_function_signature *ir)
     * block.  Any instructions at global scope will be shuffled into
     * main() at link time, so they're irrelevant to us.
     */
-   exec_list *orig_acp = this->acp;
    exec_list *orig_kills = this->kills;
    bool orig_killed_all = this->killed_all;
 
-   this->acp = new(mem_ctx) exec_list;
    this->kills = new(mem_ctx) exec_list;
    this->killed_all = false;
 
+   copy_propagation_state *orig_state = state;
+   this->state = new(mem_ctx) copy_propagation_state(mem_ctx, lin_ctx);
+
    visit_list_elements(this, &ir->body);
 
-   ralloc_free(this->acp);
-   ralloc_free(this->kills);
+   delete this->state;
+   this->state = orig_state;
 
+   ralloc_free(this->kills);
    this->kills = orig_kills;
-   this->acp = orig_acp;
    this->killed_all = orig_killed_all;
 
    return visit_continue_with_parent;
@@ -176,9 +310,9 @@ ir_copy_propagation_elements_visitor::visit_leave(ir_assignment *ir)
       kill_entry *k;
 
       if (lhs)
-        k = new(this->kills) kill_entry(var, ir->write_mask);
+        k = new(this->lin_ctx) kill_entry(var, ir->write_mask);
       else
-        k = new(this->kills) kill_entry(var, ~0);
+        k = new(this->lin_ctx) kill_entry(var, ~0);
 
       kill(k);
    }
@@ -248,17 +382,18 @@ ir_copy_propagation_elements_visitor::handle_rvalue(ir_rvalue **ir)
    /* Try to find ACP entries covering swizzle_chan[], hoping they're
     * the same source variable.
     */
-   foreach_in_list(acp_entry, entry, this->acp) {
-      if (var == entry->lhs) {
-        for (int c = 0; c < chans; c++) {
-           if (entry->write_mask & (1 << swizzle_chan[c])) {
-              source[c] = entry->rhs;
-              source_chan[c] = entry->swizzle[swizzle_chan[c]];
+   exec_list *ht_list = state->read(var);
+   if (ht_list) {
+      foreach_in_list(acp_entry, entry, ht_list) {
+         for (int c = 0; c < chans; c++) {
+            if (entry->write_mask & (1 << swizzle_chan[c])) {
+               source[c] = entry->rhs;
+               source_chan[c] = entry->swizzle[swizzle_chan[c]];
 
                if (source_chan[c] != swizzle_chan[c])
                   noop_swizzle = false;
-           }
-        }
+            }
+         }
       }
    }
 
@@ -318,7 +453,7 @@ ir_copy_propagation_elements_visitor::visit_enter(ir_call *ir)
    /* Since we're unlinked, we don't (necessarily) know the side effects of
     * this call.  So kill all copies.
     */
-   acp->make_empty();
+   this->state->erase_all();
    this->killed_all = true;
 
    return visit_continue_with_parent;
@@ -327,29 +462,26 @@ ir_copy_propagation_elements_visitor::visit_enter(ir_call *ir)
 void
 ir_copy_propagation_elements_visitor::handle_if_block(exec_list *instructions)
 {
-   exec_list *orig_acp = this->acp;
    exec_list *orig_kills = this->kills;
    bool orig_killed_all = this->killed_all;
 
-   this->acp = new(mem_ctx) exec_list;
    this->kills = new(mem_ctx) exec_list;
    this->killed_all = false;
 
    /* Populate the initial acp with a copy of the original */
-   foreach_in_list(acp_entry, a, orig_acp) {
-      this->acp->push_tail(new(this->acp) acp_entry(a));
-   }
+   copy_propagation_state *orig_state = state;
+   this->state = orig_state->clone();
 
    visit_list_elements(this, instructions);
 
-   if (this->killed_all) {
-      orig_acp->make_empty();
-   }
+   delete this->state;
+   this->state = orig_state;
+
+   if (this->killed_all)
+      this->state->erase_all();
 
    exec_list *new_kills = this->kills;
    this->kills = orig_kills;
-   ralloc_free(this->acp);
-   this->acp = orig_acp;
    this->killed_all = this->killed_all || orig_killed_all;
 
    /* Move the new kills into the parent block's list, removing them
@@ -374,31 +506,34 @@ ir_copy_propagation_elements_visitor::visit_enter(ir_if *ir)
    return visit_continue_with_parent;
 }
 
-ir_visitor_status
-ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
+void
+ir_copy_propagation_elements_visitor::handle_loop(ir_loop *ir, bool keep_acp)
 {
-   exec_list *orig_acp = this->acp;
    exec_list *orig_kills = this->kills;
    bool orig_killed_all = this->killed_all;
 
-   /* FINISHME: For now, the initial acp for loops is totally empty.
-    * We could go through once, then go through again with the acp
-    * cloned minus the killed entries after the first run through.
-    */
-   this->acp = new(mem_ctx) exec_list;
    this->kills = new(mem_ctx) exec_list;
    this->killed_all = false;
 
-   visit_list_elements(this, &ir->body_instructions);
+   copy_propagation_state *orig_state = state;
 
-   if (this->killed_all) {
-      orig_acp->make_empty();
+   if (keep_acp) {
+      /* Populate the initial acp with a copy of the original */
+      this->state = orig_state->clone();
+   } else {
+      this->state = new(mem_ctx) copy_propagation_state(mem_ctx, lin_ctx);
    }
 
+   visit_list_elements(this, &ir->body_instructions);
+
+   delete this->state;
+   this->state = orig_state;
+
+   if (this->killed_all)
+      this->state->erase_all();
+
    exec_list *new_kills = this->kills;
    this->kills = orig_kills;
-   ralloc_free(this->acp);
-   this->acp = orig_acp;
    this->killed_all = this->killed_all || orig_killed_all;
 
    foreach_in_list_safe(kill_entry, k, new_kills) {
@@ -406,6 +541,13 @@ ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
    }
 
    ralloc_free(new_kills);
+}
+
+ir_visitor_status
+ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
+{
+   handle_loop(ir, false);
+   handle_loop(ir, true);
 
    /* already descended into the children. */
    return visit_continue_with_parent;
@@ -415,24 +557,12 @@ ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
 void
 ir_copy_propagation_elements_visitor::kill(kill_entry *k)
 {
-   foreach_in_list_safe(acp_entry, entry, acp) {
-      if (entry->lhs == k->var) {
-        entry->write_mask = entry->write_mask & ~k->write_mask;
-        if (entry->write_mask == 0) {
-           entry->remove();
-           continue;
-        }
-      }
-      if (entry->rhs == k->var) {
-        entry->remove();
-      }
-   }
+   state->erase(k->var, k->write_mask);
 
    /* If we were on a list, remove ourselves before inserting */
    if (k->next)
       k->remove();
 
-   ralloc_steal(this->kills, k);
    this->kills->push_tail(k);
 }
 
@@ -443,7 +573,6 @@ ir_copy_propagation_elements_visitor::kill(kill_entry *k)
 void
 ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
 {
-   acp_entry *entry;
    int orig_swizzle[4] = {0, 1, 2, 3};
    int swizzle[4];
 
@@ -454,6 +583,10 @@ ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
    if (!lhs || !(lhs->type->is_scalar() || lhs->type->is_vector()))
       return;
 
+   if (lhs->var->data.mode == ir_var_shader_storage ||
+       lhs->var->data.mode == ir_var_shader_shared)
+      return;
+
    ir_dereference_variable *rhs = ir->rhs->as_dereference_variable();
    if (!rhs) {
       ir_swizzle *swiz = ir->rhs->as_swizzle();
@@ -470,6 +603,10 @@ ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
       orig_swizzle[3] = swiz->mask.w;
    }
 
+   if (rhs->var->data.mode == ir_var_shader_storage ||
+       rhs->var->data.mode == ir_var_shader_shared)
+      return;
+
    /* Move the swizzle channels out to the positions they match in the
     * destination.  We don't want to have to rewrite the swizzle[]
     * array every time we clear a bit of the write_mask.
@@ -493,9 +630,10 @@ ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
       }
    }
 
-   entry = new(this->mem_ctx) acp_entry(lhs->var, rhs->var, write_mask,
-                                       swizzle);
-   this->acp->push_tail(entry);
+   if (lhs->var->data.precise != rhs->var->data.precise)
+      return;
+
+   state->write(lhs->var, rhs->var, write_mask, swizzle);
 }
 
 bool