aed3550a9af2faf317666113ab208cd67f01900c
[oota-llvm.git] / test / CodeGen / X86 / dagcombine-shifts.ll
1 ; SCE: bug 39153
2
3 ; RUN: llc < %s -mtriple=x86_64-linux-gnu | FileCheck %s
4
5 ; fold (shl (zext (lshr (A, X))), X) -> (zext (shl (lshr (A, X)), X))
6
7 ; Canolicalize the sequence shl/zext/lshr performing the zeroextend
8 ; as the last instruction of the sequence.
9 ; This will help DAGCombiner to identify and then fold the sequence
10 ; of shifts into a single AND.
11 ; This transformation is profitable if the shift amounts are the same
12 ; and if there is only one use of the zext.
13
14 define i16 @fun1(i8 zeroext %v) {
15 entry:
16   %shr = lshr i8 %v, 4
17   %ext = zext i8 %shr to i16
18   %shl = shl i16 %ext, 4
19   ret i16 %shl
20 }
21
22 ; CHECK-LABEL: @fun1
23 ; CHECK: and
24 ; CHECK-NOT: shr
25 ; CHECK-NOT: shl
26 ; CHECK: ret
27
28 define i32 @fun2(i8 zeroext %v) {
29 entry:
30   %shr = lshr i8 %v, 4
31   %ext = zext i8 %shr to i32
32   %shl = shl i32 %ext, 4
33   ret i32 %shl
34 }
35
36 ; CHECK-LABEL: @fun2
37 ; CHECK: and
38 ; CHECK-NOT: shr
39 ; CHECK-NOT: shl
40 ; CHECK: ret
41
42 define i32 @fun3(i16 zeroext %v) {
43 entry:
44   %shr = lshr i16 %v, 4
45   %ext = zext i16 %shr to i32
46   %shl = shl i32 %ext, 4
47   ret i32 %shl
48 }
49
50 ; CHECK-LABEL: @fun3
51 ; CHECK: and
52 ; CHECK-NOT: shr
53 ; CHECK-NOT: shl
54 ; CHECK: ret
55
56 define i64 @fun4(i8 zeroext %v) {
57 entry:
58   %shr = lshr i8 %v, 4
59   %ext = zext i8 %shr to i64
60   %shl = shl i64 %ext, 4
61   ret i64 %shl
62 }
63
64 ; CHECK-LABEL: @fun4
65 ; CHECK: and
66 ; CHECK-NOT: shr
67 ; CHECK-NOT: shl
68 ; CHECK: ret
69
70 define i64 @fun5(i16 zeroext %v) {
71 entry:
72   %shr = lshr i16 %v, 4
73   %ext = zext i16 %shr to i64
74   %shl = shl i64 %ext, 4
75   ret i64 %shl
76 }
77
78 ; CHECK-LABEL: @fun5
79 ; CHECK: and
80 ; CHECK-NOT: shr
81 ; CHECK-NOT: shl
82 ; CHECK: ret
83
84 define i64 @fun6(i32 zeroext %v) {
85 entry:
86   %shr = lshr i32 %v, 4
87   %ext = zext i32 %shr to i64
88   %shl = shl i64 %ext, 4
89   ret i64 %shl
90 }
91
92 ; CHECK-LABEL: @fun6
93 ; CHECK: and
94 ; CHECK-NOT: shr
95 ; CHECK-NOT: shl
96 ; CHECK: ret
97
98 ; Don't fold the pattern if we use arithmetic shifts.
99
100 define i64 @fun7(i8 zeroext %v) {
101 entry:
102   %shr = ashr i8 %v, 4
103   %ext = zext i8 %shr to i64
104   %shl = shl i64 %ext, 4
105   ret i64 %shl
106 }
107
108 ; CHECK-LABEL: @fun7
109 ; CHECK: sar
110 ; CHECK: shl
111 ; CHECK: ret
112
113 define i64 @fun8(i16 zeroext %v) {
114 entry:
115   %shr = ashr i16 %v, 4
116   %ext = zext i16 %shr to i64
117   %shl = shl i64 %ext, 4
118   ret i64 %shl
119 }
120
121 ; CHECK-LABEL: @fun8
122 ; CHECK: sar
123 ; CHECK: shl
124 ; CHECK: ret
125
126 define i64 @fun9(i32 zeroext %v) {
127 entry:
128   %shr = ashr i32 %v, 4
129   %ext = zext i32 %shr to i64
130   %shl = shl i64 %ext, 4
131   ret i64 %shl
132 }
133
134 ; CHECK-LABEL: @fun9
135 ; CHECK: sar
136 ; CHECK: shl
137 ; CHECK: ret
138
139 ; Don't fold the pattern if there is more than one use of the
140 ; operand in input to the shift left.
141
142 define i64 @fun10(i8 zeroext %v) {
143 entry:
144   %shr = lshr i8 %v, 4
145   %ext = zext i8 %shr to i64
146   %shl = shl i64 %ext, 4
147   %add = add i64 %shl, %ext
148   ret i64 %add
149 }
150
151 ; CHECK-LABEL: @fun10
152 ; CHECK: shr
153 ; CHECK: shl
154 ; CHECK: ret
155
156 define i64 @fun11(i16 zeroext %v) {
157 entry:
158   %shr = lshr i16 %v, 4
159   %ext = zext i16 %shr to i64
160   %shl = shl i64 %ext, 4
161   %add = add i64 %shl, %ext
162   ret i64 %add
163 }
164
165 ; CHECK-LABEL: @fun11
166 ; CHECK: shr
167 ; CHECK: shl
168 ; CHECK: ret
169
170 define i64 @fun12(i32 zeroext %v) {
171 entry:
172   %shr = lshr i32 %v, 4
173   %ext = zext i32 %shr to i64
174   %shl = shl i64 %ext, 4
175   %add = add i64 %shl, %ext
176   ret i64 %add
177 }
178
179 ; CHECK-LABEL: @fun12
180 ; CHECK: shr
181 ; CHECK: shl
182 ; CHECK: ret
183
184 ; PR17380
185 ; Make sure that the combined dags are legal if we run the DAGCombiner after
186 ; Legalization took place. The add instruction is redundant and increases by 
187 ; one the number of uses of the zext. This prevents the transformation from
188 ; firing before dags are legalized and optimized.
189 ; Once the add is removed, the number of uses becomes one and therefore the
190 ; dags are canonicalized. After Legalization, we need to make sure that the
191 ; valuetype for the shift count is legal.
192
193 define void @g(i32 %a) {
194   %b = lshr i32 %a, 2
195   %c = zext i32 %b to i64
196   %d = add i64 %c, 1
197   %e = shl i64 %c, 2
198   tail call void @f(i64 %e)
199   ret void
200 }
201
202 declare void @f(i64)
203