1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -S -passes=instcombine | FileCheck %s
4 declare i32 @llvm.cttz.i32(i32, i1)
5 declare i32 @llvm.ctlz.i32(i32, i1)
6 declare <2 x i64> @llvm.cttz.v2i64(<2 x i64>, i1)
9 define i32 @cttz_zext_zero_undef(i16 %x) {
10 ; CHECK-LABEL: @cttz_zext_zero_undef(
11 ; CHECK-NEXT: [[TMP1:%.*]] = call range(i16 0, 17) i16 @llvm.cttz.i16(i16 [[X:%.*]], i1 true)
12 ; CHECK-NEXT: [[TZ:%.*]] = zext nneg i16 [[TMP1]] to i32
13 ; CHECK-NEXT: ret i32 [[TZ]]
15 %z = zext i16 %x to i32
16 %tz = call i32 @llvm.cttz.i32(i32 %z, i1 true)
20 define i32 @cttz_zext_zero_def(i16 %x) {
21 ; CHECK-LABEL: @cttz_zext_zero_def(
22 ; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32
23 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[Z]], i1 false)
24 ; CHECK-NEXT: ret i32 [[TZ]]
26 %z = zext i16 %x to i32
27 %tz = call i32 @llvm.cttz.i32(i32 %z, i1 false)
31 define i32 @cttz_zext_zero_undef_extra_use(i16 %x) {
32 ; CHECK-LABEL: @cttz_zext_zero_undef_extra_use(
33 ; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32
34 ; CHECK-NEXT: call void @use(i32 [[Z]])
35 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[Z]], i1 true)
36 ; CHECK-NEXT: ret i32 [[TZ]]
38 %z = zext i16 %x to i32
39 call void @use(i32 %z)
40 %tz = call i32 @llvm.cttz.i32(i32 %z, i1 true)
44 define <2 x i64> @cttz_zext_zero_undef_vec(<2 x i32> %x) {
45 ; CHECK-LABEL: @cttz_zext_zero_undef_vec(
46 ; CHECK-NEXT: [[TMP1:%.*]] = call range(i32 0, 33) <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[X:%.*]], i1 true)
47 ; CHECK-NEXT: [[TZ:%.*]] = zext nneg <2 x i32> [[TMP1]] to <2 x i64>
48 ; CHECK-NEXT: ret <2 x i64> [[TZ]]
50 %z = zext <2 x i32> %x to <2 x i64>
51 %tz = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %z, i1 true)
55 define <2 x i64> @cttz_zext_zero_def_vec(<2 x i32> %x) {
56 ; CHECK-LABEL: @cttz_zext_zero_def_vec(
57 ; CHECK-NEXT: [[Z:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64>
58 ; CHECK-NEXT: [[TZ:%.*]] = tail call range(i64 0, 65) <2 x i64> @llvm.cttz.v2i64(<2 x i64> [[Z]], i1 false)
59 ; CHECK-NEXT: ret <2 x i64> [[TZ]]
61 %z = zext <2 x i32> %x to <2 x i64>
62 %tz = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %z, i1 false)
66 define i32 @cttz_sext_zero_undef(i16 %x) {
67 ; CHECK-LABEL: @cttz_sext_zero_undef(
68 ; CHECK-NEXT: [[TMP1:%.*]] = call range(i16 0, 17) i16 @llvm.cttz.i16(i16 [[X:%.*]], i1 true)
69 ; CHECK-NEXT: [[TZ:%.*]] = zext nneg i16 [[TMP1]] to i32
70 ; CHECK-NEXT: ret i32 [[TZ]]
72 %s = sext i16 %x to i32
73 %tz = call i32 @llvm.cttz.i32(i32 %s, i1 true)
77 define i32 @cttz_sext_zero_def(i16 %x) {
78 ; CHECK-LABEL: @cttz_sext_zero_def(
79 ; CHECK-NEXT: [[TMP1:%.*]] = zext i16 [[X:%.*]] to i32
80 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[TMP1]], i1 false)
81 ; CHECK-NEXT: ret i32 [[TZ]]
83 %s = sext i16 %x to i32
84 %tz = call i32 @llvm.cttz.i32(i32 %s, i1 false)
88 define i32 @cttz_sext_zero_undef_extra_use(i16 %x) {
89 ; CHECK-LABEL: @cttz_sext_zero_undef_extra_use(
90 ; CHECK-NEXT: [[S:%.*]] = sext i16 [[X:%.*]] to i32
91 ; CHECK-NEXT: call void @use(i32 [[S]])
92 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[S]], i1 true)
93 ; CHECK-NEXT: ret i32 [[TZ]]
95 %s = sext i16 %x to i32
96 call void @use(i32 %s)
97 %tz = call i32 @llvm.cttz.i32(i32 %s, i1 true)
101 define <2 x i64> @cttz_sext_zero_undef_vec(<2 x i32> %x) {
102 ; CHECK-LABEL: @cttz_sext_zero_undef_vec(
103 ; CHECK-NEXT: [[TMP1:%.*]] = call range(i32 0, 33) <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[X:%.*]], i1 true)
104 ; CHECK-NEXT: [[TZ:%.*]] = zext nneg <2 x i32> [[TMP1]] to <2 x i64>
105 ; CHECK-NEXT: ret <2 x i64> [[TZ]]
107 %s = sext <2 x i32> %x to <2 x i64>
108 %tz = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %s, i1 true)
112 define <2 x i64> @cttz_sext_zero_def_vec(<2 x i32> %x) {
113 ; CHECK-LABEL: @cttz_sext_zero_def_vec(
114 ; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64>
115 ; CHECK-NEXT: [[TZ:%.*]] = call range(i64 0, 65) <2 x i64> @llvm.cttz.v2i64(<2 x i64> [[TMP1]], i1 false)
116 ; CHECK-NEXT: ret <2 x i64> [[TZ]]
118 %s = sext <2 x i32> %x to <2 x i64>
119 %tz = tail call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %s, i1 false)
123 define i32 @cttz_of_lowest_set_bit(i32 %x) {
124 ; CHECK-LABEL: @cttz_of_lowest_set_bit(
125 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false)
126 ; CHECK-NEXT: ret i32 [[TZ]]
129 %and = and i32 %sub, %x
130 %tz = call i32 @llvm.cttz.i32(i32 %and, i1 false)
134 define i32 @cttz_of_lowest_set_bit_commuted(i32 %xx) {
135 ; CHECK-LABEL: @cttz_of_lowest_set_bit_commuted(
136 ; CHECK-NEXT: [[X:%.*]] = udiv i32 42, [[XX:%.*]]
137 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X]], i1 false)
138 ; CHECK-NEXT: ret i32 [[TZ]]
140 %x = udiv i32 42, %xx ; thwart complexity-based canonicalization
142 %and = and i32 %x, %sub
143 %tz = call i32 @llvm.cttz.i32(i32 %and, i1 false)
147 define i32 @cttz_of_lowest_set_bit_poison_flag(i32 %x) {
148 ; CHECK-LABEL: @cttz_of_lowest_set_bit_poison_flag(
149 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
150 ; CHECK-NEXT: ret i32 [[TZ]]
153 %and = and i32 %sub, %x
154 %tz = call i32 @llvm.cttz.i32(i32 %and, i1 true)
158 define <2 x i64> @cttz_of_lowest_set_bit_vec(<2 x i64> %x) {
159 ; CHECK-LABEL: @cttz_of_lowest_set_bit_vec(
160 ; CHECK-NEXT: [[TZ:%.*]] = call range(i64 0, 65) <2 x i64> @llvm.cttz.v2i64(<2 x i64> [[X:%.*]], i1 false)
161 ; CHECK-NEXT: ret <2 x i64> [[TZ]]
163 %sub = sub <2 x i64> zeroinitializer, %x
164 %and = and <2 x i64> %sub, %x
165 %tz = call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %and, i1 false)
169 define <2 x i64> @cttz_of_lowest_set_bit_vec_undef(<2 x i64> %x) {
170 ; CHECK-LABEL: @cttz_of_lowest_set_bit_vec_undef(
171 ; CHECK-NEXT: [[TZ:%.*]] = call range(i64 0, 65) <2 x i64> @llvm.cttz.v2i64(<2 x i64> [[X:%.*]], i1 false)
172 ; CHECK-NEXT: ret <2 x i64> [[TZ]]
174 %sub = sub <2 x i64> zeroinitializer, %x
175 %and = and <2 x i64> %sub, %x
176 %tz = call <2 x i64> @llvm.cttz.v2i64(<2 x i64> %and, i1 false)
180 define i32 @cttz_of_lowest_set_bit_wrong_const(i32 %x) {
181 ; CHECK-LABEL: @cttz_of_lowest_set_bit_wrong_const(
182 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 1, [[X:%.*]]
183 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[SUB]], [[X]]
184 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 1, 33) i32 @llvm.cttz.i32(i32 [[AND]], i1 false)
185 ; CHECK-NEXT: ret i32 [[TZ]]
188 %and = and i32 %sub, %x
189 %tz = call i32 @llvm.cttz.i32(i32 %and, i1 false)
193 define i32 @cttz_of_lowest_set_bit_wrong_operand(i32 %x, i32 %y) {
194 ; CHECK-LABEL: @cttz_of_lowest_set_bit_wrong_operand(
195 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 0, [[Y:%.*]]
196 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], [[SUB]]
197 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[AND]], i1 false)
198 ; CHECK-NEXT: ret i32 [[TZ]]
201 %and = and i32 %sub, %x
202 %tz = call i32 @llvm.cttz.i32(i32 %and, i1 false)
206 define i32 @cttz_of_lowest_set_bit_wrong_intrinsic(i32 %x) {
207 ; CHECK-LABEL: @cttz_of_lowest_set_bit_wrong_intrinsic(
208 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 0, [[X:%.*]]
209 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X]], [[SUB]]
210 ; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[AND]], i1 false)
211 ; CHECK-NEXT: ret i32 [[TZ]]
214 %and = and i32 %sub, %x
215 %tz = call i32 @llvm.ctlz.i32(i32 %and, i1 false)
219 define i32 @cttz_of_power_of_two(i32 %x) {
220 ; CHECK-LABEL: @cttz_of_power_of_two(
221 ; CHECK-NEXT: [[R:%.*]] = sub i32 32, [[X:%.*]]
222 ; CHECK-NEXT: ret i32 [[R]]
224 %lshr = lshr i32 -1, %x
225 %add = add i32 %lshr, 1
226 %r = call i32 @llvm.cttz.i32(i32 %add, i1 false)
230 define i32 @cttz_of_power_of_two_zero_poison(i32 %x) {
231 ; CHECK-LABEL: @cttz_of_power_of_two_zero_poison(
232 ; CHECK-NEXT: [[R:%.*]] = sub i32 32, [[X:%.*]]
233 ; CHECK-NEXT: ret i32 [[R]]
235 %lshr = lshr i32 -1, %x
236 %add = add i32 %lshr, 1
237 %r = call i32 @llvm.cttz.i32(i32 %add, i1 true)
241 define i32 @cttz_of_power_of_two_wrong_intrinsic(i32 %x) {
242 ; CHECK-LABEL: @cttz_of_power_of_two_wrong_intrinsic(
243 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 -1, [[X:%.*]]
244 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[LSHR]], 1
245 ; CHECK-NEXT: [[R:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[ADD]], i1 false)
246 ; CHECK-NEXT: ret i32 [[R]]
248 %lshr = lshr i32 -1, %x
249 %add = add i32 %lshr, 1
250 %r = call i32 @llvm.ctlz.i32(i32 %add, i1 false)
254 define i32 @cttz_of_power_of_two_wrong_constant_1(i32 %x) {
255 ; CHECK-LABEL: @cttz_of_power_of_two_wrong_constant_1(
256 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 -2, [[X:%.*]]
257 ; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[LSHR]], 1
258 ; CHECK-NEXT: [[R:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[ADD]], i1 true)
259 ; CHECK-NEXT: ret i32 [[R]]
261 %lshr = lshr i32 -2, %x
262 %add = add i32 %lshr, 1
263 %r = call i32 @llvm.cttz.i32(i32 %add, i1 false)
267 define i32 @cttz_of_power_of_two_wrong_constant_2(i32 %x) {
268 ; CHECK-LABEL: @cttz_of_power_of_two_wrong_constant_2(
269 ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 -1, [[X:%.*]]
270 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[LSHR]], -1
271 ; CHECK-NEXT: [[R:%.*]] = call range(i32 1, 33) i32 @llvm.cttz.i32(i32 [[ADD]], i1 false)
272 ; CHECK-NEXT: ret i32 [[R]]
274 %lshr = lshr i32 -1, %x
275 %add = add i32 %lshr, -1
276 %r = call i32 @llvm.cttz.i32(i32 %add, i1 false)
280 define i16 @cttz_assume(i16 %x) {
281 ; CHECK-LABEL: @cttz_assume(
282 ; CHECK-NEXT: [[ADD:%.*]] = add i16 [[X:%.*]], 1
283 ; CHECK-NEXT: [[COND0:%.*]] = icmp ult i16 [[ADD]], 10
284 ; CHECK-NEXT: call void @llvm.assume(i1 [[COND0]])
285 ; CHECK-NEXT: [[COND1:%.*]] = icmp ne i16 [[X]], 0
286 ; CHECK-NEXT: call void @llvm.assume(i1 [[COND1]])
287 ; CHECK-NEXT: [[CTTZ:%.*]] = call range(i16 0, 17) i16 @llvm.cttz.i16(i16 [[X]], i1 true)
288 ; CHECK-NEXT: ret i16 [[CTTZ]]
291 %cond0 = icmp ult i16 %add, 10
292 call void @llvm.assume(i1 %cond0)
294 %cond1 = icmp ne i16 %x, 0
295 call void @llvm.assume(i1 %cond1)
297 %cttz = call i16 @llvm.cttz.i16(i16 %x, i1 false)