From c57b99fac5d6fc1f8d78f716e42b1df74b87a2a1 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Fri, 28 Oct 2022 04:45:55 +0200 Subject: [PATCH] :art: dead code elimination --- src/parse/scan1.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/parse/scan1.c b/src/parse/scan1.c index aad34f27..89ce3798 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -539,12 +539,29 @@ ANN static inline m_bool scan1_stmt(const Env env, const Stmt stmt) { return scan1_stmt_func[stmt->stmt_type](env, &stmt->d); } +ANN static inline bool end_flow(Stmt s) { + const ae_stmt_t t = s->stmt_type; + return t == ae_stmt_continue || + t == ae_stmt_break || + t == ae_stmt_return; +} + +ANN static void dead_code(const Env env, Stmt_List l, uint32_t len) { + for(uint32_t i = len; i < l->len; i++) { + const Stmt s = mp_vector_at(l, struct Stmt_, i); + free_stmt(env->gwion->mp, s); + } + l->len = len; +} + ANN static m_bool scan1_stmt_list(const Env env, Stmt_List l) { uint32_t i; for(i = 0; i < l->len; i++) { const Stmt s = mp_vector_at(l, struct Stmt_, i); CHECK_BB(scan1_stmt(env, s)); + if(end_flow(s)) break; } + if(++i < l->len) dead_code(env, l, i); return GW_OK; } -- 2.43.0