[InstSimplify] fold xor logic of 2 variables
authorSanjay Patel <spatel@rotateright.com>
Tue, 23 Nov 2021 21:46:55 +0000 (16:46 -0500)
committerSanjay Patel <spatel@rotateright.com>
Tue, 23 Nov 2021 21:50:23 +0000 (16:50 -0500)
commit892648b18a8cc3b8a08528112adfa74bdd432f8b
tree0f91ad5e07a35fef64966fb0234b4f670cd923e5
parentb1a68b170c44e8d5298800a080444c0b82366e51
[InstSimplify] fold xor logic of 2 variables

(a & b) ^ (~a | b) --> ~a

I was looking for a shortcut to reduce some of the complex logic
folds that are currently up for review (D113216
and others in that stack), and I found this missing from
instcombine/instsimplify.

There is a trade-off in putting it into instsimplify: because
we can't create new values here, we need a strict 'not' op (no
undef elements). Otherwise, the fold is not valid:
https://alive2.llvm.org/ce/z/k_AGGj

If this was in instcombine instead, we could create the proper
'not'. But having the fold here benefits other passes like GVN
that use instsimplify as an analysis.

There is a related fold where 'and' and 'or' are swapped, and
that is planned as a follow-up commit.

Differential Revision: https://reviews.llvm.org/D114462
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/xor.ll