| 1 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -mconstructor-aliases -emit-cir %s -o %t.cir |
| 2 |
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s |
| 3 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -mconstructor-aliases -emit-llvm %s -o %t-cir.ll |
| 4 |
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s |
| 5 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -mconstructor-aliases -emit-llvm %s -o %t.ll |
| 6 |
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s |
| 7 |
|
| 8 |
typedef __typeof(sizeof(int)) size_t; |
| 9 |
|
| 10 |
struct SizedDelete { |
| 11 |
void operator delete(void*, size_t); |
| 12 |
int member; |
| 13 |
}; |
| 14 |
void test_sized_delete(SizedDelete *x) { |
| 15 |
delete x; |
| 16 |
} |
| 17 |
|
| 18 |
// SizedDelete::operator delete(void*, unsigned long) |
| 19 |
// CIR: cir.func private @_ZN11SizedDeletedlEPvm(!cir.ptr<!void>, !u64i) |
| 20 |
// LLVM: declare void @_ZN11SizedDeletedlEPvm(ptr, i64) |
| 21 |
|
| 22 |
// CIR: cir.func {{.*}} @_Z17test_sized_deleteP11SizedDelete |
| 23 |
// CIR: %[[X:.*]] = cir.load{{.*}} %{{.*}} |
| 24 |
// CIR: %[[X_CAST:.*]] = cir.cast bitcast %[[X]] : !cir.ptr<!rec_SizedDelete> -> !cir.ptr<!void> |
| 25 |
// CIR: %[[OBJ_SIZE:.*]] = cir.const #cir.int<4> : !u64i |
| 26 |
// CIR: cir.call @_ZN11SizedDeletedlEPvm(%[[X_CAST]], %[[OBJ_SIZE]]) nothrow : (!cir.ptr<!void>, !u64i) -> () |
| 27 |
|
| 28 |
// LLVM: define dso_local void @_Z17test_sized_deleteP11SizedDelete |
| 29 |
// LLVM: %[[X:.*]] = load ptr, ptr %{{.*}} |
| 30 |
// LLVM: call void @_ZN11SizedDeletedlEPvm(ptr %[[X]], i64 4) |
| 31 |
|
| 32 |
// OGCG: define dso_local void @_Z17test_sized_deleteP11SizedDelete |
| 33 |
// OGCG: %[[X:.*]] = load ptr, ptr %{{.*}} |
| 34 |
// OGCG: %[[ISNULL:.*]] = icmp eq ptr %[[X]], null |
| 35 |
// OGCG: br i1 %[[ISNULL]], label %{{.*}}, label %[[DELETE_NOTNULL:.*]] |
| 36 |
// OGCG: [[DELETE_NOTNULL]]: |
| 37 |
// OGCG: call void @_ZN11SizedDeletedlEPvm(ptr noundef %[[X]], i64 noundef 4) |
| 38 |
|
| 39 |
// This function is declared below the call in OGCG. |
| 40 |
// OGCG: declare void @_ZN11SizedDeletedlEPvm(ptr noundef, i64 noundef) |
| 41 |
|
| 42 |
struct Contents { |
| 43 |
~Contents() {} |
| 44 |
}; |
| 45 |
struct Container { |
| 46 |
Contents *contents; |
| 47 |
~Container(); |
| 48 |
}; |
| 49 |
Container::~Container() { delete contents; } |
| 50 |
|
| 51 |
// Contents::~Contents() |
| 52 |
// CIR: cir.func {{.*}} @_ZN8ContentsD2Ev |
| 53 |
// LLVM: define linkonce_odr void @_ZN8ContentsD2Ev |
| 54 |
|
| 55 |
// operator delete(void*, unsigned long) |
| 56 |
// CIR: cir.func {{.*}} @_ZdlPvm(!cir.ptr<!void>, !u64i) |
| 57 |
// LLVM: declare void @_ZdlPvm(ptr, i64) |
| 58 |
|
| 59 |
// Container::~Container() |
| 60 |
// CIR: cir.func {{.*}} @_ZN9ContainerD2Ev |
| 61 |
// CIR: %[[THIS:.*]] = cir.load %{{.*}} |
| 62 |
// CIR: %[[CONTENTS_PTR_ADDR:.*]] = cir.get_member %[[THIS]][0] {name = "contents"} : !cir.ptr<!rec_Container> -> !cir.ptr<!cir.ptr<!rec_Contents>> |
| 63 |
// CIR: %[[CONTENTS_PTR:.*]] = cir.load{{.*}} %[[CONTENTS_PTR_ADDR]] |
| 64 |
// CIR: cir.call @_ZN8ContentsD2Ev(%[[CONTENTS_PTR]]) nothrow : (!cir.ptr<!rec_Contents>) -> () |
| 65 |
// CIR: %[[CONTENTS_CAST:.*]] = cir.cast bitcast %[[CONTENTS_PTR]] : !cir.ptr<!rec_Contents> -> !cir.ptr<!void> |
| 66 |
// CIR: %[[OBJ_SIZE:.*]] = cir.const #cir.int<1> : !u64i |
| 67 |
// CIR: cir.call @_ZdlPvm(%[[CONTENTS_CAST]], %[[OBJ_SIZE]]) nothrow : (!cir.ptr<!void>, !u64i) -> () |
| 68 |
|
| 69 |
// LLVM: define dso_local void @_ZN9ContainerD2Ev |
| 70 |
// LLVM: %[[THIS:.*]] = load ptr, ptr %{{.*}} |
| 71 |
// LLVM: %[[CONTENTS_PTR_ADDR:.*]] = getelementptr %struct.Container, ptr %[[THIS]], i32 0, i32 0 |
| 72 |
// LLVM: %[[CONTENTS_PTR:.*]] = load ptr, ptr %[[CONTENTS_PTR_ADDR]] |
| 73 |
// LLVM: call void @_ZN8ContentsD2Ev(ptr %[[CONTENTS_PTR]]) |
| 74 |
// LLVM: call void @_ZdlPvm(ptr %[[CONTENTS_PTR]], i64 1) |
| 75 |
|
| 76 |
// OGCG: define dso_local void @_ZN9ContainerD2Ev |
| 77 |
// OGCG: %[[THIS:.*]] = load ptr, ptr %{{.*}} |
| 78 |
// OGCG: %[[CONTENTS:.*]] = getelementptr inbounds nuw %struct.Container, ptr %[[THIS]], i32 0, i32 0 |
| 79 |
// OGCG: %[[CONTENTS_PTR:.*]] = load ptr, ptr %[[CONTENTS]] |
| 80 |
// OGCG: %[[ISNULL:.*]] = icmp eq ptr %[[CONTENTS_PTR]], null |
| 81 |
// OGCG: br i1 %[[ISNULL]], label %{{.*}}, label %[[DELETE_NOTNULL:.*]] |
| 82 |
// OGCG: [[DELETE_NOTNULL]]: |
| 83 |
// OGCG: call void @_ZN8ContentsD2Ev(ptr noundef nonnull align 1 dereferenceable(1) %[[CONTENTS_PTR]]) |
| 84 |
// OGCG: call void @_ZdlPvm(ptr noundef %[[CONTENTS_PTR]], i64 noundef 1) |
| 85 |
|
| 86 |
// These functions are declared/defined below the calls in OGCG. |
| 87 |
// OGCG: define linkonce_odr void @_ZN8ContentsD2Ev |
| 88 |
// OGCG: declare void @_ZdlPvm(ptr noundef, i64 noundef) |
| 89 |
|
| 90 |
struct StructWithVirtualDestructor { |
| 91 |
virtual ~StructWithVirtualDestructor(); |
| 92 |
}; |
| 93 |
|
| 94 |
void destroy(StructWithVirtualDestructor *x) { |
| 95 |
delete x; |
| 96 |
} |
| 97 |
|
| 98 |
// CIR: cir.func {{.*}} @_Z7destroyP27StructWithVirtualDestructor(%[[X_ARG:.*]]: !cir.ptr<!rec_StructWithVirtualDestructor> {{.*}}) |
| 99 |
// CIR: %[[X_ADDR:.*]] = cir.alloca !cir.ptr<!rec_StructWithVirtualDestructor> |
| 100 |
// CIR: cir.store %[[X_ARG]], %[[X_ADDR]] |
| 101 |
// CIR: %[[X:.*]] = cir.load{{.*}} %[[X_ADDR]] |
| 102 |
// CIR: %[[VTABLE_PTR:.*]] = cir.vtable.get_vptr %[[X]] : !cir.ptr<!rec_StructWithVirtualDestructor> -> !cir.ptr<!cir.vptr> |
| 103 |
// CIR: %[[VTABLE:.*]] = cir.load{{.*}} %[[VTABLE_PTR]] : !cir.ptr<!cir.vptr>, !cir.vptr |
| 104 |
// CIR: %[[DTOR_FN_ADDR_PTR:.*]] = cir.vtable.get_virtual_fn_addr %[[VTABLE]][1] |
| 105 |
// CIR: %[[DTOR_FN_ADDR:.*]] = cir.load{{.*}} %[[DTOR_FN_ADDR_PTR]] |
| 106 |
// CIR: cir.call %[[DTOR_FN_ADDR]](%[[X]]) |
| 107 |
|
| 108 |
// LLVM: define {{.*}} void @_Z7destroyP27StructWithVirtualDestructor(ptr %[[X_ARG:.*]]) |
| 109 |
// LLVM: %[[X_ADDR:.*]] = alloca ptr |
| 110 |
// LLVM: store ptr %[[X_ARG]], ptr %[[X_ADDR]] |
| 111 |
// LLVM: %[[X:.*]] = load ptr, ptr %[[X_ADDR]] |
| 112 |
// LLVM: %[[VTABLE:.*]] = load ptr, ptr %[[X]] |
| 113 |
// LLVM: %[[DTOR_FN_ADDR_PTR:.*]] = getelementptr inbounds ptr, ptr %[[VTABLE]], i32 1 |
| 114 |
// LLVM: %[[DTOR_FN_ADDR:.*]] = load ptr, ptr %[[DTOR_FN_ADDR_PTR]] |
| 115 |
// LLVM: call void %[[DTOR_FN_ADDR]](ptr %[[X]]) |
| 116 |
|
| 117 |
// OGCG: define {{.*}} void @_Z7destroyP27StructWithVirtualDestructor(ptr {{.*}} %[[X_ARG:.*]]) |
| 118 |
// OGCG: %[[X_ADDR:.*]] = alloca ptr |
| 119 |
// OGCG: store ptr %[[X_ARG]], ptr %[[X_ADDR]] |
| 120 |
// OGCG: %[[X:.*]] = load ptr, ptr %[[X_ADDR]] |
| 121 |
// OGCG: %[[ISNULL:.*]] = icmp eq ptr %[[X]], null |
| 122 |
// OGCG: br i1 %[[ISNULL]], label %{{.*}}, label %[[DELETE_NOTNULL:.*]] |
| 123 |
// OGCG: [[DELETE_NOTNULL]]: |
| 124 |
// OGCG: %[[VTABLE:.*]] = load ptr, ptr %[[X]] |
| 125 |
// OGCG: %[[DTOR_FN_ADDR_PTR:.*]] = getelementptr inbounds ptr, ptr %[[VTABLE]], i64 1 |
| 126 |
// OGCG: %[[DTOR_FN_ADDR:.*]] = load ptr, ptr %[[DTOR_FN_ADDR_PTR]] |
| 127 |
// OGCG: call void %[[DTOR_FN_ADDR]](ptr {{.*}} %[[X]]) |