1; RUN: opt -S -rewrite-statepoints-for-gc < %s | FileCheck %s
2
3; constants don't get relocated.
4@G = addrspace(1) global i8 5
5
6declare void @foo()
7
8define i8 @test() gc "statepoint-example" {
9; CHECK-LABEL: @test
10; CHECK: gc.statepoint
11; CHECK-NEXT: load i8, i8 addrspace(1)* inttoptr (i64 15 to i8 addrspace(1)*)
12; Mostly just here to show reasonable code test can come from.
13entry:
14  call void @foo() [ "deopt"() ]
15  %res = load i8, i8 addrspace(1)* inttoptr (i64 15 to i8 addrspace(1)*)
16  ret i8 %res
17}
18
19define i8 @test2(i8 addrspace(1)* %p) gc "statepoint-example" {
20; CHECK-LABEL: @test2
21; CHECK: gc.statepoint
22; CHECK-NEXT: gc.relocate
23; CHECK-NEXT: icmp
24; Globals don't move and thus don't get relocated
25entry:
26  call void @foo() [ "deopt"() ]
27  %cmp = icmp eq i8 addrspace(1)* %p, null
28  br i1 %cmp, label %taken, label %not_taken
29
30taken:                                            ; preds = %not_taken, %entry
31  ret i8 0
32
33not_taken:                                        ; preds = %entry
34  %cmp2 = icmp ne i8 addrspace(1)* %p, null
35  br i1 %cmp2, label %taken, label %dead
36
37dead:                                             ; preds = %not_taken
38  %addr = getelementptr i8, i8 addrspace(1)* %p, i32 15
39  %res = load i8, i8 addrspace(1)* %addr
40  ret i8 %res
41}
42
43define i8 @test3(i1 %always_true) gc "statepoint-example" {
44; CHECK-LABEL: @test3
45; CHECK: gc.statepoint
46; CHECK-NEXT: load i8, i8 addrspace(1)* @G
47entry:
48  call void @foo() [ "deopt"() ]
49  %res = load i8, i8 addrspace(1)* @G, align 1
50  ret i8 %res
51}
52
53; Even for source languages without constant references, we can
54; see constants can show up along paths where the value is dead.
55; This is particular relevant when computing bases of PHIs.
56define i8 addrspace(1)* @test4(i8 addrspace(1)* %p) gc "statepoint-example" {
57; CHECK-LABEL: @test4
58entry:
59  %is_null = icmp eq i8 addrspace(1)* %p, null
60  br i1 %is_null, label %split, label %join
61
62split:
63  call void @foo()
64  %arg_value_addr.i = getelementptr inbounds i8, i8 addrspace(1)* %p, i64 8
65  %arg_value_addr_casted.i = bitcast i8 addrspace(1)* %arg_value_addr.i to i8 addrspace(1)* addrspace(1)*
66  br label %join
67
68join:
69; CHECK-LABEL: join
70; CHECK: %addr2.base =
71  %addr2 = phi i8 addrspace(1)* addrspace(1)* [ %arg_value_addr_casted.i, %split ], [ inttoptr (i64 8 to i8 addrspace(1)* addrspace(1)*), %entry ]
72  ;; NOTE: This particular example can be jump-threaded, but in general,
73  ;; we can't, and have to deal with the resulting IR.
74  br i1 %is_null, label %early-exit, label %use
75
76early-exit:
77  ret i8 addrspace(1)* null
78
79use:
80; CHECK-LABEL: use:
81; CHECK: gc.statepoint
82; CHECK: gc.relocate
83  call void @foo()
84  %res = load i8 addrspace(1)*, i8 addrspace(1)* addrspace(1)* %addr2, align 1
85  ret i8 addrspace(1)* %res
86}
87
88; Globals don't move and thus don't get relocated
89define i8 addrspace(1)* @test5(i1 %always_true) gc "statepoint-example" {
90; CHECK-LABEL: @test5
91; CHECK: gc.statepoint
92; CHECK-NEXT: %res = extractelement <2 x i8 addrspace(1)*> <i8 addrspace(1)* @G, i8 addrspace(1)* @G>, i32 0
93entry:
94  call void @foo()
95  %res = extractelement <2 x i8 addrspace(1)*> <i8 addrspace(1)* @G, i8 addrspace(1)* @G>, i32 0
96  ret i8 addrspace(1)* %res
97}
98