[InstCombine] fold add+negate through select into sub
authorSanjay Patel <spatel@rotateright.com>
Wed, 7 Sep 2022 11:53:38 +0000 (07:53 -0400)
committerSanjay Patel <spatel@rotateright.com>
Wed, 7 Sep 2022 12:23:35 +0000 (08:23 -0400)
commit7c57180900fb6d703c71351e67eaea0f1a25d2e1
tree876ea11bce23de49b913e296998afb618bff1975
parente6c5d13ac762e7535191e6c3e16b00fc93433c73
[InstCombine] fold add+negate through select into sub

This transform came up as a potential DAGCombine in D133282,
so I wanted to see how it escaped in IR too.

We do general folds in InstCombiner::SimplifySelectsFeedingBinaryOp()
by checking if either arm of a select simplifies when the trailing
binop is threaded into the select.

So as long as one side simplifies, it's a good fold to combine a
negate and add into 1 subtract.

This is an example with a zero arm in the select:
https://alive2.llvm.org/ce/z/Hgu_Tj

And this models the tests with a cancelling 'not' op:
https://alive2.llvm.org/ce/z/BuzVV_

Differential Revision: https://reviews.llvm.org/D133369
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/add.ll