1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
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)
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.
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/>. */
23 #include "coretypes.h"
25 #include "tree-iterator.h"
28 /* This is a cache of STATEMENT_LIST nodes. We create and destroy them
29 fairly often during gimplification. */
31 static GTY ((deletable (""))) vec<tree, va_gc> *stmt_list_cache;
34 alloc_stmt_list (void)
37 if (!vec_safe_is_empty (stmt_list_cache))
39 list = stmt_list_cache->pop ();
40 memset (list, 0, sizeof (struct tree_base));
41 TREE_SET_CODE (list, STATEMENT_LIST);
45 list = make_node (STATEMENT_LIST);
46 TREE_SIDE_EFFECTS (list) = 0;
48 TREE_TYPE (list) = void_type_node;
53 free_stmt_list (tree t)
55 gcc_assert (!STATEMENT_LIST_HEAD (t));
56 gcc_assert (!STATEMENT_LIST_TAIL (t));
57 vec_safe_push (stmt_list_cache, t);
60 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
63 append_to_statement_list_1 (tree t, tree *list_p)
70 if (t && TREE_CODE (t) == STATEMENT_LIST)
75 *list_p = list = alloc_stmt_list ();
77 else if (TREE_CODE (list) != STATEMENT_LIST)
80 *list_p = list = alloc_stmt_list ();
82 tsi_link_after (&i, first, TSI_CONTINUE_LINKING);
86 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
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. */
93 append_to_statement_list (tree t, tree *list_p)
95 if (t && (TREE_SIDE_EFFECTS (t) || TREE_CODE (t) == DEBUG_BEGIN_STMT))
96 append_to_statement_list_1 (t, list_p);
99 /* Similar, but the statement is always added, regardless of side effects. */
102 append_to_statement_list_force (tree t, tree *list_p)
105 append_to_statement_list_1 (t, list_p);
108 /* Links a statement, or a chain of statements, before the current stmt. */
111 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
113 struct tree_statement_list_node *head, *tail, *cur;
115 /* Die on looping. */
116 gcc_assert (t != i->container);
118 if (TREE_CODE (t) == STATEMENT_LIST)
120 head = STATEMENT_LIST_HEAD (t);
121 tail = STATEMENT_LIST_TAIL (t);
122 STATEMENT_LIST_HEAD (t) = NULL;
123 STATEMENT_LIST_TAIL (t) = NULL;
127 /* Empty statement lists need no work. */
130 gcc_assert (head == tail);
136 head = ggc_alloc<tree_statement_list_node> ();
143 if (TREE_CODE (t) != DEBUG_BEGIN_STMT)
144 TREE_SIDE_EFFECTS (i->container) = 1;
148 /* Link it into the list. */
151 head->prev = cur->prev;
153 head->prev->next = head;
155 STATEMENT_LIST_HEAD (i->container) = head;
161 head->prev = STATEMENT_LIST_TAIL (i->container);
163 head->prev->next = head;
165 STATEMENT_LIST_HEAD (i->container) = head;
166 STATEMENT_LIST_TAIL (i->container) = tail;
169 /* Update the iterator, if requested. */
173 case TSI_CONTINUE_LINKING:
174 case TSI_CHAIN_START:
185 /* Links a statement, or a chain of statements, after the current stmt. */
188 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
190 struct tree_statement_list_node *head, *tail, *cur;
192 /* Die on looping. */
193 gcc_assert (t != i->container);
195 if (TREE_CODE (t) == STATEMENT_LIST)
197 head = STATEMENT_LIST_HEAD (t);
198 tail = STATEMENT_LIST_TAIL (t);
199 STATEMENT_LIST_HEAD (t) = NULL;
200 STATEMENT_LIST_TAIL (t) = NULL;
204 /* Empty statement lists need no work. */
207 gcc_assert (head == tail);
213 head = ggc_alloc<tree_statement_list_node> ();
220 if (TREE_CODE (t) != DEBUG_BEGIN_STMT)
221 TREE_SIDE_EFFECTS (i->container) = 1;
225 /* Link it into the list. */
228 tail->next = cur->next;
230 tail->next->prev = tail;
232 STATEMENT_LIST_TAIL (i->container) = tail;
238 gcc_assert (!STATEMENT_LIST_TAIL (i->container));
239 STATEMENT_LIST_HEAD (i->container) = head;
240 STATEMENT_LIST_TAIL (i->container) = tail;
243 /* Update the iterator, if requested. */
247 case TSI_CHAIN_START:
250 case TSI_CONTINUE_LINKING:
260 /* Remove a stmt from the tree list. The iterator is updated to point to
264 tsi_delink (tree_stmt_iterator *i)
266 struct tree_statement_list_node *cur, *next, *prev;
275 STATEMENT_LIST_HEAD (i->container) = next;
279 STATEMENT_LIST_TAIL (i->container) = prev;
282 TREE_SIDE_EFFECTS (i->container) = 0;
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. */
292 expr_first (tree expr)
294 if (expr == NULL_TREE)
297 if (TREE_CODE (expr) == STATEMENT_LIST)
299 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
302 while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
308 /* If the first non-debug stmt is not a statement list, we
309 already know it's what we're looking for. */
310 if (TREE_CODE (n->stmt) != STATEMENT_LIST)
313 return expr_first (n->stmt);
316 while (TREE_CODE (expr) == COMPOUND_EXPR)
317 expr = TREE_OPERAND (expr, 0);
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. */
327 expr_last (tree expr)
329 if (expr == NULL_TREE)
332 if (TREE_CODE (expr) == STATEMENT_LIST)
334 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
337 while (TREE_CODE (n->stmt) == DEBUG_BEGIN_STMT)
343 /* If the last non-debug stmt is not a statement list, we
344 already know it's what we're looking for. */
345 if (TREE_CODE (n->stmt) != STATEMENT_LIST)
348 return expr_last (n->stmt);
351 while (TREE_CODE (expr) == COMPOUND_EXPR)
352 expr = TREE_OPERAND (expr, 1);
357 #include "gt-tree-iterator.h"