Skip to content

Commit 3d0752b

Browse files
dianqktstellar
authored andcommitted
[GlobalOpt] Don't replace aliasee with alias that has weak linkage (#91483)
Fixes #91312. Don't perform the transform if the alias may be replaced at link time. (cherry picked from commit c796900)
1 parent 9208786 commit 3d0752b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,9 @@ static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
22122212

22132213
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
22142214
bool &RenameTarget) {
2215+
if (GA.isWeakForLinker())
2216+
return false;
2217+
22152218
RenameTarget = false;
22162219
bool Ret = false;
22172220
if (hasUseOtherThanLLVMUsed(GA, U))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --include-generated-funcs --version 4
2+
; RUN: opt < %s -passes=globalopt -S | FileCheck %s
3+
4+
@f1_alias = linkonce_odr hidden alias void (), ptr @f1
5+
@f2_alias = linkonce_odr hidden alias void (), ptr @f2
6+
7+
define void @foo() {
8+
call void @f1_alias()
9+
ret void
10+
}
11+
12+
define void @bar() {
13+
call void @f1()
14+
ret void
15+
}
16+
17+
define void @baz() {
18+
call void @f2_alias()
19+
ret void
20+
}
21+
22+
; We cannot use `f1_alias` to replace `f1` because they are both in use
23+
; and `f1_alias` could be replaced at link time.
24+
define internal void @f1() {
25+
ret void
26+
}
27+
28+
; FIXME: We can use `f2_alias` to replace `f2` because `b2` is not in use.
29+
define internal void @f2() {
30+
ret void
31+
}
32+
;.
33+
; CHECK: @f1_alias = linkonce_odr hidden alias void (), ptr @f1
34+
; CHECK: @f2_alias = linkonce_odr hidden alias void (), ptr @f2
35+
;.
36+
; CHECK-LABEL: define void @foo() local_unnamed_addr {
37+
; CHECK-NEXT: call void @f1_alias()
38+
; CHECK-NEXT: ret void
39+
;
40+
;
41+
; CHECK-LABEL: define void @bar() local_unnamed_addr {
42+
; CHECK-NEXT: call void @f1()
43+
; CHECK-NEXT: ret void
44+
;
45+
;
46+
; CHECK-LABEL: define void @baz() local_unnamed_addr {
47+
; CHECK-NEXT: call void @f2_alias()
48+
; CHECK-NEXT: ret void
49+
;
50+
;
51+
; CHECK-LABEL: define internal void @f1() {
52+
; CHECK-NEXT: ret void
53+
;
54+
;
55+
; CHECK-LABEL: define internal void @f2() {
56+
; CHECK-NEXT: ret void
57+
;

0 commit comments

Comments
 (0)