1 // RUN: %clang_cc1 -std=c++2a -S -emit-llvm -o - -disable-llvm-passes -triple x86_64-apple-macos10.14 %s | FileCheck %s
2 
test_scalar(int & oper)3 void test_scalar(int &oper) {
4   // CHECK-LABEL: define void @_Z11test_scalarRi
5   __builtin_bit_cast(float, oper);
6 
7   // CHECK: [[OPER:%.*]] = alloca i32*
8   // CHECK: [[REF:%.*]] = load i32*, i32**
9   // CHECK-NEXT: [[CASTED:%.*]] = bitcast i32* [[REF]] to float*
10   // CHECK-NEXT: load float, float* [[CASTED]]
11 }
12 
13 struct two_ints {
14   int x;
15   int y;
16 };
17 
test_aggregate_to_scalar(two_ints & ti)18 unsigned long test_aggregate_to_scalar(two_ints &ti) {
19   // CHECK-LABEL: define i64 @_Z24test_aggregate_to_scalarR8two_ints
20   return __builtin_bit_cast(unsigned long, ti);
21 
22   // CHECK: [[TI_ADDR:%.*]] = alloca %struct.two_ints*, align 8
23   // CHECK: [[TI_LOAD:%.*]] = load %struct.two_ints*, %struct.two_ints** [[TI_ADDR]]
24   // CHECK-NEXT: [[CASTED:%.*]] = bitcast %struct.two_ints* [[TI_LOAD]] to i64*
25   // CHECK-NEXT: load i64, i64* [[CASTED]]
26 }
27 
28 struct two_floats {
29   float x;
30   float y;
31 };
32 
test_aggregate_record(two_ints & ti)33 two_floats test_aggregate_record(two_ints& ti) {
34   // CHECK-LABEL: define <2 x float> @_Z21test_aggregate_recordR8two_int
35    return __builtin_bit_cast(two_floats, ti);
36 
37   // CHECK: [[RETVAL:%.*]] = alloca %struct.two_floats, align 4
38   // CHECK: [[TI:%.*]] = alloca %struct.two_ints*, align 8
39 
40   // CHECK: [[LOAD_TI:%.*]] = load %struct.two_ints*, %struct.two_ints** [[TI]]
41   // CHECK: [[MEMCPY_SRC:%.*]] = bitcast %struct.two_ints* [[LOAD_TI]] to i8*
42   // CHECK: [[MEMCPY_DST:%.*]] = bitcast %struct.two_floats* [[RETVAL]] to i8*
43   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[MEMCPY_DST]], i8* align 4 [[MEMCPY_SRC]]
44 }
45 
test_aggregate_array(int (& ary)[2])46 two_floats test_aggregate_array(int (&ary)[2]) {
47   // CHECK-LABEL: define <2 x float> @_Z20test_aggregate_arrayRA2_i
48   return __builtin_bit_cast(two_floats, ary);
49 
50   // CHECK: [[RETVAL:%.*]] = alloca %struct.two_floats, align 4
51   // CHECK: [[ARY:%.*]] = alloca [2 x i32]*, align 8
52 
53   // CHECK: [[LOAD_ARY:%.*]] = load [2 x i32]*, [2 x i32]** [[ARY]]
54   // CHECK: [[MEMCPY_SRC:%.*]] = bitcast [2 x i32]* [[LOAD_ARY]] to i8*
55   // CHECK: [[MEMCPY_DST:%.*]] = bitcast %struct.two_floats* [[RETVAL]] to i8*
56   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[MEMCPY_DST]], i8* align 4 [[MEMCPY_SRC]]
57 }
58 
test_scalar_to_aggregate(unsigned long ul)59 two_ints test_scalar_to_aggregate(unsigned long ul) {
60   // CHECK-LABEL: define i64 @_Z24test_scalar_to_aggregatem
61   return __builtin_bit_cast(two_ints, ul);
62 
63   // CHECK: [[TI:%.*]] = alloca %struct.two_ints, align 4
64   // CHECK: [[TITMP:%.*]] = bitcast %struct.two_ints* [[TI]] to i8*
65   // CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[TITMP]]
66 }
67 
test_complex(_Complex unsigned & cu)68 unsigned long test_complex(_Complex unsigned &cu) {
69   // CHECK-LABEL: define i64 @_Z12test_complexRCj
70   return __builtin_bit_cast(unsigned long, cu);
71 
72   // CHECK: [[REF_ALLOCA:%.*]] = alloca { i32, i32 }*, align 8
73   // CHECK-NEXT: store { i32, i32 }* {{.*}}, { i32, i32 }** [[REF_ALLOCA]]
74   // CHECK-NEXT: [[REF:%.*]] = load { i32, i32 }*, { i32, i32 }** [[REF_ALLOCA]]
75   // CHECK-NEXT: [[CASTED:%.*]] = bitcast { i32, i32 }* [[REF]] to i64*
76   // CHECK-NEXT: load i64, i64* [[CASTED]], align 4
77 }
78 
test_to_complex(unsigned long & ul)79 _Complex unsigned test_to_complex(unsigned long &ul) {
80   // CHECK-LABEL: define i64 @_Z15test_to_complexRm
81 
82   return __builtin_bit_cast(_Complex unsigned, ul);
83 
84   // CHECK: [[REF:%.*]] = alloca i64*
85   // CHECK: [[LOAD_REF:%.*]] = load i64*, i64** [[REF]]
86   // CHECK: [[CASTED:%.*]] = bitcast i64* [[LOAD_REF]] to { i32, i32 }*
87 }
88 
test_array(int (& ary)[2])89 unsigned long test_array(int (&ary)[2]) {
90   // CHECK-LABEL: define i64 @_Z10test_arrayRA2_i
91   return __builtin_bit_cast(unsigned long, ary);
92 
93   // CHECK: [[REF_ALLOCA:%.*]] = alloca [2 x i32]*
94   // CHECK: [[LOAD_REF:%.*]] = load [2 x i32]*, [2 x i32]** [[REF_ALLOCA]]
95   // CHECK: [[CASTED:%.*]] = bitcast [2 x i32]* [[LOAD_REF]] to i64*
96   // CHECK: load i64, i64* [[CASTED]], align 4
97 }
98 
test_rvalue_aggregate()99 two_ints test_rvalue_aggregate() {
100   // CHECK-LABEL: define i64 @_Z21test_rvalue_aggregate
101   return __builtin_bit_cast(two_ints, 42ul);
102 
103   // CHECK: [[TI:%.*]] = alloca %struct.two_ints, align 4
104   // CHECK: [[CASTED:%.*]] = bitcast %struct.two_ints* [[TI]] to i8*
105   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 [[CASTED]]
106 }
107