[PATCH] Globals in non-zero address spaces might be null

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 18:41:59 PDT 2015



On 08/26/2015 04:44 PM, Pete Cooper wrote:
> Hi Philip
>
> This is an offshoot of the conversation we had here: 
> http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20150824/295815.html
>
> The attached patch make sure that we only add nonnull attributes to 
> globals in address space 0.  Other address spaces may or may not 
> permit a global to live at address 0, but we have to assume that they 
> might.
>
> Thanks,
> Pete
>
>
> addrspace-nullness.patch
>
>
> diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp
> index 9b930d7..71fe1e6 100644
> --- a/lib/Analysis/ValueTracking.cpp
> +++ b/lib/Analysis/ValueTracking.cpp
> @@ -3227,8 +3227,12 @@ bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) {
>       return A->hasByValOrInAllocaAttr() || A->hasNonNullAttr();
>   
>     // Global values are not null unless extern weak.
> +  // Note that only globals in address space 0 are guaranteed to not be null.
> +  // Other address spaces are potentially able to have null addresses, so we
> +  // have to be safe here and assume that is the case.
The actual code change is fine, but the comment is inaccurate.  You 
dropped the bit about extern weak.

Possibly something along the lines of:
A global variable in address space 0 is non null unless extern weak.  
Other address spaces may have null as a valid address for a global, so 
we can't assume anything.
>     if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
> -    return !GV->hasExternalWeakLinkage();
> +    return !GV->hasExternalWeakLinkage() &&
> +           GV->getType()->getAddressSpace() == 0;
>   
>     // A Load tagged w/nonnull metadata is never null.
>     if (const LoadInst *LI = dyn_cast<LoadInst>(V))
> diff --git a/test/Transforms/InstCombine/nonnull-attribute.ll b/test/Transforms/InstCombine/nonnull-attribute.ll
> new file mode 100644
> index 0000000..74fb091
> --- /dev/null
> +++ b/test/Transforms/InstCombine/nonnull-attribute.ll
> @@ -0,0 +1,19 @@
> +; RUN: opt < %s -instcombine -S | FileCheck %s
> +
> +; This test makes sure that we do not assume globals in address spaces other
> +; than 0 are able to be null.
> +
> + at as0 = external global i32
> + at as1 = external addrspace(1) global i32
> +
> +declare void @addrspace0(i32*)
> +declare void @addrspace1(i32 addrspace(1)*)
> +
> +; CHECK: call void @addrspace0(i32* nonnull @as0)
> +; CHECK: call void @addrspace1(i32 addrspace(1)* @as1)
> +
> +define void @test() {
> +  call void @addrspace0(i32* @as0)
> +  call void @addrspace1(i32 addrspace(1)* @as1)
> +  ret void
> +}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150826/cd39d1f7/attachment.html>


More information about the llvm-commits mailing list