[PATCH] Fix reload failures on inline asm from mplayer SVN (PR rtl-optimization/39543)
Jakub Jelinek
jakub@redhat.com
Thu Mar 26 16:50:00 GMT 2009
Hi!
Since IRA merge GCC fails to compile mplayer SVN, there are asms with
many "m" and GCC isn't able to reload them (4.4 regression).
The problem seems to be that during expand we force the addresses of MEMs
into registers (memory_address called indirectly from expand_asm_operands):
/* By passing constant addresses through registers
we get a chance to cse them. */
if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
x = force_reg (Pmode, x);
but then neither CSE, nor FWPROP, nor combine is able to copy them back.
FWPROP which normally does that for normal memory uses doesn't do it
because it doesn't handle ASM_OPERANDS, nor insns with !single_set, and
combine if the pseudo is used multiple times doesn't handle it, because it
attempts to also add the pseudo initialization into the PARALLEL, making it
asm_noperands (pattern) == -1, invalid, therefore rejected.
Attached are two possible patches, the first one teaches fwprop
to propagate into __asm, with the limitation that it only propagates if
the propagation doesn't increase the number of used registers (to avoid
increasing register pressure on the __asm).
The second one just temporarily turns on cse_not_expected during
expand_asm_operands to avoid the forcing into registers.
While the fwprop patch can handle all the cases in the testcases,
the stmt.c patch doesn't handle the f2 function in first patch
pr39543-3.c, where gimple doesn't allow the more complex operands
in the __asm, so during expand the hack doesn't help.
Both patches bootstrapped/regtested on x86_64-linux and i686-linux,
ok for trunk (which one)?
Jakub
-------------- next part --------------
2009-03-26 Jakub Jelinek <jakub@redhat.com>
PR rtl-optimization/39543
* fwprop.c (check_reg_count_callback, forward_propagate_asm): New
functions.
(forward_propagate_and_simplify): Propagate also into __asm, if it
doesn't increase the number of referenced registers.
* gcc.target/i386/pr39543-1.c: New test.
* gcc.target/i386/pr39543-2.c: New test.
* gcc.target/i386/pr39543-3.c: New test.
--- gcc/fwprop.c.jj 2009-02-20 15:34:11.000000000 +0100
+++ gcc/fwprop.c 2009-03-25 16:34:49.000000000 +0100
@@ -1,5 +1,5 @@
/* RTL-based forward propagation pass for GNU compiler.
- Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+ Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
Contributed by Paolo Bonzini and Steven Bosscher.
This file is part of GCC.
@@ -852,6 +852,92 @@ forward_propagate_subreg (df_ref use, rt
return false;
}
+static int
+check_reg_count_callback (rtx *px, void *data)
+{
+ int *regnop = (int *) data;
+
+ if (!REG_P (*px))
+ return 0;
+
+ if (*regnop < 0 || *regnop == (int) REGNO (*px))
+ {
+ *regnop = REGNO (*px);
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Try to replace USE with SRC (defined in DEF_INSN) in __asm. */
+
+static bool
+forward_propagate_asm (df_ref use, rtx def_set, rtx reg)
+{
+ rtx use_insn = DF_REF_INSN (use), src, use_pat, asm_operands, new_rtx, *loc;
+ int regno, speed_p, i;
+
+ gcc_assert ((DF_REF_FLAGS (use) & DF_REF_IN_NOTE) == 0);
+
+ src = SET_SRC (def_set);
+ use_pat = PATTERN (use_insn);
+
+ /* In __asm don't replace if src might need more registers than
+ reg, as that could increase register pressure on the __asm. */
+ regno = -1;
+ if (for_each_rtx (&src, check_reg_count_callback, ®no) > 0)
+ return false;
+
+ speed_p = optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_insn));
+ asm_operands = NULL_RTX;
+ switch (GET_CODE (use_pat))
+ {
+ case ASM_OPERANDS:
+ asm_operands = use_pat;
+ break;
+ case SET:
+ loc = &SET_DEST (use_pat);
+ new_rtx = propagate_rtx (*loc, GET_MODE (*loc), reg, src, speed_p);
+ if (new_rtx)
+ validate_unshare_change (use_insn, loc, new_rtx, true);
+ asm_operands = SET_SRC (use_pat);
+ break;
+ case PARALLEL:
+ for (i = 0; i < XVECLEN (use_pat, 0); i++)
+ if (GET_CODE (XVECEXP (use_pat, 0, i)) == SET)
+ {
+ loc = &SET_DEST (XVECEXP (use_pat, 0, i));
+ new_rtx = propagate_rtx (*loc, GET_MODE (*loc), reg, src, speed_p);
+ if (new_rtx)
+ validate_unshare_change (use_insn, loc, new_rtx, true);
+ asm_operands = SET_SRC (XVECEXP (use_pat, 0, i));
+ }
+ else if (GET_CODE (XVECEXP (use_pat, 0, i)) == ASM_OPERANDS)
+ asm_operands = XVECEXP (use_pat, 0, i);
+ break;
+ default:
+ gcc_unreachable ();
+ }
+
+ gcc_assert (asm_operands && GET_CODE (asm_operands) == ASM_OPERANDS);
+ for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (asm_operands); i++)
+ {
+ loc = &ASM_OPERANDS_INPUT (asm_operands, i);
+ new_rtx = propagate_rtx (*loc, GET_MODE (*loc), reg, src, speed_p);
+ if (new_rtx)
+ validate_unshare_change (use_insn, loc, new_rtx, true);
+ }
+
+ if (num_changes_pending () == 0)
+ return false;
+
+ if (!apply_change_group ())
+ return false;
+
+ num_changes++;
+ return true;
+}
+
/* Try to replace USE with SRC (defined in DEF_INSN) and simplify the
result. */
@@ -863,12 +953,16 @@ forward_propagate_and_simplify (df_ref u
rtx src, reg, new_rtx, *loc;
bool set_reg_equal;
enum machine_mode mode;
+ int asm_use = -1;
- if (!use_set)
+ if (INSN_CODE (use_insn) < 0)
+ asm_use = asm_noperands (PATTERN (use_insn));
+
+ if (!use_set && asm_use < 0)
return false;
/* Do not propagate into PC, CC0, etc. */
- if (GET_MODE (SET_DEST (use_set)) == VOIDmode)
+ if (use_set && GET_MODE (SET_DEST (use_set)) == VOIDmode)
return false;
/* If def and use are subreg, check if they match. */
@@ -900,7 +994,7 @@ forward_propagate_and_simplify (df_ref u
if (MEM_P (src) && MEM_READONLY_P (src))
{
rtx x = avoid_constant_pool_reference (src);
- if (x != src)
+ if (x != src && use_set)
{
rtx note = find_reg_note (use_insn, REG_EQUAL, NULL_RTX);
rtx old_rtx = note ? XEXP (note, 0) : SET_SRC (use_set);
@@ -911,6 +1005,9 @@ forward_propagate_and_simplify (df_ref u
return false;
}
+ if (asm_use >= 0)
+ return forward_propagate_asm (use, def_set, reg);
+
/* Else try simplifying. */
if (DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE)
--- gcc/testsuite/gcc.target/i386/pr39543-1.c.jj 2009-03-25 16:40:18.000000000 +0100
+++ gcc/testsuite/gcc.target/i386/pr39543-1.c 2009-03-25 16:40:50.000000000 +0100
@@ -0,0 +1,52 @@
+/* PR rtl-optimization/39543 */
+/* { dg-do compile } */
+/* { dg-options "-O3 -fomit-frame-pointer" } */
+
+float __attribute__ ((aligned (16))) s0[128];
+const float s1 = 0.707;
+float s2[8] __attribute__ ((aligned (16)));
+float s3[8] __attribute__ ((aligned (16)));
+float s4[16] __attribute__ ((aligned (16)));
+float s5[16] __attribute__ ((aligned (16)));
+
+void
+foo (int k, float *x, float *y, const float *d, const float *z)
+{
+ float *a, *b, *c, *e;
+
+ a = x + 2 * k;
+ b = a + 2 * k;
+ c = b + 2 * k;
+ e = y + 2 * k;
+ __asm__ volatile (""
+ : "=m" (x[0]), "=m" (b[0]), "=m" (a[0]), "=m" (c[0])
+ : "m" (y[0]), "m" (y[k * 2]), "m" (x[0]), "m" (a[0])
+ : "memory");
+ for (;;)
+ {
+ __asm__ volatile (""
+ :
+ : "m" (y[2]), "m" (d[2]), "m" (e[2]), "m" (z[2])
+ : "memory");
+ if (!--k)
+ break;
+ }
+ __asm__ volatile (""
+ : "=m" (x[2]), "=m" (x[10]), "=m" (x[6]), "=m" (x[14])
+ : "m" (y[2]), "m" (y[6]), "m" (x[2]), "m" (x[6]),
+ "m" (y[18]), "m" (s1)
+ : "memory");
+}
+
+void
+bar (float *a)
+{
+ foo (4, a, a + 16, s2, s3);
+ foo (8, a, a + 32, s4, s5);
+}
+
+void
+baz (void)
+{
+ bar (s0);
+}
--- gcc/testsuite/gcc.target/i386/pr39543-2.c.jj 2009-03-25 16:40:18.000000000 +0100
+++ gcc/testsuite/gcc.target/i386/pr39543-2.c 2009-03-25 16:40:38.000000000 +0100
@@ -0,0 +1,51 @@
+/* PR rtl-optimization/39543 */
+/* { dg-do compile } */
+/* { dg-options "-O3" } */
+
+float __attribute__ ((aligned (16))) s0[128];
+const float s1 = 0.707;
+float s2[8] __attribute__ ((aligned (16)));
+float s3[8] __attribute__ ((aligned (16)));
+float s4[16] __attribute__ ((aligned (16)));
+float s5[16] __attribute__ ((aligned (16)));
+
+void
+foo (int k, float *x, float *y, const float *d, const float *z)
+{
+ float *a, *b, *c, *e;
+
+ a = x + 2 * k;
+ b = a + 2 * k;
+ c = b + 2 * k;
+ e = y + 2 * k;
+ __asm__ volatile (""
+ : "=m" (x[0]), "=m" (b[0]), "=m" (a[0]), "=m" (c[0])
+ : "m" (y[0]), "m" (y[k * 2]), "m" (x[0]), "m" (a[0])
+ : "memory");
+ for (;;)
+ {
+ __asm__ volatile (""
+ :
+ : "m" (y[2]), "m" (d[2]), "m" (e[2]), "m" (z[2])
+ : "memory");
+ if (!--k)
+ break;
+ }
+ __asm__ volatile (""
+ : "=m" (x[2]), "=m" (x[10]), "=m" (x[6]), "=m" (x[14])
+ : "m" (y[2]), "m" (y[6]), "m" (x[2]), "m" (x[6]), "m" (s1)
+ : "memory");
+}
+
+void
+bar (float *a)
+{
+ foo (4, a, a + 16, s2, s3);
+ foo (8, a, a + 32, s4, s5);
+}
+
+void
+baz (void)
+{
+ bar (s0);
+}
--- gcc/testsuite/gcc.target/i386/pr39543-3.c.jj 2009-03-25 16:41:29.000000000 +0100
+++ gcc/testsuite/gcc.target/i386/pr39543-3.c 2009-03-25 16:41:19.000000000 +0100
@@ -0,0 +1,42 @@
+/* PR rtl-optimization/39543 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+int s[128];
+
+void
+f1 (void)
+{
+ int i;
+ asm volatile ("# %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17"
+ : "=r" (i)
+ : "m" (s[0]), "m" (s[2]), "m" (s[4]), "m" (s[6]), "m" (s[8]),
+ "m" (s[10]), "m" (s[12]), "m" (s[14]), "m" (s[16]), "m" (s[18]),
+ "m" (s[20]), "m" (s[22]), "m" (s[24]), "m" (s[26]), "m" (s[28]),
+ "m" (s[30]), "m" (s[32]));
+ asm volatile ("# %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17"
+ : "=r" (i)
+ : "m" (s[0]), "m" (s[2]), "m" (s[4]), "m" (s[6]), "m" (s[8]),
+ "m" (s[10]), "m" (s[12]), "m" (s[14]), "m" (s[16]), "m" (s[18]),
+ "m" (s[20]), "m" (s[22]), "m" (s[24]), "m" (s[26]), "m" (s[28]),
+ "m" (s[30]), "m" (s[32]));
+}
+
+void
+f2 (int *q)
+{
+ int i;
+ int *p = q + 32;
+ asm volatile ("# %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17"
+ : "=r" (i)
+ : "m" (p[0]), "m" (p[2]), "m" (p[4]), "m" (p[6]), "m" (p[8]),
+ "m" (p[10]), "m" (p[12]), "m" (p[14]), "m" (p[16]), "m" (p[18]),
+ "m" (p[20]), "m" (p[22]), "m" (p[24]), "m" (p[26]), "m" (p[28]),
+ "m" (p[30]), "m" (p[32]));
+ asm volatile ("# %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17"
+ : "=r" (i)
+ : "m" (p[0]), "m" (p[2]), "m" (p[4]), "m" (p[6]), "m" (p[8]),
+ "m" (p[10]), "m" (p[12]), "m" (p[14]), "m" (p[16]), "m" (p[18]),
+ "m" (p[20]), "m" (p[22]), "m" (p[24]), "m" (p[26]), "m" (p[28]),
+ "m" (p[30]), "m" (p[32]));
+}
-------------- next part --------------
2009-03-26 Jakub Jelinek <jakub@redhat.com>
PR rtl-optimization/39543
* stmt.c (expand_asm_operands): If output or input operand allows
mem, set temporarily cse_not_expected around expand_expr to avoid
forcing MEM addresses into registers.
* gcc.target/i386/pr39543-1.c: New test.
* gcc.target/i386/pr39543-2.c: New test.
* gcc.target/i386/pr39543-3.c: New test.
--- gcc/stmt.c.jj 2009-03-02 09:45:47.000000000 +0100
+++ gcc/stmt.c 2009-03-26 14:26:54.000000000 +0100
@@ -803,7 +803,14 @@ expand_asm_operands (tree string, tree o
|| ! allows_reg
|| is_inout)
{
+ int old_cse_not_expected = cse_not_expected;
+ /* Avoid forcing memory addresses into registers if they are
+ constant, neither fwprop nor combiner will be able to fold
+ them back, which increases register pressure. */
+ if (allows_mem)
+ cse_not_expected = true;
op = expand_expr (val, NULL_RTX, VOIDmode, EXPAND_WRITE);
+ cse_not_expected = old_cse_not_expected;
if (MEM_P (op))
op = validize_mem (op);
@@ -862,6 +869,7 @@ expand_asm_operands (tree string, tree o
tree val, type;
rtx op;
bool ok;
+ int old_cse_not_expected = cse_not_expected;
constraint = constraints[i + noutputs];
ok = parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
@@ -870,6 +878,12 @@ expand_asm_operands (tree string, tree o
generating_concat_p = 0;
+ /* Avoid forcing memory addresses into registers if they are
+ constant, neither fwprop nor combiner will be able to fold
+ them back, which increases register pressure. */
+ if (allows_mem)
+ cse_not_expected = true;
+
val = TREE_VALUE (tail);
type = TREE_TYPE (val);
/* EXPAND_INITIALIZER will not generate code for valid initializer
@@ -885,6 +899,7 @@ expand_asm_operands (tree string, tree o
op = force_reg (GET_MODE (op), op);
else if (MEM_P (op))
op = validize_mem (op);
+ cse_not_expected = old_cse_not_expected;
if (asm_operand_ok (op, constraint, NULL) <= 0)
{
--- gcc/testsuite/gcc.target/i386/pr39543-1.c.jj 2009-03-25 16:40:18.000000000 +0100
+++ gcc/testsuite/gcc.target/i386/pr39543-1.c 2009-03-25 16:40:50.000000000 +0100
@@ -0,0 +1,52 @@
+/* PR rtl-optimization/39543 */
+/* { dg-do compile } */
+/* { dg-options "-O3 -fomit-frame-pointer" } */
+
+float __attribute__ ((aligned (16))) s0[128];
+const float s1 = 0.707;
+float s2[8] __attribute__ ((aligned (16)));
+float s3[8] __attribute__ ((aligned (16)));
+float s4[16] __attribute__ ((aligned (16)));
+float s5[16] __attribute__ ((aligned (16)));
+
+void
+foo (int k, float *x, float *y, const float *d, const float *z)
+{
+ float *a, *b, *c, *e;
+
+ a = x + 2 * k;
+ b = a + 2 * k;
+ c = b + 2 * k;
+ e = y + 2 * k;
+ __asm__ volatile (""
+ : "=m" (x[0]), "=m" (b[0]), "=m" (a[0]), "=m" (c[0])
+ : "m" (y[0]), "m" (y[k * 2]), "m" (x[0]), "m" (a[0])
+ : "memory");
+ for (;;)
+ {
+ __asm__ volatile (""
+ :
+ : "m" (y[2]), "m" (d[2]), "m" (e[2]), "m" (z[2])
+ : "memory");
+ if (!--k)
+ break;
+ }
+ __asm__ volatile (""
+ : "=m" (x[2]), "=m" (x[10]), "=m" (x[6]), "=m" (x[14])
+ : "m" (y[2]), "m" (y[6]), "m" (x[2]), "m" (x[6]),
+ "m" (y[18]), "m" (s1)
+ : "memory");
+}
+
+void
+bar (float *a)
+{
+ foo (4, a, a + 16, s2, s3);
+ foo (8, a, a + 32, s4, s5);
+}
+
+void
+baz (void)
+{
+ bar (s0);
+}
--- gcc/testsuite/gcc.target/i386/pr39543-2.c.jj 2009-03-25 16:40:18.000000000 +0100
+++ gcc/testsuite/gcc.target/i386/pr39543-2.c 2009-03-25 16:40:38.000000000 +0100
@@ -0,0 +1,51 @@
+/* PR rtl-optimization/39543 */
+/* { dg-do compile } */
+/* { dg-options "-O3" } */
+
+float __attribute__ ((aligned (16))) s0[128];
+const float s1 = 0.707;
+float s2[8] __attribute__ ((aligned (16)));
+float s3[8] __attribute__ ((aligned (16)));
+float s4[16] __attribute__ ((aligned (16)));
+float s5[16] __attribute__ ((aligned (16)));
+
+void
+foo (int k, float *x, float *y, const float *d, const float *z)
+{
+ float *a, *b, *c, *e;
+
+ a = x + 2 * k;
+ b = a + 2 * k;
+ c = b + 2 * k;
+ e = y + 2 * k;
+ __asm__ volatile (""
+ : "=m" (x[0]), "=m" (b[0]), "=m" (a[0]), "=m" (c[0])
+ : "m" (y[0]), "m" (y[k * 2]), "m" (x[0]), "m" (a[0])
+ : "memory");
+ for (;;)
+ {
+ __asm__ volatile (""
+ :
+ : "m" (y[2]), "m" (d[2]), "m" (e[2]), "m" (z[2])
+ : "memory");
+ if (!--k)
+ break;
+ }
+ __asm__ volatile (""
+ : "=m" (x[2]), "=m" (x[10]), "=m" (x[6]), "=m" (x[14])
+ : "m" (y[2]), "m" (y[6]), "m" (x[2]), "m" (x[6]), "m" (s1)
+ : "memory");
+}
+
+void
+bar (float *a)
+{
+ foo (4, a, a + 16, s2, s3);
+ foo (8, a, a + 32, s4, s5);
+}
+
+void
+baz (void)
+{
+ bar (s0);
+}
--- gcc/testsuite/gcc.target/i386/pr39543-3.c.jj 2009-03-25 16:41:29.000000000 +0100
+++ gcc/testsuite/gcc.target/i386/pr39543-3.c 2009-03-25 16:41:19.000000000 +0100
@@ -0,0 +1,23 @@
+/* PR rtl-optimization/39543 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+int s[128];
+
+void
+f1 (void)
+{
+ int i;
+ asm volatile ("# %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17"
+ : "=r" (i)
+ : "m" (s[0]), "m" (s[2]), "m" (s[4]), "m" (s[6]), "m" (s[8]),
+ "m" (s[10]), "m" (s[12]), "m" (s[14]), "m" (s[16]), "m" (s[18]),
+ "m" (s[20]), "m" (s[22]), "m" (s[24]), "m" (s[26]), "m" (s[28]),
+ "m" (s[30]), "m" (s[32]));
+ asm volatile ("# %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17"
+ : "=r" (i)
+ : "m" (s[0]), "m" (s[2]), "m" (s[4]), "m" (s[6]), "m" (s[8]),
+ "m" (s[10]), "m" (s[12]), "m" (s[14]), "m" (s[16]), "m" (s[18]),
+ "m" (s[20]), "m" (s[22]), "m" (s[24]), "m" (s[26]), "m" (s[28]),
+ "m" (s[30]), "m" (s[32]));
+}
More information about the Gcc-patches
mailing list