Skip to content

Commit 4c0b7d8

Browse files
committed
[RISCV] Use RISCVISD::SHL_ADD in transformAddShlImm [NFC]
Doing so avoids negative interactions with other combines which don't know the shl_add is a single instruction. From the commit log, we've had several combine loops already. This was originally posted as part of llvm#88791, where a bug was pointed out. That bug was fixed by llvm#89789 which hits the same issue from another angle. To confirm the fix, I included the reduce test case here.
1 parent 0c032fd commit 4c0b7d8

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12817,10 +12817,9 @@ static SDValue transformAddShlImm(SDNode *N, SelectionDAG &DAG,
1281712817
SDLoc DL(N);
1281812818
SDValue NS = (C0 < C1) ? N0->getOperand(0) : N1->getOperand(0);
1281912819
SDValue NL = (C0 > C1) ? N0->getOperand(0) : N1->getOperand(0);
12820-
SDValue NA0 =
12821-
DAG.getNode(ISD::SHL, DL, VT, NL, DAG.getConstant(Diff, DL, VT));
12822-
SDValue NA1 = DAG.getNode(ISD::ADD, DL, VT, NA0, NS);
12823-
return DAG.getNode(ISD::SHL, DL, VT, NA1, DAG.getConstant(Bits, DL, VT));
12820+
SDValue SHADD =
12821+
DAG.getNode(RISCVISD::SHL_ADD, DL, VT, NL, DAG.getConstant(Diff, DL, VT), NS);
12822+
return DAG.getNode(ISD::SHL, DL, VT, SHADD, DAG.getConstant(Bits, DL, VT));
1282412823
}
1282512824

1282612825
// Combine a constant select operand into its use:
@@ -13056,14 +13055,17 @@ static SDValue combineAddOfBooleanXor(SDNode *N, SelectionDAG &DAG) {
1305613055
N0.getOperand(0));
1305713056
}
1305813057

13059-
static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
13058+
static SDValue performADDCombine(SDNode *N,
13059+
TargetLowering::DAGCombinerInfo &DCI,
1306013060
const RISCVSubtarget &Subtarget) {
13061+
SelectionDAG &DAG = DCI.DAG;
1306113062
if (SDValue V = combineAddOfBooleanXor(N, DAG))
1306213063
return V;
1306313064
if (SDValue V = transformAddImmMulImm(N, DAG, Subtarget))
1306413065
return V;
13065-
if (SDValue V = transformAddShlImm(N, DAG, Subtarget))
13066-
return V;
13066+
if (!DCI.isBeforeLegalize() && !DCI.isCalledByLegalizer())
13067+
if (SDValue V = transformAddShlImm(N, DAG, Subtarget))
13068+
return V;
1306713069
if (SDValue V = combineBinOpToReduce(N, DAG, Subtarget))
1306813070
return V;
1306913071
if (SDValue V = combineBinOpOfExtractToReduceTree(N, DAG, Subtarget))
@@ -16027,7 +16029,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
1602716029
return V;
1602816030
if (SDValue V = combineToVWMACC(N, DAG, Subtarget))
1602916031
return V;
16030-
return performADDCombine(N, DAG, Subtarget);
16032+
return performADDCombine(N, DCI, Subtarget);
1603116033
}
1603216034
case ISD::SUB: {
1603316035
if (SDValue V = combineBinOp_VLToVWBinOp_VL(N, DCI, Subtarget))

llvm/test/CodeGen/RISCV/addimm-mulimm.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,3 +898,29 @@ define i1 @pr53831(i32 %x) {
898898
%tmp5 = icmp eq i32 %tmp4, %tmp2
899899
ret i1 %tmp5
900900
}
901+
902+
define i64 @sh2add_uw(i64 signext %0, i32 signext %1) {
903+
; RV32IMB-LABEL: sh2add_uw:
904+
; RV32IMB: # %bb.0: # %entry
905+
; RV32IMB-NEXT: srli a3, a2, 27
906+
; RV32IMB-NEXT: slli a2, a2, 5
907+
; RV32IMB-NEXT: srli a4, a0, 29
908+
; RV32IMB-NEXT: sh3add a1, a1, a4
909+
; RV32IMB-NEXT: sh3add a0, a0, a2
910+
; RV32IMB-NEXT: sltu a2, a0, a2
911+
; RV32IMB-NEXT: add a1, a3, a1
912+
; RV32IMB-NEXT: add a1, a1, a2
913+
; RV32IMB-NEXT: ret
914+
;
915+
; RV64IMB-LABEL: sh2add_uw:
916+
; RV64IMB: # %bb.0: # %entry
917+
; RV64IMB-NEXT: sh2add.uw a0, a1, a0
918+
; RV64IMB-NEXT: slli a0, a0, 3
919+
; RV64IMB-NEXT: ret
920+
entry:
921+
%2 = zext i32 %1 to i64
922+
%3 = shl i64 %2, 5
923+
%4 = shl i64 %0, 3
924+
%5 = add i64 %3, %4
925+
ret i64 %5
926+
}

0 commit comments

Comments
 (0)