Skip to content

Commit 15895da

Browse files
authored
[RISCV] Limit (and (sra x, c2), c1) -> (srli (srai x, c2-c3), c3) isel in some cases. (#102034)
If x is a shl by 32 and c1 is an simm12, we would prefer to use a SRAIW+ANDI. This prevents selecting the slli to a separate slli instruction. Fixes regression from #101868
1 parent b64ec3c commit 15895da

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,12 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) {
14611461

14621462
SDValue X = N0.getOperand(0);
14631463

1464-
if (isMask_64(C1)) {
1464+
// Prefer SRAIW + ANDI when possible.
1465+
bool Skip = C2 > 32 && isInt<12>(N1C->getSExtValue()) &&
1466+
X.getOpcode() == ISD::SHL &&
1467+
isa<ConstantSDNode>(X.getOperand(1)) &&
1468+
X.getConstantOperandVal(1) == 32;
1469+
if (isMask_64(C1) && !Skip) {
14651470
unsigned Leading = XLen - llvm::bit_width(C1);
14661471
if (C2 > Leading) {
14671472
SDNode *SRAI = CurDAG->getMachineNode(

llvm/test/CodeGen/RISCV/selectcc-to-shiftand.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,8 @@ define i64 @sraiw_andi(i32 signext %0, i32 signext %1) nounwind {
276276
; RV64-LABEL: sraiw_andi:
277277
; RV64: # %bb.0: # %entry
278278
; RV64-NEXT: add a0, a0, a1
279-
; RV64-NEXT: slli a0, a0, 32
280-
; RV64-NEXT: srai a0, a0, 2
281-
; RV64-NEXT: srli a0, a0, 61
279+
; RV64-NEXT: sraiw a0, a0, 31
280+
; RV64-NEXT: andi a0, a0, 7
282281
; RV64-NEXT: ret
283282
entry:
284283
%3 = add i32 %0, %1

0 commit comments

Comments
 (0)