1169689Skan/* Induction variable canonicalization.
2169689Skan   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3169689Skan
4169689SkanThis file is part of GCC.
5169689Skan
6169689SkanGCC is free software; you can redistribute it and/or modify it
7169689Skanunder the terms of the GNU General Public License as published by the
8169689SkanFree Software Foundation; either version 2, or (at your option) any
9169689Skanlater version.
10169689Skan
11169689SkanGCC is distributed in the hope that it will be useful, but WITHOUT
12169689SkanANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13169689SkanFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14169689Skanfor more details.
15169689Skan
16169689SkanYou should have received a copy of the GNU General Public License
17169689Skanalong with GCC; see the file COPYING.  If not, write to the Free
18169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19169689Skan02110-1301, USA.  */
20169689Skan
21169689Skan/* This pass detects the loops that iterate a constant number of times,
22169689Skan   adds a canonical induction variable (step -1, tested against 0)
23169689Skan   and replaces the exit test.  This enables the less powerful rtl
24169689Skan   level analysis to use this information.
25169689Skan
26169689Skan   This might spoil the code in some cases (by increasing register pressure).
27169689Skan   Note that in the case the new variable is not needed, ivopts will get rid
28169689Skan   of it, so it might only be a problem when there are no other linear induction
29169689Skan   variables.  In that case the created optimization possibilities are likely
30169689Skan   to pay up.
31169689Skan
32169689Skan   Additionally in case we detect that it is beneficial to unroll the
33169689Skan   loop completely, we do it right here to expose the optimization
34169689Skan   possibilities to the following passes.  */
35169689Skan
36169689Skan#include "config.h"
37169689Skan#include "system.h"
38169689Skan#include "coretypes.h"
39169689Skan#include "tm.h"
40169689Skan#include "tree.h"
41169689Skan#include "rtl.h"
42169689Skan#include "tm_p.h"
43169689Skan#include "hard-reg-set.h"
44169689Skan#include "basic-block.h"
45169689Skan#include "output.h"
46169689Skan#include "diagnostic.h"
47169689Skan#include "tree-flow.h"
48169689Skan#include "tree-dump.h"
49169689Skan#include "cfgloop.h"
50169689Skan#include "tree-pass.h"
51169689Skan#include "ggc.h"
52169689Skan#include "tree-chrec.h"
53169689Skan#include "tree-scalar-evolution.h"
54169689Skan#include "params.h"
55169689Skan#include "flags.h"
56169689Skan#include "tree-inline.h"
57169689Skan
58169689Skan/* Specifies types of loops that may be unrolled.  */
59169689Skan
60169689Skanenum unroll_level
61169689Skan{
62169689Skan  UL_SINGLE_ITER,	/* Only loops that exit immediately in the first
63169689Skan			   iteration.  */
64169689Skan  UL_NO_GROWTH,		/* Only loops whose unrolling will not cause increase
65169689Skan			   of code size.  */
66169689Skan  UL_ALL		/* All suitable loops.  */
67169689Skan};
68169689Skan
69169689Skan/* Adds a canonical induction variable to LOOP iterating NITER times.  EXIT
70169689Skan   is the exit edge whose condition is replaced.  */
71169689Skan
72169689Skanstatic void
73169689Skancreate_canonical_iv (struct loop *loop, edge exit, tree niter)
74169689Skan{
75169689Skan  edge in;
76169689Skan  tree cond, type, var;
77169689Skan  block_stmt_iterator incr_at;
78169689Skan  enum tree_code cmp;
79169689Skan
80169689Skan  if (dump_file && (dump_flags & TDF_DETAILS))
81169689Skan    {
82169689Skan      fprintf (dump_file, "Added canonical iv to loop %d, ", loop->num);
83169689Skan      print_generic_expr (dump_file, niter, TDF_SLIM);
84169689Skan      fprintf (dump_file, " iterations.\n");
85169689Skan    }
86169689Skan
87169689Skan  cond = last_stmt (exit->src);
88169689Skan  in = EDGE_SUCC (exit->src, 0);
89169689Skan  if (in == exit)
90169689Skan    in = EDGE_SUCC (exit->src, 1);
91169689Skan
92169689Skan  /* Note that we do not need to worry about overflows, since
93169689Skan     type of niter is always unsigned and all comparisons are
94169689Skan     just for equality/nonequality -- i.e. everything works
95169689Skan     with a modulo arithmetics.  */
96169689Skan
97169689Skan  type = TREE_TYPE (niter);
98169689Skan  niter = fold_build2 (PLUS_EXPR, type,
99169689Skan		       niter,
100169689Skan		       build_int_cst (type, 1));
101169689Skan  incr_at = bsi_last (in->src);
102169689Skan  create_iv (niter,
103169689Skan	     build_int_cst (type, -1),
104169689Skan	     NULL_TREE, loop,
105169689Skan	     &incr_at, false, NULL, &var);
106169689Skan
107169689Skan  cmp = (exit->flags & EDGE_TRUE_VALUE) ? EQ_EXPR : NE_EXPR;
108169689Skan  COND_EXPR_COND (cond) = build2 (cmp, boolean_type_node,
109169689Skan				  var,
110169689Skan				  build_int_cst (type, 0));
111169689Skan  update_stmt (cond);
112169689Skan}
113169689Skan
114169689Skan/* Computes an estimated number of insns in LOOP.  */
115169689Skan
116169689Skanunsigned
117169689Skantree_num_loop_insns (struct loop *loop)
118169689Skan{
119169689Skan  basic_block *body = get_loop_body (loop);
120169689Skan  block_stmt_iterator bsi;
121169689Skan  unsigned size = 1, i;
122169689Skan
123169689Skan  for (i = 0; i < loop->num_nodes; i++)
124169689Skan    for (bsi = bsi_start (body[i]); !bsi_end_p (bsi); bsi_next (&bsi))
125169689Skan      size += estimate_num_insns (bsi_stmt (bsi));
126169689Skan  free (body);
127169689Skan
128169689Skan  return size;
129169689Skan}
130169689Skan
131169689Skan/* Estimate number of insns of completely unrolled loop.  We assume
132169689Skan   that the size of the unrolled loop is decreased in the
133169689Skan   following way (the numbers of insns are based on what
134169689Skan   estimate_num_insns returns for appropriate statements):
135169689Skan
136169689Skan   1) exit condition gets removed (2 insns)
137169689Skan   2) increment of the control variable gets removed (2 insns)
138169689Skan   3) All remaining statements are likely to get simplified
139169689Skan      due to constant propagation.  Hard to estimate; just
140169689Skan      as a heuristics we decrease the rest by 1/3.
141169689Skan
142169689Skan   NINSNS is the number of insns in the loop before unrolling.
143169689Skan   NUNROLL is the number of times the loop is unrolled.  */
144169689Skan
145169689Skanstatic unsigned HOST_WIDE_INT
146169689Skanestimated_unrolled_size (unsigned HOST_WIDE_INT ninsns,
147169689Skan			 unsigned HOST_WIDE_INT nunroll)
148169689Skan{
149169689Skan  HOST_WIDE_INT unr_insns = 2 * ((HOST_WIDE_INT) ninsns - 4) / 3;
150169689Skan  if (unr_insns <= 0)
151169689Skan    unr_insns = 1;
152169689Skan  unr_insns *= (nunroll + 1);
153169689Skan
154169689Skan  return unr_insns;
155169689Skan}
156169689Skan
157169689Skan/* Tries to unroll LOOP completely, i.e. NITER times.  LOOPS is the
158169689Skan   loop tree.  UL determines which loops we are allowed to unroll.
159169689Skan   EXIT is the exit of the loop that should be eliminated.  */
160169689Skan
161169689Skanstatic bool
162169689Skantry_unroll_loop_completely (struct loops *loops ATTRIBUTE_UNUSED,
163169689Skan			    struct loop *loop,
164169689Skan			    edge exit, tree niter,
165169689Skan			    enum unroll_level ul)
166169689Skan{
167169689Skan  unsigned HOST_WIDE_INT n_unroll, ninsns, max_unroll, unr_insns;
168169689Skan  tree old_cond, cond, dont_exit, do_exit;
169169689Skan
170169689Skan  if (loop->inner)
171169689Skan    return false;
172169689Skan
173169689Skan  if (!host_integerp (niter, 1))
174169689Skan    return false;
175169689Skan  n_unroll = tree_low_cst (niter, 1);
176169689Skan
177169689Skan  max_unroll = PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES);
178169689Skan  if (n_unroll > max_unroll)
179169689Skan    return false;
180169689Skan
181169689Skan  if (n_unroll)
182169689Skan    {
183169689Skan      if (ul == UL_SINGLE_ITER)
184169689Skan	return false;
185169689Skan
186169689Skan      ninsns = tree_num_loop_insns (loop);
187169689Skan
188169689Skan      if (n_unroll * ninsns
189169689Skan	  > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS))
190169689Skan	return false;
191169689Skan
192169689Skan      if (ul == UL_NO_GROWTH)
193169689Skan	{
194169689Skan	  unr_insns = estimated_unrolled_size (ninsns, n_unroll);
195169689Skan
196169689Skan	  if (dump_file && (dump_flags & TDF_DETAILS))
197169689Skan	    {
198169689Skan	      fprintf (dump_file, "  Loop size: %d\n", (int) ninsns);
199169689Skan	      fprintf (dump_file, "  Estimated size after unrolling: %d\n",
200169689Skan		       (int) unr_insns);
201169689Skan	    }
202169689Skan
203169689Skan	  if (unr_insns > ninsns)
204169689Skan	    {
205169689Skan	      if (dump_file && (dump_flags & TDF_DETAILS))
206169689Skan		fprintf (dump_file, "Not unrolling loop %d:\n", loop->num);
207169689Skan	      return false;
208169689Skan	    }
209169689Skan	}
210169689Skan    }
211169689Skan
212169689Skan  if (exit->flags & EDGE_TRUE_VALUE)
213169689Skan    {
214169689Skan      dont_exit = boolean_false_node;
215169689Skan      do_exit = boolean_true_node;
216169689Skan    }
217169689Skan  else
218169689Skan    {
219169689Skan      dont_exit = boolean_true_node;
220169689Skan      do_exit = boolean_false_node;
221169689Skan    }
222169689Skan  cond = last_stmt (exit->src);
223169689Skan
224169689Skan  if (n_unroll)
225169689Skan    {
226169689Skan      sbitmap wont_exit;
227169689Skan      edge *edges_to_remove = XNEWVEC (edge, n_unroll);
228169689Skan      unsigned int n_to_remove = 0;
229169689Skan
230169689Skan      old_cond = COND_EXPR_COND (cond);
231169689Skan      COND_EXPR_COND (cond) = dont_exit;
232169689Skan      update_stmt (cond);
233169689Skan      initialize_original_copy_tables ();
234169689Skan
235169689Skan      wont_exit = sbitmap_alloc (n_unroll + 1);
236169689Skan      sbitmap_ones (wont_exit);
237169689Skan      RESET_BIT (wont_exit, 0);
238169689Skan
239169689Skan      if (!tree_duplicate_loop_to_header_edge (loop, loop_preheader_edge (loop),
240169689Skan					       loops, n_unroll, wont_exit,
241169689Skan					       exit, edges_to_remove,
242169689Skan					       &n_to_remove,
243169689Skan					       DLTHE_FLAG_UPDATE_FREQ
244169689Skan					       | DLTHE_FLAG_COMPLETTE_PEEL))
245169689Skan	{
246169689Skan	  COND_EXPR_COND (cond) = old_cond;
247169689Skan	  update_stmt (cond);
248169689Skan          free_original_copy_tables ();
249169689Skan	  free (wont_exit);
250169689Skan	  free (edges_to_remove);
251169689Skan	  return false;
252169689Skan	}
253169689Skan      free (wont_exit);
254169689Skan      free (edges_to_remove);
255169689Skan      free_original_copy_tables ();
256169689Skan    }
257169689Skan
258169689Skan  COND_EXPR_COND (cond) = do_exit;
259169689Skan  update_stmt (cond);
260169689Skan
261169689Skan  update_ssa (TODO_update_ssa);
262169689Skan
263169689Skan  if (dump_file && (dump_flags & TDF_DETAILS))
264169689Skan    fprintf (dump_file, "Unrolled loop %d completely.\n", loop->num);
265169689Skan
266169689Skan  return true;
267169689Skan}
268169689Skan
269169689Skan/* Adds a canonical induction variable to LOOP if suitable.  LOOPS is the loops
270169689Skan   tree.  CREATE_IV is true if we may create a new iv.  UL determines
271169689Skan   which loops we are allowed to completely unroll.  If TRY_EVAL is true, we try
272169689Skan   to determine the number of iterations of a loop by direct evaluation.
273169689Skan   Returns true if cfg is changed.  */
274169689Skan
275169689Skanstatic bool
276169689Skancanonicalize_loop_induction_variables (struct loops *loops, struct loop *loop,
277169689Skan				       bool create_iv, enum unroll_level ul,
278169689Skan				       bool try_eval)
279169689Skan{
280169689Skan  edge exit = NULL;
281169689Skan  tree niter;
282169689Skan
283169689Skan  niter = number_of_iterations_in_loop (loop);
284169689Skan  if (TREE_CODE (niter) == INTEGER_CST)
285169689Skan    {
286169689Skan      exit = loop->single_exit;
287169689Skan      if (!just_once_each_iteration_p (loop, exit->src))
288169689Skan	return false;
289169689Skan
290169689Skan      /* The result of number_of_iterations_in_loop is by one higher than
291169689Skan	 we expect (i.e. it returns number of executions of the exit
292169689Skan	 condition, not of the loop latch edge).  */
293169689Skan      niter = fold_build2 (MINUS_EXPR, TREE_TYPE (niter), niter,
294169689Skan			   build_int_cst (TREE_TYPE (niter), 1));
295169689Skan    }
296169689Skan  else
297169689Skan    {
298169689Skan      /* If the loop has more than one exit, try checking all of them
299169689Skan	 for # of iterations determinable through scev.  */
300169689Skan      if (!loop->single_exit)
301169689Skan	niter = find_loop_niter (loop, &exit);
302169689Skan
303169689Skan      /* Finally if everything else fails, try brute force evaluation.  */
304169689Skan      if (try_eval
305169689Skan	  && (chrec_contains_undetermined (niter)
306169689Skan	      || TREE_CODE (niter) != INTEGER_CST))
307169689Skan	niter = find_loop_niter_by_eval (loop, &exit);
308169689Skan
309169689Skan      if (chrec_contains_undetermined (niter)
310169689Skan	  || TREE_CODE (niter) != INTEGER_CST)
311169689Skan	return false;
312169689Skan    }
313169689Skan
314169689Skan  if (dump_file && (dump_flags & TDF_DETAILS))
315169689Skan    {
316169689Skan      fprintf (dump_file, "Loop %d iterates ", loop->num);
317169689Skan      print_generic_expr (dump_file, niter, TDF_SLIM);
318169689Skan      fprintf (dump_file, " times.\n");
319169689Skan    }
320169689Skan
321169689Skan  if (try_unroll_loop_completely (loops, loop, exit, niter, ul))
322169689Skan    return true;
323169689Skan
324169689Skan  if (create_iv)
325169689Skan    create_canonical_iv (loop, exit, niter);
326169689Skan
327169689Skan  return false;
328169689Skan}
329169689Skan
330169689Skan/* The main entry point of the pass.  Adds canonical induction variables
331169689Skan   to the suitable LOOPS.  */
332169689Skan
333169689Skanunsigned int
334169689Skancanonicalize_induction_variables (struct loops *loops)
335169689Skan{
336169689Skan  unsigned i;
337169689Skan  struct loop *loop;
338169689Skan  bool changed = false;
339169689Skan
340169689Skan  for (i = 1; i < loops->num; i++)
341169689Skan    {
342169689Skan      loop = loops->parray[i];
343169689Skan
344169689Skan      if (loop)
345169689Skan	changed |= canonicalize_loop_induction_variables (loops, loop,
346169689Skan							  true, UL_SINGLE_ITER,
347169689Skan							  true);
348169689Skan    }
349169689Skan
350169689Skan  /* Clean up the information about numbers of iterations, since brute force
351169689Skan     evaluation could reveal new information.  */
352169689Skan  scev_reset ();
353169689Skan
354169689Skan  if (changed)
355169689Skan    return TODO_cleanup_cfg;
356169689Skan  return 0;
357169689Skan}
358169689Skan
359169689Skan/* Unroll LOOPS completely if they iterate just few times.  Unless
360169689Skan   MAY_INCREASE_SIZE is true, perform the unrolling only if the
361169689Skan   size of the code does not increase.  */
362169689Skan
363169689Skanunsigned int
364169689Skantree_unroll_loops_completely (struct loops *loops, bool may_increase_size)
365169689Skan{
366169689Skan  unsigned i;
367169689Skan  struct loop *loop;
368169689Skan  bool changed = false;
369169689Skan  enum unroll_level ul;
370169689Skan
371169689Skan  for (i = 1; i < loops->num; i++)
372169689Skan    {
373169689Skan      loop = loops->parray[i];
374169689Skan
375169689Skan      if (!loop)
376169689Skan	continue;
377169689Skan
378169689Skan      if (may_increase_size && maybe_hot_bb_p (loop->header))
379169689Skan	ul = UL_ALL;
380169689Skan      else
381169689Skan	ul = UL_NO_GROWTH;
382169689Skan      changed |= canonicalize_loop_induction_variables (loops, loop,
383169689Skan							false, ul,
384169689Skan							!flag_tree_loop_ivcanon);
385169689Skan    }
386169689Skan
387169689Skan  /* Clean up the information about numbers of iterations, since complete
388169689Skan     unrolling might have invalidated it.  */
389169689Skan  scev_reset ();
390169689Skan
391169689Skan  if (changed)
392169689Skan    return TODO_cleanup_cfg;
393169689Skan  return 0;
394169689Skan}
395169689Skan
396169689Skan/* Checks whether LOOP is empty.  */
397169689Skan
398169689Skanstatic bool
399169689Skanempty_loop_p (struct loop *loop)
400169689Skan{
401169689Skan  edge exit;
402169689Skan  struct tree_niter_desc niter;
403169689Skan  tree phi, def;
404169689Skan  basic_block *body;
405169689Skan  block_stmt_iterator bsi;
406169689Skan  unsigned i;
407169689Skan  tree stmt;
408169689Skan
409169689Skan  /* If the loop has multiple exits, it is too hard for us to handle.
410169689Skan     Similarly, if the exit is not dominating, we cannot determine
411169689Skan     whether the loop is not infinite.  */
412169689Skan  exit = single_dom_exit (loop);
413169689Skan  if (!exit)
414169689Skan    return false;
415169689Skan
416169689Skan  /* The loop must be finite.  */
417169689Skan  if (!number_of_iterations_exit (loop, exit, &niter, false))
418169689Skan    return false;
419169689Skan
420169689Skan  /* Values of all loop exit phi nodes must be invariants.  */
421169689Skan  for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi))
422169689Skan    {
423169689Skan      if (!is_gimple_reg (PHI_RESULT (phi)))
424169689Skan	continue;
425169689Skan
426169689Skan      def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
427169689Skan
428169689Skan      if (!expr_invariant_in_loop_p (loop, def))
429169689Skan	return false;
430169689Skan    }
431169689Skan
432169689Skan  /* And there should be no memory modifying or from other reasons
433169689Skan     unremovable statements.  */
434169689Skan  body = get_loop_body (loop);
435169689Skan  for (i = 0; i < loop->num_nodes; i++)
436169689Skan    {
437169689Skan      /* Irreducible region might be infinite.  */
438169689Skan      if (body[i]->flags & BB_IRREDUCIBLE_LOOP)
439169689Skan	{
440169689Skan	  free (body);
441169689Skan	  return false;
442169689Skan	}
443169689Skan
444169689Skan      for (bsi = bsi_start (body[i]); !bsi_end_p (bsi); bsi_next (&bsi))
445169689Skan	{
446169689Skan	  stmt = bsi_stmt (bsi);
447169689Skan	  if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS)
448169689Skan	      || stmt_ann (stmt)->has_volatile_ops)
449169689Skan	    {
450169689Skan	      free (body);
451169689Skan	      return false;
452169689Skan	    }
453169689Skan
454169689Skan	  /* Also, asm statements and calls may have side effects and we
455169689Skan	     cannot change the number of times they are executed.  */
456169689Skan	  switch (TREE_CODE (stmt))
457169689Skan	    {
458169689Skan	    case RETURN_EXPR:
459169689Skan	    case MODIFY_EXPR:
460169689Skan	      stmt = get_call_expr_in (stmt);
461169689Skan	      if (!stmt)
462169689Skan		break;
463169689Skan
464169689Skan	    case CALL_EXPR:
465169689Skan	      if (TREE_SIDE_EFFECTS (stmt))
466169689Skan		{
467169689Skan		  free (body);
468169689Skan		  return false;
469169689Skan		}
470169689Skan	      break;
471169689Skan
472169689Skan	    case ASM_EXPR:
473169689Skan	      /* We cannot remove volatile assembler.  */
474169689Skan	      if (ASM_VOLATILE_P (stmt))
475169689Skan		{
476169689Skan		  free (body);
477169689Skan		  return false;
478169689Skan		}
479169689Skan	      break;
480169689Skan
481169689Skan	    default:
482169689Skan	      break;
483169689Skan	    }
484169689Skan	}
485169689Skan      }
486169689Skan  free (body);
487169689Skan
488169689Skan  return true;
489169689Skan}
490169689Skan
491169689Skan/* Remove LOOP by making it exit in the first iteration.  */
492169689Skan
493169689Skanstatic void
494169689Skanremove_empty_loop (struct loop *loop)
495169689Skan{
496169689Skan  edge exit = single_dom_exit (loop), non_exit;
497169689Skan  tree cond_stmt = last_stmt (exit->src);
498169689Skan  tree do_exit;
499169689Skan  basic_block *body;
500169689Skan  unsigned n_before, freq_in, freq_h;
501169689Skan  gcov_type exit_count = exit->count;
502169689Skan
503169689Skan  non_exit = EDGE_SUCC (exit->src, 0);
504169689Skan  if (non_exit == exit)
505169689Skan    non_exit = EDGE_SUCC (exit->src, 1);
506169689Skan
507169689Skan  if (exit->flags & EDGE_TRUE_VALUE)
508169689Skan    do_exit = boolean_true_node;
509169689Skan  else
510169689Skan    do_exit = boolean_false_node;
511169689Skan
512169689Skan  COND_EXPR_COND (cond_stmt) = do_exit;
513169689Skan  update_stmt (cond_stmt);
514169689Skan
515169689Skan  /* Let us set the probabilities of the edges coming from the exit block.  */
516169689Skan  exit->probability = REG_BR_PROB_BASE;
517169689Skan  non_exit->probability = 0;
518169689Skan  non_exit->count = 0;
519169689Skan
520169689Skan  /* Update frequencies and counts.  Everything before
521169689Skan     the exit needs to be scaled FREQ_IN/FREQ_H times,
522169689Skan     where FREQ_IN is the frequency of the entry edge
523169689Skan     and FREQ_H is the frequency of the loop header.
524169689Skan     Everything after the exit has zero frequency.  */
525169689Skan  freq_h = loop->header->frequency;
526169689Skan  freq_in = EDGE_FREQUENCY (loop_preheader_edge (loop));
527169689Skan  if (freq_h != 0)
528169689Skan    {
529169689Skan      body = get_loop_body_in_dom_order (loop);
530169689Skan      for (n_before = 1; n_before <= loop->num_nodes; n_before++)
531169689Skan	if (body[n_before - 1] == exit->src)
532169689Skan	  break;
533169689Skan      scale_bbs_frequencies_int (body, n_before, freq_in, freq_h);
534169689Skan      scale_bbs_frequencies_int (body + n_before, loop->num_nodes - n_before,
535169689Skan				 0, 1);
536169689Skan      free (body);
537169689Skan    }
538169689Skan
539169689Skan  /* Number of executions of exit is not changed, thus we need to restore
540169689Skan     the original value.  */
541169689Skan  exit->count = exit_count;
542169689Skan}
543169689Skan
544169689Skan/* Removes LOOP if it is empty.  Returns true if LOOP is removed.  CHANGED
545169689Skan   is set to true if LOOP or any of its subloops is removed.  */
546169689Skan
547169689Skanstatic bool
548169689Skantry_remove_empty_loop (struct loop *loop, bool *changed)
549169689Skan{
550169689Skan  bool nonempty_subloop = false;
551169689Skan  struct loop *sub;
552169689Skan
553169689Skan  /* First, all subloops must be removed.  */
554169689Skan  for (sub = loop->inner; sub; sub = sub->next)
555169689Skan    nonempty_subloop |= !try_remove_empty_loop (sub, changed);
556169689Skan
557169689Skan  if (nonempty_subloop || !empty_loop_p (loop))
558169689Skan    return false;
559169689Skan
560169689Skan  remove_empty_loop (loop);
561169689Skan  *changed = true;
562169689Skan  return true;
563169689Skan}
564169689Skan
565169689Skan/* Remove the empty LOOPS.  */
566169689Skan
567169689Skanunsigned int
568169689Skanremove_empty_loops (struct loops *loops)
569169689Skan{
570169689Skan  bool changed = false;
571169689Skan  struct loop *loop;
572169689Skan
573169689Skan  for (loop = loops->tree_root->inner; loop; loop = loop->next)
574169689Skan    try_remove_empty_loop (loop, &changed);
575169689Skan
576169689Skan  if (changed)
577169689Skan    {
578169689Skan      scev_reset ();
579169689Skan      return TODO_cleanup_cfg;
580169689Skan    }
581169689Skan  return 0;
582169689Skan}
583