[PATCH] LoongArch: Optimize math barriers

Deng Jianbo dengjianbo@loongson.cn
Thu May 7 02:17:40 GMT 2026


The constraints 'frm' used in math_opt_barrier and math_force_eval cause
GCC choose fixed-point registers with lower costs in some cases, because
in LoongArch ira_reg_class_subunion[FP_REGS][GR_REGS] is initialized to
GR_REGS, extra move costs will be added when choosing FP_REGS. This
results in unnecessary instructions to move values between FP_REGS and
GR_REGS.

Most of cases in GLIBC math barriers related macros are invoked with
floating-point type paramters, this patch removes "r" constraints,
allowing GCC to keep values in floating-point registers and avoid the
extra moves.

Example from xflow function before the change:
	movfr2gr.d      $t0, $fa0
	beqz            $a0, 12 # 69960 <xflow+0x10>
	fneg.d          $fa1, $fa0
	movfr2gr.d      $t0, $fa1
	movgr2fr.d      $fa1, $t0
	fmul.d          $fa0, $fa0, $fa1
	b               -56 # 69930 <with_errno.constprop.0>

After the patch:
	fmov.d          $fa1, $fa0
	beqz            $a0, 12 # 69ac0 <xflow+0x10>
	fneg.d          $fa1, $fa0
	nop
	fmul.d          $fa0, $fa0, $fa1
	b               -52 # 69a90 <with_errno.constprop.0>
---
 sysdeps/loongarch/fpu/math-barriers.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sysdeps/loongarch/fpu/math-barriers.h b/sysdeps/loongarch/fpu/math-barriers.h
index 6a069ff41d..76835aa210 100644
--- a/sysdeps/loongarch/fpu/math-barriers.h
+++ b/sysdeps/loongarch/fpu/math-barriers.h
@@ -21,8 +21,8 @@
 
 /* Generic code forces values to memory; we don't need to do that.  */
 #define math_opt_barrier(x)					\
-  ({ __typeof (x) __x = (x); __asm ("" : "+frm" (__x)); __x; })
+  ({ __typeof (x) __x = (x); __asm ("" : "+fm" (__x)); __x; })
 #define math_force_eval(x)						\
-  ({ __typeof (x) __x = (x); __asm __volatile__ ("" : : "frm" (__x)); })
+  ({ __typeof (x) __x = (x); __asm __volatile__ ("" : : "fm" (__x)); })
 
 #endif /* math-barriers.h */
-- 
2.20.1



More information about the Libc-alpha mailing list