1; RUN: opt -S -rewrite-statepoints-for-gc < %s | FileCheck %s
2
3; This test checks that metadata that's invalid after RS4GC is dropped.
4; We can miscompile if optimizations scheduled after RS4GC uses the
5; metadata that's infact invalid.
6
7declare void @bar()
8
9declare void @baz(i32)
10; Confirm that loadedval instruction does not contain invariant.load metadata.
11; but contains the range metadata.
12; Since loadedval is not marked invariant, it will prevent incorrectly sinking
13; %loadedval in LICM and avoid creation of an unrelocated use of %baseaddr.
14define void @test_invariant_load() gc "statepoint-example" {
15; CHECK-LABEL: @test_invariant_load
16; CHECK: %loadedval = load i32, i32 addrspace(1)* %baseaddr, align 8, !range !0
17bb:
18  br label %outerloopHdr
19
20outerloopHdr:                                              ; preds = %bb6, %bb
21  %baseaddr = phi i32 addrspace(1)* [ undef, %bb ], [ %tmp4, %bb6 ]
22; LICM may sink this load to exit block after RS4GC because it's tagged invariant.
23  %loadedval = load i32, i32 addrspace(1)* %baseaddr, align 8, !range !0, !invariant.load !1
24  br label %innerloopHdr
25
26innerloopHdr:                                              ; preds = %innerlooplatch, %outerloopHdr
27  %tmp4 = phi i32 addrspace(1)* [ %baseaddr, %outerloopHdr ], [ %gep, %innerlooplatch ]
28  br label %innermostloophdr
29
30innermostloophdr:                                              ; preds = %bb6, %innerloopHdr
31  br i1 undef, label %exitblock, label %bb6
32
33bb6:                                              ; preds = %innermostloophdr
34  switch i32 undef, label %innermostloophdr [
35    i32 0, label %outerloopHdr
36    i32 1, label %innerlooplatch
37  ]
38
39innerlooplatch:                                              ; preds = %bb6
40  call void @bar()
41  %gep = getelementptr inbounds i32, i32 addrspace(1)* %tmp4, i64 8
42  br label %innerloopHdr
43
44exitblock:                                             ; preds = %innermostloophdr
45  %tmp13 = add i32 42, %loadedval
46  call void @baz(i32 %tmp13)
47  unreachable
48}
49
50; drop the noalias metadata.
51define void @test_noalias(i32 %x, i32 addrspace(1)* %p, i32 addrspace(1)* %q) gc "statepoint-example" {
52; CHECK-LABEL: test_noalias
53; CHECK: %y = load i32, i32 addrspace(1)* %q, align 16
54; CHECK: gc.statepoint
55; CHECK: %p.relocated
56; CHECK-NEXT: %p.relocated.casted = bitcast i8 addrspace(1)* %p.relocated to i32 addrspace(1)*
57; CHECK-NEXT: store i32 %x, i32 addrspace(1)* %p.relocated.casted, align 16
58entry:
59  %y = load i32, i32 addrspace(1)* %q, align 16, !noalias !3
60  call void @baz(i32 %x)
61  store i32 %x, i32 addrspace(1)* %p, align 16, !noalias !4
62  ret void
63}
64
65; drop the dereferenceable metadata
66define void @test_dereferenceable(i32 addrspace(1)* addrspace(1)* %p, i32 %x, i32 addrspace(1)* %q) gc "statepoint-example" {
67; CHECK-LABEL: test_dereferenceable
68; CHECK: %v1 = load i32 addrspace(1)*, i32 addrspace(1)* addrspace(1)* %p
69; CHECK-NEXT: %v2 = load i32, i32 addrspace(1)* %v1
70; CHECK: gc.statepoint
71  %v1 = load i32 addrspace(1)*, i32 addrspace(1)* addrspace(1)* %p, !dereferenceable !5
72  %v2 = load i32, i32 addrspace(1)* %v1
73  call void @baz(i32 %x)
74  store i32 %v2, i32 addrspace(1)* %q, align 16
75  ret void
76}
77
78; invariant.start allows us to sink the load past the baz statepoint call into taken block, which is
79; incorrect. remove the invariant.start and RAUW undef.
80define void @test_inv_start(i1 %cond, i32 addrspace(1)* addrspace(1)* %p, i32 %x, i32 addrspace(1)* %q) gc "statepoint-example" {
81; CHECK-LABEL: test_inv_start
82; CHECK-NOT: invariant.start
83; CHECK: gc.statepoint
84  %v1 = load i32 addrspace(1)*, i32 addrspace(1)* addrspace(1)* %p
85  %invst = call {}* @llvm.invariant.start.p1i32(i64 1, i32 addrspace(1)* %v1)
86  %v2 = load i32, i32 addrspace(1)* %v1
87  call void @baz(i32 %x)
88  br i1 %cond, label %taken, label %untaken
89
90taken:
91  store i32 %v2, i32 addrspace(1)* %q, align 16
92  call void @llvm.invariant.end.p1i32({}* %invst, i64 4, i32 addrspace(1)* %v1)
93  ret void
94
95; CHECK-LABEL: untaken:
96; CHECK: gc.statepoint
97untaken:
98  %foo = call i32 @escaping.invariant.start({}* %invst)
99  call void @dummy(i32 %foo)
100  ret void
101}
102
103; invariant.start is removed and the uses are undef'ed.
104define void @test_inv_start2(i1 %cond, i32 addrspace(1)* addrspace(1)* %p, i32 %x, i32 addrspace(1)* %q) gc "statepoint-example" {
105; CHECK-LABEL: test_inv_start2
106; CHECK-NOT: invariant.start
107; CHECK: gc.statepoint
108  %v1 = load i32 addrspace(1)*, i32 addrspace(1)* addrspace(1)* %p
109  %invst = call {}* @llvm.invariant.start.p1i32(i64 1, i32 addrspace(1)* %v1)
110  %v2 = load i32, i32 addrspace(1)* %v1
111  call void @baz(i32 %x)
112  br i1 %cond, label %taken, label %untaken
113
114taken:
115  store i32 %v2, i32 addrspace(1)* %q, align 16
116  call void @llvm.invariant.end.p1i32({}* %invst, i64 4, i32 addrspace(1)* %v1)
117  ret void
118
119untaken:
120  ret void
121}
122declare {}* @llvm.invariant.start.p1i32(i64, i32 addrspace(1)*  nocapture) nounwind readonly
123declare void @llvm.invariant.end.p1i32({}*, i64, i32 addrspace(1)* nocapture) nounwind
124declare i32 @escaping.invariant.start({}*) nounwind
125declare void @dummy(i32)
126declare token @llvm.experimental.gc.statepoint.p0f_isVoidi32f(i64, i32, void (i32)*, i32, i32, ...)
127
128; Function Attrs: nounwind readonly
129declare i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token, i32, i32) #0
130
131declare token @llvm.experimental.gc.statepoint.p0f_isVoidf(i64, i32, void ()*, i32, i32, ...)
132
133attributes #0 = { nounwind readonly }
134
135!0 = !{i32 0, i32 2147483647}
136!1 = !{}
137!2 = !{i32 10, i32 1}
138!3 = !{!3}
139!4 = !{!4}
140!5 = !{i64 8}
141