[polly] 533b7c1 - [GlobalOpt] Don't replace the aliasee if it has other references.
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 26 18:54:33 PDT 2023
Author: DianQK
Date: 2023-04-27T09:53:47+08:00
New Revision: 533b7c1f6c696817df332cc7c9acbe3b454eadf2
URL: https://github.com/llvm/llvm-project/commit/533b7c1f6c696817df332cc7c9acbe3b454eadf2
DIFF: https://github.com/llvm/llvm-project/commit/533b7c1f6c696817df332cc7c9acbe3b454eadf2.diff
LOG: [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
Added:
llvm/test/Transforms/GlobalOpt/X86/alias-used-with-asm.ll
Modified:
llvm/lib/Transforms/IPO/GlobalOpt.cpp
llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll
llvm/test/Transforms/GlobalOpt/alias-used.ll
polly/test/Support/dumpfunction.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 4b6e8e397db69..50b90944c4526 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;
diff --git a/llvm/test/Transforms/GlobalOpt/X86/alias-used-with-asm.ll b/llvm/test/Transforms/GlobalOpt/X86/alias-used-with-asm.ll
new file mode 100644
index 0000000000000..3161b72ed1ef8
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/X86/alias-used-with-asm.ll
@@ -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.
+ at 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"
+
+ at bar2 = internal alias void (), ptr @bar1
+; CHECK: @bar2 = internal alias void (), ptr @bar1
+
+define internal void @bar1() {
+; CHECK: define internal void @bar1()
+ ret void
+}
diff --git a/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll b/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll
index e60d35c599743..6bac291ad9c0e 100644
--- a/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll
+++ b/llvm/test/Transforms/GlobalOpt/alias-used-address-space.ll
@@ -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
- at 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)]
+ at 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
index 35889c1e59754..7137bb13af58c 100644
--- 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]
diff --git a/polly/test/Support/dumpfunction.ll b/polly/test/Support/dumpfunction.ll
index de7e802d98e5b..863212b2ef7de 100644
--- a/polly/test/Support/dumpfunction.ll
+++ b/polly/test/Support/dumpfunction.ll
@@ -20,8 +20,6 @@
%unrelated_type = type { i32 }
- at 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
More information about the llvm-commits
mailing list