Skip to content

Commit 067c4aa

Browse files
committed
[GlobalOpt] Replace aliasee if it is not in use
1 parent ab7be41 commit 067c4aa

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,17 @@ static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
22152215
return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
22162216
}
22172217

2218+
static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V,
2219+
const LLVMUsed &U) {
2220+
unsigned N = 2;
2221+
assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
2222+
"We should have removed the duplicated "
2223+
"element from llvm.compiler.used");
2224+
if (U.usedCount(&V) || U.compilerUsedCount(&V))
2225+
++N;
2226+
return V.hasNUsesOrMore(N);
2227+
}
2228+
22182229
static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
22192230
if (!GV.hasLocalLinkage())
22202231
return true;
@@ -2224,8 +2235,6 @@ static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
22242235

22252236
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
22262237
bool &RenameTarget) {
2227-
if (GA.isWeakForLinker())
2228-
return false;
22292238

22302239
RenameTarget = false;
22312240
bool Ret = false;
@@ -2245,6 +2254,13 @@ static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
22452254
// define ... @a(...)
22462255
Constant *Aliasee = GA.getAliasee();
22472256
GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2257+
// Do not perform the transform if alias may be replaced at link time while
2258+
// someone is using the aliasee (e.g., multiple aliases potentially target it
2259+
// or someone calls it).
2260+
if (GA.isWeakForLinker())
2261+
Ret = false;
2262+
if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
2263+
return Ret;
22482264
if (mayHaveOtherReferences(*Target, U))
22492265
return Ret;
22502266

llvm/test/Transforms/GlobalOpt/alias-weak.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ define internal void @f1() {
2525
ret void
2626
}
2727

28-
; FIXME: We can use `f2_alias` to replace `f2` because `b2` is not in use.
28+
; We can use `f2_alias` to replace `f2` because `b2` is not in use.
2929
define internal void @f2() {
3030
ret void
3131
}
3232
;.
3333
; CHECK: @f1_alias = linkonce_odr hidden alias void (), ptr @f1
34-
; CHECK: @f2_alias = linkonce_odr hidden alias void (), ptr @f2
3534
;.
3635
; CHECK-LABEL: define void @foo() local_unnamed_addr {
3736
; CHECK-NEXT: call void @f1_alias()
@@ -52,6 +51,6 @@ define internal void @f2() {
5251
; CHECK-NEXT: ret void
5352
;
5453
;
55-
; CHECK-LABEL: define internal void @f2() {
54+
; CHECK-LABEL: define linkonce_odr hidden void @f2_alias() local_unnamed_addr {
5655
; CHECK-NEXT: ret void
5756
;

0 commit comments

Comments
 (0)