1 ; RUN: opt < %s -enable-coroutines -O2 -S | FileCheck %s
2 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-apple-macosx10.12.0"
5 define {i8*, i32} @f(i8* %buffer, i32* %array) {
7 %id = call token @llvm.coro.id.retcon.once(i32 8, i32 8, i8* %buffer, i8* bitcast (void (i8*, i1)* @prototype to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
8 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
9 %load = load i32, i32* %array
10 %load.pos = icmp sgt i32 %load, 0
11 br i1 %load.pos, label %pos, label %neg
14 %unwind0 = call i1 (...) @llvm.coro.suspend.retcon.i1(i32 %load)
15 br i1 %unwind0, label %cleanup, label %pos.cont
18 store i32 0, i32* %array, align 4
22 %unwind1 = call i1 (...) @llvm.coro.suspend.retcon.i1(i32 0)
23 br i1 %unwind1, label %cleanup, label %neg.cont
26 store i32 10, i32* %array, align 4
30 call i1 @llvm.coro.end(i8* %hdl, i1 0)
34 ; CHECK-LABEL: define { i8*, i32 } @f(i8* %buffer, i32* %array)
36 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32**
37 ; CHECK-NEXT: store i32* %array, i32** [[T0]], align 8
38 ; CHECK-NEXT: %load = load i32, i32* %array, align 4
39 ; CHECK-NEXT: %load.pos = icmp sgt i32 %load, 0
40 ; CHECK-NEXT: [[CONT:%.*]] = select i1 %load.pos, void (i8*, i1)* @f.resume.0, void (i8*, i1)* @f.resume.1
41 ; CHECK-NEXT: [[VAL:%.*]] = select i1 %load.pos, i32 %load, i32 0
42 ; CHECK-NEXT: [[CONT_CAST:%.*]] = bitcast void (i8*, i1)* [[CONT]] to i8*
43 ; CHECK-NEXT: [[T0:%.*]] = insertvalue { i8*, i32 } undef, i8* [[CONT_CAST]], 0
44 ; CHECK-NEXT: [[T1:%.*]] = insertvalue { i8*, i32 } [[T0]], i32 [[VAL]], 1
45 ; CHECK-NEXT: ret { i8*, i32 } [[T1]]
48 ; CHECK-LABEL: define internal void @f.resume.0(i8* noalias nonnull %0, i1 zeroext %1)
50 ; CHECK-NEXT: br i1 %1,
52 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %0 to i32**
53 ; CHECK-NEXT: [[RELOAD:%.*]] = load i32*, i32** [[T0]], align 8
54 ; CHECK-NEXT: store i32 0, i32* [[RELOAD]], align 4
55 ; CHECK-NEXT: br label
57 ; CHECK-NEXT: ret void
60 ; CHECK-LABEL: define internal void @f.resume.1(i8* noalias nonnull %0, i1 zeroext %1)
62 ; CHECK-NEXT: br i1 %1,
64 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %0 to i32**
65 ; CHECK-NEXT: [[RELOAD:%.*]] = load i32*, i32** [[T0]], align 8
66 ; CHECK-NEXT: store i32 10, i32* [[RELOAD]], align 4
67 ; CHECK-NEXT: br label
69 ; CHECK-NEXT: ret void
72 define void @test(i32* %array) {
74 %0 = alloca [8 x i8], align 8
75 %buffer = bitcast [8 x i8]* %0 to i8*
76 %prepare = call i8* @llvm.coro.prepare.retcon(i8* bitcast ({i8*, i32} (i8*, i32*)* @f to i8*))
77 %f = bitcast i8* %prepare to {i8*, i32} (i8*, i32*)*
78 %result = call {i8*, i32} %f(i8* %buffer, i32* %array)
79 %value = extractvalue {i8*, i32} %result, 1
80 call void @print(i32 %value)
81 %cont = extractvalue {i8*, i32} %result, 0
82 %cont.cast = bitcast i8* %cont to void (i8*, i1)*
83 call void %cont.cast(i8* %buffer, i1 zeroext 0)
87 ; Unfortunately, we don't seem to fully optimize this right now due
88 ; to some sort of phase-ordering thing.
89 ; CHECK-LABEL: define void @test(i32* %array)
91 ; CHECK-NEXT: [[BUFFER:%.*]] = alloca i32*, align 8
92 ; CHECK-NEXT: [[BUFFER_CAST:%.*]] = bitcast i32** [[BUFFER]] to i8*
93 ; CHECK-NEXT: store i32* %array, i32** [[BUFFER]], align 8
94 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, i32* %array, align 4
95 ; CHECK-NEXT: [[LOAD_POS:%.*]] = icmp sgt i32 [[LOAD]], 0
96 ; CHECK-NEXT: [[CONT:%.*]] = select i1 [[LOAD_POS]], void (i8*, i1)* @f.resume.0, void (i8*, i1)* @f.resume.1
97 ; CHECK-NEXT: [[VAL:%.*]] = select i1 [[LOAD_POS]], i32 [[LOAD]], i32 0
98 ; CHECK-NEXT: call void @print(i32 [[VAL]])
99 ; CHECK-NEXT: call void [[CONT]](i8* nonnull [[BUFFER_CAST]], i1 zeroext false)
100 ; CHECK-NEXT: ret void
102 declare token @llvm.coro.id.retcon.once(i32, i32, i8*, i8*, i8*, i8*)
103 declare i8* @llvm.coro.begin(token, i8*)
104 declare i1 @llvm.coro.suspend.retcon.i1(...)
105 declare i1 @llvm.coro.end(i8*, i1)
106 declare i8* @llvm.coro.prepare.retcon(i8*)
108 declare void @prototype(i8*, i1 zeroext)
110 declare noalias i8* @allocate(i32 %size)
111 declare void @deallocate(i8* %ptr)
113 declare void @print(i32)