1 ; Check that we create copy the data from the alloca into the coroutine
2 ; frame slot if it was written to.
3 ; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s
5 define ptr @f() presplitcoroutine {
7 %a.addr = alloca i64 ; read-only before coro.begin
8 %a = load i64, ptr %a.addr ; cannot modify the value, don't need to copy
11 call void @use(ptr %x.addr) ; uses %x.addr before coro.begin
16 %flag = call i1 @check()
17 br i1 %flag, label %flag_true, label %flag_merge
20 call void @use(ptr %z.addr) ; conditionally used %z.addr
24 %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
25 %size = call i32 @llvm.coro.size.i32()
26 %alloc = call ptr @myAlloc(i32 %size)
27 %hdl = call ptr @llvm.coro.begin(token %id, ptr %alloc)
28 call void @llvm.memset.p0.i32(ptr %y.addr, i8 1, i32 4, i1 false)
29 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
30 switch i8 %0, label %suspend [i8 0, label %resume
33 call void @use(ptr %a.addr)
34 call void @use(ptr %x.addr)
35 call void @use(ptr %y.addr)
36 call void @use(ptr %z.addr)
40 %mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
41 call void @free(ptr %mem)
44 call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
48 ; See that we added both x and y to the frame.
49 ; CHECK: %f.Frame = type { ptr, ptr, i64, i64, i64, i64, i1 }
51 ; See that all of the uses prior to coro-begin stays put.
52 ; CHECK-LABEL: define ptr @f() {
54 ; CHECK-NEXT: %a.addr = alloca i64
55 ; CHECK-NEXT: %x.addr = alloca i64
56 ; CHECK-NEXT: call void @use(ptr %x.addr)
57 ; CHECK-NEXT: %z.addr = alloca i64
59 ; See that we only copy the x as y was not modified prior to coro.begin.
60 ; CHECK: store ptr @f.destroy, ptr %destroy.addr
61 ; The next 3 instructions are to copy data in %x.addr from stack to frame.
62 ; CHECK-NEXT: %0 = getelementptr inbounds %f.Frame, ptr %hdl, i32 0, i32 3
63 ; CHECK-NEXT: %1 = load i64, ptr %x.addr, align 4
64 ; CHECK-NEXT: store i64 %1, ptr %0, align 4
65 ; The next 3 instructions are to copy data in %z.addr from stack to frame.
66 ; CHECK-NEXT: [[T2:%.+]] = getelementptr inbounds %f.Frame, ptr %hdl, i32 0, i32 5
67 ; CHECK-NEXT: [[T3:%.+]] = load i64, ptr %z.addr, align 4
68 ; CHECK-NEXT: store i64 [[T3]], ptr [[T2]], align 4
69 ; The next instruction is to recreate %y.cast in the original IR.
70 ; CHECK-NEXT: %y.addr.reload.addr = getelementptr inbounds %f.Frame, ptr %hdl, i32 0, i32 4
71 ; CHECK-NEXT: call void @llvm.memset.p0.i32(ptr %y.addr.reload.addr, i8 1, i32 4, i1 false)
72 ; CHECK-NEXT: %index.addr1 = getelementptr inbounds nuw %f.Frame, ptr %hdl, i32 0, i32 6
73 ; CHECK-NEXT: store i1 false, ptr %index.addr1, align 1
74 ; CHECK-NEXT: ret ptr %hdl
77 declare ptr @llvm.coro.free(token, ptr)
78 declare i32 @llvm.coro.size.i32()
79 declare i8 @llvm.coro.suspend(token, i1)
80 declare void @llvm.coro.resume(ptr)
81 declare void @llvm.coro.destroy(ptr)
83 declare token @llvm.coro.id(i32, ptr, ptr, ptr)
84 declare i1 @llvm.coro.alloc(token)
85 declare ptr @llvm.coro.begin(token, ptr)
86 declare i1 @llvm.coro.end(ptr, i1, token)
88 declare void @llvm.memset.p0.i32(ptr, i8, i32, i1)
90 declare noalias ptr @myAlloc(i32)
91 declare void @use(ptr)
92 declare void @free(ptr)