[InstCombine] Recognize another bswap idiom.
[oota-llvm.git] / test / Transforms / InstCombine / bswap.ll
1 target 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"
2
3 ; RUN: opt < %s -instcombine -S | \
4 ; RUN:    grep "call.*llvm.bswap" | count 7
5
6 define i32 @test1(i32 %i) {
7         %tmp1 = lshr i32 %i, 24         ; <i32> [#uses=1]
8         %tmp3 = lshr i32 %i, 8          ; <i32> [#uses=1]
9         %tmp4 = and i32 %tmp3, 65280            ; <i32> [#uses=1]
10         %tmp5 = or i32 %tmp1, %tmp4             ; <i32> [#uses=1]
11         %tmp7 = shl i32 %i, 8           ; <i32> [#uses=1]
12         %tmp8 = and i32 %tmp7, 16711680         ; <i32> [#uses=1]
13         %tmp9 = or i32 %tmp5, %tmp8             ; <i32> [#uses=1]
14         %tmp11 = shl i32 %i, 24         ; <i32> [#uses=1]
15         %tmp12 = or i32 %tmp9, %tmp11           ; <i32> [#uses=1]
16         ret i32 %tmp12
17 }
18
19 define i32 @test2(i32 %arg) {
20         %tmp2 = shl i32 %arg, 24                ; <i32> [#uses=1]
21         %tmp4 = shl i32 %arg, 8         ; <i32> [#uses=1]
22         %tmp5 = and i32 %tmp4, 16711680         ; <i32> [#uses=1]
23         %tmp6 = or i32 %tmp2, %tmp5             ; <i32> [#uses=1]
24         %tmp8 = lshr i32 %arg, 8                ; <i32> [#uses=1]
25         %tmp9 = and i32 %tmp8, 65280            ; <i32> [#uses=1]
26         %tmp10 = or i32 %tmp6, %tmp9            ; <i32> [#uses=1]
27         %tmp12 = lshr i32 %arg, 24              ; <i32> [#uses=1]
28         %tmp14 = or i32 %tmp10, %tmp12          ; <i32> [#uses=1]
29         ret i32 %tmp14
30 }
31
32 define i16 @test3(i16 %s) {
33         %tmp2 = lshr i16 %s, 8          ; <i16> [#uses=1]
34         %tmp4 = shl i16 %s, 8           ; <i16> [#uses=1]
35         %tmp5 = or i16 %tmp2, %tmp4             ; <i16> [#uses=1]
36         ret i16 %tmp5
37 }
38
39 define i16 @test4(i16 %s) {
40         %tmp2 = lshr i16 %s, 8          ; <i16> [#uses=1]
41         %tmp4 = shl i16 %s, 8           ; <i16> [#uses=1]
42         %tmp5 = or i16 %tmp4, %tmp2             ; <i16> [#uses=1]
43         ret i16 %tmp5
44 }
45
46 define i16 @test5(i16 %a) {
47         %tmp = zext i16 %a to i32               ; <i32> [#uses=2]
48         %tmp1 = and i32 %tmp, 65280             ; <i32> [#uses=1]
49         %tmp2 = ashr i32 %tmp1, 8               ; <i32> [#uses=1]
50         %tmp2.upgrd.1 = trunc i32 %tmp2 to i16          ; <i16> [#uses=1]
51         %tmp4 = and i32 %tmp, 255               ; <i32> [#uses=1]
52         %tmp5 = shl i32 %tmp4, 8                ; <i32> [#uses=1]
53         %tmp5.upgrd.2 = trunc i32 %tmp5 to i16          ; <i16> [#uses=1]
54         %tmp.upgrd.3 = or i16 %tmp2.upgrd.1, %tmp5.upgrd.2              ; <i16> [#uses=1]
55         %tmp6 = bitcast i16 %tmp.upgrd.3 to i16         ; <i16> [#uses=1]
56         %tmp6.upgrd.4 = zext i16 %tmp6 to i32           ; <i32> [#uses=1]
57         %retval = trunc i32 %tmp6.upgrd.4 to i16                ; <i16> [#uses=1]
58         ret i16 %retval
59 }
60
61 ; PR2842
62 define i32 @test6(i32 %x) nounwind readnone {
63         %tmp = shl i32 %x, 16           ; <i32> [#uses=1]
64         %x.mask = and i32 %x, 65280             ; <i32> [#uses=1]
65         %tmp1 = lshr i32 %x, 16         ; <i32> [#uses=1]
66         %tmp2 = and i32 %tmp1, 255              ; <i32> [#uses=1]
67         %tmp3 = or i32 %x.mask, %tmp            ; <i32> [#uses=1]
68         %tmp4 = or i32 %tmp3, %tmp2             ; <i32> [#uses=1]
69         %tmp5 = shl i32 %tmp4, 8                ; <i32> [#uses=1]
70         %tmp6 = lshr i32 %x, 24         ; <i32> [#uses=1]
71         %tmp7 = or i32 %tmp5, %tmp6             ; <i32> [#uses=1]
72         ret i32 %tmp7
73 }
74
75 ; PR23863
76 define i32 @test7(i32 %x) {
77         %shl = shl i32 %x, 16
78         %shr = lshr i32 %x, 16
79         %or = or i32 %shl, %shr
80         %and2 = shl i32 %or, 8
81         %shl3 = and i32 %and2, -16711936
82         %and4 = lshr i32 %or, 8
83         %shr5 = and i32 %and4, 16711935
84         %or6 = or i32 %shl3, %shr5
85         ret i32 %or6
86 }