Debian Codegraph @

clang/test/CIR/CodeGen/binassign.c

raw
1 // RUN: %clang_cc1 -std=c23 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2> %t-before-lp.cir
2 // RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3 // RUN: %clang_cc1 -std=c23 -triple x86_64-unknown-linux-gnu -Wno-unused-value -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=c23 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
6 // RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
7
8 void binary_assign(void) {
9 bool b;
10 char c;
11 float f;
12 int i;
13
14 b = true;
15 c = 65;
16 f = 3.14f;
17 i = 42;
18 }
19
20 // CIR-LABEL: cir.func{{.*}} @binary_assign()
21 // CIR: %[[B:.*]] = cir.alloca !cir.bool, !cir.ptr<!cir.bool>, ["b"]
22 // CIR: %[[C:.*]] = cir.alloca !s8i, !cir.ptr<!s8i>, ["c"]
23 // CIR: %[[F:.*]] = cir.alloca !cir.float, !cir.ptr<!cir.float>, ["f"]
24 // CIR: %[[I:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i"]
25 // CIR: %[[TRUE:.*]] = cir.const #true
26 // CIR: cir.store{{.*}} %[[TRUE]], %[[B]] : !cir.bool, !cir.ptr<!cir.bool>
27 // CIR: %[[CHAR_VAL:.*]] = cir.const #cir.int<65> : !s8i
28 // CIR: cir.store{{.*}} %[[CHAR_VAL]], %[[C]] : !s8i, !cir.ptr<!s8i>
29 // CIR: %[[FLOAT_VAL:.*]] = cir.const #cir.fp<3.140000e+00> : !cir.float
30 // CIR: cir.store{{.*}} %[[FLOAT_VAL]], %[[F]] : !cir.float, !cir.ptr<!cir.float>
31 // CIR: %[[INT_VAL:.*]] = cir.const #cir.int<42> : !s32i
32 // CIR: cir.store{{.*}} %[[INT_VAL]], %[[I]] : !s32i, !cir.ptr<!s32i>
33 // CIR: cir.return
34
35 // LLVM-LABEL: define {{.*}}void @binary_assign(){{.*}} {
36 // LLVM: %[[B_PTR:.*]] = alloca i8
37 // LLVM: %[[C_PTR:.*]] = alloca i8
38 // LLVM: %[[F_PTR:.*]] = alloca float
39 // LLVM: %[[I_PTR:.*]] = alloca i32
40 // LLVM: store i8 1, ptr %[[B_PTR]]
41 // LLVM: store i8 65, ptr %[[C_PTR]]
42 // LLVM: store float 0x40091EB860000000, ptr %[[F_PTR]]
43 // LLVM: store i32 42, ptr %[[I_PTR]]
44 // LLVM: ret void
45
46 // OGCG-LABEL: define {{.*}}void @binary_assign()
47 // OGCG: %[[B_PTR:.*]] = alloca i8
48 // OGCG: %[[C_PTR:.*]] = alloca i8
49 // OGCG: %[[F_PTR:.*]] = alloca float
50 // OGCG: %[[I_PTR:.*]] = alloca i32
51 // OGCG: store i8 1, ptr %[[B_PTR]]
52 // OGCG: store i8 65, ptr %[[C_PTR]]
53 // OGCG: store float 0x40091EB860000000, ptr %[[F_PTR]]
54 // OGCG: store i32 42, ptr %[[I_PTR]]
55 // OGCG: ret void
56
57 struct S {
58 int a;
59 float b;
60 };
61
62 struct SV {
63 int a;
64 volatile float b;
65 };
66
67 struct S gs;
68 struct SV gsv;
69
70 void binary_assign_struct() {
71 // Test normal struct assignment
72 struct S ls;
73 ls = gs;
74
75 // Test assignment of a struct with a volatile member
76 struct SV lsv;
77 lsv = gsv;
78 }
79
80 // CIR: cir.func{{.*}} @binary_assign_struct()
81 // CIR: %[[LS:.*]] = cir.alloca ![[REC_S:.*]], !cir.ptr<![[REC_S]]>, ["ls"]
82 // CIR: %[[LSV:.*]] = cir.alloca ![[REC_SV:.*]], !cir.ptr<![[REC_SV]]>, ["lsv"]
83 // CIR: %[[GS_PTR:.*]] = cir.get_global @gs : !cir.ptr<![[REC_S]]>
84 // CIR: cir.copy %[[GS_PTR]] to %[[LS]] : !cir.ptr<![[REC_S]]>
85 // CIR: %[[GSV_PTR:.*]] = cir.get_global @gsv : !cir.ptr<![[REC_SV]]>
86 // CIR: cir.copy %[[GSV_PTR]] to %[[LSV]] volatile : !cir.ptr<![[REC_SV]]>
87 // CIR: cir.return
88
89 // LLVM: define {{.*}}void @binary_assign_struct()
90 // LLVM: %[[LS_PTR:.*]] = alloca %struct.S
91 // LLVM: %[[LSV_PTR:.*]] = alloca %struct.SV
92 // LLVM: call void @llvm.memcpy.p0.p0.i32(ptr %[[LS_PTR]], ptr @gs, i32 8, i1 false)
93 // LLVM: call void @llvm.memcpy.p0.p0.i32(ptr %[[LSV_PTR]], ptr @gsv, i32 8, i1 true)
94 // LLVM: ret void
95
96 // OGCG: define {{.*}}void @binary_assign_struct()
97 // OGCG: %[[LS_PTR:.*]] = alloca %struct.S
98 // OGCG: %[[LSV_PTR:.*]] = alloca %struct.SV
99 // OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %[[LS_PTR]], ptr align 4 @gs, i64 8, i1 false)
100 // OGCG: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %[[LSV_PTR]], ptr align 4 @gsv, i64 8, i1 true)
101 // OGCG: ret void
102
103 int ignore_result_assign() {
104 int arr[10];
105 int i, j;
106 j = i = 123, 0;
107 j = arr[i = 5];
108 int *p, *q = 0;
109 if(p = q)
110 return 1;
111 return 0;
112 }
113
114 // CIR-LABEL: cir.func{{.*}} @ignore_result_assign() -> !s32i
115 // CIR: %[[RETVAL:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"]
116 // CIR: %[[ARR:.*]] = cir.alloca !cir.array<!s32i x 10>, !cir.ptr<!cir.array<!s32i x 10>>, ["arr"]
117 // CIR: %[[I:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i"]
118 // CIR: %[[J:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["j"]
119 // CIR: %[[P:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["p"]
120 // CIR: %[[Q:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["q", init]
121 // CIR: %[[VAL_123:.*]] = cir.const #cir.int<123> : !s32i
122 // CIR: cir.store{{.*}} %[[VAL_123]], %[[I]] : !s32i, !cir.ptr<!s32i>
123 // CIR: cir.store{{.*}} %[[VAL_123]], %[[J]] : !s32i, !cir.ptr<!s32i>
124 // CIR: %[[VAL_0:.*]] = cir.const #cir.int<0> : !s32i
125 // CIR: %[[VAL_5:.*]] = cir.const #cir.int<5> : !s32i
126 // CIR: cir.store{{.*}} %[[VAL_5]], %[[I]] : !s32i, !cir.ptr<!s32i>
127 // CIR: %[[ARR_ELEM:.*]] = cir.get_element %[[ARR]][%[[VAL_5]] : !s32i] : !cir.ptr<!cir.array<!s32i x 10>> -> !cir.ptr<!s32i>
128 // CIR: %[[ARR_LOAD:.*]] = cir.load{{.*}} %[[ARR_ELEM]] : !cir.ptr<!s32i>, !s32i
129 // CIR: cir.store{{.*}} %[[ARR_LOAD]], %[[J]] : !s32i, !cir.ptr<!s32i>
130 // CIR: %[[NULL:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i>
131 // CIR: cir.store{{.*}} %[[NULL]], %[[Q]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
132 // CIR: cir.scope {
133 // CIR: %[[Q_VAL:.*]] = cir.load{{.*}} %[[Q]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
134 // CIR: cir.store{{.*}} %[[Q_VAL]], %[[P]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
135 // CIR: %[[COND:.*]] = cir.cast ptr_to_bool %[[Q_VAL]] : !cir.ptr<!s32i> -> !cir.bool
136 // CIR: cir.if %[[COND]] {
137 // CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
138 // CIR: cir.store %[[ONE]], %[[RETVAL]] : !s32i, !cir.ptr<!s32i>
139 // CIR: %{{.*}} = cir.load %[[RETVAL]] : !cir.ptr<!s32i>, !s32i
140 // CIR: cir.return
141 // CIR: }
142 // CIR: }
143 // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
144 // CIR: cir.store %[[ZERO]], %[[RETVAL]] : !s32i, !cir.ptr<!s32i>
145 // CIR: %{{.*}} = cir.load %[[RETVAL]] : !cir.ptr<!s32i>, !s32i
146 // CIR: cir.return
147
148 // LLVM-LABEL: define {{.*}}i32 @ignore_result_assign()
149 // LLVM: %[[RETVAL_PTR:.*]] = alloca i32
150 // LLVM: %[[ARR_PTR:.*]] = alloca [10 x i32]
151 // LLVM: %[[I_PTR:.*]] = alloca i32
152 // LLVM: %[[J_PTR:.*]] = alloca i32
153 // LLVM: %[[P_PTR:.*]] = alloca ptr
154 // LLVM: %[[Q_PTR:.*]] = alloca ptr
155 // LLVM: store i32 123, ptr %[[I_PTR]]
156 // LLVM: store i32 123, ptr %[[J_PTR]]
157 // LLVM: store i32 5, ptr %[[I_PTR]]
158 // LLVM: %[[GEP:.*]] = getelementptr [10 x i32], ptr %[[ARR_PTR]], i32 0, i64 5
159 // LLVM: %[[ARR_VAL:.*]] = load i32, ptr %[[GEP]]
160 // LLVM: store i32 %[[ARR_VAL]], ptr %[[J_PTR]]
161 // LLVM: store ptr null, ptr %[[Q_PTR]]
162 // LLVM: br label
163 // LLVM: %[[Q_VAL:.*]] = load ptr, ptr %[[Q_PTR]]
164 // LLVM: store ptr %[[Q_VAL]], ptr %[[P_PTR]]
165 // LLVM: %[[CMP:.*]] = icmp ne ptr %[[Q_VAL]], null
166 // LLVM: br i1 %[[CMP]], label %[[THEN:.*]], label %[[ELSE:.*]]
167 // LLVM: [[THEN]]:
168 // LLVM: store i32 1, ptr %[[RETVAL_PTR]]
169 // LLVM: %{{.*}} = load i32, ptr %[[RETVAL_PTR]]
170 // LLVM: ret i32
171 // LLVM: [[ELSE]]:
172 // LLVM: br label
173 // LLVM: store i32 0, ptr %[[RETVAL_PTR]]
174 // LLVM: %{{.*}} = load i32, ptr %[[RETVAL_PTR]]
175 // LLVM: ret i32
176
177 // OGCG-LABEL: define {{.*}}i32 @ignore_result_assign()
178 // OGCG: %[[RETVAL:.*]] = alloca i32
179 // OGCG: %[[ARR:.*]] = alloca [10 x i32]
180 // OGCG: %[[I:.*]] = alloca i32
181 // OGCG: %[[J:.*]] = alloca i32
182 // OGCG: %[[P:.*]] = alloca ptr
183 // OGCG: %[[Q:.*]] = alloca ptr
184 // OGCG: store i32 123, ptr %[[I]]
185 // OGCG: store i32 123, ptr %[[J]]
186 // OGCG: store i32 5, ptr %[[I]]
187 // OGCG: %[[ARRAYIDX:.*]] = getelementptr inbounds [10 x i32], ptr %[[ARR]], i64 0, i64 5
188 // OGCG: %[[ARR_VAL:.*]] = load i32, ptr %[[ARRAYIDX]]
189 // OGCG: store i32 %[[ARR_VAL]], ptr %[[J]]
190 // OGCG: store ptr null, ptr %[[Q]]
191 // OGCG: %[[Q_VAL:.*]] = load ptr, ptr %[[Q]]
192 // OGCG: store ptr %[[Q_VAL]], ptr %[[P]]
193 // OGCG: %[[TOBOOL:.*]] = icmp ne ptr %[[Q_VAL]], null
194 // OGCG: br i1 %[[TOBOOL]], label %[[IF_THEN:.*]], label %[[IF_END:.*]]
195 // OGCG: [[IF_THEN]]:
196 // OGCG: store i32 1, ptr %[[RETVAL]]
197 // OGCG: br label %[[RETURN:.*]]
198 // OGCG: [[IF_END]]:
199 // OGCG: store i32 0, ptr %[[RETVAL]]
200 // OGCG: br label %[[RETURN]]
201 // OGCG: [[RETURN]]:
202 // OGCG: %{{.*}} = load i32, ptr %[[RETVAL]]
203 // OGCG: ret i32