1 ; RUN: opt -S -simplifycfg < %s | FileCheck %s
3 ; SimplifyCFG should eliminate redundant indirectbr edges.
6 ; CHECK: indirectbr i8* %t, [label %BB0, label %BB1, label %BB2]
7 ; CHECK: %x = phi i32 [ 0, %BB0 ], [ 1, %entry ]
14 define void @indbrtest0(i8** %P, i8** %Q) {
16 store i8* blockaddress(@indbrtest0, %BB0), i8** %P
17 store i8* blockaddress(@indbrtest0, %BB1), i8** %P
18 store i8* blockaddress(@indbrtest0, %BB2), i8** %P
20 %t = load i8*, i8** %Q
21 indirectbr i8* %t, [label %BB0, label %BB1, label %BB2, label %BB0, label %BB1, label %BB2]
26 %x = phi i32 [ 0, %BB0 ], [ 1, %entry ], [ 1, %entry ]
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.
39 ; CHECK: br label %BB0
41 define void @indbrtest1(i8** %P, i8** %Q) {
43 store i8* blockaddress(@indbrtest1, %BB0), i8** %P
45 %t = load i8*, i8** %Q
46 indirectbr i8* %t, [label %BB0, label %BB0]
52 ; SimplifyCFG should notice that BB0 does not have its address taken and
53 ; remove it from entry's successor list.
57 ; CHECK-NEXT: unreachable
59 define void @indbrtest2(i8* %t) {
61 indirectbr i8* %t, [label %BB0, label %BB0]
67 ; Make sure the blocks in the next few tests aren't trivially removable as
68 ; successors by taking their addresses.
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)
77 ; SimplifyCFG should turn the indirectbr into a conditional branch on the
78 ; condition of the select.
80 ; CHECK-LABEL: @indbrtest3(
82 ; CHECK-NEXT: br i1 %cond, label %L1, label %L2
83 ; CHECK-NOT: indirectbr
86 define void @indbrtest3(i1 %cond, i8* %address) nounwind {
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]
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.
107 ; CHECK-LABEL: @indbrtest4(
109 ; CHECK-NEXT: br label %L1
110 define void @indbrtest4(i1 %cond) nounwind {
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]
126 ; SimplifyCFG should turn the indirectbr into an unreachable because neither
127 ; destination is listed as a successor.
129 ; CHECK-LABEL: @indbrtest5(
131 ; CHECK-NEXT: unreachable
133 define void @indbrtest5(i1 %cond, i8* %anchor) nounwind {
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]
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]
157 ; The same as above, except the selected addresses are equal.
159 ; CHECK-LABEL: @indbrtest6(
161 ; CHECK-NEXT: unreachable
163 define void @indbrtest6(i1 %cond, i8* %anchor) nounwind {
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]
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]
186 @xblkx.bbs = internal unnamed_addr constant [9 x i8*] [i8* blockaddress(@indbrtest7, %xblkx.begin), i8* blockaddress(@indbrtest7, %xblkx.begin3), i8* blockaddress(@indbrtest7, %xblkx.begin4), i8* blockaddress(@indbrtest7, %xblkx.begin5), i8* blockaddress(@indbrtest7, %xblkx.begin6), i8* blockaddress(@indbrtest7, %xblkx.begin7), i8* blockaddress(@indbrtest7, %xblkx.begin8), i8* blockaddress(@indbrtest7, %xblkx.begin9), i8* blockaddress(@indbrtest7, %xblkx.end)]
188 define void @indbrtest7() {
190 %xval202x = call i32 @xfunc5x()
193 xlab8x: ; preds = %xlab5x
194 %xvaluex = call i32 @xselectorx()
195 %xblkx.x = getelementptr [9 x i8*], [9 x i8*]* @xblkx.bbs, i32 0, i32 %xvaluex
196 %xblkx.load = load i8*, i8** %xblkx.x
197 indirectbr i8* %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]
224 %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 ]
225 br i1 %yes.0, label %v2j, label %xlab17x
228 ; CHECK: %xunusedx = call i32 @xactionx()
229 %xunusedx = call i32 @xactionx()
236 %incr19 = add i32 %xval704x.0, 1
240 %xval704x.0 = phi i32 [ 0, %escape-string.top ], [ %incr19, %xlab4x ]
241 %xval10x = icmp ult i32 %xval704x.0, %xval202x
242 br i1 %xval10x, label %xlab8x, label %xlab9x
248 declare i32 @xfunc5x()
249 declare i8 @xfunc7x()
250 declare i32 @xselectorx()
251 declare i32 @xactionx()