[PATCH] D130862: [LegalizeTypes] Improve splitting for urem by constant for some constants.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 31 16:36:44 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: RKSimon, spatel, efriedma, arsenm.
Herald added subscribers: luke957, StephenFan, frasercrmck, luismarques, apazos, sameer.abuasal, steven.zhang, pengfei, s.egerton, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, hiraditya.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added subscribers: pcwang-thead, MaskRay, wdng.
Herald added a project: LLVM.

If (Bitwidth / 2) % Divisor == 1, we can add the high and low halves
together and use a (Bitwidth / 2) urem. If (BitWidth /2) is a legal
type, this urem will be expand by DAGCombiner using multiply by magic
constant. We do have to take into account that adding high and low
together can produce a carry, making it a (BitWidth / 2)+1 bit number.
So we need to also add back in the carry from the first addition.

This is based on the section "Remainder by Summing Digits" in
Hacker's delight.

This is similar to a trick you may have learned for determining
if a decimal number is divisible by 3. You can add all the digits
together and see if the sum is divisible by 3. If you're not sure
if the sum is divisible by 3, you can add its digits together. This
can be repeated until you have a single decimal digit. If that digit
is 3, 6, or 9, then the original number is divisible by 3. This works
because 10 % 3 == 1.

gcc already does this same trick. There are additional tricks gcc
does urem as well as srem, udiv, and sdiv that I plan to add in
future patches.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130862

Files:
  llvm/include/llvm/CodeGen/TargetLowering.h
  llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
  llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
  llvm/lib/Target/X86/X86ISelLowering.cpp
  llvm/test/CodeGen/RISCV/split-urem-by-constant.ll
  llvm/test/CodeGen/VE/Scalar/rem.ll
  llvm/test/CodeGen/X86/divide-by-constant.ll
  llvm/test/CodeGen/X86/divmod128.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130862.448896.patch
Type: text/x-patch
Size: 36706 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220731/1e4e48c6/attachment-0001.bin>


More information about the llvm-commits mailing list