Debian Codegraph @

clang/test/CIR/CodeGen/object-size.cpp

raw
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2 // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4 // RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
5 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6 // RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
7
8 // C++-specific tests for __builtin_object_size
9
10 int gi;
11
12 // CIR-LABEL: @_Z5test1v
13 // LLVM-LABEL: define{{.*}} void @_Z5test1v()
14 // OGCG-LABEL: define{{.*}} void @_Z5test1v()
15 void test1() {
16 // Guaranteeing that our cast removal logic doesn't break more interesting
17 // cases.
18 struct A { int a; };
19 struct B { int b; };
20 struct C: public A, public B {};
21
22 C c;
23
24 // CIR: cir.const #cir.int<8>
25 // LLVM: store i32 8
26 // OGCG: store i32 8
27 gi = __builtin_object_size(&c, 0);
28 // CIR: cir.const #cir.int<8>
29 // LLVM: store i32 8
30 // OGCG: store i32 8
31 gi = __builtin_object_size((A*)&c, 0);
32 // CIR: cir.const #cir.int<4>
33 // LLVM: store i32 4
34 // OGCG: store i32 4
35 gi = __builtin_object_size((B*)&c, 0);
36
37 // CIR: cir.const #cir.int<8>
38 // LLVM: store i32 8
39 // OGCG: store i32 8
40 gi = __builtin_object_size((char*)&c, 0);
41 // CIR: cir.const #cir.int<8>
42 // LLVM: store i32 8
43 // OGCG: store i32 8
44 gi = __builtin_object_size((char*)(A*)&c, 0);
45 // CIR: cir.const #cir.int<4>
46 // LLVM: store i32 4
47 // OGCG: store i32 4
48 gi = __builtin_object_size((char*)(B*)&c, 0);
49 }
50
51 // CIR-LABEL: @_Z5test2v()
52 // LLVM-LABEL: define{{.*}} void @_Z5test2v()
53 // OGCG-LABEL: define{{.*}} void @_Z5test2v()
54 void test2() {
55 struct A { char buf[16]; };
56 struct B : A {};
57 struct C { int i; B bs[1]; } *c;
58
59 // CIR: cir.objsize max nullunknown %{{.+}} : !cir.ptr<!void> -> !u64i
60 // LLVM: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 false, i1 true, i1 false)
61 // OGCG: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 false, i1 true, i1 false)
62 gi = __builtin_object_size(&c->bs[0], 0);
63 // CIR: cir.objsize max nullunknown %{{.+}} : !cir.ptr<!void> -> !u64i
64 // LLVM: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 false, i1 true, i1 false)
65 // OGCG: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 false, i1 true, i1 false)
66 gi = __builtin_object_size(&c->bs[0], 1);
67 // CIR: cir.objsize min nullunknown %{{.+}} : !cir.ptr<!void> -> !u64i
68 // LLVM: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 true, i1 true, i1 false)
69 // OGCG: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 true, i1 true, i1 false)
70 gi = __builtin_object_size(&c->bs[0], 2);
71 // CIR: cir.const #cir.int<16>
72 // LLVM: store i32 16
73 // OGCG: store i32 16
74 gi = __builtin_object_size(&c->bs[0], 3);
75
76 // NYI: DerivedToBase cast
77 // gi = __builtin_object_size((A*)&c->bs[0], 0);
78
79 // CIR: cir.const #cir.int<16>
80 // LLVM: store i32 16
81 // OGCG: store i32 16
82 gi = __builtin_object_size((A*)&c->bs[0], 1);
83
84 // NYI: DerivedToBase cast
85 // gi = __builtin_object_size((A*)&c->bs[0], 2);
86
87 // CIR: cir.const #cir.int<16>
88 // LLVM: store i32 16
89 // OGCG: store i32 16
90 gi = __builtin_object_size((A*)&c->bs[0], 3);
91
92 // CIR: cir.objsize max nullunknown %{{.+}} : !cir.ptr<!void> -> !u64i
93 // LLVM: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 false, i1 true, i1 false)
94 // OGCG: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 false, i1 true, i1 false)
95 gi = __builtin_object_size(&c->bs[0].buf[0], 0);
96 // CIR: cir.const #cir.int<16>
97 // LLVM: store i32 16
98 // OGCG: store i32 16
99 gi = __builtin_object_size(&c->bs[0].buf[0], 1);
100 // CIR: cir.objsize min nullunknown %{{.+}} : !cir.ptr<!void> -> !u64i
101 // LLVM: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 true, i1 true, i1 false)
102 // OGCG: call i64 @llvm.objectsize.i64.p0(ptr %{{.*}}, i1 true, i1 true, i1 false)
103 gi = __builtin_object_size(&c->bs[0].buf[0], 2);
104 // CIR: cir.const #cir.int<16>
105 // LLVM: store i32 16
106 // OGCG: store i32 16
107 gi = __builtin_object_size(&c->bs[0].buf[0], 3);
108 }