1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 define i1 @test2(i1 %X, i1 %Y) {
6 ; CHECK-NEXT: [[A:%.*]] = and i1 [[X:%.*]], [[Y:%.*]]
7 ; CHECK-NEXT: ret i1 [[A]]
14 define i1 @test2_logical(i1 %X, i1 %Y) {
15 ; CHECK-LABEL: @test2_logical(
16 ; CHECK-NEXT: [[A:%.*]] = select i1 [[X:%.*]], i1 [[Y:%.*]], i1 false
17 ; CHECK-NEXT: ret i1 [[A]]
19 %a = select i1 %X, i1 %Y, i1 false
20 %b = select i1 %a, i1 %X, i1 false
24 define i32 @test3(i32 %X, i32 %Y) {
25 ; CHECK-LABEL: @test3(
26 ; CHECK-NEXT: [[A:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
27 ; CHECK-NEXT: ret i32 [[A]]
34 define i1 @test7(i32 %i, i1 %b) {
35 ; CHECK-LABEL: @test7(
36 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[I:%.*]], 0
37 ; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[B:%.*]]
38 ; CHECK-NEXT: ret i1 [[TMP2]]
40 %cmp1 = icmp slt i32 %i, 1
41 %cmp2 = icmp sgt i32 %i, -1
42 %and1 = and i1 %cmp1, %b
43 %and2 = and i1 %and1, %cmp2
47 define i1 @test7_logical(i32 %i, i1 %b) {
48 ; CHECK-LABEL: @test7_logical(
49 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[I:%.*]], 1
50 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i32 [[I]], -1
51 ; CHECK-NEXT: [[AND1:%.*]] = select i1 [[CMP1]], i1 [[B:%.*]], i1 false
52 ; CHECK-NEXT: [[AND2:%.*]] = and i1 [[AND1]], [[CMP2]]
53 ; CHECK-NEXT: ret i1 [[AND2]]
55 %cmp1 = icmp slt i32 %i, 1
56 %cmp2 = icmp sgt i32 %i, -1
57 %and1 = select i1 %cmp1, i1 %b, i1 false
58 %and2 = select i1 %and1, i1 %cmp2, i1 false
62 define i1 @test8(i32 %i) {
63 ; CHECK-LABEL: @test8(
64 ; CHECK-NEXT: [[I_OFF:%.*]] = add i32 [[I:%.*]], -1
65 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[I_OFF]], 13
66 ; CHECK-NEXT: ret i1 [[TMP1]]
68 %cmp1 = icmp ne i32 %i, 0
69 %cmp2 = icmp ult i32 %i, 14
70 %cond = and i1 %cmp1, %cmp2
74 define i1 @test8_logical(i32 %i) {
75 ; CHECK-LABEL: @test8_logical(
76 ; CHECK-NEXT: [[I_OFF:%.*]] = add i32 [[I:%.*]], -1
77 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[I_OFF]], 13
78 ; CHECK-NEXT: ret i1 [[TMP1]]
80 %cmp1 = icmp ne i32 %i, 0
81 %cmp2 = icmp ult i32 %i, 14
82 %cond = select i1 %cmp1, i1 %cmp2, i1 false
86 ; FIXME: Vectors should fold too.
87 define <2 x i1> @test8vec(<2 x i32> %i) {
88 ; CHECK-LABEL: @test8vec(
89 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne <2 x i32> [[I:%.*]], zeroinitializer
90 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ult <2 x i32> [[I]], <i32 14, i32 14>
91 ; CHECK-NEXT: [[COND:%.*]] = and <2 x i1> [[CMP1]], [[CMP2]]
92 ; CHECK-NEXT: ret <2 x i1> [[COND]]
94 %cmp1 = icmp ne <2 x i32> %i, zeroinitializer
95 %cmp2 = icmp ult <2 x i32> %i, <i32 14, i32 14>
96 %cond = and <2 x i1> %cmp1, %cmp2
100 ; combine -x & 1 into x & 1
101 define i64 @test9(i64 %x) {
102 ; CHECK-LABEL: @test9(
103 ; CHECK-NEXT: [[AND:%.*]] = and i64 [[X:%.*]], 1
104 ; CHECK-NEXT: ret i64 [[AND]]
106 %sub = sub nsw i64 0, %x
107 %and = and i64 %sub, 1
111 ; combine -x & 1 into x & 1
112 define <2 x i64> @test9vec(<2 x i64> %x) {
113 ; CHECK-LABEL: @test9vec(
114 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> [[X:%.*]], <i64 1, i64 1>
115 ; CHECK-NEXT: ret <2 x i64> [[AND]]
117 %sub = sub nsw <2 x i64> <i64 0, i64 0>, %x
118 %and = and <2 x i64> %sub, <i64 1, i64 1>
122 define i64 @test10(i64 %x) {
123 ; CHECK-LABEL: @test10(
124 ; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[X:%.*]], -2
125 ; CHECK-NEXT: [[ADD:%.*]] = sub i64 0, [[TMP1]]
126 ; CHECK-NEXT: ret i64 [[ADD]]
128 %sub = sub nsw i64 0, %x
129 %and = and i64 %sub, 1
130 %add = add i64 %sub, %and
134 ; (1 << x) & 1 --> zext(x == 0)
136 define i8 @and1_shl1_is_cmp_eq_0(i8 %x) {
137 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0(
138 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
139 ; CHECK-NEXT: [[AND:%.*]] = zext i1 [[TMP1]] to i8
140 ; CHECK-NEXT: ret i8 [[AND]]
147 ; Don't do it if the shift has another use.
149 define i8 @and1_shl1_is_cmp_eq_0_multiuse(i8 %x) {
150 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_multiuse(
151 ; CHECK-NEXT: [[SH:%.*]] = shl i8 1, [[X:%.*]]
152 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[SH]], 1
153 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SH]], [[AND]]
154 ; CHECK-NEXT: ret i8 [[ADD]]
158 %add = add i8 %sh, %and
162 ; (1 << x) & 1 --> zext(x == 0)
164 define <2 x i8> @and1_shl1_is_cmp_eq_0_vec(<2 x i8> %x) {
165 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_vec(
166 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer
167 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
168 ; CHECK-NEXT: ret <2 x i8> [[AND]]
170 %sh = shl <2 x i8> <i8 1, i8 1>, %x
171 %and = and <2 x i8> %sh, <i8 1, i8 1>
175 define <2 x i8> @and1_shl1_is_cmp_eq_0_vec_undef(<2 x i8> %x) {
176 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_vec_undef(
177 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer
178 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
179 ; CHECK-NEXT: ret <2 x i8> [[AND]]
181 %sh = shl <2 x i8> <i8 1, i8 undef>, %x
182 %and = and <2 x i8> %sh, <i8 1, i8 undef>
186 ; The mask is unnecessary.
188 define i8 @and1_lshr1_is_cmp_eq_0(i8 %x) {
189 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0(
190 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 1, [[X:%.*]]
191 ; CHECK-NEXT: ret i8 [[SH]]
198 define i8 @and1_lshr1_is_cmp_eq_0_multiuse(i8 %x) {
199 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_multiuse(
200 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 1, [[X:%.*]]
201 ; CHECK-NEXT: [[ADD:%.*]] = shl nuw nsw i8 [[SH]], 1
202 ; CHECK-NEXT: ret i8 [[ADD]]
206 %add = add i8 %sh, %and
210 ; The mask is unnecessary.
212 define <2 x i8> @and1_lshr1_is_cmp_eq_0_vec(<2 x i8> %x) {
213 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_vec(
214 ; CHECK-NEXT: [[SH:%.*]] = lshr <2 x i8> <i8 1, i8 1>, [[X:%.*]]
215 ; CHECK-NEXT: ret <2 x i8> [[SH]]
217 %sh = lshr <2 x i8> <i8 1, i8 1>, %x
218 %and = and <2 x i8> %sh, <i8 1, i8 1>
222 define <2 x i8> @and1_lshr1_is_cmp_eq_0_vec_undef(<2 x i8> %x) {
223 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_vec_undef(
224 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer
225 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
226 ; CHECK-NEXT: ret <2 x i8> [[AND]]
228 %sh = lshr <2 x i8> <i8 1, i8 undef>, %x
229 %and = and <2 x i8> %sh, <i8 1, i8 undef>
233 ; The add in this test is unnecessary because the LSBs of the LHS are 0 and the 'and' only consumes bits from those LSBs. It doesn't matter what happens to the upper bits.
234 define i32 @test11(i32 %a, i32 %b) {
235 ; CHECK-LABEL: @test11(
236 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
237 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
238 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
239 ; CHECK-NEXT: ret i32 [[W]]
244 %w = mul i32 %z, %x ; to keep the shift from being removed
248 ; The add in this test is unnecessary because the LSBs of the RHS are 0 and the 'and' only consumes bits from those LSBs. It doesn't matter what happens to the upper bits.
249 define i32 @test12(i32 %a, i32 %b) {
250 ; CHECK-LABEL: @test12(
251 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
252 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
253 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
254 ; CHECK-NEXT: ret i32 [[W]]
259 %w = mul i32 %z, %x ; to keep the shift from being removed
263 ; The sub in this test is unnecessary because the LSBs of the RHS are 0 and the 'and' only consumes bits from those LSBs. It doesn't matter what happens to the upper bits.
264 define i32 @test13(i32 %a, i32 %b) {
265 ; CHECK-LABEL: @test13(
266 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
267 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
268 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
269 ; CHECK-NEXT: ret i32 [[W]]
274 %w = mul i32 %z, %x ; to keep the shift from being removed
278 ; The sub in this test cannot be removed because we need to keep the negation of %b. TODO: But we should be able to replace the LHS of it with a 0.
279 define i32 @test14(i32 %a, i32 %b) {
280 ; CHECK-LABEL: @test14(
281 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
282 ; CHECK-NEXT: [[Y:%.*]] = sub i32 0, [[B:%.*]]
283 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 128
284 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
285 ; CHECK-NEXT: ret i32 [[W]]
290 %w = mul i32 %z, %x ; to keep the shift from being removed