xref: /llvm-project/llvm/test/CodeGen/X86/ctpop-combine.ll (revision 9410019ac977141bc73aee19690b5896ded59219)
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
4
5declare i8 @llvm.ctpop.i8(i8) nounwind readnone
6declare i64 @llvm.ctpop.i64(i64) nounwind readnone
7
8define i32 @test1(i64 %x) nounwind readnone {
9; CHECK-LABEL: test1:
10; CHECK:       # %bb.0:
11; CHECK-NEXT:    leaq -1(%rdi), %rcx
12; CHECK-NEXT:    xorl %eax, %eax
13; CHECK-NEXT:    testq %rcx, %rdi
14; CHECK-NEXT:    setne %al
15; CHECK-NEXT:    retq
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
20  ret i32 %conv
21}
22
23
24define i32 @test2(i64 %x) nounwind readnone {
25; CHECK-LABEL: test2:
26; CHECK:       # %bb.0:
27; CHECK-NEXT:    leaq -1(%rdi), %rcx
28; CHECK-NEXT:    xorl %eax, %eax
29; CHECK-NEXT:    testq %rcx, %rdi
30; CHECK-NEXT:    sete %al
31; CHECK-NEXT:    retq
32  %count = tail call i64 @llvm.ctpop.i64(i64 %x)
33  %cmp = icmp ult i64 %count, 2
34  %conv = zext i1 %cmp to i32
35  ret i32 %conv
36}
37
38define i32 @test3(i64 %x) nounwind readnone {
39; POPCOUNT-LABEL: test3:
40; POPCOUNT:       # %bb.0:
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
46; POPCOUNT-NEXT:    retq
47;
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
78  ret i32 %conv
79}
80
81define i8 @test4(i8 %x) nounwind readnone {
82; POPCOUNT-LABEL: test4:
83; POPCOUNT:       # %bb.0:
84; POPCOUNT-NEXT:    andl $127, %edi
85; POPCOUNT-NEXT:    popcntl %edi, %eax
86; POPCOUNT-NEXT:    # kill: def $al killed $al killed $eax
87; POPCOUNT-NEXT:    retq
88;
89; NO-POPCOUNT-LABEL: test4:
90; NO-POPCOUNT:       # %bb.0:
91; NO-POPCOUNT-NEXT:    andl $127, %edi
92; NO-POPCOUNT-NEXT:    imull $134480385, %edi, %eax # imm = 0x8040201
93; NO-POPCOUNT-NEXT:    shrl $3, %eax
94; NO-POPCOUNT-NEXT:    andl $286331153, %eax # imm = 0x11111111
95; NO-POPCOUNT-NEXT:    imull $286331153, %eax, %eax # imm = 0x11111111
96; NO-POPCOUNT-NEXT:    shrl $28, %eax
97; NO-POPCOUNT-NEXT:    # kill: def $al killed $al killed $eax
98; NO-POPCOUNT-NEXT:    retq
99  %x2 = and i8 %x, 127
100  %count = tail call i8 @llvm.ctpop.i8(i8 %x2)
101  %and = and i8 %count, 7
102  ret i8 %and
103}
104
105define i32 @ctpop_eq_one(i64 %x) nounwind readnone {
106; POPCOUNT-LABEL: ctpop_eq_one:
107; POPCOUNT:       # %bb.0:
108; POPCOUNT-NEXT:    popcntq %rdi, %rcx
109; POPCOUNT-NEXT:    xorl %eax, %eax
110; POPCOUNT-NEXT:    cmpl $1, %ecx
111; POPCOUNT-NEXT:    sete %al
112; POPCOUNT-NEXT:    retq
113;
114; NO-POPCOUNT-LABEL: ctpop_eq_one:
115; NO-POPCOUNT:       # %bb.0:
116; NO-POPCOUNT-NEXT:    leaq -1(%rdi), %rcx
117; NO-POPCOUNT-NEXT:    xorq %rcx, %rdi
118; NO-POPCOUNT-NEXT:    xorl %eax, %eax
119; NO-POPCOUNT-NEXT:    cmpq %rcx, %rdi
120; NO-POPCOUNT-NEXT:    seta %al
121; NO-POPCOUNT-NEXT:    retq
122  %count = tail call i64 @llvm.ctpop.i64(i64 %x)
123  %cmp = icmp eq i64 %count, 1
124  %conv = zext i1 %cmp to i32
125  ret i32 %conv
126}
127
128define i32 @ctpop_ne_one(i64 %x) nounwind readnone {
129; POPCOUNT-LABEL: ctpop_ne_one:
130; POPCOUNT:       # %bb.0:
131; POPCOUNT-NEXT:    popcntq %rdi, %rcx
132; POPCOUNT-NEXT:    xorl %eax, %eax
133; POPCOUNT-NEXT:    cmpl $1, %ecx
134; POPCOUNT-NEXT:    setne %al
135; POPCOUNT-NEXT:    retq
136;
137; NO-POPCOUNT-LABEL: ctpop_ne_one:
138; NO-POPCOUNT:       # %bb.0:
139; NO-POPCOUNT-NEXT:    leaq -1(%rdi), %rcx
140; NO-POPCOUNT-NEXT:    xorq %rcx, %rdi
141; NO-POPCOUNT-NEXT:    xorl %eax, %eax
142; NO-POPCOUNT-NEXT:    cmpq %rcx, %rdi
143; NO-POPCOUNT-NEXT:    setbe %al
144; NO-POPCOUNT-NEXT:    retq
145  %count = tail call i64 @llvm.ctpop.i64(i64 %x)
146  %cmp = icmp ne i64 %count, 1
147  %conv = zext i1 %cmp to i32
148  ret i32 %conv
149}
150
151define i1 @ctpop_trunc_non_power2(i255 %x) nounwind {
152; CHECK-LABEL: ctpop_trunc_non_power2:
153; CHECK:       # %bb.0:
154; CHECK-NEXT:    movq %rdi, %rax
155; CHECK-NEXT:    addq $-1, %rax
156; CHECK-NEXT:    movq %rsi, %r8
157; CHECK-NEXT:    adcq $-1, %r8
158; CHECK-NEXT:    movq %rdx, %r9
159; CHECK-NEXT:    adcq $-1, %r9
160; CHECK-NEXT:    movabsq $9223372036854775807, %r10 # imm = 0x7FFFFFFFFFFFFFFF
161; CHECK-NEXT:    movq %rcx, %r11
162; CHECK-NEXT:    adcq %r10, %r11
163; CHECK-NEXT:    xorq %r11, %rcx
164; CHECK-NEXT:    andq %r10, %r11
165; CHECK-NEXT:    andq %r10, %rcx
166; CHECK-NEXT:    xorq %r9, %rdx
167; CHECK-NEXT:    xorq %r8, %rsi
168; CHECK-NEXT:    xorq %rax, %rdi
169; CHECK-NEXT:    cmpq %rdi, %rax
170; CHECK-NEXT:    sbbq %rsi, %r8
171; CHECK-NEXT:    sbbq %rdx, %r9
172; CHECK-NEXT:    sbbq %rcx, %r11
173; CHECK-NEXT:    setb %al
174; CHECK-NEXT:    retq
175  %a = call i255 @llvm.ctpop.i255(i255 %x)
176  %b = trunc i255 %a to i8 ; largest value from ctpop is 255, fits in 8 bits.
177  %c = icmp eq i8 %b, 1
178  ret i1 %c
179}
180declare i255 @llvm.ctpop.i255(i255)
181