Index: llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp =================================================================== --- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -7835,7 +7835,48 @@ DAG.getNode(ISD::CTLZ, dl, VT, Tmp)); } - return DAG.getNode(ISD::CTPOP, dl, VT, Tmp); + // Emit table lookup by reverting to original IR + // table[((unsigned)((x & -x) * 0x077CB531U)) >> 27] + SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, dl, VT), Op); + SDValue Lookup = DAG.getNode( + ISD::SRL, dl, VT, + DAG.getNode(ISD::MUL, dl, VT, DAG.getNode(ISD::AND, dl, VT, Op, Neg), + DAG.getConstant(0x077CB531U, dl, VT)), + DAG.getConstant(27, dl, VT)); + + // Create a table in constant data pool + std::vector Elt; + unsigned int RshrArr[NumBitsPerElt]; + unsigned int DeBrujin = 0x077CB531U; + + for (unsigned int i = 0; i < NumBitsPerElt; i++) { + unsigned int Lshr = DeBrujin << i; + unsigned int Rshr = Lshr >> 27; + RshrArr[Rshr] = i; + } + + for (unsigned int i = 0; i < NumBitsPerElt; i++) { + SDValue Index = DAG.getConstant(RshrArr[i], dl, VT); + ConstantSDNode *IndexNode = dyn_cast(Index); + ConstantInt *temp = const_cast(IndexNode->getConstantIntValue()); + Constant *ConstantData = dyn_cast(temp); + Elt.push_back(ConstantData); + } + ArrayRef Elts = Elt; + auto *FPTy = Elts[0]->getType(); + const DataLayout &TD = DAG.getDataLayout(); + + // Create a ConstantArray of NumBitsPerElt constant. + auto *CA = ConstantArray::get(ArrayType::get(FPTy, NumBitsPerElt), Elts); + SDValue CPIdx = DAG.getConstantPool(CA, getPointerTy(DAG.getDataLayout()), + TD.getPrefTypeAlign(FPTy)); + Align Alignment = cast(CPIdx)->getAlign(); + + // Index into the table + return DAG.getLoad(VT, dl, Lookup, CPIdx, + MachinePointerInfo::getConstantPool( + DAG.getMachineFunction()), Alignment); + } SDValue TargetLowering::expandABS(SDNode *N, SelectionDAG &DAG,