[SCCP] Move logic for removing ssa.copy into Solver (NFC)

So it can be reused between IPSCCP and SCCP.

Make the implementation a bit more efficient by only lookup the
PredicateInfo once.
This commit is contained in:
Nikita Popov
2025-06-19 17:26:54 +02:00
parent 36af7345df
commit f4db14229c
3 changed files with 27 additions and 13 deletions

View File

@@ -77,6 +77,8 @@ public:
LLVM_ABI void addPredicateInfo(Function &F, DominatorTree &DT,
AssumptionCache &AC);
LLVM_ABI void removeSSACopies(Function &F);
/// markBlockExecutable - This method can be used by clients to mark all of
/// the blocks that are known to be intrinsically live in the processed unit.
/// This returns true if the block was not considered live before.

View File

@@ -250,19 +250,7 @@ static bool runIPSCCP(
if (!DeadBB->hasAddressTaken())
DTU.deleteBB(DeadBB);
for (BasicBlock &BB : F) {
for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
if (Solver.getPredicateInfoFor(&Inst)) {
if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
Value *Op = II->getOperand(0);
Inst.replaceAllUsesWith(Op);
Inst.eraseFromParent();
}
}
}
}
}
Solver.removeSSACopies(F);
}
// If we inferred constant or undef return values for a function, we replaced

View File

@@ -764,6 +764,26 @@ public:
FnPredicateInfo.insert({&F, std::make_unique<PredicateInfo>(F, DT, AC)});
}
void removeSSACopies(Function &F) {
auto It = FnPredicateInfo.find(&F);
if (It == FnPredicateInfo.end())
return;
for (BasicBlock &BB : F) {
for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
if (It->second->getPredicateInfoFor(&Inst)) {
if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
Value *Op = II->getOperand(0);
Inst.replaceAllUsesWith(Op);
Inst.eraseFromParent();
}
}
}
}
}
}
void visitCallInst(CallInst &I) { visitCallBase(I); }
bool markBlockExecutable(BasicBlock *BB);
@@ -2168,6 +2188,10 @@ void SCCPSolver::addPredicateInfo(Function &F, DominatorTree &DT,
Visitor->addPredicateInfo(F, DT, AC);
}
void SCCPSolver::removeSSACopies(Function &F) {
Visitor->removeSSACopies(F);
}
bool SCCPSolver::markBlockExecutable(BasicBlock *BB) {
return Visitor->markBlockExecutable(BB);
}