1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -S -instcombine | FileCheck %s
4 declare i32 @llvm.ctpop.i32(i32)
5 declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>)
8 ; __builtin_popcount(i | -i) -> 32 - __builtin_cttz(i, false)
9 define i32 @ctpop1(i32 %0) {
10 ; CHECK-LABEL: @ctpop1(
11 ; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0:%.*]], i1 false), !range [[RNG0:![0-9]+]]
12 ; CHECK-NEXT: ret i32 [[TMP2]]
16 %4 = tail call i32 @llvm.ctpop.i32(i32 %3)
21 define <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]]
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)
33 define 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:%.*]] = xor i32 [[TMP3]], -1
38 ; CHECK-NEXT: [[TMP5:%.*]] = call i32 @llvm.ctpop.i32(i32 [[TMP4]]), !range [[RNG0]]
39 ; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP5]], [[TMP3]]
40 ; CHECK-NEXT: ret i32 [[TMP6]]
44 %4 = tail call i32 @llvm.ctpop.i32(i32 %3)
51 ; __builtin_popcount(~i & (i-1)) -> __builtin_cttz(i, false)
52 define i32 @ctpop2(i32 %0) {
53 ; CHECK-LABEL: @ctpop2(
54 ; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.cttz.i32(i32 [[TMP0:%.*]], i1 false), !range [[RNG0]]
55 ; CHECK-NEXT: ret i32 [[TMP2]]
60 %5 = tail call i32 @llvm.ctpop.i32(i32 %4)
64 define <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]]
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)
76 define 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 [[RNG0]]
82 ; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP5]], [[TMP4]]
83 ; CHECK-NEXT: ret i32 [[TMP6]]
88 %5 = tail call i32 @llvm.ctpop.i32(i32 %4)