This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Fold lsr+bfi in tryBitfieldInsertOpFromOr
ClosedPublic

Authored by bcl5980 on Apr 1 2022, 9:34 AM.

Details

Summary

In tryBitfieldInsertOpFromOr, if the new created LSR Node's source is LSR with Imm shift, try to fold them into one LSR.

Fix https://github.com/llvm/llvm-project/issues/54696

Diff Detail

Event Timeline

bcl5980 created this revision.Apr 1 2022, 9:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 1 2022, 9:34 AM
bcl5980 requested review of this revision.Apr 1 2022, 9:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 1 2022, 9:34 AM
bcl5980 edited the summary of this revision. (Show Details)Apr 1 2022, 9:37 AM
bcl5980 edited the summary of this revision. (Show Details)

If we have better solution on IR or SelectionDAG I will abandon the patch

It looks like the lsr+bfi is coming out of tryBitfieldInsertOpFromOr in AArch64ISelDAGToDAG.cpp. I see two possible alternatives:

  1. Enhance tryBitfieldInsertOpFromOr to detect when it's shifting a shift.
  2. Form the BFM op as a DAGCombine, instead of waiting for isel.

This MI peephole seems unlikely to be useful outside of this one case, so I think I'd prefer to fix the existing logic.

I would prefer AArch64 specific DAGCombine.

I would prefer AArch64 specific DAGCombine.

  1. Form the BFM op as a DAGCombine, instead of waiting for isel.

For now test case's selection DAG before instruction select is:

t0: ch = EntryToken
t2: i32,ch = CopyFromReg t0, Register:i32 %0
      t7: i32 = srl t2, Constant:i64<16>
    t9: i32 = and t7, Constant:i32<240>
    t4: i32 = and t2, Constant:i32<-241>
  t10: i32 = or t9, t4
t12: ch,glue = CopyToReg t0, Register:i32 $w0, t10
t13: ch = AArch64ISD::RET_FLAG t12, Register:i32 $w0, t12:1

Do you mean we add a new AArch64ISD like AArch64ISD::BFM, then form AArch64ISD::BFM in performOrCombine ?

bcl5980 updated this revision to Diff 420516.Apr 5 2022, 8:21 AM

Fix #54696 in tryBitfieldInsertOpFromOr

bcl5980 retitled this revision from [AArch64] UBFM fold in the MIPeepholeOpt. to [AArch64] Fold lsr+bfi in tryBitfieldInsertOpFromOr.Apr 5 2022, 8:24 AM
bcl5980 edited the summary of this revision. (Show Details)
bcl5980 added a comment.EditedApr 5 2022, 8:27 AM

It looks like the lsr+bfi is coming out of tryBitfieldInsertOpFromOr in AArch64ISelDAGToDAG.cpp. I see two possible alternatives:

  1. Enhance tryBitfieldInsertOpFromOr to detect when it's shifting a shift.
  2. Form the BFM op as a DAGCombine, instead of waiting for isel.

This MI peephole seems unlikely to be useful outside of this one case, so I think I'd prefer to fix the existing logic.

@efriedma is the new patch match solution 1?
And if possible can you help to explain solution 2?
I'm a beginner on llvm so it will be grateful if you can tell me the detail solution.

bcl5980 updated this revision to Diff 420524.Apr 5 2022, 8:34 AM
efriedma accepted this revision.Apr 5 2022, 3:47 PM

Yes, this is my "solution 1". "Solution 2" would be defining AArch64ISD::BFM, and moving all the code from tryBitfieldInsertOpFromOr into a DAGCombine. The advantage of that is that we can perform general DAGCombine optimizations afterwards, not just this specific one. The disadvantage is that you have to be careful not to block other optimizations. (And it's more work to modify the current code.)

@benshi001 any further thoughts?

This revision is now accepted and ready to land.Apr 5 2022, 3:47 PM

Yes, this is my "solution 1". "Solution 2" would be defining AArch64ISD::BFM, and moving all the code from tryBitfieldInsertOpFromOr into a DAGCombine. The advantage of that is that we can perform general DAGCombine optimizations afterwards, not just this specific one. The disadvantage is that you have to be careful not to block other optimizations. (And it's more work to modify the current code.)

Thanks for the detail explaination.

benshi001 accepted this revision.Apr 5 2022, 9:59 PM

I have no commit access so can someone help me to check in the code ?
name: chenglin.bi
email: chenglin.bi@cixcomputing.com

This revision was landed with ongoing or failed builds.Apr 6 2022, 7:02 AM
This revision was automatically updated to reflect the committed changes.

https://github.com/llvm/llvm-project/commit/87f0d55304a27ce0f6178eed65d8dad49b5dcfd9

You need to add your working email to your github account, to show your commit properly.

https://github.com/llvm/llvm-project/commit/87f0d55304a27ce0f6178eed65d8dad49b5dcfd9

You need to add your working email to your github account, to show your commit properly.

Thanks for mentioning it