Skip to content

Commit 533b7c1

Browse files
committed
[GlobalOpt] Don't replace the aliasee if it has other references.
As long as aliasee has `@llvm.used` or `@llvm.compiler.used` references, we cannot do the related replace or delete operations. Even if it is a Local Linkage, we cannot infer if there is no other use for it, such as asm or other future added cases. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D145293
1 parent 4c83674 commit 533b7c1

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,22 +2219,11 @@ static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
22192219
return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
22202220
}
22212221

2222-
static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V,
2223-
const LLVMUsed &U) {
2224-
unsigned N = 2;
2225-
assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
2226-
"We should have removed the duplicated "
2227-
"element from llvm.compiler.used");
2228-
if (U.usedCount(&V) || U.compilerUsedCount(&V))
2229-
++N;
2230-
return V.hasNUsesOrMore(N);
2231-
}
2232-
2233-
static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) {
2234-
if (!GA.hasLocalLinkage())
2222+
static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
2223+
if (!GV.hasLocalLinkage())
22352224
return true;
22362225

2237-
return U.usedCount(&GA) || U.compilerUsedCount(&GA);
2226+
return U.usedCount(&GV) || U.compilerUsedCount(&GV);
22382227
}
22392228

22402229
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
@@ -2248,21 +2237,16 @@ static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
22482237
if (!mayHaveOtherReferences(GA, U))
22492238
return Ret;
22502239

2251-
// If the aliasee has internal linkage, give it the name and linkage
2252-
// of the alias, and delete the alias. This turns:
2240+
// If the aliasee has internal linkage and no other references (e.g.,
2241+
// @llvm.used, @llvm.compiler.used), give it the name and linkage of the
2242+
// alias, and delete the alias. This turns:
22532243
// define internal ... @f(...)
22542244
// @a = alias ... @f
22552245
// into:
22562246
// define ... @a(...)
22572247
Constant *Aliasee = GA.getAliasee();
22582248
GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2259-
if (!Target->hasLocalLinkage())
2260-
return Ret;
2261-
2262-
// Do not perform the transform if multiple aliases potentially target the
2263-
// aliasee. This check also ensures that it is safe to replace the section
2264-
// and other attributes of the aliasee with those of the alias.
2265-
if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
2249+
if (mayHaveOtherReferences(*Target, U))
22662250
return Ret;
22672251

22682252
RenameTarget = true;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: opt < %s -passes=globalopt -S | FileCheck %s
2+
3+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
module asm ".global foo1"
7+
module asm "foo1: jmp bar1"
8+
module asm ".global foo2"
9+
module asm "foo2: jmp bar2"
10+
11+
; The `llvm.compiler.used` indicates that `foo1` and `foo2` have associated symbol references in asm.
12+
; Checking globalopt does not remove these two symbols.
13+
@llvm.compiler.used = appending global [2 x ptr] [ptr @bar1, ptr @bar2], section "llvm.metadata"
14+
; CHECK: @llvm.compiler.used = appending global [2 x ptr] [ptr @bar1, ptr @bar2], section "llvm.metadata"
15+
16+
@bar2 = internal alias void (), ptr @bar1
17+
; CHECK: @bar2 = internal alias void (), ptr @bar1
18+
19+
define internal void @bar1() {
20+
; CHECK: define internal void @bar1()
21+
ret void
22+
}

llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ target datalayout = "p:32:32:32-p1:16:16:16"
55
@c = hidden addrspace(1) global i8 42
66

77
@i = internal addrspace(1) global i8 42
8-
9-
; CHECK: @ia = internal addrspace(1) global i8 42
10-
@ia = internal alias i8, ptr addrspace(1) @i
8+
; CHECK: @i = internal addrspace(1) global i8 42
119

1210
@llvm.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @ca to ptr)], section "llvm.metadata"
1311
; CHECK-DAG: @llvm.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @ca to ptr)], section "llvm.metadata"
1412

1513
@llvm.compiler.used = appending global [2 x ptr] [ptr addrspacecast(ptr addrspace(1) @ia to ptr), ptr addrspacecast (ptr addrspace(1) @i to ptr)], section "llvm.metadata"
16-
; CHECK-DAG: @llvm.compiler.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @ia to ptr)], section "llvm.metadata"
14+
; CHECK-DAG: @llvm.compiler.used = appending global [2 x ptr] [ptr addrspacecast (ptr addrspace(1) @i to ptr), ptr addrspacecast (ptr addrspace(1) @ia to ptr)], section "llvm.metadata"
1715

1816
@sameAsUsed = global [1 x ptr] [ptr addrspacecast(ptr addrspace(1) @ca to ptr)]
1917
; CHECK-DAG: @sameAsUsed = local_unnamed_addr global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @c to ptr)]
2018

19+
@ia = internal alias i8, ptr addrspace(1) @i
20+
; CHECK: @ia = internal alias i8, ptr addrspace(1) @i
21+
2122
@ca = internal alias i8, ptr addrspace(1) @c
2223
; CHECK: @ca = internal alias i8, ptr addrspace(1) @c
2324

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
@c = dso_local global i8 42
44

5+
; CHECK: @i = internal global i8 42
56
@i = internal global i8 42
6-
; CHECK: @ia = internal global i8 42
7+
; CHECK-DAG: @ia = internal alias i8, ptr @i
78
@ia = internal alias i8, ptr @i
89

910
@llvm.used = appending global [3 x ptr] [ptr @fa, ptr @f, ptr @ca], section "llvm.metadata"
1011
; CHECK-DAG: @llvm.used = appending global [3 x ptr] [ptr @ca, ptr @f, ptr @fa], section "llvm.metadata"
1112

1213
@llvm.compiler.used = appending global [4 x ptr] [ptr @fa3, ptr @fa, ptr @ia, ptr @i], section "llvm.metadata"
13-
; CHECK-DAG: @llvm.compiler.used = appending global [2 x ptr] [ptr @fa3, ptr @ia], section "llvm.metadata"
14+
; CHECK-DAG: @llvm.compiler.used = appending global [3 x ptr] [ptr @fa3, ptr @i, ptr @ia], section "llvm.metadata"
1415

1516
@sameAsUsed = global [3 x ptr] [ptr @fa, ptr @f, ptr @ca]
1617
; CHECK-DAG: @sameAsUsed = local_unnamed_addr global [3 x ptr] [ptr @f, ptr @f, ptr @c]

polly/test/Support/dumpfunction.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
%unrelated_type = type { i32 }
2222

23-
@callee_alias = dso_local unnamed_addr alias void(i32, ptr, i32), ptr @callee
24-
2523
define internal void @callee(i32 %n, ptr noalias nonnull %A, i32 %i) #0 {
2624
entry:
2725
br label %for

0 commit comments

Comments
 (0)