1; RUN: opt < %s -rewrite-statepoints-for-gc -S 2>&1 | FileCheck %s
2
3; The rewriting needs to make %obj loop variant by inserting a phi
4; of the original value and it's relocation.
5
6declare i64 addrspace(1)* @generate_obj() "gc-leaf-function"
7
8declare void @use_obj(i64 addrspace(1)*) "gc-leaf-function"
9
10define void @def_use_safepoint() gc "statepoint-example" {
11; CHECK-LABEL: def_use_safepoint
12; CHECK: phi i64 addrspace(1)*
13; CHECK-DAG: [ %obj.relocated.casted, %loop ]
14; CHECK-DAG: [ %obj, %entry ]
15entry:
16  %obj = call i64 addrspace(1)* @generate_obj()
17  br label %loop
18
19loop:                                             ; preds = %loop, %entry
20  call void @use_obj(i64 addrspace(1)* %obj)
21  call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
22  br label %loop
23}
24
25declare void @do_safepoint()
26
27declare void @parse_point(i64 addrspace(1)*)
28
29define i64 addrspace(1)* @test1(i32 %caller, i8 addrspace(1)* %a, i8 addrspace(1)* %b, i32 %unknown) gc "statepoint-example" {
30; CHECK-LABEL: test1
31entry:
32  br i1 undef, label %left, label %right
33
34left:                                             ; preds = %entry
35; CHECK: left:
36; CHECK-NEXT: %a.cast = bitcast i8 addrspace(1)* %a to i64 addrspace(1)*
37; CHECK-NEXT: [[CAST_L:%.*]] = bitcast i8 addrspace(1)* %a to i64 addrspace(1)*
38; Our safepoint placement pass calls removeUnreachableBlocks, which does a bunch
39; of simplifications to branch instructions.  This bug is visible only when
40; there are multiple branches into the same block from the same predecessor, and
41; the following ceremony is to make that artefact survive a call to
42; removeUnreachableBlocks.  As an example, "br i1 undef, label %merge, label %merge"
43; will get simplified to "br label %merge" by removeUnreachableBlocks.
44  %a.cast = bitcast i8 addrspace(1)* %a to i64 addrspace(1)*
45  switch i32 %unknown, label %right [
46    i32 0, label %merge
47    i32 1, label %merge
48    i32 5, label %merge
49    i32 3, label %right
50  ]
51
52right:                                            ; preds = %left, %left, %entry
53; CHECK: right:
54; CHECK-NEXT: %b.cast = bitcast i8 addrspace(1)* %b to i64 addrspace(1)*
55; CHECK-NEXT: [[CAST_R:%.*]] = bitcast i8 addrspace(1)* %b to i64 addrspace(1)*
56  %b.cast = bitcast i8 addrspace(1)* %b to i64 addrspace(1)*
57  br label %merge
58
59merge:                                            ; preds = %right, %left, %left, %left
60; CHECK: merge:
61; CHECK-NEXT: %value.base = phi i64 addrspace(1)* [ [[CAST_L]], %left ], [ [[CAST_L]], %left ], [ [[CAST_L]], %left ], [ [[CAST_R]], %right ], !is_base_value !0
62  %value = phi i64 addrspace(1)* [ %a.cast, %left ], [ %a.cast, %left ], [ %a.cast, %left ], [ %b.cast, %right ]
63  call void @parse_point(i64 addrspace(1)* %value) [ "deopt"(i32 0, i32 0, i32 0, i32 0, i32 0) ]
64  ret i64 addrspace(1)* %value
65}
66
67;; The purpose of this test is to ensure that when two live values share a
68;;  base defining value with inherent conflicts, we end up with a *single*
69;;  base phi/select per such node.  This is testing an optimization, not a
70;;  fundemental correctness criteria
71define void @test2(i1 %cnd, i64 addrspace(1)* %base_obj, i64 addrspace(1)* %base_arg2) gc "statepoint-example" {
72; CHECK-LABEL: @test2
73entry:
74  %obj = getelementptr i64, i64 addrspace(1)* %base_obj, i32 1
75  br label %loop
76; CHECK-LABEL: loop
77; CHECK:   %current.base = phi i64 addrspace(1)*
78; CHECK-DAG: [ %base_obj, %entry ]
79
80; Given the two selects are equivelent, so are their base phis - ideally,
81; we'd have commoned these, but that's a missed optimization, not correctness.
82; CHECK-DAG: [ [[DISCARD:%.*.base.relocated.casted]], %loop ]
83; CHECK-NOT: extra.base
84; CHECK: next.base = select
85; CHECK: next = select
86; CHECK: extra2.base = select
87; CHECK: extra2 = select
88; CHECK: statepoint
89;; Both 'next' and 'extra2' are live across the backedge safepoint...
90
91loop:                                             ; preds = %loop, %entry
92  %current = phi i64 addrspace(1)* [ %obj, %entry ], [ %next, %loop ]
93  %extra = phi i64 addrspace(1)* [ %obj, %entry ], [ %extra2, %loop ]
94  %nexta = getelementptr i64, i64 addrspace(1)* %current, i32 1
95  %next = select i1 %cnd, i64 addrspace(1)* %nexta, i64 addrspace(1)* %base_arg2
96  %extra2 = select i1 %cnd, i64 addrspace(1)* %nexta, i64 addrspace(1)* %base_arg2
97  call void @foo() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
98  br label %loop
99}
100
101define i64 addrspace(1)* @test3(i1 %cnd, i64 addrspace(1)* %obj, i64 addrspace(1)* %obj2) gc "statepoint-example" {
102; CHECK-LABEL: @test3
103entry:
104  br i1 %cnd, label %merge, label %taken
105
106taken:                                            ; preds = %entry
107  br label %merge
108
109merge:                                            ; preds = %taken, %entry
110; CHECK-LABEL: merge:
111; CHECK-NEXT: phi
112; CHECK-NEXT: phi
113; CHECK-NEXT: gc.statepoint
114  %bdv = phi i64 addrspace(1)* [ %obj, %entry ], [ %obj2, %taken ]
115  call void @foo() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
116  ret i64 addrspace(1)* %bdv
117}
118
119define i64 addrspace(1)* @test4(i1 %cnd, i64 addrspace(1)* %obj, i64 addrspace(1)* %obj2) gc "statepoint-example" {
120; CHECK-LABEL: @test4
121entry:
122  br i1 %cnd, label %merge, label %taken
123
124taken:                                            ; preds = %entry
125  br label %merge
126
127merge:                                            ; preds = %taken, %entry
128; CHECK-LABEL: merge:
129; CHECK-NEXT: phi
130; CHECK-NEXT: gc.statepoint
131  %bdv = phi i64 addrspace(1)* [ %obj, %entry ], [ %obj, %taken ]
132  call void @foo() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
133  ret i64 addrspace(1)* %bdv
134}
135
136define i64 addrspace(1)* @test5(i1 %cnd, i64 addrspace(1)* %obj, i64 addrspace(1)* %obj2) gc "statepoint-example" {
137; CHECK-LABEL: @test5
138entry:
139  br label %merge
140
141merge:                                            ; preds = %merge, %entry
142; CHECK-LABEL: merge:
143; CHECK-NEXT: phi
144; CHECK-NEXT: phi
145; CHECK-NEXT: br i1
146  %bdv = phi i64 addrspace(1)* [ %obj, %entry ], [ %obj2, %merge ]
147  br i1 %cnd, label %merge, label %next
148
149next:                                             ; preds = %merge
150  call void @foo() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
151  ret i64 addrspace(1)* %bdv
152}
153
154declare void @foo()
155