diff --git a/src/SysYFIRBuilder/IRBuilder.cpp b/src/SysYFIRBuilder/IRBuilder.cpp index 0cf482d..149a1e8 100644 --- a/src/SysYFIRBuilder/IRBuilder.cpp +++ b/src/SysYFIRBuilder/IRBuilder.cpp @@ -638,11 +638,13 @@ void IRBuilder::visit(SyntaxTree::UnaryCondExpr &node) { // FINISH void IRBuilder::visit(SyntaxTree::BinaryCondExpr &node) { auto curFunc = builder->get_insert_block()->get_parent(); - auto trueBB = BasicBlock::create(module, "trueBB_and", curFunc); - auto falseBB = BasicBlock::create(module, "falseBB_and", curFunc); + Ptr trueBB; + Ptr falseBB; Ptr cond; switch (node.op) { case SyntaxTree::BinaryCondOp::LAND: + trueBB = BasicBlock::create(module, "trueBB_and", curFunc); + falseBB = BasicBlock::create(module, "falseBB_and", curFunc); node.lhs->accept(*this); TypeConvert(tmpInst, INT1_T); builder->create_cond_br(tmpInst, trueBB, falseBB); @@ -658,7 +660,8 @@ void IRBuilder::visit(SyntaxTree::BinaryCondExpr &node) { break; case SyntaxTree::BinaryCondOp::LOR: - + trueBB = BasicBlock::create(module, "trueBB_and", curFunc); + falseBB = BasicBlock::create(module, "falseBB_and", curFunc); node.lhs->accept(*this); TypeConvert(tmpInst, INT1_T); builder->create_cond_br(tmpInst, trueBB, falseBB); @@ -738,7 +741,7 @@ void IRBuilder::visit(SyntaxTree::IfStmt &node) { auto curFunc = builder->get_insert_block()->get_parent(); auto trueBB = BasicBlock::create(module, "trueBB_if", curFunc); auto falseBB = BasicBlock::create(module, "falseBB_if", curFunc); - auto exitBB = node.else_statement == nullptr ? falseBB : BasicBlock::create(module, "exitBB_if", curFunc); + auto afterBB = node.else_statement == nullptr ? falseBB : BasicBlock::create(module, "afterBB_if", curFunc); node.cond_exp->accept(*this); @@ -747,15 +750,16 @@ void IRBuilder::visit(SyntaxTree::IfStmt &node) { builder->set_insert_point(trueBB); node.if_statement->accept(*this); - builder->create_br(exitBB); + if (builder->get_insert_block()->get_terminator() == nullptr) + builder->create_br(afterBB); if (node.else_statement) { builder->set_insert_point(falseBB); node.else_statement->accept(*this); - builder->create_br(exitBB); + builder->create_br(afterBB); } - if (!exitBB->get_pre_basic_blocks().empty()) { - builder->set_insert_point(exitBB); + if (!afterBB->get_pre_basic_blocks().empty()) { + builder->set_insert_point(afterBB); } }