OSDN Git Service

[SelectionDAG] Improve the legalisation lowering of UMULO.
authorEli Friedman <efriedma@codeaurora.org>
Thu, 16 Aug 2018 18:39:39 +0000 (18:39 +0000)
committerEli Friedman <efriedma@codeaurora.org>
Thu, 16 Aug 2018 18:39:39 +0000 (18:39 +0000)
commitcb762f1f3ddc637563198f1404bff18d120ccc97
treea599ab0c083395dbb762b8dd088c517ba7f9ff81
parent7b455c4fd2fc947f57e62746093b670a152681ea
[SelectionDAG] Improve the legalisation lowering of UMULO.

There is no way in the universe, that doing a full-width division in
software will be faster than doing overflowing multiplication in
software in the first place, especially given that this same full-width
multiplication needs to be done anyway.

This patch replaces the previous implementation with a direct lowering
into an overflowing multiplication algorithm based on half-width
operations.

Correctness of the algorithm was verified by exhaustively checking the
output of this algorithm for overflowing multiplication of 16 bit
integers against an obviously correct widening multiplication. Baring
any oversights introduced by porting the algorithm to DAG, confidence in
correctness of this algorithm is extremely high.

Following table shows the change in both t = runtime and s = space. The
change is expressed as a multiplier of original, so anything under 1 is
“better” and anything above 1 is worse.

+-------+-----------+-----------+-------------+-------------+
| Arch  | u64*u64 t | u64*u64 s | u128*u128 t | u128*u128 s |
+-------+-----------+-----------+-------------+-------------+
|   X64 |     -     |     -     |    ~0.5     |    ~0.64    |
|  i686 |   ~0.5    |   ~0.6666 |    ~0.05    |    ~0.9     |
| armv7 |     -     |   ~0.75   |      -      |    ~1.4     |
+-------+-----------+-----------+-------------+-------------+

Performance numbers have been collected by running overflowing
multiplication in a loop under `perf` on two x86_64 (one Intel Haswell,
other AMD Ryzen) based machines. Size numbers have been collected by
looking at the size of function containing an overflowing multiply in
a loop.

All in all, it can be seen that both performance and size has improved
except in the case of armv7 where code size has regressed for 128-bit
multiply. u128*u128 overflowing multiply on 32-bit platforms seem to
benefit from this change a lot, taking only 5% of the time compared to
original algorithm to calculate the same thing.

The final benefit of this change is that LLVM is now capable of lowering
the overflowing unsigned multiply for integers of any bit-width as long
as the target is capable of lowering regular multiplication for the same
bit-width. Previously, 128-bit overflowing multiply was the widest
possible.

Patch by Simonas Kazlauskas!

Differential Revision: https://reviews.llvm.org/D50310

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339922 91177308-0d34-0410-b5e6-96231b3b80d8
15 files changed:
lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
test/CodeGen/AArch64/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/ARM/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/ARM/umulo-64-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/PowerPC/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/RISCV/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/SPARC/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/Thumb/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/Thumb2/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/Thumb2/umulo-64-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/WebAssembly/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/X86/muloti.ll
test/CodeGen/X86/select.ll
test/CodeGen/X86/umulo-128-legalisation-lowering.ll [new file with mode: 0644]
test/CodeGen/X86/umulo-64-legalisation-lowering.ll [new file with mode: 0644]