Skip to content

Commit 69dde47

Browse files
authored
Merge pull request #261 from sysprog21/reduce-memset
Improve arena allocation to reduce memset overhead
2 parents 3ce8ed8 + b612da3 commit 69dde47

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

src/globals.c

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,21 @@ symbol_t *arena_alloc_symbol(void)
313313

314314
constant_t *arena_alloc_constant(void)
315315
{
316-
return arena_calloc(GENERAL_ARENA, 1, sizeof(constant_t));
316+
/* constant_t is simple, can avoid zeroing */
317+
constant_t *c = arena_alloc(GENERAL_ARENA, sizeof(constant_t));
318+
c->alias[0] = '\0';
319+
c->value = 0;
320+
return c;
317321
}
318322

319323
alias_t *arena_alloc_alias(void)
320324
{
321-
return arena_calloc(GENERAL_ARENA, 1, sizeof(alias_t));
325+
/* alias_t is simple, can avoid zeroing */
326+
alias_t *a = arena_alloc(GENERAL_ARENA, sizeof(alias_t));
327+
a->alias[0] = '\0';
328+
a->value[0] = '\0';
329+
a->disabled = false;
330+
return a;
322331
}
323332

324333
macro_t *arena_alloc_macro(void)
@@ -328,6 +337,7 @@ macro_t *arena_alloc_macro(void)
328337

329338
bb_traversal_args_t *arena_alloc_traversal_args(void)
330339
{
340+
/* Keep using calloc for safety */
331341
return arena_calloc(GENERAL_ARENA, 1, sizeof(bb_traversal_args_t));
332342
}
333343

@@ -616,11 +626,18 @@ ph2_ir_t *add_existed_ph2_ir(ph2_ir_t *ph2_ir)
616626

617627
ph2_ir_t *add_ph2_ir(opcode_t op)
618628
{
619-
ph2_ir_t *ph2_ir = arena_calloc(BB_ARENA, 1, sizeof(ph2_ir_t));
629+
ph2_ir_t *ph2_ir = arena_alloc(BB_ARENA, sizeof(ph2_ir_t));
620630
ph2_ir->op = op;
621-
/* Set safe defaults; arch-lowering may annotate later */
631+
/* Initialize all fields explicitly */
622632
ph2_ir->next = NULL;
623633
ph2_ir->is_branch_detached = 0;
634+
ph2_ir->src0 = 0;
635+
ph2_ir->src1 = 0;
636+
ph2_ir->dest = 0;
637+
ph2_ir->func_name[0] = '\0';
638+
ph2_ir->next_bb = NULL;
639+
ph2_ir->then_bb = NULL;
640+
ph2_ir->else_bb = NULL;
624641
return add_existed_ph2_ir(ph2_ir);
625642
}
626643

@@ -633,14 +650,17 @@ void set_var_liveout(var_t *var, int end)
633650

634651
block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
635652
{
636-
block_t *blk = arena_calloc(BLOCK_ARENA, 1, sizeof(block_t));
653+
block_t *blk = arena_alloc(BLOCK_ARENA, sizeof(block_t));
637654

638-
blk->parent = parent;
639-
blk->func = func;
640-
blk->macro = macro;
655+
/* Initialize all fields explicitly */
656+
blk->locals.size = 0;
641657
blk->locals.capacity = 16;
642658
blk->locals.elements =
643659
arena_alloc(BLOCK_ARENA, blk->locals.capacity * sizeof(var_t *));
660+
blk->parent = parent;
661+
blk->func = func;
662+
blk->macro = macro;
663+
blk->next = NULL;
644664
return blk;
645665
}
646666

@@ -887,15 +907,20 @@ func_t *find_func(char *func_name)
887907
/* Create a basic block and set the scope of variables to 'parent' block */
888908
basic_block_t *bb_create(block_t *parent)
889909
{
910+
/* Use arena_calloc for basic_block_t as it has many arrays that need
911+
* zeroing (live_gen, live_kill, live_in, live_out, DF, RDF, dom_next, etc.)
912+
* This is simpler and safer than manually initializing everything.
913+
*/
890914
basic_block_t *bb = arena_calloc(BB_ARENA, 1, sizeof(basic_block_t));
891915

892-
for (int i = 0; i < MAX_BB_PRED; i++) {
893-
bb->prev[i].bb = NULL;
894-
bb->prev[i].type = NEXT;
895-
}
916+
/* Initialize non-zero fields */
896917
bb->scope = parent;
897918
bb->belong_to = parent->func;
898919

920+
/* Initialize prev array with NEXT type */
921+
for (int i = 0; i < MAX_BB_PRED; i++)
922+
bb->prev[i].type = NEXT;
923+
899924
if (dump_ir)
900925
snprintf(bb->bb_label_name, MAX_VAR_LEN, ".label.%d", bb_label_idx++);
901926

@@ -1003,16 +1028,23 @@ void add_insn(block_t *block,
10031028

10041029
bb->scope = block;
10051030

1006-
insn_t *n = arena_calloc(INSN_ARENA, 1, sizeof(insn_t));
1031+
insn_t *n = arena_alloc(INSN_ARENA, sizeof(insn_t));
1032+
n->next = NULL;
1033+
n->prev = NULL;
10071034
n->opcode = op;
10081035
n->rd = rd;
10091036
n->rs1 = rs1;
10101037
n->rs2 = rs2;
10111038
n->sz = sz;
1039+
n->useful = false;
10121040
n->belong_to = bb;
1041+
n->phi_ops = NULL;
1042+
n->idx = 0;
10131043

10141044
if (str)
10151045
strcpy(n->str, str);
1046+
else
1047+
n->str[0] = '\0';
10161048

10171049
if (!bb->insn_list.head)
10181050
bb->insn_list.head = n;

src/reg-alloc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,18 @@ void refresh(basic_block_t *bb, insn_t *insn)
5353

5454
ph2_ir_t *bb_add_ph2_ir(basic_block_t *bb, opcode_t op)
5555
{
56-
ph2_ir_t *n = arena_calloc(BB_ARENA, 1, sizeof(ph2_ir_t));
56+
ph2_ir_t *n = arena_alloc(BB_ARENA, sizeof(ph2_ir_t));
5757
n->op = op;
58-
/* Ensure deterministic defaults for newly created IR nodes */
58+
/* Initialize all fields explicitly */
5959
n->next = NULL; /* well-formed singly linked list */
6060
n->is_branch_detached = 0; /* arch-lowering will set for branches */
61+
n->src0 = 0;
62+
n->src1 = 0;
63+
n->dest = 0;
64+
n->func_name[0] = '\0';
65+
n->next_bb = NULL;
66+
n->then_bb = NULL;
67+
n->else_bb = NULL;
6168

6269
if (!bb->ph2_ir_list.head)
6370
bb->ph2_ir_list.head = n;

0 commit comments

Comments
 (0)