r229213 - DR1748: the reserved placement allocation functions have undefined behavior if

Richard Smith richard-llvm at metafoo.co.uk
Fri Feb 13 17:52:21 PST 2015


Author: rsmith
Date: Fri Feb 13 19:52:20 2015
New Revision: 229213

URL: http://llvm.org/viewvc/llvm-project?rev=229213&view=rev
Log:
DR1748: the reserved placement allocation functions have undefined behavior if
they're given a null pointer as an argument, so we do not need to emit null
checks on their results.

Modified:
    cfe/trunk/include/clang/AST/ExprCXX.h
    cfe/trunk/lib/AST/ExprCXX.cpp
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp
    cfe/trunk/test/CodeGenCXX/debug-info-line.cpp
    cfe/trunk/test/CodeGenCXX/new.cpp

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=229213&r1=229212&r2=229213&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Fri Feb 13 19:52:20 2015
@@ -1692,6 +1692,10 @@ public:
   ///   not be done, the deallocation function shall not be called,
   ///   and the value of the new-expression shall be null.
   ///
+  /// C++ DR1748:
+  ///   If the allocation function is a reserved placement allocation
+  ///   function that returns null, the behavior is undefined.
+  ///
   /// An allocation function is not allowed to return null unless it
   /// has a non-throwing exception-specification.  The '03 rule is
   /// identical except that the definition of a non-throwing

Modified: cfe/trunk/lib/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprCXX.cpp?rev=229213&r1=229212&r2=229213&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprCXX.cpp (original)
+++ cfe/trunk/lib/AST/ExprCXX.cpp Fri Feb 13 19:52:20 2015
@@ -208,8 +208,9 @@ void CXXNewExpr::AllocateArgsArray(const
 }
 
 bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
-  return getOperatorNew()->getType()->
-    castAs<FunctionProtoType>()->isNothrow(Ctx);
+  return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
+             Ctx) &&
+         !getOperatorNew()->isReservedGlobalPlacementOperator();
 }
 
 // CXXDeleteExpr

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=229213&r1=229212&r2=229213&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Fri Feb 13 19:52:20 2015
@@ -1279,10 +1279,9 @@ llvm::Value *CodeGenFunction::EmitCXXNew
 
   // Emit a null check on the allocation result if the allocation
   // function is allowed to return null (because it has a non-throwing
-  // exception spec; for this part, we inline
-  // CXXNewExpr::shouldNullCheckAllocation()) and we have an
+  // exception spec or is the reserved placement new) and we have an
   // interesting initializer.
-  bool nullCheck = allocatorType->isNothrow(getContext()) &&
+  bool nullCheck = E->shouldNullCheckAllocation(getContext()) &&
     (!allocType.isPODType(getContext()) || E->hasInitializer());
 
   llvm::BasicBlock *nullCheckBB = nullptr;

