1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 define i32 @main(i32 %argc, i8** %argv) {
6 ; CHECK-NEXT: ret i32 0
9 %malloc_206 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i8* getelementptr (i8, i8* null, i32 1) to i32), i32 10))
10 store i8* %malloc_206, i8** %c_19
11 %tmp_207 = load i8*, i8** %c_19
12 tail call void @free(i8* %tmp_207)
16 define i32 @dead_aligned_alloc(i32 %size, i32 %alignment, i8 %value) {
17 ; CHECK-LABEL: @dead_aligned_alloc(
18 ; CHECK-NEXT: ret i32 0
20 %aligned_allocation = tail call i8* @aligned_alloc(i32 %alignment, i32 %size)
21 store i8 %value, i8* %aligned_allocation
22 tail call void @free(i8* %aligned_allocation)
26 declare noalias i8* @calloc(i32, i32) nounwind
27 declare noalias i8* @malloc(i32)
28 declare noalias i8* @aligned_alloc(i32, i32)
29 declare void @free(i8*)
33 ; CHECK-NEXT: ret i1 false
35 %m = call i8* @malloc(i32 1)
36 %z = icmp eq i8* %m, null
37 call void @free(i8* %m)
41 declare void @llvm.lifetime.start.p0i8(i64, i8*)
42 declare void @llvm.lifetime.end.p0i8(i64, i8*)
43 declare i64 @llvm.objectsize.i64(i8*, i1)
44 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
45 declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
46 declare void @llvm.memset.p0i8.i32(i8*, i8, i32, i1) nounwind
48 define void @test3(i8* %src) {
49 ; CHECK-LABEL: @test3(
50 ; CHECK-NEXT: ret void
52 %a = call noalias i8* @malloc(i32 10)
53 call void @llvm.lifetime.start.p0i8(i64 10, i8* %a)
54 call void @llvm.lifetime.end.p0i8(i64 10, i8* %a)
55 %size = call i64 @llvm.objectsize.i64(i8* %a, i1 true)
57 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i1 false)
58 call void @llvm.memmove.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i1 false)
59 call void @llvm.memset.p0i8.i32(i8* %a, i8 5, i32 32, i1 false)
60 %alloc2 = call noalias i8* @calloc(i32 5, i32 7) nounwind
61 %z = icmp ne i8* %alloc2, null
65 ;; This used to crash.
66 define void @test4() {
67 ; CHECK-LABEL: @test4(
68 ; CHECK-NEXT: ret void
70 %A = call i8* @malloc(i32 16000)
71 %B = bitcast i8* %A to double*
72 %C = bitcast double* %B to i8*
73 call void @free(i8* %C)
77 define void @test5(i8* %ptr, i8** %esc) {
78 ; CHECK-LABEL: @test5(
79 ; CHECK-NEXT: [[A:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
80 ; CHECK-NEXT: [[B:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
81 ; CHECK-NEXT: [[C:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
82 ; CHECK-NEXT: [[D:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
83 ; CHECK-NEXT: [[E:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
84 ; CHECK-NEXT: [[F:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
85 ; CHECK-NEXT: [[G:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
86 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(32) [[PTR:%.*]], i8* noundef nonnull align 1 dereferenceable(32) [[A]], i32 32, i1 false)
87 ; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(32) [[PTR]], i8* noundef nonnull align 1 dereferenceable(32) [[B]], i32 32, i1 false)
88 ; CHECK-NEXT: store i8* [[C]], i8** [[ESC:%.*]], align 8
89 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[D]], i8* [[PTR]], i32 32, i1 true)
90 ; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i32(i8* [[E]], i8* [[PTR]], i32 32, i1 true)
91 ; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* [[F]], i8 5, i32 32, i1 true)
92 ; CHECK-NEXT: store volatile i8 4, i8* [[G]], align 1
93 ; CHECK-NEXT: ret void
95 %a = call i8* @malloc(i32 700)
96 %b = call i8* @malloc(i32 700)
97 %c = call i8* @malloc(i32 700)
98 %d = call i8* @malloc(i32 700)
99 %e = call i8* @malloc(i32 700)
100 %f = call i8* @malloc(i32 700)
101 %g = call i8* @malloc(i32 700)
102 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %a, i32 32, i1 false)
103 call void @llvm.memmove.p0i8.p0i8.i32(i8* %ptr, i8* %b, i32 32, i1 false)
104 store i8* %c, i8** %esc
105 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %d, i8* %ptr, i32 32, i1 true)
106 call void @llvm.memmove.p0i8.p0i8.i32(i8* %e, i8* %ptr, i32 32, i1 true)
107 call void @llvm.memset.p0i8.i32(i8* %f, i8 5, i32 32, i1 true)
108 store volatile i8 4, i8* %g
112 ;; When a basic block contains only a call to free and this block is accessed
113 ;; through a test of the argument of free against null, move the call in the
114 ;; predecessor block.
115 ;; Using simplifycfg will remove the empty basic block and the branch operation
116 ;; Then, performing a dead elimination will remove the comparison.
117 ;; This is what happens with -O1 and upper.
118 define void @test6(i8* %foo) minsize {
119 ; CHECK-LABEL: @test6(
121 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i8* [[FOO:%.*]], null
122 ; CHECK-NEXT: tail call void @free(i8* [[FOO]])
123 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
125 ; CHECK-NEXT: br label [[IF_END]]
127 ; CHECK-NEXT: ret void
129 ;; Call to free moved
130 ;; Block is now empty and may be simplified by simplifycfg
132 %tobool = icmp eq i8* %foo, null
133 br i1 %tobool, label %if.end, label %if.then
135 if.then: ; preds = %entry
136 tail call void @free(i8* %foo)
139 if.end: ; preds = %entry, %if.then
143 ; Same optimization with even a builtin 'operator delete' would be
144 ; incorrect in general.
145 ; 'if (p) delete p;' cannot result in a call to 'operator delete(0)'.
146 define void @test6a(i8* %foo) minsize {
147 ; CHECK-LABEL: @test6a(
149 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i8* [[FOO:%.*]], null
150 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
152 ; CHECK-NEXT: tail call void @_ZdlPv(i8* [[FOO]]) #[[ATTR10:[0-9]+]]
153 ; CHECK-NEXT: br label [[IF_END]]
155 ; CHECK-NEXT: ret void
158 %tobool = icmp eq i8* %foo, null
159 br i1 %tobool, label %if.end, label %if.then
161 if.then: ; preds = %entry
162 tail call void @_ZdlPv(i8* %foo) builtin
165 if.end: ; preds = %entry, %if.then
169 declare i8* @_ZnwmRKSt9nothrow_t(i64, i8*) nobuiltin
170 declare void @_ZdlPvRKSt9nothrow_t(i8*, i8*) nobuiltin
171 declare i32 @__gxx_personality_v0(...)
172 declare void @_ZN1AC2Ev(i8* %this)
174 define void @test7() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
175 ; CHECK-LABEL: @test7(
177 ; CHECK-NEXT: invoke void @_ZN1AC2Ev(i8* undef)
178 ; CHECK-NEXT: to label [[DOTNOEXC_I:%.*]] unwind label [[LPAD_I:%.*]]
180 ; CHECK-NEXT: unreachable
182 ; CHECK-NEXT: [[TMP0:%.*]] = landingpad { i8*, i32 }
183 ; CHECK-NEXT: cleanup
184 ; CHECK-NEXT: resume { i8*, i32 } [[TMP0]]
188 %call.i = tail call i8* @_ZnwmRKSt9nothrow_t(i64 1, i8* %nt) builtin nounwind
189 invoke void @_ZN1AC2Ev(i8* undef)
190 to label %.noexc.i unwind label %lpad.i
192 .noexc.i: ; preds = %entry
195 lpad.i: ; preds = %entry
196 %0 = landingpad { i8*, i32 } cleanup
197 call void @_ZdlPvRKSt9nothrow_t(i8* %call.i, i8* %nt) builtin nounwind
198 resume { i8*, i32 } %0
201 declare i8* @_Znwm(i64) nobuiltin
202 define i8* @_Znwj(i32 %n) nobuiltin {
203 ; CHECK-LABEL: @_Znwj(
204 ; CHECK-NEXT: [[Z:%.*]] = zext i32 [[N:%.*]] to i64
205 ; CHECK-NEXT: [[M:%.*]] = call i8* @_Znwm(i64 [[Z]])
206 ; CHECK-NEXT: ret i8* [[M]]
208 %z = zext i32 %n to i64
209 %m = call i8* @_Znwm(i64 %z)
212 declare i8* @_Znam(i64) nobuiltin
213 declare i8* @_Znaj(i32) nobuiltin
214 declare void @_ZdlPv(i8*) nobuiltin
215 declare void @_ZdaPv(i8*) nobuiltin
217 define linkonce void @_ZdlPvm(i8* %p, i64) nobuiltin {
218 ; CHECK-LABEL: @_ZdlPvm(
219 ; CHECK-NEXT: call void @_ZdlPv(i8* [[P:%.*]])
220 ; CHECK-NEXT: ret void
222 call void @_ZdlPv(i8* %p)
225 define linkonce void @_ZdlPvj(i8* %p, i32) nobuiltin {
226 ; CHECK-LABEL: @_ZdlPvj(
227 ; CHECK-NEXT: call void @_ZdlPv(i8* [[P:%.*]])
228 ; CHECK-NEXT: ret void
230 call void @_ZdlPv(i8* %p)
233 define linkonce void @_ZdaPvm(i8* %p, i64) nobuiltin {
234 ; CHECK-LABEL: @_ZdaPvm(
235 ; CHECK-NEXT: call void @_ZdaPv(i8* [[P:%.*]])
236 ; CHECK-NEXT: ret void
238 call void @_ZdaPv(i8* %p)
241 define linkonce void @_ZdaPvj(i8* %p, i32) nobuiltin {
242 ; CHECK-LABEL: @_ZdaPvj(
243 ; CHECK-NEXT: call void @_ZdaPv(i8* [[P:%.*]])
244 ; CHECK-NEXT: ret void
246 call void @_ZdaPv(i8* %p)
251 ; new(size_t, align_val_t)
252 declare i8* @_ZnwmSt11align_val_t(i64, i64) nobuiltin
253 declare i8* @_ZnwjSt11align_val_t(i32, i32) nobuiltin
254 ; new[](size_t, align_val_t)
255 declare i8* @_ZnamSt11align_val_t(i64, i64) nobuiltin
256 declare i8* @_ZnajSt11align_val_t(i32, i32) nobuiltin
257 ; new(size_t, align_val_t, nothrow)
258 declare i8* @_ZnwmSt11align_val_tRKSt9nothrow_t(i64, i64, i8*) nobuiltin
259 declare i8* @_ZnwjSt11align_val_tRKSt9nothrow_t(i32, i32, i8*) nobuiltin
260 ; new[](size_t, align_val_t, nothrow)
261 declare i8* @_ZnamSt11align_val_tRKSt9nothrow_t(i64, i64, i8*) nobuiltin
262 declare i8* @_ZnajSt11align_val_tRKSt9nothrow_t(i32, i32, i8*) nobuiltin
263 ; delete(void*, align_val_t)
264 declare void @_ZdlPvSt11align_val_t(i8*, i64) nobuiltin
265 ; delete[](void*, align_val_t)
266 declare void @_ZdaPvSt11align_val_t(i8*, i64) nobuiltin
267 ; delete(void*, align_val_t, nothrow)
268 declare void @_ZdlPvSt11align_val_tRKSt9nothrow_t(i8*, i64, i8*) nobuiltin
269 ; delete[](void*, align_val_t, nothrow)
270 declare void @_ZdaPvSt11align_val_tRKSt9nothrow_t(i8*, i64, i8*) nobuiltin
271 ; delete(void*, unsigned int, align_val_t)
272 declare void @_ZdlPvjSt11align_val_t(i8*, i32, i32) nobuiltin
273 ; delete(void*, unsigned long, align_val_t)
274 declare void @_ZdlPvmSt11align_val_t(i8*, i64, i64) nobuiltin
275 ; delete[](void*, unsigned int, align_val_t)
276 declare void @_ZdaPvjSt11align_val_t(i8*, i32, i32) nobuiltin
277 ; delete[](void*, unsigned long, align_val_t)
278 declare void @_ZdaPvmSt11align_val_t(i8*, i64, i64) nobuiltin
280 declare void @llvm.assume(i1)
282 define void @test8() {
283 ; CHECK-LABEL: @test8(
284 ; CHECK-NEXT: ret void
287 %nw = call i8* @_Znwm(i64 32) builtin
288 call void @_ZdlPv(i8* %nw) builtin
289 %na = call i8* @_Znam(i64 32) builtin
290 call void @_ZdaPv(i8* %na) builtin
291 %nwm = call i8* @_Znwm(i64 32) builtin
292 call void @_ZdlPvm(i8* %nwm, i64 32) builtin
293 %nwj = call i8* @_Znwj(i32 32) builtin
294 call void @_ZdlPvj(i8* %nwj, i32 32) builtin
295 %nam = call i8* @_Znam(i64 32) builtin
296 call void @_ZdaPvm(i8* %nam, i64 32) builtin
297 %naj = call i8* @_Znaj(i32 32) builtin
298 call void @_ZdaPvj(i8* %naj, i32 32) builtin
299 %nwa = call i8* @_ZnwmSt11align_val_t(i64 32, i64 8) builtin
300 call void @_ZdlPvSt11align_val_t(i8* %nwa, i64 8) builtin
301 %naa = call i8* @_ZnamSt11align_val_t(i64 32, i64 8) builtin
302 call void @_ZdaPvSt11align_val_t(i8* %naa, i64 8) builtin
303 %nwja = call i8* @_ZnwjSt11align_val_t(i32 32, i32 8) builtin
304 call void @_ZdlPvSt11align_val_t(i8* %nwja, i64 8) builtin
305 %naja = call i8* @_ZnajSt11align_val_t(i32 32, i32 8) builtin
306 call void @_ZdaPvSt11align_val_t(i8* %naja, i64 8) builtin
307 %nwat = call i8* @_ZnwmSt11align_val_tRKSt9nothrow_t(i64 32, i64 8, i8* %nt) builtin
308 call void @_ZdlPvSt11align_val_tRKSt9nothrow_t(i8* %nwat, i64 8, i8* %nt) builtin
309 %naat = call i8* @_ZnamSt11align_val_tRKSt9nothrow_t(i64 32, i64 8, i8* %nt) builtin
310 call void @_ZdaPvSt11align_val_tRKSt9nothrow_t(i8* %naat, i64 8, i8* %nt) builtin
311 %nwjat = call i8* @_ZnwjSt11align_val_tRKSt9nothrow_t(i32 32, i32 8, i8* %nt) builtin
312 call void @_ZdlPvSt11align_val_tRKSt9nothrow_t(i8* %nwjat, i64 8, i8* %nt) builtin
313 %najat = call i8* @_ZnajSt11align_val_tRKSt9nothrow_t(i32 32, i32 8, i8* %nt) builtin
314 call void @_ZdaPvSt11align_val_tRKSt9nothrow_t(i8* %najat, i64 8, i8* %nt) builtin
315 %nwa2 = call i8* @_ZnwmSt11align_val_t(i64 32, i64 8) builtin
316 call void @_ZdlPvmSt11align_val_t(i8* %nwa2, i64 32, i64 8) builtin
317 %nwja2 = call i8* @_ZnwjSt11align_val_t(i32 32, i32 8) builtin
318 call void @_ZdlPvjSt11align_val_t(i8* %nwa2, i32 32, i32 8) builtin
319 %naa2 = call i8* @_ZnamSt11align_val_t(i64 32, i64 8) builtin
320 call void @_ZdaPvmSt11align_val_t(i8* %naa2, i64 32, i64 8) builtin
321 %naja2 = call i8* @_ZnajSt11align_val_t(i32 32, i32 8) builtin
322 call void @_ZdaPvjSt11align_val_t(i8* %naja2, i32 32, i32 8) builtin
324 ; Check that the alignment assume does not prevent the removal.
325 %nwa3 = call i8* @_ZnwmSt11align_val_t(i64 32, i64 16) builtin
326 call void @llvm.assume(i1 true) [ "align"(i8* %nwa3, i64 16) ]
327 call void @_ZdlPvmSt11align_val_t(i8* %nwa3, i64 32, i64 16) builtin
332 declare noalias i8* @"\01??2@YAPEAX_K@Z"(i64) nobuiltin
333 declare void @"\01??3@YAXPEAX@Z"(i8*) nobuiltin
335 define void @test9() {
336 ; CHECK-LABEL: @test9(
337 ; CHECK-NEXT: ret void
339 %new_long_long = call noalias i8* @"\01??2@YAPEAX_K@Z"(i64 32) builtin
340 call void @"\01??3@YAXPEAX@Z"(i8* %new_long_long) builtin
344 define void @test10() {
345 ; CHECK-LABEL: @test10(
346 ; CHECK-NEXT: call void @_ZdlPv(i8* null)
347 ; CHECK-NEXT: ret void
349 call void @_ZdlPv(i8* null)
353 define void @test11() {
354 ; CHECK-LABEL: @test11(
355 ; CHECK-NEXT: [[CALL:%.*]] = call dereferenceable(8) i8* @_Znwm(i64 8) #[[ATTR10]]
356 ; CHECK-NEXT: call void @_ZdlPv(i8* nonnull [[CALL]])
357 ; CHECK-NEXT: ret void
359 %call = call i8* @_Znwm(i64 8) builtin
360 call void @_ZdlPv(i8* %call)
364 ;; Check that the optimization that moves a call to free in its predecessor
365 ;; block (see test6) also happens when noop casts are involved.
366 define void @test12(i32* %foo) minsize {
367 ; CHECK-LABEL: @test12(
369 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32* [[FOO:%.*]], null
370 ; CHECK-NEXT: [[BITCAST:%.*]] = bitcast i32* [[FOO]] to i8*
371 ; CHECK-NEXT: tail call void @free(i8* [[BITCAST]])
372 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
374 ; CHECK-NEXT: br label [[IF_END]]
376 ; CHECK-NEXT: ret void
378 ;; Everything before the call to free should have been moved as well.
379 ;; Call to free moved
380 ;; Block is now empty and may be simplified by simplifycfg
382 %tobool = icmp eq i32* %foo, null
383 br i1 %tobool, label %if.end, label %if.then
385 if.then: ; preds = %entry
386 %bitcast = bitcast i32* %foo to i8*
387 tail call void @free(i8* %bitcast)
390 if.end: ; preds = %entry, %if.then
394 ; The next four tests cover the semantics of the nofree attributes. These
395 ; are thought to be legal transforms, but an implementation thereof has
396 ; been reverted once due to difficult to isolate fallout.
398 ; TODO: Freeing a no-free pointer -> %foo must be null
399 define void @test13(i8* nofree %foo) {
400 ; CHECK-LABEL: @test13(
401 ; CHECK-NEXT: call void @free(i8* [[FOO:%.*]])
402 ; CHECK-NEXT: ret void
404 call void @free(i8* %foo)
408 ; TODO: Freeing a no-free pointer -> %foo must be null
409 define void @test14(i8* %foo) nofree {
410 ; CHECK-LABEL: @test14(
411 ; CHECK-NEXT: call void @free(i8* [[FOO:%.*]])
412 ; CHECK-NEXT: ret void
414 call void @free(i8* %foo)
418 ; TODO: free call marked no-free -> %foo must be null
419 define void @test15(i8* %foo) {
420 ; CHECK-LABEL: @test15(
421 ; CHECK-NEXT: call void @free(i8* [[FOO:%.*]]) #[[ATTR7:[0-9]+]]
422 ; CHECK-NEXT: ret void
424 call void @free(i8* %foo) nofree
428 ; TODO: freeing a nonnull nofree pointer -> full UB
429 define void @test16(i8* nonnull nofree %foo) {
430 ; CHECK-LABEL: @test16(
431 ; CHECK-NEXT: call void @free(i8* [[FOO:%.*]])
432 ; CHECK-NEXT: ret void
434 call void @free(i8* %foo)
438 declare i8* @llvm.launder.invariant.group(i8*)
439 declare i8* @llvm.strip.invariant.group(i8*)
441 define void @test17() {
442 ; CHECK-LABEL: @test17(
443 ; CHECK-NEXT: ret void
445 %nw1 = call i8* @_Znwm(i64 32) builtin
446 %nw2 = call i8* @llvm.launder.invariant.group(i8* %nw1)
447 %nw3 = call i8* @llvm.strip.invariant.group(i8* %nw2)
449 call void @_ZdlPv(i8* %nw2) builtin