[PATCH] D136882: ConstantFold: Fold out compare of addrspacecasted null with global
Matt Arsenault via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 27 13:40:41 PDT 2022
arsenm created this revision.
arsenm added reviewers: nikic, efriedma, nhaehnle, jrtc27, theraven, jdoerfert.
Herald added a subscriber: hiraditya.
Herald added a project: All.
arsenm requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.
null in addrspace(0) is defined to not point to any valid object.
Casting the pointer to another address space cannot make it point
to a valid object.
This is consistent with alias analysis's interpretation of null, e.g.
; Function: ptr_compare: 2 pointers, 0 call sites
; MayAlias: i32* %ptr, i32 addrspace(3)* @lds
define void @ptr_compare(i32* %ptr) {
%load = load i32, i32 addrspace(3)* @lds
store i32 0, i32* %ptr
ret void
}
; Function: null_compare: 2 pointers, 0 call sites
; NoAlias: i32 addrspace(3)* @lds, i32* null
define void @null_compare() {
%load = load i32, i32 addrspace(3)* @lds
store i32 0, i32* null
ret void
}
Fixes issue 58617, for the basic case. More complex cases,
like an offset from a GEP should also fold out. I'm not sure
it's appropriate for ConstantFolding to handle the general case,
which should use getUnderlyingObject.
https://reviews.llvm.org/D136882
Files:
llvm/lib/IR/ConstantFold.cpp
llvm/test/Assembler/constant-fold-compare-null-to-global-issue58617.ll
Index: llvm/test/Assembler/constant-fold-compare-null-to-global-issue58617.ll
===================================================================
--- llvm/test/Assembler/constant-fold-compare-null-to-global-issue58617.ll
+++ llvm/test/Assembler/constant-fold-compare-null-to-global-issue58617.ll
@@ -7,7 +7,7 @@
define i1 @cmp_addrspacecast_null() {
; CHECK-LABEL: @cmp_addrspacecast_null(
; CHECK-NEXT: bb0:
-; CHECK-NEXT: ret i1 icmp eq (ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), ptr addrspace(3) @lds)
+; CHECK-NEXT: ret i1 false
;
bb0:
ret i1 icmp eq (ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), ptr addrspace(3) @lds)
@@ -15,7 +15,7 @@
define i1 @cmp_addrspacecast_null_commute() {
; CHECK-LABEL: @cmp_addrspacecast_null_commute(
-; CHECK-NEXT: ret i1 icmp eq (ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), ptr addrspace(3) @lds)
+; CHECK-NEXT: ret i1 false
;
ret i1 icmp eq (ptr addrspace(3) @lds, ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)))
}
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -1582,13 +1582,21 @@
static Constant *constantFoldCompareGlobalToNull(CmpInst::Predicate Predicate,
Constant *C1, Constant *C2) {
const GlobalValue *GV = dyn_cast<GlobalValue>(C2);
- if (!GV || !C1->isNullValue())
+ if (!GV)
return nullptr;
// Don't try to evaluate aliases. External weak GV can be null.
- if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
+ if (isa<GlobalAlias>(GV) || GV->hasExternalWeakLinkage())
+ return nullptr;
+
+ // Strip away addrspacecast to see if this is derived from null.
+ C1 = C1->stripPointerCasts();
+
+ // A null pointer in the default address space cannot point to any valid
+ // object, regardless of the address space.
+ if (isa<ConstantPointerNull>(C1) &&
!NullPointerIsDefined(nullptr /* F */,
- GV->getType()->getAddressSpace())) {
+ cast<PointerType>(C1->getType())->getAddressSpace())) {
if (Predicate == ICmpInst::ICMP_EQ)
return ConstantInt::getFalse(C1->getContext());
else if (Predicate == ICmpInst::ICMP_NE)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136882.471282.patch
Type: text/x-patch
Size: 2354 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221027/3f6a8cf1/attachment.bin>
More information about the llvm-commits
mailing list