1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 define i64 @test1(i64 %A, i32 %B) {
4 %tmp12 = zext i32 %B to i64
5 %tmp3 = shl i64 %tmp12, 32
6 %tmp5 = add i64 %tmp3, %A
7 %tmp6 = and i64 %tmp5, 123
10 ; CHECK-NEXT: and i64 %A, 123
14 define i32 @test2(i32 %A) {
19 ; CHECK-LABEL: @test2(
20 ; CHECK-NEXT: and i32 %A, 39
24 define i32 @test3(i32 %A) {
29 ; CHECK-LABEL: @test3(
32 ; CHECK-NEXT: or i32 %B, %C
36 define i32 @test4(i32 %A) {
37 %B = add nuw i32 %A, %A
39 ; CHECK-LABEL: @test4(
40 ; CHECK-NEXT: %B = shl nuw i32 %A, 1
41 ; CHECK-NEXT: ret i32 %B
44 define <2 x i1> @test5(<2 x i1> %A, <2 x i1> %B) {
45 %add = add <2 x i1> %A, %B
47 ; CHECK-LABEL: @test5(
48 ; CHECK-NEXT: %add = xor <2 x i1> %A, %B
49 ; CHECK-NEXT: ret <2 x i1> %add
52 define <2 x i64> @test6(<2 x i64> %A) {
53 %shl = shl <2 x i64> %A, <i64 2, i64 3>
54 %add = add <2 x i64> %shl, %A
56 ; CHECK-LABEL: @test6(
57 ; CHECK-NEXT: %add = mul <2 x i64> %A, <i64 5, i64 9>
58 ; CHECK-NEXT: ret <2 x i64> %add
61 define <2 x i64> @test7(<2 x i64> %A) {
62 %shl = shl <2 x i64> %A, <i64 2, i64 3>
63 %mul = mul <2 x i64> %A, <i64 3, i64 4>
64 %add = add <2 x i64> %shl, %mul
66 ; CHECK-LABEL: @test7(
67 ; CHECK-NEXT: %add = mul <2 x i64> %A, <i64 7, i64 12>
68 ; CHECK-NEXT: ret <2 x i64> %add
71 define <2 x i64> @test8(<2 x i64> %A) {
72 %xor = xor <2 x i64> %A, <i64 -1, i64 -1>
73 %add = add <2 x i64> %xor, <i64 2, i64 3>
75 ; CHECK-LABEL: @test8(
76 ; CHECK-NEXT: %add = sub <2 x i64> <i64 1, i64 2>, %A
77 ; CHECK-NEXT: ret <2 x i64> %add
80 define i16 @test9(i16 %a) {
82 %c = mul i16 %a, 32767
85 ; CHECK-LABEL: @test9(
86 ; CHECK-NEXT: %d = mul i16 %a, -32767
87 ; CHECK-NEXT: ret i16 %d
90 define i32 @test10(i32 %x) {
91 %x.not = or i32 %x, -1431655766
92 %neg = xor i32 %x.not, 1431655765
94 %add1 = add i32 %add, %neg
96 ; CHECK-LABEL: @test10(
97 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, -1431655766
98 ; CHECK-NEXT: ret i32 [[AND]]
101 define i32 @test11(i32 %x, i32 %y) {
102 %x.not = or i32 %x, -1431655766
103 %neg = xor i32 %x.not, 1431655765
105 %add1 = add i32 %add, %neg
107 ; CHECK-LABEL: @test11(
108 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 1431655765
109 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = sub i32 %y, [[AND]]
110 ; CHECK-NEXT: ret i32 [[SUB]]
113 define i32 @test12(i32 %x, i32 %y) {
114 %shr = ashr i32 %x, 3
115 %shr.not = or i32 %shr, -1431655766
116 %neg = xor i32 %shr.not, 1431655765
118 %add1 = add i32 %add, %neg
120 ; CHECK-LABEL: @test12(
121 ; CHECK-NEXT: [[SHR:%[a-z0-9]+]] = ashr i32 %x, 3
122 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHR]], 1431655765
123 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = sub i32 %y, [[AND]]
124 ; CHECK-NEXT: ret i32 [[SUB]]
127 define i32 @test13(i32 %x, i32 %y) {
128 %x.not = or i32 %x, -1431655767
129 %neg = xor i32 %x.not, 1431655766
131 %add1 = add i32 %add, %neg
133 ; CHECK-LABEL: @test13(
134 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %x, 1431655766
135 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = sub i32 %y, [[AND]]
136 ; CHECK-NEXT: ret i32 [[SUB]]
139 define i32 @test14(i32 %x, i32 %y) {
140 %shr = ashr i32 %x, 3
141 %shr.not = or i32 %shr, -1431655767
142 %neg = xor i32 %shr.not, 1431655766
144 %add1 = add i32 %add, %neg
146 ; CHECK-LABEL: @test14(
147 ; CHECK-NEXT: [[SHR:%[a-z0-9]+]] = ashr i32 %x, 3
148 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHR]], 1431655766
149 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = sub i32 %y, [[AND]]
150 ; CHECK-NEXT: ret i32 [[SUB]]
153 define i32 @test15(i32 %x, i32 %y) {
154 %x.not = and i32 %x, -1431655767
155 %neg = xor i32 %x.not, -1431655767
157 %add1 = add i32 %add, %neg
159 ; CHECK-LABEL: @test15(
160 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, 1431655766
161 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = sub i32 %y, [[OR]]
162 ; CHECK-NEXT: ret i32 [[SUB]]
165 define i32 @test16(i32 %x, i32 %y) {
166 %shr = ashr i32 %x, 3
167 %shr.not = and i32 %shr, -1431655767
168 %neg = xor i32 %shr.not, -1431655767
170 %add1 = add i32 %add, %neg
172 ; CHECK-LABEL: @test16(
173 ; CHECK-NEXT: [[SHR:%[a-z0-9]+]] = ashr i32 %x, 3
174 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 [[SHR]], 1431655766
175 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = sub i32 %y, [[OR]]
176 ; CHECK-NEXT: ret i32 [[SUB]]
179 define i16 @add_nsw_mul_nsw(i16 %x) {
180 %add1 = add nsw i16 %x, %x
181 %add2 = add nsw i16 %add1, %x
183 ; CHECK-LABEL: @add_nsw_mul_nsw(
184 ; CHECK-NEXT: %add2 = mul nsw i16 %x, 3
185 ; CHECK-NEXT: ret i16 %add2
188 define i16 @mul_add_to_mul_1(i16 %x) {
189 %mul1 = mul nsw i16 %x, 8
190 %add2 = add nsw i16 %x, %mul1
192 ; CHECK-LABEL: @mul_add_to_mul_1(
193 ; CHECK-NEXT: %add2 = mul nsw i16 %x, 9
194 ; CHECK-NEXT: ret i16 %add2
197 define i16 @mul_add_to_mul_2(i16 %x) {
198 %mul1 = mul nsw i16 %x, 8
199 %add2 = add nsw i16 %mul1, %x
201 ; CHECK-LABEL: @mul_add_to_mul_2(
202 ; CHECK-NEXT: %add2 = mul nsw i16 %x, 9
203 ; CHECK-NEXT: ret i16 %add2
206 define i16 @mul_add_to_mul_3(i16 %a) {
207 %mul1 = mul i16 %a, 2
208 %mul2 = mul i16 %a, 3
209 %add = add nsw i16 %mul1, %mul2
211 ; CHECK-LABEL: @mul_add_to_mul_3(
212 ; CHECK-NEXT: %add = mul i16 %a, 5
213 ; CHECK-NEXT: ret i16 %add
216 define i16 @mul_add_to_mul_4(i16 %a) {
217 %mul1 = mul nsw i16 %a, 2
218 %mul2 = mul nsw i16 %a, 7
219 %add = add nsw i16 %mul1, %mul2
221 ; CHECK-LABEL: @mul_add_to_mul_4(
222 ; CHECK-NEXT: %add = mul nsw i16 %a, 9
223 ; CHECK-NEXT: ret i16 %add
226 define i16 @mul_add_to_mul_5(i16 %a) {
227 %mul1 = mul nsw i16 %a, 3
228 %mul2 = mul nsw i16 %a, 7
229 %add = add nsw i16 %mul1, %mul2
231 ; CHECK-LABEL: @mul_add_to_mul_5(
232 ; CHECK-NEXT: %add = mul nsw i16 %a, 10
233 ; CHECK-NEXT: ret i16 %add
236 define i32 @mul_add_to_mul_6(i32 %x, i32 %y) {
237 %mul1 = mul nsw i32 %x, %y
238 %mul2 = mul nsw i32 %mul1, 5
239 %add = add nsw i32 %mul1, %mul2
241 ; CHECK-LABEL: @mul_add_to_mul_6(
242 ; CHECK-NEXT: %mul1 = mul nsw i32 %x, %y
243 ; CHECK-NEXT: %add = mul nsw i32 %mul1, 6
244 ; CHECK-NEXT: ret i32 %add
247 ; This test and the next test verify that when a range metadata is attached to
248 ; llvm.cttz, ValueTracking correctly intersects the range specified by the
249 ; metadata and the range implied by the intrinsic.
251 ; In this test, the range specified by the metadata is more strict. Therefore,
252 ; ValueTracking uses that range.
253 define i16 @add_cttz(i16 %a) {
254 ; CHECK-LABEL: @add_cttz(
255 ; llvm.cttz.i16(..., /*is_zero_undefined=*/true) implies the value returned
256 ; is in [0, 16). The range metadata indicates the value returned is in [0, 8).
257 ; Intersecting these ranges, we know the value returned is in [0, 8).
258 ; Therefore, InstCombine will transform
259 ; add %cttz, 1111 1111 1111 1000 ; decimal -8
261 ; or %cttz, 1111 1111 1111 1000
262 %cttz = call i16 @llvm.cttz.i16(i16 %a, i1 true), !range !0
263 %b = add i16 %cttz, -8
264 ; CHECK: or i16 %cttz, -8
267 declare i16 @llvm.cttz.i16(i16, i1)
268 !0 = metadata !{i16 0, i16 8}
270 ; Similar to @add_cttz, but in this test, the range implied by the
271 ; intrinsic is more strict. Therefore, ValueTracking uses that range.
272 define i16 @add_cttz_2(i16 %a) {
273 ; CHECK-LABEL: @add_cttz_2(
274 ; llvm.cttz.i16(..., /*is_zero_undefined=*/true) implies the value returned
275 ; is in [0, 16). The range metadata indicates the value returned is in
276 ; [0, 32). Intersecting these ranges, we know the value returned is in
277 ; [0, 16). Therefore, InstCombine will transform
278 ; add %cttz, 1111 1111 1111 0000 ; decimal -16
280 ; or %cttz, 1111 1111 1111 0000
281 %cttz = call i16 @llvm.cttz.i16(i16 %a, i1 true), !range !1
282 %b = add i16 %cttz, -16
283 ; CHECK: or i16 %cttz, -16
286 !1 = metadata !{i16 0, i16 32}