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 sgt i8 [[X:%.*]], [[Y:%.*]]
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 sgt i16 [[X:%.*]], [[Y:%.*]]
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 sgt i32 [[X:%.*]], [[Y:%.*]]
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 sgt i64 [[X:%.*]], [[Y:%.*]]
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
77 define i32 @sub_ashr_or_i32_nuw_nsw(i32 %x, i32 %y) {
78 ; CHECK-LABEL: @sub_ashr_or_i32_nuw_nsw(
79 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
80 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
81 ; CHECK-NEXT: ret i32 [[OR]]
83 %sub = sub nuw nsw i32 %y, %x
84 %shr = ashr i32 %sub, 31
91 define i32 @sub_ashr_or_i32_commute(i32 %x, i32 %y) {
92 ; CHECK-LABEL: @sub_ashr_or_i32_commute(
93 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
94 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
95 ; CHECK-NEXT: ret i32 [[OR]]
97 %sub = sub nsw i32 %y, %x
98 %shr = ashr i32 %sub, 31
99 %or = or i32 %x, %shr ; commute %shr and %x
105 define <4 x i32> @sub_ashr_or_i32_vec(<4 x i32> %x, <4 x i32> %y) {
106 ; CHECK-LABEL: @sub_ashr_or_i32_vec(
107 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[X:%.*]], [[Y:%.*]]
108 ; CHECK-NEXT: [[OR:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, <4 x i32> [[X]]
109 ; CHECK-NEXT: ret <4 x i32> [[OR]]
111 %sub = sub nsw <4 x i32> %y, %x
112 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
113 %or = or <4 x i32> %shr, %x
117 define <4 x i32> @sub_ashr_or_i32_vec_nuw_nsw(<4 x i32> %x, <4 x i32> %y) {
118 ; CHECK-LABEL: @sub_ashr_or_i32_vec_nuw_nsw(
119 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[X:%.*]], [[Y:%.*]]
120 ; CHECK-NEXT: [[OR:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, <4 x i32> [[X]]
121 ; CHECK-NEXT: ret <4 x i32> [[OR]]
123 %sub = sub nuw nsw <4 x i32> %y, %x
124 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
125 %or = or <4 x i32> %shr, %x
129 define <4 x i32> @sub_ashr_or_i32_vec_commute(<4 x i32> %x, <4 x i32> %y) {
130 ; CHECK-LABEL: @sub_ashr_or_i32_vec_commute(
131 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[X:%.*]], [[Y:%.*]]
132 ; CHECK-NEXT: [[OR:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>, <4 x i32> [[X]]
133 ; CHECK-NEXT: ret <4 x i32> [[OR]]
135 %sub = sub nsw <4 x i32> %y, %x
136 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
137 %or = or <4 x i32> %x, %shr
143 define i32 @sub_ashr_or_i32_extra_use_sub(i32 %x, i32 %y, i32* %p) {
144 ; CHECK-LABEL: @sub_ashr_or_i32_extra_use_sub(
145 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
146 ; CHECK-NEXT: store i32 [[SUB]], i32* [[P:%.*]], align 4
147 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[Y]], [[X]]
148 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
149 ; CHECK-NEXT: ret i32 [[OR]]
151 %sub = sub nsw i32 %y, %x
152 store i32 %sub, i32* %p
153 %shr = ashr i32 %sub, 31
154 %or = or i32 %shr, %x
158 define i32 @sub_ashr_or_i32_extra_use_or(i32 %x, i32 %y, i32* %p) {
159 ; CHECK-LABEL: @sub_ashr_or_i32_extra_use_or(
160 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
161 ; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i32 -1, i32 [[X]]
162 ; CHECK-NEXT: store i32 [[OR]], i32* [[P:%.*]], align 4
163 ; CHECK-NEXT: ret i32 [[OR]]
165 %sub = sub nsw i32 %y, %x
166 %shr = ashr i32 %sub, 31
167 %or = or i32 %shr, %x
168 store i32 %or, i32* %p
174 define i32 @sub_ashr_or_i32_extra_use_ashr(i32 %x, i32 %y, i32* %p) {
175 ; CHECK-LABEL: @sub_ashr_or_i32_extra_use_ashr(
176 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
177 ; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31
178 ; CHECK-NEXT: store i32 [[SHR]], i32* [[P:%.*]], align 4
179 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHR]], [[X]]
180 ; CHECK-NEXT: ret i32 [[OR]]
182 %sub = sub nsw i32 %y, %x
183 %shr = ashr i32 %sub, 31
184 store i32 %shr, i32* %p
185 %or = or i32 %shr, %x
189 define i32 @sub_ashr_or_i32_no_nsw_nuw(i32 %x, i32 %y) {
190 ; CHECK-LABEL: @sub_ashr_or_i32_no_nsw_nuw(
191 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[Y:%.*]], [[X:%.*]]
192 ; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 31
193 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHR]], [[X]]
194 ; CHECK-NEXT: ret i32 [[OR]]
196 %sub = sub i32 %y, %x
197 %shr = ashr i32 %sub, 31
198 %or = or i32 %shr, %x
202 define <4 x i32> @sub_ashr_or_i32_vec_undef1(<4 x i32> %x) {
203 ; CHECK-LABEL: @sub_ashr_or_i32_vec_undef1(
204 ; CHECK-NEXT: [[SUB:%.*]] = sub <4 x i32> <i32 255, i32 255, i32 undef, i32 255>, [[X:%.*]]
205 ; CHECK-NEXT: [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 31, i32 31, i32 31, i32 31>
206 ; CHECK-NEXT: [[OR:%.*]] = or <4 x i32> [[SHR]], [[X]]
207 ; CHECK-NEXT: ret <4 x i32> [[OR]]
209 %sub = sub <4 x i32> <i32 255, i32 255, i32 undef, i32 255>, %x
210 %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
211 %or = or <4 x i32> %shr, %x
215 define <4 x i32> @sub_ashr_or_i32_vec_undef2(<4 x i32> %x) {
216 ; CHECK-LABEL: @sub_ashr_or_i32_vec_undef2(
217 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw <4 x i32> <i32 255, i32 255, i32 255, i32 255>, [[X:%.*]]
218 ; CHECK-NEXT: [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 undef, i32 31, i32 31, i32 31>
219 ; CHECK-NEXT: [[OR:%.*]] = or <4 x i32> [[SHR]], [[X]]
220 ; CHECK-NEXT: ret <4 x i32> [[OR]]
222 %sub = sub nsw <4 x i32> <i32 255, i32 255, i32 255, i32 255>, %x
223 %shr = ashr <4 x i32> %sub, <i32 undef, i32 31, i32 31, i32 31>
224 %or = or <4 x i32> %shr, %x
228 define i32 @sub_ashr_or_i32_shift_wrong_bit(i32 %x, i32 %y) {
229 ; CHECK-LABEL: @sub_ashr_or_i32_shift_wrong_bit(
230 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
231 ; CHECK-NEXT: [[SHR:%.*]] = ashr i32 [[SUB]], 11
232 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[SHR]], [[X]]
233 ; CHECK-NEXT: ret i32 [[OR]]
235 %sub = sub nsw i32 %y, %x
236 %shr = ashr i32 %sub, 11
237 %or = or i32 %shr, %x