This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] (~a & b & c) | ~(a | b) -> (c | ~b) & ~a
ClosedPublic

Authored by rampitec on Nov 2 2021, 11:23 AM.

Details

Summary

Transform

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

and swapped case

(~a | b | c) & ~(a & b) -> (c & ~b) | ~a
----------------------------------------
define i4 @src(i4 %a, i4 %b, i4 %c) {
%0:
  %or1 = or i4 %b, %a
  %not1 = xor i4 %or1, 15
  %not2 = xor i4 %a, 15
  %and1 = and i4 %b, %not2
  %and2 = and i4 %and1, %c
  %or2 = or i4 %and2, %not1
  ret i4 %or2
}
=>
define i4 @tgt(i4 %a, i4 %b, i4 %c) {
%0:
  %notb = xor i4 %b, 15
  %or = or i4 %notb, %c
  %nota = xor i4 %a, 15
  %and = and i4 %or, %nota
  ret i4 %and
}
Transformation seems to be correct!
----------------------------------------
define i4 @src(i4 %a, i4 %b, i4 %c) {
%0:
  %and1 = and i4 %b, %a
  %not1 = xor i4 %and1, 15
  %not2 = xor i4 %a, 15
  %or1 = or i4 %b, %not2
  %or2 = or i4 %or1, %c
  %and2 = and i4 %or2, %not1
  ret i4 %and2
}
=>
define i4 @tgt(i4 %a, i4 %b, i4 %c) {
%0:
  %notb = xor i4 %b, 15
  %and = and i4 %notb, %c
  %nota = xor i4 %a, 15
  %or = or i4 %and, %nota
  ret i4 %or
}
Transformation seems to be correct!

Diff Detail

Event Timeline

rampitec created this revision.Nov 2 2021, 11:23 AM
rampitec requested review of this revision.Nov 2 2021, 11:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 2 2021, 11:23 AM
rampitec planned changes to this revision.Nov 10 2021, 12:53 PM

In the light of D113526 this needs to be rebased and handle inverted pattern:

(~a | b | c) & ~(a & b) -> (c & ~b) | ~a
define i4 @src(i4 %a, i4 %b, i4 %c) {
  %and1 = and i4 %b, %a
  %not1 = xor i4 %and1, -1
  %not2 = xor i4 %a, -1
  %or1 = or i4 %b, %not2
  %or2 = or i4 %or1, %c
  %and2 = and i4 %or2, %not1
  ret i4 %and2
}

define i4 @tgt(i4 %a, i4 %b, i4 %c) {
  %notb = xor i4 %b, -1
  %and = and i4 %notb, %c
  %nota = xor i4 %a, -1
  %or = or i4 %and, %nota
  ret i4 %or
}
rampitec updated this revision to Diff 386926.Nov 12 2021, 12:44 PM
rampitec edited the summary of this revision. (Show Details)

Added swapped case

(~a | b | c) & ~(a & b) -> (c & ~b) | ~a
spatel accepted this revision.Dec 15 2021, 8:07 AM

LGTM - sorry for the delay.
As with the previous patch in this set, it's tough to follow all of the commute/use cases, but we've established that the logic is correct.

This revision is now accepted and ready to land.Dec 15 2021, 8:07 AM
This revision was landed with ongoing or failed builds.Dec 15 2021, 9:37 AM
This revision was automatically updated to reflect the committed changes.