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
78declare token @llvm.experimental.gc.statepoint.p0f_isVoidi32f(i64, i32, void (i32)*, i32, i32, ...)
79
80; Function Attrs: nounwind readonly
81declare i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token, i32, i32) #0
82
83declare token @llvm.experimental.gc.statepoint.p0f_isVoidf(i64, i32, void ()*, i32, i32, ...)
84
85attributes #0 = { nounwind readonly }
86
87!0 = !{i32 0, i32 2147483647}
88!1 = !{}
89!2 = !{i32 10, i32 1}
90!3 = !{!3}
91!4 = !{!4}
92!5 = !{i64 8}
93