i386: Use add for a = a + b and a = b + a when possible
diff mbox series

Message ID CAMe9rOo91ajBMiSgY+wfdKHdv8OxVtDH+khrj5XECZeXWZDvxA@mail.gmail.com
State New
Headers show
Series
  • i386: Use add for a = a + b and a = b + a when possible
Related show

Commit Message

H.J. Lu Dec. 6, 2019, 7:27 p.m. UTC
Since except for Bonnell,

01 fb                add    %edi,%ebx

is faster and shorter than

8d 1c 1f              lea    (%rdi,%rbx,1),%ebx

we should use add for a = a + b and a = b + a when possible if not
optimizing for Bonnell.

Tested on x86-64.

gcc/

PR target/92807
* config/i386/i386.c (ix86_lea_outperforms): Check !TARGET_BONNELL.
(ix86_avoid_lea_for_addr): When not optimizing for Bonnell, use add
for a = a + b and a = b + a.

gcc/testsuite/

PR target/92807
* gcc.target/i386/pr92807-1.c: New test.

Patch
diff mbox series

From ad803a967a6c18ae3bd6f8381ebc8a78c31a82ae Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Tue, 3 Dec 2019 15:27:51 -0800
Subject: [PATCH] i386: Use add for a = a + b and a = b + a when possible

Since except for Bonnell,

01 fb                	add    %edi,%ebx

is faster and shorter than

8d 1c 1f             	lea    (%rdi,%rbx,1),%ebx

we should use add for a = a + b and a = b + a when possible if not
optimizing for Bonnell.

Tested on x86-64.

gcc/

	PR target/92807
	* config/i386/i386.c (ix86_lea_outperforms): Check !TARGET_BONNELL.
	(ix86_avoid_lea_for_addr): When not optimizing for Bonnell, use add
	for a = a + b and a = b + a.

gcc/testsuite/

	PR target/92807
	* gcc.target/i386/pr92807-1.c: New test.
---
 gcc/config/i386/i386.c                    | 27 +++++++++++++++--------
 gcc/testsuite/gcc.target/i386/pr92807-1.c | 11 +++++++++
 2 files changed, 29 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr92807-1.c

diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
index 04cbbd532c0d..65f0d44916a8 100644
--- a/gcc/config/i386/i386.c
+++ b/gcc/config/i386/i386.c
@@ -14393,11 +14393,10 @@  ix86_lea_outperforms (rtx_insn *insn, unsigned int regno0, unsigned int regno1,
 {
   int dist_define, dist_use;
 
-  /* For Silvermont if using a 2-source or 3-source LEA for
-     non-destructive destination purposes, or due to wanting
-     ability to use SCALE, the use of LEA is justified.  */
-  if (TARGET_SILVERMONT || TARGET_GOLDMONT || TARGET_GOLDMONT_PLUS
-      || TARGET_TREMONT || TARGET_INTEL)
+  /* For Atom processors newer than Bonnell, if using a 2-source or
+     3-source LEA for non-destructive destination purposes, or due to
+     wanting ability to use SCALE, the use of LEA is justified.  */
+  if (!TARGET_BONNELL)
     {
       if (has_scale)
 	return true;
@@ -14532,10 +14531,6 @@  ix86_avoid_lea_for_addr (rtx_insn *insn, rtx operands[])
   struct ix86_address parts;
   int ok;
 
-  /* Check we need to optimize.  */
-  if (!TARGET_AVOID_LEA_FOR_ADDR || optimize_function_for_size_p (cfun))
-    return false;
-
   /* The "at least two components" test below might not catch simple
      move or zero extension insns if parts.base is non-NULL and parts.disp
      is const0_rtx as the only components in the address, e.g. if the
@@ -14572,6 +14567,20 @@  ix86_avoid_lea_for_addr (rtx_insn *insn, rtx operands[])
   if (parts.index)
     regno2 = true_regnum (parts.index);
 
+  /* Use add for a = a + b and a = b + a since it is faster and shorter
+     than lea for most processors.  For the processors like BONNELL, if
+     the destination register of LEA holds an actual address which will
+     be used soon, LEA is better and otherwise ADD is better.  */
+  if (!TARGET_BONNELL
+      && parts.scale == 1
+      && (!parts.disp || parts.disp == const0_rtx)
+      && (regno0 == regno1 || regno0 == regno2))
+    return true;
+
+  /* Check we need to optimize.  */
+  if (!TARGET_AVOID_LEA_FOR_ADDR || optimize_function_for_size_p (cfun))
+    return false;
+
   split_cost = 0;
 
   /* Compute how many cycles we will add to execution time
diff --git a/gcc/testsuite/gcc.target/i386/pr92807-1.c b/gcc/testsuite/gcc.target/i386/pr92807-1.c
new file mode 100644
index 000000000000..00f92930af92
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr92807-1.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+unsigned int
+abs2 (unsigned int a) 
+{
+  unsigned int s = ((a>>15)&0x10001)*0xffff;
+  return (a+s)^s;
+}
+
+/* { dg-final { scan-assembler-not "leal" } } */
-- 
2.21.0