This is an archive of the discontinued LLVM Phabricator instance.

[DAG] SimplifyDemandedBits - relax "xor (X >> ShiftC), XorC --> (not X) >> ShiftC" to match only demanded bits
ClosedPublic

Authored by RKSimon on Jul 16 2022, 5:13 AM.

Details

Summary

The "xor (X >> ShiftC), XorC --> (not X) >> ShiftC" fold is currently limited to the XOR mask being a shifted all-bits mask, but we can relax this to only need to match under the demanded bits.

This helps expose more bit extraction/clearing patterns and fixes the PowerPC testCompares*.ll regressions from D127115

Alive2: https://alive2.llvm.org/ce/z/fl7T7K

Diff Detail

Event Timeline

RKSimon created this revision.Jul 16 2022, 5:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2022, 5:13 AM
RKSimon requested review of this revision.Jul 16 2022, 5:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2022, 5:13 AM
RKSimon added inline comments.Jul 16 2022, 5:18 AM
llvm/test/CodeGen/AArch64/selectcc-to-shiftand.ll
81 ↗(On Diff #445229)

I don't think this will appear in DAG, and Instcombine will fold this to a xor-shift-mask pattern: https://gcc.godbolt.org/z/nMdqeaGYh

Do people think this is going to need a TLI callback ?

Do people think this is going to need a TLI callback ?

We might be able to adjust visitShiftByConstant + isDesirableToCommuteWithShift to handle this - I'll take a look.

We might be able to adjust visitShiftByConstant + isDesirableToCommuteWithShift to handle this - I'll take a look.

That might be best - this does seem to come up in practice. For example:

define i32 @test(i32 %conv73, i32 %i63.0) {
  %and74 = shl i32 %i63.0, 2
  %sub75 = and i32 %and74, 4
  %shl76 = xor i32 %sub75, 4
  ret i32 %shl76
}
movs    r0, #4               |   42         mvns    r0, r1            
bic.w   r0, r0, r1, lsl #2   |   43         movs    r1, #4            
-----------------------------|   44         and.w   r0, r1, r0, lsl #2
RKSimon updated this revision to Diff 445491.Jul 18 2022, 7:29 AM

Hide the fold behind a isDesirableToCommuteXorWithShift TLI override (default - true)

RKSimon added inline comments.Jul 18 2022, 7:31 AM
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
13633

I'm not totally happy that we have all this (duplicated) logic in the arm/aarch64 overrides - an alternative would be to add a 'bool IsPartialMatch' argument?

dmgreen accepted this revision.Jul 18 2022, 11:54 PM

I think it looks OK. LGTM. Thanks for the changes

This revision is now accepted and ready to land.Jul 18 2022, 11:54 PM
This revision was landed with ongoing or failed builds.Jul 19 2022, 3:01 AM
This revision was automatically updated to reflect the committed changes.