[libc-commits] [libc] [libc] Save one instruction on ARM (PR #181515)
via libc-commits
libc-commits at lists.llvm.org
Sat Feb 14 14:53:31 PST 2026
https://github.com/SiliconA-Z created https://github.com/llvm/llvm-project/pull/181515
For assembler functions, doing them as best as possible is paramount. Save sp in ARM, restore sp in ARM.
>From 97df66ddb1c5b62838d69ca9af35e023536ce6c3 Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Sat, 14 Feb 2026 17:50:10 -0500
Subject: [PATCH] Save one instruction on ARM
For assembler functions, doing them as best as possible is paramount.
---
libc/src/setjmp/arm/longjmp.cpp | 19 +++++++++++++++++--
libc/src/setjmp/arm/setjmp.cpp | 17 ++++++++++++++++-
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/libc/src/setjmp/arm/longjmp.cpp b/libc/src/setjmp/arm/longjmp.cpp
index 623ca8fc77be6..dec47aa20b6d1 100644
--- a/libc/src/setjmp/arm/longjmp.cpp
+++ b/libc/src/setjmp/arm/longjmp.cpp
@@ -49,7 +49,7 @@ namespace LIBC_NAMESPACE_DECL {
bx lr)");
}
-#else // Thumb2 or ARM
+#elif defined(__thumb__) && __ARM_ARCH_ISA_THUMB == 2
// TODO(https://github.com/llvm/llvm-project/issues/94061): fp registers
// (d0-d16)
@@ -59,7 +59,6 @@ namespace LIBC_NAMESPACE_DECL {
# While sp may appear in a register list for ARM mode, it may not for
# Thumb2 mode. Just load the previous value of sp into r12 then move it
# into sp, so that this code is portable between ARM and Thumb2.
-
ldm r0, {r4-r12, lr}
mov sp, r12
@@ -70,6 +69,22 @@ namespace LIBC_NAMESPACE_DECL {
bx lr)");
}
+#else // ARM
+
+// TODO(https://github.com/llvm/llvm-project/issues/94061): fp registers
+// (d0-d16)
+// TODO(https://github.com/llvm/llvm-project/issues/94062): pac+bti
+[[gnu::naked]] LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf buf, int val)) {
+ asm(R"(
+ # Store r4, r5, r6, r7, r8, r9, r10, r11, sp, and lr into buf.
+ ldm r0, {r4-r11, sp, lr}
+
+ # return val ?: 1;
+ movs r0, r1
+ moveq r0, #1
+ bx lr)");
+}
+
#endif
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/setjmp/arm/setjmp.cpp b/libc/src/setjmp/arm/setjmp.cpp
index 891ac3016c050..a4ba7949c5227 100644
--- a/libc/src/setjmp/arm/setjmp.cpp
+++ b/libc/src/setjmp/arm/setjmp.cpp
@@ -39,7 +39,7 @@ namespace LIBC_NAMESPACE_DECL {
bx lr)");
}
-#else // Thumb2 or ARM
+#elif defined(__thumb__) && __ARM_ARCH_ISA_THUMB == 2
// TODO(https://github.com/llvm/llvm-project/issues/94061): fp registers
// (d0-d16)
@@ -59,6 +59,21 @@ namespace LIBC_NAMESPACE_DECL {
bx lr)");
}
+#else // ARM
+
+// TODO(https://github.com/llvm/llvm-project/issues/94061): fp registers
+// (d0-d16)
+// TODO(https://github.com/llvm/llvm-project/issues/94062): pac+bti
+[[gnu::naked]] LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {
+ asm(R"(
+ # Store r4, r5, r6, r7, r8, r9, r10, r11, sp, and lr into buf.
+ stm r0, {r4-r11, sp, lr}
+
+ # Return zero.
+ mov r0, #0
+ bx lr)");
+}
+
#endif
} // namespace LIBC_NAMESPACE_DECL
More information about the libc-commits
mailing list