优化二元运算add,sub,mul

main
wqz 3 years ago
parent fcc8d47c1f
commit 66bb92bfb0

@ -945,6 +945,7 @@ namespace sysy
{
// move(exit_blocks.back());
blocks.emplace_back(move(exit_blocks.back()));
exit_blocks.pop_back();
}
void removeBasicBlock(BasicBlock *block)
{

@ -312,7 +312,6 @@ namespace sysy
builder.push_truetarget(thenblock);
builder.push_falsetarget(exitblock);
auto cond = any_cast<Value *>(ctx->exp()->accept(this));
func->moveExitBlock();
char flagname[20];
sprintf(flagname, "flag%d", builder.get_ifcnt() + builder.get_whilecnt());
// cond->setName(flagname);
@ -325,6 +324,7 @@ namespace sysy
Value *then_br = builder.createUncondBrInst(exitblock, vector<Value *>());
// setup the instruction insert position
builder.setPosition(exitblock, exitblock->begin());
func->moveExitBlock();
}
if (ctx->stmt().size() == 2)
{
@ -348,7 +348,6 @@ namespace sysy
builder.push_truetarget(thenblock);
builder.push_falsetarget(elseblock);
auto cond = any_cast<Value *>(ctx->exp()->accept(this));
func->moveExitBlock();
CondBrInst *CondBr = builder.createCondBrInst(cond, builder.get_truetarget(), builder.get_falsetarget(), vector<Value *>(), vector<Value *>());
builder.poptarget();
builder.setPosition(thenblock, thenblock->begin());
@ -359,6 +358,7 @@ namespace sysy
Value *else_br = builder.createUncondBrInst(exitblock, vector<Value *>());
// setup the instruction insert position
builder.setPosition(exitblock, exitblock->begin());
func->moveExitBlock();
}
return builder.getBasicBlock();
}
@ -396,7 +396,6 @@ namespace sysy
builder.push_truetarget(bodyblock);
builder.push_falsetarget(exitblock);
auto cond = any_cast<Value *>(ctx->exp()->accept(this));
func->moveExitBlock();
// char flagname[20];
// sprintf(flagname, "flag%d", builder.get_ifcnt() + builder.get_whilecnt());
// cond->setName(flagname);
@ -412,6 +411,7 @@ namespace sysy
Value *body_uncondbr = builder.createUncondBrInst(headerblock, vector<Value *>());
// setup the instruction insert position
builder.setPosition(exitblock, exitblock->begin());
func->moveExitBlock();
return builder.getBasicBlock();
}
any SysYIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext *ctx)

@ -242,28 +242,97 @@ namespace backend
*/
string lname, rname;
auto lhs = bInst->getLhs();
bool lconst = false, rconst = false;
if (isa<ConstantValue>(lhs))
{
lname = "#" + to_string(dynamic_cast<ConstantValue *>(lhs)->getInt());
lconst = true;
}
else if (isa<CallInst>(lhs))
lname = "r0";
else
lname = "r" + lhs->getName();
auto rhs = bInst->getRhs();
if (isa<ConstantValue>(rhs))
{
rname = "#" + to_string(dynamic_cast<ConstantValue *>(rhs)->getInt());
rconst = true;
}
else if (isa<CallInst>(lhs))
lname = "r0";
else
rname = "r" + rhs->getName();
auto res = stoi(bInst->getName());
auto res = bInst->getName();
if (bInst->getKind() == Instruction::kAdd)
code += space + "add\tr" + to_string(res) + ", " + lname + ", " + rname + endl;
{
if (lconst && rconst)
{
int val = dynamic_cast<ConstantValue *>(lhs)->getInt() + dynamic_cast<ConstantValue *>(rhs)->getInt();
if (val >= 0)
code += space + "mov\tr" + res + ", #" + to_string(val) + endl;
else
{
code += space + "mov\tr" + res + ", #" + to_string(-val) + endl;
code += space + "mvn\tr" + res + ", #1" + endl;
}
}
else if (lconst)
{
code += space + "mov\tr" + res + ", " + lname + endl;
code += space + "add\tr" + res + ", r" + res + ", " + rname + endl;
}
else
code += space + "add\tr" + res + ", " + lname + ", " + rname + endl;
}
else if (bInst->getKind() == Instruction::kSub)
code += space + "sub\tr" + to_string(res) + ", " + lname + ", " + rname + endl;
{
if (lconst && rconst)
{
int val = dynamic_cast<ConstantValue *>(lhs)->getInt() - dynamic_cast<ConstantValue *>(rhs)->getInt();
if (val >= 0)
code += space + "mov\tr" + res + ", #" + to_string(val) + endl;
else
{
code += space + "mov\tr" + res + ", #" + to_string(-val) + endl;
code += space + "mvn\tr" + res + ", #1" + endl;
}
}
else if (lconst)
{
code += space + "mov\tr" + res + ", " + lname + endl;
code += space + "sub\tr" + res + ", r" + res + ", " + rname + endl;
}
else
code += space + "sub\tr" + res + ", " + lname + ", " + rname + endl;
}
else if (bInst->getKind() == Instruction::kMul)
code += space + "mul\tr" + to_string(res) + ", " + lname + ", " + rname + endl;
{
if (lconst && rconst)
{
int val = dynamic_cast<ConstantValue *>(lhs)->getInt() * dynamic_cast<ConstantValue *>(rhs)->getInt();
if (val >= 0)
code += space + "mov\tr" + res + ", #" + to_string(val) + endl;
else
{
code += space + "mov\tr" + res + ", #" + to_string(-val) + endl;
code += space + "mvn\tr" + res + ", #1" + endl;
}
}
else if (lconst)
{
code += space + "mov\tr" + res + ", " + lname + endl;
code += space + "mul\tr" + res + ", r" + res + ", " + rname + endl;
}
else if (rconst)
{
code += space + "mov\tr" + res + ", " + rname + endl;
code += space + "mul\tr" + res + ", " + lname + ", r" + res + endl;
}
else
code += space + "mul\tr" + res + ", " + lname + ", " + rname + endl;
}
else if (bInst->getKind() == Instruction::kDiv)
code += space + "div\tr" + to_string(res) + ", " + lname + ", " + rname + endl;
code += space + "div\tr" + res + ", " + lname + ", " + rname + endl;
else if (bInst->getKind() == Instruction::kICmpEQ)
code += space + "cmp\t" + lname + ", " + rname + endl;
else if (bInst->getKind() == Instruction::kICmpGE)

Loading…
Cancel
Save