Home | History | Annotate | Line # | Download | only in gcc
      1  1.1  mrg /* Code to analyze doloop loops in order for targets to perform late
      2  1.1  mrg    optimizations converting doloops to other forms of hardware loops.
      3  1.1  mrg    Copyright (C) 2011-2022 Free Software Foundation, Inc.
      4  1.1  mrg 
      5  1.1  mrg This file is part of GCC.
      6  1.1  mrg 
      7  1.1  mrg GCC is free software; you can redistribute it and/or modify it under
      8  1.1  mrg the terms of the GNU General Public License as published by the Free
      9  1.1  mrg Software Foundation; either version 3, or (at your option) any later
     10  1.1  mrg version.
     11  1.1  mrg 
     12  1.1  mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     13  1.1  mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  1.1  mrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  1.1  mrg for more details.
     16  1.1  mrg 
     17  1.1  mrg You should have received a copy of the GNU General Public License
     18  1.1  mrg along with GCC; see the file COPYING3.  If not see
     19  1.1  mrg <http://www.gnu.org/licenses/>.  */
     20  1.1  mrg 
     21  1.1  mrg #include "config.h"
     22  1.1  mrg #include "system.h"
     23  1.1  mrg #include "coretypes.h"
     24  1.1  mrg #include "backend.h"
     25  1.1  mrg #include "rtl.h"
     26  1.1  mrg #include "df.h"
     27  1.1  mrg #include "insn-config.h"
     28  1.1  mrg #include "regs.h"
     29  1.1  mrg #include "memmodel.h"
     30  1.1  mrg #include "emit-rtl.h"
     31  1.1  mrg #include "recog.h"
     32  1.1  mrg #include "cfgrtl.h"
     33  1.1  mrg #include "hw-doloop.h"
     34  1.1  mrg #include "dumpfile.h"
     35  1.1  mrg 
     36  1.1  mrg /* Dump information collected in LOOPS.  */
     37  1.1  mrg static void
     38  1.1  mrg dump_hwloops (hwloop_info loops)
     39  1.1  mrg {
     40  1.1  mrg   hwloop_info loop;
     41  1.1  mrg 
     42  1.1  mrg   for (loop = loops; loop; loop = loop->next)
     43  1.1  mrg     {
     44  1.1  mrg       hwloop_info i;
     45  1.1  mrg       basic_block b;
     46  1.1  mrg       unsigned ix;
     47  1.1  mrg 
     48  1.1  mrg       fprintf (dump_file, ";; loop %d: ", loop->loop_no);
     49  1.1  mrg       if (loop->bad)
     50  1.1  mrg 	fprintf (dump_file, "(bad) ");
     51  1.1  mrg       fprintf (dump_file, "{head:%d, depth:%d, reg:%u}",
     52  1.1  mrg 	       loop->head == NULL ? -1 : loop->head->index,
     53  1.1  mrg 	       loop->depth, REGNO (loop->iter_reg));
     54  1.1  mrg 
     55  1.1  mrg       fprintf (dump_file, " blocks: [ ");
     56  1.1  mrg       for (ix = 0; loop->blocks.iterate (ix, &b); ix++)
     57  1.1  mrg 	fprintf (dump_file, "%d ", b->index);
     58  1.1  mrg       fprintf (dump_file, "] ");
     59  1.1  mrg 
     60  1.1  mrg       fprintf (dump_file, " inner loops: [ ");
     61  1.1  mrg       for (ix = 0; loop->loops.iterate (ix, &i); ix++)
     62  1.1  mrg 	fprintf (dump_file, "%d ", i->loop_no);
     63  1.1  mrg       fprintf (dump_file, "]\n");
     64  1.1  mrg     }
     65  1.1  mrg   fprintf (dump_file, "\n");
     66  1.1  mrg }
     67  1.1  mrg 
     68  1.1  mrg /* Return true if BB is part of LOOP.  */
     69  1.1  mrg static bool
     70  1.1  mrg bb_in_loop_p (hwloop_info loop, basic_block bb)
     71  1.1  mrg {
     72  1.1  mrg   return bitmap_bit_p (loop->block_bitmap, bb->index);
     73  1.1  mrg }
     74  1.1  mrg 
     75  1.1  mrg /* Scan the blocks of LOOP (and its inferiors), and record things such as
     76  1.1  mrg    hard registers set, jumps out of and within the loop.  */
     77  1.1  mrg static void
     78  1.1  mrg scan_loop (hwloop_info loop)
     79  1.1  mrg {
     80  1.1  mrg   unsigned ix;
     81  1.1  mrg   basic_block bb;
     82  1.1  mrg 
     83  1.1  mrg   if (loop->bad)
     84  1.1  mrg     return;
     85  1.1  mrg 
     86  1.1  mrg   if (REGNO_REG_SET_P (df_get_live_in (loop->successor),
     87  1.1  mrg 		       REGNO (loop->iter_reg)))
     88  1.1  mrg     loop->iter_reg_used_outside = true;
     89  1.1  mrg 
     90  1.1  mrg   for (ix = 0; loop->blocks.iterate (ix, &bb); ix++)
     91  1.1  mrg     {
     92  1.1  mrg       rtx_insn *insn;
     93  1.1  mrg       edge e;
     94  1.1  mrg       edge_iterator ei;
     95  1.1  mrg 
     96  1.1  mrg       if (bb != loop->tail)
     97  1.1  mrg 	FOR_EACH_EDGE (e, ei, bb->succs)
     98  1.1  mrg 	  {
     99  1.1  mrg 	    if (bb_in_loop_p (loop, e->dest))
    100  1.1  mrg 	      {
    101  1.1  mrg 		if (!(e->flags & EDGE_FALLTHRU))
    102  1.1  mrg 		  loop->jumps_within = true;
    103  1.1  mrg 	      }
    104  1.1  mrg 	    else
    105  1.1  mrg 	      {
    106  1.1  mrg 		loop->jumps_outof = true;
    107  1.1  mrg 		if (!loop->bad)
    108  1.1  mrg 		  gcc_assert (!REGNO_REG_SET_P (df_get_live_in (e->dest),
    109  1.1  mrg 						REGNO (loop->iter_reg)));
    110  1.1  mrg 	      }
    111  1.1  mrg 	  }
    112  1.1  mrg 
    113  1.1  mrg       for (insn = BB_HEAD (bb);
    114  1.1  mrg 	   insn != NEXT_INSN (BB_END (bb));
    115  1.1  mrg 	   insn = NEXT_INSN (insn))
    116  1.1  mrg 	{
    117  1.1  mrg 	  df_ref def;
    118  1.1  mrg 	  HARD_REG_SET set_this_insn;
    119  1.1  mrg 
    120  1.1  mrg 	  if (!NONDEBUG_INSN_P (insn))
    121  1.1  mrg 	    continue;
    122  1.1  mrg 
    123  1.1  mrg 	  if (recog_memoized (insn) < 0
    124  1.1  mrg 	      && (GET_CODE (PATTERN (insn)) == ASM_INPUT
    125  1.1  mrg 		  || asm_noperands (PATTERN (insn)) >= 0))
    126  1.1  mrg 	    loop->has_asm = true;
    127  1.1  mrg 
    128  1.1  mrg 	  CLEAR_HARD_REG_SET (set_this_insn);
    129  1.1  mrg 	  FOR_EACH_INSN_DEF (def, insn)
    130  1.1  mrg 	    {
    131  1.1  mrg 	      rtx dreg = DF_REF_REG (def);
    132  1.1  mrg 
    133  1.1  mrg 	      if (!REG_P (dreg))
    134  1.1  mrg 		continue;
    135  1.1  mrg 
    136  1.1  mrg 	      add_to_hard_reg_set (&set_this_insn, GET_MODE (dreg),
    137  1.1  mrg 				   REGNO (dreg));
    138  1.1  mrg 	    }
    139  1.1  mrg 
    140  1.1  mrg 	  if (insn == loop->loop_end)
    141  1.1  mrg 	    CLEAR_HARD_REG_BIT (set_this_insn, REGNO (loop->iter_reg));
    142  1.1  mrg 	  else if (reg_mentioned_p (loop->iter_reg, PATTERN (insn)))
    143  1.1  mrg 	    loop->iter_reg_used = true;
    144  1.1  mrg 	  loop->regs_set_in_loop |= set_this_insn;
    145  1.1  mrg 	}
    146  1.1  mrg     }
    147  1.1  mrg }
    148  1.1  mrg 
    149  1.1  mrg /* Compute the incoming_dest and incoming_src members of LOOP by
    150  1.1  mrg    identifying the edges that jump into the loop.  If there is more
    151  1.1  mrg    than one block that jumps into the loop, incoming_src will be set
    152  1.1  mrg    to NULL; likewise, if there is more than one block in the loop that
    153  1.1  mrg    is the destination of an incoming edge, incoming_dest will be NULL.
    154  1.1  mrg 
    155  1.1  mrg    Return true if either of these two fields is nonnull, false
    156  1.1  mrg    otherwise.  */
    157  1.1  mrg static bool
    158  1.1  mrg process_incoming_edges (hwloop_info loop)
    159  1.1  mrg {
    160  1.1  mrg   edge e;
    161  1.1  mrg   edge_iterator ei;
    162  1.1  mrg   bool first = true;
    163  1.1  mrg 
    164  1.1  mrg   FOR_EACH_EDGE (e, ei, loop->incoming)
    165  1.1  mrg     {
    166  1.1  mrg       if (first)
    167  1.1  mrg 	{
    168  1.1  mrg 	  loop->incoming_src = e->src;
    169  1.1  mrg 	  loop->incoming_dest = e->dest;
    170  1.1  mrg 	  first = false;
    171  1.1  mrg 	}
    172  1.1  mrg       else
    173  1.1  mrg 	{
    174  1.1  mrg 	  if (e->dest != loop->incoming_dest)
    175  1.1  mrg 	    loop->incoming_dest = NULL;
    176  1.1  mrg 	  if (e->src != loop->incoming_src)
    177  1.1  mrg 	    loop->incoming_src = NULL;
    178  1.1  mrg 	}
    179  1.1  mrg     }
    180  1.1  mrg   if (loop->incoming_src == NULL && loop->incoming_dest == NULL)
    181  1.1  mrg     return false;
    182  1.1  mrg 
    183  1.1  mrg   return true;
    184  1.1  mrg }
    185  1.1  mrg 
    186  1.1  mrg /* Try to identify a forwarder block that jump into LOOP, and add it to
    187  1.1  mrg    the set of blocks in the loop, updating the vector of incoming blocks as
    188  1.1  mrg    well.  This transformation gives a second chance to loops we couldn't
    189  1.1  mrg    otherwise handle by increasing the chance that we'll end up with one
    190  1.1  mrg    incoming_src block.
    191  1.1  mrg    Return true if we made a change, false otherwise.  */
    192  1.1  mrg static bool
    193  1.1  mrg add_forwarder_blocks (hwloop_info loop)
    194  1.1  mrg {
    195  1.1  mrg   edge e;
    196  1.1  mrg   edge_iterator ei;
    197  1.1  mrg 
    198  1.1  mrg   FOR_EACH_EDGE (e, ei, loop->incoming)
    199  1.1  mrg     {
    200  1.1  mrg       if (forwarder_block_p (e->src))
    201  1.1  mrg 	{
    202  1.1  mrg 	  edge e2;
    203  1.1  mrg 	  edge_iterator ei2;
    204  1.1  mrg 
    205  1.1  mrg 	  if (dump_file)
    206  1.1  mrg 	    fprintf (dump_file,
    207  1.1  mrg 		     ";; Adding forwarder block %d to loop %d and retrying\n",
    208  1.1  mrg 		     e->src->index, loop->loop_no);
    209  1.1  mrg 	  loop->blocks.safe_push (e->src);
    210  1.1  mrg 	  bitmap_set_bit (loop->block_bitmap, e->src->index);
    211  1.1  mrg 	  FOR_EACH_EDGE (e2, ei2, e->src->preds)
    212  1.1  mrg 	    vec_safe_push (loop->incoming, e2);
    213  1.1  mrg 	  loop->incoming->unordered_remove (ei.index);
    214  1.1  mrg 	  return true;
    215  1.1  mrg 	}
    216  1.1  mrg     }
    217  1.1  mrg   return false;
    218  1.1  mrg }
    219  1.1  mrg 
    220  1.1  mrg /* Called from reorg_loops when a potential loop end is found.  LOOP is
    221  1.1  mrg    a newly set up structure describing the loop, it is this function's
    222  1.1  mrg    responsibility to fill most of it.  TAIL_BB and TAIL_INSN point to the
    223  1.1  mrg    loop_end insn and its enclosing basic block.  REG is the loop counter
    224  1.1  mrg    register.
    225  1.1  mrg    For our purposes, a loop is defined by the set of blocks reachable from
    226  1.1  mrg    the loop head in which the loop counter register is live.  This matches
    227  1.1  mrg    the expected use; targets that call into this code usually replace the
    228  1.1  mrg    loop counter with a different special register.  */
    229  1.1  mrg static void
    230  1.1  mrg discover_loop (hwloop_info loop, basic_block tail_bb, rtx_insn *tail_insn, rtx reg)
    231  1.1  mrg {
    232  1.1  mrg   bool found_tail;
    233  1.1  mrg   unsigned dwork = 0;
    234  1.1  mrg   basic_block bb;
    235  1.1  mrg 
    236  1.1  mrg   loop->tail = tail_bb;
    237  1.1  mrg   loop->loop_end = tail_insn;
    238  1.1  mrg   loop->iter_reg = reg;
    239  1.1  mrg   vec_alloc (loop->incoming, 2);
    240  1.1  mrg   loop->start_label = as_a <rtx_insn *> (JUMP_LABEL (tail_insn));
    241  1.1  mrg 
    242  1.1  mrg   if (EDGE_COUNT (tail_bb->succs) != 2)
    243  1.1  mrg     {
    244  1.1  mrg       loop->bad = true;
    245  1.1  mrg       return;
    246  1.1  mrg     }
    247  1.1  mrg   loop->head = BRANCH_EDGE (tail_bb)->dest;
    248  1.1  mrg   loop->successor = FALLTHRU_EDGE (tail_bb)->dest;
    249  1.1  mrg 
    250  1.1  mrg   auto_vec<basic_block, 20> works;
    251  1.1  mrg   works.safe_push (loop->head);
    252  1.1  mrg 
    253  1.1  mrg   found_tail = false;
    254  1.1  mrg   for (dwork = 0; works.iterate (dwork, &bb); dwork++)
    255  1.1  mrg     {
    256  1.1  mrg       edge e;
    257  1.1  mrg       edge_iterator ei;
    258  1.1  mrg       if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
    259  1.1  mrg 	{
    260  1.1  mrg 	  /* We've reached the exit block.  The loop must be bad. */
    261  1.1  mrg 	  if (dump_file)
    262  1.1  mrg 	    fprintf (dump_file,
    263  1.1  mrg 		     ";; Loop is bad - reached exit block while scanning\n");
    264  1.1  mrg 	  loop->bad = true;
    265  1.1  mrg 	  break;
    266  1.1  mrg 	}
    267  1.1  mrg 
    268  1.1  mrg       if (bitmap_bit_p (loop->block_bitmap, bb->index))
    269  1.1  mrg 	continue;
    270  1.1  mrg 
    271  1.1  mrg       /* We've not seen this block before.  Add it to the loop's
    272  1.1  mrg 	 list and then add each successor to the work list.  */
    273  1.1  mrg 
    274  1.1  mrg       loop->blocks.safe_push (bb);
    275  1.1  mrg       bitmap_set_bit (loop->block_bitmap, bb->index);
    276  1.1  mrg 
    277  1.1  mrg       if (bb == tail_bb)
    278  1.1  mrg 	found_tail = true;
    279  1.1  mrg       else
    280  1.1  mrg 	{
    281  1.1  mrg 	  FOR_EACH_EDGE (e, ei, bb->succs)
    282  1.1  mrg 	    {
    283  1.1  mrg 	      basic_block succ = EDGE_SUCC (bb, ei.index)->dest;
    284  1.1  mrg 	      if (REGNO_REG_SET_P (df_get_live_in (succ),
    285  1.1  mrg 				   REGNO (loop->iter_reg)))
    286  1.1  mrg 		works.safe_push (succ);
    287  1.1  mrg 	    }
    288  1.1  mrg 	}
    289  1.1  mrg     }
    290  1.1  mrg 
    291  1.1  mrg   if (!found_tail)
    292  1.1  mrg     loop->bad = true;
    293  1.1  mrg 
    294  1.1  mrg   /* Find the predecessor, and make sure nothing else jumps into this loop.  */
    295  1.1  mrg   if (!loop->bad)
    296  1.1  mrg     {
    297  1.1  mrg       FOR_EACH_VEC_ELT (loop->blocks, dwork, bb)
    298  1.1  mrg 	{
    299  1.1  mrg 	  edge e;
    300  1.1  mrg 	  edge_iterator ei;
    301  1.1  mrg 	  FOR_EACH_EDGE (e, ei, bb->preds)
    302  1.1  mrg 	    {
    303  1.1  mrg 	      basic_block pred = e->src;
    304  1.1  mrg 
    305  1.1  mrg 	      if (!bb_in_loop_p (loop, pred))
    306  1.1  mrg 		{
    307  1.1  mrg 		  if (dump_file)
    308  1.1  mrg 		    fprintf (dump_file, ";; Loop %d: incoming edge %d -> %d\n",
    309  1.1  mrg 			     loop->loop_no, pred->index,
    310  1.1  mrg 			     e->dest->index);
    311  1.1  mrg 		  vec_safe_push (loop->incoming, e);
    312  1.1  mrg 		}
    313  1.1  mrg 	    }
    314  1.1  mrg 	}
    315  1.1  mrg 
    316  1.1  mrg       if (!process_incoming_edges (loop))
    317  1.1  mrg 	{
    318  1.1  mrg 	  if (dump_file)
    319  1.1  mrg 	    fprintf (dump_file,
    320  1.1  mrg 		     ";; retrying loop %d with forwarder blocks\n",
    321  1.1  mrg 		     loop->loop_no);
    322  1.1  mrg 	  if (!add_forwarder_blocks (loop))
    323  1.1  mrg 	    {
    324  1.1  mrg 	      if (dump_file)
    325  1.1  mrg 		fprintf (dump_file, ";; No forwarder blocks found\n");
    326  1.1  mrg 	      loop->bad = true;
    327  1.1  mrg 	    }
    328  1.1  mrg 	  else if (!process_incoming_edges (loop))
    329  1.1  mrg 	    {
    330  1.1  mrg 	      if (dump_file)
    331  1.1  mrg 		fprintf (dump_file,
    332  1.1  mrg 			 ";; can't find suitable entry for loop %d\n",
    333  1.1  mrg 			 loop->loop_no);
    334  1.1  mrg 	    }
    335  1.1  mrg 	}
    336  1.1  mrg     }
    337  1.1  mrg }
    338  1.1  mrg 
    339  1.1  mrg /* Analyze the structure of the loops in the current function.  Use
    340  1.1  mrg    LOOP_STACK for bitmap allocations.  Returns all the valid candidates for
    341  1.1  mrg    hardware loops found in this function.  HOOKS is the argument
    342  1.1  mrg    passed to reorg_loops, used here to find the iteration registers
    343  1.1  mrg    from a loop_end pattern.  */
    344  1.1  mrg static hwloop_info
    345  1.1  mrg discover_loops (bitmap_obstack *loop_stack, struct hw_doloop_hooks *hooks)
    346  1.1  mrg {
    347  1.1  mrg   hwloop_info loops = NULL;
    348  1.1  mrg   hwloop_info loop;
    349  1.1  mrg   basic_block bb;
    350  1.1  mrg   int nloops = 0;
    351  1.1  mrg 
    352  1.1  mrg   /* Find all the possible loop tails.  This means searching for every
    353  1.1  mrg      loop_end instruction.  For each one found, create a hwloop_info
    354  1.1  mrg      structure and add the head block to the work list. */
    355  1.1  mrg   FOR_EACH_BB_FN (bb, cfun)
    356  1.1  mrg     {
    357  1.1  mrg       rtx_insn *tail = BB_END (bb);
    358  1.1  mrg       rtx_insn *insn;
    359  1.1  mrg       rtx reg;
    360  1.1  mrg 
    361  1.1  mrg       while (tail && NOTE_P (tail) && tail != BB_HEAD (bb))
    362  1.1  mrg 	tail = PREV_INSN (tail);
    363  1.1  mrg 
    364  1.1  mrg       if (tail == NULL_RTX)
    365  1.1  mrg 	continue;
    366  1.1  mrg 
    367  1.1  mrg       if (!JUMP_P (tail))
    368  1.1  mrg 	continue;
    369  1.1  mrg       reg = hooks->end_pattern_reg (tail);
    370  1.1  mrg       if (reg == NULL_RTX)
    371  1.1  mrg 	continue;
    372  1.1  mrg 
    373  1.1  mrg       /* A possible loop end */
    374  1.1  mrg 
    375  1.1  mrg       /* There's a degenerate case we can handle - an empty loop consisting
    376  1.1  mrg 	 of only a back branch.  Handle that by deleting the branch.  */
    377  1.1  mrg       insn = JUMP_LABEL_AS_INSN (tail);
    378  1.1  mrg       while (insn && !NONDEBUG_INSN_P (insn))
    379  1.1  mrg 	insn = NEXT_INSN (insn);
    380  1.1  mrg       if (insn == tail)
    381  1.1  mrg 	{
    382  1.1  mrg 	  basic_block succ = FALLTHRU_EDGE (bb)->dest;
    383  1.1  mrg 	  if (dump_file)
    384  1.1  mrg 	    {
    385  1.1  mrg 	      fprintf (dump_file, ";; degenerate loop ending at\n");
    386  1.1  mrg 	      print_rtl_single (dump_file, tail);
    387  1.1  mrg 	    }
    388  1.1  mrg 	  if (!REGNO_REG_SET_P (df_get_live_in (succ), REGNO (reg)))
    389  1.1  mrg 	    {
    390  1.1  mrg 	      if (dump_file)
    391  1.1  mrg 		fprintf (dump_file, ";; deleting it\n");
    392  1.1  mrg 	      delete_insn_and_edges (tail);
    393  1.1  mrg 	      continue;
    394  1.1  mrg 	    }
    395  1.1  mrg 	}
    396  1.1  mrg 
    397  1.1  mrg       loop = XCNEW (struct hwloop_info_d);
    398  1.1  mrg       loop->next = loops;
    399  1.1  mrg       loops = loop;
    400  1.1  mrg       loop->loop_no = nloops++;
    401  1.1  mrg       loop->blocks.create (20);
    402  1.1  mrg       loop->block_bitmap = BITMAP_ALLOC (loop_stack);
    403  1.1  mrg 
    404  1.1  mrg       if (dump_file)
    405  1.1  mrg 	{
    406  1.1  mrg 	  fprintf (dump_file, ";; potential loop %d ending at\n",
    407  1.1  mrg 		   loop->loop_no);
    408  1.1  mrg 	  print_rtl_single (dump_file, tail);
    409  1.1  mrg 	}
    410  1.1  mrg 
    411  1.1  mrg       discover_loop (loop, bb, tail, reg);
    412  1.1  mrg     }
    413  1.1  mrg 
    414  1.1  mrg   /* Compute loop nestings.  Given two loops A and B, either the sets
    415  1.1  mrg      of their blocks don't intersect at all, or one is the subset of
    416  1.1  mrg      the other, or the blocks don't form a good nesting structure.  */
    417  1.1  mrg   for (loop = loops; loop; loop = loop->next)
    418  1.1  mrg     {
    419  1.1  mrg       hwloop_info other;
    420  1.1  mrg 
    421  1.1  mrg       if (loop->bad)
    422  1.1  mrg 	continue;
    423  1.1  mrg 
    424  1.1  mrg       for (other = loops; other; other = other->next)
    425  1.1  mrg 	{
    426  1.1  mrg 	  if (other->bad)
    427  1.1  mrg 	    continue;
    428  1.1  mrg 
    429  1.1  mrg 	  if (!bitmap_intersect_p (other->block_bitmap, loop->block_bitmap))
    430  1.1  mrg 	    continue;
    431  1.1  mrg 	  if (!bitmap_intersect_compl_p (other->block_bitmap,
    432  1.1  mrg 					 loop->block_bitmap))
    433  1.1  mrg 	    loop->loops.safe_push (other);
    434  1.1  mrg 	  else if (!bitmap_intersect_compl_p (loop->block_bitmap,
    435  1.1  mrg 					      other->block_bitmap))
    436  1.1  mrg 	    other->loops.safe_push (loop);
    437  1.1  mrg 	  else
    438  1.1  mrg 	    {
    439  1.1  mrg 	      if (dump_file)
    440  1.1  mrg 		fprintf (dump_file,
    441  1.1  mrg 			 ";; can't find suitable nesting for loops %d and %d\n",
    442  1.1  mrg 			 loop->loop_no, other->loop_no);
    443  1.1  mrg 	      loop->bad = other->bad = true;
    444  1.1  mrg 	    }
    445  1.1  mrg 	}
    446  1.1  mrg     }
    447  1.1  mrg 
    448  1.1  mrg   if (dump_file)
    449  1.1  mrg     dump_hwloops (loops);
    450  1.1  mrg 
    451  1.1  mrg   return loops;
    452  1.1  mrg }
    453  1.1  mrg 
    454  1.1  mrg /* Free up the loop structures in LOOPS.  */
    455  1.1  mrg static void
    456  1.1  mrg free_loops (hwloop_info loops)
    457  1.1  mrg {
    458  1.1  mrg   while (loops)
    459  1.1  mrg     {
    460  1.1  mrg       hwloop_info loop = loops;
    461  1.1  mrg       loops = loop->next;
    462  1.1  mrg       loop->loops.release ();
    463  1.1  mrg       loop->blocks.release ();
    464  1.1  mrg       BITMAP_FREE (loop->block_bitmap);
    465  1.1  mrg       XDELETE (loop);
    466  1.1  mrg     }
    467  1.1  mrg }
    468  1.1  mrg 
    469  1.1  mrg #define BB_AUX_INDEX(BB) ((intptr_t) (BB)->aux)
    470  1.1  mrg 
    471  1.1  mrg /* Initialize the aux fields to give ascending indices to basic blocks.  */
    472  1.1  mrg static void
    473  1.1  mrg set_bb_indices (void)
    474  1.1  mrg {
    475  1.1  mrg   basic_block bb;
    476  1.1  mrg   intptr_t index;
    477  1.1  mrg 
    478  1.1  mrg   index = 0;
    479  1.1  mrg   FOR_EACH_BB_FN (bb, cfun)
    480  1.1  mrg     bb->aux = (void *) index++;
    481  1.1  mrg }
    482  1.1  mrg 
    483  1.1  mrg /* The taken-branch edge from the loop end can actually go forward.
    484  1.1  mrg    If the target's hardware loop support requires that the loop end be
    485  1.1  mrg    after the loop start, try to reorder a loop's basic blocks when we
    486  1.1  mrg    find such a case.
    487  1.1  mrg    This is not very aggressive; it only moves at most one block.  It
    488  1.1  mrg    does not introduce new branches into loops; it may remove them, or
    489  1.1  mrg    it may switch fallthru/jump edges.  */
    490  1.1  mrg static void
    491  1.1  mrg reorder_loops (hwloop_info loops)
    492  1.1  mrg {
    493  1.1  mrg   basic_block bb;
    494  1.1  mrg   hwloop_info loop;
    495  1.1  mrg 
    496  1.1  mrg   cfg_layout_initialize (0);
    497  1.1  mrg 
    498  1.1  mrg   set_bb_indices ();
    499  1.1  mrg 
    500  1.1  mrg   for (loop = loops; loop; loop = loop->next)
    501  1.1  mrg     {
    502  1.1  mrg       edge e;
    503  1.1  mrg       edge_iterator ei;
    504  1.1  mrg 
    505  1.1  mrg       if (loop->bad)
    506  1.1  mrg 	continue;
    507  1.1  mrg 
    508  1.1  mrg       if (BB_AUX_INDEX (loop->head) <= BB_AUX_INDEX (loop->tail))
    509  1.1  mrg 	continue;
    510  1.1  mrg 
    511  1.1  mrg       FOR_EACH_EDGE (e, ei, loop->head->succs)
    512  1.1  mrg 	{
    513  1.1  mrg 	  if (bitmap_bit_p (loop->block_bitmap, e->dest->index)
    514  1.1  mrg 	      && BB_AUX_INDEX (e->dest) < BB_AUX_INDEX (loop->tail))
    515  1.1  mrg 	    {
    516  1.1  mrg 	      basic_block start_bb = e->dest;
    517  1.1  mrg 	      basic_block start_prev_bb = start_bb->prev_bb;
    518  1.1  mrg 
    519  1.1  mrg 	      if (dump_file)
    520  1.1  mrg 		fprintf (dump_file, ";; Moving block %d before block %d\n",
    521  1.1  mrg 			 loop->head->index, start_bb->index);
    522  1.1  mrg 	      loop->head->prev_bb->next_bb = loop->head->next_bb;
    523  1.1  mrg 	      loop->head->next_bb->prev_bb = loop->head->prev_bb;
    524  1.1  mrg 
    525  1.1  mrg 	      loop->head->prev_bb = start_prev_bb;
    526  1.1  mrg 	      loop->head->next_bb = start_bb;
    527  1.1  mrg 	      start_prev_bb->next_bb = start_bb->prev_bb = loop->head;
    528  1.1  mrg 
    529  1.1  mrg 	      set_bb_indices ();
    530  1.1  mrg 	      break;
    531  1.1  mrg 	    }
    532  1.1  mrg 	}
    533  1.1  mrg       loops = loops->next;
    534  1.1  mrg     }
    535  1.1  mrg 
    536  1.1  mrg   FOR_EACH_BB_FN (bb, cfun)
    537  1.1  mrg     {
    538  1.1  mrg       if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
    539  1.1  mrg 	bb->aux = bb->next_bb;
    540  1.1  mrg       else
    541  1.1  mrg 	bb->aux = NULL;
    542  1.1  mrg     }
    543  1.1  mrg   cfg_layout_finalize ();
    544  1.1  mrg   clear_aux_for_blocks ();
    545  1.1  mrg   df_analyze ();
    546  1.1  mrg }
    547  1.1  mrg 
    548  1.1  mrg /* Call the OPT function for LOOP and all of its sub-loops.  This is
    549  1.1  mrg    done in a depth-first search; innermost loops are visited first.
    550  1.1  mrg    OPTIMIZE and FAIL are the functions passed to reorg_loops by the
    551  1.1  mrg    target's reorg pass.  */
    552  1.1  mrg static void
    553  1.1  mrg optimize_loop (hwloop_info loop, struct hw_doloop_hooks *hooks)
    554  1.1  mrg {
    555  1.1  mrg   int ix;
    556  1.1  mrg   hwloop_info inner;
    557  1.1  mrg   int inner_depth = 0;
    558  1.1  mrg 
    559  1.1  mrg   if (loop->visited)
    560  1.1  mrg     return;
    561  1.1  mrg 
    562  1.1  mrg   loop->visited = 1;
    563  1.1  mrg 
    564  1.1  mrg   if (loop->bad)
    565  1.1  mrg     {
    566  1.1  mrg       if (dump_file)
    567  1.1  mrg 	fprintf (dump_file, ";; loop %d bad when found\n", loop->loop_no);
    568  1.1  mrg       goto bad_loop;
    569  1.1  mrg     }
    570  1.1  mrg 
    571  1.1  mrg   /* Every loop contains in its list of inner loops every loop nested inside
    572  1.1  mrg      it, even if there are intermediate loops.  This works because we're doing
    573  1.1  mrg      a depth-first search here and never visit a loop more than once.
    574  1.1  mrg      Recursion depth is effectively limited by the number of available
    575  1.1  mrg      hardware registers.  */
    576  1.1  mrg   for (ix = 0; loop->loops.iterate (ix, &inner); ix++)
    577  1.1  mrg     {
    578  1.1  mrg       optimize_loop (inner, hooks);
    579  1.1  mrg 
    580  1.1  mrg       if (!inner->bad && inner_depth < inner->depth)
    581  1.1  mrg 	inner_depth = inner->depth;
    582  1.1  mrg       /* The set of registers may be changed while optimizing the inner
    583  1.1  mrg 	 loop.  */
    584  1.1  mrg       loop->regs_set_in_loop |= inner->regs_set_in_loop;
    585  1.1  mrg     }
    586  1.1  mrg 
    587  1.1  mrg   loop->depth = inner_depth + 1;
    588  1.1  mrg 
    589  1.1  mrg   if (hooks->opt (loop))
    590  1.1  mrg     return;
    591  1.1  mrg 
    592  1.1  mrg  bad_loop:
    593  1.1  mrg   if (dump_file)
    594  1.1  mrg     fprintf (dump_file, ";; loop %d is bad\n", loop->loop_no);
    595  1.1  mrg 
    596  1.1  mrg   loop->bad = true;
    597  1.1  mrg   hooks->fail (loop);
    598  1.1  mrg }
    599  1.1  mrg 
    600  1.1  mrg /* This function can be used from a port's machine_dependent_reorg to
    601  1.1  mrg    find and analyze loops that end in loop_end instructions.  It uses
    602  1.1  mrg    a set of function pointers in HOOKS to call back into the
    603  1.1  mrg    target-specific functions to perform the actual machine-specific
    604  1.1  mrg    transformations.
    605  1.1  mrg 
    606  1.1  mrg    Such transformations typically involve additional set-up
    607  1.1  mrg    instructions before the loop, to define loop bounds or set up a
    608  1.1  mrg    special loop counter register.
    609  1.1  mrg 
    610  1.1  mrg    DO_REORDER should be set to true if we should try to use the
    611  1.1  mrg    reorder_loops function to ensure the loop end occurs after the loop
    612  1.1  mrg    start.  This is for use by targets where the loop hardware requires
    613  1.1  mrg    this condition.
    614  1.1  mrg 
    615  1.1  mrg    HOOKS is used to pass in target specific hooks; see
    616  1.1  mrg    hw-doloop.h.  */
    617  1.1  mrg void
    618  1.1  mrg reorg_loops (bool do_reorder, struct hw_doloop_hooks *hooks)
    619  1.1  mrg {
    620  1.1  mrg   hwloop_info loops = NULL;
    621  1.1  mrg   hwloop_info loop;
    622  1.1  mrg   bitmap_obstack loop_stack;
    623  1.1  mrg 
    624  1.1  mrg   df_live_add_problem ();
    625  1.1  mrg   df_live_set_all_dirty ();
    626  1.1  mrg   df_analyze ();
    627  1.1  mrg 
    628  1.1  mrg   bitmap_obstack_initialize (&loop_stack);
    629  1.1  mrg 
    630  1.1  mrg   if (dump_file)
    631  1.1  mrg     fprintf (dump_file, ";; Find loops, first pass\n\n");
    632  1.1  mrg 
    633  1.1  mrg   loops = discover_loops (&loop_stack, hooks);
    634  1.1  mrg 
    635  1.1  mrg   /* We can't enter cfglayout mode anymore if basic block partitioning
    636  1.1  mrg      already happened.  */
    637  1.1  mrg   if (do_reorder && !crtl->has_bb_partition)
    638  1.1  mrg     {
    639  1.1  mrg       reorder_loops (loops);
    640  1.1  mrg       free_loops (loops);
    641  1.1  mrg 
    642  1.1  mrg       if (dump_file)
    643  1.1  mrg 	fprintf (dump_file, ";; Find loops, second pass\n\n");
    644  1.1  mrg 
    645  1.1  mrg       loops = discover_loops (&loop_stack, hooks);
    646  1.1  mrg     }
    647  1.1  mrg 
    648  1.1  mrg   for (loop = loops; loop; loop = loop->next)
    649  1.1  mrg     scan_loop (loop);
    650  1.1  mrg 
    651  1.1  mrg   /* Now apply the optimizations.  */
    652  1.1  mrg   for (loop = loops; loop; loop = loop->next)
    653  1.1  mrg     optimize_loop (loop, hooks);
    654  1.1  mrg 
    655  1.1  mrg   if (dump_file)
    656  1.1  mrg     {
    657  1.1  mrg       fprintf (dump_file, ";; After hardware loops optimization:\n\n");
    658  1.1  mrg       dump_hwloops (loops);
    659  1.1  mrg     }
    660  1.1  mrg 
    661  1.1  mrg   free_loops (loops);
    662  1.1  mrg   bitmap_obstack_release (&loop_stack);
    663  1.1  mrg 
    664  1.1  mrg   if (dump_file)
    665  1.1  mrg     print_rtl (dump_file, get_insns ());
    666  1.1  mrg }
    667