diff --git a/include/lightir/BasicBlock.hpp b/include/lightir/BasicBlock.hpp index 01d07d825981458c5a001e8f49575e60570eeabc..bbd1c758fe5e3db2ba9c3ba13aff290245c224d1 100755 --- a/include/lightir/BasicBlock.hpp +++ b/include/lightir/BasicBlock.hpp @@ -30,7 +30,7 @@ class BasicBlock : public Value, public llvm::ilist_node { void add_succ_basic_block(BasicBlock *bb) { succ_bbs_.push_back(bb); } void remove_pre_basic_block(BasicBlock *bb) { pre_bbs_.remove(bb); } void remove_succ_basic_block(BasicBlock *bb) { succ_bbs_.remove(bb); } - BasicBlock* get_entry_block_of_same_function(); + BasicBlock* get_entry_block_of_same_function(); // If the Block is terminated by ret/br bool is_terminated() const; @@ -40,6 +40,9 @@ class BasicBlock : public Value, public llvm::ilist_node { /****************api about Instruction****************/ void add_instruction(Instruction *instr); void add_instr_begin(Instruction *instr) { instr_list_.push_front(instr); } + void add_instr_before_end(Instruction *instr) { + instr_list_.insert(std::prev(instr_list_.end()), instr); + } void erase_instr(Instruction *instr) { instr_list_.erase(instr); } void remove_instr(Instruction *instr) { instr_list_.remove(instr); } diff --git a/src/lightir/Instruction.cpp b/src/lightir/Instruction.cpp index 693655e3ac2852530d22c5052fbc5f83d4bc08e3..c9e4b7a8d010307ed609869dacee223a46cc41d8 100755 --- a/src/lightir/Instruction.cpp +++ b/src/lightir/Instruction.cpp @@ -303,7 +303,10 @@ AllocaInst *AllocaInst::create_alloca_begin(Type *ty, BasicBlock *bb) { if(bb != nullptr) { ret->set_parent(bb); - bb->add_instr_begin(ret); + if (bb->is_terminated()) + bb->add_instr_before_end(ret); + else + bb->add_instruction(ret); } return ret; }