[llvm] 6140b5b - [RISCV] Use RISCVISD::SHL_ADD in transformAddShlImm (#89832)
via llvm-commits
llvm-commits at lists.llvm.org
Mon May 13 09:48:49 PDT 2024
Author: Philip Reames
Date: 2024-05-13T09:48:46-07:00
New Revision: 6140b5bae475069f958f90a81fb9d69c969daab6
URL: https://github.com/llvm/llvm-project/commit/6140b5bae475069f958f90a81fb9d69c969daab6
DIFF: https://github.com/llvm/llvm-project/commit/6140b5bae475069f958f90a81fb9d69c969daab6.diff
LOG: [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.
Added:
Modified:
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/addimm-mulimm.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index d0f62b1d54143..5a84ad4d436b8 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -12987,10 +12987,9 @@ static SDValue transformAddShlImm(SDNode *N, SelectionDAG &DAG,
SDLoc DL(N);
SDValue NS = (C0 < C1) ? N0->getOperand(0) : N1->getOperand(0);
SDValue NL = (C0 > C1) ? N0->getOperand(0) : N1->getOperand(0);
- SDValue NA0 =
- DAG.getNode(ISD::SHL, DL, VT, NL, DAG.getConstant(Diff, DL, VT));
- SDValue NA1 = DAG.getNode(ISD::ADD, DL, VT, NA0, NS);
- return DAG.getNode(ISD::SHL, DL, VT, NA1, DAG.getConstant(Bits, DL, VT));
+ SDValue SHADD = DAG.getNode(RISCVISD::SHL_ADD, DL, VT, NL,
+ DAG.getConstant(Diff, DL, VT), NS);
+ return DAG.getNode(ISD::SHL, DL, VT, SHADD, DAG.getConstant(Bits, DL, VT));
}
// Combine a constant select operand into its use:
@@ -13226,14 +13225,17 @@ static SDValue combineAddOfBooleanXor(SDNode *N, SelectionDAG &DAG) {
N0.getOperand(0));
}
-static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
+static SDValue performADDCombine(SDNode *N,
+ TargetLowering::DAGCombinerInfo &DCI,
const RISCVSubtarget &Subtarget) {
+ SelectionDAG &DAG = DCI.DAG;
if (SDValue V = combineAddOfBooleanXor(N, DAG))
return V;
if (SDValue V = transformAddImmMulImm(N, DAG, Subtarget))
return V;
- if (SDValue V = transformAddShlImm(N, DAG, Subtarget))
- return V;
+ if (!DCI.isBeforeLegalize() && !DCI.isCalledByLegalizer())
+ if (SDValue V = transformAddShlImm(N, DAG, Subtarget))
+ return V;
if (SDValue V = combineBinOpToReduce(N, DAG, Subtarget))
return V;
if (SDValue V = combineBinOpOfExtractToReduceTree(N, DAG, Subtarget))
@@ -16230,7 +16232,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
return V;
if (SDValue V = combineToVWMACC(N, DAG, Subtarget))
return V;
- return performADDCombine(N, DAG, Subtarget);
+ return performADDCombine(N, DCI, Subtarget);
}
case ISD::SUB: {
if (SDValue V = combineBinOp_VLToVWBinOp_VL(N, DCI, Subtarget))
diff --git a/llvm/test/CodeGen/RISCV/addimm-mulimm.ll b/llvm/test/CodeGen/RISCV/addimm-mulimm.ll
index e2f7be2e6d7f7..a18526718461e 100644
--- a/llvm/test/CodeGen/RISCV/addimm-mulimm.ll
+++ b/llvm/test/CodeGen/RISCV/addimm-mulimm.ll
@@ -944,3 +944,29 @@ define i1 @pr53831(i32 %x) {
%tmp5 = icmp eq i32 %tmp4, %tmp2
ret i1 %tmp5
}
+
+define i64 @sh2add_uw(i64 signext %0, i32 signext %1) {
+; RV32IMB-LABEL: sh2add_uw:
+; RV32IMB: # %bb.0: # %entry
+; RV32IMB-NEXT: srli a3, a2, 27
+; RV32IMB-NEXT: slli a2, a2, 5
+; RV32IMB-NEXT: srli a4, a0, 29
+; RV32IMB-NEXT: sh3add a1, a1, a4
+; RV32IMB-NEXT: sh3add a0, a0, a2
+; RV32IMB-NEXT: sltu a2, a0, a2
+; RV32IMB-NEXT: add a1, a3, a1
+; RV32IMB-NEXT: add a1, a1, a2
+; RV32IMB-NEXT: ret
+;
+; RV64IMB-LABEL: sh2add_uw:
+; RV64IMB: # %bb.0: # %entry
+; RV64IMB-NEXT: sh2add.uw a0, a1, a0
+; RV64IMB-NEXT: slli a0, a0, 3
+; RV64IMB-NEXT: ret
+entry:
+ %2 = zext i32 %1 to i64
+ %3 = shl i64 %2, 5
+ %4 = shl i64 %0, 3
+ %5 = add i64 %3, %4
+ ret i64 %5
+}
More information about the llvm-commits
mailing list