xref: /llvm-project/llvm/test/Transforms/Util/strip-gc-relocates.ll (revision 90ba33099cbb17e7c159e9ebc5a512037db99d6d)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -passes=strip-gc-relocates,instcombine < %s | FileCheck %s
3; test utility/debugging pass which removes gc.relocates, inserted by -rewrite-statepoints-for-gc
4declare void @use_obj32(ptr addrspace(1)) "gc-leaf-function"
5
6declare void @g()
7declare token @llvm.experimental.gc.statepoint.p0(i64, i32, ptr, i32, i32, ...)
8declare ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token, i32, i32) #0
9declare void @do_safepoint()
10
11declare ptr addrspace(1) @new_instance() #1
12
13
14; Simple case: remove gc.relocate
15define ptr addrspace(1) @test1(ptr addrspace(1) %arg) gc "statepoint-example" {
16; CHECK-LABEL: @test1(
17; CHECK-NEXT:  entry:
18; CHECK-NEXT:    [[STATEPOINT_TOKEN:%.*]] = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr nonnull elementtype(void ()) @g, i32 0, i32 0, i32 0, i32 0) [ "deopt"(i32 100), "gc-live"() ]
19; CHECK-NEXT:    ret ptr addrspace(1) [[ARG:%.*]]
20;
21entry:
22  %statepoint_token = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr elementtype(void ()) @g, i32 0, i32 0, i32 0, i32 0) ["gc-live"(ptr addrspace(1) %arg), "deopt" (i32 100)]
23  %arg.relocated = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token, i32 0, i32 0) ; (%arg, %arg)
24  ret ptr addrspace(1) %arg.relocated
25}
26
27; Remove gc.relocates in presence of nested relocates.
28define void @test2(ptr addrspace(1) %base) gc "statepoint-example" {
29; CHECK-LABEL: @test2(
30; CHECK-NEXT:  entry:
31; CHECK-NEXT:    [[PTR_GEP1:%.*]] = getelementptr i8, ptr addrspace(1) [[BASE:%.*]], i64 120
32; CHECK-NEXT:    [[STATEPOINT_TOKEN:%.*]] = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr nonnull elementtype(void ()) @do_safepoint, i32 0, i32 0, i32 0, i32 0) [ "gc-live"() ]
33; CHECK-NEXT:    call void @use_obj32(ptr addrspace(1) [[PTR_GEP1]])
34; CHECK-NEXT:    [[STATEPOINT_TOKEN1:%.*]] = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr nonnull elementtype(void ()) @do_safepoint, i32 0, i32 0, i32 0, i32 0) [ "gc-live"() ]
35; CHECK-NEXT:    call void @use_obj32(ptr addrspace(1) [[PTR_GEP1]])
36; CHECK-NEXT:    ret void
37;
38entry:
39  %ptr.gep = getelementptr i32, ptr addrspace(1) %base, i32 15
40  %ptr.gep1 = getelementptr i32, ptr addrspace(1) %ptr.gep, i32 15
41  %statepoint_token = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr elementtype(void ()) @do_safepoint, i32 0, i32 0, i32 0, i32 0) ["gc-live"(ptr addrspace(1) %ptr.gep1, ptr addrspace(1) %base)]
42  %ptr.gep1.relocated = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token, i32 1, i32 0) ; (%base, %ptr.gep1)
43  %base.relocated = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token, i32 1, i32 1) ; (%base, %base)
44  call void @use_obj32(ptr addrspace(1) %ptr.gep1.relocated)
45  %statepoint_token1 = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr elementtype(void ()) @do_safepoint, i32 0, i32 0, i32 0, i32 0) ["gc-live"(ptr addrspace(1) %ptr.gep1.relocated, ptr addrspace(1) %base.relocated)]
46  %ptr.gep1.relocated2 = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token1, i32 1, i32 0) ; (%base.relocated, %ptr.gep1.relocated)
47  %base.relocated3 = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token1, i32 1, i32 1) ; (%base.relocated, %base.relocated)
48  call void @use_obj32(ptr addrspace(1) %ptr.gep1.relocated2)
49  ret void
50}
51
52; landing pad gc.relocates removed by instcombine since it has no uses.
53define ptr addrspace(1) @test3(ptr addrspace(1) %arg) gc "statepoint-example" personality i32 8 {
54; CHECK-LABEL: @test3(
55; CHECK-NEXT:  entry:
56; CHECK-NEXT:    [[STATEPOINT_TOKEN:%.*]] = invoke token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr nonnull elementtype(void ()) @g, i32 0, i32 0, i32 0, i32 0) [ "deopt"(i32 100), "gc-live"() ]
57; CHECK-NEXT:    to label [[NORMAL_DEST:%.*]] unwind label [[UNWIND_DEST:%.*]]
58; CHECK:       normal_dest:
59; CHECK-NEXT:    ret ptr addrspace(1) [[ARG:%.*]]
60; CHECK:       unwind_dest:
61; CHECK-NEXT:    [[LPAD:%.*]] = landingpad token
62; CHECK-NEXT:    cleanup
63; CHECK-NEXT:    resume token undef
64;
65entry:
66  %statepoint_token = invoke token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr elementtype(void ()) @g, i32 0, i32 0, i32 0, i32 0) ["gc-live"(ptr addrspace(1) %arg), "deopt" (i32 100)]
67  to label %normal_dest unwind label %unwind_dest
68
69normal_dest:                                      ; preds = %entry
70  %arg.relocated1 = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token, i32 0, i32 0) ; (%arg, %arg)
71  ret ptr addrspace(1) %arg.relocated1
72
73unwind_dest:                                      ; preds = %entry
74  %lpad = landingpad token
75  cleanup
76  %arg.relocated = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %lpad, i32 0, i32 0) ; (%arg, %arg)
77  resume token undef
78}
79
80; in presence of phi
81define void @test4(i1 %cond) gc "statepoint-example" {
82; CHECK-LABEL: @test4(
83; CHECK-NEXT:  entry:
84; CHECK-NEXT:    [[BASE1:%.*]] = call ptr addrspace(1) @new_instance()
85; CHECK-NEXT:    [[BASE2:%.*]] = call ptr addrspace(1) @new_instance()
86; CHECK-NEXT:    br i1 [[COND:%.*]], label [[HERE:%.*]], label [[THERE:%.*]]
87; CHECK:       here:
88; CHECK-NEXT:    br label [[MERGE:%.*]]
89; CHECK:       there:
90; CHECK-NEXT:    br label [[MERGE]]
91; CHECK:       merge:
92; CHECK-NEXT:    [[BASEPHI_BASE:%.*]] = phi ptr addrspace(1) [ [[BASE1]], [[HERE]] ], [ [[BASE2]], [[THERE]] ], !is_base_value !0
93; CHECK-NEXT:    [[STATEPOINT_TOKEN:%.*]] = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr nonnull elementtype(void ()) @do_safepoint, i32 0, i32 0, i32 0, i32 0) [ "gc-live"() ]
94; CHECK-NEXT:    [[PTR_GEP_REMAT:%.*]] = getelementptr i8, ptr addrspace(1) [[BASEPHI_BASE]], i64 60
95; CHECK-NEXT:    call void @use_obj32(ptr addrspace(1) [[PTR_GEP_REMAT]])
96; CHECK-NEXT:    ret void
97;
98entry:
99  %base1 = call ptr addrspace(1) @new_instance()
100  %base2 = call ptr addrspace(1) @new_instance()
101  br i1 %cond, label %here, label %there
102
103here:                                             ; preds = %entry
104  br label %merge
105
106there:                                            ; preds = %entry
107  br label %merge
108
109merge:                                            ; preds = %there, %here
110  %basephi.base = phi ptr addrspace(1) [ %base1, %here ], [ %base2, %there ], !is_base_value !0
111  %basephi = phi ptr addrspace(1) [ %base1, %here ], [ %base2, %there ]
112  %ptr.gep = getelementptr i32, ptr addrspace(1) %basephi, i32 15
113  %statepoint_token = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr elementtype(void ()) @do_safepoint, i32 0, i32 0, i32 0, i32 0) ["gc-live"(ptr addrspace(1) %basephi.base)]
114  %basephi.base.relocated = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token, i32 0, i32 0) ; (%basephi.base, %basephi.base)
115  %ptr.gep.remat = getelementptr i32, ptr addrspace(1) %basephi.base.relocated, i32 15
116  call void @use_obj32(ptr addrspace(1) %ptr.gep.remat)
117  ret void
118}
119
120; The gc.relocate type is different from %arg, but removing the gc.relocate,
121; needs a bitcast to be added from ptr addrspace(1) to ptr addrspace(1)
122define ptr addrspace(1) @test5(ptr addrspace(1) %arg) gc "statepoint-example" {
123; CHECK-LABEL: @test5(
124; CHECK-NEXT:  entry:
125; CHECK-NEXT:    [[STATEPOINT_TOKEN:%.*]] = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr nonnull elementtype(void ()) @g, i32 0, i32 0, i32 0, i32 0) [ "deopt"(i32 100), "gc-live"() ]
126; CHECK-NEXT:    ret ptr addrspace(1) [[ARG:%.*]]
127;
128entry:
129  %statepoint_token = call token (i64, i32, ptr, i32, i32, ...) @llvm.experimental.gc.statepoint.p0(i64 2882400000, i32 0, ptr elementtype(void ()) @g, i32 0, i32 0, i32 0, i32 0) ["gc-live"(ptr addrspace(1) %arg), "deopt" (i32 100)]
130  %arg.relocated = call coldcc ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token %statepoint_token, i32 0, i32 0) ; (%arg, %arg)
131  ret ptr addrspace(1) %arg.relocated
132}
133
134attributes #0 = { nounwind readonly }
135attributes #1 = { nounwind "gc-leaf-function" }
136!0 = !{}
137