Skip to content

Commit b901a98

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

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

+18-3
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,9 +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;
2229-
22302238
RenameTarget = false;
22312239
bool Ret = false;
22322240
if (hasUseOtherThanLLVMUsed(GA, U))
@@ -2245,6 +2253,13 @@ static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
22452253
// define ... @a(...)
22462254
Constant *Aliasee = GA.getAliasee();
22472255
GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2256+
// Don't perform the transform if the alias may be replaced at link time while
2257+
// someone is using the aliasee (e.g., multiple aliases potentially target it
2258+
// or someone calls it).
2259+
if (GA.isWeakForLinker())
2260+
Ret = false;
2261+
if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
2262+
return Ret;
22482263
if (mayHaveOtherReferences(*Target, U))
22492264
return Ret;
22502265

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

+2-3
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
;

llvm/test/Transforms/LowerTypeTests/cfi-nounwind-direct-call.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ attributes #6 = { noreturn nounwind }
121121
; CHECK-LABEL: define dso_local noundef range(i32 0, 2) i32 @_Z10call_catchi
122122
; CHECK-SAME: (i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] !type [[META4]] !type [[META5]] !type [[META6]] {
123123
; CHECK-NEXT: entry:
124-
; CHECK-NEXT: store ptr @_Z9nothrow_ei.cfi_jt, ptr @catch_ptr, align 8, !tbaa [[TBAA7:![0-9]+]]
124+
; CHECK-NEXT: store ptr @.cfi.jumptable, ptr @catch_ptr, align 8, !tbaa [[TBAA7:![0-9]+]]
125125
; CHECK-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp ne i32 [[NUM]], 0
126126
; CHECK-NEXT: [[DOT_I:%.*]] = zext i1 [[TOBOOL_NOT_I]] to i32
127127
; CHECK-NEXT: ret i32 [[DOT_I]]
@@ -152,7 +152,7 @@ attributes #6 = { noreturn nounwind }
152152
;
153153
;
154154
; CHECK: Function Attrs: naked nocf_check noinline nounwind
155-
; CHECK-LABEL: define internal void @_Z9nothrow_ei.cfi_jt
155+
; CHECK-LABEL: define private void @.cfi.jumptable
156156
; CHECK-SAME: () #[[ATTR5:[0-9]+]] align 8 {
157157
; CHECK-NEXT: entry:
158158
; CHECK-NEXT: tail call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr nonnull @_Z9nothrow_ei) #[[ATTR7:[0-9]+]]

llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ attributes #8 = { noreturn nounwind }
174174
; CHECK-LABEL: define dso_local void @_Z10call_catchi(
175175
; CHECK-SAME: i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR0]] personality ptr @__gxx_personality_v0 !type [[META4]] !type [[META5]] !type [[META6]] {
176176
; CHECK-NEXT: entry:
177-
; CHECK-NEXT: store ptr @_Z7throw_ei.cfi_jt, ptr @catch_ptr, align 8, !tbaa [[TBAA11:![0-9]+]]
178-
; CHECK-NEXT: invoke void @_Z7throw_ei.cfi_jt(i32 noundef [[NUM]]) #[[ATTR8:[0-9]+]]
177+
; CHECK-NEXT: store ptr @.cfi.jumptable, ptr @catch_ptr, align 8, !tbaa [[TBAA11:![0-9]+]]
178+
; CHECK-NEXT: invoke void @.cfi.jumptable(i32 noundef [[NUM]]) #[[ATTR8:[0-9]+]]
179179
; CHECK-NEXT: to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]], !callees [[META13:![0-9]+]]
180180
; CHECK: lpad:
181181
; CHECK-NEXT: [[TMP0:%.*]] = landingpad { ptr, i32 }
@@ -220,7 +220,7 @@ attributes #8 = { noreturn nounwind }
220220
;
221221
;
222222
; CHECK: Function Attrs: naked nocf_check noinline
223-
; CHECK-LABEL: define internal void @_Z7throw_ei.cfi_jt(
223+
; CHECK-LABEL: define private void @.cfi.jumptable(
224224
; CHECK-SAME: ) #[[ATTR5:[0-9]+]] align 8 {
225225
; CHECK-NEXT: entry:
226226
; CHECK-NEXT: tail call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr nonnull @_Z7throw_ei) #[[ATTR6]]
@@ -236,6 +236,6 @@ attributes #8 = { noreturn nounwind }
236236
; CHECK: [[META10]] = !{!"Simple C++ TBAA"}
237237
; CHECK: [[TBAA11]] = !{[[META12:![0-9]+]], [[META12]], i64 0}
238238
; CHECK: [[META12]] = !{!"any pointer", [[META9]], i64 0}
239-
; CHECK: [[META13]] = !{ptr @_Z7throw_ei.cfi_jt}
239+
; CHECK: [[META13]] = !{ptr @.cfi.jumptable}
240240
; CHECK: [[META14]] = !{}
241241
;.

0 commit comments

Comments
 (0)