re PR tree-optimization/62112 (Optimize out malloc when block is unused or write...
[gcc.git] / gcc / gimple-iterator.h
1 /* Header file for gimple iterators.
2 Copyright (C) 2013-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #ifndef GCC_GIMPLE_ITERATOR_H
21 #define GCC_GIMPLE_ITERATOR_H
22
23 /* Iterator object for GIMPLE statement sequences. */
24
25 struct gimple_stmt_iterator
26 {
27 /* Sequence node holding the current statement. */
28 gimple_seq_node ptr;
29
30 /* Sequence and basic block holding the statement. These fields
31 are necessary to handle edge cases such as when statement is
32 added to an empty basic block or when the last statement of a
33 block/sequence is removed. */
34 gimple_seq *seq;
35 basic_block bb;
36 };
37
38 enum gsi_iterator_update
39 {
40 GSI_NEW_STMT, /* Only valid when single statement is added, move
41 iterator to it. */
42 GSI_SAME_STMT, /* Leave the iterator at the same statement. */
43 GSI_CONTINUE_LINKING /* Move iterator to whatever position is suitable
44 for linking other statements in the same
45 direction. */
46 };
47
48 extern void gsi_insert_seq_before_without_update (gimple_stmt_iterator *,
49 gimple_seq,
50 enum gsi_iterator_update);
51 extern void gsi_insert_seq_before (gimple_stmt_iterator *, gimple_seq,
52 enum gsi_iterator_update);
53 extern void gsi_insert_seq_after_without_update (gimple_stmt_iterator *,
54 gimple_seq,
55 enum gsi_iterator_update);
56 extern void gsi_insert_seq_after (gimple_stmt_iterator *, gimple_seq,
57 enum gsi_iterator_update);
58 extern gimple_seq gsi_split_seq_after (gimple_stmt_iterator);
59 extern void gsi_set_stmt (gimple_stmt_iterator *, gimple);
60 extern void gsi_split_seq_before (gimple_stmt_iterator *, gimple_seq *);
61 extern bool gsi_replace (gimple_stmt_iterator *, gimple, bool);
62 extern void gsi_replace_with_seq (gimple_stmt_iterator *, gimple_seq, bool);
63 extern void gsi_insert_before_without_update (gimple_stmt_iterator *, gimple,
64 enum gsi_iterator_update);
65 extern void gsi_insert_before (gimple_stmt_iterator *, gimple,
66 enum gsi_iterator_update);
67 extern void gsi_insert_after_without_update (gimple_stmt_iterator *, gimple,
68 enum gsi_iterator_update);
69 extern void gsi_insert_after (gimple_stmt_iterator *, gimple,
70 enum gsi_iterator_update);
71 extern bool gsi_remove (gimple_stmt_iterator *, bool);
72 extern gimple_stmt_iterator gsi_for_stmt (gimple);
73 extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
74 extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
75 extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
76 extern void gsi_insert_on_edge (edge, gimple);
77 extern void gsi_insert_seq_on_edge (edge, gimple_seq);
78 extern basic_block gsi_insert_on_edge_immediate (edge, gimple);
79 extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
80 extern void gsi_commit_edge_inserts (void);
81 extern void gsi_commit_one_edge_insert (edge, basic_block *);
82 extern gimple_stmt_iterator gsi_start_phis (basic_block);
83
84 /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
85
86 static inline gimple_stmt_iterator
87 gsi_start_1 (gimple_seq *seq)
88 {
89 gimple_stmt_iterator i;
90
91 i.ptr = gimple_seq_first (*seq);
92 i.seq = seq;
93 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
94
95 return i;
96 }
97
98 #define gsi_start(x) gsi_start_1 (&(x))
99
100 static inline gimple_stmt_iterator
101 gsi_none (void)
102 {
103 gimple_stmt_iterator i;
104 i.ptr = NULL;
105 i.seq = NULL;
106 i.bb = NULL;
107 return i;
108 }
109
110 /* Return a new iterator pointing to the first statement in basic block BB. */
111
112 static inline gimple_stmt_iterator
113 gsi_start_bb (basic_block bb)
114 {
115 gimple_stmt_iterator i;
116 gimple_seq *seq;
117
118 seq = bb_seq_addr (bb);
119 i.ptr = gimple_seq_first (*seq);
120 i.seq = seq;
121 i.bb = bb;
122
123 return i;
124 }
125
126 gimple_stmt_iterator gsi_start_edge (edge e);
127
128 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
129
130 static inline gimple_stmt_iterator
131 gsi_last_1 (gimple_seq *seq)
132 {
133 gimple_stmt_iterator i;
134
135 i.ptr = gimple_seq_last (*seq);
136 i.seq = seq;
137 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
138
139 return i;
140 }
141
142 #define gsi_last(x) gsi_last_1 (&(x))
143
144 /* Return a new iterator pointing to the last statement in basic block BB. */
145
146 static inline gimple_stmt_iterator
147 gsi_last_bb (basic_block bb)
148 {
149 gimple_stmt_iterator i;
150 gimple_seq *seq;
151
152 seq = bb_seq_addr (bb);
153 i.ptr = gimple_seq_last (*seq);
154 i.seq = seq;
155 i.bb = bb;
156
157 return i;
158 }
159
160 /* Return true if I is at the end of its sequence. */
161
162 static inline bool
163 gsi_end_p (gimple_stmt_iterator i)
164 {
165 return i.ptr == NULL;
166 }
167
168 /* Return true if I is one statement before the end of its sequence. */
169
170 static inline bool
171 gsi_one_before_end_p (gimple_stmt_iterator i)
172 {
173 return i.ptr != NULL && i.ptr->next == NULL;
174 }
175
176 /* Advance the iterator to the next gimple statement. */
177
178 static inline void
179 gsi_next (gimple_stmt_iterator *i)
180 {
181 i->ptr = i->ptr->next;
182 }
183
184 /* Advance the iterator to the previous gimple statement. */
185
186 static inline void
187 gsi_prev (gimple_stmt_iterator *i)
188 {
189 gimple prev = i->ptr->prev;
190 if (prev->next)
191 i->ptr = prev;
192 else
193 i->ptr = NULL;
194 }
195
196 /* Return the current stmt. */
197
198 static inline gimple
199 gsi_stmt (gimple_stmt_iterator i)
200 {
201 return i.ptr;
202 }
203
204 /* Return a block statement iterator that points to the first non-label
205 statement in block BB. */
206
207 static inline gimple_stmt_iterator
208 gsi_after_labels (basic_block bb)
209 {
210 gimple_stmt_iterator gsi = gsi_start_bb (bb);
211
212 while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
213 gsi_next (&gsi);
214
215 return gsi;
216 }
217
218 /* Advance the iterator to the next non-debug gimple statement. */
219
220 static inline void
221 gsi_next_nondebug (gimple_stmt_iterator *i)
222 {
223 do
224 {
225 gsi_next (i);
226 }
227 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
228 }
229
230 /* Advance the iterator to the next non-debug gimple statement. */
231
232 static inline void
233 gsi_prev_nondebug (gimple_stmt_iterator *i)
234 {
235 do
236 {
237 gsi_prev (i);
238 }
239 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
240 }
241
242 /* Return a new iterator pointing to the first non-debug statement in
243 basic block BB. */
244
245 static inline gimple_stmt_iterator
246 gsi_start_nondebug_bb (basic_block bb)
247 {
248 gimple_stmt_iterator i = gsi_start_bb (bb);
249
250 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
251 gsi_next_nondebug (&i);
252
253 return i;
254 }
255
256 /* Return a new iterator pointing to the first non-debug non-label statement in
257 basic block BB. */
258
259 static inline gimple_stmt_iterator
260 gsi_start_nondebug_after_labels_bb (basic_block bb)
261 {
262 gimple_stmt_iterator i = gsi_after_labels (bb);
263
264 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
265 gsi_next_nondebug (&i);
266
267 return i;
268 }
269
270 /* Return a new iterator pointing to the last non-debug statement in
271 basic block BB. */
272
273 static inline gimple_stmt_iterator
274 gsi_last_nondebug_bb (basic_block bb)
275 {
276 gimple_stmt_iterator i = gsi_last_bb (bb);
277
278 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
279 gsi_prev_nondebug (&i);
280
281 return i;
282 }
283
284 /* Iterates I statement iterator to the next non-virtual statement. */
285
286 static inline void
287 gsi_next_nonvirtual_phi (gimple_stmt_iterator *i)
288 {
289 gimple phi;
290
291 if (gsi_end_p (*i))
292 return;
293
294 phi = gsi_stmt (*i);
295 gcc_assert (phi != NULL);
296
297 while (virtual_operand_p (gimple_phi_result (phi)))
298 {
299 gsi_next (i);
300
301 if (gsi_end_p (*i))
302 return;
303
304 phi = gsi_stmt (*i);
305 }
306 }
307
308 /* Return the basic block associated with this iterator. */
309
310 static inline basic_block
311 gsi_bb (gimple_stmt_iterator i)
312 {
313 return i.bb;
314 }
315
316 /* Return the sequence associated with this iterator. */
317
318 static inline gimple_seq
319 gsi_seq (gimple_stmt_iterator i)
320 {
321 return *i.seq;
322 }
323
324 #endif /* GCC_GIMPLE_ITERATOR_H */