diff options
| author | Richard Smith <richard-llvm@metafoo.co.uk> | 2015-02-14 01:52:20 +0000 |
|---|---|---|
| committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2015-02-14 01:52:20 +0000 |
| commit | 902a0238aa1514832a0d88661c9d7ccf112b71e0 (patch) | |
| tree | b8f1416976d83e831505038bf401ff6881b1dcf2 | |
| parent | ce27e42d47bbc136cf1deb1859e10e9e8da5b501 (diff) | |
| download | bcm5719-llvm-902a0238aa1514832a0d88661c9d7ccf112b71e0.tar.gz bcm5719-llvm-902a0238aa1514832a0d88661c9d7ccf112b71e0.zip | |
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.
llvm-svn: 229213
| -rw-r--r-- | clang/include/clang/AST/ExprCXX.h | 4 | ||||
| -rw-r--r-- | clang/lib/AST/ExprCXX.cpp | 5 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGExprCXX.cpp | 5 | ||||
| -rw-r--r-- | clang/test/CodeGenCXX/debug-info-line.cpp | 3 | ||||
| -rw-r--r-- | clang/test/CodeGenCXX/new.cpp | 49 |
5 files changed, 50 insertions, 16 deletions
diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index dc5d78b1f53..d1a6063b6b9 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -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 diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index fd25b58a70a..1f54d24c471 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -208,8 +208,9 @@ void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray, } bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const { - return getOperatorNew()->getType()-> - castAs<FunctionProtoType>()->isNothrow(Ctx); + return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow( + Ctx) && + !getOperatorNew()->isReservedGlobalPlacementOperator(); } // CXXDeleteExpr diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp index cead8a429df..6852d3afc7f 100644 --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -1279,10 +1279,9 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { // 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; diff --git a/clang/test/CodeGenCXX/debug-info-line.cpp b/clang/test/CodeGenCXX/debug-info-line.cpp index 923f183d543..98b99d55b3c 100644 --- a/clang/test/CodeGenCXX/debug-info-line.cpp +++ b/clang/test/CodeGenCXX/debug-info-line.cpp @@ -113,7 +113,7 @@ inline void *operator new(decltype(sizeof(1)), void *p) noexcept { return p; } // 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, diff --git a/clang/test/CodeGenCXX/new.cpp b/clang/test/CodeGenCXX/new.cpp index 862161b1938..5138c1bdb56 100644 --- a/clang/test/CodeGenCXX/new.cpp +++ b/clang/test/CodeGenCXX/new.cpp @@ -36,6 +36,10 @@ void *operator new[](size_t, const std::nothrow_t &) throw(); 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]], |

