This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Enhance bit-field-positioning op matcher to see through "any_extend" for pattern "and(any_extend(shl(val, N)), shifted-mask)"
ClosedPublic

Authored by mingmingl on Oct 12 2022, 11:24 PM.

Details

Summary

Before this patch (and refactor patch D135843), isBitfieldPositioningOp won't handle and(any_extend(shl(val, N), shifted-mask) (bail out if AND op is not SHL)

After this patch, isBitfieldPositioningOp will see through any_extend to find shl to find possible bit-field-positioning nodes.

https://gcc.godbolt.org/z/3ncGKbGW6 is a four-liner LLVM IR that could be optimized to UBFIZ (see added test case test_and_extended_shift_with_imm in llvm/test/CodeGen/AArch64/bitfield-insert.ll). One existing test case also improves.

Diff Detail

Event Timeline

mingmingl created this revision.Oct 12 2022, 11:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2022, 11:24 PM
mingmingl requested review of this revision.Oct 12 2022, 11:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2022, 11:24 PM
mingmingl retitled this revision from [AArch64]Enhance bit-field-positioning op matcher to find out (and(shl(val,N), shifted-mask) to [AArch64][4/4] Enhance bit-field-positioning op matcher to see through "any_extend" for pattern "and(any_extend(shl(val, N)), shifted-mask)".Oct 13 2022, 12:27 AM
mingmingl edited the summary of this revision. (Show Details)
mingmingl added a reviewer: dmgreen.
dmgreen added inline comments.
llvm/test/CodeGen/AArch64/bitfield-insert.ll
598

This is nice. I've seen this come up before. https://godbolt.org/z/hhhTKex4d

mingmingl updated this revision to Diff 468084.Oct 16 2022, 9:38 AM
mingmingl marked an inline comment as done.
mingmingl retitled this revision from [AArch64][4/4] Enhance bit-field-positioning op matcher to see through "any_extend" for pattern "and(any_extend(shl(val, N)), shifted-mask)" to [AArch64] Enhance bit-field-positioning op matcher to see through "any_extend" for pattern "and(any_extend(shl(val, N)), shifted-mask)".

Changes:

  1. Did some changes on local commit log to use D135843 (the refactor) as parent revision (previous A -> B -> C -> D, now A -> D), in a way that after A is upstreamed this one could be rebased.
  2. Minor comments update, no functional change.
llvm/test/CodeGen/AArch64/bitfield-insert.ll
598

Yes, and the 2nd function in https://godbolt.org/z/hhhTKex4d are optimized to UBFIZ with this patch.

dmgreen added inline comments.Oct 17 2022, 3:04 AM
llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
2585

Can we create a new variable for the temporary SDValue?

2589

Add an assert that the type of ShlVal is i32?

2594

-> Widens

2615

I think this will always be a MVT::i64 or MVT::i32?

2617

Do we have any tests where this applies?

mingmingl marked 5 inline comments as done.

address comments.

llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
2585

Renamed this to 'ShlOp0'

2589

Add the type assertion and comment for why ShlVal type is i32 (based on my understanding).

2615

Yes, I agree this should always be i64 or i32 after reading the comment and think about it.

p.s. if I understand correctly, this is because

  1. this function gets called during selection (i.e., after legalization) and caller verifies i64 or i32 above; &&
  2. AArch64 general purpose registers are for 32 or 64 bit integers, and SimpleVT legalization (from target-lowering-base) class is based on register info.
2617

This is a good point. I think the answer is no (or at least very rare if any) -> changed implementation to bail out if Width >= VT.getSizeInBits() and added comment.

Longer story is that, if AndImm (as a superset of shifted-mask 'NonZeroBits`) from and(op, AndImm) requires more than 32 bits after proper dag combination, the op shouldn't be lowered to any_extend (smaller-type op)

  • the higher bits are undefined after any_extend, so previous combination should at least realize the higher 32 bits and results don't really matter.
dmgreen accepted this revision.Oct 18 2022, 1:16 AM

LGTM

llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp
2615

Yep. Sounds good.

2617

There are quite a lot of words here for "we don't expect this to happen but it would be bad if it did" :)

This revision is now accepted and ready to land.Oct 18 2022, 1:16 AM
mingmingl updated this revision to Diff 468548.Oct 18 2022, 8:07 AM
mingmingl marked 2 inline comments as done.

Diff: reworded lengthy comment for why bailing out upon Width >= VT.getSizeInBits. No other change.

Thanks for the reviews and feedback!

This revision was landed with ongoing or failed builds.Oct 18 2022, 9:08 AM
This revision was automatically updated to reflect the committed changes.