1 ; RUN: opt < %s -enable-coroutines -O2 -S | FileCheck %s
2 target datalayout = "E-p:32:32"
4 define i8* @f(i8* %buffer, i32 %n, i8** swifterror %errorslot) {
6 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, i8* %buffer, i8* bitcast (i8* (i8*, i1, i8**)* @f_prototype to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
7 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
11 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
12 call void @print(i32 %n.val)
13 call void @maybeThrow(i8** swifterror %errorslot)
14 %errorload1 = load i8*, i8** %errorslot
15 call void @logError(i8* %errorload1)
16 %suspend_result = call { i1, i8** } (...) @llvm.coro.suspend.retcon.i1p0p0i8()
17 %unwind0 = extractvalue { i1, i8** } %suspend_result, 0
18 br i1 %unwind0, 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* @f(i8* %buffer, i32 %n, i8** swifterror %errorslot)
31 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32*
32 ; CHECK-NEXT: store i32 %n, i32* [[T0]], align 4
33 ; CHECK-NEXT: call void @print(i32 %n)
34 ; TODO: figure out a way to eliminate this
35 ; CHECK-NEXT: store i8* null, i8** %errorslot
36 ; CHECK-NEXT: call void @maybeThrow(i8** swifterror %errorslot)
37 ; CHECK-NEXT: [[T1:%.*]] = load i8*, i8** %errorslot
38 ; CHECK-NEXT: call void @logError(i8* [[T1]])
39 ; CHECK-NEXT: store i8* [[T1]], i8** %errorslot
40 ; CHECK-NEXT: ret i8* bitcast (i8* (i8*, i1, i8**)* @f.resume.0 to i8*)
43 ; CHECK-LABEL: define internal i8* @f.resume.0(i8* noalias nonnull %0, i1 zeroext %1, i8** swifterror %2)
45 ; CHECK-NEXT: br i1 %1,
47 ; CHECK-NEXT: [[ERROR:%.*]] = load i8*, i8** %2, align 4
48 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %0 to i32*
49 ; CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[T0]], align 4
50 ; CHECK-NEXT: %inc = add i32 [[T1]], 1
51 ; CHECK-NEXT: store i32 %inc, i32* [[T0]], align 4
52 ; CHECK-NEXT: call void @print(i32 %inc)
53 ; CHECK-NEXT: store i8* [[ERROR]], i8** %2
54 ; CHECK-NEXT: call void @maybeThrow(i8** swifterror %2)
55 ; CHECK-NEXT: [[T2:%.*]] = load i8*, i8** %2
56 ; CHECK-NEXT: call void @logError(i8* [[T2]])
57 ; CHECK-NEXT: store i8* [[T2]], i8** %2
58 ; CHECK-NEXT: ret i8* bitcast (i8* (i8*, i1, i8**)* @f.resume.0 to i8*)
60 ; CHECK-NEXT: ret i8* null
63 define i8* @g(i8* %buffer, i32 %n) {
65 %errorslot = alloca swifterror i8*, align 4
66 store i8* null, i8** %errorslot
67 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, i8* %buffer, i8* bitcast (i8* (i8*, i1)* @g_prototype to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
68 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
72 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
73 call void @print(i32 %n.val)
74 call void @maybeThrow(i8** swifterror %errorslot)
75 %errorload1 = load i8*, i8** %errorslot
76 call void @logError(i8* %errorload1)
77 %unwind0 = call i1 (...) @llvm.coro.suspend.retcon.i1()
78 br i1 %unwind0, label %cleanup, label %resume
81 %inc = add i32 %n.val, 1
85 call i1 @llvm.coro.end(i8* %hdl, i1 0)
89 ; CHECK-LABEL: define i8* @g(i8* %buffer, i32 %n)
91 ; CHECK-NEXT: [[ERRORSLOT:%.*]] = alloca swifterror i8*, align 4
92 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32*
93 ; CHECK-NEXT: store i32 %n, i32* [[T0]], align 4
94 ; CHECK-NEXT: call void @print(i32 %n)
95 ; CHECK-NEXT: store i8* null, i8** [[ERRORSLOT]], align 4
96 ; CHECK-NEXT: call void @maybeThrow(i8** nonnull swifterror [[ERRORSLOT]])
97 ; CHECK-NEXT: [[T1:%.*]] = load i8*, i8** [[ERRORSLOT]], align 4
98 ; CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i8, i8* %buffer, i32 4
99 ; CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to i8**
100 ; CHECK-NEXT: store i8* [[T1]], i8** [[T3]], align 4
101 ; CHECK-NEXT: call void @logError(i8* [[T1]])
102 ; CHECK-NEXT: ret i8* bitcast (i8* (i8*, i1)* @g.resume.0 to i8*)
105 ; CHECK-LABEL: define internal i8* @g.resume.0(i8* noalias nonnull %0, i1 zeroext %1)
107 ; CHECK-NEXT: [[ERRORSLOT:%.*]] = alloca swifterror i8*, align 4
108 ; CHECK-NEXT: br i1 %1,
110 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %0 to i32*
111 ; CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[T0]], align 4
112 ; CHECK-NEXT: %inc = add i32 [[T1]], 1
113 ; CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i8, i8* %0, i32 4
114 ; CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to i8**
115 ; CHECK-NEXT: [[T4:%.*]] = load i8*, i8** [[T3]]
116 ; CHECK-NEXT: store i32 %inc, i32* [[T0]], align 4
117 ; CHECK-NEXT: call void @print(i32 %inc)
118 ; CHECK-NEXT: store i8* [[T4]], i8** [[ERRORSLOT]]
119 ; CHECK-NEXT: call void @maybeThrow(i8** nonnull swifterror [[ERRORSLOT]])
120 ; CHECK-NEXT: [[T5:%.*]] = load i8*, i8** [[ERRORSLOT]]
121 ; CHECK-NEXT: store i8* [[T5]], i8** [[T3]], align 4
122 ; CHECK-NEXT: call void @logError(i8* [[T5]])
123 ; CHECK-NEXT: ret i8* bitcast (i8* (i8*, i1)* @g.resume.0 to i8*)
125 ; CHECK-NEXT: ret i8* null
128 declare token @llvm.coro.id.retcon(i32, i32, i8*, i8*, i8*, i8*)
129 declare i8* @llvm.coro.begin(token, i8*)
130 declare { i1, i8** } @llvm.coro.suspend.retcon.i1p0p0i8(...)
131 declare i1 @llvm.coro.suspend.retcon.i1(...)
132 declare i1 @llvm.coro.end(i8*, i1)
133 declare i8* @llvm.coro.prepare.retcon(i8*)
135 declare i8* @f_prototype(i8*, i1 zeroext, i8** swifterror)
136 declare i8* @g_prototype(i8*, i1 zeroext)
138 declare noalias i8* @allocate(i32 %size)
139 declare void @deallocate(i8* %ptr)
141 declare void @print(i32)
142 declare void @maybeThrow(i8** swifterror)
143 declare void @logError(i8*)