Skip to content

Commit 257c479

Browse files
authored
[LegalizeTypes][RISCV] Use SExtOrZExtPromotedOperands to promote operands for USUBSAT. (llvm#102781)
It doesn't matter which extend we use to promote the operands. Use whatever is the most efficient. The custom handler for RISC-V was using SIGN_EXTEND when the Zbb extension is enabled so we no longer need that.
1 parent 846dccc commit 257c479

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,17 +1045,25 @@ SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSHLSAT(SDNode *N) {
10451045
SDValue Op1 = N->getOperand(0);
10461046
SDValue Op2 = N->getOperand(1);
10471047
MatchContextClass matcher(DAG, TLI, N);
1048-
unsigned OldBits = Op1.getScalarValueSizeInBits();
10491048

10501049
unsigned Opcode = matcher.getRootBaseOpcode();
1050+
unsigned OldBits = Op1.getScalarValueSizeInBits();
1051+
1052+
// USUBSAT can always be promoted as long as we have zero/sign-extended the
1053+
// args.
1054+
if (Opcode == ISD::USUBSAT) {
1055+
SExtOrZExtPromotedOperands(Op1, Op2);
1056+
return matcher.getNode(ISD::USUBSAT, dl, Op1.getValueType(), Op1, Op2);
1057+
}
1058+
10511059
bool IsShift = Opcode == ISD::USHLSAT || Opcode == ISD::SSHLSAT;
10521060

10531061
// FIXME: We need vp-aware PromotedInteger functions.
10541062
SDValue Op1Promoted, Op2Promoted;
10551063
if (IsShift) {
10561064
Op1Promoted = GetPromotedInteger(Op1);
10571065
Op2Promoted = ZExtPromotedInteger(Op2);
1058-
} else if (Opcode == ISD::UADDSAT || Opcode == ISD::USUBSAT) {
1066+
} else if (Opcode == ISD::UADDSAT) {
10591067
Op1Promoted = ZExtPromotedInteger(Op1);
10601068
Op2Promoted = ZExtPromotedInteger(Op2);
10611069
} else {
@@ -1073,11 +1081,6 @@ SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSHLSAT(SDNode *N) {
10731081
return matcher.getNode(ISD::UMIN, dl, PromotedType, Add, SatMax);
10741082
}
10751083

1076-
// USUBSAT can always be promoted as long as we have zero-extended the args.
1077-
if (Opcode == ISD::USUBSAT)
1078-
return matcher.getNode(ISD::USUBSAT, dl, PromotedType, Op1Promoted,
1079-
Op2Promoted);
1080-
10811084
// Shift cannot use a min/max expansion, we can't detect overflow if all of
10821085
// the bits have been shifted out.
10831086
if (IsShift || matcher.isOperationLegal(Opcode, PromotedType)) {

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,11 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
268268
setOperationAction(ISD::LOAD, MVT::i32, Custom);
269269
setOperationAction({ISD::ADD, ISD::SUB, ISD::SHL, ISD::SRA, ISD::SRL},
270270
MVT::i32, Custom);
271-
setOperationAction({ISD::UADDO, ISD::USUBO, ISD::UADDSAT, ISD::USUBSAT},
272-
MVT::i32, Custom);
271+
setOperationAction({ISD::UADDO, ISD::USUBO, ISD::UADDSAT}, MVT::i32,
272+
Custom);
273273
if (!Subtarget.hasStdExtZbb())
274-
setOperationAction({ISD::SADDSAT, ISD::SSUBSAT}, MVT::i32, Custom);
274+
setOperationAction({ISD::SADDSAT, ISD::SSUBSAT, ISD::USUBSAT}, MVT::i32,
275+
Custom);
275276
setOperationAction(ISD::SADDO, MVT::i32, Custom);
276277
}
277278
if (!Subtarget.hasStdExtZmmul()) {

llvm/test/CodeGen/RISCV/usub_sat_plus.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ define i32 @func32(i32 %x, i32 %y, i32 %z) nounwind {
3939
;
4040
; RV64IZbb-LABEL: func32:
4141
; RV64IZbb: # %bb.0:
42-
; RV64IZbb-NEXT: mulw a1, a1, a2
4342
; RV64IZbb-NEXT: sext.w a0, a0
43+
; RV64IZbb-NEXT: mulw a1, a1, a2
4444
; RV64IZbb-NEXT: maxu a0, a0, a1
4545
; RV64IZbb-NEXT: sub a0, a0, a1
4646
; RV64IZbb-NEXT: ret

0 commit comments

Comments
 (0)