[midend-SCCP]修改BaiscBlock的析构逻辑,将CFG修改的职责交给优化遍,注释Mem2Reg的调试信息。
This commit is contained in:
@@ -514,12 +514,15 @@ public:
|
||||
explicit BasicBlock(Function *parent, const std::string &name = "")
|
||||
: Value(Type::getLabelType(), name), parent(parent) {}
|
||||
~BasicBlock() override {
|
||||
for (auto pre : predecessors) {
|
||||
pre->removeSuccessor(this);
|
||||
}
|
||||
for (auto suc : successors) {
|
||||
suc->removePredecessor(this);
|
||||
}
|
||||
// for (auto pre : predecessors) {
|
||||
// pre->removeSuccessor(this);
|
||||
// }
|
||||
// for (auto suc : successors) {
|
||||
// suc->removePredecessor(this);
|
||||
// }
|
||||
// 这些关系应该在 BasicBlock 被从 Function 中移除时,
|
||||
// 由负责 CFG 优化的 Pass (例如 SCCP 的 RemoveDeadBlock) 显式地清理。
|
||||
// 析构函数只负责清理 BasicBlock 自身拥有的资源(例如,指令列表)。
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -240,7 +240,9 @@ void Mem2RegContext::renameVariables(AllocaInst *currentAlloca, BasicBlock *curr
|
||||
// loadInst->getPointer() 返回 AllocaInst*
|
||||
// 将 LoadInst 的所有用途替换为当前 alloca 值栈顶部的 SSA 值
|
||||
assert(!allocaToValueStackMap[alloca].empty() && "Value stack empty for alloca during load replacement!");
|
||||
std::cout << "Mem2Reg: Replacing load " << loadInst->getPointer()->getName() << " with SSA value." << std::endl;
|
||||
if(DEBUG){
|
||||
std::cout << "Mem2Reg: Replacing load " << loadInst->getPointer()->getName() << " with SSA value." << std::endl;
|
||||
}
|
||||
loadInst->replaceAllUsesWith(allocaToValueStackMap[alloca].top());
|
||||
instIter = SysYIROptUtils::usedelete(instIter);
|
||||
instDeleted = true;
|
||||
@@ -256,7 +258,9 @@ void Mem2RegContext::renameVariables(AllocaInst *currentAlloca, BasicBlock *curr
|
||||
if (storeInst->getPointer() == alloca) {
|
||||
// 假设 storeInst->getPointer() 返回 AllocaInst*
|
||||
// 将 StoreInst 存储的值作为新的 SSA 值,压入值栈
|
||||
std::cout << "Mem2Reg: Replacing store to " << storeInst->getPointer()->getName() << " with SSA value." << std::endl;
|
||||
if(DEBUG){
|
||||
std::cout << "Mem2Reg: Replacing store to " << storeInst->getPointer()->getName() << " with SSA value." << std::endl;
|
||||
}
|
||||
allocaToValueStackMap[alloca].push(storeInst->getValue());
|
||||
localStackPushed.push(storeInst->getValue()); // 记录以便弹出
|
||||
instIter = SysYIROptUtils::usedelete(instIter);
|
||||
@@ -297,7 +301,9 @@ void Mem2RegContext::renameVariables(AllocaInst *currentAlloca, BasicBlock *curr
|
||||
if(dominatedBlocks){
|
||||
for (auto dominatedBB : *dominatedBlocks) {
|
||||
if (dominatedBB) {
|
||||
std::cout << "Mem2Reg: Recursively renaming variables in dominated block: " << dominatedBB->getName() << std::endl;
|
||||
if(DEBUG){
|
||||
std::cout << "Mem2Reg: Recursively renaming variables in dominated block: " << dominatedBB->getName() << std::endl;
|
||||
}
|
||||
renameVariables(currentAlloca, dominatedBB);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "SCCP.h"
|
||||
#include "Dom.h"
|
||||
#include "Liveness.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cmath> // For std::fmod, std::fabs
|
||||
@@ -236,6 +238,8 @@ SSAPValue SCCPContext::ComputeConstant(UnaryInst *unaryInst, SSAPValue operandVa
|
||||
if (operandVal.constant_type == ValueType::Integer) {
|
||||
int val = std::get<int>(operandVal.constantVal);
|
||||
switch (unaryInst->getKind()) {
|
||||
case Instruction::kAdd:
|
||||
return SSAPValue(val);
|
||||
case Instruction::kNeg:
|
||||
return SSAPValue(-val);
|
||||
case Instruction::kNot:
|
||||
@@ -246,6 +250,8 @@ SSAPValue SCCPContext::ComputeConstant(UnaryInst *unaryInst, SSAPValue operandVa
|
||||
} else if (operandVal.constant_type == ValueType::Float) {
|
||||
float val = std::get<float>(operandVal.constantVal);
|
||||
switch (unaryInst->getKind()) {
|
||||
case Instruction::kAdd:
|
||||
return SSAPValue(val);
|
||||
case Instruction::kFNeg:
|
||||
return SSAPValue(-val);
|
||||
case Instruction::kFNot:
|
||||
@@ -593,6 +599,7 @@ bool SCCPContext::PropagateConstants(Function *func) {
|
||||
}
|
||||
|
||||
// 实际删除指令
|
||||
// TODO: 删除的逻辑需要考虑修改
|
||||
for (Instruction *inst : instsToDelete) {
|
||||
// 在尝试删除之前,先检查指令是否仍然附加到其父基本块。
|
||||
// 如果它已经没有父块,可能说明它已被其他方式处理或已处于无效状态。
|
||||
@@ -752,6 +759,7 @@ void SCCPContext::RemoveDeadBlock(BasicBlock *bb, Function *func) {
|
||||
}
|
||||
for (BasicBlock *succ : succs_to_update) {
|
||||
RemovePhiIncoming(succ, bb);
|
||||
succ->removePredecessor(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 {
|
||||
analysisInvalidations.insert(nullptr); // 表示使所有默认分析失效
|
||||
// analysisInvalidations.insert(nullptr); // 表示使所有默认分析失效
|
||||
analysisInvalidations.insert(&DominatorTreeAnalysisPass::ID); // 支配树可能受影响
|
||||
analysisInvalidations.insert(&LivenessAnalysisPass::ID); // 活跃性分析很可能失效
|
||||
}
|
||||
|
||||
} // namespace sysy
|
||||
Reference in New Issue
Block a user