1; RUN: opt < %s -instcombine -S | FileCheck %s
2; PR1201
3define i32 @main(i32 %argc, i8** %argv) {
4; CHECK-LABEL: @main(
5    %c_19 = alloca i8*
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)
10    ret i32 0
11; CHECK-NEXT: ret i32 0
12}
13
14declare noalias i8* @calloc(i32, i32) nounwind
15declare noalias i8* @malloc(i32)
16declare void @free(i8*)
17
18define i1 @foo() {
19; CHECK-LABEL: @foo(
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)
24  ret i1 %z
25}
26
27declare void @llvm.lifetime.start.p0i8(i64, i8*)
28declare void @llvm.lifetime.end.p0i8(i64, i8*)
29declare i64 @llvm.objectsize.i64(i8*, i1)
30declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
31declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i1) nounwind
32declare void @llvm.memset.p0i8.i32(i8*, i8, i32, i1) nounwind
33
34define 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)
41  store i8 42, i8* %a
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
47  ret void
48}
49
50;; This used to crash.
51define 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)
58  ret void
59}
60
61; CHECK-LABEL: @test5(
62define 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
72; CHECK-NEXT: store
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
77; CHECK-NEXT: ret
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
92  ret void
93}
94
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
97;; predecessor block.
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(
102define 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
107; CHECK: if.then:
108;; Block is now empty and may be simplified by simplifycfg
109; CHECK-NEXT:   br label %if.end
110; CHECK: if.end:
111; CHECK-NEXT:  ret void
112entry:
113  %tobool = icmp eq i8* %foo, null
114  br i1 %tobool, label %if.end, label %if.then
115
116if.then:                                          ; preds = %entry
117  tail call void @free(i8* %foo)
118  br label %if.end
119
120if.end:                                           ; preds = %entry, %if.then
121  ret void
122}
123
124declare i8* @_ZnwmRKSt9nothrow_t(i64, i8*) nobuiltin
125declare void @_ZdlPvRKSt9nothrow_t(i8*, i8*) nobuiltin
126declare i32 @__gxx_personality_v0(...)
127declare void @_ZN1AC2Ev(i8* %this)
128
129; CHECK-LABEL: @test7(
130define void @test7() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
131entry:
132  %nt = alloca 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
137
138.noexc.i:                                         ; preds = %entry
139  unreachable
140
141lpad.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
146}
147
148declare i8* @_Znwm(i64) nobuiltin
149define i8* @_Znwj(i32 %n) nobuiltin {
150  %z = zext i32 %n to i64
151  %m = call i8* @_Znwm(i64 %z)
152  ret i8* %m
153}
154declare i8* @_Znam(i64) nobuiltin
155declare i8* @_Znaj(i32) nobuiltin
156declare void @_ZdlPv(i8*) nobuiltin
157declare void @_ZdaPv(i8*) nobuiltin
158
159define linkonce void @_ZdlPvm(i8* %p, i64) nobuiltin {
160  call void @_ZdlPv(i8* %p)
161  ret void
162}
163define linkonce void @_ZdlPvj(i8* %p, i32) nobuiltin {
164  call void @_ZdlPv(i8* %p)
165  ret void
166}
167define linkonce void @_ZdaPvm(i8* %p, i64) nobuiltin {
168  call void @_ZdaPv(i8* %p)
169  ret void
170}
171define linkonce void @_ZdaPvj(i8* %p, i32) nobuiltin {
172  call void @_ZdaPv(i8* %p)
173  ret void
174}
175
176
177; new(size_t, align_val_t)
178declare i8* @_ZnwmSt11align_val_t(i64, i64) nobuiltin
179declare i8* @_ZnwjSt11align_val_t(i32, i32) nobuiltin
180; new[](size_t, align_val_t)
181declare i8* @_ZnamSt11align_val_t(i64, i64) nobuiltin
182declare i8* @_ZnajSt11align_val_t(i32, i32) nobuiltin
183; new(size_t, align_val_t, nothrow)
184declare i8* @_ZnwmSt11align_val_tRKSt9nothrow_t(i64, i64, i8*) nobuiltin
185declare i8* @_ZnwjSt11align_val_tRKSt9nothrow_t(i32, i32, i8*) nobuiltin
186; new[](size_t, align_val_t, nothrow)
187declare i8* @_ZnamSt11align_val_tRKSt9nothrow_t(i64, i64, i8*) nobuiltin
188declare i8* @_ZnajSt11align_val_tRKSt9nothrow_t(i32, i32, i8*) nobuiltin
189; delete(void*, align_val_t)
190declare void @_ZdlPvSt11align_val_t(i8*, i64) nobuiltin
191; delete[](void*, align_val_t)
192declare void @_ZdaPvSt11align_val_t(i8*, i64) nobuiltin
193; delete(void*, align_val_t, nothrow)
194declare void @_ZdlPvSt11align_val_tRKSt9nothrow_t(i8*, i64, i8*) nobuiltin
195; delete[](void*, align_val_t, nothrow)
196declare void @_ZdaPvSt11align_val_tRKSt9nothrow_t(i8*, i64, i8*) nobuiltin
197
198
199; CHECK-LABEL: @test8(
200define void @test8() {
201  ; CHECK-NOT: call
202  %nt = alloca i8
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
231  ret void
232}
233
234declare noalias i8* @"\01??2@YAPEAX_K@Z"(i64) nobuiltin
235declare void @"\01??3@YAXPEAX@Z"(i8*) nobuiltin
236
237; CHECK-LABEL: @test9(
238define void @test9() {
239  ; CHECK-NOT: call
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
242  ret void
243}
244
245define void @test10()  {
246; CHECK-LABEL: @test10
247; CHECK: call void @_ZdlPv
248  call void @_ZdlPv(i8* null)
249  ret void
250}
251
252define 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)
258  ret void
259}
260