summaryrefslogtreecommitdiff
path: root/ir.c
diff options
context:
space:
mode:
authorWormHeamer2025-10-27 23:26:44 -0400
committerWormHeamer2025-10-27 23:26:44 -0400
commitdd62801133cddca25d94c9c59f8ca7d0748850c6 (patch)
treedb5210bd773d0c57f1cfe53b303d1a6debdde0b7 /ir.c
parenta2c5243af5bd8482385aeb3395e073ff17f57a0d (diff)
small size optimization on <= 1-node NodeList
Diffstat (limited to 'ir.c')
-rw-r--r--ir.c55
1 files changed, 41 insertions, 14 deletions
diff --git a/ir.c b/ir.c
index 88676bc..b15e3a8 100644
--- a/ir.c
+++ b/ir.c
@@ -6,17 +6,41 @@
/* node lists */
-void nodelist_push(NodeList *l, Node *p, Arena *a) {
- ZDA_PUSH(a, l, p);
+void nodelist_fit(NodeList *l, uint32_t sz, Arena *a) {
+ if (l->cap) {
+ if (sz > l->cap) {
+ uint32_t c = l->cap;
+ while (sz > c) c <<= 1;
+ l->data = resize(a, l->data, l->cap, c);
+ l->cap = c;
+ }
+ } else {
+ if (sz > 1) {
+ uint32_t cap = 2;
+ while (cap < sz) cap <<= 1;
+ Node **data = new_arr(a, Node *, cap);
+ data[0] = l->sbo;
+ l->data = data;
+ l->cap = cap;
+ }
+ }
}
-void nodelist_remove(NodeList *l, Node *p) {
- for (int i = l->len - 1; i >= 0; i--) {
- if (l->data[i] == p) {
- if (p) p->refs--;
+void nodelist_push(NodeList *l, Node *n, Arena *a) {
+ nodelist_fit(l, l->len + 1, a);
+ Node **p = nodelist_nth(l, l->len++);
+ if (n) n->refs++;
+ *p = n;
+}
+
+void nodelist_remove(NodeList *l, Node *n) {
+ Node **data = nodelist_data(l);
+ for (unsigned i = 0; i < l->len; i++) {
+ if (data[i] == n) {
+ if (n) n->refs--;
l->len--;
if (i < l->len) {
- memmove(&l->data[i], &l->data[i + 1], sizeof(Node*) * (l->len - i));
+ memmove(&data[i], &data[i+1], (l->len - i) * sizeof(Node *));
}
break;
}
@@ -24,12 +48,15 @@ void nodelist_remove(NodeList *l, Node *p) {
}
void nodelist_remove_unordered(NodeList *l, Node *p) {
- for (int i = l->len - 1; i >= 0; i--) {
- if (l->data[i] == p) {
+ Node **data = nodelist_data(l);
+ unsigned i = l->len;
+ while (i) {
+ i--;
+ if (data[i] == p) {
if (p) p->refs--;
l->len--;
if (i < l->len) {
- l->data[i] = l->data[l->len];
+ data[i] = data[l->len];
}
break;
}
@@ -74,13 +101,13 @@ void node_kill(Node *n, Graph *p) {
}
void node_add_out(Graph *p, Node *a, Node *b) {
+ assert(b > 0xfffff || !b);
nodelist_push(&a->out, b, p->pool->arena);
- if (b) b->refs++;
}
void node_add_in(Graph *p, Node *a, Node *b) {
+ assert(b > 0xfffff || !b);
nodelist_push(&a->in, b, p->pool->arena);
- if (b) b->refs++;
}
void node_set_in(Graph *p, Node *n, int idx, Node *to) {
@@ -93,10 +120,10 @@ void node_set_in(Graph *p, Node *n, int idx, Node *to) {
}
void node_add(Graph *p, Node *src, Node *dest) {
- if (src) assert(src->op != N_DEAD);
- if (dest) assert(dest->op != N_DEAD);
+ assert(dest->op != N_DEAD);
node_add_in(p, dest, src);
if (!src) return;
+ assert(src->op != N_DEAD);
node_add_out(p, src, dest);
if (dest->src_pos.n == 0) dest->src_pos = src->src_pos;
else if (src->src_pos.n != 0) {