From 234bad3c4713b9795b9ec21b278fca78fcf65a1c Mon Sep 17 00:00:00 2001 From: Teddy Date: Mon, 5 May 2014 06:10:29 +0800 Subject: sne seq opt --- mips.c | 20 ++++++----- ssa.c | 117 +++++++++++++++++++++++++++-------------------------------------- ssa.h | 4 +-- 3 files changed, 63 insertions(+), 78 deletions(-) diff --git a/mips.c b/mips.c index e4039ee..3c5179d 100644 --- a/mips.c +++ b/mips.c @@ -345,16 +345,20 @@ void mips_generate(void) { } } break; - case BEQZ: + case BEQ: + case BNE: { int rs = mips_to_reg(i->src1, reg_v0); - printf("\tbeqz $%d, _L%d\n", rs, i->dest->info.imm); - } - break; - case BNEZ: - { - int rs = mips_to_reg(i->src1, reg_v0); - printf("\tbnez $%d, _L%d\n", rs, i->dest->info.imm); + 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, + i->dest->info.imm); + else + { + rt = mips_to_reg(i->src2, reg_v1); + printf("\t%s$%d, $%d, _L%d\n", b, rs, rt, i->dest->info.imm); + } } break; case GOTO: diff --git a/ssa.c b/ssa.c index dbe36e5..02805d3 100644 --- a/ssa.c +++ b/ssa.c @@ -489,6 +489,39 @@ COpr_t ssa_postfix(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) { return base->dest; } +CInst_t compress_branch(COpr_t r, CBlock_t blk, int rev) { + int flag = -1; + CInst_t b; + if (r->kind == TMP) + { + b = cblock_getback(blk); + if (b) + { + assert(r == b->dest); + if (b->op == EQ) + flag = 0; + else if (b->op == NE) + flag = 1; + } + } + if (flag != -1) + b->op = flag ? BNE : BEQ; + else + { + b = cinst_create(); + b->op = BNE; + b->src1 = r; + b->src2 = copr_create(); + b->src2->kind = IMM; + b->src2->info.imm = 0; + cblock_append(blk, b); + } + b->op ^= rev; + b->dest = copr_create(); + b->dest->kind = IMM; + return b; +} + #define IS_PTR(tt) ((tt) == CPTR || (tt) == CARR) COpr_t ssa_exp(CNode *, CBlock_t *, int); COpr_t ssa_exp_(CNode *p, CBlock_t *cur, CInst_t lval, CBlock_t succ) {/*{{{*/ @@ -651,27 +684,8 @@ 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); - b = cinst_create(); - b->op = BEQ; - b->src1 = r0; - b->src2 = copr_create(); - b->src2->kind = IMM; - b->src2->info.imm = 0; - b->dest = copr_create(); - b->dest->kind = IMM; - b->dest->info.imm = zero_blk->id + gbbase; - cblock_append(*cur, b); - - b = cinst_create(); - b->op = BEQ; - b->src1 = r1; - b->src2 = copr_create(); - b->src2->kind = IMM; - b->src2->info.imm = 0; - b->dest = copr_create(); - b->dest->kind = IMM; - b->dest->info.imm = zero_blk->id + gbbase; - cblock_append(else_t, b); + compress_branch(r0, *cur, 1)->dest->info.imm = zero_blk->id + gbbase; + compress_branch(r1, else_t, 1)->dest->info.imm = zero_blk->id + gbbase; zero_blk->ref = 1; b = cinst_create(); @@ -739,27 +753,8 @@ 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); - b = cinst_create(); - b->op = BNE; - b->src1 = r0; - b->src2 = copr_create(); - b->src2->kind = IMM; - b->src2->info.imm = 0; - b->dest = copr_create(); - b->dest->kind = IMM; - b->dest->info.imm = one_blk->id + gbbase; - cblock_append(*cur, b); - - b = cinst_create(); - b->op = BNE; - b->src1 = r1; - b->src2 = copr_create(); - b->src2->kind = IMM; - b->src2->info.imm = 0; - b->dest = copr_create(); - b->dest->kind = IMM; - b->dest->info.imm = one_blk->id + gbbase; - cblock_append(else_t, b); + compress_branch(r0, *cur, 0)->dest->info.imm = one_blk->id + gbbase; + compress_branch(r1, else_t, 0)->dest->info.imm = one_blk->id + gbbase; one_blk->ref = 1; b = cinst_create(); @@ -1081,19 +1076,10 @@ CBlock_t ssa_while(CNode *p, CBlock_t cur) {/*{{{*/ CBlock_t loop_h = cblock_create(1), loop_t, cond_h= cblock_create(1), cond_t = cond_h, next_blk = cblock_create(1); - CInst_t j_inst = cinst_create(), - if_inst = cinst_create(); - - if_inst->op = BNE; - if_inst->src2 = copr_create(); - if_inst->src2->kind = IMM; - if_inst->src2->info.imm = 0; - if_inst->src1 = ssa_exp(exp, &cond_t, 0); - if_inst->dest = copr_create(); - if_inst->dest->kind = IMM; - if_inst->dest->info.imm = loop_h->id + gbbase; + CInst_t j_inst = cinst_create(); + COpr_t e = ssa_exp(exp, &cond_t, 0); + compress_branch(e, cond_t, 0)->dest->info.imm = loop_h->id + gbbase; loop_h->ref = 1; - cblock_append(cond_t, if_inst); DBLINK(cond_t, next_blk); loop_t = ssa_stmt(exp->next, loop_h, next_blk); @@ -1123,19 +1109,10 @@ CBlock_t ssa_for(CNode *p, CBlock_t cur) {/*{{{*/ CBlock_t loop_h = cblock_create(1), loop_t, cond_h = cblock_create(1), cond_t = cond_h, next_blk = cblock_create(1); - CInst_t j_inst = cinst_create(), - if_inst = cinst_create(); - - if_inst->op = BNE; - if_inst->src1 = ssa_exp(exp2, &cond_t, 0); - if_inst->src2 = copr_create(); - if_inst->src2->kind = IMM; - if_inst->src2->info.imm = 0; - if_inst->dest = copr_create(); - if_inst->dest->kind = IMM; - if_inst->dest->info.imm = loop_h->id + gbbase; + CInst_t j_inst = cinst_create(); + COpr_t e = ssa_exp(exp2, &cond_t, 0); + compress_branch(e, cond_t, 0)->dest->info.imm = loop_h->id + gbbase; loop_h->ref = 1; - cblock_append(cond_t, if_inst); DBLINK(cond_t, next_blk); loop_t = ssa_stmt(exp3->next, loop_h, next_blk); @@ -1166,7 +1143,7 @@ CBlock_t ssa_if(CNode *p, CBlock_t cur, CBlock_t loop_exit) {/*{{{*/ *body2 = body1->next; CBlock_t then_blk, then_t, next_blk, else_blk, else_t; - CInst_t if_inst = cinst_create(); + CInst_t if_inst; /* = cinst_create(); */ COpr_t rt = ssa_exp(p->chd, &cur, 0); if (rt->kind == IMM) { @@ -1178,14 +1155,18 @@ CBlock_t ssa_if(CNode *p, CBlock_t cur, CBlock_t loop_exit) {/*{{{*/ return cur; } then_blk = cblock_create(1); + if_inst = compress_branch(rt, cur, 1); +/* calculated cond */ + /* if_inst->op = BEQ; - if_inst->src1 = rt; /* calculated cond */ + if_inst->src1 = rt; if_inst->src2 = copr_create(); if_inst->src2->kind = IMM; if_inst->src2->info.imm = 0; if_inst->dest = copr_create(); if_inst->dest->kind = IMM; cblock_append(cur, if_inst); + */ cfg_add_edge(cur, then_blk); DBLINK(cur, then_blk); diff --git a/ssa.h b/ssa.h index 81857c7..307328b 100644 --- a/ssa.h +++ b/ssa.h @@ -54,8 +54,8 @@ void colist_remove(COList_t node); struct CInst { enum OpCode { - BEQ, /* conditional jump */ - BNE, + BEQ = 0, /* conditional jump */ + BNE = 1, GOTO, /* unconditional jump */ ARR, /* displacement */ PUSH, /* push to stack top */ -- cgit v1.2.3