[InstSimplify] fold exact divide to poison if it is known to not divide evenly
authorSanjay Patel <spatel@rotateright.com>
Thu, 29 Dec 2022 14:55:59 +0000 (09:55 -0500)
committerSanjay Patel <spatel@rotateright.com>
Thu, 29 Dec 2022 15:26:50 +0000 (10:26 -0500)
commitf0faea571403eb75a1d2d0dceca1dd52a7824d33
treee2f71339e565b0bf6aacfd0c99829a3f3bef31be
parenta3d58bbaff92686434473f3f00cc153d0840ca91
[InstSimplify] fold exact divide to poison if it is known to not divide evenly

This is related to the discussion in D140665. I was looking over the demanded
bits implementation in IR and noticed that we just bail out of a potential
fold if a udiv is exact:
https://github.com/llvm/llvm-project/blob/82be8a1d2b00f6e89096b86f670a8be894c7b9e6/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp#L799

Also, see tests added with 7f0c11509e8f.

Then, I saw that we could lose a fold to poison if we zap the exact with that
transform, so this patch tries to catch that as a preliminary step.

Alive2 proofs:
https://alive2.llvm.org/ce/z/zCjKM7
https://alive2.llvm.org/ce/z/-tz_RK (trailing zeros must be "less-than")
https://alive2.llvm.org/ce/z/c9CMsJ (general proof and specific example)

Differential Revision: https://reviews.llvm.org/D140733
llvm/include/llvm/Analysis/InstructionSimplify.h
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/udiv-simplify.ll
llvm/test/Transforms/InstSimplify/div.ll