Skip to content

Commit

Permalink
Implement dead code elimination
Browse files Browse the repository at this point in the history
Introduces the creaetion of reverse dominance frontier (RDF) to support
dead code elimination (DCE). Method for implementing RDF is similar to
that of dominance frontier (DF). The key difference is that RDF is
computed on the reverse CFG. In other words, operations were performed
on "prev[]" which in the basic block structure, now switched to operate
on "next", "then_" and "else_".

In the "dce_insn" function, mark useful instructions during the initial
analysis of the current basic block. Continue identifying useful
instructions by tracing back from the last assigned instruction of the
both operands of the current "insn". In the "dce_sweep" function remove
the useless instruction from the current "insn_list". If a branch
instruction is encountered, remove it and reconnect the current basic
block to its reverse immediate dominator.

Before implementing DCE, compiling "src/main.c" resulted in an
executable with 51,357 instructions for ARMv7-A. After DCE, the
executable was 51,330 instructions. DCE reduced the executable by 27
instructions.
  • Loading branch information
nosba0957 committed Aug 10, 2024
1 parent c84d25e commit e9300b1
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
#define MAX_TYPE_LEN 32
#define MAX_PARAMS 8
#define MAX_LOCALS 1500
#define MAX_FIELDS 32
#define MAX_FIELDS 64
#define MAX_FUNCS 512
#define MAX_FUNC_TRIES 2160
#define MAX_BLOCKS 2048
#define MAX_TYPES 64
#define MAX_IR_INSTR 40000
#define MAX_BB_PRED 128
#define MAX_BB_DOM_SUCC 64
#define MAX_BB_RDOM_SUCC 256
#define MAX_GLOBAL_IR 256
#define MAX_LABEL 4096
#define MAX_SOURCE 327680
Expand Down Expand Up @@ -173,6 +174,7 @@ struct var {
int subscripts_idx;
rename_t rename;
ref_block_list_t ref_block_list; /* blocks which kill variable */
struct insn *last_assign;
int consumed;
bool is_ternary_ret;
bool is_log_and_ret;
Expand Down Expand Up @@ -308,6 +310,8 @@ struct insn {
var_t *rs1;
var_t *rs2;
int sz;
bool useful; /* Used in DCE process. Set true if instruction is useful. */
basic_block_t *belong_to;
phi_operand_t *phi_ops;
char str[64];
};
Expand Down Expand Up @@ -352,6 +356,7 @@ struct basic_block {
struct basic_block *then_; /* conditional BB */
struct basic_block *else_;
struct basic_block *idom;
struct basic_block *r_idom;
struct basic_block *rpo_next;
struct basic_block *rpo_r_next;
var_t *live_gen[MAX_ANALYSIS_STACK_SIZE];
Expand All @@ -365,10 +370,15 @@ struct basic_block {
int rpo;
int rpo_r;
struct basic_block *DF[64];
struct basic_block *RDF[64];
int df_idx;
int rdf_idx;
int visited;
bool useful; /* indicate whether this BB contains useful instructions */
struct basic_block *dom_next[64];
struct basic_block *dom_prev;
struct basic_block *rdom_next[256];
struct basic_block *rdom_prev;
fn_t *belong_to;
block_t *scope;
symbol_list_t symbol_list; /* variable declaration */
Expand Down
1 change: 1 addition & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ void add_insn(block_t *block,
n->rs1 = rs1;
n->rs2 = rs2;
n->sz = sz;
n->belong_to = bb;

if (str)
strcpy(n->str, str);
Expand Down
Loading

0 comments on commit e9300b1

Please sign in to comment.