Skip to content

Commit dcf1296

Browse files
authored
Revert "[SDag][ARM][RISCV] Allow lowering CTPOP into a libcall (#99752)"
This reverts commit 92e18ff.
1 parent 90617e9 commit dcf1296

17 files changed

+1612
-415
lines changed

llvm/include/llvm/IR/RuntimeLibcalls.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ HANDLE_LIBCALL(NEG_I64, "__negdi2")
8585
HANDLE_LIBCALL(CTLZ_I32, "__clzsi2")
8686
HANDLE_LIBCALL(CTLZ_I64, "__clzdi2")
8787
HANDLE_LIBCALL(CTLZ_I128, "__clzti2")
88-
HANDLE_LIBCALL(CTPOP_I32, "__popcountsi2")
89-
HANDLE_LIBCALL(CTPOP_I64, "__popcountdi2")
90-
HANDLE_LIBCALL(CTPOP_I128, "__popcountti2")
9188

9289
// Floating-point
9390
HANDLE_LIBCALL(ADD_F32, "__addsf3")

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,12 @@ class SelectionDAGLegalize {
140140
RTLIB::Libcall Call_F128,
141141
RTLIB::Libcall Call_PPCF128,
142142
SmallVectorImpl<SDValue> &Results);
143-
SDValue ExpandIntLibCall(SDNode *Node, bool IsSigned, RTLIB::Libcall Call_I8,
144-
RTLIB::Libcall Call_I16, RTLIB::Libcall Call_I32,
145-
RTLIB::Libcall Call_I64, RTLIB::Libcall Call_I128);
143+
SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
144+
RTLIB::Libcall Call_I8,
145+
RTLIB::Libcall Call_I16,
146+
RTLIB::Libcall Call_I32,
147+
RTLIB::Libcall Call_I64,
148+
RTLIB::Libcall Call_I128);
146149
void ExpandArgFPLibCall(SDNode *Node,
147150
RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64,
148151
RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
@@ -2206,7 +2209,7 @@ void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
22062209
ExpandFPLibCall(Node, LC, Results);
22072210
}
22082211

2209-
SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode *Node, bool IsSigned,
2212+
SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
22102213
RTLIB::Libcall Call_I8,
22112214
RTLIB::Libcall Call_I16,
22122215
RTLIB::Libcall Call_I32,
@@ -2221,9 +2224,7 @@ SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode *Node, bool IsSigned,
22212224
case MVT::i64: LC = Call_I64; break;
22222225
case MVT::i128: LC = Call_I128; break;
22232226
}
2224-
assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2225-
"LibCall explicitly requested, but not available");
2226-
return ExpandLibCall(LC, Node, IsSigned).first;
2227+
return ExpandLibCall(LC, Node, isSigned).first;
22272228
}
22282229

22292230
/// Expand the node to a libcall based on first argument type (for instance
@@ -4999,16 +5000,19 @@ void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
49995000
RTLIB::MUL_I64, RTLIB::MUL_I128));
50005001
break;
50015002
case ISD::CTLZ_ZERO_UNDEF:
5002-
Results.push_back(ExpandIntLibCall(Node, /*IsSigned=*/false,
5003-
RTLIB::UNKNOWN_LIBCALL,
5004-
RTLIB::UNKNOWN_LIBCALL, RTLIB::CTLZ_I32,
5005-
RTLIB::CTLZ_I64, RTLIB::CTLZ_I128));
5006-
break;
5007-
case ISD::CTPOP:
5008-
Results.push_back(ExpandIntLibCall(Node, /*IsSigned=*/false,
5009-
RTLIB::UNKNOWN_LIBCALL,
5010-
RTLIB::UNKNOWN_LIBCALL, RTLIB::CTPOP_I32,
5011-
RTLIB::CTPOP_I64, RTLIB::CTPOP_I128));
5003+
switch (Node->getSimpleValueType(0).SimpleTy) {
5004+
default:
5005+
llvm_unreachable("LibCall explicitly requested, but not available");
5006+
case MVT::i32:
5007+
Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false).first);
5008+
break;
5009+
case MVT::i64:
5010+
Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false).first);
5011+
break;
5012+
case MVT::i128:
5013+
Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false).first);
5014+
break;
5015+
}
50125016
break;
50135017
case ISD::RESET_FPENV: {
50145018
// It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets

llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3850,33 +3850,15 @@ void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
38503850
Hi = DAG.getConstant(0, dl, NVT);
38513851
}
38523852

