Implement the isSafeToDiscardIfUnused predicate and use it in globalopt and

globaldce. Globaldce was already removing linkonce globals, but globalopt was
not.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158476 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2012-06-14 22:48:13 +00:00
parent 312091ece3
commit 0397729d3b
4 changed files with 34 additions and 8 deletions

View File

@ -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); }

View File

@ -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);
}

View File

@ -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.

View File

@ -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