1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine %s -S -o - | FileCheck %s
4 ; Clamp positive to allOnes:
5 ; E.g., clamp255 implemented in a shifty way, could be optimized as v > 255 ? 255 : v, where sub hasNoSignedWrap.
6 ; int32 clamp255(int32 v) {
7 ; return (((255 - (v)) >> 31) | (v)) & 255;
13 define i32 @clamp255_i32(i32 %x) {
14 ; CHECK-LABEL: @clamp255_i32(
15 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 255
16 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 255
17 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[OR]], 255
18 ; CHECK-NEXT: ret i32 [[AND]]
20 %sub = sub nsw i32 255, %x
21 %shr = ashr i32 %sub, 31
23 %and = and i32 %or, 255
27 define i8 @sub_ashr_or_i8(i8 %x, i8 %y) {
28 ; CHECK-LABEL: @sub_ashr_or_i8(
29 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i8 [[Y:%.*]], [[X:%.*]]
30 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i8 -1, i8 [[X]]
31 ; CHECK-NEXT: ret i8 [[OR]]
33 %sub = sub nsw i8 %y, %x
34 %shr = ashr i8 %sub, 7
39 define i16 @sub_ashr_or_i16(i16 %x, i16 %y) {
40 ; CHECK-LABEL: @sub_ashr_or_i16(
41 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i16 [[Y:%.*]], [[X:%.*]]
42 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i16 -1, i16 [[X]]
43 ; CHECK-NEXT: ret i16 [[OR]]
45 %sub = sub nsw i16 %y, %x
46 %shr = ashr i16 %sub, 15
51 define i32 @sub_ashr_or_i32(i32 %x, i32 %y) {
52 ; CHECK-LABEL: @sub_ashr_or_i32(
53 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y:%.*]], [[X:%.*]]
54 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
55 ; CHECK-NEXT: ret i32 [[OR]]
57 %sub = sub nsw i32 %y, %x
58 %shr = ashr i32 %sub, 31
63 define i64 @sub_ashr_or_i64(i64 %x, i64 %y) {
64 ; CHECK-LABEL: @sub_ashr_or_i64(
65 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i64 [[Y:%.*]], [[X:%.*]]
66 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i64 -1, i64 [[X]]
67 ; CHECK-NEXT: ret i64 [[OR]]
69 %sub = sub nsw i64 %y, %x
70 %shr = ashr i64 %sub, 63
75 define i32 @neg_or_ashr_i32(i32 %x) {
76 ; CHECK-LABEL: @neg_or_ashr_i32(
77 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 0
78 ; CHECK-NEXT: [[SHR:%.*]] = sext i1 [[TMP1]] to i32
79 ; CHECK-NEXT: ret i32 [[SHR]]
83 %shr = ashr i32 %or, 31
89 define i32 @sub_ashr_or_i32_nuw_nsw(i32 %x, i32 %y) {
90 ; CHECK-LABEL: @sub_ashr_or_i32_nuw_nsw(
91 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y:%.*]], [[X:%.*]]
92 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
93 ; CHECK-NEXT: ret i32 [[OR]]
95 %sub = sub nuw nsw i32 %y, %x
96 %shr = ashr i32 %sub, 31
103 define i32 @sub_ashr_or_i32_commute(i32 %x, i32 %y) {
104 ; CHECK-LABEL: @sub_ashr_or_i32_commute(
105 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y:%.*]], [[X:%.*]]
106 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
107 ; CHECK-NEXT: ret i32 [[OR]]
109 %sub = sub nsw i32 %y, %x
110 %shr = ashr i32 %sub, 31
111 %or = or i32 %x, %shr ; commute %shr and %x
115 define i32 @neg_or_ashr_i32_commute(i32 %x0) {
116 ; CHECK-LABEL: @neg_or_ashr_i32_commute(
117 ; CHECK-NEXT: [[X:%.*]] = sdiv i32 42, [[X0:%.*]]
118 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X]], 0
119 ; CHECK-NEXT: [[SHR:%.*]] = sext i1 [[TMP1]] to i32
120 ; CHECK-NEXT: ret i32 [[SHR]]
122 %x = sdiv i32 42, %x0 ; thwart complexity-based canonicalization
124 %or = or i32 %x, %neg
125 %shr = ashr i32 %or, 31
131 define <4 x i32> @sub_ashr_or_i32_vec(<4 x i32> %x, <4 x i32> %y) {
132 ; CHECK-LABEL: @sub_ashr_or_i32_vec(
133 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <4 x i32> [[Y:%.*]], [[X:%.*]]
134 ; CHECK-NEXT: [[OR:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, <4 x i32> [[X]]
135 ; CHECK-NEXT: ret <4 x i32> [[OR]]
137 %sub = sub nsw <4 x i32> %y, %x
138 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
139 %or = or <4 x i32> %shr, %x
143 define <4 x i32> @sub_ashr_or_i32_vec_nuw_nsw(<4 x i32> %x, <4 x i32> %y) {
144 ; CHECK-LABEL: @sub_ashr_or_i32_vec_nuw_nsw(
145 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <4 x i32> [[Y:%.*]], [[X:%.*]]
146 ; CHECK-NEXT: [[OR:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, <4 x i32> [[X]]
147 ; CHECK-NEXT: ret <4 x i32> [[OR]]
149 %sub = sub nuw nsw <4 x i32> %y, %x
150 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
151 %or = or <4 x i32> %shr, %x
155 define <4 x i32> @neg_or_ashr_i32_vec(<4 x i32> %x) {
156 ; CHECK-LABEL: @neg_or_ashr_i32_vec(
157 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <4 x i32> [[X:%.*]], zeroinitializer
158 ; CHECK-NEXT: [[SHR:%.*]] = sext <4 x i1> [[TMP1]] to <4 x i32>
159 ; CHECK-NEXT: ret <4 x i32> [[SHR]]
161 %neg = sub <4 x i32> zeroinitializer, %x
162 %or = or <4 x i32> %neg, %x
163 %shr = ashr <4 x i32> %or, <i32 31, i32 31, i32 31, i32 31>
167 define <4 x i32> @sub_ashr_or_i32_vec_commute(<4 x i32> %x, <4 x i32> %y) {
168 ; CHECK-LABEL: @sub_ashr_or_i32_vec_commute(
169 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <4 x i32> [[Y:%.*]], [[X:%.*]]
170 ; CHECK-NEXT: [[OR:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, <4 x i32> [[X]]
171 ; CHECK-NEXT: ret <4 x i32> [[OR]]
173 %sub = sub nsw <4 x i32> %y, %x
174 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
175 %or = or <4 x i32> %x, %shr
179 define <4 x i32> @neg_or_ashr_i32_vec_commute(<4 x i32> %x0) {
180 ; CHECK-LABEL: @neg_or_ashr_i32_vec_commute(
181 ; CHECK-NEXT: [[X:%.*]] = sdiv <4 x i32> <i32 42, i32 42, i32 42, i32 42>, [[X0:%.*]]
182 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <4 x i32> [[X]], zeroinitializer
183 ; CHECK-NEXT: [[SHR:%.*]] = sext <4 x i1> [[TMP1]] to <4 x i32>
184 ; CHECK-NEXT: ret <4 x i32> [[SHR]]
186 %x = sdiv <4 x i32> <i32 42, i32 42, i32 42, i32 42>, %x0 ; thwart complexity-based canonicalization
187 %neg = sub <4 x i32> zeroinitializer, %x
188 %or = or <4 x i32> %x, %neg
189 %shr = ashr <4 x i32> %or, <i32 31, i32 31, i32 31, i32 31>
195 define i32 @sub_ashr_or_i32_extra_use_sub(i32 %x, i32 %y, i32* %p) {
196 ; CHECK-LABEL: @sub_ashr_or_i32_extra_use_sub(
197 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
198 ; CHECK-NEXT: store i32 [[SUB]], i32* [[P:%.*]], align 4
199 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y]], [[X]]
200 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
201 ; CHECK-NEXT: ret i32 [[OR]]
203 %sub = sub nsw i32 %y, %x
204 store i32 %sub, i32* %p
205 %shr = ashr i32 %sub, 31
206 %or = or i32 %shr, %x
210 define i32 @sub_ashr_or_i32_extra_use_or(i32 %x, i32 %y, i32* %p) {
211 ; CHECK-LABEL: @sub_ashr_or_i32_extra_use_or(
212 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y:%.*]], [[X:%.*]]
213 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
214 ; CHECK-NEXT: store i32 [[OR]], i32* [[P:%.*]], align 4
215 ; CHECK-NEXT: ret i32 [[OR]]
217 %sub = sub nsw i32 %y, %x
218 %shr = ashr i32 %sub, 31
219 %or = or i32 %shr, %x
220 store i32 %or, i32* %p
224 define i32 @neg_extra_use_or_ashr_i32(i32 %x, i32* %p) {
225 ; CHECK-LABEL: @neg_extra_use_or_ashr_i32(
226 ; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
227 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X]], 0
228 ; CHECK-NEXT: [[SHR:%.*]] = sext i1 [[TMP1]] to i32
229 ; CHECK-NEXT: store i32 [[NEG]], i32* [[P:%.*]], align 4
230 ; CHECK-NEXT: ret i32 [[SHR]]
233 %or = or i32 %neg, %x
234 %shr = ashr i32 %or, 31
235 store i32 %neg, i32* %p
241 define i32 @sub_ashr_or_i32_extra_use_ashr(i32 %x, i32 %y, i32* %p) {
242 ; CHECK-LABEL: @sub_ashr_or_i32_extra_use_ashr(
243 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y:%.*]], [[X:%.*]]
244 ; CHECK-NEXT: [[SHR:%.*]] = sext i1 [[TMP1]] to i32
245 ; CHECK-NEXT: store i32 [[SHR]], i32* [[P:%.*]], align 4
246 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHR]], [[X]]
247 ; CHECK-NEXT: ret i32 [[OR]]
249 %sub = sub nsw i32 %y, %x
250 %shr = ashr i32 %sub, 31
251 store i32 %shr, i32* %p
252 %or = or i32 %shr, %x
256 define i32 @sub_ashr_or_i32_no_nsw_nuw(i32 %x, i32 %y) {
257 ; CHECK-LABEL: @sub_ashr_or_i32_no_nsw_nuw(
258 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[Y:%.*]], [[X:%.*]]
259 ; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31
260 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHR]], [[X]]
261 ; CHECK-NEXT: ret i32 [[OR]]
263 %sub = sub i32 %y, %x
264 %shr = ashr i32 %sub, 31
265 %or = or i32 %shr, %x
269 define i32 @neg_or_extra_use_ashr_i32(i32 %x, i32* %p) {
270 ; CHECK-LABEL: @neg_or_extra_use_ashr_i32(
271 ; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
272 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[NEG]], [[X]]
273 ; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[OR]], 31
274 ; CHECK-NEXT: store i32 [[OR]], i32* [[P:%.*]], align 4
275 ; CHECK-NEXT: ret i32 [[SHR]]
278 %or = or i32 %neg, %x
279 %shr = ashr i32 %or, 31
280 store i32 %or, i32* %p
284 define <4 x i32> @sub_ashr_or_i32_vec_undef1(<4 x i32> %x) {
285 ; CHECK-LABEL: @sub_ashr_or_i32_vec_undef1(
286 ; CHECK-NEXT: [[SUB:%.*]] = sub <4 x i32> <i32 255, i32 255, i32 undef, i32 255>, [[X:%.*]]
287 ; CHECK-NEXT: [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 31, i32 31, i32 31, i32 31>
288 ; CHECK-NEXT: [[OR:%.*]] = or <4 x i32> [[SHR]], [[X]]
289 ; CHECK-NEXT: ret <4 x i32> [[OR]]
291 %sub = sub <4 x i32> <i32 255, i32 255, i32 undef, i32 255>, %x
292 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
293 %or = or <4 x i32> %shr, %x
297 define <4 x i32> @sub_ashr_or_i32_vec_undef2(<4 x i32> %x) {
298 ; CHECK-LABEL: @sub_ashr_or_i32_vec_undef2(
299 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw <4 x i32> <i32 255, i32 255, i32 255, i32 255>, [[X:%.*]]
300 ; CHECK-NEXT: [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 undef, i32 31, i32 31, i32 31>
301 ; CHECK-NEXT: [[OR:%.*]] = or <4 x i32> [[SHR]], [[X]]
302 ; CHECK-NEXT: ret <4 x i32> [[OR]]
304 %sub = sub nsw <4 x i32> <i32 255, i32 255, i32 255, i32 255>, %x
305 %shr = ashr <4 x i32> %sub, <i32 undef, i32 31, i32 31, i32 31>
306 %or = or <4 x i32> %shr, %x
310 define i32 @sub_ashr_or_i32_shift_wrong_bit(i32 %x, i32 %y) {
311 ; CHECK-LABEL: @sub_ashr_or_i32_shift_wrong_bit(
312 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
313 ; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 11
314 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHR]], [[X]]
315 ; CHECK-NEXT: ret i32 [[OR]]
317 %sub = sub nsw i32 %y, %x
318 %shr = ashr i32 %sub, 11
319 %or = or i32 %shr, %x