MC/Mach-O/ARM: We always use the SECTDIFF reloc type on ARM, which is
[oota-llvm.git] / test / Transforms / SimplifyCFG / indirectbr.ll
1 ; RUN: opt -S -simplifycfg < %s | FileCheck %s
2
3 ; SimplifyCFG should eliminate redundant indirectbr edges.
4
5 ; CHECK: indbrtest0
6 ; CHECK: indirectbr i8* %t, [label %BB0, label %BB1, label %BB2]
7 ; CHECK: %x = phi i32 [ 0, %BB0 ], [ 1, %entry ]
8
9 declare void @foo()
10 declare void @A()
11 declare void @B(i32)
12 declare void @C()
13
14 define void @indbrtest0(i8** %P, i8** %Q) {
15 entry:
16   store i8* blockaddress(@indbrtest0, %BB0), i8** %P
17   store i8* blockaddress(@indbrtest0, %BB1), i8** %P
18   store i8* blockaddress(@indbrtest0, %BB2), i8** %P
19   call void @foo()
20   %t = load i8** %Q
21   indirectbr i8* %t, [label %BB0, label %BB1, label %BB2, label %BB0, label %BB1, label %BB2]
22 BB0:
23   call void @A()
24   br label %BB1
25 BB1:
26   %x = phi i32 [ 0, %BB0 ], [ 1, %entry ], [ 1, %entry ]
27   call void @B(i32 %x)
28   ret void
29 BB2:
30   call void @C()
31   ret void
32 }
33
34 ; SimplifyCFG should convert the indirectbr into a directbr. It would be even
35 ; better if it removed the branch altogether, but simplifycfdg currently misses
36 ; that because the predecessor is the entry block.
37
38 ; CHECK: indbrtest1
39 ; CHECK: br label %BB0
40
41 define void @indbrtest1(i8** %P, i8** %Q) {
42 entry:
43   store i8* blockaddress(@indbrtest1, %BB0), i8** %P
44   call void @foo()
45   %t = load i8** %Q
46   indirectbr i8* %t, [label %BB0, label %BB0]
47 BB0:
48   call void @A()
49   ret void
50 }
51
52 ; SimplifyCFG should notice that BB0 does not have its address taken and
53 ; remove it from entry's successor list.
54
55 ; CHECK: indbrtest2
56 ; CHECK: entry:
57 ; CHECK-NEXT: unreachable
58
59 define void @indbrtest2(i8* %t) {
60 entry:
61   indirectbr i8* %t, [label %BB0, label %BB0]
62 BB0:
63   ret void
64 }
65
66
67 ; Make sure the blocks in the next few tests aren't trivially removable as
68 ; successors by taking their addresses.
69
70 @anchor = constant [13 x i8*] [
71   i8* blockaddress(@indbrtest3, %L1), i8* blockaddress(@indbrtest3, %L2), i8* blockaddress(@indbrtest3, %L3),
72   i8* blockaddress(@indbrtest4, %L1), i8* blockaddress(@indbrtest4, %L2), i8* blockaddress(@indbrtest4, %L3),
73   i8* blockaddress(@indbrtest5, %L1), i8* blockaddress(@indbrtest5, %L2), i8* blockaddress(@indbrtest5, %L3), i8* blockaddress(@indbrtest5, %L4),
74   i8* blockaddress(@indbrtest6, %L1), i8* blockaddress(@indbrtest6, %L2), i8* blockaddress(@indbrtest6, %L3)
75 ]
76
77 ; SimplifyCFG should turn the indirectbr into a conditional branch on the
78 ; condition of the select.
79
80 ; CHECK: @indbrtest3
81 ; CHECK-NEXT: entry:
82 ; CHECK-NEXT: br i1 %cond, label %L1, label %L2
83 ; CHECK-NOT: indirectbr
84 ; CHECK-NOT: br
85 ; CHECK-NOT: L3:
86 define void @indbrtest3(i1 %cond, i8* %address) nounwind {
87 entry:
88   %indirect.goto.dest = select i1 %cond, i8* blockaddress(@indbrtest3, %L1), i8* blockaddress(@indbrtest3, %L2)
89   indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
90
91 L1:
92   call void @A()
93   ret void
94 L2:
95   call void @C()
96   ret void
97 L3:
98   call void @foo()
99   ret void
100 }
101
102 ; SimplifyCFG should turn the indirectbr into an unconditional branch to the
103 ; only possible destination.
104 ; As in @indbrtest1, it should really remove the branch entirely, but it doesn't
105 ; because it's in the entry block.
106
107 ; CHECK: @indbrtest4
108 ; CHECK-NEXT: entry:
109 ; CHECK-NEXT: br label %L1
110 define void @indbrtest4(i1 %cond) nounwind {
111 entry:
112   %indirect.goto.dest = select i1 %cond, i8* blockaddress(@indbrtest4, %L1), i8* blockaddress(@indbrtest4, %L1)
113   indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
114
115 L1:
116   call void @A()
117   ret void
118 L2:
119   call void @C()
120   ret void
121 L3:
122   call void @foo()
123   ret void
124 }
125
126 ; SimplifyCFG should turn the indirectbr into an unreachable because neither
127 ; destination is listed as a successor.
128
129 ; CHECK: @indbrtest5
130 ; CHECK-NEXT: entry:
131 ; CHECK-NEXT: unreachable
132 ; CHECK-NEXT: }
133 define void @indbrtest5(i1 %cond, i8* %anchor) nounwind {
134 entry:
135   %indirect.goto.dest = select i1 %cond, i8* blockaddress(@indbrtest5, %L1), i8* blockaddress(@indbrtest5, %L2)
136 ; This needs to have more than one successor for this test, otherwise it gets
137 ; replaced with an unconditional branch to the single successor.
138   indirectbr i8* %indirect.goto.dest, [label %L3, label %L4]
139
140 L1:
141   call void @A()
142   ret void
143 L2:
144   call void @C()
145   ret void
146 L3:
147   call void @foo()
148   ret void
149 L4:
150   call void @foo()
151
152 ; This keeps blockaddresses not otherwise listed as successors from being zapped
153 ; before SimplifyCFG even looks at the indirectbr.
154   indirectbr i8* %anchor, [label %L1, label %L2]
155 }
156
157 ; The same as above, except the selected addresses are equal.
158
159 ; CHECK: @indbrtest6
160 ; CHECK-NEXT: entry:
161 ; CHECK-NEXT: unreachable
162 ; CHECK-NEXT: }
163 define void @indbrtest6(i1 %cond, i8* %anchor) nounwind {
164 entry:
165   %indirect.goto.dest = select i1 %cond, i8* blockaddress(@indbrtest6, %L1), i8* blockaddress(@indbrtest6, %L1)
166 ; This needs to have more than one successor for this test, otherwise it gets
167 ; replaced with an unconditional branch to the single successor.
168   indirectbr i8* %indirect.goto.dest, [label %L2, label %L3]
169
170 L1:
171   call void @A()
172   ret void
173 L2:
174   call void @C()
175   ret void
176 L3:
177   call void @foo()
178
179 ; This keeps blockaddresses not otherwise listed as successors from being zapped
180 ; before SimplifyCFG even looks at the indirectbr.
181   indirectbr i8* %anchor, [label %L1, label %L2]
182 }