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 @@ -2200,11 +2200,11 @@ 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,15 +2218,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()) + if (mayHaveOtherReferences(*Target, U)) return Ret; // Do not perform the transform if multiple aliases potentially target the 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]