1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; For pattern ((X l>> Y) & ~C) ==/!= 0; when C+1 is power of 2
5 ; it may be optimal to fold into (X l>> Y) </>= C+1
6 ; rather than X & (~C << Y) ==/!= 0
10 define i1 @scalar_i8_lshr_and_negC_eq(i8 %x, i8 %y) {
11 ; CHECK-LABEL: @scalar_i8_lshr_and_negC_eq(
12 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i8 [[X:%.*]], [[Y:%.*]]
13 ; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[LSHR]], 4
14 ; CHECK-NEXT: ret i1 [[R]]
16 %lshr = lshr i8 %x, %y
17 %and = and i8 %lshr, 252 ; ~3
18 %r = icmp eq i8 %and, 0
22 define i1 @scalar_i16_lshr_and_negC_eq(i16 %x, i16 %y) {
23 ; CHECK-LABEL: @scalar_i16_lshr_and_negC_eq(
24 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i16 [[X:%.*]], [[Y:%.*]]
25 ; CHECK-NEXT: [[R:%.*]] = icmp ult i16 [[LSHR]], 128
26 ; CHECK-NEXT: ret i1 [[R]]
28 %lshr = lshr i16 %x, %y
29 %and = and i16 %lshr, 65408 ; ~127
30 %r = icmp eq i16 %and, 0
34 define i1 @scalar_i32_lshr_and_negC_eq(i32 %x, i32 %y) {
35 ; CHECK-LABEL: @scalar_i32_lshr_and_negC_eq(
36 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
37 ; CHECK-NEXT: [[R:%.*]] = icmp ult i32 [[LSHR]], 262144
38 ; CHECK-NEXT: ret i1 [[R]]
40 %lshr = lshr i32 %x, %y
41 %and = and i32 %lshr, 4294705152 ; ~262143
42 %r = icmp eq i32 %and, 0
46 define i1 @scalar_i64_lshr_and_negC_eq(i64 %x, i64 %y) {
47 ; CHECK-LABEL: @scalar_i64_lshr_and_negC_eq(
48 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i64 [[X:%.*]], [[Y:%.*]]
49 ; CHECK-NEXT: [[R:%.*]] = icmp ult i64 [[LSHR]], 8589934592
50 ; CHECK-NEXT: ret i1 [[R]]
52 %lshr = lshr i64 %x, %y
53 %and = and i64 %lshr, 18446744065119617024 ; ~8589934591
54 %r = icmp eq i64 %and, 0
58 define i1 @scalar_i32_lshr_and_negC_ne(i32 %x, i32 %y) {
59 ; CHECK-LABEL: @scalar_i32_lshr_and_negC_ne(
60 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
61 ; CHECK-NEXT: [[R:%.*]] = icmp ugt i32 [[LSHR]], 262143
62 ; CHECK-NEXT: ret i1 [[R]]
64 %lshr = lshr i32 %x, %y
65 %and = and i32 %lshr, 4294705152 ; ~262143
66 %r = icmp ne i32 %and, 0 ; check 'ne' predicate
72 define <4 x i1> @vec_4xi32_lshr_and_negC_eq(<4 x i32> %x, <4 x i32> %y) {
73 ; CHECK-LABEL: @vec_4xi32_lshr_and_negC_eq(
74 ; CHECK-NEXT: [[LSHR:%.*]] = lshr <4 x i32> [[X:%.*]], [[Y:%.*]]
75 ; CHECK-NEXT: [[R:%.*]] = icmp ult <4 x i32> [[LSHR]], <i32 8, i32 8, i32 8, i32 8>
76 ; CHECK-NEXT: ret <4 x i1> [[R]]
78 %lshr = lshr <4 x i32> %x, %y
79 %and = and <4 x i32> %lshr, <i32 4294967288, i32 4294967288, i32 4294967288, i32 4294967288> ; ~7
80 %r = icmp eq <4 x i32> %and, <i32 0, i32 0, i32 0, i32 0>
84 define <4 x i1> @vec_lshr_and_negC_eq_undef1(<4 x i32> %x, <4 x i32> %y) {
85 ; CHECK-LABEL: @vec_lshr_and_negC_eq_undef1(
86 ; CHECK-NEXT: [[LSHR:%.*]] = lshr <4 x i32> [[X:%.*]], [[Y:%.*]]
87 ; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[LSHR]], <i32 -8, i32 undef, i32 -8, i32 -8>
88 ; CHECK-NEXT: [[R:%.*]] = icmp eq <4 x i32> [[AND]], zeroinitializer
89 ; CHECK-NEXT: ret <4 x i1> [[R]]
91 %lshr = lshr <4 x i32> %x, %y
92 %and = and <4 x i32> %lshr, <i32 4294967288, i32 undef, i32 4294967288, i32 4294967288> ; ~7
93 %r = icmp eq <4 x i32> %and, <i32 0, i32 0, i32 0, i32 0>
97 define <4 x i1> @vec_lshr_and_negC_eq_undef2(<4 x i32> %x, <4 x i32> %y) {
98 ; CHECK-LABEL: @vec_lshr_and_negC_eq_undef2(
99 ; CHECK-NEXT: [[LSHR:%.*]] = lshr <4 x i32> [[X:%.*]], [[Y:%.*]]
100 ; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[LSHR]], <i32 -8, i32 -8, i32 -8, i32 -8>
101 ; CHECK-NEXT: [[R:%.*]] = icmp eq <4 x i32> [[AND]], <i32 0, i32 0, i32 0, i32 undef>
102 ; CHECK-NEXT: ret <4 x i1> [[R]]
104 %lshr = lshr <4 x i32> %x, %y
105 %and = and <4 x i32> %lshr, <i32 4294967288, i32 4294967288, i32 4294967288, i32 4294967288> ; ~7
106 %r = icmp eq <4 x i32> %and, <i32 0, i32 0, i32 0, i32 undef>
110 define <4 x i1> @vec_lshr_and_negC_eq_undef3(<4 x i32> %x, <4 x i32> %y) {
111 ; CHECK-LABEL: @vec_lshr_and_negC_eq_undef3(
112 ; CHECK-NEXT: [[LSHR:%.*]] = lshr <4 x i32> [[X:%.*]], [[Y:%.*]]
113 ; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[LSHR]], <i32 -8, i32 -8, i32 undef, i32 -8>
114 ; CHECK-NEXT: [[R:%.*]] = icmp eq <4 x i32> [[AND]], <i32 0, i32 0, i32 0, i32 undef>
115 ; CHECK-NEXT: ret <4 x i1> [[R]]
117 %lshr = lshr <4 x i32> %x, %y
118 %and = and <4 x i32> %lshr, <i32 4294967288, i32 4294967288, i32 undef, i32 4294967288> ; ~7
119 %r = icmp eq <4 x i32> %and, <i32 0, i32 0, i32 0, i32 undef>
126 define i1 @scalar_lshr_and_negC_eq_extra_use_lshr(i32 %x, i32 %y, i32 %z, i32* %p) {
127 ; CHECK-LABEL: @scalar_lshr_and_negC_eq_extra_use_lshr(
128 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
129 ; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[LSHR]], [[Z:%.*]]
130 ; CHECK-NEXT: store i32 [[XOR]], i32* [[P:%.*]], align 4
131 ; CHECK-NEXT: [[R:%.*]] = icmp ult i32 [[LSHR]], 8
132 ; CHECK-NEXT: ret i1 [[R]]
134 %lshr = lshr i32 %x, %y
135 %xor = xor i32 %lshr, %z ; extra use of lshr
136 store i32 %xor, i32* %p
137 %and = and i32 %lshr, 4294967288 ; ~7
138 %r = icmp eq i32 %and, 0
143 define i1 @scalar_lshr_and_negC_eq_extra_use_and(i32 %x, i32 %y, i32 %z, i32* %p) {
144 ; CHECK-LABEL: @scalar_lshr_and_negC_eq_extra_use_and(
145 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
146 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[LSHR]], -8
147 ; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[AND]], [[Z:%.*]]
148 ; CHECK-NEXT: store i32 [[MUL]], i32* [[P:%.*]], align 4
149 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[AND]], 0
150 ; CHECK-NEXT: ret i1 [[R]]
152 %lshr = lshr i32 %x, %y
153 %and = and i32 %lshr, 4294967288 ; ~7
154 %mul = mul i32 %and, %z ; extra use of and
155 store i32 %mul, i32* %p
156 %r = icmp eq i32 %and, 0
161 define i1 @scalar_lshr_and_negC_eq_extra_use_lshr_and(i32 %x, i32 %y, i32 %z, i32* %p, i32* %q) {
162 ; CHECK-LABEL: @scalar_lshr_and_negC_eq_extra_use_lshr_and(
163 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
164 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[LSHR]], -8
165 ; CHECK-NEXT: store i32 [[AND]], i32* [[P:%.*]], align 4
166 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[LSHR]], [[Z:%.*]]
167 ; CHECK-NEXT: store i32 [[ADD]], i32* [[Q:%.*]], align 4
168 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[AND]], 0
169 ; CHECK-NEXT: ret i1 [[R]]
171 %lshr = lshr i32 %x, %y
172 %and = and i32 %lshr, 4294967288 ; ~7
173 store i32 %and, i32* %p ; extra use of and
174 %add = add i32 %lshr, %z ; extra use of lshr
175 store i32 %add, i32* %q
176 %r = icmp eq i32 %and, 0
182 ; TODO: This could be reduced to lshr+icmp ult.
184 define i1 @scalar_i32_lshr_and_negC_eq_X_is_constant1(i32 %y) {
185 ; CHECK-LABEL: @scalar_i32_lshr_and_negC_eq_X_is_constant1(
186 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 12345, [[Y:%.*]]
187 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[LSHR]], 16376
188 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[AND]], 0
189 ; CHECK-NEXT: ret i1 [[R]]
191 %lshr = lshr i32 12345, %y
192 %and = and i32 %lshr, 4294967288 ; ~7
193 %r = icmp eq i32 %and, 0
197 ; TODO: This could be reduced to lshr+icmp ult.
199 define i1 @scalar_i32_lshr_and_negC_eq_X_is_constant2(i32 %y) {
200 ; CHECK-LABEL: @scalar_i32_lshr_and_negC_eq_X_is_constant2(
201 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 268435456, [[Y:%.*]]
202 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[LSHR]], 536870904
203 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[AND]], 0
204 ; CHECK-NEXT: ret i1 [[R]]
206 %lshr = lshr i32 268435456, %y
207 %and = and i32 %lshr, 4294967288 ; ~7
208 %r = icmp eq i32 %and, 0
212 ; Check 'slt' predicate
214 define i1 @scalar_i32_lshr_and_negC_slt(i32 %x, i32 %y) {
215 ; CHECK-LABEL: @scalar_i32_lshr_and_negC_slt(
216 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]]
217 ; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[LSHR]], 0
218 ; CHECK-NEXT: ret i1 [[R]]
220 %lshr = lshr i32 %x, %y
221 %and = and i32 %lshr, 4294967288 ; ~7
222 %r = icmp slt i32 %and, 0
226 ; Compare with nonzero
228 define i1 @scalar_i32_lshr_and_negC_eq_nonzero(i32 %x, i32 %y) {
229 ; CHECK-LABEL: @scalar_i32_lshr_and_negC_eq_nonzero(
230 ; CHECK-NEXT: ret i1 false
232 %lshr = lshr i32 %x, %y
233 %and = and i32 %lshr, 4294967288 ; ~7
234 %r = icmp eq i32 %and, 1 ; should be comparing with 0