1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes='default<O2>' -S | FileCheck %s
4 target datalayout = "p:64:64:64"
6 declare {ptr, ptr, i32} @prototype_f(ptr, i1)
7 define {ptr, ptr, i32} @f(ptr %buffer, i32 %n) {
9 ; CHECK-NEXT: coro.return:
10 ; CHECK-NEXT: [[N_VAL_SPILL_ADDR:%.*]] = getelementptr inbounds [[F_FRAME:%.*]], ptr [[BUFFER:%.*]], i64 0, i32 1
11 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[N_VAL_SPILL_ADDR]], align 4
12 ; CHECK-NEXT: [[TMP0:%.*]] = tail call ptr @allocate(i32 [[N]])
13 ; CHECK-NEXT: store ptr [[TMP0]], ptr [[BUFFER]], align 8
14 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { ptr, ptr, i32 } { ptr @f.resume.0, ptr poison, i32 poison }, ptr [[TMP0]], 1
15 ; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { ptr, ptr, i32 } [[TMP1]], i32 [[N]], 2
16 ; CHECK-NEXT: ret { ptr, ptr, i32 } [[TMP2]]
19 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, ptr %buffer, ptr @prototype_f, ptr @allocate, ptr @deallocate)
20 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
24 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
25 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %n.val, i32 8)
26 %ptr = call ptr @llvm.coro.alloca.get(token %alloca)
27 %unwind = call i1 (...) @llvm.coro.suspend.retcon.i1(ptr %ptr, i32 %n.val)
28 call void @llvm.coro.alloca.free(token %alloca)
29 br i1 %unwind, label %cleanup, label %resume
32 %inc = add i32 %n.val, 1
36 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
42 declare {ptr, i32} @prototype_g(ptr, i1)
43 define {ptr, i32} @g(ptr %buffer, i32 %n) {
45 ; CHECK-NEXT: coro.return:
46 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[BUFFER:%.*]], align 4
47 ; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[N]] to i64
48 ; CHECK-NEXT: [[TMP1:%.*]] = alloca i8, i64 [[TMP0]], align 8
49 ; CHECK-NEXT: tail call void @use(ptr nonnull [[TMP1]])
50 ; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { ptr, i32 } { ptr @g.resume.0, i32 poison }, i32 [[N]], 1
51 ; CHECK-NEXT: ret { ptr, i32 } [[TMP2]]
54 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, ptr %buffer, ptr @prototype_g, ptr @allocate, ptr @deallocate)
55 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
59 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
60 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %n.val, i32 8)
61 %ptr = call ptr @llvm.coro.alloca.get(token %alloca)
62 call void @use(ptr %ptr)
63 call void @llvm.coro.alloca.free(token %alloca)
64 %unwind = call i1 (...) @llvm.coro.suspend.retcon.i1(i32 %n.val)
65 br i1 %unwind, label %cleanup, label %resume
68 %inc = add i32 %n.val, 1
72 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
78 declare {ptr, i32} @prototype_h(ptr, i1)
79 define {ptr, i32} @h(ptr %buffer, i32 %n) {
81 ; CHECK-NEXT: coro.return:
82 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[BUFFER:%.*]], align 4
83 ; CHECK-NEXT: [[TMP0:%.*]] = insertvalue { ptr, i32 } { ptr @h.resume.0, i32 poison }, i32 [[N]], 1
84 ; CHECK-NEXT: ret { ptr, i32 } [[TMP0]]
87 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, ptr %buffer, ptr @prototype_h, ptr @allocate, ptr @deallocate)
88 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
92 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
93 %unwind = call i1 (...) @llvm.coro.suspend.retcon.i1(i32 %n.val)
94 br i1 %unwind, label %cleanup, label %resume
97 %inc = add i32 %n.val, 1
98 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %inc, i32 8)
99 %ptr = call ptr @llvm.coro.alloca.get(token %alloca)
100 call void @use(ptr %ptr)
101 call void @llvm.coro.alloca.free(token %alloca)
105 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
111 declare {ptr, i32} @prototype_i(ptr)
112 define {ptr, i32} @i(ptr %buffer, i32 %n) {
114 ; CHECK-NEXT: coro.return:
115 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[BUFFER:%.*]], align 4
116 ; CHECK-NEXT: [[TMP0:%.*]] = insertvalue { ptr, i32 } { ptr @i.resume.0, i32 poison }, i32 [[N]], 1
117 ; CHECK-NEXT: ret { ptr, i32 } [[TMP0]]
120 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, ptr %buffer, ptr @prototype_i, ptr @allocate, ptr @deallocate)
121 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
125 %n.val = phi i32 [ %n, %entry ], [ %k, %loop2 ]
126 call void (...) @llvm.coro.suspend.retcon.isVoid(i32 %n.val)
127 %inc = add i32 %n.val, 1
131 %k = phi i32 [ %inc, %loop ], [ %k2, %loop2 ]
132 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %k, i32 8)
133 %ptr = call ptr @llvm.coro.alloca.get(token %alloca)
134 call void @use(ptr %ptr)
135 call void @llvm.coro.alloca.free(token %alloca)
137 %cmp = icmp ugt i32 %k, 128
138 br i1 %cmp, label %loop2, label %loop
143 declare {ptr, i32} @prototype_j(ptr)
144 define {ptr, i32} @j(ptr %buffer, i32 %n) {
146 ; CHECK-NEXT: coro.return:
147 ; CHECK-NEXT: store i32 [[N:%.*]], ptr [[BUFFER:%.*]], align 4
148 ; CHECK-NEXT: [[TMP0:%.*]] = insertvalue { ptr, i32 } { ptr @j.resume.0, i32 poison }, i32 [[N]], 1
149 ; CHECK-NEXT: ret { ptr, i32 } [[TMP0]]
152 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, ptr %buffer, ptr @prototype_j, ptr @allocate, ptr @deallocate)
153 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
157 ; We should encounter this 'get' before we encounter the 'alloc'.
158 %ptr = call ptr @llvm.coro.alloca.get(token %alloca)
159 call void @use(ptr %ptr)
160 call void @llvm.coro.alloca.free(token %alloca)
161 %k = add i32 %n.val, 1
162 %cmp = icmp ugt i32 %k, 128
163 br i1 %cmp, label %forward, label %end
166 %n.val = phi i32 [ %n, %entry ], [ %k, %back ]
167 call void (...) @llvm.coro.suspend.retcon.isVoid(i32 %n.val)
168 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %n.val, i32 8)
169 %inc = add i32 %n.val, 1
173 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
177 declare i32 @getSize()
178 define {ptr, i32} @k(ptr %buffer, i32 %n, i1 %cond) {
180 ; CHECK-NEXT: PostSpill:
181 ; CHECK-NEXT: [[SIZE:%.*]] = tail call i32 @getSize()
182 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[ALLOCA_BLOCK:%.*]], label [[CORO_RETURN:%.*]]
183 ; CHECK: coro.return:
184 ; CHECK-NEXT: [[TMP0:%.*]] = insertvalue { ptr, i32 } { ptr @k.resume.0, i32 poison }, i32 [[N:%.*]], 1
185 ; CHECK-NEXT: ret { ptr, i32 } [[TMP0]]
186 ; CHECK: alloca_block:
187 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[SIZE]] to i64
188 ; CHECK-NEXT: [[TMP2:%.*]] = alloca i8, i64 [[TMP1]], align 8
189 ; CHECK-NEXT: tail call void @use(ptr nonnull [[TMP2]])
190 ; CHECK-NEXT: br label [[CORO_RETURN]]
193 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, ptr %buffer, ptr @prototype_g, ptr @allocate, ptr @deallocate)
194 %hdl = call ptr @llvm.coro.begin(token %id, ptr null)
195 br i1 %cond, label %alloca_block, label %non_alloca_block
198 %unwind = call i1 (...) @llvm.coro.suspend.retcon.i1(i32 %n)
199 br i1 %unwind, label %cleanup, label %resume
205 %size = call i32 @getSize()
206 ; This will get lowered to a dynamic alloca.
207 ; Make sure code that runs after that lowering does not hoist the dynamic
208 ; alloca into the entry block of the resume function.
209 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %size, i32 8)
210 %ptr = call ptr @llvm.coro.alloca.get(token %alloca)
211 call void @use(ptr %ptr)
212 call void @llvm.coro.alloca.free(token %alloca)
216 %ignore = call i32 @getSize()
220 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
224 declare token @llvm.coro.id.retcon(i32, i32, ptr, ptr, ptr, ptr)
225 declare ptr @llvm.coro.begin(token, ptr)
226 declare i1 @llvm.coro.suspend.retcon.i1(...)
227 declare void @llvm.coro.suspend.retcon.isVoid(...)
228 declare i1 @llvm.coro.end(ptr, i1, token)
229 declare ptr @llvm.coro.prepare.retcon(ptr)
230 declare token @llvm.coro.alloca.alloc.i32(i32, i32)
231 declare ptr @llvm.coro.alloca.get(token)
232 declare void @llvm.coro.alloca.free(token)
234 declare noalias ptr @allocate(i32 %size)
235 declare void @deallocate(ptr %ptr)
237 declare void @print(i32)
238 declare void @use(ptr)