[llvm] [IR] Implementation and lowering of bitinsert and bitextract (PR #200605)

Pedro Lobo via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 12 11:02:45 PDT 2026


================
@@ -4193,6 +4193,85 @@ void SelectionDAGBuilder::visitInsertElement(const User &I) {
                            InVec, InVal, InIdx));
 }
 
+void SelectionDAGBuilder::visitBitInsert(const User &I) {
+  SDValue Base = getValue(I.getOperand(0));
+  SDValue Val = getValue(I.getOperand(1));
+  SDValue Offset = getValue(I.getOperand(2));
+  EVT BaseVT = Base.getValueType();
+  EVT ValVT = Val.getValueType();
+  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
+  SDLoc dl = getCurSDLoc();
+
+  assert(BaseVT.getSizeInBits() >= ValVT.getSizeInBits() &&
+         "bitinsert val wider than base should be rejected by verifier");
+
+  // If Val is a float, cast it to an integer of the same bitwidth
+  // so DAG.getZExtOrTrunc can process it safely.
+  if (ValVT.isFloatingPoint()) {
+    ValVT = EVT::getIntegerVT(*DAG.getContext(), ValVT.getSizeInBits());
+    Val = DAG.getBitcast(ValVT, Val);
+  }
+
+  SDValue LegalOffset = DAG.getZExtOrTrunc(Offset, dl, BaseVT);
+
+  // Legalize rotate amount to the target's shift amount type.
+  EVT ShiftAmtTy = TLI.getShiftAmountTy(BaseVT, DAG.getDataLayout());
+  SDValue LegalRotateAmount = DAG.getZExtOrTrunc(LegalOffset, dl, ShiftAmtTy);
+
+  SDValue RotatedBase =
+      DAG.getNode(ISD::ROTR, dl, BaseVT, Base, LegalRotateAmount);
----------------
pedroclobo wrote:

We changed it to shift in 54d315a42dbf.

https://github.com/llvm/llvm-project/pull/200605


More information about the llvm-commits mailing list