From c54e751d4a5b5617cbf59e74fceef408e7972022 Mon Sep 17 00:00:00 2001 From: wyh <1002682355@qq.com> Date: Tue, 11 Nov 2025 12:28:27 +0800 Subject: [PATCH] lab3 publish --- include/lightir/BasicBlock.hpp | 5 ++++- src/lightir/Instruction.cpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/lightir/BasicBlock.hpp b/include/lightir/BasicBlock.hpp index 01d07d8..bbd1c75 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 693655e..c9e4b7a 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; } -- GitLab