[PATCH v1] LoongArch: Optimize fixed-point and floating-point conversion operations.

Lulu Cheng chenglulu@loongson.cn
Thu Aug 31 11:19:25 GMT 2023


Before optimization, the operation of taking fixed-point numbers from memory
and then forcing type conversion needs to be loaded into fixed-point registers
before conversion. After the optimization is completed, the fixed-point value
is directly transferred to the floating-point register for type conversion.

eg:
    extern int a;
    float
    test(void)
    {
      return (float)a;
    }

Assembly code before optimization:
	pcalau12i	$r12,%got_pc_hi20(a)
	ld.d	$r12,$r12,%got_pc_lo12(a)
	ldptr.w	$r12,$r12,0
	movgr2fr.w	$f0,$r12
	ffint.s.w	$f0,$f0

Optimized assembly code:
	pcalau12i	$r12,%got_pc_hi20(a)
	ld.d	$r12,$r12,%got_pc_lo12(a)
	fld.s	$f0,$r12,0
	ffint.s.w	$f0,$f0

gcc/ChangeLog:

	* config/loongarch/loongarch.md: Allows fixed-point values to be loaded
	from memory into floating-point registers.

gcc/testsuite/ChangeLog:

	* gcc.target/loongarch/float-load.c: New test.
---
 gcc/config/loongarch/loongarch.md               |  4 ++--
 gcc/testsuite/gcc.target/loongarch/float-load.c | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/loongarch/float-load.c

diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md
index a6fb2b935fa..eb580697915 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -1828,8 +1828,8 @@ (define_expand "movsi"
 })
 
 (define_insn_and_split "*movsi_internal"
-  [(set (match_operand:SI 0 "nonimmediate_operand" "=r,r,r,w,*f,*f,*r,*m,*r,*z")
-	(match_operand:SI 1 "move_operand" "r,Yd,w,rJ,*r*J,*m,*f,*f,*z,*r"))]
+  [(set (match_operand:SI 0 "nonimmediate_operand" "=r,r,r,w,*f,f,*r,*m,*r,*z")
+	(match_operand:SI 1 "move_operand" "r,Yd,w,rJ,*r*J,m,*f,*f,*z,*r"))]
   "(register_operand (operands[0], SImode)
     || reg_or_0_operand (operands[1], SImode))"
   { return loongarch_output_move (operands[0], operands[1]); }
diff --git a/gcc/testsuite/gcc.target/loongarch/float-load.c b/gcc/testsuite/gcc.target/loongarch/float-load.c
new file mode 100644
index 00000000000..c757a795e21
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/float-load.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler "fld\\.s" } } */
+
+extern int a;
+float
+test (void)
+{
+  return (float)a;
+}
+
-- 
2.31.1



More information about the Gcc-patches mailing list