Skip to content

Commit 0c032fd

Browse files
authored
[RISCV] Use SHL_ADD in remaining strength reduce cases for MUL (#89789)
The interesting bit is the zext folding. This is the first case where we end up with a profitable fold of shNadd (zext x), y to shNadd.uw x, y. See zext_mul68 from rv64zba.ll. The test differences are cases where we can legally fold (only because there's no one use check). These are not profitable or harmful, but we can't a oneuse check without breaking the zext_mul68 case. Note that XTHeadBa doesn't appear to have the equivalent patterns so this only shows up in Zba.
1 parent dc8f6a8 commit 0c032fd

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13449,9 +13449,8 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
1344913449
SDValue X = DAG.getFreeze(N->getOperand(0));
1345013450
SDValue Shift1 =
1345113451
DAG.getNode(ISD::SHL, DL, VT, X, DAG.getConstant(ShiftAmt, DL, VT));
13452-
SDValue Shift2 =
13453-
DAG.getNode(ISD::SHL, DL, VT, X, DAG.getConstant(ScaleShift, DL, VT));
13454-
return DAG.getNode(ISD::ADD, DL, VT, Shift1, Shift2);
13452+
return DAG.getNode(RISCVISD::SHL_ADD, DL, VT, X,
13453+
DAG.getConstant(ScaleShift, DL, VT), Shift1);
1345513454
}
1345613455
}
1345713456

@@ -13485,10 +13484,9 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
1348513484
SDValue X = DAG.getFreeze(N->getOperand(0));
1348613485
SDValue Shift1 =
1348713486
DAG.getNode(ISD::SHL, DL, VT, X, DAG.getConstant(ShiftAmt, DL, VT));
13488-
SDValue Shift2 =
13489-
DAG.getNode(ISD::SHL, DL, VT, X, DAG.getConstant(ScaleShift, DL, VT));
1349013487
return DAG.getNode(ISD::ADD, DL, VT, Shift1,
13491-
DAG.getNode(ISD::ADD, DL, VT, Shift2, X));
13488+
DAG.getNode(RISCVISD::SHL_ADD, DL, VT, X,
13489+
DAG.getConstant(ScaleShift, DL, VT), X));
1349213490
}
1349313491
}
1349413492

llvm/lib/Target/RISCV/RISCVInstrInfoZb.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ foreach i = {1,2,3} in {
747747
defvar shxadd_uw = !cast<Instruction>("SH"#i#"ADD_UW");
748748
def : Pat<(i64 (add_like_non_imm12 (shl (and GPR:$rs1, 0xFFFFFFFF), (i64 i)), (XLenVT GPR:$rs2))),
749749
(shxadd_uw GPR:$rs1, GPR:$rs2)>;
750+
def : Pat<(i64 (riscv_shl_add (and GPR:$rs1, 0xFFFFFFFF), (i64 i), GPR:$rs2)),
751+
(shxadd_uw GPR:$rs1, GPR:$rs2)>;
750752
}
751753

752754
def : Pat<(i64 (add_like_non_imm12 (and (shl GPR:$rs1, (i64 1)), 0x1FFFFFFFF), (XLenVT GPR:$rs2))),

llvm/test/CodeGen/RISCV/rv64-legal-i32/xaluo.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,9 @@ define zeroext i1 @umulo2.i32(i32 signext %v1, ptr %res) {
810810
;
811811
; RV64ZBA-LABEL: umulo2.i32:
812812
; RV64ZBA: # %bb.0: # %entry
813-
; RV64ZBA-NEXT: zext.w a0, a0
814-
; RV64ZBA-NEXT: sh1add a2, a0, a0
815-
; RV64ZBA-NEXT: sh2add a2, a2, a0
813+
; RV64ZBA-NEXT: zext.w a2, a0
814+
; RV64ZBA-NEXT: sh1add.uw a0, a0, a2
815+
; RV64ZBA-NEXT: sh2add a2, a0, a2
816816
; RV64ZBA-NEXT: srli a0, a2, 32
817817
; RV64ZBA-NEXT: snez a0, a0
818818
; RV64ZBA-NEXT: sw a2, 0(a1)

llvm/test/CodeGen/RISCV/xaluo.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,9 +1759,9 @@ define zeroext i1 @umulo2.i32(i32 signext %v1, ptr %res) {
17591759
;
17601760
; RV64ZBA-LABEL: umulo2.i32:
17611761
; RV64ZBA: # %bb.0: # %entry
1762-
; RV64ZBA-NEXT: zext.w a0, a0
1763-
; RV64ZBA-NEXT: sh1add a2, a0, a0
1764-
; RV64ZBA-NEXT: sh2add a2, a2, a0
1762+
; RV64ZBA-NEXT: zext.w a2, a0
1763+
; RV64ZBA-NEXT: sh1add.uw a0, a0, a2
1764+
; RV64ZBA-NEXT: sh2add a2, a0, a2
17651765
; RV64ZBA-NEXT: srli a0, a2, 32
17661766
; RV64ZBA-NEXT: snez a0, a0
17671767
; RV64ZBA-NEXT: sw a2, 0(a1)

0 commit comments

Comments
 (0)