1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; This test makes sure that these instructions are properly eliminated.
3;
4; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5
6define i32 @shl_C1_add_A_C2_i32(i16 %A) {
7; CHECK-LABEL: @shl_C1_add_A_C2_i32(
8; CHECK-NEXT:    [[B:%.*]] = zext i16 [[A:%.*]] to i32
9; CHECK-NEXT:    [[D:%.*]] = shl i32 192, [[B]]
10; CHECK-NEXT:    ret i32 [[D]]
11;
12  %B = zext i16 %A to i32
13  %C = add i32 %B, 5
14  %D = shl i32 6, %C
15  ret i32 %D
16}
17
18define i32 @ashr_C1_add_A_C2_i32(i32 %A) {
19; CHECK-LABEL: @ashr_C1_add_A_C2_i32(
20; CHECK-NEXT:    ret i32 0
21;
22  %B = and i32 %A, 65535
23  %C = add i32 %B, 5
24  %D = ashr i32 6, %C
25  ret i32 %D
26}
27
28define i32 @lshr_C1_add_A_C2_i32(i32 %A) {
29; CHECK-LABEL: @lshr_C1_add_A_C2_i32(
30; CHECK-NEXT:    [[B:%.*]] = and i32 [[A:%.*]], 65535
31; CHECK-NEXT:    [[D:%.*]] = shl i32 192, [[B]]
32; CHECK-NEXT:    ret i32 [[D]]
33;
34  %B = and i32 %A, 65535
35  %C = add i32 %B, 5
36  %D = shl i32 6, %C
37  ret i32 %D
38}
39
40define <4 x i32> @shl_C1_add_A_C2_v4i32(<4 x i16> %A) {
41; CHECK-LABEL: @shl_C1_add_A_C2_v4i32(
42; CHECK-NEXT:    [[B:%.*]] = zext <4 x i16> [[A:%.*]] to <4 x i32>
43; CHECK-NEXT:    [[D:%.*]] = shl <4 x i32> <i32 6, i32 4, i32 poison, i32 -458752>, [[B]]
44; CHECK-NEXT:    ret <4 x i32> [[D]]
45;
46  %B = zext <4 x i16> %A to <4 x i32>
47  %C = add <4 x i32> %B, <i32 0, i32 1, i32 50, i32 16>
48  %D = shl <4 x i32> <i32 6, i32 2, i32 1, i32 -7>, %C
49  ret <4 x i32> %D
50}
51
52define <4 x i32> @ashr_C1_add_A_C2_v4i32(<4 x i32> %A) {
53; CHECK-LABEL: @ashr_C1_add_A_C2_v4i32(
54; CHECK-NEXT:    [[B:%.*]] = and <4 x i32> [[A:%.*]], <i32 0, i32 15, i32 255, i32 65535>
55; CHECK-NEXT:    [[D:%.*]] = ashr <4 x i32> <i32 6, i32 1, i32 poison, i32 -1>, [[B]]
56; CHECK-NEXT:    ret <4 x i32> [[D]]
57;
58  %B = and <4 x i32> %A, <i32 0, i32 15, i32 255, i32 65535>
59  %C = add <4 x i32> %B, <i32 0, i32 1, i32 50, i32 16>
60  %D = ashr <4 x i32> <i32 6, i32 2, i32 1, i32 -7>, %C
61  ret <4 x i32> %D
62}
63
64define <4 x i32> @lshr_C1_add_A_C2_v4i32(<4 x i32> %A) {
65; CHECK-LABEL: @lshr_C1_add_A_C2_v4i32(
66; CHECK-NEXT:    [[B:%.*]] = and <4 x i32> [[A:%.*]], <i32 0, i32 15, i32 255, i32 65535>
67; CHECK-NEXT:    [[D:%.*]] = lshr <4 x i32> <i32 6, i32 1, i32 poison, i32 65535>, [[B]]
68; CHECK-NEXT:    ret <4 x i32> [[D]]
69;
70  %B = and <4 x i32> %A, <i32 0, i32 15, i32 255, i32 65535>
71  %C = add <4 x i32> %B, <i32 0, i32 1, i32 50, i32 16>
72  %D = lshr <4 x i32> <i32 6, i32 2, i32 1, i32 -7>, %C
73  ret <4 x i32> %D
74}
75
76define <4 x i32> @shl_C1_add_A_C2_v4i32_splat(i16 %I) {
77; CHECK-LABEL: @shl_C1_add_A_C2_v4i32_splat(
78; CHECK-NEXT:    [[A:%.*]] = zext i16 [[I:%.*]] to i32
79; CHECK-NEXT:    [[B:%.*]] = insertelement <4 x i32> undef, i32 [[A]], i64 0
80; CHECK-NEXT:    [[C:%.*]] = shufflevector <4 x i32> [[B]], <4 x i32> undef, <4 x i32> zeroinitializer
81; CHECK-NEXT:    [[E:%.*]] = shl <4 x i32> <i32 6, i32 4, i32 poison, i32 -458752>, [[C]]
82; CHECK-NEXT:    ret <4 x i32> [[E]]
83;
84  %A = zext i16 %I to i32
85  %B = insertelement <4 x i32> undef, i32 %A, i32 0
86  %C = shufflevector <4 x i32> %B, <4 x i32> undef, <4 x i32> zeroinitializer
87  %D = add <4 x i32> %C, <i32 0, i32 1, i32 50, i32 16>
88  %E = shl <4 x i32> <i32 6, i32 2, i32 1, i32 -7>, %D
89  ret <4 x i32> %E
90}
91
92define <4 x i32> @ashr_C1_add_A_C2_v4i32_splat(i16 %I) {
93; CHECK-LABEL: @ashr_C1_add_A_C2_v4i32_splat(
94; CHECK-NEXT:    [[A:%.*]] = zext i16 [[I:%.*]] to i32
95; CHECK-NEXT:    [[B:%.*]] = insertelement <4 x i32> undef, i32 [[A]], i64 0
96; CHECK-NEXT:    [[C:%.*]] = shufflevector <4 x i32> [[B]], <4 x i32> undef, <4 x i32> zeroinitializer
97; CHECK-NEXT:    [[E:%.*]] = ashr <4 x i32> <i32 6, i32 1, i32 poison, i32 -1>, [[C]]
98; CHECK-NEXT:    ret <4 x i32> [[E]]
99;
100  %A = zext i16 %I to i32
101  %B = insertelement <4 x i32> undef, i32 %A, i32 0
102  %C = shufflevector <4 x i32> %B, <4 x i32> undef, <4 x i32> zeroinitializer
103  %D = add <4 x i32> %C, <i32 0, i32 1, i32 50, i32 16>
104  %E = ashr <4 x i32> <i32 6, i32 2, i32 1, i32 -7>, %D
105  ret <4 x i32> %E
106}
107
108define <4 x i32> @lshr_C1_add_A_C2_v4i32_splat(i16 %I) {
109; CHECK-LABEL: @lshr_C1_add_A_C2_v4i32_splat(
110; CHECK-NEXT:    [[A:%.*]] = zext i16 [[I:%.*]] to i32
111; CHECK-NEXT:    [[B:%.*]] = insertelement <4 x i32> undef, i32 [[A]], i64 0
112; CHECK-NEXT:    [[C:%.*]] = shufflevector <4 x i32> [[B]], <4 x i32> undef, <4 x i32> zeroinitializer
113; CHECK-NEXT:    [[E:%.*]] = lshr <4 x i32> <i32 6, i32 1, i32 poison, i32 65535>, [[C]]
114; CHECK-NEXT:    ret <4 x i32> [[E]]
115;
116  %A = zext i16 %I to i32
117  %B = insertelement <4 x i32> undef, i32 %A, i32 0
118  %C = shufflevector <4 x i32> %B, <4 x i32> undef, <4 x i32> zeroinitializer
119  %D = add <4 x i32> %C, <i32 0, i32 1, i32 50, i32 16>
120  %E = lshr <4 x i32> <i32 6, i32 2, i32 1, i32 -7>, %D
121  ret <4 x i32> %E
122}
123
124define i32 @shl_add_nuw(i32 %x) {
125; CHECK-LABEL: @shl_add_nuw(
126; CHECK-NEXT:    [[R:%.*]] = shl i32 192, [[X:%.*]]
127; CHECK-NEXT:    ret i32 [[R]]
128;
129  %a = add nuw i32 %x, 5
130  %r = shl i32 6, %a
131  ret i32 %r
132}
133
134; vectors with arbitrary constants work too
135
136define <2 x i12> @lshr_add_nuw(<2 x i12> %x) {
137; CHECK-LABEL: @lshr_add_nuw(
138; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i12> <i12 0, i12 21>, [[X:%.*]]
139; CHECK-NEXT:    ret <2 x i12> [[R]]
140;
141  %a = add nuw <2 x i12> %x, <i12 5, i12 1>
142  %r = lshr <2 x i12> <i12 6, i12 42>, %a
143  ret <2 x i12> %r
144}
145
146; extra use is ok and in this case the result can be simplified to a constant
147
148define i32 @ashr_add_nuw(i32 %x, i32* %p) {
149; CHECK-LABEL: @ashr_add_nuw(
150; CHECK-NEXT:    [[A:%.*]] = add nuw i32 [[X:%.*]], 5
151; CHECK-NEXT:    store i32 [[A]], i32* [[P:%.*]], align 4
152; CHECK-NEXT:    ret i32 -1
153;
154  %a = add nuw i32 %x, 5
155  store i32 %a, i32* %p
156  %r = ashr i32 -6, %a
157  ret i32 %r
158}
159
160; negative test - must have 'nuw'
161
162define i32 @shl_add_nsw(i32 %x) {
163; CHECK-LABEL: @shl_add_nsw(
164; CHECK-NEXT:    [[A:%.*]] = add nsw i32 [[X:%.*]], 5
165; CHECK-NEXT:    [[R:%.*]] = shl i32 6, [[A]]
166; CHECK-NEXT:    ret i32 [[R]]
167;
168  %a = add nsw i32 %x, 5
169  %r = shl i32 6, %a
170  ret i32 %r
171}
172
173; PR54890
174
175define i32 @shl_nsw_add_negative(i32 %x) {
176; CHECK-LABEL: @shl_nsw_add_negative(
177; CHECK-NEXT:    [[R:%.*]] = shl i32 1, [[X:%.*]]
178; CHECK-NEXT:    ret i32 [[R]]
179;
180  %a = add i32 %x, -1
181  %r = shl nsw i32 2, %a
182  ret i32 %r
183}
184
185; vectors and extra uses are allowed
186; nuw propagates to the new shift
187
188define <2 x i8> @shl_nuw_add_negative_splat_uses(<2 x i8> %x, <2 x i8>* %p) {
189; CHECK-LABEL: @shl_nuw_add_negative_splat_uses(
190; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -2, i8 -2>
191; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
192; CHECK-NEXT:    [[R:%.*]] = shl nuw <2 x i8> <i8 3, i8 3>, [[X]]
193; CHECK-NEXT:    ret <2 x i8> [[R]]
194;
195  %a = add <2 x i8> %x, <i8 -2, i8 -2>
196  store <2 x i8> %a, <2 x i8>* %p
197  %r = shl nuw <2 x i8> <i8 12, i8 12>, %a
198  ret <2 x i8> %r
199}
200
201; negative test - shift constant must have enough trailing zeros to allow the pre-shift
202
203define i32 @shl_nsw_add_negative_invalid_constant(i32 %x) {
204; CHECK-LABEL: @shl_nsw_add_negative_invalid_constant(
205; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], -2
206; CHECK-NEXT:    [[R:%.*]] = shl nsw i32 2, [[A]]
207; CHECK-NEXT:    ret i32 [[R]]
208;
209  %a = add i32 %x, -2
210  %r = shl nsw i32 2, %a
211  ret i32 %r
212}
213
214; negative test - the offset constant must be negative
215
216define i32 @shl_nsw_add_positive_invalid_constant(i32 %x) {
217; CHECK-LABEL: @shl_nsw_add_positive_invalid_constant(
218; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], 2
219; CHECK-NEXT:    [[R:%.*]] = shl nsw i32 4, [[A]]
220; CHECK-NEXT:    ret i32 [[R]]
221;
222  %a = add i32 %x, 2
223  %r = shl nsw i32 4, %a
224  ret i32 %r
225}
226
227; negative test - a large shift must be detected without crashing
228
229define i32 @shl_nsw_add_negative_invalid_constant2(i32 %x) {
230; CHECK-LABEL: @shl_nsw_add_negative_invalid_constant2(
231; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], -33
232; CHECK-NEXT:    [[R:%.*]] = shl nsw i32 2, [[A]]
233; CHECK-NEXT:    ret i32 [[R]]
234;
235  %a = add i32 %x, -33
236  %r = shl nsw i32 2, %a
237  ret i32 %r
238}
239
240; negative test - currently transformed to 'xor' before we see it,
241; but INT_MIN should be handled too
242
243define i4 @shl_nsw_add_negative_invalid_constant3(i4 %x) {
244; CHECK-LABEL: @shl_nsw_add_negative_invalid_constant3(
245; CHECK-NEXT:    [[A:%.*]] = xor i4 [[X:%.*]], -8
246; CHECK-NEXT:    [[R:%.*]] = shl nsw i4 2, [[A]]
247; CHECK-NEXT:    ret i4 [[R]]
248;
249  %a = add i4 %x, 8
250  %r = shl nsw i4 2, %a
251  ret i4 %r
252}
253