GCC Middle and Back End API Reference
gimple-iterator.h
Go to the documentation of this file.
1/* Header file for gimple iterators.
2 Copyright (C) 2013-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
26{
27 gimple *operator * () const { return ptr; }
28
29 /* Sequence node holding the current statement. */
31
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. */
38};
39
40/* Iterator over GIMPLE_PHI statements. */
42{
43 gphi *operator * () const { return as_a <gphi *> (ptr); }
44
45 gphi *phi () const
46 {
47 return as_a <gphi *> (ptr);
48 }
49};
50
52{
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. */
59};
60
74extern bool gsi_replace (gimple_stmt_iterator *, gimple *, bool);
84extern bool gsi_remove (gimple_stmt_iterator *, bool);
92extern void gsi_insert_on_edge (edge, gimple *);
98extern void gsi_commit_edge_inserts (void);
102
103/* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
104
107{
109
110 i.ptr = gimple_seq_first (seq);
111 i.seq = &seq;
112 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
113
114 return i;
115}
116
119{
121 i.ptr = NULL;
122 i.seq = NULL;
123 i.bb = NULL;
124 return i;
125}
126
127/* Return a new iterator pointing to the first statement in basic block BB. */
128
131{
133 gimple_seq *seq;
134
135 seq = bb_seq_addr (bb);
136 i.ptr = gimple_seq_first (*seq);
137 i.seq = seq;
138 i.bb = bb;
139
140 return i;
141}
142
144
145/* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
146
149{
151
152 i.ptr = gimple_seq_last (seq);
153 i.seq = &seq;
154 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
155
156 return i;
157}
158
159/* Return a new iterator pointing to the last statement in basic block BB. */
160
163{
165 gimple_seq *seq;
166
167 seq = bb_seq_addr (bb);
168 i.ptr = gimple_seq_last (*seq);
169 i.seq = seq;
170 i.bb = bb;
171
172 return i;
173}
174
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. */
178
181{
183 gimple *g = gimple_seq_last (seq);
184
185 i.ptr = NULL;
186 i.seq = &seq;
187 i.bb = g ? gimple_bb (g) : NULL;
188
189 return i;
190}
191
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. */
195
198{
200 gimple_seq *seq;
201
202 seq = bb_seq_addr (bb);
203 i.ptr = NULL;
204 i.seq = seq;
205 i.bb = bb;
206
207 return i;
208}
209
210/* Return true if I is at the end of its sequence. */
211
212inline bool
214{
215 return i.ptr == NULL;
216}
217
218/* Return true if I is one statement before the end of its sequence. */
219
220inline bool
222{
223 return i.ptr != NULL && i.ptr->next == NULL;
224}
225
226/* Advance the iterator to the next gimple statement. */
227
228inline void
230{
231 i->ptr = i->ptr->next;
232}
233
234/* Advance the iterator to the previous gimple statement. */
235
236inline void
238{
239 gimple *prev = i->ptr->prev;
240 if (prev->next)
241 i->ptr = prev;
242 else
243 i->ptr = NULL;
244}
245
246/* Return the current stmt. */
247
248inline gimple *
250{
251 return i.ptr;
252}
253
254/* Return a block statement iterator that points to the first
255 non-label statement in block BB. */
256
259{
261
262 for (; !gsi_end_p (gsi); )
263 {
264 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
265 gsi_next (&gsi);
266 else
267 break;
268 }
269
270 return gsi;
271}
272
273/* Return a statement iterator that points to the first
274 non-label statement in sequence SEQ. */
275
278{
280
281 for (; !gsi_end_p (gsi); )
282 {
283 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
284 gsi_next (&gsi);
285 else
286 break;
287 }
288
289 return gsi;
290}
291
292/* Advance the iterator to the next non-debug gimple statement. */
293
294inline void
296{
297 do
298 {
299 gsi_next (i);
300 }
301 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
302}
303
304/* Advance the iterator to the previous non-debug gimple statement. */
305
306inline void
308{
309 do
310 {
311 gsi_prev (i);
312 }
313 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
314}
315
316/* Return a new iterator pointing to the first non-debug statement in
317 SEQ. */
318
321{
323 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
324 gsi_next_nondebug (&gsi);
325
326 return gsi;
327}
328
329/* Return a new iterator pointing to the first non-debug statement in
330 basic block BB. */
331
334{
336
337 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
339
340 return i;
341}
342
343/* Return a new iterator pointing to the first non-debug non-label statement in
344 basic block BB. */
345
356
357/* Return a new iterator pointing to the last non-debug statement in
358 basic block BB. */
359
362{
364
365 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
367
368 return i;
369}
370
371/* Return true if I is followed only by debug statements in its
372 sequence. */
373
374inline bool
376{
378 return true;
379 if (gsi_end_p (i))
380 return false;
382 return gsi_end_p (i);
383}
384
385/* Advance I statement iterator to the next non-virtual GIMPLE_PHI
386 statement. */
387
388inline void
390{
391 do
392 {
393 gsi_next (i);
394 }
395 while (!gsi_end_p (*i) && virtual_operand_p (gimple_phi_result (i->phi ())));
396}
397
398/* Return a new iterator pointing to the first non-virtual phi statement in
399 basic block BB. */
400
401inline gphi_iterator
403{
405
406 if (!gsi_end_p (i) && virtual_operand_p (gimple_phi_result (i.phi ())))
408
409 return i;
410}
411
412/* Return the basic block associated with this iterator. */
413
414inline basic_block
416{
417 return i.bb;
418}
419
420/* Return the sequence associated with this iterator. */
421
422inline gimple_seq
424{
425 return *i.seq;
426}
427
428/* Determine whether SEQ is a nondebug singleton. */
429
430inline bool
432{
434
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);
442
443 /* No nondebug gimple found, not a singleton. */
444 if (gsi_end_p (gsi))
445 return false;
446
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);
452
453 /* Only a singleton if there's no next nondebug gimple. */
454 return gsi_end_p (gsi);
455}
456
457#endif /* GCC_GIMPLE_ITERATOR_H */
gcc::context * g
Definition context.cc:29
class edge_def * edge
Definition coretypes.h:342
T * ggc_alloc(ALONE_CXX_MEM_STAT_INFO)
Definition ggc.h:184
bool virtual_operand_p(tree op)
Definition gimple-expr.h:106
gimple_stmt_iterator gsi_for_stmt(gimple *)
Definition gimple-iterator.cc:615
bool gsi_one_nondebug_before_end_p(gimple_stmt_iterator i)
Definition gimple-iterator.h:375
void gsi_insert_before_without_update(gimple_stmt_iterator *, gimple *, enum gsi_iterator_update)
Definition gimple-iterator.cc:499
void gsi_next_nondebug(gimple_stmt_iterator *i)
Definition gimple-iterator.h:295
void gsi_move_before(gimple_stmt_iterator *, gimple_stmt_iterator *, gsi_iterator_update=GSI_SAME_STMT)
Definition gimple-iterator.cc:673
gimple_stmt_iterator gsi_start_edge(edge e)
Definition gimple-iterator.cc:726
bool gsi_one_before_end_p(gimple_stmt_iterator i)
Definition gimple-iterator.h:221
bool gimple_seq_nondebug_singleton_p(gimple_seq seq)
Definition gimple-iterator.h:431
void gsi_insert_seq_on_edge(edge, gimple_seq)
Definition gimple-iterator.cc:716
bool gsi_replace(gimple_stmt_iterator *, gimple *, bool)
Definition gimple-iterator.cc:430
gimple_stmt_iterator gsi_last_bb(basic_block bb)
Definition gimple-iterator.h:162
void gsi_insert_seq_before_without_update(gimple_stmt_iterator *, gimple_seq, enum gsi_iterator_update)
Definition gimple-iterator.cc:187
void gsi_insert_seq_before(gimple_stmt_iterator *, gimple_seq, enum gsi_iterator_update)
Definition gimple-iterator.cc:218
gimple_seq gsi_seq(gimple_stmt_iterator i)
Definition gimple-iterator.h:423
gimple_stmt_iterator gsi_after_labels(basic_block bb)
Definition gimple-iterator.h:258
gimple_stmt_iterator gsi_start_bb(basic_block bb)
Definition gimple-iterator.h:130
gimple_stmt_iterator gsi_none(void)
Definition gimple-iterator.h:118
gimple_stmt_iterator gsi_last_nondebug_bb(basic_block bb)
Definition gimple-iterator.h:361
gimple_stmt_iterator gsi_last(gimple_seq &seq)
Definition gimple-iterator.h:148
gphi_iterator gsi_start_nonvirtual_phis(basic_block bb)
Definition gimple-iterator.h:402
void gsi_commit_one_edge_insert(edge, basic_block *)
Definition gimple-iterator.cc:909
void gsi_commit_edge_inserts(void)
Definition gimple-iterator.cc:890
gimple_stmt_iterator gsi_end_bb(basic_block bb)
Definition gimple-iterator.h:197
gimple_stmt_iterator gsi_end(gimple_seq &seq)
Definition gimple-iterator.h:180
gphi_iterator gsi_start_phis(basic_block)
Definition gimple-iterator.cc:935
gimple * gsi_stmt(gimple_stmt_iterator i)
Definition gimple-iterator.h:249
void gsi_insert_after(gimple_stmt_iterator *, gimple *, enum gsi_iterator_update)
Definition gimple-iterator.cc:542
gimple_stmt_iterator gsi_start_nondebug(gimple_seq seq)
Definition gimple-iterator.h:320
void gsi_move_after(gimple_stmt_iterator *, gimple_stmt_iterator *)
Definition gimple-iterator.cc:658
gimple_stmt_iterator gsi_start(gimple_seq &seq)
Definition gimple-iterator.h:106
void gsi_insert_seq_after(gimple_stmt_iterator *, gimple_seq, enum gsi_iterator_update)
Definition gimple-iterator.cc:330
void gsi_insert_seq_after_without_update(gimple_stmt_iterator *, gimple_seq, enum gsi_iterator_update)
Definition gimple-iterator.cc:300
void gsi_prev(gimple_stmt_iterator *i)
Definition gimple-iterator.h:237
void gsi_split_seq_before(gimple_stmt_iterator *, gimple_seq *)
Definition gimple-iterator.cc:396
void gsi_insert_after_without_update(gimple_stmt_iterator *, gimple *, enum gsi_iterator_update)
Definition gimple-iterator.cc:529
bool gsi_end_p(gimple_stmt_iterator i)
Definition gimple-iterator.h:213
void gsi_prev_nondebug(gimple_stmt_iterator *i)
Definition gimple-iterator.h:307
gsi_iterator_update
Definition gimple-iterator.h:52
@ GSI_SAME_STMT
Definition gimple-iterator.h:55
@ GSI_LAST_NEW_STMT
Definition gimple-iterator.h:54
@ GSI_NEW_STMT
Definition gimple-iterator.h:53
@ GSI_CONTINUE_LINKING
Definition gimple-iterator.h:56
void gsi_safe_insert_seq_before(gimple_stmt_iterator *, gimple_seq)
Definition gimple-iterator.cc:1069
basic_block gsi_bb(gimple_stmt_iterator i)
Definition gimple-iterator.h:415
gimple_stmt_iterator gsi_start_nondebug_bb(basic_block bb)
Definition gimple-iterator.h:333
void gsi_set_stmt(gimple_stmt_iterator *, gimple *)
Definition gimple-iterator.cc:369
basic_block gsi_insert_on_edge_immediate(edge, gimple *)
Definition gimple-iterator.cc:843
basic_block gsi_insert_seq_on_edge_immediate(edge, gimple_seq)
Definition gimple-iterator.cc:867
gphi_iterator gsi_for_phi(gphi *)
Definition gimple-iterator.cc:644
void update_modified_stmts(gimple_seq)
Definition gimple-iterator.cc:52
void gsi_move_to_bb_end(gimple_stmt_iterator *, basic_block)
Definition gimple-iterator.cc:689
void gsi_next_nonvirtual_phi(gphi_iterator *i)
Definition gimple-iterator.h:389
bool gsi_remove(gimple_stmt_iterator *, bool)
Definition gimple-iterator.cc:560
void gsi_insert_on_edge(edge, gimple *)
Definition gimple-iterator.cc:706
void gsi_next(gimple_stmt_iterator *i)
Definition gimple-iterator.h:229
gimple_stmt_iterator gsi_start_nondebug_after_labels_bb(basic_block bb)
Definition gimple-iterator.h:347
void gsi_insert_before(gimple_stmt_iterator *, gimple *, enum gsi_iterator_update)
Definition gimple-iterator.cc:511
void gsi_replace_with_seq(gimple_stmt_iterator *, gimple_seq, bool)
Definition gimple-iterator.cc:471
gimple_seq gsi_split_seq_after(gimple_stmt_iterator)
Definition gimple-iterator.cc:342
void gsi_safe_insert_before(gimple_stmt_iterator *, gimple *)
Definition gimple-iterator.cc:1047
gimple_seq_node gimple_seq_last(gimple_seq s)
Definition gimple.h:1713
gimple_code
Definition gimple.h:30
tree gimple_phi_result(const gphi *gs)
Definition gimple.h:4560
bool is_gimple_debug(const gimple *gs)
Definition gimple.h:4911
basic_block gimple_bb(const gimple *g)
Definition gimple.h:1860
gimple_seq * bb_seq_addr(basic_block bb)
Definition gimple.h:1775
gimple_seq_node gimple_seq_first(gimple_seq s)
Definition gimple.h:1684
i
Definition poly-int.h:772
Definition basic-block.h:117
Definition gimple-iterator.h:26
gimple * operator*() const
Definition gimple-iterator.h:27
gimple_seq_node ptr
Definition gimple-iterator.h:30
basic_block bb
Definition gimple-iterator.h:37
gimple_seq * seq
Definition gimple-iterator.h:36
Definition gimple.h:225
basic_block bb
Definition gimple.h:278
gimple * next
Definition gimple.h:286
gimple * prev
Definition gimple.h:287
Definition gimple-iterator.h:42
gphi * operator*() const
Definition gimple-iterator.h:43
gphi * phi() const
Definition gimple-iterator.h:45
Definition gimple.h:462
#define NULL
Definition system.h:50