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 i8 @llvm.ctpop.i8(i8)
6declare i1 @llvm.ctpop.i1(i1)
7declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>)
8declare void @llvm.assume(i1)
9
10define i1 @test1(i32 %arg) {
11; CHECK-LABEL: @test1(
12; CHECK-NEXT:    ret i1 false
13;
14  %and = and i32 %arg, 15
15  %cnt = call i32 @llvm.ctpop.i32(i32 %and)
16  %res = icmp eq i32 %cnt, 9
17  ret i1 %res
18}
19
20define i1 @test2(i32 %arg) {
21; CHECK-LABEL: @test2(
22; CHECK-NEXT:    ret i1 false
23;
24  %and = and i32 %arg, 1
25  %cnt = call i32 @llvm.ctpop.i32(i32 %and)
26  %res = icmp eq i32 %cnt, 2
27  ret i1 %res
28}
29
30define i1 @test3(i32 %arg) {
31; CHECK-LABEL: @test3(
32; CHECK-NEXT:    [[ASSUME:%.*]] = icmp eq i32 [[ARG:%.*]], 0
33; CHECK-NEXT:    call void @llvm.assume(i1 [[ASSUME]])
34; CHECK-NEXT:    ret i1 false
35;
36  ;; Use an assume to make all the bits known without triggering constant
37  ;; folding.  This is trying to hit a corner case where we have to avoid
38  ;; taking the log of 0.
39  %assume = icmp eq i32 %arg, 0
40  call void @llvm.assume(i1 %assume)
41  %cnt = call i32 @llvm.ctpop.i32(i32 %arg)
42  %res = icmp eq i32 %cnt, 2
43  ret i1 %res
44}
45
46; Negative test for when we know nothing
47define i1 @test4(i8 %arg) {
48; CHECK-LABEL: @test4(
49; CHECK-NEXT:    [[CNT:%.*]] = call i8 @llvm.ctpop.i8(i8 [[ARG:%.*]]), !range ![[$RANGE:[0-9]+]]
50; CHECK-NEXT:    [[RES:%.*]] = icmp eq i8 [[CNT]], 2
51; CHECK-NEXT:    ret i1 [[RES]]
52;
53  %cnt = call i8 @llvm.ctpop.i8(i8 %arg)
54  %res = icmp eq i8 %cnt, 2
55  ret i1 %res
56}
57
58; Test when the number of possible known bits isn't one less than a power of 2
59; and the compare value is greater but less than the next power of 2.
60define i1 @test5(i32 %arg) {
61; CHECK-LABEL: @test5(
62; CHECK-NEXT:    ret i1 false
63;
64  %and = and i32 %arg, 3
65  %cnt = call i32 @llvm.ctpop.i32(i32 %and)
66  %res = icmp eq i32 %cnt, 3
67  ret i1 %res
68}
69
70; Test when the number of possible known bits isn't one less than a power of 2
71; and the compare value is greater but less than the next power of 2.
72; TODO: The icmp is unnecessary given the known bits of the input, but range
73; metadata doesn't support vectors
74define <2 x i1> @test5vec(<2 x i32> %arg) {
75; CHECK-LABEL: @test5vec(
76; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> [[ARG:%.*]], <i32 3, i32 3>
77; CHECK-NEXT:    [[CNT:%.*]] = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> [[AND]])
78; CHECK-NEXT:    [[RES:%.*]] = icmp eq <2 x i32> [[CNT]], <i32 3, i32 3>
79; CHECK-NEXT:    ret <2 x i1> [[RES]]
80;
81  %and = and <2 x i32> %arg, <i32 3, i32 3>
82  %cnt = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %and)
83  %res = icmp eq <2 x i32> %cnt, <i32 3, i32 3>
84  ret <2 x i1> %res
85}
86
87; Make sure we don't add range metadata to i1 ctpop.
88define i1 @test6(i1 %arg) {
89; CHECK-LABEL: @test6(
90; CHECK-NEXT:    [[CNT:%.*]] = call i1 @llvm.ctpop.i1(i1 [[ARG:%.*]])
91; CHECK-NEXT:    ret i1 [[CNT]]
92;
93  %cnt = call i1 @llvm.ctpop.i1(i1 %arg)
94  ret i1 %cnt
95}
96
97; CHECK: ![[$RANGE]] = !{i8 0, i8 9}
98