Skip to content

Commit 0607c8d

Browse files
author
Esme-Yi
committed
[PowerPC] Legalize SREM/UREM directly on P9.
Summary: As Bugzilla-35090 reported, the rationale for using custom lowering SREM/UREM should no longer be true. At the IR level, the div-rem-pairs pass performs the transformation where the remainder is computed from the result of the division when both a required. We should now be able to lower these directly on P9. And the pass also fixed the problem that divide is in a different block than the remainder. This is a patch to remove redundant code and make SREM/UREM legal directly on P9. Reviewed By: lkail Differential Revision: https://reviews.llvm.org/D82145
1 parent 5689b38 commit 0607c8d

3 files changed

Lines changed: 27 additions & 36 deletions

File tree

‎llvm/lib/Target/PowerPC/PPCISelLowering.cpp‎

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,16 @@ PPCTargetLowering::PPCTargetLowering(const PPCTargetMachine &TM,
261261

262262
// PowerPC has no SREM/UREM instructions unless we are on P9
263263
// On P9 we may use a hardware instruction to compute the remainder.
264-
// The instructions are not legalized directly because in the cases where the
265-
// result of both the remainder and the division is required it is more
266-
// efficient to compute the remainder from the result of the division rather
267-
// than use the remainder instruction.
264+
// When the result of both the remainder and the division is required it is
265+
// more efficient to compute the remainder from the result of the division
266+
// rather than use the remainder instruction. The instructions are legalized
267+
// directly because the DivRemPairsPass performs the transformation at the IR
268+
// level.
268269
if (Subtarget.isISA3_0()) {
269-
setOperationAction(ISD::SREM, MVT::i32, Custom);
270-
setOperationAction(ISD::UREM, MVT::i32, Custom);
271-
setOperationAction(ISD::SREM, MVT::i64, Custom);
272-
setOperationAction(ISD::UREM, MVT::i64, Custom);
270+
setOperationAction(ISD::SREM, MVT::i32, Legal);
271+
setOperationAction(ISD::UREM, MVT::i32, Legal);
272+
setOperationAction(ISD::SREM, MVT::i64, Legal);
273+
setOperationAction(ISD::UREM, MVT::i64, Legal);
273274
} else {
274275
setOperationAction(ISD::SREM, MVT::i32, Expand);
275276
setOperationAction(ISD::UREM, MVT::i32, Expand);
@@ -10492,18 +10493,6 @@ SDValue PPCTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1049210493
return SDValue();
1049310494
}
1049410495

10495-
SDValue PPCTargetLowering::LowerREM(SDValue Op, SelectionDAG &DAG) const {
10496-
// Check for a DIV with the same operands as this REM.
10497-
for (auto UI : Op.getOperand(1)->uses()) {
10498-
if ((Op.getOpcode() == ISD::SREM && UI->getOpcode() == ISD::SDIV) ||
10499-
(Op.getOpcode() == ISD::UREM && UI->getOpcode() == ISD::UDIV))
10500-
if (UI->getOperand(0) == Op.getOperand(0) &&
10501-
UI->getOperand(1) == Op.getOperand(1))
10502-
return SDValue();
10503-
}
10504-
return Op;
10505-
}
10506-
1050710496
// Lower scalar BSWAP64 to xxbrd.
1050810497
SDValue PPCTargetLowering::LowerBSWAP(SDValue Op, SelectionDAG &DAG) const {
1050910498
SDLoc dl(Op);
@@ -11121,9 +11110,6 @@ SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1112111110

1112211111
case ISD::INTRINSIC_VOID:
1112311112
return LowerINTRINSIC_VOID(Op, DAG);
11124-
case ISD::SREM:
11125-
case ISD::UREM:
11126-
return LowerREM(Op, DAG);
1112711113
case ISD::BSWAP:
1112811114
return LowerBSWAP(Op, DAG);
1112911115
case ISD::ATOMIC_CMP_SWAP:

‎llvm/lib/Target/PowerPC/PPCISelLowering.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,6 @@ namespace llvm {
11191119
SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
11201120
SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
11211121
SDValue LowerINTRINSIC_VOID(SDValue Op, SelectionDAG &DAG) const;
1122-
SDValue LowerREM(SDValue Op, SelectionDAG &DAG) const;
11231122
SDValue LowerBSWAP(SDValue Op, SelectionDAG &DAG) const;
11241123
SDValue LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const;
11251124
SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const;

‎llvm/test/CodeGen/PowerPC/ppc64-P9-mod.ll‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,16 @@ entry:
8888
store i32 %div, i32* @div_resultsw, align 4
8989
ret void
9090
; CHECK-LABEL: modulo_div_sw
91-
; CHECK-NOT: modsw
92-
; CHECK: div
93-
; CHECK-NOT: modsw
94-
; CHECK: mull
95-
; CHECK-NOT: modsw
96-
; CHECK: sub
91+
; CHECK: modsw {{[0-9]+}}, 3, 4
9792
; CHECK: blr
93+
; CHECK-DRP-LABEL: modulo_div_sw
94+
; CHECK-DRP-NOT: modsw
95+
; CHECK-DRP: div
96+
; CHECK-DRP-NOT: modsw
97+
; CHECK-DRP: mull
98+
; CHECK-DRP-NOT: modsw
99+
; CHECK-DRP: sub
100+
; CHECK-DRP: blr
98101
; CHECK-PWR8-LABEL: modulo_div_sw
99102
; CHECK-PWR8: div
100103
; CHECK-PWR8: mull
@@ -129,13 +132,16 @@ entry:
129132
store i32 %div, i32* @div_resultuw, align 4
130133
ret void
131134
; CHECK-LABEL: modulo_div_uw
132-
; CHECK-NOT: modsw
133-
; CHECK: div
134-
; CHECK-NOT: modsw
135-
; CHECK: mull
136-
; CHECK-NOT: modsw
137-
; CHECK: sub
135+
; CHECK: moduw {{[0-9]+}}, 3, 4
138136
; CHECK: blr
137+
; CHECK-DRP-LABEL: modulo_div_uw
138+
; CHECK-DRP-NOT: moduw
139+
; CHECK-DRP: div
140+
; CHECK-DRP-NOT: moduw
141+
; CHECK-DRP: mull
142+
; CHECK-DRP-NOT: moduw
143+
; CHECK-DRP: sub
144+
; CHECK-DRP: blr
139145
; CHECK-PWR8-LABEL: modulo_div_uw
140146
; CHECK-PWR8: div
141147
; CHECK-PWR8: mull

0 commit comments

Comments
 (0)