blob: b062e5fe73453b2cf519f0562b889a7d6109660d [file] [log] [blame]
Jun Bum Lim90b6b502016-12-16 20:38:39 +00001; RUN: opt -codegenprepare < %s -mtriple=aarch64-none-linux-gnu -S | FileCheck %s
2
3target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
4target triple = "aarch64--linux-gnu"
5
6; Expect to skip merging two empty blocks (sw.bb and sw.bb2) into sw.epilog
7; as both of them are unlikely executed.
8define i32 @f_switch(i32 %c) {
9; CHECK-LABEL: @f_switch
10; CHECK-LABEL: entry:
11; CHECK: i32 10, label %sw.bb
12; CHECK: i32 20, label %sw.bb2
13entry:
14 switch i32 %c, label %sw.default [
15 i32 10, label %sw.bb
16 i32 20, label %sw.bb2
17 i32 30, label %sw.bb3
18 i32 40, label %sw.bb4
19 ], !prof !0
20
21sw.bb: ; preds = %entry
22 br label %sw.epilog
23
24sw.bb2: ; preds = %entry
25 br label %sw.epilog
26
27sw.bb3: ; preds = %entry
28 call void bitcast (void (...)* @callcase3 to void ()*)()
29 br label %sw.epilog
30
31sw.bb4: ; preds = %entry
32 call void bitcast (void (...)* @callcase4 to void ()*)()
33 br label %sw.epilog
34
35sw.default: ; preds = %entry
36 call void bitcast (void (...)* @calldefault to void ()*)()
37 br label %sw.epilog
38
39; CHECK-LABEL: sw.epilog:
40; CHECK: %fp.0 = phi void (...)* [ @FD, %sw.default ], [ @F4, %sw.bb4 ], [ @F3, %sw.bb3 ], [ @F2, %sw.bb2 ], [ @F1, %sw.bb ]
41sw.epilog: ; preds = %sw.default, %sw.bb3, %sw.bb2, %sw.bb
42 %fp.0 = phi void (...)* [ @FD, %sw.default ], [ @F4, %sw.bb4 ], [ @F3, %sw.bb3 ], [ @F2, %sw.bb2 ], [ @F1, %sw.bb ]
43 %callee.knr.cast = bitcast void (...)* %fp.0 to void ()*
44 call void %callee.knr.cast()
45 ret i32 0
46}
47
48; Expect not to merge sw.bb2 because of the conflict in the incoming value from
49; sw.bb which is already merged.
50define i32 @f_switch2(i32 %c) {
51; CHECK-LABEL: @f_switch2
52; CHECK-LABEL: entry:
53; CHECK: i32 10, label %sw.epilog
54; CHECK: i32 20, label %sw.bb2
55entry:
56 switch i32 %c, label %sw.default [
57 i32 10, label %sw.bb
58 i32 20, label %sw.bb2
59 i32 30, label %sw.bb3
60 i32 40, label %sw.bb4
61 ], !prof !1
62
63sw.bb: ; preds = %entry
64 br label %sw.epilog
65
66sw.bb2: ; preds = %entry
67 br label %sw.epilog
68
69sw.bb3: ; preds = %entry
70 call void bitcast (void (...)* @callcase3 to void ()*)()
71 br label %sw.epilog
72
73sw.bb4: ; preds = %entry
74 call void bitcast (void (...)* @callcase4 to void ()*)()
75 br label %sw.epilog
76
77sw.default: ; preds = %entry
78 call void bitcast (void (...)* @calldefault to void ()*)()
79 br label %sw.epilog
80
81; CHECK-LABEL: sw.epilog:
82; CHECK: %fp.0 = phi void (...)* [ @FD, %sw.default ], [ @F4, %sw.bb4 ], [ @F3, %sw.bb3 ], [ @F2, %sw.bb2 ], [ @F1, %entry ]
83sw.epilog: ; preds = %sw.default, %sw.bb3, %sw.bb2, %sw.bb
84 %fp.0 = phi void (...)* [ @FD, %sw.default ], [ @F4, %sw.bb4 ], [ @F3, %sw.bb3 ], [ @F2, %sw.bb2 ], [ @F1, %sw.bb ]
85 %callee.knr.cast = bitcast void (...)* %fp.0 to void ()*
86 call void %callee.knr.cast()
87 ret i32 0
88}
89
90; Multiple empty blocks should be considered together if all incoming values
91; from them are same. We expect to merge both empty blocks (sw.bb and sw.bb2)
92; because the sum of frequencies are higer than the threshold.
93define i32 @f_switch3(i32 %c) {
94; CHECK-LABEL: @f_switch3
95; CHECK-LABEL: entry:
96; CHECK: i32 10, label %sw.epilog
97; CHECK: i32 20, label %sw.epilog
98entry:
99 switch i32 %c, label %sw.default [
100 i32 10, label %sw.bb
101 i32 20, label %sw.bb2
102 i32 30, label %sw.bb3
103 i32 40, label %sw.bb4
104 ], !prof !2
105
106sw.bb: ; preds = %entry
107 br label %sw.epilog
108
109sw.bb2: ; preds = %entry
110 br label %sw.epilog
111
112sw.bb3: ; preds = %entry
113 call void bitcast (void (...)* @callcase3 to void ()*)()
114 br label %sw.epilog
115
116sw.bb4: ; preds = %entry
117 call void bitcast (void (...)* @callcase4 to void ()*)()
118 br label %sw.epilog
119
120sw.default: ; preds = %entry
121 call void bitcast (void (...)* @calldefault to void ()*)()
122 br label %sw.epilog
123
124; CHECK-LABEL: sw.epilog:
125; CHECK: %fp.0 = phi void (...)* [ @FD, %sw.default ], [ @F4, %sw.bb4 ], [ @F3, %sw.bb3 ], [ @F1, %entry ], [ @F1, %entry ]
126sw.epilog: ; preds = %sw.default, %sw.bb3, %sw.bb2, %sw.bb
127 %fp.0 = phi void (...)* [ @FD, %sw.default ], [ @F4, %sw.bb4 ], [ @F3, %sw.bb3 ], [ @F1, %sw.bb2 ], [ @F1, %sw.bb ]
128 %callee.knr.cast = bitcast void (...)* %fp.0 to void ()*
129 call void %callee.knr.cast()
130 ret i32 0
131}
132
133declare void @F1(...) local_unnamed_addr
134declare void @F2(...) local_unnamed_addr
135declare void @F3(...) local_unnamed_addr
136declare void @F4(...) local_unnamed_addr
137declare void @FD(...) local_unnamed_addr
138declare void @callcase3(...) local_unnamed_addr
139declare void @callcase4(...) local_unnamed_addr
140declare void @calldefault(...) local_unnamed_addr
141
142!0 = !{!"branch_weights", i32 5, i32 1, i32 1,i32 5, i32 5}
143!1 = !{!"branch_weights", i32 1 , i32 5, i32 1,i32 1, i32 1}
144!2 = !{!"branch_weights", i32 1 , i32 4, i32 1,i32 1, i32 1}
145
Balaram Makamcddf3c52017-10-26 22:34:01 +0000146; while.cond does not dominate return, expect to skip merging empty block
147; return.loopexit into return.
148@b = external global i32, align 4
149@a = external global i32*, align 8
150
151define void @f_switch4(i32 %i) local_unnamed_addr personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
152; CHECK-LABEL: @f_switch4
153entry:
154 %0 = load i32, i32* @b, align 4
155 %cond = icmp eq i32 %0, 6
156 br i1 %cond, label %return, label %if.end
157
158if.end: ; preds = %entry
159 %add = add i32 %i, 2
160 %1 = load i32*, i32** @a, align 8
161 %magicptr = ptrtoint i32* %1 to i32
162 br label %while.cond
163
164; CHECK-LABEL: while.cond:
165; CHECK: i32 0, label %return.loopexit
166; CHECK: i32 47, label %return.loopexit
167while.cond: ; preds = %while.cond.backedge, %if.end
168 switch i32 %magicptr, label %while.cond.if.end10_crit_edge [
169 i32 0, label %return.loopexit
170 i32 47, label %return.loopexit
171 ]
172
173while.cond.if.end10_crit_edge: ; preds = %while.cond
174 br label %while.cond.backedge
175
176while.cond.backedge: ; preds = %while.cond.if.end10_crit_edge, %if.then9
177 br label %while.cond
178
179return.loopexit: ; preds = %while.cond
180 br label %return
181
182; CHECK_LABEL: return:
183; CHECK: %{{.*}} = phi i32 [ 0, %entry ], [ %add, %return.loopexit ]
184return: ; preds = %return.loopexit, %entry
185 %retval.4 = phi i32 [ 0, %entry ], [ %add, %return.loopexit ]
186 ret void
187}
188declare i32 @__gxx_personality_v0(...)
189
190; Expect to merge empty block while.cond2.loopexit into while.cond2
191define i32 @f_switch5(i32 %i) local_unnamed_addr {
192; CHECK-LABEL: @f_switch5
193entry:
194 %0 = load i32, i32* @b, align 4
195 %cond = icmp eq i32 %0, 6
196 br i1 %cond, label %while.cond.preheader, label %sw.epilog
197
198while.cond.preheader: ; preds = %entry
199 %1 = load i32*, i32** @a, align 8
200 %magicptr = ptrtoint i32* %1 to i64
201 %arrayidx = getelementptr inbounds i32, i32* %1, i64 1
202 br label %while.cond
203
204; CHECK-LABEL: while.cond:
205; CHECK: i64 32, label %while.cond2
206; CHECK: i64 0, label %while.cond2
207while.cond: ; preds = %land.rhs, %while.cond.preheader
208 switch i64 %magicptr, label %land.rhs [
209 i64 32, label %while.cond2.loopexit
210 i64 0, label %while.cond2.loopexit
211 ]
212
213land.rhs: ; preds = %while.cond
214 %2 = load i32, i32* %arrayidx, align 4
215 %tobool1 = icmp eq i32 %2, 0
216 br i1 %tobool1, label %while.cond2thread-pre-split.loopexit, label %while.cond
217
218while.cond2thread-pre-split.loopexit: ; preds = %land.rhs
219 br label %while.cond2thread-pre-split
220
221while.cond2thread-pre-split: ; preds = %while.body4, %while.cond2thread-pre-split.loopexit
222 %.pr = phi i32* [ %.pr.pre, %while.body4 ], [ %1, %while.cond2thread-pre-split.loopexit ]
223 br label %while.cond2
224
225while.cond2.loopexit: ; preds = %while.cond, %while.cond
226 br label %while.cond2
227
228; CHECK-LABEL: while.cond2:
229; CHECK: %{{.*}} = phi i32* [ %.pr.pre, %while.body4 ], [ %1, %land.rhs ], [ %1, %while.cond ], [ %1, %while.cond ]
230while.cond2: ; preds = %while.cond2.loopexit, %while.cond2thread-pre-split
231 %3 = phi i32* [ %.pr, %while.cond2thread-pre-split ], [ %1, %while.cond2.loopexit ]
232 %tobool3 = icmp eq i32* %3, null
233 br i1 %tobool3, label %sw.epilog, label %while.body4
234
235while.body4: ; preds = %while.cond2
236 tail call void bitcast (void (...)* @fn2 to void ()*)()
237 %.pr.pre = load i32*, i32** @a, align 8
238 br label %while.cond2thread-pre-split
239
240sw.epilog: ; preds = %while.cond2, %entry
241 ret i32 undef
242}
243
Jun Bum Lim90b6b502016-12-16 20:38:39 +0000244
245; This test that BFI/BPI is created without any assertion in isMergingEmptyBlockProfitable()
246; in the case where empty blocks are removed before creating BFI/BPI.
Jun Bum Lim90b6b502016-12-16 20:38:39 +0000247define i32 @should_not_assert(i32 %i) local_unnamed_addr {
248entry:
249 %0 = load i32, i32* @b, align 4
250 %cond = icmp eq i32 %0, 6
251 br i1 %cond, label %while.cond.preheader, label %sw.epilog
252
253while.cond.preheader: ; preds = %entry
254 %1 = load i32*, i32** @a, align 8
255 %magicptr = ptrtoint i32* %1 to i64
256 %arrayidx = getelementptr inbounds i32, i32* %1, i64 1
257 br label %while.cond
258
259while.cond: ; preds = %while.cond.preheader, %land.rhs
260 switch i64 %magicptr, label %land.rhs [
261 i64 32, label %while.cond2.loopexit
262 i64 0, label %while.cond2.loopexit
263 ]
264
265land.rhs: ; preds = %while.cond
266 %2 = load i32, i32* %arrayidx, align 4
267 %tobool1 = icmp eq i32 %2, 0
268 br i1 %tobool1, label %while.cond2thread-pre-split.loopexit, label %while.cond
269
270while.cond2thread-pre-split.loopexit: ; preds = %land.rhs
271 br label %while.cond2thread-pre-split
272
273while.cond2thread-pre-split: ; preds = %while.cond2thread-pre-split.loopexit, %while.body4
274 %.pr = phi i32* [ %.pr.pre, %while.body4 ], [ %1, %while.cond2thread-pre-split.loopexit ]
275 br label %while.cond2
276
277while.cond2.loopexit: ; preds = %while.cond, %while.cond
278 br label %while.cond2
279
280while.cond2: ; preds = %while.cond2.loopexit, %while.cond2thread-pre-split
281 %3 = phi i32* [ %.pr, %while.cond2thread-pre-split ], [ %1, %while.cond2.loopexit ]
282 %tobool3 = icmp eq i32* %3, null
283 br i1 %tobool3, label %sw.epilog, label %while.body4
284
285while.body4: ; preds = %while.cond2
286 tail call void bitcast (void (...)* @fn2 to void ()*)()
287 %.pr.pre = load i32*, i32** @a, align 8
288 br label %while.cond2thread-pre-split
289
290sw.epilog: ; preds = %while.cond2, %entry
291 ret i32 undef
292}
293
294
295declare void @fn2(...) local_unnamed_addr
296