2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
6 .. index:: Sequence iterators
8 .. _sequence-iterators:
13 Sequence iterators are convenience constructs for iterating
14 through statements in a sequence. Given a sequence ``SEQ``, here is
15 a typical use of gimple sequence iterators:
19 gimple_stmt_iterator gsi;
21 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
23 gimple g = gsi_stmt (gsi);
24 /* Do something with gimple statement G. */
27 Backward iterations are possible:
31 for (gsi = gsi_last (seq); !gsi_end_p (gsi); gsi_prev (&gsi))
33 Forward and backward iterations on basic blocks are possible with
34 ``gsi_start_bb`` and ``gsi_last_bb``.
36 In the documentation below we sometimes refer to enum
37 ``gsi_iterator_update``. The valid options for this enumeration are:
40 Only valid when a single statement is added. Move the iterator to it.
43 Leave the iterator at the same statement.
45 * ``GSI_CONTINUE_LINKING``
46 Move iterator to whatever position is suitable for linking other
47 statements in the same direction.
49 Below is a list of the functions used to manipulate and use
52 .. function:: gimple_stmt_iterator gsi_start (gimple_seq seq)
54 Return a new iterator pointing to the sequence ``SEQ`` 's first
55 statement. If ``SEQ`` is empty, the iterator's basic block is ``NULL``.
56 Use ``gsi_start_bb`` instead when the iterator needs to always have
57 the correct basic block set.
59 .. function:: gimple_stmt_iterator gsi_start_bb (basic_block bb)
61 Return a new iterator pointing to the first statement in basic
64 .. function:: gimple_stmt_iterator gsi_last (gimple_seq seq)
66 Return a new iterator initially pointing to the last statement of
67 sequence ``SEQ``. If ``SEQ`` is empty, the iterator's basic block is
68 ``NULL``. Use ``gsi_last_bb`` instead when the iterator needs to always
69 have the correct basic block set.
71 .. function:: gimple_stmt_iterator gsi_last_bb (basic_block bb)
73 Return a new iterator pointing to the last statement in basic
76 .. function:: bool gsi_end_p (gimple_stmt_iterator i)
78 Return ``TRUE`` if at the end of ``I``.
80 .. function:: bool gsi_one_before_end_p (gimple_stmt_iterator i)
82 Return ``TRUE`` if we're one statement before the end of ``I``.
84 .. function:: void gsi_next (gimple_stmt_iterator *i)
86 Advance the iterator to the next gimple statement.
88 .. function:: void gsi_prev (gimple_stmt_iterator *i)
90 Advance the iterator to the previous gimple statement.
92 .. function:: gimple gsi_stmt (gimple_stmt_iterator i)
94 Return the current stmt.
96 .. function:: gimple_stmt_iterator gsi_after_labels (basic_block bb)
98 Return a block statement iterator that points to the first
99 non-label statement in block ``BB``.
101 .. function:: gimple * gsi_stmt_ptr (gimple_stmt_iterator *i)
103 Return a pointer to the current stmt.
105 .. function:: basic_block gsi_bb (gimple_stmt_iterator i)
107 Return the basic block associated with this iterator.
109 .. function:: gimple_seq gsi_seq (gimple_stmt_iterator i)
111 Return the sequence associated with this iterator.
113 .. function:: void gsi_remove (gimple_stmt_iterator *i, bool remove_eh_info)
115 Remove the current stmt from the sequence. The iterator is
116 updated to point to the next statement. When ``REMOVE_EH_INFO`` is
117 true we remove the statement pointed to by iterator ``I`` from the ``EH``
118 tables. Otherwise we do not modify the ``EH`` tables. Generally,
119 ``REMOVE_EH_INFO`` should be true when the statement is going to be
120 removed from the ``IL`` and not reinserted elsewhere.
122 .. function:: void gsi_link_seq_before (gimple_stmt_iterator *i, gimple_seq seq, enum gsi_iterator_update mode)
124 Links the sequence of statements ``SEQ`` before the statement pointed
125 by iterator ``I``. ``MODE`` indicates what to do with the iterator
126 after insertion (see ``enum gsi_iterator_update`` above).
128 .. function:: void gsi_link_before (gimple_stmt_iterator *i, gimple g, enum gsi_iterator_update mode)
130 Links statement ``G`` before the statement pointed-to by iterator ``I``.
131 Updates iterator ``I`` according to ``MODE``.
133 .. function:: void gsi_link_seq_after (gimple_stmt_iterator *i, gimple_seq seq, enum gsi_iterator_update mode)
135 Links sequence ``SEQ`` after the statement pointed-to by iterator ``I``.
136 ``MODE`` is as in ``gsi_insert_after``.
138 .. function:: void gsi_link_after (gimple_stmt_iterator *i, gimple g, enum gsi_iterator_update mode)
140 Links statement ``G`` after the statement pointed-to by iterator ``I``.
141 ``MODE`` is as in ``gsi_insert_after``.
143 .. function:: gimple_seq gsi_split_seq_after (gimple_stmt_iterator i)
145 Move all statements in the sequence after ``I`` to a new sequence.
146 Return this new sequence.
148 .. function:: gimple_seq gsi_split_seq_before (gimple_stmt_iterator *i)
150 Move all statements in the sequence before ``I`` to a new sequence.
151 Return this new sequence.
153 .. function:: void gsi_replace (gimple_stmt_iterator *i, gimple stmt, bool update_eh_info)
155 Replace the statement pointed-to by ``I`` to ``STMT``. If ``UPDATE_EH_INFO``
156 is true, the exception handling information of the original
157 statement is moved to the new statement.
159 .. function:: void gsi_insert_before (gimple_stmt_iterator *i, gimple stmt, enum gsi_iterator_update mode)
161 Insert statement ``STMT`` before the statement pointed-to by iterator
162 ``I``, update ``STMT`` 's basic block and scan it for new operands. ``MODE``
163 specifies how to update iterator ``I`` after insertion (see enum
164 ``gsi_iterator_update``).
166 .. function:: void gsi_insert_seq_before (gimple_stmt_iterator *i, gimple_seq seq, enum gsi_iterator_update mode)
168 Like ``gsi_insert_before``, but for all the statements in ``SEQ``.
170 .. function:: void gsi_insert_after (gimple_stmt_iterator *i, gimple stmt, enum gsi_iterator_update mode)
172 Insert statement ``STMT`` after the statement pointed-to by iterator
173 ``I``, update ``STMT`` 's basic block and scan it for new operands. ``MODE``
174 specifies how to update iterator ``I`` after insertion (see enum
175 ``gsi_iterator_update``).
177 .. function:: void gsi_insert_seq_after (gimple_stmt_iterator *i, gimple_seq seq, enum gsi_iterator_update mode)
179 Like ``gsi_insert_after``, but for all the statements in ``SEQ``.
181 .. function:: gimple_stmt_iterator gsi_for_stmt (gimple stmt)
183 Finds iterator for ``STMT``.
185 .. function:: void gsi_move_after (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
187 Move the statement at ``FROM`` so it comes right after the statement
190 .. function:: void gsi_move_before (gimple_stmt_iterator *from, gimple_stmt_iterator *to)
192 Move the statement at ``FROM`` so it comes right before the statement
195 .. function:: void gsi_move_to_bb_end (gimple_stmt_iterator *from, basic_block bb)
197 Move the statement at ``FROM`` to the end of basic block ``BB``.
199 .. function:: void gsi_insert_on_edge (edge e, gimple stmt)
201 Add ``STMT`` to the pending list of edge ``E``. No actual insertion is
202 made until a call to ``gsi_commit_edge_inserts`` () is made.
204 .. function:: void gsi_insert_seq_on_edge (edge e, gimple_seq seq)
206 Add the sequence of statements in ``SEQ`` to the pending list of edge
207 ``E``. No actual insertion is made until a call to
208 ``gsi_commit_edge_inserts`` () is made.
210 .. function:: basic_block gsi_insert_on_edge_immediate (edge e, gimple stmt)
212 Similar to ``gsi_insert_on_edge`` + ``gsi_commit_edge_inserts``. If a new
213 block has to be created, it is returned.
215 .. function:: void gsi_commit_one_edge_insert (edge e, basic_block *new_bb)
217 Commit insertions pending at edge ``E``. If a new block is created,
218 set ``NEW_BB`` to this block, otherwise set it to ``NULL``.
220 .. function:: void gsi_commit_edge_inserts (void)
222 This routine will commit all pending edge insertions, creating
223 any new basic blocks which are necessary.