diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -2189,22 +2189,11 @@ 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, @@ -2218,21 +2207,16 @@ 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(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; diff --git a/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll b/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll --- a/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll +++ b/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll @@ -5,19 +5,20 @@ @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 diff --git a/llvm/test/Transforms/GlobalOpt/alias-used.ll b/llvm/test/Transforms/GlobalOpt/alias-used.ll --- a/llvm/test/Transforms/GlobalOpt/alias-used.ll +++ b/llvm/test/Transforms/GlobalOpt/alias-used.ll @@ -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]