This is an archive of the discontinued LLVM Phabricator instance.

[DAG] Allow XOR(X,MIN_SIGNED_VALUE) to perform AddLike folds
ClosedPublic

Authored by RKSimon on Mar 30 2022, 11:36 AM.

Details

Summary

As raised on PR52267, XOR(X,MIN_SIGNED_VALUE) can be treated as ADD(X,MIN_SIGNED_VALUE), so let these cases use the 'AddLike' folds, similar to how we perform no-common-bits OR(X,Y) cases.

define i8 @src(i8 %x) {
%0:
  %r = xor i8 %x, 128
  ret i8 %r
}
=>
define i8 @tgt(i8 %x) {
%0:
  %r = add i8 %x, 128
  ret i8 %r
}
Transformation seems to be correct!

https://alive2.llvm.org/ce/z/qV46E2

Diff Detail

Event Timeline

RKSimon created this revision.Mar 30 2022, 11:36 AM
RKSimon requested review of this revision.Mar 30 2022, 11:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 30 2022, 11:36 AM

What about (add (xor X, MIN_SIGNED), C). Should we check for the XOR inside of visitAddLike for constant folding? I believe we check for ISD::OR with no common bits in there. I'm fine if you want to do it as a separate patch.

RKSimon updated this revision to Diff 419398.Mar 31 2022, 4:46 AM

Add isAddLike() helper to match addlike(addlike(x,y),z) patterns

RKSimon added inline comments.Mar 31 2022, 4:49 AM
llvm/test/CodeGen/X86/movmsk-cmp.ll
4548

Looks like we're missing this fold, but I haven't had time to investigate yet:

----------------------------------------
define i1 @src(i8 %a) {
%0:
  %x = xor i8 %a, 1
  %r = icmp eq i8 %x, 2
  ret i1 %r
}
=>
define i1 @tgt(i8 %a) {
%0:
  %r = icmp eq i8 %a, 3
  ret i1 %r
}
Transformation seems to be correct!
RKSimon updated this revision to Diff 420741.Apr 6 2022, 1:52 AM

rebase - regressions are now fixed

lebedev.ri accepted this revision.Apr 6 2022, 2:07 AM

Seems fine to me.

This revision is now accepted and ready to land.Apr 6 2022, 2:07 AM
This revision was landed with ongoing or failed builds.Apr 6 2022, 2:37 AM
This revision was automatically updated to reflect the committed changes.