hppa: Fix pr110279-1.c on hppa
[official-gcc.git] / gcc / gimple-iterator.h
bloba7a80773a91d42df585b04ac92d0747855cfd095
1 /* Header file for gimple iterators.
2 Copyright (C) 2013-2023 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 gimple *operator * () const { return ptr; }
29 /* Sequence node holding the current statement. */
30 gimple_seq_node ptr;
32 /* Sequence and basic block holding the statement. These fields
33 are necessary to handle edge cases such as when statement is
34 added to an empty basic block or when the last statement of a
35 block/sequence is removed. */
36 gimple_seq *seq;
37 basic_block bb;
40 /* Iterator over GIMPLE_PHI statements. */
41 struct gphi_iterator : public gimple_stmt_iterator
43 gphi *operator * () const { return as_a <gphi *> (ptr); }
45 gphi *phi () const
47 return as_a <gphi *> (ptr);
51 enum gsi_iterator_update
53 GSI_NEW_STMT = 2, /* Move the iterator to the first statement added. */
54 GSI_LAST_NEW_STMT, /* Move the iterator to the last statement added. */
55 GSI_SAME_STMT, /* Leave the iterator at the same statement. */
56 GSI_CONTINUE_LINKING /* Move iterator to whatever position is suitable
57 for linking other statements in the same
58 direction. */
61 extern void gsi_insert_seq_before_without_update (gimple_stmt_iterator *,
62 gimple_seq,
63 enum gsi_iterator_update);
64 extern void gsi_insert_seq_before (gimple_stmt_iterator *, gimple_seq,
65 enum gsi_iterator_update);
66 extern void gsi_insert_seq_after_without_update (gimple_stmt_iterator *,
67 gimple_seq,
68 enum gsi_iterator_update);
69 extern void gsi_insert_seq_after (gimple_stmt_iterator *, gimple_seq,
70 enum gsi_iterator_update);
71 extern gimple_seq gsi_split_seq_after (gimple_stmt_iterator);
72 extern void gsi_set_stmt (gimple_stmt_iterator *, gimple *);
73 extern void gsi_split_seq_before (gimple_stmt_iterator *, gimple_seq *);
74 extern bool gsi_replace (gimple_stmt_iterator *, gimple *, bool);
75 extern void gsi_replace_with_seq (gimple_stmt_iterator *, gimple_seq, bool);
76 extern void gsi_insert_before_without_update (gimple_stmt_iterator *, gimple *,
77 enum gsi_iterator_update);
78 extern void gsi_insert_before (gimple_stmt_iterator *, gimple *,
79 enum gsi_iterator_update);
80 extern void gsi_insert_after_without_update (gimple_stmt_iterator *, gimple *,
81 enum gsi_iterator_update);
82 extern void gsi_insert_after (gimple_stmt_iterator *, gimple *,
83 enum gsi_iterator_update);
84 extern bool gsi_remove (gimple_stmt_iterator *, bool);
85 extern gimple_stmt_iterator gsi_for_stmt (gimple *);
86 extern gimple_stmt_iterator gsi_for_stmt (gimple *, gimple_seq *);
87 extern gphi_iterator gsi_for_phi (gphi *);
88 extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
89 extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
90 extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
91 extern void gsi_insert_on_edge (edge, gimple *);
92 extern void gsi_insert_seq_on_edge (edge, gimple_seq);
93 extern basic_block gsi_insert_on_edge_immediate (edge, gimple *);
94 extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
95 extern void gsi_commit_edge_inserts (void);
96 extern void gsi_commit_one_edge_insert (edge, basic_block *);
97 extern gphi_iterator gsi_start_phis (basic_block);
98 extern void update_modified_stmts (gimple_seq);
100 /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
102 inline gimple_stmt_iterator
103 gsi_start (gimple_seq &seq)
105 gimple_stmt_iterator i;
107 i.ptr = gimple_seq_first (seq);
108 i.seq = &seq;
109 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
111 return i;
114 inline gimple_stmt_iterator
115 gsi_none (void)
117 gimple_stmt_iterator i;
118 i.ptr = NULL;
119 i.seq = NULL;
120 i.bb = NULL;
121 return i;
124 /* Return a new iterator pointing to the first statement in basic block BB. */
126 inline gimple_stmt_iterator
127 gsi_start_bb (basic_block bb)
129 gimple_stmt_iterator i;
130 gimple_seq *seq;
132 seq = bb_seq_addr (bb);
133 i.ptr = gimple_seq_first (*seq);
134 i.seq = seq;
135 i.bb = bb;
137 return i;
140 gimple_stmt_iterator gsi_start_edge (edge e);
142 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
144 inline gimple_stmt_iterator
145 gsi_last (gimple_seq &seq)
147 gimple_stmt_iterator i;
149 i.ptr = gimple_seq_last (seq);
150 i.seq = &seq;
151 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
153 return i;
156 /* Return a new iterator pointing to the last statement in basic block BB. */
158 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 a new iterator pointing to before the first statement or after
173 last statement (depending on whether adding statements after it or before it)
174 in a GIMPLE_SEQ. */
176 inline gimple_stmt_iterator
177 gsi_end (gimple_seq &seq)
179 gimple_stmt_iterator i;
180 gimple *g = gimple_seq_last (seq);
182 i.ptr = NULL;
183 i.seq = &seq;
184 i.bb = g ? gimple_bb (g) : NULL;
186 return i;
189 /* Return a new iterator pointing to before the first statement or after
190 last statement (depending on whether adding statements after it or before it)
191 in basic block BB. */
193 inline gimple_stmt_iterator
194 gsi_end_bb (basic_block bb)
196 gimple_stmt_iterator i;
197 gimple_seq *seq;
199 seq = bb_seq_addr (bb);
200 i.ptr = NULL;
201 i.seq = seq;
202 i.bb = bb;
204 return i;
207 /* Return true if I is at the end of its sequence. */
209 inline bool
210 gsi_end_p (gimple_stmt_iterator i)
212 return i.ptr == NULL;
215 /* Return true if I is one statement before the end of its sequence. */
217 inline bool
218 gsi_one_before_end_p (gimple_stmt_iterator i)
220 return i.ptr != NULL && i.ptr->next == NULL;
223 /* Advance the iterator to the next gimple statement. */
225 inline void
226 gsi_next (gimple_stmt_iterator *i)
228 i->ptr = i->ptr->next;
231 /* Advance the iterator to the previous gimple statement. */
233 inline void
234 gsi_prev (gimple_stmt_iterator *i)
236 gimple *prev = i->ptr->prev;
237 if (prev->next)
238 i->ptr = prev;
239 else
240 i->ptr = NULL;
243 /* Return the current stmt. */
245 inline gimple *
246 gsi_stmt (gimple_stmt_iterator i)
248 return i.ptr;
251 /* Return a block statement iterator that points to the first
252 non-label statement in block BB. */
254 inline gimple_stmt_iterator
255 gsi_after_labels (basic_block bb)
257 gimple_stmt_iterator gsi = gsi_start_bb (bb);
259 for (; !gsi_end_p (gsi); )
261 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
262 gsi_next (&gsi);
263 else
264 break;
267 return gsi;
270 /* Return a statement iterator that points to the first
271 non-label statement in sequence SEQ. */
273 inline gimple_stmt_iterator
274 gsi_after_labels (gimple_seq &seq)
276 gimple_stmt_iterator gsi = gsi_start (seq);
278 for (; !gsi_end_p (gsi); )
280 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
281 gsi_next (&gsi);
282 else
283 break;
286 return gsi;
289 /* Advance the iterator to the next non-debug gimple statement. */
291 inline void
292 gsi_next_nondebug (gimple_stmt_iterator *i)
296 gsi_next (i);
298 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
301 /* Advance the iterator to the previous non-debug gimple statement. */
303 inline void
304 gsi_prev_nondebug (gimple_stmt_iterator *i)
308 gsi_prev (i);
310 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
313 /* Return a new iterator pointing to the first non-debug statement in
314 SEQ. */
316 inline gimple_stmt_iterator
317 gsi_start_nondebug (gimple_seq seq)
319 gimple_stmt_iterator gsi = gsi_start (seq);
320 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
321 gsi_next_nondebug (&gsi);
323 return gsi;
326 /* Return a new iterator pointing to the first non-debug statement in
327 basic block BB. */
329 inline gimple_stmt_iterator
330 gsi_start_nondebug_bb (basic_block bb)
332 gimple_stmt_iterator i = gsi_start_bb (bb);
334 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
335 gsi_next_nondebug (&i);
337 return i;
340 /* Return a new iterator pointing to the first non-debug non-label statement in
341 basic block BB. */
343 inline gimple_stmt_iterator
344 gsi_start_nondebug_after_labels_bb (basic_block bb)
346 gimple_stmt_iterator i = gsi_after_labels (bb);
348 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
349 gsi_next_nondebug (&i);
351 return i;
354 /* Return a new iterator pointing to the last non-debug statement in
355 basic block BB. */
357 inline gimple_stmt_iterator
358 gsi_last_nondebug_bb (basic_block bb)
360 gimple_stmt_iterator i = gsi_last_bb (bb);
362 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
363 gsi_prev_nondebug (&i);
365 return i;
368 /* Return true if I is followed only by debug statements in its
369 sequence. */
371 inline bool
372 gsi_one_nondebug_before_end_p (gimple_stmt_iterator i)
374 if (gsi_one_before_end_p (i))
375 return true;
376 if (gsi_end_p (i))
377 return false;
378 gsi_next_nondebug (&i);
379 return gsi_end_p (i);
382 /* Advance I statement iterator to the next non-virtual GIMPLE_PHI
383 statement. */
385 inline void
386 gsi_next_nonvirtual_phi (gphi_iterator *i)
390 gsi_next (i);
392 while (!gsi_end_p (*i) && virtual_operand_p (gimple_phi_result (i->phi ())));
395 /* Return a new iterator pointing to the first non-virtual phi statement in
396 basic block BB. */
398 inline gphi_iterator
399 gsi_start_nonvirtual_phis (basic_block bb)
401 gphi_iterator i = gsi_start_phis (bb);
403 if (!gsi_end_p (i) && virtual_operand_p (gimple_phi_result (i.phi ())))
404 gsi_next_nonvirtual_phi (&i);
406 return i;
409 /* Return the basic block associated with this iterator. */
411 inline basic_block
412 gsi_bb (gimple_stmt_iterator i)
414 return i.bb;
417 /* Return the sequence associated with this iterator. */
419 inline gimple_seq
420 gsi_seq (gimple_stmt_iterator i)
422 return *i.seq;
425 /* Determine whether SEQ is a nondebug singleton. */
427 inline bool
428 gimple_seq_nondebug_singleton_p (gimple_seq seq)
430 gimple_stmt_iterator gsi;
432 /* Find a nondebug gimple. */
433 gsi.ptr = gimple_seq_first (seq);
434 gsi.seq = &seq;
435 gsi.bb = NULL;
436 while (!gsi_end_p (gsi)
437 && is_gimple_debug (gsi_stmt (gsi)))
438 gsi_next (&gsi);
440 /* No nondebug gimple found, not a singleton. */
441 if (gsi_end_p (gsi))
442 return false;
444 /* Find a next nondebug gimple. */
445 gsi_next (&gsi);
446 while (!gsi_end_p (gsi)
447 && is_gimple_debug (gsi_stmt (gsi)))
448 gsi_next (&gsi);
450 /* Only a singleton if there's no next nondebug gimple. */
451 return gsi_end_p (gsi);
454 #endif /* GCC_GIMPLE_ITERATOR_H */