[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
This commit is contained in:
DianQK 2023-04-27 08:10:15 +08:00
parent 4c83674679
commit 533b7c1f6c
No known key found for this signature in database
GPG Key ID: 46BDB1AC96C48912
5 changed files with 37 additions and 31 deletions

View File

@ -2219,22 +2219,11 @@ static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
}
static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V,
const LLVMUsed &U) {
unsigned N = 2;
assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
"We should have removed the duplicated "
"element from llvm.compiler.used");
if (U.usedCount(&V) || U.compilerUsedCount(&V))
++N;
return V.hasNUsesOrMore(N);
}
static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) {
if (!GA.hasLocalLinkage())
static bool mayHaveOtherReferences(GlobalValue &GV, const LLVMUsed &U) {
if (!GV.hasLocalLinkage())
return true;
return U.usedCount(&GA) || U.compilerUsedCount(&GA);
return U.usedCount(&GV) || U.compilerUsedCount(&GV);
}
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
@ -2248,21 +2237,16 @@ static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
if (!mayHaveOtherReferences(GA, U))
return Ret;
// If the aliasee has internal linkage, give it the name and linkage
// of the alias, and delete the alias. This turns:
// If the aliasee has internal linkage and no other references (e.g.,
// @llvm.used, @llvm.compiler.used), give it the name and linkage of the
// alias, and delete the alias. This turns:
// define internal ... @f(...)
// @a = alias ... @f
// into:
// define ... @a(...)
Constant *Aliasee = GA.getAliasee();
GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
if (!Target->hasLocalLinkage())
return Ret;
// Do not perform the transform if multiple aliases potentially target the
// aliasee. This check also ensures that it is safe to replace the section
// and other attributes of the aliasee with those of the alias.
if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
if (mayHaveOtherReferences(*Target, U))
return Ret;
RenameTarget = true;

View File

@ -0,0 +1,22 @@
; RUN: opt < %s -passes=globalopt -S | FileCheck %s
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
module asm ".global foo1"
module asm "foo1: jmp bar1"
module asm ".global foo2"
module asm "foo2: jmp bar2"
; The `llvm.compiler.used` indicates that `foo1` and `foo2` have associated symbol references in asm.
; Checking globalopt does not remove these two symbols.
@llvm.compiler.used = appending global [2 x ptr] [ptr @bar1, ptr @bar2], section "llvm.metadata"
; CHECK: @llvm.compiler.used = appending global [2 x ptr] [ptr @bar1, ptr @bar2], section "llvm.metadata"
@bar2 = internal alias void (), ptr @bar1
; CHECK: @bar2 = internal alias void (), ptr @bar1
define internal void @bar1() {
; CHECK: define internal void @bar1()
ret void
}

View File

@ -5,19 +5,20 @@ target datalayout = "p:32:32:32-p1:16:16:16"
@c = hidden addrspace(1) global i8 42
@i = internal addrspace(1) global i8 42
; CHECK: @ia = internal addrspace(1) global i8 42
@ia = internal alias i8, ptr addrspace(1) @i
; CHECK: @i = internal addrspace(1) global i8 42
@llvm.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @ca to ptr)], section "llvm.metadata"
; CHECK-DAG: @llvm.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @ca to ptr)], section "llvm.metadata"
@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"
; CHECK-DAG: @llvm.compiler.used = appending global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @ia to ptr)], section "llvm.metadata"
; 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"
@sameAsUsed = global [1 x ptr] [ptr addrspacecast(ptr addrspace(1) @ca to ptr)]
; CHECK-DAG: @sameAsUsed = local_unnamed_addr global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @c to ptr)]
@ia = internal alias i8, ptr addrspace(1) @i
; CHECK: @ia = internal alias i8, ptr addrspace(1) @i
@ca = internal alias i8, ptr addrspace(1) @c
; CHECK: @ca = internal alias i8, ptr addrspace(1) @c

View File

@ -2,15 +2,16 @@
@c = dso_local global i8 42
; CHECK: @i = internal global i8 42
@i = internal global i8 42
; CHECK: @ia = internal global i8 42
; CHECK-DAG: @ia = internal alias i8, ptr @i
@ia = internal alias i8, ptr @i
@llvm.used = appending global [3 x ptr] [ptr @fa, ptr @f, ptr @ca], section "llvm.metadata"
; CHECK-DAG: @llvm.used = appending global [3 x ptr] [ptr @ca, ptr @f, ptr @fa], section "llvm.metadata"
@llvm.compiler.used = appending global [4 x ptr] [ptr @fa3, ptr @fa, ptr @ia, ptr @i], section "llvm.metadata"
; CHECK-DAG: @llvm.compiler.used = appending global [2 x ptr] [ptr @fa3, ptr @ia], section "llvm.metadata"
; CHECK-DAG: @llvm.compiler.used = appending global [3 x ptr] [ptr @fa3, ptr @i, ptr @ia], section "llvm.metadata"
@sameAsUsed = global [3 x ptr] [ptr @fa, ptr @f, ptr @ca]
; CHECK-DAG: @sameAsUsed = local_unnamed_addr global [3 x ptr] [ptr @f, ptr @f, ptr @c]

View File

@ -20,8 +20,6 @@
%unrelated_type = type { i32 }
@callee_alias = dso_local unnamed_addr alias void(i32, ptr, i32), ptr @callee
define internal void @callee(i32 %n, ptr noalias nonnull %A, i32 %i) #0 {
entry:
br label %for