1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
3 ; RUN: opt < %s -basic-aa -dse -S | FileCheck %s
5 target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
6 declare void @unknown_func()
7 declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) nounwind
8 declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture) nounwind
9 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1) nounwind
10 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
12 declare noalias i8* @calloc(i64, i64) #5
13 declare noalias i8* @malloc(i64) #0
14 declare noalias i8* @strdup(i8* nocapture readonly) #1
15 declare void @free(i8* nocapture) #2
17 define void @test16(i32* noalias %P) {
18 ; CHECK-LABEL: @test16(
19 ; CHECK-NEXT: [[P2:%.*]] = bitcast i32* [[P:%.*]] to i8*
20 ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB3:%.*]]
22 ; CHECK-NEXT: br label [[BB3]]
24 ; CHECK-NEXT: call void @free(i8* [[P2]])
25 ; CHECK-NEXT: store i32 1, i32* [[P]], align 4
26 ; CHECK-NEXT: ret void
28 %P2 = bitcast i32* %P to i8*
30 br i1 true, label %bb1, label %bb3
35 call void @free(i8* %P2)
40 ; We cannot remove the store in the entry block, because @unknown_func could
41 ; unwind and the stored value could be read by the caller.
42 define void @test17(i32* noalias %P) {
43 ; CHECK-LABEL: @test17(
44 ; CHECK-NEXT: [[P2:%.*]] = bitcast i32* [[P:%.*]] to i8*
45 ; CHECK-NEXT: store i32 1, i32* [[P]], align 4
46 ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB3:%.*]]
48 ; CHECK-NEXT: call void @unknown_func()
49 ; CHECK-NEXT: br label [[BB3]]
51 ; CHECK-NEXT: call void @free(i8* [[P2]])
52 ; CHECK-NEXT: ret void
54 %P2 = bitcast i32* %P to i8*
56 br i1 true, label %bb1, label %bb3
58 call void @unknown_func()
62 call void @free(i8* %P2)
66 define void @test17_read_after_free(i32* noalias %P) {
67 ; CHECK-LABEL: @test17_read_after_free(
68 ; CHECK-NEXT: [[P2:%.*]] = bitcast i32* [[P:%.*]] to i8*
69 ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB3:%.*]]
71 ; CHECK-NEXT: br label [[BB3]]
73 ; CHECK-NEXT: call void @free(i8* [[P2]])
74 ; CHECK-NEXT: [[LV:%.*]] = load i8, i8* [[P2]], align 1
75 ; CHECK-NEXT: ret void
77 %P2 = bitcast i32* %P to i8*
79 br i1 true, label %bb1, label %bb3
84 call void @free(i8* %P2)
85 %lv = load i8, i8* %P2
89 define void @test19(i32* noalias %P) {
90 ; CHECK-LABEL: @test19(
92 ; CHECK-NEXT: [[ARRAYIDX0:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 1
93 ; CHECK-NEXT: [[P3:%.*]] = bitcast i32* [[ARRAYIDX0]] to i8*
94 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 4 [[P3]], i8 0, i64 28, i1 false)
95 ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB2:%.*]]
97 ; CHECK-NEXT: br label [[BB3:%.*]]
99 ; CHECK-NEXT: [[ARRAYIDX1:%.*]] = getelementptr inbounds i32, i32* [[P]], i64 1
100 ; CHECK-NEXT: store i32 1, i32* [[ARRAYIDX1]], align 4
101 ; CHECK-NEXT: br label [[BB3]]
103 ; CHECK-NEXT: ret void
106 %arrayidx0 = getelementptr inbounds i32, i32* %P, i64 1
107 %p3 = bitcast i32* %arrayidx0 to i8*
108 call void @llvm.memset.p0i8.i64(i8* %p3, i8 0, i64 28, i32 4, i1 false)
109 br i1 true, label %bb1, label %bb2
113 %arrayidx1 = getelementptr inbounds i32, i32* %P, i64 1
114 store i32 1, i32* %arrayidx1, align 4
121 define void @test20(i32* noalias %P) {
122 ; CHECK-LABEL: @test20(
124 ; CHECK-NEXT: [[ARRAYIDX0:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 1
125 ; CHECK-NEXT: [[P3:%.*]] = bitcast i32* [[ARRAYIDX0]] to i8*
126 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i8, i8* [[P3]], i64 4
127 ; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 4 [[TMP0]], i8 0, i64 24, i1 false)
128 ; CHECK-NEXT: br i1 true, label [[BB1:%.*]], label [[BB2:%.*]]
130 ; CHECK-NEXT: br label [[BB3:%.*]]
132 ; CHECK-NEXT: br label [[BB3]]
134 ; CHECK-NEXT: [[ARRAYIDX1:%.*]] = getelementptr inbounds i32, i32* [[P]], i64 1
135 ; CHECK-NEXT: store i32 1, i32* [[ARRAYIDX1]], align 4
136 ; CHECK-NEXT: ret void
139 %arrayidx0 = getelementptr inbounds i32, i32* %P, i64 1
140 %p3 = bitcast i32* %arrayidx0 to i8*
141 call void @llvm.memset.p0i8.i64(i8* %p3, i8 0, i64 28, i32 4, i1 false)
142 br i1 true, label %bb1, label %bb2
148 %arrayidx1 = getelementptr inbounds i32, i32* %P, i64 1
149 store i32 1, i32* %arrayidx1, align 4
153 define i8* @test26() {
154 ; CHECK-LABEL: @test26(
156 ; CHECK-NEXT: br i1 true, label [[BB2:%.*]], label [[BB3:%.*]]
158 ; CHECK-NEXT: [[M:%.*]] = call noalias i8* @malloc(i64 10)
159 ; CHECK-NEXT: store i8 1, i8* [[M]], align 1
160 ; CHECK-NEXT: br label [[BB3]]
162 ; CHECK-NEXT: [[R:%.*]] = phi i8* [ null, [[BB1:%.*]] ], [ [[M]], [[BB2]] ]
163 ; CHECK-NEXT: ret i8* [[R]]
166 br i1 true, label %bb2, label %bb3
168 %m = call noalias i8* @malloc(i64 10)
172 %r = phi i8* [ null, %bb1 ], [ %m, %bb2 ]
177 define void @test27() {
178 ; CHECK-LABEL: @test27(
180 ; CHECK-NEXT: br i1 true, label [[BB2:%.*]], label [[BB3:%.*]]
182 ; CHECK-NEXT: [[M:%.*]] = call noalias i8* @malloc(i64 10)
183 ; CHECK-NEXT: store i8 1, i8* [[M]], align 1
184 ; CHECK-NEXT: br label [[BB3]]
186 ; CHECK-NEXT: [[R:%.*]] = phi i8* [ null, [[BB1:%.*]] ], [ [[M]], [[BB2]] ]
187 ; CHECK-NEXT: ret void
190 br i1 true, label %bb2, label %bb3
192 %m = call noalias i8* @malloc(i64 10)
196 %r = phi i8* [ null, %bb1 ], [ %m, %bb2 ]
201 define i8* @test28() {
202 ; CHECK-LABEL: @test28(
204 ; CHECK-NEXT: [[M:%.*]] = call noalias i8* @malloc(i64 10)
205 ; CHECK-NEXT: [[MC0:%.*]] = bitcast i8* [[M]] to i8*
206 ; CHECK-NEXT: [[MC1:%.*]] = bitcast i8* [[MC0]] to i8*
207 ; CHECK-NEXT: [[MC2:%.*]] = bitcast i8* [[MC1]] to i8*
208 ; CHECK-NEXT: [[MC3:%.*]] = bitcast i8* [[MC2]] to i8*
209 ; CHECK-NEXT: [[MC4:%.*]] = bitcast i8* [[MC3]] to i8*
210 ; CHECK-NEXT: [[MC5:%.*]] = bitcast i8* [[MC4]] to i8*
211 ; CHECK-NEXT: [[MC6:%.*]] = bitcast i8* [[MC5]] to i8*
212 ; CHECK-NEXT: [[M0:%.*]] = bitcast i8* [[MC6]] to i8*
213 ; CHECK-NEXT: store i8 2, i8* [[M]], align 1
214 ; CHECK-NEXT: ret i8* [[M0]]
217 %m = call noalias i8* @malloc(i64 10)
218 %mc0 = bitcast i8* %m to i8*
219 %mc1 = bitcast i8* %mc0 to i8*
220 %mc2 = bitcast i8* %mc1 to i8*
221 %mc3 = bitcast i8* %mc2 to i8*
222 %mc4 = bitcast i8* %mc3 to i8*
223 %mc5 = bitcast i8* %mc4 to i8*
224 %mc6 = bitcast i8* %mc5 to i8*
225 %m0 = bitcast i8* %mc6 to i8*
230 %struct.SystemCallMapElementStruct = type { i8*, i32, %struct.NodePtrVecStruct* }
231 %struct.NodePtrVecStruct = type { i32, i32, %struct.NodeStruct** }
232 %struct.NodeStruct = type { i32, i32, i8*, i32, i32, %struct.NodeStruct*, %struct.NodeListStruct*, %struct.EdgeListStruct*, i32, i32 }
233 %struct.NodeListStruct = type { %struct.NodeStruct*, %struct.NodeListStruct* }
234 %struct.EdgeListStruct = type { i32, %struct.NodeStruct*, %struct.EdgeListStruct* }
235 %struct.SystemCallMapStruct = type { i32, i32, %struct.SystemCallMapElementStruct** }
237 declare %struct.NodePtrVecStruct* @NodePtrVec_new(i32)
239 define noalias %struct.SystemCallMapElementStruct* @SystemCallMapElement_new(i8* nocapture readonly %label, i32 %initialSize) {
240 ; CHECK-LABEL: @SystemCallMapElement_new(
242 ; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(24) i8* @malloc(i64 24) #[[ATTR7:[0-9]+]]
243 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i8* [[CALL]] to %struct.SystemCallMapElementStruct*
244 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i8* [[CALL]], null
245 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[CLEANUP:%.*]], label [[IF_THEN:%.*]]
247 ; CHECK-NEXT: [[CALL1:%.*]] = tail call i8* @strdup(i8* [[LABEL:%.*]])
248 ; CHECK-NEXT: [[LABEL2:%.*]] = bitcast i8* [[CALL]] to i8**
249 ; CHECK-NEXT: store i8* [[CALL1]], i8** [[LABEL2]], align 8
250 ; CHECK-NEXT: [[INDEX:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 8
251 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[INDEX]] to i32*
252 ; CHECK-NEXT: store i32 -1, i32* [[TMP1]], align 8
253 ; CHECK-NEXT: [[TOBOOL4:%.*]] = icmp eq i8* [[CALL1]], null
254 ; CHECK-NEXT: br i1 [[TOBOOL4]], label [[IF_THEN5:%.*]], label [[IF_END:%.*]]
256 ; CHECK-NEXT: tail call void @free(i8* nonnull [[CALL]])
257 ; CHECK-NEXT: br label [[CLEANUP]]
259 ; CHECK-NEXT: [[CALL6:%.*]] = tail call %struct.NodePtrVecStruct* @NodePtrVec_new(i32 [[INITIALSIZE:%.*]]) #[[ATTR5:[0-9]+]]
260 ; CHECK-NEXT: [[NODES:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 16
261 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast i8* [[NODES]] to %struct.NodePtrVecStruct**
262 ; CHECK-NEXT: store %struct.NodePtrVecStruct* [[CALL6]], %struct.NodePtrVecStruct** [[TMP2]], align 8
263 ; CHECK-NEXT: [[TOBOOL8:%.*]] = icmp eq %struct.NodePtrVecStruct* [[CALL6]], null
264 ; CHECK-NEXT: br i1 [[TOBOOL8]], label [[IF_THEN9:%.*]], label [[CLEANUP]]
266 ; CHECK-NEXT: tail call void @free(i8* nonnull [[CALL]])
267 ; CHECK-NEXT: br label [[CLEANUP]]
269 ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi %struct.SystemCallMapElementStruct* [ null, [[IF_THEN9]] ], [ null, [[IF_THEN5]] ], [ [[TMP0]], [[IF_END]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
270 ; CHECK-NEXT: ret %struct.SystemCallMapElementStruct* [[RETVAL_0]]
273 %call = tail call dereferenceable_or_null(24) i8* @malloc(i64 24) #4
274 %0 = bitcast i8* %call to %struct.SystemCallMapElementStruct*
275 %tobool = icmp eq i8* %call, null
276 br i1 %tobool, label %cleanup, label %if.then
278 if.then: ; preds = %entry
279 %call1 = tail call i8* @strdup(i8* %label)
280 %label2 = bitcast i8* %call to i8**
281 store i8* %call1, i8** %label2, align 8
282 %index = getelementptr inbounds i8, i8* %call, i64 8
283 %1 = bitcast i8* %index to i32*
284 store i32 -1, i32* %1, align 8
285 %tobool4 = icmp eq i8* %call1, null
286 br i1 %tobool4, label %if.then5, label %if.end
288 if.then5: ; preds = %if.then
289 tail call void @free(i8* nonnull %call)
292 if.end: ; preds = %if.then
293 %call6 = tail call %struct.NodePtrVecStruct* @NodePtrVec_new(i32 %initialSize) #2
294 %nodes = getelementptr inbounds i8, i8* %call, i64 16
295 %2 = bitcast i8* %nodes to %struct.NodePtrVecStruct**
296 store %struct.NodePtrVecStruct* %call6, %struct.NodePtrVecStruct** %2, align 8
297 %tobool8 = icmp eq %struct.NodePtrVecStruct* %call6, null
298 br i1 %tobool8, label %if.then9, label %cleanup
300 if.then9: ; preds = %if.end
301 tail call void @free(i8* nonnull %call)
304 cleanup: ; preds = %entry, %if.end, %if.then9, %if.then5
305 %retval.0 = phi %struct.SystemCallMapElementStruct* [ null, %if.then9 ], [ null, %if.then5 ], [ %0, %if.end ], [ %0, %entry ]
306 ret %struct.SystemCallMapElementStruct* %retval.0
309 %struct.BitfieldStruct = type { i32, i8* }
311 define noalias %struct.BitfieldStruct* @Bitfield_new(i32 %bitsNeeded) {
312 ; CHECK-LABEL: @Bitfield_new(
314 ; CHECK-NEXT: [[CALL:%.*]] = tail call dereferenceable_or_null(16) i8* @malloc(i64 16) #[[ATTR7]]
315 ; CHECK-NEXT: [[TMP0:%.*]] = bitcast i8* [[CALL]] to %struct.BitfieldStruct*
316 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i8* [[CALL]], null
317 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[CLEANUP:%.*]], label [[IF_END:%.*]]
319 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[BITSNEEDED:%.*]], 7
320 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[ADD]], 8
321 ; CHECK-NEXT: [[CONV:%.*]] = sext i32 [[DIV]] to i64
322 ; CHECK-NEXT: [[CALL1:%.*]] = tail call i8* @calloc(i64 [[CONV]], i64 1) #[[ATTR8:[0-9]+]]
323 ; CHECK-NEXT: [[BITFIELD:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 8
324 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[BITFIELD]] to i8**
325 ; CHECK-NEXT: store i8* [[CALL1]], i8** [[TMP1]], align 8
326 ; CHECK-NEXT: [[TOBOOL3:%.*]] = icmp eq i8* [[CALL1]], null
327 ; CHECK-NEXT: br i1 [[TOBOOL3]], label [[IF_THEN4:%.*]], label [[IF_END5:%.*]]
329 ; CHECK-NEXT: tail call void @free(i8* nonnull [[CALL]])
330 ; CHECK-NEXT: br label [[CLEANUP]]
332 ; CHECK-NEXT: [[BITSNEEDED6:%.*]] = bitcast i8* [[CALL]] to i32*
333 ; CHECK-NEXT: store i32 [[BITSNEEDED]], i32* [[BITSNEEDED6]], align 8
334 ; CHECK-NEXT: br label [[CLEANUP]]
336 ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi %struct.BitfieldStruct* [ [[TMP0]], [[IF_END5]] ], [ null, [[IF_THEN4]] ], [ null, [[ENTRY:%.*]] ]
337 ; CHECK-NEXT: ret %struct.BitfieldStruct* [[RETVAL_0]]
340 %call = tail call dereferenceable_or_null(16) i8* @malloc(i64 16) #4
341 %0 = bitcast i8* %call to %struct.BitfieldStruct*
342 %tobool = icmp eq i8* %call, null
343 br i1 %tobool, label %cleanup, label %if.end
345 if.end: ; preds = %entry
346 %add = add nsw i32 %bitsNeeded, 7
347 %div = sdiv i32 %add, 8
348 %conv = sext i32 %div to i64
349 %call1 = tail call i8* @calloc(i64 %conv, i64 1) #3
350 %bitfield = getelementptr inbounds i8, i8* %call, i64 8
351 %1 = bitcast i8* %bitfield to i8**
352 store i8* %call1, i8** %1, align 8
353 %tobool3 = icmp eq i8* %call1, null
354 br i1 %tobool3, label %if.then4, label %if.end5
356 if.then4: ; preds = %if.end
357 tail call void @free(i8* nonnull %call)
360 if.end5: ; preds = %if.end
361 %bitsNeeded6 = bitcast i8* %call to i32*
362 store i32 %bitsNeeded, i32* %bitsNeeded6, align 8
365 cleanup: ; preds = %entry, %if.end5, %if.then4
366 %retval.0 = phi %struct.BitfieldStruct* [ %0, %if.end5 ], [ null, %if.then4 ], [ null, %entry ]
367 ret %struct.BitfieldStruct* %retval.0
370 attributes #0 = { nofree nounwind allocsize(0) }
371 attributes #1 = { nofree nounwind }
372 attributes #2 = { nounwind }
373 attributes #3 = { allocsize(0,1) }
374 attributes #4 = { allocsize(0) }
375 attributes #5 = { nofree nounwind allocsize(0,1) }