]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-iterator.h
Fix profiledbootstrap
[thirdparty/gcc.git] / gcc / gimple-iterator.h
1 /* Header file for gimple iterators.
2 Copyright (C) 2013-2023 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #ifndef GCC_GIMPLE_ITERATOR_H
21 #define GCC_GIMPLE_ITERATOR_H
22
23 /* Iterator object for GIMPLE statement sequences. */
24
25 struct gimple_stmt_iterator
26 {
27 gimple *operator * () const { return ptr; }
28
29 /* Sequence node holding the current statement. */
30 gimple_seq_node ptr;
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. */
36 gimple_seq *seq;
37 basic_block bb;
38 };
39
40 /* Iterator over GIMPLE_PHI statements. */
41 struct gphi_iterator : public gimple_stmt_iterator
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
51 enum gsi_iterator_update
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
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);
99
100 /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
101
102 inline gimple_stmt_iterator
103 gsi_start (gimple_seq &seq)
104 {
105 gimple_stmt_iterator i;
106
107 i.ptr = gimple_seq_first (seq);
108 i.seq = &seq;
109 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
110
111 return i;
112 }
113
114 inline gimple_stmt_iterator
115 gsi_none (void)
116 {
117 gimple_stmt_iterator i;
118 i.ptr = NULL;
119 i.seq = NULL;
120 i.bb = NULL;
121 return i;
122 }
123
124 /* Return a new iterator pointing to the first statement in basic block BB. */
125
126 inline gimple_stmt_iterator
127 gsi_start_bb (basic_block bb)
128 {
129 gimple_stmt_iterator i;
130 gimple_seq *seq;
131
132 seq = bb_seq_addr (bb);
133 i.ptr = gimple_seq_first (*seq);
134 i.seq = seq;
135 i.bb = bb;
136
137 return i;
138 }
139
140 gimple_stmt_iterator gsi_start_edge (edge e);
141
142 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
143
144 inline gimple_stmt_iterator
145 gsi_last (gimple_seq &seq)
146 {
147 gimple_stmt_iterator i;
148
149 i.ptr = gimple_seq_last (seq);
150 i.seq = &seq;
151 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
152
153 return i;
154 }
155
156 /* Return a new iterator pointing to the last statement in basic block BB. */
157
158 inline gimple_stmt_iterator
159 gsi_last_bb (basic_block bb)
160 {
161 gimple_stmt_iterator i;
162 gimple_seq *seq;
163
164 seq = bb_seq_addr (bb);
165 i.ptr = gimple_seq_last (*seq);
166 i.seq = seq;
167 i.bb = bb;
168
169 return i;
170 }
171
172 /* Return true if I is at the end of its sequence. */
173
174 inline bool
175 gsi_end_p (gimple_stmt_iterator i)
176 {
177 return i.ptr == NULL;
178 }
179
180 /* Return true if I is one statement before the end of its sequence. */
181
182 inline bool
183 gsi_one_before_end_p (gimple_stmt_iterator i)
184 {
185 return i.ptr != NULL && i.ptr->next == NULL;
186 }
187
188 /* Advance the iterator to the next gimple statement. */
189
190 inline void
191 gsi_next (gimple_stmt_iterator *i)
192 {
193 i->ptr = i->ptr->next;
194 }
195
196 /* Advance the iterator to the previous gimple statement. */
197
198 inline void
199 gsi_prev (gimple_stmt_iterator *i)
200 {
201 gimple *prev = i->ptr->prev;
202 if (prev->next)
203 i->ptr = prev;
204 else
205 i->ptr = NULL;
206 }
207
208 /* Return the current stmt. */
209
210 inline gimple *
211 gsi_stmt (gimple_stmt_iterator i)
212 {
213 return i.ptr;
214 }
215
216 /* Return a block statement iterator that points to the first
217 non-label statement in block BB. */
218
219 inline gimple_stmt_iterator
220 gsi_after_labels (basic_block bb)
221 {
222 gimple_stmt_iterator gsi = gsi_start_bb (bb);
223
224 for (; !gsi_end_p (gsi); )
225 {
226 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
227 gsi_next (&gsi);
228 else
229 break;
230 }
231
232 return gsi;
233 }
234
235 /* Return a statement iterator that points to the first
236 non-label statement in sequence SEQ. */
237
238 inline gimple_stmt_iterator
239 gsi_after_labels (gimple_seq &seq)
240 {
241 gimple_stmt_iterator gsi = gsi_start (seq);
242
243 for (; !gsi_end_p (gsi); )
244 {
245 if (gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
246 gsi_next (&gsi);
247 else
248 break;
249 }
250
251 return gsi;
252 }
253
254 /* Advance the iterator to the next non-debug gimple statement. */
255
256 inline void
257 gsi_next_nondebug (gimple_stmt_iterator *i)
258 {
259 do
260 {
261 gsi_next (i);
262 }
263 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
264 }
265
266 /* Advance the iterator to the previous non-debug gimple statement. */
267
268 inline void
269 gsi_prev_nondebug (gimple_stmt_iterator *i)
270 {
271 do
272 {
273 gsi_prev (i);
274 }
275 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
276 }
277
278 /* Return a new iterator pointing to the first non-debug statement in
279 SEQ. */
280
281 inline gimple_stmt_iterator
282 gsi_start_nondebug (gimple_seq seq)
283 {
284 gimple_stmt_iterator gsi = gsi_start (seq);
285 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
286 gsi_next_nondebug (&gsi);
287
288 return gsi;
289 }
290
291 /* Return a new iterator pointing to the first non-debug statement in
292 basic block BB. */
293
294 inline gimple_stmt_iterator
295 gsi_start_nondebug_bb (basic_block bb)
296 {
297 gimple_stmt_iterator i = gsi_start_bb (bb);
298
299 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
300 gsi_next_nondebug (&i);
301
302 return i;
303 }
304
305 /* Return a new iterator pointing to the first non-debug non-label statement in
306 basic block BB. */
307
308 inline gimple_stmt_iterator
309 gsi_start_nondebug_after_labels_bb (basic_block bb)
310 {
311 gimple_stmt_iterator i = gsi_after_labels (bb);
312
313 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
314 gsi_next_nondebug (&i);
315
316 return i;
317 }
318
319 /* Return a new iterator pointing to the last non-debug statement in
320 basic block BB. */
321
322 inline gimple_stmt_iterator
323 gsi_last_nondebug_bb (basic_block bb)
324 {
325 gimple_stmt_iterator i = gsi_last_bb (bb);
326
327 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
328 gsi_prev_nondebug (&i);
329
330 return i;
331 }
332
333 /* Return true if I is followed only by debug statements in its
334 sequence. */
335
336 inline bool
337 gsi_one_nondebug_before_end_p (gimple_stmt_iterator i)
338 {
339 if (gsi_one_before_end_p (i))
340 return true;
341 if (gsi_end_p (i))
342 return false;
343 gsi_next_nondebug (&i);
344 return gsi_end_p (i);
345 }
346
347 /* Advance I statement iterator to the next non-virtual GIMPLE_PHI
348 statement. */
349
350 inline void
351 gsi_next_nonvirtual_phi (gphi_iterator *i)
352 {
353 do
354 {
355 gsi_next (i);
356 }
357 while (!gsi_end_p (*i) && virtual_operand_p (gimple_phi_result (i->phi ())));
358 }
359
360 /* Return a new iterator pointing to the first non-virtual phi statement in
361 basic block BB. */
362
363 inline gphi_iterator
364 gsi_start_nonvirtual_phis (basic_block bb)
365 {
366 gphi_iterator i = gsi_start_phis (bb);
367
368 if (!gsi_end_p (i) && virtual_operand_p (gimple_phi_result (i.phi ())))
369 gsi_next_nonvirtual_phi (&i);
370
371 return i;
372 }
373
374 /* Return the basic block associated with this iterator. */
375
376 inline basic_block
377 gsi_bb (gimple_stmt_iterator i)
378 {
379 return i.bb;
380 }
381
382 /* Return the sequence associated with this iterator. */
383
384 inline gimple_seq
385 gsi_seq (gimple_stmt_iterator i)
386 {
387 return *i.seq;
388 }
389
390 /* Determine whether SEQ is a nondebug singleton. */
391
392 inline bool
393 gimple_seq_nondebug_singleton_p (gimple_seq seq)
394 {
395 gimple_stmt_iterator gsi;
396
397 /* Find a nondebug gimple. */
398 gsi.ptr = gimple_seq_first (seq);
399 gsi.seq = &seq;
400 gsi.bb = NULL;
401 while (!gsi_end_p (gsi)
402 && is_gimple_debug (gsi_stmt (gsi)))
403 gsi_next (&gsi);
404
405 /* No nondebug gimple found, not a singleton. */
406 if (gsi_end_p (gsi))
407 return false;
408
409 /* Find a next nondebug gimple. */
410 gsi_next (&gsi);
411 while (!gsi_end_p (gsi)
412 && is_gimple_debug (gsi_stmt (gsi)))
413 gsi_next (&gsi);
414
415 /* Only a singleton if there's no next nondebug gimple. */
416 return gsi_end_p (gsi);
417 }
418
419 #endif /* GCC_GIMPLE_ITERATOR_H */