LCOV - code coverage report
Current view: top level - gcc - tree-iterator.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.5 % 178 170
Test Date: 2026-03-07 14:18:32 Functions: 100.0 % 12 12
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
       2              :    Copyright (C) 2003-2026 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    950184467 : alloc_stmt_list (void)
      35              : {
      36    950184467 :   tree list;
      37    950184467 :   if (!vec_safe_is_empty (stmt_list_cache))
      38              :     {
      39    572151549 :       list = stmt_list_cache->pop ();
      40    572151549 :       memset (list, 0, sizeof (struct tree_base));
      41    572151549 :       TREE_SET_CODE (list, STATEMENT_LIST);
      42              :     }
      43              :   else
      44              :     {
      45    378032918 :       list = make_node (STATEMENT_LIST);
      46    378032918 :       TREE_SIDE_EFFECTS (list) = 0;
      47              :     }
      48    950184467 :   TREE_TYPE (list) = void_type_node;
      49    950184467 :   return list;
      50              : }
      51              : 
      52              : void
      53    573045490 : free_stmt_list (tree t)
      54              : {
      55    573045490 :   gcc_assert (!STATEMENT_LIST_HEAD (t));
      56    573045490 :   gcc_assert (!STATEMENT_LIST_TAIL (t));
      57    573045490 :   vec_safe_push (stmt_list_cache, t);
      58    573045490 : }
      59              : 
      60              : /* A subroutine of append_to_statement_list{,_force}.  T is not NULL.  */
      61              : 
      62              : static void
      63   1519099055 : append_to_statement_list_1 (tree t, tree *list_p)
      64              : {
      65   1519099055 :   tree list = *list_p;
      66   1519099055 :   tree_stmt_iterator i;
      67              : 
      68   1519099055 :   if (!list)
      69              :     {
      70     13358173 :       if (t && TREE_CODE (t) == STATEMENT_LIST)
      71              :         {
      72      3466146 :           *list_p = t;
      73      3466146 :           return;
      74              :         }
      75      9892027 :       *list_p = list = alloc_stmt_list ();
      76              :     }
      77   1505740882 :   else if (TREE_CODE (list) != STATEMENT_LIST)
      78              :     {
      79     20006121 :       tree first = list;
      80     20006121 :       *list_p = list = alloc_stmt_list ();
      81     20006121 :       i = tsi_last (list);
      82     20006121 :       tsi_link_after (&i, first, TSI_CONTINUE_LINKING);
      83              :     }
      84              : 
      85   1515632909 :   i = tsi_last (list);
      86   1515632909 :   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     74474509 : append_to_statement_list (tree t, tree *list_p)
      94              : {
      95     74474509 :   if (t && (TREE_SIDE_EFFECTS (t) || TREE_CODE (t) == DEBUG_BEGIN_STMT))
      96     70138426 :     append_to_statement_list_1 (t, list_p);
      97     74474509 : }
      98              : 
      99              : /* Similar, but the statement is always added, regardless of side effects.  */
     100              : 
     101              : void
     102   1448960629 : append_to_statement_list_force (tree t, tree *list_p)
     103              : {
     104   1448960629 :   if (t != NULL_TREE)
     105   1448960629 :     append_to_statement_list_1 (t, list_p);
     106   1448960629 : }
     107              : 
     108              : /* Links a statement, or a chain of statements, before the current stmt.  */
     109              : 
     110              : void
     111       164056 : tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
     112              : {
     113       164056 :   struct tree_statement_list_node *head, *tail, *cur;
     114              : 
     115              :   /* Die on looping.  */
     116       164056 :   gcc_assert (t != i->container);
     117              : 
     118       164056 :   if (TREE_CODE (t) == STATEMENT_LIST)
     119              :     {
     120        11871 :       head = STATEMENT_LIST_HEAD (t);
     121        11871 :       tail = STATEMENT_LIST_TAIL (t);
     122        11871 :       STATEMENT_LIST_HEAD (t) = NULL;
     123        11871 :       STATEMENT_LIST_TAIL (t) = NULL;
     124              : 
     125        11871 :       free_stmt_list (t);
     126              : 
     127              :       /* Empty statement lists need no work.  */
     128        11871 :       if (!head || !tail)
     129              :         {
     130           15 :           gcc_assert (head == tail);
     131              :           return;
     132              :         }
     133              :     }
     134              :   else
     135              :     {
     136       152185 :       head = ggc_alloc<tree_statement_list_node> ();
     137       152185 :       head->prev = NULL;
     138       152185 :       head->next = NULL;
     139       152185 :       head->stmt = t;
     140       152185 :       tail = head;
     141              :     }
     142              : 
     143       164041 :   if (TREE_CODE (t) != DEBUG_BEGIN_STMT)
     144       164041 :     TREE_SIDE_EFFECTS (i->container) = 1;
     145              : 
     146       164041 :   cur = i->ptr;
     147              : 
     148              :   /* Link it into the list.  */
     149       164041 :   if (cur)
     150              :     {
     151       164038 :       head->prev = cur->prev;
     152       164038 :       if (head->prev)
     153          129 :         head->prev->next = head;
     154              :       else
     155       163909 :         STATEMENT_LIST_HEAD (i->container) = head;
     156       164038 :       tail->next = cur;
     157       164038 :       cur->prev = tail;
     158              :     }
     159              :   else
     160              :     {
     161            3 :       head->prev = STATEMENT_LIST_TAIL (i->container);
     162            3 :       if (head->prev)
     163            0 :        head->prev->next = head;
     164              :       else
     165            3 :        STATEMENT_LIST_HEAD (i->container) = head;
     166            3 :       STATEMENT_LIST_TAIL (i->container) = tail;
     167              :     }
     168              : 
     169              :   /* Update the iterator, if requested.  */
     170       164041 :   switch (mode)
     171              :     {
     172        27965 :     case TSI_NEW_STMT:
     173        27965 :     case TSI_CONTINUE_LINKING:
     174        27965 :     case TSI_CHAIN_START:
     175        27965 :       i->ptr = head;
     176        27965 :       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   1630871333 : tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
     189              : {
     190   1630871333 :   struct tree_statement_list_node *head, *tail, *cur;
     191              : 
     192              :   /* Die on looping.  */
     193   1630871333 :   gcc_assert (t != i->container);
     194              : 
     195   1630871333 :   if (TREE_CODE (t) == STATEMENT_LIST)
     196              :     {
     197     52384755 :       head = STATEMENT_LIST_HEAD (t);
     198     52384755 :       tail = STATEMENT_LIST_TAIL (t);
     199     52384755 :       STATEMENT_LIST_HEAD (t) = NULL;
     200     52384755 :       STATEMENT_LIST_TAIL (t) = NULL;
     201              : 
     202     52384755 :       free_stmt_list (t);
     203              : 
     204              :       /* Empty statement lists need no work.  */
     205     52384755 :       if (!head || !tail)
     206              :         {
     207     16452619 :           gcc_assert (head == tail);
     208              :           return;
     209              :         }
     210              :     }
     211              :   else
     212              :     {
     213   1578486578 :       head = ggc_alloc<tree_statement_list_node> ();
     214   1578486578 :       head->prev = NULL;
     215   1578486578 :       head->next = NULL;
     216   1578486578 :       head->stmt = t;
     217   1578486578 :       tail = head;
     218              :     }
     219              : 
     220   1614418714 :   if (TREE_CODE (t) != DEBUG_BEGIN_STMT)
     221   1237598482 :     TREE_SIDE_EFFECTS (i->container) = 1;
     222              : 
     223   1614418714 :   cur = i->ptr;
     224              : 
     225              :   /* Link it into the list.  */
     226   1614418714 :   if (cur)
     227              :     {
     228    764741378 :       tail->next = cur->next;
     229    764741378 :       if (tail->next)
     230           35 :         tail->next->prev = tail;
     231              :       else
     232    764741343 :         STATEMENT_LIST_TAIL (i->container) = tail;
     233    764741378 :       head->prev = cur;
     234    764741378 :       cur->next = head;
     235              :     }
     236              :   else
     237              :     {
     238    849677336 :       gcc_assert (!STATEMENT_LIST_TAIL (i->container));
     239    849677336 :       STATEMENT_LIST_HEAD (i->container) = head;
     240    849677336 :       STATEMENT_LIST_TAIL (i->container) = tail;
     241              :     }
     242              : 
     243              :   /* Update the iterator, if requested.  */
     244   1614418714 :   switch (mode)
     245              :     {
     246            0 :     case TSI_NEW_STMT:
     247            0 :     case TSI_CHAIN_START:
     248            0 :       i->ptr = head;
     249            0 :       break;
     250   1614418510 :     case TSI_CONTINUE_LINKING:
     251   1614418510 :     case TSI_CHAIN_END:
     252   1614418510 :       i->ptr = tail;
     253   1614418510 :       break;
     254          204 :     case TSI_SAME_STMT:
     255          204 :       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    558522080 : tsi_delink (tree_stmt_iterator *i)
     265              : {
     266    558522080 :   struct tree_statement_list_node *cur, *next, *prev;
     267              : 
     268    558522080 :   cur = i->ptr;
     269    558522080 :   next = cur->next;
     270    558522080 :   prev = cur->prev;
     271              : 
     272    558522080 :   if (prev)
     273        74054 :     prev->next = next;
     274              :   else
     275    558448026 :     STATEMENT_LIST_HEAD (i->container) = next;
     276    558522080 :   if (next)
     277     29635661 :     next->prev = prev;
     278              :   else
     279    528886419 :     STATEMENT_LIST_TAIL (i->container) = prev;
     280              : 
     281    558522080 :   if (!next && !prev)
     282    528876392 :     TREE_SIDE_EFFECTS (i->container) = 0;
     283              : 
     284    558522080 :   i->ptr = next;
     285    558522080 : }
     286              : 
     287              : /* Split a STATEMENT_LIST in I.contrainer into two, all statements
     288              :    from the start until I.ptr inclusive will remain in the original
     289              :    one, all statements after I.ptr are removed from that STATEMENT_LIST
     290              :    and returned as a new STATEMENT_LIST.  If I is the last statement,
     291              :    an empty statement with LOC location is returned.  */
     292              : 
     293              : tree
     294         9301 : tsi_split_stmt_list (location_t loc, tree_stmt_iterator i)
     295              : {
     296         9301 :   if (tsi_one_before_end_p (i))
     297           57 :     return build_empty_stmt (loc);
     298         9244 :   tsi_next (&i);
     299         9244 :   tree ret = NULL_TREE;
     300        81897 :   while (!tsi_end_p (i))
     301              :     {
     302        72653 :       tree t = tsi_stmt (i);
     303        72653 :       tsi_delink (&i);
     304        72653 :       append_to_statement_list_force (t, &ret);
     305              :     }
     306         9244 :   return ret;
     307              : }
     308              : 
     309              : /* Return the first expression in a sequence of COMPOUND_EXPRs, or in
     310              :    a STATEMENT_LIST, disregarding DEBUG_BEGIN_STMTs, recursing into a
     311              :    STATEMENT_LIST if that's the first non-DEBUG_BEGIN_STMT.  */
     312              : 
     313              : tree
     314     13002032 : expr_first (tree expr)
     315              : {
     316     13003312 :   if (expr == NULL_TREE)
     317              :     return expr;
     318              : 
     319     13003282 :   if (TREE_CODE (expr) == STATEMENT_LIST)
     320              :     {
     321      4440852 :       struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
     322      4440852 :       if (!n)
     323              :         return NULL_TREE;
     324      8374429 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
     325              :         {
     326      4029969 :           n = n->next;
     327      4029969 :           if (!n)
     328              :             return NULL_TREE;
     329              :         }
     330              :       /* If the first non-debug stmt is not a statement list, we
     331              :          already know it's what we're looking for.  */
     332      4344460 :       if (TREE_CODE (n->stmt) != STATEMENT_LIST)
     333              :         return n->stmt;
     334              : 
     335              :       return expr_first (n->stmt);
     336              :     }
     337              : 
     338      8562431 :   while (TREE_CODE (expr) == COMPOUND_EXPR)
     339            1 :     expr = TREE_OPERAND (expr, 0);
     340              : 
     341              :   return expr;
     342              : }
     343              : 
     344              : /* Return the last expression in a sequence of COMPOUND_EXPRs, or in a
     345              :    STATEMENT_LIST, disregarding DEBUG_BEGIN_STMTs, recursing into a
     346              :    STATEMENT_LIST if that's the last non-DEBUG_BEGIN_STMT.  */
     347              : 
     348              : tree
     349     14062781 : expr_last (tree expr)
     350              : {
     351     14064236 :   if (expr == NULL_TREE)
     352              :     return expr;
     353              : 
     354     13886852 :   if (TREE_CODE (expr) == STATEMENT_LIST)
     355              :     {
     356      7124835 :       struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
     357      7124835 :       if (!n)
     358              :         return NULL_TREE;
     359      8988612 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
     360              :         {
     361      3633232 :           n = n->prev;
     362      3633232 :           if (!n)
     363              :             return NULL_TREE;
     364              :         }
     365              :       /* If the last non-debug stmt is not a statement list, we
     366              :          already know it's what we're looking for.  */
     367      5355380 :       if (TREE_CODE (n->stmt) != STATEMENT_LIST)
     368              :         return n->stmt;
     369              : 
     370              :       return expr_last (n->stmt);
     371              :     }
     372              : 
     373      6773168 :   while (TREE_CODE (expr) == COMPOUND_EXPR)
     374        11151 :     expr = TREE_OPERAND (expr, 1);
     375              : 
     376              :   return expr;
     377              : }
     378              : 
     379              : /* If EXPR is a STATEMENT_LIST containing just DEBUG_BEGIN_STMTs and
     380              :    a single other stmt, return that other stmt (recursively).
     381              :    If it is a STATEMENT_LIST containing no non-DEBUG_BEGIN_STMTs or
     382              :    multiple, return NULL_TREE.
     383              :    Otherwise return EXPR.  */
     384              : 
     385              : tree
     386     36267639 : expr_single (tree expr)
     387              : {
     388     36313709 :   if (expr == NULL_TREE)
     389              :     return expr;
     390              : 
     391     33634000 :   if (TREE_CODE (expr) == STATEMENT_LIST)
     392              :     {
     393              :       /* With -gstatement-frontiers we could have a STATEMENT_LIST with
     394              :          DEBUG_BEGIN_STMT(s) and only a single other stmt, which with
     395              :          -g wouldn't be present and we'd have that single other stmt
     396              :          directly instead.  */
     397       176977 :       struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
     398       176977 :       if (!n)
     399              :         return NULL_TREE;
     400       223259 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
     401              :         {
     402        52513 :           n = n->next;
     403        52513 :           if (!n)
     404              :             return NULL_TREE;
     405              :         }
     406       177510 :       expr = n->stmt;
     407       177510 :       do
     408              :         {
     409       177510 :           n = n->next;
     410       177510 :           if (!n)
     411              :             return expr_single (expr);
     412              :         }
     413       131440 :       while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT);
     414              :       return NULL_TREE;
     415              :     }
     416              : 
     417              :   return expr;
     418              : }
     419              : 
     420              : #include "gt-tree-iterator.h"
        

Generated by: LCOV version 2.4-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.