<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
<br>
<div class="moz-cite-prefix">On 08/26/2015 04:44 PM, Pete Cooper
wrote:<br>
</div>
<blockquote
cite="mid:1DBC309A-F4DD-4DDD-85BE-720EA362A036@apple.com"
type="cite">
<div class="moz-text-html" lang="x-western">Hi Philip
<div class=""><br class="">
</div>
<div class="">This is an offshoot of the conversation we had
here: <a moz-do-not-send="true"
href="http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20150824/295815.html"
class="">http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20150824/295815.html</a></div>
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">Pete</div>
<div class=""><br class="">
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"><legend
class="mimeAttachmentHeaderName">addrspace-nullness.patch</legend></fieldset>
<br>
<div class="moz-text-plain" wrap="true" graphical-quote="true"
style="font-family: -moz-fixed; font-size: 12px;"
lang="x-western">
<pre wrap="">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.</pre>
</div>
</blockquote>
The actual code change is fine, but the comment is inaccurate. You
dropped the bit about extern weak.<br>
<br>
Possibly something along the lines of:<br>
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.<br>
<blockquote
cite="mid:1DBC309A-F4DD-4DDD-85BE-720EA362A036@apple.com"
type="cite">
<div class="moz-text-plain" wrap="true" graphical-quote="true"
style="font-family: -moz-fixed; font-size: 12px;"
lang="x-western">
<pre wrap="">
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.
+
+@as0 = external global i32
+@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
+}
</pre>
</div>
</blockquote>
<br>
</body>
</html>