[llvm] [RISCV] Add isel optimization for (add x, (and (sra y, c2), c1)) to r… (PR #104114)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 14 11:07:36 PDT 2024
https://github.com/topperc created https://github.com/llvm/llvm-project/pull/104114
…ecover regression from #101751.
If 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 ammount.
I'll also improve the non-Zba case in a follow up patch.
>From 9ac2de51dce5e7549dd54922881c23879821fa07 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 14 Aug 2024 10:58:55 -0700
Subject: [PATCH] [RISCV] Add isel optimization for (add x, (and (sra y, c2),
c1)) to recover regression from #101751.
If 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 ammount.
I'll also improve the non-Zba case in a follow up patch.
---
llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp | 27 +++++++++++++++++++++
llvm/test/CodeGen/RISCV/rv64zba.ll | 23 ++++++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
index 35681c620eed51..bcc719737b405a 100644
--- a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
@@ -3019,6 +3019,33 @@ bool RISCVDAGToDAGISel::selectSHXADDOp(SDValue N, unsigned ShAmt,
return true;
}
}
+ } else if (N0.getOpcode() == ISD::SRA && N0.hasOneUse() &&
+ isa<ConstantSDNode>(N.getOperand(1))) {
+ 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 ammount.
+ if (isShiftedMask_64(Mask)) {
+ unsigned XLen = Subtarget->getXLen();
+ unsigned Leading = XLen - llvm::bit_width(Mask);
+ unsigned Trailing = llvm::countr_zero(Mask);
+ if (C2 > Leading && 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) &&
diff --git a/llvm/test/CodeGen/RISCV/rv64zba.ll b/llvm/test/CodeGen/RISCV/rv64zba.ll
index 87796e2c7b72e9..b1b4707265d7fb 100644
--- a/llvm/test/CodeGen/RISCV/rv64zba.ll
+++ b/llvm/test/CodeGen/RISCV/rv64zba.ll
@@ -2988,3 +2988,26 @@ entry:
%2 = and i64 %1, 34359738360
ret i64 %2
}
+
+define ptr @srai_srli_sh3add(ptr %0, i64 %1) nounwind {
+; RV64I-LABEL: srai_srli_sh3add:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: srai a1, a1, 35
+; RV64I-NEXT: li a2, -57
+; RV64I-NEXT: srli a2, a2, 3
+; RV64I-NEXT: and a1, a1, a2
+; RV64I-NEXT: add a0, a0, a1
+; RV64I-NEXT: ret
+;
+; RV64ZBA-LABEL: srai_srli_sh3add:
+; RV64ZBA: # %bb.0: # %entry
+; RV64ZBA-NEXT: srai a1, a1, 32
+; RV64ZBA-NEXT: srli a1, a1, 6
+; RV64ZBA-NEXT: sh3add a0, a1, a0
+; RV64ZBA-NEXT: ret
+entry:
+ %2 = ashr i64 %1, 32
+ %3 = lshr i64 %2, 6
+ %4 = getelementptr i64, ptr %0, i64 %3
+ ret ptr %4
+}
More information about the llvm-commits
mailing list