* config/sh/sh.h: Delete dead GO_IF_LEGITIMATE_INDEX macro.
[official-gcc.git] / gcc / tree-iterator.c
blob44b6bed7ad8bb97f367eecb4f98ec8acb3f0e7bb
1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andrew MacLeod <amacleod@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-iterator.h"
28 #include "ggc.h"
31 /* This is a cache of STATEMENT_LIST nodes. We create and destroy them
32 fairly often during gimplification. */
34 static GTY ((deletable (""))) VEC(tree,gc) *stmt_list_cache;
36 tree
37 alloc_stmt_list (void)
39 tree list;
40 if (!VEC_empty (tree, stmt_list_cache))
42 list = VEC_pop (tree, stmt_list_cache);
43 memset (list, 0, sizeof(struct tree_base));
44 TREE_SET_CODE (list, STATEMENT_LIST);
46 else
47 list = make_node (STATEMENT_LIST);
48 TREE_TYPE (list) = void_type_node;
49 return list;
52 void
53 free_stmt_list (tree t)
55 gcc_assert (!STATEMENT_LIST_HEAD (t));
56 gcc_assert (!STATEMENT_LIST_TAIL (t));
57 VEC_safe_push (tree, gc, stmt_list_cache, t);
60 /* A subroutine of append_to_statement_list{,_force}. T is not NULL. */
62 static void
63 append_to_statement_list_1 (tree t, tree *list_p)
65 tree list = *list_p;
66 tree_stmt_iterator i;
68 if (!list)
70 if (t && TREE_CODE (t) == STATEMENT_LIST)
72 *list_p = t;
73 return;
75 *list_p = list = alloc_stmt_list ();
78 i = tsi_last (list);
79 tsi_link_after (&i, t, TSI_CONTINUE_LINKING);
82 /* Add T to the end of the list container pointed to by LIST_P.
83 If T is an expression with no effects, it is ignored. */
85 void
86 append_to_statement_list (tree t, tree *list_p)
88 if (t && TREE_SIDE_EFFECTS (t))
89 append_to_statement_list_1 (t, list_p);
92 /* Similar, but the statement is always added, regardless of side effects. */
94 void
95 append_to_statement_list_force (tree t, tree *list_p)
97 if (t != NULL_TREE)
98 append_to_statement_list_1 (t, list_p);
101 /* Links a statement, or a chain of statements, before the current stmt. */
103 void
104 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
106 struct tree_statement_list_node *head, *tail, *cur;
108 /* Die on looping. */
109 gcc_assert (t != i->container);
111 if (TREE_CODE (t) == STATEMENT_LIST)
113 head = STATEMENT_LIST_HEAD (t);
114 tail = STATEMENT_LIST_TAIL (t);
115 STATEMENT_LIST_HEAD (t) = NULL;
116 STATEMENT_LIST_TAIL (t) = NULL;
118 free_stmt_list (t);
120 /* Empty statement lists need no work. */
121 if (!head || !tail)
123 gcc_assert (head == tail);
124 return;
127 else
129 head = ggc_alloc_tree_statement_list_node ();
130 head->prev = NULL;
131 head->next = NULL;
132 head->stmt = t;
133 tail = head;
136 TREE_SIDE_EFFECTS (i->container) = 1;
138 cur = i->ptr;
140 /* Link it into the list. */
141 if (cur)
143 head->prev = cur->prev;
144 if (head->prev)
145 head->prev->next = head;
146 else
147 STATEMENT_LIST_HEAD (i->container) = head;
148 tail->next = cur;
149 cur->prev = tail;
151 else
153 head->prev = STATEMENT_LIST_TAIL (i->container);
154 if (head->prev)
155 head->prev->next = head;
156 else
157 STATEMENT_LIST_HEAD (i->container) = head;
158 STATEMENT_LIST_TAIL (i->container) = tail;
161 /* Update the iterator, if requested. */
162 switch (mode)
164 case TSI_NEW_STMT:
165 case TSI_CONTINUE_LINKING:
166 case TSI_CHAIN_START:
167 i->ptr = head;
168 break;
169 case TSI_CHAIN_END:
170 i->ptr = tail;
171 break;
172 case TSI_SAME_STMT:
173 break;
177 /* Links a statement, or a chain of statements, after the current stmt. */
179 void
180 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
182 struct tree_statement_list_node *head, *tail, *cur;
184 /* Die on looping. */
185 gcc_assert (t != i->container);
187 if (TREE_CODE (t) == STATEMENT_LIST)
189 head = STATEMENT_LIST_HEAD (t);
190 tail = STATEMENT_LIST_TAIL (t);
191 STATEMENT_LIST_HEAD (t) = NULL;
192 STATEMENT_LIST_TAIL (t) = NULL;
194 free_stmt_list (t);
196 /* Empty statement lists need no work. */
197 if (!head || !tail)
199 gcc_assert (head == tail);
200 return;
203 else
205 head = ggc_alloc_tree_statement_list_node ();
206 head->prev = NULL;
207 head->next = NULL;
208 head->stmt = t;
209 tail = head;
212 TREE_SIDE_EFFECTS (i->container) = 1;
214 cur = i->ptr;
216 /* Link it into the list. */
217 if (cur)
219 tail->next = cur->next;
220 if (tail->next)
221 tail->next->prev = tail;
222 else
223 STATEMENT_LIST_TAIL (i->container) = tail;
224 head->prev = cur;
225 cur->next = head;
227 else
229 gcc_assert (!STATEMENT_LIST_TAIL (i->container));
230 STATEMENT_LIST_HEAD (i->container) = head;
231 STATEMENT_LIST_TAIL (i->container) = tail;
234 /* Update the iterator, if requested. */
235 switch (mode)
237 case TSI_NEW_STMT:
238 case TSI_CHAIN_START:
239 i->ptr = head;
240 break;
241 case TSI_CONTINUE_LINKING:
242 case TSI_CHAIN_END:
243 i->ptr = tail;
244 break;
245 case TSI_SAME_STMT:
246 gcc_assert (cur);
247 break;
251 /* Remove a stmt from the tree list. The iterator is updated to point to
252 the next stmt. */
254 void
255 tsi_delink (tree_stmt_iterator *i)
257 struct tree_statement_list_node *cur, *next, *prev;
259 cur = i->ptr;
260 next = cur->next;
261 prev = cur->prev;
263 if (prev)
264 prev->next = next;
265 else
266 STATEMENT_LIST_HEAD (i->container) = next;
267 if (next)
268 next->prev = prev;
269 else
270 STATEMENT_LIST_TAIL (i->container) = prev;
272 if (!next && !prev)
273 TREE_SIDE_EFFECTS (i->container) = 0;
275 i->ptr = next;
278 /* Return the first expression in a sequence of COMPOUND_EXPRs,
279 or in a STATEMENT_LIST. */
281 tree
282 expr_first (tree expr)
284 if (expr == NULL_TREE)
285 return expr;
287 if (TREE_CODE (expr) == STATEMENT_LIST)
289 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
290 return n ? n->stmt : NULL_TREE;
293 while (TREE_CODE (expr) == COMPOUND_EXPR)
294 expr = TREE_OPERAND (expr, 0);
296 return expr;
299 /* Return the last expression in a sequence of COMPOUND_EXPRs,
300 or in a STATEMENT_LIST. */
302 tree
303 expr_last (tree expr)
305 if (expr == NULL_TREE)
306 return expr;
308 if (TREE_CODE (expr) == STATEMENT_LIST)
310 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
311 return n ? n->stmt : NULL_TREE;
314 while (TREE_CODE (expr) == COMPOUND_EXPR)
315 expr = TREE_OPERAND (expr, 1);
317 return expr;
320 #include "gt-tree-iterator.h"