[midend]消除冗余维度信息记录,适配IR生成器,TODO:其他优化遍生成指令修改,或者后端的访问
This commit is contained in:
@@ -98,7 +98,7 @@ std::any SysYIRGenerator::visitGlobalConstDecl(SysYParser::GlobalConstDeclContex
|
||||
if (!dims.empty()) { // 如果有维度,说明是数组
|
||||
variableType = buildArrayType(type, dims); // 构建完整的 ArrayType
|
||||
}
|
||||
module->createConstVar(name, Type::getPointerType(variableType), values, dims);
|
||||
module->createConstVar(name, Type::getPointerType(variableType), values);
|
||||
}
|
||||
return std::any();
|
||||
}
|
||||
@@ -127,7 +127,7 @@ std::any SysYIRGenerator::visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *c
|
||||
if (!dims.empty()) { // 如果有维度,说明是数组
|
||||
variableType = buildArrayType(type, dims); // 构建完整的 ArrayType
|
||||
}
|
||||
module->createGlobalValue(name, Type::getPointerType(variableType), dims, values);
|
||||
module->createGlobalValue(name, Type::getPointerType(variableType), values);
|
||||
}
|
||||
return std::any();
|
||||
}
|
||||
@@ -151,7 +151,7 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext *ctx) {
|
||||
|
||||
// 显式地为局部常量在栈上分配空间
|
||||
// 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));
|
||||
ValueCounter values;
|
||||
@@ -272,7 +272,7 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
|
||||
// 对于数组,alloca 的类型将是指针指向数组类型,例如 `int[2][3]*`
|
||||
// 对于标量,alloca 的类型将是指针指向标量类型,例如 `int*`
|
||||
AllocaInst* alloca =
|
||||
builder.createAllocaInst(Type::getPointerType(variableType), {}, name);
|
||||
builder.createAllocaInst(Type::getPointerType(variableType), name);
|
||||
|
||||
if (varDef->initVal() != nullptr) {
|
||||
ValueCounter values;
|
||||
@@ -510,7 +510,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
auto funcArgs = function->getArguments();
|
||||
std::vector<AllocaInst *> allocas;
|
||||
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);
|
||||
module->addVariable(paramNames[i], alloca);
|
||||
}
|
||||
@@ -525,7 +525,7 @@ std::any SysYIRGenerator::visitFuncDef(SysYParser::FuncDefContext *ctx){
|
||||
BasicBlock* funcBodyEntry = function->addBasicBlock("funcBodyEntry_" + name);
|
||||
|
||||
// 从 entryBB 无条件跳转到 funcBodyEntry
|
||||
builder.createUncondBrInst(funcBodyEntry, {});
|
||||
builder.createUncondBrInst(funcBodyEntry);
|
||||
builder.setPosition(funcBodyEntry,funcBodyEntry->end()); // 将插入点设置到 funcBodyEntry
|
||||
|
||||
for (auto item : ctx->blockStmt()->blockItem()) {
|
||||
@@ -690,7 +690,7 @@ std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
|
||||
ctx->stmt(0)->accept(this);
|
||||
module->leaveScope();
|
||||
}
|
||||
builder.createUncondBrInst(exitBlock, {});
|
||||
builder.createUncondBrInst(exitBlock);
|
||||
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||
|
||||
labelstring << "if_else.L" << builder.getLabelIndex();
|
||||
@@ -707,7 +707,7 @@ std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
|
||||
ctx->stmt(1)->accept(this);
|
||||
module->leaveScope();
|
||||
}
|
||||
builder.createUncondBrInst(exitBlock, {});
|
||||
builder.createUncondBrInst(exitBlock);
|
||||
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||
|
||||
labelstring << "if_exit.L" << builder.getLabelIndex();
|
||||
@@ -737,7 +737,7 @@ std::any SysYIRGenerator::visitIfStmt(SysYParser::IfStmtContext *ctx) {
|
||||
ctx->stmt(0)->accept(this);
|
||||
module->leaveScope();
|
||||
}
|
||||
builder.createUncondBrInst(exitBlock, {});
|
||||
builder.createUncondBrInst(exitBlock);
|
||||
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||
|
||||
labelstring << "if_exit.L" << builder.getLabelIndex();
|
||||
@@ -759,7 +759,7 @@ std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
|
||||
labelstring << "while_head.L" << builder.getLabelIndex();
|
||||
BasicBlock *headBlock = function->addBasicBlock(labelstring.str());
|
||||
labelstring.str("");
|
||||
builder.createUncondBrInst(headBlock, {});
|
||||
builder.createUncondBrInst(headBlock);
|
||||
BasicBlock::conectBlocks(curBlock, headBlock);
|
||||
builder.setPosition(headBlock, headBlock->end());
|
||||
|
||||
@@ -792,7 +792,7 @@ std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
|
||||
module->leaveScope();
|
||||
}
|
||||
|
||||
builder.createUncondBrInst(headBlock, {});
|
||||
builder.createUncondBrInst(headBlock);
|
||||
BasicBlock::conectBlocks(builder.getBasicBlock(), exitBlock);
|
||||
builder.popBreakBlock();
|
||||
builder.popContinueBlock();
|
||||
@@ -808,14 +808,14 @@ std::any SysYIRGenerator::visitWhileStmt(SysYParser::WhileStmtContext *ctx) {
|
||||
|
||||
std::any SysYIRGenerator::visitBreakStmt(SysYParser::BreakStmtContext *ctx) {
|
||||
BasicBlock* breakBlock = builder.getBreakBlock();
|
||||
builder.createUncondBrInst(breakBlock, {});
|
||||
builder.createUncondBrInst(breakBlock);
|
||||
BasicBlock::conectBlocks(builder.getBasicBlock(), breakBlock);
|
||||
return std::any();
|
||||
}
|
||||
|
||||
std::any SysYIRGenerator::visitContinueStmt(SysYParser::ContinueStmtContext *ctx) {
|
||||
BasicBlock* continueBlock = builder.getContinueBlock();
|
||||
builder.createUncondBrInst(continueBlock, {});
|
||||
builder.createUncondBrInst(continueBlock);
|
||||
BasicBlock::conectBlocks(builder.getBasicBlock(), continueBlock);
|
||||
return std::any();
|
||||
}
|
||||
@@ -1504,7 +1504,7 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext *ctx){
|
||||
labelstring.str("");
|
||||
|
||||
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, falseBlock);
|
||||
@@ -1514,7 +1514,7 @@ std::any SysYIRGenerator::visitLAndExp(SysYParser::LAndExpContext *ctx){
|
||||
}
|
||||
|
||||
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, falseBlock);
|
||||
@@ -1631,7 +1631,7 @@ void Utils::createExternalFunction(
|
||||
for (int i = 0; i < paramTypes.size(); ++i) {
|
||||
auto arg = new Argument(paramTypes[i], function, i, paramNames[i]);
|
||||
auto alloca = pBuilder->createAllocaInst(
|
||||
Type::getPointerType(paramTypes[i]), {}, paramNames[i]);
|
||||
Type::getPointerType(paramTypes[i]), paramNames[i]);
|
||||
function->insertArgument(arg);
|
||||
auto store = pBuilder->createStoreInst(arg, alloca);
|
||||
pModule->addVariable(paramNames[i], alloca);
|
||||
|
||||
Reference in New Issue
Block a user