1/* Header file for gimple iterators.
2 Copyright (C) 2013-2017 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
25struct gimple_stmt_iterator
26{
27 /* Sequence node holding the current statement. */
28 gimple_seq_node ptr;
29
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;
36};
37
38/* Iterator over GIMPLE_PHI statements. */
39struct gphi_iterator : public gimple_stmt_iterator
40{
41 gphi *phi () const
42 {
43 return as_a <gphi *> (ptr);
44 }
45};
46
47enum gsi_iterator_update
48{
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. */
55};
56
57extern void gsi_insert_seq_before_without_update (gimple_stmt_iterator *,
58 gimple_seq,
59 enum gsi_iterator_update);
60extern void gsi_insert_seq_before (gimple_stmt_iterator *, gimple_seq,
61 enum gsi_iterator_update);
62extern void gsi_insert_seq_after_without_update (gimple_stmt_iterator *,
63 gimple_seq,
64 enum gsi_iterator_update);
65extern void gsi_insert_seq_after (gimple_stmt_iterator *, gimple_seq,
66 enum gsi_iterator_update);
67extern gimple_seq gsi_split_seq_after (gimple_stmt_iterator);
68extern void gsi_set_stmt (gimple_stmt_iterator *, gimple *);
69extern void gsi_split_seq_before (gimple_stmt_iterator *, gimple_seq *);
70extern bool gsi_replace (gimple_stmt_iterator *, gimple *, bool);
71extern void gsi_replace_with_seq (gimple_stmt_iterator *, gimple_seq, bool);
72extern void gsi_insert_before_without_update (gimple_stmt_iterator *, gimple *,
73 enum gsi_iterator_update);
74extern void gsi_insert_before (gimple_stmt_iterator *, gimple *,
75 enum gsi_iterator_update);
76extern void gsi_insert_after_without_update (gimple_stmt_iterator *, gimple *,
77 enum gsi_iterator_update);
78extern void gsi_insert_after (gimple_stmt_iterator *, gimple *,
79 enum gsi_iterator_update);
80extern bool gsi_remove (gimple_stmt_iterator *, bool);
81extern gimple_stmt_iterator gsi_for_stmt (gimple *);
82extern gphi_iterator gsi_for_phi (gphi *);
83extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
84extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
85extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
86extern void gsi_insert_on_edge (edge, gimple *);
87extern void gsi_insert_seq_on_edge (edge, gimple_seq);
88extern basic_block gsi_insert_on_edge_immediate (edge, gimple *);
89extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
90extern void gsi_commit_edge_inserts (void);
91extern void gsi_commit_one_edge_insert (edge, basic_block *);
92extern gphi_iterator gsi_start_phis (basic_block);
93extern void update_modified_stmts (gimple_seq);
94
95/* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
96
97static inline gimple_stmt_iterator
98gsi_start_1 (gimple_seq *seq)
99{
100 gimple_stmt_iterator i;
101
102 i.ptr = gimple_seq_first (*seq);
103 i.seq = seq;
104 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
105
106 return i;
107}
108
109#define gsi_start(x) gsi_start_1 (&(x))
110
111static inline gimple_stmt_iterator
112gsi_none (void)
113{
114 gimple_stmt_iterator i;
115 i.ptr = NULL;
116 i.seq = NULL;
117 i.bb = NULL;
118 return i;
119}
120
121/* Return a new iterator pointing to the first statement in basic block BB. */
122
123static inline gimple_stmt_iterator
124gsi_start_bb (basic_block bb)
125{
126 gimple_stmt_iterator i;
127 gimple_seq *seq;
128
129 seq = bb_seq_addr (bb);
130 i.ptr = gimple_seq_first (*seq);
131 i.seq = seq;
132 i.bb = bb;
133
134 return i;
135}
136
137gimple_stmt_iterator gsi_start_edge (edge e);
138
139/* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
140
141static inline gimple_stmt_iterator
142gsi_last_1 (gimple_seq *seq)
143{
144 gimple_stmt_iterator i;
145
146 i.ptr = gimple_seq_last (*seq);
147 i.seq = seq;
148 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
149
150 return i;
151}
152
153#define gsi_last(x) gsi_last_1 (&(x))
154
155/* Return a new iterator pointing to the last statement in basic block BB. */
156
157static inline gimple_stmt_iterator
158gsi_last_bb (basic_block bb)
159{
160 gimple_stmt_iterator i;
161 gimple_seq *seq;
162
163 seq = bb_seq_addr (bb);
164 i.ptr = gimple_seq_last (*seq);
165 i.seq = seq;
166 i.bb = bb;
167
168 return i;
169}
170
171/* Return true if I is at the end of its sequence. */
172
173static inline bool
174gsi_end_p (gimple_stmt_iterator i)
175{
176 return i.ptr == NULL;
177}
178
179/* Return true if I is one statement before the end of its sequence. */
180
181static inline bool
182gsi_one_before_end_p (gimple_stmt_iterator i)
183{
184 return i.ptr != NULL && i.ptr->next == NULL;
185}
186
187/* Advance the iterator to the next gimple statement. */
188
189static inline void
190gsi_next (gimple_stmt_iterator *i)
191{
192 i->ptr = i->ptr->next;
193}
194
195/* Advance the iterator to the previous gimple statement. */
196
197static inline void
198gsi_prev (gimple_stmt_iterator *i)
199{
200 gimple *prev = i->ptr->prev;
201 if (prev->next)
202 i->ptr = prev;
203 else
204 i->ptr = NULL;
205}
206
207/* Return the current stmt. */
208
209static inline gimple *
210gsi_stmt (gimple_stmt_iterator i)
211{
212 return i.ptr;
213}
214
215/* Return a block statement iterator that points to the first
216 non-label statement in block BB. Skip debug stmts only if they
217 precede labels. */
218
219static inline gimple_stmt_iterator
220gsi_after_labels (basic_block bb)
221{
222 gimple_stmt_iterator gsi = gsi_start_bb (bb);
223
224 for (gimple_stmt_iterator gskip = gsi;
225 !gsi_end_p (gskip); )
226 {
227 if (is_gimple_debug (gsi_stmt (gskip)))
228 gsi_next (&gskip);
229 else if (gimple_code (gsi_stmt (gskip)) == GIMPLE_LABEL)
230 {
231 gsi_next (&gskip);
232 gsi = gskip;
233 }
234 else
235 break;
236 }
237
238 return gsi;
239}
240
241/* Advance the iterator to the next non-debug gimple statement. */
242
243static inline void
244gsi_next_nondebug (gimple_stmt_iterator *i)
245{
246 do
247 {
248 gsi_next (i);
249 }
250 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
251}
252
253/* Advance the iterator to the previous non-debug gimple statement. */
254
255static inline void
256gsi_prev_nondebug (gimple_stmt_iterator *i)
257{
258 do
259 {
260 gsi_prev (i);
261 }
262 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
263}
264
265/* Return a new iterator pointing to the first non-debug statement in
266 SEQ. */
267
268static inline gimple_stmt_iterator
269gsi_start_nondebug (gimple_seq seq)
270{
271 gimple_stmt_iterator gsi = gsi_start (seq);
272 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
273 gsi_next_nondebug (&gsi);
274
275 return gsi;
276}
277
278/* Return a new iterator pointing to the first non-debug statement in
279 basic block BB. */
280
281static inline gimple_stmt_iterator
282gsi_start_nondebug_bb (basic_block bb)
283{
284 gimple_stmt_iterator i = gsi_start_bb (bb);
285
286 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
287 gsi_next_nondebug (&i);
288
289 return i;
290}
291
292/* Return a new iterator pointing to the first non-debug non-label statement in
293 basic block BB. */
294
295static inline gimple_stmt_iterator
296gsi_start_nondebug_after_labels_bb (basic_block bb)
297{
298 gimple_stmt_iterator i = gsi_after_labels (bb);
299
300 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
301 gsi_next_nondebug (&i);
302
303 return i;
304}
305
306/* Return a new iterator pointing to the last non-debug statement in
307 basic block BB. */
308
309static inline gimple_stmt_iterator
310gsi_last_nondebug_bb (basic_block bb)
311{
312 gimple_stmt_iterator i = gsi_last_bb (bb);
313
314 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
315 gsi_prev_nondebug (&i);
316
317 return i;
318}
319
320/* Return true if I is followed only by debug statements in its
321 sequence. */
322
323static inline bool
324gsi_one_nondebug_before_end_p (gimple_stmt_iterator i)
325{
326 if (gsi_one_before_end_p (i))
327 return true;
328 if (gsi_end_p (i))
329 return false;
330 gsi_next_nondebug (&i);
331 return gsi_end_p (i);
332}
333
334/* Iterates I statement iterator to the next non-virtual statement. */
335
336static inline void
337gsi_next_nonvirtual_phi (gphi_iterator *i)
338{
339 gphi *phi;
340
341 if (gsi_end_p (*i))
342 return;
343
344 phi = i->phi ();
345 gcc_assert (phi != NULL);
346
347 while (virtual_operand_p (gimple_phi_result (phi)))
348 {
349 gsi_next (i);
350
351 if (gsi_end_p (*i))
352 return;
353
354 phi = i->phi ();
355 }
356}
357
358/* Return the basic block associated with this iterator. */
359
360static inline basic_block
361gsi_bb (gimple_stmt_iterator i)
362{
363 return i.bb;
364}
365
366/* Return the sequence associated with this iterator. */
367
368static inline gimple_seq
369gsi_seq (gimple_stmt_iterator i)
370{
371 return *i.seq;
372}
373
374/* Determine whether SEQ is a nondebug singleton. */
375
376static inline bool
377gimple_seq_nondebug_singleton_p (gimple_seq seq)
378{
379 gimple_stmt_iterator gsi;
380
381 /* Find a nondebug gimple. */
382 gsi.ptr = gimple_seq_first (seq);
383 gsi.seq = &seq;
384 gsi.bb = NULL;
385 while (!gsi_end_p (gsi)
386 && is_gimple_debug (gsi_stmt (gsi)))
387 gsi_next (&gsi);
388
389 /* No nondebug gimple found, not a singleton. */
390 if (gsi_end_p (gsi))
391 return false;
392
393 /* Find a next nondebug gimple. */
394 gsi_next (&gsi);
395 while (!gsi_end_p (gsi)
396 && is_gimple_debug (gsi_stmt (gsi)))
397 gsi_next (&gsi);
398
399 /* Only a singleton if there's no next nondebug gimple. */
400 return gsi_end_p (gsi);
401}
402
403#endif /* GCC_GIMPLE_ITERATOR_H */
404