Skip to content

Commit 44b5746

Browse files
authored
Merge pull request #263 from sysprog21/improve-liveness
Improve liveness analysis by combining traversal passes
2 parents 73bfbf4 + 385fe3b commit 44b5746

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/ssa.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,37 @@ void bb_reset_live_kill_idx(func_t *func, basic_block_t *bb)
19371937
bb->live_kill_idx = 0;
19381938
}
19391939

1940+
void add_live_gen(basic_block_t *bb, var_t *var);
1941+
void update_consumed(insn_t *insn, var_t *var);
1942+
1943+
/* Combined function to reset and solve locals in one pass */
1944+
void bb_reset_and_solve_locals(func_t *func, basic_block_t *bb)
1945+
{
1946+
UNUSED(func);
1947+
1948+
/* Reset live_kill index */
1949+
bb->live_kill_idx = 0;
1950+
1951+
/* Solve locals */
1952+
int i = 0;
1953+
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
1954+
insn->idx = i++;
1955+
1956+
if (insn->rs1) {
1957+
if (!var_check_killed(insn->rs1, bb))
1958+
add_live_gen(bb, insn->rs1);
1959+
update_consumed(insn, insn->rs1);
1960+
}
1961+
if (insn->rs2) {
1962+
if (!var_check_killed(insn->rs2, bb))
1963+
add_live_gen(bb, insn->rs2);
1964+
update_consumed(insn, insn->rs2);
1965+
}
1966+
if (insn->rd && insn->opcode != OP_unwound_phi)
1967+
bb_add_killed_var(bb, insn->rd);
1968+
}
1969+
}
1970+
19401971
void add_live_gen(basic_block_t *bb, var_t *var)
19411972
{
19421973
if (var->is_global)
@@ -2120,16 +2151,14 @@ void liveness_analysis(void)
21202151
args->func = func;
21212152
args->bb = func->bbs;
21222153

2154+
/* Combined traversal: reset and solve locals in one pass */
21232155
func->visited++;
2124-
args->preorder_cb = bb_reset_live_kill_idx;
2156+
args->preorder_cb = bb_reset_and_solve_locals;
21252157
bb_forward_traversal(args);
21262158

2159+
/* Add function parameters as killed in entry block */
21272160
for (int i = 0; i < func->num_params; i++)
21282161
bb_add_killed_var(func->bbs, func->param_defs[i].subscripts[0]);
2129-
2130-
func->visited++;
2131-
args->preorder_cb = bb_solve_locals;
2132-
bb_forward_traversal(args);
21332162
}
21342163

21352164
for (func_t *func = FUNC_LIST.head; func; func = func->next) {

0 commit comments

Comments
 (0)