[InstCombine] Signed saturation patterns
[llvm-core.git] / test / Transforms / InstCombine / sub-ashr-and-to-icmp-select.ll
blob66609f1418439fc93c4c86b7b0d7db1cd6226253
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine %s -S -o - | FileCheck %s
4 ; Clamp negative to zero:
5 ; E.g., clamp0 implemented in a shifty way, could be optimized as v > 0 ? v : 0, where sub hasNoSignedWrap.
6 ; int32 clamp0(int32 v) {
7 ;   return ((-(v) >> 31) & (v));
8 ; }
11 ; Scalar Types
13 define i8 @sub_ashr_and_i8(i8 %x, i8 %y) {
14 ; CHECK-LABEL: @sub_ashr_and_i8(
15 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i8 [[X:%.*]], [[Y:%.*]]
16 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i8 [[X]], i8 0
17 ; CHECK-NEXT:    ret i8 [[AND]]
19   %sub = sub nsw i8 %y, %x
20   %shr = ashr i8 %sub, 7
21   %and = and i8 %shr, %x
22   ret i8 %and
25 define i16 @sub_ashr_and_i16(i16 %x, i16 %y) {
26 ; CHECK-LABEL: @sub_ashr_and_i16(
27 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i16 [[X:%.*]], [[Y:%.*]]
28 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i16 [[X]], i16 0
29 ; CHECK-NEXT:    ret i16 [[AND]]
32   %sub = sub nsw i16 %y, %x
33   %shr = ashr i16 %sub, 15
34   %and = and i16 %shr, %x
35   ret i16 %and
38 define i32 @sub_ashr_and_i32(i32 %x, i32 %y) {
39 ; CHECK-LABEL: @sub_ashr_and_i32(
40 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
41 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0
42 ; CHECK-NEXT:    ret i32 [[AND]]
44   %sub = sub nsw i32 %y, %x
45   %shr = ashr i32 %sub, 31
46   %and = and i32 %shr, %x
47   ret i32 %and
50 define i64 @sub_ashr_and_i64(i64 %x, i64 %y) {
51 ; CHECK-LABEL: @sub_ashr_and_i64(
52 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i64 [[X:%.*]], [[Y:%.*]]
53 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i64 [[X]], i64 0
54 ; CHECK-NEXT:    ret i64 [[AND]]
56   %sub = sub nsw i64 %y, %x
57   %shr = ashr i64 %sub, 63
58   %and = and i64 %shr, %x
59   ret i64 %and
62 ; nuw nsw
64 define i32 @sub_ashr_and_i32_nuw_nsw(i32 %x, i32 %y) {
65 ; CHECK-LABEL: @sub_ashr_and_i32_nuw_nsw(
66 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
67 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0
68 ; CHECK-NEXT:    ret i32 [[AND]]
70   %sub = sub nuw nsw i32 %y, %x
71   %shr = ashr i32 %sub, 31
72   %and = and i32 %shr, %x
73   ret i32 %and
76 ; Commute
78 define i32 @sub_ashr_and_i32_commute(i32 %x, i32 %y) {
79 ; CHECK-LABEL: @sub_ashr_and_i32_commute(
80 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
81 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0
82 ; CHECK-NEXT:    ret i32 [[AND]]
84   %sub = sub nsw i32 %y, %x
85   %shr = ashr i32 %sub, 31
86   %and = and i32 %x, %shr  ; commute %x and %shr
87   ret i32 %and
90 ; Vector Types
92 define <4 x i32> @sub_ashr_and_i32_vec(<4 x i32> %x, <4 x i32> %y) {
93 ; CHECK-LABEL: @sub_ashr_and_i32_vec(
94 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <4 x i32> [[X:%.*]], [[Y:%.*]]
95 ; CHECK-NEXT:    [[AND:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[X]], <4 x i32> zeroinitializer
96 ; CHECK-NEXT:    ret <4 x i32> [[AND]]
98   %sub = sub nsw <4 x i32> %y, %x
99   %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
100   %and = and <4 x i32> %shr, %x
101   ret <4 x i32> %and
104 define <4 x i32> @sub_ashr_and_i32_vec_nuw_nsw(<4 x i32> %x, <4 x i32> %y) {
105 ; CHECK-LABEL: @sub_ashr_and_i32_vec_nuw_nsw(
106 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <4 x i32> [[X:%.*]], [[Y:%.*]]
107 ; CHECK-NEXT:    [[AND:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[X]], <4 x i32> zeroinitializer
108 ; CHECK-NEXT:    ret <4 x i32> [[AND]]
110   %sub = sub nuw nsw <4 x i32> %y, %x
111   %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
112   %and = and <4 x i32> %shr, %x
113   ret <4 x i32> %and
116 define <4 x i32> @sub_ashr_and_i32_vec_commute(<4 x i32> %x, <4 x i32> %y) {
117 ; CHECK-LABEL: @sub_ashr_and_i32_vec_commute(
118 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <4 x i32> [[X:%.*]], [[Y:%.*]]
119 ; CHECK-NEXT:    [[AND:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[X]], <4 x i32> zeroinitializer
120 ; CHECK-NEXT:    ret <4 x i32> [[AND]]
122   %sub = sub nsw <4 x i32> %y, %x
123   %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 31>
124   %and = and <4 x i32> %x, %shr  ; commute %x and %shr
125   ret <4 x i32> %and
128 ; Extra uses
130 define i32 @sub_ashr_and_i32_extra_use_sub(i32 %x, i32 %y, i32* %p) {
131 ; CHECK-LABEL: @sub_ashr_and_i32_extra_use_sub(
132 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
133 ; CHECK-NEXT:    store i32 [[SUB]], i32* [[P:%.*]], align 4
134 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp slt i32 [[Y]], [[X]]
135 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0
136 ; CHECK-NEXT:    ret i32 [[AND]]
138   %sub = sub nsw i32 %y, %x
139   store i32 %sub, i32* %p
140   %shr = ashr i32 %sub, 31
141   %and = and i32 %shr, %x
142   ret i32 %and
145 define i32 @sub_ashr_and_i32_extra_use_and(i32 %x, i32 %y, i32* %p) {
146 ; CHECK-LABEL: @sub_ashr_and_i32_extra_use_and(
147 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
148 ; CHECK-NEXT:    [[AND:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 0
149 ; CHECK-NEXT:    store i32 [[AND]], i32* [[P:%.*]], align 4
150 ; CHECK-NEXT:    ret i32 [[AND]]
152   %sub = sub nsw i32 %y, %x
153   %shr = ashr i32 %sub, 31
154   %and = and i32 %shr, %x
155   store i32 %and, i32* %p
156   ret i32 %and
159 ; Negative Tests
161 define i32 @sub_ashr_and_i32_extra_use_ashr(i32 %x, i32 %y, i32* %p) {
162 ; CHECK-LABEL: @sub_ashr_and_i32_extra_use_ashr(
163 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
164 ; CHECK-NEXT:    [[SHR:%.*]] = ashr i32 [[SUB]], 31
165 ; CHECK-NEXT:    store i32 [[SHR]], i32* [[P:%.*]], align 4
166 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[SHR]], [[X]]
167 ; CHECK-NEXT:    ret i32 [[AND]]
169   %sub = sub nsw i32 %y, %x
170   %shr = ashr i32 %sub, 31
171   store i32 %shr, i32* %p
172   %and = and i32 %shr, %x
173   ret i32 %and
176 define i32 @sub_ashr_and_i32_no_nuw_nsw(i32 %x, i32 %y) {
177 ; CHECK-LABEL: @sub_ashr_and_i32_no_nuw_nsw(
178 ; CHECK-NEXT:    [[SUB:%.*]] = sub i32 [[Y:%.*]], [[X:%.*]]
179 ; CHECK-NEXT:    [[SHR:%.*]] = ashr i32 [[SUB]], 7
180 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[SHR]], [[X]]
181 ; CHECK-NEXT:    ret i32 [[AND]]
183   %sub = sub i32 %y, %x
184   %shr = ashr i32 %sub, 7
185   %and = and i32 %shr, %x
186   ret i32 %and
189 define <4 x i32> @sub_ashr_and_i32_vec_undef(<4 x i32> %x, <4 x i32> %y) {
190 ; CHECK-LABEL: @sub_ashr_and_i32_vec_undef(
191 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw <4 x i32> [[Y:%.*]], [[X:%.*]]
192 ; CHECK-NEXT:    [[SHR:%.*]] = ashr <4 x i32> [[SUB]], <i32 31, i32 31, i32 31, i32 undef>
193 ; CHECK-NEXT:    [[AND:%.*]] = and <4 x i32> [[SHR]], [[X]]
194 ; CHECK-NEXT:    ret <4 x i32> [[AND]]
196   %sub = sub nsw <4 x i32> %y, %x
197   %shr = ashr <4 x i32> %sub, <i32 31, i32 31, i32 31, i32 undef>
198   %and = and <4 x i32> %shr, %x
199   ret <4 x i32> %and
202 define i32 @sub_ashr_and_i32_shift_wrong_bit(i32 %x, i32 %y) {
203 ; CHECK-LABEL: @sub_ashr_and_i32_shift_wrong_bit(
204 ; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 [[Y:%.*]], [[X:%.*]]
205 ; CHECK-NEXT:    [[SHR:%.*]] = ashr i32 [[SUB]], 15
206 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[SHR]], [[X]]
207 ; CHECK-NEXT:    ret i32 [[AND]]
209   %sub = sub nsw i32 %y, %x
210   %shr = ashr i32 %sub, 15
211   %and = and i32 %shr, %x
212   ret i32 %and