1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 ; For pattern ((X << Y) & signbit) ==/!= 0
5 ; it may be optimal to fold into (X << Y) >=/< 0
6 ; rather than X & (signbit l>> Y) ==/!= 0
10 define i1 @scalar_i8_shl_and_signbit_eq(i8 %x, i8 %y) {
11 ; CHECK-LABEL: @scalar_i8_shl_and_signbit_eq(
12 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 [[X:%.*]], [[Y:%.*]]
13 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[SHL]], -1
14 ; CHECK-NEXT: ret i1 [[R]]
17 %and = and i8 %shl, 128
18 %r = icmp eq i8 %and, 0
22 define i1 @scalar_i16_shl_and_signbit_eq(i16 %x, i16 %y) {
23 ; CHECK-LABEL: @scalar_i16_shl_and_signbit_eq(
24 ; CHECK-NEXT: [[SHL:%.*]] = shl i16 [[X:%.*]], [[Y:%.*]]
25 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i16 [[SHL]], -1
26 ; CHECK-NEXT: ret i1 [[R]]
29 %and = and i16 %shl, 32768
30 %r = icmp eq i16 %and, 0
34 define i1 @scalar_i32_shl_and_signbit_eq(i32 %x, i32 %y) {
35 ; CHECK-LABEL: @scalar_i32_shl_and_signbit_eq(
36 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
37 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i32 [[SHL]], -1
38 ; CHECK-NEXT: ret i1 [[R]]
41 %and = and i32 %shl, 2147483648
42 %r = icmp eq i32 %and, 0
46 define i1 @scalar_i64_shl_and_signbit_eq(i64 %x, i64 %y) {
47 ; CHECK-LABEL: @scalar_i64_shl_and_signbit_eq(
48 ; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[X:%.*]], [[Y:%.*]]
49 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i64 [[SHL]], -1
50 ; CHECK-NEXT: ret i1 [[R]]
53 %and = and i64 %shl, 9223372036854775808
54 %r = icmp eq i64 %and, 0
58 define i1 @scalar_i32_shl_and_signbit_ne(i32 %x, i32 %y) {
59 ; CHECK-LABEL: @scalar_i32_shl_and_signbit_ne(
60 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
61 ; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[SHL]], 0
62 ; CHECK-NEXT: ret i1 [[R]]
65 %and = and i32 %shl, 2147483648
66 %r = icmp ne i32 %and, 0 ; check 'ne' predicate
72 define <4 x i1> @vec_4xi32_shl_and_signbit_eq(<4 x i32> %x, <4 x i32> %y) {
73 ; CHECK-LABEL: @vec_4xi32_shl_and_signbit_eq(
74 ; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], [[Y:%.*]]
75 ; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[SHL]], splat (i32 -1)
76 ; CHECK-NEXT: ret <4 x i1> [[R]]
78 %shl = shl <4 x i32> %x, %y
79 %and = and <4 x i32> %shl, <i32 2147483648, i32 2147483648, i32 2147483648, i32 2147483648>
80 %r = icmp eq <4 x i32> %and, <i32 0, i32 0, i32 0, i32 0>
84 define <4 x i1> @vec_4xi32_shl_and_signbit_eq_poison1(<4 x i32> %x, <4 x i32> %y) {
85 ; CHECK-LABEL: @vec_4xi32_shl_and_signbit_eq_poison1(
86 ; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], [[Y:%.*]]
87 ; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[SHL]], splat (i32 -1)
88 ; CHECK-NEXT: ret <4 x i1> [[R]]
90 %shl = shl <4 x i32> %x, %y
91 %and = and <4 x i32> %shl, <i32 2147483648, i32 poison, i32 2147483648, i32 2147483648>
92 %r = icmp eq <4 x i32> %and, <i32 0, i32 0, i32 0, i32 0>
96 define <4 x i1> @vec_4xi32_shl_and_signbit_eq_poison2(<4 x i32> %x, <4 x i32> %y) {
97 ; CHECK-LABEL: @vec_4xi32_shl_and_signbit_eq_poison2(
98 ; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], [[Y:%.*]]
99 ; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[SHL]], splat (i32 -1)
100 ; CHECK-NEXT: ret <4 x i1> [[R]]
102 %shl = shl <4 x i32> %x, %y
103 %and = and <4 x i32> %shl, <i32 2147483648, i32 2147483648, i32 2147483648, i32 2147483648>
104 %r = icmp eq <4 x i32> %and, <i32 poison, i32 0, i32 0, i32 0>
108 define <4 x i1> @vec_4xi32_shl_and_signbit_eq_poison3(<4 x i32> %x, <4 x i32> %y) {
109 ; CHECK-LABEL: @vec_4xi32_shl_and_signbit_eq_poison3(
110 ; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], [[Y:%.*]]
111 ; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[SHL]], splat (i32 -1)
112 ; CHECK-NEXT: ret <4 x i1> [[R]]
114 %shl = shl <4 x i32> %x, %y
115 %and = and <4 x i32> %shl, <i32 2147483648, i32 poison, i32 2147483648, i32 2147483648>
116 %r = icmp eq <4 x i32> %and, <i32 0, i32 0, i32 0, i32 poison>
123 define i1 @scalar_shl_and_signbit_eq_extra_use_shl(i32 %x, i32 %y, i32 %z, ptr %p) {
124 ; CHECK-LABEL: @scalar_shl_and_signbit_eq_extra_use_shl(
125 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
126 ; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[SHL]], [[Z:%.*]]
127 ; CHECK-NEXT: store i32 [[XOR]], ptr [[P:%.*]], align 4
128 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i32 [[SHL]], -1
129 ; CHECK-NEXT: ret i1 [[R]]
131 %shl = shl i32 %x, %y
132 %xor = xor i32 %shl, %z ; extra use of shl
133 store i32 %xor, ptr %p
134 %and = and i32 %shl, 2147483648
135 %r = icmp eq i32 %and, 0
140 define i1 @scalar_shl_and_signbit_eq_extra_use_and(i32 %x, i32 %y, i32 %z, ptr %p) {
141 ; CHECK-LABEL: @scalar_shl_and_signbit_eq_extra_use_and(
142 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
143 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], -2147483648
144 ; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[AND]], [[Z:%.*]]
145 ; CHECK-NEXT: store i32 [[MUL]], ptr [[P:%.*]], align 4
146 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[AND]], 0
147 ; CHECK-NEXT: ret i1 [[R]]
149 %shl = shl i32 %x, %y
150 %and = and i32 %shl, 2147483648
151 %mul = mul i32 %and, %z ; extra use of and
152 store i32 %mul, ptr %p
153 %r = icmp eq i32 %and, 0
158 define i1 @scalar_shl_and_signbit_eq_extra_use_shl_and(i32 %x, i32 %y, i32 %z, ptr %p, ptr %q) {
159 ; CHECK-LABEL: @scalar_shl_and_signbit_eq_extra_use_shl_and(
160 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
161 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], -2147483648
162 ; CHECK-NEXT: store i32 [[AND]], ptr [[P:%.*]], align 4
163 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[SHL]], [[Z:%.*]]
164 ; CHECK-NEXT: store i32 [[ADD]], ptr [[Q:%.*]], align 4
165 ; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[AND]], 0
166 ; CHECK-NEXT: ret i1 [[R]]
168 %shl = shl i32 %x, %y
169 %and = and i32 %shl, 2147483648
170 store i32 %and, ptr %p ; extra use of and
171 %add = add i32 %shl, %z ; extra use of shl
172 store i32 %add, ptr %q
173 %r = icmp eq i32 %and, 0
181 define i1 @scalar_i32_shl_and_signbit_eq_X_is_constant1(i32 %y) {
182 ; CHECK-LABEL: @scalar_i32_shl_and_signbit_eq_X_is_constant1(
183 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 12345, [[Y:%.*]]
184 ; CHECK-NEXT: [[R:%.*]] = icmp sgt i32 [[SHL]], -1
185 ; CHECK-NEXT: ret i1 [[R]]
187 %shl = shl i32 12345, %y
188 %and = and i32 %shl, 2147483648
189 %r = icmp eq i32 %and, 0
193 define i1 @scalar_i32_shl_and_signbit_eq_X_is_constant2(i32 %y) {
194 ; CHECK-LABEL: @scalar_i32_shl_and_signbit_eq_X_is_constant2(
195 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[Y:%.*]], 31
196 ; CHECK-NEXT: ret i1 [[TMP1]]
199 %and = and i32 %shl, 2147483648
200 %r = icmp eq i32 %and, 0
204 ; Check 'slt' predicate
206 define i1 @scalar_i32_shl_and_signbit_slt(i32 %x, i32 %y) {
207 ; CHECK-LABEL: @scalar_i32_shl_and_signbit_slt(
208 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[Y:%.*]]
209 ; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[SHL]], 0
210 ; CHECK-NEXT: ret i1 [[R]]
212 %shl = shl i32 %x, %y
213 %and = and i32 %shl, 2147483648
214 %r = icmp slt i32 %and, 0
218 ; Compare with nonzero
220 define i1 @scalar_i32_shl_and_signbit_eq_nonzero(i32 %x, i32 %y) {
221 ; CHECK-LABEL: @scalar_i32_shl_and_signbit_eq_nonzero(
222 ; CHECK-NEXT: ret i1 false
224 %shl = shl i32 %x, %y
225 %and = and i32 %shl, 2147483648
226 %r = icmp eq i32 %and, 1 ; should be comparing with 0