LCOV - code coverage report
Current view: top level - gcc - tree-iterator.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.3 % 168 155
Test Date: 2024-04-20 14:03:02 Functions: 100.0 % 11 11
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
       2                 :             :    Copyright (C) 2003-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Andrew MacLeod  <amacleod@redhat.com>
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       7                 :             : GCC is free software; you can redistribute it and/or modify
       8                 :             : it under the terms of the GNU General Public License as published by
       9                 :             : the Free Software Foundation; either version 3, or (at your option)
      10                 :             : any later version.
      11                 :             : 
      12                 :             : GCC is distributed in the hope that it will be useful,
      13                 :             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :             : GNU General Public License for more details.
      16                 :             : 
      17                 :             : You should have received a copy of the GNU General Public License
      18                 :             : along with GCC; see the file COPYING3.  If not see
      19                 :             : <http://www.gnu.org/licenses/>.  */
      20                 :             : 
      21                 :             : #include "config.h"
      22                 :             : #include "system.h"
      23                 :             : #include "coretypes.h"
      24                 :             : #include "tree.h"
      25                 :             : #include "tree-iterator.h"
      26                 :             : 
      27                 :             : 
      28                 :             : /* This is a cache of STATEMENT_LIST nodes.  We create and destroy them
      29                 :             :    fairly often during gimplification.  */
      30                 :             : 
      31                 :             : static GTY ((deletable (""))) vec<tree, va_gc> *stmt_list_cache;
      32                 :             : 
      33                 :             : tree
      34                 :   682898071 : alloc_stmt_list (void)
      35                 :             : {
      36                 :   682898071 :   tree list;
      37                 :   682898071 :   if (!vec_safe_is_empty (stmt_list_cache))
      38                 :             :     {
      39                 :   400586992 :       list = stmt_list_cache->pop ();
      40                 :   400586992 :       memset (list, 0, sizeof (struct tree_base));
      41                 :   400586992 :       TREE_SET_CODE (list, STATEMENT_LIST);
      42                 :             :     }
      43                 :             :   else
      44                 :             :     {
      45                 :   282311079 :       list = make_node (STATEMENT_LIST);
      46                 :   282311079 :       TREE_SIDE_EFFECTS (list) = 0;
      47                 :             :     }
      48                 :   682898071 :   TREE_TYPE (list) = void_type_node;
      49                 :   682898071 :   return list;
      50                 :             : }
      51                 :             : 
      52                 :             : void
      53                 :   401556623 : free_stmt_list (tree t)
      54                 :             : {
      55                 :   401556623 :   gcc_assert (!STATEMENT_LIST_HEAD (t));
      56                 :   401556623 :   gcc_assert (!STATEMENT_LIST_TAIL (t));
      57                 :   401556623 :   vec_safe_push (stmt_list_cache, t);
      58                 :   401556623 : }
      59                 :             : 
      60                 :             : /* A subroutine of append_to_statement_list{,_force}.  T is not NULL.  */
      61                 :             : 
      62                 :             : static void
      63                 :  1110194078 : append_to_statement_list_1 (tree t, tree *list_p)
      64                 :             : {
      65                 :  1110194078 :   tree list = *list_p;
      66                 :  1110194078 :   tree_stmt_iterator i;
      67                 :             : 
      68                 :  1110194078 :   if (!list)
      69                 :             :     {
      70                 :    10864765 :       if (t && TREE_CODE (t) == STATEMENT_LIST)
      71                 :             :         {
      72                 :     2849338 :           *list_p = t;
      73                 :     2849338 :           return;
      74                 :             :         }
      75                 :     8015427 :       *list_p = list = alloc_stmt_list ();
      76                 :             :     }
      77                 :  1099329313 :   else if (TREE_CODE (list) != STATEMENT_LIST)
      78                 :             :     {
      79                 :    12776977 :       tree first = list;
      80                 :    12776977 :       *list_p = list = alloc_stmt_list ();
      81                 :    12776977 :       i = tsi_last (list);
      82                 :    12776977 :       tsi_link_after (&i, first, TSI_CONTINUE_LINKING);
      83                 :             :     }
      84                 :             : 
      85                 :  1107344740 :   i = tsi_last (list);
      86                 :  1107344740 :   tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
      87                 :             : }
      88                 :             : 
      89                 :             : /* Add T to the end of the list container pointed to by LIST_P.
      90                 :             :    If T is an expression with no effects, it is ignored.  */
      91                 :             : 
      92                 :             : void
      93                 :    53958514 : append_to_statement_list (tree t, tree *list_p)
      94                 :             : {
      95                 :    53958514 :   if (t && (TREE_SIDE_EFFECTS (t) || TREE_CODE (t) == DEBUG_BEGIN_STMT))
      96                 :    51001726 :     append_to_statement_list_1 (t, list_p);
      97                 :    53958514 : }
      98                 :             : 
      99                 :             : /* Similar, but the statement is always added, regardless of side effects.  */
     100                 :             : 
     101                 :             : void
     102                 :  1059192352 : append_to_statement_list_force (tree t, tree *list_p)
     103                 :             : {
     104                 :  1059192352 :   if (t != NULL_TREE)
     105                 :  1059192352 :     append_to_statement_list_1 (t, list_p);
     106                 :  1059192352 : }
     107                 :             : 
     108                 :             : /* Links a statement, or a chain of statements, before the current stmt.  */
     109                 :             : 
     110                 :             : void
     111                 :      140140 : tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
     112                 :             : {
     113                 :      140140 :   struct tree_statement_list_node *head, *tail, *cur;
     114                 :             : 
     115                 :             :   /* Die on looping.  */
     116                 :      140140 :   gcc_assert (t != i->container);
     117                 :             : 
     118                 :      140140 :   if (TREE_CODE (t) == STATEMENT_LIST)
     119                 :             :     {
     120                 :        9586 :       head = STATEMENT_LIST_HEAD (t);
     121                 :        9586 :       tail = STATEMENT_LIST_TAIL (t);
     122                 :        9586 :       STATEMENT_LIST_HEAD (t) = NULL;
     123                 :        9586 :       STATEMENT_LIST_TAIL (t) = NULL;
     124                 :             : 
     125                 :        9586 :       free_stmt_list (t);
     126                 :             : 
     127                 :             :       /* Empty statement lists need no work.  */
     128                 :        9586 :       if (!head || !tail)
     129                 :             :         {
     130                 :           0 :           gcc_assert (head == tail);
     131                 :             :           return;
     132                 :             :         }
     133                 :             :     }
     134                 :             :   else
     135                 :             :     {
     136                 :      130554 :       head = ggc_alloc<tree_statement_list_node> ();
     137                 :      130554 :       head->prev = NULL;
     138                 :      130554 :       head->next = NULL;
     139                 :      130554 :       head->stmt = t;
     140                 :      130554 :       tail = head;
     141                 :             :     }
     142                 :             : 
     143                 :      140140 :   if (TREE_CODE (t) != DEBUG_BEGIN_STMT)
     144                 :      140140 :     TREE_SIDE_EFFECTS (i->container) = 1;
     145                 :             : 
     146                 :      140140 :   cur = i->ptr;
     147                 :             : 
     148                 :             :   /* Link it into the list.  */
     149                 :      140140 :   if (cur)
     150                 :             :     {
     151                 :      140140 :       head->prev = cur->prev;
     152                 :      140140 :       if (head->prev)
     153                 :          17 :         head->prev->next = head;
     154                 :             :       else
     155                 :      140123 :         STATEMENT_LIST_HEAD (i->container) = head;
     156                 :      140140 :       tail->next = cur;
     157                 :      140140 :       cur->prev = tail;
     158                 :             :     }
     159                 :             :   else
     160                 :             :     {
     161                 :           0 :       head->prev = STATEMENT_LIST_TAIL (i->container);
     162                 :           0 :       if (head->prev)
     163                 :           0 :        head->prev->next = head;
     164                 :             :       else
     165                 :           0 :        STATEMENT_LIST_HEAD (i->container) = head;
     166                 :           0 :       STATEMENT_LIST_TAIL (i->container) = tail;
     167                 :             :     }
     168                 :             : 
     169                 :             :   /* Update the iterator, if requested.  */
     170                 :      140140 :   switch (mode)
     171                 :             :     {
     172                 :       19094 :     case TSI_NEW_STMT:
     173                 :       19094 :     case TSI_CONTINUE_LINKING:
     174                 :       19094 :     case TSI_CHAIN_START:
     175                 :       19094 :       i->ptr = head;
     176                 :       19094 :       break;
     177                 :           0 :     case TSI_CHAIN_END:
     178                 :           0 :       i->ptr = tail;
     179                 :           0 :       break;
     180                 :             :     case TSI_SAME_STMT:
     181                 :             :       break;
     182                 :             :     }
     183                 :             : }
     184                 :             : 
     185                 :             : /* Links a statement, or a chain of statements, after the current stmt.  */
     186                 :             : 
     187                 :             : void
     188                 :  1175343579 : tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
     189                 :             : {
     190                 :  1175343579 :   struct tree_statement_list_node *head, *tail, *cur;
     191                 :             : 
     192                 :             :   /* Die on looping.  */
     193                 :  1175343579 :   gcc_assert (t != i->container);
     194                 :             : 
     195                 :  1175343579 :   if (TREE_CODE (t) == STATEMENT_LIST)
     196                 :             :     {
     197                 :    42661099 :       head = STATEMENT_LIST_HEAD (t);
     198                 :    42661099 :       tail = STATEMENT_LIST_TAIL (t);
     199                 :    42661099 :       STATEMENT_LIST_HEAD (t) = NULL;
     200                 :    42661099 :       STATEMENT_LIST_TAIL (t) = NULL;
     201                 :             : 
     202                 :    42661099 :       free_stmt_list (t);
     203                 :             : 
     204                 :             :       /* Empty statement lists need no work.  */
     205                 :    42661099 :       if (!head || !tail)
     206                 :             :         {
     207                 :    13720251 :           gcc_assert (head == tail);
     208                 :             :           return;
     209                 :             :         }
     210                 :             :     }
     211                 :             :   else
     212                 :             :     {
     213                 :  1132682480 :       head = ggc_alloc<tree_statement_list_node> ();
     214                 :  1132682480 :       head->prev = NULL;
     215                 :  1132682480 :       head->next = NULL;
     216                 :  1132682480 :       head->stmt = t;
     217                 :  1132682480 :       tail = head;
     218                 :             :     }
     219                 :             : 
     220                 :  1161623328 :   if (TREE_CODE (t) != DEBUG_BEGIN_STMT)
     221                 :   872300648 :     TREE_SIDE_EFFECTS (i->container) = 1;
     222                 :             : 
     223                 :  1161623328 :   cur = i->ptr;
     224                 :             : 
     225                 :             :   /* Link it into the list.  */
     226                 :  1161623328 :   if (cur)
     227                 :             :     {
     228                 :   550027501 :       tail->next = cur->next;
     229                 :   550027501 :       if (tail->next)
     230                 :          28 :         tail->next->prev = tail;
     231                 :             :       else
     232                 :   550027473 :         STATEMENT_LIST_TAIL (i->container) = tail;
     233                 :   550027501 :       head->prev = cur;
     234                 :   550027501 :       cur->next = head;
     235                 :             :     }
     236                 :             :   else
     237                 :             :     {
     238                 :   611595827 :       gcc_assert (!STATEMENT_LIST_TAIL (i->container));
     239                 :   611595827 :       STATEMENT_LIST_HEAD (i->container) = head;
     240                 :   611595827 :       STATEMENT_LIST_TAIL (i->container) = tail;
     241                 :             :     }
     242                 :             : 
     243                 :             :   /* Update the iterator, if requested.  */
     244                 :  1161623328 :   switch (mode)
     245                 :             :     {
     246                 :           0 :     case TSI_NEW_STMT:
     247                 :           0 :     case TSI_CHAIN_START:
     248                 :           0 :       i->ptr = head;
     249                 :           0 :       break;
     250                 :  1161623116 :     case TSI_CONTINUE_LINKING:
     251                 :  1161623116 :     case TSI_CHAIN_END:
     252                 :  1161623116 :       i->ptr = tail;
     253                 :  1161623116 :       break;
     254                 :         212 :     case TSI_SAME_STMT:
     255                 :         212 :       gcc_assert (cur);
     256                 :             :       break;
     257                 :             :     }
     258                 :             : }
     259                 :             : 
     260                 :             : /* Remove a stmt from the tree list.  The iterator is updated to point to
     261                 :             :    the next stmt.  */
     262                 :             : 
     263                 :             : void
     264                 :   394410664 : tsi_delink (tree_stmt_iterator *i)
     265                 :             : {
     266                 :   394410664 :   struct tree_statement_list_node *cur, *next, *prev;
     267                 :             : 
     268                 :   394410664 :   cur = i->ptr;
     269                 :   394410664 :   next = cur->next;
     270                 :   394410664 :   prev = cur->prev;
     271                 :             : 
     272                 :   394410664 :   if (prev)
     273                 :        1372 :     prev->next = next;
     274                 :             :   else
     275                 :   394409292 :     STATEMENT_LIST_HEAD (i->container) = next;
     276                 :   394410664 :   if (next)
     277                 :    27609937 :     next->prev = prev;
     278                 :             :   else
     279                 :   366800727 :     STATEMENT_LIST_TAIL (i->container) = prev;
     280                 :             : 
     281                 :   394410664 :   if (!next && !prev)
     282                 :   366800015 :     TREE_SIDE_EFFECTS (i->container) = 0;
     283                 :             : 
     284                 :   394410664 :   i->ptr = next;
     285                 :   394410664 : }
     286                 :             : 
     287                 :             : /* Return the first expression in a sequence of COMPOUND_EXPRs, or in
     288                 :             :    a STATEMENT_LIST, disregarding DEBUG_BEGIN_STMTs, recursing into a
     289                 :             :    STATEMENT_LIST if that's the first non-DEBUG_BEGIN_STMT.  */
     290                 :             : 
     291                 :             : tree
     292                 :    10419209 : expr_first (tree expr)
     293                 :             : {
     294                 :    10420298 :   if (expr == NULL_TREE)
     295                 :             :     return expr;
     296                 :             : 
     297                 :    10420266 :   if (TREE_CODE (expr) == STATEMENT_LIST)
     298                 :             :     {
     299                 :     3391795 :       struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
     300                 :     3391795 :       if (!n)
     301                 :             :         return NULL_TREE;
     302                 :     6471241 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
     303                 :             :         {
     304                 :     3150728 :           n = n->next;
     305                 :     3150728 :           if (!n)
     306                 :             :             return NULL_TREE;
     307                 :             :         }
     308                 :             :       /* If the first non-debug stmt is not a statement list, we
     309                 :             :          already know it's what we're looking for.  */
     310                 :     3320513 :       if (TREE_CODE (n->stmt) != STATEMENT_LIST)
     311                 :             :         return n->stmt;
     312                 :             : 
     313                 :             :       return expr_first (n->stmt);
     314                 :             :     }
     315                 :             : 
     316                 :     7028472 :   while (TREE_CODE (expr) == COMPOUND_EXPR)
     317                 :           1 :     expr = TREE_OPERAND (expr, 0);
     318                 :             : 
     319                 :             :   return expr;
     320                 :             : }
     321                 :             : 
     322                 :             : /* Return the last expression in a sequence of COMPOUND_EXPRs, or in a
     323                 :             :    STATEMENT_LIST, disregarding DEBUG_BEGIN_STMTs, recursing into a
     324                 :             :    STATEMENT_LIST if that's the last non-DEBUG_BEGIN_STMT.  */
     325                 :             : 
     326                 :             : tree
     327                 :     7140137 : expr_last (tree expr)
     328                 :             : {
     329                 :     7141511 :   if (expr == NULL_TREE)
     330                 :             :     return expr;
     331                 :             : 
     332                 :     7137517 :   if (TREE_CODE (expr) == STATEMENT_LIST)
     333                 :             :     {
     334                 :     4083982 :       struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
     335                 :     4083982 :       if (!n)
     336                 :             :         return NULL_TREE;
     337                 :     4627524 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
     338                 :             :         {
     339                 :     2203789 :           n = n->prev;
     340                 :     2203789 :           if (!n)
     341                 :             :             return NULL_TREE;
     342                 :             :         }
     343                 :             :       /* If the last non-debug stmt is not a statement list, we
     344                 :             :          already know it's what we're looking for.  */
     345                 :     2423735 :       if (TREE_CODE (n->stmt) != STATEMENT_LIST)
     346                 :             :         return n->stmt;
     347                 :             : 
     348                 :             :       return expr_last (n->stmt);
     349                 :             :     }
     350                 :             : 
     351                 :     3057123 :   while (TREE_CODE (expr) == COMPOUND_EXPR)
     352                 :        3588 :     expr = TREE_OPERAND (expr, 1);
     353                 :             : 
     354                 :             :   return expr;
     355                 :             : }
     356                 :             : 
     357                 :             : /* If EXPR is a STATEMENT_LIST containing just DEBUG_BEGIN_STMTs and
     358                 :             :    a single other stmt, return that other stmt (recursively).
     359                 :             :    If it is a STATEMENT_LIST containing no non-DEBUG_BEGIN_STMTs or
     360                 :             :    multiple, return NULL_TREE.
     361                 :             :    Otherwise return EXPR.  */
     362                 :             : 
     363                 :             : tree
     364                 :    19307084 : expr_single (tree expr)
     365                 :             : {
     366                 :    19328357 :   if (expr == NULL_TREE)
     367                 :             :     return expr;
     368                 :             : 
     369                 :    17590547 :   if (TREE_CODE (expr) == STATEMENT_LIST)
     370                 :             :     {
     371                 :             :       /* With -gstatement-frontiers we could have a STATEMENT_LIST with
     372                 :             :          DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
     373                 :             :          -g wouldn't be present and we'd have that single other stmt
     374                 :             :          directly instead.  */
     375                 :       79441 :       struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
     376                 :       79441 :       if (!n)
     377                 :             :         return NULL_TREE;
     378                 :      106412 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
     379                 :             :         {
     380                 :       28717 :           n = n->next;
     381                 :       28717 :           if (!n)
     382                 :             :             return NULL_TREE;
     383                 :             :         }
     384                 :       85139 :       expr = n->stmt;
     385                 :       85139 :       do
     386                 :             :         {
     387                 :       85139 :           n = n->next;
     388                 :       85139 :           if (!n)
     389                 :             :             return expr_single (expr);
     390                 :             :         }
     391                 :       63866 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
     392                 :             :       return NULL_TREE;
     393                 :             :     }
     394                 :             : 
     395                 :             :   return expr;
     396                 :             : }
     397                 :             : 
     398                 :             : #include "gt-tree-iterator.h"
        

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.