[clang] [llvm] [Mips] Add -mllsc and -mno-llsc (PR #247561)

Brad Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 01:47:13 PST 2026


https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/247561

MIPS I and the R5900 have no ll and sc, and MIPS I has no sync, so
atomic operations on them are libcalls. Linux emulates these
instructions, though, and GCC has -mllsc to use them anyway, which is
the default for Linux. -mno-llsc makes GCC not use ll, sc and sync on any
ISA.

Add llsc and nollsc target features for these. llsc uses ll and sc for
atomic operations and sync for fences, and lets the assembler accept
them. The R5900 still has no lld and scd, so 64-bit atomic operations
stay libcalls there. nollsc makes all atomic operations libcalls, and as
GCC does, makes fences only compiler barriers.

In Clang, add -mllsc and -mno-llsc, and as GCC does, use llsc by default
on Linux for MIPS I and the R5900. Set the atomic widths and the
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_* macros to match.

>From 9d4698663d70ccd11d8d88878621d28b392dff5d Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Fri, 27 Nov 2026 05:30:17 -0500
Subject: [PATCH] [Mips] Add -mllsc and -mno-llsc

MIPS I and the R5900 have no ll and sc, and MIPS I has no sync, so
atomic operations on them are libcalls. Linux emulates these
instructions, though, and GCC has -mllsc to use them anyway, which is
the default for Linux. -mno-llsc makes GCC not use ll, sc and sync on any
ISA.

Add llsc and nollsc target features for these. llsc uses ll and sc for
atomic operations and sync for fences, and lets the assembler accept
them. The R5900 still has no lld and scd, so 64-bit atomic operations
stay libcalls there. nollsc makes all atomic operations libcalls, and as
GCC does, makes fences only compiler barriers.

In Clang, add -mllsc and -mno-llsc, and as GCC does, use llsc by default
on Linux for MIPS I and the R5900. Set the atomic widths and the
__GCC_HAVE_SYNC_COMPARE_AND_SWAP_* macros to match.

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 clang/include/clang/Options/Options.td    |  5 ++
 clang/lib/Basic/Targets/Mips.cpp          |  2 +-
 clang/lib/Basic/Targets/Mips.h            | 15 +++++-
 clang/lib/Driver/ToolChains/Arch/Mips.cpp | 11 +++++
 clang/test/Driver/mips-features.c         | 25 ++++++++++
 clang/test/Preprocessor/init-mips.c       | 22 ++++++++-
 llvm/lib/Target/Mips/Mips.td              |  6 +++
 llvm/lib/Target/Mips/Mips64InstrInfo.td   |  4 +-
 llvm/lib/Target/Mips/MipsISelLowering.cpp | 15 +++---
 llvm/lib/Target/Mips/MipsInstrInfo.td     | 26 ++++++++--
 llvm/lib/Target/Mips/MipsSubtarget.h      | 14 ++++++
 llvm/test/CodeGen/Mips/atomic-llsc.ll     | 60 +++++++++++++++++++++++
 llvm/test/MC/Mips/llsc-feature.s          | 26 ++++++++++
 13 files changed, 213 insertions(+), 18 deletions(-)
 create mode 100644 llvm/test/CodeGen/Mips/atomic-llsc.ll
 create mode 100644 llvm/test/MC/Mips/llsc-feature.s

diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 9ff712eaa0e7fd..e2e27fc3b5634a 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -6720,6 +6720,11 @@ def modd_spreg : Flag<["-"], "modd-spreg">, Group<m_mips_Features_Group>,
 def mno_odd_spreg : Flag<["-"], "mno-odd-spreg">, Group<m_mips_Features_Group>,
   HelpText<"Disable odd single-precision floating point registers">,
   Flags<[HelpHidden]>;
+def mllsc : Flag<["-"], "mllsc">, Group<m_mips_Features_Group>,
+  HelpText<"Use ll, sc and sync for atomic operations even if the ISA does "
+           "not have them (MIPS only)">;
+def mno_llsc : Flag<["-"], "mno-llsc">, Group<m_mips_Features_Group>,
+  HelpText<"Do not use ll, sc and sync (MIPS only)">;
 def mrelax_pic_calls : Flag<["-"], "mrelax-pic-calls">,
   Group<m_mips_Features_Group>,
   HelpText<"Produce relaxation hints for linkers to try optimizing PIC "
diff --git a/clang/lib/Basic/Targets/Mips.cpp b/clang/lib/Basic/Targets/Mips.cpp
index d9803969150e75..7b72e5f12ca772 100644
--- a/clang/lib/Basic/Targets/Mips.cpp
+++ b/clang/lib/Basic/Targets/Mips.cpp
@@ -220,7 +220,7 @@ void MipsTargetInfo::getTargetDefines(const LangOptions &Opts,
     // found in 64-bit processors. In the case of O32 on a 64-bit processor,
     // the instructions exist but using them violates the ABI since they
     // require 64-bit GPRs and O32 only supports 32-bit GPRs.
-    if (ABI == "n32" || ABI == "n64")
+    if (HasLLDSCD && (ABI == "n32" || ABI == "n64"))
       Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
   }
 }
