[InstCombine] try to determine "exact" for sdiv
authorSanjay Patel <spatel@rotateright.com>
Sun, 16 Oct 2022 14:01:10 +0000 (10:01 -0400)
committerSanjay Patel <spatel@rotateright.com>
Sun, 16 Oct 2022 14:59:56 +0000 (10:59 -0400)
commite5ee0b06d694fe7749b56706f1bf67e22eaef628
tree93de1215ac29801dbb90865e80b4e8743718f4de
parent78e3aeda3ce22d68362660b7712793d698800926
[InstCombine] try to determine "exact" for sdiv

If the divisor is a power-of-2 or negative-power-of-2 and the dividend
is known to have >= trailing zeros than the divisor, the division is exact:
https://alive2.llvm.org/ce/z/UGBksM (general proof)
https://alive2.llvm.org/ce/z/D4yPS- (examples based on regression tests)

This isn't the most direct optimization (we could create ashr in these
examples instead of relying on existing folds for exact divides), but
it's possible that there's a more general constraint than just a pow2
divisor, so this might be extended in the future.

This should solve issue #58348.

Differential Revision: https://reviews.llvm.org/D135970
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/sdiv-exact-by-negative-power-of-two.ll
llvm/test/Transforms/InstCombine/sdiv-exact-by-power-of-two.ll