<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61644>61644</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Generate better code for absl::bit_ceil
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:instcombine,
            missed-optimization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kazutakahirata
      </td>
    </tr>
</table>

<pre>
    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
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVctu6zYQ_RpqM7BBDfXyQovEroquuuyyoMixxYaifEUq9fXXF5TlXMfXMVpUEPRIzpk58ziW9N4cHFHN8leW7xI5hW4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