[42/46] Add vec_info::replace_stmt
[official-gcc.git] / gcc / gimple-iterator.h
blobe23d4b2c47c5b0660fca6ef1de347def80b3dd2c
1 /* Header file for gimple iterators.
2 Copyright (C) 2013-2018 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 gimple_stmt_iterator gsi_for_stmt (gimple *, gimple_seq *);
83 extern gphi_iterator gsi_for_phi (gphi *);
84 extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
85 extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
86 extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
87 extern void gsi_insert_on_edge (edge, gimple *);
88 extern void gsi_insert_seq_on_edge (edge, gimple_seq);
89 extern basic_block gsi_insert_on_edge_immediate (edge, gimple *);
90 extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
91 extern void gsi_commit_edge_inserts (void);
92 extern void gsi_commit_one_edge_insert (edge, basic_block *);
93 extern gphi_iterator gsi_start_phis (basic_block);
94 extern void update_modified_stmts (gimple_seq);
96 /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
98 static inline gimple_stmt_iterator
99 gsi_start_1 (gimple_seq *seq)
101 gimple_stmt_iterator i;
103 i.ptr = gimple_seq_first (*seq);
104 i.seq = seq;
105 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
107 return i;
110 #define gsi_start(x) gsi_start_1 (&(x))
112 static inline gimple_stmt_iterator
113 gsi_none (void)
115 gimple_stmt_iterator i;
116 i.ptr = NULL;
117 i.seq = NULL;
118 i.bb = NULL;
119 return i;
122 /* Return a new iterator pointing to the first statement in basic block BB. */
124 static inline gimple_stmt_iterator
125 gsi_start_bb (basic_block bb)
127 gimple_stmt_iterator i;
128 gimple_seq *seq;
130 seq = bb_seq_addr (bb);
131 i.ptr = gimple_seq_first (*seq);
132 i.seq = seq;
133 i.bb = bb;
135 return i;
138 gimple_stmt_iterator gsi_start_edge (edge e);
140 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
142 static inline gimple_stmt_iterator
143 gsi_last_1 (gimple_seq *seq)
145 gimple_stmt_iterator i;
147 i.ptr = gimple_seq_last (*seq);
148 i.seq = seq;
149 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
151 return i;
154 #define gsi_last(x) gsi_last_1 (&(x))
156 /* Return a new iterator pointing to the last statement in basic block BB. */
158 static inline gimple_stmt_iterator
159 gsi_last_bb (basic_block bb)
161 gimple_stmt_iterator i;
162 gimple_seq *seq;
164 seq = bb_seq_addr (bb);
165 i.ptr = gimple_seq_last (*seq);
166 i.seq = seq;
167 i.bb = bb;
169 return i;
172 /* Return true if I is at the end of its sequence. */
174 static inline bool
175 gsi_end_p (gimple_stmt_iterator i)
177 return i.ptr == NULL;
180 /* Return true if I is one statement before the end of its sequence. */
182 static inline bool
183 gsi_one_before_end_p (gimple_stmt_iterator i)
185 return i.ptr != NULL && i.ptr->next == NULL;
188 /* Advance the iterator to the next gimple statement. */
190 static inline void
191 gsi_next (gimple_stmt_iterator *i)
193 i->ptr = i->ptr->next;
196 /* Advance the iterator to the previous gimple statement. */
198 static inline void
199 gsi_prev (gimple_stmt_iterator *i)
201 gimple *prev = i->ptr->prev;
202 if (prev->next)
203 i->ptr = prev;
204 else
205 i->ptr = NULL;
208 /* Return the current stmt. */
210 static inline gimple *
211 gsi_stmt (gimple_stmt_iterator i)
213 return i.ptr;
216 /* Return a block statement iterator that points to the first
217 non-label statement in block BB. */
219 static inline gimple_stmt_iterator
220 gsi_after_labels (basic_block bb)
222 gimple_stmt_iterator gsi = gsi_start_bb (bb);
224 for (; !gsi_end_p (gsi); )
226 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
227 gsi_next (&gsi);
228 else
229 break;
232 return gsi;
235 /* Advance the iterator to the next non-debug gimple statement. */
237 static inline void
238 gsi_next_nondebug (gimple_stmt_iterator *i)
242 gsi_next (i);
244 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
247 /* Advance the iterator to the previous non-debug gimple statement. */
249 static inline void
250 gsi_prev_nondebug (gimple_stmt_iterator *i)
254 gsi_prev (i);
256 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
259 /* Return a new iterator pointing to the first non-debug statement in
260 SEQ. */
262 static inline gimple_stmt_iterator
263 gsi_start_nondebug (gimple_seq seq)
265 gimple_stmt_iterator gsi = gsi_start (seq);
266 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
267 gsi_next_nondebug (&gsi);
269 return gsi;
272 /* Return a new iterator pointing to the first non-debug statement in
273 basic block BB. */
275 static inline gimple_stmt_iterator
276 gsi_start_nondebug_bb (basic_block bb)
278 gimple_stmt_iterator i = gsi_start_bb (bb);
280 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
281 gsi_next_nondebug (&i);
283 return i;
286 /* Return a new iterator pointing to the first non-debug non-label statement in
287 basic block BB. */
289 static inline gimple_stmt_iterator
290 gsi_start_nondebug_after_labels_bb (basic_block bb)
292 gimple_stmt_iterator i = gsi_after_labels (bb);
294 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
295 gsi_next_nondebug (&i);
297 return i;
300 /* Return a new iterator pointing to the last non-debug statement in
301 basic block BB. */
303 static inline gimple_stmt_iterator
304 gsi_last_nondebug_bb (basic_block bb)
306 gimple_stmt_iterator i = gsi_last_bb (bb);
308 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
309 gsi_prev_nondebug (&i);
311 return i;
314 /* Return true if I is followed only by debug statements in its
315 sequence. */
317 static inline bool
318 gsi_one_nondebug_before_end_p (gimple_stmt_iterator i)
320 if (gsi_one_before_end_p (i))
321 return true;
322 if (gsi_end_p (i))
323 return false;
324 gsi_next_nondebug (&i);
325 return gsi_end_p (i);
328 /* Iterates I statement iterator to the next non-virtual statement. */
330 static inline void
331 gsi_next_nonvirtual_phi (gphi_iterator *i)
333 gphi *phi;
335 if (gsi_end_p (*i))
336 return;
338 phi = i->phi ();
339 gcc_assert (phi != NULL);
341 while (virtual_operand_p (gimple_phi_result (phi)))
343 gsi_next (i);
345 if (gsi_end_p (*i))
346 return;
348 phi = i->phi ();
352 /* Return the basic block associated with this iterator. */
354 static inline basic_block
355 gsi_bb (gimple_stmt_iterator i)
357 return i.bb;
360 /* Return the sequence associated with this iterator. */
362 static inline gimple_seq
363 gsi_seq (gimple_stmt_iterator i)
365 return *i.seq;
368 /* Determine whether SEQ is a nondebug singleton. */
370 static inline bool
371 gimple_seq_nondebug_singleton_p (gimple_seq seq)
373 gimple_stmt_iterator gsi;
375 /* Find a nondebug gimple. */
376 gsi.ptr = gimple_seq_first (seq);
377 gsi.seq = &seq;
378 gsi.bb = NULL;
379 while (!gsi_end_p (gsi)
380 && is_gimple_debug (gsi_stmt (gsi)))
381 gsi_next (&gsi);
383 /* No nondebug gimple found, not a singleton. */
384 if (gsi_end_p (gsi))
385 return false;
387 /* Find a next nondebug gimple. */
388 gsi_next (&gsi);
389 while (!gsi_end_p (gsi)
390 && is_gimple_debug (gsi_stmt (gsi)))
391 gsi_next (&gsi);
393 /* Only a singleton if there's no next nondebug gimple. */
394 return gsi_end_p (gsi);
397 #endif /* GCC_GIMPLE_ITERATOR_H */