| 1 |
; RUN: opt -S -passes=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 |
|
| 7 |
declare void @bar() |
| 8 |
|
| 9 |
declare 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. |
| 14 |
define void @test_invariant_load(i1 %c) gc "statepoint-example" { |
| 15 |
; CHECK-LABEL: @test_invariant_load |
| 16 |
; CHECK: %loadedval = load i32, ptr addrspace(1) %baseaddr, align 8, !range !0 |
| 17 |
bb: |
| 18 |
br label %outerloopHdr |
| 19 |
|
| 20 |
outerloopHdr: ; preds = %bb6, %bb |
| 21 |
%baseaddr = phi ptr 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, ptr addrspace(1) %baseaddr, align 8, !range !0, !invariant.load !1 |
| 24 |
br label %innerloopHdr |
| 25 |
|
| 26 |
innerloopHdr: ; preds = %innerlooplatch, %outerloopHdr |
| 27 |
%tmp4 = phi ptr addrspace(1) [ %baseaddr, %outerloopHdr ], [ %gep, %innerlooplatch ] |
| 28 |
br label %innermostloophdr |
| 29 |
|
| 30 |
innermostloophdr: ; preds = %bb6, %innerloopHdr |
| 31 |
br i1 %c, label %exitblock, label %bb6 |
| 32 |
|
| 33 |
bb6: ; preds = %innermostloophdr |
| 34 |
switch i32 undef, label %innermostloophdr [ |
| 35 |
i32 0, label %outerloopHdr |
| 36 |
i32 1, label %innerlooplatch |
| 37 |
] |
| 38 |
|
| 39 |
innerlooplatch: ; preds = %bb6 |
| 40 |
call void @bar() |
| 41 |
%gep = getelementptr inbounds i32, ptr addrspace(1) %tmp4, i64 8 |
| 42 |
br label %innerloopHdr |
| 43 |
|
| 44 |
exitblock: ; preds = %innermostloophdr |
| 45 |
%tmp13 = add i32 42, %loadedval |
| 46 |
call void @baz(i32 %tmp13) |
| 47 |
unreachable |
| 48 |
} |
| 49 |
|
| 50 |
; drop the noalias metadata. |
| 51 |
define void @test_noalias(i32 %x, ptr addrspace(1) %p, ptr addrspace(1) %q) gc "statepoint-example" { |
| 52 |
; CHECK-LABEL: test_noalias |
| 53 |
; CHECK: %y = load i32, ptr addrspace(1) %q, align 16 |
| 54 |
; CHECK: gc.statepoint |
| 55 |
; CHECK: %p.relocated |
| 56 |
; CHECK-NEXT: store i32 %x, ptr addrspace(1) %p.relocated, align 16 |
| 57 |
entry: |
| 58 |
%y = load i32, ptr addrspace(1) %q, align 16, !noalias !5 |
| 59 |
call void @baz(i32 %x) |
| 60 |
store i32 %x, ptr addrspace(1) %p, align 16, !noalias !5 |
| 61 |
ret void |
| 62 |
} |
| 63 |
|
| 64 |
; drop the dereferenceable metadata |
| 65 |
define void @test_dereferenceable(ptr addrspace(1) %p, i32 %x, ptr addrspace(1) %q) gc "statepoint-example" { |
| 66 |
; CHECK-LABEL: test_dereferenceable |
| 67 |
; CHECK: %v1 = load ptr addrspace(1), ptr addrspace(1) %p |
| 68 |
; CHECK-NEXT: %v2 = load i32, ptr addrspace(1) %v1 |
| 69 |
; CHECK: gc.statepoint |
| 70 |
%v1 = load ptr addrspace(1), ptr addrspace(1) %p, !dereferenceable !6 |
| 71 |
%v2 = load i32, ptr addrspace(1) %v1 |
| 72 |
call void @baz(i32 %x) |
| 73 |
store i32 %v2, ptr addrspace(1) %q, align 16 |
| 74 |
ret void |
| 75 |
} |
| 76 |
|
| 77 |
; invariant.start allows us to sink the load past the baz statepoint call into taken block, which is |
| 78 |
; incorrect. remove the invariant.start and RAUW undef. |
| 79 |
define void @test_inv_start(i1 %cond, ptr addrspace(1) %p, i32 %x, ptr addrspace(1) %q) gc "statepoint-example" { |
| 80 |
; CHECK-LABEL: test_inv_start |
| 81 |
; CHECK-NOT: invariant.start |
| 82 |
; CHECK: gc.statepoint |
| 83 |
%v1 = load ptr addrspace(1), ptr addrspace(1) %p |
| 84 |
%invst = call ptr @llvm.invariant.start.p1(i64 1, ptr addrspace(1) %v1) |
| 85 |
%v2 = load i32, ptr addrspace(1) %v1 |
| 86 |
call void @baz(i32 %x) |
| 87 |
br i1 %cond, label %taken, label %untaken |
| 88 |
|
| 89 |
taken: |
| 90 |
store i32 %v2, ptr addrspace(1) %q, align 16 |
| 91 |
call void @llvm.invariant.end.p1(ptr %invst, i64 4, ptr addrspace(1) %v1) |
| 92 |
ret void |
| 93 |
|
| 94 |
; CHECK-LABEL: untaken: |
| 95 |
; CHECK: gc.statepoint |
| 96 |
untaken: |
| 97 |
%foo = call i32 @escaping.invariant.start(ptr %invst) |
| 98 |
call void @dummy(i32 %foo) |
| 99 |
ret void |
| 100 |
} |
| 101 |
|
| 102 |
; invariant.start is removed and the uses are undef'ed. |
| 103 |
define void @test_inv_start2(i1 %cond, ptr addrspace(1) %p, i32 %x, ptr addrspace(1) %q) gc "statepoint-example" { |
| 104 |
; CHECK-LABEL: test_inv_start2 |
| 105 |
; CHECK-NOT: invariant.start |
| 106 |
; CHECK: gc.statepoint |
| 107 |
%v1 = load ptr addrspace(1), ptr addrspace(1) %p |
| 108 |
%invst = call ptr @llvm.invariant.start.p1(i64 1, ptr addrspace(1) %v1) |
| 109 |
%v2 = load i32, ptr addrspace(1) %v1 |
| 110 |
call void @baz(i32 %x) |
| 111 |
br i1 %cond, label %taken, label %untaken |
| 112 |
|
| 113 |
taken: |
| 114 |
store i32 %v2, ptr addrspace(1) %q, align 16 |
| 115 |
call void @llvm.invariant.end.p1(ptr %invst, i64 4, ptr addrspace(1) %v1) |
| 116 |
ret void |
| 117 |
|
| 118 |
untaken: |
| 119 |
ret void |
| 120 |
} |
| 121 |
declare ptr @llvm.invariant.start.p1(i64, ptr addrspace(1) nocapture) nounwind readonly |
| 122 |
declare void @llvm.invariant.end.p1(ptr, i64, ptr addrspace(1) nocapture) nounwind |
| 123 |
declare i32 @escaping.invariant.start(ptr) nounwind |
| 124 |
declare void @dummy(i32) |
| 125 |
declare token @llvm.experimental.gc.statepoint.p0(i64, i32, ptr, i32, i32, ...) |
| 126 |
|
| 127 |
; Function Attrs: nounwind readonly |
| 128 |
declare ptr addrspace(1) @llvm.experimental.gc.relocate.p1(token, i32, i32) #0 |
| 129 |
|
| 130 |
|
| 131 |
attributes #0 = { nounwind readonly } |
| 132 |
|
| 133 |
!0 = !{i32 0, i32 2147483647} |
| 134 |
!1 = !{} |
| 135 |
!2 = !{i32 10, i32 1} |
| 136 |
!3 = !{!3} |
| 137 |
!4 = !{!4, !3} |
| 138 |
!5 = !{!4} |
| 139 |
!6 = !{i64 8} |