@@ -433,6 +433,7 @@ void SCCPContext::ProcessInstruction(Instruction *inst) {
case Instruction : : kBr : // 对应 kBr
case Instruction : : kCondBr : // 对应 kCondBr
case Instruction : : kReturn : // 对应 kReturn
case Instruction : : kUnreachable : // 对应 kUnreachable
// 终结符指令不产生值
newState = SSAPValue ( ) ; // 保持 Top
break ;
@@ -451,8 +452,10 @@ void SCCPContext::ProcessInstruction(Instruction *inst) {
// 特殊处理终结符指令,影响 CFG 边的可达性
if ( inst - > isTerminator ( ) ) {
if ( auto branchInst = dynamic_cast < CondBrInst * > ( inst ) ) {
if ( branchInst - > isCondBr ( ) ) { // 使用 kCondBr
if ( inst - > isBranch ( ) ) {
if ( inst - > isCondBr ( ) ) { // 使用 kCondBr
CondBrInst * branchInst = static_cast < CondBrInst * > ( inst ) ;
SSAPValue condVal = GetValueState ( branchInst - > getOperand ( 0 ) ) ;
if ( condVal . state = = LatticeVal : : Constant ) {
bool condition_is_true = false ;
@@ -472,7 +475,8 @@ void SCCPContext::ProcessInstruction(Instruction *inst) {
AddEdgeToWorkList ( branchInst - > getParent ( ) , branchInst - > getElseBlock ( ) ) ;
}
} else { // 无条件分支 (kBr)
AddEdgeToWorkList ( branchInst - > getParent ( ) , branch Inst- > getThenBlock ( ) ) ;
UncondBrInst * branchInst = static_cast < UncondBr Inst * > ( inst ) ;
AddEdgeToWorkList ( branchInst - > getParent ( ) , branchInst - > getBlock ( ) ) ;
}
}
}
@@ -560,8 +564,7 @@ bool SCCPContext::PropagateConstants(Function *func) {
}
inst - > replaceAllUsesWith ( constVal ) ;
instsToDelete . push_back ( inst ) ;
// it = bb->removeInst(it); // 从块中移除指令
it = bb - > getInstructions ( ) . erase ( it ) ;
+ + it ;
changed = true ;
} else {
// 如果操作数是常量,直接替换为常量值(常量折叠)
@@ -595,14 +598,7 @@ bool SCCPContext::PropagateConstants(Function *func) {
// 如果它已经没有父块,可能说明它已被其他方式处理或已处于无效状态。
if ( inst - > getParent ( ) ! = nullptr ) {
// 调用负责完整删除的函数, 该函数应负责清除uses并将其从父块中移除。
SysYIROptUtils : : usedelete_withinstdelte ( inst ) ;
if ( inst - > getParent ( ) ! = nullptr ) {
// 如果执行到这里,说明 usedelete_withinstdelte 没有成功将其从父块中移除。
if ( DEBUG ) {
std : : cerr < < " Warning: Instruction " < < inst - > getName ( )
< < " was not fully deleted by usedelete_withinstdelte and is still in parent. " < < std : : endl ;
}
}
SysYIROptUtils : : usedelete ( inst ) ;
}
else {
// 指令已不属于任何父块,无需再次删除。
@@ -642,8 +638,10 @@ bool SCCPContext::SimplifyControlFlow(Function *func) {
continue ; // 只处理可达块
Instruction * terminator = bb - > terminator ( ) - > get ( ) ;
if ( auto branchInst = dynamic_cast < CondBrInst * > ( terminator ) ) {
if ( branchInst - > isCondBr ( ) ) { // 检查是否是条件分支 (kCondBr)
if ( terminator - > isBranch ( ) ) {
if ( terminator - > isCondBr ( ) ) { // 检查是否是条件分支 (kCondBr)
CondBrInst * branchInst = static_cast < CondBrInst * > ( terminator ) ;
SSAPValue condVal = GetValueState ( branchInst - > getOperand ( 0 ) ) ;
if ( condVal . state = = LatticeVal : : Constant ) {
bool condition_is_true = false ;
@@ -680,8 +678,9 @@ std::unordered_set<BasicBlock *> SCCPContext::FindReachableBlocks(Function *func
if ( ! terminator )
continue ;
if ( auto branchInst = dynamic_cast < CondBrInst * > ( terminator ) ) {
if ( branchInst - > isCondBr ( ) ) { // 检查是否是条件分支 (kCondBr)
if ( terminator - > isBranch ( ) ) {
if ( terminator - > isCondBr ( ) ) { // 检查是否是条件分支 (kCondBr)
CondBrInst * branchInst = static_cast < CondBrInst * > ( terminator ) ;
SSAPValue condVal = GetValueState ( branchInst - > getOperand ( 0 ) ) ;
if ( condVal . state = = LatticeVal : : Constant ) {
bool condition_is_true = false ;
@@ -713,14 +712,16 @@ std::unordered_set<BasicBlock *> SCCPContext::FindReachableBlocks(Function *func
}
}
} else { // 无条件分支 (kBr)
BasicBlock * targetBlock = branch Inst- > getThenBlock ( ) ;
UncondBrInst * branchInst = static_cast < UncondBr Inst * > ( terminator ) ;
BasicBlock * targetBlock = branchInst - > getBlock ( ) ;
if ( reachable . find ( targetBlock ) = = reachable . end ( ) ) {
reachable . insert ( targetBlock ) ;
q . push ( targetBlock ) ;
}
}
} else if ( auto retInst = dynamic_cast < ReturnInst * > ( terminator ) ) {
} else if ( terminator - > is Return( ) | | terminator - > isUnreachable ( ) ) {
// ReturnInst 没有后继,不需要处理
// UnreachableInst 也没有后继,不需要处理
}
}
return reachable ;
@@ -771,16 +772,12 @@ void SCCPContext::SimplifyBranch(CondBrInst *brInst, bool condVal) {
if ( condVal ) { // 条件为真,跳转到真分支
builder - > createUncondBrInst ( trueBlock ) ; // 插入无条件分支 kBr
SysYIROptUtils : : usedelete ( brInst ) ; // 移除旧的条件分支指令
// TODO now: 移出指令
parentBB - > removeSuccessor ( falseBlock ) ;
falseBlock - > removePredecessor ( parentBB ) ;
RemovePhiIncoming ( falseBlock , parentBB ) ;
} else { // 条件为假,跳转到假分支
builder - > createUncondBrInst ( falseBlock ) ; // 插入无条件分支 kBr
SysYIROptUtils : : usedelete ( brInst ) ; // 移除旧的条件分支指令
// TODO now: 移出指令
parentBB - > removeSuccessor ( trueBlock ) ;
trueBlock - > removePredecessor ( parentBB ) ;
RemovePhiIncoming ( trueBlock , parentBB ) ;
@@ -793,8 +790,9 @@ void SCCPContext::UpdateTerminator(BasicBlock *predBB, BasicBlock *removedSucc)
if ( ! terminator )
return ;
if ( auto branchInst = dynamic_cast < CondBrInst * > ( terminator ) ) {
if ( branchInst - > isCondBr ( ) ) { // 如果是条件分支
if ( terminator - > isBranch ( ) ) {
if ( terminator - > isCondBr ( ) ) { // 如果是条件分支
CondBrInst * branchInst = static_cast < CondBrInst * > ( terminator ) ;
if ( branchInst - > getThenBlock ( ) = = removedSucc ) {
if ( DEBUG ) {
std : : cout < < " Updating cond br in " < < predBB - > getName ( ) < < " : True block ( " < < removedSucc - > getName ( )
@@ -802,7 +800,7 @@ void SCCPContext::UpdateTerminator(BasicBlock *predBB, BasicBlock *removedSucc)
}
builder - > setPosition ( predBB , predBB - > findInstIterator ( branchInst ) ) ;
builder - > createUncondBrInst ( branchInst - > getElseBlock ( ) ) ;
SysYIROptUtils : : usedelete_withinstdelte ( branchInst ) ;
SysYIROptUtils : : usedelete ( branchInst ) ;
predBB - > removeSuccessor ( removedSucc ) ;
} else if ( branchInst - > getElseBlock ( ) = = removedSucc ) {
if ( DEBUG ) {
@@ -811,16 +809,17 @@ void SCCPContext::UpdateTerminator(BasicBlock *predBB, BasicBlock *removedSucc)
}
builder - > setPosition ( predBB , predBB - > findInstIterator ( branchInst ) ) ;
builder - > createUncondBrInst ( branchInst - > getThenBlock ( ) ) ;
SysYIROptUtils : : usedelete_withinstdelte ( branchInst ) ;
SysYIROptUtils : : usedelete ( branchInst ) ;
predBB - > removeSuccessor ( removedSucc ) ;
}
} else { // 无条件分支 (kBr)
if ( branchInst - > getThenBlock ( ) = = removedSucc ) {
UncondBrInst * branchInst = static_cast < UncondBrInst * > ( terminator ) ;
if ( branchInst - > getBlock ( ) = = removedSucc ) {
if ( DEBUG ) {
std : : cout < < " Updating unconditional br in " < < predBB - > getName ( ) < < " : Target block ( "
< < removedSucc - > getName ( ) < < " ) removed. Replacing with Unreachable. " < < std : : endl ;
}
SysYIROptUtils : : usedelete_withinstdelte ( branchInst ) ;
SysYIROptUtils : : usedelete ( branchInst ) ;
predBB - > removeSuccessor ( removedSucc ) ;
builder - > setPosition ( predBB , predBB - > end ( ) ) ;
builder - > createUnreachableInst ( ) ;