2016-11-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / gimple-iterator.h
bloba58b0a4b5df65f51c59950ad50ebadbb8c8d733f
1 /* Header file for gimple iterators.
2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifndef GCC_GIMPLE_ITERATOR_H
21 #define GCC_GIMPLE_ITERATOR_H
23 /* Iterator object for GIMPLE statement sequences. */
25 struct gimple_stmt_iterator
27 /* Sequence node holding the current statement. */
28 gimple_seq_node ptr;
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;
38 /* Iterator over GIMPLE_PHI statements. */
39 struct gphi_iterator : public gimple_stmt_iterator
41 gphi *phi () const
43 return as_a <gphi *> (ptr);
47 enum gsi_iterator_update
49 GSI_NEW_STMT, /* Only valid when single statement is added, move
50 iterator to it. */
51 GSI_SAME_STMT, /* Leave the iterator at the same statement. */
52 GSI_CONTINUE_LINKING /* Move iterator to whatever position is suitable
53 for linking other statements in the same
54 direction. */
57 extern void gsi_insert_seq_before_without_update (gimple_stmt_iterator *,
58 gimple_seq,
59 enum gsi_iterator_update);
60 extern void gsi_insert_seq_before (gimple_stmt_iterator *, gimple_seq,
61 enum gsi_iterator_update);
62 extern void gsi_insert_seq_after_without_update (gimple_stmt_iterator *,
63 gimple_seq,
64 enum gsi_iterator_update);
65 extern void gsi_insert_seq_after (gimple_stmt_iterator *, gimple_seq,
66 enum gsi_iterator_update);
67 extern gimple_seq gsi_split_seq_after (gimple_stmt_iterator);
68 extern void gsi_set_stmt (gimple_stmt_iterator *, gimple *);
69 extern void gsi_split_seq_before (gimple_stmt_iterator *, gimple_seq *);
70 extern bool gsi_replace (gimple_stmt_iterator *, gimple *, bool);
71 extern void gsi_replace_with_seq (gimple_stmt_iterator *, gimple_seq, bool);
72 extern void gsi_insert_before_without_update (gimple_stmt_iterator *, gimple *,
73 enum gsi_iterator_update);
74 extern void gsi_insert_before (gimple_stmt_iterator *, gimple *,
75 enum gsi_iterator_update);
76 extern void gsi_insert_after_without_update (gimple_stmt_iterator *, gimple *,
77 enum gsi_iterator_update);
78 extern void gsi_insert_after (gimple_stmt_iterator *, gimple *,
79 enum gsi_iterator_update);
80 extern bool gsi_remove (gimple_stmt_iterator *, bool);
81 extern gimple_stmt_iterator gsi_for_stmt (gimple *);
82 extern gphi_iterator gsi_for_phi (gphi *);
83 extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
84 extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
85 extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
86 extern void gsi_insert_on_edge (edge, gimple *);
87 extern void gsi_insert_seq_on_edge (edge, gimple_seq);
88 extern basic_block gsi_insert_on_edge_immediate (edge, gimple *);
89 extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
90 extern void gsi_commit_edge_inserts (void);
91 extern void gsi_commit_one_edge_insert (edge, basic_block *);
92 extern gphi_iterator gsi_start_phis (basic_block);
93 extern void update_modified_stmts (gimple_seq);
95 /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
97 static inline gimple_stmt_iterator
98 gsi_start_1 (gimple_seq *seq)
100 gimple_stmt_iterator i;
102 i.ptr = gimple_seq_first (*seq);
103 i.seq = seq;
104 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
106 return i;
109 #define gsi_start(x) gsi_start_1 (&(x))
111 static inline gimple_stmt_iterator
112 gsi_none (void)
114 gimple_stmt_iterator i;
115 i.ptr = NULL;
116 i.seq = NULL;
117 i.bb = NULL;
118 return i;
121 /* Return a new iterator pointing to the first statement in basic block BB. */
123 static inline gimple_stmt_iterator
124 gsi_start_bb (basic_block bb)
126 gimple_stmt_iterator i;
127 gimple_seq *seq;
129 seq = bb_seq_addr (bb);
130 i.ptr = gimple_seq_first (*seq);
131 i.seq = seq;
132 i.bb = bb;
134 return i;
137 gimple_stmt_iterator gsi_start_edge (edge e);
139 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
141 static inline gimple_stmt_iterator
142 gsi_last_1 (gimple_seq *seq)
144 gimple_stmt_iterator i;
146 i.ptr = gimple_seq_last (*seq);
147 i.seq = seq;
148 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
150 return i;
153 #define gsi_last(x) gsi_last_1 (&(x))
155 /* Return a new iterator pointing to the last statement in basic block BB. */
157 static inline gimple_stmt_iterator
158 gsi_last_bb (basic_block bb)
160 gimple_stmt_iterator i;
161 gimple_seq *seq;
163 seq = bb_seq_addr (bb);
164 i.ptr = gimple_seq_last (*seq);
165 i.seq = seq;
166 i.bb = bb;
168 return i;
171 /* Return true if I is at the end of its sequence. */
173 static inline bool
174 gsi_end_p (gimple_stmt_iterator i)
176 return i.ptr == NULL;
179 /* Return true if I is one statement before the end of its sequence. */
181 static inline bool
182 gsi_one_before_end_p (gimple_stmt_iterator i)
184 return i.ptr != NULL && i.ptr->next == NULL;
187 /* Advance the iterator to the next gimple statement. */
189 static inline void
190 gsi_next (gimple_stmt_iterator *i)
192 i->ptr = i->ptr->next;
195 /* Advance the iterator to the previous gimple statement. */
197 static inline void
198 gsi_prev (gimple_stmt_iterator *i)
200 gimple *prev = i->ptr->prev;
201 if (prev->next)
202 i->ptr = prev;
203 else
204 i->ptr = NULL;
207 /* Return the current stmt. */
209 static inline gimple *
210 gsi_stmt (gimple_stmt_iterator i)
212 return i.ptr;
215 /* Return a new iterator pointing to the first non-debug statement
216 in basic block BB. */
218 static inline gimple_stmt_iterator
219 gsi_start_bb_nondebug (basic_block bb)
221 gimple_stmt_iterator gsi = gsi_start_bb (bb);
222 while (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
223 gsi_next (&gsi);
225 return gsi;
228 /* Return a block statement iterator that points to the first non-label
229 statement in block BB. */
231 static inline gimple_stmt_iterator
232 gsi_after_labels (basic_block bb)
234 gimple_stmt_iterator gsi = gsi_start_bb (bb);
236 while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
237 gsi_next (&gsi);
239 return gsi;
242 /* Advance the iterator to the next non-debug gimple statement. */
244 static inline void
245 gsi_next_nondebug (gimple_stmt_iterator *i)
249 gsi_next (i);
251 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
254 /* Advance the iterator to the previous non-debug gimple statement. */
256 static inline void
257 gsi_prev_nondebug (gimple_stmt_iterator *i)
261 gsi_prev (i);
263 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
266 /* Return a new iterator pointing to the first non-debug statement in
267 basic block BB. */
269 static inline gimple_stmt_iterator
270 gsi_start_nondebug_bb (basic_block bb)
272 gimple_stmt_iterator i = gsi_start_bb (bb);
274 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
275 gsi_next_nondebug (&i);
277 return i;
280 /* Return a new iterator pointing to the first non-debug non-label statement in
281 basic block BB. */
283 static inline gimple_stmt_iterator
284 gsi_start_nondebug_after_labels_bb (basic_block bb)
286 gimple_stmt_iterator i = gsi_after_labels (bb);
288 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
289 gsi_next_nondebug (&i);
291 return i;
294 /* Return a new iterator pointing to the last non-debug statement in
295 basic block BB. */
297 static inline gimple_stmt_iterator
298 gsi_last_nondebug_bb (basic_block bb)
300 gimple_stmt_iterator i = gsi_last_bb (bb);
302 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
303 gsi_prev_nondebug (&i);
305 return i;
308 /* Iterates I statement iterator to the next non-virtual statement. */
310 static inline void
311 gsi_next_nonvirtual_phi (gphi_iterator *i)
313 gphi *phi;
315 if (gsi_end_p (*i))
316 return;
318 phi = i->phi ();
319 gcc_assert (phi != NULL);
321 while (virtual_operand_p (gimple_phi_result (phi)))
323 gsi_next (i);
325 if (gsi_end_p (*i))
326 return;
328 phi = i->phi ();
332 /* Return the basic block associated with this iterator. */
334 static inline basic_block
335 gsi_bb (gimple_stmt_iterator i)
337 return i.bb;
340 /* Return the sequence associated with this iterator. */
342 static inline gimple_seq
343 gsi_seq (gimple_stmt_iterator i)
345 return *i.seq;
348 /* Determine whether SEQ is a nondebug singleton. */
350 static inline bool
351 gimple_seq_nondebug_singleton_p (gimple_seq seq)
353 gimple_stmt_iterator gsi;
355 /* Find a nondebug gimple. */
356 gsi.ptr = gimple_seq_first (seq);
357 gsi.seq = &seq;
358 gsi.bb = NULL;
359 while (!gsi_end_p (gsi)
360 && is_gimple_debug (gsi_stmt (gsi)))
361 gsi_next (&gsi);
363 /* No nondebug gimple found, not a singleton. */
364 if (gsi_end_p (gsi))
365 return false;
367 /* Find a next nondebug gimple. */
368 gsi_next (&gsi);
369 while (!gsi_end_p (gsi)
370 && is_gimple_debug (gsi_stmt (gsi)))
371 gsi_next (&gsi);
373 /* Only a singleton if there's no next nondebug gimple. */
374 return gsi_end_p (gsi);
377 #endif /* GCC_GIMPLE_ITERATOR_H */