1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
6 define i32 @test1(i32 %a, i32 %b) nounwind readnone {
8 ; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B:%.*]], [[A:%.*]]
9 ; CHECK-NEXT: [[T2:%.*]] = icmp sgt i32 [[TMP1]], -1
10 ; CHECK-NEXT: [[T3:%.*]] = zext i1 [[T2]] to i32
11 ; CHECK-NEXT: ret i32 [[T3]]
13 %t0 = icmp sgt i32 %a, -1
14 %t1 = icmp slt i32 %b, 0
16 %t3 = zext i1 %t2 to i32
20 ; TODO: This optimizes partially but not all the way.
21 define i32 @test2(i32 %a, i32 %b) nounwind readnone {
22 ; CHECK-LABEL: @test2(
23 ; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
24 ; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1]], 3
25 ; CHECK-NEXT: [[DOTLOBIT:%.*]] = and i32 [[TMP2]], 1
26 ; CHECK-NEXT: [[T3:%.*]] = xor i32 [[DOTLOBIT]], 1
27 ; CHECK-NEXT: ret i32 [[T3]]
31 %t2 = icmp eq i32 %t0, %t1
32 %t3 = zext i1 %t2 to i32
36 define i32 @test3(i32 %a, i32 %b) nounwind readnone {
37 ; CHECK-LABEL: @test3(
38 ; CHECK-NEXT: [[T2_UNSHIFTED:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
39 ; CHECK-NEXT: [[T2:%.*]] = icmp sgt i32 [[T2_UNSHIFTED]], -1
40 ; CHECK-NEXT: [[T3:%.*]] = zext i1 [[T2]] to i32
41 ; CHECK-NEXT: ret i32 [[T3]]
45 %t2 = icmp eq i32 %t0, %t1
46 %t3 = zext i1 %t2 to i32
50 define <2 x i32> @test3vec(<2 x i32> %a, <2 x i32> %b) nounwind readnone {
51 ; CHECK-LABEL: @test3vec(
52 ; CHECK-NEXT: [[T2_UNSHIFTED:%.*]] = xor <2 x i32> [[A:%.*]], [[B:%.*]]
53 ; CHECK-NEXT: [[T2:%.*]] = icmp sgt <2 x i32> [[T2_UNSHIFTED]], <i32 -1, i32 -1>
54 ; CHECK-NEXT: [[T3:%.*]] = zext <2 x i1> [[T2]] to <2 x i32>
55 ; CHECK-NEXT: ret <2 x i32> [[T3]]
57 %t0 = lshr <2 x i32> %a, <i32 31, i32 31>
58 %t1 = lshr <2 x i32> %b, <i32 31, i32 31>
59 %t2 = icmp eq <2 x i32> %t0, %t1
60 %t3 = zext <2 x i1> %t2 to <2 x i32>
64 define <2 x i32> @test3vec_undef1(<2 x i32> %a, <2 x i32> %b) nounwind readnone {
65 ; CHECK-LABEL: @test3vec_undef1(
66 ; CHECK-NEXT: [[T2_UNSHIFTED:%.*]] = xor <2 x i32> [[A:%.*]], [[B:%.*]]
67 ; CHECK-NEXT: [[T2:%.*]] = icmp ult <2 x i32> [[T2_UNSHIFTED]], <i32 16777216, i32 16777216>
68 ; CHECK-NEXT: [[T3:%.*]] = zext <2 x i1> [[T2]] to <2 x i32>
69 ; CHECK-NEXT: ret <2 x i32> [[T3]]
71 %t0 = lshr <2 x i32> %a, <i32 24, i32 undef>
72 %t1 = lshr <2 x i32> %b, <i32 24, i32 24>
73 %t2 = icmp eq <2 x i32> %t0, %t1
74 %t3 = zext <2 x i1> %t2 to <2 x i32>
78 define <2 x i32> @test3vec_undef2(<2 x i32> %a, <2 x i32> %b) nounwind readnone {
79 ; CHECK-LABEL: @test3vec_undef2(
80 ; CHECK-NEXT: [[T2_UNSHIFTED:%.*]] = xor <2 x i32> [[A:%.*]], [[B:%.*]]
81 ; CHECK-NEXT: [[T2:%.*]] = icmp ult <2 x i32> [[T2_UNSHIFTED]], <i32 131072, i32 131072>
82 ; CHECK-NEXT: [[T3:%.*]] = zext <2 x i1> [[T2]] to <2 x i32>
83 ; CHECK-NEXT: ret <2 x i32> [[T3]]
85 %t0 = lshr <2 x i32> %a, <i32 undef, i32 17>
86 %t1 = lshr <2 x i32> %b, <i32 undef, i32 17>
87 %t2 = icmp eq <2 x i32> %t0, %t1
88 %t3 = zext <2 x i1> %t2 to <2 x i32>
94 define <2 x i32> @test3vec_diff(<2 x i32> %a, <2 x i32> %b) nounwind readnone {
95 ; CHECK-LABEL: @test3vec_diff(
96 ; CHECK-NEXT: [[T0:%.*]] = lshr <2 x i32> [[A:%.*]], <i32 31, i32 31>
97 ; CHECK-NEXT: [[T1:%.*]] = lshr <2 x i32> [[B:%.*]], <i32 30, i32 30>
98 ; CHECK-NEXT: [[T2:%.*]] = icmp eq <2 x i32> [[T0]], [[T1]]
99 ; CHECK-NEXT: [[T3:%.*]] = zext <2 x i1> [[T2]] to <2 x i32>
100 ; CHECK-NEXT: ret <2 x i32> [[T3]]
102 %t0 = lshr <2 x i32> %a, <i32 31, i32 31>
103 %t1 = lshr <2 x i32> %b, <i32 30, i32 30>
104 %t2 = icmp eq <2 x i32> %t0, %t1
105 %t3 = zext <2 x i1> %t2 to <2 x i32>
109 define <2 x i32> @test3vec_non-uniform(<2 x i32> %a, <2 x i32> %b) nounwind readnone {
110 ; CHECK-LABEL: @test3vec_non-uniform(
111 ; CHECK-NEXT: [[T0:%.*]] = lshr <2 x i32> [[A:%.*]], <i32 30, i32 31>
112 ; CHECK-NEXT: [[T1:%.*]] = lshr <2 x i32> [[B:%.*]], <i32 30, i32 31>
113 ; CHECK-NEXT: [[T2:%.*]] = icmp eq <2 x i32> [[T0]], [[T1]]
114 ; CHECK-NEXT: [[T3:%.*]] = zext <2 x i1> [[T2]] to <2 x i32>
115 ; CHECK-NEXT: ret <2 x i32> [[T3]]
117 %t0 = lshr <2 x i32> %a, <i32 30, i32 31>
118 %t1 = lshr <2 x i32> %b, <i32 30, i32 31>
119 %t2 = icmp eq <2 x i32> %t0, %t1
120 %t3 = zext <2 x i1> %t2 to <2 x i32>
124 ; Variation on @test3: checking the 2nd bit in a situation where the 5th bit
126 define i32 @test3i(i32 %a, i32 %b) nounwind readnone {
127 ; CHECK-LABEL: @test3i(
128 ; CHECK-NEXT: [[T01:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
129 ; CHECK-NEXT: [[T4:%.*]] = icmp sgt i32 [[T01]], -1
130 ; CHECK-NEXT: [[T5:%.*]] = zext i1 [[T4]] to i32
131 ; CHECK-NEXT: ret i32 [[T5]]
133 %t0 = lshr i32 %a, 29
134 %t1 = lshr i32 %b, 29
137 %t4 = icmp eq i32 %t2, %t3
138 %t5 = zext i1 %t4 to i32
142 define i1 @test4a(i32 %a) {
143 ; CHECK-LABEL: @test4a(
144 ; CHECK-NEXT: [[C:%.*]] = icmp slt i32 [[A:%.*]], 1
145 ; CHECK-NEXT: ret i1 [[C]]
149 %r = lshr i32 %na, 31
150 %signum = or i32 %l, %r
151 %c = icmp slt i32 %signum, 1
155 define <2 x i1> @test4a_vec(<2 x i32> %a) {
156 ; CHECK-LABEL: @test4a_vec(
157 ; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i32> [[A:%.*]], <i32 1, i32 1>
158 ; CHECK-NEXT: ret <2 x i1> [[C]]
160 %l = ashr <2 x i32> %a, <i32 31, i32 31>
161 %na = sub <2 x i32> zeroinitializer, %a
162 %r = lshr <2 x i32> %na, <i32 31, i32 31>
163 %signum = or <2 x i32> %l, %r
164 %c = icmp slt <2 x i32> %signum, <i32 1, i32 1>
168 define i1 @test4b(i64 %a) {
169 ; CHECK-LABEL: @test4b(
170 ; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
171 ; CHECK-NEXT: ret i1 [[C]]
175 %r = lshr i64 %na, 63
176 %signum = or i64 %l, %r
177 %c = icmp slt i64 %signum, 1
181 define i1 @test4c(i64 %a) {
182 ; CHECK-LABEL: @test4c(
183 ; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
184 ; CHECK-NEXT: ret i1 [[C]]
188 %r = lshr i64 %na, 63
189 %signum = or i64 %l, %r
190 %signum.trunc = trunc i64 %signum to i32
191 %c = icmp slt i32 %signum.trunc, 1
195 define <2 x i1> @test4c_vec(<2 x i64> %a) {
196 ; CHECK-LABEL: @test4c_vec(
197 ; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i64> [[A:%.*]], <i64 1, i64 1>
198 ; CHECK-NEXT: ret <2 x i1> [[C]]
200 %l = ashr <2 x i64> %a, <i64 63, i64 63>
201 %na = sub <2 x i64> zeroinitializer, %a
202 %r = lshr <2 x i64> %na, <i64 63, i64 63>
203 %signum = or <2 x i64> %l, %r
204 %signum.trunc = trunc <2 x i64> %signum to <2 x i32>
205 %c = icmp slt <2 x i32> %signum.trunc, <i32 1, i32 1>
211 define i1 @shift_trunc_signbit_test(i32 %x) {
212 ; CHECK-LABEL: @shift_trunc_signbit_test(
213 ; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[X:%.*]], 0
214 ; CHECK-NEXT: ret i1 [[R]]
216 %sh = lshr i32 %x, 24
217 %tr = trunc i32 %sh to i8
218 %r = icmp slt i8 %tr, 0
222 define <2 x i1> @shift_trunc_signbit_test_vec_uses(<2 x i17> %x, ptr %p1, ptr %p2) {
223 ; CHECK-LABEL: @shift_trunc_signbit_test_vec_uses(
224 ; CHECK-NEXT: [[SH:%.*]] = lshr <2 x i17> [[X:%.*]], <i17 4, i17 4>
225 ; CHECK-NEXT: store <2 x i17> [[SH]], ptr [[P1:%.*]], align 8
226 ; CHECK-NEXT: [[TR:%.*]] = trunc <2 x i17> [[SH]] to <2 x i13>
227 ; CHECK-NEXT: store <2 x i13> [[TR]], ptr [[P2:%.*]], align 4
228 ; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i17> [[X]], <i17 -1, i17 -1>
229 ; CHECK-NEXT: ret <2 x i1> [[R]]
231 %sh = lshr <2 x i17> %x, <i17 4, i17 4>
232 store <2 x i17> %sh, ptr %p1
233 %tr = trunc <2 x i17> %sh to <2 x i13>
234 store <2 x i13> %tr, ptr %p2
235 %r = icmp sgt <2 x i13> %tr, <i13 -1, i13 -1>
239 ; negative test - but this reduces with a mask op
241 define i1 @shift_trunc_wrong_shift(i32 %x) {
242 ; CHECK-LABEL: @shift_trunc_wrong_shift(
243 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 1073741824
244 ; CHECK-NEXT: [[R:%.*]] = icmp ne i32 [[TMP1]], 0
245 ; CHECK-NEXT: ret i1 [[R]]
247 %sh = lshr i32 %x, 23
248 %tr = trunc i32 %sh to i8
249 %r = icmp slt i8 %tr, 0
255 define i1 @shift_trunc_wrong_cmp(i32 %x) {
256 ; CHECK-LABEL: @shift_trunc_wrong_cmp(
257 ; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 24
258 ; CHECK-NEXT: [[TR:%.*]] = trunc i32 [[SH]] to i8
259 ; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[TR]], 1
260 ; CHECK-NEXT: ret i1 [[R]]
262 %sh = lshr i32 %x, 24
263 %tr = trunc i32 %sh to i8
264 %r = icmp slt i8 %tr, 1