[PATCH] D145890: [InstCombine] Generate better code for std::bit_floor from libstdc++

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 12 18:03:46 PDT 2023


kazu created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Without this patch, std::bit_floor<uint32_t> in libstdc++ is compiled
as:

  %eq0 = icmp eq i32 %x, 0
  %lshr = lshr i32 %x, 1
  %ctlz = tail call i32 @llvm.ctlz.i32(i32 %lshr, i1 false)
  %sub = sub i32 32, %ctlz
  %shl = shl i32 1, %sub
  %sel = select i1 %eq0, i32 0, i32 %shl

With this patch:

  %eq0 = icmp eq i32 %x, 0
  %ctlz = call i32 @llvm.ctlz.i32(i32 %x, i1 false)
  %lshr = lshr i32 -2147483648, %1
  %sel = select i1 %eq0, i32 0, i32 %lshr

This patch recognizes the specific pattern emitted for std::bit_floor
in libstdc++.

https://alive2.llvm.org/ce/z/piMdFX

This patch fixes:

https://github.com/llvm/llvm-project/issues/61183


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145890

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/bit_floor.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145890.504489.patch
Type: text/x-patch
Size: 4069 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230313/61c900c1/attachment.bin>


More information about the llvm-commits mailing list