[llvm] [SCCP] Determine early whether GlobalVariable is Constant or not (PR #107245)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 4 07:04:23 PDT 2024
https://github.com/ParkHanbum created https://github.com/llvm/llvm-project/pull/107245
Previously, we determine if 1.`LoadInst` could be replaced with `Constant`, and 2. if the instruction can be removed by used the `wouldInstructionBeTriviallyDead` function. if it can then we replace it with `Constant`. However, in special case, we do not set `GlobalVariable` to `Constant` even if it is a constant, and `wouldInstructionBeTriviallyDead` returns false.
Because of this, we created a `canRemoveInstruction` function to determine if an instruction is removable, returning true if and only if `LoadInst` can be replaced with `Constant`.
This patch eliminates the `canRemoveInstruction` function by setting `GlobalVariable` to `Constant` at the end of Solver's Solve if `GlobalVariable` can be specified as `Constant` and can be replaced by a `Constant`.
>From c86317976b42bec447eb2708abb8f5e1df775f09 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] Determine early whether GlobalVariable is Constant or
not
Previously, we determine if 1.`LoadInst` could be replaced with
`Constant`, and 2. if the instruction can be removed by used the
`wouldInstructionBeTriviallyDead` function. if it can then we
replace it with `Constant`. However, in special case, we do not set
`GlobalVariable` to `Constant` even if it is a constant, and
`wouldInstructionBeTriviallyDead` returns false.
Because of this, we created a `canRemoveInstruction` function to
determine if an instruction is removable, returning true if and
only if `LoadInst` can be replaced with `Constant`.
This patch eliminates the `canRemoveInstruction` function by setting
`GlobalVariable` to `Constant` at the end of Solver's Solve if
`GlobalVariable` can be specified as `Constant` and can be replaced
by a `Constant`.
---
llvm/lib/Transforms/Utils/SCCPSolver.cpp | 28 ++++++++++++------------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index c944859cc69b89..416658202378a5 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)
@@ -75,7 +63,7 @@ bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
// those uses cannot be updated with a constant.
CallBase *CB = dyn_cast<CallBase>(V);
if (CB && ((CB->isMustTailCall() &&
- !canRemoveInstruction(CB)) ||
+ !wouldInstructionBeTriviallyDead(CB)) ||
CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) {
Function *F = CB->getCalledFunction();
@@ -92,6 +80,7 @@ bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
// Replaces all of the uses of a variable with uses of the constant.
V->replaceAllUsesWith(Const);
+
return true;
}
@@ -243,7 +232,7 @@ bool SCCPSolver::simplifyInstsInBlock(BasicBlock &BB,
if (Inst.getType()->isVoidTy())
continue;
if (tryToReplaceWithConstant(&Inst)) {
- if (canRemoveInstruction(&Inst))
+ if (wouldInstructionBeTriviallyDead(&Inst))
Inst.eraseFromParent();
MadeChanges = true;
@@ -1945,6 +1934,17 @@ void SCCPInstVisitor::solve() {
visit(BB);
}
}
+
+ // If GlobalVariable is the constant, set it constant.
+ for (const auto &I : getTrackedGlobals()) {
+ const ValueLatticeElement &LV = I.second;
+ if (SCCPSolver::isOverdefined(LV))
+ continue;
+ if (SCCPSolver::isConstant(LV)) {
+ GlobalVariable *GV = I.first;
+ GV->setConstant(true);
+ }
+ }
}
bool SCCPInstVisitor::resolvedUndef(Instruction &I) {
More information about the llvm-commits
mailing list