diff --git a/src/include/midend/IR.h b/src/include/midend/IR.h index 901a3b7..91d7f72 100644 --- a/src/include/midend/IR.h +++ b/src/include/midend/IR.h @@ -695,15 +695,13 @@ class Instruction : public User { kCondBr = 0x1UL << 30, kBr = 0x1UL << 31, kReturn = 0x1UL << 32, + kUnreachable = 0x1UL << 32, ///< Unreachable instruction, used for optimization purposes // mem op - kAlloca = 0x1UL << 33, - kLoad = 0x1UL << 34, - kStore = 0x1UL << 35, - kGetElementPtr = 0x1UL << 36, - kMemset = 0x1UL << 37, - // kGetSubArray = 0x1UL << 38, - // Constant Kind removed as Constants are now Values, not Instructions. - // kConstant = 0x1UL << 37, // Conflicts with kMemset if kept as is + kAlloca = 0x1UL << 34, + kLoad = 0x1UL << 35, + kStore = 0x1UL << 36, + kGetElementPtr = 0x1UL << 37, + kMemset = 0x1UL << 38, // phi kPhi = 0x1UL << 39, kBitItoF = 0x1UL << 40, @@ -852,6 +850,7 @@ public: } bool isUnconditional() const { return kind == kBr; } bool isConditional() const { return kind == kCondBr; } + bool isCondBr() const { return kind == kCondBr; } bool isPhi() const { return kind == kPhi; } bool isAlloca() const { return kind == kAlloca; } bool isLoad() const { return kind == kLoad; } @@ -894,6 +893,9 @@ class PhiInst : public Instruction { public: Value* getValue(unsigned k) const {return getOperand(2 * k);} ///< 获取位置为k的值 BasicBlock* getBlock(unsigned k) const {return dynamic_cast(getOperand(2 * k + 1));} + //增加llvm同名方法实现获取value和block + Value* getIncomingValue(unsigned k) const {return getOperand(2 * k);} ///< 获取位置为k的值 + BasicBlock* getIncomingBlock(unsigned k) const {return dynamic_cast(getOperand(2 * k + 1));} auto& getincomings() const {return blk2val;} ///< 获取所有的基本块和对应的值 @@ -1117,6 +1119,14 @@ public: }; // class CondBrInst +class UnreachableInst : public Instruction { +public: + // 构造函数:设置指令类型为 kUnreachable + explicit UnreachableInst(const std::string& name, BasicBlock *parent = nullptr) + : Instruction(kUnreachable, Type::getVoidType(), parent, "") {} + +}; + //! Allocate memory for stack variables, used for non-global variable declartion class AllocaInst : public Instruction { friend class IRBuilder; diff --git a/src/include/midend/IRBuilder.h b/src/include/midend/IRBuilder.h index 760ef85..f761d57 100644 --- a/src/include/midend/IRBuilder.h +++ b/src/include/midend/IRBuilder.h @@ -239,19 +239,25 @@ class IRBuilder { block->getInstructions().emplace(position, inst); return inst; } ///< 创建return指令 - UncondBrInst * createUncondBrInst(BasicBlock *thenBlock, const std::vector &args) { + UncondBrInst * createUncondBrInst(BasicBlock *thenBlock, const std::vector &args = {}) { auto inst = new UncondBrInst(thenBlock, args, block); assert(inst); block->getInstructions().emplace(position, inst); return inst; } ///< 创建无条件指令 CondBrInst * createCondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock, - const std::vector &thenArgs, const std::vector &elseArgs) { + const std::vector &thenArgs = {}, const std::vector &elseArgs = {}) { auto inst = new CondBrInst(condition, thenBlock, elseBlock, thenArgs, elseArgs, block); assert(inst); block->getInstructions().emplace(position, inst); return inst; } ///< 创建条件跳转指令 + UnreachableInst * createUnreachableInst(const std::string &name = "") { + auto inst = new UnreachableInst(name, block); + assert(inst); + block->getInstructions().emplace(position, inst); + return inst; + } ///< 创建不可达指令 AllocaInst * createAllocaInst(Type *type, const std::vector &dims = {}, const std::string &name = "") { auto inst = new AllocaInst(type, dims, block, name); assert(inst);