120
|
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 define i1 @test2(i1 %X, i1 %Y) {
|
120
|
5 ; CHECK-LABEL: @test2(
|
|
6 ; CHECK-NEXT: [[B:%.*]] = and i1 %X, %Y
|
|
7 ; CHECK-NEXT: ret i1 [[B]]
|
|
8 ;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 %a = and i1 %X, %Y
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 %b = and i1 %a, %X
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 ret i1 %b
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 define i32 @test3(i32 %X, i32 %Y) {
|
120
|
15 ; CHECK-LABEL: @test3(
|
|
16 ; CHECK-NEXT: [[B:%.*]] = and i32 %X, %Y
|
|
17 ; CHECK-NEXT: ret i32 [[B]]
|
|
18 ;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 %a = and i32 %X, %Y
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 %b = and i32 %Y, %a
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 ret i32 %b
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23
|
77
|
24 define i1 @test7(i32 %i, i1 %b) {
|
|
25 ; CHECK-LABEL: @test7(
|
120
|
26 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 %i, 0
|
|
27 ; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], %b
|
|
28 ; CHECK-NEXT: ret i1 [[TMP2]]
|
|
29 ;
|
77
|
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
|
|
34 ret i1 %and2
|
|
35 }
|
83
|
36
|
|
37 define i1 @test8(i32 %i) {
|
|
38 ; CHECK-LABEL: @test8(
|
120
|
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]]
|
|
42 ;
|
83
|
43 %cmp1 = icmp ne i32 %i, 0
|
|
44 %cmp2 = icmp ult i32 %i, 14
|
|
45 %cond = and i1 %cmp1, %cmp2
|
|
46 ret i1 %cond
|
|
47 }
|
95
|
48
|
120
|
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]]
|
|
56 ;
|
|
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
|
|
60 ret <2 x i1> %cond
|
|
61 }
|
|
62
|
95
|
63 ; combine -x & 1 into x & 1
|
|
64 define i64 @test9(i64 %x) {
|
|
65 ; CHECK-LABEL: @test9(
|
120
|
66 ; CHECK-NEXT: [[AND:%.*]] = and i64 %x, 1
|
|
67 ; CHECK-NEXT: ret i64 [[AND]]
|
|
68 ;
|
95
|
69 %sub = sub nsw i64 0, %x
|
|
70 %and = and i64 %sub, 1
|
|
71 ret i64 %and
|
|
72 }
|
|
73
|
121
|
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]]
|
|
79 ;
|
|
80 %sub = sub nsw <2 x i64> <i64 0, i64 0>, %x
|
|
81 %and = and <2 x i64> %sub, <i64 1, i64 1>
|
|
82 ret <2 x i64> %and
|
|
83 }
|
|
84
|
95
|
85 define i64 @test10(i64 %x) {
|
|
86 ; CHECK-LABEL: @test10(
|
120
|
87 ; CHECK-NEXT: [[AND:%.*]] = and i64 %x, 1
|
|
88 ; CHECK-NEXT: [[ADD:%.*]] = sub i64 [[AND]], %x
|
|
89 ; CHECK-NEXT: ret i64 [[ADD]]
|
|
90 ;
|
95
|
91 %sub = sub nsw i64 0, %x
|
|
92 %and = and i64 %sub, 1
|
|
93 %add = add i64 %sub, %and
|
|
94 ret i64 %add
|
|
95 }
|
|
96
|
121
|
97 ; (1 << x) & 1 --> zext(x == 0)
|
|
98
|
|
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]]
|
|
104 ;
|
|
105 %sh = shl i8 1, %x
|
|
106 %and = and i8 %sh, 1
|
|
107 ret i8 %and
|
|
108 }
|
|
109
|
|
110 ; Don't do it if the shift has another use.
|
|
111
|
|
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]]
|
|
118 ;
|
|
119 %sh = shl i8 1, %x
|
|
120 %and = and i8 %sh, 1
|
|
121 %add = add i8 %sh, %and
|
|
122 ret i8 %add
|
|
123 }
|
|
124
|
|
125 ; (1 << x) & 1 --> zext(x == 0)
|
|
126
|
|
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]]
|
|
132 ;
|
|
133 %sh = shl <2 x i8> <i8 1, i8 1>, %x
|
|
134 %and = and <2 x i8> %sh, <i8 1, i8 1>
|
|
135 ret <2 x i8> %and
|
|
136 }
|
|
137
|
|
138 ; (1 >> x) & 1 --> zext(x == 0)
|
|
139
|
|
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]]
|
|
145 ;
|
|
146 %sh = lshr i8 1, %x
|
|
147 %and = and i8 %sh, 1
|
|
148 ret i8 %and
|
|
149 }
|
|
150
|
|
151 ; Don't do it if the shift has another use.
|
|
152
|
|
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
|
147
|
157 ; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i8 [[SH]], [[AND]]
|
121
|
158 ; CHECK-NEXT: ret i8 [[ADD]]
|
|
159 ;
|
|
160 %sh = lshr i8 1, %x
|
|
161 %and = and i8 %sh, 1
|
|
162 %add = add i8 %sh, %and
|
|
163 ret i8 %add
|
|
164 }
|
|
165
|
|
166 ; (1 >> x) & 1 --> zext(x == 0)
|
|
167
|
|
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]]
|
|
173 ;
|
|
174 %sh = lshr <2 x i8> <i8 1, i8 1>, %x
|
|
175 %and = and <2 x i8> %sh, <i8 1, i8 1>
|
|
176 ret <2 x i8> %and
|
|
177 }
|
|
178
|
|
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]]
|
|
186 ;
|
|
187 %x = shl i32 %a, 8
|
|
188 %y = add i32 %x, %b
|
|
189 %z = and i32 %y, 128
|
|
190 %w = mul i32 %z, %x ; to keep the shift from being removed
|
|
191 ret i32 %w
|
|
192 }
|
|
193
|
|
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]]
|
|
201 ;
|
|
202 %x = shl i32 %a, 8
|
|
203 %y = add i32 %b, %x
|
|
204 %z = and i32 %y, 128
|
|
205 %w = mul i32 %z, %x ; to keep the shift from being removed
|
|
206 ret i32 %w
|
|
207 }
|
|
208
|
|
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]]
|
|
216 ;
|
|
217 %x = shl i32 %a, 8
|
|
218 %y = sub i32 %b, %x
|
|
219 %z = and i32 %y, 128
|
|
220 %w = mul i32 %z, %x ; to keep the shift from being removed
|
|
221 ret i32 %w
|
|
222 }
|
|
223
|
|
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]]
|
|
232 ;
|
|
233 %x = shl i32 %a, 8
|
|
234 %y = sub i32 %x, %b
|
|
235 %z = and i32 %y, 128
|
|
236 %w = mul i32 %z, %x ; to keep the shift from being removed
|
|
237 ret i32 %w
|
|
238 }
|