Skip to content

Commit 861caf9

Browse files
authored
[SCCP] Remove LoadInst if it loaded from Constant GlobalVariable (llvm#107245)
This patch removes the `LoadInst` when it loaded from Constant GlobalVariable. This allows `canRemoveInstruction` function to be removed.
1 parent 5602bd5 commit 861caf9

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

llvm/lib/Transforms/IPO/SCCP.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,13 @@ static bool runIPSCCP(
337337
continue;
338338
LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
339339
<< "' is constant!\n");
340-
while (!GV->use_empty()) {
341-
StoreInst *SI = cast<StoreInst>(GV->user_back());
342-
SI->eraseFromParent();
340+
for (User *U : make_early_inc_range(GV->users())) {
341+
// We can remove LoadInst here, because we already replaced its users
342+
// with a constant.
343+
assert((isa<StoreInst>(U) || isa<LoadInst>(U)) &&
344+
"Only Store|Load Instruction can be user of GlobalVariable at "
345+
"reaching here.");
346+
cast<Instruction>(U)->eraseFromParent();
343347
}
344348

345349
// Try to create a debug constant expression for the global variable

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,6 @@ bool SCCPSolver::isOverdefined(const ValueLatticeElement &LV) {
5454
return !LV.isUnknownOrUndef() && !SCCPSolver::isConstant(LV);
5555
}
5656

57-
static bool canRemoveInstruction(Instruction *I) {
58-
if (wouldInstructionBeTriviallyDead(I))
59-
return true;
60-
61-
// Some instructions can be handled but are rejected above. Catch
62-
// those cases by falling through to here.
63-
// TODO: Mark globals as being constant earlier, so
64-
// TODO: wouldInstructionBeTriviallyDead() knows that atomic loads
65-
// TODO: are safe to remove.
66-
return isa<LoadInst>(I);
67-
}
68-
6957
bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
7058
Constant *Const = getConstantOrNull(V);
7159
if (!Const)
@@ -75,8 +63,7 @@ bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
7563
// Calls with "clang.arc.attachedcall" implicitly use the return value and
7664
// those uses cannot be updated with a constant.
7765
CallBase *CB = dyn_cast<CallBase>(V);
78-
if (CB && ((CB->isMustTailCall() &&
79-
!canRemoveInstruction(CB)) ||
66+
if (CB && ((CB->isMustTailCall() && !wouldInstructionBeTriviallyDead(CB)) ||
8067
CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) {
8168
Function *F = CB->getCalledFunction();
8269

@@ -244,7 +231,7 @@ bool SCCPSolver::simplifyInstsInBlock(BasicBlock &BB,
244231
if (Inst.getType()->isVoidTy())
245232
continue;
246233
if (tryToReplaceWithConstant(&Inst)) {
247-
if (canRemoveInstruction(&Inst))
234+
if (wouldInstructionBeTriviallyDead(&Inst))
248235
Inst.eraseFromParent();
249236

250237
MadeChanges = true;

0 commit comments

Comments
 (0)