1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 define i32 @main(i32 %argc, i8** %argv) {
6 %malloc_206 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i8* getelementptr (i8, i8* null, i32 1) to i32), i32 10))
7 store i8* %malloc_206, i8** %c_19
8 %tmp_207 = load i8*, i8** %c_19
9 tail call void @free(i8* %tmp_207)
11 ; CHECK-NEXT: ret i32 0
14 declare noalias i8* @calloc(i32, i32) nounwind
15 declare noalias i8* @malloc(i32)
16 declare void @free(i8*)
20 ; CHECK-NEXT: ret i1 false
21 %m = call i8* @malloc(i32 1)
22 %z = icmp eq i8* %m, null
23 call void @free(i8* %m)
27 declare void @llvm.lifetime.start.p0i8(i64, i8*)
28 declare void @llvm.lifetime.end.p0i8(i64, i8*)
29 declare i64 @llvm.objectsize.i64(i8*, i1)
30 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
31 declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
32 declare void @llvm.memset.p0i8.i32(i8*, i8, i32, i1) nounwind
34 define void @test3(i8* %src) {
35 ; CHECK-LABEL: @test3(
36 ; CHECK-NEXT: ret void
37 %a = call noalias i8* @malloc(i32 10)
38 call void @llvm.lifetime.start.p0i8(i64 10, i8* %a)
39 call void @llvm.lifetime.end.p0i8(i64 10, i8* %a)
40 %size = call i64 @llvm.objectsize.i64(i8* %a, i1 true)
42 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i1 false)
43 call void @llvm.memmove.p0i8.p0i8.i32(i8* %a, i8* %src, i32 32, i1 false)
44 call void @llvm.memset.p0i8.i32(i8* %a, i8 5, i32 32, i1 false)
45 %alloc2 = call noalias i8* @calloc(i32 5, i32 7) nounwind
46 %z = icmp ne i8* %alloc2, null
50 ;; This used to crash.
51 define void @test4() {
52 ; CHECK-LABEL: @test4(
53 ; CHECK-NEXT: ret void
54 %A = call i8* @malloc(i32 16000)
55 %B = bitcast i8* %A to double*
56 %C = bitcast double* %B to i8*
57 call void @free(i8* %C)
61 ; CHECK-LABEL: @test5(
62 define void @test5(i8* %ptr, i8** %esc) {
63 ; CHECK-NEXT: call i8* @malloc
64 ; CHECK-NEXT: call i8* @malloc
65 ; CHECK-NEXT: call i8* @malloc
66 ; CHECK-NEXT: call i8* @malloc
67 ; CHECK-NEXT: call i8* @malloc
68 ; CHECK-NEXT: call i8* @malloc
69 ; CHECK-NEXT: call i8* @malloc
70 ; CHECK-NEXT: call void @llvm.memcpy
71 ; CHECK-NEXT: call void @llvm.memmove
73 ; CHECK-NEXT: call void @llvm.memcpy
74 ; CHECK-NEXT: call void @llvm.memmove
75 ; CHECK-NEXT: call void @llvm.memset
76 ; CHECK-NEXT: store volatile
78 %a = call i8* @malloc(i32 700)
79 %b = call i8* @malloc(i32 700)
80 %c = call i8* @malloc(i32 700)
81 %d = call i8* @malloc(i32 700)
82 %e = call i8* @malloc(i32 700)
83 %f = call i8* @malloc(i32 700)
84 %g = call i8* @malloc(i32 700)
85 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %ptr, i8* %a, i32 32, i1 false)
86 call void @llvm.memmove.p0i8.p0i8.i32(i8* %ptr, i8* %b, i32 32, i1 false)
87 store i8* %c, i8** %esc
88 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %d, i8* %ptr, i32 32, i1 true)
89 call void @llvm.memmove.p0i8.p0i8.i32(i8* %e, i8* %ptr, i32 32, i1 true)
90 call void @llvm.memset.p0i8.i32(i8* %f, i8 5, i32 32, i1 true)
91 store volatile i8 4, i8* %g
95 ;; When a basic block contains only a call to free and this block is accessed
96 ;; through a test of the argument of free against null, move the call in the
98 ;; Using simplifycfg will remove the empty basic block and the branch operation
99 ;; Then, performing a dead elimination will remove the comparison.
100 ;; This is what happens with -O1 and upper.
101 ; CHECK-LABEL: @test6(
102 define void @test6(i8* %foo) minsize {
103 ; CHECK: %tobool = icmp eq i8* %foo, null
104 ;; Call to free moved
105 ; CHECK-NEXT: tail call void @free(i8* %foo)
106 ; CHECK-NEXT: br i1 %tobool, label %if.end, label %if.then
108 ;; Block is now empty and may be simplified by simplifycfg
109 ; CHECK-NEXT: br label %if.end
111 ; CHECK-NEXT: ret void
113 %tobool = icmp eq i8* %foo, null
114 br i1 %tobool, label %if.end, label %if.then
116 if.then: ; preds = %entry
117 tail call void @free(i8* %foo)
120 if.end: ; preds = %entry, %if.then
124 declare i8* @_ZnwmRKSt9nothrow_t(i64, i8*) nobuiltin
125 declare void @_ZdlPvRKSt9nothrow_t(i8*, i8*) nobuiltin
126 declare i32 @__gxx_personality_v0(...)
127 declare void @_ZN1AC2Ev(i8* %this)
129 ; CHECK-LABEL: @test7(
130 define void @test7() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
133 ; CHECK-NOT: call {{.*}}@_ZnwmRKSt9nothrow_t(
134 %call.i = tail call i8* @_ZnwmRKSt9nothrow_t(i64 1, i8* %nt) builtin nounwind
135 invoke void @_ZN1AC2Ev(i8* undef)
136 to label %.noexc.i unwind label %lpad.i
138 .noexc.i: ; preds = %entry
141 lpad.i: ; preds = %entry
142 %0 = landingpad { i8*, i32 } cleanup
143 ; CHECK-NOT: call {{.*}}@_ZdlPvRKSt9nothrow_t(
144 call void @_ZdlPvRKSt9nothrow_t(i8* %call.i, i8* %nt) builtin nounwind
145 resume { i8*, i32 } %0
148 declare i8* @_Znwm(i64) nobuiltin
149 define i8* @_Znwj(i32 %n) nobuiltin {
150 %z = zext i32 %n to i64
151 %m = call i8* @_Znwm(i64 %z)
154 declare i8* @_Znam(i64) nobuiltin
155 declare i8* @_Znaj(i32) nobuiltin
156 declare void @_ZdlPv(i8*) nobuiltin
157 declare void @_ZdaPv(i8*) nobuiltin
159 define linkonce void @_ZdlPvm(i8* %p, i64) nobuiltin {
160 call void @_ZdlPv(i8* %p)
163 define linkonce void @_ZdlPvj(i8* %p, i32) nobuiltin {
164 call void @_ZdlPv(i8* %p)
167 define linkonce void @_ZdaPvm(i8* %p, i64) nobuiltin {
168 call void @_ZdaPv(i8* %p)
171 define linkonce void @_ZdaPvj(i8* %p, i32) nobuiltin {
172 call void @_ZdaPv(i8* %p)
177 ; new(size_t, align_val_t)
178 declare i8* @_ZnwmSt11align_val_t(i64, i64) nobuiltin
179 declare i8* @_ZnwjSt11align_val_t(i32, i32) nobuiltin
180 ; new[](size_t, align_val_t)
181 declare i8* @_ZnamSt11align_val_t(i64, i64) nobuiltin
182 declare i8* @_ZnajSt11align_val_t(i32, i32) nobuiltin
183 ; new(size_t, align_val_t, nothrow)
184 declare i8* @_ZnwmSt11align_val_tRKSt9nothrow_t(i64, i64, i8*) nobuiltin
185 declare i8* @_ZnwjSt11align_val_tRKSt9nothrow_t(i32, i32, i8*) nobuiltin
186 ; new[](size_t, align_val_t, nothrow)
187 declare i8* @_ZnamSt11align_val_tRKSt9nothrow_t(i64, i64, i8*) nobuiltin
188 declare i8* @_ZnajSt11align_val_tRKSt9nothrow_t(i32, i32, i8*) nobuiltin
189 ; delete(void*, align_val_t)
190 declare void @_ZdlPvSt11align_val_t(i8*, i64) nobuiltin
191 ; delete[](void*, align_val_t)
192 declare void @_ZdaPvSt11align_val_t(i8*, i64) nobuiltin
193 ; delete(void*, align_val_t, nothrow)
194 declare void @_ZdlPvSt11align_val_tRKSt9nothrow_t(i8*, i64, i8*) nobuiltin
195 ; delete[](void*, align_val_t, nothrow)
196 declare void @_ZdaPvSt11align_val_tRKSt9nothrow_t(i8*, i64, i8*) nobuiltin
199 ; CHECK-LABEL: @test8(
200 define void @test8() {
203 %nw = call i8* @_Znwm(i64 32) builtin
204 call void @_ZdlPv(i8* %nw) builtin
205 %na = call i8* @_Znam(i64 32) builtin
206 call void @_ZdaPv(i8* %na) builtin
207 %nwm = call i8* @_Znwm(i64 32) builtin
208 call void @_ZdlPvm(i8* %nwm, i64 32) builtin
209 %nwj = call i8* @_Znwj(i32 32) builtin
210 call void @_ZdlPvj(i8* %nwj, i32 32) builtin
211 %nam = call i8* @_Znam(i64 32) builtin
212 call void @_ZdaPvm(i8* %nam, i64 32) builtin
213 %naj = call i8* @_Znaj(i32 32) builtin
214 call void @_ZdaPvj(i8* %naj, i32 32) builtin
215 %nwa = call i8* @_ZnwmSt11align_val_t(i64 32, i64 8) builtin
216 call void @_ZdlPvSt11align_val_t(i8* %nwa, i64 8) builtin
217 %naa = call i8* @_ZnamSt11align_val_t(i64 32, i64 8) builtin
218 call void @_ZdaPvSt11align_val_t(i8* %naa, i64 8) builtin
219 %nwja = call i8* @_ZnwjSt11align_val_t(i32 32, i32 8) builtin
220 call void @_ZdlPvSt11align_val_t(i8* %nwja, i64 8) builtin
221 %naja = call i8* @_ZnajSt11align_val_t(i32 32, i32 8) builtin
222 call void @_ZdaPvSt11align_val_t(i8* %naja, i64 8) builtin
223 %nwat = call i8* @_ZnwmSt11align_val_tRKSt9nothrow_t(i64 32, i64 8, i8* %nt) builtin
224 call void @_ZdlPvSt11align_val_tRKSt9nothrow_t(i8* %nwat, i64 8, i8* %nt) builtin
225 %naat = call i8* @_ZnamSt11align_val_tRKSt9nothrow_t(i64 32, i64 8, i8* %nt) builtin
226 call void @_ZdaPvSt11align_val_tRKSt9nothrow_t(i8* %naat, i64 8, i8* %nt) builtin
227 %nwjat = call i8* @_ZnwjSt11align_val_tRKSt9nothrow_t(i32 32, i32 8, i8* %nt) builtin
228 call void @_ZdlPvSt11align_val_tRKSt9nothrow_t(i8* %nwjat, i64 8, i8* %nt) builtin
229 %najat = call i8* @_ZnajSt11align_val_tRKSt9nothrow_t(i32 32, i32 8, i8* %nt) builtin
230 call void @_ZdaPvSt11align_val_tRKSt9nothrow_t(i8* %najat, i64 8, i8* %nt) builtin
234 declare noalias i8* @"\01??2@YAPEAX_K@Z"(i64) nobuiltin
235 declare void @"\01??3@YAXPEAX@Z"(i8*) nobuiltin
237 ; CHECK-LABEL: @test9(
238 define void @test9() {
240 %new_long_long = call noalias i8* @"\01??2@YAPEAX_K@Z"(i64 32) builtin
241 call void @"\01??3@YAXPEAX@Z"(i8* %new_long_long) builtin
245 define void @test10() {
246 ; CHECK-LABEL: @test10
247 ; CHECK: call void @_ZdlPv
248 call void @_ZdlPv(i8* null)
252 define void @test11() {
253 ; CHECK-LABEL: @test11
254 ; CHECK: call i8* @_Znwm
255 ; CHECK: call void @_ZdlPv
256 %call = call i8* @_Znwm(i64 8) builtin
257 call void @_ZdlPv(i8* %call)
261 ;; Check that the optimization that moves a call to free in its predecessor
262 ;; block (see test6) also happens when noop casts are involved.
263 ; CHECK-LABEL: @test12(
264 define void @test12(i32* %foo) minsize {
265 ; CHECK: %tobool = icmp eq i32* %foo, null
266 ;; Everything before the call to free should have been moved as well.
267 ; CHECK-NEXT: %bitcast = bitcast i32* %foo to i8*
268 ;; Call to free moved
269 ; CHECK-NEXT: tail call void @free(i8* %bitcast)
270 ; CHECK-NEXT: br i1 %tobool, label %if.end, label %if.then
272 ;; Block is now empty and may be simplified by simplifycfg
273 ; CHECK-NEXT: br label %if.end
275 ; CHECK-NEXT: ret void
277 %tobool = icmp eq i32* %foo, null
278 br i1 %tobool, label %if.end, label %if.then
280 if.then: ; preds = %entry
281 %bitcast = bitcast i32* %foo to i8*
282 tail call void @free(i8* %bitcast)
285 if.end: ; preds = %entry, %if.then