llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-sparc

Author: Brad Smith (brad0)

<details>
<summary>Changes</summary>

Replaces #<!-- -->241629, which was closed by a bad force-push.

Implement 64-bit atomics for 32-bit SPARC when targeting V9 in V8+
mode, mirroring what GCC does:

- 64-bit atomic loads and stores use ldd/std (GCC's atomic_loaddi_1
  and atomic_storedi_1).
- 64-bit compare-and-swap packs the 32-bit halves into %g/%o
  registers, issues casx and splits the result back, using the same
  sequence as GCC's *atomic_compare_and_swapdi_v8plus. Only %g/%o
  registers are used for the 64-bit temporaries since those are the
  only registers whose upper halves are preserved under the V8+ ABI
  (GCC's "h" constraint / I64_REGS class, minus %o7, which holds the
  return address in leaf procedures). The pseudo is expanded after
  register allocation so no 64-bit value is spilled via a 32-bit slot.
- Other atomic RMW operations are expanded to cmpxchg loops.

The new %g/%o register class can allocate %o registers before %l0 is
used, which breaks the assumption behind the leaf procedure %i -&gt; %o
remapping. When a function uses the 64-bit compare-and-swap, don't
treat it as a leaf procedure if any %iN and %oN are both used.

On the clang side, only report 64-bit atomics as lock-free / inline
for 32-bit SPARC when the CPU is V9 and V8+ is enabled, as GCC does,
deciding this in setMaxAtomicWidth(). Plain V8 CPUs without CASA no
longer report any atomics as lock-free.
The previous check against the CPU in the SparcV8TargetInfo
constructor could never be true since setCPU() runs later.
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 is likewise only defined in V8+
mode, and the 1, 2 and 4 byte variants are now also defined for the
LEON and Myriad 2 CPUs with CASA.

Fixes #<!-- -->81711.

Co-authored-by: Mintsuki &lt;mintsuki@<!-- -->protonmail.com&gt;

---

Patch is 36.15 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/242578.diff


11 Files Affected:

- (modified) clang/lib/Basic/Targets/Sparc.cpp (+29-2) 
- (modified) clang/lib/Basic/Targets/Sparc.h (+21-6) 
- (modified) clang/test/Preprocessor/init.c (+1) 
- (modified) clang/test/Preprocessor/predefined-arch-macros.c (+38) 
- (modified) llvm/lib/Target/Sparc/SparcFrameLowering.cpp (+18-6) 
- (modified) llvm/lib/Target/Sparc/SparcISelLowering.cpp (+60-4) 
- (modified) llvm/lib/Target/Sparc/SparcInstrInfo.cpp (+46-2) 
- (modified) llvm/lib/Target/Sparc/SparcInstrInfo.td (+15) 
- (modified) llvm/lib/Target/Sparc/SparcMachineFunctionInfo.h (+7) 
- (modified) llvm/lib/Target/Sparc/SparcRegisterInfo.td (+10) 
- (added) llvm/test/CodeGen/SPARC/atomics-v8plus.ll (+498) 


``````````diff
diff --git a/clang/lib/Basic/Targets/Sparc.cpp 
b/clang/lib/Basic/Targets/Sparc.cpp
index c4f3a64cc2ef7..f8cf9452cf1e8 100644
--- a/clang/lib/Basic/Targets/Sparc.cpp
+++ b/clang/lib/Basic/Targets/Sparc.cpp
@@ -156,6 +156,31 @@ SparcTargetInfo::CPUFamily 
SparcTargetInfo::getCPUFamily(CPUKind Kind) const {
   return Item->Family;
 }
 