diff --git a/clang/lib/Basic/Targets/Mips.h b/clang/lib/Basic/Targets/Mips.h
index bb183b825901a3..e739651c9efb60 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -42,8 +42,10 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
   // to 6 for MIPS32 and MIPS64 and 0 for the older ISAs.
   unsigned ISALevel = 0;
   unsigned ISARev = 0;
-  // MIPS I and the R5900 have no ll and sc.
+  // MIPS I and the R5900 have no ll and sc, unless +llsc is given. The R5900
+  // has no lld and scd either.
   bool HasLLSC = true;
+  bool HasLLDSCD = true;
 
 protected:
   enum FPModeEnum { FPXX, FP32, FP64 } FPMode;
@@ -319,6 +321,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
     bool StrictAlign = false;
     bool FpGiven = false;
     bool IsR5900 = false;
+    bool UseLLSC = false;
+    bool NoLLSC = false;
 
     for (const auto &Feature : Features) {
       if (Feature == "+single-float")
@@ -366,6 +370,10 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
         UseIndirectJumpHazard = true;
       else if (Feature == "+r5900")
         IsR5900 = true;
+      else if (Feature == "+llsc")
+        UseLLSC = true;
+      else if (Feature == "+nollsc")
+        NoLLSC = true;
       else if (Feature == "+nooddspreg") {
         NoOddSpreg = true;
         OddSpregGiven = false;
@@ -397,9 +405,12 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
     }
 
     // Without ll and sc, the backend uses libcalls for atomic operations.
-    HasLLSC = ISALevel != 1 && !IsR5900;
+    HasLLSC = !NoLLSC && (UseLLSC || (ISALevel != 1 && !IsR5900));
+    HasLLDSCD = HasLLSC && !IsR5900;
     if (!HasLLSC)
       MaxAtomicInlineWidth = 0;
+    else if (!HasLLDSCD && MaxAtomicInlineWidth > 32)
+      MaxAtomicInlineWidth = 32;
 
     resetDataLayout();
 
diff --git a/clang/lib/Driver/ToolChains/Arch/Mips.cpp b/clang/lib/Driver/ToolChains/Arch/Mips.cpp
index e3187351a0b555..3dfeccf0588329 100644
--- a/clang/lib/Driver/ToolChains/Arch/Mips.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/Mips.cpp
@@ -417,6 +417,17 @@ void mips::getMIPSTargetFeatures(const Driver &D, const llvm::Triple &Triple,
 
   AddTargetFeature(Args, Features, options::OPT_mno_odd_spreg,
                    options::OPT_modd_spreg, "nooddspreg");
+
+  // MIPS I and the R5900 have no ll and sc, but Linux emulates them, so as
+  // GCC does, use them there unless -mno-llsc is given.
+  if (Arg *A = Args.getLastArg(options::OPT_mllsc, options::OPT_mno_llsc)) {
+    if (A->getOption().matches(options::OPT_mllsc))
+      Features.push_back("+llsc");
+    else
+      Features.push_back("+nollsc");
+  } else if (Triple.isOSLinux() && (CPUName == "mips1" || CPUName == "r5900")) {
+    Features.push_back("+llsc");
+  }
   AddTargetFeature(Args, Features, options::OPT_mno_madd4, options::OPT_mmadd4,
                    "nomadd4");
   AddTargetFeature(Args, Features, options::OPT_mmt, options::OPT_mno_mt, "mt");
diff --git a/clang/test/Driver/mips-features.c b/clang/test/Driver/mips-features.c
index ee370051d1ebed..e729e5fa5742ce 100644
--- a/clang/test/Driver/mips-features.c
+++ b/clang/test/Driver/mips-features.c
@@ -186,6 +186,31 @@
 // RUN:   | FileCheck --check-prefix=CHECK-MODDSPREG %s
 // CHECK-MODDSPREG: "-target-feature" "-nooddspreg"
 //
+// -mllsc
+// RUN: %clang --target=mips-linux-gnu -### -c %s -mno-llsc -mllsc 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-MLLSC %s
+// RUN: %clang --target=mips-unknown-elf -### -c %s -mllsc 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-MLLSC %s
+// CHECK-MLLSC: "-target-feature" "+llsc"
+//
+// -mno-llsc
+// RUN: %clang --target=mips-linux-gnu -### -c %s -mips1 -mllsc -mno-llsc 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-MNOLLSC %s
+// CHECK-MNOLLSC: "-target-feature" "+nollsc"
+// CHECK-MNOLLSC-NOT: "+llsc"
+//
+// Linux emulates ll and sc, so they are used by default on MIPS I and the
+// R5900 there, as GCC does.
+// RUN: %clang --target=mips-linux-gnu -### -c %s -mips1 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-MLLSC %s
+// RUN: %clang --target=mipsel-linux-gnu -### -c %s -march=r5900 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-MLLSC %s
+// RUN: %clang --target=mips-unknown-elf -### -c %s -mips1 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-NO-LLSC-FEATURE %s
+// RUN: %clang --target=mips-linux-gnu -### -c %s -mips32 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-NO-LLSC-FEATURE %s
+// CHECK-NO-LLSC-FEATURE-NOT: llsc
+//
 // -mno-odd-spreg
 // RUN: %clang --target=mips-linux-gnu -### -c %s -modd-spreg -mno-odd-spreg 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-NOMODDSPREG %s
diff --git a/clang/test/Preprocessor/init-mips.c b/clang/test/Preprocessor/init-mips.c
index 33184b735faf29..979fde297c3bdc 100644
--- a/clang/test/Preprocessor/init-mips.c
+++ b/clang/test/Preprocessor/init-mips.c
@@ -1967,7 +1967,8 @@
 // RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
 // SPFPSET-32:#define _MIPS_SPFPSET 32
 
-// MIPS I and the R5900 have no ll and sc, so atomic operations are libcalls.
+// MIPS I and the R5900 have no ll and sc, so atomic operations are libcalls,
+// unless +llsc is given. With +nollsc, they are libcalls on any ISA.
 //
 // RUN: %clang_cc1 -target-cpu mips1 \
 // RUN:   -E -dM -triple=mips-none-none < /dev/null \
@@ -1981,11 +1982,30 @@
 // NO-LLSC:#define __CLANG_ATOMIC_INT_LOCK_FREE 1
 // NO-LLSC-NOT:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_{{[0-9]}} 1
 //
+// RUN: %clang_cc1 -target-cpu mips32 -target-feature +nollsc \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix NO-LLSC %s
+//
 // RUN: %clang_cc1 -target-cpu mips2 \
 // RUN:   -E -dM -triple=mips-none-none < /dev/null \
 // RUN:   | FileCheck -match-full-lines -check-prefix LLSC %s
+// RUN: %clang_cc1 -target-cpu mips1 -target-feature +llsc \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix LLSC %s
+// RUN: %clang_cc1 -target-cpu r5900 -target-feature +llsc \
+// RUN:   -E -dM -triple=mipsel-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix LLSC %s
 // LLSC:#define __CLANG_ATOMIC_INT_LOCK_FREE 2
 // LLSC:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
+//
+// The R5900 has no lld and scd, so 64-bit atomic operations stay libcalls.
+// RUN: %clang_cc1 -target-cpu r5900 -target-feature +llsc -target-abi n64 \
+// RUN:   -E -dM -triple=mips64el-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix R5900-N64-LLSC %s
+// R5900-N64-LLSC:#define __CLANG_ATOMIC_INT_LOCK_FREE 2
+// R5900-N64-LLSC:#define __CLANG_ATOMIC_LLONG_LOCK_FREE 1
+// R5900-N64-LLSC:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
+// R5900-N64-LLSC-NOT:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 1
 
 // RUN: %clang_cc1 -target-cpu mips32 \
 // RUN:   -E -dM -triple=mips-unknown-netbsd -mrelocation-model pic < /dev/null \
diff --git a/llvm/lib/Target/Mips/Mips.td b/llvm/lib/Target/Mips/Mips.td
index 0b55ffcc12637f..d32cf272f1692a 100644
--- a/llvm/lib/Target/Mips/Mips.td
+++ b/llvm/lib/Target/Mips/Mips.td
@@ -198,6 +198,12 @@ def FeatureR5900 : SubtargetFeature<"r5900", "IsR5900", "true",
                                     [FeatureMips3, FeatureSingleFloat,
                                      FeatureFixR5900]>;
 
+def FeatureLLSC : SubtargetFeature<"llsc", "UseLLSC", "true",
+                                   "Use ll, sc and sync for atomic operations "
+                                   "even if the ISA does not have them">;
+def FeatureNoLLSC : SubtargetFeature<"nollsc", "NoLLSC", "true",
+                                     "Do not use ll, sc and sync">;
+
 def FeatureUseTCCInDIV : SubtargetFeature<
                                "use-tcc-in-div",
                                "UseTCCInDIV", "false",
diff --git a/llvm/lib/Target/Mips/Mips64InstrInfo.td b/llvm/lib/Target/Mips/Mips64InstrInfo.td
index 31529409b18cf8..318f56d340ef8f 100644
--- a/llvm/lib/Target/Mips/Mips64InstrInfo.td
+++ b/llvm/lib/Target/Mips/Mips64InstrInfo.td
@@ -256,11 +256,11 @@ let AdditionalPredicates = [NotInMicroMips],
   def LL64 : LLBase<"ll", GPR32Opnd>,
              LW_FM<0x30>,
              PTR_64,
-             ISA_MIPS2_NOT_32R6_64R6_R5900;
+             ISA_LLSC_NOT_32R6_64R6;
   def SC64 : SCBase<"sc", GPR32Opnd>,
              LW_FM<0x38>,
              PTR_64,
-             ISA_MIPS2_NOT_32R6_64R6_R5900;
+             ISA_LLSC_NOT_32R6_64R6;
   def JR64 : IndirectBranch<"jr", GPR64Opnd>, MTLO_FM<8>, PTR_64;
 }
 
diff --git a/llvm/lib/Target/Mips/MipsISelLowering.cpp b/llvm/lib/Target/Mips/MipsISelLowering.cpp
index ec1a376c60306e..239cf662e9c820 100644
--- a/llvm/lib/Target/Mips/MipsISelLowering.cpp
+++ b/llvm/lib/Target/Mips/MipsISelLowering.cpp
@@ -427,9 +427,10 @@ MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
   // Sink shifts into their users' blocks to expose extract patterns.
   setHasExtractBitsInsn(Subtarget.hasExtractInsert());
 
-  // MIPS I and the R5900 have no LL/SC instructions for atomic operations.
-  bool HasLLSC = Subtarget.hasMips2() && !Subtarget.isR5900();
-  if (HasLLSC && Subtarget.isGP64bit())
+  // MIPS I and the R5900 have no LL/SC instructions for atomic operations,
+  // unless llsc is given. The R5900 has no LLD/SCD either.
+  bool HasLLSC = Subtarget.hasLLSC();
+  if (HasLLSC && Subtarget.isGP64bit() && !Subtarget.isR5900())
     setMaxAtomicSizeInBitsSupported(64);
   else if (HasLLSC)
     setMaxAtomicSizeInBitsSupported(32);
@@ -2925,15 +2926,15 @@ SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
   SyncScope::ID FenceSSID =
       static_cast<SyncScope::ID>(Op.getConstantOperandVal(2));
 
-  if (Subtarget.hasMips2() && FenceSSID == SyncScope::System)
+  if (Subtarget.hasSync() && FenceSSID == SyncScope::System)
     return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
                        DAG.getTargetConstant(SType, DL, MVT::i32));
 
   // singlethread fences only synchronize with signal handlers on the same
   // thread and thus only need to preserve instruction order, not actually
