aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <[email protected]>2014-05-05 07:53:39 +0800
committerTeddy <[email protected]>2014-05-05 07:53:39 +0800
commitcd01997d0cc7cbdbcb8b68ddca877a29f29723a4 (patch)
tree294d527770f59e6dc0e6243c1e6e4d9e3580c33d
parentb10940f3570f7faad91b165df6b520a308a3b070 (diff)
consecutive short-circuit optoptimization
-rw-r--r--mips.c2
-rw-r--r--ssa.c60
2 files changed, 46 insertions, 16 deletions
diff --git a/mips.c b/mips.c
index 2ca88e6..9b08344 100644
--- a/mips.c
+++ b/mips.c
@@ -350,7 +350,7 @@ void mips_generate(void) {
int rt;
const char *b = i->op == BEQ ? "beq" : "bne";
if (i->src2->kind == IMM)
- printf("\t%s$%d, %d, _L%d\n", b, rs, i->src2->info.imm,
+ printf("\t%s $%d, %d, _L%d\n", b, rs, i->src2->info.imm,
i->dest->info.imm);
else
{
diff --git a/ssa.c b/ssa.c
index f8a8376..bab4ffe 100644
--- a/ssa.c
+++ b/ssa.c
@@ -191,7 +191,7 @@ void cinst_print(FILE *f, CInst_t inst) {
case BEQ:
fprintf(f, "if (");
copr_print(f, inst->src1);
- fprintf(f, " != ");
+ fprintf(f, " == ");
copr_print(f, inst->src2);
fprintf(f, ") goto _L");
copr_print(f, inst->dest);
@@ -647,11 +647,11 @@ COpr_t ssa_exp_(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) {/*{{{*/
CBlock_t else_h = cblock_create(1), else_t = else_h,
one_blk = cblock_create(1),
zero_blk = cblock_create(1),
- next_blk = cblock_create(1);
- COpr_t r0 = ssa_exp_(p->chd, cur, NULL, succ),
- r1 = ssa_exp_(p->chd->next, &else_t, NULL, succ);
+ next_blk = cblock_create(1), sblk;
+ COpr_t r0, r1, ri;
CInst_t b, a0, a1;
CPhi_t m = NEW(CPhi);
+ CNode *t;
/* constant opt */
a0 = cinst_create();
a0->op = MOVE;
@@ -684,10 +684,28 @@ COpr_t ssa_exp_(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) {/*{{{*/
m->oprs[1] = a1->dest;
cblock_pappend(next_blk, m);
- compress_branch(r0, *cur, 1)->dest->info.imm = zero_blk->id + gbbase;
+ r1 = ssa_exp_(p->chd->next, &else_t, NULL, succ);
compress_branch(r1, else_t, 1)->dest->info.imm = zero_blk->id + gbbase;
zero_blk->ref = 1;
+ sblk = else_h;
+ for (t = p->chd; t->rec.subtype == OPT_AND; t = t->chd)
+ {
+ CBlock_t c_h = cblock_create(1), c_t = c_h;
+ ri = ssa_exp_(t->chd->next, &c_t, NULL, succ);
+ compress_branch(ri, c_t, 1)->dest->info.imm = zero_blk->id + gbbase;
+ cfg_add_edge(c_t, zero_blk); /* tail */
+ DBLINK(c_t, sblk);
+ cfg_add_edge(c_t, sblk); /* connect to header */
+ sblk = c_h;
+ }
+
+ r0 = ssa_exp_(t, cur, NULL, succ);
+ compress_branch(r0, *cur, 1)->dest->info.imm = zero_blk->id + gbbase;
+ cfg_add_edge(*cur, zero_blk);
+ DBLINK(*cur, sblk);
+ cfg_add_edge(*cur, sblk);
+
b = cinst_create();
b->op = GOTO;
b->dest = copr_create();
@@ -696,13 +714,10 @@ COpr_t ssa_exp_(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) {/*{{{*/
cblock_append(one_blk, b);
next_blk->ref = 1;
- DBLINK(*cur, else_h);
DBLINK(else_t, one_blk);
DBLINK(one_blk, zero_blk);
DBLINK(zero_blk, next_blk);
- cfg_add_edge(*cur, else_h);
- cfg_add_edge(*cur, zero_blk);
cfg_add_edge(else_t, one_blk);
cfg_add_edge(else_t, zero_blk);
cfg_add_edge(one_blk, next_blk);
@@ -716,11 +731,11 @@ COpr_t ssa_exp_(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) {/*{{{*/
CBlock_t else_h = cblock_create(1), else_t = else_h,
one_blk = cblock_create(1),
zero_blk = cblock_create(1),
- next_blk = cblock_create(1);
- COpr_t r0 = ssa_exp_(p->chd, cur, NULL, succ),
- r1 = ssa_exp_(p->chd->next, &else_t, NULL, succ);
+ next_blk = cblock_create(1), sblk;
+ COpr_t r0, r1, ri;
CInst_t b, a0, a1;
CPhi_t m = NEW(CPhi);
+ CNode *t;
/* constant opt */
a0 = cinst_create();
a0->op = MOVE;
@@ -753,10 +768,28 @@ COpr_t ssa_exp_(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) {/*{{{*/
m->oprs[1] = a0->dest;
cblock_pappend(next_blk, m);
- compress_branch(r0, *cur, 0)->dest->info.imm = one_blk->id + gbbase;
+ r1 = ssa_exp_(p->chd->next, &else_t, NULL, succ);
compress_branch(r1, else_t, 0)->dest->info.imm = one_blk->id + gbbase;
one_blk->ref = 1;
+ sblk = else_h;
+ for (t = p->chd; t->rec.subtype == OPT_OR; t = t->chd)
+ {
+ CBlock_t c_h = cblock_create(1), c_t = c_h;
+ ri = ssa_exp_(t->chd->next, &c_t, NULL, succ);
+ compress_branch(ri, c_t, 0)->dest->info.imm = one_blk->id + gbbase;
+ cfg_add_edge(c_t, one_blk); /* tail */
+ DBLINK(c_t, sblk);
+ cfg_add_edge(c_t, sblk); /* connect to header */
+ sblk = c_h;
+ }
+
+ r0 = ssa_exp_(t, cur, NULL, succ);
+ compress_branch(r0, *cur, 0)->dest->info.imm = one_blk->id + gbbase;
+ cfg_add_edge(*cur, one_blk);
+ DBLINK(*cur, sblk);
+ cfg_add_edge(*cur, sblk);
+
b = cinst_create();
b->op = GOTO;
b->dest = copr_create();
@@ -765,13 +798,10 @@ COpr_t ssa_exp_(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) {/*{{{*/
cblock_append(zero_blk, b);
next_blk->ref = 1;
- DBLINK(*cur, else_h);
DBLINK(else_t, zero_blk);
DBLINK(zero_blk, one_blk);
DBLINK(one_blk, next_blk);
- cfg_add_edge(*cur, else_h);
- cfg_add_edge(*cur, one_blk);
cfg_add_edge(else_t, zero_blk);
cfg_add_edge(else_t, one_blk);
cfg_add_edge(zero_blk, next_blk);