[clang] 5426281 - [Sparc] Support 64-bit atomics in 32-bit V8+ code (#242578)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 00:44:14 PST 2026
Author: Brad Smith
Date: 2026-11-23T03:44:09-05:00
New Revision: 5426281720dcb4d2006140fce25cd6fcfd71d5dc
URL: https://github.com/llvm/llvm-project/commit/5426281720dcb4d2006140fce25cd6fcfd71d5dc
DIFF: https://github.com/llvm/llvm-project/commit/5426281720dcb4d2006140fce25cd6fcfd71d5dc.diff
LOG: [Sparc] Support 64-bit atomics in 32-bit V8+ code (#242578)
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 -> %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.
Spilling and reloading also accepts the integer and integer pair
register
classes TableGen infers from the new class.
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 <mintsuki at protonmail.com>
Co-authored-by: Mintsuki <mintsuki at protonmail.com>
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Added:
llvm/test/CodeGen/SPARC/atomics-v8plus.ll
Modified:
clang/lib/Basic/Targets/Sparc.cpp
clang/lib/Basic/Targets/Sparc.h
clang/test/Preprocessor/init.c
clang/test/Preprocessor/predefined-arch-macros.c
llvm/lib/Target/Sparc/SparcFrameLowering.cpp
llvm/lib/Target/Sparc/SparcISelLowering.cpp
llvm/lib/Target/Sparc/SparcInstrInfo.cpp
llvm/lib/Target/Sparc/SparcInstrInfo.td
llvm/lib/Target/Sparc/SparcMachineFunctionInfo.h
llvm/lib/Target/Sparc/SparcRegisterInfo.td
Removed:
################################################################################
diff --git a/clang/lib/Basic/Targets/Sparc.cpp b/clang/lib/Basic/Targets/Sparc.cpp
index 1cfa52e136332d..d07a6c10483818 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 a3d9b2d73d1375..2b5a55b6d91d3a 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 6bcce58cd18d76..2d5f37c6831c04 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 b571fa241bcb10..b24af6d81758ad 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 5ce8845124dddf..1dda1b23c72ae0 100644
--- a/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
+++ b/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
@@ -268,12 +268,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
diff erent
+ // 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 062e3141527c2b..741295ab439648 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()) {
@@ -3552,7 +3560,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();
}
@@ -4229,6 +4251,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 e8f219793a65d5..01fcb53f6d7722 100644
--- a/llvm/lib/Target/Sparc/SparcInstrInfo.cpp
+++ b/llvm/lib/Target/Sparc/SparcInstrInfo.cpp
@@ -652,14 +652,21 @@ void SparcInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
+ // Integer and integer pair registers can come in TableGen-inferred
+ // subclasses (e.g. IntPair_with_sub_even_in_IntRegsV8Plus), so match the
+ // register class families and pick the GPR instruction by spill size.
+ const TargetRegisterInfo &TRI = getRegisterInfo();
+ bool IsGPR = SP::IntRegsRegClass.hasSubClassEq(RC) ||
+ SP::I64RegsRegClass.hasSubClassEq(RC);
+
// On the order of operands here: think "[FrameIdx + 0] = SrcReg".
- if (RC == &SP::I64RegsRegClass)
+ if (IsGPR && TRI.getSpillSize(*RC) == 8)
BuildMI(MBB, I, DL, get(SP::STXri)).addFrameIndex(FI).addImm(0)
.addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
- else if (RC == &SP::IntRegsRegClass)
+ else if (IsGPR)
BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
.addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
- else if (RC == &SP::IntPairRegClass)
+ else if (SP::IntPairRegClass.hasSubClassEq(RC))
BuildMI(MBB, I, DL, get(SP::STDri)).addFrameIndex(FI).addImm(0)
.addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
else if (RC == &SP::FPRegsRegClass)
@@ -692,13 +699,18 @@ void SparcInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
- if (RC == &SP::I64RegsRegClass)
+ // See storeRegToStackSlot.
+ const TargetRegisterInfo &TRI = getRegisterInfo();
+ bool IsGPR = SP::IntRegsRegClass.hasSubClassEq(RC) ||
+ SP::I64RegsRegClass.hasSubClassEq(RC);
+
+ if (IsGPR && TRI.getSpillSize(*RC) == 8)
BuildMI(MBB, I, DL, get(SP::LDXri), DestReg).addFrameIndex(FI).addImm(0)
.addMemOperand(MMO);
- else if (RC == &SP::IntRegsRegClass)
+ else if (IsGPR)
BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0)
.addMemOperand(MMO);
- else if (RC == &SP::IntPairRegClass)
+ else if (SP::IntPairRegClass.hasSubClassEq(RC))
BuildMI(MBB, I, DL, get(SP::LDDri), DestReg).addFrameIndex(FI).addImm(0)
.addMemOperand(MMO);
else if (RC == &SP::FPRegsRegClass)
@@ -980,6 +992,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 473b49b0c0ad80..f905327dd3158b 100644
--- a/llvm/lib/Target/Sparc/SparcInstrInfo.td
+++ b/llvm/lib/Target/Sparc/SparcInstrInfo.td
@@ -1932,6 +1932,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, at earlyclobber $rd_lo, at 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 4ed1a2b2a08cf2..e2c8b913bf7e5e 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 6ef3c734bef60b..333b3ef9948916 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 class (the "h" constraint), minus %o7, which holds the
+// return address in leaf procedures (%o6 is %sp and always reserved).
+def IntRegsV8Plus : RegisterClass<"SP", [i32], 32,
+ (add (sequence "G%u", 0, 7),
+ (sequence "O%u", 0, 6))>;
+
// Floating point register classes.
def FPRegs : RegisterClass<"SP", [f32], 32, (sequence "F%u", 0, 31)>;
def DFPRegs : RegisterClass<"SP", [f64], 64, (sequence "D%u", 0, 31)>;
diff --git a/llvm/test/CodeGen/SPARC/atomics-v8plus.ll b/llvm/test/CodeGen/SPARC/atomics-v8plus.ll
new file mode 100644
index 00000000000000..8e9d0dd1d7fc14
--- /dev/null
+++ b/llvm/test/CodeGen/SPARC/atomics-v8plus.ll
@@ -0,0 +1,548 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=sparc -mcpu=v9 -mattr=+v8plus -verify-machineinstrs | FileCheck %s --check-prefix=V8PLUS
+; RUN: llc < %s -mtriple=sparc -mcpu=v9 -verify-machineinstrs | FileCheck %s --check-prefix=V9
+
+;; 64-bit atomics in 32-bit V8+ code. Like GCC, atomic loads and stores use
+;; ldd/std, and compare-and-swap uses casx on values packed into %g/%o
+;; registers. Without V8+ these are all libcalls.
+
+define i64 @load_monotonic(ptr %p) nounwind {
+; V8PLUS-LABEL: load_monotonic:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: ldd [%o0], %o0
+;
+; V9-LABEL: load_monotonic:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -96, %sp
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: call __atomic_load_8
+; V9-NEXT: mov %g0, %o1
+; V9-NEXT: mov %o0, %i0
+; V9-NEXT: ret
+; V9-NEXT: restore %g0, %o1, %o1
+ %v = load atomic i64, ptr %p monotonic, align 8
+ ret i64 %v
+}
+
+define i64 @load_seq_cst(ptr %p) nounwind {
+; V8PLUS-LABEL: load_seq_cst:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: ldd [%o0], %o0
+; V8PLUS-NEXT: membar #LoadLoad | #LoadStore
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: nop
+;
+; V9-LABEL: load_seq_cst:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -96, %sp
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: call __atomic_load_8
+; V9-NEXT: mov 5, %o1
+; V9-NEXT: mov %o0, %i0
+; V9-NEXT: ret
+; V9-NEXT: restore %g0, %o1, %o1
+ %v = load atomic i64, ptr %p seq_cst, align 8
+ ret i64 %v
+}
+
+define void @store_monotonic(ptr %p, i64 %v) nounwind {
+; V8PLUS-LABEL: store_monotonic:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: mov %o2, %o3
+; V8PLUS-NEXT: mov %o1, %o2
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: std %o2, [%o0]
+;
+; V9-LABEL: store_monotonic:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -96, %sp
+; V9-NEXT: mov %i2, %o2
+; V9-NEXT: mov %i1, %o1
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: call __atomic_store_8
+; V9-NEXT: mov %g0, %o3
+; V9-NEXT: ret
+; V9-NEXT: restore
+ store atomic i64 %v, ptr %p monotonic, align 8
+ ret void
+}
+
+define void @store_seq_cst(ptr %p, i64 %v) nounwind {
+; V8PLUS-LABEL: store_seq_cst:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: mov %o2, %o3
+; V8PLUS-NEXT: membar #LoadStore | #StoreStore
+; V8PLUS-NEXT: mov %o1, %o2
+; V8PLUS-NEXT: std %o2, [%o0]
+; V8PLUS-NEXT: membar #LoadLoad | #StoreLoad | #LoadStore | #StoreStore
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: nop
+;
+; V9-LABEL: store_seq_cst:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -96, %sp
+; V9-NEXT: mov %i2, %o2
+; V9-NEXT: mov %i1, %o1
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: call __atomic_store_8
+; V9-NEXT: mov 5, %o3
+; V9-NEXT: ret
+; V9-NEXT: restore
+ store atomic i64 %v, ptr %p seq_cst, align 8
+ ret void
+}
+
+define void @store_zero(ptr %p) nounwind {
+; V8PLUS-LABEL: store_zero:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: mov %g0, %o2
+; V8PLUS-NEXT: mov %g0, %o3
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: std %o2, [%o0]
+;
+; V9-LABEL: store_zero:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -96, %sp
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: mov %g0, %o1
+; V9-NEXT: mov %g0, %o2
+; V9-NEXT: call __atomic_store_8
+; V9-NEXT: mov %g0, %o3
+; V9-NEXT: ret
+; V9-NEXT: restore
+ store atomic i64 0, ptr %p monotonic, align 8
+ ret void
+}
+
+define i64 @cmpxchg_monotonic(ptr %p, i64 %cmp, i64 %new) nounwind {
+; V8PLUS-LABEL: cmpxchg_monotonic:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: srl %o4, 0, %g3
+; V8PLUS-NEXT: sllx %o3, 32, %g4
+; V8PLUS-NEXT: or %g3, %g4, %g3
+; V8PLUS-NEXT: srl %o2, 0, %g4
+; V8PLUS-NEXT: sllx %o1, 32, %g2
+; V8PLUS-NEXT: or %g4, %g2, %g4
+; V8PLUS-NEXT: casx [%o0], %g4, %g3
+; V8PLUS-NEXT: srlx %g3, 32, %g2
+; V8PLUS-NEXT: mov %g2, %o0
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: mov %g3, %o1
+;
+; V9-LABEL: cmpxchg_monotonic:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -104, %sp
+; V9-NEXT: mov %i2, %g3
+; V9-NEXT: mov %i4, %o3
+; V9-NEXT: mov %i3, %o2
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: mov %i1, %g2
+; V9-NEXT: std %g2, [%fp+-8]
+; V9-NEXT: add %fp, -8, %o1
+; V9-NEXT: mov %g0, %o4
+; V9-NEXT: call __atomic_compare_exchange_8
+; V9-NEXT: mov %g0, %o5
+; V9-NEXT: ldd [%fp+-8], %i0
+; V9-NEXT: ret
+; V9-NEXT: restore
+ %pair = cmpxchg ptr %p, i64 %cmp, i64 %new monotonic monotonic
+ %v = extractvalue { i64, i1 } %pair, 0
+ ret i64 %v
+}
+
+define i1 @cmpxchg_seq_cst_success(ptr %p, i64 %cmp, i64 %new) nounwind {
+; V8PLUS-LABEL: cmpxchg_seq_cst_success:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: membar #LoadStore | #StoreStore
+; V8PLUS-NEXT: srl %o4, 0, %g3
+; V8PLUS-NEXT: sllx %o3, 32, %g4
+; V8PLUS-NEXT: or %g3, %g4, %g3
+; V8PLUS-NEXT: srl %o2, 0, %g4
+; V8PLUS-NEXT: sllx %o1, 32, %g2
+; V8PLUS-NEXT: or %g4, %g2, %g4
+; V8PLUS-NEXT: casx [%o0], %g4, %g3
+; V8PLUS-NEXT: srlx %g3, 32, %g2
+; V8PLUS-NEXT: mov %g0, %o0
+; V8PLUS-NEXT: xor %g2, %o1, %o1
+; V8PLUS-NEXT: xor %g3, %o2, %o2
+; V8PLUS-NEXT: orcc %o2, %o1, %g0
+; V8PLUS-NEXT: move %icc, 1, %o0
+; V8PLUS-NEXT: membar #LoadLoad | #LoadStore
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: nop
+;
+; V9-LABEL: cmpxchg_seq_cst_success:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -104, %sp
+; V9-NEXT: mov %i2, %g3
+; V9-NEXT: mov %i4, %o3
+; V9-NEXT: mov %i3, %o2
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: mov %i1, %g2
+; V9-NEXT: std %g2, [%fp+-8]
+; V9-NEXT: add %fp, -8, %o1
+; V9-NEXT: mov 5, %o4
+; V9-NEXT: call __atomic_compare_exchange_8
+; V9-NEXT: mov %o4, %o5
+; V9-NEXT: ret
+; V9-NEXT: restore %g0, %o0, %o0
+ %pair = cmpxchg ptr %p, i64 %cmp, i64 %new seq_cst seq_cst
+ %ok = extractvalue { i64, i1 } %pair, 1
+ ret i1 %ok
+}
+
+define i64 @cmpxchg_const(ptr %p, i64 %cmp) nounwind {
+; V8PLUS-LABEL: cmpxchg_const:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: mov 123, %o3
+; V8PLUS-NEXT: srl %o3, 0, %g3
+; V8PLUS-NEXT: sllx %g0, 32, %g4
+; V8PLUS-NEXT: or %g3, %g4, %g3
+; V8PLUS-NEXT: srl %o2, 0, %g4
+; V8PLUS-NEXT: sllx %o1, 32, %g2
+; V8PLUS-NEXT: or %g4, %g2, %g4
+; V8PLUS-NEXT: casx [%o0], %g4, %g3
+; V8PLUS-NEXT: srlx %g3, 32, %g2
+; V8PLUS-NEXT: mov %g2, %o0
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: mov %g3, %o1
+;
+; V9-LABEL: cmpxchg_const:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -104, %sp
+; V9-NEXT: mov %i2, %i3
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: mov %i1, %i2
+; V9-NEXT: std %i2, [%fp+-8]
+; V9-NEXT: add %fp, -8, %o1
+; V9-NEXT: mov 123, %o3
+; V9-NEXT: mov %g0, %o2
+; V9-NEXT: mov %g0, %o4
+; V9-NEXT: call __atomic_compare_exchange_8
+; V9-NEXT: mov %g0, %o5
+; V9-NEXT: ldd [%fp+-8], %i0
+; V9-NEXT: ret
+; V9-NEXT: restore
+ %pair = cmpxchg ptr %p, i64 %cmp, i64 123 monotonic monotonic
+ %v = extractvalue { i64, i1 } %pair, 0
+ ret i64 %v
+}
+
+;; The compare and new values are the same, so the inputs can share registers.
+define i64 @cmpxchg_same(ptr %p, i64 %v) nounwind {
+; V8PLUS-LABEL: cmpxchg_same:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: srl %o2, 0, %g3
+; V8PLUS-NEXT: sllx %o1, 32, %g4
+; V8PLUS-NEXT: or %g3, %g4, %g3
+; V8PLUS-NEXT: srl %o2, 0, %g4
+; V8PLUS-NEXT: sllx %o1, 32, %g2
+; V8PLUS-NEXT: or %g4, %g2, %g4
+; V8PLUS-NEXT: casx [%o0], %g4, %g3
+; V8PLUS-NEXT: srlx %g3, 32, %g2
+; V8PLUS-NEXT: mov %g2, %o0
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: mov %g3, %o1
+;
+; V9-LABEL: cmpxchg_same:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -104, %sp
+; V9-NEXT: mov %i2, %o3
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: mov %i1, %o2
+; V9-NEXT: std %o2, [%fp+-8]
+; V9-NEXT: add %fp, -8, %o1
+; V9-NEXT: mov %g0, %o4
+; V9-NEXT: call __atomic_compare_exchange_8
+; V9-NEXT: mov %g0, %o5
+; V9-NEXT: ldd [%fp+-8], %i0
+; V9-NEXT: ret
+; V9-NEXT: restore
+ %pair = cmpxchg ptr %p, i64 %v, i64 %v monotonic monotonic
+ %r = extractvalue { i64, i1 } %pair, 0
+ ret i64 %r
+}
+
+define i64 @atomicrmw_xchg(ptr %p, i64 %v) nounwind {
+; V8PLUS-LABEL: atomicrmw_xchg:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: ldd [%o0], %o4
+; V8PLUS-NEXT: .LBB9_1: ! %atomicrmw.start
+; V8PLUS-NEXT: ! =>This Inner Loop Header: Depth=1
+; V8PLUS-NEXT: srl %o2, 0, %g3
+; V8PLUS-NEXT: sllx %o1, 32, %g4
+; V8PLUS-NEXT: or %g3, %g4, %g3
+; V8PLUS-NEXT: srl %o5, 0, %g4
+; V8PLUS-NEXT: sllx %o4, 32, %g2
+; V8PLUS-NEXT: or %g4, %g2, %g4
+; V8PLUS-NEXT: casx [%o0], %g4, %g3
+; V8PLUS-NEXT: srlx %g3, 32, %g2
+; V8PLUS-NEXT: xor %g2, %o4, %o3
+; V8PLUS-NEXT: xor %g3, %o5, %o4
+; V8PLUS-NEXT: orcc %o4, %o3, %g0
+; V8PLUS-NEXT: mov %g3, %o5
+; V8PLUS-NEXT: bne %icc, .LBB9_1
+; V8PLUS-NEXT: mov %g2, %o4
+; V8PLUS-NEXT: ! %bb.2: ! %atomicrmw.end
+; V8PLUS-NEXT: mov %o4, %o0
+; V8PLUS-NEXT: retl
+; V8PLUS-NEXT: mov %o5, %o1
+;
+; V9-LABEL: atomicrmw_xchg:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -96, %sp
+; V9-NEXT: mov %i2, %o2
+; V9-NEXT: mov %i1, %o1
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: call __atomic_exchange_8
+; V9-NEXT: mov %g0, %o3
+; V9-NEXT: mov %o0, %i0
+; V9-NEXT: ret
+; V9-NEXT: restore %g0, %o1, %o1
+ %r = atomicrmw xchg ptr %p, i64 %v monotonic
+ ret i64 %r
+}
+
+define i64 @atomicrmw_add(ptr %p, i64 %v) nounwind {
+; V8PLUS-LABEL: atomicrmw_add:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: save %sp, -96, %sp
+; V8PLUS-NEXT: membar #LoadStore | #StoreStore
+; V8PLUS-NEXT: ldd [%i0], %i4
+; V8PLUS-NEXT: .LBB10_1: ! %atomicrmw.start
+; V8PLUS-NEXT: ! =>This Inner Loop Header: Depth=1
+; V8PLUS-NEXT: addcc %i5, %i2, %i3
+; V8PLUS-NEXT: addxcc %i4, %i1, %g2
+; V8PLUS-NEXT: srl %i3, 0, %g4
+; V8PLUS-NEXT: sllx %g2, 32, %o0
+; V8PLUS-NEXT: or %g4, %o0, %g4
+; V8PLUS-NEXT: srl %i5, 0, %o0
+; V8PLUS-NEXT: sllx %i4, 32, %g3
+; V8PLUS-NEXT: or %o0, %g3, %o0
+; V8PLUS-NEXT: casx [%i0], %o0, %g4
+; V8PLUS-NEXT: srlx %g4, 32, %g3
+; V8PLUS-NEXT: xor %g3, %i4, %i3
+; V8PLUS-NEXT: xor %g4, %i5, %i4
+; V8PLUS-NEXT: orcc %i4, %i3, %g0
+; V8PLUS-NEXT: mov %g4, %i5
+; V8PLUS-NEXT: bne %icc, .LBB10_1
+; V8PLUS-NEXT: mov %g3, %i4
+; V8PLUS-NEXT: ! %bb.2: ! %atomicrmw.end
+; V8PLUS-NEXT: membar #LoadLoad | #LoadStore
+; V8PLUS-NEXT: mov %i4, %i0
+; V8PLUS-NEXT: ret
+; V8PLUS-NEXT: restore %g0, %i5, %o1
+;
+; V9-LABEL: atomicrmw_add:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -96, %sp
+; V9-NEXT: mov %i2, %o2
+; V9-NEXT: mov %i1, %o1
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: call __atomic_fetch_add_8
+; V9-NEXT: mov 5, %o3
+; V9-NEXT: mov %o0, %i0
+; V9-NEXT: ret
+; V9-NEXT: restore %g0, %o1, %o1
+ %r = atomicrmw add ptr %p, i64 %v seq_cst
+ ret i64 %r
+}
+
+define i64 @atomicrmw_max(ptr %p, i64 %v) nounwind {
+; V8PLUS-LABEL: atomicrmw_max:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: save %sp, -96, %sp
+; V8PLUS-NEXT: membar #LoadStore | #StoreStore
+; V8PLUS-NEXT: ldd [%i0], %i4
+; V8PLUS-NEXT: .LBB11_1: ! %atomicrmw.start
+; V8PLUS-NEXT: ! =>This Inner Loop Header: Depth=1
+; V8PLUS-NEXT: mov %g0, %i3
+; V8PLUS-NEXT: mov %g0, %g2
+; V8PLUS-NEXT: cmp %i4, %i1
+; V8PLUS-NEXT: movg %icc, 1, %i3
+; V8PLUS-NEXT: cmp %i5, %i2
+; V8PLUS-NEXT: movgu %icc, 1, %g2
+; V8PLUS-NEXT: cmp %i4, %i1
+; V8PLUS-NEXT: move %icc, %g2, %i3
+; V8PLUS-NEXT: cmp %i3, 0
+; V8PLUS-NEXT: mov %i2, %i3
+; V8PLUS-NEXT: movne %icc, %i5, %i3
+; V8PLUS-NEXT: mov %i1, %g2
+; V8PLUS-NEXT: movne %icc, %i4, %g2
+; V8PLUS-NEXT: srl %i3, 0, %g4
+; V8PLUS-NEXT: sllx %g2, 32, %o0
+; V8PLUS-NEXT: or %g4, %o0, %g4
+; V8PLUS-NEXT: srl %i5, 0, %o0
+; V8PLUS-NEXT: sllx %i4, 32, %g3
+; V8PLUS-NEXT: or %o0, %g3, %o0
+; V8PLUS-NEXT: casx [%i0], %o0, %g4
+; V8PLUS-NEXT: srlx %g4, 32, %g3
+; V8PLUS-NEXT: xor %g3, %i4, %i3
+; V8PLUS-NEXT: xor %g4, %i5, %i4
+; V8PLUS-NEXT: orcc %i4, %i3, %g0
+; V8PLUS-NEXT: mov %g4, %i5
+; V8PLUS-NEXT: bne %icc, .LBB11_1
+; V8PLUS-NEXT: mov %g3, %i4
+; V8PLUS-NEXT: ! %bb.2: ! %atomicrmw.end
+; V8PLUS-NEXT: membar #LoadLoad | #LoadStore
+; V8PLUS-NEXT: mov %i4, %i0
+; V8PLUS-NEXT: ret
+; V8PLUS-NEXT: restore %g0, %i5, %o1
+;
+; V9-LABEL: atomicrmw_max:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -104, %sp
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: call __atomic_load_8
+; V9-NEXT: mov %g0, %o1
+; V9-NEXT: mov %o0, %g2
+; V9-NEXT: add %fp, -8, %i3
+; V9-NEXT: mov 5, %i4
+; V9-NEXT: mov %o1, %g3
+; V9-NEXT: .LBB11_1: ! %atomicrmw.start
+; V9-NEXT: ! =>This Inner Loop Header: Depth=1
+; V9-NEXT: mov %g0, %i5
+; V9-NEXT: mov %g0, %g4
+; V9-NEXT: cmp %g2, %i1
+; V9-NEXT: movg %icc, 1, %i5
+; V9-NEXT: cmp %g3, %i2
+; V9-NEXT: movgu %icc, 1, %g4
+; V9-NEXT: cmp %g2, %i1
+; V9-NEXT: move %icc, %g4, %i5
+; V9-NEXT: cmp %i5, 0
+; V9-NEXT: mov %i1, %o2
+; V9-NEXT: movne %icc, %g2, %o2
+; V9-NEXT: mov %i2, %o3
+; V9-NEXT: movne %icc, %g3, %o3
+; V9-NEXT: std %g2, [%fp+-8]
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: mov %i3, %o1
+; V9-NEXT: mov %i4, %o4
+; V9-NEXT: call __atomic_compare_exchange_8
+; V9-NEXT: mov %i4, %o5
+; V9-NEXT: cmp %o0, 0
+; V9-NEXT: be %icc, .LBB11_1
+; V9-NEXT: ldd [%fp+-8], %g2
+; V9-NEXT: ! %bb.2: ! %atomicrmw.end
+; V9-NEXT: mov %g2, %i0
+; V9-NEXT: ret
+; V9-NEXT: restore %g0, %g3, %o1
+ %r = atomicrmw max ptr %p, i64 %v seq_cst
+ ret i64 %r
+}
+
+;; The 64-bit temporaries must stay in %g/%o registers even when values are
+;; live across the casx in callee-saved %l/%i registers.
+declare void @use(i64)
+define i64 @cmpxchg_live_across(ptr %p, i64 %cmp, i64 %new, i64 %x) nounwind {
+; V8PLUS-LABEL: cmpxchg_live_across:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: save %sp, -104, %sp
+; V8PLUS-NEXT: ld [%fp+92], %o1
+; V8PLUS-NEXT: srl %i4, 0, %g3
+; V8PLUS-NEXT: sllx %i3, 32, %g2
+; V8PLUS-NEXT: or %g3, %g2, %g3
+; V8PLUS-NEXT: srl %i2, 0, %g2
+; V8PLUS-NEXT: sllx %i1, 32, %g4
+; V8PLUS-NEXT: or %g2, %g4, %g2
+; V8PLUS-NEXT: casx [%i0], %g2, %g3
+; V8PLUS-NEXT: srlx %g3, 32, %g4
+; V8PLUS-NEXT: st %g4, [%fp+-8] ! 4-byte Folded Spill
+; V8PLUS-NEXT: st %g3, [%fp+-4] ! 4-byte Folded Spill
+; V8PLUS-NEXT: call use
+; V8PLUS-NEXT: mov %i5, %o0
+; V8PLUS-NEXT: mov %i1, %o0
+; V8PLUS-NEXT: call use
+; V8PLUS-NEXT: mov %i2, %o1
+; V8PLUS-NEXT: mov %i3, %o0
+; V8PLUS-NEXT: call use
+; V8PLUS-NEXT: mov %i4, %o1
+; V8PLUS-NEXT: ld [%fp+-8], %i0 ! 4-byte Folded Reload
+; V8PLUS-NEXT: ld [%fp+-4], %i1 ! 4-byte Folded Reload
+; V8PLUS-NEXT: ret
+; V8PLUS-NEXT: restore
+;
+; V9-LABEL: cmpxchg_live_across:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -104, %sp
+; V9-NEXT: ld [%fp+92], %l2
+; V9-NEXT: mov %i2, %l1
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: mov %i1, %l0
+; V9-NEXT: std %l0, [%fp+-8]
+; V9-NEXT: add %fp, -8, %o1
+; V9-NEXT: mov %i3, %o2
+; V9-NEXT: mov %i4, %o3
+; V9-NEXT: mov %g0, %o4
+; V9-NEXT: call __atomic_compare_exchange_8
+; V9-NEXT: mov %g0, %o5
+; V9-NEXT: ldd [%fp+-8], %i0
+; V9-NEXT: mov %i5, %o0
+; V9-NEXT: call use
+; V9-NEXT: mov %l2, %o1
+; V9-NEXT: mov %l0, %o0
+; V9-NEXT: call use
+; V9-NEXT: mov %l1, %o1
+; V9-NEXT: mov %i3, %o0
+; V9-NEXT: call use
+; V9-NEXT: mov %i4, %o1
+; V9-NEXT: ret
+; V9-NEXT: restore
+ %pair = cmpxchg ptr %p, i64 %cmp, i64 %new monotonic monotonic
+ %v = extractvalue { i64, i1 } %pair, 0
+ call void @use(i64 %x)
+ call void @use(i64 %cmp)
+ call void @use(i64 %new)
+ ret i64 %v
+}
+
+;; The 64-bit result is stored with std after a call, so it is spilled as a
+;; register pair whose halves are restricted to %g/%o registers.
+define void @cmpxchg_spill_pair(ptr %p, ptr %q, i64 %cmp, i64 %new) nounwind {
+; V8PLUS-LABEL: cmpxchg_spill_pair:
+; V8PLUS: ! %bb.0:
+; V8PLUS-NEXT: save %sp, -104, %sp
+; V8PLUS-NEXT: srl %i5, 0, %o1
+; V8PLUS-NEXT: sllx %i4, 32, %g2
+; V8PLUS-NEXT: or %o1, %g2, %o1
+; V8PLUS-NEXT: srl %i3, 0, %g2
+; V8PLUS-NEXT: sllx %i2, 32, %o0
+; V8PLUS-NEXT: or %g2, %o0, %g2
+; V8PLUS-NEXT: casx [%i0], %g2, %o1
+; V8PLUS-NEXT: srlx %o1, 32, %o0
+; V8PLUS-NEXT: std %o0, [%fp+-8] ! 8-byte Folded Spill
+; V8PLUS-NEXT: mov %g0, %o0
+; V8PLUS-NEXT: call use
+; V8PLUS-NEXT: mov %g0, %o1
+; V8PLUS-NEXT: ldd [%fp+-8], %g2 ! 8-byte Folded Reload
+; V8PLUS-NEXT: std %g2, [%i1]
+; V8PLUS-NEXT: ret
+; V8PLUS-NEXT: restore
+;
+; V9-LABEL: cmpxchg_spill_pair:
+; V9: ! %bb.0:
+; V9-NEXT: save %sp, -104, %sp
+; V9-NEXT: ! kill: def $i3 killed $i3 killed $i2_i3 def $i2_i3
+; V9-NEXT: mov %i5, %o3
+; V9-NEXT: mov %i4, %o2
+; V9-NEXT: mov %i0, %o0
+; V9-NEXT: ! kill: def $i2 killed $i2 killed $i2_i3 def $i2_i3
+; V9-NEXT: std %i2, [%fp+-8]
+; V9-NEXT: add %fp, -8, %o1
+; V9-NEXT: mov %g0, %o4
+; V9-NEXT: call __atomic_compare_exchange_8
+; V9-NEXT: mov %g0, %o5
+; V9-NEXT: ldd [%fp+-8], %i2
+; V9-NEXT: mov %g0, %o0
+; V9-NEXT: call use
+; V9-NEXT: mov %g0, %o1
+; V9-NEXT: std %i2, [%i1]
+; V9-NEXT: ret
+; V9-NEXT: restore
+ %pair = cmpxchg ptr %p, i64 %cmp, i64 %new monotonic monotonic
+ %v = extractvalue { i64, i1 } %pair, 0
+ call void @use(i64 0)
+ store i64 %v, ptr %q, align 8
+ ret void
+}
More information about the cfe-commits
mailing list