Skip to content

Commit 1ea0f4f

Browse files
authored
Merge pull request #23 from feat/deadcode
Feat(optimizer): Otimização de eliminação de deadcode após return
2 parents 41f52da + 24c4bdb commit 1ea0f4f

File tree

7 files changed

+43
-4
lines changed

7 files changed

+43
-4
lines changed

src/ast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void ensure_capacity(void **buffer, size_t elem_size, size_t *capacity, s
3737
}
3838

3939
static void ast_expr_destroy(AstExpr *expr);
40-
static void ast_stmt_destroy(AstStmt *stmt);
40+
void ast_stmt_destroy(AstStmt *stmt);
4141

4242
AstProgram *ast_program_create(void)
4343
{
@@ -229,7 +229,7 @@ void ast_stmt_list_destroy(AstStmtList *list)
229229
list->capacity = 0;
230230
}
231231

232-
static void ast_stmt_destroy(AstStmt *stmt)
232+
void ast_stmt_destroy(AstStmt *stmt)
233233
{
234234
if (!stmt)
235235
{

src/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ void ast_expr_list_destroy(AstExprList *list);
223223
AstStmtList ast_stmt_list_make(void);
224224
void ast_stmt_list_push(AstStmtList *list, AstStmt *stmt);
225225
void ast_stmt_list_destroy(AstStmtList *list);
226+
void ast_stmt_destroy(AstStmt *stmt);
226227

227228
AstBlock ast_block_from_list(AstStmtList *list);
228229

src/optimizer.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static char *dup_string(const char *value);
5151
static void *xmalloc(size_t size);
5252
static int compare_entries_by_index(const void *lhs, const void *rhs);
5353
static int occurrence_survives_dce(const CseOccurrence *occ);
54+
static void eliminate_unreachable(AstStmtList *list);
5455

5556
void optimize_program(AstProgram *program)
5657
{
@@ -89,6 +90,7 @@ static void optimize_block(AstBlock *block)
8990
{
9091
optimize_statement_children(block->statements.items[i]);
9192
}
93+
eliminate_unreachable(&block->statements);
9294
}
9395

9496
static void optimize_statement_children(AstStmt *stmt)
@@ -608,3 +610,29 @@ static char *expr_make_key(const AstExpr *expr)
608610
return dup_string("<unsupported>");
609611
}
610612
}
613+
614+
static void eliminate_unreachable(AstStmtList *list)
615+
{
616+
if (!list)
617+
return;
618+
619+
int after_return = 0;
620+
size_t write = 0;
621+
622+
for (size_t i = 0; i < list->count; i++)
623+
{
624+
AstStmt *s = list->items[i];
625+
if (after_return)
626+
{
627+
ast_stmt_destroy(s);
628+
continue;
629+
}
630+
list->items[write++] = s;
631+
if (s->kind == STMT_RETURN)
632+
{
633+
after_return = 1;
634+
}
635+
}
636+
637+
list->count = write;
638+
}

tests/pass/deadcode.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int main()
2+
{
3+
int a = 10;
4+
return a;
5+
int b = 20;
6+
}

tests/pass/deadcode.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
os.exit((function(args)
2+
local a = 10
3+
return a
4+
end)(arg))

tests/pass/expressions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ int main(int K)
1818
int a = 10;
1919
int b = 20;
2020
int r = sum(a, b) + add(a, b);
21-
return 0;
21+
return r;
2222
}

tests/pass/expressions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ os.exit((function(args)
1313
local a = 10
1414
local b = 20
1515
local r = (sum(a, b) + add(a, b))
16-
return 0
16+
return r
1717
end)(arg))

0 commit comments

Comments
 (0)