common.opt (ftree-loop-optimize): New flag.
[gcc.git] / gcc / tree-iterator.c
1 /* Iterator routines for manipulating GENERIC and GIMPLE tree statements.
2 Copyright (C) 2003, 2004 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "tree-gimple.h"
27 #include "tree-iterator.h"
28 #include "ggc.h"
29
30
31 /* This is a cache of STATEMENT_LIST nodes. We create and destroy them
32 fairly often during gimplification. */
33
34 static GTY ((deletable (""))) tree stmt_list_cache;
35
36 tree
37 alloc_stmt_list (void)
38 {
39 tree list = stmt_list_cache;
40 if (list)
41 {
42 stmt_list_cache = TREE_CHAIN (list);
43 memset (list, 0, sizeof(struct tree_common));
44 TREE_SET_CODE (list, STATEMENT_LIST);
45 }
46 else
47 list = make_node (STATEMENT_LIST);
48 TREE_TYPE (list) = void_type_node;
49 return list;
50 }
51
52 void
53 free_stmt_list (tree t)
54 {
55 #ifdef ENABLE_CHECKING
56 if (STATEMENT_LIST_HEAD (t) || STATEMENT_LIST_TAIL (t))
57 abort ();
58 #endif
59 TREE_CHAIN (t) = stmt_list_cache;
60 stmt_list_cache = t;
61 }
62
63 /* Links a statement, or a chain of statements, before the current stmt. */
64
65 void
66 tsi_link_before (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
67 {
68 struct tree_statement_list_node *head, *tail, *cur;
69
70 /* Die on looping. */
71 if (t == i->container)
72 abort ();
73
74 if (TREE_CODE (t) == STATEMENT_LIST)
75 {
76 head = STATEMENT_LIST_HEAD (t);
77 tail = STATEMENT_LIST_TAIL (t);
78 STATEMENT_LIST_HEAD (t) = NULL;
79 STATEMENT_LIST_TAIL (t) = NULL;
80
81 free_stmt_list (t);
82
83 /* Empty statement lists need no work. */
84 if (!head || !tail)
85 {
86 if (head != tail)
87 abort ();
88 return;
89 }
90 }
91 else
92 {
93 head = ggc_alloc (sizeof (*head));
94 head->prev = NULL;
95 head->next = NULL;
96 head->stmt = t;
97 tail = head;
98 }
99
100 TREE_SIDE_EFFECTS (i->container) = 1;
101
102 cur = i->ptr;
103
104 /* Link it into the list. */
105 if (cur)
106 {
107 head->prev = cur->prev;
108 if (head->prev)
109 head->prev->next = head;
110 else
111 STATEMENT_LIST_HEAD (i->container) = head;
112 tail->next = cur;
113 cur->prev = tail;
114 }
115 else
116 {
117 if (STATEMENT_LIST_TAIL (i->container))
118 abort ();
119 STATEMENT_LIST_HEAD (i->container) = head;
120 STATEMENT_LIST_TAIL (i->container) = tail;
121 }
122
123 /* Update the iterator, if requested. */
124 switch (mode)
125 {
126 case TSI_NEW_STMT:
127 case TSI_CONTINUE_LINKING:
128 case TSI_CHAIN_START:
129 i->ptr = head;
130 break;
131 case TSI_CHAIN_END:
132 i->ptr = tail;
133 break;
134 case TSI_SAME_STMT:
135 if (!cur)
136 abort ();
137 break;
138 }
139 }
140
141 /* Links a statement, or a chain of statements, after the current stmt. */
142
143 void
144 tsi_link_after (tree_stmt_iterator *i, tree t, enum tsi_iterator_update mode)
145 {
146 struct tree_statement_list_node *head, *tail, *cur;
147
148 /* Die on looping. */
149 if (t == i->container)
150 abort ();
151
152 if (TREE_CODE (t) == STATEMENT_LIST)
153 {
154 head = STATEMENT_LIST_HEAD (t);
155 tail = STATEMENT_LIST_TAIL (t);
156 STATEMENT_LIST_HEAD (t) = NULL;
157 STATEMENT_LIST_TAIL (t) = NULL;
158
159 free_stmt_list (t);
160
161 /* Empty statement lists need no work. */
162 if (!head || !tail)
163 {
164 if (head != tail)
165 abort ();
166 return;
167 }
168 }
169 else
170 {
171 head = ggc_alloc (sizeof (*head));
172 head->prev = NULL;
173 head->next = NULL;
174 head->stmt = t;
175 tail = head;
176 }
177
178 TREE_SIDE_EFFECTS (i->container) = 1;
179
180 cur = i->ptr;
181
182 /* Link it into the list. */
183 if (cur)
184 {
185 tail->next = cur->next;
186 if (tail->next)
187 tail->next->prev = tail;
188 else
189 STATEMENT_LIST_TAIL (i->container) = tail;
190 head->prev = cur;
191 cur->next = head;
192 }
193 else
194 {
195 if (STATEMENT_LIST_TAIL (i->container))
196 abort ();
197 STATEMENT_LIST_HEAD (i->container) = head;
198 STATEMENT_LIST_TAIL (i->container) = tail;
199 }
200
201 /* Update the iterator, if requested. */
202 switch (mode)
203 {
204 case TSI_NEW_STMT:
205 case TSI_CHAIN_START:
206 i->ptr = head;
207 break;
208 case TSI_CONTINUE_LINKING:
209 case TSI_CHAIN_END:
210 i->ptr = tail;
211 break;
212 case TSI_SAME_STMT:
213 if (!cur)
214 abort ();
215 break;
216 }
217 }
218
219 /* Remove a stmt from the tree list. The iterator is updated to point to
220 the next stmt. */
221
222 void
223 tsi_delink (tree_stmt_iterator *i)
224 {
225 struct tree_statement_list_node *cur, *next, *prev;
226
227 cur = i->ptr;
228 next = cur->next;
229 prev = cur->prev;
230
231 if (prev)
232 prev->next = next;
233 else
234 STATEMENT_LIST_HEAD (i->container) = next;
235 if (next)
236 next->prev = prev;
237 else
238 STATEMENT_LIST_TAIL (i->container) = prev;
239
240 if (!next && !prev)
241 TREE_SIDE_EFFECTS (i->container) = 0;
242
243 i->ptr = next;
244 }
245
246 /* Move all statements in the statement list after I to a new
247 statement list. I itself is unchanged. */
248
249 tree
250 tsi_split_statement_list_after (const tree_stmt_iterator *i)
251 {
252 struct tree_statement_list_node *cur, *next;
253 tree old_sl, new_sl;
254
255 cur = i->ptr;
256 /* How can we possibly split after the end, or before the beginning? */
257 if (cur == NULL)
258 abort ();
259 next = cur->next;
260
261 old_sl = i->container;
262 new_sl = alloc_stmt_list ();
263 TREE_SIDE_EFFECTS (new_sl) = 1;
264
265 STATEMENT_LIST_HEAD (new_sl) = next;
266 STATEMENT_LIST_TAIL (new_sl) = STATEMENT_LIST_TAIL (old_sl);
267 STATEMENT_LIST_TAIL (old_sl) = cur;
268 cur->next = NULL;
269 next->prev = NULL;
270
271 return new_sl;
272 }
273
274 /* Move all statements in the statement list before I to a new
275 statement list. I is set to the head of the new list. */
276
277 tree
278 tsi_split_statement_list_before (tree_stmt_iterator *i)
279 {
280 struct tree_statement_list_node *cur, *prev;
281 tree old_sl, new_sl;
282
283 cur = i->ptr;
284 /* How can we possibly split after the end, or before the beginning? */
285 if (cur == NULL)
286 abort ();
287 prev = cur->prev;
288
289 old_sl = i->container;
290 new_sl = alloc_stmt_list ();
291 TREE_SIDE_EFFECTS (new_sl) = 1;
292 i->container = new_sl;
293
294 STATEMENT_LIST_HEAD (new_sl) = cur;
295 STATEMENT_LIST_TAIL (new_sl) = STATEMENT_LIST_TAIL (old_sl);
296 STATEMENT_LIST_TAIL (old_sl) = prev;
297 cur->prev = NULL;
298 prev->next = NULL;
299
300 return new_sl;
301 }
302
303 /* Return the first expression in a sequence of COMPOUND_EXPRs,
304 or in a STATEMENT_LIST. */
305
306 tree
307 expr_first (tree expr)
308 {
309 if (expr == NULL_TREE)
310 return expr;
311
312 if (TREE_CODE (expr) == STATEMENT_LIST)
313 {
314 struct tree_statement_list_node *n = STATEMENT_LIST_HEAD (expr);
315 return n ? n->stmt : NULL_TREE;
316 }
317
318 while (TREE_CODE (expr) == COMPOUND_EXPR)
319 expr = TREE_OPERAND (expr, 0);
320 return expr;
321 }
322
323 /* Return the last expression in a sequence of COMPOUND_EXPRs,
324 or in a STATEMENT_LIST. */
325
326 tree
327 expr_last (tree expr)
328 {
329 if (expr == NULL_TREE)
330 return expr;
331
332 if (TREE_CODE (expr) == STATEMENT_LIST)
333 {
334 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
335 return n ? n->stmt : NULL_TREE;
336 }
337
338 while (TREE_CODE (expr) == COMPOUND_EXPR)
339 expr = TREE_OPERAND (expr, 1);
340 return expr;
341 }
342
343 /* If EXPR is a single statement, naked or in a STATEMENT_LIST, then
344 return it. Otherwise return NULL. */
345
346 tree
347 expr_only (tree expr)
348 {
349 if (expr == NULL_TREE)
350 return NULL_TREE;
351
352 if (TREE_CODE (expr) == STATEMENT_LIST)
353 {
354 struct tree_statement_list_node *n = STATEMENT_LIST_TAIL (expr);
355 if (n && STATEMENT_LIST_HEAD (expr) == n)
356 return n->stmt;
357 else
358 return NULL_TREE;
359 }
360
361 if (TREE_CODE (expr) == COMPOUND_EXPR)
362 return NULL_TREE;
363
364 return expr;
365 }
366
367 #include "gt-tree-iterator.h"