Skip to content

Commit 55e9517

Browse files
committed
[RISCV] Support P extension ABSW instruction.
1 parent 0b01b96 commit 55e9517

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 11 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 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64,
14823+
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,

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/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)