On Mon, Mar 04, 2013 at 09:08:36PM +0000, Richard Sandiford wrote:
> ...truncations can't be to a narrower mode (already asserted at the top
> of the function) so in some ways I think it would be clearer to have an
> "if ... else" rather than two "if"s.

So like this instead?

2013-03-04  Jakub Jelinek  <ja...@redhat.com>

        PR rtl-optimization/56494
        * simplify-rtx.c (simplify_truncation): If C is narrower than A,
        optimize (truncate:A (subreg:B (truncate:C X) 0)) into
        (subreg:A (truncate:C X) 0) instead of (truncate:A X).

        * gcc.dg/pr56494.c: New test.

--- gcc/simplify-rtx.c.jj       2013-03-04 12:10:39.007677516 +0100
+++ gcc/simplify-rtx.c  2013-03-04 22:23:49.450894108 +0100
@@ -757,8 +757,17 @@ simplify_truncation (enum machine_mode m
       && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op)))
       && GET_CODE (SUBREG_REG (op)) == TRUNCATE
       && subreg_lowpart_p (op))
-    return simplify_gen_unary (TRUNCATE, mode, XEXP (SUBREG_REG (op), 0),
-                              GET_MODE (XEXP (SUBREG_REG (op), 0)));
+    {
+      rtx inner = XEXP (SUBREG_REG (op), 0);
+      if (GET_MODE_PRECISION (mode)
+         <= GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op))))
+       return simplify_gen_unary (TRUNCATE, mode, inner, GET_MODE (inner));
+      else
+       /* If subreg above is paradoxical and C is narrower
+          than A, return (subreg:A (truncate:C X) 0).  */
+       return simplify_gen_subreg (mode, SUBREG_REG (op),
+                                   GET_MODE (SUBREG_REG (op)), 0);
+    }
 
   /* (truncate:A (truncate:B X)) is (truncate:A X).  */
   if (GET_CODE (op) == TRUNCATE)
--- gcc/testsuite/gcc.dg/pr56494.c.jj   2013-03-04 22:23:10.016124685 +0100
+++ gcc/testsuite/gcc.dg/pr56494.c      2013-03-04 22:23:10.016124685 +0100
@@ -0,0 +1,13 @@
+/* PR rtl-optimization/56494 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -ftracer -w" } */
+
+char a;
+short b;
+void bar (int);
+
+void
+foo (void)
+{
+  bar ((!!b ? : (a *= a / 0)) >= (a = b));
+}


        Jakub

Reply via email to