]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-iterator.h
gimple-walk.h: New File.
[thirdparty/gcc.git] / gcc / gimple-iterator.h
1 /* Header file for gimple iterators.
2 Copyright (C) 2013 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 typedef struct gimple_stmt_iterator_d
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 } gimple_stmt_iterator;
37
38 extern void gsi_insert_seq_before_without_update (gimple_stmt_iterator *,
39 gimple_seq,
40 enum gsi_iterator_update);
41 extern void gsi_insert_seq_before (gimple_stmt_iterator *, gimple_seq,
42 enum gsi_iterator_update);
43 extern void gsi_insert_seq_after_without_update (gimple_stmt_iterator *,
44 gimple_seq,
45 enum gsi_iterator_update);
46 extern void gsi_insert_seq_after (gimple_stmt_iterator *, gimple_seq,
47 enum gsi_iterator_update);
48 extern gimple_seq gsi_split_seq_after (gimple_stmt_iterator);
49 extern void gsi_set_stmt (gimple_stmt_iterator *, gimple);
50 extern void gsi_split_seq_before (gimple_stmt_iterator *, gimple_seq *);
51 extern void gsi_replace (gimple_stmt_iterator *, gimple, bool);
52 extern void gsi_replace_with_seq (gimple_stmt_iterator *, gimple_seq, bool);
53 extern void gsi_insert_before_without_update (gimple_stmt_iterator *, gimple,
54 enum gsi_iterator_update);
55 extern void gsi_insert_before (gimple_stmt_iterator *, gimple,
56 enum gsi_iterator_update);
57 extern void gsi_insert_after_without_update (gimple_stmt_iterator *, gimple,
58 enum gsi_iterator_update);
59 extern void gsi_insert_after (gimple_stmt_iterator *, gimple,
60 enum gsi_iterator_update);
61 extern bool gsi_remove (gimple_stmt_iterator *, bool);
62 extern gimple_stmt_iterator gsi_for_stmt (gimple);
63 extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
64 extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
65 extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
66 extern void gsi_insert_on_edge (edge, gimple);
67 extern void gsi_insert_seq_on_edge (edge, gimple_seq);
68 extern basic_block gsi_insert_on_edge_immediate (edge, gimple);
69 extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
70 extern void gsi_commit_edge_inserts (void);
71 extern void gsi_commit_one_edge_insert (edge, basic_block *);
72 extern gimple_stmt_iterator gsi_start_phis (basic_block);
73
74 /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
75
76 static inline gimple_stmt_iterator
77 gsi_start_1 (gimple_seq *seq)
78 {
79 gimple_stmt_iterator i;
80
81 i.ptr = gimple_seq_first (*seq);
82 i.seq = seq;
83 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
84
85 return i;
86 }
87
88 #define gsi_start(x) gsi_start_1 (&(x))
89
90 static inline gimple_stmt_iterator
91 gsi_none (void)
92 {
93 gimple_stmt_iterator i;
94 i.ptr = NULL;
95 i.seq = NULL;
96 i.bb = NULL;
97 return i;
98 }
99
100 /* Return a new iterator pointing to the first statement in basic block BB. */
101
102 static inline gimple_stmt_iterator
103 gsi_start_bb (basic_block bb)
104 {
105 gimple_stmt_iterator i;
106 gimple_seq *seq;
107
108 seq = bb_seq_addr (bb);
109 i.ptr = gimple_seq_first (*seq);
110 i.seq = seq;
111 i.bb = bb;
112
113 return i;
114 }
115
116 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
117
118 static inline gimple_stmt_iterator
119 gsi_last_1 (gimple_seq *seq)
120 {
121 gimple_stmt_iterator i;
122
123 i.ptr = gimple_seq_last (*seq);
124 i.seq = seq;
125 i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
126
127 return i;
128 }
129
130 #define gsi_last(x) gsi_last_1 (&(x))
131
132 /* Return a new iterator pointing to the last statement in basic block BB. */
133
134 static inline gimple_stmt_iterator
135 gsi_last_bb (basic_block bb)
136 {
137 gimple_stmt_iterator i;
138 gimple_seq *seq;
139
140 seq = bb_seq_addr (bb);
141 i.ptr = gimple_seq_last (*seq);
142 i.seq = seq;
143 i.bb = bb;
144
145 return i;
146 }
147
148 /* Return true if I is at the end of its sequence. */
149
150 static inline bool
151 gsi_end_p (gimple_stmt_iterator i)
152 {
153 return i.ptr == NULL;
154 }
155
156 /* Return true if I is one statement before the end of its sequence. */
157
158 static inline bool
159 gsi_one_before_end_p (gimple_stmt_iterator i)
160 {
161 return i.ptr != NULL && i.ptr->gsbase.next == NULL;
162 }
163
164 /* Advance the iterator to the next gimple statement. */
165
166 static inline void
167 gsi_next (gimple_stmt_iterator *i)
168 {
169 i->ptr = i->ptr->gsbase.next;
170 }
171
172 /* Advance the iterator to the previous gimple statement. */
173
174 static inline void
175 gsi_prev (gimple_stmt_iterator *i)
176 {
177 gimple prev = i->ptr->gsbase.prev;
178 if (prev->gsbase.next)
179 i->ptr = prev;
180 else
181 i->ptr = NULL;
182 }
183
184 /* Return the current stmt. */
185
186 static inline gimple
187 gsi_stmt (gimple_stmt_iterator i)
188 {
189 return i.ptr;
190 }
191
192 /* Return a block statement iterator that points to the first non-label
193 statement in block BB. */
194
195 static inline gimple_stmt_iterator
196 gsi_after_labels (basic_block bb)
197 {
198 gimple_stmt_iterator gsi = gsi_start_bb (bb);
199
200 while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
201 gsi_next (&gsi);
202
203 return gsi;
204 }
205
206 /* Advance the iterator to the next non-debug gimple statement. */
207
208 static inline void
209 gsi_next_nondebug (gimple_stmt_iterator *i)
210 {
211 do
212 {
213 gsi_next (i);
214 }
215 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
216 }
217
218 /* Advance the iterator to the next non-debug gimple statement. */
219
220 static inline void
221 gsi_prev_nondebug (gimple_stmt_iterator *i)
222 {
223 do
224 {
225 gsi_prev (i);
226 }
227 while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
228 }
229
230 /* Return a new iterator pointing to the first non-debug statement in
231 basic block BB. */
232
233 static inline gimple_stmt_iterator
234 gsi_start_nondebug_bb (basic_block bb)
235 {
236 gimple_stmt_iterator i = gsi_start_bb (bb);
237
238 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
239 gsi_next_nondebug (&i);
240
241 return i;
242 }
243
244 /* Return a new iterator pointing to the first non-debug non-label statement in
245 basic block BB. */
246
247 static inline gimple_stmt_iterator
248 gsi_start_nondebug_after_labels_bb (basic_block bb)
249 {
250 gimple_stmt_iterator i = gsi_after_labels (bb);
251
252 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
253 gsi_next_nondebug (&i);
254
255 return i;
256 }
257
258 /* Return a new iterator pointing to the last non-debug statement in
259 basic block BB. */
260
261 static inline gimple_stmt_iterator
262 gsi_last_nondebug_bb (basic_block bb)
263 {
264 gimple_stmt_iterator i = gsi_last_bb (bb);
265
266 if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
267 gsi_prev_nondebug (&i);
268
269 return i;
270 }
271
272 /* Return the basic block associated with this iterator. */
273
274 static inline basic_block
275 gsi_bb (gimple_stmt_iterator i)
276 {
277 return i.bb;
278 }
279
280 /* Return the sequence associated with this iterator. */
281
282 static inline gimple_seq
283 gsi_seq (gimple_stmt_iterator i)
284 {
285 return *i.seq;
286 }
287
288 #endif /* GCC_GIMPLE_ITERATOR_H */