Modified: cfe/trunk/test/CodeGenCXX/debug-info-line.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/debug-info-line.cpp?rev=229213&r1=229212&r2=229213&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/debug-info-line.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/debug-info-line.cpp Fri Feb 13 19:52:20 2015
@@ -113,7 +113,7 @@ inline void *operator new(decltype(sizeo
 // CHECK-LABEL: define
 void f10() {
   void *void_src();
-  ( // CHECK: icmp {{.*}} !dbg [[DBG_F10_ICMP:.*]]
+  (
     // CHECK: store {{.*}} !dbg [[DBG_F10_STORE:!.*]]
 #line 1100
       new (void_src()) int(src()));
@@ -305,7 +305,6 @@ void f24() {
 // CHECK: [[DBG_F7]] = !MDLocation(line: 800,
 // CHECK: [[DBG_F8]] = !MDLocation(line: 900,
 // CHECK: [[DBG_F9]] = !MDLocation(line: 1000,
-// CHECK: [[DBG_F10_ICMP]] = !MDLocation(line: 1100,
 // CHECK: [[DBG_F10_STORE]] = !MDLocation(line: 1100,
 // CHECK: [[DBG_GLBL_CTOR_B]] = !MDLocation(line: 1200,
 // CHECK: [[DBG_GLBL_DTOR_B]] = !MDLocation(line: 1200,

Modified: cfe/trunk/test/CodeGenCXX/new.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/new.cpp?rev=229213&r1=229212&r2=229213&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/new.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/new.cpp Fri Feb 13 19:52:20 2015
@@ -36,6 +36,10 @@ void *operator new[](size_t, const std::
 void operator delete(void *, const std::nothrow_t &) throw();
 void operator delete[](void *, const std::nothrow_t &) throw();
 
+// Declare some other placemenet operators.
+void *operator new(size_t, void*, bool) throw();
+void *operator new[](size_t, void*, bool) throw();
+
 void t2(int* a) {
   int* b = new (a) int;
 }
@@ -191,20 +195,31 @@ void f() {
 namespace test15 {
   struct A { A(); ~A(); };
 
-  // CHECK-LABEL:    define void @_ZN6test155test0EPv(
+  // CHECK-LABEL:    define void @_ZN6test156test0aEPv(
   // CHECK:      [[P:%.*]] = load i8*
-  // CHECK-NEXT: icmp eq i8* [[P]], null
-  // CHECK-NEXT: br i1
+  // CHECK-NOT:  icmp eq i8* [[P]], null
+  // CHECK-NOT:  br i1
   // CHECK:      [[T0:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
   // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[T0]])
-  void test0(void *p) {
+  void test0a(void *p) {
     new (p) A();
   }
 
-  // CHECK-LABEL:    define void @_ZN6test155test1EPv(
-  // CHECK:      [[P:%.*]] = load i8**
+  // CHECK-LABEL:    define void @_ZN6test156test0bEPv(
+  // CHECK:      [[P0:%.*]] = load i8*
+  // CHECK:      [[P:%.*]] = call i8* @_ZnwmPvb(i64 1, i8* [[P0]]
   // CHECK-NEXT: icmp eq i8* [[P]], null
   // CHECK-NEXT: br i1
+  // CHECK:      [[T0:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
+  // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[T0]])
+  void test0b(void *p) {
+    new (p, true) A();
+  }
+
+  // CHECK-LABEL:    define void @_ZN6test156test1aEPv(
+  // CHECK:      [[P:%.*]] = load i8**
+  // CHECK-NOT:  icmp eq i8* [[P]], null
+  // CHECK-NOT:  br i1
   // CHECK:      [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
   // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 5
   // CHECK-NEXT: br label
@@ -213,10 +228,28 @@ namespace test15 {
   // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]]* [[CUR]], i64 1
   // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
   // CHECK-NEXT: br i1 [[DONE]]
-  void test1(void *p) {
+  void test1a(void *p) {
     new (p) A[5];
   }
 
+  // CHECK-LABEL:    define void @_ZN6test156test1bEPv(
+  // CHECK:      [[P0:%.*]] = load i8**
+  // CHECK:      [[P:%.*]] = call i8* @_ZnamPvb(i64 13, i8* [[P0]]
+  // CHECK-NEXT: icmp eq i8* [[P]], null
+  // CHECK-NEXT: br i1
+  // CHECK:      [[AFTER_COOKIE:%.*]] = getelementptr inbounds i8* [[P]], i64 8
+  // CHECK:      [[BEGIN:%.*]] = bitcast i8* [[AFTER_COOKIE]] to [[A:%.*]]*
+  // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]]* [[BEGIN]], i64 5
+  // CHECK-NEXT: br label
+  // CHECK:      [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
+  // CHECK-NEXT: call void @_ZN6test151AC1Ev([[A]]* [[CUR]])
+  // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]]* [[CUR]], i64 1
+  // CHECK-NEXT: [[DONE:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
+  // CHECK-NEXT: br i1 [[DONE]]
+  void test1b(void *p) {
+    new (p, true) A[5];
+  }
+
   // TODO: it's okay if all these size calculations get dropped.
   // FIXME: maybe we should try to throw on overflow?
   // CHECK-LABEL:    define void @_ZN6test155test2EPvi(
@@ -225,8 +258,6 @@ namespace test15 {
   // CHECK-NEXT: [[T1:%.*]] = icmp slt i64 [[T0]], 0
   // CHECK-NEXT: [[T2:%.*]] = select i1 [[T1]], i64 -1, i64 [[T0]]
   // CHECK-NEXT: [[P:%.*]] = load i8*
-  // CHECK-NEXT: icmp eq i8* [[P]], null
-  // CHECK-NEXT: br i1
   // CHECK:      [[BEGIN:%.*]] = bitcast i8* [[P]] to [[A:%.*]]*
   // CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq i64 [[T0]], 0
   // CHECK-NEXT: br i1 [[ISEMPTY]],





More information about the cfe-commits mailing list