cfgloopmanip.c revision 302408
150397Sobrien/* Loop manipulation code for GNU compiler.
2132718Skan   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3169689Skan
418334SpeterThis file is part of GCC.
5117395Skan
618334SpeterGCC is free software; you can redistribute it and/or modify it under
7117395Skanthe terms of the GNU General Public License as published by the Free
8117395SkanSoftware Foundation; either version 2, or (at your option) any later
9117395Skanversion.
10117395Skan
1118334SpeterGCC is distributed in the hope that it will be useful, but WITHOUT ANY
12117395SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or
13117395SkanFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14117395Skanfor more details.
15117395Skan
1618334SpeterYou should have received a copy of the GNU General Public License
17117395Skanalong with GCC; see the file COPYING.  If not, write to the Free
18117395SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19169689Skan02110-1301, USA.  */
20169689Skan
2118334Speter#include "config.h"
2290075Sobrien#include "system.h"
2390075Sobrien#include "coretypes.h"
2418334Speter#include "tm.h"
25117395Skan#include "rtl.h"
2618334Speter#include "hard-reg-set.h"
27117395Skan#include "obstack.h"
2818334Speter#include "basic-block.h"
29117395Skan#include "cfgloop.h"
30117395Skan#include "cfglayout.h"
31117395Skan#include "cfghooks.h"
32117395Skan#include "output.h"
33117395Skan
34117395Skanstatic void duplicate_subloops (struct loops *, struct loop *, struct loop *);
35117395Skanstatic void copy_loops_to (struct loops *, struct loop **, int,
3618334Speter			   struct loop *);
37117395Skanstatic void loop_redirect_edge (edge, basic_block);
38169689Skanstatic bool loop_delete_branch_edge (edge, int);
39117395Skanstatic void remove_bbs (basic_block *, int);
40117395Skanstatic bool rpe_enum_p (basic_block, void *);
41117395Skanstatic int find_path (edge, basic_block **);
4218334Speterstatic bool alp_enum_p (basic_block, void *);
43117395Skanstatic void add_loop (struct loops *, struct loop *);
44117395Skanstatic void fix_loop_placements (struct loops *, struct loop *, bool *);
45169689Skanstatic bool fix_bb_placement (struct loops *, basic_block);
46169689Skanstatic void fix_bb_placements (struct loops *, basic_block, bool *);
47169689Skanstatic void place_new_loop (struct loops *, struct loop *);
48169689Skanstatic void scale_loop_frequencies (struct loop *, int, int);
49169689Skanstatic basic_block create_preheader (struct loop *, int);
50117395Skanstatic void unloop (struct loops *, struct loop *, bool *);
51132718Skan
52132718Skan#define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
53169689Skan
54117395Skan/* Checks whether basic block BB is dominated by DATA.  */
55117395Skanstatic bool
5690075Sobrienrpe_enum_p (basic_block bb, void *data)
57169689Skan{
58169689Skan  return dominated_by_p (CDI_DOMINATORS, bb, data);
59169689Skan}
60169689Skan
61169689Skan/* Remove basic blocks BBS from loop structure and dominance info,
62169689Skan   and delete them afterwards.  */
63117395Skanstatic void
64117395Skanremove_bbs (basic_block *bbs, int nbbs)
65117395Skan{
66117395Skan  int i;
6718334Speter
68117395Skan  for (i = 0; i < nbbs; i++)
69117395Skan    {
70117395Skan      remove_bb_from_loops (bbs[i]);
7118334Speter      delete_basic_block (bbs[i]);
72117395Skan    }
73117395Skan}
74117395Skan
75117395Skan/* Find path -- i.e. the basic blocks dominated by edge E and put them
7618334Speter   into array BBS, that will be allocated large enough to contain them.
77117395Skan   E->dest must have exactly one predecessor for this to work (it is
78117395Skan   easy to achieve and we do not put it here because we do not want to
79117395Skan   alter anything by this function).  The number of basic blocks in the
80117395Skan   path is returned.  */
81117395Skanstatic int
82117395Skanfind_path (edge e, basic_block **bbs)
83117395Skan{
84117395Skan  gcc_assert (EDGE_COUNT (e->dest->preds) <= 1);
85117395Skan
86117395Skan  /* Find bbs in the path.  */
87117395Skan  *bbs = XCNEWVEC (basic_block, n_basic_blocks);
88117395Skan  return dfs_enumerate_from (e->dest, 0, rpe_enum_p, *bbs,
89117395Skan			     n_basic_blocks, e->dest);
9018334Speter}
91117395Skan
92117395Skan/* Fix placement of basic block BB inside loop hierarchy stored in LOOPS --
93117395Skan   Let L be a loop to that BB belongs.  Then every successor of BB must either
94117395Skan     1) belong to some superloop of loop L, or
95117395Skan     2) be a header of loop K such that K->outer is superloop of L
96117395Skan   Returns true if we had to move BB into other loop to enforce this condition,
97117395Skan   false if the placement of BB was already correct (provided that placements
98117395Skan   of its successors are correct).  */
99117395Skanstatic bool
100117395Skanfix_bb_placement (struct loops *loops, basic_block bb)
101117395Skan{
102117395Skan  edge e;
103117395Skan  edge_iterator ei;
104117395Skan  struct loop *loop = loops->tree_root, *act;
105117395Skan
106117395Skan  FOR_EACH_EDGE (e, ei, bb->succs)
107117395Skan    {
108117395Skan      if (e->dest == EXIT_BLOCK_PTR)
109117395Skan	continue;
110117395Skan
111117395Skan      act = e->dest->loop_father;
11218334Speter      if (act->header == e->dest)
11318334Speter	act = act->outer;
11418334Speter
115117395Skan      if (flow_loop_nested_p (loop, act))
116117395Skan	loop = act;
117117395Skan    }
118117395Skan
119132718Skan  if (loop == bb->loop_father)
120132718Skan    return false;
121132718Skan
122132718Skan  remove_bb_from_loops (bb);
12318334Speter  add_bb_to_loop (bb, loop);
124117395Skan
125117395Skan  return true;
12618334Speter}
127117395Skan
128117395Skan/* Fix placements of basic blocks inside loop hierarchy stored in loops; i.e.
12918334Speter   enforce condition condition stated in description of fix_bb_placement. We
130117395Skan   start from basic block FROM that had some of its successors removed, so that
131117395Skan   his placement no longer has to be correct, and iteratively fix placement of
13218334Speter   its predecessors that may change if placement of FROM changed.  Also fix
133132718Skan   placement of subloops of FROM->loop_father, that might also be altered due
134132718Skan   to this change; the condition for them is similar, except that instead of
135132718Skan   successors we consider edges coming out of the loops.
136117395Skan
137117395Skan   If the changes may invalidate the information about irreducible regions,
13818334Speter   IRRED_INVALIDATED is set to true.  */
139117395Skan
140117395Skanstatic void
14118334Speterfix_bb_placements (struct loops *loops, basic_block from,
142169689Skan		   bool *irred_invalidated)
143169689Skan{
144169689Skan  sbitmap in_queue;
145132718Skan  basic_block *queue, *qtop, *qbeg, *qend;
146169689Skan  struct loop *base_loop;
147169689Skan  edge e;
148169689Skan
149169689Skan  /* We pass through blocks back-reachable from FROM, testing whether some
150117395Skan     of their successors moved to outer loop.  It may be necessary to
151117395Skan     iterate several times, but it is finite, as we stop unless we move
152117395Skan     the basic block up the loop structure.  The whole story is a bit
153117395Skan     more complicated due to presence of subloops, those are moved using
154117395Skan     fix_loop_placement.  */
155117395Skan
156117395Skan  base_loop = from->loop_father;
15718334Speter  if (base_loop == loops->tree_root)
15818334Speter    return;
159169689Skan
160169689Skan  in_queue = sbitmap_alloc (last_basic_block);
161169689Skan  sbitmap_zero (in_queue);
162169689Skan  SET_BIT (in_queue, from->index);
163169689Skan  /* Prevent us from going out of the base_loop.  */
164132718Skan  SET_BIT (in_queue, base_loop->header->index);
165169689Skan
166169689Skan  queue = XNEWVEC (basic_block, base_loop->num_nodes + 1);
16718334Speter  qtop = queue + base_loop->num_nodes + 1;
168169689Skan  qbeg = queue;
169169689Skan  qend = queue + 1;
170169689Skan  *qbeg = from;
171169689Skan
172169689Skan  while (qbeg != qend)
173117395Skan    {
174169689Skan      edge_iterator ei;
175169689Skan      from = *qbeg;
176169689Skan      qbeg++;
177169689Skan      if (qbeg == qtop)
178169689Skan	qbeg = queue;
179169689Skan      RESET_BIT (in_queue, from->index);
18090075Sobrien
18118334Speter      if (from->loop_father->header == from)
182117395Skan	{
183169689Skan	  /* Subloop header, maybe move the loop upward.  */
184132718Skan	  if (!fix_loop_placement (from->loop_father))
18518334Speter	    continue;
186117395Skan	}
187132718Skan      else
18818334Speter	{
189117395Skan	  /* Ordinary basic block.  */
190132718Skan	  if (!fix_bb_placement (loops, from))
19118334Speter	    continue;
192117395Skan	}
193132718Skan
19418334Speter      FOR_EACH_EDGE (e, ei, from->succs)
195117395Skan	{
196132718Skan	  if (e->flags & EDGE_IRREDUCIBLE_LOOP)
19752284Sobrien	    *irred_invalidated = true;
198117395Skan	}
199132718Skan
20018334Speter      /* Something has changed, insert predecessors into queue.  */
201117395Skan      FOR_EACH_EDGE (e, ei, from->preds)
202132718Skan	{
20318334Speter	  basic_block pred = e->src;
204117395Skan	  struct loop *nca;
205132718Skan
206132718Skan	  if (e->flags & EDGE_IRREDUCIBLE_LOOP)
20718334Speter	    *irred_invalidated = true;
208117395Skan
209132718Skan	  if (TEST_BIT (in_queue, pred->index))
21018334Speter	    continue;
211117395Skan
212132718Skan	  /* If it is subloop, then it either was not moved, or
213132718Skan	     the path up the loop tree from base_loop do not contain
21418334Speter	     it.  */
215117395Skan	  nca = find_common_loop (pred->loop_father, base_loop);
216132718Skan	  if (pred->loop_father != base_loop
217132718Skan	      && (nca == base_loop
21818334Speter		  || nca != pred->loop_father))
219117395Skan	    pred = pred->loop_father->header;
220132718Skan	  else if (!flow_loop_nested_p (from->loop_father, pred->loop_father))
221132718Skan	    {
222132718Skan	      /* No point in processing it.  */
22318334Speter	      continue;
224117395Skan	    }
225132718Skan
226169689Skan	  if (TEST_BIT (in_queue, pred->index))
227169689Skan	    continue;
22850397Sobrien
229117395Skan	  /* Schedule the basic block.  */
230132718Skan	  *qend = pred;
231132718Skan	  qend++;
23250397Sobrien	  if (qend == qtop)
233132718Skan	    qend = queue;
234132718Skan	  SET_BIT (in_queue, pred->index);
235132718Skan	}
23618334Speter    }
237132718Skan  free (in_queue);
238132718Skan  free (queue);
239132718Skan}
240132718Skan
24118334Speter/* Removes path beginning at edge E, i.e. remove basic blocks dominated by E
242132718Skan   and update loop structure stored in LOOPS and dominators.  Return true if
24318334Speter   we were able to remove the path, false otherwise (and nothing is affected
244132718Skan   then).  */
24518334Speterbool
246132718Skanremove_path (struct loops *loops, edge e)
24718334Speter{
248132718Skan  edge ae;
24918334Speter  basic_block *rem_bbs, *bord_bbs, *dom_bbs, from, bb;
250132718Skan  int i, nrem, n_bord_bbs, n_dom_bbs;
25118334Speter  sbitmap seen;
252132718Skan  bool deleted, irred_invalidated = false;
253117395Skan
254117395Skan  if (!loop_delete_branch_edge (e, 0))
255132718Skan    return false;
256117395Skan
257132718Skan  /* Keep track of whether we need to update information about irreducible
258117395Skan     regions.  This is the case if the removed area is a part of the
259117395Skan     irreducible region, or if the set of basic blocks that belong to a loop
260117395Skan     that is inside an irreducible region is changed, or if such a loop is
261117395Skan     removed.  */
262117395Skan  if (e->flags & EDGE_IRREDUCIBLE_LOOP)
263132718Skan    irred_invalidated = true;
264117395Skan
265132718Skan  /* We need to check whether basic blocks are dominated by the edge
266117395Skan     e, but we only have basic block dominators.  This is easy to
267117395Skan     fix -- when e->dest has exactly one predecessor, this corresponds
268117395Skan     to blocks dominated by e->dest, if not, split the edge.  */
269117395Skan  if (!single_pred_p (e->dest))
270117395Skan    e = single_pred_edge (loop_split_edge_with (e, NULL_RTX));
271117395Skan
272117395Skan  /* It may happen that by removing path we remove one or more loops
273117395Skan     we belong to.  In this case first unloop the loops, then proceed
274169689Skan     normally.   We may assume that e->dest is not a header of any loop,
275169689Skan     as it now has exactly one predecessor.  */
276169689Skan  while (e->src->loop_father->outer
27718334Speter	 && dominated_by_p (CDI_DOMINATORS,
27818334Speter			    e->src->loop_father->latch, e->dest))
279117395Skan    unloop (loops, e->src->loop_father, &irred_invalidated);
280117395Skan
28118334Speter  /* Identify the path.  */
282117395Skan  nrem = find_path (e, &rem_bbs);
283117395Skan
28418334Speter  n_bord_bbs = 0;
285117395Skan  bord_bbs = XCNEWVEC (basic_block, n_basic_blocks);
286117395Skan  seen = sbitmap_alloc (last_basic_block);
287117395Skan  sbitmap_zero (seen);
28818334Speter
289117395Skan  /* Find "border" hexes -- i.e. those with predecessor in removed path.  */
290117395Skan  for (i = 0; i < nrem; i++)
29118334Speter    SET_BIT (seen, rem_bbs[i]->index);
292117395Skan  for (i = 0; i < nrem; i++)
293117395Skan    {
29450397Sobrien      edge_iterator ei;
295117395Skan      bb = rem_bbs[i];
296117395Skan      FOR_EACH_EDGE (ae, ei, rem_bbs[i]->succs)
29718334Speter	if (ae->dest != EXIT_BLOCK_PTR && !TEST_BIT (seen, ae->dest->index))
298117395Skan	  {
299117395Skan	    SET_BIT (seen, ae->dest->index);
30018334Speter	    bord_bbs[n_bord_bbs++] = ae->dest;
301117395Skan
302117395Skan	    if (ae->flags & EDGE_IRREDUCIBLE_LOOP)
303117395Skan	      irred_invalidated = true;
304117395Skan	  }
30518334Speter    }
306117395Skan
307117395Skan  /* Remove the path.  */
30818334Speter  from = e->src;
309117395Skan  deleted = loop_delete_branch_edge (e, 1);
310117395Skan  gcc_assert (deleted);
311117395Skan  dom_bbs = XCNEWVEC (basic_block, n_basic_blocks);
31218334Speter
313117395Skan  /* Cancel loops contained in the path.  */
314117395Skan  for (i = 0; i < nrem; i++)
31518334Speter    if (rem_bbs[i]->loop_father->header == rem_bbs[i])
316117395Skan      cancel_loop_tree (loops, rem_bbs[i]->loop_father);
317117395Skan
31818334Speter  remove_bbs (rem_bbs, nrem);
319169689Skan  free (rem_bbs);
320169689Skan
321169689Skan  /* Find blocks whose dominators may be affected.  */
322169689Skan  n_dom_bbs = 0;
323169689Skan  sbitmap_zero (seen);
324169689Skan  for (i = 0; i < n_bord_bbs; i++)
325169689Skan    {
326169689Skan      basic_block ldom;
327169689Skan
328169689Skan      bb = get_immediate_dominator (CDI_DOMINATORS, bord_bbs[i]);
329169689Skan      if (TEST_BIT (seen, bb->index))
330169689Skan	continue;
331169689Skan      SET_BIT (seen, bb->index);
332132718Skan
333132718Skan      for (ldom = first_dom_son (CDI_DOMINATORS, bb);
33418334Speter	   ldom;
335117395Skan	   ldom = next_dom_son (CDI_DOMINATORS, ldom))
336117395Skan	if (!dominated_by_p (CDI_DOMINATORS, from, ldom))
33752284Sobrien	  dom_bbs[n_dom_bbs++] = ldom;
338132718Skan    }
339132718Skan
34018334Speter  free (seen);
341117395Skan
342117395Skan  /* Recount dominators.  */
34318334Speter  iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
344117395Skan  free (dom_bbs);
345117395Skan  free (bord_bbs);
34618334Speter
347132718Skan  /* Fix placements of basic blocks inside loops and the placement of
34818334Speter     loops in the loop tree.  */
349132718Skan  fix_bb_placements (loops, from, &irred_invalidated);
35018334Speter  fix_loop_placements (loops, from->loop_father, &irred_invalidated);
351117395Skan
352117395Skan  if (irred_invalidated
35318334Speter      && (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS) != 0)
354117395Skan    mark_irreducible_loops (loops);
355117395Skan
35618334Speter  return true;
357117395Skan}
358117395Skan
359117395Skan/* Predicate for enumeration in add_loop.  */
360117395Skanstatic bool
361117395Skanalp_enum_p (basic_block bb, void *alp_header)
362117395Skan{
363117395Skan  return bb != (basic_block) alp_header;
364117395Skan}
365117395Skan
366117395Skan/* Given LOOP structure with filled header and latch, find the body of the
367132718Skan   corresponding loop and add it to LOOPS tree.  */
368117395Skanstatic void
369117395Skanadd_loop (struct loops *loops, struct loop *loop)
370132718Skan{
371117395Skan  basic_block *bbs;
372117395Skan  int i, n;
37318334Speter
374132718Skan  /* Add it to loop structure.  */
37518334Speter  place_new_loop (loops, loop);
37618334Speter  loop->level = 1;
37718334Speter
37818334Speter  /* Find its nodes.  */
379132718Skan  bbs = XCNEWVEC (basic_block, n_basic_blocks);
380132718Skan  n = dfs_enumerate_from (loop->latch, 1, alp_enum_p,
38118334Speter			  bbs, n_basic_blocks, loop->header);
382132718Skan
383132718Skan  for (i = 0; i < n; i++)
384132718Skan    add_bb_to_loop (bbs[i], loop);
385132718Skan  add_bb_to_loop (loop->header, loop);
386132718Skan
38718334Speter  free (bbs);
38818334Speter}
38918334Speter
390132718Skan/* Multiply all frequencies in LOOP by NUM/DEN.  */
39118334Speterstatic void
392117395Skanscale_loop_frequencies (struct loop *loop, int num, int den)
393117395Skan{
394169689Skan  basic_block *bbs;
39518334Speter
39618334Speter  bbs = get_loop_body (loop);
397117395Skan  scale_bbs_frequencies_int (bbs, loop->num_nodes, num, den);
398117395Skan  free (bbs);
399132718Skan}
40018334Speter
401117395Skan/* Make area between HEADER_EDGE and LATCH_EDGE a loop by connecting
402132718Skan   latch to header and update loop tree stored in LOOPS and dominators
40318334Speter   accordingly. Everything between them plus LATCH_EDGE destination must
404117395Skan   be dominated by HEADER_EDGE destination, and back-reachable from
405132718Skan   LATCH_EDGE source.  HEADER_EDGE is redirected to basic block SWITCH_BB,
40618334Speter   FALSE_EDGE of SWITCH_BB to original destination of HEADER_EDGE and
407132718Skan   TRUE_EDGE of SWITCH_BB to original destination of LATCH_EDGE.
408132718Skan   Returns newly created loop.  */
409132718Skan
41018334Speterstruct loop *
411132718Skanloopify (struct loops *loops, edge latch_edge, edge header_edge,
412132718Skan	 basic_block switch_bb, edge true_edge, edge false_edge,
413132718Skan	 bool redirect_all_edges)
414132718Skan{
415132718Skan  basic_block succ_bb = latch_edge->dest;
416132718Skan  basic_block pred_bb = header_edge->src;
417132718Skan  basic_block *dom_bbs, *body;
418132718Skan  unsigned n_dom_bbs, i;
419132718Skan  sbitmap seen;
420132718Skan  struct loop *loop = XCNEW (struct loop);
421132718Skan  struct loop *outer = succ_bb->loop_father->outer;
422169689Skan  int freq, prob, tot_prob;
423169689Skan  gcov_type cnt;
424132718Skan  edge e;
425169689Skan  edge_iterator ei;
426169689Skan
427169689Skan  loop->header = header_edge->dest;
42890075Sobrien  loop->latch = latch_edge->src;
429
430  freq = EDGE_FREQUENCY (header_edge);
431  cnt = header_edge->count;
432  prob = EDGE_SUCC (switch_bb, 0)->probability;
433  tot_prob = prob + EDGE_SUCC (switch_bb, 1)->probability;
434  if (tot_prob == 0)
435    tot_prob = 1;
436
437  /* Redirect edges.  */
438  loop_redirect_edge (latch_edge, loop->header);
439  loop_redirect_edge (true_edge, succ_bb);
440
441  /* During loop versioning, one of the switch_bb edge is already properly
442     set. Do not redirect it again unless redirect_all_edges is true.  */
443  if (redirect_all_edges)
444    {
445      loop_redirect_edge (header_edge, switch_bb);
446      loop_redirect_edge (false_edge, loop->header);
447
448      /* Update dominators.  */
449      set_immediate_dominator (CDI_DOMINATORS, switch_bb, pred_bb);
450      set_immediate_dominator (CDI_DOMINATORS, loop->header, switch_bb);
451    }
452
453  set_immediate_dominator (CDI_DOMINATORS, succ_bb, switch_bb);
454
455  /* Compute new loop.  */
456  add_loop (loops, loop);
457  flow_loop_tree_node_add (outer, loop);
458
459  /* Add switch_bb to appropriate loop.  */
460  add_bb_to_loop (switch_bb, outer);
461
462  /* Fix frequencies.  */
463  switch_bb->frequency = freq;
464  switch_bb->count = cnt;
465  FOR_EACH_EDGE (e, ei, switch_bb->succs)
466    e->count = (switch_bb->count * e->probability) / REG_BR_PROB_BASE;
467  scale_loop_frequencies (loop, prob, tot_prob);
468  scale_loop_frequencies (succ_bb->loop_father, tot_prob - prob, tot_prob);
469
470  /* Update dominators of blocks outside of LOOP.  */
471  dom_bbs = XCNEWVEC (basic_block, n_basic_blocks);
472  n_dom_bbs = 0;
473  seen = sbitmap_alloc (last_basic_block);
474  sbitmap_zero (seen);
475  body = get_loop_body (loop);
476
477  for (i = 0; i < loop->num_nodes; i++)
478    SET_BIT (seen, body[i]->index);
479
480  for (i = 0; i < loop->num_nodes; i++)
481    {
482      basic_block ldom;
483
484      for (ldom = first_dom_son (CDI_DOMINATORS, body[i]);
485	   ldom;
486	   ldom = next_dom_son (CDI_DOMINATORS, ldom))
487	if (!TEST_BIT (seen, ldom->index))
488	  {
489	    SET_BIT (seen, ldom->index);
490	    dom_bbs[n_dom_bbs++] = ldom;
491	  }
492    }
493
494  iterate_fix_dominators (CDI_DOMINATORS, dom_bbs, n_dom_bbs);
495
496  free (body);
497  free (seen);
498  free (dom_bbs);
499
500  return loop;
501}
502
503/* Remove the latch edge of a LOOP and update LOOPS tree to indicate that
504   the LOOP was removed.  After this function, original loop latch will
505   have no successor, which caller is expected to fix somehow.
506
507   If this may cause the information about irreducible regions to become
508   invalid, IRRED_INVALIDATED is set to true.  */
509
510static void
511unloop (struct loops *loops, struct loop *loop, bool *irred_invalidated)
512{
513  basic_block *body;
514  struct loop *ploop;
515  unsigned i, n;
516  basic_block latch = loop->latch;
517  bool dummy = false;
518
519  if (loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP)
520    *irred_invalidated = true;
521
522  /* This is relatively straightforward.  The dominators are unchanged, as
523     loop header dominates loop latch, so the only thing we have to care of
524     is the placement of loops and basic blocks inside the loop tree.  We
525     move them all to the loop->outer, and then let fix_bb_placements do
526     its work.  */
527
528  body = get_loop_body (loop);
529  n = loop->num_nodes;
530  for (i = 0; i < n; i++)
531    if (body[i]->loop_father == loop)
532      {
533	remove_bb_from_loops (body[i]);
534	add_bb_to_loop (body[i], loop->outer);
535      }
536  free(body);
537
538  while (loop->inner)
539    {
540      ploop = loop->inner;
541      flow_loop_tree_node_remove (ploop);
542      flow_loop_tree_node_add (loop->outer, ploop);
543    }
544
545  /* Remove the loop and free its data.  */
546  flow_loop_tree_node_remove (loop);
547  loops->parray[loop->num] = NULL;
548  flow_loop_free (loop);
549
550  remove_edge (single_succ_edge (latch));
551
552  /* We do not pass IRRED_INVALIDATED to fix_bb_placements here, as even if
553     there is an irreducible region inside the cancelled loop, the flags will
554     be still correct.  */
555  fix_bb_placements (loops, latch, &dummy);
556}
557
558/* Fix placement of LOOP inside loop tree, i.e. find the innermost superloop
559   FATHER of LOOP such that all of the edges coming out of LOOP belong to
560   FATHER, and set it as outer loop of LOOP.  Return true if placement of
561   LOOP changed.  */
562
563int
564fix_loop_placement (struct loop *loop)
565{
566  basic_block *body;
567  unsigned i;
568  edge e;
569  edge_iterator ei;
570  struct loop *father = loop->pred[0], *act;
571
572  body = get_loop_body (loop);
573  for (i = 0; i < loop->num_nodes; i++)
574    FOR_EACH_EDGE (e, ei, body[i]->succs)
575      if (!flow_bb_inside_loop_p (loop, e->dest))
576	{
577	  act = find_common_loop (loop, e->dest->loop_father);
578	  if (flow_loop_nested_p (father, act))
579	    father = act;
580	}
581  free (body);
582
583  if (father != loop->outer)
584    {
585      for (act = loop->outer; act != father; act = act->outer)
586	act->num_nodes -= loop->num_nodes;
587      flow_loop_tree_node_remove (loop);
588      flow_loop_tree_node_add (father, loop);
589      return 1;
590    }
591  return 0;
592}
593
594/* Fix placement of superloops of LOOP inside loop tree, i.e. ensure that
595   condition stated in description of fix_loop_placement holds for them.
596   It is used in case when we removed some edges coming out of LOOP, which
597   may cause the right placement of LOOP inside loop tree to change.
598
599   IRRED_INVALIDATED is set to true if a change in the loop structures might
600   invalidate the information about irreducible regions.  */
601
602static void
603fix_loop_placements (struct loops *loops, struct loop *loop,
604		     bool *irred_invalidated)
605{
606  struct loop *outer;
607
608  while (loop->outer)
609    {
610      outer = loop->outer;
611      if (!fix_loop_placement (loop))
612	break;
613
614      /* Changing the placement of a loop in the loop tree may alter the
615	 validity of condition 2) of the description of fix_bb_placement
616	 for its preheader, because the successor is the header and belongs
617	 to the loop.  So call fix_bb_placements to fix up the placement
618	 of the preheader and (possibly) of its predecessors.  */
619      fix_bb_placements (loops, loop_preheader_edge (loop)->src,
620			 irred_invalidated);
621      loop = outer;
622    }
623}
624
625/* Creates place for a new LOOP in LOOPS structure.  */
626static void
627place_new_loop (struct loops *loops, struct loop *loop)
628{
629  loops->parray =
630    xrealloc (loops->parray, (loops->num + 1) * sizeof (struct loop *));
631  loops->parray[loops->num] = loop;
632
633  loop->num = loops->num++;
634}
635
636/* Copies copy of LOOP as subloop of TARGET loop, placing newly
637   created loop into LOOPS structure.  */
638struct loop *
639duplicate_loop (struct loops *loops, struct loop *loop, struct loop *target)
640{
641  struct loop *cloop;
642  cloop = XCNEW (struct loop);
643  place_new_loop (loops, cloop);
644
645  /* Initialize copied loop.  */
646  cloop->level = loop->level;
647
648  /* Set it as copy of loop.  */
649  loop->copy = cloop;
650
651  /* Add it to target.  */
652  flow_loop_tree_node_add (target, cloop);
653
654  return cloop;
655}
656
657/* Copies structure of subloops of LOOP into TARGET loop, placing
658   newly created loops into loop tree stored in LOOPS.  */
659static void
660duplicate_subloops (struct loops *loops, struct loop *loop, struct loop *target)
661{
662  struct loop *aloop, *cloop;
663
664  for (aloop = loop->inner; aloop; aloop = aloop->next)
665    {
666      cloop = duplicate_loop (loops, aloop, target);
667      duplicate_subloops (loops, aloop, cloop);
668    }
669}
670
671/* Copies structure of subloops of N loops, stored in array COPIED_LOOPS,
672   into TARGET loop, placing newly created loops into loop tree LOOPS.  */
673static void
674copy_loops_to (struct loops *loops, struct loop **copied_loops, int n, struct loop *target)
675{
676  struct loop *aloop;
677  int i;
678
679  for (i = 0; i < n; i++)
680    {
681      aloop = duplicate_loop (loops, copied_loops[i], target);
682      duplicate_subloops (loops, copied_loops[i], aloop);
683    }
684}
685
686/* Redirects edge E to basic block DEST.  */
687static void
688loop_redirect_edge (edge e, basic_block dest)
689{
690  if (e->dest == dest)
691    return;
692
693  redirect_edge_and_branch_force (e, dest);
694}
695
696/* Deletes edge E from a branch if possible.  Unless REALLY_DELETE is set,
697   just test whether it is possible to remove the edge.  */
698static bool
699loop_delete_branch_edge (edge e, int really_delete)
700{
701  basic_block src = e->src;
702  basic_block newdest;
703  int irr;
704  edge snd;
705
706  gcc_assert (EDGE_COUNT (src->succs) > 1);
707
708  /* Cannot handle more than two exit edges.  */
709  if (EDGE_COUNT (src->succs) > 2)
710    return false;
711  /* And it must be just a simple branch.  */
712  if (!any_condjump_p (BB_END (src)))
713    return false;
714
715  snd = e == EDGE_SUCC (src, 0) ? EDGE_SUCC (src, 1) : EDGE_SUCC (src, 0);
716  newdest = snd->dest;
717  if (newdest == EXIT_BLOCK_PTR)
718    return false;
719
720  /* Hopefully the above conditions should suffice.  */
721  if (!really_delete)
722    return true;
723
724  /* Redirecting behaves wrongly wrto this flag.  */
725  irr = snd->flags & EDGE_IRREDUCIBLE_LOOP;
726
727  if (!redirect_edge_and_branch (e, newdest))
728    return false;
729  single_succ_edge (src)->flags &= ~EDGE_IRREDUCIBLE_LOOP;
730  single_succ_edge (src)->flags |= irr;
731
732  return true;
733}
734
735/* Check whether LOOP's body can be duplicated.  */
736bool
737can_duplicate_loop_p (struct loop *loop)
738{
739  int ret;
740  basic_block *bbs = get_loop_body (loop);
741
742  ret = can_copy_bbs_p (bbs, loop->num_nodes);
743  free (bbs);
744
745  return ret;
746}
747
748/* The NBBS blocks in BBS will get duplicated and the copies will be placed
749   to LOOP.  Update the single_exit information in superloops of LOOP.  */
750
751static void
752update_single_exits_after_duplication (basic_block *bbs, unsigned nbbs,
753				       struct loop *loop)
754{
755  unsigned i;
756
757  for (i = 0; i < nbbs; i++)
758    bbs[i]->flags |= BB_DUPLICATED;
759
760  for (; loop->outer; loop = loop->outer)
761    {
762      if (!loop->single_exit)
763	continue;
764
765      if (loop->single_exit->src->flags & BB_DUPLICATED)
766	loop->single_exit = NULL;
767    }
768
769  for (i = 0; i < nbbs; i++)
770    bbs[i]->flags &= ~BB_DUPLICATED;
771}
772
773/* Duplicates body of LOOP to given edge E NDUPL times.  Takes care of updating
774   LOOPS structure and dominators.  E's destination must be LOOP header for
775   this to work, i.e. it must be entry or latch edge of this loop; these are
776   unique, as the loops must have preheaders for this function to work
777   correctly (in case E is latch, the function unrolls the loop, if E is entry
778   edge, it peels the loop).  Store edges created by copying ORIG edge from
779   copies corresponding to set bits in WONT_EXIT bitmap (bit 0 corresponds to
780   original LOOP body, the other copies are numbered in order given by control
781   flow through them) into TO_REMOVE array.  Returns false if duplication is
782   impossible.  */
783bool
784duplicate_loop_to_header_edge (struct loop *loop, edge e, struct loops *loops,
785			       unsigned int ndupl, sbitmap wont_exit,
786			       edge orig, edge *to_remove,
787			       unsigned int *n_to_remove, int flags)
788{
789  struct loop *target, *aloop;
790  struct loop **orig_loops;
791  unsigned n_orig_loops;
792  basic_block header = loop->header, latch = loop->latch;
793  basic_block *new_bbs, *bbs, *first_active;
794  basic_block new_bb, bb, first_active_latch = NULL;
795  edge ae, latch_edge;
796  edge spec_edges[2], new_spec_edges[2];
797#define SE_LATCH 0
798#define SE_ORIG 1
799  unsigned i, j, n;
800  int is_latch = (latch == e->src);
801  int scale_act = 0, *scale_step = NULL, scale_main = 0;
802  int p, freq_in, freq_le, freq_out_orig;
803  int prob_pass_thru, prob_pass_wont_exit, prob_pass_main;
804  int add_irreducible_flag;
805  basic_block place_after;
806
807  gcc_assert (e->dest == loop->header);
808  gcc_assert (ndupl > 0);
809
810  if (orig)
811    {
812      /* Orig must be edge out of the loop.  */
813      gcc_assert (flow_bb_inside_loop_p (loop, orig->src));
814      gcc_assert (!flow_bb_inside_loop_p (loop, orig->dest));
815    }
816
817  n = loop->num_nodes;
818  bbs = get_loop_body_in_dom_order (loop);
819  gcc_assert (bbs[0] == loop->header);
820  gcc_assert (bbs[n  - 1] == loop->latch);
821
822  /* Check whether duplication is possible.  */
823  if (!can_copy_bbs_p (bbs, loop->num_nodes))
824    {
825      free (bbs);
826      return false;
827    }
828  new_bbs = XNEWVEC (basic_block, loop->num_nodes);
829
830  /* In case we are doing loop peeling and the loop is in the middle of
831     irreducible region, the peeled copies will be inside it too.  */
832  add_irreducible_flag = e->flags & EDGE_IRREDUCIBLE_LOOP;
833  gcc_assert (!is_latch || !add_irreducible_flag);
834
835  /* Find edge from latch.  */
836  latch_edge = loop_latch_edge (loop);
837
838  if (flags & DLTHE_FLAG_UPDATE_FREQ)
839    {
840      /* Calculate coefficients by that we have to scale frequencies
841	 of duplicated loop bodies.  */
842      freq_in = header->frequency;
843      freq_le = EDGE_FREQUENCY (latch_edge);
844      if (freq_in == 0)
845	freq_in = 1;
846      if (freq_in < freq_le)
847	freq_in = freq_le;
848      freq_out_orig = orig ? EDGE_FREQUENCY (orig) : freq_in - freq_le;
849      if (freq_out_orig > freq_in - freq_le)
850	freq_out_orig = freq_in - freq_le;
851      prob_pass_thru = RDIV (REG_BR_PROB_BASE * freq_le, freq_in);
852      prob_pass_wont_exit =
853	      RDIV (REG_BR_PROB_BASE * (freq_le + freq_out_orig), freq_in);
854
855      scale_step = XNEWVEC (int, ndupl);
856
857	for (i = 1; i <= ndupl; i++)
858	  scale_step[i - 1] = TEST_BIT (wont_exit, i)
859				? prob_pass_wont_exit
860				: prob_pass_thru;
861
862      /* Complete peeling is special as the probability of exit in last
863	 copy becomes 1.  */
864      if (flags & DLTHE_FLAG_COMPLETTE_PEEL)
865	{
866	  int wanted_freq = EDGE_FREQUENCY (e);
867
868	  if (wanted_freq > freq_in)
869	    wanted_freq = freq_in;
870
871	  gcc_assert (!is_latch);
872	  /* First copy has frequency of incoming edge.  Each subsequent
873	     frequency should be reduced by prob_pass_wont_exit.  Caller
874	     should've managed the flags so all except for original loop
875	     has won't exist set.  */
876	  scale_act = RDIV (wanted_freq * REG_BR_PROB_BASE, freq_in);
877	  /* Now simulate the duplication adjustments and compute header
878	     frequency of the last copy.  */
879	  for (i = 0; i < ndupl; i++)
880	    wanted_freq = RDIV (wanted_freq * scale_step[i], REG_BR_PROB_BASE);
881	  scale_main = RDIV (wanted_freq * REG_BR_PROB_BASE, freq_in);
882	}
883      else if (is_latch)
884	{
885	  prob_pass_main = TEST_BIT (wont_exit, 0)
886				? prob_pass_wont_exit
887				: prob_pass_thru;
888	  p = prob_pass_main;
889	  scale_main = REG_BR_PROB_BASE;
890	  for (i = 0; i < ndupl; i++)
891	    {
892	      scale_main += p;
893	      p = RDIV (p * scale_step[i], REG_BR_PROB_BASE);
894	    }
895	  scale_main = RDIV (REG_BR_PROB_BASE * REG_BR_PROB_BASE, scale_main);
896	  scale_act = RDIV (scale_main * prob_pass_main, REG_BR_PROB_BASE);
897	}
898      else
899	{
900	  scale_main = REG_BR_PROB_BASE;
901	  for (i = 0; i < ndupl; i++)
902	    scale_main = RDIV (scale_main * scale_step[i], REG_BR_PROB_BASE);
903	  scale_act = REG_BR_PROB_BASE - prob_pass_thru;
904	}
905      for (i = 0; i < ndupl; i++)
906	gcc_assert (scale_step[i] >= 0 && scale_step[i] <= REG_BR_PROB_BASE);
907      gcc_assert (scale_main >= 0 && scale_main <= REG_BR_PROB_BASE
908		  && scale_act >= 0  && scale_act <= REG_BR_PROB_BASE);
909    }
910
911  /* Loop the new bbs will belong to.  */
912  target = e->src->loop_father;
913
914  /* Original loops.  */
915  n_orig_loops = 0;
916  for (aloop = loop->inner; aloop; aloop = aloop->next)
917    n_orig_loops++;
918  orig_loops = XCNEWVEC (struct loop *, n_orig_loops);
919  for (aloop = loop->inner, i = 0; aloop; aloop = aloop->next, i++)
920    orig_loops[i] = aloop;
921
922  loop->copy = target;
923
924  first_active = XNEWVEC (basic_block, n);
925  if (is_latch)
926    {
927      memcpy (first_active, bbs, n * sizeof (basic_block));
928      first_active_latch = latch;
929    }
930
931  /* Update the information about single exits.  */
932  if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
933    update_single_exits_after_duplication (bbs, n, target);
934
935  /* Record exit edge in original loop body.  */
936  if (orig && TEST_BIT (wont_exit, 0))
937    to_remove[(*n_to_remove)++] = orig;
938
939  spec_edges[SE_ORIG] = orig;
940  spec_edges[SE_LATCH] = latch_edge;
941
942  place_after = e->src;
943  for (j = 0; j < ndupl; j++)
944    {
945      /* Copy loops.  */
946      copy_loops_to (loops, orig_loops, n_orig_loops, target);
947
948      /* Copy bbs.  */
949      copy_bbs (bbs, n, new_bbs, spec_edges, 2, new_spec_edges, loop,
950		place_after);
951      place_after = new_spec_edges[SE_LATCH]->src;
952
953      if (flags & DLTHE_RECORD_COPY_NUMBER)
954	for (i = 0; i < n; i++)
955	  {
956	    gcc_assert (!new_bbs[i]->aux);
957	    new_bbs[i]->aux = (void *)(size_t)(j + 1);
958	  }
959
960      /* Note whether the blocks and edges belong to an irreducible loop.  */
961      if (add_irreducible_flag)
962	{
963	  for (i = 0; i < n; i++)
964	    new_bbs[i]->flags |= BB_DUPLICATED;
965	  for (i = 0; i < n; i++)
966	    {
967	      edge_iterator ei;
968	      new_bb = new_bbs[i];
969	      if (new_bb->loop_father == target)
970		new_bb->flags |= BB_IRREDUCIBLE_LOOP;
971
972	      FOR_EACH_EDGE (ae, ei, new_bb->succs)
973		if ((ae->dest->flags & BB_DUPLICATED)
974		    && (ae->src->loop_father == target
975			|| ae->dest->loop_father == target))
976		  ae->flags |= EDGE_IRREDUCIBLE_LOOP;
977	    }
978	  for (i = 0; i < n; i++)
979	    new_bbs[i]->flags &= ~BB_DUPLICATED;
980	}
981
982      /* Redirect the special edges.  */
983      if (is_latch)
984	{
985	  redirect_edge_and_branch_force (latch_edge, new_bbs[0]);
986	  redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
987					  loop->header);
988	  set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], latch);
989	  latch = loop->latch = new_bbs[n - 1];
990	  e = latch_edge = new_spec_edges[SE_LATCH];
991	}
992      else
993	{
994	  redirect_edge_and_branch_force (new_spec_edges[SE_LATCH],
995					  loop->header);
996	  redirect_edge_and_branch_force (e, new_bbs[0]);
997	  set_immediate_dominator (CDI_DOMINATORS, new_bbs[0], e->src);
998	  e = new_spec_edges[SE_LATCH];
999	}
1000
1001      /* Record exit edge in this copy.  */
1002      if (orig && TEST_BIT (wont_exit, j + 1))
1003	to_remove[(*n_to_remove)++] = new_spec_edges[SE_ORIG];
1004
1005      /* Record the first copy in the control flow order if it is not
1006	 the original loop (i.e. in case of peeling).  */
1007      if (!first_active_latch)
1008	{
1009	  memcpy (first_active, new_bbs, n * sizeof (basic_block));
1010	  first_active_latch = new_bbs[n - 1];
1011	}
1012
1013      /* Set counts and frequencies.  */
1014      if (flags & DLTHE_FLAG_UPDATE_FREQ)
1015	{
1016	  scale_bbs_frequencies_int (new_bbs, n, scale_act, REG_BR_PROB_BASE);
1017	  scale_act = RDIV (scale_act * scale_step[j], REG_BR_PROB_BASE);
1018	}
1019    }
1020  free (new_bbs);
1021  free (orig_loops);
1022
1023  /* Update the original loop.  */
1024  if (!is_latch)
1025    set_immediate_dominator (CDI_DOMINATORS, e->dest, e->src);
1026  if (flags & DLTHE_FLAG_UPDATE_FREQ)
1027    {
1028      scale_bbs_frequencies_int (bbs, n, scale_main, REG_BR_PROB_BASE);
1029      free (scale_step);
1030    }
1031
1032  /* Update dominators of outer blocks if affected.  */
1033  for (i = 0; i < n; i++)
1034    {
1035      basic_block dominated, dom_bb, *dom_bbs;
1036      int n_dom_bbs,j;
1037
1038      bb = bbs[i];
1039      bb->aux = 0;
1040
1041      n_dom_bbs = get_dominated_by (CDI_DOMINATORS, bb, &dom_bbs);
1042      for (j = 0; j < n_dom_bbs; j++)
1043	{
1044	  dominated = dom_bbs[j];
1045	  if (flow_bb_inside_loop_p (loop, dominated))
1046	    continue;
1047	  dom_bb = nearest_common_dominator (
1048			CDI_DOMINATORS, first_active[i], first_active_latch);
1049	  set_immediate_dominator (CDI_DOMINATORS, dominated, dom_bb);
1050	}
1051      free (dom_bbs);
1052    }
1053  free (first_active);
1054
1055  free (bbs);
1056
1057  return true;
1058}
1059
1060/* A callback for make_forwarder block, to redirect all edges except for
1061   MFB_KJ_EDGE to the entry part.  E is the edge for that we should decide
1062   whether to redirect it.  */
1063
1064static edge mfb_kj_edge;
1065static bool
1066mfb_keep_just (edge e)
1067{
1068  return e != mfb_kj_edge;
1069}
1070
1071/* A callback for make_forwarder block, to update data structures for a basic
1072   block JUMP created by redirecting an edge (only the latch edge is being
1073   redirected).  */
1074
1075static void
1076mfb_update_loops (basic_block jump)
1077{
1078  struct loop *loop = single_succ (jump)->loop_father;
1079
1080  if (dom_computed[CDI_DOMINATORS])
1081    set_immediate_dominator (CDI_DOMINATORS, jump, single_pred (jump));
1082  add_bb_to_loop (jump, loop);
1083  loop->latch = jump;
1084}
1085
1086/* Creates a pre-header for a LOOP.  Returns newly created block.  Unless
1087   CP_SIMPLE_PREHEADERS is set in FLAGS, we only force LOOP to have single
1088   entry; otherwise we also force preheader block to have only one successor.
1089   The function also updates dominators.  */
1090
1091static basic_block
1092create_preheader (struct loop *loop, int flags)
1093{
1094  edge e, fallthru;
1095  basic_block dummy;
1096  struct loop *cloop, *ploop;
1097  int nentry = 0;
1098  bool irred = false;
1099  bool latch_edge_was_fallthru;
1100  edge one_succ_pred = 0;
1101  edge_iterator ei;
1102
1103  cloop = loop->outer;
1104
1105  FOR_EACH_EDGE (e, ei, loop->header->preds)
1106    {
1107      if (e->src == loop->latch)
1108	continue;
1109      irred |= (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
1110      nentry++;
1111      if (single_succ_p (e->src))
1112	one_succ_pred = e;
1113    }
1114  gcc_assert (nentry);
1115  if (nentry == 1)
1116    {
1117      /* Get an edge that is different from the one from loop->latch
1118	 to loop->header.  */
1119      e = EDGE_PRED (loop->header,
1120		     EDGE_PRED (loop->header, 0)->src == loop->latch);
1121
1122      if (!(flags & CP_SIMPLE_PREHEADERS) || single_succ_p (e->src))
1123	return NULL;
1124    }
1125
1126  mfb_kj_edge = loop_latch_edge (loop);
1127  latch_edge_was_fallthru = (mfb_kj_edge->flags & EDGE_FALLTHRU) != 0;
1128  fallthru = make_forwarder_block (loop->header, mfb_keep_just,
1129				   mfb_update_loops);
1130  dummy = fallthru->src;
1131  loop->header = fallthru->dest;
1132
1133  /* The header could be a latch of some superloop(s); due to design of
1134     split_block, it would now move to fallthru->dest.  */
1135  for (ploop = loop; ploop; ploop = ploop->outer)
1136    if (ploop->latch == dummy)
1137      ploop->latch = fallthru->dest;
1138
1139  /* Try to be clever in placing the newly created preheader.  The idea is to
1140     avoid breaking any "fallthruness" relationship between blocks.
1141
1142     The preheader was created just before the header and all incoming edges
1143     to the header were redirected to the preheader, except the latch edge.
1144     So the only problematic case is when this latch edge was a fallthru
1145     edge: it is not anymore after the preheader creation so we have broken
1146     the fallthruness.  We're therefore going to look for a better place.  */
1147  if (latch_edge_was_fallthru)
1148    {
1149      if (one_succ_pred)
1150	e = one_succ_pred;
1151      else
1152	e = EDGE_PRED (dummy, 0);
1153
1154      move_block_after (dummy, e->src);
1155    }
1156
1157  loop->header->loop_father = loop;
1158  add_bb_to_loop (dummy, cloop);
1159
1160  if (irred)
1161    {
1162      dummy->flags |= BB_IRREDUCIBLE_LOOP;
1163      single_succ_edge (dummy)->flags |= EDGE_IRREDUCIBLE_LOOP;
1164    }
1165
1166  if (dump_file)
1167    fprintf (dump_file, "Created preheader block for loop %i\n",
1168	     loop->num);
1169
1170  return dummy;
1171}
1172
1173/* Create preheaders for each loop from loop tree stored in LOOPS; for meaning
1174   of FLAGS see create_preheader.  */
1175void
1176create_preheaders (struct loops *loops, int flags)
1177{
1178  unsigned i;
1179  for (i = 1; i < loops->num; i++)
1180    create_preheader (loops->parray[i], flags);
1181  loops->state |= LOOPS_HAVE_PREHEADERS;
1182}
1183
1184/* Forces all loop latches of loops from loop tree LOOPS to have only single
1185   successor.  */
1186void
1187force_single_succ_latches (struct loops *loops)
1188{
1189  unsigned i;
1190  struct loop *loop;
1191  edge e;
1192
1193  for (i = 1; i < loops->num; i++)
1194    {
1195      loop = loops->parray[i];
1196      if (loop->latch != loop->header && single_succ_p (loop->latch))
1197	continue;
1198
1199      e = find_edge (loop->latch, loop->header);
1200
1201      loop_split_edge_with (e, NULL_RTX);
1202    }
1203  loops->state |= LOOPS_HAVE_SIMPLE_LATCHES;
1204}
1205
1206/* A quite stupid function to put INSNS on edge E. They are supposed to form
1207   just one basic block.  Jumps in INSNS are not handled, so cfg do not have to
1208   be ok after this function.  The created block is placed on correct place
1209   in LOOPS structure and its dominator is set.  */
1210basic_block
1211loop_split_edge_with (edge e, rtx insns)
1212{
1213  basic_block src, dest, new_bb;
1214  struct loop *loop_c;
1215
1216  src = e->src;
1217  dest = e->dest;
1218
1219  loop_c = find_common_loop (src->loop_father, dest->loop_father);
1220
1221  /* Create basic block for it.  */
1222
1223  new_bb = split_edge (e);
1224  add_bb_to_loop (new_bb, loop_c);
1225  new_bb->flags |= (insns ? BB_SUPERBLOCK : 0);
1226
1227  if (insns)
1228    emit_insn_after (insns, BB_END (new_bb));
1229
1230  if (dest->loop_father->latch == src)
1231    dest->loop_father->latch = new_bb;
1232
1233  return new_bb;
1234}
1235
1236/* This function is called from loop_version.  It splits the entry edge
1237   of the loop we want to version, adds the versioning condition, and
1238   adjust the edges to the two versions of the loop appropriately.
1239   e is an incoming edge. Returns the basic block containing the
1240   condition.
1241
1242   --- edge e ---- > [second_head]
1243
1244   Split it and insert new conditional expression and adjust edges.
1245
1246    --- edge e ---> [cond expr] ---> [first_head]
1247			|
1248			+---------> [second_head]
1249*/
1250
1251static basic_block
1252lv_adjust_loop_entry_edge (basic_block first_head,
1253			   basic_block second_head,
1254			   edge e,
1255			   void *cond_expr)
1256{
1257  basic_block new_head = NULL;
1258  edge e1;
1259
1260  gcc_assert (e->dest == second_head);
1261
1262  /* Split edge 'e'. This will create a new basic block, where we can
1263     insert conditional expr.  */
1264  new_head = split_edge (e);
1265
1266
1267  lv_add_condition_to_bb (first_head, second_head, new_head,
1268			  cond_expr);
1269
1270  /* Don't set EDGE_TRUE_VALUE in RTL mode, as it's invalid there.  */
1271  e1 = make_edge (new_head, first_head, ir_type () ? EDGE_TRUE_VALUE : 0);
1272  set_immediate_dominator (CDI_DOMINATORS, first_head, new_head);
1273  set_immediate_dominator (CDI_DOMINATORS, second_head, new_head);
1274
1275  /* Adjust loop header phi nodes.  */
1276  lv_adjust_loop_header_phi (first_head, second_head, new_head, e1);
1277
1278  return new_head;
1279}
1280
1281/* Main entry point for Loop Versioning transformation.
1282
1283   This transformation given a condition and a loop, creates
1284   -if (condition) { loop_copy1 } else { loop_copy2 },
1285   where loop_copy1 is the loop transformed in one way, and loop_copy2
1286   is the loop transformed in another way (or unchanged). 'condition'
1287   may be a run time test for things that were not resolved by static
1288   analysis (overlapping ranges (anti-aliasing), alignment, etc.).
1289
1290   If PLACE_AFTER is true, we place the new loop after LOOP in the
1291   instruction stream, otherwise it is placed before LOOP.  */
1292
1293struct loop *
1294loop_version (struct loops *loops, struct loop * loop,
1295	      void *cond_expr, basic_block *condition_bb,
1296	      bool place_after)
1297{
1298  basic_block first_head, second_head;
1299  edge entry, latch_edge, exit, true_edge, false_edge;
1300  int irred_flag;
1301  struct loop *nloop;
1302  basic_block cond_bb;
1303
1304  /* CHECKME: Loop versioning does not handle nested loop at this point.  */
1305  if (loop->inner)
1306    return NULL;
1307
1308  /* Record entry and latch edges for the loop */
1309  entry = loop_preheader_edge (loop);
1310  irred_flag = entry->flags & EDGE_IRREDUCIBLE_LOOP;
1311  entry->flags &= ~EDGE_IRREDUCIBLE_LOOP;
1312
1313  /* Note down head of loop as first_head.  */
1314  first_head = entry->dest;
1315
1316  /* Duplicate loop.  */
1317  if (!cfg_hook_duplicate_loop_to_header_edge (loop, entry, loops, 1,
1318					       NULL, NULL, NULL, NULL, 0))
1319    return NULL;
1320
1321  /* After duplication entry edge now points to new loop head block.
1322     Note down new head as second_head.  */
1323  second_head = entry->dest;
1324
1325  /* Split loop entry edge and insert new block with cond expr.  */
1326  cond_bb =  lv_adjust_loop_entry_edge (first_head, second_head,
1327					entry, cond_expr);
1328  if (condition_bb)
1329    *condition_bb = cond_bb;
1330
1331  if (!cond_bb)
1332    {
1333      entry->flags |= irred_flag;
1334      return NULL;
1335    }
1336
1337  latch_edge = single_succ_edge (get_bb_copy (loop->latch));
1338
1339  extract_cond_bb_edges (cond_bb, &true_edge, &false_edge);
1340  nloop = loopify (loops,
1341		   latch_edge,
1342		   single_pred_edge (get_bb_copy (loop->header)),
1343		   cond_bb, true_edge, false_edge,
1344		   false /* Do not redirect all edges.  */);
1345
1346  exit = loop->single_exit;
1347  if (exit)
1348    nloop->single_exit = find_edge (get_bb_copy (exit->src), exit->dest);
1349
1350  /* loopify redirected latch_edge. Update its PENDING_STMTS.  */
1351  lv_flush_pending_stmts (latch_edge);
1352
1353  /* loopify redirected condition_bb's succ edge. Update its PENDING_STMTS.  */
1354  extract_cond_bb_edges (cond_bb, &true_edge, &false_edge);
1355  lv_flush_pending_stmts (false_edge);
1356  /* Adjust irreducible flag.  */
1357  if (irred_flag)
1358    {
1359      cond_bb->flags |= BB_IRREDUCIBLE_LOOP;
1360      loop_preheader_edge (loop)->flags |= EDGE_IRREDUCIBLE_LOOP;
1361      loop_preheader_edge (nloop)->flags |= EDGE_IRREDUCIBLE_LOOP;
1362      single_pred_edge (cond_bb)->flags |= EDGE_IRREDUCIBLE_LOOP;
1363    }
1364
1365  if (place_after)
1366    {
1367      basic_block *bbs = get_loop_body_in_dom_order (nloop), after;
1368      unsigned i;
1369
1370      after = loop->latch;
1371
1372      for (i = 0; i < nloop->num_nodes; i++)
1373	{
1374	  move_block_after (bbs[i], after);
1375	  after = bbs[i];
1376	}
1377      free (bbs);
1378    }
1379
1380  /* At this point condition_bb is loop predheader with two successors,
1381     first_head and second_head.   Make sure that loop predheader has only
1382     one successor.  */
1383  loop_split_edge_with (loop_preheader_edge (loop), NULL);
1384  loop_split_edge_with (loop_preheader_edge (nloop), NULL);
1385
1386  return nloop;
1387}
1388
1389/* The structure of LOOPS might have changed.  Some loops might get removed
1390   (and their headers and latches were set to NULL), loop exists might get
1391   removed (thus the loop nesting may be wrong), and some blocks and edges
1392   were changed (so the information about bb --> loop mapping does not have
1393   to be correct).  But still for the remaining loops the header dominates
1394   the latch, and loops did not get new subloobs (new loops might possibly
1395   get created, but we are not interested in them).  Fix up the mess.
1396
1397   If CHANGED_BBS is not NULL, basic blocks whose loop has changed are
1398   marked in it.  */
1399
1400void
1401fix_loop_structure (struct loops *loops, bitmap changed_bbs)
1402{
1403  basic_block bb;
1404  struct loop *loop, *ploop;
1405  unsigned i;
1406
1407  /* Remove the old bb -> loop mapping.  */
1408  FOR_EACH_BB (bb)
1409    {
1410      bb->aux = (void *) (size_t) bb->loop_father->depth;
1411      bb->loop_father = loops->tree_root;
1412    }
1413
1414  /* Remove the dead loops from structures.  */
1415  loops->tree_root->num_nodes = n_basic_blocks;
1416  for (i = 1; i < loops->num; i++)
1417    {
1418      loop = loops->parray[i];
1419      if (!loop)
1420	continue;
1421
1422      loop->num_nodes = 0;
1423      if (loop->header)
1424	continue;
1425
1426      while (loop->inner)
1427	{
1428	  ploop = loop->inner;
1429	  flow_loop_tree_node_remove (ploop);
1430	  flow_loop_tree_node_add (loop->outer, ploop);
1431	}
1432
1433      /* Remove the loop and free its data.  */
1434      flow_loop_tree_node_remove (loop);
1435      loops->parray[loop->num] = NULL;
1436      flow_loop_free (loop);
1437    }
1438
1439  /* Rescan the bodies of loops, starting from the outermost.  */
1440  loop = loops->tree_root;
1441  while (1)
1442    {
1443      if (loop->inner)
1444	loop = loop->inner;
1445      else
1446	{
1447	  while (!loop->next
1448		 && loop != loops->tree_root)
1449	    loop = loop->outer;
1450	  if (loop == loops->tree_root)
1451	    break;
1452
1453	  loop = loop->next;
1454	}
1455
1456      loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
1457    }
1458
1459  /* Now fix the loop nesting.  */
1460  for (i = 1; i < loops->num; i++)
1461    {
1462      loop = loops->parray[i];
1463      if (!loop)
1464	continue;
1465
1466      bb = loop_preheader_edge (loop)->src;
1467      if (bb->loop_father != loop->outer)
1468	{
1469	  flow_loop_tree_node_remove (loop);
1470	  flow_loop_tree_node_add (bb->loop_father, loop);
1471	}
1472    }
1473
1474  /* Mark the blocks whose loop has changed.  */
1475  FOR_EACH_BB (bb)
1476    {
1477      if (changed_bbs
1478	  && (void *) (size_t) bb->loop_father->depth != bb->aux)
1479	bitmap_set_bit (changed_bbs, bb->index);
1480
1481      bb->aux = NULL;
1482    }
1483
1484  if (loops->state & LOOPS_HAVE_MARKED_SINGLE_EXITS)
1485    mark_single_exit_loops (loops);
1486  if (loops->state & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
1487    mark_irreducible_loops (loops);
1488}
1489