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: movl %edi, %eax
12 ; NOBMI-NEXT: decl %eax
13 ; NOBMI-NEXT: je .LBB0_1
14 ; NOBMI-NEXT: # %bb.2: # %cond.false
15 ; NOBMI-NEXT: bsrl %eax, %ecx
16 ; NOBMI-NEXT: xorl $31, %ecx
17 ; NOBMI-NEXT: jmp .LBB0_3
18 ; NOBMI-NEXT: .LBB0_1:
19 ; NOBMI-NEXT: movl $32, %ecx
20 ; NOBMI-NEXT: .LBB0_3: # %cond.end
21 ; NOBMI-NEXT: negb %cl
22 ; NOBMI-NEXT: movl $1, %edx
23 ; NOBMI-NEXT: movl $1, %eax
24 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $ecx
25 ; NOBMI-NEXT: shll %cl, %eax
26 ; NOBMI-NEXT: cmpl $2, %edi
27 ; NOBMI-NEXT: cmovbl %edx, %eax
30 ; BMI-LABEL: bit_ceil_i32:
32 ; BMI-NEXT: # kill: def $edi killed $edi def $rdi
33 ; BMI-NEXT: leal -1(%rdi), %eax
34 ; BMI-NEXT: lzcntl %eax, %eax
36 ; BMI-NEXT: movl $1, %ecx
37 ; BMI-NEXT: shlxl %eax, %ecx, %eax
38 ; BMI-NEXT: cmpl $2, %edi
39 ; BMI-NEXT: cmovbl %ecx, %eax
42 %lz = tail call i32 @llvm.ctlz.i32(i32 %dec, i1 false)
43 %cnt = sub i32 32, %lz
44 %res = shl i32 1, %cnt
45 %ugt = icmp ugt i32 %x, 1
46 %sel = select i1 %ugt, i32 %res, i32 1
50 ; std::bit_ceil<uint32_t>(x + 1)
51 define i32 @bit_ceil_i32_plus1(i32 noundef %x) {
52 ; NOBMI-LABEL: bit_ceil_i32_plus1:
53 ; NOBMI: # %bb.0: # %entry
54 ; NOBMI-NEXT: testl %edi, %edi
55 ; NOBMI-NEXT: je .LBB1_1
56 ; NOBMI-NEXT: # %bb.2: # %cond.false
57 ; NOBMI-NEXT: bsrl %edi, %ecx
58 ; NOBMI-NEXT: xorl $31, %ecx
59 ; NOBMI-NEXT: jmp .LBB1_3
60 ; NOBMI-NEXT: .LBB1_1:
61 ; NOBMI-NEXT: movl $32, %ecx
62 ; NOBMI-NEXT: .LBB1_3: # %cond.end
63 ; NOBMI-NEXT: negb %cl
64 ; NOBMI-NEXT: movl $1, %edx
65 ; NOBMI-NEXT: movl $1, %eax
66 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $ecx
67 ; NOBMI-NEXT: shll %cl, %eax
68 ; NOBMI-NEXT: decl %edi
69 ; NOBMI-NEXT: cmpl $-2, %edi
70 ; NOBMI-NEXT: cmovael %edx, %eax
73 ; BMI-LABEL: bit_ceil_i32_plus1:
74 ; BMI: # %bb.0: # %entry
75 ; BMI-NEXT: lzcntl %edi, %eax
77 ; BMI-NEXT: movl $1, %ecx
78 ; BMI-NEXT: shlxl %eax, %ecx, %eax
80 ; BMI-NEXT: cmpl $-2, %edi
81 ; BMI-NEXT: cmovael %ecx, %eax
84 %ctlz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false)
85 %cnt = sub i32 32, %ctlz
86 %shl = shl i32 1, %cnt
88 %ult = icmp ult i32 %dec, -2
89 %sel = select i1 %ult, i32 %shl, i32 1
93 ; std::bit_ceil<uint64_t>(x)
94 define i64 @bit_ceil_i64(i64 %x) {
95 ; NOBMI-LABEL: bit_ceil_i64:
97 ; NOBMI-NEXT: movq %rdi, %rax
98 ; NOBMI-NEXT: decq %rax
99 ; NOBMI-NEXT: je .LBB2_1
100 ; NOBMI-NEXT: # %bb.2: # %cond.false
101 ; NOBMI-NEXT: bsrq %rax, %rcx
102 ; NOBMI-NEXT: xorq $63, %rcx
103 ; NOBMI-NEXT: jmp .LBB2_3
104 ; NOBMI-NEXT: .LBB2_1:
105 ; NOBMI-NEXT: movl $64, %ecx
106 ; NOBMI-NEXT: .LBB2_3: # %cond.end
107 ; NOBMI-NEXT: negb %cl
108 ; NOBMI-NEXT: movl $1, %edx
109 ; NOBMI-NEXT: movl $1, %eax
110 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $rcx
111 ; NOBMI-NEXT: shlq %cl, %rax
112 ; NOBMI-NEXT: cmpq $2, %rdi
113 ; NOBMI-NEXT: cmovbq %rdx, %rax
116 ; BMI-LABEL: bit_ceil_i64:
118 ; BMI-NEXT: leaq -1(%rdi), %rax
119 ; BMI-NEXT: lzcntq %rax, %rax
121 ; BMI-NEXT: movl $1, %ecx
122 ; BMI-NEXT: shlxq %rax, %rcx, %rax
123 ; BMI-NEXT: cmpq $2, %rdi
124 ; BMI-NEXT: cmovbq %rcx, %rax
126 %dec = add i64 %x, -1
127 %lz = tail call i64 @llvm.ctlz.i64(i64 %dec, i1 false)
128 %cnt = sub i64 64, %lz
129 %res = shl i64 1, %cnt
130 %ugt = icmp ugt i64 %x, 1
131 %sel = select i1 %ugt, i64 %res, i64 1
135 ; std::bit_ceil<uint64_t>(x + 1)
136 define i64 @bit_ceil_i64_plus1(i64 noundef %x) {
137 ; NOBMI-LABEL: bit_ceil_i64_plus1:
138 ; NOBMI: # %bb.0: # %entry
139 ; NOBMI-NEXT: testq %rdi, %rdi
140 ; NOBMI-NEXT: je .LBB3_1
141 ; NOBMI-NEXT: # %bb.2: # %cond.false
142 ; NOBMI-NEXT: bsrq %rdi, %rcx
143 ; NOBMI-NEXT: xorq $63, %rcx
144 ; NOBMI-NEXT: jmp .LBB3_3
145 ; NOBMI-NEXT: .LBB3_1:
146 ; NOBMI-NEXT: movl $64, %ecx
147 ; NOBMI-NEXT: .LBB3_3: # %cond.end
148 ; NOBMI-NEXT: negb %cl
149 ; NOBMI-NEXT: movl $1, %edx
150 ; NOBMI-NEXT: movl $1, %eax
151 ; NOBMI-NEXT: # kill: def $cl killed $cl killed $rcx
152 ; NOBMI-NEXT: shlq %cl, %rax
153 ; NOBMI-NEXT: decq %rdi
154 ; NOBMI-NEXT: cmpq $-2, %rdi
155 ; NOBMI-NEXT: cmovaeq %rdx, %rax
158 ; BMI-LABEL: bit_ceil_i64_plus1:
159 ; BMI: # %bb.0: # %entry
160 ; BMI-NEXT: lzcntq %rdi, %rax
162 ; BMI-NEXT: movl $1, %ecx
163 ; BMI-NEXT: shlxq %rax, %rcx, %rax
164 ; BMI-NEXT: decq %rdi
165 ; BMI-NEXT: cmpq $-2, %rdi
166 ; BMI-NEXT: cmovaeq %rcx, %rax
169 %ctlz = tail call i64 @llvm.ctlz.i64(i64 %x, i1 false)
170 %cnt = sub i64 64, %ctlz
171 %shl = shl i64 1, %cnt
172 %dec = add i64 %x, -1
173 %ult = icmp ult i64 %dec, -2
174 %sel = select i1 %ult, i64 %shl, i64 1
178 declare i32 @llvm.ctlz.i32(i32, i1 immarg)
179 declare i64 @llvm.ctlz.i64(i64, i1 immarg)