Skip to content

Commit d8aafb4

Browse files
committed
[RISCV] Fold Zba-expanded (mul (shr exact X, C1), C2)
1 parent 6325a17 commit d8aafb4

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,11 @@ template <typename LHS, typename RHS>
903903
inline BinaryOpc_match<LHS, RHS> m_Srl(const LHS &L, const RHS &R) {
904904
return BinaryOpc_match<LHS, RHS>(ISD::SRL, L, R);
905905
}
906+
template <typename LHS, typename RHS>
907+
inline auto m_ExactSr(const LHS &L, const RHS &R) {
908+
return m_AnyOf(BinaryOpc_match<LHS, RHS>(ISD::SRA, L, R, SDNodeFlags::Exact),
909+
BinaryOpc_match<LHS, RHS>(ISD::SRL, L, R, SDNodeFlags::Exact));
910+
}
906911

907912
template <typename LHS, typename RHS>
908913
inline BinaryOpc_match<LHS, RHS> m_Rotl(const LHS &L, const RHS &R) {

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16829,10 +16829,13 @@ static SDValue getShlAddShlAdd(SDNode *N, SelectionDAG &DAG, unsigned ShX,
1682916829
SDLoc DL(N);
1683016830
EVT VT = N->getValueType(0);
1683116831
SDValue X = N->getOperand(0);
16832-
// Put the shift first if we can fold a zext into the shift forming a slli.uw.
16832+
// Put the shift first if we can fold:
16833+
// a. a zext into the shift forming a slli.uw
16834+
// b. an exact shift right forming one shorter shift or no shift at all
1683316835
using namespace SDPatternMatch;
1683416836
if (Shift != 0 &&
16835-
sd_match(X, m_And(m_Value(), m_SpecificInt(UINT64_C(0xffffffff))))) {
16837+
sd_match(X, m_AnyOf(m_And(m_Value(), m_SpecificInt(UINT64_C(0xffffffff))),
16838+
m_ExactSr(m_Value(), m_ConstInt())))) {
1683616839
X = DAG.getNode(ISD::SHL, DL, VT, X, DAG.getConstant(Shift, DL, VT));
1683716840
Shift = 0;
1683816841
}

llvm/test/CodeGen/RISCV/rv64zba.ll

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5026,16 +5026,12 @@ define i64 @exactashr1mul6(i64 %a) {
50265026
;
50275027
; RV64ZBA-LABEL: exactashr1mul6:
50285028
; RV64ZBA: # %bb.0:
5029-
; RV64ZBA-NEXT: srli a0, a0, 1
50305029
; RV64ZBA-NEXT: sh1add a0, a0, a0
5031-
; RV64ZBA-NEXT: slli a0, a0, 1
50325030
; RV64ZBA-NEXT: ret
50335031
;
50345032
; RV64XANDESPERF-LABEL: exactashr1mul6:
50355033
; RV64XANDESPERF: # %bb.0:
5036-
; RV64XANDESPERF-NEXT: srli a0, a0, 1
50375034
; RV64XANDESPERF-NEXT: nds.lea.h a0, a0, a0
5038-
; RV64XANDESPERF-NEXT: slli a0, a0, 1
50395035
; RV64XANDESPERF-NEXT: ret
50405036
%c = ashr exact i64 %a, 1
50415037
%d = mul i64 %c, 6
@@ -5052,18 +5048,16 @@ define i64 @exactlshr3mul22(i64 %a) {
50525048
;
50535049
; RV64ZBA-LABEL: exactlshr3mul22:
50545050
; RV64ZBA: # %bb.0:
5055-
; RV64ZBA-NEXT: srli a0, a0, 3
5051+
; RV64ZBA-NEXT: srli a0, a0, 2
50565052
; RV64ZBA-NEXT: sh2add a1, a0, a0
50575053
; RV64ZBA-NEXT: sh1add a0, a1, a0
5058-
; RV64ZBA-NEXT: slli a0, a0, 1
50595054
; RV64ZBA-NEXT: ret
50605055
;
50615056
; RV64XANDESPERF-LABEL: exactlshr3mul22:
50625057
; RV64XANDESPERF: # %bb.0:
5063-
; RV64XANDESPERF-NEXT: srli a0, a0, 3
5058+
; RV64XANDESPERF-NEXT: srli a0, a0, 2
50645059
; RV64XANDESPERF-NEXT: nds.lea.w a1, a0, a0
50655060
; RV64XANDESPERF-NEXT: nds.lea.h a0, a0, a1
5066-
; RV64XANDESPERF-NEXT: slli a0, a0, 1
50675061
; RV64XANDESPERF-NEXT: ret
50685062
%c = lshr exact i64 %a, 3
50695063
%d = mul i64 %c, 22
@@ -5080,16 +5074,14 @@ define i64 @exactashr1mul36(i64 %a) {
50805074
;
50815075
; RV64ZBA-LABEL: exactashr1mul36:
50825076
; RV64ZBA: # %bb.0:
5083-
; RV64ZBA-NEXT: srli a0, a0, 1
5077+
; RV64ZBA-NEXT: slli a0, a0, 1
50845078
; RV64ZBA-NEXT: sh3add a0, a0, a0
5085-
; RV64ZBA-NEXT: slli a0, a0, 2
50865079
; RV64ZBA-NEXT: ret
50875080
;
50885081
; RV64XANDESPERF-LABEL: exactashr1mul36:
50895082
; RV64XANDESPERF: # %bb.0:
5090-
; RV64XANDESPERF-NEXT: srli a0, a0, 1
5083+
; RV64XANDESPERF-NEXT: slli a0, a0, 1
50915084
; RV64XANDESPERF-NEXT: nds.lea.d a0, a0, a0
5092-
; RV64XANDESPERF-NEXT: slli a0, a0, 2
50935085
; RV64XANDESPERF-NEXT: ret
50945086
%c = ashr exact i64 %a, 1
50955087
%d = mul i64 %c, 36

0 commit comments

Comments
 (0)