@@ -129,7 +129,8 @@ class SelectionDAGLegalize {
129129 ArrayRef<int > Mask) const ;
130130
131131 std::pair<SDValue, SDValue> ExpandLibCall (RTLIB::Libcall LC, SDNode *Node,
132- TargetLowering::ArgListTy &&Args, bool isSigned);
132+ TargetLowering::ArgListTy &&Args,
133+ bool IsSigned, EVT RetVT);
133134 std::pair<SDValue, SDValue> ExpandLibCall (RTLIB::Libcall LC, SDNode *Node, bool isSigned);
134135
135136 void ExpandFPLibCall (SDNode *Node, RTLIB::Libcall LC,
@@ -150,6 +151,9 @@ class SelectionDAGLegalize {
150151 RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
151152 RTLIB::Libcall Call_PPCF128,
152153 SmallVectorImpl<SDValue> &Results);
154+ SDValue ExpandBitCountingLibCall (SDNode *Node, RTLIB::Libcall CallI32,
155+ RTLIB::Libcall CallI64,
156+ RTLIB::Libcall CallI128);
153157 void ExpandDivRemLibCall (SDNode *Node, SmallVectorImpl<SDValue> &Results);
154158 void ExpandSinCosLibCall (SDNode *Node, SmallVectorImpl<SDValue> &Results);
155159
@@ -2114,9 +2118,10 @@ SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) {
21142118// register, return the lo part and set the hi part to the by-reg argument in
21152119// the first. If it does fit into a single register, return the result and
21162120// leave the Hi part unset.
2117- std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall (RTLIB::Libcall LC, SDNode *Node,
2118- TargetLowering::ArgListTy &&Args,
2119- bool isSigned) {
2121+ std::pair<SDValue, SDValue>
2122+ SelectionDAGLegalize::ExpandLibCall (RTLIB::Libcall LC, SDNode *Node,
2123+ TargetLowering::ArgListTy &&Args,
2124+ bool IsSigned, EVT RetVT) {
21202125 EVT CodePtrTy = TLI.getPointerTy (DAG.getDataLayout ());
21212126 SDValue Callee;
21222127 if (const char *LibcallName = TLI.getLibcallName (LC))
@@ -2127,7 +2132,6 @@ std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall L
21272132 Node->getOperationName (&DAG));
21282133 }
21292134
2130- EVT RetVT = Node->getValueType (0 );
21312135 Type *RetTy = RetVT.getTypeForEVT (*DAG.getContext ());
21322136
21332137 // By default, the input chain to this libcall is the entry node of the
@@ -2147,7 +2151,7 @@ std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall L
21472151 InChain = TCChain;
21482152
21492153 TargetLowering::CallLoweringInfo CLI (DAG);
2150- bool signExtend = TLI.shouldSignExtendTypeInLibCall (RetTy, isSigned );
2154+ bool signExtend = TLI.shouldSignExtendTypeInLibCall (RetTy, IsSigned );
21512155 CLI.setDebugLoc (SDLoc (Node))
21522156 .setChain (InChain)
21532157 .setLibCallee (TLI.getLibcallCallingConv (LC), RetTy, Callee,
@@ -2183,7 +2187,8 @@ std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall L
21832187 Args.push_back (Entry);
21842188 }
21852189
2186- return ExpandLibCall (LC, Node, std::move (Args), isSigned);
2190+ return ExpandLibCall (LC, Node, std::move (Args), isSigned,
2191+ Node->getValueType (0 ));
21872192}
21882193
21892194void SelectionDAGLegalize::ExpandFPLibCall (SDNode* Node,
@@ -2259,6 +2264,50 @@ void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node,
22592264 ExpandFPLibCall (Node, LC, Results);
22602265}
22612266
2267+ SDValue SelectionDAGLegalize::ExpandBitCountingLibCall (
2268+ SDNode *Node, RTLIB::Libcall CallI32, RTLIB::Libcall CallI64,
2269+ RTLIB::Libcall CallI128) {
2270+ RTLIB::Libcall LC;
2271+ switch (Node->getSimpleValueType (0 ).SimpleTy ) {
2272+ default :
2273+ llvm_unreachable (" Unexpected request for libcall!" );
2274+ case MVT::i32 :
2275+ LC = CallI32;
2276+ break ;
2277+ case MVT::i64 :
2278+ LC = CallI64;
2279+ break ;
2280+ case MVT::i128 :
2281+ LC = CallI128;
2282+ break ;
2283+ }
2284+
2285+ // Bit-counting libcalls have one unsigned argument and return `int`.
2286+ // Note that `int` may be illegal on this target; ExpandLibCall will
2287+ // take care of promoting it to a legal type.
2288+ SDValue Op = Node->getOperand (0 );
2289+ EVT IntVT =
2290+ EVT::getIntegerVT (*DAG.getContext (), DAG.getLibInfo ().getIntSize ());
2291+
2292+ TargetLowering::ArgListEntry Arg;
2293+ EVT ArgVT = Op.getValueType ();
2294+ Type *ArgTy = ArgVT.getTypeForEVT (*DAG.getContext ());
2295+ Arg.Node = Op;
2296+ Arg.Ty = ArgTy;
2297+ Arg.IsSExt = TLI.shouldSignExtendTypeInLibCall (ArgTy, /* IsSigned=*/ false );
2298+ Arg.IsZExt = !Arg.IsSExt ;
2299+
2300+ SDValue Res = ExpandLibCall (LC, Node, TargetLowering::ArgListTy{Arg},
2301+ /* IsSigned=*/ true , IntVT)
2302+ .first ;
2303+
2304+ // If ExpandLibCall created a tail call, the result was already
2305+ // of the correct type. Otherwise, we need to sign extend it.
2306+ if (Res.getValueType () != MVT::Other)
2307+ Res = DAG.getSExtOrTrunc (Res, SDLoc (Node), Node->getValueType (0 ));
2308+ return Res;
2309+ }
2310+
22622311// / Issue libcalls to __{u}divmod to compute div / rem pairs.
22632312void
22642313SelectionDAGLegalize::ExpandDivRemLibCall (SDNode *Node,
@@ -4993,19 +5042,12 @@ void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
49935042 RTLIB::MUL_I64, RTLIB::MUL_I128));
49945043 break ;
49955044 case ISD::CTLZ_ZERO_UNDEF:
4996- switch (Node->getSimpleValueType (0 ).SimpleTy ) {
4997- default :
4998- llvm_unreachable (" LibCall explicitly requested, but not available" );
4999- case MVT::i32 :
5000- Results.push_back (ExpandLibCall (RTLIB::CTLZ_I32, Node, false ).first );
5001- break ;
5002- case MVT::i64 :
5003- Results.push_back (ExpandLibCall (RTLIB::CTLZ_I64, Node, false ).first );
5004- break ;
5005- case MVT::i128 :
5006- Results.push_back (ExpandLibCall (RTLIB::CTLZ_I128, Node, false ).first );
5007- break ;
5008- }
5045+ Results.push_back (ExpandBitCountingLibCall (
5046+ Node, RTLIB::CTLZ_I32, RTLIB::CTLZ_I64, RTLIB::CTLZ_I128));
5047+ break ;
5048+ case ISD::CTPOP:
5049+ Results.push_back (ExpandBitCountingLibCall (
5050+ Node, RTLIB::CTPOP_I32, RTLIB::CTPOP_I64, RTLIB::CTPOP_I128));
50095051 break ;
50105052 case ISD::RESET_FPENV: {
50115053 // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets
0 commit comments