xref: /llvm-project/llvm/test/Transforms/Coroutines/coro-split-musttail7.ll (revision 3bb39690d729d85cd93c9dd6e750d82d6f367541)
1; Tests that sinked lifetime markers wouldn't provent optimization
2; to convert a coro.await.suspend.handle call to a musttail call.
3; The difference between this and coro-split-musttail5.ll and coro-split-musttail6.ll
4; is that this contains dead instruction generated during the transformation,
5; which makes the optimization harder.
6; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s
7; RUN: opt < %s -passes='pgo-instr-gen,cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s
8
9define i64 @g() #0 {
10entry:
11  %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
12  %alloc = call ptr @malloc(i64 16) #3
13  %alloc.var = alloca i64
14  call void @llvm.lifetime.start.p0(i64 1, ptr %alloc.var)
15  %vFrame = call noalias nonnull ptr @llvm.coro.begin(token %id, ptr %alloc)
16
17  %save = call token @llvm.coro.save(ptr null)
18  %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
19
20  switch i8 %suspend, label %exit [
21    i8 0, label %await.suspend
22    i8 1, label %exit
23  ]
24await.suspend:
25  %save2 = call token @llvm.coro.save(ptr null)
26  call void @llvm.coro.await.suspend.handle(ptr null, ptr null, ptr @await_suspend_function)
27  %suspend2 = call i8 @llvm.coro.suspend(token %save2, i1 false)
28
29  ; These (non-trivially) dead instructions are in the way.
30  %gep = getelementptr inbounds i64, ptr %alloc.var, i32 0
31  %foo = ptrtoint ptr %gep to i64
32
33  switch i8 %suspend2, label %exit [
34    i8 0, label %await.ready
35    i8 1, label %exit
36  ]
37await.ready:
38  call void @consume(ptr %alloc.var)
39  call void @llvm.lifetime.end.p0(i64 1, ptr %alloc.var)
40  br label %exit
41exit:
42  %result = phi i64 [0, %entry], [0, %entry], [%foo, %await.suspend], [%foo, %await.suspend], [%foo, %await.ready]
43  call i1 @llvm.coro.end(ptr null, i1 false, token none)
44  ret i64 %result
45}
46
47; Verify that in the resume part resume call is marked with musttail.
48; CHECK-LABEL: @g.resume(
49; CHECK:         %[[FRAME:[0-9]+]] = call ptr @await_suspend_function(ptr null, ptr null)
50; CHECK:         %[[RESUMEADDR:[0-9]+]] = call ptr @llvm.coro.subfn.addr(ptr %[[FRAME]], i8 0)
51; CHECK:         musttail call fastcc void %[[RESUMEADDR]](ptr %[[FRAME]])
52; CHECK-NEXT:    ret void
53
54; It has a cleanup bb.
55define void @f() #0 {
56entry:
57  %id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
58  %alloc = call ptr @malloc(i64 16) #3
59  %alloc.var = alloca i64
60  call void @llvm.lifetime.start.p0(i64 1, ptr %alloc.var)
61  %vFrame = call noalias nonnull ptr @llvm.coro.begin(token %id, ptr %alloc)
62
63  %save = call token @llvm.coro.save(ptr null)
64  %suspend = call i8 @llvm.coro.suspend(token %save, i1 false)
65
66  switch i8 %suspend, label %exit [
67    i8 0, label %await.suspend
68    i8 1, label %exit
69  ]
70await.suspend:
71  %save2 = call token @llvm.coro.save(ptr null)
72  call void @llvm.coro.await.suspend.handle(ptr null, ptr null, ptr @await_suspend_function)
73  %suspend2 = call i8 @llvm.coro.suspend(token %save2, i1 false)
74  switch i8 %suspend2, label %exit [
75    i8 0, label %await.ready
76    i8 1, label %cleanup
77  ]
78await.ready:
79  call void @consume(ptr %alloc.var)
80  call void @llvm.lifetime.end.p0(i64 1, ptr %alloc.var)
81  br label %exit
82
83cleanup:
84  %free.handle = call ptr @llvm.coro.free(token %id, ptr %vFrame)
85  %.not = icmp eq ptr %free.handle, null
86  br i1 %.not, label %exit, label %coro.free
87
88coro.free:
89  call void @delete(ptr nonnull %free.handle) #2
90  br label %exit
91
92exit:
93  call i1 @llvm.coro.end(ptr null, i1 false, token none)
94  ret void
95}
96
97; Verify that in the resume part resume call is marked with musttail.
98; CHECK-LABEL: @f.resume(
99; CHECK:         %[[FRAME:[0-9]+]] = call ptr @await_suspend_function(ptr null, ptr null)
100; CHECK:         %[[RESUMEADDR:[0-9]+]] = call ptr @llvm.coro.subfn.addr(ptr %[[FRAME]], i8 0)
101; CHECK:         musttail call fastcc void %[[RESUMEADDR]](ptr %[[FRAME]])
102; CHECK-NEXT:    ret void
103
104declare token @llvm.coro.id(i32, ptr readnone, ptr nocapture readonly, ptr) #1
105declare i1 @llvm.coro.alloc(token) #2
106declare i64 @llvm.coro.size.i64() #3
107declare ptr @llvm.coro.begin(token, ptr writeonly) #2
108declare token @llvm.coro.save(ptr) #2
109declare ptr @llvm.coro.frame() #3
110declare i8 @llvm.coro.suspend(token, i1) #2
111declare ptr @llvm.coro.free(token, ptr nocapture readonly) #1
112declare i1 @llvm.coro.end(ptr, i1, token) #2
113declare ptr @llvm.coro.subfn.addr(ptr nocapture readonly, i8) #1
114declare ptr @malloc(i64)
115declare void @delete(ptr nonnull) #2
116declare void @consume(ptr)
117declare void @llvm.lifetime.start.p0(i64, ptr nocapture)
118declare void @llvm.lifetime.end.p0(i64, ptr nocapture)
119declare ptr @await_suspend_function(ptr %awaiter, ptr %hdl)
120
121attributes #0 = { presplitcoroutine }
122attributes #1 = { argmemonly nounwind readonly }
123attributes #2 = { nounwind }
124attributes #3 = { nounwind readnone }
125