1 ; bswap should be constant folded when it is passed a constant argument
3 ; RUN: llc < %s -mtriple=i686-- -mcpu=i686 | FileCheck %s
4 ; RUN: llc < %s -mtriple=x86_64-- | FileCheck %s --check-prefix=CHECK64
6 declare i16 @llvm.bswap.i16(i16)
8 declare i32 @llvm.bswap.i32(i32)
10 declare i64 @llvm.bswap.i64(i64)
12 define i16 @W(i16 %A) {
18 %Z = call i16 @llvm.bswap.i16( i16 %A ) ; <i16> [#uses=1]
22 define i32 @X(i32 %A) {
28 %Z = call i32 @llvm.bswap.i32( i32 %A ) ; <i32> [#uses=1]
32 define i64 @Y(i64 %A) {
39 %Z = call i64 @llvm.bswap.i64( i64 %A ) ; <i64> [#uses=1]
44 define i32 @test1(i32 %a) nounwind readnone {
47 ; CHECK: bswapl [[REG:%.*]]
48 ; CHECK: shrl $16, [[REG]]
50 ; CHECK64-LABEL: test1:
51 ; CHECK64: bswapl [[REG:%.*]]
52 ; CHECK64: shrl $16, [[REG]]
54 %shr3 = and i32 %and, 255
56 %shl = and i32 %and2, 65280
57 %or = or i32 %shr3, %shl
61 define i32 @test2(i32 %a) nounwind readnone {
64 ; CHECK: bswapl [[REG:%.*]]
65 ; CHECK: sarl $16, [[REG]]
67 ; CHECK64-LABEL: test2:
68 ; CHECK64: bswapl [[REG:%.*]]
69 ; CHECK64: sarl $16, [[REG]]
71 %shr4 = and i32 %and, 255
73 %or = or i32 %shr4, %and2
74 %sext = shl i32 %or, 16
75 %conv3 = ashr exact i32 %sext, 16
82 ; The "shl" below can move bits into the high parts of the value, so the
83 ; operation is not a "bswap, shr" pair.
85 ; rdar://problem/14814049
86 define i64 @not_bswap() {
87 ; CHECK-LABEL: not_bswap:
91 ; CHECK64-LABEL: not_bswap:
94 %init = load i16, i16* @var16
95 %big = zext i16 %init to i64
97 %hishifted = lshr i64 %big, 8
98 %loshifted = shl i64 %big, 8
100 %notswapped = or i64 %hishifted, %loshifted
105 ; This time, the lshr (and subsequent or) is completely useless. While it's
106 ; technically correct to convert this into a "bswap, shr", it's suboptimal. A
107 ; simple shl works better.
109 define i64 @not_useful_bswap() {
110 ; CHECK-LABEL: not_useful_bswap:
114 ; CHECK64-LABEL: not_useful_bswap:
115 ; CHECK64-NOT: bswapq
118 %init = load i8, i8* @var8
119 %big = zext i8 %init to i64
121 %hishifted = lshr i64 %big, 8
122 %loshifted = shl i64 %big, 8
124 %notswapped = or i64 %hishifted, %loshifted
129 ; Finally, it *is* OK to just mask off the shl if we know that the value is zero
130 ; beyond 16 bits anyway. This is a legitimate bswap.
132 define i64 @finally_useful_bswap() {
133 ; CHECK-LABEL: finally_useful_bswap:
134 ; CHECK: bswapl [[REG:%.*]]
135 ; CHECK: shrl $16, [[REG]]
138 ; CHECK64-LABEL: finally_useful_bswap:
139 ; CHECK64: bswapq [[REG:%.*]]
140 ; CHECK64: shrq $48, [[REG]]
143 %init = load i16, i16* @var16
144 %big = zext i16 %init to i64
146 %hishifted = lshr i64 %big, 8
147 %lomasked = and i64 %big, 255
148 %loshifted = shl i64 %lomasked, 8
150 %swapped = or i64 %hishifted, %loshifted