tree-optimization/114485 - neg induction with partial vectors
[official-gcc.git] / gcc / gimple-iterator.h
blob501f0549d9257d27351d0142a4db7688cf0aa1f8
1 /* Header file for gimple iterators.
2 Copyright (C) 2013-2024 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 gsi_iterator_update = GSI_SAME_STMT);
91 extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
92 extern void gsi_insert_on_edge (edge, gimple *);
93 extern void gsi_insert_seq_on_edge (edge, gimple_seq);
94 extern basic_block gsi_insert_on_edge_immediate (edge, gimple *);
95 extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
96 extern void gsi_safe_insert_before (gimple_stmt_iterator *, gimple *);
97 extern void gsi_safe_insert_seq_before (gimple_stmt_iterator *, gimple_seq);
98 extern void gsi_commit_edge_inserts (void);
99 extern void gsi_commit_one_edge_insert (edge, basic_block *);
100 extern gphi_iterator gsi_start_phis (basic_block);
101 extern void update_modified_stmts (gimple_seq);
103 /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
105 inline gimple_stmt_iterator
106 gsi_start (gimple_seq &seq)
108 gimple_stmt_iterator i;
110 i.ptr = gimple_seq_first (seq);
111 i.seq = &seq;
112 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
114 return i;
117 inline gimple_stmt_iterator
118 gsi_none (void)
120 gimple_stmt_iterator i;
121 i.ptr = NULL;
122 i.seq = NULL;
123 i.bb = NULL;
124 return i;
127 /* Return a new iterator pointing to the first statement in basic block BB. */
129 inline gimple_stmt_iterator
130 gsi_start_bb (basic_block bb)
132 gimple_stmt_iterator i;
133 gimple_seq *seq;
135 seq = bb_seq_addr (bb);
136 i.ptr = gimple_seq_first (*seq);
137 i.seq = seq;
138 i.bb = bb;
140 return i;
143 gimple_stmt_iterator gsi_start_edge (edge e);
145 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
147 inline gimple_stmt_iterator
148 gsi_last (gimple_seq &seq)
150 gimple_stmt_iterator i;
152 i.ptr = gimple_seq_last (seq);
153 i.seq = &seq;
154 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
156 return i;
159 /* Return a new iterator pointing to the last statement in basic block BB. */
161 inline gimple_stmt_iterator
162 gsi_last_bb (basic_block bb)
164 gimple_stmt_iterator i;
165 gimple_seq *seq;
167 seq = bb_seq_addr (bb);
168 i.ptr = gimple_seq_last (*seq);
169 i.seq = seq;
170 i.bb = bb;
172 return i;
175 /* Return a new iterator pointing to before the first statement or after
176 last statement (depending on whether adding statements after it or before it)
177 in a GIMPLE_SEQ. */
179 inline gimple_stmt_iterator
180 gsi_end (gimple_seq &seq)
182 gimple_stmt_iterator i;
183 gimple *g = gimple_seq_last (seq);
185 i.ptr = NULL;
186 i.seq = &seq;
187 i.bb = g ? gimple_bb (g) : NULL;
189 return i;
192 /* Return a new iterator pointing to before the first statement or after
193 last statement (depending on whether adding statements after it or before it)
194 in basic block BB. */
196 inline gimple_stmt_iterator
197 gsi_end_bb (basic_block bb)
199 gimple_stmt_iterator i;
200 gimple_seq *seq;
202 seq = bb_seq_addr (bb);
203 i.ptr = NULL;
204 i.seq = seq;
205 i.bb = bb;
207 return i;
210 /* Return true if I is at the end of its sequence. */
212 inline bool
213 gsi_end_p (gimple_stmt_iterator i)
215 return i.ptr == NULL;
218 /* Return true if I is one statement before the end of its sequence. */
220 inline bool
221 gsi_one_before_end_p (gimple_stmt_iterator i)
223 return i.ptr != NULL && i.ptr->next == NULL;
226 /* Advance the iterator to the next gimple statement. */
228 inline void
229 gsi_next (gimple_stmt_iterator *i)
231 i->ptr = i->ptr->next;
234 /* Advance the iterator to the previous gimple statement. */
236 inline void
237 gsi_prev (gimple_stmt_iterator *i)
239 gimple *prev = i->ptr->prev;
240 if (prev->next)
241 i->ptr = prev;
242 else
243 i->ptr = NULL;
246 /* Return the current stmt. */
248 inline gimple *
249 gsi_stmt (gimple_stmt_iterator i)
251 return i.ptr;
254 /* Return a block statement iterator that points to the first
255 non-label statement in block BB. */
257 inline gimple_stmt_iterator
258 gsi_after_labels (basic_block bb)
260 gimple_stmt_iterator gsi = gsi_start_bb (bb);
262 for (; !gsi_end_p (gsi); )
264 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
265 gsi_next (&gsi);
266 else
267 break;
270 return gsi;
273 /* Return a statement iterator that points to the first
274 non-label statement in sequence SEQ. */
276 inline gimple_stmt_iterator
277 gsi_after_labels (gimple_seq &seq)
279 gimple_stmt_iterator gsi = gsi_start (seq);
281 for (; !gsi_end_p (gsi); )
283 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
284 gsi_next (&gsi);
285 else
286 break;
289 return gsi;
292 /* Advance the iterator to the next non-debug gimple statement. */
294 inline void
295 gsi_next_nondebug (gimple_stmt_iterator *i)
299 gsi_next (i);
301 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
304 /* Advance the iterator to the previous non-debug gimple statement. */
306 inline void
307 gsi_prev_nondebug (gimple_stmt_iterator *i)
311 gsi_prev (i);
313 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
316 /* Return a new iterator pointing to the first non-debug statement in
317 SEQ. */
319 inline gimple_stmt_iterator
320 gsi_start_nondebug (gimple_seq seq)
322 gimple_stmt_iterator gsi = gsi_start (seq);
323 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
324 gsi_next_nondebug (&gsi);
326 return gsi;
329 /* Return a new iterator pointing to the first non-debug statement in
330 basic block BB. */
332 inline gimple_stmt_iterator
333 gsi_start_nondebug_bb (basic_block bb)
335 gimple_stmt_iterator i = gsi_start_bb (bb);
337 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
338 gsi_next_nondebug (&i);
340 return i;
343 /* Return a new iterator pointing to the first non-debug non-label statement in
344 basic block BB. */
346 inline gimple_stmt_iterator
347 gsi_start_nondebug_after_labels_bb (basic_block bb)
349 gimple_stmt_iterator i = gsi_after_labels (bb);
351 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
352 gsi_next_nondebug (&i);
354 return i;
357 /* Return a new iterator pointing to the last non-debug statement in
358 basic block BB. */
360 inline gimple_stmt_iterator
361 gsi_last_nondebug_bb (basic_block bb)
363 gimple_stmt_iterator i = gsi_last_bb (bb);
365 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
366 gsi_prev_nondebug (&i);
368 return i;
371 /* Return true if I is followed only by debug statements in its
372 sequence. */
374 inline bool
375 gsi_one_nondebug_before_end_p (gimple_stmt_iterator i)
377 if (gsi_one_before_end_p (i))
378 return true;
379 if (gsi_end_p (i))
380 return false;
381 gsi_next_nondebug (&i);
382 return gsi_end_p (i);
385 /* Advance I statement iterator to the next non-virtual GIMPLE_PHI
386 statement. */
388 inline void
389 gsi_next_nonvirtual_phi (gphi_iterator *i)
393 gsi_next (i);
395 while (!gsi_end_p (*i) && virtual_operand_p (gimple_phi_result (i->phi ())));
398 /* Return a new iterator pointing to the first non-virtual phi statement in
399 basic block BB. */
401 inline gphi_iterator
402 gsi_start_nonvirtual_phis (basic_block bb)
404 gphi_iterator i = gsi_start_phis (bb);
406 if (!gsi_end_p (i) && virtual_operand_p (gimple_phi_result (i.phi ())))
407 gsi_next_nonvirtual_phi (&i);
409 return i;
412 /* Return the basic block associated with this iterator. */
414 inline basic_block
415 gsi_bb (gimple_stmt_iterator i)
417 return i.bb;
420 /* Return the sequence associated with this iterator. */
422 inline gimple_seq
423 gsi_seq (gimple_stmt_iterator i)
425 return *i.seq;
428 /* Determine whether SEQ is a nondebug singleton. */
430 inline bool
431 gimple_seq_nondebug_singleton_p (gimple_seq seq)
433 gimple_stmt_iterator gsi;
435 /* Find a nondebug gimple. */
436 gsi.ptr = gimple_seq_first (seq);
437 gsi.seq = &seq;
438 gsi.bb = NULL;
439 while (!gsi_end_p (gsi)
440 && is_gimple_debug (gsi_stmt (gsi)))
441 gsi_next (&gsi);
443 /* No nondebug gimple found, not a singleton. */
444 if (gsi_end_p (gsi))
445 return false;
447 /* Find a next nondebug gimple. */
448 gsi_next (&gsi);
449 while (!gsi_end_p (gsi)
450 && is_gimple_debug (gsi_stmt (gsi)))
451 gsi_next (&gsi);
453 /* Only a singleton if there's no next nondebug gimple. */
454 return gsi_end_p (gsi);
457 #endif /* GCC_GIMPLE_ITERATOR_H */