Issue 61644
Summary Generate better code for absl::bit_ceil
Labels llvm:instcombine, missed-optimization
Assignees
Reporter kazutakahirata
    Compile:

```
// clang -std=c++20 -march=skylake -O3
#include "absl/numeric/bits.h"

unsigned foo(unsigned x) {
  return absl::bit_ceil<unsigned>(x);
}
```

I get:

```
  %2 = tail call i32 @llvm.ctpop.i32(i32 %0)
  %3 = icmp eq i32 %2, 1
  %4 = tail call i32 @llvm.ctlz.i32(i32 %0, i1 false)
  %5 = select i1 %3, i32 31, i32 32
  %6 = sub nsw i32 %5, %4
  %7 = shl nuw i32 1, %6
  ret i32 %7
```

```
  f3 0f b8 c7                popcnt %edi,%eax
 31 c9                      xor    %ecx,%ecx
  83 f8 01 cmp    $0x1,%eax
  0f 94 c1                   sete   %cl
  f3 0f bd c7 lzcnt  %edi,%eax
  01 c1                      add %eax,%ecx
  f7 d9                      neg    %ecx
  b8 01 00 00 00 mov    $0x1,%eax
  c4 e2 71 f7 c0             shlx %ecx,%eax,%eax
```

We could generate the identical code as in https://github.com/llvm/llvm-project/issues/60802:

```
  %dec = add i32 %x, -1
  %ctlz = tail call i32 @llvm.ctlz.i32(i32 %dec, i1 false)
 %sub = sub nsw i32 0, %ctlz
  %and = and i32 %1, 31
  %sel = shl nuw i32 1, %and
  ret i32 %sel
```

https://alive2.llvm.org/ce/z/7aqxmc
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to