[midend]消除冗余维度信息记录,适配IR生成器,TODO:其他优化遍生成指令修改,或者后端的访问

This commit is contained in:
rain2133
2025-07-31 19:36:39 +08:00
parent de0f8422e9
commit f3f603a032
4 changed files with 66 additions and 118 deletions

View File

@@ -1061,12 +1061,10 @@ class UncondBrInst : public Instruction {
friend class Function; friend class Function;
protected: protected:
UncondBrInst(BasicBlock *block, std::vector<Value *> args, UncondBrInst(BasicBlock *block,
BasicBlock *parent = nullptr) BasicBlock *parent = nullptr)
: Instruction(kBr, Type::getVoidType(), parent, "") { : Instruction(kBr, Type::getVoidType(), parent, "") {
// assert(block->getNumArguments() == args.size());
addOperand(block); addOperand(block);
addOperands(args);
} }
public: public:
@@ -1095,17 +1093,12 @@ class CondBrInst : public Instruction {
friend class Function; friend class Function;
protected: protected:
CondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock, CondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock,
const std::vector<Value *> &thenArgs, BasicBlock *parent = nullptr)
const std::vector<Value *> &elseArgs, BasicBlock *parent = nullptr)
: Instruction(kCondBr, Type::getVoidType(), parent, "") { : Instruction(kCondBr, Type::getVoidType(), parent, "") {
// assert(thenBlock->getNumArguments() == thenArgs.size() and
// elseBlock->getNumArguments() == elseArgs.size());
addOperand(condition); addOperand(condition);
addOperand(thenBlock); addOperand(thenBlock);
addOperand(elseBlock); addOperand(elseBlock);
addOperands(thenArgs);
addOperands(elseArgs);
} }
public: public:
Value* getCondition() const { return getOperand(0); } Value* getCondition() const { return getOperand(0); }
@@ -1129,17 +1122,6 @@ public:
} }
return succs; return succs;
} }
// auto getThenArguments() const {
// auto begin = std::next(operand_begin(), 3);
// // auto end = std::next(begin, getThenBlock()->getNumArguments());
// return make_range(begin, end);
// }
// auto getElseArguments() const {
// auto begin =
// std::next(operand_begin(), 3 + getThenBlock()->getNumArguments());
// auto end = operand_end();
// return make_range(begin, end);
// }
}; // class CondBrInst }; // class CondBrInst
@@ -1156,10 +1138,9 @@ class AllocaInst : public Instruction {
friend class IRBuilder; friend class IRBuilder;
friend class Function; friend class Function;
protected: protected:
AllocaInst(Type *type, const std::vector<Value *> &dims = {}, AllocaInst(Type *type,
BasicBlock *parent = nullptr, const std::string &name = "") BasicBlock *parent = nullptr, const std::string &name = "")
: Instruction(kAlloca, type, parent, name) { : Instruction(kAlloca, type, parent, name) {
addOperands(dims);
} }
public: public:
@@ -1168,8 +1149,6 @@ public:
return getType()->as<PointerType>()->getBaseType(); return getType()->as<PointerType>()->getBaseType();
} ///< 获取分配的类型 } ///< 获取分配的类型
int getNumDims() const { return getNumOperands(); } int getNumDims() const { return getNumOperands(); }
auto getDims() const { return getOperands(); }
Value* getDim(int index) { return getOperand(index); }
}; // class AllocaInst }; // class AllocaInst
@@ -1216,12 +1195,11 @@ class LoadInst : public Instruction {
friend class Function; friend class Function;
protected: protected:
LoadInst(Value *pointer, const std::vector<Value *> &indices = {}, LoadInst(Value *pointer,
BasicBlock *parent = nullptr, const std::string &name = "") BasicBlock *parent = nullptr, const std::string &name = "")
: Instruction(kLoad, pointer->getType()->as<PointerType>()->getBaseType(), : Instruction(kLoad, pointer->getType()->as<PointerType>()->getBaseType(),
parent, name) { parent, name) {
addOperand(pointer); addOperand(pointer);
addOperands(indices);
} }
public: public:
@@ -1241,22 +1219,15 @@ class StoreInst : public Instruction {
protected: protected:
StoreInst(Value *value, Value *pointer, StoreInst(Value *value, Value *pointer,
const std::vector<Value *> &indices = {},
BasicBlock *parent = nullptr, const std::string &name = "") BasicBlock *parent = nullptr, const std::string &name = "")
: Instruction(kStore, Type::getVoidType(), parent, name) { : Instruction(kStore, Type::getVoidType(), parent, name) {
addOperand(value); addOperand(value);
addOperand(pointer); addOperand(pointer);
addOperands(indices);
} }
public: public:
int getNumIndices() const { return getNumOperands() - 2; }
Value* getValue() const { return getOperand(0); } Value* getValue() const { return getOperand(0); }
Value* getPointer() const { return getOperand(1); } Value* getPointer() const { return getOperand(1); }
auto getIndices() const {
return make_range(std::next(operand_begin(), 2), operand_end());
}
Value* getIndex(int index) const { return getOperand(index + 2); }
}; // class StoreInst }; // class StoreInst
@@ -1395,20 +1366,18 @@ protected:
protected: protected:
GlobalValue(Module *parent, Type *type, const std::string &name, GlobalValue(Module *parent, Type *type, const std::string &name,
const std::vector<Value *> &dims = {},
ValueCounter init = {}) ValueCounter init = {})
: Value(type, name), parent(parent) { : Value(type, name), parent(parent) {
assert(type->isPointer()); assert(type->isPointer());
// addOperands(dims);
// 维度信息已经被记录到Type中dim只是为了方便初始化 // 维度信息已经被记录到Type中dim只是为了方便初始化
numDims = dims.size(); numDims = 0;
if (init.size() == 0) { if (init.size() == 0) {
unsigned num = 1; unsigned num = 1;
for (unsigned i = 0; i < numDims; i++) { auto arrayType = type->as<ArrayType>();
// Assume dims elements are ConstantInteger and cast appropriately while (arrayType) {
auto dim_val = dynamic_cast<ConstantInteger*>(dims[i]); numDims++;
assert(dim_val && "GlobalValue dims must be constant integers"); num *= arrayType->getNumElements();
num *= dim_val->getInt(); arrayType = arrayType->getElementType()->as<ArrayType>();
} }
if (dynamic_cast<PointerType *>(type)->getBaseType() == Type::getFloatType()) { if (dynamic_cast<PointerType *>(type)->getBaseType() == Type::getFloatType()) {
init.push_back(ConstantFloating::get(0.0F), num); // Use new constant factory init.push_back(ConstantFloating::get(0.0F), num); // Use new constant factory
@@ -1420,9 +1389,6 @@ protected:
} }
public: public:
// unsigned getNumDims() const { return numDims; } ///< 获取维度数量
// Value* getDim(unsigned index) const { return getOperand(index); } ///< 获取位置为index的维度
// auto getDims() const { return getOperands(); } ///< 获取维度列表
unsigned getNumIndices() const { unsigned getNumIndices() const {
return numDims; return numDims;
} ///< 获取维度数量 } ///< 获取维度数量
@@ -1464,13 +1430,19 @@ class ConstantVariable : public Value {
ValueCounter initValues; ///< 值 ValueCounter initValues; ///< 值
protected: protected:
ConstantVariable(Module *parent, Type *type, const std::string &name, const ValueCounter &init, ConstantVariable(Module *parent, Type *type, const std::string &name, const ValueCounter &init)
const std::vector<Value *> &dims = {})
: Value(type, name), parent(parent) { : Value(type, name), parent(parent) {
assert(type->isPointer()); assert(type->isPointer());
numDims = dims.size(); // numDims = dims.size();
numDims = 0;
if(type->as<PointerType>()->getBaseType()->isArray()) {
auto arrayType = type->as<ArrayType>();
while (arrayType) {
numDims++;
arrayType = arrayType->getElementType()->as<ArrayType>();
}
}
initValues = init; initValues = init;
// addOperands(dims); 同GlobalValue维度信息已经被记录到Type中dim只是为了方便初始化
} }
public: public:
@@ -1563,13 +1535,12 @@ class Module {
return result.first->second.get(); return result.first->second.get();
} ///< 创建外部函数 } ///< 创建外部函数
///< 变量创建伴随着符号表的更新 ///< 变量创建伴随着符号表的更新
GlobalValue* createGlobalValue(const std::string &name, Type *type, const std::vector<Value *> &dims = {}, GlobalValue* createGlobalValue(const std::string &name, Type *type, const ValueCounter &init = {}) {
const ValueCounter &init = {}) {
bool isFinished = variableTable.isCurNodeNull(); bool isFinished = variableTable.isCurNodeNull();
if (isFinished) { if (isFinished) {
variableTable.enterGlobalScope(); variableTable.enterGlobalScope();
} }
auto result = variableTable.addVariable(name, new GlobalValue(this, type, name, dims, init)); auto result = variableTable.addVariable(name, new GlobalValue(this, type, name, init));
if (isFinished) { if (isFinished) {
variableTable.leaveScope(); variableTable.leaveScope();
} }
@@ -1578,9 +1549,8 @@ class Module {
} }
return dynamic_cast<GlobalValue *>(result); return dynamic_cast<GlobalValue *>(result);
} ///< 创建全局变量 } ///< 创建全局变量
ConstantVariable* createConstVar(const std::string &name, Type *type, const ValueCounter &init, ConstantVariable* createConstVar(const std::string &name, Type *type, const ValueCounter &init) {
const std::vector<Value *> &dims = {}) { auto result = variableTable.addVariable(name, new ConstantVariable(this, type, name, init));
auto result = variableTable.addVariable(name, new ConstantVariable(this, type, name, init, dims));
if (result == nullptr) { if (result == nullptr) {
return nullptr; return nullptr;
} }

View File

@@ -239,15 +239,14 @@ class IRBuilder {
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
} ///< 创建return指令 } ///< 创建return指令
UncondBrInst * createUncondBrInst(BasicBlock *thenBlock, const std::vector<Value *> &args = {}) { UncondBrInst * createUncondBrInst(BasicBlock *thenBlock) {
auto inst = new UncondBrInst(thenBlock, args, block); auto inst = new UncondBrInst(thenBlock, block);
assert(inst); assert(inst);
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
} ///< 创建无条件指令 } ///< 创建无条件指令
CondBrInst * createCondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock, CondBrInst * createCondBrInst(Value *condition, BasicBlock *thenBlock, BasicBlock *elseBlock) {
const std::vector<Value *> &thenArgs = {}, const std::vector<Value *> &elseArgs = {}) { auto inst = new CondBrInst(condition, thenBlock, elseBlock, block);
auto inst = new CondBrInst(condition, thenBlock, elseBlock, thenArgs, elseArgs, block);
assert(inst); assert(inst);
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
@@ -258,18 +257,12 @@ class IRBuilder {
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
} ///< 创建不可达指令 } ///< 创建不可达指令
AllocaInst * createAllocaInst(Type *type, const std::vector<Value *> &dims = {}, const std::string &name = "") { AllocaInst * createAllocaInst(Type *type, const std::string &name = "") {
auto inst = new AllocaInst(type, dims, block, name); auto inst = new AllocaInst(type, block, name);
assert(inst); assert(inst);
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
} ///< 创建分配指令 } ///< 创建分配指令
AllocaInst * createAllocaInstWithoutInsert(Type *type, const std::vector<Value *> &dims = {}, BasicBlock *parent = nullptr,
const std::string &name = "") {
auto inst = new AllocaInst(type, dims, parent, name);
assert(inst);
return inst;
} ///< 创建不插入指令列表的分配指令[仅用于phi指令]
LoadInst * createLoadInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "") { LoadInst * createLoadInst(Value *pointer, const std::vector<Value *> &indices = {}, const std::string &name = "") {
std::string newName; std::string newName;
if (name.empty()) { if (name.empty()) {
@@ -281,7 +274,7 @@ class IRBuilder {
newName = name; newName = name;
} }
auto inst = new LoadInst(pointer, indices, block, newName); auto inst = new LoadInst(pointer, block, newName);
assert(inst); assert(inst);
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
@@ -292,9 +285,8 @@ class IRBuilder {
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
} ///< 创建memset指令 } ///< 创建memset指令
StoreInst * createStoreInst(Value *value, Value *pointer, const std::vector<Value *> &indices = {}, StoreInst * createStoreInst(Value *value, Value *pointer, const std::string &name = "") {
const std::string &name = "") { auto inst = new StoreInst(value, pointer, block, name);
auto inst = new StoreInst(value, pointer, indices, block, name);
assert(inst); assert(inst);
block->getInstructions().emplace(position, inst); block->getInstructions().emplace(position, inst);
return inst; return inst;
@@ -314,24 +306,6 @@ class IRBuilder {
block->getInstructions().emplace(block->begin(), inst); block->getInstructions().emplace(block->begin(), inst);
return inst; return inst;
} ///< 创建Phi指令 } ///< 创建Phi指令
// GetElementPtrInst* createGetElementPtrInst(Value *basePointer,
// const std::vector<Value *> &indices = {},
// const std::string &name = "") {
// std::string newName;
// if (name.empty()) {
// std::stringstream ss;
// ss << tmpIndex;
// newName = ss.str();
// tmpIndex++;
// } else {
// newName = name;
// }
// auto inst = new GetElementPtrInst(basePointer, indices, block, newName);
// assert(inst);
// block->getInstructions().emplace(position, inst);
// return inst;
// }
/** /**
* @brief 根据 LLVM 设计模式创建 GEP 指令。 * @brief 根据 LLVM 设计模式创建 GEP 指令。
* 它会自动推断返回类型,无需手动指定。 * 它会自动推断返回类型,无需手动指定。

View File

@@ -227,9 +227,10 @@ Function * Function::clone(const std::string &suffix) const {
auto oldAllocInst = dynamic_cast<AllocaInst *>(value); auto oldAllocInst = dynamic_cast<AllocaInst *>(value);
if (oldAllocInst != nullptr) { if (oldAllocInst != nullptr) {
std::vector<Value *> dims; std::vector<Value *> dims;
for (const auto &dim : oldAllocInst->getDims()) { // TODO: 这里的dims用type推断
dims.emplace_back(dim->getValue()); // for (const auto &dim : oldAllocInst->getDims()) {
} // dims.emplace_back(dim->getValue());
// }
ss << oldAllocInst->getName() << suffix; ss << oldAllocInst->getName() << suffix;
auto newAllocInst = auto newAllocInst =
new AllocaInst(oldAllocInst->getType(), dims, oldNewBlockMap.at(oldAllocInst->getParent()), ss.str()); new AllocaInst(oldAllocInst->getType(), dims, oldNewBlockMap.at(oldAllocInst->getParent()), ss.str());
@@ -252,9 +253,10 @@ Function * Function::clone(const std::string &suffix) const {
if (oldNewValueMap.find(inst.get()) == oldNewValueMap.end()) { if (oldNewValueMap.find(inst.get()) == oldNewValueMap.end()) {
auto oldAllocInst = dynamic_cast<AllocaInst *>(inst.get()); auto oldAllocInst = dynamic_cast<AllocaInst *>(inst.get());
std::vector<Value *> dims; std::vector<Value *> dims;
for (const auto &dim : oldAllocInst->getDims()) { // TODO: 这里的dims用type推断
dims.emplace_back(dim->getValue()); // for (const auto &dim : oldAllocInst->getDims()) {
} // dims.emplace_back(dim->getValue());
// }
ss << oldAllocInst->getName() << suffix; ss << oldAllocInst->getName() << suffix;
auto newAllocInst = auto newAllocInst =
new AllocaInst(oldAllocInst->getType(), dims, oldNewBlockMap.at(oldAllocInst->getParent()), ss.str()); new AllocaInst(oldAllocInst->getType(), dims, oldNewBlockMap.at(oldAllocInst->getParent()), ss.str());
@@ -422,7 +424,7 @@ Function * Function::clone(const std::string &suffix) const {
Value *newCond; Value *newCond;
newCond = oldNewValueMap.at(oldCond); newCond = oldNewValueMap.at(oldCond);
auto newCondBrInst = new CondBrInst(newCond, oldNewBlockMap.at(oldCondBrInst->getThenBlock()), auto newCondBrInst = new CondBrInst(newCond, oldNewBlockMap.at(oldCondBrInst->getThenBlock()),
oldNewBlockMap.at(oldCondBrInst->getElseBlock()), {}, {}, oldNewBlockMap.at(oldCondBrInst->getElseBlock()),
oldNewBlockMap.at(oldCondBrInst->getParent())); oldNewBlockMap.at(oldCondBrInst->getParent()));
oldNewValueMap.emplace(oldCondBrInst, newCondBrInst); oldNewValueMap.emplace(oldCondBrInst, newCondBrInst);
break; break;
@@ -431,7 +433,7 @@ Function * Function::clone(const std::string &suffix) const {
case Instruction::kBr: { case Instruction::kBr: {
auto oldBrInst = dynamic_cast<UncondBrInst *>(inst); auto oldBrInst = dynamic_cast<UncondBrInst *>(inst);
auto newBrInst = auto newBrInst =
new UncondBrInst(oldNewBlockMap.at(oldBrInst->getBlock()), {}, oldNewBlockMap.at(oldBrInst->getParent())); new UncondBrInst(oldNewBlockMap.at(oldBrInst->getBlock()), oldNewBlockMap.at(oldBrInst->getParent()));
oldNewValueMap.emplace(oldBrInst, newBrInst); oldNewValueMap.emplace(oldBrInst, newBrInst);
break; break;
} }
@@ -464,7 +466,8 @@ Function * Function::clone(const std::string &suffix) const {
newIndices.emplace_back(oldNewValueMap.at(index->getValue())); newIndices.emplace_back(oldNewValueMap.at(index->getValue()));
} }
ss << oldLoadInst->getName() << suffix; ss << oldLoadInst->getName() << suffix;
auto newLoadInst = new LoadInst(newPointer, newIndices, oldNewBlockMap.at(oldLoadInst->getParent()), ss.str()); // TODO : 这里的newLoadInst的类型需要根据oldLoadInst的类型来推断
auto newLoadInst = new LoadInst(newPointer, oldNewBlockMap.at(oldLoadInst->getParent()), ss.str());
ss.str(""); ss.str("");
oldNewValueMap.emplace(oldLoadInst, newLoadInst); oldNewValueMap.emplace(oldLoadInst, newLoadInst);
break; break;
@@ -479,9 +482,10 @@ Function * Function::clone(const std::string &suffix) const {
std::vector<Value *> newIndices; std::vector<Value *> newIndices;
newPointer = oldNewValueMap.at(oldPointer); newPointer = oldNewValueMap.at(oldPointer);
newValue = oldNewValueMap.at(oldValue); newValue = oldNewValueMap.at(oldValue);
for (const auto &index : oldStoreInst->getIndices()) { // TODO: 这里的newIndices需要根据oldStoreInst的类型来推断
newIndices.emplace_back(oldNewValueMap.at(index->getValue())); // for (const auto &index : oldStoreInst->getIndices()) {
} // newIndices.emplace_back(oldNewValueMap.at(index->getValue()));
// }
auto newStoreInst = new StoreInst(newValue, newPointer, newIndices, auto newStoreInst = new StoreInst(newValue, newPointer, newIndices,
oldNewBlockMap.at(oldStoreInst->getParent()), oldStoreInst->getName()); oldNewBlockMap.at(oldStoreInst->getParent()), oldStoreInst->getName());
oldNewValueMap.emplace(oldStoreInst, newStoreInst); oldNewValueMap.emplace(oldStoreInst, newStoreInst);

View File

@@ -98,7 +98,7 @@ std::any SysYIRGenerator::visitGlobalConstDecl(SysYParser::GlobalConstDeclContex
if (!dims.empty()) { // 如果有维度,说明是数组 if (!dims.empty()) { // 如果有维度,说明是数组
variableType = buildArrayType(type, dims); // 构建完整的 ArrayType variableType = buildArrayType(type, dims); // 构建完整的 ArrayType
} }
module->createConstVar(name, Type::getPointerType(variableType), values, dims); module->createConstVar(name, Type::getPointerType(variableType), values);
} }
return std::any(); return std::any();
} }
@@ -127,7 +127,7 @@ std::any SysYIRGenerator::visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *c
if (!dims.empty()) { // 如果有维度,说明是数组 if (!dims.empty()) { // 如果有维度,说明是数组
variableType = buildArrayType(type, dims); // 构建完整的 ArrayType variableType = buildArrayType(type, dims); // 构建完整的 ArrayType
} }
module->createGlobalValue(name, Type::getPointerType(variableType), dims, values); module->createGlobalValue(name, Type::getPointerType(variableType), values);
} }
return std::any(); return std::any();
} }
@@ -151,7 +151,7 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext *ctx) {
// 显式地为局部常量在栈上分配空间 // 显式地为局部常量在栈上分配空间
// alloca 的类型将是指针指向常量类型,例如 `int*` 或 `int[2][3]*` // alloca 的类型将是指针指向常量类型,例如 `int*` 或 `int[2][3]*`
AllocaInst *alloca = builder.createAllocaInst(Type::getPointerType(variableType), {}, name); AllocaInst *alloca = builder.createAllocaInst(Type::getPointerType(variableType), name);
ArrayValueTree *root = std::any_cast<ArrayValueTree *>(constDef->constInitVal()->accept(this)); ArrayValueTree *root = std::any_cast<ArrayValueTree *>(constDef->constInitVal()->accept(this));
ValueCounter values; ValueCounter values;
@@ -272,7 +272,7 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
// 对于数组alloca 的类型将是指针指向数组类型,例如 `int[2][3]*` // 对于数组alloca 的类型将是指针指向数组类型,例如 `int[2][3]*`
// 对于标量alloca 的类型将是指针指向标量类型,例如 `int*` // 对于标量alloca 的类型将是指针指向标量类型,例如 `int*`
AllocaInst* alloca = AllocaInst* alloca =
builder.createAllocaInst(Type::getPointerType(variableType), {}, name); builder.createAllocaInst(Type::getPointerType(variableType), name);
if (varDef->initVal() != nullptr) { if (varDef->initVal() != nullptr) {
ValueCounter values; ValueCounter values;
@@ -510,7 +510,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
auto funcArgs = function->getArguments(); auto funcArgs = function->getArguments();
std::vector<AllocaInst *> allocas; std::vector<AllocaInst *> allocas;
for (int i = 0; i < paramActualTypes.size(); ++i) { for (int i = 0; i < paramActualTypes.size(); ++i) {
AllocaInst *alloca = builder.createAllocaInst(Type::getPointerType(paramActualTypes[i]), {}, paramNames[i]); AllocaInst *alloca = builder.createAllocaInst(Type::getPointerType(paramActualTypes[i]), paramNames[i]);
allocas.push_back(alloca); allocas.push_back(alloca);
module->addVariable(paramNames[i], alloca); module->addVariable(paramNames[i], alloca);
} }
@@ -525,7 +525,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
BasicBlock* funcBodyEntry = function->addBasicBlock("funcBodyEntry_" + name); BasicBlock* funcBodyEntry = function->addBasicBlock("funcBodyEntry_" + name);
// 从 entryBB 无条件跳转到 funcBodyEntry // 从 entryBB 无条件跳转到 funcBodyEntry
builder.createUncondBrInst(funcBodyEntry, {}); builder.createUncondBrInst(funcBodyEntry);
builder.setPosition(funcBodyEntry,funcBodyEntry->end()); // 将插入点设置到 funcBodyEntry builder.setPosition(funcBodyEntry,funcBodyEntry->end()); // 将插入点设置到 funcBodyEntry
for (auto item : ctx->blockStmt()->blockItem()) { for (auto item : ctx->blockStmt()->blockItem()) {
@@ -690,7 +690,7 @@ std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
ctx->stmt(0)->accept(this); ctx->stmt(0)->accept(this);
module->leaveScope(); module->leaveScope();
} }
builder.createUncondBrInst(exitBlock, {}); builder.createUncondBrInst(exitBlock);
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock); BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
labelstring << "if_else.L" << builder.getLabelIndex(); labelstring << "if_else.L" << builder.getLabelIndex();
@@ -707,7 +707,7 @@ std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
ctx->stmt(1)->accept(this); ctx->stmt(1)->accept(this);
module->leaveScope(); module->leaveScope();
} }
builder.createUncondBrInst(exitBlock, {}); builder.createUncondBrInst(exitBlock);
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock); BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
labelstring << "if_exit.L" << builder.getLabelIndex(); labelstring << "if_exit.L" << builder.getLabelIndex();
@@ -737,7 +737,7 @@ std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
ctx->stmt(0)->accept(this); ctx->stmt(0)->accept(this);
module->leaveScope(); module->leaveScope();
} }
builder.createUncondBrInst(exitBlock, {}); builder.createUncondBrInst(exitBlock);
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock); BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
labelstring << "if_exit.L" << builder.getLabelIndex(); labelstring << "if_exit.L" << builder.getLabelIndex();
@@ -759,7 +759,7 @@ std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
labelstring << "while_head.L" << builder.getLabelIndex(); labelstring << "while_head.L" << builder.getLabelIndex();
BasicBlock *headBlock = function->addBasicBlock(labelstring.str()); BasicBlock *headBlock = function->addBasicBlock(labelstring.str());
labelstring.str(""); labelstring.str("");
builder.createUncondBrInst(headBlock, {}); builder.createUncondBrInst(headBlock);
BasicBlock::conectBlocks(curBlock, headBlock); BasicBlock::conectBlocks(curBlock, headBlock);
builder.setPosition(headBlock, headBlock->end()); builder.setPosition(headBlock, headBlock->end());
@@ -792,7 +792,7 @@ std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
module->leaveScope(); module->leaveScope();
} }
builder.createUncondBrInst(headBlock, {}); builder.createUncondBrInst(headBlock);
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock); BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
builder.popBreakBlock(); builder.popBreakBlock();
builder.popContinueBlock(); builder.popContinueBlock();
@@ -808,14 +808,14 @@ std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
std::any SysYIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext *ctx) { std::any SysYIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext *ctx) {
BasicBlock* breakBlock = builder.getBreakBlock(); BasicBlock* breakBlock = builder.getBreakBlock();
builder.createUncondBrInst(breakBlock, {}); builder.createUncondBrInst(breakBlock);
BasicBlock::conectBlocks(builder.getBasicBlock(), breakBlock); BasicBlock::conectBlocks(builder.getBasicBlock(), breakBlock);
return std::any(); return std::any();
} }
std::any SysYIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext *ctx) { std::any SysYIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext *ctx) {
BasicBlock* continueBlock = builder.getContinueBlock(); BasicBlock* continueBlock = builder.getContinueBlock();
builder.createUncondBrInst(continueBlock, {}); builder.createUncondBrInst(continueBlock);
BasicBlock::conectBlocks(builder.getBasicBlock(), continueBlock); BasicBlock::conectBlocks(builder.getBasicBlock(), continueBlock);
return std::any(); return std::any();
} }
@@ -1504,7 +1504,7 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext *ctx){
labelstring.str(""); labelstring.str("");
auto cond = std::any_cast<Value *>(visitEqExp(ctx->eqExp(i))); auto cond = std::any_cast<Value *>(visitEqExp(ctx->eqExp(i)));
builder.createCondBrInst(cond, newtrueBlock, falseBlock, {}, {}); builder.createCondBrInst(cond, newtrueBlock, falseBlock);
BasicBlock::conectBlocks(curBlock, newtrueBlock); BasicBlock::conectBlocks(curBlock, newtrueBlock);
BasicBlock::conectBlocks(curBlock, falseBlock); BasicBlock::conectBlocks(curBlock, falseBlock);
@@ -1514,7 +1514,7 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext *ctx){
} }
auto cond = std::any_cast<Value *>(visitEqExp(conds.back())); auto cond = std::any_cast<Value *>(visitEqExp(conds.back()));
builder.createCondBrInst(cond, trueBlock, falseBlock, {}, {}); builder.createCondBrInst(cond, trueBlock, falseBlock);
BasicBlock::conectBlocks(curBlock, trueBlock); BasicBlock::conectBlocks(curBlock, trueBlock);
BasicBlock::conectBlocks(curBlock, falseBlock); BasicBlock::conectBlocks(curBlock, falseBlock);
@@ -1631,7 +1631,7 @@ void Utils::createExternalFunction(
for (int i = 0; i < paramTypes.size(); ++i) { for (int i = 0; i < paramTypes.size(); ++i) {
auto arg = new Argument(paramTypes[i], function, i, paramNames[i]); auto arg = new Argument(paramTypes[i], function, i, paramNames[i]);
auto alloca = pBuilder->createAllocaInst( auto alloca = pBuilder->createAllocaInst(
Type::getPointerType(paramTypes[i]), {}, paramNames[i]); Type::getPointerType(paramTypes[i]), paramNames[i]);
function->insertArgument(arg); function->insertArgument(arg);
auto store = pBuilder->createStoreInst(arg, alloca); auto store = pBuilder->createStoreInst(arg, alloca);
pModule->addVariable(paramNames[i], alloca); pModule->addVariable(paramNames[i], alloca);