| 1 |
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -ffine-grained-bitfield-accesses %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 -ffine-grained-bitfield-accesses %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 -ffine-grained-bitfield-accesses %s -o %t.ll |
| 6 |
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG |
| 7 |
|
| 8 |
struct S1 { |
| 9 |
unsigned f1:2; |
| 10 |
unsigned f2:6; |
| 11 |
unsigned f3:8; |
| 12 |
unsigned f4:4; |
| 13 |
unsigned f5:8; |
| 14 |
}; |
| 15 |
|
| 16 |
// CIR-DAG: !rec_S1 = !cir.record<struct "S1" {!u8i, !u8i, !u16i}> |
| 17 |
// LLVM-DAG: %struct.S1 = type { i8, i8, i16 } |
| 18 |
// OGCG-DAG: %struct.S1 = type { i8, i8, i16 } |
| 19 |
|
| 20 |
struct S2 { |
| 21 |
unsigned long f1:16; |
| 22 |
unsigned long f2:16; |
| 23 |
unsigned long f3:6; |
| 24 |
}; |
| 25 |
|
| 26 |
// CIR-DAG: !rec_S2 = !cir.record<struct "S2" padded {!u16i, !u16i, !u8i, !cir.array<!u8i x 3>}> |
| 27 |
// LLVM-DAG: %struct.S2 = type { i16, i16, i8, [3 x i8] } |
| 28 |
// OGCG-DAG: %struct.S2 = type { i16, i16, i8, [3 x i8] } |
| 29 |
|
| 30 |
struct S3 { |
| 31 |
unsigned long f1:14; |
| 32 |
unsigned long f2:18; |
| 33 |
unsigned long f3:32; |
| 34 |
}; |
| 35 |
|
| 36 |
// CIR-DAG: !rec_S3 = !cir.record<struct "S3" {!u32i, !u32i}> |
| 37 |
// LLVM-DAG: %struct.S3 = type { i32, i32 } |
| 38 |
// OGCG-DAG: %struct.S3 = type { i32, i32 } |
| 39 |
|
| 40 |
S1 a1; |
| 41 |
S2 a2; |
| 42 |
S3 a3; |
| 43 |
|
| 44 |
unsigned read8_1() { |
| 45 |
return a1.f3; |
| 46 |
} |
| 47 |
|
| 48 |
// CIR-LABEL: @_Z7read8_1v |
| 49 |
// CIR: [[MEMBER:%.*]] = cir.get_member %1[1] {name = "f3"} : !cir.ptr<!rec_S1> -> !cir.ptr<!u8i> |
| 50 |
// CIR: [[BITFI:%.*]] = cir.get_bitfield align(1) (#bfi_f3, [[MEMBER]] : !cir.ptr<!u8i>) -> !u32i |
| 51 |
// CIR: cir.store [[BITFI]], {{.*}} : !u32i, !cir.ptr<!u32i> |
| 52 |
// CIR: [[RET:%.*]] = cir.load {{.*}} : !cir.ptr<!u32i>, !u32i |
| 53 |
// CIR: cir.return [[RET]] : !u32i |
| 54 |
|
| 55 |
// LLVM-LABEL: @_Z7read8_1v |
| 56 |
// LLVM: [[MEMBER:%.*]] = load i8, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 1), align 1 |
| 57 |
// LLVM: [[BFCAST:%.*]] = zext i8 [[MEMBER]] to i32 |
| 58 |
// LLVM: store i32 [[BFCAST]], ptr {{.*}}, align 4 |
| 59 |
// LLVM: [[RET:%.*]] = load i32, ptr {{.*}}, align 4 |
| 60 |
// LLVM: ret i32 [[RET]] |
| 61 |
|
| 62 |
// OGCG-LABEL: @_Z7read8_1v |
| 63 |
// OGCG: [[BFLOAD:%.*]] = load i8, ptr getelementptr inbounds nuw (%struct.S1, ptr {{.*}}, i32 0, i32 1), align 1 |
| 64 |
// OGCG-NEXT: [[BFCAST:%.*]] = zext i8 [[BFLOAD]] to i32 |
| 65 |
// OGCG-NEXT: ret i32 [[BFCAST]] |
| 66 |
|
| 67 |
void write8_1() { |
| 68 |
a1.f3 = 3; |
| 69 |
} |
| 70 |
|
| 71 |
// CIR-LABEL: @_Z8write8_1v |
| 72 |
// CIR: [[CONST3:%.*]] = cir.const #cir.int<3> : !u32i |
| 73 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[1] {name = "f3"} : !cir.ptr<!rec_S1> -> !cir.ptr<!u8i> |
| 74 |
// CIR: cir.set_bitfield align(1) (#bfi_f3, [[MEMBER]] : !cir.ptr<!u8i>, [[CONST3]] : !u32i) -> !u32i |
| 75 |
|
| 76 |
// LLVM-LABEL: @_Z8write8_1v |
| 77 |
// LLVM: store i8 3, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 1), align 1 |
| 78 |
// LLVM: ret void |
| 79 |
|
| 80 |
// OGCG-LABEL: @_Z8write8_1v |
| 81 |
// OGCG: store i8 3, ptr getelementptr inbounds nuw (%struct.S1, ptr {{.*}}, i32 0, i32 1), align 1 |
| 82 |
// OGCG-NEXT: ret void |
| 83 |
|
| 84 |
unsigned read8_2() { |
| 85 |
|
| 86 |
return a1.f5; |
| 87 |
} |
| 88 |
|
| 89 |
// CIR-LABEL: @_Z7read8_2v |
| 90 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[2] {name = "f5"} : !cir.ptr<!rec_S1> -> !cir.ptr<!u16i> |
| 91 |
// CIR: [[BITFI:%.*]] = cir.get_bitfield align(2) (#bfi_f5, [[MEMBER]] : !cir.ptr<!u16i>) -> !u32i |
| 92 |
// CIR: cir.store [[BITFI]], {{.*}} : !u32i, !cir.ptr<!u32i> |
| 93 |
// CIR: [[RET:%.*]] = cir.load {{.*}} : !cir.ptr<!u32i>, !u32i |
| 94 |
// CIR: cir.return [[RET]] : !u32i |
| 95 |
|
| 96 |
// LLVM-LABEL: @_Z7read8_2v |
| 97 |
// LLVM: [[BFLOAD:%.*]] = load i16, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 2), align 2 |
| 98 |
// LLVM: [[BFLSHR:%.*]] = lshr i16 [[BFLOAD]], 4 |
| 99 |
// LLVM: [[BFCLEAR:%.*]] = and i16 [[BFLSHR]], 255 |
| 100 |
// LLVM: [[BFCAST:%.*]] = zext i16 [[BFCLEAR]] to i32 |
| 101 |
// LLVM: store i32 [[BFCAST]], ptr {{.*}}, align 4 |
| 102 |
// LLVM: [[RET:%.*]] = load i32, ptr {{.*}}, align 4 |
| 103 |
// LLVM: ret i32 [[RET]] |
| 104 |
|
| 105 |
// OGCG-LABEL: @_Z7read8_2v |
| 106 |
// OGCG: [[BFLOAD:%.*]] = load i16, ptr getelementptr inbounds nuw (%struct.S1, ptr {{.*}}, i32 0, i32 2), align 2 |
| 107 |
// OGCG-NEXT: [[BFLSHR:%.*]] = lshr i16 [[BFLOAD]], 4 |
| 108 |
// OGCG-NEXT: [[BFCLEAR:%.*]] = and i16 [[BFLSHR]], 255 |
| 109 |
// OGCG-NEXT: [[BFCAST:%.*]] = zext i16 [[BFCLEAR]] to i32 |
| 110 |
// OGCG-NEXT: ret i32 [[BFCAST]] |
| 111 |
|
| 112 |
void write8_2() { |
| 113 |
a1.f5 = 3; |
| 114 |
} |
| 115 |
|
| 116 |
// CIR-LABEL: @_Z8write8_2v |
| 117 |
// CIR: [[CONST3:%.*]] = cir.const #cir.int<3> : !u32i |
| 118 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[2] {name = "f5"} : !cir.ptr<!rec_S1> -> !cir.ptr<!u16i> |
| 119 |
// CIR: cir.set_bitfield align(2) (#bfi_f5, [[MEMBER]] : !cir.ptr<!u16i>, [[CONST3]] : !u32i) -> !u32i |
| 120 |
|
| 121 |
// LLVM-LABEL: @_Z8write8_2v |
| 122 |
// LLVM: [[BFLOAD:%.*]] = load i16, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 2), align 2 |
| 123 |
// LLVM: [[BFCLEAR:%.*]] = and i16 [[BFLOAD]], -4081 |
| 124 |
// LLVM: [[BFSET:%.*]] = or i16 [[BFCLEAR]], 48 |
| 125 |
// LLVM: store i16 [[BFSET]], ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 2), align 2 |
| 126 |
// LLVM: ret void |
| 127 |
|
| 128 |
// OGCG-LABEL: @_Z8write8_2v |
| 129 |
// OGCG: [[BFLOAD:%.*]] = load i16, ptr getelementptr inbounds nuw (%struct.S1, ptr {{.*}}, i32 0, i32 2), align 2 |
| 130 |
// OGCG-NEXT: [[BFCLEAR:%.*]] = and i16 [[BFLOAD]], -4081 |
| 131 |
// OGCG-NEXT: [[BFSET:%.*]] = or i16 [[BFCLEAR]], 48 |
| 132 |
// OGCG-NEXT: store i16 [[BFSET]], ptr getelementptr inbounds nuw (%struct.S1, ptr {{.*}}, i32 0, i32 2), align 2 |
| 133 |
// OGCG-NEXT: ret void |
| 134 |
|
| 135 |
unsigned read16_1() { |
| 136 |
return a2.f1; |
| 137 |
} |
| 138 |
|
| 139 |
// CIR-LABEL: @_Z8read16_1v |
| 140 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[0] {name = "f1"} : !cir.ptr<!rec_S2> -> !cir.ptr<!u16i> |
| 141 |
// CIR: [[BITFI:%.*]] = cir.get_bitfield align(8) (#bfi_f1, [[MEMBER]] : !cir.ptr<!u16i>) -> !u64i |
| 142 |
// CIR: [[BFCAST:%.*]] = cir.cast integral [[BITFI]] : !u64i -> !u32i |
| 143 |
// CIR: cir.store [[BFCAST]], {{.*}} : !u32i, !cir.ptr<!u32i> |
| 144 |
// CIR: [[RET:%.*]] = cir.load {{.*}} : !cir.ptr<!u32i>, !u32i |
| 145 |
// CIR: cir.return [[RET]] : !u32i |
| 146 |
|
| 147 |
// LLVM-LABEL: @_Z8read16_1v |
| 148 |
// LLVM: [[BFLOAD:%.*]] = load i16, ptr {{.*}}, align 8 |
| 149 |
// LLVM: [[BFCAST:%.*]] = zext i16 [[BFLOAD]] to i64 |
| 150 |
// LLVM: [[BF:%.*]] = trunc i64 [[BFCAST]] to i32 |
| 151 |
// LLVM: store i32 [[BF]], ptr {{.*}}, align 4 |
| 152 |
// LLVM: [[RET:%.*]] = load i32, ptr {{.*}}, align 4 |
| 153 |
// LLVM: ret i32 [[RET]] |
| 154 |
|
| 155 |
// OGCG-LABEL: @_Z8read16_1v |
| 156 |
// OGCG: [[BFLOAD:%.*]] = load i16, ptr {{.*}}, align 8 |
| 157 |
// OGCG-NEXT: [[BFCAST:%.*]] = zext i16 [[BFLOAD]] to i64 |
| 158 |
// OGCG-NEXT: [[RET:%.*]] = trunc i64 [[BFCAST]] to i32 |
| 159 |
// OGCG-NEXT: ret i32 [[RET]] |
| 160 |
|
| 161 |
unsigned read16_2() { |
| 162 |
return a2.f2; |
| 163 |
} |
| 164 |
|
| 165 |
// CIR-LABEL: @_Z8read16_2v |
| 166 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[1] {name = "f2"} : !cir.ptr<!rec_S2> -> !cir.ptr<!u16i> |
| 167 |
// CIR: [[BITFI:%.*]] = cir.get_bitfield align(2) (#bfi_f2, [[MEMBER]] : !cir.ptr<!u16i>) -> !u64i |
| 168 |
// CIR: [[BFCAST:%.*]] = cir.cast integral [[BITFI]] : !u64i -> !u32i |
| 169 |
// CIR: cir.store [[BFCAST]], {{.*}} : !u32i, !cir.ptr<!u32i> |
| 170 |
// CIR: [[RET:%.*]] = cir.load {{.*}} : !cir.ptr<!u32i>, !u32i |
| 171 |
// CIR: cir.return [[RET]] : !u32i |
| 172 |
|
| 173 |
// LLVM-LABEL: @_Z8read16_2v |
| 174 |
// LLVM: [[BFLOAD:%.*]] = load i16, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 2), align 2 |
| 175 |
// LLVM: [[BFCAST:%.*]] = zext i16 [[BFLOAD]] to i64 |
| 176 |
// LLVM: [[BF:%.*]] = trunc i64 [[BFCAST]] to i32 |
| 177 |
// LLVM: store i32 [[BF]], ptr {{.*}}, align 4 |
| 178 |
// LLVM: [[RET:%.*]] = load i32, ptr {{.*}}, align 4 |
| 179 |
// LLVM: ret i32 [[RET]] |
| 180 |
|
| 181 |
// OGCG-LABEL: @_Z8read16_2v |
| 182 |
// OGCG: [[BFLOAD:%.*]] = load i16, ptr getelementptr inbounds nuw (%struct.S2, ptr {{.*}}, i32 0, i32 1), align 2 |
| 183 |
// OGCG-NEXT: [[BFCAST:%.*]] = zext i16 [[BFLOAD]] to i64 |
| 184 |
// OGCG-NEXT: [[RET:%.*]] = trunc i64 [[BFCAST]] to i32 |
| 185 |
// OGCG-NEXT: ret i32 [[RET]] |
| 186 |
|
| 187 |
void write16_1() { |
| 188 |
a2.f1 = 5; |
| 189 |
} |
| 190 |
|
| 191 |
// CIR-LABEL: @_Z9write16_1v |
| 192 |
// CIR: [[CONST5:%.*]] = cir.const #cir.int<5> : !u64i |
| 193 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[0] {name = "f1"} : !cir.ptr<!rec_S2> -> !cir.ptr<!u16i> |
| 194 |
// CIR: cir.set_bitfield align(8) (#bfi_f1, [[MEMBER]] : !cir.ptr<!u16i>, [[CONST5]] : !u64i) -> !u64i |
| 195 |
// CIR: cir.return |
| 196 |
|
| 197 |
// LLVM-LABEL: @_Z9write16_1v |
| 198 |
// LLVM: store i16 5, ptr {{.*}}, align 8 |
| 199 |
// LLVM: ret void |
| 200 |
|
| 201 |
// OGCG-LABEL: @_Z9write16_1v |
| 202 |
// OGCG: store i16 5, ptr {{.*}}, align 8 |
| 203 |
// OGCG-NEXT: ret void |
| 204 |
|
| 205 |
void write16_2() { |
| 206 |
|
| 207 |
a2.f2 = 5; |
| 208 |
} |
| 209 |
|
| 210 |
// CIR-LABEL: @_Z9write16_2v |
| 211 |
// CIR: [[CONST5:%.*]] = cir.const #cir.int<5> : !u64i |
| 212 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[1] {name = "f2"} : !cir.ptr<!rec_S2> -> !cir.ptr<!u16i> |
| 213 |
// CIR: cir.set_bitfield align(2) (#bfi_f2, [[MEMBER]] : !cir.ptr<!u16i>, [[CONST5]] : !u64i) -> !u64i |
| 214 |
// CIR: cir.return |
| 215 |
|
| 216 |
// LLVM-LABEL: @_Z9write16_2v |
| 217 |
// LLVM: store i16 5, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 2), align 2 |
| 218 |
// LLVM: ret void |
| 219 |
|
| 220 |
// OGCG-LABEL: @_Z9write16_2v |
| 221 |
// OGCG: store i16 5, ptr getelementptr inbounds nuw (%struct.S2, ptr {{.*}}, i32 0, i32 1), align 2 |
| 222 |
// OGCG-NEXT: ret void |
| 223 |
|
| 224 |
unsigned read32_1() { |
| 225 |
|
| 226 |
return a3.f3; |
| 227 |
} |
| 228 |
// CIR-LABEL: @_Z8read32_1v |
| 229 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[1] {name = "f3"} : !cir.ptr<!rec_S3> -> !cir.ptr<!u32i> |
| 230 |
// CIR: [[BITFI:%.*]] = cir.get_bitfield align(4) (#bfi_f3_1, [[MEMBER]] : !cir.ptr<!u32i>) -> !u64i |
| 231 |
// CIR: [[BFCAST:%.*]] = cir.cast integral [[BITFI]] : !u64i -> !u32i |
| 232 |
// CIR: cir.store [[BFCAST]], {{.*}} : !u32i, !cir.ptr<!u32i> |
| 233 |
// CIR: [[RET:%.*]] = cir.load {{.*}} : !cir.ptr<!u32i>, !u32i |
| 234 |
// CIR: cir.return [[RET]] : !u32i |
| 235 |
|
| 236 |
// LLVM-LABEL: @_Z8read32_1v |
| 237 |
// LLVM: [[BFLOAD:%.*]] = load i32, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 4), align 4 |
| 238 |
// LLVM: [[BFCAST:%.*]] = zext i32 [[BFLOAD]] to i64 |
| 239 |
// LLVM: [[BF:%.*]] = trunc i64 [[BFCAST]] to i32 |
| 240 |
// LLVM: store i32 [[BF]], ptr {{.*}}, align 4 |
| 241 |
// LLVM: [[RET:%.*]] = load i32, ptr {{.*}}, align 4 |
| 242 |
// LLVM: ret i32 [[RET]] |
| 243 |
|
| 244 |
// OGCG-LABEL: @_Z8read32_1v |
| 245 |
// OGCG: [[BFLOAD:%.*]] = load i32, ptr getelementptr inbounds nuw (%struct.S3, ptr {{.*}}, i32 0, i32 1), align 4 |
| 246 |
// OGCG-NEXT: [[BFCAST:%.*]] = zext i32 %bf.load to i64 |
| 247 |
// OGCG-NEXT: [[RET:%.*]] = trunc i64 %bf.cast to i32 |
| 248 |
// OGCG-NEXT: ret i32 [[RET]] |
| 249 |
|
| 250 |
void write32_1() { |
| 251 |
a3.f3 = 5; |
| 252 |
} |
| 253 |
|
| 254 |
// CIR-LABEL: @_Z9write32_1v |
| 255 |
// CIR: [[CONST5:%.*]] = cir.const #cir.int<5> : !u64i |
| 256 |
// CIR: [[MEMBER:%.*]] = cir.get_member {{.*}}[1] {name = "f3"} : !cir.ptr<!rec_S3> -> !cir.ptr<!u32i> |
| 257 |
// CIR: cir.set_bitfield align(4) (#bfi_f3_1, [[MEMBER]] : !cir.ptr<!u32i>, [[CONST5]] : !u64i) -> !u64i |
| 258 |
// CIR: cir.return |
| 259 |
|
| 260 |
// LLVM-LABEL: @_Z9write32_1v |
| 261 |
// LLVM: store i32 5, ptr getelementptr inbounds nuw (i8, ptr {{.*}}, i64 4), align 4 |
| 262 |
// LLVM: ret void |
| 263 |
|
| 264 |
// OGCG-LABEL: @_Z9write32_1v |
| 265 |
// OGCG: store i32 5, ptr getelementptr inbounds nuw (%struct.S3, ptr {{.*}}, i32 0, i32 1), align 4 |
| 266 |
// OGCG-NEXT: ret void |