+// The CPUs that the backend gives the LeonCASA feature.
+bool SparcTargetInfo::hasLeonCASA(CPUKind Kind) {
+  switch (Kind) {
+  case CK_MYRIAD2100:
+  case CK_MYRIAD2150:
+  case CK_MYRIAD2155:
+  case CK_MYRIAD2450:
+  case CK_MYRIAD2455:
+  case CK_MYRIAD2x5x:
+  case CK_MYRIAD2080:
+  case CK_MYRIAD2085:
+  case CK_MYRIAD2480:
+  case CK_MYRIAD2485:
+  case CK_MYRIAD2x8x:
+  case CK_LEON3:
+  case CK_LEON3_GR712RC:
+  case CK_LEON4:
+  case CK_LEON4_GR740:
+  case CK_LEON5:
+    return true;
+  default:
+    return false;
+  }
+}
+
 SparcTargetInfo::CPUKind SparcTargetInfo::getCPUKind(StringRef Name) const {
   const SparcCPUInfo *Item = llvm::find_if(
       CPUInfo, [Name](const SparcCPUInfo &Info) { return Info.Name == Name; });
@@ -199,11 +224,13 @@ void SparcV8TargetInfo::getTargetDefines(const 
LangOptions &Opts,
   }
   if (getCPUFamily(CPU) == CF_LEON)
     Builder.defineMacro("__leon__");
-  if (getCPUGeneration(CPU) == CG_V9) {
+  if (getCPUGeneration(CPU) == CG_V9 || hasLeonCASA(CPU)) {
     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
-    Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
+    // As with GCC, 64-bit compare-and-swap is only inlined in V8+ mode.
+    if (getCPUGeneration(CPU) == CG_V9 && V8Plus)
+      Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
   }
   if (LongDoubleWidth == 128)
     Builder.defineMacro("__LONG_DOUBLE_128__");
diff --git a/clang/lib/Basic/Targets/Sparc.h b/clang/lib/Basic/Targets/Sparc.h
index a3d9b2d73d137..2b5a55b6d91d3 100644
--- a/clang/lib/Basic/Targets/Sparc.h
+++ b/clang/lib/Basic/Targets/Sparc.h
@@ -134,6 +134,7 @@ class LLVM_LIBRARY_VISIBILITY SparcTargetInfo : public 
TargetInfo {
   CPUGeneration getCPUGeneration(CPUKind Kind) const;
 
   CPUFamily getCPUFamily(CPUKind Kind) const;
+  static bool hasLeonCASA(CPUKind Kind);
 
   CPUKind getCPUKind(StringRef Name) const;
 
@@ -155,6 +156,8 @@ class LLVM_LIBRARY_VISIBILITY SparcTargetInfo : public 
TargetInfo {
 
 // SPARC v8 is the 32-bit mode selected by Triple::sparc.
 class LLVM_LIBRARY_VISIBILITY SparcV8TargetInfo : public SparcTargetInfo {
+  bool V8Plus = false;
+
 public:
   SparcV8TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
       : SparcTargetInfo(Triple, Opts) {
@@ -186,15 +189,27 @@ class LLVM_LIBRARY_VISIBILITY SparcV8TargetInfo : public 
SparcTargetInfo {
       LongDoubleFormat = &llvm::APFloat::IEEEquad();
     }
 
-    // Up to 32 bits (V8) or 64 bits (V9) are lock-free atomic, but we're
-    // willing to do atomic ops on up to 64 bits.
+    // We're willing to do atomic ops on up to 64 bits. Which of those are
+    // lock-free is decided in setMaxAtomicWidth.
     MaxAtomicPromoteWidth = 64;
+  }
+
+  bool handleTargetFeatures(std::vector<std::string> &Features,
+                            DiagnosticsEngine &Diags) override {
+    if (!SparcTargetInfo::handleTargetFeatures(Features, Diags))
+      return false;
+    V8Plus = llvm::is_contained(Features, "+v8plus");
+    return true;
+  }
+
+  void setMaxAtomicWidth() override {
     if (getCPUGeneration(CPU) == CG_V9)
-      MaxAtomicInlineWidth = 64;
+      // Like GCC, 64-bit atomics in 32-bit code need V8+ mode, where the
+      // %g and %o registers are 64 bits wide.
+      MaxAtomicInlineWidth = V8Plus ? 64 : 32;
     else
-      // FIXME: This isn't correct for plain V8 which lacks CAS,
-      // only for LEON 3+ and Myriad.
-      MaxAtomicInlineWidth = 32;
+      // Plain V8 lacks CAS; only the LEON and Myriad 2 CPUs with CASA have it.
+      MaxAtomicInlineWidth = hasLeonCASA(CPU) ? 32 : 0;
   }
 
   void getTargetDefines(const LangOptions &Opts,
diff --git a/clang/test/Preprocessor/init.c b/clang/test/Preprocessor/init.c
index 6bcce58cd18d7..2d5f37c6831c0 100644
--- a/clang/test/Preprocessor/init.c
+++ b/clang/test/Preprocessor/init.c
@@ -1050,6 +1050,7 @@
 // SPARC:#define __FLT_MIN_EXP__ (-125)
 // SPARC:#define __FLT_MIN__ 1.17549435e-38F
 // SPARC:#define __FLT_RADIX__ 2
+// SPARC:#define __GCC_ATOMIC_INT_LOCK_FREE 1
 // SPARC:#define __GCC_ATOMIC_LLONG_LOCK_FREE 1
 // SPARC:#define __INT16_C(c) c
 // SPARC:#define __INT16_C_SUFFIX__
diff --git a/clang/test/Preprocessor/predefined-arch-macros.c 
b/clang/test/Preprocessor/predefined-arch-macros.c
index c732348450075..108bbeac3a474 100644
--- a/clang/test/Preprocessor/predefined-arch-macros.c
+++ b/clang/test/Preprocessor/predefined-arch-macros.c
@@ -4822,6 +4822,44 @@
 // CHECK_SPARC_SOLARIS_GCC_ATOMICS: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 
1
 // CHECK_SPARC_SOLARIS_GCC_ATOMICS: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 
1
 
+// RUN: %clang -E -dM %s -o - 2>&1 \
+// RUN:     -target sparc-sun-solaris \
+// RUN:   | FileCheck -match-full-lines %s 
-check-prefix=CHECK_SPARC_V8PLUS_LOCK_FREE
+// RUN: %clang -mcpu=v9 -mv8plus -E -dM %s -o - 2>&1 \
+// RUN:     -target sparc-unknown-linux \
+// RUN:   | FileCheck -match-full-lines %s 
-check-prefixes=CHECK_SPARC_V8PLUS_LOCK_FREE,CHECK_SPARC_V8PLUS_CAS8
+// CHECK_SPARC_V8PLUS_LOCK_FREE: #define __GCC_ATOMIC_LLONG_LOCK_FREE 2
+// CHECK_SPARC_V8PLUS_CAS8: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1
+
+// Without V8+, 64-bit atomics are not lock-free in 32-bit code.
+// RUN: %clang -mcpu=v9 -E -dM %s -o - 2>&1 \
+// RUN:     -target sparc-unknown-linux \
+// RUN:   | FileCheck -match-full-lines %s 
-check-prefix=CHECK_SPARC_V9_NO_V8PLUS
+// RUN: %clang -mcpu=v9 -mno-v8plus -E -dM %s -o - 2>&1 \
+// RUN:     -target sparc-sun-solaris \
+// RUN:   | FileCheck -match-full-lines %s 
-check-prefix=CHECK_SPARC_V9_NO_V8PLUS
+// CHECK_SPARC_V9_NO_V8PLUS: #define __GCC_ATOMIC_LLONG_LOCK_FREE 1
+// CHECK_SPARC_V9_NO_V8PLUS: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
+// CHECK_SPARC_V9_NO_V8PLUS-NOT: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1
+
+// Plain V8 has no CAS, so no atomics are lock-free; LEON/Myriad CASA CPUs
+// have 32-bit ones.
+// RUN: %clang -mcpu=v8 -E -dM %s -o - 2>&1 \
+// RUN:     -target sparc-unknown-elf \
+// RUN:   | FileCheck -match-full-lines %s -check-prefix=CHECK_SPARC_NO_CAS
+// RUN: %clang -mcpu=leon2 -E -dM %s -o - 2>&1 \
+// RUN:     -target sparc-unknown-elf \
+// RUN:   | FileCheck -match-full-lines %s -check-prefix=CHECK_SPARC_NO_CAS
+// CHECK_SPARC_NO_CAS: #define __GCC_ATOMIC_INT_LOCK_FREE 1
+// CHECK_SPARC_NO_CAS-NOT: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
+// RUN: %clang -mcpu=leon3 -E -dM %s -o - 2>&1 \
+// RUN:     -target sparc-unknown-elf \
+// RUN:   | FileCheck -match-full-lines %s -check-prefix=CHECK_SPARC_CASA
+// CHECK_SPARC_CASA: #define __GCC_ATOMIC_INT_LOCK_FREE 2
+// CHECK_SPARC_CASA: #define __GCC_ATOMIC_LLONG_LOCK_FREE 1
+// CHECK_SPARC_CASA: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
+// CHECK_SPARC_CASA-NOT: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1
+
 // RUN: %clang -mcpu=v8 -E -dM %s -o - 2>&1 \
 // RUN:     -target sparc-sun-solaris \
 // RUN:   | FileCheck -match-full-lines %s 
-check-prefix=CHECK_SPARC_SOLARIS_GCC_ATOMICS-V8
diff --git a/llvm/lib/Target/Sparc/SparcFrameLowering.cpp 
b/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
index 72c4ce75455a0..0e28024ad202e 100644
--- a/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
+++ b/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
@@ -265,12 +265,24 @@ bool SparcFrameLowering::isLeafProc(MachineFunction &MF) 
const
   MachineFrameInfo    &MFI = MF.getFrameInfo();
   SparcMachineFunctionInfo *SMFI = MF.getInfo<SparcMachineFunctionInfo>();
 
-  return !(MFI.hasCalls()               // has calls
-           || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
-           || MRI.isPhysRegUsed(SP::O6) // %sp is used
-           || hasFP(MF)                 // need %fp
-           || MF.hasInlineAsm()         // has inline assembly
-           || SMFI->isLargeProc());     // is a large procedure
+  if (MFI.hasCalls()               // has calls
+      || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
+      || MRI.isPhysRegUsed(SP::O6) // %sp is used
+      || hasFP(MF)                 // need %fp
+      || MF.hasInlineAsm()         // has inline assembly
+      || SMFI->isLargeProc())      // is a large procedure
+    return false;
+
+  // %o registers are normally only allocated once all the %i, %g and %l
+  // registers are used, but IntRegsV8Plus is restricted to %g/%o registers and
+  // can use them earlier. Remapping %i to %o would then merge two different
+  // registers.
+  if (SMFI->usesV8PlusRegs())
+    for (unsigned N = 0; N != 8; ++N)
+      if (MRI.isPhysRegUsed(SP::I0 + N) && MRI.isPhysRegUsed(SP::O0 + N))
+        return false;
+
+  return true;
 }
 
 void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {
diff --git a/llvm/lib/Target/Sparc/SparcISelLowering.cpp 
b/llvm/lib/Target/Sparc/SparcISelLowering.cpp
index f8d70e5b1aa68..9b269a9cca818 100644
--- a/llvm/lib/Target/Sparc/SparcISelLowering.cpp
+++ b/llvm/lib/Target/Sparc/SparcISelLowering.cpp
@@ -1866,10 +1866,11 @@ SparcTargetLowering::SparcTargetLowering(const 
TargetMachine &TM,
   // Atomics are supported on SparcV9. 32-bit atomics are also
   // supported by some Leon SparcV8 variants. Otherwise, atomics
   // are unsupported.
+  //
+  // 32-bit V9 code can only use 64-bit atomics under the V8+ ABI, which
+  // guarantees that the upper halves of the %g and %o registers are preserved.
   if (Subtarget->isV9()) {
-    // TODO: we _ought_ to be able to support 64-bit atomics on 32-bit sparcv9,
-    // but it hasn't been implemented in the backend yet.
-    if (Subtarget->is64Bit())
+    if (Subtarget->is64Bit() || Subtarget->isV8Plus())
       setMaxAtomicSizeInBitsSupported(64);
     else
       setMaxAtomicSizeInBitsSupported(32);
@@ -1893,6 +1894,13 @@ SparcTargetLowering::SparcTargetLowering(const 
TargetMachine &TM,
     setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
     setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
     setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
+  } else if (Subtarget->isV9() && Subtarget->isV8Plus()) {
+    // As in GCC, 64-bit atomic loads and stores use ldd/std, and 64-bit
+    // compare-and-swap uses casx on values packed into %g/%o registers.
+    // Everything else is expanded to a compare-and-swap loop.
+    setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
+    setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
+    setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
   }
 
   if (!Subtarget->isV9()) {
@@ -3563,7 +3571,21 @@ static SDValue LowerFNEGorFABS(SDValue Op, SelectionDAG 
&DAG, bool isV9) {
 }
 
 static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
-  if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getSuccessOrdering())) {
+  auto *AN = cast<AtomicSDNode>(Op);
+
+  // 64-bit atomic store in 32-bit V8+ code: like GCC's atomic_storedi_1, use
+  // std, which is atomic for doubleword-aligned addresses. The load side is
+  // handled in ReplaceNodeResults.
+  if (Op.getOpcode() == ISD::ATOMIC_STORE &&
+      Op.getOperand(1).getValueType() == MVT::i64 &&
+      !DAG.getSubtarget<SparcSubtarget>().is64Bit()) {
+    SDLoc DL(Op);
+    SDValue Val = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
+    return DAG.getStore(AN->getChain(), DL, Val, AN->getBasePtr(),
+                        AN->getMemOperand());
+  }
+
+  if (isStrongerThanMonotonic(AN->getSuccessOrdering())) {
     // Expand with a fence.
     return SDValue();
   }
@@ -4201,6 +4223,40 @@ void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
     Results.push_back(LoadRes.getValue(1));
     return;
   }
+  case ISD::ATOMIC_LOAD: {
+    // 64-bit atomic load in 32-bit V8+ code: like GCC's atomic_loaddi_1, use
+    // ldd, which is atomic for doubleword-aligned addresses. Ordering is
+    // handled by the fences AtomicExpand inserted around it.
+    auto *AN = cast<AtomicSDNode>(N);
+    assert(AN->getMemoryVT() == MVT::i64 && !Subtarget->is64Bit());
+    SDValue LoadRes = DAG.getLoad(MVT::v2i32, dl, AN->getChain(),
+                                  AN->getBasePtr(), AN->getMemOperand());
+    Results.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i64, LoadRes));
+    Results.push_back(LoadRes.getValue(1));
+    return;
+  }
+  case ISD::ATOMIC_CMP_SWAP: {
+    // 64-bit compare-and-swap in 32-bit V8+ code. See CASX_V8PLUS.
+    auto *AN = cast<AtomicSDNode>(N);
+    assert(AN->getMemoryVT() == MVT::i64 && !Subtarget->is64Bit());
+    auto [CmpLo, CmpHi] =
+        DAG.SplitScalar(N->getOperand(2), dl, MVT::i32, MVT::i32);
+    auto [NewLo, NewHi] =
+        DAG.SplitScalar(N->getOperand(3), dl, MVT::i32, MVT::i32);
+    SDValue Ops[] = {N->getOperand(1), CmpHi, CmpLo, NewHi, NewLo,
+                     N->getOperand(0)};
+    MachineSDNode *CAS = DAG.getMachineNode(
+        SP::CASX_V8PLUS, dl,
+        DAG.getVTList(MVT::i32, MVT::i32, MVT::i32, MVT::Other), Ops);
+    DAG.setNodeMemRefs(CAS, {AN->getMemOperand()});
+    DAG.getMachineFunction()
+        .getInfo<SparcMachineFunctionInfo>()
+        ->setUsesV8PlusRegs();
+    SDValue Hi(CAS, 0), Lo(CAS, 1);
+    Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi));
+    Results.push_back(SDValue(CAS, 3));
+    return;
+  }
   }
 }
 
diff --git a/llvm/lib/Target/Sparc/SparcInstrInfo.cpp 
b/llvm/lib/Target/Sparc/SparcInstrInfo.cpp
index f124dc6d0ed59..8e67aedc7a7e9 100644
--- a/llvm/lib/Target/Sparc/SparcInstrInfo.cpp
+++ b/llvm/lib/Target/Sparc/SparcInstrInfo.cpp
@@ -641,7 +641,7 @@ void SparcInstrInfo::storeRegToStackSlot(MachineBasicBlock 
&MBB,
   if (RC == &SP::I64RegsRegClass)
     BuildMI(MBB, I, DL, get(SP::STXri)).addFrameIndex(FI).addImm(0)
       .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
-  else if (RC == &SP::IntRegsRegClass)
+  else if (RC == &SP::IntRegsRegClass || RC == &SP::IntRegsV8PlusRegClass)
     BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
       .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
   else if (RC == &SP::IntPairRegClass)
@@ -680,7 +680,7 @@ void SparcInstrInfo::loadRegFromStackSlot(MachineBasicBlock 
&MBB,
   if (RC == &SP::I64RegsRegClass)
     BuildMI(MBB, I, DL, get(SP::LDXri), DestReg).addFrameIndex(FI).addImm(0)
       .addMemOperand(MMO);
-  else if (RC == &SP::IntRegsRegClass)
+  else if (RC == &SP::IntRegsRegClass || RC == &SP::IntRegsV8PlusRegClass)
     BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0)
       .addMemOperand(MMO);
   else if (RC == &SP::IntPairRegClass)
@@ -965,6 +965,50 @@ bool SparcInstrInfo::expandPostRAPseudo(MachineInstr &MI) 
const {
     MBB.erase(MI);
     return true;
   }
+  case SP::CASX_V8PLUS: {
+    // Same sequence as GCC's *atomic_compare_and_swapdi_v8plus:
+    //   srl  %new_lo, 0, %rd_lo
+    //   sllx %new_hi, 32, %tmp
+    //   or   %rd_lo, %tmp, %rd_lo    ! %rd_lo = 64-bit new value
+    //   srl  %cmp_lo, 0, %tmp
+    //   sllx %cmp_hi, 32, %rd_hi
+    //   or   %tmp, %rd_hi, %tmp      ! %tmp = 64-bit compare value
+    //   casx [%rs1], %tmp, %rd_lo
+    //   srlx %rd_lo, 32, %rd_hi
+    // The lower 32 bits of %rd_lo hold the low half of the result.
+    assert(Subtarget.isV9() && !Subtarget.is64Bit() &&
+           "CASX_V8PLUS is only used for 32-bit V9 code");
+    MachineBasicBlock &MBB = *MI.getParent();
+    const DebugLoc &DL = MI.getDebugLoc();
+    Register RdHi = MI.getOperand(0).getReg();
+    Register RdLo = MI.getOperand(1).getReg();
+    Register Tmp = MI.getOperand(2).getReg();
+    Register Ptr = MI.getOperand(3).getReg();
+    Register CmpHi = MI.getOperand(4).getReg();
+    Register CmpLo = MI.getOperand(5).getReg();
+    Register NewHi = MI.getOperand(6).getReg();
+    Register NewLo = MI.getOperand(7).getReg();
+
+    BuildMI(MBB, MI, DL, get(SP::SRLri), RdLo).addReg(NewLo).addImm(0);
+    BuildMI(MBB, MI, DL, get(SP::SLLXri), Tmp).addReg(NewHi).addImm(32);
+    BuildMI(MBB, MI, DL, get(SP::ORrr), RdLo)
+        .addReg(RdLo, RegState::Kill)
+        .addReg(Tmp, RegState::Kill);
+    BuildMI(MBB, MI, DL, get(SP::SRLri), Tmp).addReg(CmpLo).addImm(0);
+    BuildMI(MBB, MI, DL, get(SP::SLLXri), RdHi).addReg(CmpHi).addImm(32);
+    BuildMI(MBB, MI, DL, get(SP::ORrr), Tmp)
+        .addReg(Tmp, RegState::Kill)
+        .addReg(RdHi, RegState::Kill);
+    BuildMI(MBB, MI, DL, get(SP::CASXArr), RdLo)
+        .addReg(Ptr)
+        .addReg(Tmp, RegState::Kill)
+        .addReg(RdLo, RegState::Kill)
+        .addImm(0x80) // ASI_PRIMARY
+        .cloneMemRefs(MI);
+    BuildMI(MBB, MI, DL, get(SP::SRLXri), RdHi).addReg(RdLo).addImm(32);
+    MBB.erase(MI);
+    return true;
+  }
   }
   return false;
 }
