[llvm] [SCCP] Remove LoadInst if it loaded from Constant GlobalVariable (PR #107245)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 4 22:56:51 PDT 2024
https://github.com/ParkHanbum updated https://github.com/llvm/llvm-project/pull/107245
>From 4f8fd3c2334e17a78ed546031315a9168e981725 Mon Sep 17 00:00:00 2001
From: hanbeom <kese111 at gmail.com>
Date: Wed, 4 Sep 2024 19:33:42 +0900
Subject: [PATCH] [SCCP] Remove LoadInst if it loaded from Constant
GlobalVariable
This patch removes the `LoadInst` when it loaded from Constant
GlobalVariable. This allows `canRemoveInstruction` function to be
removed.
---
llvm/lib/Transforms/IPO/SCCP.cpp | 7 +++++--
llvm/lib/Transforms/Utils/SCCPSolver.cpp | 17 ++---------------
2 files changed, 7 insertions(+), 17 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/SCCP.cpp b/llvm/lib/Transforms/IPO/SCCP.cpp
index 94ae511b2e4a1e..ceae22d3cc9421 100644
--- a/llvm/lib/Transforms/IPO/SCCP.cpp
+++ b/llvm/lib/Transforms/IPO/SCCP.cpp
@@ -357,8 +357,11 @@ static bool runIPSCCP(
LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
<< "' is constant!\n");
while (!GV->use_empty()) {
- StoreInst *SI = cast<StoreInst>(GV->user_back());
- SI->eraseFromParent();
+ auto User = GV->user_back();
+ // We can remove LoadInst at here, because we already replace user of this
+ // to constant.
+ if (isa<StoreInst>(User) || isa<LoadInst>(User))
+ cast<Instruction>(User)->eraseFromParent();
}
// Try to create a debug constant expression for the global variable
diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index c944859cc69b89..2713ad80489d4f 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -53,18 +53,6 @@ bool SCCPSolver::isOverdefined(const ValueLatticeElement &LV) {
return !LV.isUnknownOrUndef() && !SCCPSolver::isConstant(LV);
}
-static bool canRemoveInstruction(Instruction *I) {
- if (wouldInstructionBeTriviallyDead(I))
- return true;
-
- // Some instructions can be handled but are rejected above. Catch
- // those cases by falling through to here.
- // TODO: Mark globals as being constant earlier, so
- // TODO: wouldInstructionBeTriviallyDead() knows that atomic loads
- // TODO: are safe to remove.
- return isa<LoadInst>(I);
-}
-
bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
Constant *Const = getConstantOrNull(V);
if (!Const)
@@ -74,8 +62,7 @@ bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
// Calls with "clang.arc.attachedcall" implicitly use the return value and
// those uses cannot be updated with a constant.
CallBase *CB = dyn_cast<CallBase>(V);
- if (CB && ((CB->isMustTailCall() &&
- !canRemoveInstruction(CB)) ||
+ if (CB && ((CB->isMustTailCall() && !wouldInstructionBeTriviallyDead(CB)) ||
CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) {
Function *F = CB->getCalledFunction();
@@ -243,7 +230,7 @@ bool SCCPSolver::simplifyInstsInBlock(BasicBlock &BB,
if (Inst.getType()->isVoidTy())
continue;
if (tryToReplaceWithConstant(&Inst)) {
- if (canRemoveInstruction(&Inst))
+ if (wouldInstructionBeTriviallyDead(&Inst))
Inst.eraseFromParent();
MadeChanges = true;
More information about the llvm-commits
mailing list