1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2; RUN: llc < %s -mtriple=i686-unknown | FileCheck %s --check-prefix=X32
3; RUN: llc < %s -mtriple=x86_64-unknown | FileCheck %s --check-prefix=X64
4; RUN: llc < %s -mtriple=i686-unknown -mattr=+popcnt | FileCheck %s --check-prefix=X32-POPCNT
5; RUN: llc < %s -mtriple=x86_64-unknown -mattr=+popcnt | FileCheck %s --check-prefix=X64-POPCNT
6
7define i8 @cnt8(i8 %x) nounwind readnone {
8; X32-LABEL: cnt8:
9; X32:       # BB#0:
10; X32-NEXT:    movb {{[0-9]+}}(%esp), %cl
11; X32-NEXT:    movl %ecx, %eax
12; X32-NEXT:    shrb %al
13; X32-NEXT:    andb $85, %al
14; X32-NEXT:    subb %al, %cl
15; X32-NEXT:    movl %ecx, %eax
16; X32-NEXT:    andb $51, %al
17; X32-NEXT:    shrb $2, %cl
18; X32-NEXT:    andb $51, %cl
19; X32-NEXT:    addb %al, %cl
20; X32-NEXT:    movl %ecx, %eax
21; X32-NEXT:    shrb $4, %al
22; X32-NEXT:    addb %cl, %al
23; X32-NEXT:    andb $15, %al
24; X32-NEXT:    retl
25;
26; X64-LABEL: cnt8:
27; X64:       # BB#0:
28; X64-NEXT:    movl %edi, %eax
29; X64-NEXT:    shrb %al
30; X64-NEXT:    andb $85, %al
31; X64-NEXT:    subb %al, %dil
32; X64-NEXT:    movl %edi, %eax
33; X64-NEXT:    andb $51, %al
34; X64-NEXT:    shrb $2, %dil
35; X64-NEXT:    andb $51, %dil
36; X64-NEXT:    addb %al, %dil
37; X64-NEXT:    movl %edi, %eax
38; X64-NEXT:    shrb $4, %al
39; X64-NEXT:    addb %dil, %al
40; X64-NEXT:    andb $15, %al
41; X64-NEXT:    retq
42;
43; X32-POPCNT-LABEL: cnt8:
44; X32-POPCNT:       # BB#0:
45; X32-POPCNT-NEXT:    movzbl {{[0-9]+}}(%esp), %eax
46; X32-POPCNT-NEXT:    popcntw %ax, %ax
47; X32-POPCNT-NEXT:    # kill: %AL<def> %AL<kill> %AX<kill>
48; X32-POPCNT-NEXT:    retl
49;
50; X64-POPCNT-LABEL: cnt8:
51; X64-POPCNT:       # BB#0:
52; X64-POPCNT-NEXT:    movzbl %dil, %eax
53; X64-POPCNT-NEXT:    popcntw %ax, %ax
54; X64-POPCNT-NEXT:    # kill: %AL<def> %AL<kill> %AX<kill>
55; X64-POPCNT-NEXT:    retq
56  %cnt = tail call i8 @llvm.ctpop.i8(i8 %x)
57  ret i8 %cnt
58}
59
60define i16 @cnt16(i16 %x) nounwind readnone {
61; X32-LABEL: cnt16:
62; X32:       # BB#0:
63; X32-NEXT:    movzwl {{[0-9]+}}(%esp), %eax
64; X32-NEXT:    movl %eax, %ecx
65; X32-NEXT:    shrl %ecx
66; X32-NEXT:    andl $21845, %ecx # imm = 0x5555
67; X32-NEXT:    subl %ecx, %eax
68; X32-NEXT:    movl %eax, %ecx
69; X32-NEXT:    andl $13107, %ecx # imm = 0x3333
70; X32-NEXT:    shrl $2, %eax
71; X32-NEXT:    andl $13107, %eax # imm = 0x3333
72; X32-NEXT:    addl %ecx, %eax
73; X32-NEXT:    movl %eax, %ecx
74; X32-NEXT:    andl $32752, %ecx # imm = 0x7FF0
75; X32-NEXT:    shrl $4, %ecx
76; X32-NEXT:    addl %eax, %ecx
77; X32-NEXT:    andl $3855, %ecx # imm = 0xF0F
78; X32-NEXT:    movl %ecx, %eax
79; X32-NEXT:    shll $8, %eax
80; X32-NEXT:    addl %ecx, %eax
81; X32-NEXT:    movzbl %ah, %eax
82; X32-NEXT:    # kill: %AX<def> %AX<kill> %EAX<kill>
83; X32-NEXT:    retl
84;
85; X64-LABEL: cnt16:
86; X64:       # BB#0:
87; X64-NEXT:    movl %edi, %eax
88; X64-NEXT:    shrl %eax
89; X64-NEXT:    andl $21845, %eax # imm = 0x5555
90; X64-NEXT:    subl %eax, %edi
91; X64-NEXT:    movl %edi, %eax
92; X64-NEXT:    andl $13107, %eax # imm = 0x3333
93; X64-NEXT:    shrl $2, %edi
94; X64-NEXT:    andl $13107, %edi # imm = 0x3333
95; X64-NEXT:    addl %eax, %edi
96; X64-NEXT:    movl %edi, %eax
97; X64-NEXT:    andl $32752, %eax # imm = 0x7FF0
98; X64-NEXT:    shrl $4, %eax
99; X64-NEXT:    addl %edi, %eax
100; X64-NEXT:    andl $3855, %eax # imm = 0xF0F
101; X64-NEXT:    movl %eax, %ecx
102; X64-NEXT:    shll $8, %ecx
103; X64-NEXT:    addl %eax, %ecx
104; X64-NEXT:    movzbl %ch, %eax # NOREX
105; X64-NEXT:    # kill: %AX<def> %AX<kill> %EAX<kill>
106; X64-NEXT:    retq
107;
108; X32-POPCNT-LABEL: cnt16:
109; X32-POPCNT:       # BB#0:
110; X32-POPCNT-NEXT:    popcntw {{[0-9]+}}(%esp), %ax
111; X32-POPCNT-NEXT:    retl
112;
113; X64-POPCNT-LABEL: cnt16:
114; X64-POPCNT:       # BB#0:
115; X64-POPCNT-NEXT:    popcntw %di, %ax
116; X64-POPCNT-NEXT:    retq
117  %cnt = tail call i16 @llvm.ctpop.i16(i16 %x)
118  ret i16 %cnt
119}
120
121define i32 @cnt32(i32 %x) nounwind readnone {
122; X32-LABEL: cnt32:
123; X32:       # BB#0:
124; X32-NEXT:    movl {{[0-9]+}}(%esp), %eax
125; X32-NEXT:    movl %eax, %ecx
126; X32-NEXT:    shrl %ecx
127; X32-NEXT:    andl $1431655765, %ecx # imm = 0x55555555
128; X32-NEXT:    subl %ecx, %eax
129; X32-NEXT:    movl %eax, %ecx
130; X32-NEXT:    andl $858993459, %ecx # imm = 0x33333333
131; X32-NEXT:    shrl $2, %eax
132; X32-NEXT:    andl $858993459, %eax # imm = 0x33333333
133; X32-NEXT:    addl %ecx, %eax
134; X32-NEXT:    movl %eax, %ecx
135; X32-NEXT:    shrl $4, %ecx
136; X32-NEXT:    addl %eax, %ecx
137; X32-NEXT:    andl $252645135, %ecx # imm = 0xF0F0F0F
138; X32-NEXT:    imull $16843009, %ecx, %eax # imm = 0x1010101
139; X32-NEXT:    shrl $24, %eax
140; X32-NEXT:    retl
141;
142; X64-LABEL: cnt32:
143; X64:       # BB#0:
144; X64-NEXT:    movl %edi, %eax
145; X64-NEXT:    shrl %eax
146; X64-NEXT:    andl $1431655765, %eax # imm = 0x55555555
147; X64-NEXT:    subl %eax, %edi
148; X64-NEXT:    movl %edi, %eax
149; X64-NEXT:    andl $858993459, %eax # imm = 0x33333333
150; X64-NEXT:    shrl $2, %edi
151; X64-NEXT:    andl $858993459, %edi # imm = 0x33333333
152; X64-NEXT:    addl %eax, %edi
153; X64-NEXT:    movl %edi, %eax
154; X64-NEXT:    shrl $4, %eax
155; X64-NEXT:    addl %edi, %eax
156; X64-NEXT:    andl $252645135, %eax # imm = 0xF0F0F0F
157; X64-NEXT:    imull $16843009, %eax, %eax # imm = 0x1010101
158; X64-NEXT:    shrl $24, %eax
159; X64-NEXT:    retq
160;
161; X32-POPCNT-LABEL: cnt32:
162; X32-POPCNT:       # BB#0:
163; X32-POPCNT-NEXT:    popcntl {{[0-9]+}}(%esp), %eax
164; X32-POPCNT-NEXT:    retl
165;
166; X64-POPCNT-LABEL: cnt32:
167; X64-POPCNT:       # BB#0:
168; X64-POPCNT-NEXT:    popcntl %edi, %eax
169; X64-POPCNT-NEXT:    retq
170  %cnt = tail call i32 @llvm.ctpop.i32(i32 %x)
171  ret i32 %cnt
172}
173
174define i64 @cnt64(i64 %x) nounwind readnone {
175; X32-LABEL: cnt64:
176; X32:       # BB#0:
177; X32-NEXT:    movl {{[0-9]+}}(%esp), %eax
178; X32-NEXT:    movl {{[0-9]+}}(%esp), %ecx
179; X32-NEXT:    movl %ecx, %edx
180; X32-NEXT:    shrl %edx
181; X32-NEXT:    andl $1431655765, %edx # imm = 0x55555555
182; X32-NEXT:    subl %edx, %ecx
183; X32-NEXT:    movl %ecx, %edx
184; X32-NEXT:    andl $858993459, %edx # imm = 0x33333333
185; X32-NEXT:    shrl $2, %ecx
186; X32-NEXT:    andl $858993459, %ecx # imm = 0x33333333
187; X32-NEXT:    addl %edx, %ecx
188; X32-NEXT:    movl %ecx, %edx
189; X32-NEXT:    shrl $4, %edx
190; X32-NEXT:    addl %ecx, %edx
191; X32-NEXT:    andl $252645135, %edx # imm = 0xF0F0F0F
192; X32-NEXT:    imull $16843009, %edx, %ecx # imm = 0x1010101
193; X32-NEXT:    shrl $24, %ecx
194; X32-NEXT:    movl %eax, %edx
195; X32-NEXT:    shrl %edx
196; X32-NEXT:    andl $1431655765, %edx # imm = 0x55555555
197; X32-NEXT:    subl %edx, %eax
198; X32-NEXT:    movl %eax, %edx
199; X32-NEXT:    andl $858993459, %edx # imm = 0x33333333
200; X32-NEXT:    shrl $2, %eax
201; X32-NEXT:    andl $858993459, %eax # imm = 0x33333333
202; X32-NEXT:    addl %edx, %eax
203; X32-NEXT:    movl %eax, %edx
204; X32-NEXT:    shrl $4, %edx
205; X32-NEXT:    addl %eax, %edx
206; X32-NEXT:    andl $252645135, %edx # imm = 0xF0F0F0F
207; X32-NEXT:    imull $16843009, %edx, %eax # imm = 0x1010101
208; X32-NEXT:    shrl $24, %eax
209; X32-NEXT:    addl %ecx, %eax
210; X32-NEXT:    xorl %edx, %edx
211; X32-NEXT:    retl
212;
213; X64-LABEL: cnt64:
214; X64:       # BB#0:
215; X64-NEXT:    movq %rdi, %rax
216; X64-NEXT:    shrq %rax
217; X64-NEXT:    movabsq $6148914691236517205, %rcx # imm = 0x5555555555555555
218; X64-NEXT:    andq %rax, %rcx
219; X64-NEXT:    subq %rcx, %rdi
220; X64-NEXT:    movabsq $3689348814741910323, %rax # imm = 0x3333333333333333
221; X64-NEXT:    movq %rdi, %rcx
222; X64-NEXT:    andq %rax, %rcx
223; X64-NEXT:    shrq $2, %rdi
224; X64-NEXT:    andq %rax, %rdi
225; X64-NEXT:    addq %rcx, %rdi
226; X64-NEXT:    movq %rdi, %rax
227; X64-NEXT:    shrq $4, %rax
228; X64-NEXT:    leaq (%rax,%rdi), %rax
229; X64-NEXT:    movabsq $1085102592571150095, %rcx # imm = 0xF0F0F0F0F0F0F0F
230; X64-NEXT:    andq %rax, %rcx
231; X64-NEXT:    movabsq $72340172838076673, %rax # imm = 0x101010101010101
232; X64-NEXT:    imulq %rcx, %rax
233; X64-NEXT:    shrq $56, %rax
234; X64-NEXT:    retq
235;
236; X32-POPCNT-LABEL: cnt64:
237; X32-POPCNT:       # BB#0:
238; X32-POPCNT-NEXT:    popcntl {{[0-9]+}}(%esp), %ecx
239; X32-POPCNT-NEXT:    popcntl {{[0-9]+}}(%esp), %eax
240; X32-POPCNT-NEXT:    addl %ecx, %eax
241; X32-POPCNT-NEXT:    xorl %edx, %edx
242; X32-POPCNT-NEXT:    retl
243;
244; X64-POPCNT-LABEL: cnt64:
245; X64-POPCNT:       # BB#0:
246; X64-POPCNT-NEXT:    popcntq %rdi, %rax
247; X64-POPCNT-NEXT:    retq
248  %cnt = tail call i64 @llvm.ctpop.i64(i64 %x)
249  ret i64 %cnt
250}
251
252declare i8 @llvm.ctpop.i8(i8) nounwind readnone
253declare i16 @llvm.ctpop.i16(i16) nounwind readnone
254declare i32 @llvm.ctpop.i32(i32) nounwind readnone
255declare i64 @llvm.ctpop.i64(i64) nounwind readnone
256