1 ; RUN: opt < %s -enable-coroutines -O2 -S | FileCheck %s
3 target datalayout = "p:64:64:64"
5 declare {i8*, i8*, i32} @prototype_f(i8*, i1)
6 define {i8*, i8*, i32} @f(i8* %buffer, i32 %n) {
8 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, i8* %buffer, i8* bitcast ({i8*, i8*, i32} (i8*, i1)* @prototype_f to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
9 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
13 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
14 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %n.val, i32 8)
15 %ptr = call i8* @llvm.coro.alloca.get(token %alloca)
16 %unwind = call i1 (...) @llvm.coro.suspend.retcon.i1(i8* %ptr, i32 %n.val)
17 call void @llvm.coro.alloca.free(token %alloca)
18 br i1 %unwind, label %cleanup, label %resume
21 %inc = add i32 %n.val, 1
25 call i1 @llvm.coro.end(i8* %hdl, i1 0)
29 ; CHECK-LABEL: define { i8*, i8*, i32 } @f(i8* %buffer, i32 %n)
31 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32*
32 ; CHECK-NEXT: store i32 %n, i32* [[T0]], align 4
33 ; CHECK-NEXT: [[ALLOC:%.*]] = tail call i8* @allocate(i32 %n)
34 ; CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds i8, i8* %buffer, i64 8
35 ; CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to i8**
36 ; CHECK-NEXT: store i8* [[ALLOC]], i8** [[T1]], align 8
37 ; CHECK-NEXT: [[T0:%.*]] = insertvalue { i8*, i8*, i32 } { i8* bitcast ({ i8*, i8*, i32 } (i8*, i1)* @f.resume.0 to i8*), i8* undef, i32 undef }, i8* [[ALLOC]], 1
38 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i8*, i32 } [[T0]], i32 %n, 2
39 ; CHECK-NEXT: ret { i8*, i8*, i32 } [[RET]]
42 ; CHECK-LABEL: define internal { i8*, i8*, i32 } @f.resume.0(i8* noalias nonnull %0, i1 %1)
44 ; CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds i8, i8* %0, i64 8
45 ; CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to i8**
46 ; CHECK-NEXT: [[ALLOC:%.*]] = load i8*, i8** [[T1]], align 8
47 ; CHECK-NEXT: tail call void @deallocate(i8* [[ALLOC]])
48 ; CHECK-NEXT: br i1 %1,
50 declare {i8*, i32} @prototype_g(i8*, i1)
51 define {i8*, i32} @g(i8* %buffer, i32 %n) {
53 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, i8* %buffer, i8* bitcast ({i8*, i32} (i8*, i1)* @prototype_g to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
54 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
58 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
59 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %n.val, i32 8)
60 %ptr = call i8* @llvm.coro.alloca.get(token %alloca)
61 call void @use(i8* %ptr)
62 call void @llvm.coro.alloca.free(token %alloca)
63 %unwind = call i1 (...) @llvm.coro.suspend.retcon.i1(i32 %n.val)
64 br i1 %unwind, label %cleanup, label %resume
67 %inc = add i32 %n.val, 1
71 call i1 @llvm.coro.end(i8* %hdl, i1 0)
75 ; CHECK-LABEL: define { i8*, i32 } @g(i8* %buffer, i32 %n)
77 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32*
78 ; CHECK-NEXT: store i32 %n, i32* [[T0]], align 4
79 ; CHECK-NEXT: [[T0:%.*]] = zext i32 %n to i64
80 ; CHECK-NEXT: [[ALLOC:%.*]] = alloca i8, i64 [[T0]], align 8
81 ; CHECK-NEXT: call void @use(i8* nonnull [[ALLOC]])
82 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*, i1)* @g.resume.0 to i8*), i32 undef }, i32 %n, 1
83 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
86 ; CHECK-LABEL: define internal { i8*, i32 } @g.resume.0(i8* noalias nonnull %0, i1 %1)
88 ; CHECK-NEXT: br i1 %1,
90 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %0 to i32*
91 ; CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[T0]], align 4
92 ; CHECK-NEXT: %inc = add i32 [[T1]], 1
93 ; CHECK-NEXT: store i32 %inc, i32* [[T0]], align 4
94 ; CHECK-NEXT: [[T0:%.*]] = zext i32 %inc to i64
95 ; CHECK-NEXT: [[ALLOC:%.*]] = alloca i8, i64 [[T0]], align 8
96 ; CHECK-NEXT: call void @use(i8* nonnull [[ALLOC]])
97 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*, i1)* @g.resume.0 to i8*), i32 undef }, i32 %inc, 1
98 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
100 ; CHECK-NEXT: ret { i8*, i32 } { i8* null, i32 undef }
102 declare {i8*, i32} @prototype_h(i8*, i1)
103 define {i8*, i32} @h(i8* %buffer, i32 %n) {
105 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, i8* %buffer, i8* bitcast ({i8*, i32} (i8*, i1)* @prototype_h to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
106 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
110 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
111 %unwind = call i1 (...) @llvm.coro.suspend.retcon.i1(i32 %n.val)
112 br i1 %unwind, label %cleanup, label %resume
115 %inc = add i32 %n.val, 1
116 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %inc, i32 8)
117 %ptr = call i8* @llvm.coro.alloca.get(token %alloca)
118 call void @use(i8* %ptr)
119 call void @llvm.coro.alloca.free(token %alloca)
123 call i1 @llvm.coro.end(i8* %hdl, i1 0)
127 ; CHECK-LABEL: define { i8*, i32 } @h(i8* %buffer, i32 %n)
129 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32*
130 ; CHECK-NEXT: store i32 %n, i32* [[T0]], align 4
131 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*, i1)* @h.resume.0 to i8*), i32 undef }, i32 %n, 1
132 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
135 ; CHECK-LABEL: define internal { i8*, i32 } @h.resume.0(i8* noalias nonnull %0, i1 %1)
137 ; CHECK-NEXT: br i1 %1,
139 ; CHECK-NEXT: [[NSLOT:%.*]] = bitcast i8* %0 to i32*
140 ; CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[NSLOT]], align 4
141 ; CHECK-NEXT: %inc = add i32 [[T1]], 1
142 ; CHECK-NEXT: [[T0:%.*]] = zext i32 %inc to i64
143 ; CHECK-NEXT: [[ALLOC:%.*]] = alloca i8, i64 [[T0]], align 8
144 ; CHECK-NEXT: call void @use(i8* nonnull [[ALLOC]])
145 ; CHECK-NEXT: store i32 %inc, i32* [[NSLOT]], align 4
146 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*, i1)* @h.resume.0 to i8*), i32 undef }, i32 %inc, 1
147 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
149 ; CHECK-NEXT: ret { i8*, i32 } { i8* null, i32 undef }
151 declare {i8*, i32} @prototype_i(i8*)
152 define {i8*, i32} @i(i8* %buffer, i32 %n) {
154 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, i8* %buffer, i8* bitcast ({i8*, i32} (i8*)* @prototype_i to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
155 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
159 %n.val = phi i32 [ %n, %entry ], [ %k, %loop2 ]
160 call void (...) @llvm.coro.suspend.retcon.isVoid(i32 %n.val)
161 %inc = add i32 %n.val, 1
165 %k = phi i32 [ %inc, %loop ], [ %k2, %loop2 ]
166 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %k, i32 8)
167 %ptr = call i8* @llvm.coro.alloca.get(token %alloca)
168 call void @use(i8* %ptr)
169 call void @llvm.coro.alloca.free(token %alloca)
171 %cmp = icmp ugt i32 %k, 128
172 br i1 %cmp, label %loop2, label %loop
175 ; CHECK-LABEL: define { i8*, i32 } @i(i8* %buffer, i32 %n)
177 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32*
178 ; CHECK-NEXT: store i32 %n, i32* [[T0]], align 4
179 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*)* @i.resume.0 to i8*), i32 undef }, i32 %n, 1
180 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
183 ; CHECK-LABEL: define internal { i8*, i32 } @i.resume.0(i8* noalias nonnull %0)
185 ; CHECK-NEXT: [[NSLOT:%.*]] = bitcast i8* %0 to i32*
186 ; CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[NSLOT]], align 4
187 ; CHECK-NEXT: %inc = add i32 [[T1]], 1
188 ; CHECK-NEXT: br label %loop2
190 ; CHECK-NEXT: store i32 %k, i32* [[NSLOT]], align 4
191 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*)* @i.resume.0 to i8*), i32 undef }, i32 %k, 1
192 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
194 ; CHECK-NEXT: %k = phi i32 [ %inc, {{.*}} ], [ %k2, %loop2 ]
195 ; CHECK-NEXT: [[SAVE:%.*]] = call i8* @llvm.stacksave()
196 ; CHECK-NEXT: [[T0:%.*]] = zext i32 %k to i64
197 ; CHECK-NEXT: [[ALLOC:%.*]] = alloca i8, i64 [[T0]], align 8
198 ; CHECK-NEXT: call void @use(i8* nonnull [[ALLOC]])
199 ; CHECK-NEXT: call void @llvm.stackrestore(i8* [[SAVE]])
200 ; CHECK-NEXT: %k2 = lshr i32 %k, 1
201 ; CHECK-NEXT: %cmp = icmp ugt i32 %k, 128
202 ; CHECK-NEXT: br i1 %cmp, label %loop2,
205 declare {i8*, i32} @prototype_j(i8*)
206 define {i8*, i32} @j(i8* %buffer, i32 %n) {
208 %id = call token @llvm.coro.id.retcon(i32 1024, i32 8, i8* %buffer, i8* bitcast ({i8*, i32} (i8*)* @prototype_j to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
209 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
213 ; We should encounter this 'get' before we encounter the 'alloc'.
214 %ptr = call i8* @llvm.coro.alloca.get(token %alloca)
215 call void @use(i8* %ptr)
216 call void @llvm.coro.alloca.free(token %alloca)
217 %k = add i32 %n.val, 1
218 %cmp = icmp ugt i32 %k, 128
219 br i1 %cmp, label %forward, label %end
222 %n.val = phi i32 [ %n, %entry ], [ %k, %back ]
223 call void (...) @llvm.coro.suspend.retcon.isVoid(i32 %n.val)
224 %alloca = call token @llvm.coro.alloca.alloc.i32(i32 %n.val, i32 8)
225 %inc = add i32 %n.val, 1
229 call i1 @llvm.coro.end(i8* %hdl, i1 0)
233 declare token @llvm.coro.id.retcon(i32, i32, i8*, i8*, i8*, i8*)
234 declare i8* @llvm.coro.begin(token, i8*)
235 declare i1 @llvm.coro.suspend.retcon.i1(...)
236 declare void @llvm.coro.suspend.retcon.isVoid(...)
237 declare i1 @llvm.coro.end(i8*, i1)
238 declare i8* @llvm.coro.prepare.retcon(i8*)
239 declare token @llvm.coro.alloca.alloc.i32(i32, i32)
240 declare i8* @llvm.coro.alloca.get(token)
241 declare void @llvm.coro.alloca.free(token)
243 declare noalias i8* @allocate(i32 %size)
244 declare void @deallocate(i8* %ptr)
246 declare void @print(i32)
247 declare void @use(i8*)