public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Fix PR41497: apparent integer wrong code bug (due to scev analysis)
@ 2009-10-22  0:36 Sebastian Pop
  0 siblings, 0 replies; only message in thread
From: Sebastian Pop @ 2009-10-22  0:36 UTC (permalink / raw)
  To: GCC Patches, Richard Guenther

[-- Attachment #1: Type: text/plain, Size: 1099 bytes --]

Hi,

The attached patch solves a problem in the scalar evolution analyzer.
SCEV analyzer used to return an evolution that was incompatible with
the initial value of the induction variable without knowing whether
the loop was running at least once.  The problem occurs only when
there is no evolution in the analyzed loop, in which case the loop is
equivalent to a condition that merges the values coming from before
the loop and the value computed in the loop.  If these two symbolic
expressions do not match, the scev analysis now returns "don't know"
instead of returning the value computed in the loop.

I will commit the attached patch to trunk.  The patch passed bootstrap
and test on amd64-linux.

	* tree-scalar-evolution.c (analyze_evolution_in_loop): Return
	chrec_dont_know if the evolution function returned by follow_ssa_edge
	is constant in the analyzed loop and is not compatible with the
	initial value before the loop.
	* tree-chrec.h (no_evolution_in_loop_p): Call STRIP_NOPS.

	* gcc.dg/tree-ssa/pr41497.c: New.

Sebastian Pop
--
AMD / Open Source Compiler Engineering / GNU Tools

[-- Attachment #2: 1706_pr41497.diff --]
[-- Type: text/x-patch, Size: 2617 bytes --]


	* tree-scalar-evolution.c (analyze_evolution_in_loop): Return
	chrec_dont_know if the evolution function returned by follow_ssa_edge
	is constant in the analyzed loop and is not compatible with the
	initial value before the loop.
	* tree-chrec.h (no_evolution_in_loop_p): Call STRIP_NOPS.

	* gcc.dg/tree-ssa/pr41497.c: New.

Index: tree-scalar-evolution.c
===================================================================
--- tree-scalar-evolution.c	(revision 153033)
+++ tree-scalar-evolution.c	(working copy)
@@ -1492,18 +1492,29 @@ analyze_evolution_in_loop (gimple loop_p
       bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
       if (!flow_bb_inside_loop_p (loop, bb))
 	continue;
-      
+
       if (TREE_CODE (arg) == SSA_NAME)
 	{
+	  bool val = false;
+
 	  ssa_chain = SSA_NAME_DEF_STMT (arg);
 
 	  /* Pass in the initial condition to the follow edge function.  */
 	  ev_fn = init_cond;
 	  res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn, 0);
+
+	  /* If ev_fn has no evolution in the inner loop, and the
+	     init_cond is not equal to ev_fn, then we have an
+	     ambiguity between two possible values, as we cannot know
+	     the number of iterations at this point.  */
+	  if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
+	      && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
+	      && !operand_equal_p (init_cond, ev_fn, 0))
+	    ev_fn = chrec_dont_know;
 	}
       else
 	res = t_false;
-	      
+
       /* When it is impossible to go back on the same
 	 loop_phi_node by following the ssa edges, the
 	 evolution is represented by a peeled chrec, i.e. the
Index: tree-chrec.h
===================================================================
--- tree-chrec.h	(revision 153033)
+++ tree-chrec.h	(working copy)
@@ -115,6 +115,7 @@ no_evolution_in_loop_p (tree chrec, unsi
       || chrec_contains_symbols_defined_in_loop (chrec, loop_num))
     return false;
 
+  STRIP_NOPS (chrec);
   scev = hide_evolution_in_other_loops_than_loop (chrec, loop_num);
   *res = !tree_is_chrec (scev);
   return true;
Index: testsuite/gcc.dg/tree-ssa/pr41497.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/pr41497.c	(revision 0)
+++ testsuite/gcc.dg/tree-ssa/pr41497.c	(revision 0)
@@ -0,0 +1,27 @@
+/* { dg-do run } */
+/* { dg-options "-Os" } */
+
+extern void abort (void);
+
+unsigned int a;
+int b, c;
+
+void
+foo (void)
+{
+  b = 0;
+  do {
+    for (a = -13; a == 0; a = (unsigned short)a)
+      c = 1;
+    b++;
+  } while (b == 0);
+}
+
+int
+main ()
+{
+  foo ();
+  if (a != -13)
+    abort ();
+  return 0;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-10-21 22:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-22  0:36 Fix PR41497: apparent integer wrong code bug (due to scev analysis) Sebastian Pop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).