[llvm] r336611 - Make llvm.objectsize more conservative with null

George Burgess IV via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 9 15:21:17 PDT 2018


Author: gbiv
Date: Mon Jul  9 15:21:16 2018
New Revision: 336611

URL: http://llvm.org/viewvc/llvm-project?rev=336611&view=rev
Log:
Make llvm.objectsize more conservative with null

In non-zero address spaces, we were reporting that an object at `null`
always occupies zero bytes. This is incorrect in many cases, so just
return `unknown` in those cases for now.

Differential Revision: https://reviews.llvm.org/D48860

Modified:
    llvm/trunk/docs/LangRef.rst
    llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
    llvm/trunk/test/Transforms/CodeGenPrepare/basic.ll
    llvm/trunk/test/Transforms/InstCombine/objsize.ll

Modified: llvm/trunk/docs/LangRef.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/LangRef.rst?rev=336611&r1=336610&r2=336611&view=diff
==============================================================================
--- llvm/trunk/docs/LangRef.rst (original)
+++ llvm/trunk/docs/LangRef.rst Mon Jul  9 15:21:16 2018
@@ -14459,10 +14459,10 @@ The ``llvm.objectsize`` intrinsic takes
 a pointer to or into the ``object``. The second argument determines whether
 ``llvm.objectsize`` returns 0 (if true) or -1 (if false) when the object size
 is unknown. The third argument controls how ``llvm.objectsize`` acts when
-``null`` is used as its pointer argument. If it's true and the pointer is in
-address space 0, ``null`` is treated as an opaque value with an unknown number
-of bytes. Otherwise, ``llvm.objectsize`` reports 0 bytes available when given
-``null``.
+``null`` in address space 0 is used as its pointer argument. If it's ``false``,
+``llvm.objectsize`` reports 0 bytes available when given ``null``. Otherwise, if
+the ``null`` is in a non-zero address space or if ``true`` is given for the
+third argument of ``llvm.objectsize``, we assume its size is unknown.
 
 The second and third arguments only accept constants.
 

Modified: llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryBuiltins.cpp?rev=336611&r1=336610&r2=336611&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/MemoryBuiltins.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryBuiltins.cpp Mon Jul  9 15:21:16 2018
@@ -642,7 +642,14 @@ SizeOffsetType ObjectSizeOffsetVisitor::
 
 SizeOffsetType
 ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) {
-  if (Options.NullIsUnknownSize && CPN.getType()->getAddressSpace() == 0)
+  // If null is unknown, there's nothing we can do. Additionally, non-zero
+  // address spaces can make use of null, so we don't presume to know anything
+  // about that.
+  //
+  // TODO: How should this work with address space casts? We currently just drop
+  // them on the floor, but it's unclear what we should do when a NULL from
+  // addrspace(1) gets casted to addrspace(0) (or vice-versa).
+  if (Options.NullIsUnknownSize || CPN.getType()->getAddressSpace())
     return unknown();
   return std::make_pair(Zero, Zero);
 }

Modified: llvm/trunk/test/Transforms/CodeGenPrepare/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/CodeGenPrepare/basic.ll?rev=336611&r1=336610&r2=336611&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/CodeGenPrepare/basic.ll (original)
+++ llvm/trunk/test/Transforms/CodeGenPrepare/basic.ll Mon Jul  9 15:21:16 2018
@@ -46,7 +46,7 @@ entry:
 ; CHECK-LABEL: @test_objectsize_null_flag_noas0(
 define i64 @test_objectsize_null_flag_noas0() {
 entry:
-  ; CHECK: ret i64 0
+  ; CHECK: ret i64 -1
   %0 = tail call i64 @llvm.objectsize.i64.p1i8(i8 addrspace(1)* null, i1 false,
                                                i1 true)
   ret i64 %0
@@ -61,6 +61,24 @@ entry:
   ret i64 %0
 }
 
+; CHECK-LABEL: @test_objectsize_null_known_flag_noas0
+define i64 @test_objectsize_null_known_flag_noas0() {
+entry:
+  ; CHECK: ret i64 -1
+  %0 = tail call i64 @llvm.objectsize.i64.p1i8(i8 addrspace(1)* null, i1 false,
+                                               i1 false)
+  ret i64 %0
+}
+
+; CHECK-LABEL: @test_objectsize_null_known_flag_min_noas0
+define i64 @test_objectsize_null_known_flag_min_noas0() {
+entry:
+  ; CHECK: ret i64 0
+  %0 = tail call i64 @llvm.objectsize.i64.p1i8(i8 addrspace(1)* null, i1 true,
+                                               i1 false)
+  ret i64 %0
+}
+
 
 declare i64 @llvm.objectsize.i64(i8*, i1, i1) nounwind readonly
 declare i64 @llvm.objectsize.i64.p1i8(i8 addrspace(1)*, i1, i1) nounwind readonly

Modified: llvm/trunk/test/Transforms/InstCombine/objsize.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/objsize.ll?rev=336611&r1=336610&r2=336611&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/objsize.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/objsize.ll Mon Jul  9 15:21:16 2018
@@ -271,7 +271,7 @@ define i32 @test23() {
 
 ; 1 is an arbitrary non-zero address space.
 ; CHECK-LABEL: @test24(
-; CHECK: ret i32 0
+; CHECK: llvm.objectsize
 define i32 @test24() {
   %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false,
                                           i1 false)
@@ -279,7 +279,7 @@ define i32 @test24() {
 }
 
 ; CHECK-LABEL: @test25(
-; CHECK: ret i32 0
+; CHECK: llvm.objectsize
 define i32 @test25() {
   %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true,
                                           i1 false)
@@ -287,7 +287,7 @@ define i32 @test25() {
 }
 
 ; CHECK-LABEL: @test26(
-; CHECK: ret i32 0
+; CHECK: llvm.objectsize
 define i32 @test26() {
   %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 false,
                                           i1 true)
@@ -295,7 +295,7 @@ define i32 @test26() {
 }
 
 ; CHECK-LABEL: @test27(
-; CHECK: ret i32 0
+; CHECK: llvm.objectsize
 define i32 @test27() {
   %1 = call i32 @llvm.objectsize.i32.p1i8(i8 addrspace(1)* null, i1 true,
                                           i1 true)




More information about the llvm-commits mailing list