Compare commits

...

1 Commits

2 changed files with 29 additions and 2 deletions

View File

@ -108,6 +108,10 @@ void PassManager::runOptimizationPipeline(Module* moduleIR, IRBuilder* builderIR
printPasses();
}
this->clearPasses();
this->addPass(&DCE::ID);
this->run();
if (DEBUG) std::cout << "--- Custom optimization sequence finished ---\n";
}

View File

@ -586,7 +586,18 @@ std::any SysYIRGenerator::visitConstDecl(SysYParser::ConstDeclContext *ctx) {
// 显式地为局部常量在栈上分配空间
// alloca 的类型将是指针指向常量类型,例如 `int*` 或 `int[2][3]*`
// 将 alloca 全部集中到entry函数中
// 记录当前位置
BasicBlock *curBB = builder.getBasicBlock();
auto curPos =builder.getPosition();
Function *currentFunction = builder.getBasicBlock()->getParent();
BasicBlock *entryBB = currentFunction->getEntryBlock();
// 在terminator前插入
auto entryPos = entryBB->terminator();
builder.setPosition(entryBB, entryPos);
AllocaInst *alloca = builder.createAllocaInst(Type::getPointerType(variableType), name);
// 恢复当前位置
builder.setPosition(curBB, curPos);
ArrayValueTree *root = std::any_cast<ArrayValueTree *>(constDef->constInitVal()->accept(this));
ValueCounter values;
@ -743,8 +754,20 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
// 对于数组alloca 的类型将是指针指向数组类型,例如 `int[2][3]*`
// 对于标量alloca 的类型将是指针指向标量类型,例如 `int*`
AllocaInst* alloca =
builder.createAllocaInst(Type::getPointerType(variableType), name);
BasicBlock *curBB = builder.getBasicBlock();
auto curPos =builder.getPosition();
Function *currentFunction = builder.getBasicBlock()->getParent();
BasicBlock *entryBB = currentFunction->getEntryBlock();
// 在terminator前插入
auto entryPos = entryBB->terminator();
builder.setPosition(entryBB, entryPos);
AllocaInst *alloca = builder.createAllocaInst(Type::getPointerType(variableType), name);
// 恢复当前位置
builder.setPosition(curBB, curPos);
// AllocaInst* alloca =
// builder.createAllocaInst(Type::getPointerType(variableType), name);
if (varDef->initVal() != nullptr) {
ValueCounter values;