]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Reduce cost of MEM (A + imm).
authorliuhongt <hongtao.liu@intel.com>
Mon, 19 Feb 2024 05:57:24 +0000 (13:57 +0800)
committerliuhongt <hongtao.liu@intel.com>
Tue, 28 May 2024 22:59:15 +0000 (06:59 +0800)
For MEM, rtx_cost iterates each subrtx, and adds up the costs,
so for MEM (reg) and MEM (reg + 4), the former costs 5,
the latter costs 9, it is not accurate for x86. Ideally
address_cost should be used, but it reduce cost too much.
So current solution is make constant disp as cheap as possible.

gcc/ChangeLog:

PR target/67325
* config/i386/i386.cc (ix86_rtx_costs): Reduce cost of MEM (A
+ imm) to "cost of MEM (A)" + 1.

gcc/testsuite/ChangeLog:

* gcc.target/i386/pr67325.c: New test.

gcc/config/i386/i386.cc
gcc/testsuite/gcc.target/i386/pr67325.c [new file with mode: 0644]

index 3e2a3a194f1c812dca22f71d419b285d30f120e0..85d87b9f7787e16435042c1d5835e990c74751f7 100644 (file)
@@ -22194,7 +22194,23 @@ ix86_rtx_costs (rtx x, machine_mode mode, int outer_code_i, int opno,
       /* An insn that accesses memory is slightly more expensive
          than one that does not.  */
       if (speed)
-        *total += 1;
+       {
+         *total += 1;
+         rtx addr = XEXP (x, 0);
+         /* For MEM, rtx_cost iterates each subrtx, and adds up the costs,
+            so for MEM (reg) and MEM (reg + 4), the former costs 5,
+            the latter costs 9, it is not accurate for x86. Ideally
+            address_cost should be used, but it reduce cost too much.
+            So current solution is make constant disp as cheap as possible.  */
+         if (GET_CODE (addr) == PLUS
+             && x86_64_immediate_operand (XEXP (addr, 1), Pmode))
+           {
+             *total += 1;
+             *total += rtx_cost (XEXP (addr, 0), Pmode, PLUS, 0, speed);
+             return true;
+           }
+       }
+
       return false;
 
     case ZERO_EXTRACT:
diff --git a/gcc/testsuite/gcc.target/i386/pr67325.c b/gcc/testsuite/gcc.target/i386/pr67325.c
new file mode 100644 (file)
index 0000000..c3c1e4c
--- /dev/null
@@ -0,0 +1,7 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler-not "(?:sar|shr)" } } */
+
+int f(long*l){
+  return *l>>32;
+}