1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s
4 ; SimplifyCFG should eliminate redundant indirectbr edges.
11 define void @indbrtest0(ptr %P, ptr %Q) {
12 ; CHECK-LABEL: @indbrtest0(
14 ; CHECK-NEXT: store ptr blockaddress(@indbrtest0, [[BB0:%.*]]), ptr [[P:%.*]], align 8
15 ; CHECK-NEXT: store ptr blockaddress(@indbrtest0, [[BB1:%.*]]), ptr [[P]], align 8
16 ; CHECK-NEXT: store ptr blockaddress(@indbrtest0, [[BB2:%.*]]), ptr [[P]], align 8
17 ; CHECK-NEXT: call void @foo()
18 ; CHECK-NEXT: [[T:%.*]] = load ptr, ptr [[Q:%.*]], align 8
19 ; CHECK-NEXT: indirectbr ptr [[T]], [label [[BB0]], label [[BB1]], label %BB2]
21 ; CHECK-NEXT: call void @A()
22 ; CHECK-NEXT: br label [[BB1]]
24 ; CHECK-NEXT: ret void
26 ; CHECK-NEXT: [[X:%.*]] = phi i32 [ 0, [[BB0]] ], [ 1, [[ENTRY:%.*]] ]
27 ; CHECK-NEXT: call void @B(i32 [[X]])
28 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
30 ; CHECK-NEXT: call void @C()
31 ; CHECK-NEXT: br label [[COMMON_RET]]
34 store ptr blockaddress(@indbrtest0, %BB0), ptr %P
35 store ptr blockaddress(@indbrtest0, %BB1), ptr %P
36 store ptr blockaddress(@indbrtest0, %BB2), ptr %P
39 indirectbr ptr %t, [label %BB0, label %BB1, label %BB2, label %BB0, label %BB1, label %BB2]
44 %x = phi i32 [ 0, %BB0 ], [ 1, %entry ], [ 1, %entry ]
52 ; SimplifyCFG should convert the indirectbr into a directbr. It would be even
53 ; better if it removed the branch altogether, but simplifycfdg currently misses
54 ; that because the predecessor is the entry block.
57 define void @indbrtest1(ptr %P, ptr %Q) {
58 ; CHECK-LABEL: @indbrtest1(
60 ; CHECK-NEXT: store ptr blockaddress(@indbrtest1, [[BB0:%.*]]), ptr [[P:%.*]], align 8
61 ; CHECK-NEXT: call void @foo()
62 ; CHECK-NEXT: br label [[BB0]]
64 ; CHECK-NEXT: call void @A()
65 ; CHECK-NEXT: ret void
68 store ptr blockaddress(@indbrtest1, %BB0), ptr %P
71 indirectbr ptr %t, [label %BB0, label %BB0]
77 ; SimplifyCFG should notice that BB0 does not have its address taken and
78 ; remove it from entry's successor list.
81 define void @indbrtest2(ptr %t) {
82 ; CHECK-LABEL: @indbrtest2(
84 ; CHECK-NEXT: unreachable
87 indirectbr ptr %t, [label %BB0, label %BB0]
93 ; Make sure the blocks in the next few tests aren't trivially removable as
94 ; successors by taking their addresses.
96 @anchor = constant [13 x ptr] [
97 ptr blockaddress(@indbrtest3, %L1), ptr blockaddress(@indbrtest3, %L2), ptr blockaddress(@indbrtest3, %L3),
98 ptr blockaddress(@indbrtest4, %L1), ptr blockaddress(@indbrtest4, %L2), ptr blockaddress(@indbrtest4, %L3),
99 ptr blockaddress(@indbrtest5, %L1), ptr blockaddress(@indbrtest5, %L2), ptr blockaddress(@indbrtest5, %L3), ptr blockaddress(@indbrtest5, %L4),
100 ptr blockaddress(@indbrtest6, %L1), ptr blockaddress(@indbrtest6, %L2), ptr blockaddress(@indbrtest6, %L3)
103 ; SimplifyCFG should turn the indirectbr into a conditional branch on the
104 ; condition of the select.
106 define void @indbrtest3(i1 %cond, ptr %address) nounwind {
107 ; CHECK-LABEL: @indbrtest3(
109 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[L1:%.*]], label [[L2:%.*]]
111 ; CHECK-NEXT: ret void
113 ; CHECK-NEXT: call void @A()
114 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
116 ; CHECK-NEXT: call void @C()
117 ; CHECK-NEXT: br label [[COMMON_RET]]
120 %indirect.goto.dest = select i1 %cond, ptr blockaddress(@indbrtest3, %L1), ptr blockaddress(@indbrtest3, %L2)
121 indirectbr ptr %indirect.goto.dest, [label %L1, label %L2, label %L3]
134 ; SimplifyCFG should turn the indirectbr into an unconditional branch to the
135 ; only possible destination.
136 ; As in @indbrtest1, it should really remove the branch entirely, but it doesn't
137 ; because it's in the entry block.
139 define void @indbrtest4(i1 %cond) nounwind {
140 ; CHECK-LABEL: @indbrtest4(
142 ; CHECK-NEXT: br label [[L1:%.*]]
144 ; CHECK-NEXT: call void @A()
145 ; CHECK-NEXT: ret void
148 %indirect.goto.dest = select i1 %cond, ptr blockaddress(@indbrtest4, %L1), ptr blockaddress(@indbrtest4, %L1)
149 indirectbr ptr %indirect.goto.dest, [label %L1, label %L2, label %L3]
162 ; SimplifyCFG should turn the indirectbr into an unreachable because neither
163 ; destination is listed as a successor.
165 define void @indbrtest5(i1 %cond, ptr %anchor) nounwind {
166 ; CHECK-LABEL: @indbrtest5(
168 ; CHECK-NEXT: unreachable
171 %indirect.goto.dest = select i1 %cond, ptr blockaddress(@indbrtest5, %L1), ptr blockaddress(@indbrtest5, %L2)
172 ; This needs to have more than one successor for this test, otherwise it gets
173 ; replaced with an unconditional branch to the single successor.
174 indirectbr ptr %indirect.goto.dest, [label %L3, label %L4]
188 ; This keeps blockaddresses not otherwise listed as successors from being zapped
189 ; before SimplifyCFG even looks at the indirectbr.
190 indirectbr ptr %anchor, [label %L1, label %L2]
193 ; The same as above, except the selected addresses are equal.
195 define void @indbrtest6(i1 %cond, ptr %anchor) nounwind {
196 ; CHECK-LABEL: @indbrtest6(
198 ; CHECK-NEXT: unreachable
201 %indirect.goto.dest = select i1 %cond, ptr blockaddress(@indbrtest6, %L1), ptr blockaddress(@indbrtest6, %L1)
202 ; This needs to have more than one successor for this test, otherwise it gets
203 ; replaced with an unconditional branch to the single successor.
204 indirectbr ptr %indirect.goto.dest, [label %L2, label %L3]
215 ; This keeps blockaddresses not otherwise listed as successors from being zapped
216 ; before SimplifyCFG even looks at the indirectbr.
217 indirectbr ptr %anchor, [label %L1, label %L2]
222 @xblkx.bbs = internal unnamed_addr constant [9 x ptr] [ptr blockaddress(@indbrtest7, %xblkx.begin), ptr blockaddress(@indbrtest7, %xblkx.begin3), ptr blockaddress(@indbrtest7, %xblkx.begin4), ptr blockaddress(@indbrtest7, %xblkx.begin5), ptr blockaddress(@indbrtest7, %xblkx.begin6), ptr blockaddress(@indbrtest7, %xblkx.begin7), ptr blockaddress(@indbrtest7, %xblkx.begin8), ptr blockaddress(@indbrtest7, %xblkx.begin9), ptr blockaddress(@indbrtest7, %xblkx.end)]
224 define void @indbrtest7() {
225 ; CHECK-LABEL: @indbrtest7(
226 ; CHECK-NEXT: escape-string.top:
227 ; CHECK-NEXT: [[XVAL202X:%.*]] = call i32 @xfunc5x()
228 ; CHECK-NEXT: br label [[XLAB5X:%.*]]
230 ; CHECK-NEXT: [[XVALUEX:%.*]] = call i32 @xselectorx()
231 ; CHECK-NEXT: [[XBLKX_X:%.*]] = getelementptr [9 x ptr], ptr @xblkx.bbs, i32 0, i32 [[XVALUEX]]
232 ; CHECK-NEXT: [[XBLKX_LOAD:%.*]] = load ptr, ptr [[XBLKX_X]], align 8
233 ; CHECK-NEXT: indirectbr ptr [[XBLKX_LOAD]], [label [[XLAB4X:%.*]], label %v2j]
235 ; CHECK-NEXT: [[XUNUSEDX:%.*]] = call i32 @xactionx()
236 ; CHECK-NEXT: br label [[XLAB4X]]
238 ; CHECK-NEXT: [[INCR19:%.*]] = add i32 [[XVAL704X_0:%.*]], 1
239 ; CHECK-NEXT: br label [[XLAB5X]]
241 ; CHECK-NEXT: [[XVAL704X_0]] = phi i32 [ 0, [[ESCAPE_STRING_TOP:%.*]] ], [ [[INCR19]], [[XLAB4X]] ]
242 ; CHECK-NEXT: [[XVAL10X:%.*]] = icmp ult i32 [[XVAL704X_0]], [[XVAL202X]]
243 ; CHECK-NEXT: br i1 [[XVAL10X]], label [[XLAB8X:%.*]], label [[XLAB9X:%.*]]
245 ; CHECK-NEXT: ret void
248 %xval202x = call i32 @xfunc5x()
251 xlab8x: ; preds = %xlab5x
252 %xvaluex = call i32 @xselectorx()
253 %xblkx.x = getelementptr [9 x ptr], ptr @xblkx.bbs, i32 0, i32 %xvaluex
254 %xblkx.load = load ptr, ptr %xblkx.x
255 indirectbr ptr %xblkx.load, [label %xblkx.begin, label %xblkx.begin3, label %xblkx.begin4, label %xblkx.begin5, label %xblkx.begin6, label %xblkx.begin7, label %xblkx.begin8, label %xblkx.begin9, label %xblkx.end]
282 %yes.0 = phi i1 [ false, %xblkx.begin ], [ true, %xlab8x ], [ false, %xblkx.begin9 ], [ false, %xblkx.begin8 ], [ false, %xblkx.begin7 ], [ false, %xblkx.begin6 ], [ false, %xblkx.begin5 ], [ true, %xblkx.begin4 ], [ false, %xblkx.begin3 ]
283 br i1 %yes.0, label %v2j, label %xlab17x
286 %xunusedx = call i32 @xactionx()
293 %incr19 = add i32 %xval704x.0, 1
297 %xval704x.0 = phi i32 [ 0, %escape-string.top ], [ %incr19, %xlab4x ]
298 %xval10x = icmp ult i32 %xval704x.0, %xval202x
299 br i1 %xval10x, label %xlab8x, label %xlab9x
305 define void @indbrtest8() {
306 ; CHECK-LABEL: @indbrtest8(
308 ; CHECK-NEXT: call void @B(i32 undef)
309 ; CHECK-NEXT: ret void
312 indirectbr ptr blockaddress(@indbrtest8, %BB1), [label %BB0, label %BB1]
317 call void @B(i32 undef)
321 define void @indbrtest9() {
322 ; CHECK-LABEL: @indbrtest9(
324 ; CHECK-NEXT: unreachable
327 indirectbr ptr blockaddress(@indbrtest9, %BB1), [label %BB0]
332 call void @B(i32 undef)
336 define void @indbrtest10() {
337 ; CHECK-LABEL: @indbrtest10(
339 ; CHECK-NEXT: call void @B(i32 undef)
340 ; CHECK-NEXT: ret void
343 indirectbr ptr blockaddress(@indbrtest10, %BB1), [label %BB1]
348 call void @B(i32 undef)
352 define void @indbrtest11() {
353 ; CHECK-LABEL: @indbrtest11(
355 ; CHECK-NEXT: call void @A()
356 ; CHECK-NEXT: ret void
359 indirectbr ptr blockaddress(@indbrtest11, %BB0), [label %BB0, label %BB1, label %BB1]
364 call void @B(i32 undef)
368 define void @indbrtest12() {
369 ; CHECK-LABEL: @indbrtest12(
371 ; CHECK-NEXT: call void @B(i32 undef)
372 ; CHECK-NEXT: ret void
375 indirectbr ptr blockaddress(@indbrtest12, %BB1), [label %BB0, label %BB1, label %BB1]
380 call void @B(i32 undef)
384 declare i32 @xfunc5x()
385 declare i8 @xfunc7x()
386 declare i32 @xselectorx()
387 declare i32 @xactionx()