diff --git a/llvm/lib/Target/Sparc/SparcInstrInfo.td 
b/llvm/lib/Target/Sparc/SparcInstrInfo.td
index edf0d34194635..fd2dca7b167da 100644
--- a/llvm/lib/Target/Sparc/SparcInstrInfo.td
+++ b/llvm/lib/Target/Sparc/SparcInstrInfo.td
@@ -1926,6 +1926,21 @@ let Predicates = [HasV9], Uses = [ASR3], Constraints = 
"$swap = $rd" in
                                      IntRegs:$swap),
                  "casa [$rs1] %asi, $rs2, $rd", []>;
 
+// 64-bit compare-and-swap in 32-bit V8+ code, the equivalent of GCC's
+// *atomic_compare_and_swapdi_v8plus. The 32-bit halves of the compare and new
+// values are packed into %g/%o registers, casx is issued and the result is
+// split back into halves. The results are written before all inputs are read,
+// so they are early-clobber; $tmp is a scratch register. Expanded after
+// register allocation so no 64-bit value is spilled to a 32-bit stack slot.
+let Predicates = [HasV9], mayLoad = 1, mayStore = 1, hasSideEffects = 0,
+    Constraints = "@earlyclobber $rd_hi,@earlyclobber $rd_lo,@earlyclobber 
$tmp" in
+  def CASX_V8PLUS
+      : Pseudo<(outs IntRegsV8Plus:$rd_hi, IntRegsV8Plus:$rd_lo,
+                     IntRegsV8Plus:$tmp),
+               (ins IntRegs:$rs1, IntRegs:$cmp_hi, IntRegs:$cmp_lo,
+                    IntRegs:$new_hi, IntRegs:$new_lo),
+               "!CASX_V8PLUS", []>;
+
 // TODO: Add DAG sequence to lower these instructions. Currently, only provided
 // as inline assembler-supported instructions.
 let Predicates = [HasUMAC_SMAC], Defs = [Y, ASR18], Uses = [Y, ASR18] in {
diff --git a/llvm/lib/Target/Sparc/SparcMachineFunctionInfo.h 
b/llvm/lib/Target/Sparc/SparcMachineFunctionInfo.h
index 4ed1a2b2a08cf..e2c8b913bf7e5 100644
--- a/llvm/lib/Target/Sparc/SparcMachineFunctionInfo.h
+++ b/llvm/lib/Target/Sparc/SparcMachineFunctionInfo.h
@@ -35,6 +35,10 @@ namespace llvm {
     /// instruction.
     bool IsLargeProc = false;
 
+    /// True if the function allocates IntRegsV8Plus registers, which can put
+    /// values in %o registers that clash with %i registers in leaf procedures.
+    bool UsesV8PlusRegs = false;
+
     /// BranchRelaxationSpillFrameIndex - FrameIndex of the spill slot when
     /// there is no scavenged register in insertIndirectBranch.
     int BranchRelaxationSpillFrameIndex = -1;
@@ -67,6 +71,9 @@ namespace llvm {
     void setLargeProc(bool rhs) { IsLargeProc = rhs; }
     bool isLargeProc() const { return IsLargeProc; }
 
+    void setUsesV8PlusRegs() { UsesV8PlusRegs = true; }
+    bool usesV8PlusRegs() const { return UsesV8PlusRegs; }
+
     int getBranchRelaxationSpillFrameIndex() {
       return BranchRelaxationSpillFrameIndex;
     }
diff --git a/llvm/lib/Target/Sparc/SparcRegisterInfo.td 
b/llvm/lib/Target/Sparc/SparcRegisterInfo.td
index 6ef3c734bef60..333b3ef994891 100644
--- a/llvm/lib/Target/Sparc/SparcRegisterInfo.td
+++ b/llvm/lib/Target/Sparc/SparcRegisterInfo.td
@@ -358,6 +358,16 @@ def IntPair : RegisterClass<"SP", [v2i32], 64,
 // spill slot is a stricter constraint than only requiring a 32-bit spill slot.
 def I64Regs : RegisterClass<"SP", [i64], 64, (add IntRegs)>;
 
+// Global and out registers. Under the V8+ ABI these are the only integer
+// registers whose upper 32 bits are preserved (window spills of %l/%i
+// registers in 32-bit code only save the lower halves), so they are the only
+// registers that may hold a 64-bit value in 32-bit code. This is the same set
+// as GCC's I64_REGS...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/242578
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to