Debian Codegraph @

clang/test/CIR/CodeGen/bitfields.cpp

raw
1 // RUN: %clang_cc1 -std=c++17 -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 -std=c++17 -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 -std=c++17 -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 typedef struct {
9 int a : 4;
10 int b : 27;
11 int c : 17;
12 int d : 2;
13 int e : 15;
14 unsigned f; // type other than int above, not a bitfield
15 } S;
16 // CIR-DAG: !rec_S = !cir.record<struct "S" {!u64i, !u16i, !u32i}>
17 // CIR-DAG: #bfi_c = #cir.bitfield_info<name = "c", storage_type = !u64i, size = 17, offset = 32, is_signed = true>
18 // LLVM-DAG: %struct.S = type { i64, i16, i32 }
19 // OGCG-DAG: %struct.S = type { i64, i16, i32 }
20
21 typedef struct {
22 int a : 3; // one bitfield with size < 8
23 unsigned b;
24 } T;
25
26 // CIR-DAG: !rec_T = !cir.record<struct "T" {!u8i, !u32i}>
27 // LLVM-DAG: %struct.T = type { i8, i32 }
28 // OGCG-DAG: %struct.T = type { i8, i32 }
29
30 void def() {
31 S s;
32 T t;
33 }
34
35 int load_field(S* s) {
36 return s->c;
37 }
38 // CIR: cir.func {{.*}} @_Z10load_field
39 // CIR: [[TMP0:%.*]] = cir.alloca !cir.ptr<!rec_S>, !cir.ptr<!cir.ptr<!rec_S>>, ["s", init]
40 // CIR: [[TMP1:%.*]] = cir.load{{.*}} [[TMP0]] : !cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
41 // CIR: [[TMP2:%.*]] = cir.get_member [[TMP1]][0] {name = "c"} : !cir.ptr<!rec_S> -> !cir.ptr<!u64i>
42 // CIR: [[TMP3:%.*]] = cir.get_bitfield align(4) (#bfi_c, [[TMP2]] : !cir.ptr<!u64i>) -> !s32i
43
44 // LLVM: define dso_local i32 @_Z10load_fieldP1S
45 // LLVM: [[TMP0:%.*]] = alloca ptr, i64 1, align 8
46 // LLVM: [[TMP1:%.*]] = alloca i32, i64 1, align 4
47 // LLVM: [[TMP2:%.*]] = load ptr, ptr [[TMP0]], align 8
48 // LLVM: [[TMP3:%.*]] = getelementptr %struct.S, ptr [[TMP2]], i32 0, i32 0
49 // LLVM: [[TMP4:%.*]] = load i64, ptr [[TMP3]], align 4
50 // LLVM: [[TMP5:%.*]] = shl i64 [[TMP4]], 15
51 // LLVM: [[TMP6:%.*]] = ashr i64 [[TMP5]], 47
52 // LLVM: [[TMP7:%.*]] = trunc i64 [[TMP6]] to i32
53
54 // OGCG: define dso_local noundef i32 @_Z10load_fieldP1S
55 // OGCG: [[TMP0:%.*]] = alloca ptr, align 8
56 // OGCG: [[TMP1:%.*]] = load ptr, ptr [[TMP0]], align 8
57 // OGCG: [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 4
58 // OGCG: [[TMP3:%.*]] = shl i64 [[TMP2]], 15
59 // OGCG: [[TMP4:%.*]] = ashr i64 [[TMP3]], 47
60 // OGCG: [[TMP5:%.*]] = trunc i64 [[TMP4]] to i32
61
62 void store_field() {
63 S s;
64 s.a = 3;
65 }
66 // CIR: cir.func {{.*}} @_Z11store_field
67 // CIR: [[TMP0:%.*]] = cir.alloca !rec_S, !cir.ptr<!rec_S>
68 // CIR: [[TMP1:%.*]] = cir.const #cir.int<3> : !s32i
69 // CIR: [[TMP2:%.*]] = cir.get_member [[TMP0]][0] {name = "a"} : !cir.ptr<!rec_S> -> !cir.ptr<!u64i>
70 // CIR: cir.set_bitfield align(4) (#bfi_a, [[TMP2]] : !cir.ptr<!u64i>, [[TMP1]] : !s32i)
71
72 // LLVM: define dso_local void @_Z11store_fieldv
73 // LLVM: [[TMP0:%.*]] = alloca %struct.S, i64 1, align 4
74 // LLVM: [[TMP1:%.*]] = getelementptr %struct.S, ptr [[TMP0]], i32 0, i32 0
75 // LLVM: [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 4
76 // LLVM: [[TMP3:%.*]] = and i64 [[TMP2]], -16
77 // LLVM: [[TMP4:%.*]] = or i64 [[TMP3]], 3
78 // LLVM: store i64 [[TMP4]], ptr [[TMP1]], align 4
79
80 // OGCG: define dso_local void @_Z11store_fieldv()
81 // OGCG: [[TMP0:%.*]] = alloca %struct.S, align 4
82 // OGCG: [[TMP1:%.*]] = load i64, ptr [[TMP0]], align 4
83 // OGCG: [[TMP2:%.*]] = and i64 [[TMP1]], -16
84 // OGCG: [[TMP3:%.*]] = or i64 [[TMP2]], 3
85 // OGCG: store i64 [[TMP3]], ptr [[TMP0]], align 4
86
87 void store_bitfield_to_bitfield(S* s) {
88 s->a = s->b = 3;
89 }
90
91 // CIR: cir.func {{.*}} @_Z26store_bitfield_to_bitfieldP1S
92 // CIR: [[TMP0:%.*]] = cir.alloca !cir.ptr<!rec_S>, !cir.ptr<!cir.ptr<!rec_S>>, ["s", init] {alignment = 8 : i64}
93 // CIR: [[TMP1:%.*]] = cir.const #cir.int<3> : !s32i
94 // CIR: [[TMP2:%.*]] = cir.load align(8) [[TMP0]] : !cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
95 // CIR: [[TMP3:%.*]] = cir.get_member [[TMP2]][0] {name = "b"} : !cir.ptr<!rec_S> -> !cir.ptr<!u64i>
96 // CIR: [[TMP4:%.*]] = cir.set_bitfield align(4) (#bfi_b, [[TMP3]] : !cir.ptr<!u64i>, [[TMP1]] : !s32i) -> !s32i
97 // CIR: [[TMP5:%.*]] = cir.load align(8) [[TMP0]] : !cir.ptr<!cir.ptr<!rec_S>>, !cir.ptr<!rec_S>
98 // CIR: [[TMP6:%.*]] = cir.get_member [[TMP5]][0] {name = "a"} : !cir.ptr<!rec_S> -> !cir.ptr<!u64i>
99 // CIR: [[TMP7:%.*]] = cir.set_bitfield align(4) (#bfi_a, [[TMP6]] : !cir.ptr<!u64i>, [[TMP4]] : !s32i) -> !s32i
100
101 // LLVM: define dso_local void @_Z26store_bitfield_to_bitfieldP1S
102 // LLVM: [[TMP0:%.*]] = alloca ptr, i64 1, align 8
103 // LLVM: [[TMP1:%.*]] = load ptr, ptr [[TMP0]], align 8
104 // LLVM: [[TMP2:%.*]] = getelementptr %struct.S, ptr [[TMP1]], i32 0, i32 0
105 // LLVM: [[TMP3:%.*]] = load i64, ptr [[TMP2]], align 4
106 // LLVM: [[TMP4:%.*]] = and i64 [[TMP3]], -2147483633
107 // LLVM: [[TMP5:%.*]] = or i64 [[TMP4]], 48
108 // LLVM: store i64 [[TMP5]], ptr [[TMP2]], align 4
109 // LLVM: [[TMP6:%.*]] = load ptr, ptr [[TMP0]], align 8
110 // LLVM: [[TMP7:%.*]] = getelementptr %struct.S, ptr [[TMP6]], i32 0, i32 0
111 // LLVM: [[TMP8:%.*]] = load i64, ptr [[TMP7]], align 4
112 // LLVM: [[TMP9:%.*]] = and i64 [[TMP8]], -16
113 // LLVM: [[TMP10:%.*]] = or i64 [[TMP9]], 3
114 // LLVM: store i64 [[TMP10]], ptr [[TMP7]], align 4
115
116 // OGCG: define dso_local void @_Z26store_bitfield_to_bitfieldP1S
117 // OGCG: [[TMP0:%.*]] = alloca ptr, align 8
118 // OGCG: [[TMP1:%.*]] = load ptr, ptr [[TMP0]], align 8
119 // OGCG: [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 4
120 // OGCG: [[TMP3:%.*]] = and i64 [[TMP2]], -2147483633
121 // OGCG: [[TMP4:%.*]] = or i64 [[TMP3]], 48
122 // OGCG: store i64 [[TMP4]], ptr [[TMP1]], align 4
123 // OGCG: [[TMP5:%.*]] = load ptr, ptr [[TMP0]], align 8
124 // OGCG: [[TMP6:%.*]] = load i64, ptr [[TMP5]], align 4
125 // OGCG: [[TMP7:%.*]] = and i64 [[TMP6]], -16
126 // OGCG: [[TMP8:%.*]] = or i64 [[TMP7]], 3
127 // OGCG: store i64 [[TMP8]], ptr [[TMP5]], align 4