LCOV - code coverage report
Current view: top level - gcc - cfgloop.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.8 % 983 893
Test Date: 2024-04-20 14:03:02 Functions: 95.8 % 72 69
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Natural loop discovery code for GNU compiler.
       2                 :             :    Copyright (C) 2000-2024 Free Software Foundation, Inc.
       3                 :             : 
       4                 :             : This file is part of GCC.
       5                 :             : 
       6                 :             : GCC is free software; you can redistribute it and/or modify it under
       7                 :             : the terms of the GNU General Public License as published by the Free
       8                 :             : Software Foundation; either version 3, or (at your option) any later
       9                 :             : version.
      10                 :             : 
      11                 :             : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12                 :             : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13                 :             : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14                 :             : for more details.
      15                 :             : 
      16                 :             : You should have received a copy of the GNU General Public License
      17                 :             : along with GCC; see the file COPYING3.  If not see
      18                 :             : <http://www.gnu.org/licenses/>.  */
      19                 :             : 
      20                 :             : #include "config.h"
      21                 :             : #include "system.h"
      22                 :             : #include "coretypes.h"
      23                 :             : #include "backend.h"
      24                 :             : #include "rtl.h"
      25                 :             : #include "tree.h"
      26                 :             : #include "gimple.h"
      27                 :             : #include "cfghooks.h"
      28                 :             : #include "gimple-ssa.h"
      29                 :             : #include "diagnostic-core.h"
      30                 :             : #include "cfganal.h"
      31                 :             : #include "cfgloop.h"
      32                 :             : #include "gimple-iterator.h"
      33                 :             : #include "dumpfile.h"
      34                 :             : #include "tree-ssa.h"
      35                 :             : #include "tree-pretty-print.h"
      36                 :             : #include "sreal.h"
      37                 :             : 
      38                 :             : static void flow_loops_cfg_dump (FILE *);
      39                 :             : 
      40                 :             : /* Dump loop related CFG information.  */
      41                 :             : 
      42                 :             : static void
      43                 :        5640 : flow_loops_cfg_dump (FILE *file)
      44                 :             : {
      45                 :        5640 :   basic_block bb;
      46                 :             : 
      47                 :        5640 :   if (!file)
      48                 :             :     return;
      49                 :             : 
      50                 :       25694 :   FOR_EACH_BB_FN (bb, cfun)
      51                 :             :     {
      52                 :       20054 :       edge succ;
      53                 :       20054 :       edge_iterator ei;
      54                 :             : 
      55                 :       20054 :       fprintf (file, ";; %d succs { ", bb->index);
      56                 :       46477 :       FOR_EACH_EDGE (succ, ei, bb->succs)
      57                 :       26423 :         fprintf (file, "%d ", succ->dest->index);
      58                 :       20054 :       fprintf (file, "}\n");
      59                 :             :     }
      60                 :             : }
      61                 :             : 
      62                 :             : /* Return nonzero if the nodes of LOOP are a subset of OUTER.  */
      63                 :             : 
      64                 :             : bool
      65                 :   666355898 : flow_loop_nested_p (const class loop *outer, const class loop *loop)
      66                 :             : {
      67                 :   666355898 :   unsigned odepth = loop_depth (outer);
      68                 :             : 
      69                 :   666355898 :   return (loop_depth (loop) > odepth
      70                 :   430190842 :           && (*loop->superloops)[odepth] == outer);
      71                 :             : }
      72                 :             : 
      73                 :             : /* Returns the loop such that LOOP is nested DEPTH (indexed from zero)
      74                 :             :    loops within LOOP.  */
      75                 :             : 
      76                 :             : class loop *
      77                 :    19263744 : superloop_at_depth (class loop *loop, unsigned depth)
      78                 :             : {
      79                 :    19263744 :   unsigned ldepth = loop_depth (loop);
      80                 :             : 
      81                 :    19263744 :   gcc_assert (depth <= ldepth);
      82                 :             : 
      83                 :    19263744 :   if (depth == ldepth)
      84                 :             :     return loop;
      85                 :             : 
      86                 :     5098161 :   return (*loop->superloops)[depth];
      87                 :             : }
      88                 :             : 
      89                 :             : /* Returns the list of the latch edges of LOOP.  */
      90                 :             : 
      91                 :             : static vec<edge> 
      92                 :      172533 : get_loop_latch_edges (const class loop *loop)
      93                 :             : {
      94                 :      172533 :   edge_iterator ei;
      95                 :      172533 :   edge e;
      96                 :      172533 :   vec<edge> ret = vNULL;
      97                 :             : 
      98                 :      780533 :   FOR_EACH_EDGE (e, ei, loop->header->preds)
      99                 :             :     {
     100                 :      608000 :       if (dominated_by_p (CDI_DOMINATORS, e->src, loop->header))
     101                 :      423447 :         ret.safe_push (e);
     102                 :             :     }
     103                 :             : 
     104                 :      172533 :   return ret;
     105                 :             : }
     106                 :             : 
     107                 :             : /* Dump the loop information specified by LOOP to the stream FILE
     108                 :             :    using auxiliary dump callback function LOOP_DUMP_AUX if non null.  */
     109                 :             : 
     110                 :             : void
     111                 :        7246 : flow_loop_dump (const class loop *loop, FILE *file,
     112                 :             :                 void (*loop_dump_aux) (const class loop *, FILE *, int),
     113                 :             :                 int verbose)
     114                 :             : {
     115                 :        7246 :   basic_block *bbs;
     116                 :        7246 :   unsigned i;
     117                 :        7246 :   vec<edge> latches;
     118                 :        7246 :   edge e;
     119                 :             : 
     120                 :        7246 :   if (! loop || ! loop->header)
     121                 :           0 :     return;
     122                 :             : 
     123                 :        7246 :   fprintf (file, ";;\n;; Loop %d\n", loop->num);
     124                 :             : 
     125                 :        7246 :   fprintf (file, ";;  header %d, ", loop->header->index);
     126                 :        7246 :   if (loop->latch)
     127                 :        7242 :     fprintf (file, "latch %d\n", loop->latch->index);
     128                 :             :   else
     129                 :             :     {
     130                 :           4 :       fprintf (file, "multiple latches:");
     131                 :           4 :       latches = get_loop_latch_edges (loop);
     132                 :          16 :       FOR_EACH_VEC_ELT (latches, i, e)
     133                 :           8 :         fprintf (file, " %d", e->src->index);
     134                 :           4 :       latches.release ();
     135                 :           4 :       fprintf (file, "\n");
     136                 :             :     }
     137                 :             : 
     138                 :       14492 :   fprintf (file, ";;  depth %d, outer %ld",
     139                 :        7246 :            loop_depth (loop), (long) (loop_outer (loop)
     140                 :        1585 :                                       ? loop_outer (loop)->num : -1));
     141                 :        7246 :   print_loop_info (file, loop, ";;  ");
     142                 :             : 
     143                 :        7246 :   fprintf (file, "\n;;  nodes:");
     144                 :        7246 :   bbs = get_loop_body (loop);
     145                 :       51306 :   for (i = 0; i < loop->num_nodes; i++)
     146                 :       36814 :     fprintf (file, " %d", bbs[i]->index);
     147                 :        7246 :   free (bbs);
     148                 :        7246 :   fprintf (file, "\n");
     149                 :             : 
     150                 :        7246 :   if (loop_dump_aux)
     151                 :           0 :     loop_dump_aux (loop, file, verbose);
     152                 :             : }
     153                 :             : 
     154                 :             : /* Dump the loop information about loops to the stream FILE,
     155                 :             :    using auxiliary dump callback function LOOP_DUMP_AUX if non null.  */
     156                 :             : 
     157                 :             : void
     158                 :    45855419 : flow_loops_dump (FILE *file, void (*loop_dump_aux) (const class loop *, FILE *, int), int verbose)
     159                 :             : {
     160                 :    45855419 :   if (!current_loops || ! file)
     161                 :             :     return;
     162                 :             : 
     163                 :       11322 :   fprintf (file, ";; %d loops found\n", number_of_loops (cfun));
     164                 :             : 
     165                 :       24153 :   for (auto loop : loops_list (cfun, LI_INCLUDE_ROOT))
     166                 :             :     {
     167                 :        7170 :       flow_loop_dump (loop, file, loop_dump_aux, verbose);
     168                 :        5661 :     }
     169                 :             : 
     170                 :        5661 :   if (verbose)
     171                 :        5640 :     flow_loops_cfg_dump (file);
     172                 :             : }
     173                 :             : 
     174                 :             : /* Free data allocated for LOOP.  */
     175                 :             : 
     176                 :             : void
     177                 :    14235062 : flow_loop_free (class loop *loop)
     178                 :             : {
     179                 :    14235062 :   struct loop_exit *exit, *next;
     180                 :             : 
     181                 :    14235062 :   vec_free (loop->superloops);
     182                 :             : 
     183                 :             :   /* Break the list of the loop exit records.  They will be freed when the
     184                 :             :      corresponding edge is rescanned or removed, and this avoids
     185                 :             :      accessing the (already released) head of the list stored in the
     186                 :             :      loop structure.  */
     187                 :    14246481 :   for (exit = loop->exits->next; exit != loop->exits; exit = next)
     188                 :             :     {
     189                 :       11419 :       next = exit->next;
     190                 :       11419 :       exit->next = exit;
     191                 :       11419 :       exit->prev = exit;
     192                 :             :     }
     193                 :             : 
     194                 :    14235062 :   ggc_free (loop->exits);
     195                 :    14235062 :   ggc_free (loop);
     196                 :    14235062 : }
     197                 :             : 
     198                 :             : /* Free all the memory allocated for LOOPS.  */
     199                 :             : 
     200                 :             : void
     201                 :     9325222 : flow_loops_free (struct loops *loops)
     202                 :             : {
     203                 :     9325222 :   if (loops->larray)
     204                 :             :     {
     205                 :             :       unsigned i;
     206                 :             :       loop_p loop;
     207                 :             : 
     208                 :             :       /* Free the loop descriptors.  */
     209                 :    23647564 :       FOR_EACH_VEC_SAFE_ELT (loops->larray, i, loop)
     210                 :             :         {
     211                 :    14322342 :           if (!loop)
     212                 :      435707 :             continue;
     213                 :             : 
     214                 :    13886635 :           flow_loop_free (loop);
     215                 :             :         }
     216                 :             : 
     217                 :    18650444 :       vec_free (loops->larray);
     218                 :             :     }
     219                 :     9325222 : }
     220                 :             : 
     221                 :             : /* Find the nodes contained within the LOOP with header HEADER.
     222                 :             :    Return the number of nodes within the loop.  */
     223                 :             : 
     224                 :             : int
     225                 :    19079198 : flow_loop_nodes_find (basic_block header, class loop *loop)
     226                 :             : {
     227                 :    19079198 :   vec<basic_block> stack = vNULL;
     228                 :    19079198 :   int num_nodes = 1;
     229                 :    19079198 :   edge latch;
     230                 :    19079198 :   edge_iterator latch_ei;
     231                 :             : 
     232                 :    19079198 :   header->loop_father = loop;
     233                 :             : 
     234                 :    58013667 :   FOR_EACH_EDGE (latch, latch_ei, loop->header->preds)
     235                 :             :     {
     236                 :    63302663 :       if (latch->src->loop_father == loop
     237                 :    38934469 :           || !dominated_by_p (CDI_DOMINATORS, latch->src, loop->header))
     238                 :    24368194 :         continue;
     239                 :             : 
     240                 :    14566275 :       num_nodes++;
     241                 :    14566275 :       stack.safe_push (latch->src);
     242                 :    14566275 :       latch->src->loop_father = loop;
     243                 :             : 
     244                 :   148777489 :       while (!stack.is_empty ())
     245                 :             :         {
     246                 :    95276745 :           basic_block node;
     247                 :    95276745 :           edge e;
     248                 :    95276745 :           edge_iterator ei;
     249                 :             : 
     250                 :    95276745 :           node = stack.pop ();
     251                 :             : 
     252                 :   225187515 :           FOR_EACH_EDGE (e, ei, node->preds)
     253                 :             :             {
     254                 :   129910770 :               basic_block ancestor = e->src;
     255                 :             : 
     256                 :   129910770 :               if (ancestor->loop_father != loop)
     257                 :             :                 {
     258                 :    80710470 :                   ancestor->loop_father = loop;
     259                 :    80710470 :                   num_nodes++;
     260                 :    80710470 :                   stack.safe_push (ancestor);
     261                 :             :                 }
     262                 :             :             }
     263                 :             :         }
     264                 :             :     }
     265                 :    19079198 :   stack.release ();
     266                 :             : 
     267                 :    19079198 :   return num_nodes;
     268                 :             : }
     269                 :             : 
     270                 :             : /* Records the vector of superloops of the loop LOOP, whose immediate
     271                 :             :    superloop is FATHER.  */
     272                 :             : 
     273                 :             : static void
     274                 :    19906433 : establish_preds (class loop *loop, class loop *father)
     275                 :             : {
     276                 :    19906433 :   loop_p ploop;
     277                 :    19906433 :   unsigned depth = loop_depth (father) + 1;
     278                 :    19906433 :   unsigned i;
     279                 :             : 
     280                 :    19906433 :   loop->superloops = 0;
     281                 :    19906433 :   vec_alloc (loop->superloops, depth);
     282                 :    47779580 :   FOR_EACH_VEC_SAFE_ELT (father->superloops, i, ploop)
     283                 :     7966714 :     loop->superloops->quick_push (ploop);
     284                 :    19906433 :   loop->superloops->quick_push (father);
     285                 :             : 
     286                 :    19979120 :   for (ploop = loop->inner; ploop; ploop = ploop->next)
     287                 :       72687 :     establish_preds (ploop, loop);
     288                 :    19906433 : }
     289                 :             : 
     290                 :             : /* Add LOOP to the loop hierarchy tree where FATHER is father of the
     291                 :             :    added loop.  If LOOP has some children, take care of that their
     292                 :             :    pred field will be initialized correctly.  If AFTER is non-null
     293                 :             :    then it's expected it's a pointer into FATHERs inner sibling
     294                 :             :    list and LOOP is added behind AFTER, otherwise it's added in front
     295                 :             :    of FATHERs siblings.  */
     296                 :             : 
     297                 :             : void
     298                 :    19833746 : flow_loop_tree_node_add (class loop *father, class loop *loop,
     299                 :             :                          class loop *after)
     300                 :             : {
     301                 :    19833746 :   if (after)
     302                 :             :     {
     303                 :        3023 :       loop->next = after->next;
     304                 :        3023 :       after->next = loop;
     305                 :             :     }
     306                 :             :   else
     307                 :             :     {
     308                 :    19830723 :       loop->next = father->inner;
     309                 :    19830723 :       father->inner = loop;
     310                 :             :     }
     311                 :             : 
     312                 :    19833746 :   establish_preds (loop, father);
     313                 :    19833746 : }
     314                 :             : 
     315                 :             : /* Remove LOOP from the loop hierarchy tree.  */
     316                 :             : 
     317                 :             : void
     318                 :    15259099 : flow_loop_tree_node_remove (class loop *loop)
     319                 :             : {
     320                 :    15259099 :   class loop *prev, *father;
     321                 :             : 
     322                 :    15259099 :   father = loop_outer (loop);
     323                 :             : 
     324                 :             :   /* Remove loop from the list of sons.  */
     325                 :    15259099 :   if (father->inner == loop)
     326                 :     7254262 :     father->inner = loop->next;
     327                 :             :   else
     328                 :             :     {
     329                 :   201482269 :       for (prev = father->inner; prev->next != loop; prev = prev->next)
     330                 :   193477432 :         continue;
     331                 :     8004837 :       prev->next = loop->next;
     332                 :             :     }
     333                 :             : 
     334                 :    15259099 :   loop->superloops = NULL;
     335                 :    15259099 : }
     336                 :             : 
     337                 :             : /* Allocates and returns new loop structure.  */
     338                 :             : 
     339                 :             : class loop *
     340                 :    14351829 : alloc_loop (void)
     341                 :             : {
     342                 :    14351829 :   class loop *loop = ggc_cleared_alloc<class loop> ();
     343                 :             : 
     344                 :    14351829 :   loop->exits = ggc_cleared_alloc<loop_exit> ();
     345                 :    14351829 :   loop->exits->next = loop->exits->prev = loop->exits;
     346                 :    14351829 :   loop->can_be_parallel = false;
     347                 :    14351829 :   loop->constraints = 0;
     348                 :    14351829 :   loop->nb_iterations_upper_bound = 0;
     349                 :    14351829 :   loop->nb_iterations_likely_upper_bound = 0;
     350                 :    14351829 :   loop->nb_iterations_estimate = 0;
     351                 :    14351829 :   return loop;
     352                 :             : }
     353                 :             : 
     354                 :             : /* Initializes loops structure LOOPS, reserving place for NUM_LOOPS loops
     355                 :             :    (including the root of the loop tree).  */
     356                 :             : 
     357                 :             : void
     358                 :     9428755 : init_loops_structure (struct function *fn,
     359                 :             :                       struct loops *loops, unsigned num_loops)
     360                 :             : {
     361                 :     9428755 :   class loop *root;
     362                 :             : 
     363                 :     9428755 :   memset (loops, 0, sizeof *loops);
     364                 :     9428755 :   vec_alloc (loops->larray, num_loops);
     365                 :             : 
     366                 :             :   /* Dummy loop containing whole function.  */
     367                 :     9428755 :   root = alloc_loop ();
     368                 :     9428755 :   root->num_nodes = n_basic_blocks_for_fn (fn);
     369                 :     9428755 :   root->latch = EXIT_BLOCK_PTR_FOR_FN (fn);
     370                 :     9428755 :   root->header = ENTRY_BLOCK_PTR_FOR_FN (fn);
     371                 :     9428755 :   ENTRY_BLOCK_PTR_FOR_FN (fn)->loop_father = root;
     372                 :     9428755 :   EXIT_BLOCK_PTR_FOR_FN (fn)->loop_father = root;
     373                 :             : 
     374                 :     9428755 :   loops->larray->quick_push (root);
     375                 :     9428755 :   loops->tree_root = root;
     376                 :     9428755 : }
     377                 :             : 
     378                 :             : /* Returns whether HEADER is a loop header.  */
     379                 :             : 
     380                 :             : bool
     381                 :  3511953195 : bb_loop_header_p (basic_block header)
     382                 :             : {
     383                 :  3511953195 :   edge_iterator ei;
     384                 :  3511953195 :   edge e;
     385                 :             : 
     386                 :             :   /* If we have an abnormal predecessor, do not consider the
     387                 :             :      loop (not worth the problems).  */
     388                 :  3511953195 :   if (bb_has_abnormal_pred (header))
     389                 :             :     return false;
     390                 :             : 
     391                 :             :   /* Look for back edges where a predecessor is dominated
     392                 :             :      by this block.  A natural loop has a single entry
     393                 :             :      node (header) that dominates all the nodes in the
     394                 :             :      loop.  It also has single back edge to the header
     395                 :             :      from a latch node.  */
     396                 :  7996198599 :   FOR_EACH_EDGE (e, ei, header->preds)
     397                 :             :     {
     398                 :  4869008548 :       basic_block latch = e->src;
     399                 :  4869008548 :       if (latch != ENTRY_BLOCK_PTR_FOR_FN (cfun)
     400                 :  4869008548 :           && dominated_by_p (CDI_DOMINATORS, latch, header))
     401                 :             :         return true;
     402                 :             :     }
     403                 :             : 
     404                 :             :   return false;
     405                 :             : }
     406                 :             : 
     407                 :             : /* Find all the natural loops in the function and save in LOOPS structure and
     408                 :             :    recalculate loop_father information in basic block structures.
     409                 :             :    If LOOPS is non-NULL then the loop structures for already recorded loops
     410                 :             :    will be re-used and their number will not change.  We assume that no
     411                 :             :    stale loops exist in LOOPS.
     412                 :             :    When LOOPS is NULL it is allocated and re-built from scratch.
     413                 :             :    Return the built LOOPS structure.  */
     414                 :             : 
     415                 :             : struct loops *
     416                 :    19484954 : flow_loops_find (struct loops *loops)
     417                 :             : {
     418                 :    19484954 :   bool from_scratch = (loops == NULL);
     419                 :    19484954 :   int *rc_order;
     420                 :    19484954 :   int b;
     421                 :    19484954 :   unsigned i;
     422                 :             : 
     423                 :             :   /* Ensure that the dominators are computed.  */
     424                 :    19484954 :   calculate_dominance_info (CDI_DOMINATORS);
     425                 :             : 
     426                 :    19484954 :   if (!loops)
     427                 :             :     {
     428                 :     9285977 :       loops = ggc_cleared_alloc<struct loops> ();
     429                 :     9285977 :       init_loops_structure (cfun, loops, 1);
     430                 :             :     }
     431                 :             : 
     432                 :             :   /* Ensure that loop exits were released.  */
     433                 :    19484954 :   gcc_assert (loops->exits == NULL);
     434                 :             : 
     435                 :             :   /* Taking care of this degenerate case makes the rest of
     436                 :             :      this code simpler.  */
     437                 :    19484954 :   if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
     438                 :             :     return loops;
     439                 :             : 
     440                 :             :   /* The root loop node contains all basic-blocks.  */
     441                 :    19284373 :   loops->tree_root->num_nodes = n_basic_blocks_for_fn (cfun);
     442                 :             : 
     443                 :             :   /* Compute depth first search order of the CFG so that outer
     444                 :             :      natural loops will be found before inner natural loops.  */
     445                 :    19284373 :   rc_order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
     446                 :    19284373 :   pre_and_rev_post_order_compute (NULL, rc_order, false);
     447                 :             : 
     448                 :             :   /* Gather all loop headers in reverse completion order and allocate
     449                 :             :      loop structures for loops that are not already present.  */
     450                 :    19284373 :   auto_vec<loop_p> larray (loops->larray->length ());
     451                 :   322184726 :   for (b = 0; b < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; b++)
     452                 :             :     {
     453                 :   302900353 :       basic_block header = BASIC_BLOCK_FOR_FN (cfun, rc_order[b]);
     454                 :   302900353 :       if (bb_loop_header_p (header))
     455                 :             :         {
     456                 :    19079198 :           class loop *loop;
     457                 :             : 
     458                 :             :           /* The current active loop tree has valid loop-fathers for
     459                 :             :              header blocks.  */
     460                 :    19079198 :           if (!from_scratch
     461                 :    14880210 :               && header->loop_father->header == header)
     462                 :             :             {
     463                 :    14835722 :               loop = header->loop_father;
     464                 :             :               /* If we found an existing loop remove it from the
     465                 :             :                  loop tree.  It is going to be inserted again
     466                 :             :                  below.  */
     467                 :    14835722 :               flow_loop_tree_node_remove (loop);
     468                 :             :             }
     469                 :             :           else
     470                 :             :             {
     471                 :             :               /* Otherwise allocate a new loop structure for the loop.  */
     472                 :     4243476 :               loop = alloc_loop ();
     473                 :             :               /* ???  We could re-use unused loop slots here.  */
     474                 :     4243476 :               loop->num = loops->larray->length ();
     475                 :     4243476 :               vec_safe_push (loops->larray, loop);
     476                 :     4243476 :               loop->header = header;
     477                 :             : 
     478                 :     4243476 :               if (!from_scratch
     479                 :     4243476 :                   && dump_file && (dump_flags & TDF_DETAILS))
     480                 :           2 :                 fprintf (dump_file, "flow_loops_find: discovered new "
     481                 :             :                          "loop %d with header %d\n",
     482                 :             :                          loop->num, header->index);
     483                 :             :             }
     484                 :             :           /* Reset latch, we recompute it below.  */
     485                 :    19079198 :           loop->latch = NULL;
     486                 :    19079198 :           larray.safe_push (loop);
     487                 :             :         }
     488                 :             : 
     489                 :             :       /* Make blocks part of the loop root node at start.  */
     490                 :   302900353 :       header->loop_father = loops->tree_root;
     491                 :             :     }
     492                 :             : 
     493                 :    19284373 :   free (rc_order);
     494                 :             : 
     495                 :             :   /* Now iterate over the loops found, insert them into the loop tree
     496                 :             :      and assign basic-block ownership.  */
     497                 :    76727142 :   for (i = 0; i < larray.length (); ++i)
     498                 :             :     {
     499                 :    19079198 :       class loop *loop = larray[i];
     500                 :    19079198 :       basic_block header = loop->header;
     501                 :    19079198 :       edge_iterator ei;
     502                 :    19079198 :       edge e;
     503                 :             : 
     504                 :    19079198 :       flow_loop_tree_node_add (header->loop_father, loop);
     505                 :    19079198 :       loop->num_nodes = flow_loop_nodes_find (loop->header, loop);
     506                 :             : 
     507                 :             :       /* Look for the latch for this header block, if it has just a
     508                 :             :          single one.  */
     509                 :    57592559 :       FOR_EACH_EDGE (e, ei, header->preds)
     510                 :             :         {
     511                 :    38765985 :           basic_block latch = e->src;
     512                 :             : 
     513                 :    38765985 :           if (flow_bb_inside_loop_p (loop, latch))
     514                 :             :             {
     515                 :    19331822 :               if (loop->latch != NULL)
     516                 :             :                 {
     517                 :             :                   /* More than one latch edge.  */
     518                 :      252624 :                   loop->latch = NULL;
     519                 :      252624 :                   break;
     520                 :             :                 }
     521                 :    19079198 :               loop->latch = latch;
     522                 :             :             }
     523                 :             :         }
     524                 :             :     }
     525                 :             : 
     526                 :    19284373 :   return loops;
     527                 :    19284373 : }
     528                 :             : 
     529                 :             : /* qsort helper for sort_sibling_loops.  */
     530                 :             : 
     531                 :             : static int *sort_sibling_loops_cmp_rpo;
     532                 :             : static int
     533                 :        2571 : sort_sibling_loops_cmp (const void *la_, const void *lb_)
     534                 :             : {
     535                 :        2571 :   const class loop *la = *(const class loop * const *)la_;
     536                 :        2571 :   const class loop *lb = *(const class loop * const *)lb_;
     537                 :        2571 :   return (sort_sibling_loops_cmp_rpo[la->header->index]
     538                 :        2571 :           - sort_sibling_loops_cmp_rpo[lb->header->index]);
     539                 :             : }
     540                 :             : 
     541                 :             : /* Sort sibling loops in RPO order.  */
     542                 :             : 
     543                 :             : void
     544                 :         556 : sort_sibling_loops (function *fn)
     545                 :             : {
     546                 :             :   /* Match flow_loops_find in the order we sort sibling loops.  */
     547                 :         556 :   sort_sibling_loops_cmp_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
     548                 :         556 :   int *rc_order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
     549                 :         556 :   pre_and_rev_post_order_compute_fn (fn, NULL, rc_order, false);
     550                 :        7715 :   for (int i = 0; i < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; ++i)
     551                 :        7159 :     sort_sibling_loops_cmp_rpo[rc_order[i]] = i;
     552                 :         556 :   free (rc_order);
     553                 :             : 
     554                 :         556 :   auto_vec<loop_p, 3> siblings;
     555                 :        3570 :   for (auto loop : loops_list (fn, LI_INCLUDE_ROOT))
     556                 :        1902 :     if (loop->inner && loop->inner->next)
     557                 :             :       {
     558                 :         220 :         loop_p sibling = loop->inner;
     559                 :         582 :         do
     560                 :             :           {
     561                 :         582 :             siblings.safe_push (sibling);
     562                 :         582 :             sibling = sibling->next;
     563                 :             :           }
     564                 :         582 :         while (sibling);
     565                 :         220 :         siblings.qsort (sort_sibling_loops_cmp);
     566                 :         220 :         loop_p *siblingp = &loop->inner;
     567                 :        1604 :         for (unsigned i = 0; i < siblings.length (); ++i)
     568                 :             :           {
     569                 :         582 :             *siblingp = siblings[i];
     570                 :         582 :             siblingp = &(*siblingp)->next;
     571                 :             :           }
     572                 :         220 :         *siblingp = NULL;
     573                 :         220 :         siblings.truncate (0);
     574                 :         556 :       }
     575                 :             : 
     576                 :         556 :   free (sort_sibling_loops_cmp_rpo);
     577                 :         556 :   sort_sibling_loops_cmp_rpo = NULL;
     578                 :         556 : }
     579                 :             : 
     580                 :             : /* Ratio of frequencies of edges so that one of more latch edges is
     581                 :             :    considered to belong to inner loop with same header.  */
     582                 :             : #define HEAVY_EDGE_RATIO 8
     583                 :             : 
     584                 :             : /* Minimum number of samples for that we apply
     585                 :             :    find_subloop_latch_edge_by_profile heuristics.  */
     586                 :             : #define HEAVY_EDGE_MIN_SAMPLES 10
     587                 :             : 
     588                 :             : /* If the profile info is available, finds an edge in LATCHES that much more
     589                 :             :    frequent than the remaining edges.  Returns such an edge, or NULL if we do
     590                 :             :    not find one.
     591                 :             : 
     592                 :             :    We do not use guessed profile here, only the measured one.  The guessed
     593                 :             :    profile is usually too flat and unreliable for this (and it is mostly based
     594                 :             :    on the loop structure of the program, so it does not make much sense to
     595                 :             :    derive the loop structure from it).  */
     596                 :             : 
     597                 :             : static edge
     598                 :       81151 : find_subloop_latch_edge_by_profile (vec<edge> latches)
     599                 :             : {
     600                 :       81151 :   unsigned i;
     601                 :       81151 :   edge e, me = NULL;
     602                 :       81151 :   profile_count mcount = profile_count::zero (), tcount = profile_count::zero ();
     603                 :             : 
     604                 :      291762 :   FOR_EACH_VEC_ELT (latches, i, e)
     605                 :             :     {
     606                 :      210611 :       if (e->count ()> mcount)
     607                 :             :         {
     608                 :       71783 :           me = e;
     609                 :       71783 :           mcount = e->count();
     610                 :             :         }
     611                 :      210611 :       tcount += e->count();
     612                 :             :     }
     613                 :             : 
     614                 :       81151 :   if (!tcount.initialized_p () || !(tcount.ipa () > HEAVY_EDGE_MIN_SAMPLES)
     615                 :       81169 :       || (tcount - mcount) * HEAVY_EDGE_RATIO > tcount)
     616                 :       81139 :     return NULL;
     617                 :             : 
     618                 :          12 :   if (dump_file)
     619                 :           0 :     fprintf (dump_file,
     620                 :             :              "Found latch edge %d -> %d using profile information.\n",
     621                 :           0 :              me->src->index, me->dest->index);
     622                 :             :   return me;
     623                 :             : }
     624                 :             : 
     625                 :             : /* Among LATCHES, guesses a latch edge of LOOP corresponding to subloop, based
     626                 :             :    on the structure of induction variables.  Returns this edge, or NULL if we
     627                 :             :    do not find any.
     628                 :             : 
     629                 :             :    We are quite conservative, and look just for an obvious simple innermost
     630                 :             :    loop (which is the case where we would lose the most performance by not
     631                 :             :    disambiguating the loop).  More precisely, we look for the following
     632                 :             :    situation: The source of the chosen latch edge dominates sources of all
     633                 :             :    the other latch edges.  Additionally, the header does not contain a phi node
     634                 :             :    such that the argument from the chosen edge is equal to the argument from
     635                 :             :    another edge.  */
     636                 :             : 
     637                 :             : static edge
     638                 :       80182 : find_subloop_latch_edge_by_ivs (class loop *loop ATTRIBUTE_UNUSED, vec<edge> latches)
     639                 :             : {
     640                 :       80182 :   edge e, latch = latches[0];
     641                 :       80182 :   unsigned i;
     642                 :       80182 :   gphi *phi;
     643                 :       80182 :   gphi_iterator psi;
     644                 :       80182 :   tree lop;
     645                 :       80182 :   basic_block bb;
     646                 :             : 
     647                 :             :   /* Find the candidate for the latch edge.  */
     648                 :      208483 :   for (i = 1; latches.iterate (i, &e); i++)
     649                 :      128301 :     if (dominated_by_p (CDI_DOMINATORS, latch->src, e->src))
     650                 :       35485 :       latch = e;
     651                 :             : 
     652                 :             :   /* Verify that it dominates all the latch edges.  */
     653                 :      217997 :   FOR_EACH_VEC_ELT (latches, i, e)
     654                 :      180490 :     if (!dominated_by_p (CDI_DOMINATORS, e->src, latch->src))
     655                 :             :       return NULL;
     656                 :             : 
     657                 :             :   /* Check for a phi node that would deny that this is a latch edge of
     658                 :             :      a subloop.  */
     659                 :       72476 :   for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
     660                 :             :     {
     661                 :       65465 :       phi = psi.phi ();
     662                 :       65465 :       lop = PHI_ARG_DEF_FROM_EDGE (phi, latch);
     663                 :             : 
     664                 :             :       /* Ignore the values that are not changed inside the subloop.  */
     665                 :       84768 :       if (TREE_CODE (lop) != SSA_NAME
     666                 :       65465 :           || SSA_NAME_DEF_STMT (lop) == phi)
     667                 :       19303 :         continue;
     668                 :       46162 :       bb = gimple_bb (SSA_NAME_DEF_STMT (lop));
     669                 :       46162 :       if (!bb || !flow_bb_inside_loop_p (loop, bb))
     670                 :          22 :         continue;
     671                 :             : 
     672                 :      108468 :       FOR_EACH_VEC_ELT (latches, i, e)
     673                 :       73499 :         if (e != latch
     674                 :       73499 :             && PHI_ARG_DEF_FROM_EDGE (phi, e) == lop)
     675                 :             :           return NULL;
     676                 :             :     }
     677                 :             : 
     678                 :        7011 :   if (dump_file)
     679                 :           2 :     fprintf (dump_file,
     680                 :             :              "Found latch edge %d -> %d using iv structure.\n",
     681                 :           2 :              latch->src->index, latch->dest->index);
     682                 :             :   return latch;
     683                 :             : }
     684                 :             : 
     685                 :             : /* If we can determine that one of the several latch edges of LOOP behaves
     686                 :             :    as a latch edge of a separate subloop, returns this edge.  Otherwise
     687                 :             :    returns NULL.  */
     688                 :             : 
     689                 :             : static edge
     690                 :       89776 : find_subloop_latch_edge (class loop *loop)
     691                 :             : {
     692                 :       89776 :   vec<edge> latches = get_loop_latch_edges (loop);
     693                 :       89776 :   edge latch = NULL;
     694                 :             : 
     695                 :       89776 :   if (latches.length () > 1)
     696                 :             :     {
     697                 :       81151 :       latch = find_subloop_latch_edge_by_profile (latches);
     698                 :             : 
     699                 :       81151 :       if (!latch
     700                 :             :           /* We consider ivs to guess the latch edge only in SSA.  Perhaps we
     701                 :             :              should use cfghook for this, but it is hard to imagine it would
     702                 :             :              be useful elsewhere.  */
     703                 :       81151 :           && current_ir_type () == IR_GIMPLE)
     704                 :       80182 :         latch = find_subloop_latch_edge_by_ivs (loop, latches);
     705                 :             :     }
     706                 :             : 
     707                 :       89776 :   latches.release ();
     708                 :       89776 :   return latch;
     709                 :             : }
     710                 :             : 
     711                 :             : /* Callback for make_forwarder_block.  Returns true if the edge E is marked
     712                 :             :    in the set MFB_REIS_SET.  */
     713                 :             : 
     714                 :             : static hash_set<edge> *mfb_reis_set;
     715                 :             : static bool
     716                 :      297738 : mfb_redirect_edges_in_set (edge e)
     717                 :             : {
     718                 :      297738 :   return mfb_reis_set->contains (e);
     719                 :             : }
     720                 :             : 
     721                 :             : /* Creates a subloop of LOOP with latch edge LATCH.  */
     722                 :             : 
     723                 :             : static void
     724                 :        7023 : form_subloop (class loop *loop, edge latch)
     725                 :             : {
     726                 :        7023 :   edge_iterator ei;
     727                 :        7023 :   edge e, new_entry;
     728                 :        7023 :   class loop *new_loop;
     729                 :             : 
     730                 :        7023 :   mfb_reis_set = new hash_set<edge>;
     731                 :       29181 :   FOR_EACH_EDGE (e, ei, loop->header->preds)
     732                 :             :     {
     733                 :       22158 :       if (e != latch)
     734                 :       15135 :         mfb_reis_set->add (e);
     735                 :             :     }
     736                 :        7023 :   new_entry = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
     737                 :             :                                     NULL);
     738                 :       14046 :   delete mfb_reis_set;
     739                 :             : 
     740                 :        7023 :   loop->header = new_entry->src;
     741                 :             : 
     742                 :             :   /* Find the blocks and subloops that belong to the new loop, and add it to
     743                 :             :      the appropriate place in the loop tree.  */
     744                 :        7023 :   new_loop = alloc_loop ();
     745                 :        7023 :   new_loop->header = new_entry->dest;
     746                 :        7023 :   new_loop->latch = latch->src;
     747                 :        7023 :   add_loop (new_loop, loop);
     748                 :        7023 : }
     749                 :             : 
     750                 :             : /* Make all the latch edges of LOOP to go to a single forwarder block --
     751                 :             :    a new latch of LOOP.  */
     752                 :             : 
     753                 :             : static void
     754                 :       82753 : merge_latch_edges (class loop *loop)
     755                 :             : {
     756                 :       82753 :   vec<edge> latches = get_loop_latch_edges (loop);
     757                 :       82753 :   edge latch, e;
     758                 :       82753 :   unsigned i;
     759                 :             : 
     760                 :       82753 :   gcc_assert (latches.length () > 0);
     761                 :             : 
     762                 :       82753 :   if (latches.length () == 1)
     763                 :        8625 :     loop->latch = latches[0]->src;
     764                 :             :   else
     765                 :             :     {
     766                 :       74128 :       if (dump_file)
     767                 :          11 :         fprintf (dump_file, "Merged latch edges of loop %d\n", loop->num);
     768                 :             : 
     769                 :       74128 :       mfb_reis_set = new hash_set<edge>;
     770                 :      269706 :       FOR_EACH_VEC_ELT (latches, i, e)
     771                 :      195578 :         mfb_reis_set->add (e);
     772                 :       74128 :       latch = make_forwarder_block (loop->header, mfb_redirect_edges_in_set,
     773                 :             :                                     NULL);
     774                 :      148256 :       delete mfb_reis_set;
     775                 :             : 
     776                 :       74128 :       loop->header = latch->dest;
     777                 :       74128 :       loop->latch = latch->src;
     778                 :             :     }
     779                 :             : 
     780                 :       82753 :   latches.release ();
     781                 :       82753 : }
     782                 :             : 
     783                 :             : /* LOOP may have several latch edges.  Transform it into (possibly several)
     784                 :             :    loops with single latch edge.  */
     785                 :             : 
     786                 :             : static void
     787                 :       82753 : disambiguate_multiple_latches (class loop *loop)
     788                 :             : {
     789                 :       82753 :   edge e;
     790                 :             : 
     791                 :             :   /* We eliminate the multiple latches by splitting the header to the forwarder
     792                 :             :      block F and the rest R, and redirecting the edges.  There are two cases:
     793                 :             : 
     794                 :             :      1) If there is a latch edge E that corresponds to a subloop (we guess
     795                 :             :         that based on profile -- if it is taken much more often than the
     796                 :             :         remaining edges; and on trees, using the information about induction
     797                 :             :         variables of the loops), we redirect E to R, all the remaining edges to
     798                 :             :         F, then rescan the loops and try again for the outer loop.
     799                 :             :      2) If there is no such edge, we redirect all latch edges to F, and the
     800                 :             :         entry edges to R, thus making F the single latch of the loop.  */
     801                 :             : 
     802                 :       82753 :   if (dump_file)
     803                 :          12 :     fprintf (dump_file, "Disambiguating loop %d with multiple latches\n",
     804                 :             :              loop->num);
     805                 :             : 
     806                 :             :   /* During latch merging, we may need to redirect the entry edges to a new
     807                 :             :      block.  This would cause problems if the entry edge was the one from the
     808                 :             :      entry block.  To avoid having to handle this case specially, split
     809                 :             :      such entry edge.  */
     810                 :       82753 :   e = find_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), loop->header);
     811                 :       82753 :   if (e)
     812                 :         995 :     split_edge (e);
     813                 :             : 
     814                 :       96799 :   while (1)
     815                 :             :     {
     816                 :       89776 :       e = find_subloop_latch_edge (loop);
     817                 :       89776 :       if (!e)
     818                 :             :         break;
     819                 :             : 
     820                 :        7023 :       form_subloop (loop, e);
     821                 :             :     }
     822                 :             : 
     823                 :       82753 :   merge_latch_edges (loop);
     824                 :       82753 : }
     825                 :             : 
     826                 :             : /* Split loops with multiple latch edges.  */
     827                 :             : 
     828                 :             : void
     829                 :    26893137 : disambiguate_loops_with_multiple_latches (void)
     830                 :             : {
     831                 :    96146890 :   for (auto loop : loops_list (cfun, 0))
     832                 :             :     {
     833                 :    15467479 :       if (!loop->latch)
     834                 :       82753 :         disambiguate_multiple_latches (loop);
     835                 :    26893137 :     }
     836                 :    26893137 : }
     837                 :             : 
     838                 :             : /* Return nonzero if basic block BB belongs to LOOP.  */
     839                 :             : bool
     840                 :  2819078413 : flow_bb_inside_loop_p (const class loop *loop, const_basic_block bb)
     841                 :             : {
     842                 :  2819078413 :   class loop *source_loop;
     843                 :             : 
     844                 :  2819078413 :   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
     845                 :  2819067592 :       || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
     846                 :             :     return 0;
     847                 :             : 
     848                 :  2802341622 :   source_loop = bb->loop_father;
     849                 :  2802341622 :   return loop == source_loop || flow_loop_nested_p (loop, source_loop);
     850                 :             : }
     851                 :             : 
     852                 :             : /* Enumeration predicate for get_loop_body_with_size.  */
     853                 :             : static bool
     854                 :  1069314579 : glb_enum_p (const_basic_block bb, const void *glb_loop)
     855                 :             : {
     856                 :  1069314579 :   const class loop *const loop = (const class loop *) glb_loop;
     857                 :  1069314579 :   return (bb != loop->header
     858                 :  1069314579 :           && dominated_by_p (CDI_DOMINATORS, bb, loop->header));
     859                 :             : }
     860                 :             : 
     861                 :             : /* Gets basic blocks of a LOOP.  Header is the 0-th block, rest is in dfs
     862                 :             :    order against direction of edges from latch.  Specially, if
     863                 :             :    header != latch, latch is the 1-st block.  LOOP cannot be the fake
     864                 :             :    loop tree root, and its size must be at most MAX_SIZE.  The blocks
     865                 :             :    in the LOOP body are stored to BODY, and the size of the LOOP is
     866                 :             :    returned.  */
     867                 :             : 
     868                 :             : unsigned
     869                 :   188981387 : get_loop_body_with_size (const class loop *loop, basic_block *body,
     870                 :             :                          unsigned max_size)
     871                 :             : {
     872                 :   188981387 :   return dfs_enumerate_from (loop->header, 1, glb_enum_p,
     873                 :   188981387 :                              body, max_size, loop);
     874                 :             : }
     875                 :             : 
     876                 :             : /* Gets basic blocks of a LOOP.  Header is the 0-th block, rest is in dfs
     877                 :             :    order against direction of edges from latch.  Specially, if
     878                 :             :    header != latch, latch is the 1-st block.  */
     879                 :             : 
     880                 :             : basic_block *
     881                 :    18235723 : get_loop_body (const class loop *loop)
     882                 :             : {
     883                 :    18235723 :   basic_block *body, bb;
     884                 :    18235723 :   unsigned tv = 0;
     885                 :             : 
     886                 :    18235723 :   gcc_assert (loop->num_nodes);
     887                 :             : 
     888                 :    18235723 :   body = XNEWVEC (basic_block, loop->num_nodes);
     889                 :             : 
     890                 :    18235723 :   if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
     891                 :             :     {
     892                 :             :       /* There may be blocks unreachable from EXIT_BLOCK, hence we need to
     893                 :             :          special-case the fake loop that contains the whole function.  */
     894                 :        5784 :       gcc_assert (loop->num_nodes == (unsigned) n_basic_blocks_for_fn (cfun));
     895                 :        5784 :       body[tv++] = loop->header;
     896                 :        5784 :       body[tv++] = EXIT_BLOCK_PTR_FOR_FN (cfun);
     897                 :       26175 :       FOR_EACH_BB_FN (bb, cfun)
     898                 :       20391 :         body[tv++] = bb;
     899                 :             :     }
     900                 :             :   else
     901                 :    18229939 :     tv = get_loop_body_with_size (loop, body, loop->num_nodes);
     902                 :             : 
     903                 :    18235723 :   gcc_assert (tv == loop->num_nodes);
     904                 :    18235723 :   return body;
     905                 :             : }
     906                 :             : 
     907                 :             : /* Fills dominance descendants inside LOOP of the basic block BB into
     908                 :             :    array TOVISIT from index *TV.  */
     909                 :             : 
     910                 :             : static void
     911                 :     4927946 : fill_sons_in_loop (const class loop *loop, basic_block bb,
     912                 :             :                    basic_block *tovisit, int *tv)
     913                 :             : {
     914                 :     8104076 :   basic_block son, postpone = NULL;
     915                 :             : 
     916                 :     8104076 :   tovisit[(*tv)++] = bb;
     917                 :     8104076 :   for (son = first_dom_son (CDI_DOMINATORS, bb);
     918                 :    16785630 :        son;
     919                 :     8681554 :        son = next_dom_son (CDI_DOMINATORS, son))
     920                 :             :     {
     921                 :     8681554 :       if (!flow_bb_inside_loop_p (loop, son))
     922                 :     2234712 :         continue;
     923                 :             : 
     924                 :     6446842 :       if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
     925                 :             :         {
     926                 :     3176130 :           postpone = son;
     927                 :     3176130 :           continue;
     928                 :             :         }
     929                 :     3270712 :       fill_sons_in_loop (loop, son, tovisit, tv);
     930                 :             :     }
     931                 :             : 
     932                 :     8104076 :   if (postpone)
     933                 :             :     fill_sons_in_loop (loop, postpone, tovisit, tv);
     934                 :     4927946 : }
     935                 :             : 
     936                 :             : /* Gets body of a LOOP (that must be different from the outermost loop)
     937                 :             :    sorted by dominance relation.  Additionally, if a basic block s dominates
     938                 :             :    the latch, then only blocks dominated by s are be after it.  */
     939                 :             : 
     940                 :             : basic_block *
     941                 :     1657234 : get_loop_body_in_dom_order (const class loop *loop)
     942                 :             : {
     943                 :     1657234 :   basic_block *tovisit;
     944                 :     1657234 :   int tv;
     945                 :             : 
     946                 :     1657234 :   gcc_assert (loop->num_nodes);
     947                 :             : 
     948                 :     1657234 :   tovisit = XNEWVEC (basic_block, loop->num_nodes);
     949                 :             : 
     950                 :     1657234 :   gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
     951                 :             : 
     952                 :     1657234 :   tv = 0;
     953                 :     1657234 :   fill_sons_in_loop (loop, loop->header, tovisit, &tv);
     954                 :             : 
     955                 :     1657234 :   gcc_assert (tv == (int) loop->num_nodes);
     956                 :             : 
     957                 :     1657234 :   return tovisit;
     958                 :             : }
     959                 :             : 
     960                 :             : /* Gets body of a LOOP sorted via provided BB_COMPARATOR.  */
     961                 :             : 
     962                 :             : basic_block *
     963                 :          58 : get_loop_body_in_custom_order (const class loop *loop,
     964                 :             :                                int (*bb_comparator) (const void *, const void *))
     965                 :             : {
     966                 :          58 :   basic_block *bbs = get_loop_body (loop);
     967                 :             : 
     968                 :          58 :   qsort (bbs, loop->num_nodes, sizeof (basic_block), bb_comparator);
     969                 :             : 
     970                 :          58 :   return bbs;
     971                 :             : }
     972                 :             : 
     973                 :             : /* Same as above, but use gcc_sort_r instead of qsort.  */
     974                 :             : 
     975                 :             : basic_block *
     976                 :      123472 : get_loop_body_in_custom_order (const class loop *loop, void *data,
     977                 :             :                                int (*bb_comparator) (const void *, const void *, void *))
     978                 :             : {
     979                 :      123472 :   basic_block *bbs = get_loop_body (loop);
     980                 :             : 
     981                 :      123472 :   gcc_sort_r (bbs, loop->num_nodes, sizeof (basic_block), bb_comparator, data);
     982                 :             : 
     983                 :      123472 :   return bbs;
     984                 :             : }
     985                 :             : 
     986                 :             : /* Get body of a LOOP in breadth first sort order.  */
     987                 :             : 
     988                 :             : basic_block *
     989                 :      346154 : get_loop_body_in_bfs_order (const class loop *loop)
     990                 :             : {
     991                 :      346154 :   basic_block *blocks;
     992                 :      346154 :   basic_block bb;
     993                 :      346154 :   unsigned int i = 1;
     994                 :      346154 :   unsigned int vc = 0;
     995                 :             : 
     996                 :      346154 :   gcc_assert (loop->num_nodes);
     997                 :      346154 :   gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
     998                 :             : 
     999                 :      346154 :   blocks = XNEWVEC (basic_block, loop->num_nodes);
    1000                 :      346154 :   auto_bitmap visited;
    1001                 :      346154 :   blocks[0] = loop->header;
    1002                 :      346154 :   bitmap_set_bit (visited, loop->header->index);
    1003                 :     1880776 :   while (i < loop->num_nodes)
    1004                 :             :     {
    1005                 :     1188468 :       edge e;
    1006                 :     1188468 :       edge_iterator ei;
    1007                 :     1188468 :       gcc_assert (i > vc);
    1008                 :     1188468 :       bb = blocks[vc++];
    1009                 :             : 
    1010                 :     3260832 :       FOR_EACH_EDGE (e, ei, bb->succs)
    1011                 :             :         {
    1012                 :     2072364 :           if (flow_bb_inside_loop_p (loop, e->dest))
    1013                 :             :             {
    1014                 :             :               /* This bb is now visited.  */
    1015                 :     1423196 :               if (bitmap_set_bit (visited, e->dest->index))
    1016                 :     1236120 :                 blocks[i++] = e->dest;
    1017                 :             :             }
    1018                 :             :         }
    1019                 :             :     }
    1020                 :             : 
    1021                 :      346154 :   return blocks;
    1022                 :      346154 : }
    1023                 :             : 
    1024                 :             : /* Hash function for struct loop_exit.  */
    1025                 :             : 
    1026                 :             : hashval_t
    1027                 :   218390694 : loop_exit_hasher::hash (loop_exit *exit)
    1028                 :             : {
    1029                 :   218390694 :   return htab_hash_pointer (exit->e);
    1030                 :             : }
    1031                 :             : 
    1032                 :             : /* Equality function for struct loop_exit.  Compares with edge.  */
    1033                 :             : 
    1034                 :             : bool
    1035                 :   257804867 : loop_exit_hasher::equal (loop_exit *exit, edge e)
    1036                 :             : {
    1037                 :   257804867 :   return exit->e == e;
    1038                 :             : }
    1039                 :             : 
    1040                 :             : /* Frees the list of loop exit descriptions EX.  */
    1041                 :             : 
    1042                 :             : void
    1043                 :    16982354 : loop_exit_hasher::remove (loop_exit *exit)
    1044                 :             : {
    1045                 :    16982354 :   loop_exit *next;
    1046                 :    35373289 :   for (; exit; exit = next)
    1047                 :             :     {
    1048                 :    18390935 :       next = exit->next_e;
    1049                 :             : 
    1050                 :    18390935 :       exit->next->prev = exit->prev;
    1051                 :    18390935 :       exit->prev->next = exit->next;
    1052                 :             : 
    1053                 :    18390935 :       ggc_free (exit);
    1054                 :             :     }
    1055                 :    16982354 : }
    1056                 :             : 
    1057                 :             : /* Returns the list of records for E as an exit of a loop.  */
    1058                 :             : 
    1059                 :             : static struct loop_exit *
    1060                 :    35784095 : get_exit_descriptions (edge e)
    1061                 :             : {
    1062                 :    35784095 :   return current_loops->exits->find_with_hash (e, htab_hash_pointer (e));
    1063                 :             : }
    1064                 :             : 
    1065                 :             : /* Updates the lists of loop exits in that E appears.
    1066                 :             :    If REMOVED is true, E is being removed, and we
    1067                 :             :    just remove it from the lists of exits.
    1068                 :             :    If NEW_EDGE is true and E is not a loop exit, we
    1069                 :             :    do not try to remove it from loop exit lists.  */
    1070                 :             : 
    1071                 :             : void
    1072                 :   485268484 : rescan_loop_exit (edge e, bool new_edge, bool removed)
    1073                 :             : {
    1074                 :   485268484 :   struct loop_exit *exits = NULL, *exit;
    1075                 :   485268484 :   class loop *aloop, *cloop;
    1076                 :             : 
    1077                 :   485268484 :   if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
    1078                 :             :     return;
    1079                 :             : 
    1080                 :   222711423 :   if (!removed
    1081                 :   211133536 :       && e->src->loop_father != NULL
    1082                 :   211133536 :       && e->dest->loop_father != NULL
    1083                 :   432210183 :       && !flow_bb_inside_loop_p (e->src->loop_father, e->dest))
    1084                 :             :     {
    1085                 :    33702651 :       cloop = find_common_loop (e->src->loop_father, e->dest->loop_father);
    1086                 :    33702651 :       for (aloop = e->src->loop_father;
    1087                 :    52093586 :            aloop != cloop;
    1088                 :    18390935 :            aloop = loop_outer (aloop))
    1089                 :             :         {
    1090                 :    18390935 :           exit = ggc_alloc<loop_exit> ();
    1091                 :    18390935 :           exit->e = e;
    1092                 :             : 
    1093                 :    18390935 :           exit->next = aloop->exits->next;
    1094                 :    18390935 :           exit->prev = aloop->exits;
    1095                 :    18390935 :           exit->next->prev = exit;
    1096                 :    18390935 :           exit->prev->next = exit;
    1097                 :             : 
    1098                 :    18390935 :           exit->next_e = exits;
    1099                 :    18390935 :           exits = exit;
    1100                 :             :         }
    1101                 :             :     }
    1102                 :             : 
    1103                 :   222711423 :   if (!exits && new_edge)
    1104                 :             :     return;
    1105                 :             : 
    1106                 :    31264178 :   loop_exit **slot
    1107                 :    45546002 :     = current_loops->exits->find_slot_with_hash (e, htab_hash_pointer (e),
    1108                 :             :                                                  exits ? INSERT : NO_INSERT);
    1109                 :    31264178 :   if (!slot)
    1110                 :             :     return;
    1111                 :             : 
    1112                 :    17953026 :   if (exits)
    1113                 :             :     {
    1114                 :    16982354 :       if (*slot)
    1115                 :      129935 :         loop_exit_hasher::remove (*slot);
    1116                 :    16982354 :       *slot = exits;
    1117                 :             :     }
    1118                 :             :   else
    1119                 :      970672 :     current_loops->exits->clear_slot (slot);
    1120                 :             : }
    1121                 :             : 
    1122                 :             : /* For each loop, record list of exit edges, and start maintaining these
    1123                 :             :    lists.  */
    1124                 :             : 
    1125                 :             : void
    1126                 :    16138189 : record_loop_exits (void)
    1127                 :             : {
    1128                 :    16138189 :   basic_block bb;
    1129                 :    16138189 :   edge_iterator ei;
    1130                 :    16138189 :   edge e;
    1131                 :             : 
    1132                 :    16138189 :   if (!current_loops)
    1133                 :           0 :     return;
    1134                 :             : 
    1135                 :    16138189 :   if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
    1136                 :             :     return;
    1137                 :    16138189 :   loops_state_set (LOOPS_HAVE_RECORDED_EXITS);
    1138                 :             : 
    1139                 :    16138189 :   gcc_assert (current_loops->exits == NULL);
    1140                 :    16138189 :   current_loops->exits
    1141                 :    32276378 :     = hash_table<loop_exit_hasher>::create_ggc (2 * number_of_loops (cfun));
    1142                 :             : 
    1143                 :   156231434 :   FOR_EACH_BB_FN (bb, cfun)
    1144                 :             :     {
    1145                 :   336766158 :       FOR_EACH_EDGE (e, ei, bb->succs)
    1146                 :             :         {
    1147                 :   196672913 :           rescan_loop_exit (e, true, false);
    1148                 :             :         }
    1149                 :             :     }
    1150                 :             : }
    1151                 :             : 
    1152                 :             : /* Dumps information about the exit in *SLOT to FILE.
    1153                 :             :    Callback for htab_traverse.  */
    1154                 :             : 
    1155                 :             : int
    1156                 :           0 : dump_recorded_exit (loop_exit **slot, FILE *file)
    1157                 :             : {
    1158                 :           0 :   struct loop_exit *exit = *slot;
    1159                 :           0 :   unsigned n = 0;
    1160                 :           0 :   edge e = exit->e;
    1161                 :             : 
    1162                 :           0 :   for (; exit != NULL; exit = exit->next_e)
    1163                 :           0 :     n++;
    1164                 :             : 
    1165                 :           0 :   fprintf (file, "Edge %d->%d exits %u loops\n",
    1166                 :           0 :            e->src->index, e->dest->index, n);
    1167                 :             : 
    1168                 :           0 :   return 1;
    1169                 :             : }
    1170                 :             : 
    1171                 :             : /* Dumps the recorded exits of loops to FILE.  */
    1172                 :             : 
    1173                 :             : extern void dump_recorded_exits (FILE *);
    1174                 :             : void
    1175                 :           0 : dump_recorded_exits (FILE *file)
    1176                 :             : {
    1177                 :           0 :   if (!current_loops->exits)
    1178                 :             :     return;
    1179                 :           0 :   current_loops->exits->traverse<FILE *, dump_recorded_exit> (file);
    1180                 :             : }
    1181                 :             : 
    1182                 :             : /* Releases lists of loop exits.  */
    1183                 :             : 
    1184                 :             : void
    1185                 :    16138189 : release_recorded_exits (function *fn)
    1186                 :             : {
    1187                 :    16138189 :   gcc_assert (loops_state_satisfies_p (fn, LOOPS_HAVE_RECORDED_EXITS));
    1188                 :    16138189 :   loops_for_fn (fn)->exits->empty ();
    1189                 :    16138189 :   loops_for_fn (fn)->exits = NULL;
    1190                 :    16138189 :   loops_state_clear (fn, LOOPS_HAVE_RECORDED_EXITS);
    1191                 :    16138189 : }
    1192                 :             : 
    1193                 :             : /* Returns the list of the exit edges of a LOOP.  */
    1194                 :             : 
    1195                 :             : auto_vec<edge>
    1196                 :    34592095 : get_loop_exit_edges (const class loop *loop, basic_block *body)
    1197                 :             : {
    1198                 :    34592095 :   auto_vec<edge> edges;
    1199                 :    34592095 :   edge e;
    1200                 :    34592095 :   unsigned i;
    1201                 :    34592095 :   edge_iterator ei;
    1202                 :    34592095 :   struct loop_exit *exit;
    1203                 :             : 
    1204                 :    34592095 :   gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
    1205                 :             : 
    1206                 :             :   /* If we maintain the lists of exits, use them.  Otherwise we must
    1207                 :             :      scan the body of the loop.  */
    1208                 :    34592095 :   if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
    1209                 :             :     {
    1210                 :   105951416 :       for (exit = loop->exits->next; exit->e; exit = exit->next)
    1211                 :    71948923 :         edges.safe_push (exit->e);
    1212                 :             :     }
    1213                 :             :   else
    1214                 :             :     {
    1215                 :      589602 :       bool body_from_caller = true;
    1216                 :      589602 :       if (!body)
    1217                 :             :         {
    1218                 :      584571 :           body = get_loop_body (loop);
    1219                 :      584571 :           body_from_caller = false;
    1220                 :             :         }
    1221                 :     4298288 :       for (i = 0; i < loop->num_nodes; i++)
    1222                 :     9716721 :         FOR_EACH_EDGE (e, ei, body[i]->succs)
    1223                 :             :           {
    1224                 :     6008035 :             if (!flow_bb_inside_loop_p (loop, e->dest))
    1225                 :     1499543 :               edges.safe_push (e);
    1226                 :             :           }
    1227                 :      589602 :       if (!body_from_caller)
    1228                 :      584571 :         free (body);
    1229                 :             :     }
    1230                 :             : 
    1231                 :    34592095 :   return edges;
    1232                 :             : }
    1233                 :             : 
    1234                 :             : /* Counts the number of conditional branches inside LOOP.  */
    1235                 :             : 
    1236                 :             : unsigned
    1237                 :          47 : num_loop_branches (const class loop *loop)
    1238                 :             : {
    1239                 :          47 :   unsigned i, n;
    1240                 :          47 :   basic_block * body;
    1241                 :             : 
    1242                 :          47 :   gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
    1243                 :             : 
    1244                 :          47 :   body = get_loop_body (loop);
    1245                 :          47 :   n = 0;
    1246                 :         189 :   for (i = 0; i < loop->num_nodes; i++)
    1247                 :         135 :     if (EDGE_COUNT (body[i]->succs) >= 2)
    1248                 :          40 :       n++;
    1249                 :          47 :   free (body);
    1250                 :             : 
    1251                 :          47 :   return n;
    1252                 :             : }
    1253                 :             : 
    1254                 :             : /* Adds basic block BB to LOOP.  */
    1255                 :             : void
    1256                 :    39976783 : add_bb_to_loop (basic_block bb, class loop *loop)
    1257                 :             : {
    1258                 :    39976783 :   unsigned i;
    1259                 :    39976783 :   loop_p ploop;
    1260                 :    39976783 :   edge_iterator ei;
    1261                 :    39976783 :   edge e;
    1262                 :             : 
    1263                 :    39976783 :   gcc_assert (bb->loop_father == NULL);
    1264                 :    39976783 :   bb->loop_father = loop;
    1265                 :    39976783 :   loop->num_nodes++;
    1266                 :    57404309 :   FOR_EACH_VEC_SAFE_ELT (loop->superloops, i, ploop)
    1267                 :    17427526 :     ploop->num_nodes++;
    1268                 :             : 
    1269                 :    82793177 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1270                 :             :     {
    1271                 :    42816394 :       rescan_loop_exit (e, true, false);
    1272                 :             :     }
    1273                 :    67515043 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1274                 :             :     {
    1275                 :    27538260 :       rescan_loop_exit (e, true, false);
    1276                 :             :     }
    1277                 :    39976783 : }
    1278                 :             : 
    1279                 :             : /* Remove basic block BB from loops.  */
    1280                 :             : void
    1281                 :    49398887 : remove_bb_from_loops (basic_block bb)
    1282                 :             : {
    1283                 :    49398887 :   unsigned i;
    1284                 :    49398887 :   class loop *loop = bb->loop_father;
    1285                 :    49398887 :   loop_p ploop;
    1286                 :    49398887 :   edge_iterator ei;
    1287                 :    49398887 :   edge e;
    1288                 :             : 
    1289                 :    49398887 :   gcc_assert (loop != NULL);
    1290                 :    49398887 :   loop->num_nodes--;
    1291                 :    67875103 :   FOR_EACH_VEC_SAFE_ELT (loop->superloops, i, ploop)
    1292                 :    18476216 :     ploop->num_nodes--;
    1293                 :    49398887 :   bb->loop_father = NULL;
    1294                 :             : 
    1295                 :    73415453 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1296                 :             :     {
    1297                 :    24016566 :       rescan_loop_exit (e, false, true);
    1298                 :             :     }
    1299                 :    66201475 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1300                 :             :     {
    1301                 :    16802588 :       rescan_loop_exit (e, false, true);
    1302                 :             :     }
    1303                 :    49398887 : }
    1304                 :             : 
    1305                 :             : /* Finds nearest common ancestor in loop tree for given loops.  */
    1306                 :             : class loop *
    1307                 :   182861965 : find_common_loop (class loop *loop_s, class loop *loop_d)
    1308                 :             : {
    1309                 :   182861965 :   unsigned sdepth, ddepth;
    1310                 :             : 
    1311                 :   182861965 :   if (!loop_s) return loop_d;
    1312                 :   182861408 :   if (!loop_d) return loop_s;
    1313                 :             : 
    1314                 :   182861408 :   sdepth = loop_depth (loop_s);
    1315                 :   182861408 :   ddepth = loop_depth (loop_d);
    1316                 :             : 
    1317                 :    71954414 :   if (sdepth < ddepth)
    1318                 :     5739466 :     loop_d = (*loop_d->superloops)[sdepth];
    1319                 :   177121942 :   else if (sdepth > ddepth)
    1320                 :    88967661 :     loop_s = (*loop_s->superloops)[ddepth];
    1321                 :             : 
    1322                 :   183658655 :   while (loop_s != loop_d)
    1323                 :             :     {
    1324                 :      797247 :       loop_s = loop_outer (loop_s);
    1325                 :      797247 :       loop_d = loop_outer (loop_d);
    1326                 :             :     }
    1327                 :             :   return loop_s;
    1328                 :             : }
    1329                 :             : 
    1330                 :             : /* Removes LOOP from structures and frees its data.  */
    1331                 :             : 
    1332                 :             : void
    1333                 :      117302 : delete_loop (class loop *loop)
    1334                 :             : {
    1335                 :             :   /* Remove the loop from structure.  */
    1336                 :      117302 :   flow_loop_tree_node_remove (loop);
    1337                 :             : 
    1338                 :             :   /* Remove loop from loops array.  */
    1339                 :      117302 :   (*current_loops->larray)[loop->num] = NULL;
    1340                 :             : 
    1341                 :             :   /* Free loop data.  */
    1342                 :      117302 :   flow_loop_free (loop);
    1343                 :      117302 : }
    1344                 :             : 
    1345                 :             : /* Cancels the LOOP; it must be innermost one.  */
    1346                 :             : 
    1347                 :             : static void
    1348                 :        9638 : cancel_loop (class loop *loop)
    1349                 :             : {
    1350                 :        9638 :   basic_block *bbs;
    1351                 :        9638 :   unsigned i;
    1352                 :        9638 :   class loop *outer = loop_outer (loop);
    1353                 :             : 
    1354                 :        9638 :   gcc_assert (!loop->inner);
    1355                 :             : 
    1356                 :             :   /* Move blocks up one level (they should be removed as soon as possible).  */
    1357                 :        9638 :   bbs = get_loop_body (loop);
    1358                 :       40072 :   for (i = 0; i < loop->num_nodes; i++)
    1359                 :       20796 :     bbs[i]->loop_father = outer;
    1360                 :             : 
    1361                 :        9638 :   free (bbs);
    1362                 :        9638 :   delete_loop (loop);
    1363                 :        9638 : }
    1364                 :             : 
    1365                 :             : /* Cancels LOOP and all its subloops.  */
    1366                 :             : void
    1367                 :        9638 : cancel_loop_tree (class loop *loop)
    1368                 :             : {
    1369                 :       10129 :   while (loop->inner)
    1370                 :         491 :     cancel_loop_tree (loop->inner);
    1371                 :        9638 :   cancel_loop (loop);
    1372                 :        9638 : }
    1373                 :             : 
    1374                 :             : /* Disable warnings about missing quoting in GCC diagnostics for
    1375                 :             :    the verification errors.  Their format strings don't follow GCC
    1376                 :             :    diagnostic conventions and the calls are ultimately followed by
    1377                 :             :    a deliberate ICE triggered by a failed assertion.  */
    1378                 :             : #if __GNUC__ >= 10
    1379                 :             : #  pragma GCC diagnostic push
    1380                 :             : #  pragma GCC diagnostic ignored "-Wformat-diag"
    1381                 :             : #endif
    1382                 :             : 
    1383                 :             : /* Checks that information about loops is correct
    1384                 :             :      -- sizes of loops are all right
    1385                 :             :      -- results of get_loop_body really belong to the loop
    1386                 :             :      -- loop header have just single entry edge and single latch edge
    1387                 :             :      -- loop latches have only single successor that is header of their loop
    1388                 :             :      -- irreducible loops are correctly marked
    1389                 :             :      -- the cached loop depth and loop father of each bb is correct
    1390                 :             :   */
    1391                 :             : DEBUG_FUNCTION void
    1392                 :   345320047 : verify_loop_structure (void)
    1393                 :             : {
    1394                 :   345320047 :   unsigned *sizes, i, j;
    1395                 :   345320047 :   basic_block bb, *bbs;
    1396                 :   345320047 :   int err = 0;
    1397                 :   345320047 :   edge e;
    1398                 :   345320047 :   unsigned num = number_of_loops (cfun);
    1399                 :   345320047 :   struct loop_exit *exit, *mexit;
    1400                 :   345320047 :   bool dom_available = dom_info_available_p (CDI_DOMINATORS);
    1401                 :             : 
    1402                 :   345320047 :   if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
    1403                 :             :     {
    1404                 :           0 :       error ("loop verification on loop tree that needs fixup");
    1405                 :           0 :       err = 1;
    1406                 :             :     }
    1407                 :             : 
    1408                 :             :   /* We need up-to-date dominators, compute or verify them.  */
    1409                 :   345320047 :   if (!dom_available)
    1410                 :    93380411 :     calculate_dominance_info (CDI_DOMINATORS);
    1411                 :             :   else
    1412                 :   251939636 :     verify_dominators (CDI_DOMINATORS);
    1413                 :             : 
    1414                 :             :   /* Check the loop tree root.  */
    1415                 :   345320047 :   if (current_loops->tree_root->header != ENTRY_BLOCK_PTR_FOR_FN (cfun)
    1416                 :   345320047 :       || current_loops->tree_root->latch != EXIT_BLOCK_PTR_FOR_FN (cfun)
    1417                 :   345320047 :       || (current_loops->tree_root->num_nodes
    1418                 :   345320047 :           != (unsigned) n_basic_blocks_for_fn (cfun)))
    1419                 :             :     {
    1420                 :           0 :       error ("corrupt loop tree root");
    1421                 :           0 :       err = 1;
    1422                 :             :     }
    1423                 :             : 
    1424                 :             :   /* Check the headers.  */
    1425                 :  3340045952 :   FOR_EACH_BB_FN (bb, cfun)
    1426                 :  2994725905 :     if (bb_loop_header_p (bb))
    1427                 :             :       {
    1428                 :   170449599 :         if (bb->loop_father->header == NULL)
    1429                 :             :           {
    1430                 :           0 :             error ("loop with header %d marked for removal", bb->index);
    1431                 :           0 :             err = 1;
    1432                 :             :           }
    1433                 :   170449599 :         else if (bb->loop_father->header != bb)
    1434                 :             :           {
    1435                 :           0 :             error ("loop with header %d not in loop tree", bb->index);
    1436                 :           0 :             err = 1;
    1437                 :             :           }
    1438                 :             :       }
    1439                 :  2824276306 :     else if (bb->loop_father->header == bb)
    1440                 :             :       {
    1441                 :           0 :         error ("non-loop with header %d not marked for removal", bb->index);
    1442                 :           0 :         err = 1;
    1443                 :             :       }
    1444                 :             : 
    1445                 :             :   /* Check the recorded loop father and sizes of loops.  */
    1446                 :   345320047 :   auto_sbitmap visited (last_basic_block_for_fn (cfun));
    1447                 :   345320047 :   bitmap_clear (visited);
    1448                 :   345320047 :   bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
    1449                 :  1206409740 :   for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
    1450                 :             :     {
    1451                 :   170449599 :       unsigned n;
    1452                 :             : 
    1453                 :   170449599 :       if (loop->header == NULL)
    1454                 :             :         {
    1455                 :           0 :           error ("removed loop %d in loop tree", loop->num);
    1456                 :           0 :           err = 1;
    1457                 :           0 :           continue;
    1458                 :             :         }
    1459                 :             : 
    1460                 :   170449599 :       n = get_loop_body_with_size (loop, bbs, n_basic_blocks_for_fn (cfun));
    1461                 :   170449599 :       if (loop->num_nodes != n)
    1462                 :             :         {
    1463                 :           0 :           error ("size of loop %d should be %d, not %d",
    1464                 :             :                  loop->num, n, loop->num_nodes);
    1465                 :           0 :           err = 1;
    1466                 :             :         }
    1467                 :             : 
    1468                 :  1140987800 :       for (j = 0; j < n; j++)
    1469                 :             :         {
    1470                 :   970538201 :           bb = bbs[j];
    1471                 :             : 
    1472                 :   970538201 :           if (!flow_bb_inside_loop_p (loop, bb))
    1473                 :             :             {
    1474                 :           0 :               error ("bb %d does not belong to loop %d",
    1475                 :             :                      bb->index, loop->num);
    1476                 :           0 :               err = 1;
    1477                 :             :             }
    1478                 :             : 
    1479                 :             :           /* Ignore this block if it is in an inner loop.  */
    1480                 :   970538201 :           if (bitmap_bit_p (visited, bb->index))
    1481                 :   222228983 :             continue;
    1482                 :   748309218 :           bitmap_set_bit (visited, bb->index);
    1483                 :             : 
    1484                 :   748309218 :           if (bb->loop_father != loop)
    1485                 :             :             {
    1486                 :           0 :               error ("bb %d has father loop %d, should be loop %d",
    1487                 :             :                      bb->index, bb->loop_father->num, loop->num);
    1488                 :           0 :               err = 1;
    1489                 :             :             }
    1490                 :             :         }
    1491                 :   345320047 :     }
    1492                 :   345320047 :   free (bbs);
    1493                 :             : 
    1494                 :             :   /* Check headers and latches.  */
    1495                 :  1206409740 :   for (auto loop : loops_list (cfun, 0))
    1496                 :             :     {
    1497                 :   170449599 :       i = loop->num;
    1498                 :   170449599 :       if (loop->header == NULL)
    1499                 :           0 :         continue;
    1500                 :   170449599 :       if (!bb_loop_header_p (loop->header))
    1501                 :             :         {
    1502                 :           0 :           error ("loop %d%'s header is not a loop header", i);
    1503                 :           0 :           err = 1;
    1504                 :             :         }
    1505                 :   170449599 :       if (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS)
    1506                 :   170449599 :           && EDGE_COUNT (loop->header->preds) != 2)
    1507                 :             :         {
    1508                 :           0 :           error ("loop %d%'s header does not have exactly 2 entries", i);
    1509                 :           0 :           err = 1;
    1510                 :             :         }
    1511                 :   170449599 :       if (loop->latch)
    1512                 :             :         {
    1513                 :   169588517 :           if (!find_edge (loop->latch, loop->header))
    1514                 :             :             {
    1515                 :           0 :               error ("loop %d%'s latch does not have an edge to its header", i);
    1516                 :           0 :               err = 1;
    1517                 :             :             }
    1518                 :   169588517 :           if (!dominated_by_p (CDI_DOMINATORS, loop->latch, loop->header))
    1519                 :             :             {
    1520                 :           0 :               error ("loop %d%'s latch is not dominated by its header", i);
    1521                 :           0 :               err = 1;
    1522                 :             :             }
    1523                 :             :         }
    1524                 :   170449599 :       if (loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
    1525                 :             :         {
    1526                 :    50381898 :           if (!single_succ_p (loop->latch))
    1527                 :             :             {
    1528                 :           0 :               error ("loop %d%'s latch does not have exactly 1 successor", i);
    1529                 :           0 :               err = 1;
    1530                 :             :             }
    1531                 :    25190949 :           if (single_succ (loop->latch) != loop->header)
    1532                 :             :             {
    1533                 :           0 :               error ("loop %d%'s latch does not have header as successor", i);
    1534                 :           0 :               err = 1;
    1535                 :             :             }
    1536                 :    25190949 :           if (loop->latch->loop_father != loop)
    1537                 :             :             {
    1538                 :           0 :               error ("loop %d%'s latch does not belong directly to it", i);
    1539                 :           0 :               err = 1;
    1540                 :             :             }
    1541                 :             :         }
    1542                 :   170449599 :       if (loop->header->loop_father != loop)
    1543                 :             :         {
    1544                 :           0 :           error ("loop %d%'s header does not belong directly to it", i);
    1545                 :           0 :           err = 1;
    1546                 :             :         }
    1547                 :   170449599 :       if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
    1548                 :             :         {
    1549                 :    22297522 :           edge_iterator ei;
    1550                 :    66892720 :           FOR_EACH_EDGE (e, ei, loop->header->preds)
    1551                 :    44595198 :             if (dominated_by_p (CDI_DOMINATORS, e->src, loop->header)
    1552                 :    44595198 :                 && e->flags & EDGE_IRREDUCIBLE_LOOP)
    1553                 :             :               {
    1554                 :           0 :                 error ("loop %d%'s latch is marked as part of irreducible"
    1555                 :             :                        " region", i);
    1556                 :           0 :                 err = 1;
    1557                 :             :               }
    1558                 :             :         }
    1559                 :             : 
    1560                 :             :       /* Check cached number of iterations for released SSA names.  */
    1561                 :   170449599 :       tree ref;
    1562                 :   170449599 :       if (loop->nb_iterations
    1563                 :   170449599 :           && (ref = walk_tree (&loop->nb_iterations,
    1564                 :             :                                find_released_ssa_name, NULL, NULL)))
    1565                 :             :         {
    1566                 :           0 :           error ("loop %d%'s number of iterations %qE references the"
    1567                 :             :                  " released SSA name %qE", i, loop->nb_iterations, ref);
    1568                 :           0 :           err = 1;
    1569                 :             :         }
    1570                 :   345320047 :     }
    1571                 :             : 
    1572                 :             :   /* Check irreducible loops.  */
    1573                 :   345320047 :   if (loops_state_satisfies_p (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS))
    1574                 :             :     {
    1575                 :    31141500 :       auto_edge_flag saved_edge_irr (cfun);
    1576                 :    31141500 :       auto_bb_flag saved_bb_irr (cfun);
    1577                 :             :       /* Save old info.  */
    1578                 :   382288964 :       FOR_EACH_BB_FN (bb, cfun)
    1579                 :             :         {
    1580                 :   351147464 :           edge_iterator ei;
    1581                 :   351147464 :           if (bb->flags & BB_IRREDUCIBLE_LOOP)
    1582                 :      488829 :             bb->flags |= saved_bb_irr;
    1583                 :   842114666 :           FOR_EACH_EDGE (e, ei, bb->succs)
    1584                 :   490967202 :             if (e->flags & EDGE_IRREDUCIBLE_LOOP)
    1585                 :      684778 :               e->flags |= saved_edge_irr;
    1586                 :             :         }
    1587                 :             : 
    1588                 :             :       /* Recount it.  */
    1589                 :    31141500 :       mark_irreducible_loops ();
    1590                 :             : 
    1591                 :             :       /* Compare.  */
    1592                 :   382288964 :       FOR_EACH_BB_FN (bb, cfun)
    1593                 :             :         {
    1594                 :   351147464 :           edge_iterator ei;
    1595                 :             : 
    1596                 :   351147464 :           if ((bb->flags & BB_IRREDUCIBLE_LOOP)
    1597                 :   351147464 :               && !(bb->flags & saved_bb_irr))
    1598                 :             :             {
    1599                 :           0 :               error ("basic block %d should be marked irreducible", bb->index);
    1600                 :           0 :               err = 1;
    1601                 :             :             }
    1602                 :   351147464 :           else if (!(bb->flags & BB_IRREDUCIBLE_LOOP)
    1603                 :   351147464 :                    && (bb->flags & saved_bb_irr))
    1604                 :             :             {
    1605                 :           0 :               error ("basic block %d should not be marked irreducible", bb->index);
    1606                 :           0 :               err = 1;
    1607                 :             :             }
    1608                 :   351147464 :           bb->flags &= ~saved_bb_irr;
    1609                 :   842114666 :           FOR_EACH_EDGE (e, ei, bb->succs)
    1610                 :             :             {
    1611                 :   490967202 :               if ((e->flags & EDGE_IRREDUCIBLE_LOOP)
    1612                 :   490967202 :                   && !(e->flags & saved_edge_irr))
    1613                 :             :                 {
    1614                 :           0 :                   error ("edge from %d to %d should be marked irreducible",
    1615                 :           0 :                          e->src->index, e->dest->index);
    1616                 :           0 :                   err = 1;
    1617                 :             :                 }
    1618                 :   490967202 :               else if (!(e->flags & EDGE_IRREDUCIBLE_LOOP)
    1619                 :   490967202 :                        && (e->flags & saved_edge_irr))
    1620                 :             :                 {
    1621                 :           0 :                   error ("edge from %d to %d should not be marked irreducible",
    1622                 :           0 :                          e->src->index, e->dest->index);
    1623                 :           0 :                   err = 1;
    1624                 :             :                 }
    1625                 :   490967202 :               e->flags &= ~saved_edge_irr;
    1626                 :             :             }
    1627                 :             :         }
    1628                 :    31141500 :     }
    1629                 :             : 
    1630                 :             :   /* Check the recorded loop exits.  */
    1631                 :  1206409740 :   for (auto loop : loops_list (cfun, 0))
    1632                 :             :     {
    1633                 :   170449599 :       if (!loop->exits || loop->exits->e != NULL)
    1634                 :             :         {
    1635                 :           0 :           error ("corrupted head of the exits list of loop %d",
    1636                 :             :                  loop->num);
    1637                 :           0 :           err = 1;
    1638                 :             :         }
    1639                 :             :       else
    1640                 :             :         {
    1641                 :             :           /* Check that the list forms a cycle, and all elements except
    1642                 :             :              for the head are nonnull.  */
    1643                 :   170449599 :           for (mexit = loop->exits, exit = mexit->next, i = 0;
    1644                 :   209319940 :                exit->e && exit != mexit;
    1645                 :    38870341 :                exit = exit->next)
    1646                 :             :             {
    1647                 :    38870341 :               if (i++ & 1)
    1648                 :    12068968 :                 mexit = mexit->next;
    1649                 :             :             }
    1650                 :             : 
    1651                 :   170449599 :           if (exit != loop->exits)
    1652                 :             :             {
    1653                 :           0 :               error ("corrupted exits list of loop %d", loop->num);
    1654                 :           0 :               err = 1;
    1655                 :             :             }
    1656                 :             :         }
    1657                 :             : 
    1658                 :   170449599 :       if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
    1659                 :             :         {
    1660                 :   150666284 :           if (loop->exits->next != loop->exits)
    1661                 :             :             {
    1662                 :           0 :               error ("nonempty exits list of loop %d, but exits are not recorded",
    1663                 :             :                      loop->num);
    1664                 :           0 :               err = 1;
    1665                 :             :             }
    1666                 :             :         }
    1667                 :   345320047 :     }
    1668                 :             : 
    1669                 :   345320047 :   if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
    1670                 :             :     {
    1671                 :    23605341 :       unsigned n_exits = 0, eloops;
    1672                 :             : 
    1673                 :    23605341 :       sizes = XCNEWVEC (unsigned, num);
    1674                 :    23605341 :       memset (sizes, 0, sizeof (unsigned) * num);
    1675                 :   318856314 :       FOR_EACH_BB_FN (bb, cfun)
    1676                 :             :         {
    1677                 :   295250973 :           edge_iterator ei;
    1678                 :   295250973 :           if (bb->loop_father == current_loops->tree_root)
    1679                 :   200596714 :             continue;
    1680                 :   242583855 :           FOR_EACH_EDGE (e, ei, bb->succs)
    1681                 :             :             {
    1682                 :   147929596 :               if (flow_bb_inside_loop_p (bb->loop_father, e->dest))
    1683                 :   112145501 :                 continue;
    1684                 :             : 
    1685                 :    35784095 :               n_exits++;
    1686                 :    35784095 :               exit = get_exit_descriptions (e);
    1687                 :    35784095 :               if (!exit)
    1688                 :             :                 {
    1689                 :           0 :                   error ("exit %d->%d not recorded",
    1690                 :           0 :                          e->src->index, e->dest->index);
    1691                 :           0 :                   err = 1;
    1692                 :             :                 }
    1693                 :    35784095 :               eloops = 0;
    1694                 :    74654436 :               for (; exit; exit = exit->next_e)
    1695                 :    38870341 :                 eloops++;
    1696                 :             : 
    1697                 :    35784095 :               for (class loop *loop = bb->loop_father;
    1698                 :    74654436 :                    loop != e->dest->loop_father
    1699                 :             :                    /* When a loop exit is also an entry edge which
    1700                 :             :                       can happen when avoiding CFG manipulations
    1701                 :             :                       then the last loop exited is the outer loop
    1702                 :             :                       of the loop entered.  */
    1703                 :    74654436 :                    && loop != loop_outer (e->dest->loop_father);
    1704                 :    38870341 :                    loop = loop_outer (loop))
    1705                 :             :                 {
    1706                 :    38870341 :                   eloops--;
    1707                 :    38870341 :                   sizes[loop->num]++;
    1708                 :             :                 }
    1709                 :             : 
    1710                 :    35784095 :               if (eloops != 0)
    1711                 :             :                 {
    1712                 :           0 :                   error ("wrong list of exited loops for edge %d->%d",
    1713                 :           0 :                          e->src->index, e->dest->index);
    1714                 :           0 :                   err = 1;
    1715                 :             :                 }
    1716                 :             :             }
    1717                 :             :         }
    1718                 :             : 
    1719                 :    23605341 :       if (n_exits != current_loops->exits->elements ())
    1720                 :             :         {
    1721                 :           0 :           error ("too many loop exits recorded");
    1722                 :           0 :           err = 1;
    1723                 :             :         }
    1724                 :             : 
    1725                 :    90599338 :       for (auto loop : loops_list (cfun, 0))
    1726                 :             :         {
    1727                 :    19783315 :           eloops = 0;
    1728                 :    58653656 :           for (exit = loop->exits->next; exit->e; exit = exit->next)
    1729                 :    38870341 :             eloops++;
    1730                 :    19783315 :           if (eloops != sizes[loop->num])
    1731                 :             :             {
    1732                 :           0 :               error ("%d exits recorded for loop %d (having %d exits)",
    1733                 :             :                      eloops, loop->num, sizes[loop->num]);
    1734                 :           0 :               err = 1;
    1735                 :             :             }
    1736                 :    23605341 :         }
    1737                 :             : 
    1738                 :    23605341 :       free (sizes);
    1739                 :             :     }
    1740                 :             : 
    1741                 :   345320047 :   gcc_assert (!err);
    1742                 :             : 
    1743                 :   345320047 :   if (!dom_available)
    1744                 :    93380411 :     free_dominance_info (CDI_DOMINATORS);
    1745                 :   345320047 : }
    1746                 :             : 
    1747                 :             : #if __GNUC__ >= 10
    1748                 :             : #  pragma GCC diagnostic pop
    1749                 :             : #endif
    1750                 :             : 
    1751                 :             : /* Returns latch edge of LOOP.  */
    1752                 :             : edge
    1753                 :    10271040 : loop_latch_edge (const class loop *loop)
    1754                 :             : {
    1755                 :    10271040 :   return find_edge (loop->latch, loop->header);
    1756                 :             : }
    1757                 :             : 
    1758                 :             : /* Returns preheader edge of LOOP.  */
    1759                 :             : edge
    1760                 :   365551612 : loop_preheader_edge (const class loop *loop)
    1761                 :             : {
    1762                 :   365551612 :   edge e;
    1763                 :   365551612 :   edge_iterator ei;
    1764                 :             : 
    1765                 :   365551612 :   gcc_assert (loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS)
    1766                 :             :               && ! loops_state_satisfies_p (LOOPS_MAY_HAVE_MULTIPLE_LATCHES));
    1767                 :             : 
    1768                 :   553509508 :   FOR_EACH_EDGE (e, ei, loop->header->preds)
    1769                 :   552877880 :     if (e->src != loop->latch)
    1770                 :             :       break;
    1771                 :             : 
    1772                 :   365551612 :   if (! e)
    1773                 :             :     {
    1774                 :      631628 :       gcc_assert (! loop_outer (loop));
    1775                 :      631628 :       return single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
    1776                 :             :     }
    1777                 :             : 
    1778                 :             :   return e;
    1779                 :             : }
    1780                 :             : 
    1781                 :             : /* Returns true if E is an exit of LOOP.  */
    1782                 :             : 
    1783                 :             : bool
    1784                 :    29443677 : loop_exit_edge_p (const class loop *loop, const_edge e)
    1785                 :             : {
    1786                 :    29443677 :   return (flow_bb_inside_loop_p (loop, e->src)
    1787                 :    29443677 :           && !flow_bb_inside_loop_p (loop, e->dest));
    1788                 :             : }
    1789                 :             : 
    1790                 :             : /* Returns the single exit edge of LOOP, or NULL if LOOP has either no exit
    1791                 :             :    or more than one exit.  If loops do not have the exits recorded, NULL
    1792                 :             :    is returned always.  */
    1793                 :             : 
    1794                 :             : edge
    1795                 :    29581608 : single_exit (const class loop *loop)
    1796                 :             : {
    1797                 :    29581608 :   struct loop_exit *exit = loop->exits->next;
    1798                 :             : 
    1799                 :    29581608 :   if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
    1800                 :             :     return NULL;
    1801                 :             : 
    1802                 :    29565411 :   if (exit->e && exit->next == loop->exits)
    1803                 :             :     return exit->e;
    1804                 :             :   else
    1805                 :     8828363 :     return NULL;
    1806                 :             : }
    1807                 :             : 
    1808                 :             : /* Returns true when BB has an incoming edge exiting LOOP.  */
    1809                 :             : 
    1810                 :             : bool
    1811                 :           0 : loop_exits_to_bb_p (class loop *loop, basic_block bb)
    1812                 :             : {
    1813                 :           0 :   edge e;
    1814                 :           0 :   edge_iterator ei;
    1815                 :             : 
    1816                 :           0 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1817                 :           0 :     if (loop_exit_edge_p (loop, e))
    1818                 :             :       return true;
    1819                 :             : 
    1820                 :             :   return false;
    1821                 :             : }
    1822                 :             : 
    1823                 :             : /* Returns true when BB has an outgoing edge exiting LOOP.  */
    1824                 :             : 
    1825                 :             : bool
    1826                 :      819426 : loop_exits_from_bb_p (class loop *loop, basic_block bb)
    1827                 :             : {
    1828                 :      819426 :   edge e;
    1829                 :      819426 :   edge_iterator ei;
    1830                 :             : 
    1831                 :     1500503 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1832                 :     1389606 :     if (loop_exit_edge_p (loop, e))
    1833                 :             :       return true;
    1834                 :             : 
    1835                 :             :   return false;
    1836                 :             : }
    1837                 :             : 
    1838                 :             : /* Return location corresponding to the loop control condition if possible.  */
    1839                 :             : 
    1840                 :             : dump_user_location_t
    1841                 :      503205 : get_loop_location (class loop *loop)
    1842                 :             : {
    1843                 :      503205 :   rtx_insn *insn = NULL;
    1844                 :      503205 :   class niter_desc *desc = NULL;
    1845                 :      503205 :   edge exit;
    1846                 :             : 
    1847                 :             :   /* For a for or while loop, we would like to return the location
    1848                 :             :      of the for or while statement, if possible.  To do this, look
    1849                 :             :      for the branch guarding the loop back-edge.  */
    1850                 :             : 
    1851                 :             :   /* If this is a simple loop with an in_edge, then the loop control
    1852                 :             :      branch is typically at the end of its source.  */
    1853                 :      503205 :   desc = get_simple_loop_desc (loop);
    1854                 :      503205 :   if (desc->in_edge)
    1855                 :             :     {
    1856                 :      486662 :       FOR_BB_INSNS_REVERSE (desc->in_edge->src, insn)
    1857                 :             :         {
    1858                 :      481943 :           if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
    1859                 :      343648 :             return insn;
    1860                 :             :         }
    1861                 :             :     }
    1862                 :             :   /* If loop has a single exit, then the loop control branch
    1863                 :             :      must be at the end of its source.  */
    1864                 :      159557 :   if ((exit = single_exit (loop)))
    1865                 :             :     {
    1866                 :      193572 :       FOR_BB_INSNS_REVERSE (exit->src, insn)
    1867                 :             :         {
    1868                 :      184718 :           if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
    1869                 :       87344 :             return insn;
    1870                 :             :         }
    1871                 :             :     }
    1872                 :             :   /* Next check the latch, to see if it is non-empty.  */
    1873                 :      188959 :   FOR_BB_INSNS_REVERSE (loop->latch, insn)
    1874                 :             :     {
    1875                 :      131025 :       if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
    1876                 :       14279 :         return insn;
    1877                 :             :     }
    1878                 :             :   /* Finally, if none of the above identifies the loop control branch,
    1879                 :             :      return the first location in the loop header.  */
    1880                 :      282362 :   FOR_BB_INSNS (loop->header, insn)
    1881                 :             :     {
    1882                 :      270430 :       if (INSN_P (insn) && INSN_HAS_LOCATION (insn))
    1883                 :       46002 :         return insn;
    1884                 :             :     }
    1885                 :             :   /* If all else fails, simply return the current function location.  */
    1886                 :       11932 :   return dump_user_location_t::from_function_decl (current_function_decl);
    1887                 :             : }
    1888                 :             : 
    1889                 :             : /* Records that every statement in LOOP is executed I_BOUND times.
    1890                 :             :    REALISTIC is true if I_BOUND is expected to be close to the real number
    1891                 :             :    of iterations.  UPPER is true if we are sure the loop iterates at most
    1892                 :             :    I_BOUND times.  */
    1893                 :             : 
    1894                 :             : void
    1895                 :    14503598 : record_niter_bound (class loop *loop, const widest_int &i_bound,
    1896                 :             :                     bool realistic, bool upper)
    1897                 :             : {
    1898                 :    14503598 :   if (wi::min_precision (i_bound, SIGNED) > bound_wide_int ().get_precision ())
    1899                 :           0 :     return;
    1900                 :             : 
    1901                 :    14503598 :   bound_wide_int bound = bound_wide_int::from (i_bound, SIGNED);
    1902                 :             : 
    1903                 :             :   /* Update the bounds only when there is no previous estimation, or when the
    1904                 :             :      current estimation is smaller.  */
    1905                 :    14503598 :   if (upper
    1906                 :    14503598 :       && (!loop->any_upper_bound
    1907                 :    13167716 :           || wi::ltu_p (bound, loop->nb_iterations_upper_bound)))
    1908                 :             :     {
    1909                 :      732687 :       loop->any_upper_bound = true;
    1910                 :      732687 :       loop->nb_iterations_upper_bound = bound;
    1911                 :      732687 :       if (!loop->any_likely_upper_bound)
    1912                 :             :         {
    1913                 :      434194 :           loop->any_likely_upper_bound = true;
    1914                 :      434194 :           loop->nb_iterations_likely_upper_bound = bound;
    1915                 :             :         }
    1916                 :             :     }
    1917                 :    14503598 :   if (realistic
    1918                 :    14503598 :       && (!loop->any_estimate
    1919                 :     2396083 :           || wi::ltu_p (bound, loop->nb_iterations_estimate)))
    1920                 :             :     {
    1921                 :      196823 :       loop->any_estimate = true;
    1922                 :      196823 :       loop->nb_iterations_estimate = bound;
    1923                 :             :     }
    1924                 :    14503598 :   if (!realistic
    1925                 :    14503598 :       && (!loop->any_likely_upper_bound
    1926                 :    11912805 :           || wi::ltu_p (bound, loop->nb_iterations_likely_upper_bound)))
    1927                 :             :     {
    1928                 :      192338 :       loop->any_likely_upper_bound = true;
    1929                 :      192338 :       loop->nb_iterations_likely_upper_bound = bound;
    1930                 :             :     }
    1931                 :             : 
    1932                 :             :   /* If an upper bound is smaller than the realistic estimate of the
    1933                 :             :      number of iterations, use the upper bound instead.  */
    1934                 :    14503598 :   if (loop->any_upper_bound
    1935                 :    14503598 :       && loop->any_estimate
    1936                 :    22252862 :       && wi::ltu_p (loop->nb_iterations_upper_bound,
    1937                 :     7749264 :                     loop->nb_iterations_estimate))
    1938                 :        3209 :     loop->nb_iterations_estimate = loop->nb_iterations_upper_bound;
    1939                 :    14503598 :   if (loop->any_upper_bound
    1940                 :    14503598 :       && loop->any_likely_upper_bound
    1941                 :    29003788 :       && wi::ltu_p (loop->nb_iterations_upper_bound,
    1942                 :    14500190 :                     loop->nb_iterations_likely_upper_bound))
    1943                 :       23998 :     loop->nb_iterations_likely_upper_bound = loop->nb_iterations_upper_bound;
    1944                 :             : }
    1945                 :             : 
    1946                 :             : /* Similar to get_estimated_loop_iterations, but returns the estimate only
    1947                 :             :    if it fits to HOST_WIDE_INT.  If this is not the case, or the estimate
    1948                 :             :    on the number of iterations of LOOP could not be derived, returns -1.  */
    1949                 :             : 
    1950                 :             : HOST_WIDE_INT
    1951                 :         176 : get_estimated_loop_iterations_int (class loop *loop)
    1952                 :             : {
    1953                 :         176 :   widest_int nit;
    1954                 :         176 :   HOST_WIDE_INT hwi_nit;
    1955                 :             : 
    1956                 :         176 :   if (!get_estimated_loop_iterations (loop, &nit))
    1957                 :             :     return -1;
    1958                 :             : 
    1959                 :          52 :   if (!wi::fits_shwi_p (nit))
    1960                 :             :     return -1;
    1961                 :          52 :   hwi_nit = nit.to_shwi ();
    1962                 :             : 
    1963                 :          52 :   return hwi_nit < 0 ? -1 : hwi_nit;
    1964                 :         176 : }
    1965                 :             : 
    1966                 :             : /* Returns an upper bound on the number of executions of statements
    1967                 :             :    in the LOOP.  For statements before the loop exit, this exceeds
    1968                 :             :    the number of execution of the latch by one.  */
    1969                 :             : 
    1970                 :             : HOST_WIDE_INT
    1971                 :      247719 : max_stmt_executions_int (class loop *loop)
    1972                 :             : {
    1973                 :      247719 :   HOST_WIDE_INT nit = get_max_loop_iterations_int (loop);
    1974                 :      247719 :   HOST_WIDE_INT snit;
    1975                 :             : 
    1976                 :      247719 :   if (nit == -1)
    1977                 :             :     return -1;
    1978                 :             : 
    1979                 :      231177 :   snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
    1980                 :             : 
    1981                 :             :   /* If the computation overflows, return -1.  */
    1982                 :      231177 :   return snit < 0 ? -1 : snit;
    1983                 :             : }
    1984                 :             : 
    1985                 :             : /* Returns an likely upper bound on the number of executions of statements
    1986                 :             :    in the LOOP.  For statements before the loop exit, this exceeds
    1987                 :             :    the number of execution of the latch by one.  */
    1988                 :             : 
    1989                 :             : HOST_WIDE_INT
    1990                 :     4270868 : likely_max_stmt_executions_int (class loop *loop)
    1991                 :             : {
    1992                 :     4270868 :   HOST_WIDE_INT nit = get_likely_max_loop_iterations_int (loop);
    1993                 :     4270868 :   HOST_WIDE_INT snit;
    1994                 :             : 
    1995                 :     4270868 :   if (nit == -1)
    1996                 :             :     return -1;
    1997                 :             : 
    1998                 :     3606939 :   snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1);
    1999                 :             : 
    2000                 :             :   /* If the computation overflows, return -1.  */
    2001                 :     3606939 :   return snit < 0 ? -1 : snit;
    2002                 :             : }
    2003                 :             : 
    2004                 :             : /* Sets NIT to the estimated number of executions of the latch of the
    2005                 :             :    LOOP.  If we have no reliable estimate, the function returns false, otherwise
    2006                 :             :    returns true.  */
    2007                 :             : 
    2008                 :             : bool
    2009                 :     7815970 : get_estimated_loop_iterations (class loop *loop, widest_int *nit)
    2010                 :             : {
    2011                 :             :   /* Even if the bound is not recorded, possibly we can derrive one from
    2012                 :             :      profile.  */
    2013                 :     7815970 :   if (!loop->any_estimate)
    2014                 :             :     {
    2015                 :     4496466 :       sreal snit;
    2016                 :     4496466 :       bool reliable;
    2017                 :     4496466 :       if (expected_loop_iterations_by_profile (loop, &snit, &reliable)
    2018                 :     4496466 :           && reliable)
    2019                 :             :         {
    2020                 :           3 :           *nit = snit.to_nearest_int ();
    2021                 :           3 :           return true;
    2022                 :             :         }
    2023                 :             :       return false;
    2024                 :             :     }
    2025                 :             : 
    2026                 :     3319504 :   *nit = widest_int::from (loop->nb_iterations_estimate, SIGNED);
    2027                 :     3319504 :   return true;
    2028                 :             : }
    2029                 :             : 
    2030                 :             : /* Sets NIT to an upper bound for the maximum number of executions of the
    2031                 :             :    latch of the LOOP.  If we have no reliable estimate, the function returns
    2032                 :             :    false, otherwise returns true.  */
    2033                 :             : 
    2034                 :             : bool
    2035                 :    20570000 : get_max_loop_iterations (const class loop *loop, widest_int *nit)
    2036                 :             : {
    2037                 :    20570000 :   if (!loop->any_upper_bound)
    2038                 :             :     return false;
    2039                 :             : 
    2040                 :    18395614 :   *nit = widest_int::from (loop->nb_iterations_upper_bound, SIGNED);
    2041                 :    18395614 :   return true;
    2042                 :             : }
    2043                 :             : 
    2044                 :             : /* Similar to get_max_loop_iterations, but returns the estimate only
    2045                 :             :    if it fits to HOST_WIDE_INT.  If this is not the case, or the estimate
    2046                 :             :    on the number of iterations of LOOP could not be derived, returns -1.  */
    2047                 :             : 
    2048                 :             : HOST_WIDE_INT
    2049                 :     1481432 : get_max_loop_iterations_int (const class loop *loop)
    2050                 :             : {
    2051                 :     1481432 :   widest_int nit;
    2052                 :     1481432 :   HOST_WIDE_INT hwi_nit;
    2053                 :             : 
    2054                 :     1481432 :   if (!get_max_loop_iterations (loop, &nit))
    2055                 :             :     return -1;
    2056                 :             : 
    2057                 :     1150106 :   if (!wi::fits_shwi_p (nit))
    2058                 :             :     return -1;
    2059                 :     1097024 :   hwi_nit = nit.to_shwi ();
    2060                 :             : 
    2061                 :     1097024 :   return hwi_nit < 0 ? -1 : hwi_nit;
    2062                 :     1481432 : }
    2063                 :             : 
    2064                 :             : /* Sets NIT to an upper bound for the maximum number of executions of the
    2065                 :             :    latch of the LOOP.  If we have no reliable estimate, the function returns
    2066                 :             :    false, otherwise returns true.  */
    2067                 :             : 
    2068                 :             : bool
    2069                 :     4652436 : get_likely_max_loop_iterations (class loop *loop, widest_int *nit)
    2070                 :             : {
    2071                 :     4652436 :   if (!loop->any_likely_upper_bound)
    2072                 :             :     return false;
    2073                 :             : 
    2074                 :     4126113 :   *nit = widest_int::from (loop->nb_iterations_likely_upper_bound, SIGNED);
    2075                 :     4126113 :   return true;
    2076                 :             : }
    2077                 :             : 
    2078                 :             : /* Similar to get_max_loop_iterations, but returns the estimate only
    2079                 :             :    if it fits to HOST_WIDE_INT.  If this is not the case, or the estimate
    2080                 :             :    on the number of iterations of LOOP could not be derived, returns -1.  */
    2081                 :             : 
    2082                 :             : HOST_WIDE_INT
    2083                 :     4317465 : get_likely_max_loop_iterations_int (class loop *loop)
    2084                 :             : {
    2085                 :     4317465 :   widest_int nit;
    2086                 :     4317465 :   HOST_WIDE_INT hwi_nit;
    2087                 :             : 
    2088                 :     4317465 :   if (!get_likely_max_loop_iterations (loop, &nit))
    2089                 :             :     return -1;
    2090                 :             : 
    2091                 :     3909692 :   if (!wi::fits_shwi_p (nit))
    2092                 :             :     return -1;
    2093                 :     3652925 :   hwi_nit = nit.to_shwi ();
    2094                 :             : 
    2095                 :     3652925 :   return hwi_nit < 0 ? -1 : hwi_nit;
    2096                 :     4317465 : }
    2097                 :             : 
    2098                 :             : /* Returns the loop depth of the loop BB belongs to.  */
    2099                 :             : 
    2100                 :             : int
    2101                 :    82334403 : bb_loop_depth (const_basic_block bb)
    2102                 :             : {
    2103                 :   109209700 :   return bb->loop_father ? loop_depth (bb->loop_father) : 0;
    2104                 :             : }
    2105                 :             : 
    2106                 :             : /* Marks LOOP for removal and sets LOOPS_NEED_FIXUP.  */
    2107                 :             : 
    2108                 :             : void
    2109                 :      224310 : mark_loop_for_removal (loop_p loop)
    2110                 :             : {
    2111                 :      224310 :   if (loop->header == NULL)
    2112                 :             :     return;
    2113                 :      224310 :   loop->former_header = loop->header;
    2114                 :      224310 :   loop->header = NULL;
    2115                 :      224310 :   loop->latch = NULL;
    2116                 :      224310 :   loops_state_set (LOOPS_NEED_FIXUP);
    2117                 :             : }
    2118                 :             : 
    2119                 :             : /* Starting from loop tree ROOT, walk loop tree as the visiting
    2120                 :             :    order specified by FLAGS.  The supported visiting orders
    2121                 :             :    are:
    2122                 :             :      - LI_ONLY_INNERMOST
    2123                 :             :      - LI_FROM_INNERMOST
    2124                 :             :      - Preorder (if neither of above is specified)  */
    2125                 :             : 
    2126                 :             : void
    2127                 :  1264607137 : loops_list::walk_loop_tree (class loop *root, unsigned flags)
    2128                 :             : {
    2129                 :  1264607137 :   bool only_innermost_p = flags & LI_ONLY_INNERMOST;
    2130                 :  1264607137 :   bool from_innermost_p = flags & LI_FROM_INNERMOST;
    2131                 :  1264607137 :   bool preorder_p = !(only_innermost_p || from_innermost_p);
    2132                 :             : 
    2133                 :             :   /* Early handle root without any inner loops, make later
    2134                 :             :      processing simpler, that is all loops processed in the
    2135                 :             :      following while loop are impossible to be root.  */
    2136                 :  1264607137 :   if (!root->inner)
    2137                 :             :     {
    2138                 :  1026936873 :       if (flags & LI_INCLUDE_ROOT)
    2139                 :       10829 :         this->to_visit.quick_push (root->num);
    2140                 :  1026936873 :       return;
    2141                 :             :     }
    2142                 :   237670264 :   else if (preorder_p && flags & LI_INCLUDE_ROOT)
    2143                 :       43760 :     this->to_visit.quick_push (root->num);
    2144                 :             : 
    2145                 :             :   class loop *aloop;
    2146                 :    60368897 :   for (aloop = root->inner;
    2147                 :   298039161 :        aloop->inner != NULL;
    2148                 :    60368897 :        aloop = aloop->inner)
    2149                 :             :     {
    2150                 :    60368897 :       if (preorder_p)
    2151                 :    43817973 :         this->to_visit.quick_push (aloop->num);
    2152                 :    60368897 :       continue;
    2153                 :             :     }
    2154                 :             : 
    2155                 :   751370139 :   while (1)
    2156                 :             :     {
    2157                 :   751370139 :       gcc_assert (aloop != root);
    2158                 :   751370139 :       if (from_innermost_p || aloop->inner == NULL)
    2159                 :   659287070 :         this->to_visit.quick_push (aloop->num);
    2160                 :             : 
    2161                 :   751370139 :       if (aloop->next)
    2162                 :             :         {
    2163                 :    64682154 :           for (aloop = aloop->next;
    2164                 :   453330978 :                aloop->inner != NULL;
    2165                 :    64682154 :                aloop = aloop->inner)
    2166                 :             :             {
    2167                 :    64682154 :               if (preorder_p)
    2168                 :    48265096 :                 this->to_visit.quick_push (aloop->num);
    2169                 :    64682154 :               continue;
    2170                 :             :             }
    2171                 :             :         }
    2172                 :   362721315 :       else if (loop_outer (aloop) == root)
    2173                 :             :         break;
    2174                 :             :       else
    2175                 :             :         aloop = loop_outer (aloop);
    2176                 :             :     }
    2177                 :             : 
    2178                 :             :   /* When visiting from innermost, we need to consider root here
    2179                 :             :      since the previous while loop doesn't handle it.  */
    2180                 :   237670264 :   if (from_innermost_p && flags & LI_INCLUDE_ROOT)
    2181                 :           0 :     this->to_visit.quick_push (root->num);
    2182                 :    60368897 : }
    2183                 :             : 
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.