From 520ebd96f0dce957517da573dc6f8c01b5e8c684 Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Thu, 31 Jul 2025 16:59:22 +0800 Subject: [PATCH] =?UTF-8?q?[midend-SCCP]=E5=A2=9E=E5=8A=A0=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E8=BE=BE=E6=8C=87=E4=BB=A4=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E6=8C=87=E4=BB=A4=E5=8F=82=E6=95=B0=EF=BC=88?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=9D=97args=E5=B7=B2=E5=BC=83=E7=94=A8?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=BA{}=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/include/midend/IR.h | 26 ++++++++++++++++++-------- src/include/midend/IRBuilder.h | 10 ++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) 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);