1; RUN: opt < %s -instcombine -S | FileCheck %s
2
3target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
4
5define i32 @test1(i32 %i) {
6; CHECK-LABEL: @test1(
7; CHECK-NEXT:    [[TMP12:%.*]] = call i32 @llvm.bswap.i32(i32 %i)
8; CHECK-NEXT:    ret i32 [[TMP12]]
9;
10  %tmp1 = lshr i32 %i, 24
11  %tmp3 = lshr i32 %i, 8
12  %tmp4 = and i32 %tmp3, 65280
13  %tmp5 = or i32 %tmp1, %tmp4
14  %tmp7 = shl i32 %i, 8
15  %tmp8 = and i32 %tmp7, 16711680
16  %tmp9 = or i32 %tmp5, %tmp8
17  %tmp11 = shl i32 %i, 24
18  %tmp12 = or i32 %tmp9, %tmp11
19  ret i32 %tmp12
20}
21
22define i32 @test2(i32 %arg) {
23; CHECK-LABEL: @test2(
24; CHECK-NEXT:    [[TMP14:%.*]] = call i32 @llvm.bswap.i32(i32 %arg)
25; CHECK-NEXT:    ret i32 [[TMP14]]
26;
27  %tmp2 = shl i32 %arg, 24
28  %tmp4 = shl i32 %arg, 8
29  %tmp5 = and i32 %tmp4, 16711680
30  %tmp6 = or i32 %tmp2, %tmp5
31  %tmp8 = lshr i32 %arg, 8
32  %tmp9 = and i32 %tmp8, 65280
33  %tmp10 = or i32 %tmp6, %tmp9
34  %tmp12 = lshr i32 %arg, 24
35  %tmp14 = or i32 %tmp10, %tmp12
36  ret i32 %tmp14
37}
38
39define i16 @test3(i16 %s) {
40; CHECK-LABEL: @test3(
41; CHECK-NEXT:    [[TMP5:%.*]] = call i16 @llvm.bswap.i16(i16 %s)
42; CHECK-NEXT:    ret i16 [[TMP5]]
43;
44  %tmp2 = lshr i16 %s, 8
45  %tmp4 = shl i16 %s, 8
46  %tmp5 = or i16 %tmp2, %tmp4
47  ret i16 %tmp5
48}
49
50define i16 @test4(i16 %s) {
51; CHECK-LABEL: @test4(
52; CHECK-NEXT:    [[TMP5:%.*]] = call i16 @llvm.bswap.i16(i16 %s)
53; CHECK-NEXT:    ret i16 [[TMP5]]
54;
55  %tmp2 = lshr i16 %s, 8
56  %tmp4 = shl i16 %s, 8
57  %tmp5 = or i16 %tmp4, %tmp2
58  ret i16 %tmp5
59}
60
61define i16 @test5(i16 %a) {
62; CHECK-LABEL: @test5(
63; CHECK-NEXT:    [[TMP_UPGRD_3:%.*]] = call i16 @llvm.bswap.i16(i16 %a)
64; CHECK-NEXT:    ret i16 [[TMP_UPGRD_3]]
65;
66  %tmp = zext i16 %a to i32
67  %tmp1 = and i32 %tmp, 65280
68  %tmp2 = ashr i32 %tmp1, 8
69  %tmp2.upgrd.1 = trunc i32 %tmp2 to i16
70  %tmp4 = and i32 %tmp, 255
71  %tmp5 = shl i32 %tmp4, 8
72  %tmp5.upgrd.2 = trunc i32 %tmp5 to i16
73  %tmp.upgrd.3 = or i16 %tmp2.upgrd.1, %tmp5.upgrd.2
74  %tmp6 = bitcast i16 %tmp.upgrd.3 to i16
75  %tmp6.upgrd.4 = zext i16 %tmp6 to i32
76  %retval = trunc i32 %tmp6.upgrd.4 to i16
77  ret i16 %retval
78}
79
80; PR2842
81define i32 @test6(i32 %x) nounwind readnone {
82; CHECK-LABEL: @test6(
83; CHECK-NEXT:    [[TMP7:%.*]] = call i32 @llvm.bswap.i32(i32 %x)
84; CHECK-NEXT:    ret i32 [[TMP7]]
85;
86  %tmp = shl i32 %x, 16
87  %x.mask = and i32 %x, 65280
88  %tmp1 = lshr i32 %x, 16
89  %tmp2 = and i32 %tmp1, 255
90  %tmp3 = or i32 %x.mask, %tmp
91  %tmp4 = or i32 %tmp3, %tmp2
92  %tmp5 = shl i32 %tmp4, 8
93  %tmp6 = lshr i32 %x, 24
94  %tmp7 = or i32 %tmp5, %tmp6
95  ret i32 %tmp7
96}
97
98declare void @extra_use(i32)
99
100; swaphalf = (x << 16 | x >> 16)
101; ((swaphalf & 0x00ff00ff) << 8) | ((swaphalf >> 8) & 0x00ff00ff)
102
103define i32 @bswap32_and_first(i32 %x) {
104; CHECK-LABEL: @bswap32_and_first(
105; CHECK-NEXT:    [[BSWAP:%.*]] = call i32 @llvm.bswap.i32(i32 %x)
106; CHECK-NEXT:    ret i32 [[BSWAP]]
107;
108  %shl = shl i32 %x, 16
109  %shr = lshr i32 %x, 16
110  %swaphalf = or i32 %shl, %shr
111  %t = and i32 %swaphalf, 16711935
112  %tshl = shl nuw i32 %t, 8
113  %b = lshr i32 %swaphalf, 8
114  %band = and i32 %b, 16711935
115  %bswap = or i32 %tshl, %band
116  ret i32 %bswap
117}
118
119; Extra use should not prevent matching to bswap.
120; swaphalf = (x << 16 | x >> 16)
121; ((swaphalf & 0x00ff00ff) << 8) | ((swaphalf >> 8) & 0x00ff00ff)
122
123define i32 @bswap32_and_first_extra_use(i32 %x) {
124; CHECK-LABEL: @bswap32_and_first_extra_use(
125; CHECK-NEXT:    [[SHL:%.*]] = shl i32 %x, 16
126; CHECK-NEXT:    [[SHR:%.*]] = lshr i32 %x, 16
127; CHECK-NEXT:    [[SWAPHALF:%.*]] = or i32 [[SHL]], [[SHR]]
128; CHECK-NEXT:    [[T:%.*]] = and i32 [[SWAPHALF]], 16711935
129; CHECK-NEXT:    [[BSWAP:%.*]] = call i32 @llvm.bswap.i32(i32 %x)
130; CHECK-NEXT:    call void @extra_use(i32 [[T]])
131; CHECK-NEXT:    ret i32 [[BSWAP]]
132;
133  %shl = shl i32 %x, 16
134  %shr = lshr i32 %x, 16
135  %swaphalf = or i32 %shl, %shr
136  %t = and i32 %swaphalf, 16711935
137  %tshl = shl nuw i32 %t, 8
138  %b = lshr i32 %swaphalf, 8
139  %band = and i32 %b, 16711935
140  %bswap = or i32 %tshl, %band
141  call void @extra_use(i32 %t)
142  ret i32 %bswap
143}
144
145; swaphalf = (x << 16 | x >> 16)
146; ((swaphalf << 8) & 0xff00ff00) | ((swaphalf >> 8) & 0x00ff00ff)
147
148; PR23863
149define i32 @bswap32_shl_first(i32 %x) {
150; CHECK-LABEL: @bswap32_shl_first(
151; CHECK-NEXT:    [[BSWAP:%.*]] = call i32 @llvm.bswap.i32(i32 %x)
152; CHECK-NEXT:    ret i32 [[BSWAP]]
153;
154  %shl = shl i32 %x, 16
155  %shr = lshr i32 %x, 16
156  %swaphalf = or i32 %shl, %shr
157  %t = shl i32 %swaphalf, 8
158  %tand = and i32 %t, -16711936
159  %b = lshr i32 %swaphalf, 8
160  %band = and i32 %b, 16711935
161  %bswap = or i32 %tand, %band
162  ret i32 %bswap
163}
164
165; Extra use should not prevent matching to bswap.
166; swaphalf = (x << 16 | x >> 16)
167; ((swaphalf << 8) & 0xff00ff00) | ((swaphalf >> 8) & 0x00ff00ff)
168
169define i32 @bswap32_shl_first_extra_use(i32 %x) {
170; CHECK-LABEL: @bswap32_shl_first_extra_use(
171; CHECK-NEXT:    [[SHL:%.*]] = shl i32 %x, 16
172; CHECK-NEXT:    [[SHR:%.*]] = lshr i32 %x, 16
173; CHECK-NEXT:    [[SWAPHALF:%.*]] = or i32 [[SHL]], [[SHR]]
174; CHECK-NEXT:    [[T:%.*]] = shl i32 [[SWAPHALF]], 8
175; CHECK-NEXT:    [[BSWAP:%.*]] = call i32 @llvm.bswap.i32(i32 %x)
176; CHECK-NEXT:    call void @extra_use(i32 [[T]])
177; CHECK-NEXT:    ret i32 [[BSWAP]]
178;
179  %shl = shl i32 %x, 16
180  %shr = lshr i32 %x, 16
181  %swaphalf = or i32 %shl, %shr
182  %t = shl i32 %swaphalf, 8
183  %tand = and i32 %t, -16711936
184  %b = lshr i32 %swaphalf, 8
185  %band = and i32 %b, 16711935
186  %bswap = or i32 %tand, %band
187  call void @extra_use(i32 %t)
188  ret i32 %bswap
189}
190
191define i16 @test8(i16 %a) {
192; CHECK-LABEL: @test8(
193; CHECK-NEXT:    [[REV:%.*]] = call i16 @llvm.bswap.i16(i16 %a)
194; CHECK-NEXT:    ret i16 [[REV]]
195;
196  %conv = zext i16 %a to i32
197  %shr = lshr i16 %a, 8
198  %shl = shl i32 %conv, 8
199  %conv1 = zext i16 %shr to i32
200  %or = or i32 %conv1, %shl
201  %conv2 = trunc i32 %or to i16
202  ret i16 %conv2
203}
204
205define i16 @test9(i16 %a) {
206; CHECK-LABEL: @test9(
207; CHECK-NEXT:    [[REV:%.*]] = call i16 @llvm.bswap.i16(i16 %a)
208; CHECK-NEXT:    ret i16 [[REV]]
209;
210  %conv = zext i16 %a to i32
211  %shr = lshr i32 %conv, 8
212  %shl = shl i32 %conv, 8
213  %or = or i32 %shr, %shl
214  %conv2 = trunc i32 %or to i16
215  ret i16 %conv2
216}
217
218define i16 @test10(i32 %a) {
219; CHECK-LABEL: @test10(
220; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 %a to i16
221; CHECK-NEXT:    [[REV:%.*]] = call i16 @llvm.bswap.i16(i16 [[TRUNC]])
222; CHECK-NEXT:    ret i16 [[REV]]
223;
224  %shr1 = lshr i32 %a, 8
225  %and1 = and i32 %shr1, 255
226  %and2 = shl i32 %a, 8
227  %shl1 = and i32 %and2, 65280
228  %or = or i32 %and1, %shl1
229  %conv = trunc i32 %or to i16
230  ret i16 %conv
231}
232
233