[PATCH] D128911: Emit table lookup from TargetLowering::expandCTTZ()

Shubham Narlawar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 8 05:03:17 PDT 2022


gsocshubham updated this revision to Diff 443214.
gsocshubham added a comment.

Added table lookup in TargetLowering::expandCTTZ()


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128911/new/

https://reviews.llvm.org/D128911

Files:
  llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp


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<Constant *> 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<ConstantSDNode>(Index);
+    ConstantInt *temp = const_cast<ConstantInt *>(IndexNode->getConstantIntValue());
+    Constant *ConstantData =  dyn_cast<Constant >(temp);
+    Elt.push_back(ConstantData);
+  }
+  ArrayRef<Constant *> 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<ConstantPoolSDNode>(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,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128911.443214.patch
Type: text/x-patch
Size: 2203 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220708/34b32570/attachment.bin>


More information about the llvm-commits mailing list