Skip to content

Commit 8729626

Browse files
committed
Implement dead code elimination
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, reducing the total by 27 instructions.
1 parent 562ec5f commit 8729626

File tree

3 files changed

+316
-3
lines changed

3 files changed

+316
-3
lines changed

src/defs.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
#define MAX_TYPE_LEN 32
1616
#define MAX_PARAMS 8
1717
#define MAX_LOCALS 1500
18-
#define MAX_FIELDS 32
18+
#define MAX_FIELDS 64
1919
#define MAX_FUNCS 512
2020
#define MAX_FUNC_TRIES 2160
2121
#define MAX_BLOCKS 2048
2222
#define MAX_TYPES 64
2323
#define MAX_IR_INSTR 40000
2424
#define MAX_BB_PRED 128
2525
#define MAX_BB_DOM_SUCC 64
26+
#define MAX_BB_RDOM_SUCC 256
2627
#define MAX_GLOBAL_IR 256
2728
#define MAX_LABEL 4096
2829
#define MAX_SOURCE 327680
@@ -173,6 +174,7 @@ struct var {
173174
int subscripts_idx;
174175
rename_t rename;
175176
ref_block_list_t ref_block_list; /* blocks which kill variable */
177+
struct insn *last_assign;
176178
int consumed;
177179
bool is_ternary_ret;
178180
bool is_log_and_ret;
@@ -308,6 +310,8 @@ struct insn {
308310
var_t *rs1;
309311
var_t *rs2;
310312
int sz;
313+
bool useful; /* Used in DCE process. Set true if instruction is useful. */
314+
basic_block_t *belong_to;
311315
phi_operand_t *phi_ops;
312316
char str[64];
313317
};
@@ -352,6 +356,7 @@ struct basic_block {
352356
struct basic_block *then_; /* conditional BB */
353357
struct basic_block *else_;
354358
struct basic_block *idom;
359+
struct basic_block *r_idom;
355360
struct basic_block *rpo_next;
356361
struct basic_block *rpo_r_next;
357362
var_t *live_gen[MAX_ANALYSIS_STACK_SIZE];
@@ -365,10 +370,15 @@ struct basic_block {
365370
int rpo;
366371
int rpo_r;
367372
struct basic_block *DF[64];
373+
struct basic_block *RDF[64];
368374
int df_idx;
375+
int rdf_idx;
369376
int visited;
377+
bool useful; /* indicate whether this BB contains useful instructions */
370378
struct basic_block *dom_next[64];
371379
struct basic_block *dom_prev;
380+
struct basic_block *rdom_next[256];
381+
struct basic_block *rdom_prev;
372382
fn_t *belong_to;
373383
block_t *scope;
374384
symbol_list_t symbol_list; /* variable declaration */

src/globals.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ void add_insn(block_t *block,
552552
n->rs1 = rs1;
553553
n->rs2 = rs2;
554554
n->sz = sz;
555+
n->belong_to = bb;
555556

556557
if (str)
557558
strcpy(n->str, str);

0 commit comments

Comments
 (0)