Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/arch-lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
void arm_lower(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
for (ph2_ir_t *insn = bb->ph2_ir_list.head; insn;
insn = insn->next) {
Expand All @@ -37,6 +41,10 @@ void arm_lower(void)
void riscv_lower(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
for (ph2_ir_t *insn = bb->ph2_ir_list.head; insn;
insn = insn->next) {
Expand Down
4 changes: 4 additions & 0 deletions src/arm-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ void cfg_flatten(void)
elf_offset += 32; /* 6 insns for main call + 2 for exit */

for (func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

/* reserve stack */
ph2_ir_t *flatten_ir = add_ph2_ir(OP_define);
flatten_ir->src0 = func->stack_size;
Expand Down
20 changes: 15 additions & 5 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,19 @@ strbuf_t *SOURCE;
hashmap_t *INCLUSION_MAP;

/* ELF sections */
strbuf_t *elf_code, *elf_data;
strbuf_t *elf_code;
strbuf_t *elf_data;
strbuf_t *elf_rodata;
strbuf_t *elf_header;
strbuf_t *elf_symtab;
strbuf_t *elf_strtab;
strbuf_t *elf_section;
int elf_header_len = 0x54; /* ELF fixed: 0x34 + 1 * 0x20 */
int elf_code_start, elf_data_start;
int elf_code_start;
int elf_data_start;
int elf_rodata_start;
int elf_bss_start, elf_bss_size;
int elf_bss_start;
int elf_bss_size;

/* Create a new arena block with given capacity.
* @capacity: The capacity of the arena block. Must be positive.
Expand Down Expand Up @@ -347,7 +350,8 @@ bb_traversal_args_t *arena_alloc_traversal_args(void)

void arena_free(arena_t *arena)
{
arena_block_t *block = arena->head, *next;
arena_block_t *block = arena->head;
arena_block_t *next;

while (block) {
next = block->next;
Expand Down Expand Up @@ -472,7 +476,9 @@ void hashmap_rehash(hashmap_t *map)
}

for (int i = 0; i < old_cap; i++) {
hashmap_node_t *cur = old_buckets[i], *next, *target_cur;
hashmap_node_t *cur = old_buckets[i];
hashmap_node_t *next;
hashmap_node_t *target_cur;

while (cur) {
next = cur->next;
Expand Down Expand Up @@ -1649,6 +1655,10 @@ void dump_insn(void)
printf("==<START OF INSN DUMP>==\n");

for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

bool at_func_start = true;

printf("def %s", func->return_def.type->type_name);
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
int main(int argc, char *argv[])
{
bool libc = true;
char *out = NULL, *in = NULL;
char *out = NULL;
char *in = NULL;

for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--dump-ir"))
Expand Down
4 changes: 4 additions & 0 deletions src/peephole.c
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,10 @@ bool triple_pattern_optimization(ph2_ir_t *ph2_ir)
void peephole(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

/* Local peephole optimizations on post-register-allocation IR */
for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
for (ph2_ir_t *ir = bb->ph2_ir_list.head; ir; ir = ir->next) {
Expand Down
4 changes: 4 additions & 0 deletions src/reg-alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ void reg_alloc(void)
}

for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

func->visited++;

if (!strcmp(func->return_def.var_name, "main"))
Expand Down
4 changes: 4 additions & 0 deletions src/riscv-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ void cfg_flatten(void)
elf_offset += 24;

for (func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

/* reserve stack */
ph2_ir_t *flatten_ir = add_ph2_ir(OP_define);
flatten_ir->src0 = func->stack_size;
Expand Down
97 changes: 95 additions & 2 deletions src/ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void build_rpo(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->bbs;

Expand Down Expand Up @@ -158,6 +162,10 @@ basic_block_t *intersect(basic_block_t *i, basic_block_t *j)
void build_idom(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

bool changed;

func->bbs->idom = func->bbs;
Expand Down Expand Up @@ -230,6 +238,10 @@ void build_dom(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->bbs;

Expand Down Expand Up @@ -264,6 +276,10 @@ void build_df(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->bbs;

Expand All @@ -287,6 +303,10 @@ basic_block_t *reverse_intersect(basic_block_t *i, basic_block_t *j)
void build_r_idom(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

bool changed;

func->exit->r_idom = func->exit;
Expand Down Expand Up @@ -354,6 +374,10 @@ void build_rdom(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->exit;

Expand Down Expand Up @@ -398,6 +422,10 @@ void build_rdf(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->exit;

Expand Down Expand Up @@ -439,6 +467,10 @@ void use_chain_delete(use_chain_t *u, var_t *var)
void use_chain_build(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
for (insn_t *i = bb->insn_list.head; i; i = i->next) {
if (i->rs1)
Expand Down Expand Up @@ -545,6 +577,10 @@ void solve_globals(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->bbs;

Expand Down Expand Up @@ -606,6 +642,10 @@ bool insert_phi_insn(basic_block_t *bb, var_t *var)
void solve_phi_insertion(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (symbol_t *sym = func->global_sym_list.head; sym; sym = sym->next) {
var_t *var = sym->var;

Expand Down Expand Up @@ -798,6 +838,10 @@ void bb_solve_phi_params(basic_block_t *bb)
void solve_phi_params(void)
{
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (int i = 0; i < func->num_params; i++) {
/* FIXME: Direct argument renaming in SSA construction phase may
* interfere with later optimization passes
Expand Down Expand Up @@ -874,6 +918,10 @@ void unwind_phi(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->bbs;

Expand Down Expand Up @@ -1135,7 +1183,7 @@ void bb_dump(FILE *fd, func_t *func, basic_block_t *bb)
break;
case OP_sign_ext:
sprintf(str,
"<%s<SUB>%s</SUB> := sign_ext %s<SUB>%d</SUB>, %d>",
"<%s<SUB>%d</SUB> := sign_ext %s<SUB>%d</SUB>, %d>",
insn->rd->var_name, insn->rd->subscript,
insn->rs1->var_name, insn->rs1->subscript, insn->sz);
break;
Expand Down Expand Up @@ -1180,6 +1228,10 @@ void dump_cfg(char name[])
fprintf(fd, "strict digraph CFG {\n");
fprintf(fd, "node [shape=box]\n");
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

func->visited++;
fprintf(fd, "subgraph cluster_%p {\n", func);
fprintf(fd, "label=\"%p (%s)\"\n", func, func->return_def.var_name);
Expand Down Expand Up @@ -1209,6 +1261,10 @@ void dump_dom(char name[])
fprintf(fd, "node [shape=box]\n");
fprintf(fd, "splines=polyline\n");
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

fprintf(fd, "subgraph cluster_%p {\n", func);
fprintf(fd, "label=\"%p\"\n", func);
dom_dump(fd, func->bbs);
Expand Down Expand Up @@ -1740,6 +1796,10 @@ void dce_sweep(void)
int total_eliminated = 0; /* Track effectiveness */

for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
/* Skip unreachable blocks entirely */
if (is_block_unreachable(bb)) {
Expand Down Expand Up @@ -1812,17 +1872,30 @@ void optimize(void)
while (sccp_changed && sccp_iterations < 5) {
sccp_changed = false;
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

if (simple_sccp(func))
sccp_changed = true;
}
sccp_iterations++;
}

/* Run constant cast optimization for truncation */
for (func_t *func = FUNC_LIST.head; func; func = func->next)
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

optimize_constant_casts(func);
}

for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

/* basic block level (control flow) optimizations */

for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
Expand Down Expand Up @@ -2190,6 +2263,10 @@ void optimize(void)

/* Phi node optimization - eliminate trivial phi nodes */
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
if (insn->opcode == OP_phi && insn->phi_ops) {
Expand Down Expand Up @@ -2242,6 +2319,10 @@ void optimize(void)

/* Mark useful instructions */
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
dce_insn(bb);
}
Expand Down Expand Up @@ -2286,6 +2367,10 @@ void build_reversed_rpo(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

func->bb_cnt = 0;
args->func = func;
args->bb = func->exit;
Expand Down Expand Up @@ -2521,6 +2606,10 @@ void liveness_analysis(void)
{
bb_traversal_args_t *args = arena_alloc_traversal_args();
for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

args->func = func;
args->bb = func->bbs;

Expand All @@ -2535,6 +2624,10 @@ void liveness_analysis(void)
}

for (func_t *func = FUNC_LIST.head; func; func = func->next) {
/* Skip function declarations without bodies */
if (!func->bbs)
continue;

basic_block_t *bb = func->exit;
bool changed;
do {
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/fib-arm.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshots/fib-riscv.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshots/hello-arm.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshots/hello-riscv.json

Large diffs are not rendered by default.

Loading