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: [[TMP1:%.*]] = add i32 [[I:%.*]], -1
65 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 13
66 ; CHECK-NEXT: ret i1 [[TMP2]]
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: [[TMP1:%.*]] = add i32 [[I:%.*]], -1
77 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 13
78 ; CHECK-NEXT: ret i1 [[TMP2]]
80 %cmp1 = icmp ne i32 %i, 0
81 %cmp2 = icmp ult i32 %i, 14
82 %cond = select i1 %cmp1, i1 %cmp2, i1 false
86 define <2 x i1> @test8vec(<2 x i32> %i) {
87 ; CHECK-LABEL: @test8vec(
88 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i32> [[I:%.*]], <i32 -1, i32 -1>
89 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult <2 x i32> [[TMP1]], <i32 13, i32 13>
90 ; CHECK-NEXT: ret <2 x i1> [[TMP2]]
92 %cmp1 = icmp ne <2 x i32> %i, zeroinitializer
93 %cmp2 = icmp ult <2 x i32> %i, <i32 14, i32 14>
94 %cond = and <2 x i1> %cmp1, %cmp2
98 ; combine -x & 1 into x & 1
99 define i64 @test9(i64 %x) {
100 ; CHECK-LABEL: @test9(
101 ; CHECK-NEXT: [[AND:%.*]] = and i64 [[X:%.*]], 1
102 ; CHECK-NEXT: ret i64 [[AND]]
104 %sub = sub nsw i64 0, %x
105 %and = and i64 %sub, 1
109 ; combine -x & 1 into x & 1
110 define <2 x i64> @test9vec(<2 x i64> %x) {
111 ; CHECK-LABEL: @test9vec(
112 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> [[X:%.*]], <i64 1, i64 1>
113 ; CHECK-NEXT: ret <2 x i64> [[AND]]
115 %sub = sub nsw <2 x i64> <i64 0, i64 0>, %x
116 %and = and <2 x i64> %sub, <i64 1, i64 1>
120 define i64 @test10(i64 %x) {
121 ; CHECK-LABEL: @test10(
122 ; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[X:%.*]], -2
123 ; CHECK-NEXT: [[ADD:%.*]] = sub i64 0, [[TMP1]]
124 ; CHECK-NEXT: ret i64 [[ADD]]
126 %sub = sub nsw i64 0, %x
127 %and = and i64 %sub, 1
128 %add = add i64 %sub, %and
132 ; (1 << x) & 1 --> zext(x == 0)
134 define i8 @and1_shl1_is_cmp_eq_0(i8 %x) {
135 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0(
136 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
137 ; CHECK-NEXT: [[AND:%.*]] = zext i1 [[TMP1]] to i8
138 ; CHECK-NEXT: ret i8 [[AND]]
145 ; Don't do it if the shift has another use.
147 define i8 @and1_shl1_is_cmp_eq_0_multiuse(i8 %x) {
148 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_multiuse(
149 ; CHECK-NEXT: [[SH:%.*]] = shl i8 1, [[X:%.*]]
150 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[SH]], 1
151 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SH]], [[AND]]
152 ; CHECK-NEXT: ret i8 [[ADD]]
156 %add = add i8 %sh, %and
160 ; (1 << x) & 1 --> zext(x == 0)
162 define <2 x i8> @and1_shl1_is_cmp_eq_0_vec(<2 x i8> %x) {
163 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_vec(
164 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer
165 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
166 ; CHECK-NEXT: ret <2 x i8> [[AND]]
168 %sh = shl <2 x i8> <i8 1, i8 1>, %x
169 %and = and <2 x i8> %sh, <i8 1, i8 1>
173 define <2 x i8> @and1_shl1_is_cmp_eq_0_vec_undef(<2 x i8> %x) {
174 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_vec_undef(
175 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer
176 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
177 ; CHECK-NEXT: ret <2 x i8> [[AND]]
179 %sh = shl <2 x i8> <i8 1, i8 undef>, %x
180 %and = and <2 x i8> %sh, <i8 1, i8 undef>
184 ; The mask is unnecessary.
186 define i8 @and1_lshr1_is_cmp_eq_0(i8 %x) {
187 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0(
188 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 1, [[X:%.*]]
189 ; CHECK-NEXT: ret i8 [[SH]]
196 define i8 @and1_lshr1_is_cmp_eq_0_multiuse(i8 %x) {
197 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_multiuse(
198 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 1, [[X:%.*]]
199 ; CHECK-NEXT: [[ADD:%.*]] = shl nuw nsw i8 [[SH]], 1
200 ; CHECK-NEXT: ret i8 [[ADD]]
204 %add = add i8 %sh, %and
208 ; The mask is unnecessary.
210 define <2 x i8> @and1_lshr1_is_cmp_eq_0_vec(<2 x i8> %x) {
211 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_vec(
212 ; CHECK-NEXT: [[SH:%.*]] = lshr <2 x i8> <i8 1, i8 1>, [[X:%.*]]
213 ; CHECK-NEXT: ret <2 x i8> [[SH]]
215 %sh = lshr <2 x i8> <i8 1, i8 1>, %x
216 %and = and <2 x i8> %sh, <i8 1, i8 1>
220 define <2 x i8> @and1_lshr1_is_cmp_eq_0_vec_undef(<2 x i8> %x) {
221 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_vec_undef(
222 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer
223 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
224 ; CHECK-NEXT: ret <2 x i8> [[AND]]
226 %sh = lshr <2 x i8> <i8 1, i8 undef>, %x
227 %and = and <2 x i8> %sh, <i8 1, i8 undef>
231 ; 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.
232 define i32 @test11(i32 %a, i32 %b) {
233 ; CHECK-LABEL: @test11(
234 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
235 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
236 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
237 ; CHECK-NEXT: ret i32 [[W]]
242 %w = mul i32 %z, %x ; to keep the shift from being removed
246 ; 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.
247 define i32 @test12(i32 %a, i32 %b) {
248 ; CHECK-LABEL: @test12(
249 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
250 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
251 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
252 ; CHECK-NEXT: ret i32 [[W]]
257 %w = mul i32 %z, %x ; to keep the shift from being removed
261 ; 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.
262 define i32 @test13(i32 %a, i32 %b) {
263 ; CHECK-LABEL: @test13(
264 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
265 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
266 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
267 ; CHECK-NEXT: ret i32 [[W]]
272 %w = mul i32 %z, %x ; to keep the shift from being removed
276 ; 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.
277 define i32 @test14(i32 %a, i32 %b) {
278 ; CHECK-LABEL: @test14(
279 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
280 ; CHECK-NEXT: [[Y:%.*]] = sub i32 0, [[B:%.*]]
281 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 128
282 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
283 ; CHECK-NEXT: ret i32 [[W]]
288 %w = mul i32 %z, %x ; to keep the shift from being removed