[llvm] [SPARC] Use hardware byteswapper when we have V9 (PR #191720)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 05:45:45 PDT 2026


================
@@ -3001,6 +3004,37 @@ static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
   return DAG.getMergeValues(Ops, dl);
 }
 
+SDValue SparcTargetLowering::LowerBSWAP(SDValue Op, SelectionDAG &DAG) const {
+  // We don't have an in-register bswap, so expand bswap(x) into
+  // load(store-swapped(x)). The reason the swap is done during the store is
+  // that on some implementations (mainly older ones) ASI-tagged memory
+  // operations are not pipelined, and generally stores finish faster than
+  // loads.
+
+  MachineFunction &MF = DAG.getMachineFunction();
+  MachineFrameInfo &MFI = MF.getFrameInfo();
+  auto PtrVT = getPointerTy(DAG.getDataLayout());
+  SDValue Chain = DAG.getEntryNode();
+  bool IsLittleEndian = DAG.getDataLayout().isLittleEndian();
+  SDLoc DL(Op);
+
+  SDValue BSwapOp = Op.getOperand(0);
+  EVT BSwapVT = BSwapOp.getValueType();
+
+  // Create a stack object to serve as temporary storage.
+  int BSwapFI = MFI.CreateStackObject(BSwapVT.getStoreSize(), Align(8), false);
+  SDValue BSwapPtr = DAG.getFrameIndex(BSwapFI, PtrVT);
+
+  // Store-swap the value, then load it back.
+  SDValue Ops[] = {Chain, BSwapOp, BSwapPtr, DAG.getValueType(BSwapVT)};
+  SDValue SwappedMem = DAG.getMemIntrinsicNode(
+      IsLittleEndian ? SPISD::STORE_BIG : SPISD::STORE_LITTLE, DL,
+      DAG.getVTList(MVT::Other), Ops, BSwapVT, MachinePointerInfo(), Align(8),
+      MachineMemOperand::MOStore);
+  return DAG.getLoad(BSwapVT, DL, SwappedMem, BSwapPtr, MachinePointerInfo(),
+                     Align(8));
----------------
s-barannikov wrote:

* These should have more specific MachinePointerInfo.
* Not sure if you need to specify alignment here, I think it could be inferred from the loaded/stored value?


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


More information about the llvm-commits mailing list