-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[SelectionDAG] Optimize BSWAP yet again once more #165292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AZero13
wants to merge
4
commits into
llvm:main
Choose a base branch
from
AZero13:bswaps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,6 +118,7 @@ using namespace llvm; | |
| #define DEBUG_TYPE "arm-isel" | ||
|
|
||
| STATISTIC(NumTailCalls, "Number of tail calls"); | ||
| STATISTIC(NumOptimizedImms, "Number of times immediates were optimized"); | ||
| STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt"); | ||
| STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments"); | ||
| STATISTIC(NumConstpoolPromoted, | ||
|
|
@@ -142,6 +143,12 @@ static cl::opt<unsigned> ConstpoolPromotionMaxTotal( | |
| cl::desc("Maximum size of ALL constants to promote into a constant pool"), | ||
| cl::init(128)); | ||
|
|
||
| static cl::opt<bool> | ||
| EnableOptimizeLogicalImm("arm-enable-logical-imm", cl::Hidden, | ||
| cl::desc("Enable ARM logical imm instruction " | ||
| "optimization"), | ||
| cl::init(true)); | ||
|
|
||
| cl::opt<unsigned> | ||
| MVEMaxSupportedInterleaveFactor("mve-max-interleave-factor", cl::Hidden, | ||
| cl::desc("Maximum interleave factor for MVE VLDn to generate."), | ||
|
|
@@ -20138,6 +20145,16 @@ void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, | |
| } | ||
| } | ||
|
|
||
| static bool isLegalLogicalImmediate(unsigned Imm, | ||
| const ARMSubtarget *Subtarget) { | ||
| if (!Subtarget->isThumb()) | ||
| return ARM_AM::getSOImmVal(Imm) != -1; | ||
| if (Subtarget->isThumb2()) | ||
| return ARM_AM::getT2SOImmVal(Imm) != -1; | ||
| // Thumb1 only has 8-bit unsigned immediate. | ||
| return Imm <= 255; | ||
| } | ||
|
|
||
| bool ARMTargetLowering::targetShrinkDemandedConstant( | ||
| SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, | ||
| TargetLoweringOpt &TLO) const { | ||
|
|
@@ -20146,8 +20163,7 @@ bool ARMTargetLowering::targetShrinkDemandedConstant( | |
| if (!TLO.LegalOps) | ||
| return false; | ||
|
|
||
| // Only optimize AND for now. | ||
| if (Op.getOpcode() != ISD::AND) | ||
| if (!EnableOptimizeLogicalImm) | ||
| return false; | ||
|
|
||
| EVT VT = Op.getValueType(); | ||
|
|
@@ -20158,28 +20174,28 @@ bool ARMTargetLowering::targetShrinkDemandedConstant( | |
|
|
||
| assert(VT == MVT::i32 && "Unexpected integer type"); | ||
|
|
||
| // Exit early if we demand all bits. | ||
| if (DemandedBits.popcount() == 32) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isAllOnes?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just the copy paste of the logic in AArch64 |
||
| return false; | ||
|
|
||
| // Only optimize AND for now. | ||
| if (Op.getOpcode() != ISD::AND) | ||
| return false; | ||
|
|
||
| // Make sure the RHS really is a constant. | ||
| ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); | ||
| if (!C) | ||
| return false; | ||
|
|
||
| unsigned Mask = C->getZExtValue(); | ||
|
|
||
| if (Mask == 0 || Mask == ~0U) | ||
| return false; | ||
|
|
||
| unsigned Demanded = DemandedBits.getZExtValue(); | ||
| unsigned ShrunkMask = Mask & Demanded; | ||
| unsigned ExpandedMask = Mask | ~Demanded; | ||
|
|
||
| // If the mask is all zeros, let the target-independent code replace the | ||
| // result with zero. | ||
| if (ShrunkMask == 0) | ||
| return false; | ||
|
|
||
| // If the mask is all ones, erase the AND. (Currently, the target-independent | ||
| // code won't do this, so we have to do it explicitly to avoid an infinite | ||
| // loop in obscure cases.) | ||
| if (ExpandedMask == ~0U) | ||
| return TLO.CombineTo(Op, Op.getOperand(0)); | ||
|
|
||
| auto IsLegalMask = [ShrunkMask, ExpandedMask](unsigned Mask) -> bool { | ||
| return (ShrunkMask & Mask) == ShrunkMask && (~ExpandedMask & Mask) == 0; | ||
| }; | ||
|
|
@@ -20192,30 +20208,61 @@ bool ARMTargetLowering::targetShrinkDemandedConstant( | |
| return TLO.CombineTo(Op, NewOp); | ||
| }; | ||
|
|
||
| // Prefer uxtb mask. | ||
| if (IsLegalMask(0xFF)) | ||
| return UseMask(0xFF); | ||
| // If the mask is all zeros, let the target-independent code replace the | ||
| // result with zero. | ||
| if (ShrunkMask == 0) { | ||
| ++NumOptimizedImms; | ||
| return UseMask(ShrunkMask); | ||
| } | ||
|
|
||
| // Prefer uxth mask. | ||
| if (IsLegalMask(0xFFFF)) | ||
| return UseMask(0xFFFF); | ||
| // If the mask is all ones, erase the AND. (Currently, the target-independent | ||
| // code won't do this, so we have to do it explicitly to avoid an infinite | ||
| // loop in obscure cases.) | ||
| if (ExpandedMask == ~0U) { | ||
| ++NumOptimizedImms; | ||
| return UseMask(ExpandedMask); | ||
| } | ||
|
|
||
| // [1, 255] is Thumb1 movs+ands, legal immediate for ARM/Thumb2. | ||
| // FIXME: Prefer a contiguous sequence of bits for other optimizations. | ||
| if (ShrunkMask < 256) | ||
| // If thumb, check for uxth and uxtb masks first and foremost. | ||
| if (Subtarget->isThumb1Only() && Subtarget->hasV6Ops()) { | ||
| if (IsLegalMask(0xFF)) { | ||
| ++NumOptimizedImms; | ||
| return UseMask(0xFF); | ||
| } | ||
|
|
||
| if (IsLegalMask(0xFFFF)) { | ||
| ++NumOptimizedImms; | ||
| return UseMask(0xFFFF); | ||
| } | ||
| } | ||
|
|
||
| // Don't optimize if it is legal already. | ||
| if (isLegalLogicalImmediate(Mask, Subtarget)) | ||
| return false; | ||
|
|
||
| if (isLegalLogicalImmediate(ShrunkMask, Subtarget)) { | ||
| ++NumOptimizedImms; | ||
| return UseMask(ShrunkMask); | ||
| } | ||
|
|
||
| // [-256, -2] is Thumb1 movs+bics, legal immediate for ARM/Thumb2. | ||
| // FIXME: Prefer a contiguous sequence of bits for other optimizations. | ||
| if ((int)ExpandedMask <= -2 && (int)ExpandedMask >= -256) | ||
| // FIXME: The check for v6 is because this interferes with some ubfx | ||
| // optimizations | ||
| if (!Subtarget->hasV6Ops() && | ||
| isLegalLogicalImmediate(~ExpandedMask, Subtarget)) { | ||
| ++NumOptimizedImms; | ||
| return UseMask(ExpandedMask); | ||
| } | ||
|
|
||
| if ((~ExpandedMask) < 256) { | ||
| ++NumOptimizedImms; | ||
| return UseMask(ExpandedMask); | ||
| } | ||
|
|
||
| // Potential improvements: | ||
| // | ||
| // We could try to recognize lsls+lsrs or lsrs+lsls pairs here. | ||
| // We could try to prefer Thumb1 immediates which can be lowered to a | ||
| // two-instruction sequence. | ||
| // We could try to recognize more legal ARM/Thumb2 immediates here. | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too specific, just don't add this?