Add hashtable/set_empty to go with _reset, which removes all items from the
authorDavid Given <dg@cowlark.com>
Fri, 16 Dec 2016 21:16:45 +0000 (22:16 +0100)
committerDavid Given <dg@cowlark.com>
Fri, 16 Dec 2016 21:16:45 +0000 (22:16 +0100)
table but doesn't free the bucket array or reset the size --- saves an
allocation when reusing the hashtable.

mach/proto/mcg/pass_livevreganalysis.c
mach/proto/mcg/pass_nonlocalphis.c
mach/proto/mcg/pass_removedeadphis.c
mach/proto/mcg/pass_typeinference.c
mach/proto/mcg/pass_vregusage.c
modules/src/data/hashtable.c
modules/src/data/hashtable.h
modules/src/data/set.c
modules/src/data/set.h

index 5481c65..7e361b9 100644 (file)
@@ -23,32 +23,63 @@ static void preload_blocks(void)
     }
 }
 
+static bool add_set_to_array(struct set* set, void* arrayp)
+{
+    bool nochange = true;
+    struct array* array = arrayp;
+    struct set_iterator sit = {};
+
+    while (set_next(set, &sit))
+        nochange &= array_appendu(array, sit.item);
+
+    return nochange;
+}
+
+static void add_array_to_set(void* arrayp, struct set* set)
+{
+    struct array* array = arrayp;
+    int i;
+
+    for (i=0; i<array->count; i++)
+        set_add(set, array->item[i]);
+}
+
+static void remove_array_from_set(void* arrayp, struct set* set)
+{
+    struct array* array = arrayp;
+    int i;
+
+    for (i=0; i<array->count; i++)
+        set_remove(set, array->item[i]);
+}
+
 static void propagate_liveness(struct basicblock* bb)
 {
-       static ARRAYOF(struct vreg) current;
-       int i;
+    static struct set current;
+       int i, j;
 
-       current.count = 0;
-       array_appendall(&current, &bb->liveouts);
+    set_empty(&current);
+
+    add_array_to_set(&bb->liveouts, &current);
 
        for (i=bb->hops.count-1; i>=0; i--)
        {
                struct hop* hop = bb->hops.item[i];
 
-               array_removeall(&current, &hop->outs);
-               finished &= array_appendallu(&hop->throughs, &current);
-               array_appendallu(&current, &hop->ins);
+        remove_array_from_set(&hop->outs, &current);
+        finished &= add_set_to_array(&current, &hop->throughs);
+        add_array_to_set(&hop->ins, &current);
        }
 
     for (i=0; i<bb->phis.count; i++)
-        array_remove(&current, bb->phis.item[i].left);
+        set_remove(&current, bb->phis.item[i].left);
 
-       finished &= array_appendallu(&bb->liveins, &current);
+    finished &= add_set_to_array(&current, &bb->liveins);
 
        for (i=0; i<bb->prevs.count; i++)
        {
                struct basicblock* prev = bb->prevs.item[i];
-               finished &= array_appendallu(&prev->liveouts, &current);
+        finished &= add_set_to_array(&current, &prev->liveouts);
        }
 }
 
@@ -67,8 +98,6 @@ void pass_live_vreg_analysis(void)
                        propagate_liveness(dominance.postorder.item[i]);
        }
        while (!finished);
-
-    //assert(cfg.entry->liveins.count == 0);
 }
 
 /* vim: set sw=4 ts=4 expandtab : */
index 55dfed2..8f0fdc7 100644 (file)
@@ -121,8 +121,8 @@ static void import_ir(struct ir* phi)
 {
     int i;
 
-    set_reset(&confirmed);
-    set_reset(&pending);
+    set_empty(&confirmed);
+    set_empty(&pending);
 
     recursively_add_children_to_pending(current_dest);
     set_add(&confirmed, current_dest);
index 8d46b80..fb53f75 100644 (file)
@@ -70,7 +70,7 @@ void pass_remove_dead_phis(void)
     {
         changed = false;
 
-        set_reset(&phis);
+        set_empty(&phis);
         for (i=0; i<cfg.preorder.count; i++)
             collect_phis(cfg.preorder.item[i]);
 
index 5bc12ac..ae45468 100644 (file)
@@ -18,7 +18,7 @@ static void collect_irs(void)
 {
     int i;
     
-    set_reset(&irs);
+    set_empty(&irs);
        for (i=0; i<cfg.preorder.count; i++)
     {
         struct basicblock* bb = cfg.preorder.item[i];
index 8681844..e8d400f 100644 (file)
@@ -36,7 +36,7 @@ void pass_determine_vreg_usage(void)
 {
     int i, j;
 
-    set_reset(&vregs);
+    set_empty(&vregs);
     hop_walk(assign_uses_cb, NULL);
 
     for (i=0; i<dominance.preorder.count; i++)
index d4dba61..35dfe5a 100644 (file)
@@ -37,11 +37,15 @@ static void lazy_init(struct hashtable* ht)
         ht->buckets = calloc(ht->num_buckets, sizeof(struct hashnode*));
 }
 
-void hashtable_reset(struct hashtable* ht)
+void hashtable_empty(struct hashtable* ht)
 {
     while (ht->size)
         hashtable_pop(ht);
+}
 
+void hashtable_reset(struct hashtable* ht)
+{
+       hashtable_empty(ht);
     free(ht->buckets);
     ht->buckets = NULL;
 }
index e89a052..2838d81 100644 (file)
@@ -29,6 +29,7 @@ struct hashtable_iterator
     struct hashnode* node;
 };
 
+extern void hashtable_empty(struct hashtable* ht);
 extern void hashtable_reset(struct hashtable* ht);
 extern void hashtable_rebucket(struct hashtable* ht, unsigned int num_buckets);
 
index 4a437d8..fd0ab9a 100644 (file)
@@ -3,6 +3,11 @@
 #include <stdbool.h>
 #include "set.h"
 
+void set_empty(struct set* s)
+{
+       hashtable_empty(&s->table);
+}
+
 void set_reset(struct set* s)
 {
        hashtable_reset(&s->table);
index 7cce54f..9795681 100644 (file)
@@ -10,6 +10,7 @@ struct set
        struct hashtable table;
 };
 
+extern void set_empty(struct set* s);
 extern void set_reset(struct set* s);
 
 extern bool set_add(struct set* s, void* item);