diff options
author | DianQK <dianqk@dianqk.net> | 2023-04-27 08:10:15 +0800 |
---|---|---|
committer | DianQK <dianqk@dianqk.net> | 2023-04-27 09:53:47 +0800 |
commit | 533b7c1f6c696817df332cc7c9acbe3b454eadf2 (patch) | |
tree | b2ef8330029141f275bd287d24f972b69b9ecea2 /llvm/lib | |
parent | 4c83674679365dd990fef5f834a07ad32798e7f2 (diff) | |
download | llvm-533b7c1f6c696817df332cc7c9acbe3b454eadf2.zip llvm-533b7c1f6c696817df332cc7c9acbe3b454eadf2.tar.gz llvm-533b7c1f6c696817df332cc7c9acbe3b454eadf2.tar.bz2 |
[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
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/IPO/GlobalOpt.cpp | 30 |
1 files changed, 7 insertions, 23 deletions
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index 4b6e8e3..50b9094 100644 --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -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; |