-  // enforce memory ordering.
-  if ((Subtarget.hasMips1() && !Subtarget.hasMips2()) ||
-      FenceSSID == SyncScope::SingleThread) {
+  // enforce memory ordering. Without sync, GCC also does only this for other
+  // fences.
+  if (!Subtarget.hasSync() || FenceSSID == SyncScope::SingleThread) {
     // MEMBARRIER is a compiler barrier; it codegens to a no-op.
     return DAG.getNode(ISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
   }
diff --git a/llvm/lib/Target/Mips/MipsInstrInfo.td b/llvm/lib/Target/Mips/MipsInstrInfo.td
index af308119bea538..97dc952e707c1f 100644
--- a/llvm/lib/Target/Mips/MipsInstrInfo.td
+++ b/llvm/lib/Target/Mips/MipsInstrInfo.td
@@ -268,6 +268,12 @@ def IsR5900 : Predicate<"Subtarget->isR5900()">,
               AssemblerPredicate<(all_of FeatureR5900)>;
 def NotR5900 : Predicate<"!Subtarget->isR5900()">,
                AssemblerPredicate<(none_of FeatureR5900)>;
+// llsc allows ll, sc and sync on MIPS I and the R5900.
+def HasMips2OrLLSC : Predicate<"Subtarget->hasMips2() || Subtarget->useLLSC()">,
+                     AssemblerPredicate<(any_of FeatureMips2, FeatureLLSC)>;
+def NotR5900OrLLSC : Predicate<"!Subtarget->isR5900() || Subtarget->useLLSC()">,
+                     AssemblerPredicate<(any_of (not FeatureR5900),
+                                                FeatureLLSC)>;
 def HasCRC   : Predicate<"Subtarget->hasCRC()">,
                AssemblerPredicate<(all_of FeatureCRC)>;
 def HasVirt  : Predicate<"Subtarget->hasVirt()">,
@@ -343,6 +349,16 @@ class ISA_MIPS2_NOT_32R6_64R6_R5900 {
                                     NotR5900];
   list<Predicate> EncodingPredicates = [HasStdEnc];
 }
+// ll, sc and sync, which llsc allows on MIPS I and the R5900.
+class ISA_MIPS2_OR_LLSC {
+  list<Predicate> InsnPredicates = [HasMips2OrLLSC];
+  list<Predicate> EncodingPredicates = [HasStdEnc];
+}
+class ISA_LLSC_NOT_32R6_64R6 {
+  list<Predicate> InsnPredicates = [HasMips2OrLLSC, NotMips32r6, NotMips64r6,
+                                    NotR5900OrLLSC];
+  list<Predicate> EncodingPredicates = [HasStdEnc];
+}
 class ISA_MIPS3 {
   list<Predicate> InsnPredicates = [HasMips3];
   list<Predicate> EncodingPredicates = [HasStdEnc];
@@ -2243,7 +2259,7 @@ let DecoderNamespace = "COP3_" in {
              NOT_ASE_CNMIPS;
 }
 
-  def SYNC : MMRel, StdMMR6Rel, SYNC_FT<"sync">, SYNC_FM, ISA_MIPS2;
+  def SYNC : MMRel, StdMMR6Rel, SYNC_FT<"sync">, SYNC_FM, ISA_MIPS2_OR_LLSC;
   def SYNCI : MMRel, StdMMR6Rel, SYNCI_FT<"synci", mem_simm16>, SYNCI_FM,
               ISA_MIPS32R2;
 
@@ -2308,11 +2324,11 @@ let AdditionalPredicates = [NotInMicroMips] in {
 def LL : LLBase<"ll", GPR32Opnd>,
          LW_FM<0x30>,
          PTR_32,
-         ISA_MIPS2_NOT_32R6_64R6_R5900;
+         ISA_LLSC_NOT_32R6_64R6;
 def SC : SCBase<"sc", GPR32Opnd>,
          LW_FM<0x38>,
          PTR_32,
-         ISA_MIPS2_NOT_32R6_64R6_R5900;
+         ISA_LLSC_NOT_32R6_64R6;
 }
 /// Jump and Branch Instructions
 let AdditionalPredicates = [NotInMicroMips, RelocNotPIC] in
@@ -2995,7 +3011,7 @@ let AdditionalPredicates = [NotInMicroMips, NotNanoMips] in {
 }
 def : MipsInstAlias<"sdbbp", (SDBBP 0)>, ISA_MIPS32_NOT_32R6_64R6;
 let AdditionalPredicates = [NotInMicroMips] in
-  def : MipsInstAlias<"sync", (SYNC 0), 1>, ISA_MIPS2;
+  def : MipsInstAlias<"sync", (SYNC 0), 1>, ISA_MIPS2_OR_LLSC;
 
 def : MipsInstAlias<"mulo $rs, $rt",
                     (MULOMacro GPR32Opnd:$rs, GPR32Opnd:$rs, GPR32Opnd:$rt), 0>,
@@ -3276,7 +3292,7 @@ def : MipsPat<(mul GPR32:$lhs, GPR32:$rhs),
 
 // SYNC
 def : MipsPat<(MipsSync (i32 timmz)),
-              (SYNC 0)>, ISA_MIPS2;
+              (SYNC 0)>, ISA_MIPS2_OR_LLSC;
 
 // Call and tail call. On R6 these are only used when compact branches are
 // disabled, otherwise JAL_R6 and TAILCALL_R6 are used.
diff --git a/llvm/lib/Target/Mips/MipsSubtarget.h b/llvm/lib/Target/Mips/MipsSubtarget.h
index 2d0a06de405d49..b22239902976f7 100644
--- a/llvm/lib/Target/Mips/MipsSubtarget.h
+++ b/llvm/lib/Target/Mips/MipsSubtarget.h
@@ -154,6 +154,13 @@ class MipsSubtarget : public MipsGenSubtargetInfo {
   // FixR5900 - Enable R5900 short loop erratum fix.
   bool FixR5900;
 
+  // UseLLSC - Use ll, sc and sync even if the ISA does not have them, as when
+  // the kernel emulates them.
+  bool UseLLSC = false;
+
+  // NoLLSC - Do not use ll, sc and sync.
+  bool NoLLSC = false;
+
   // UseSmallSection - Small section is used.
   bool UseSmallSection;
 
@@ -321,6 +328,13 @@ class MipsSubtarget : public MipsGenSubtargetInfo {
   bool hasCnMips() const { return HasCnMips; }
   bool hasCnMipsP() const { return HasCnMipsP; }
   bool isR5900() const { return IsR5900; }
+  bool useLLSC() const { return UseLLSC; }
+  // MIPS I and the R5900 have no ll and sc.
+  bool hasLLSC() const {
+    return !NoLLSC && (UseLLSC || (hasMips2() && !isR5900()));
+  }
+  // MIPS I has no sync.
+  bool hasSync() const { return !NoLLSC && (UseLLSC || hasMips2()); }
   bool fixR5900() const { return FixR5900; }
 
   bool isLittle() const { return IsLittle; }
diff --git a/llvm/test/CodeGen/Mips/atomic-llsc.ll b/llvm/test/CodeGen/Mips/atomic-llsc.ll
new file mode 100644
index 00000000000000..21aa2feef98c38
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/atomic-llsc.ll
@@ -0,0 +1,60 @@
+; RUN: llc -mtriple=mipsel-linux-gnu -mcpu=mips1 -mattr=+llsc < %s \
+; RUN:   | FileCheck %s --check-prefixes=LLSC,LLSC32
+; RUN: llc -mtriple=mipsel-linux-gnu -mcpu=r5900 -mattr=+llsc < %s \
+; RUN:   | FileCheck %s --check-prefixes=LLSC,LLSC32
+; RUN: llc -mtriple=mips64el-linux-gnuabi64 -mcpu=r5900 -mattr=+llsc < %s \
+; RUN:   | FileCheck %s --check-prefixes=LLSC,R5900-N64
+; RUN: llc -mtriple=mipsel-linux-gnu -mcpu=mips32 -mattr=+nollsc < %s \
+; RUN:   | FileCheck %s --check-prefix=NOLLSC
+; RUN: llc -mtriple=mipsel-linux-gnu -mcpu=mips1 -mattr=+llsc -filetype=obj \
+; RUN:   < %s -o /dev/null
+
+; MIPS I and the R5900 have no ll and sc, but with +llsc they are used anyway,
+; as when the kernel emulates them. The R5900 has no lld and scd either, so
+; 64-bit atomic operations stay libcalls there. With +nollsc, ll, sc and sync
+; are not used at all, and as with GCC, fences are only compiler barriers.
+
+define i32 @rmw32(ptr %p, i32 %v) {
+; LLSC-LABEL:   rmw32:
+; LLSC:           sync
+; LLSC:           ll
+; LLSC:           sc
+; LLSC-NOT:       jal
+; NOLLSC-LABEL: rmw32:
+; NOLLSC:         jal __atomic_fetch_add_4
+  %r = atomicrmw add ptr %p, i32 %v seq_cst
+  ret i32 %r
+}
+
+define i32 @cas32(ptr %p, i32 %o, i32 %n) {
+; LLSC-LABEL:   cas32:
+; LLSC:           ll
+; LLSC:           sc
+; LLSC-NOT:       jal
+; NOLLSC-LABEL: cas32:
+; NOLLSC:         jal __atomic_compare_exchange_4
+  %r = cmpxchg ptr %p, i32 %o, i32 %n seq_cst seq_cst
+  %v = extractvalue { i32, i1 } %r, 0
+  ret i32 %v
+}
+
+define i64 @rmw64(ptr %p, i64 %v) {
+; LLSC32-LABEL:    rmw64:
+; LLSC32:            jal __atomic_fetch_add_8
+; R5900-N64-LABEL: rmw64:
+; R5900-N64:         jal __atomic_fetch_add_8
+; NOLLSC-LABEL:    rmw64:
+; NOLLSC:            jal __atomic_fetch_add_8
+  %r = atomicrmw add ptr %p, i64 %v seq_cst
+  ret i64 %r
+}
+
+define void @fence() {
+; LLSC-LABEL:   fence:
+; LLSC:           sync
+; NOLLSC-LABEL: fence:
+; NOLLSC-NOT:     sync
+; NOLLSC:         .end fence
+  fence seq_cst
+  ret void
+}
diff --git a/llvm/test/MC/Mips/llsc-feature.s b/llvm/test/MC/Mips/llsc-feature.s
new file mode 100644
index 00000000000000..95e3d086cb13c4
--- /dev/null
+++ b/llvm/test/MC/Mips/llsc-feature.s
@@ -0,0 +1,26 @@
+# MIPS I and the R5900 have no ll and sc, and MIPS I has no sync, but +llsc
+# allows them, as when the kernel emulates them.
+
+# RUN: llvm-mc %s -triple=mips -mcpu=mips1 -mattr=+llsc -show-encoding \
+# RUN:   | FileCheck %s
+# RUN: llvm-mc %s -triple=mipsel -mcpu=r5900 -mattr=+llsc -show-encoding \
+# RUN:   | FileCheck %s --check-prefix=R5900
+# RUN: not llvm-mc %s -triple=mips -mcpu=mips1 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=MIPS1-ERR
+# RUN: not llvm-mc %s -triple=mipsel -mcpu=r5900 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=R5900-ERR
+
+  ll $2, 0($4)
+# CHECK: ll $2, 0($4)    # encoding: [0xc0,0x82,0x00,0x00]
+# R5900: ll $2, 0($4)    # encoding: [0x00,0x00,0x82,0xc0]
+# MIPS1-ERR: :[[@LINE-3]]:3: error: instruction requires a CPU feature not currently enabled
+# R5900-ERR: :[[@LINE-4]]:3: error: instruction requires a CPU feature not currently enabled
+  sc $2, 0($4)
+# CHECK: sc $2, 0($4)    # encoding: [0xe0,0x82,0x00,0x00]
+# R5900: sc $2, 0($4)    # encoding: [0x00,0x00,0x82,0xe0]
+# MIPS1-ERR: :[[@LINE-3]]:3: error: instruction requires a CPU feature not currently enabled
+# R5900-ERR: :[[@LINE-4]]:3: error: instruction requires a CPU feature not currently enabled
+  sync
+# CHECK: sync            # encoding: [0x00,0x00,0x00,0x0f]
+# R5900: sync            # encoding: [0x0f,0x00,0x00,0x00]
+# MIPS1-ERR: :[[@LINE-3]]:3: error: instruction requires a CPU feature not currently enabled



More information about the cfe-commits mailing list