[InstCombine] improve demanded bits for Sub operand 0
authorSanjay Patel <spatel@rotateright.com>
Thu, 27 Oct 2022 12:54:07 +0000 (08:54 -0400)
committerSanjay Patel <spatel@rotateright.com>
Thu, 27 Oct 2022 13:41:57 +0000 (09:41 -0400)
commitd2d23795cac9ecfae9f5f18be05e4211d4c0cac2
tree8d1bee61f968e949b9bbd006d9a9ddace3df2f12
parentae48d1c76a6c14cdbf3a3e73f6c2c2befd0a81b0
[InstCombine] improve demanded bits for Sub operand 0

This is copying the code that was added for 'add' with D130075.
(That patch removed a fallthrough in the cases, but we can
probably still share at least some code again as a follow-up
cleanup, but I didn't want to risk it here.)

The reasoning is similar to the carry propagation for 'add':
if we don't demand low bits of the subtraction and the
subtrahend (aka RHS or operand 1) is known zero in those low
bits, then there can't be any borrowing required from the
higher bits of operand 0, so the low bits don't matter.

Also, the no-wrap flags can be propagated (and I think that
should be true for add too).

Here's an attempt to prove that in Alive2:
https://alive2.llvm.org/ce/z/xqh7Pa
(can add nsw or nuw to src and tgt, and it should still pass)

Differential Revision: https://reviews.llvm.org/D136788
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
llvm/test/Transforms/InstCombine/sub.ll