From 3b1bafde9c11cd1f57a5043fd952799fc4766d3e Mon Sep 17 00:00:00 2001 From: rain2133 <1370973498@qq.com> Date: Sun, 3 Aug 2025 13:45:13 +0800 Subject: [PATCH] =?UTF-8?q?[midend]=E5=B0=86alloca=E5=A3=B0=E6=98=8E?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=88=B0entry=E5=9D=97=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0mem2reg=E7=9A=84=E6=9C=BA=E4=BC=9A=EF=BC=8CTODO?= =?UTF-8?q?=EF=BC=9A=E4=B8=8D=E6=98=AF=E5=BE=88=E6=98=8E=E7=99=BD=E4=B8=BA?= =?UTF-8?q?=E4=BB=80=E4=B9=88=E5=BC=80=E4=BA=86=E4=BC=98=E5=8C=96=E8=BF=87?= =?UTF-8?q?=E4=B8=8D=E4=BA=86=E4=B8=80=E4=BA=9B=E6=A0=B7=E4=BE=8B=E6=AD=A3?= =?UTF-8?q?=E5=9C=A8=E6=8E=92=E6=9F=A5=E5=8E=9F=E5=9B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/midend/Pass/Pass.cpp | 4 ++++ src/midend/SysYIRGenerator.cpp | 27 +++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/midend/Pass/Pass.cpp b/src/midend/Pass/Pass.cpp index 3c91c00..5f74334 100644 --- a/src/midend/Pass/Pass.cpp +++ b/src/midend/Pass/Pass.cpp @@ -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"; } diff --git a/src/midend/SysYIRGenerator.cpp b/src/midend/SysYIRGenerator.cpp index 16f9423..882c7db 100644 --- a/src/midend/SysYIRGenerator.cpp +++ b/src/midend/SysYIRGenerator.cpp @@ -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(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;