This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix try_combine splitting (PR target/20322)


Hi!

The testcase below is miscompiled at -O2 on x86-64.
try_combine when run on (I1/I2/I3):
(insn 79 78 52 0 (parallel [
            (set (reg:DI 64 [ e$a ])
                (plus:DI (plus:DI (ltu:DI (reg:CC 17 flags)
                            (const_int 0 [0x0]))
                        (reg:DI 64 [ e$a ]))
                    (const_int 0 [0x0])))
            (clobber (reg:CC 17 flags))
        ]) 174 {adddi3_carry_rex64} (insn_list:REG_DEP_TRUE 77 (insn_list:REG_DEP_TRUE 78 (nil)))
    (expr_list:REG_DEAD (reg:CC 17 flags)
        (expr_list:REG_UNUSED (reg:CC 17 flags)
            (nil))))

(insn 52 79 53 0 (parallel [
            (set (reg:DI 76)
                (lshiftrt:DI (reg:DI 61 [ f$0 ])
                    (const_int 32 [0x20])))
            (clobber (reg:CC 17 flags))
        ]) 435 {*lshrdi3_1_rex64} (nil)
    (expr_list:REG_DEAD (reg:DI 61 [ f$0 ])
        (expr_list:REG_UNUSED (reg:CC 17 flags)
            (nil))))

(insn 53 52 55 0 (parallel [
            (set (reg:DI 60 [ e$a.6 ])
                (plus:DI (reg:DI 64 [ e$a ])
                    (reg:DI 76)))
            (clobber (reg:CC 17 flags))
        ]) 192 {*adddi_1_rex64} (insn_list:REG_DEP_TRUE 79 (insn_list:REG_DEP_TRUE 52 (nil)))
    (expr_list:REG_DEAD (reg:DI 64 [ e$a ])
        (expr_list:REG_DEAD (reg:DI 76)
            (expr_list:REG_UNUSED (reg:CC 17 flags)
                (nil)))))

first merges them all into one NEWPAT:
(set (reg:DI 60 [ e$a.6 ])
    (plus:DI (plus:DI (ltu:DI (reg:CC 17 flags)
                (const_int 0 [0x0]))
            (reg:DI 64 [ e$a ]))
        (lshiftrt:DI (reg:DI 61 [ f$0 ])
            (const_int 32 [0x20]))))
but because that is not recognized, find_split_point finds the LSHIFTRT
in it as desirable split point and the pattern is split into:
(set (reg:DI 76)
    (lshiftrt:DI (reg:DI 61 [ f$0 ])
        (const_int 32 [0x20])))
and
(set (reg:DI 60 [ e$a.6 ])
    (plus:DI (plus:DI (ltu:DI (reg:CC 17 flags)
                (const_int 0 [0x0]))
            (reg:DI 64 [ e$a ]))
        (reg:DI 76)))
But, recog_for_combine adds clobbers to the NEWI2PAT pattern
(reg:CC 17 flags) which is used by NEWPAT (and set earlier).

The following patch checks for that condition and punts.

Ok to commit to 4.0 and HEAD if testing succeeds?
It is a 4.0/4.1 regression and causes miscompilation of libcpp.

2005-03-07  Jakub Jelinek  <jakub@redhat.com>

	PR target/20322
	* combine.c (try_combine): If recog_for_combine added CLOBBERs
	to NEWI2PAT, fail to combine if they are used by NEWPAT.

	* gcc.dg/20050307-1.c: New test.

--- gcc/combine.c.jj	2005-03-02 18:53:00.000000000 +0100
+++ gcc/combine.c	2005-03-07 16:41:25.624400924 +0100
@@ -2417,6 +2417,20 @@ try_combine (rtx i3, rtx i2, rtx i1, int
 	  SUBST (*split, newdest);
 	  i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
 
+	  /* recog_for_combine might have added CLOBBERs to newi2pat.
+	     Make sure NEWPAT does not depend on the clobbered regs.  */
+	  if (GET_CODE (newi2pat) == PARALLEL)
+	    for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
+	      if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
+		{
+		  rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
+		  if (reg_overlap_mentioned_p (reg, newpat))
+		    {
+		      undo_all ();
+		      return 0;
+		    }
+		}
+
 	  /* If the split point was a MULT and we didn't have one before,
 	     don't use one now.  */
 	  if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
--- gcc/testsuite/gcc.dg/20050307-1.c.jj	2005-03-07 16:42:10.036508060 +0100
+++ gcc/testsuite/gcc.dg/20050307-1.c	2005-03-07 16:42:54.877537797 +0100
@@ -0,0 +1,52 @@
+/* PR target/20322 */
+
+extern void abort (void);
+
+typedef unsigned long T;
+typedef struct
+{
+  T a, b;
+  unsigned char c, d;
+} S;
+
+#define M (sizeof (T) * 4)
+
+S __attribute__((noinline))
+foo (T x, T y)
+{
+  S e;
+  T f[2], g;
+
+  e.b = (x & (~(T) 0 >> M)) * (y & (~(T) 0 >> M));
+  e.a = (x >> M) * (y >> M);
+
+  f[0] = (x & (~(T) 0 >> M)) * (y >> M);
+  f[1] = (x >> M) * (y & (~(T) 0 >> M));
+
+  g = e.b;
+  e.b += (f[0] & (~(T) 0 >> M)) << M;
+  if (e.b < g)
+    e.a++;
+
+  g = e.b;
+  e.b += (f[1] & (~(T) 0 >> M)) << M;
+  if (e.b < g)
+    e.a++;
+
+  e.a += (f[0] >> M);
+  e.a += (f[1] >> M);
+  e.c = 1;
+  e.d = 0;
+
+  return e;
+}
+
+int
+main (void)
+{
+  T x = 1UL << (M * 2 - 1);
+  S y = foo (1, x);
+  if (y.a || y.b != x || y.c != 1 || y.d)
+    abort ();
+  return 0;
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]