3853-
void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N, SDValue &Lo, SDValue &Hi) {
3854-
SDValue Op = N->getOperand(0);
3855-
EVT VT = N->getValueType(0);
3856-
SDLoc DL(N);
3857-
3858-
if (TLI.getOperationAction(ISD::CTPOP, VT) == TargetLoweringBase::LibCall) {
3859-
RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3860-
if (VT == MVT::i32)
3861-
LC = RTLIB::CTPOP_I32;
3862-
else if (VT == MVT::i64)
3863-
LC = RTLIB::CTPOP_I64;
3864-
else if (VT == MVT::i128)
3865-
LC = RTLIB::CTPOP_I128;
3866-
assert(LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC) &&
3867-
"LibCall explicitly requested, but not available");
3868-
TargetLowering::MakeLibCallOptions CallOptions;
3869-
SDValue Res = TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, DL).first;
3870-
SplitInteger(Res, Lo, Hi);
3871-
return;
3872-
}
3873-
3853+
void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
3854+
SDValue &Lo, SDValue &Hi) {
3855+
SDLoc dl(N);
38743856
// ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
3875-
GetExpandedInteger(Op, Lo, Hi);
3857+
GetExpandedInteger(N->getOperand(0), Lo, Hi);
38763858
EVT NVT = Lo.getValueType();
3877-
Lo = DAG.getNode(ISD::ADD, DL, NVT, DAG.getNode(ISD::CTPOP, DL, NVT, Lo),
3878-
DAG.getNode(ISD::CTPOP, DL, NVT, Hi));
3879-
Hi = DAG.getConstant(0, DL, NVT);
3859+
Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
3860+
DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
3861+
Hi = DAG.getConstant(0, dl, NVT);
38803862
}
38813863

38823864
void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9171,9 +9171,8 @@ SDValue TargetLowering::expandCTTZ(SDNode *Node, SelectionDAG &DAG) const {
91719171
!isOperationLegalOrCustomOrPromote(ISD::XOR, VT)))
91729172
return SDValue();
91739173

9174-
// Emit Table Lookup if ISD::CTPOP used in the fallback path below is going
9175-
// to be expanded or converted to a libcall.
9176-
if (!VT.isVector() && !isOperationLegalOrCustomOrPromote(ISD::CTPOP, VT) &&
9174+
// Emit Table Lookup if ISD::CTLZ and ISD::CTPOP are not legal.
9175+
if (!VT.isVector() && isOperationExpand(ISD::CTPOP, VT) &&
91779176
!isOperationLegal(ISD::CTLZ, VT))
91789177
if (SDValue V = CTTZTableLookup(Node, DAG, dl, VT, Op, NumBitsPerElt))
91799178
return V;

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
393393
setOperationAction({ISD::CTTZ, ISD::CTTZ_ZERO_UNDEF}, MVT::i32, Custom);
394394
}
395395
} else {
396-
setOperationAction(ISD::CTTZ, XLenVT, Expand);
397-
if (!Subtarget.is64Bit())
398-
setOperationAction(ISD::CTPOP, MVT::i32, LibCall);
399-
setOperationAction(ISD::CTPOP, MVT::i64, LibCall);
396+
setOperationAction({ISD::CTTZ, ISD::CTPOP}, XLenVT, Expand);
400397
if (RV64LegalI32 && Subtarget.is64Bit())
401398
setOperationAction({ISD::CTTZ, ISD::CTPOP}, MVT::i32, Expand);
402399
}

