[midend-SCCP]修改BaiscBlock的析构逻辑,将CFG修改的职责交给优化遍,注释Mem2Reg的调试信息。

This commit is contained in:
rain2133
2025-08-01 01:44:33 +08:00
parent 0f1fcc835d
commit c268191826
3 changed files with 29 additions and 10 deletions

View File

@@ -514,12 +514,15 @@ public:
explicit BasicBlock(Function *parent, const std::string &name = "") explicit BasicBlock(Function *parent, const std::string &name = "")
: Value(Type::getLabelType(), name), parent(parent) {} : Value(Type::getLabelType(), name), parent(parent) {}
~BasicBlock() override { ~BasicBlock() override {
for (auto pre : predecessors) { // for (auto pre : predecessors) {
pre->removeSuccessor(this); // pre->removeSuccessor(this);
} // }
for (auto suc : successors) { // for (auto suc : successors) {
suc->removePredecessor(this); // suc->removePredecessor(this);
} // }
// 这些关系应该在 BasicBlock 被从 Function 中移除时,
// 由负责 CFG 优化的 Pass (例如 SCCP 的 RemoveDeadBlock) 显式地清理。
// 析构函数只负责清理 BasicBlock 自身拥有的资源(例如,指令列表)。
} }
public: public:

View File

@@ -240,7 +240,9 @@ void Mem2RegContext::renameVariables(AllocaInst *currentAlloca, BasicBlock *curr
// loadInst->getPointer() 返回 AllocaInst* // loadInst->getPointer() 返回 AllocaInst*
// 将 LoadInst 的所有用途替换为当前 alloca 值栈顶部的 SSA 值 // 将 LoadInst 的所有用途替换为当前 alloca 值栈顶部的 SSA 值
assert(!allocaToValueStackMap[alloca].empty() && "Value stack empty for alloca during load replacement!"); assert(!allocaToValueStackMap[alloca].empty() && "Value stack empty for alloca during load replacement!");
if(DEBUG){
std::cout << "Mem2Reg: Replacing load " << loadInst->getPointer()->getName() << " with SSA value." << std::endl; std::cout << "Mem2Reg: Replacing load " << loadInst->getPointer()->getName() << " with SSA value." << std::endl;
}
loadInst->replaceAllUsesWith(allocaToValueStackMap[alloca].top()); loadInst->replaceAllUsesWith(allocaToValueStackMap[alloca].top());
instIter = SysYIROptUtils::usedelete(instIter); instIter = SysYIROptUtils::usedelete(instIter);
instDeleted = true; instDeleted = true;
@@ -256,7 +258,9 @@ void Mem2RegContext::renameVariables(AllocaInst *currentAlloca, BasicBlock *curr
if (storeInst->getPointer() == alloca) { if (storeInst->getPointer() == alloca) {
// 假设 storeInst->getPointer() 返回 AllocaInst* // 假设 storeInst->getPointer() 返回 AllocaInst*
// 将 StoreInst 存储的值作为新的 SSA 值,压入值栈 // 将 StoreInst 存储的值作为新的 SSA 值,压入值栈
if(DEBUG){
std::cout << "Mem2Reg: Replacing store to " << storeInst->getPointer()->getName() << " with SSA value." << std::endl; std::cout << "Mem2Reg: Replacing store to " << storeInst->getPointer()->getName() << " with SSA value." << std::endl;
}
allocaToValueStackMap[alloca].push(storeInst->getValue()); allocaToValueStackMap[alloca].push(storeInst->getValue());
localStackPushed.push(storeInst->getValue()); // 记录以便弹出 localStackPushed.push(storeInst->getValue()); // 记录以便弹出
instIter = SysYIROptUtils::usedelete(instIter); instIter = SysYIROptUtils::usedelete(instIter);
@@ -297,7 +301,9 @@ void Mem2RegContext::renameVariables(AllocaInst *currentAlloca, BasicBlock *curr
if(dominatedBlocks){ if(dominatedBlocks){
for (auto dominatedBB : *dominatedBlocks) { for (auto dominatedBB : *dominatedBlocks) {
if (dominatedBB) { if (dominatedBB) {
if(DEBUG){
std::cout << "Mem2Reg: Recursively renaming variables in dominated block: " << dominatedBB->getName() << std::endl; std::cout << "Mem2Reg: Recursively renaming variables in dominated block: " << dominatedBB->getName() << std::endl;
}
renameVariables(currentAlloca, dominatedBB); renameVariables(currentAlloca, dominatedBB);
} }
} }

View File

@@ -1,4 +1,6 @@
#include "SCCP.h" #include "SCCP.h"
#include "Dom.h"
#include "Liveness.h"
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <cmath> // For std::fmod, std::fabs #include <cmath> // For std::fmod, std::fabs
@@ -236,6 +238,8 @@ SSAPValue SCCPContext::ComputeConstant(UnaryInst *unaryInst, SSAPValue operandVa
if (operandVal.constant_type == ValueType::Integer) { if (operandVal.constant_type == ValueType::Integer) {
int val = std::get<int>(operandVal.constantVal); int val = std::get<int>(operandVal.constantVal);
switch (unaryInst->getKind()) { switch (unaryInst->getKind()) {
case Instruction::kAdd:
return SSAPValue(val);
case Instruction::kNeg: case Instruction::kNeg:
return SSAPValue(-val); return SSAPValue(-val);
case Instruction::kNot: case Instruction::kNot:
@@ -246,6 +250,8 @@ SSAPValue SCCPContext::ComputeConstant(UnaryInst *unaryInst, SSAPValue operandVa
} else if (operandVal.constant_type == ValueType::Float) { } else if (operandVal.constant_type == ValueType::Float) {
float val = std::get<float>(operandVal.constantVal); float val = std::get<float>(operandVal.constantVal);
switch (unaryInst->getKind()) { switch (unaryInst->getKind()) {
case Instruction::kAdd:
return SSAPValue(val);
case Instruction::kFNeg: case Instruction::kFNeg:
return SSAPValue(-val); return SSAPValue(-val);
case Instruction::kFNot: case Instruction::kFNot:
@@ -593,6 +599,7 @@ bool SCCPContext::PropagateConstants(Function *func) {
} }
// 实际删除指令 // 实际删除指令
// TODO: 删除的逻辑需要考虑修改
for (Instruction *inst : instsToDelete) { for (Instruction *inst : instsToDelete) {
// 在尝试删除之前,先检查指令是否仍然附加到其父基本块。 // 在尝试删除之前,先检查指令是否仍然附加到其父基本块。
// 如果它已经没有父块,可能说明它已被其他方式处理或已处于无效状态。 // 如果它已经没有父块,可能说明它已被其他方式处理或已处于无效状态。
@@ -752,6 +759,7 @@ void SCCPContext::RemoveDeadBlock(BasicBlock *bb, Function *func) {
} }
for (BasicBlock *succ : succs_to_update) { for (BasicBlock *succ : succs_to_update) {
RemovePhiIncoming(succ, bb); RemovePhiIncoming(succ, bb);
succ->removePredecessor(bb);
} }
func->removeBasicBlock(bb); // 从函数中移除基本块 func->removeBasicBlock(bb); // 从函数中移除基本块
@@ -864,7 +872,9 @@ bool SCCP::runOnFunction(Function *F, AnalysisManager &AM) {
} }
void SCCP::getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const { void SCCP::getAnalysisUsage(std::set<void *> &analysisDependencies, std::set<void *> &analysisInvalidations) const {
analysisInvalidations.insert(nullptr); // 表示使所有默认分析失效 // analysisInvalidations.insert(nullptr); // 表示使所有默认分析失效
analysisInvalidations.insert(&DominatorTreeAnalysisPass::ID); // 支配树可能受影响
analysisInvalidations.insert(&LivenessAnalysisPass::ID); // 活跃性分析很可能失效
} }
} // namespace sysy } // namespace sysy