This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC][Peephole] Combine rldicl/rldicr and andi/andis after isel.
ClosedPublic

Authored by Esme on Aug 29 2023, 2:19 AM.

Details

Summary

rldicl/rldicr can be eliminated if it's used to clear the high-order or low-order n bits and all bits cleared will be ANDed with 0 by andi/andis.
This patch optimizes such pattern.

Diff Detail

Event Timeline

Esme created this revision.Aug 29 2023, 2:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 2:19 AM
Esme requested review of this revision.Aug 29 2023, 2:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 2:19 AM
Esme updated this revision to Diff 556764.EditedSep 14 2023, 3:25 AM
Esme retitled this revision from [PowerPC][Peephole] Combine rldicl/rldicr and andi after isel. to [PowerPC][Peephole] Combine rldicl/rldicr and andi/andis after isel..
Esme edited the summary of this revision. (Show Details)
  1. Add optimizations for ANDIS_rec.
  2. Add mir tests.
qiucf accepted this revision as: qiucf.Sep 18 2023, 3:43 AM

LGTM, thanks

This revision is now accepted and ready to land.Sep 18 2023, 3:43 AM
shchenz added inline comments.Sep 19 2023, 2:04 AM
llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
1287

So we should guard this optimization before RA?

1313

A follow up: is it possible to get result 0 for the ANDI if we know RLDICR clears more than andi masks, or for the ANDIS, if we know RLDICL leading 0 count is not smaller than 48? There should be other combinations... And if we are going to handle this case, maybe SHSrc does not need to be 0.

Esme updated this revision to Diff 557157.Sep 20 2023, 10:22 PM

Addressed comments -- handled more cases.

llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
1287

Yes, I do guard it by

if (!TrueReg.isVirtual())
         break;
1313

Good point! In such scenarios we can eliminate ANDI_rec/ANDIS_rec instead of RLDICL/RLDICR and replace RLDICL/RLDICR with their record form RLDICL_rec/RLDICR_rec.

Esme added a comment.Sep 24 2023, 10:18 PM

This patch passed Spec CPU2017 and bootstrap test.

shchenz added inline comments.Sep 25 2023, 12:05 AM
llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
1299

If we know that all bits to AND are already zero, why not we can just delete the RLDICL/RLDICR, and set the operand 2 of ANDI/ANDIS to 0 to indicate that ANDI/ANDIS products 0 result here? To me that would be more simple and later pass may have the opportunity to do more optimizations based on the 0?

Esme updated this revision to Diff 557295.Sep 25 2023, 2:07 AM

Thanks a lot to @shchenz !
Addressed comments.

LGTM with one nit.

Thanks for improving this.

llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
1299

nit: PatternResultZero & PatternRemoveRotate for Pattern1 and Pattern2?

shchenz accepted this revision as: shchenz.Sep 25 2023, 2:43 AM
This revision was landed with ongoing or failed builds.Sep 25 2023, 8:12 PM
This revision was automatically updated to reflect the committed changes.
Esme added inline comments.Sep 25 2023, 8:20 PM
llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
1247

JTBC. The patch was committed with addRegToUpdate() after rebasing on https://reviews.llvm.org/D133103.