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 i8 @llvm.ctpop.i8(i8)
6 declare i1 @llvm.ctpop.i1(i1)
7 declare <2 x i32> @llvm.ctpop.v2i32(<2 x i32>)
8 declare void @llvm.assume(i1)
10 define i1 @test1(i32 %arg) {
11 ; CHECK-LABEL: @test1(
12 ; CHECK-NEXT: ret i1 false
14 %and = and i32 %arg, 15
15 %cnt = call i32 @llvm.ctpop.i32(i32 %and)
16 %res = icmp eq i32 %cnt, 9
20 define i1 @test2(i32 %arg) {
21 ; CHECK-LABEL: @test2(
22 ; CHECK-NEXT: ret i1 false
24 %and = and i32 %arg, 1
25 %cnt = call i32 @llvm.ctpop.i32(i32 %and)
26 %res = icmp eq i32 %cnt, 2
30 define 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
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
46 ; Negative test for when we know nothing
47 define 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]]
53 %cnt = call i8 @llvm.ctpop.i8(i8 %arg)
54 %res = icmp eq i8 %cnt, 2
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.
60 define i1 @test5(i32 %arg) {
61 ; CHECK-LABEL: @test5(
62 ; CHECK-NEXT: ret i1 false
64 %and = and i32 %arg, 3
65 %cnt = call i32 @llvm.ctpop.i32(i32 %and)
66 %res = icmp eq i32 %cnt, 3
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
74 define <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]]
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>
87 ; Make sure we don't add range metadata to i1 ctpop.
88 define 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]]
93 %cnt = call i1 @llvm.ctpop.i1(i1 %arg)
97 ; CHECK: ![[$RANGE]] = !{i8 0, i8 9}