2023-02-16 15:30:35 +01:00
|
|
|
// RUN: %clang_cc1 -triple i686-pc-linux-gnu %s -o - -emit-llvm -verify | FileCheck %s
|
2013-12-14 04:49:06 +00:00
|
|
|
// expected-no-diagnostics
|
2009-11-16 05:16:40 +00:00
|
|
|
|
2011-01-27 09:37:56 +00:00
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
2009-11-16 05:16:40 +00:00
|
|
|
|
2011-01-27 09:37:56 +00:00
|
|
|
namespace test1 {
|
|
|
|
struct A { void operator delete(void*,size_t); int x; };
|
|
|
|
|
2020-12-31 00:27:11 -08:00
|
|
|
// CHECK-LABEL: define{{.*}} void @_ZN5test11aEPNS_1AE(
|
2011-01-27 09:37:56 +00:00
|
|
|
void a(A *x) {
|
|
|
|
// CHECK: load
|
|
|
|
// CHECK-NEXT: icmp eq {{.*}}, null
|
|
|
|
// CHECK-NEXT: br i1
|
2023-02-16 15:30:35 +01:00
|
|
|
// CHECK: call void @_ZN5test11AdlEPvj(ptr noundef %{{.*}}, i32 noundef 4)
|
2011-01-27 09:37:56 +00:00
|
|
|
delete x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we make cookies for the two-arg delete even when using
|
|
|
|
// the global allocator and deallocator.
|
|
|
|
namespace test2 {
|
|
|
|
struct A {
|
|
|
|
int x;
|
|
|
|
void *operator new[](size_t);
|
|
|
|
void operator delete[](void *, size_t);
|
|
|
|
};
|
|
|
|
|
2023-02-16 15:30:35 +01:00
|
|
|
// CHECK: define{{.*}} ptr @_ZN5test24testEv()
|
2011-01-27 09:37:56 +00:00
|
|
|
A *test() {
|
2023-02-16 15:30:35 +01:00
|
|
|
// CHECK: [[NEW:%.*]] = call noalias noundef nonnull ptr @_Znaj(i32 noundef 44)
|
|
|
|
// CHECK-NEXT: store i32 10, ptr [[NEW]]
|
|
|
|
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, ptr [[NEW]], i32 4
|
|
|
|
// CHECK-NEXT: ret ptr [[T1]]
|
2011-01-27 09:37:56 +00:00
|
|
|
return ::new A[10];
|
|
|
|
}
|
|
|
|
|
2020-12-31 00:27:11 -08:00
|
|
|
// CHECK-LABEL: define{{.*}} void @_ZN5test24testEPNS_1AE(
|
2011-01-27 09:37:56 +00:00
|
|
|
void test(A *p) {
|
2023-02-16 15:30:35 +01:00
|
|
|
// CHECK: [[P:%.*]] = alloca ptr, align 4
|
|
|
|
// CHECK-NEXT: store ptr {{%.*}}, ptr [[P]], align 4
|
|
|
|
// CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[P]], align 4
|
|
|
|
// CHECK-NEXT: [[T1:%.*]] = icmp eq ptr [[T0]], null
|
2011-01-27 09:37:56 +00:00
|
|
|
// CHECK-NEXT: br i1 [[T1]],
|
2023-02-16 15:30:35 +01:00
|
|
|
// CHECK: [[T3:%.*]] = getelementptr inbounds i8, ptr [[T0]], i32 -4
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = load i32, ptr [[T3]]
|
2024-05-22 12:37:27 +08:00
|
|
|
// CHECK-NEXT: [[T6:%.*]] = mul i32 4, [[T5]]
|
|
|
|
// CHECK-NEXT: [[T7:%.*]] = add i32 [[T6]], 4
|
|
|
|
// CHECK-NEXT: call void @_ZdaPvj(ptr noundef [[T3]], i32 noundef [[T7]])
|
2011-01-27 09:37:56 +00:00
|
|
|
// CHECK-NEXT: br label
|
|
|
|
::delete[] p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace test3 {
|
|
|
|
struct A {
|
|
|
|
int x;
|
|
|
|
void operator delete[](void *, size_t);
|
[clang] Annotating C++'s `operator new` with more attributes
Summary:
Right now we annotate C++'s `operator new` with `noalias` attribute,
which very much is healthy for optimizations.
However as per [[ http://eel.is/c++draft/basic.stc.dynamic.allocation | `[basic.stc.dynamic.allocation]` ]],
there are more promises on global `operator new`, namely:
* non-`std::nothrow_t` `operator new` *never* returns `nullptr`
* If `std::align_val_t align` parameter is taken, the pointer will also be `align`-aligned
* ~~global `operator new`-returned pointer is `__STDCPP_DEFAULT_NEW_ALIGNMENT__`-aligned ~~ It's more caveated than that.
Supplying this information may not cause immediate landslide effects
on any specific benchmarks, but it for sure will be healthy for optimizer
in the sense that the IR will better reflect the guarantees provided in the source code.
The caveat is `-fno-assume-sane-operator-new`, which currently prevents emitting `noalias`
attribute, and is automatically passed by Sanitizers ([[ https://bugs.llvm.org/show_bug.cgi?id=16386 | PR16386 ]]) - should it also cover these attributes?
The problem is that the flag is back-end-specific, as seen in `test/Modules/explicit-build-flags.cpp`.
But while it is okay to add `noalias` metadata in backend, we really should be adding at least
the alignment metadata to the AST, since that allows us to perform sema checks on it.
Reviewers: erichkeane, rjmccall, jdoerfert, eugenis, rsmith
Reviewed By: rsmith
Subscribers: xbolva00, jrtc27, atanasyan, nlopes, cfe-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D73380
2020-02-26 01:37:17 +03:00
|
|
|
};
|
2011-01-27 09:37:56 +00:00
|
|
|
struct B : A {};
|
|
|
|
|
2020-12-31 00:27:11 -08:00
|
|
|
// CHECK-LABEL: define{{.*}} void @_ZN5test34testEv()
|
2011-01-27 09:37:56 +00:00
|
|
|
void test() {
|
2023-02-16 15:30:35 +01:00
|
|
|
// CHECK: [[CALL:%.*]] = call noalias noundef nonnull ptr @_Znaj(i32 noundef 24)
|
2011-01-27 09:37:56 +00:00
|
|
|
// CHECK-NEXT: store i32 5
|
|
|
|
(void) new B[5];
|
|
|
|
}
|
|
|
|
}
|