[llvm] 6923a67 - GlobalOpt: Improve addrspacecast handling
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue May 16 08:32:43 PDT 2023
Author: Matt Arsenault
Date: 2023-05-16T16:32:30+01:00
New Revision: 6923a67db8439616ffb48b215dca6cd0674b43f8
URL: https://github.com/llvm/llvm-project/commit/6923a67db8439616ffb48b215dca6cd0674b43f8
DIFF: https://github.com/llvm/llvm-project/commit/6923a67db8439616ffb48b215dca6cd0674b43f8.diff
LOG: GlobalOpt: Improve addrspacecast handling
Handle addrspacecast when looking at uses.
Added:
Modified:
llvm/lib/Transforms/IPO/GlobalOpt.cpp
llvm/test/Transforms/GlobalOpt/load-store-global.ll
llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index bae94fd763d08..5fa53b289ae07 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -665,6 +665,9 @@ static bool AllUsesOfValueWillTrapIfNull(const Value *V,
}
} else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
+ } else if (const AddrSpaceCastInst *CI = dyn_cast<AddrSpaceCastInst>(U)) {
+ if (!AllUsesOfValueWillTrapIfNull(CI, PHIs))
+ return false;
} else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
} else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
@@ -845,7 +848,8 @@ static bool OptimizeAwayTrappingUsesOfLoads(
assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
isa<BitCastInst>(GlobalUser) ||
- isa<GetElementPtrInst>(GlobalUser)) &&
+ isa<GetElementPtrInst>(GlobalUser) ||
+ isa<AddrSpaceCastInst>(GlobalUser)) &&
"Only expect load and stores!");
}
}
diff --git a/llvm/test/Transforms/GlobalOpt/load-store-global.ll b/llvm/test/Transforms/GlobalOpt/load-store-global.ll
index 521279b5882f2..5001bef6a8497 100644
--- a/llvm/test/Transforms/GlobalOpt/load-store-global.ll
+++ b/llvm/test/Transforms/GlobalOpt/load-store-global.ll
@@ -35,3 +35,14 @@ define void @qux() nounwind {
; CHECK-NOT: load
}
+ at addrspacecast_a = internal global ptr null
+
+define void @addrspacecast_user() {
+; CHECK-LABEL: @addrspacecast_user
+; CHECK-NOT: store
+; CHECK-NOT: load
+ %g = addrspacecast ptr @addrspacecast_a to ptr addrspace(1)
+ store ptr inttoptr (i64 1 to ptr), ptr @addrspacecast_a, align 8
+ %l = load ptr, ptr @addrspacecast_a, align 8
+ ret void
+}
diff --git a/llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll b/llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
index 05dffd708ba27..8c2cc7a79a8c0 100644
--- a/llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
+++ b/llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
@@ -6,16 +6,8 @@ declare noalias ptr @malloc(i64) allockind("alloc,uninitialized") allocsize(0)
@G = internal global ptr null
-;.
-; CHECK: @[[G:[a-zA-Z0-9_$"\\.-]+]] = internal unnamed_addr global ptr null
-;.
define void @init() {
; CHECK-LABEL: @init(
-; CHECK-NEXT: [[MALLOCCALL:%.*]] = tail call ptr @malloc(i64 4)
-; CHECK-NEXT: store ptr [[MALLOCCALL]], ptr @G, align 8
-; CHECK-NEXT: [[GV:%.*]] = load ptr, ptr @G, align 8
-; CHECK-NEXT: [[ADDRSPACECAST:%.*]] = addrspacecast ptr [[GV]] to ptr addrspace(1)
-; CHECK-NEXT: store i32 20, ptr addrspace(1) [[ADDRSPACECAST]], align 4
; CHECK-NEXT: ret void
;
%malloccall = tail call ptr @malloc(i64 4)
@@ -28,9 +20,7 @@ define void @init() {
define i32 @get() {
; CHECK-LABEL: @get(
-; CHECK-NEXT: [[GV:%.*]] = load ptr, ptr @G, align 8
-; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[GV]], align 4
-; CHECK-NEXT: ret i32 [[V]]
+; CHECK-NEXT: ret i32 20
;
%GV = load ptr, ptr @G
%V = load i32, ptr %GV
@@ -40,7 +30,6 @@ define i32 @get() {
define void @foo(i64 %Size) nounwind noinline #0 {
; CHECK-LABEL: @foo(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr @G, align 4
; CHECK-NEXT: ret void
;
entry:
@@ -51,6 +40,5 @@ entry:
attributes #0 = { null_pointer_is_valid }
;.
-; CHECK: attributes #[[ATTR0:[0-9]+]] = { allockind("alloc,uninitialized") allocsize(0) }
-; CHECK: attributes #[[ATTR1:[0-9]+]] = { noinline nounwind null_pointer_is_valid }
+; CHECK: attributes #[[ATTR0:[0-9]+]] = { noinline nounwind null_pointer_is_valid }
;.
More information about the llvm-commits
mailing list