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