1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; First example from Doc/Coroutines.rst (two block loop) converted to retcon
3 ; RUN: opt < %s -enable-coroutines -passes='default<O2>' -S | FileCheck %s
5 define i8* @f(i8* %buffer, i32 %n) {
7 ; CHECK-NEXT: coro.return:
8 ; CHECK-NEXT: [[N_VAL_SPILL_ADDR:%.*]] = bitcast i8* [[BUFFER:%.*]] to i32*
9 ; CHECK-NEXT: store i32 [[N:%.*]], i32* [[N_VAL_SPILL_ADDR]], align 4
10 ; CHECK-NEXT: tail call void @print(i32 [[N]])
11 ; CHECK-NEXT: ret i8* bitcast (i8* (i8*, i1)* @f.resume.0 to i8*)
14 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, i8* %buffer, i8* bitcast (i8* (i8*, i1)* @prototype to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
15 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
19 %n.val = phi i32 [ %n, %entry ], [ %inc, %resume ]
20 call void @print(i32 %n.val)
21 %unwind0 = call i1 (...) @llvm.coro.suspend.retcon.i1()
22 br i1 %unwind0, label %cleanup, label %resume
25 %inc = add i32 %n.val, 1
29 call i1 @llvm.coro.end(i8* %hdl, i1 0)
38 ; CHECK-NEXT: [[TMP0:%.*]] = alloca [8 x i8], align 4
39 ; CHECK-NEXT: [[DOTSUB:%.*]] = getelementptr inbounds [8 x i8], [8 x i8]* [[TMP0]], i64 0, i64 0
40 ; CHECK-NEXT: [[N_VAL_SPILL_ADDR_I:%.*]] = bitcast [8 x i8]* [[TMP0]] to i32*
41 ; CHECK-NEXT: store i32 4, i32* [[N_VAL_SPILL_ADDR_I]], align 4
42 ; CHECK-NEXT: call void @print(i32 4)
43 ; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META0:![0-9]+]])
44 ; CHECK-NEXT: [[N_VAL_RELOAD_I:%.*]] = load i32, i32* [[N_VAL_SPILL_ADDR_I]], align 4, !alias.scope !0
45 ; CHECK-NEXT: [[INC_I:%.*]] = add i32 [[N_VAL_RELOAD_I]], 1
46 ; CHECK-NEXT: store i32 [[INC_I]], i32* [[N_VAL_SPILL_ADDR_I]], align 4, !alias.scope !0
47 ; CHECK-NEXT: call void @print(i32 [[INC_I]]), !noalias !0
48 ; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META3:![0-9]+]])
49 ; CHECK-NEXT: [[N_VAL_RELOAD_I3:%.*]] = load i32, i32* [[N_VAL_SPILL_ADDR_I]], align 4, !alias.scope !3
50 ; CHECK-NEXT: [[INC_I4:%.*]] = add i32 [[N_VAL_RELOAD_I3]], 1
51 ; CHECK-NEXT: call void @print(i32 [[INC_I4]]), !noalias !3
52 ; CHECK-NEXT: ret i32 0
55 %0 = alloca [8 x i8], align 4
56 %buffer = bitcast [8 x i8]* %0 to i8*
57 %prepare = call i8* @llvm.coro.prepare.retcon(i8* bitcast (i8* (i8*, i32)* @f to i8*))
58 %f = bitcast i8* %prepare to i8* (i8*, i32)*
59 %cont0 = call i8* %f(i8* %buffer, i32 4)
60 %cont0.cast = bitcast i8* %cont0 to i8* (i8*, i1)*
61 %cont1 = call i8* %cont0.cast(i8* %buffer, i1 zeroext false)
62 %cont1.cast = bitcast i8* %cont1 to i8* (i8*, i1)*
63 %cont2 = call i8* %cont1.cast(i8* %buffer, i1 zeroext false)
64 %cont2.cast = bitcast i8* %cont2 to i8* (i8*, i1)*
65 call i8* %cont2.cast(i8* %buffer, i1 zeroext true)
69 ; Unfortunately, we don't seem to fully optimize this right now due
70 ; to some sort of phase-ordering thing.
72 define hidden { i8*, i8* } @g(i8* %buffer, i16* %ptr) {
74 ; CHECK-NEXT: coro.return:
75 ; CHECK-NEXT: [[TMP0:%.*]] = tail call i8* @allocate(i32 8) #[[ATTR0:[0-9]+]]
76 ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i8* [[BUFFER:%.*]] to i8**
77 ; CHECK-NEXT: store i8* [[TMP0]], i8** [[TMP1]], align 8
78 ; CHECK-NEXT: [[PTR_SPILL_ADDR:%.*]] = bitcast i8* [[TMP0]] to i16**
79 ; CHECK-NEXT: store i16* [[PTR:%.*]], i16** [[PTR_SPILL_ADDR]], align 8
80 ; CHECK-NEXT: [[TMP2:%.*]] = bitcast i16* [[PTR]] to i8*
81 ; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { i8*, i8* } { i8* bitcast ({ i8*, i8* } (i8*, i1)* @g.resume.0 to i8*), i8* undef }, i8* [[TMP2]], 1
82 ; CHECK-NEXT: ret { i8*, i8* } [[TMP3]]
85 %id = call token @llvm.coro.id.retcon(i32 8, i32 4, i8* %buffer, i8* bitcast ({ i8*, i8* } (i8*, i1)* @g_prototype to i8*), i8* bitcast (i8* (i32)* @allocate to i8*), i8* bitcast (void (i8*)* @deallocate to i8*))
86 %hdl = call i8* @llvm.coro.begin(token %id, i8* null)
90 %ptr2 = bitcast i16* %ptr to i8*
91 %unwind0 = call i1 (...) @llvm.coro.suspend.retcon.i1(i8* %ptr2)
92 br i1 %unwind0, label %cleanup, label %resume
98 call i1 @llvm.coro.end(i8* %hdl, i1 0)
102 declare token @llvm.coro.id.retcon(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 i8* @prototype(i8*, i1 zeroext)
109 declare {i8*,i8*} @g_prototype(i8*, i1 zeroext)
111 declare noalias i8* @allocate(i32 %size)
112 declare void @deallocate(i8* %ptr)
114 declare void @print(i32)