1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -S -instcombine | FileCheck %s
3
4declare i32 @llvm.ctpop.i32(i32)
5declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>)
6
7; PR43513
8; __builtin_popcount(i | -i) -> 32 - __builtin_cttz(i, false)
9define i32 @ctpop1(i32 %0) {
10; CHECK-LABEL: @ctpop1(
11; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0:%.*]], i1 false), !range !0
12; CHECK-NEXT:    ret i32 [[TMP2]]
13;
14  %2 = sub i32 0, %0
15  %3 = or i32 %0, %2
16  %4 = tail call i32 @llvm.ctpop.i32(i32 %3)
17  %5 = sub i32 32, %4
18  ret i32 %5
19}
20
21define <2 x i32> @ctpop1v(<2 x i32> %0) {
22; CHECK-LABEL: @ctpop1v(
23; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[TMP0:%.*]], i1 false)
24; CHECK-NEXT:    [[TMP3:%.*]] = sub nuw nsw <2 x i32> <i32 32, i32 32>, [[TMP2]]
25; CHECK-NEXT:    ret <2 x i32> [[TMP3]]
26;
27  %2 = sub <2 x i32> zeroinitializer, %0
28  %3 = or <2 x i32> %2, %0
29  %4 = tail call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %3)
30  ret <2 x i32> %4
31}
32
33define i32 @ctpop1_multiuse(i32 %0) {
34; CHECK-LABEL: @ctpop1_multiuse(
35; CHECK-NEXT:    [[TMP2:%.*]] = sub i32 0, [[TMP0:%.*]]
36; CHECK-NEXT:    [[TMP3:%.*]] = or i32 [[TMP2]], [[TMP0]]
37; CHECK-NEXT:    [[TMP4:%.*]] = tail call i32 @llvm.ctpop.i32(i32 [[TMP3]]), !range !0
38; CHECK-NEXT:    [[TMP5:%.*]] = sub nuw nsw i32 32, [[TMP4]]
39; CHECK-NEXT:    [[TMP6:%.*]] = add i32 [[TMP5]], [[TMP3]]
40; CHECK-NEXT:    ret i32 [[TMP6]]
41;
42  %2 = sub i32 0, %0
43  %3 = or i32 %0, %2
44  %4 = tail call i32 @llvm.ctpop.i32(i32 %3)
45  %5 = sub i32 32, %4
46  %6 = add i32 %5, %3
47  ret i32 %6
48}
49
50; PR43513
51; __builtin_popcount(~i & (i-1)) -> __builtin_cttz(i, false)
52define i32 @ctpop2(i32 %0) {
53; CHECK-LABEL: @ctpop2(
54; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0:%.*]], i1 false), !range !0
55; CHECK-NEXT:    ret i32 [[TMP2]]
56;
57  %2 = xor i32 %0, -1
58  %3 = sub i32 %0, 1
59  %4 = and i32 %3, %2
60  %5 = tail call i32 @llvm.ctpop.i32(i32 %4)
61  ret i32 %5
62}
63
64define <2 x i32> @ctpop2v(<2 x i32> %0) {
65; CHECK-LABEL: @ctpop2v(
66; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x i32> @llvm.cttz.v2i32(<2 x i32> [[TMP0:%.*]], i1 false)
67; CHECK-NEXT:    ret <2 x i32> [[TMP2]]
68;
69  %2 = xor <2 x i32> %0, <i32 -1, i32 -1>
70  %3 = add <2 x i32> %0, <i32 -1, i32 -1>
71  %4 = and <2 x i32> %2, %3
72  %5 = tail call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %4)
73  ret <2 x i32> %5
74}
75
76define i32 @ctpop2_multiuse(i32 %0) {
77; CHECK-LABEL: @ctpop2_multiuse(
78; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP0:%.*]], -1
79; CHECK-NEXT:    [[TMP3:%.*]] = add i32 [[TMP0]], -1
80; CHECK-NEXT:    [[TMP4:%.*]] = and i32 [[TMP3]], [[TMP2]]
81; CHECK-NEXT:    [[TMP5:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0]], i1 false), !range !0
82; CHECK-NEXT:    [[TMP6:%.*]] = add i32 [[TMP5]], [[TMP4]]
83; CHECK-NEXT:    ret i32 [[TMP6]]
84;
85  %2 = xor i32 %0, -1
86  %3 = sub i32 %0, 1
87  %4 = and i32 %3, %2
88  %5 = tail call i32 @llvm.ctpop.i32(i32 %4)
89  %6 = add i32 %5, %4
90  ret i32 %6
91}
92