1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=-bmi2,-lzcnt | FileCheck %s --check-prefix=NOBMI
3 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+bmi2,+lzcnt | FileCheck %s --check-prefix=BMI
5 ; Check the assembly sequence generated for std::bit_ceil.
7 ; std::bit_ceil<uint32_t>(x)
8 define i32 @bit_ceil_i32(i32 %x) {
9 ; NOBMI-LABEL: bit_ceil_i32:
11 ; NOBMI-NEXT: # kill: def $edi killed $edi def $rdi
12 ; NOBMI-NEXT: leal -1(%rdi), %eax
13 ; NOBMI-NEXT: bsrl %eax, %eax
14 ; NOBMI-NEXT: movl $63, %ecx
15 ; NOBMI-NEXT: cmovnel %eax, %ecx
16 ; NOBMI-NEXT: xorl $31, %ecx
17 ; NOBMI-NEXT: negb %cl
18 ; NOBMI-NEXT: movl $1, %edx
19 ; NOBMI-NEXT: movl $1, %eax
20 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $ecx
21 ; NOBMI-NEXT: shll %cl, %eax
22 ; NOBMI-NEXT: cmpl $2, %edi
23 ; NOBMI-NEXT: cmovbl %edx, %eax
26 ; BMI-LABEL: bit_ceil_i32:
28 ; BMI-NEXT: # kill: def $edi killed $edi def $rdi
29 ; BMI-NEXT: leal -1(%rdi), %eax
30 ; BMI-NEXT: lzcntl %eax, %eax
32 ; BMI-NEXT: movl $1, %ecx
33 ; BMI-NEXT: shlxl %eax, %ecx, %eax
34 ; BMI-NEXT: cmpl $2, %edi
35 ; BMI-NEXT: cmovbl %ecx, %eax
38 %lz = tail call i32 @llvm.ctlz.i32(i32 %dec, i1 false)
39 %cnt = sub i32 32, %lz
40 %res = shl i32 1, %cnt
41 %ugt = icmp ugt i32 %x, 1
42 %sel = select i1 %ugt, i32 %res, i32 1
46 ; std::bit_ceil<uint32_t>(x + 1)
47 define i32 @bit_ceil_i32_plus1(i32 noundef %x) {
48 ; NOBMI-LABEL: bit_ceil_i32_plus1:
49 ; NOBMI: # %bb.0: # %entry
50 ; NOBMI-NEXT: bsrl %edi, %eax
51 ; NOBMI-NEXT: movl $63, %ecx
52 ; NOBMI-NEXT: cmovnel %eax, %ecx
53 ; NOBMI-NEXT: xorl $31, %ecx
54 ; NOBMI-NEXT: negb %cl
55 ; NOBMI-NEXT: movl $1, %edx
56 ; NOBMI-NEXT: movl $1, %eax
57 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $ecx
58 ; NOBMI-NEXT: shll %cl, %eax
59 ; NOBMI-NEXT: decl %edi
60 ; NOBMI-NEXT: cmpl $-2, %edi
61 ; NOBMI-NEXT: cmovael %edx, %eax
64 ; BMI-LABEL: bit_ceil_i32_plus1:
65 ; BMI: # %bb.0: # %entry
66 ; BMI-NEXT: lzcntl %edi, %eax
68 ; BMI-NEXT: movl $1, %ecx
69 ; BMI-NEXT: shlxl %eax, %ecx, %eax
71 ; BMI-NEXT: cmpl $-2, %edi
72 ; BMI-NEXT: cmovael %ecx, %eax
75 %ctlz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false)
76 %cnt = sub i32 32, %ctlz
77 %shl = shl i32 1, %cnt
79 %ult = icmp ult i32 %dec, -2
80 %sel = select i1 %ult, i32 %shl, i32 1
84 ; std::bit_ceil<uint64_t>(x)
85 define i64 @bit_ceil_i64(i64 %x) {
86 ; NOBMI-LABEL: bit_ceil_i64:
88 ; NOBMI-NEXT: leaq -1(%rdi), %rax
89 ; NOBMI-NEXT: bsrq %rax, %rax
90 ; NOBMI-NEXT: movl $127, %ecx
91 ; NOBMI-NEXT: cmovneq %rax, %rcx
92 ; NOBMI-NEXT: xorl $63, %ecx
93 ; NOBMI-NEXT: negb %cl
94 ; NOBMI-NEXT: movl $1, %edx
95 ; NOBMI-NEXT: movl $1, %eax
96 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $rcx
97 ; NOBMI-NEXT: shlq %cl, %rax
98 ; NOBMI-NEXT: cmpq $2, %rdi
99 ; NOBMI-NEXT: cmovbq %rdx, %rax
102 ; BMI-LABEL: bit_ceil_i64:
104 ; BMI-NEXT: leaq -1(%rdi), %rax
105 ; BMI-NEXT: lzcntq %rax, %rax
107 ; BMI-NEXT: movl $1, %ecx
108 ; BMI-NEXT: shlxq %rax, %rcx, %rax
109 ; BMI-NEXT: cmpq $2, %rdi
110 ; BMI-NEXT: cmovbq %rcx, %rax
112 %dec = add i64 %x, -1
113 %lz = tail call i64 @llvm.ctlz.i64(i64 %dec, i1 false)
114 %cnt = sub i64 64, %lz
115 %res = shl i64 1, %cnt
116 %ugt = icmp ugt i64 %x, 1
117 %sel = select i1 %ugt, i64 %res, i64 1
121 ; std::bit_ceil<uint64_t>(x + 1)
122 define i64 @bit_ceil_i64_plus1(i64 noundef %x) {
123 ; NOBMI-LABEL: bit_ceil_i64_plus1:
124 ; NOBMI: # %bb.0: # %entry
125 ; NOBMI-NEXT: bsrq %rdi, %rax
126 ; NOBMI-NEXT: movl $127, %ecx
127 ; NOBMI-NEXT: cmovneq %rax, %rcx
128 ; NOBMI-NEXT: xorl $63, %ecx
129 ; NOBMI-NEXT: negb %cl
130 ; NOBMI-NEXT: movl $1, %edx
131 ; NOBMI-NEXT: movl $1, %eax
132 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $rcx
133 ; NOBMI-NEXT: shlq %cl, %rax
134 ; NOBMI-NEXT: decq %rdi
135 ; NOBMI-NEXT: cmpq $-2, %rdi
136 ; NOBMI-NEXT: cmovaeq %rdx, %rax
139 ; BMI-LABEL: bit_ceil_i64_plus1:
140 ; BMI: # %bb.0: # %entry
141 ; BMI-NEXT: lzcntq %rdi, %rax
143 ; BMI-NEXT: movl $1, %ecx
144 ; BMI-NEXT: shlxq %rax, %rcx, %rax
145 ; BMI-NEXT: decq %rdi
146 ; BMI-NEXT: cmpq $-2, %rdi
147 ; BMI-NEXT: cmovaeq %rcx, %rax
150 %ctlz = tail call i64 @llvm.ctlz.i64(i64 %x, i1 false)
151 %cnt = sub i64 64, %ctlz
152 %shl = shl i64 1, %cnt
153 %dec = add i64 %x, -1
154 %ult = icmp ult i64 %dec, -2
155 %sel = select i1 %ult, i64 %shl, i64 1
159 declare i32 @llvm.ctlz.i32(i32, i1 immarg)
160 declare i64 @llvm.ctlz.i64(i64, i1 immarg)