Skip to content

Commit 6140b5b

Browse files
authored
[RISCV] Use RISCVISD::SHL_ADD in transformAddShlImm (#89832)
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 #88791, where a bug was pointed out. That bug was fixed by #89789 which hits the same issue from another angle. To confirm the fix, I included the reduced test case here.
1 parent 91a14db commit 6140b5b

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
@@ -12987,10 +12987,9 @@ static SDValue transformAddShlImm(SDNode *N, SelectionDAG &DAG,
1298712987
SDLoc DL(N);
1298812988
SDValue NS = (C0 < C1) ? N0->getOperand(0) : N1->getOperand(0);
1298912989
SDValue NL = (C0 > C1) ? N0->getOperand(0) : N1->getOperand(0);
12990-
SDValue NA0 =
12991-
DAG.getNode(ISD::SHL, DL, VT, NL, DAG.getConstant(Diff, DL, VT));
12992-
SDValue NA1 = DAG.getNode(ISD::ADD, DL, VT, NA0, NS);
12993-
return DAG.getNode(ISD::SHL, DL, VT, NA1, DAG.getConstant(Bits, DL, VT));
12990+
SDValue SHADD = DAG.getNode(RISCVISD::SHL_ADD, DL, VT, NL,
12991+
DAG.getConstant(Diff, DL, VT), NS);
12992+
return DAG.getNode(ISD::SHL, DL, VT, SHADD, DAG.getConstant(Bits, DL, VT));
1299412993
}
1299512994

1299612995
// Combine a constant select operand into its use:
@@ -13226,14 +13225,17 @@ static SDValue combineAddOfBooleanXor(SDNode *N, SelectionDAG &DAG) {
1322613225
N0.getOperand(0));
1322713226
}
1322813227

13229-
static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
13228+
static SDValue performADDCombine(SDNode *N,
13229+
TargetLowering::DAGCombinerInfo &DCI,
1323013230
const RISCVSubtarget &Subtarget) {
13231+
SelectionDAG &DAG = DCI.DAG;
1323113232
if (SDValue V = combineAddOfBooleanXor(N, DAG))
1323213233
return V;
1323313234
if (SDValue V = transformAddImmMulImm(N, DAG, Subtarget))
1323413235
return V;
13235-
if (SDValue V = transformAddShlImm(N, DAG, Subtarget))
13236-
return V;
13236+
if (!DCI.isBeforeLegalize() && !DCI.isCalledByLegalizer())
13237+
if (SDValue V = transformAddShlImm(N, DAG, Subtarget))
13238+
return V;
1323713239
if (SDValue V = combineBinOpToReduce(N, DAG, Subtarget))
1323813240
return V;
1323913241
if (SDValue V = combineBinOpOfExtractToReduceTree(N, DAG, Subtarget))
@@ -16230,7 +16232,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
1623016232
return V;
1623116233
if (SDValue V = combineToVWMACC(N, DAG, Subtarget))
1623216234
return V;
16233-
return performADDCombine(N, DAG, Subtarget);
16235+
return performADDCombine(N, DCI, Subtarget);
1623416236
}
1623516237
case ISD::SUB: {
1623616238
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
@@ -944,3 +944,29 @@ define i1 @pr53831(i32 %x) {
944944
%tmp5 = icmp eq i32 %tmp4, %tmp2
945945
ret i1 %tmp5
946946
}
947+
948+
define i64 @sh2add_uw(i64 signext %0, i32 signext %1) {
949+
; RV32IMB-LABEL: sh2add_uw:
950+
; RV32IMB: # %bb.0: # %entry
951+
; RV32IMB-NEXT: srli a3, a2, 27
952+
; RV32IMB-NEXT: slli a2, a2, 5
953+
; RV32IMB-NEXT: srli a4, a0, 29
954+
; RV32IMB-NEXT: sh3add a1, a1, a4
955+
; RV32IMB-NEXT: sh3add a0, a0, a2
956+
; RV32IMB-NEXT: sltu a2, a0, a2
957+
; RV32IMB-NEXT: add a1, a3, a1
958+
; RV32IMB-NEXT: add a1, a1, a2
959+
; RV32IMB-NEXT: ret
960+
;
961+
; RV64IMB-LABEL: sh2add_uw:
962+
; RV64IMB: # %bb.0: # %entry
963+
; RV64IMB-NEXT: sh2add.uw a0, a1, a0
964+
; RV64IMB-NEXT: slli a0, a0, 3
965+
; RV64IMB-NEXT: ret
966+
entry:
967+
%2 = zext i32 %1 to i64
968+
%3 = shl i64 %2, 5
969+
%4 = shl i64 %0, 3
970+
%5 = add i64 %3, %4
971+
ret i64 %5
972+
}

0 commit comments

Comments
 (0)