[CodeGen][SelectionDAG] More efficient code for X % C == 0 (SREM case)
authorRoman Lebedev <lebedev.ri@gmail.com>
Tue, 13 Aug 2019 14:57:37 +0000 (14:57 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Tue, 13 Aug 2019 14:57:37 +0000 (14:57 +0000)
commit676594305a16bb085b8dfa24a5adbb5e97be6d69
treefae53f62fe043b462a76bee72aac2b0b47fadf4f
parentf4de7eda4a7098ea3a5d60c56af8ab8e9d28ff08
[CodeGen][SelectionDAG] More efficient code for X % C == 0 (SREM case)

Summary:
This implements an optimization described in Hacker's Delight 10-17:
when `C` is constant, the result of `X % C == 0` can be computed
more cheaply without actually calculating the remainder.
The motivation is discussed here: https://bugs.llvm.org/show_bug.cgi?id=35479.

One huge caveat: this signed case is only valid for positive divisors.

While we can freely negate negative divisors, we can't negate `INT_MIN`,
so for now if `INT_MIN` is encountered, we bailout.
As a follow-up, it should be possible to handle that more gracefully
via extra `and`+`setcc`+`select`.

This passes llvm's test-suite, and from cursory(!) cross-examination
the folds (the assembly) match those of GCC, and manual checking via alive
did not reveal any issues (other than the `INT_MIN` case)

Reviewers: RKSimon, spatel, hermord, craig.topper, xbolva00

Reviewed By: RKSimon, xbolva00

Subscribers: xbolva00, thakis, javed.absar, hiraditya, dexonsmith, llvm-commits

Tags: #llvm

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

llvm-svn: 368702
llvm/include/llvm/CodeGen/TargetLowering.h
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
llvm/test/CodeGen/AArch64/srem-seteq-optsize.ll
llvm/test/CodeGen/AArch64/srem-seteq-vec-nonsplat.ll
llvm/test/CodeGen/AArch64/srem-seteq-vec-splat.ll
llvm/test/CodeGen/AArch64/srem-seteq.ll
llvm/test/CodeGen/X86/srem-seteq-optsize.ll
llvm/test/CodeGen/X86/srem-seteq-vec-nonsplat.ll
llvm/test/CodeGen/X86/srem-seteq-vec-splat.ll
llvm/test/CodeGen/X86/srem-seteq.ll
llvm/test/CodeGen/X86/vselect-avx.ll