Skip to content

Commit 521fb93

Browse files
authored
[RISCV] Support P extension ABSW instruction. (#165047)
1 parent 64f1ca7 commit 521fb93

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,6 +3995,7 @@ bool RISCVDAGToDAGISel::hasAllNBitUsers(SDNode *Node, unsigned Bits,
39953995
case RISCV::CTZW:
39963996
case RISCV::CPOPW:
39973997
case RISCV::SLLI_UW:
3998+
case RISCV::ABSW:
39983999
case RISCV::FMV_W_X:
39994000
case RISCV::FCVT_H_W:
40004001
case RISCV::FCVT_H_W_INX:

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
433433
if (Subtarget.hasStdExtP() ||
434434
(Subtarget.hasVendorXCValu() && !Subtarget.is64Bit())) {
435435
setOperationAction(ISD::ABS, XLenVT, Legal);
436+
if (Subtarget.is64Bit())
437+
setOperationAction(ISD::ABS, MVT::i32, Custom);
436438
} else if (Subtarget.hasShortForwardBranchOpt()) {
437439
// We can use PseudoCCSUB to implement ABS.
438440
setOperationAction(ISD::ABS, XLenVT, Legal);
@@ -14816,8 +14818,16 @@ void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
1481614818
assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1481714819
"Unexpected custom legalisation");
1481814820

14821+
if (Subtarget.hasStdExtP()) {
14822+
SDValue Src =
14823+
DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
14824+
SDValue Abs = DAG.getNode(RISCVISD::ABSW, DL, MVT::i64, Src);
14825+
Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Abs));
14826+
return;
14827+
}
14828+
1481914829
if (Subtarget.hasStdExtZbb()) {
14820-
// Emit a special ABSW node that will be expanded to NEGW+MAX at isel.
14830+
// Emit a special node that will be expanded to NEGW+MAX at isel.
1482114831
// This allows us to remember that the result is sign extended. Expanding
1482214832
// to NEGW+MAX here requires a Freeze which breaks ComputeNumSignBits.
1482314833
SDValue Src = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64,
@@ -20290,6 +20300,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
2029020300

2029120301
break;
2029220302
}
20303+
case RISCVISD::ABSW:
2029320304
case RISCVISD::CLZW:
2029420305
case RISCVISD::CTZW: {
2029520306
// Only the lower 32 bits of the first operand are read
@@ -21862,6 +21873,7 @@ unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
2186221873
case RISCVISD::REMUW:
2186321874
case RISCVISD::ROLW:
2186421875
case RISCVISD::RORW:
21876+
case RISCVISD::ABSW:
2186521877
case RISCVISD::FCVT_W_RV64:
2186621878
case RISCVISD::FCVT_WU_RV64:
2186721879
case RISCVISD::STRICT_FCVT_W_RV64:

llvm/lib/Target/RISCV/RISCVInstrInfoP.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,5 +1461,10 @@ let Predicates = [HasStdExtP, IsRV32] in {
14611461
// Codegen patterns
14621462
//===----------------------------------------------------------------------===//
14631463

1464+
def riscv_absw : RVSDNode<"ABSW", SDTIntUnaryOp>;
1465+
14641466
let Predicates = [HasStdExtP] in
14651467
def : PatGpr<abs, ABS>;
1468+
1469+
let Predicates = [HasStdExtP, IsRV64] in
1470+
def : PatGpr<riscv_absw, ABSW>;

llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ static bool hasAllNBitUsers(const MachineInstr &OrigMI,
172172
case RISCV::CTZW:
173173
case RISCV::CPOPW:
174174
case RISCV::SLLI_UW:
175+
case RISCV::ABSW:
175176
case RISCV::FMV_W_X:
176177
case RISCV::FCVT_H_W:
177178
case RISCV::FCVT_H_W_INX:

llvm/test/CodeGen/RISCV/rv64p.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ declare i32 @llvm.abs.i32(i32, i1 immarg)
297297
define i32 @abs_i32(i32 %x) {
298298
; CHECK-LABEL: abs_i32:
299299
; CHECK: # %bb.0:
300-
; CHECK-NEXT: sext.w a0, a0
301-
; CHECK-NEXT: abs a0, a0
300+
; CHECK-NEXT: absw a0, a0
302301
; CHECK-NEXT: ret
303302
%abs = tail call i32 @llvm.abs.i32(i32 %x, i1 true)
304303
ret i32 %abs
@@ -307,8 +306,7 @@ define i32 @abs_i32(i32 %x) {
307306
define signext i32 @abs_i32_sext(i32 signext %x) {
308307
; CHECK-LABEL: abs_i32_sext:
309308
; CHECK: # %bb.0:
310-
; CHECK-NEXT: abs a0, a0
311-
; CHECK-NEXT: sext.w a0, a0
309+
; CHECK-NEXT: absw a0, a0
312310
; CHECK-NEXT: ret
313311
%abs = tail call i32 @llvm.abs.i32(i32 %x, i1 true)
314312
ret i32 %abs

0 commit comments

Comments
 (0)