From bccad350df1a832c5008eef67b641031be8ac3c7 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 22 Feb 2004 10:59:46 -0700 Subject: [PATCH] Clean up linearization, and make the basic blocks be true basic blocks (with all exits at the bottom). This will simplify some of the analysis. --- linearize.c | 32 ++++++++++++++++++++++---------- linearize.h | 8 ++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/linearize.c b/linearize.c index 5a067486..8e947afb 100644 --- a/linearize.c +++ b/linearize.c @@ -85,11 +85,6 @@ static struct basic_block * new_basic_block(struct entrypoint *ep, struct symbol return bb; } -static void linearize_simple_statement(struct entrypoint *ep, struct statement *stmt) -{ - add_statement(&ep->active->stmts, stmt); -} - static void add_label(struct entrypoint *ep, struct symbol *sym) { struct basic_block *new_bb = new_basic_block(ep, sym); @@ -98,6 +93,19 @@ static void add_label(struct entrypoint *ep, struct symbol *sym) ep->active = new_bb; } +static void linearize_simple_statement(struct entrypoint *ep, struct statement *stmt) +{ + struct basic_block *bb = ep->active; + + if (bb_reachable(bb)) { + if (bb->flags & BB_HASBRANCH) { + add_label(ep, alloc_symbol(stmt->pos, SYM_LABEL)); + bb = ep->active; + } + add_statement(&bb->stmts, stmt); + } +} + static void set_unreachable(struct entrypoint *ep) { ep->active = new_basic_block(ep, NULL); @@ -105,10 +113,15 @@ static void set_unreachable(struct entrypoint *ep) static void add_branch(struct entrypoint *ep, struct statement *stmt, int true, struct expression *cond, struct symbol *target) { - struct statement *jump = alloc_statement(stmt->pos, true); - jump->bb_conditional = cond; - jump->bb_target = target; - linearize_simple_statement(ep, jump); + struct basic_block *bb = ep->active; + + if (bb_reachable(bb)) { + struct statement *jump = alloc_statement(stmt->pos, true); + jump->bb_conditional = cond; + jump->bb_target = target; + bb->flags |= BB_HASBRANCH; + add_statement(&bb->stmts, jump); + } } void linearize_statement(struct entrypoint *ep, struct statement *stmt) @@ -190,7 +203,6 @@ void linearize_statement(struct entrypoint *ep, struct statement *stmt) struct symbol *merge = alloc_symbol(never->pos, SYM_LABEL); add_label(ep, merge); bb->next = merge; - ep->active = new_basic_block(ep, merge); break; } diff --git a/linearize.h b/linearize.h index fdd135a4..7fc2b0d1 100644 --- a/linearize.h +++ b/linearize.h @@ -8,7 +8,15 @@ struct basic_block_list; +/* + * Basic block flags. Right now we only have one, which keeps + * track (at build time) whether the basic block has been branched + * out of yet. + */ +#define BB_HASBRANCH 0x00000001 + struct basic_block { + unsigned long flags; /* BB status flags */ struct symbol *this; /* Points to the symbol that owns "this" basic block - NULL if unreachable */ struct statement_list *stmts; /* Linear list of statements */ struct symbol *next; /* Points to the symbol that describes the fallthrough */ -- 2.11.4.GIT