1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+popcnt | FileCheck %s -check-prefixes=CHECK,POPCOUNT
3 ; RUN: llc < %s -mtriple=x86_64-unknown -mattr=-popcnt | FileCheck %s -check-prefixes=CHECK,NO-POPCOUNT
5 declare i8 @llvm.ctpop.i8(i8) nounwind readnone
6 declare i64 @llvm.ctpop.i64(i64) nounwind readnone
8 define i32 @test1(i64 %x) nounwind readnone {
11 ; CHECK-NEXT: leaq -1(%rdi), %rcx
12 ; CHECK-NEXT: xorl %eax, %eax
13 ; CHECK-NEXT: testq %rcx, %rdi
14 ; CHECK-NEXT: setne %al
16 %count = tail call i64 @llvm.ctpop.i64(i64 %x)
17 %cast = trunc i64 %count to i32
18 %cmp = icmp ugt i32 %cast, 1
19 %conv = zext i1 %cmp to i32
24 define i32 @test2(i64 %x) nounwind readnone {
27 ; CHECK-NEXT: leaq -1(%rdi), %rcx
28 ; CHECK-NEXT: xorl %eax, %eax
29 ; CHECK-NEXT: testq %rcx, %rdi
30 ; CHECK-NEXT: sete %al
32 %count = tail call i64 @llvm.ctpop.i64(i64 %x)
33 %cmp = icmp ult i64 %count, 2
34 %conv = zext i1 %cmp to i32
38 define i32 @test3(i64 %x) nounwind readnone {
39 ; POPCOUNT-LABEL: test3:
41 ; POPCOUNT-NEXT: popcntq %rdi, %rcx
42 ; POPCOUNT-NEXT: andb $63, %cl
43 ; POPCOUNT-NEXT: xorl %eax, %eax
44 ; POPCOUNT-NEXT: cmpb $2, %cl
45 ; POPCOUNT-NEXT: setb %al
48 ; NO-POPCOUNT-LABEL: test3:
49 ; NO-POPCOUNT: # %bb.0:
50 ; NO-POPCOUNT-NEXT: movq %rdi, %rax
51 ; NO-POPCOUNT-NEXT: shrq %rax
52 ; NO-POPCOUNT-NEXT: movabsq $6148914691236517205, %rcx # imm = 0x5555555555555555
53 ; NO-POPCOUNT-NEXT: andq %rax, %rcx
54 ; NO-POPCOUNT-NEXT: subq %rcx, %rdi
55 ; NO-POPCOUNT-NEXT: movabsq $3689348814741910323, %rax # imm = 0x3333333333333333
56 ; NO-POPCOUNT-NEXT: movq %rdi, %rcx
57 ; NO-POPCOUNT-NEXT: andq %rax, %rcx
58 ; NO-POPCOUNT-NEXT: shrq $2, %rdi
59 ; NO-POPCOUNT-NEXT: andq %rax, %rdi
60 ; NO-POPCOUNT-NEXT: addq %rcx, %rdi
61 ; NO-POPCOUNT-NEXT: movq %rdi, %rax
62 ; NO-POPCOUNT-NEXT: shrq $4, %rax
63 ; NO-POPCOUNT-NEXT: addq %rdi, %rax
64 ; NO-POPCOUNT-NEXT: movabsq $1085102592571150095, %rcx # imm = 0xF0F0F0F0F0F0F0F
65 ; NO-POPCOUNT-NEXT: andq %rax, %rcx
66 ; NO-POPCOUNT-NEXT: movabsq $72340172838076673, %rdx # imm = 0x101010101010101
67 ; NO-POPCOUNT-NEXT: imulq %rcx, %rdx
68 ; NO-POPCOUNT-NEXT: shrq $56, %rdx
69 ; NO-POPCOUNT-NEXT: andb $63, %dl
70 ; NO-POPCOUNT-NEXT: xorl %eax, %eax
71 ; NO-POPCOUNT-NEXT: cmpb $2, %dl
72 ; NO-POPCOUNT-NEXT: setb %al
73 ; NO-POPCOUNT-NEXT: retq
74 %count = tail call i64 @llvm.ctpop.i64(i64 %x)
75 %cast = trunc i64 %count to i6 ; Too small for 0-64
76 %cmp = icmp ult i6 %cast, 2
77 %conv = zext i1 %cmp to i32
81 define i8 @test4(i8 %x) nounwind readnone {
82 ; POPCOUNT-LABEL: test4:
84 ; POPCOUNT-NEXT: andl $127, %edi
85 ; POPCOUNT-NEXT: popcntl %edi, %eax
86 ; POPCOUNT-NEXT: # kill: def $al killed $al killed $eax
89 ; NO-POPCOUNT-LABEL: test4:
90 ; NO-POPCOUNT: # %bb.0:
91 ; NO-POPCOUNT-NEXT: # kill: def $edi killed $edi def $rdi
92 ; NO-POPCOUNT-NEXT: andb $127, %dil
93 ; NO-POPCOUNT-NEXT: movl %edi, %eax
94 ; NO-POPCOUNT-NEXT: shrb %al
95 ; NO-POPCOUNT-NEXT: andb $21, %al
96 ; NO-POPCOUNT-NEXT: subb %al, %dil
97 ; NO-POPCOUNT-NEXT: movl %edi, %eax
98 ; NO-POPCOUNT-NEXT: andb $51, %al
99 ; NO-POPCOUNT-NEXT: shrb $2, %dil
100 ; NO-POPCOUNT-NEXT: andb $51, %dil
101 ; NO-POPCOUNT-NEXT: addb %al, %dil
102 ; NO-POPCOUNT-NEXT: movl %edi, %eax
103 ; NO-POPCOUNT-NEXT: shrb $4, %al
104 ; NO-POPCOUNT-NEXT: addl %edi, %eax
105 ; NO-POPCOUNT-NEXT: andb $15, %al
106 ; NO-POPCOUNT-NEXT: # kill: def $al killed $al killed $eax
107 ; NO-POPCOUNT-NEXT: retq
109 %count = tail call i8 @llvm.ctpop.i8(i8 %x2)
110 %and = and i8 %count, 7
114 define i32 @ctpop_eq_one(i64 %x) nounwind readnone {
115 ; POPCOUNT-LABEL: ctpop_eq_one:
117 ; POPCOUNT-NEXT: popcntq %rdi, %rcx
118 ; POPCOUNT-NEXT: xorl %eax, %eax
119 ; POPCOUNT-NEXT: cmpq $1, %rcx
120 ; POPCOUNT-NEXT: sete %al
121 ; POPCOUNT-NEXT: retq
123 ; NO-POPCOUNT-LABEL: ctpop_eq_one:
124 ; NO-POPCOUNT: # %bb.0:
125 ; NO-POPCOUNT-NEXT: leaq -1(%rdi), %rax
126 ; NO-POPCOUNT-NEXT: testq %rax, %rdi
127 ; NO-POPCOUNT-NEXT: sete %al
128 ; NO-POPCOUNT-NEXT: testq %rdi, %rdi
129 ; NO-POPCOUNT-NEXT: setne %cl
130 ; NO-POPCOUNT-NEXT: andb %al, %cl
131 ; NO-POPCOUNT-NEXT: movzbl %cl, %eax
132 ; NO-POPCOUNT-NEXT: retq
133 %count = tail call i64 @llvm.ctpop.i64(i64 %x)
134 %cmp = icmp eq i64 %count, 1
135 %conv = zext i1 %cmp to i32
139 define i32 @ctpop_ne_one(i64 %x) nounwind readnone {
140 ; POPCOUNT-LABEL: ctpop_ne_one:
142 ; POPCOUNT-NEXT: popcntq %rdi, %rcx
143 ; POPCOUNT-NEXT: xorl %eax, %eax
144 ; POPCOUNT-NEXT: cmpq $1, %rcx
145 ; POPCOUNT-NEXT: setne %al
146 ; POPCOUNT-NEXT: retq
148 ; NO-POPCOUNT-LABEL: ctpop_ne_one:
149 ; NO-POPCOUNT: # %bb.0:
150 ; NO-POPCOUNT-NEXT: leaq -1(%rdi), %rax
151 ; NO-POPCOUNT-NEXT: testq %rax, %rdi
152 ; NO-POPCOUNT-NEXT: setne %al
153 ; NO-POPCOUNT-NEXT: testq %rdi, %rdi
154 ; NO-POPCOUNT-NEXT: sete %cl
155 ; NO-POPCOUNT-NEXT: orb %al, %cl
156 ; NO-POPCOUNT-NEXT: movzbl %cl, %eax
157 ; NO-POPCOUNT-NEXT: retq
158 %count = tail call i64 @llvm.ctpop.i64(i64 %x)
159 %cmp = icmp ne i64 %count, 1
160 %conv = zext i1 %cmp to i32