diff mbox

Adjust nb_iterations_upper_bound in loop header copying

Message ID alpine.LNX.2.00.1311211002490.8615@zhemvz.fhfr.qr
State New
Headers show

Commit Message

Richard Biener Nov. 21, 2013, 9:14 a.m. UTC
This patch decrements nb_iterations_upper_bound by one after we copied
the loop header.  This allows niter + 1 to more often not overflow.

Bootstrapped and tested on x86_64-unknown-linux-gnu, installed to trunk.

Richard.

2013-11-21  Richard Biener  <rguenther@suse.de>

	* tree-ssa-loop-ch.c (copy_loop_headers): Decrement
	nb_iterations_upper_bound by one.

Comments

Richard Sandiford Nov. 22, 2013, 12:55 p.m. UTC | #1
Richard Biener <rguenther@suse.de> writes:
> This patch decrements nb_iterations_upper_bound by one after we copied
> the loop header.  This allows niter + 1 to more often not overflow.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu, installed to trunk.
>
> Richard.
>
> 2013-11-21  Richard Biener  <rguenther@suse.de>
>
> 	* tree-ssa-loop-ch.c (copy_loop_headers): Decrement
> 	nb_iterations_upper_bound by one.

This seems to regress the following testcase, compiled at -O2 on
x86_64-linux-gnu.

Thanks,
Richard


void __attribute__ ((noinline))
f (long *s, long *t, int len1, int len2)
{
  int i, j;

  j = 2;
  for (i = len1 - 1; i >= 0; i--)
    {
      s[j--] = (i < len2 ? t[i] : t[len2 - 1] < 0 ? -1 : 0);
      if (j < 0)
	break;
    }
}

long s[3];
long t[3];

int
main (void)
{
  t[0] = 1;
  t[1] = 2;
  t[2] = 3;
  f (s, t, 3, 3);
  if (memcmp (s, t, sizeof (s)) != 0)
    abort ();
  return 0;
}
Richard Biener Nov. 22, 2013, 1:18 p.m. UTC | #2
On Fri, 22 Nov 2013, Richard Sandiford wrote:

> Richard Biener <rguenther@suse.de> writes:
> > This patch decrements nb_iterations_upper_bound by one after we copied
> > the loop header.  This allows niter + 1 to more often not overflow.
> >
> > Bootstrapped and tested on x86_64-unknown-linux-gnu, installed to trunk.
> >
> > Richard.
> >
> > 2013-11-21  Richard Biener  <rguenther@suse.de>
> >
> > 	* tree-ssa-loop-ch.c (copy_loop_headers): Decrement
> > 	nb_iterations_upper_bound by one.
> 
> This seems to regress the following testcase, compiled at -O2 on
> x86_64-linux-gnu.

Thanks for the testcase, I have reverted the patch and installed
the testcase.

Thanks,
Richard.

> Thanks,
> Richard
> 
> 
> void __attribute__ ((noinline))
> f (long *s, long *t, int len1, int len2)
> {
>   int i, j;
> 
>   j = 2;
>   for (i = len1 - 1; i >= 0; i--)
>     {
>       s[j--] = (i < len2 ? t[i] : t[len2 - 1] < 0 ? -1 : 0);
>       if (j < 0)
> 	break;
>     }
> }
> 
> long s[3];
> long t[3];
> 
> int
> main (void)
> {
>   t[0] = 1;
>   t[1] = 2;
>   t[2] = 3;
>   f (s, t, 3, 3);
>   if (memcmp (s, t, sizeof (s)) != 0)
>     abort ();
>   return 0;
> }
diff mbox

Patch

Index: gcc/tree-ssa-loop-ch.c
===================================================================
--- gcc/tree-ssa-loop-ch.c	(revision 205097)
+++ gcc/tree-ssa-loop-ch.c	(working copy)
@@ -243,6 +243,16 @@  copy_loop_headers (void)
 	 are not now, since there was the loop exit condition.  */
       split_edge (loop_preheader_edge (loop));
       split_edge (loop_latch_edge (loop));
+
+      /* We peeled off one iteration of the loop thus we can lower
+	 the maximum number of iterations if we have a previously
+	 recorded value for that.  */
+      double_int max;
+      if (get_max_loop_iterations (loop, &max))
+	{
+	  max -= double_int_one;
+	  loop->nb_iterations_upper_bound = max;
+	}
     }
 
   update_ssa (TODO_update_ssa);