llvm/test/Analysis/CostModel/RISCV/int-bit-manip.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ define void @bitreverse() {
159159

160160
define void @ctpop() {
161161
; NOZVBB-LABEL: 'ctpop'
162-
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %1 = call i8 @llvm.ctpop.i8(i8 undef)
162+
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %1 = call i8 @llvm.ctpop.i8(i8 undef)
163163
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %2 = call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> undef)
164164
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %3 = call <4 x i8> @llvm.ctpop.v4i8(<4 x i8> undef)
165165
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %4 = call <8 x i8> @llvm.ctpop.v8i8(<8 x i8> undef)
@@ -169,7 +169,7 @@ define void @ctpop() {
169169
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %8 = call <vscale x 4 x i8> @llvm.ctpop.nxv4i8(<vscale x 4 x i8> undef)
170170
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %9 = call <vscale x 8 x i8> @llvm.ctpop.nxv8i8(<vscale x 8 x i8> undef)
171171
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 12 for instruction: %10 = call <vscale x 16 x i8> @llvm.ctpop.nxv16i8(<vscale x 16 x i8> undef)
172-
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %11 = call i16 @llvm.ctpop.i16(i16 undef)
172+
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %11 = call i16 @llvm.ctpop.i16(i16 undef)
173173
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 19 for instruction: %12 = call <2 x i16> @llvm.ctpop.v2i16(<2 x i16> undef)
174174
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 19 for instruction: %13 = call <4 x i16> @llvm.ctpop.v4i16(<4 x i16> undef)
175175
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 19 for instruction: %14 = call <8 x i16> @llvm.ctpop.v8i16(<8 x i16> undef)
@@ -179,7 +179,7 @@ define void @ctpop() {
179179
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 19 for instruction: %18 = call <vscale x 4 x i16> @llvm.ctpop.nxv4i16(<vscale x 4 x i16> undef)
180180
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 19 for instruction: %19 = call <vscale x 8 x i16> @llvm.ctpop.nxv8i16(<vscale x 8 x i16> undef)
181181
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 19 for instruction: %20 = call <vscale x 16 x i16> @llvm.ctpop.nxv16i16(<vscale x 16 x i16> undef)
182-
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %21 = call i32 @llvm.ctpop.i32(i32 undef)
182+
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %21 = call i32 @llvm.ctpop.i32(i32 undef)
183183
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %22 = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> undef)
184184
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %23 = call <4 x i32> @llvm.ctpop.v4i32(<4 x i32> undef)
185185
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %24 = call <8 x i32> @llvm.ctpop.v8i32(<8 x i32> undef)
@@ -189,7 +189,7 @@ define void @ctpop() {
189189
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %28 = call <vscale x 4 x i32> @llvm.ctpop.nxv4i32(<vscale x 4 x i32> undef)
190190
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %29 = call <vscale x 8 x i32> @llvm.ctpop.nxv8i32(<vscale x 8 x i32> undef)
191191
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %30 = call <vscale x 16 x i32> @llvm.ctpop.nxv16i32(<vscale x 16 x i32> undef)
192-
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %31 = call i64 @llvm.ctpop.i64(i64 undef)
192+
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %31 = call i64 @llvm.ctpop.i64(i64 undef)
193193
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 21 for instruction: %32 = call <2 x i64> @llvm.ctpop.v2i64(<2 x i64> undef)
194194
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 21 for instruction: %33 = call <4 x i64> @llvm.ctpop.v4i64(<4 x i64> undef)
195195
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 21 for instruction: %34 = call <8 x i64> @llvm.ctpop.v8i64(<8 x i64> undef)
@@ -202,7 +202,7 @@ define void @ctpop() {
202202
; NOZVBB-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
203203
;
204204
; ZVBB-LABEL: 'ctpop'
205-
; ZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %1 = call i8 @llvm.ctpop.i8(i8 undef)
205+
; ZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %1 = call i8 @llvm.ctpop.i8(i8 undef)
206206
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %2 = call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> undef)
207207
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %3 = call <4 x i8> @llvm.ctpop.v4i8(<4 x i8> undef)
208208
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %4 = call <8 x i8> @llvm.ctpop.v8i8(<8 x i8> undef)
@@ -212,7 +212,7 @@ define void @ctpop() {
212212
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %8 = call <vscale x 4 x i8> @llvm.ctpop.nxv4i8(<vscale x 4 x i8> undef)
213213
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %9 = call <vscale x 8 x i8> @llvm.ctpop.nxv8i8(<vscale x 8 x i8> undef)
214214
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %10 = call <vscale x 16 x i8> @llvm.ctpop.nxv16i8(<vscale x 16 x i8> undef)
215-
; ZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %11 = call i16 @llvm.ctpop.i16(i16 undef)
215+
; ZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %11 = call i16 @llvm.ctpop.i16(i16 undef)
216216
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %12 = call <2 x i16> @llvm.ctpop.v2i16(<2 x i16> undef)
217217
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %13 = call <4 x i16> @llvm.ctpop.v4i16(<4 x i16> undef)
218218
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %14 = call <8 x i16> @llvm.ctpop.v8i16(<8 x i16> undef)
@@ -222,7 +222,7 @@ define void @ctpop() {
222222
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %18 = call <vscale x 4 x i16> @llvm.ctpop.nxv4i16(<vscale x 4 x i16> undef)
223223
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %19 = call <vscale x 8 x i16> @llvm.ctpop.nxv8i16(<vscale x 8 x i16> undef)
224224
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %20 = call <vscale x 16 x i16> @llvm.ctpop.nxv16i16(<vscale x 16 x i16> undef)
225-
; ZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %21 = call i32 @llvm.ctpop.i32(i32 undef)
225+
; ZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %21 = call i32 @llvm.ctpop.i32(i32 undef)
226226
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %22 = call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> undef)
227227
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %23 = call <4 x i32> @llvm.ctpop.v4i32(<4 x i32> undef)
228228
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %24 = call <8 x i32> @llvm.ctpop.v8i32(<8 x i32> undef)
@@ -232,7 +232,7 @@ define void @ctpop() {
232232
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %28 = call <vscale x 4 x i32> @llvm.ctpop.nxv4i32(<vscale x 4 x i32> undef)
233233
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %29 = call <vscale x 8 x i32> @llvm.ctpop.nxv8i32(<vscale x 8 x i32> undef)
234234
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %30 = call <vscale x 16 x i32> @llvm.ctpop.nxv16i32(<vscale x 16 x i32> undef)
235-
; ZVBB-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %31 = call i64 @llvm.ctpop.i64(i64 undef)
235+
; ZVBB-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %31 = call i64 @llvm.ctpop.i64(i64 undef)
236236
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %32 = call <2 x i64> @llvm.ctpop.v2i64(<2 x i64> undef)
237237
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %33 = call <4 x i64> @llvm.ctpop.v4i64(<4 x i64> undef)
238238
; ZVBB-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %34 = call <8 x i64> @llvm.ctpop.v8i64(<8 x i64> undef)

0 commit comments

Comments
 (0)