glsl: Delete linker stuff relating to built-in functions.
[mesa.git] / src / compiler / glsl / opt_copy_propagation.cpp
index 310708db868acde5a8e5d433005757f4c6e1a8bc..02628cd7e52858f78dd989d95d1c52dad08e33b2 100644 (file)
 #include "ir_basic_block.h"
 #include "ir_optimization.h"
 #include "compiler/glsl_types.h"
+#include "util/hash_table.h"
 
 namespace {
 
-class acp_entry : public exec_node
-{
-public:
-   acp_entry(ir_variable *lhs, ir_variable *rhs)
-   {
-      assert(lhs);
-      assert(rhs);
-      this->lhs = lhs;
-      this->rhs = rhs;
-   }
-
-   ir_variable *lhs;
-   ir_variable *rhs;
-};
-
-
 class kill_entry : public exec_node
 {
 public:
@@ -74,8 +59,10 @@ public:
    {
       progress = false;
       mem_ctx = ralloc_context(0);
-      this->acp = new(mem_ctx) exec_list;
+      acp = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
+                                    _mesa_key_pointer_equal);
       this->kills = new(mem_ctx) exec_list;
+      killed_all = false;
    }
    ~ir_copy_propagation_visitor()
    {
@@ -83,6 +70,7 @@ public:
    }
 
    virtual ir_visitor_status visit(class ir_dereference_variable *);
+   void handle_loop(class 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_enter(class ir_function *);
@@ -94,8 +82,8 @@ public:
    void kill(ir_variable *ir);
    void handle_if_block(exec_list *instructions);
 
-   /** List of acp_entry: The available copies to propagate */
-   exec_list *acp;
+   /** Hash of lhs->rhs: The available copies to propagate */
+   hash_table *acp;
    /**
     * List of kill_entry: The variables whose values were killed in this
     * block.
@@ -118,17 +106,18 @@ ir_copy_propagation_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;
+   hash_table *orig_acp = this->acp;
    exec_list *orig_kills = this->kills;
    bool orig_killed_all = this->killed_all;
 
-   this->acp = new(mem_ctx) exec_list;
+   acp = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
+                                 _mesa_key_pointer_equal);
    this->kills = new(mem_ctx) exec_list;
    this->killed_all = false;
 
    visit_list_elements(this, &ir->body);
 
-   ralloc_free(this->acp);
+   _mesa_hash_table_destroy(acp, NULL);
    ralloc_free(this->kills);
 
    this->kills = orig_kills;
@@ -168,14 +157,10 @@ ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
    if (this->in_assignee)
       return visit_continue;
 
-   ir_variable *var = ir->var;
-
-   foreach_in_list(acp_entry, entry, this->acp) {
-      if (var == entry->lhs) {
-        ir->var = entry->rhs;
-        this->progress = true;
-        break;
-      }
+   struct hash_entry *entry = _mesa_hash_table_search(acp, ir->var);
+   if (entry) {
+      ir->var = (ir_variable *) entry->data;
+      progress = true;
    }
 
    return visit_continue;
@@ -199,7 +184,7 @@ ir_copy_propagation_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();
+   _mesa_hash_table_clear(acp, NULL);
    this->killed_all = true;
 
    return visit_continue_with_parent;
@@ -208,28 +193,30 @@ ir_copy_propagation_visitor::visit_enter(ir_call *ir)
 void
 ir_copy_propagation_visitor::handle_if_block(exec_list *instructions)
 {
-   exec_list *orig_acp = this->acp;
+   hash_table *orig_acp = this->acp;
    exec_list *orig_kills = this->kills;
    bool orig_killed_all = this->killed_all;
 
-   this->acp = new(mem_ctx) exec_list;
+   acp = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
+                                 _mesa_key_pointer_equal);
    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->lhs, a->rhs));
+   struct hash_entry *entry;
+   hash_table_foreach(orig_acp, entry) {
+      _mesa_hash_table_insert(acp, entry->key, entry->data);
    }
 
    visit_list_elements(this, instructions);
 
    if (this->killed_all) {
-      orig_acp->make_empty();
+      _mesa_hash_table_clear(orig_acp, NULL);
    }
 
    exec_list *new_kills = this->kills;
    this->kills = orig_kills;
-   ralloc_free(this->acp);
+   _mesa_hash_table_destroy(acp, NULL);
    this->acp = orig_acp;
    this->killed_all = this->killed_all || orig_killed_all;
 
@@ -252,30 +239,34 @@ ir_copy_propagation_visitor::visit_enter(ir_if *ir)
    return visit_continue_with_parent;
 }
 
-ir_visitor_status
-ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
+void
+ir_copy_propagation_visitor::handle_loop(ir_loop *ir, bool keep_acp)
 {
-   exec_list *orig_acp = this->acp;
+   hash_table *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;
+   acp = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
+                                 _mesa_key_pointer_equal);
    this->kills = new(mem_ctx) exec_list;
    this->killed_all = false;
 
+   if (keep_acp) {
+      struct hash_entry *entry;
+      hash_table_foreach(orig_acp, entry) {
+         _mesa_hash_table_insert(acp, entry->key, entry->data);
+      }
+   }
+
    visit_list_elements(this, &ir->body_instructions);
 
    if (this->killed_all) {
-      orig_acp->make_empty();
+      _mesa_hash_table_clear(orig_acp, NULL);
    }
 
    exec_list *new_kills = this->kills;
    this->kills = orig_kills;
-   ralloc_free(this->acp);
+   _mesa_hash_table_destroy(acp, NULL);
    this->acp = orig_acp;
    this->killed_all = this->killed_all || orig_killed_all;
 
@@ -284,6 +275,20 @@ ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
    }
 
    ralloc_free(new_kills);
+}
+
+ir_visitor_status
+ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
+{
+   /* Make a conservative first pass over the loop with an empty ACP set.
+    * This also removes any killed entries from the original ACP set.
+    */
+   handle_loop(ir, false);
+
+   /* Then, run it again with the real ACP set, minus any killed entries.
+    * This takes care of propagating values from before the loop into it.
+    */
+   handle_loop(ir, true);
 
    /* already descended into the children. */
    return visit_continue_with_parent;
@@ -295,9 +300,14 @@ ir_copy_propagation_visitor::kill(ir_variable *var)
    assert(var != NULL);
 
    /* Remove any entries currently in the ACP for this kill. */
-   foreach_in_list_safe(acp_entry, entry, acp) {
-      if (entry->lhs == var || entry->rhs == var) {
-        entry->remove();
+   struct hash_entry *entry = _mesa_hash_table_search(acp, var);
+   if (entry) {
+      _mesa_hash_table_remove(acp, entry);
+   }
+
+   hash_table_foreach(acp, entry) {
+      if (var == (ir_variable *) entry->data) {
+         _mesa_hash_table_remove(acp, entry);
       }
    }
 
@@ -313,8 +323,6 @@ ir_copy_propagation_visitor::kill(ir_variable *var)
 void
 ir_copy_propagation_visitor::add_copy(ir_assignment *ir)
 {
-   acp_entry *entry;
-
    if (ir->condition)
       return;
 
@@ -331,9 +339,11 @@ ir_copy_propagation_visitor::add_copy(ir_assignment *ir)
         ir->condition = new(ralloc_parent(ir)) ir_constant(false);
         this->progress = true;
       } else if (lhs_var->data.mode != ir_var_shader_storage &&
-                 lhs_var->data.mode != ir_var_shader_shared) {
-        entry = new(this->acp) acp_entry(lhs_var, rhs_var);
-        this->acp->push_tail(entry);
+                 lhs_var->data.mode != ir_var_shader_shared &&
+                 lhs_var->data.precise == rhs_var->data.precise) {
+         assert(lhs_var);
+         assert(rhs_var);
+         _mesa_hash_table_insert(acp, lhs_var, rhs_var);
       }
    }
 }