This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] isSeveralBitsExtractOpFromShr - match UBFM patterns with value tracking (RFC)
AbandonedPublic

Authored by RKSimon on Jun 12 2022, 9:21 AM.

Details

Reviewers
dmgreen
efriedma
Summary

This came about while I was investigating the lost bit extractions on D125836 - it didn't help but is a more general implementation than just matching AND masks.

With a bit more work I'm wondering how many of the 'isBitfieldExtractOp' patterns could actually be performed with just value tracking and SimplifyMultipleUseDemandedBits.

Diff Detail

Event Timeline

RKSimon created this revision.Jun 12 2022, 9:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 12 2022, 9:21 AM
RKSimon requested review of this revision.Jun 12 2022, 9:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 12 2022, 9:21 AM
efriedma added inline comments.Jun 13 2022, 4:37 PM
llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
1958

The pattern we're looking for is SRL+AND. The modified version of the function doesn't explicitly check for the "AND" operation at all. I guess you can describe "AND" as "an operation with known zero bits in the result, and can be eliminated by SimplifyMultipleUseDemandedBits", but that seems like an awfully confusing description.

How does this change actually generalize the matching in practice? Are we just looking for different AND masks, or can we actually match operations which aren't AND?

Maybe it would make the result easier to read if we extract the code that checks for the masking operation into a separate "MatchMaskOperation" function?

RKSimon added inline comments.Jun 14 2022, 1:54 AM
llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
1958

Yes its most likely only ever going to match AND masks.

This was an experiment as a lot of recent SimplifyDemandedBits work (e.g. D125836) has managed to break the fragile bit extract patterns, and I was curious how general value tracking and demanded bits handling would work - although we're limited on how much we can manipulate the DAG at this late stage. I was curious what others thought, so I put up this RFC.

Maybe the aarch64 backend just needs additional canonicalization for some UBFX/SBFX patterns?

efriedma added inline comments.Jun 16 2022, 12:45 PM
llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
1958

DAGCombine likes to manipulate basic arithmetic operations, so it's sort of hard to canonicalize them... but maybe some sort of canonicalization would be helpful. We do already have AArch64TargetLowering::isDesirableToCommuteWithShift.

I haven't really thought about the ideal canonical form.

RKSimon abandoned this revision.Jun 20 2022, 3:51 AM

D125836 tweaks AArch64TargetLowering::shouldFoldConstantShiftPairToMask to ensure we don't mess with srl(shl(x,c1),c2) if it matches a UBFX pattern.

Abandoning this for now, but we could revive this in the future if it ever becomes useful (I also noticed that ARM has some similar matching). Bit extraction instructions are common enough that a generic opcode could even be considered....