1 ; First example from Doc/Coroutines.rst (two block loop) converted to retcon
2 ; RUN: opt < %s -enable-coroutines -O2 -S | FileCheck %s
4 define {i8*, i32} @f(i8* %buffer, i32 %n) {
6 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, i8* %buffer, i8* bitcast ({i8*, i32} (i8*, i8)* @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 %unwind = call i8 (...) @llvm.coro.suspend.retcon.i8(i32 %n.val)
13 %unwind0 = icmp ne i8 %unwind, 0
14 br i1 %unwind0, label %cleanup, label %resume
17 %inc = add i32 %n.val, 1
21 call i1 @llvm.coro.end(i8* %hdl, i1 0)
25 ; CHECK-LABEL: define { i8*, i32 } @f(i8* %buffer, i32 %n)
27 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %buffer to i32*
28 ; CHECK-NEXT: store i32 %n, i32* [[T0]], align 4
29 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*, i8)* @f.resume.0 to i8*), i32 undef }, i32 %n, 1
30 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
33 ; CHECK-LABEL: define internal { i8*, i32 } @f.resume.0(i8* noalias nonnull %0, i8 zeroext %1)
35 ; CHECK-NEXT: [[T0:%.*]] = icmp eq i8 %1, 0
36 ; CHECK-NEXT: br i1 [[T0]],
38 ; CHECK-NEXT: [[T0:%.*]] = bitcast i8* %0 to i32*
39 ; CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[T0]], align 4
40 ; CHECK-NEXT: %inc = add i32 [[T1]], 1
41 ; CHECK-NEXT: store i32 %inc, i32* [[T0]], align 4
42 ; CHECK-NEXT: [[RET:%.*]] = insertvalue { i8*, i32 } { i8* bitcast ({ i8*, i32 } (i8*, i8)* @f.resume.0 to i8*), i32 undef }, i32 %inc, 1
43 ; CHECK-NEXT: ret { i8*, i32 } [[RET]]
45 ; CHECK-NEXT: ret { i8*, i32 } { i8* null, i32 undef }
50 %0 = alloca [8 x i8], align 4
51 %buffer = bitcast [8 x i8]* %0 to i8*
52 %prepare = call i8* @llvm.coro.prepare.retcon(i8* bitcast ({i8*, i32} (i8*, i32)* @f to i8*))
53 %f = bitcast i8* %prepare to {i8*, i32} (i8*, i32)*
54 %result0 = call {i8*, i32} %f(i8* %buffer, i32 4)
55 %value0 = extractvalue {i8*, i32} %result0, 1
56 call void @print(i32 %value0)
57 %cont0 = extractvalue {i8*, i32} %result0, 0
58 %cont0.cast = bitcast i8* %cont0 to {i8*, i32} (i8*, i8)*
59 %result1 = call {i8*, i32} %cont0.cast(i8* %buffer, i8 zeroext 0)
60 %value1 = extractvalue {i8*, i32} %result1, 1
61 call void @print(i32 %value1)
62 %cont1 = extractvalue {i8*, i32} %result1, 0
63 %cont1.cast = bitcast i8* %cont1 to {i8*, i32} (i8*, i8)*
64 %result2 = call {i8*, i32} %cont1.cast(i8* %buffer, i8 zeroext 0)
65 %value2 = extractvalue {i8*, i32} %result2, 1
66 call void @print(i32 %value2)
67 %cont2 = extractvalue {i8*, i32} %result2, 0
68 %cont2.cast = bitcast i8* %cont2 to {i8*, i32} (i8*, i8)*
69 call {i8*, i32} %cont2.cast(i8* %buffer, i8 zeroext 1)
73 ; Unfortunately, we don't seem to fully optimize this right now due
74 ; to some sort of phase-ordering thing.
75 ; CHECK-LABEL: define i32 @main
77 ; CHECK: [[BUFFER:%.*]] = alloca [8 x i8], align 4
78 ; CHECK: [[SLOT:%.*]] = bitcast [8 x i8]* [[BUFFER]] to i32*
79 ; CHECK-NEXT: store i32 4, i32* [[SLOT]], align 4
80 ; CHECK-NEXT: call void @print(i32 4)
81 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, i32* [[SLOT]], align 4
82 ; CHECK-NEXT: [[INC:%.*]] = add i32 [[LOAD]], 1
83 ; CHECK-NEXT: store i32 [[INC]], i32* [[SLOT]], align 4
84 ; CHECK-NEXT: call void @print(i32 [[INC]])
85 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, i32* [[SLOT]], align 4
86 ; CHECK-NEXT: [[INC:%.*]] = add i32 [[LOAD]], 1
87 ; CHECK-NEXT: store i32 [[INC]], i32* [[SLOT]], align 4
88 ; CHECK-NEXT: call void @print(i32 [[INC]])
89 ; CHECK-NEXT: ret i32 0
91 declare token @llvm.coro.id.retcon(i32, i32, i8*, i8*, i8*, i8*)
92 declare i8* @llvm.coro.begin(token, i8*)
93 declare i8 @llvm.coro.suspend.retcon.i8(...)
94 declare i1 @llvm.coro.end(i8*, i1)
95 declare i8* @llvm.coro.prepare.retcon(i8*)
97 declare {i8*, i32} @prototype(i8*, i8 zeroext)
99 declare noalias i8* @allocate(i32 %size)
100 declare void @deallocate(i8* %ptr)
102 declare void @print(i32)