-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[RISCV] Fold vector shift of sext/zext to widening multiply #121563
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
15412e7
[RISCV] Fold vector shift of sext/zext to widening multiply
pfusik 5aa9da4
[RISCV] Fold vector shift to widening multiply on rv32
pfusik 1868507
[RISCV] Use `getSimpleValueType`
pfusik 3a59255
[RISCV] Add a TODO comment
pfusik 174e59e
[RISCV] Handle fixed-length vectors
pfusik fa18fca
[RISCV] Handle VSEXT_VL/VZEXT_VL
pfusik 0da5f8c
[RISCV] Delay the transform so that vwsll has priority
pfusik d84396c
[RISCV] Remove dead code handling fixed-length vectors after DAG lega…
pfusik 6df5335
[RISCV] Fix formatting
pfusik 63f902d
[RISCV] Disable this transform by default, except for SpacemiT X60
pfusik b243fbc
[RISCV] Rename function for consistency
pfusik bddd7fd
Revert "[RISCV] Disable this transform by default, except for Spacemi…
pfusik 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17341,6 +17341,96 @@ static SDValue combineScalarCTPOPToVCPOP(SDNode *N, SelectionDAG &DAG, | |
| return DAG.getZExtOrTrunc(Pop, DL, VT); | ||
| } | ||
|
|
||
| static SDValue combineSHL(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, | ||
| const RISCVSubtarget &Subtarget) { | ||
| if (DCI.isBeforeLegalize()) | ||
| return SDValue(); | ||
|
|
||
| // (shl (zext x), y) -> (vwsll x, y) | ||
| if (SDValue V = combineOp_VLToVWOp_VL(N, DCI, Subtarget)) | ||
| return V; | ||
|
|
||
| // (shl (sext x), C) -> (vwmulsu x, 1u << C) | ||
| // (shl (zext x), C) -> (vwmulu x, 1u << C) | ||
|
|
||
| SDValue LHS = N->getOperand(0); | ||
| if (!LHS.hasOneUse()) | ||
| return SDValue(); | ||
|
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. For now, this only handles single-use of sext/zext. |
||
| unsigned Opcode; | ||
| switch (LHS.getOpcode()) { | ||
| case ISD::SIGN_EXTEND: | ||
lukel97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Opcode = RISCVISD::VWMULSU_VL; | ||
| break; | ||
| case ISD::ZERO_EXTEND: | ||
| Opcode = RISCVISD::VWMULU_VL; | ||
| break; | ||
| // TODO: | ||
| // case RISCVISD::VSEXT_VL: | ||
| // case RISCVISD::VZEXT_VL: | ||
| default: | ||
| return SDValue(); | ||
| } | ||
|
|
||
| SDValue RHS = N->getOperand(1); | ||
| APInt ShAmt; | ||
| uint64_t ShAmtInt; | ||
| if (ISD::isConstantSplatVector(RHS.getNode(), ShAmt)) | ||
| ShAmtInt = ShAmt.getZExtValue(); | ||
| else if (RHS.getOpcode() == RISCVISD::VMV_V_X_VL && | ||
| RHS.getOperand(1).getOpcode() == ISD::Constant) | ||
| ShAmtInt = RHS.getConstantOperandVal(1); | ||
| else | ||
| return SDValue(); | ||
|
|
||
| // Better foldings: | ||
| // (shl (sext x), 1) -> (vwadd x, x) | ||
| // (shl (zext x), 1) -> (vwaddu x, x) | ||
| if (ShAmtInt <= 1) | ||
| return SDValue(); | ||
|
|
||
| SDValue NarrowOp = LHS.getOperand(0); | ||
| MVT NarrowVT = NarrowOp.getSimpleValueType(); | ||
| uint64_t NarrowBits = NarrowVT.getScalarSizeInBits(); | ||
| if (ShAmtInt >= NarrowBits) | ||
| return SDValue(); | ||
| MVT VT = N->getSimpleValueType(0); | ||
| if (NarrowBits * 2 != VT.getScalarSizeInBits()) | ||
| return SDValue(); | ||
|
|
||
| SelectionDAG &DAG = DCI.DAG; | ||
| MVT NarrowContainerVT = NarrowVT; | ||
| MVT ContainerVT = VT; | ||
| SDLoc DL(N); | ||
| SDValue Passthru, Mask, VL; | ||
| switch (N->getOpcode()) { | ||
| case ISD::SHL: | ||
| if (VT.isFixedLengthVector()) { | ||
| NarrowContainerVT = | ||
| getContainerForFixedLengthVector(DAG, NarrowVT, Subtarget); | ||
| NarrowOp = | ||
| convertToScalableVector(NarrowContainerVT, NarrowOp, DAG, Subtarget); | ||
| ContainerVT = getContainerForFixedLengthVector(DAG, VT, Subtarget); | ||
| } | ||
| Passthru = DAG.getUNDEF(VT); | ||
lukel97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget); | ||
| break; | ||
| case RISCVISD::SHL_VL: | ||
| Passthru = N->getOperand(2); | ||
| Mask = N->getOperand(3); | ||
| VL = N->getOperand(4); | ||
| break; | ||
| default: | ||
| llvm_unreachable("Expected SHL"); | ||
| } | ||
| SDValue Mul = | ||
| DAG.getNode(Opcode, DL, ContainerVT, NarrowOp, | ||
| DAG.getConstant(1ULL << ShAmtInt, SDLoc(RHS), ContainerVT), | ||
| Passthru, Mask, VL); | ||
| if (VT.isFixedLengthVector()) | ||
| return convertFromScalableVector(VT, Mul, DAG, Subtarget); | ||
| return Mul; | ||
| } | ||
|
|
||
| SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N, | ||
| DAGCombinerInfo &DCI) const { | ||
| SelectionDAG &DAG = DCI.DAG; | ||
|
|
@@ -17970,7 +18060,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N, | |
| break; | ||
| } | ||
| case RISCVISD::SHL_VL: | ||
| if (SDValue V = combineOp_VLToVWOp_VL(N, DCI, Subtarget)) | ||
| if (SDValue V = combineSHL(N, DCI, Subtarget)) | ||
| return V; | ||
| [[fallthrough]]; | ||
| case RISCVISD::SRA_VL: | ||
|
|
@@ -17995,7 +18085,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N, | |
| case ISD::SRL: | ||
| case ISD::SHL: { | ||
| if (N->getOpcode() == ISD::SHL) { | ||
| if (SDValue V = combineOp_VLToVWOp_VL(N, DCI, Subtarget)) | ||
| if (SDValue V = combineSHL(N, DCI, Subtarget)) | ||
| return V; | ||
| } | ||
| SDValue ShAmt = N->getOperand(1); | ||
|
|
||
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.
The naming convention in this file would have this routine named performSHLCombine
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.
Renamed, thanks!