1; RUN: opt -S -bdce -instsimplify < %s | FileCheck %s
2; RUN: opt -S -instsimplify < %s | FileCheck %s -check-prefix=CHECK-IO
3; RUN: opt -S -debugify -bdce < %s | FileCheck %s -check-prefix=DEBUGIFY
4target datalayout = "E-m:e-i64:64-n32:64"
5target triple = "powerpc64-unknown-linux-gnu"
6
7; Function Attrs: nounwind readnone
8define signext i32 @bar(i32 signext %x) #0 {
9entry:
10  %call = tail call signext i32 @foo(i32 signext 5) #0
11  %and = and i32 %call, 4
12  %or = or i32 %and, %x
13  %call1 = tail call signext i32 @foo(i32 signext 3) #0
14  %and2 = and i32 %call1, 8
15  %or3 = or i32 %or, %and2
16  %call4 = tail call signext i32 @foo(i32 signext 2) #0
17  %and5 = and i32 %call4, 16
18  %or6 = or i32 %or3, %and5
19  %call7 = tail call signext i32 @foo(i32 signext 1) #0
20  %and8 = and i32 %call7, 32
21  %or9 = or i32 %or6, %and8
22  %call10 = tail call signext i32 @foo(i32 signext 0) #0
23  %and11 = and i32 %call10, 64
24  %or12 = or i32 %or9, %and11
25  %call13 = tail call signext i32 @foo(i32 signext 4) #0
26  %and14 = and i32 %call13, 128
27  %or15 = or i32 %or12, %and14
28  %shr = ashr i32 %or15, 4
29  ret i32 %shr
30
31; CHECK-LABEL: @bar
32; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
33; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
34; CHECK: tail call signext i32 @foo(i32 signext 2)
35; CHECK: tail call signext i32 @foo(i32 signext 1)
36; CHECK: tail call signext i32 @foo(i32 signext 0)
37; CHECK: tail call signext i32 @foo(i32 signext 4)
38; CHECK: ret i32
39
40; Check that instsimplify is not doing this all on its own.
41; CHECK-IO-LABEL: @bar
42; CHECK-IO: tail call signext i32 @foo(i32 signext 5)
43; CHECK-IO: tail call signext i32 @foo(i32 signext 3)
44; CHECK-IO: tail call signext i32 @foo(i32 signext 2)
45; CHECK-IO: tail call signext i32 @foo(i32 signext 1)
46; CHECK-IO: tail call signext i32 @foo(i32 signext 0)
47; CHECK-IO: tail call signext i32 @foo(i32 signext 4)
48; CHECK-IO: ret i32
49}
50
51; Function Attrs: nounwind readnone
52declare signext i32 @foo(i32 signext) #0
53
54; Function Attrs: nounwind readnone
55define signext i32 @far(i32 signext %x) #1 {
56entry:
57  %call = tail call signext i32 @goo(i32 signext 5) #1
58  %and = and i32 %call, 4
59  %or = or i32 %and, %x
60  %call1 = tail call signext i32 @goo(i32 signext 3) #1
61  %and2 = and i32 %call1, 8
62  %or3 = or i32 %or, %and2
63  %call4 = tail call signext i32 @goo(i32 signext 2) #1
64  %and5 = and i32 %call4, 16
65  %or6 = or i32 %or3, %and5
66  %call7 = tail call signext i32 @goo(i32 signext 1) #1
67  %and8 = and i32 %call7, 32
68  %or9 = or i32 %or6, %and8
69  %call10 = tail call signext i32 @goo(i32 signext 0) #1
70  %and11 = and i32 %call10, 64
71  %or12 = or i32 %or9, %and11
72  %call13 = tail call signext i32 @goo(i32 signext 4) #1
73  %and14 = and i32 %call13, 128
74  %or15 = or i32 %or12, %and14
75  %shr = ashr i32 %or15, 4
76  ret i32 %shr
77
78; CHECK-LABEL: @far
79; Calls to foo(5) and foo(3) are still there, but their results are not used.
80; CHECK: tail call signext i32 @goo(i32 signext 5)
81; CHECK-NEXT: tail call signext i32 @goo(i32 signext 3)
82; CHECK-NEXT: tail call signext i32 @goo(i32 signext 2)
83; CHECK: tail call signext i32 @goo(i32 signext 1)
84; CHECK: tail call signext i32 @goo(i32 signext 0)
85; CHECK: tail call signext i32 @goo(i32 signext 4)
86; CHECK: ret i32
87
88; Check that instsimplify is not doing this all on its own.
89; CHECK-IO-LABEL: @far
90; CHECK-IO: tail call signext i32 @goo(i32 signext 5)
91; CHECK-IO: tail call signext i32 @goo(i32 signext 3)
92; CHECK-IO: tail call signext i32 @goo(i32 signext 2)
93; CHECK-IO: tail call signext i32 @goo(i32 signext 1)
94; CHECK-IO: tail call signext i32 @goo(i32 signext 0)
95; CHECK-IO: tail call signext i32 @goo(i32 signext 4)
96; CHECK-IO: ret i32
97}
98
99declare signext i32 @goo(i32 signext) #1
100
101; Function Attrs: nounwind readnone
102define signext i32 @tar1(i32 signext %x) #0 {
103entry:
104  %call = tail call signext i32 @foo(i32 signext 5) #0
105  %and = and i32 %call, 33554432
106  %or = or i32 %and, %x
107  %call1 = tail call signext i32 @foo(i32 signext 3) #0
108  %and2 = and i32 %call1, 67108864
109  %or3 = or i32 %or, %and2
110  %call4 = tail call signext i32 @foo(i32 signext 2) #0
111  %and5 = and i32 %call4, 16
112  %or6 = or i32 %or3, %and5
113  %call7 = tail call signext i32 @foo(i32 signext 1) #0
114  %and8 = and i32 %call7, 32
115  %or9 = or i32 %or6, %and8
116  %call10 = tail call signext i32 @foo(i32 signext 0) #0
117  %and11 = and i32 %call10, 64
118  %or12 = or i32 %or9, %and11
119  %call13 = tail call signext i32 @foo(i32 signext 4) #0
120  %and14 = and i32 %call13, 128
121  %or15 = or i32 %or12, %and14
122  %bs = tail call i32 @llvm.bswap.i32(i32 %or15) #0
123  %shr = ashr i32 %bs, 4
124  ret i32 %shr
125
126; CHECK-LABEL: @tar1
127; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
128; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
129; CHECK: tail call signext i32 @foo(i32 signext 2)
130; CHECK: tail call signext i32 @foo(i32 signext 1)
131; CHECK: tail call signext i32 @foo(i32 signext 0)
132; CHECK: tail call signext i32 @foo(i32 signext 4)
133; CHECK: ret i32
134}
135
136; Function Attrs: nounwind readnone
137declare i32 @llvm.bswap.i32(i32) #0
138
139; Function Attrs: nounwind readnone
140define signext i32 @tim(i32 signext %x) #0 {
141entry:
142  %call = tail call signext i32 @foo(i32 signext 5) #0
143  %and = and i32 %call, 536870912
144  %or = or i32 %and, %x
145  %call1 = tail call signext i32 @foo(i32 signext 3) #0
146  %and2 = and i32 %call1, 1073741824
147  %or3 = or i32 %or, %and2
148  %call4 = tail call signext i32 @foo(i32 signext 2) #0
149  %and5 = and i32 %call4, 16
150  %or6 = or i32 %or3, %and5
151  %call7 = tail call signext i32 @foo(i32 signext 1) #0
152  %and8 = and i32 %call7, 32
153  %or9 = or i32 %or6, %and8
154  %call10 = tail call signext i32 @foo(i32 signext 0) #0
155  %and11 = and i32 %call10, 64
156  %or12 = or i32 %or9, %and11
157  %call13 = tail call signext i32 @foo(i32 signext 4) #0
158  %and14 = and i32 %call13, 128
159  %or15 = or i32 %or12, %and14
160  %bs = tail call i32 @llvm.bitreverse.i32(i32 %or15) #0
161  %shr = ashr i32 %bs, 4
162  ret i32 %shr
163
164; CHECK-LABEL: @tim
165; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
166; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
167; CHECK: tail call signext i32 @foo(i32 signext 2)
168; CHECK: tail call signext i32 @foo(i32 signext 1)
169; CHECK: tail call signext i32 @foo(i32 signext 0)
170; CHECK: tail call signext i32 @foo(i32 signext 4)
171; CHECK: ret i32
172}
173
174; Function Attrs: nounwind readnone
175declare i32 @llvm.bitreverse.i32(i32) #0
176
177; Function Attrs: nounwind readnone
178define signext i32 @tar2(i32 signext %x) #0 {
179entry:
180  %call = tail call signext i32 @foo(i32 signext 5) #0
181  %and = and i32 %call, 33554432
182  %or = or i32 %and, %x
183  %call1 = tail call signext i32 @foo(i32 signext 3) #0
184  %and2 = and i32 %call1, 67108864
185  %or3 = or i32 %or, %and2
186  %call4 = tail call signext i32 @foo(i32 signext 2) #0
187  %and5 = and i32 %call4, 16
188  %or6 = or i32 %or3, %and5
189  %call7 = tail call signext i32 @foo(i32 signext 1) #0
190  %and8 = and i32 %call7, 32
191  %or9 = or i32 %or6, %and8
192  %call10 = tail call signext i32 @foo(i32 signext 0) #0
193  %and11 = and i32 %call10, 64
194  %or12 = or i32 %or9, %and11
195  %call13 = tail call signext i32 @foo(i32 signext 4) #0
196  %and14 = and i32 %call13, 128
197  %or15 = or i32 %or12, %and14
198  %shl = shl i32 %or15, 10
199  ret i32 %shl
200
201; CHECK-LABEL: @tar2
202; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
203; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
204; CHECK: tail call signext i32 @foo(i32 signext 2)
205; CHECK: tail call signext i32 @foo(i32 signext 1)
206; CHECK: tail call signext i32 @foo(i32 signext 0)
207; CHECK: tail call signext i32 @foo(i32 signext 4)
208; CHECK: ret i32
209}
210
211; Function Attrs: nounwind readnone
212define signext i32 @tar3(i32 signext %x) #0 {
213entry:
214  %call = tail call signext i32 @foo(i32 signext 5) #0
215  %and = and i32 %call, 33554432
216  %or = or i32 %and, %x
217  %call1 = tail call signext i32 @foo(i32 signext 3) #0
218  %and2 = and i32 %call1, 67108864
219  %or3 = or i32 %or, %and2
220  %call4 = tail call signext i32 @foo(i32 signext 2) #0
221  %and5 = and i32 %call4, 16
222  %or6 = or i32 %or3, %and5
223  %call7 = tail call signext i32 @foo(i32 signext 1) #0
224  %and8 = and i32 %call7, 32
225  %or9 = or i32 %or6, %and8
226  %call10 = tail call signext i32 @foo(i32 signext 0) #0
227  %and11 = and i32 %call10, 64
228  %or12 = or i32 %or9, %and11
229  %call13 = tail call signext i32 @foo(i32 signext 4) #0
230  %and14 = and i32 %call13, 128
231  %or15 = or i32 %or12, %and14
232  %add = add i32 %or15, 5
233  %shl = shl i32 %add, 10
234  ret i32 %shl
235
236; CHECK-LABEL: @tar3
237; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
238; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
239; CHECK: tail call signext i32 @foo(i32 signext 2)
240; CHECK: tail call signext i32 @foo(i32 signext 1)
241; CHECK: tail call signext i32 @foo(i32 signext 0)
242; CHECK: tail call signext i32 @foo(i32 signext 4)
243; CHECK: ret i32
244}
245
246; Function Attrs: nounwind readnone
247define signext i32 @tar4(i32 signext %x) #0 {
248entry:
249  %call = tail call signext i32 @foo(i32 signext 5) #0
250  %and = and i32 %call, 33554432
251  %or = or i32 %and, %x
252  %call1 = tail call signext i32 @foo(i32 signext 3) #0
253  %and2 = and i32 %call1, 67108864
254  %or3 = or i32 %or, %and2
255  %call4 = tail call signext i32 @foo(i32 signext 2) #0
256  %and5 = and i32 %call4, 16
257  %or6 = or i32 %or3, %and5
258  %call7 = tail call signext i32 @foo(i32 signext 1) #0
259  %and8 = and i32 %call7, 32
260  %or9 = or i32 %or6, %and8
261  %call10 = tail call signext i32 @foo(i32 signext 0) #0
262  %and11 = and i32 %call10, 64
263  %or12 = or i32 %or9, %and11
264  %call13 = tail call signext i32 @foo(i32 signext 4) #0
265  %and14 = and i32 %call13, 128
266  %or15 = or i32 %or12, %and14
267  %sub = sub i32 %or15, 5
268  %shl = shl i32 %sub, 10
269  ret i32 %shl
270
271; CHECK-LABEL: @tar4
272; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
273; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
274; CHECK: tail call signext i32 @foo(i32 signext 2)
275; CHECK: tail call signext i32 @foo(i32 signext 1)
276; CHECK: tail call signext i32 @foo(i32 signext 0)
277; CHECK: tail call signext i32 @foo(i32 signext 4)
278; CHECK: ret i32
279}
280
281; Function Attrs: nounwind readnone
282define signext i32 @tar5(i32 signext %x) #0 {
283entry:
284  %call = tail call signext i32 @foo(i32 signext 5) #0
285  %and = and i32 %call, 33554432
286  %or = or i32 %and, %x
287  %call1 = tail call signext i32 @foo(i32 signext 3) #0
288  %and2 = and i32 %call1, 67108864
289  %or3 = or i32 %or, %and2
290  %call4 = tail call signext i32 @foo(i32 signext 2) #0
291  %and5 = and i32 %call4, 16
292  %or6 = or i32 %or3, %and5
293  %call7 = tail call signext i32 @foo(i32 signext 1) #0
294  %and8 = and i32 %call7, 32
295  %or9 = or i32 %or6, %and8
296  %call10 = tail call signext i32 @foo(i32 signext 0) #0
297  %and11 = and i32 %call10, 64
298  %or12 = or i32 %or9, %and11
299  %call13 = tail call signext i32 @foo(i32 signext 4) #0
300  %and14 = and i32 %call13, 128
301  %or15 = or i32 %or12, %and14
302  %xor = xor i32 %or15, 5
303  %shl = shl i32 %xor, 10
304  ret i32 %shl
305
306; CHECK-LABEL: @tar5
307; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
308; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
309; CHECK: tail call signext i32 @foo(i32 signext 2)
310; CHECK: tail call signext i32 @foo(i32 signext 1)
311; CHECK: tail call signext i32 @foo(i32 signext 0)
312; CHECK: tail call signext i32 @foo(i32 signext 4)
313; CHECK: ret i32
314}
315
316; Function Attrs: nounwind readnone
317define signext i32 @tar7(i32 signext %x, i1 %b) #0 {
318entry:
319  %call = tail call signext i32 @foo(i32 signext 5) #0
320  %and = and i32 %call, 33554432
321  %or = or i32 %and, %x
322  %call1 = tail call signext i32 @foo(i32 signext 3) #0
323  %and2 = and i32 %call1, 67108864
324  %or3 = or i32 %or, %and2
325  %call4 = tail call signext i32 @foo(i32 signext 2) #0
326  %and5 = and i32 %call4, 16
327  %or6 = or i32 %or3, %and5
328  %call7 = tail call signext i32 @foo(i32 signext 1) #0
329  %and8 = and i32 %call7, 32
330  %or9 = or i32 %or6, %and8
331  %call10 = tail call signext i32 @foo(i32 signext 0) #0
332  %and11 = and i32 %call10, 64
333  %or12 = or i32 %or9, %and11
334  %call13 = tail call signext i32 @foo(i32 signext 4) #0
335  %and14 = and i32 %call13, 128
336  %or15 = or i32 %or12, %and14
337  %v = select i1 %b, i32 %or15, i32 5
338  %shl = shl i32 %v, 10
339  ret i32 %shl
340
341; CHECK-LABEL: @tar7
342; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
343; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
344; CHECK: tail call signext i32 @foo(i32 signext 2)
345; CHECK: tail call signext i32 @foo(i32 signext 1)
346; CHECK: tail call signext i32 @foo(i32 signext 0)
347; CHECK: tail call signext i32 @foo(i32 signext 4)
348; CHECK: ret i32
349}
350
351; Function Attrs: nounwind readnone
352define signext i16 @tar8(i32 signext %x) #0 {
353entry:
354  %call = tail call signext i32 @foo(i32 signext 5) #0
355  %and = and i32 %call, 33554432
356  %or = or i32 %and, %x
357  %call1 = tail call signext i32 @foo(i32 signext 3) #0
358  %and2 = and i32 %call1, 67108864
359  %or3 = or i32 %or, %and2
360  %call4 = tail call signext i32 @foo(i32 signext 2) #0
361  %and5 = and i32 %call4, 16
362  %or6 = or i32 %or3, %and5
363  %call7 = tail call signext i32 @foo(i32 signext 1) #0
364  %and8 = and i32 %call7, 32
365  %or9 = or i32 %or6, %and8
366  %call10 = tail call signext i32 @foo(i32 signext 0) #0
367  %and11 = and i32 %call10, 64
368  %or12 = or i32 %or9, %and11
369  %call13 = tail call signext i32 @foo(i32 signext 4) #0
370  %and14 = and i32 %call13, 128
371  %or15 = or i32 %or12, %and14
372  %tr = trunc i32 %or15 to i16
373  ret i16 %tr
374
375; CHECK-LABEL: @tar8
376; CHECK-NOT: tail call signext i32 @foo(i32 signext 5)
377; CHECK-NOT: tail call signext i32 @foo(i32 signext 3)
378; CHECK: tail call signext i32 @foo(i32 signext 2)
379; CHECK: tail call signext i32 @foo(i32 signext 1)
380; CHECK: tail call signext i32 @foo(i32 signext 0)
381; CHECK: tail call signext i32 @foo(i32 signext 4)
382; CHECK: ret i16
383}
384
385; DEBUGIFY-LABEL: @tar9
386define signext i16 @tar9(i32 signext %x) #0 {
387entry:
388  %call = tail call signext i32 @foo(i32 signext 5) #0
389  %and = and i32 %call, 33554432
390; DEBUGIFY: call void @llvm.dbg.value(metadata i32 %call, metadata {{.*}}, metadata !DIExpression(DW_OP_constu, 33554432, DW_OP_and, DW_OP_stack_value))
391  %cast = trunc i32 %call to i16
392  ret i16 %cast
393}
394
395attributes #0 = { nounwind readnone }
396attributes #1 = { nounwind }
397
398