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: [[B:%.*]] = and i1 %X, %Y
7 ; CHECK-NEXT: ret i1 [[B]]
14 define i32 @test3(i32 %X, i32 %Y) {
15 ; CHECK-LABEL: @test3(
16 ; CHECK-NEXT: [[B:%.*]] = and i32 %X, %Y
17 ; CHECK-NEXT: ret i32 [[B]]
24 define i1 @test7(i32 %i, i1 %b) {
25 ; CHECK-LABEL: @test7(
26 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 %i, 0
27 ; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], %b
28 ; CHECK-NEXT: ret i1 [[TMP2]]
30 %cmp1 = icmp slt i32 %i, 1
31 %cmp2 = icmp sgt i32 %i, -1
32 %and1 = and i1 %cmp1, %b
33 %and2 = and i1 %and1, %cmp2
37 define i1 @test8(i32 %i) {
38 ; CHECK-LABEL: @test8(
39 ; CHECK-NEXT: [[I_OFF:%.*]] = add i32 %i, -1
40 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[I_OFF]], 13
41 ; CHECK-NEXT: ret i1 [[TMP1]]
43 %cmp1 = icmp ne i32 %i, 0
44 %cmp2 = icmp ult i32 %i, 14
45 %cond = and i1 %cmp1, %cmp2
49 ; FIXME: Vectors should fold too.
50 define <2 x i1> @test8vec(<2 x i32> %i) {
51 ; CHECK-LABEL: @test8vec(
52 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne <2 x i32> %i, zeroinitializer
53 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ult <2 x i32> %i, <i32 14, i32 14>
54 ; CHECK-NEXT: [[COND:%.*]] = and <2 x i1> [[CMP1]], [[CMP2]]
55 ; CHECK-NEXT: ret <2 x i1> [[COND]]
57 %cmp1 = icmp ne <2 x i32> %i, zeroinitializer
58 %cmp2 = icmp ult <2 x i32> %i, <i32 14, i32 14>
59 %cond = and <2 x i1> %cmp1, %cmp2
63 ; combine -x & 1 into x & 1
64 define i64 @test9(i64 %x) {
65 ; CHECK-LABEL: @test9(
66 ; CHECK-NEXT: [[AND:%.*]] = and i64 %x, 1
67 ; CHECK-NEXT: ret i64 [[AND]]
69 %sub = sub nsw i64 0, %x
70 %and = and i64 %sub, 1
74 ; combine -x & 1 into x & 1
75 define <2 x i64> @test9vec(<2 x i64> %x) {
76 ; CHECK-LABEL: @test9vec(
77 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i64> %x, <i64 1, i64 1>
78 ; CHECK-NEXT: ret <2 x i64> [[AND]]
80 %sub = sub nsw <2 x i64> <i64 0, i64 0>, %x
81 %and = and <2 x i64> %sub, <i64 1, i64 1>
85 define i64 @test10(i64 %x) {
86 ; CHECK-LABEL: @test10(
87 ; CHECK-NEXT: [[AND:%.*]] = and i64 %x, 1
88 ; CHECK-NEXT: [[ADD:%.*]] = sub i64 [[AND]], %x
89 ; CHECK-NEXT: ret i64 [[ADD]]
91 %sub = sub nsw i64 0, %x
92 %and = and i64 %sub, 1
93 %add = add i64 %sub, %and
97 ; (1 << x) & 1 --> zext(x == 0)
99 define i8 @and1_shl1_is_cmp_eq_0(i8 %x) {
100 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0(
101 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 %x, 0
102 ; CHECK-NEXT: [[AND:%.*]] = zext i1 [[TMP1]] to i8
103 ; CHECK-NEXT: ret i8 [[AND]]
110 ; Don't do it if the shift has another use.
112 define i8 @and1_shl1_is_cmp_eq_0_multiuse(i8 %x) {
113 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_multiuse(
114 ; CHECK-NEXT: [[SH:%.*]] = shl i8 1, %x
115 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[SH]], 1
116 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SH]], [[AND]]
117 ; CHECK-NEXT: ret i8 [[ADD]]
121 %add = add i8 %sh, %and
125 ; (1 << x) & 1 --> zext(x == 0)
127 define <2 x i8> @and1_shl1_is_cmp_eq_0_vec(<2 x i8> %x) {
128 ; CHECK-LABEL: @and1_shl1_is_cmp_eq_0_vec(
129 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> %x, zeroinitializer
130 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
131 ; CHECK-NEXT: ret <2 x i8> [[AND]]
133 %sh = shl <2 x i8> <i8 1, i8 1>, %x
134 %and = and <2 x i8> %sh, <i8 1, i8 1>
138 ; (1 >> x) & 1 --> zext(x == 0)
140 define i8 @and1_lshr1_is_cmp_eq_0(i8 %x) {
141 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0(
142 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 %x, 0
143 ; CHECK-NEXT: [[AND:%.*]] = zext i1 [[TMP1]] to i8
144 ; CHECK-NEXT: ret i8 [[AND]]
151 ; Don't do it if the shift has another use.
153 define i8 @and1_lshr1_is_cmp_eq_0_multiuse(i8 %x) {
154 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_multiuse(
155 ; CHECK-NEXT: [[SH:%.*]] = lshr i8 1, %x
156 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[SH]], 1
157 ; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i8 [[SH]], [[AND]]
158 ; CHECK-NEXT: ret i8 [[ADD]]
162 %add = add i8 %sh, %and
166 ; (1 >> x) & 1 --> zext(x == 0)
168 define <2 x i8> @and1_lshr1_is_cmp_eq_0_vec(<2 x i8> %x) {
169 ; CHECK-LABEL: @and1_lshr1_is_cmp_eq_0_vec(
170 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> %x, zeroinitializer
171 ; CHECK-NEXT: [[AND:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
172 ; CHECK-NEXT: ret <2 x i8> [[AND]]
174 %sh = lshr <2 x i8> <i8 1, i8 1>, %x
175 %and = and <2 x i8> %sh, <i8 1, i8 1>
179 ; 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.
180 define i32 @test11(i32 %a, i32 %b) {
181 ; CHECK-LABEL: @test11(
182 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
183 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
184 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
185 ; CHECK-NEXT: ret i32 [[W]]
190 %w = mul i32 %z, %x ; to keep the shift from being removed
194 ; 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.
195 define i32 @test12(i32 %a, i32 %b) {
196 ; CHECK-LABEL: @test12(
197 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
198 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
199 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
200 ; CHECK-NEXT: ret i32 [[W]]
205 %w = mul i32 %z, %x ; to keep the shift from being removed
209 ; 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.
210 define i32 @test13(i32 %a, i32 %b) {
211 ; CHECK-LABEL: @test13(
212 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
213 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128
214 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
215 ; CHECK-NEXT: ret i32 [[W]]
220 %w = mul i32 %z, %x ; to keep the shift from being removed
224 ; 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.
225 define i32 @test14(i32 %a, i32 %b) {
226 ; CHECK-LABEL: @test14(
227 ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8
228 ; CHECK-NEXT: [[Y:%.*]] = sub i32 0, [[B:%.*]]
229 ; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 128
230 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]]
231 ; CHECK-NEXT: ret i32 [[W]]
236 %w = mul i32 %z, %x ; to keep the shift from being removed