mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-23 11:13:28 +01:00
Implement the isSafeToDiscardIfUnused predicate and use it in globalopt and
globaldce. Globaldce was already removing linkonce globals, but globalopt was not. llvm-svn: 158476
This commit is contained in:
parent
e1c5e7bf9f
commit
41f1be2080
@ -164,6 +164,12 @@ public:
|
||||
return Linkage == CommonLinkage;
|
||||
}
|
||||
|
||||
/// isDiscardableIfUnused - Whether the definition of this global may be
|
||||
/// discarded if it is not used in its compilation unit.
|
||||
static bool isDiscardableIfUnused(LinkageTypes Linkage) {
|
||||
return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
|
||||
}
|
||||
|
||||
/// mayBeOverridden - Whether the definition of this global may be replaced
|
||||
/// by something non-equivalent at link time. For example, if a function has
|
||||
/// weak linkage then the code defining it may be replaced by different code.
|
||||
@ -221,6 +227,10 @@ public:
|
||||
void setLinkage(LinkageTypes LT) { Linkage = LT; }
|
||||
LinkageTypes getLinkage() const { return Linkage; }
|
||||
|
||||
bool isDiscardableIfUnused() const {
|
||||
return isDiscardableIfUnused(Linkage);
|
||||
}
|
||||
|
||||
bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
|
||||
|
||||
bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
|
||||
|
@ -65,7 +65,7 @@ bool GlobalDCE::runOnModule(Module &M) {
|
||||
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
||||
Changed |= RemoveUnusedGlobalValue(*I);
|
||||
// Functions with external linkage are needed if they have a body
|
||||
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
|
||||
if (!I->isDiscardableIfUnused() &&
|
||||
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
|
||||
GlobalIsNeeded(I);
|
||||
}
|
||||
@ -75,7 +75,7 @@ bool GlobalDCE::runOnModule(Module &M) {
|
||||
Changed |= RemoveUnusedGlobalValue(*I);
|
||||
// Externally visible & appending globals are needed, if they have an
|
||||
// initializer.
|
||||
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage() &&
|
||||
if (!I->isDiscardableIfUnused() &&
|
||||
!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
|
||||
GlobalIsNeeded(I);
|
||||
}
|
||||
@ -84,7 +84,7 @@ bool GlobalDCE::runOnModule(Module &M) {
|
||||
I != E; ++I) {
|
||||
Changed |= RemoveUnusedGlobalValue(*I);
|
||||
// Externally visible aliases are needed.
|
||||
if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage())
|
||||
if (!I->isDiscardableIfUnused())
|
||||
GlobalIsNeeded(I);
|
||||
}
|
||||
|
||||
|
@ -1716,7 +1716,7 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
|
||||
/// possible. If we make a change, return true.
|
||||
bool GlobalOpt::ProcessGlobal(GlobalVariable *GV,
|
||||
Module::global_iterator &GVI) {
|
||||
if (!GV->hasLocalLinkage())
|
||||
if (!GV->isDiscardableIfUnused())
|
||||
return false;
|
||||
|
||||
// Do more involved optimizations if the global is internal.
|
||||
|
@ -1,9 +1,25 @@
|
||||
; RUN: opt < %s -globalopt -S | not grep internal
|
||||
; RUN: opt < %s -globalopt -S | FileCheck %s
|
||||
|
||||
@G = internal global i32 123 ; <i32*> [#uses=1]
|
||||
@G1 = internal global i32 123 ; <i32*> [#uses=1]
|
||||
|
||||
define void @foo() {
|
||||
store i32 1, i32* @G
|
||||
; CHECK-NOT: @G1
|
||||
; CHECK: @G2
|
||||
; CHECK-NOT: @G3
|
||||
|
||||
define void @foo1() {
|
||||
; CHECK: define void @foo
|
||||
; CHECK-NEXT: ret
|
||||
store i32 1, i32* @G1
|
||||
ret void
|
||||
}
|
||||
|
||||
@G2 = linkonce_odr constant i32 42
|
||||
|
||||
define void @foo2() {
|
||||
; CHECK: define void @foo2
|
||||
; CHECK-NEXT: store
|
||||
store i32 1, i32* @G2
|
||||
ret void
|
||||
}
|
||||
|
||||
@G3 = linkonce_odr constant i32 42
|
||||
|
Loading…
Reference in New Issue
Block a user