1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -aggressive-instcombine -S | FileCheck %s
4 define i16 @ashr_15(i16 %x) {
5 ; CHECK-LABEL: @ashr_15(
6 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i16 [[X:%.*]] to i32
7 ; CHECK-NEXT: [[ASHR:%.*]] = ashr i32 [[ZEXT]], 15
8 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[ASHR]] to i16
9 ; CHECK-NEXT: ret i16 [[TRUNC]]
11 %zext = zext i16 %x to i32
12 %ashr = ashr i32 %zext, 15
13 %trunc = trunc i32 %ashr to i16
19 define i16 @ashr_16(i16 %x) {
20 ; CHECK-LABEL: @ashr_16(
21 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i16 [[X:%.*]] to i32
22 ; CHECK-NEXT: [[ASHR:%.*]] = ashr i32 [[ZEXT]], 16
23 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[ASHR]] to i16
24 ; CHECK-NEXT: ret i16 [[TRUNC]]
26 %zext = zext i16 %x to i32
27 %ashr = ashr i32 %zext, 16
28 %trunc = trunc i32 %ashr to i16
34 define i16 @ashr_var_shift_amount(i8 %x, i8 %amt) {
35 ; CHECK-LABEL: @ashr_var_shift_amount(
36 ; CHECK-NEXT: [[Z:%.*]] = zext i8 [[X:%.*]] to i32
37 ; CHECK-NEXT: [[ZA:%.*]] = zext i8 [[AMT:%.*]] to i32
38 ; CHECK-NEXT: [[S:%.*]] = ashr i32 [[Z]], [[ZA]]
39 ; CHECK-NEXT: [[A:%.*]] = add i32 [[S]], [[Z]]
40 ; CHECK-NEXT: [[S2:%.*]] = ashr i32 [[A]], 2
41 ; CHECK-NEXT: [[T:%.*]] = trunc i32 [[S2]] to i16
42 ; CHECK-NEXT: ret i16 [[T]]
44 %z = zext i8 %x to i32
45 %za = zext i8 %amt to i32
49 %t = trunc i32 %s2 to i16
53 define i16 @ashr_var_bounded_shift_amount(i8 %x, i8 %amt) {
54 ; CHECK-LABEL: @ashr_var_bounded_shift_amount(
55 ; CHECK-NEXT: [[Z:%.*]] = zext i8 [[X:%.*]] to i32
56 ; CHECK-NEXT: [[ZA:%.*]] = zext i8 [[AMT:%.*]] to i32
57 ; CHECK-NEXT: [[ZA2:%.*]] = and i32 [[ZA]], 15
58 ; CHECK-NEXT: [[S:%.*]] = ashr i32 [[Z]], [[ZA2]]
59 ; CHECK-NEXT: [[A:%.*]] = add i32 [[S]], [[Z]]
60 ; CHECK-NEXT: [[S2:%.*]] = ashr i32 [[A]], 2
61 ; CHECK-NEXT: [[T:%.*]] = trunc i32 [[S2]] to i16
62 ; CHECK-NEXT: ret i16 [[T]]
64 %z = zext i8 %x to i32
65 %za = zext i8 %amt to i32
66 %za2 = and i32 %za, 15
67 %s = ashr i32 %z, %za2
70 %t = trunc i32 %s2 to i16
76 define i32 @ashr_check_no_overflow(i32 %x, i16 %amt) {
77 ; CHECK-LABEL: @ashr_check_no_overflow(
78 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i32 [[X:%.*]] to i64
79 ; CHECK-NEXT: [[SEXT:%.*]] = sext i16 [[AMT:%.*]] to i64
80 ; CHECK-NEXT: [[AND:%.*]] = and i64 [[SEXT]], 4294967295
81 ; CHECK-NEXT: [[SHL:%.*]] = ashr i64 [[ZEXT]], [[AND]]
82 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i64 [[SHL]] to i32
83 ; CHECK-NEXT: ret i32 [[TRUNC]]
85 %zext = zext i32 %x to i64
86 %sext = sext i16 %amt to i64
87 %and = and i64 %sext, 4294967295
88 %shl = ashr i64 %zext, %and
89 %trunc = trunc i64 %shl to i32
93 define void @ashr_big_dag(i16* %a, i8 %b, i8 %c) {
94 ; CHECK-LABEL: @ashr_big_dag(
95 ; CHECK-NEXT: [[ZEXT1:%.*]] = zext i8 [[B:%.*]] to i32
96 ; CHECK-NEXT: [[ZEXT2:%.*]] = zext i8 [[C:%.*]] to i32
97 ; CHECK-NEXT: [[ADD1:%.*]] = add i32 [[ZEXT1]], [[ZEXT2]]
98 ; CHECK-NEXT: [[SFT1:%.*]] = and i32 [[ADD1]], 15
99 ; CHECK-NEXT: [[SHR1:%.*]] = ashr i32 [[ADD1]], [[SFT1]]
100 ; CHECK-NEXT: [[ADD2:%.*]] = add i32 [[ADD1]], [[SHR1]]
101 ; CHECK-NEXT: [[SFT2:%.*]] = and i32 [[ADD2]], 7
102 ; CHECK-NEXT: [[SHR2:%.*]] = ashr i32 [[ADD2]], [[SFT2]]
103 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[SHR2]] to i16
104 ; CHECK-NEXT: store i16 [[TRUNC]], i16* [[A:%.*]], align 2
105 ; CHECK-NEXT: ret void
107 %zext1 = zext i8 %b to i32
108 %zext2 = zext i8 %c to i32
109 %add1 = add i32 %zext1, %zext2
110 %sft1 = and i32 %add1, 15
111 %shr1 = ashr i32 %add1, %sft1
112 %add2 = add i32 %add1, %shr1
113 %sft2 = and i32 %add2, 7
114 %shr2 = ashr i32 %add2, %sft2
115 %trunc = trunc i32 %shr2 to i16
116 store i16 %trunc, i16* %a, align 2
122 define i8 @ashr_check_not_i8_trunc(i16 %x) {
123 ; CHECK-LABEL: @ashr_check_not_i8_trunc(
124 ; CHECK-NEXT: [[ASHR:%.*]] = ashr i16 [[X:%.*]], 1
125 ; CHECK-NEXT: [[ZEXT2:%.*]] = zext i16 [[ASHR]] to i32
126 ; CHECK-NEXT: [[ASHR2:%.*]] = ashr i32 [[ZEXT2]], 2
127 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[ASHR2]] to i8
128 ; CHECK-NEXT: ret i8 [[TRUNC]]
130 %ashr = ashr i16 %x, 1
131 %zext2 = zext i16 %ashr to i32
132 %ashr2 = ashr i32 %zext2, 2
133 %trunc = trunc i32 %ashr2 to i8
137 define <2 x i16> @ashr_vector(<2 x i8> %x) {
138 ; CHECK-LABEL: @ashr_vector(
139 ; CHECK-NEXT: [[Z:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>
140 ; CHECK-NEXT: [[ZA:%.*]] = and <2 x i32> [[Z]], <i32 7, i32 8>
141 ; CHECK-NEXT: [[S:%.*]] = ashr <2 x i32> [[Z]], [[ZA]]
142 ; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[S]], [[Z]]
143 ; CHECK-NEXT: [[S2:%.*]] = ashr <2 x i32> [[A]], <i32 4, i32 5>
144 ; CHECK-NEXT: [[T:%.*]] = trunc <2 x i32> [[S2]] to <2 x i16>
145 ; CHECK-NEXT: ret <2 x i16> [[T]]
147 %z = zext <2 x i8> %x to <2 x i32>
148 %za = and <2 x i32> %z, <i32 7, i32 8>
149 %s = ashr <2 x i32> %z, %za
150 %a = add <2 x i32> %s, %z
151 %s2 = ashr <2 x i32> %a, <i32 4, i32 5>
152 %t = trunc <2 x i32> %s2 to <2 x i16>
156 ; Negative test - can only fold to <2 x i16>, requiring new vector type
158 define <2 x i8> @ashr_vector_no_new_vector_type(<2 x i8> %x) {
159 ; CHECK-LABEL: @ashr_vector_no_new_vector_type(
160 ; CHECK-NEXT: [[Z:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>
161 ; CHECK-NEXT: [[ZA:%.*]] = and <2 x i32> [[Z]], <i32 7, i32 8>
162 ; CHECK-NEXT: [[S:%.*]] = ashr <2 x i32> [[Z]], [[ZA]]
163 ; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[S]], [[Z]]
164 ; CHECK-NEXT: [[S2:%.*]] = ashr <2 x i32> [[A]], <i32 4, i32 5>
165 ; CHECK-NEXT: [[T:%.*]] = trunc <2 x i32> [[S2]] to <2 x i8>
166 ; CHECK-NEXT: ret <2 x i8> [[T]]
168 %z = zext <2 x i8> %x to <2 x i32>
169 %za = and <2 x i32> %z, <i32 7, i32 8>
170 %s = ashr <2 x i32> %z, %za
171 %a = add <2 x i32> %s, %z
172 %s2 = ashr <2 x i32> %a, <i32 4, i32 5>
173 %t = trunc <2 x i32> %s2 to <2 x i8>
179 define <2 x i16> @ashr_vector_large_shift_amount(<2 x i8> %x) {
180 ; CHECK-LABEL: @ashr_vector_large_shift_amount(
181 ; CHECK-NEXT: [[Z:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>
182 ; CHECK-NEXT: [[ZA:%.*]] = and <2 x i32> [[Z]], <i32 7, i32 8>
183 ; CHECK-NEXT: [[S:%.*]] = ashr <2 x i32> [[Z]], [[ZA]]
184 ; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[S]], [[Z]]
185 ; CHECK-NEXT: [[S2:%.*]] = ashr <2 x i32> [[A]], <i32 16, i32 5>
186 ; CHECK-NEXT: [[T:%.*]] = trunc <2 x i32> [[S2]] to <2 x i16>
187 ; CHECK-NEXT: ret <2 x i16> [[T]]
189 %z = zext <2 x i8> %x to <2 x i32>
190 %za = and <2 x i32> %z, <i32 7, i32 8>
191 %s = ashr <2 x i32> %z, %za
192 %a = add <2 x i32> %s, %z
193 %s2 = ashr <2 x i32> %a, <i32 16, i32 5>
194 %t = trunc <2 x i32> %s2 to <2 x i16>
198 define i16 @ashr_exact(i16 %x) {
199 ; CHECK-LABEL: @ashr_exact(
200 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i16 [[X:%.*]] to i32
201 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[ZEXT]], 32767
202 ; CHECK-NEXT: [[ASHR:%.*]] = ashr exact i32 [[AND]], 15
203 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[ASHR]] to i16
204 ; CHECK-NEXT: ret i16 [[TRUNC]]
206 %zext = zext i16 %x to i32
207 %and = and i32 %zext, 32767
208 %ashr = ashr exact i32 %and, 15
209 %trunc = trunc i32 %ashr to i16
215 define i16 @ashr_negative_operand(i16 %x) {
216 ; CHECK-LABEL: @ashr_negative_operand(
217 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i16 [[X:%.*]] to i32
218 ; CHECK-NEXT: [[XOR:%.*]] = xor i32 -1, [[ZEXT]]
219 ; CHECK-NEXT: [[LSHR2:%.*]] = ashr i32 [[XOR]], 2
220 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[LSHR2]] to i16
221 ; CHECK-NEXT: ret i16 [[TRUNC]]
223 %zext = zext i16 %x to i32
224 %xor = xor i32 -1, %zext
225 %lshr2 = ashr i32 %xor, 2
226 %trunc = trunc i32 %lshr2 to i16
230 define i16 @ashr_negative_operand_but_short(i16 %x) {
231 ; CHECK-LABEL: @ashr_negative_operand_but_short(
232 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i16 [[X:%.*]] to i32
233 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[ZEXT]], 32767
234 ; CHECK-NEXT: [[XOR:%.*]] = xor i32 -1, [[AND]]
235 ; CHECK-NEXT: [[LSHR2:%.*]] = ashr i32 [[XOR]], 2
236 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 [[LSHR2]] to i16
237 ; CHECK-NEXT: ret i16 [[TRUNC]]
239 %zext = zext i16 %x to i32
240 %and = and i32 %zext, 32767
241 %xor = xor i32 -1, %and
242 %lshr2 = ashr i32 %xor, 2
243 %trunc = trunc i32 %lshr2 to i16