-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[RISCV] Add isel optimization for (and (sra y, c2), c1) to recover regression from #101751. #104114
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ac2de5
[RISCV] Add isel optimization for (add x, (and (sra y, c2), c1)) to r…
topperc 02e8345
fixup! fix typo in comment
topperc 76711df
fixup! Add non-Zba case too.
topperc 3962d50
fixup! address review comment
topperc b522885
fixup! Ensure there are leading zeros.
topperc abe4728
fixup! add tests
topperc 56ab9cd
fixup! fix CHECK line.
topperc e96b8f3
fixup! Add Leading > 0 check to selectSHXADDOp too.
topperc 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 |
---|---|---|
|
@@ -1451,8 +1451,6 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) { | |
|
||
const uint64_t C1 = N1C->getZExtValue(); | ||
|
||
// Turn (and (sra x, c2), c1) -> (srli (srai x, c2-c3), c3) if c1 is a mask | ||
// with c3 leading zeros and c2 is larger than c3. | ||
if (N0.getOpcode() == ISD::SRA && isa<ConstantSDNode>(N0.getOperand(1)) && | ||
N0.hasOneUse()) { | ||
unsigned C2 = N0.getConstantOperandVal(1); | ||
|
@@ -1466,6 +1464,8 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) { | |
X.getOpcode() == ISD::SHL && | ||
isa<ConstantSDNode>(X.getOperand(1)) && | ||
X.getConstantOperandVal(1) == 32; | ||
// Turn (and (sra x, c2), c1) -> (srli (srai x, c2-c3), c3) if c1 is a | ||
// mask with c3 leading zeros and c2 is larger than c3. | ||
if (isMask_64(C1) && !Skip) { | ||
unsigned Leading = XLen - llvm::bit_width(C1); | ||
if (C2 > Leading) { | ||
|
@@ -1479,6 +1479,27 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) { | |
return; | ||
} | ||
} | ||
|
||
// Look for (and (sra y, c2), c1) where c1 is a shifted mask with c3 | ||
// leading zeros and c4 trailing zeros. If c2 is greater than c3, we can | ||
// use (slli (srli (srai y, c2 - c3), c3 + c4), c4). | ||
if (isShiftedMask_64(C1) && !Skip) { | ||
unsigned Leading = XLen - llvm::bit_width(C1); | ||
unsigned Trailing = llvm::countr_zero(C1); | ||
if (C2 > Leading && Leading > 0 && Trailing > 0) { | ||
SDNode *SRAI = CurDAG->getMachineNode( | ||
RISCV::SRAI, DL, VT, N0.getOperand(0), | ||
CurDAG->getTargetConstant(C2 - Leading, DL, VT)); | ||
SDNode *SRLI = CurDAG->getMachineNode( | ||
RISCV::SRLI, DL, VT, SDValue(SRAI, 0), | ||
CurDAG->getTargetConstant(Leading + Trailing, DL, VT)); | ||
SDNode *SLLI = CurDAG->getMachineNode( | ||
RISCV::SLLI, DL, VT, SDValue(SRLI, 0), | ||
CurDAG->getTargetConstant(Trailing, DL, VT)); | ||
ReplaceNode(Node, SLLI); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// If C1 masks off the upper bits only (but can't be formed as an | ||
|
@@ -3019,6 +3040,33 @@ bool RISCVDAGToDAGISel::selectSHXADDOp(SDValue N, unsigned ShAmt, | |
return true; | ||
} | ||
} | ||
} else if (N0.getOpcode() == ISD::SRA && N0.hasOneUse() && | ||
isa<ConstantSDNode>(N.getOperand(1))) { | ||
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.
|
||
uint64_t Mask = N.getConstantOperandVal(1); | ||
unsigned C2 = N0.getConstantOperandVal(1); | ||
|
||
// Look for (and (sra y, c2), c1) where c1 is a shifted mask with c3 | ||
// leading zeros and c4 trailing zeros. If c2 is greater than c3, we can | ||
// use (srli (srai y, c2 - c3), c3 + c4) followed by a SHXADD with c4 as | ||
// the X amount. | ||
if (isShiftedMask_64(Mask)) { | ||
unsigned XLen = Subtarget->getXLen(); | ||
unsigned Leading = XLen - llvm::bit_width(Mask); | ||
unsigned Trailing = llvm::countr_zero(Mask); | ||
if (C2 > Leading && Leading > 0 && Trailing == ShAmt) { | ||
SDLoc DL(N); | ||
EVT VT = N.getValueType(); | ||
Val = SDValue(CurDAG->getMachineNode( | ||
RISCV::SRAI, DL, VT, N0.getOperand(0), | ||
CurDAG->getTargetConstant(C2 - Leading, DL, VT)), | ||
0); | ||
Val = SDValue(CurDAG->getMachineNode( | ||
RISCV::SRLI, DL, VT, Val, | ||
CurDAG->getTargetConstant(Leading + ShAmt, DL, VT)), | ||
0); | ||
return true; | ||
} | ||
} | ||
} | ||
} else if (bool LeftShift = N.getOpcode() == ISD::SHL; | ||
(LeftShift || N.getOpcode() == ISD::SRL) && | ||
|
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.
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.
Uh oh!
There was an error while loading. Please reload this page.