Skip to content

Commit

Permalink
Align loops
Browse files Browse the repository at this point in the history
  • Loading branch information
dstogov committed Mar 14, 2024
1 parent fe6e534 commit b447d62
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ir_aarch64.dasc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
|.globals ir_lb
|.section code, cold_code, rodata, jmp_table

|.define IR_LOOP_ALIGNMENT, 16

#ifdef IR_DEBUG
typedef struct _ir_mem {uint64_t v;} ir_mem;

Expand Down Expand Up @@ -5700,6 +5702,9 @@ void *ir_emit_code(ir_ctx *ctx, size_t *size_ptr)
if ((bb->flags & (IR_BB_START|IR_BB_ENTRY|IR_BB_EMPTY)) == IR_BB_EMPTY) {
continue;
}
if (bb->flags & IR_BB_ALIGN_LOOP) {
| .align IR_LOOP_ALIGNMENT
}
|=>b:

i = bb->start;
Expand Down
16 changes: 14 additions & 2 deletions ir_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,19 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx)
#endif
}

/* 5. Group chains according to the most frequent edge between them */
/* 5. Align loop headers */
for (b = 1; b <= ctx->cfg_blocks_count; b++) {
if (chains[b].head == b) {
bb = &ctx->cfg_blocks[b];
if (bb->loop_depth) {
if ((bb->flags & IR_BB_LOOP_HEADER) || ir_chain_head(chains, bb->loop_header) == b) {
bb->flags |= IR_BB_ALIGN_LOOP;
}
}
}
}

/* 6. Group chains according to the most frequent edge between them */
// TODO: Try to find a better heuristic
for (e = edges, i = edges_count; i > 0; e++, i--) {
#if !IR_DEBUG_BB_SCHEDULE_GRAPH
Expand All @@ -2380,7 +2392,7 @@ static int ir_schedule_blocks_bottom_up(ir_ctx *ctx)
ir_dump_chains(ctx, chains);
#endif

/* 6. Form a final BB order */
/* 7. Form a final BB order */
count = 0;
for (b = 1; b <= ctx->cfg_blocks_count; b++) {
if (chains[b].head == b) {
Expand Down
2 changes: 2 additions & 0 deletions ir_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,8 @@ bool ir_use_list_add(ir_ctx *ctx, ir_ref to, ir_ref new_use);
#define IR_BB_HAS_PARAM (1<<12)
#define IR_BB_HAS_VAR (1<<13)

/* The following flags are set by BB scheduler */
#define IR_BB_ALIGN_LOOP (1<<14)

struct _ir_block {
uint32_t flags;
Expand Down
5 changes: 5 additions & 0 deletions ir_x86.dasc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
|.globals ir_lb
|.section code, cold_code, rodata, jmp_table

|.define IR_LOOP_ALIGNMENT, 16

#ifdef IR_DEBUG
typedef struct _ir_mem {uint64_t v;} ir_mem;

Expand Down Expand Up @@ -9742,6 +9744,9 @@ void *ir_emit_code(ir_ctx *ctx, size_t *size_ptr)
if ((bb->flags & (IR_BB_START|IR_BB_ENTRY|IR_BB_EMPTY)) == IR_BB_EMPTY) {
continue;
}
if (bb->flags & IR_BB_ALIGN_LOOP) {
| .align IR_LOOP_ALIGNMENT
}
|=>b:

i = bb->start;
Expand Down

0 comments on commit b447d62

Please sign in to comment.