[PATCH] D149536: GlobalOpt: Improve addrspacecast handling
Matt Arsenault via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 29 15:44:02 PDT 2023
arsenm created this revision.
arsenm added reviewers: nikic, aeubanks, jhuber6, fhahn, jdoerfert.
Herald added subscribers: hoy, ormris, StephenFan, hiraditya.
Herald added a project: All.
arsenm requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.
Handle addrspacecast when looking at uses.
https://reviews.llvm.org/D149536
Files:
llvm/lib/Transforms/IPO/GlobalOpt.cpp
llvm/test/Transforms/GlobalOpt/load-store-global.ll
llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
Index: llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
===================================================================
--- llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
+++ llvm/test/Transforms/GlobalOpt/malloc-promote-addrspacecast.ll
@@ -6,16 +6,8 @@
@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 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 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 @@
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 }
;.
Index: llvm/test/Transforms/GlobalOpt/load-store-global.ll
===================================================================
--- llvm/test/Transforms/GlobalOpt/load-store-global.ll
+++ llvm/test/Transforms/GlobalOpt/load-store-global.ll
@@ -35,3 +35,14 @@
; 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
+}
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -665,6 +665,9 @@
}
} 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 @@
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!");
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149536.518257.patch
Type: text/x-patch
Size: 3516 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230429/889ac6d9/attachment.bin>
More information about the llvm-commits
mailing list