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]

Re: [PATCH] Fix regression in IVOPTS


> I think this is not sufficient -- the same problem could appear if NIT
> is not a constant.

That's my understanding as well.

> Therefore, if NIT is not a constant, then we can proceed only if the loop
> has exactly one exit (loop_only_exit_p), so that we know that the comparison
> that is eliminated is not always false.

Thanks for the tip.  Like so?


	* tree-flow.h (loop_only_exit_p): Declare.
	* tree-ssa-loop-niter.c (loop_only_exit_p): Make public.
        * tree-ssa-loop-ivopts.c (may_eliminate_iv): Reinstate direct check on
        the number of iterations if it is constant.  Otherwise, if this is the
	only possible exit of the loop, use the conservative estimate on the
	number of iterations of the entire loop if available.


-- 
Eric Botcazou
Index: tree-flow.h
===================================================================
--- tree-flow.h	(revision 137041)
+++ tree-flow.h	(working copy)
@@ -989,6 +989,7 @@ void tree_ssa_iv_optimize (void);
 unsigned tree_predictive_commoning (void);
 bool parallelize_loops (void);
 
+bool loop_only_exit_p (const struct loop *, const_edge);
 bool number_of_iterations_exit (struct loop *, edge,
 				struct tree_niter_desc *niter, bool);
 tree find_loop_niter (struct loop *, edge *);
Index: tree-ssa-loop-niter.c
===================================================================
--- tree-ssa-loop-niter.c	(revision 137041)
+++ tree-ssa-loop-niter.c	(working copy)
@@ -1672,7 +1672,7 @@ simplify_using_outer_evolutions (struct 
 
 /* Returns true if EXIT is the only possible exit from LOOP.  */
 
-static bool
+bool
 loop_only_exit_p (const struct loop *loop, const_edge exit)
 {
   basic_block *body;
Index: tree-ssa-loop-ivopts.c
===================================================================
--- tree-ssa-loop-ivopts.c	(revision 137041)
+++ tree-ssa-loop-ivopts.c	(working copy)
@@ -3725,7 +3725,6 @@ may_eliminate_iv (struct ivopts_data *da
   tree nit, period;
   struct loop *loop = data->current_loop;
   aff_tree bnd;
-  double_int period_value, max_niter;
 
   if (TREE_CODE (cand->iv->step) != INTEGER_CST)
     return false;
@@ -3749,19 +3748,33 @@ may_eliminate_iv (struct ivopts_data *da
   if (!nit)
     return false;
 
-  /* Determine whether we may use the variable to test whether niter iterations
-     elapsed.  This is the case iff the period of the induction variable is
-     greater than the number of iterations.  */
+  /* Determine whether we can use the variable to test the exit condition.
+     This is the case if the period of the induction variable is greater
+     than the number of iterations for which the exit condition is true.  */
   period = iv_period (cand->iv);
-  if (!period)
-    return false;
 
-  /* Compare the period with the estimate on the number of iterations of the
-     loop.  */
-  if (!estimated_loop_iterations (loop, true, &max_niter))
-    return false;
-  period_value = tree_to_double_int (period);
-  if (double_int_ucmp (period_value, max_niter) <= 0)
+  /* If the number of iterations is constant, test the condition directly.  */
+  if (TREE_CODE (nit) == INTEGER_CST)
+    {
+      if (!tree_int_cst_lt (nit, period))
+	return false;
+    }
+
+  /* If not, and if this is the only possible exit of the loop, see whether
+     we can get a conservative estimate on the number of iterations of the
+     entire loop and use that instead.  */
+  else if (loop_only_exit_p (loop, exit))
+    {
+      double_int period_value, max_niter;
+      if (!estimated_loop_iterations (loop, true, &max_niter))
+	return false;
+      period_value = tree_to_double_int (period);
+      if (double_int_ucmp (max_niter, period_value) >= 0)
+	return false;
+    }
+
+  /* Otherwise, punt.  */
+  else
     return false;
 
   cand_value_at (loop, cand, use->stmt, nit, &bnd);

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