blob: 595cacbcbf5489fbcb38a911ed9c5b03017dd220 [file] [log] [blame]
Frits van Bommel3d180342010-12-15 09:51:20 +00001; RUN: opt -S -jump-threading < %s | FileCheck %s
2
3declare void @foo()
4declare void @bar()
5declare void @baz()
6declare void @quux()
7
8
9; Jump threading of branch with select as condition.
10; Mostly theoretical since instruction combining simplifies all selects of
11; booleans where at least one operand is true/false/undef.
12
Stephen Linc1c7a132013-07-14 01:42:54 +000013; CHECK-LABEL: @test_br(
Frits van Bommel3d180342010-12-15 09:51:20 +000014; CHECK-NEXT: entry:
15; CHECK-NEXT: br i1 %cond, label %L1,
16define void @test_br(i1 %cond, i1 %value) nounwind {
17entry:
18 br i1 %cond, label %L0, label %L3
19L0:
20 %expr = select i1 %cond, i1 true, i1 %value
21 br i1 %expr, label %L1, label %L2
22
23L1:
24 call void @foo()
25 ret void
26L2:
27 call void @bar()
28 ret void
29L3:
30 call void @baz()
31 br label %L0
32}
33
34
35; Jump threading of switch with select as condition.
36
Stephen Linc1c7a132013-07-14 01:42:54 +000037; CHECK-LABEL: @test_switch(
Frits van Bommel3d180342010-12-15 09:51:20 +000038; CHECK-NEXT: entry:
39; CHECK-NEXT: br i1 %cond, label %L1,
40define void @test_switch(i1 %cond, i8 %value) nounwind {
41entry:
42 br i1 %cond, label %L0, label %L4
43L0:
44 %expr = select i1 %cond, i8 1, i8 %value
45 switch i8 %expr, label %L3 [i8 1, label %L1 i8 2, label %L2]
46
47L1:
48 call void @foo()
49 ret void
50L2:
51 call void @bar()
52 ret void
53L3:
54 call void @baz()
55 ret void
56L4:
57 call void @quux()
58 br label %L0
59}
60
61; Make sure the blocks in the indirectbr test aren't trivially removable as
62; successors by taking their addresses.
63@anchor = constant [3 x i8*] [
64 i8* blockaddress(@test_indirectbr, %L1),
65 i8* blockaddress(@test_indirectbr, %L2),
66 i8* blockaddress(@test_indirectbr, %L3)
67]
68
69
70; Jump threading of indirectbr with select as address.
71
Stephen Linc1c7a132013-07-14 01:42:54 +000072; CHECK-LABEL: @test_indirectbr(
Frits van Bommel3d180342010-12-15 09:51:20 +000073; CHECK-NEXT: entry:
74; CHECK-NEXT: br i1 %cond, label %L1, label %L3
75define void @test_indirectbr(i1 %cond, i8* %address) nounwind {
76entry:
77 br i1 %cond, label %L0, label %L3
78L0:
79 %indirect.goto.dest = select i1 %cond, i8* blockaddress(@test_indirectbr, %L1), i8* %address
80 indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
81
82L1:
83 call void @foo()
84 ret void
85L2:
86 call void @bar()
87 ret void
88L3:
89 call void @baz()
90 ret void
91}
92
93
Geoff Berry43dc2852015-12-29 18:10:16 +000094; Jump threading of indirectbr with select as address. Test increased
95; duplication threshold for cases where indirectbr is being threaded
96; through.
97
98; CHECK-LABEL: @test_indirectbr_thresh(
99; CHECK-NEXT: entry:
100; CHECK-NEXT: br i1 %cond, label %L1, label %L3
101; CHECK-NOT: indirectbr
102define void @test_indirectbr_thresh(i1 %cond, i8* %address) nounwind {
103entry:
104 br i1 %cond, label %L0, label %L3
105L0:
106 %indirect.goto.dest = select i1 %cond, i8* blockaddress(@test_indirectbr_thresh, %L1), i8* %address
107 call void @quux()
108 call void @quux()
109 call void @quux()
110 indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
111
112L1:
113 call void @foo()
114 ret void
115L2:
116 call void @bar()
117 ret void
118L3:
119 call void @baz()
120 ret void
121}
122
123
Frits van Bommel3d180342010-12-15 09:51:20 +0000124; A more complicated case: the condition is a select based on a comparison.
125
Stephen Linc1c7a132013-07-14 01:42:54 +0000126; CHECK-LABEL: @test_switch_cmp(
Frits van Bommel3d180342010-12-15 09:51:20 +0000127; CHECK-NEXT: entry:
128; CHECK-NEXT: br i1 %cond, label %L0, label %[[THREADED:[A-Za-z.0-9]+]]
129; CHECK: [[THREADED]]:
130; CHECK-NEXT: call void @quux
131; CHECK-NEXT: br label %L1
132define void @test_switch_cmp(i1 %cond, i32 %val, i8 %value) nounwind {
133entry:
134 br i1 %cond, label %L0, label %L4
135L0:
136 %val.phi = phi i32 [%val, %entry], [-1, %L4]
137 %cmp = icmp slt i32 %val.phi, 0
138 %expr = select i1 %cmp, i8 1, i8 %value
139 switch i8 %expr, label %L3 [i8 1, label %L1 i8 2, label %L2]
140
141L1:
142 call void @foo()
143 ret void
144L2:
145 call void @bar()
146 ret void
147L3:
148 call void @baz()
149 ret void
150L4:
151 call void @quux()
152 br label %L0
153}
Manman Renf3fedb62012-09-05 23:45:58 +0000154
155; Make sure the edge value of %0 from entry to L2 includes 0 and L3 is
156; reachable.
157; CHECK: test_switch_default
158; CHECK: entry:
159; CHECK: load
Dinesh Dwivedi657105e2014-06-17 14:34:19 +0000160; CHECK: icmp
Manman Renf3fedb62012-09-05 23:45:58 +0000161; CHECK: [[THREADED:[A-Za-z.0-9]+]]:
162; CHECK: store
163; CHECK: br
164; CHECK: L2:
165; CHECK: icmp
166define void @test_switch_default(i32* nocapture %status) nounwind {
167entry:
David Blaikiea79ac142015-02-27 21:17:42 +0000168 %0 = load i32, i32* %status, align 4
Manman Renf3fedb62012-09-05 23:45:58 +0000169 switch i32 %0, label %L2 [
170 i32 5061, label %L1
171 i32 0, label %L2
172 ]
173
174L1:
175 store i32 10025, i32* %status, align 4
176 br label %L2
177
178L2:
David Blaikiea79ac142015-02-27 21:17:42 +0000179 %1 = load i32, i32* %status, align 4
Manman Renf3fedb62012-09-05 23:45:58 +0000180 %cmp57.i = icmp eq i32 %1, 0
181 br i1 %cmp57.i, label %L3, label %L4
182
183L3:
184 store i32 10000, i32* %status, align 4
185 br label %L4
186
187L4:
188 ret void
189}
Benjamin Kramer6a4976d2013-08-07 10:29:38 +0000190
191define void @unfold1(double %x, double %y) nounwind {
192entry:
193 %sub = fsub double %x, %y
194 %cmp = fcmp ogt double %sub, 1.000000e+01
195 br i1 %cmp, label %cond.end4, label %cond.false
196
197cond.false: ; preds = %entry
198 %add = fadd double %x, %y
199 %cmp1 = fcmp ogt double %add, 1.000000e+01
200 %add. = select i1 %cmp1, double %add, double 0.000000e+00
201 br label %cond.end4
202
203cond.end4: ; preds = %entry, %cond.false
204 %cond5 = phi double [ %add., %cond.false ], [ %sub, %entry ]
205 %cmp6 = fcmp oeq double %cond5, 0.000000e+00
206 br i1 %cmp6, label %if.then, label %if.end
207
208if.then: ; preds = %cond.end4
209 call void @foo()
210 br label %if.end
211
212if.end: ; preds = %if.then, %cond.end4
213 ret void
214
215; CHECK-LABEL: @unfold1
216; CHECK: br i1 %cmp, label %cond.end4, label %cond.false
217; CHECK: br i1 %cmp1, label %cond.end4, label %if.then
218; CHECK: br i1 %cmp6, label %if.then, label %if.end
219; CHECK: br label %if.end
220}
221
222
223define void @unfold2(i32 %x, i32 %y) nounwind {
224entry:
225 %sub = sub nsw i32 %x, %y
226 %cmp = icmp sgt i32 %sub, 10
227 br i1 %cmp, label %cond.end4, label %cond.false
228
229cond.false: ; preds = %entry
230 %add = add nsw i32 %x, %y
231 %cmp1 = icmp sgt i32 %add, 10
232 %add. = select i1 %cmp1, i32 0, i32 %add
233 br label %cond.end4
234
235cond.end4: ; preds = %entry, %cond.false
236 %cond5 = phi i32 [ %add., %cond.false ], [ %sub, %entry ]
237 %cmp6 = icmp eq i32 %cond5, 0
238 br i1 %cmp6, label %if.then, label %if.end
239
240if.then: ; preds = %cond.end4
241 call void @foo()
242 br label %if.end
243
244if.end: ; preds = %if.then, %cond.end4
245 ret void
246
247; CHECK-LABEL: @unfold2
248; CHECK: br i1 %cmp, label %if.end, label %cond.false
249; CHECK: br i1 %cmp1, label %if.then, label %cond.end4
250; CHECK: br i1 %cmp6, label %if.then, label %if.end
251; CHECK: br label %if.end
252}