Skip to content

Commit 908a172

Browse files
committed
[PowerPC] Add custom lowering for ssubo (llvm#111748)
This patch is to improve the codegen for ssubo node for i32 in 64-bit mode by custom lowering.
1 parent 4f24d03 commit 908a172

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
200200

201201
setOperationAction(ISD::UADDO, isPPC64 ? MVT::i64 : MVT::i32, Custom);
202202

203+
// On P10, the default lowering generates better code using the
204+
// setbc instruction.
205+
if (!Subtarget.hasP10Vector() && isPPC64)
206+
setOperationAction(ISD::SSUBO, MVT::i32, Custom);
207+
203208
// Match BITREVERSE to customized fast code sequence in the td file.
204209
setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
205210
setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
@@ -12020,6 +12025,27 @@ SDValue PPCTargetLowering::LowerUaddo(SDValue Op, SelectionDAG &DAG) const {
1202012025
return Res;
1202112026
}
1202212027

12028+
SDValue PPCTargetLowering::LowerSSUBO(SDValue Op, SelectionDAG &DAG) const {
12029+
12030+
SDLoc dl(Op);
12031+
SDValue LHS = Op.getOperand(0);
12032+
SDValue RHS = Op.getOperand(1);
12033+
12034+
SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, LHS, RHS);
12035+
12036+
SDValue Xor1 = DAG.getNode(ISD::XOR, dl, MVT::i32, RHS, LHS);
12037+
SDValue Xor2 = DAG.getNode(ISD::XOR, dl, MVT::i32, Sub, LHS);
12038+
12039+
SDValue And = DAG.getNode(ISD::AND, dl, MVT::i32, Xor1, Xor2);
12040+
12041+
SDValue Overflow = DAG.getNode(ISD::SRL, dl, MVT::i32, And,
12042+
DAG.getConstant(31, dl, MVT::i32));
12043+
SDValue OverflowTrunc =
12044+
DAG.getNode(ISD::TRUNCATE, dl, Op.getNode()->getValueType(1), Overflow);
12045+
12046+
return DAG.getMergeValues({Sub, OverflowTrunc}, dl);
12047+
}
12048+
1202312049
/// LowerOperation - Provide custom lowering hooks for some operations.
1202412050
///
1202512051
SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
@@ -12042,6 +12068,8 @@ SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1204212068
case ISD::SETCC: return LowerSETCC(Op, DAG);
1204312069
case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
1204412070
case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
12071+
case ISD::SSUBO:
12072+
return LowerSSUBO(Op, DAG);
1204512073

1204612074
case ISD::INLINEASM:
1204712075
case ISD::INLINEASM_BR: return LowerINLINEASM(Op, DAG);

llvm/lib/Target/PowerPC/PPCISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,7 @@ namespace llvm {
12791279
SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
12801280
SDValue LowerUaddo(SDValue Op, SelectionDAG &DAG) const;
12811281
SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
1282+
SDValue LowerSSUBO(SDValue Op, SelectionDAG &DAG) const;
12821283
SDValue LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
12831284
SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
12841285
SDValue LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const;

llvm/test/CodeGen/PowerPC/saddo-ssubo.ll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,11 @@ entry:
129129
define i1 @test_ssubo_i32(i32 %a, i32 %b) nounwind {
130130
; CHECK-LABEL: test_ssubo_i32:
131131
; CHECK: # %bb.0: # %entry
132-
; CHECK-NEXT: sub 5, 3, 4
133-
; CHECK-NEXT: cmpwi 1, 4, 0
134-
; CHECK-NEXT: cmpw 5, 3
135-
; CHECK-NEXT: li 3, 1
136-
; CHECK-NEXT: creqv 20, 5, 0
137-
; CHECK-NEXT: isel 3, 0, 3, 20
132+
; CHECK-NEXT: xor 5, 4, 3
133+
; CHECK-NEXT: sub 4, 3, 4
134+
; CHECK-NEXT: xor 3, 4, 3
135+
; CHECK-NEXT: and 3, 5, 3
136+
; CHECK-NEXT: srwi 3, 3, 31
138137
; CHECK-NEXT: blr
139138
entry:
140139
%res = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a, i32 %b) nounwind

0 commit comments

Comments
 (0)