[RFC PATCH v3 5/6] riscv/cmpxchg: Deduplicate xchg() macros

From: Leonardo Bras
Date: Tue Apr 04 2023 - 12:39:13 EST


Every xchg define (_relaxed, _acquire, _release, vanilla) contain it's own
define for creating tmp variables and selecting the correct asm code for
given variable size.

All those defines are mostly the same code (other than specific barriers),
so there is no need to keep the 4 copies.

Unify those under a more general define, that can reproduce the previous 4
versions.

(This did not cause any change in generated asm)

Signed-off-by: Leonardo Bras <leobras@xxxxxxxxxx>
---
arch/riscv/include/asm/cmpxchg.h | 72 ++++++--------------------------
1 file changed, 12 insertions(+), 60 deletions(-)

diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h
index a524484946815..480234719b78c 100644
--- a/arch/riscv/include/asm/cmpxchg.h
+++ b/arch/riscv/include/asm/cmpxchg.h
@@ -11,7 +11,7 @@
#include <asm/barrier.h>
#include <asm/fence.h>

-#define ___xchg(sfx, prepend, append, r, p, n) \
+#define ____xchg(sfx, prepend, append, r, p, n) \
({ \
__asm__ __volatile__ ( \
prepend \
@@ -22,19 +22,19 @@
: "memory"); \
})

-#define __xchg_relaxed(ptr, new, size) \
+#define ___xchg(ptr, new, size, sfx, prepend, append) \
({ \
__typeof__(ptr) __ptr = (ptr); \
__typeof__(new) __new = (new); \
__typeof__(*(ptr)) __ret; \
switch (size) { \
case 4: \
- ___xchg(".w", "", "", \
- __ret, __ptr, __new); \
+ ____xchg(".w" sfx, prepend, append, \
+ __ret, __ptr, __new); \
break; \
case 8: \
- ___xchg(".d", "", "", \
- __ret, __ptr, __new); \
+ ____xchg(".d" sfx, prepend, append, \
+ __ret, __ptr, __new); \
break; \
default: \
BUILD_BUG(); \
@@ -42,6 +42,9 @@
__ret; \
})

+#define __xchg_relaxed(ptr, new, size) \
+ ___xchg(ptr, new, size, "", "", "")
+
#define arch_xchg_relaxed(ptr, x) \
({ \
__typeof__(*(ptr)) _x_ = (x); \
@@ -50,24 +53,7 @@
})

#define __xchg_acquire(ptr, new, size) \
-({ \
- __typeof__(ptr) __ptr = (ptr); \
- __typeof__(new) __new = (new); \
- __typeof__(*(ptr)) __ret; \
- switch (size) { \
- case 4: \
- ___xchg(".w", "", RISCV_ACQUIRE_BARRIER, \
- __ret, __ptr, __new); \
- break; \
- case 8: \
- ___xchg(".d", "", RISCV_ACQUIRE_BARRIER, \
- __ret, __ptr, __new); \
- break; \
- default: \
- BUILD_BUG(); \
- } \
- __ret; \
-})
+ ___xchg(ptr, new, size, "", "", RISCV_ACQUIRE_BARRIER)

#define arch_xchg_acquire(ptr, x) \
({ \
@@ -77,24 +63,7 @@
})

#define __xchg_release(ptr, new, size) \
-({ \
- __typeof__(ptr) __ptr = (ptr); \
- __typeof__(new) __new = (new); \
- __typeof__(*(ptr)) __ret; \
- switch (size) { \
- case 4: \
- ___xchg(".w", RISCV_RELEASE_BARRIER, "", \
- __ret, __ptr, __new); \
- break; \
- case 8: \
- ___xchg(".d", RISCV_RELEASE_BARRIER, "", \
- __ret, __ptr, __new); \
- break; \
- default: \
- BUILD_BUG(); \
- } \
- __ret; \
-})
+ ___xchg(ptr, new, size, "", RISCV_RELEASE_BARRIER, "")

#define arch_xchg_release(ptr, x) \
({ \
@@ -104,24 +73,7 @@
})

#define __xchg(ptr, new, size) \
-({ \
- __typeof__(ptr) __ptr = (ptr); \
- __typeof__(new) __new = (new); \
- __typeof__(*(ptr)) __ret; \
- switch (size) { \
- case 4: \
- ___xchg(".w.aqrl", "", "", \
- __ret, __ptr, __new); \
- break; \
- case 8: \
- ___xchg(".d.aqrl", "", "", \
- __ret, __ptr, __new); \
- break; \
- default: \
- BUILD_BUG(); \
- } \
- __ret; \
-})
+ ___xchg(ptr, new, size, ".aqrl", "", "")

#define arch_xchg(ptr, x) \
({ \
--
2.40.0