Skip to content

Commit 2b9ad5d

Browse files
authored
Merge pull request #145 from nosba0957/master
Implement dead code elimination
2 parents 4b8c4e6 + 8729626 commit 2b9ad5d

File tree

4 files changed

+318
-3
lines changed

4 files changed

+318
-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);

src/parser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,8 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
26082608
if (body_) {
26092609
bb_connect(body_, inc_, NEXT);
26102610
bb_connect(inc_, cond_start, NEXT);
2611+
} else if (inc_->insn_list.head) {
2612+
bb_connect(inc_, cond_start, NEXT);
26112613
} else {
26122614
/* TODO: Release dangling inc basic block */
26132615
}

0 commit comments

Comments
 (0)