]>
Commit | Line | Data |
---|---|---|
80560f95 | 1 | /* Header file for SSA iterators. |
818ab71a | 2 | Copyright (C) 2013-2016 Free Software Foundation, Inc. |
80560f95 AM |
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_SSA_ITERATORS_H | |
21 | #define GCC_SSA_ITERATORS_H | |
22 | ||
23 | /* Immediate use lists are used to directly access all uses for an SSA | |
24 | name and get pointers to the statement for each use. | |
25 | ||
84562394 | 26 | The structure ssa_use_operand_t consists of PREV and NEXT pointers |
80560f95 AM |
27 | to maintain the list. A USE pointer, which points to address where |
28 | the use is located and a LOC pointer which can point to the | |
29 | statement where the use is located, or, in the case of the root | |
30 | node, it points to the SSA name itself. | |
31 | ||
32 | The list is anchored by an occurrence of ssa_operand_d *in* the | |
33 | ssa_name node itself (named 'imm_uses'). This node is uniquely | |
34 | identified by having a NULL USE pointer. and the LOC pointer | |
35 | pointing back to the ssa_name node itself. This node forms the | |
36 | base for a circular list, and initially this is the only node in | |
37 | the list. | |
38 | ||
39 | Fast iteration allows each use to be examined, but does not allow | |
40 | any modifications to the uses or stmts. | |
41 | ||
42 | Normal iteration allows insertion, deletion, and modification. the | |
43 | iterator manages this by inserting a marker node into the list | |
44 | immediately before the node currently being examined in the list. | |
45 | this marker node is uniquely identified by having null stmt *and* a | |
46 | null use pointer. | |
47 | ||
48 | When iterating to the next use, the iteration routines check to see | |
49 | if the node after the marker has changed. if it has, then the node | |
50 | following the marker is now the next one to be visited. if not, the | |
51 | marker node is moved past that node in the list (visualize it as | |
52 | bumping the marker node through the list). this continues until | |
53 | the marker node is moved to the original anchor position. the | |
54 | marker node is then removed from the list. | |
55 | ||
56 | If iteration is halted early, the marker node must be removed from | |
57 | the list before continuing. */ | |
84562394 | 58 | struct imm_use_iterator |
80560f95 AM |
59 | { |
60 | /* This is the current use the iterator is processing. */ | |
61 | ssa_use_operand_t *imm_use; | |
62 | /* This marks the last use in the list (use node from SSA_NAME) */ | |
63 | ssa_use_operand_t *end_p; | |
64 | /* This node is inserted and used to mark the end of the uses for a stmt. */ | |
65 | ssa_use_operand_t iter_node; | |
66 | /* This is the next ssa_name to visit. IMM_USE may get removed before | |
67 | the next one is traversed to, so it must be cached early. */ | |
68 | ssa_use_operand_t *next_imm_name; | |
84562394 | 69 | }; |
80560f95 AM |
70 | |
71 | ||
72 | /* Use this iterator when simply looking at stmts. Adding, deleting or | |
73 | modifying stmts will cause this iterator to malfunction. */ | |
74 | ||
75 | #define FOR_EACH_IMM_USE_FAST(DEST, ITER, SSAVAR) \ | |
76 | for ((DEST) = first_readonly_imm_use (&(ITER), (SSAVAR)); \ | |
77 | !end_readonly_imm_use_p (&(ITER)); \ | |
78 | (void) ((DEST) = next_readonly_imm_use (&(ITER)))) | |
79 | ||
80 | /* Use this iterator to visit each stmt which has a use of SSAVAR. */ | |
81 | ||
82 | #define FOR_EACH_IMM_USE_STMT(STMT, ITER, SSAVAR) \ | |
83 | for ((STMT) = first_imm_use_stmt (&(ITER), (SSAVAR)); \ | |
84 | !end_imm_use_stmt_p (&(ITER)); \ | |
85 | (void) ((STMT) = next_imm_use_stmt (&(ITER)))) | |
86 | ||
87 | /* Use this to terminate the FOR_EACH_IMM_USE_STMT loop early. Failure to | |
88 | do so will result in leaving a iterator marker node in the immediate | |
89 | use list, and nothing good will come from that. */ | |
90 | #define BREAK_FROM_IMM_USE_STMT(ITER) \ | |
91 | { \ | |
92 | end_imm_use_stmt_traverse (&(ITER)); \ | |
93 | break; \ | |
94 | } | |
95 | ||
96 | ||
97 | /* Use this iterator in combination with FOR_EACH_IMM_USE_STMT to | |
98 | get access to each occurrence of ssavar on the stmt returned by | |
99 | that iterator.. for instance: | |
100 | ||
50e6a148 | 101 | FOR_EACH_IMM_USE_STMT (stmt, iter, ssavar) |
80560f95 AM |
102 | { |
103 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
104 | { | |
105 | SET_USE (use_p, blah); | |
106 | } | |
107 | update_stmt (stmt); | |
108 | } */ | |
109 | ||
110 | #define FOR_EACH_IMM_USE_ON_STMT(DEST, ITER) \ | |
111 | for ((DEST) = first_imm_use_on_stmt (&(ITER)); \ | |
112 | !end_imm_use_on_stmt_p (&(ITER)); \ | |
113 | (void) ((DEST) = next_imm_use_on_stmt (&(ITER)))) | |
114 | ||
115 | ||
116 | ||
80560f95 | 117 | extern bool single_imm_use_1 (const ssa_use_operand_t *head, |
355fe088 | 118 | use_operand_p *use_p, gimple **stmt); |
80560f95 AM |
119 | |
120 | ||
121 | enum ssa_op_iter_type { | |
122 | ssa_op_iter_none = 0, | |
123 | ssa_op_iter_tree, | |
124 | ssa_op_iter_use, | |
125 | ssa_op_iter_def | |
126 | }; | |
127 | ||
128 | /* This structure is used in the operand iterator loops. It contains the | |
129 | items required to determine which operand is retrieved next. During | |
130 | optimization, this structure is scalarized, and any unused fields are | |
131 | optimized away, resulting in little overhead. */ | |
132 | ||
84562394 | 133 | struct ssa_op_iter |
80560f95 AM |
134 | { |
135 | enum ssa_op_iter_type iter_type; | |
136 | bool done; | |
137 | int flags; | |
138 | unsigned i; | |
139 | unsigned numops; | |
140 | use_optype_p uses; | |
355fe088 | 141 | gimple *stmt; |
84562394 | 142 | }; |
80560f95 | 143 | |
50e6a148 | 144 | /* NOTE: Keep these in sync with doc/tree-ssa.texi. */ |
80560f95 AM |
145 | /* These flags are used to determine which operands are returned during |
146 | execution of the loop. */ | |
147 | #define SSA_OP_USE 0x01 /* Real USE operands. */ | |
148 | #define SSA_OP_DEF 0x02 /* Real DEF operands. */ | |
149 | #define SSA_OP_VUSE 0x04 /* VUSE operands. */ | |
150 | #define SSA_OP_VDEF 0x08 /* VDEF operands. */ | |
80560f95 AM |
151 | /* These are commonly grouped operand flags. */ |
152 | #define SSA_OP_VIRTUAL_USES (SSA_OP_VUSE) | |
153 | #define SSA_OP_VIRTUAL_DEFS (SSA_OP_VDEF) | |
154 | #define SSA_OP_ALL_VIRTUALS (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_DEFS) | |
155 | #define SSA_OP_ALL_USES (SSA_OP_VIRTUAL_USES | SSA_OP_USE) | |
156 | #define SSA_OP_ALL_DEFS (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF) | |
157 | #define SSA_OP_ALL_OPERANDS (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS) | |
158 | ||
159 | /* This macro executes a loop over the operands of STMT specified in FLAG, | |
160 | returning each operand as a 'tree' in the variable TREEVAR. ITER is an | |
161 | ssa_op_iter structure used to control the loop. */ | |
162 | #define FOR_EACH_SSA_TREE_OPERAND(TREEVAR, STMT, ITER, FLAGS) \ | |
163 | for (TREEVAR = op_iter_init_tree (&(ITER), STMT, FLAGS); \ | |
164 | !op_iter_done (&(ITER)); \ | |
165 | (void) (TREEVAR = op_iter_next_tree (&(ITER)))) | |
166 | ||
167 | /* This macro executes a loop over the operands of STMT specified in FLAG, | |
168 | returning each operand as a 'use_operand_p' in the variable USEVAR. | |
169 | ITER is an ssa_op_iter structure used to control the loop. */ | |
170 | #define FOR_EACH_SSA_USE_OPERAND(USEVAR, STMT, ITER, FLAGS) \ | |
171 | for (USEVAR = op_iter_init_use (&(ITER), STMT, FLAGS); \ | |
172 | !op_iter_done (&(ITER)); \ | |
173 | USEVAR = op_iter_next_use (&(ITER))) | |
174 | ||
175 | /* This macro executes a loop over the operands of STMT specified in FLAG, | |
176 | returning each operand as a 'def_operand_p' in the variable DEFVAR. | |
177 | ITER is an ssa_op_iter structure used to control the loop. */ | |
178 | #define FOR_EACH_SSA_DEF_OPERAND(DEFVAR, STMT, ITER, FLAGS) \ | |
179 | for (DEFVAR = op_iter_init_def (&(ITER), STMT, FLAGS); \ | |
180 | !op_iter_done (&(ITER)); \ | |
181 | DEFVAR = op_iter_next_def (&(ITER))) | |
182 | ||
183 | /* This macro will execute a loop over all the arguments of a PHI which | |
184 | match FLAGS. A use_operand_p is always returned via USEVAR. FLAGS | |
185 | can be either SSA_OP_USE or SSA_OP_VIRTUAL_USES or SSA_OP_ALL_USES. */ | |
186 | #define FOR_EACH_PHI_ARG(USEVAR, STMT, ITER, FLAGS) \ | |
187 | for ((USEVAR) = op_iter_init_phiuse (&(ITER), STMT, FLAGS); \ | |
188 | !op_iter_done (&(ITER)); \ | |
189 | (USEVAR) = op_iter_next_use (&(ITER))) | |
190 | ||
191 | ||
192 | /* This macro will execute a loop over a stmt, regardless of whether it is | |
193 | a real stmt or a PHI node, looking at the USE nodes matching FLAGS. */ | |
194 | #define FOR_EACH_PHI_OR_STMT_USE(USEVAR, STMT, ITER, FLAGS) \ | |
195 | for ((USEVAR) = (gimple_code (STMT) == GIMPLE_PHI \ | |
538dd0b7 DM |
196 | ? op_iter_init_phiuse (&(ITER), \ |
197 | as_a <gphi *> (STMT), \ | |
198 | FLAGS) \ | |
80560f95 AM |
199 | : op_iter_init_use (&(ITER), STMT, FLAGS)); \ |
200 | !op_iter_done (&(ITER)); \ | |
201 | (USEVAR) = op_iter_next_use (&(ITER))) | |
202 | ||
203 | /* This macro will execute a loop over a stmt, regardless of whether it is | |
204 | a real stmt or a PHI node, looking at the DEF nodes matching FLAGS. */ | |
205 | #define FOR_EACH_PHI_OR_STMT_DEF(DEFVAR, STMT, ITER, FLAGS) \ | |
206 | for ((DEFVAR) = (gimple_code (STMT) == GIMPLE_PHI \ | |
538dd0b7 DM |
207 | ? op_iter_init_phidef (&(ITER), \ |
208 | as_a <gphi *> (STMT), \ | |
209 | FLAGS) \ | |
80560f95 AM |
210 | : op_iter_init_def (&(ITER), STMT, FLAGS)); \ |
211 | !op_iter_done (&(ITER)); \ | |
212 | (DEFVAR) = op_iter_next_def (&(ITER))) | |
213 | ||
214 | /* This macro returns an operand in STMT as a tree if it is the ONLY | |
215 | operand matching FLAGS. If there are 0 or more than 1 operand matching | |
216 | FLAGS, then NULL_TREE is returned. */ | |
217 | #define SINGLE_SSA_TREE_OPERAND(STMT, FLAGS) \ | |
218 | single_ssa_tree_operand (STMT, FLAGS) | |
219 | ||
220 | /* This macro returns an operand in STMT as a use_operand_p if it is the ONLY | |
221 | operand matching FLAGS. If there are 0 or more than 1 operand matching | |
222 | FLAGS, then NULL_USE_OPERAND_P is returned. */ | |
223 | #define SINGLE_SSA_USE_OPERAND(STMT, FLAGS) \ | |
224 | single_ssa_use_operand (STMT, FLAGS) | |
225 | ||
226 | /* This macro returns an operand in STMT as a def_operand_p if it is the ONLY | |
227 | operand matching FLAGS. If there are 0 or more than 1 operand matching | |
228 | FLAGS, then NULL_DEF_OPERAND_P is returned. */ | |
229 | #define SINGLE_SSA_DEF_OPERAND(STMT, FLAGS) \ | |
230 | single_ssa_def_operand (STMT, FLAGS) | |
231 | ||
232 | /* This macro returns TRUE if there are no operands matching FLAGS in STMT. */ | |
233 | #define ZERO_SSA_OPERANDS(STMT, FLAGS) zero_ssa_operands (STMT, FLAGS) | |
234 | ||
235 | /* This macro counts the number of operands in STMT matching FLAGS. */ | |
236 | #define NUM_SSA_OPERANDS(STMT, FLAGS) num_ssa_operands (STMT, FLAGS) | |
237 | ||
238 | ||
239 | /* Delink an immediate_uses node from its chain. */ | |
240 | static inline void | |
241 | delink_imm_use (ssa_use_operand_t *linknode) | |
242 | { | |
243 | /* Return if this node is not in a list. */ | |
244 | if (linknode->prev == NULL) | |
245 | return; | |
246 | ||
247 | linknode->prev->next = linknode->next; | |
248 | linknode->next->prev = linknode->prev; | |
249 | linknode->prev = NULL; | |
250 | linknode->next = NULL; | |
251 | } | |
252 | ||
253 | /* Link ssa_imm_use node LINKNODE into the chain for LIST. */ | |
254 | static inline void | |
255 | link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list) | |
256 | { | |
257 | /* Link the new node at the head of the list. If we are in the process of | |
258 | traversing the list, we won't visit any new nodes added to it. */ | |
259 | linknode->prev = list; | |
260 | linknode->next = list->next; | |
261 | list->next->prev = linknode; | |
262 | list->next = linknode; | |
263 | } | |
264 | ||
265 | /* Link ssa_imm_use node LINKNODE into the chain for DEF. */ | |
266 | static inline void | |
267 | link_imm_use (ssa_use_operand_t *linknode, tree def) | |
268 | { | |
269 | ssa_use_operand_t *root; | |
270 | ||
271 | if (!def || TREE_CODE (def) != SSA_NAME) | |
272 | linknode->prev = NULL; | |
273 | else | |
274 | { | |
275 | root = &(SSA_NAME_IMM_USE_NODE (def)); | |
276 | if (linknode->use) | |
277 | gcc_checking_assert (*(linknode->use) == def); | |
278 | link_imm_use_to_list (linknode, root); | |
279 | } | |
280 | } | |
281 | ||
282 | /* Set the value of a use pointed to by USE to VAL. */ | |
283 | static inline void | |
284 | set_ssa_use_from_ptr (use_operand_p use, tree val) | |
285 | { | |
286 | delink_imm_use (use); | |
287 | *(use->use) = val; | |
288 | link_imm_use (use, val); | |
289 | } | |
290 | ||
291 | /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring | |
292 | in STMT. */ | |
293 | static inline void | |
355fe088 | 294 | link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple *stmt) |
80560f95 AM |
295 | { |
296 | if (stmt) | |
297 | link_imm_use (linknode, def); | |
298 | else | |
299 | link_imm_use (linknode, NULL); | |
300 | linknode->loc.stmt = stmt; | |
301 | } | |
302 | ||
303 | /* Relink a new node in place of an old node in the list. */ | |
304 | static inline void | |
305 | relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old) | |
306 | { | |
307 | /* The node one had better be in the same list. */ | |
308 | gcc_checking_assert (*(old->use) == *(node->use)); | |
309 | node->prev = old->prev; | |
310 | node->next = old->next; | |
311 | if (old->prev) | |
312 | { | |
313 | old->prev->next = node; | |
314 | old->next->prev = node; | |
315 | /* Remove the old node from the list. */ | |
316 | old->prev = NULL; | |
317 | } | |
318 | } | |
319 | ||
320 | /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring | |
321 | in STMT. */ | |
322 | static inline void | |
323 | relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old, | |
355fe088 | 324 | gimple *stmt) |
80560f95 AM |
325 | { |
326 | if (stmt) | |
327 | relink_imm_use (linknode, old); | |
328 | else | |
329 | link_imm_use (linknode, NULL); | |
330 | linknode->loc.stmt = stmt; | |
331 | } | |
332 | ||
333 | ||
334 | /* Return true is IMM has reached the end of the immediate use list. */ | |
335 | static inline bool | |
336 | end_readonly_imm_use_p (const imm_use_iterator *imm) | |
337 | { | |
338 | return (imm->imm_use == imm->end_p); | |
339 | } | |
340 | ||
341 | /* Initialize iterator IMM to process the list for VAR. */ | |
342 | static inline use_operand_p | |
343 | first_readonly_imm_use (imm_use_iterator *imm, tree var) | |
344 | { | |
345 | imm->end_p = &(SSA_NAME_IMM_USE_NODE (var)); | |
346 | imm->imm_use = imm->end_p->next; | |
80560f95 | 347 | imm->iter_node.next = imm->imm_use->next; |
80560f95 AM |
348 | if (end_readonly_imm_use_p (imm)) |
349 | return NULL_USE_OPERAND_P; | |
350 | return imm->imm_use; | |
351 | } | |
352 | ||
353 | /* Bump IMM to the next use in the list. */ | |
354 | static inline use_operand_p | |
355 | next_readonly_imm_use (imm_use_iterator *imm) | |
356 | { | |
357 | use_operand_p old = imm->imm_use; | |
358 | ||
80560f95 AM |
359 | /* If this assertion fails, it indicates the 'next' pointer has changed |
360 | since the last bump. This indicates that the list is being modified | |
361 | via stmt changes, or SET_USE, or somesuch thing, and you need to be | |
362 | using the SAFE version of the iterator. */ | |
b2b29377 MM |
363 | if (flag_checking) |
364 | { | |
365 | gcc_assert (imm->iter_node.next == old->next); | |
366 | imm->iter_node.next = old->next->next; | |
367 | } | |
80560f95 AM |
368 | |
369 | imm->imm_use = old->next; | |
370 | if (end_readonly_imm_use_p (imm)) | |
371 | return NULL_USE_OPERAND_P; | |
372 | return imm->imm_use; | |
373 | } | |
374 | ||
375 | ||
376 | /* Return true if VAR has no nondebug uses. */ | |
377 | static inline bool | |
378 | has_zero_uses (const_tree var) | |
379 | { | |
f9ffade0 AM |
380 | const ssa_use_operand_t *const head = &(SSA_NAME_IMM_USE_NODE (var)); |
381 | const ssa_use_operand_t *ptr; | |
80560f95 | 382 | |
f9ffade0 AM |
383 | for (ptr = head->next; ptr != head; ptr = ptr->next) |
384 | if (USE_STMT (ptr) && !is_gimple_debug (USE_STMT (ptr))) | |
385 | return false; | |
80560f95 | 386 | |
f9ffade0 | 387 | return true; |
80560f95 AM |
388 | } |
389 | ||
390 | /* Return true if VAR has a single nondebug use. */ | |
391 | static inline bool | |
392 | has_single_use (const_tree var) | |
393 | { | |
f9ffade0 AM |
394 | const ssa_use_operand_t *const head = &(SSA_NAME_IMM_USE_NODE (var)); |
395 | const ssa_use_operand_t *ptr; | |
396 | bool single = false; | |
397 | ||
398 | for (ptr = head->next; ptr != head; ptr = ptr->next) | |
399 | if (USE_STMT(ptr) && !is_gimple_debug (USE_STMT (ptr))) | |
400 | { | |
401 | if (single) | |
402 | return false; | |
403 | else | |
404 | single = true; | |
405 | } | |
406 | ||
407 | return single; | |
80560f95 | 408 | } |
f9ffade0 | 409 | |
80560f95 AM |
410 | /* If VAR has only a single immediate nondebug use, return true, and |
411 | set USE_P and STMT to the use pointer and stmt of occurrence. */ | |
412 | static inline bool | |
355fe088 | 413 | single_imm_use (const_tree var, use_operand_p *use_p, gimple **stmt) |
80560f95 AM |
414 | { |
415 | const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var)); | |
416 | ||
417 | /* If there aren't any uses whatsoever, we're done. */ | |
418 | if (ptr == ptr->next) | |
419 | { | |
420 | return_false: | |
421 | *use_p = NULL_USE_OPERAND_P; | |
422 | *stmt = NULL; | |
423 | return false; | |
424 | } | |
425 | ||
426 | /* If there's a single use, check that it's not a debug stmt. */ | |
427 | if (ptr == ptr->next->next) | |
428 | { | |
f9ffade0 | 429 | if (USE_STMT (ptr->next) && !is_gimple_debug (USE_STMT (ptr->next))) |
80560f95 AM |
430 | { |
431 | *use_p = ptr->next; | |
432 | *stmt = ptr->next->loc.stmt; | |
433 | return true; | |
434 | } | |
435 | else | |
436 | goto return_false; | |
437 | } | |
438 | ||
80560f95 AM |
439 | return single_imm_use_1 (ptr, use_p, stmt); |
440 | } | |
441 | ||
442 | /* Return the number of nondebug immediate uses of VAR. */ | |
443 | static inline unsigned int | |
444 | num_imm_uses (const_tree var) | |
445 | { | |
446 | const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var)); | |
447 | const ssa_use_operand_t *ptr; | |
448 | unsigned int num = 0; | |
449 | ||
450 | if (!MAY_HAVE_DEBUG_STMTS) | |
451 | for (ptr = start->next; ptr != start; ptr = ptr->next) | |
f9ffade0 AM |
452 | if (USE_STMT (ptr)) |
453 | num++; | |
80560f95 AM |
454 | else |
455 | for (ptr = start->next; ptr != start; ptr = ptr->next) | |
f9ffade0 | 456 | if (USE_STMT (ptr) && !is_gimple_debug (USE_STMT (ptr))) |
80560f95 AM |
457 | num++; |
458 | ||
459 | return num; | |
460 | } | |
461 | ||
462 | /* ----------------------------------------------------------------------- */ | |
463 | ||
464 | /* The following set of routines are used to iterator over various type of | |
465 | SSA operands. */ | |
466 | ||
467 | /* Return true if PTR is finished iterating. */ | |
468 | static inline bool | |
469 | op_iter_done (const ssa_op_iter *ptr) | |
470 | { | |
471 | return ptr->done; | |
472 | } | |
473 | ||
474 | /* Get the next iterator use value for PTR. */ | |
475 | static inline use_operand_p | |
476 | op_iter_next_use (ssa_op_iter *ptr) | |
477 | { | |
478 | use_operand_p use_p; | |
479 | gcc_checking_assert (ptr->iter_type == ssa_op_iter_use); | |
480 | if (ptr->uses) | |
481 | { | |
482 | use_p = USE_OP_PTR (ptr->uses); | |
483 | ptr->uses = ptr->uses->next; | |
484 | return use_p; | |
485 | } | |
486 | if (ptr->i < ptr->numops) | |
487 | { | |
488 | return PHI_ARG_DEF_PTR (ptr->stmt, (ptr->i)++); | |
489 | } | |
490 | ptr->done = true; | |
491 | return NULL_USE_OPERAND_P; | |
492 | } | |
493 | ||
494 | /* Get the next iterator def value for PTR. */ | |
495 | static inline def_operand_p | |
496 | op_iter_next_def (ssa_op_iter *ptr) | |
497 | { | |
498 | gcc_checking_assert (ptr->iter_type == ssa_op_iter_def); | |
499 | if (ptr->flags & SSA_OP_VDEF) | |
500 | { | |
501 | tree *p; | |
502 | ptr->flags &= ~SSA_OP_VDEF; | |
503 | p = gimple_vdef_ptr (ptr->stmt); | |
504 | if (p && *p) | |
505 | return p; | |
506 | } | |
507 | if (ptr->flags & SSA_OP_DEF) | |
508 | { | |
509 | while (ptr->i < ptr->numops) | |
510 | { | |
511 | tree *val = gimple_op_ptr (ptr->stmt, ptr->i); | |
512 | ptr->i++; | |
513 | if (*val) | |
514 | { | |
515 | if (TREE_CODE (*val) == TREE_LIST) | |
516 | val = &TREE_VALUE (*val); | |
517 | if (TREE_CODE (*val) == SSA_NAME | |
518 | || is_gimple_reg (*val)) | |
519 | return val; | |
520 | } | |
521 | } | |
522 | ptr->flags &= ~SSA_OP_DEF; | |
523 | } | |
524 | ||
525 | ptr->done = true; | |
526 | return NULL_DEF_OPERAND_P; | |
527 | } | |
528 | ||
529 | /* Get the next iterator tree value for PTR. */ | |
530 | static inline tree | |
531 | op_iter_next_tree (ssa_op_iter *ptr) | |
532 | { | |
533 | tree val; | |
534 | gcc_checking_assert (ptr->iter_type == ssa_op_iter_tree); | |
535 | if (ptr->uses) | |
536 | { | |
537 | val = USE_OP (ptr->uses); | |
538 | ptr->uses = ptr->uses->next; | |
539 | return val; | |
540 | } | |
541 | if (ptr->flags & SSA_OP_VDEF) | |
542 | { | |
543 | ptr->flags &= ~SSA_OP_VDEF; | |
544 | if ((val = gimple_vdef (ptr->stmt))) | |
545 | return val; | |
546 | } | |
547 | if (ptr->flags & SSA_OP_DEF) | |
548 | { | |
549 | while (ptr->i < ptr->numops) | |
550 | { | |
551 | val = gimple_op (ptr->stmt, ptr->i); | |
552 | ptr->i++; | |
553 | if (val) | |
554 | { | |
555 | if (TREE_CODE (val) == TREE_LIST) | |
556 | val = TREE_VALUE (val); | |
557 | if (TREE_CODE (val) == SSA_NAME | |
558 | || is_gimple_reg (val)) | |
559 | return val; | |
560 | } | |
561 | } | |
562 | ptr->flags &= ~SSA_OP_DEF; | |
563 | } | |
564 | ||
565 | ptr->done = true; | |
566 | return NULL_TREE; | |
567 | } | |
568 | ||
569 | ||
570 | /* This functions clears the iterator PTR, and marks it done. This is normally | |
571 | used to prevent warnings in the compile about might be uninitialized | |
572 | components. */ | |
573 | ||
574 | static inline void | |
575 | clear_and_done_ssa_iter (ssa_op_iter *ptr) | |
576 | { | |
577 | ptr->i = 0; | |
578 | ptr->numops = 0; | |
579 | ptr->uses = NULL; | |
580 | ptr->iter_type = ssa_op_iter_none; | |
581 | ptr->stmt = NULL; | |
582 | ptr->done = true; | |
583 | ptr->flags = 0; | |
584 | } | |
585 | ||
586 | /* Initialize the iterator PTR to the virtual defs in STMT. */ | |
587 | static inline void | |
355fe088 | 588 | op_iter_init (ssa_op_iter *ptr, gimple *stmt, int flags) |
80560f95 AM |
589 | { |
590 | /* PHI nodes require a different iterator initialization path. We | |
591 | do not support iterating over virtual defs or uses without | |
592 | iterating over defs or uses at the same time. */ | |
593 | gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI | |
594 | && (!(flags & SSA_OP_VDEF) || (flags & SSA_OP_DEF)) | |
595 | && (!(flags & SSA_OP_VUSE) || (flags & SSA_OP_USE))); | |
596 | ptr->numops = 0; | |
597 | if (flags & (SSA_OP_DEF | SSA_OP_VDEF)) | |
598 | { | |
599 | switch (gimple_code (stmt)) | |
600 | { | |
601 | case GIMPLE_ASSIGN: | |
602 | case GIMPLE_CALL: | |
603 | ptr->numops = 1; | |
604 | break; | |
605 | case GIMPLE_ASM: | |
538dd0b7 | 606 | ptr->numops = gimple_asm_noutputs (as_a <gasm *> (stmt)); |
80560f95 AM |
607 | break; |
608 | default: | |
609 | ptr->numops = 0; | |
610 | flags &= ~(SSA_OP_DEF | SSA_OP_VDEF); | |
611 | break; | |
612 | } | |
613 | } | |
614 | ptr->uses = (flags & (SSA_OP_USE|SSA_OP_VUSE)) ? gimple_use_ops (stmt) : NULL; | |
615 | if (!(flags & SSA_OP_VUSE) | |
616 | && ptr->uses | |
617 | && gimple_vuse (stmt) != NULL_TREE) | |
618 | ptr->uses = ptr->uses->next; | |
619 | ptr->done = false; | |
620 | ptr->i = 0; | |
621 | ||
622 | ptr->stmt = stmt; | |
623 | ptr->flags = flags; | |
624 | } | |
625 | ||
626 | /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return | |
627 | the first use. */ | |
628 | static inline use_operand_p | |
355fe088 | 629 | op_iter_init_use (ssa_op_iter *ptr, gimple *stmt, int flags) |
80560f95 AM |
630 | { |
631 | gcc_checking_assert ((flags & SSA_OP_ALL_DEFS) == 0 | |
632 | && (flags & SSA_OP_USE)); | |
633 | op_iter_init (ptr, stmt, flags); | |
634 | ptr->iter_type = ssa_op_iter_use; | |
635 | return op_iter_next_use (ptr); | |
636 | } | |
637 | ||
638 | /* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return | |
639 | the first def. */ | |
640 | static inline def_operand_p | |
355fe088 | 641 | op_iter_init_def (ssa_op_iter *ptr, gimple *stmt, int flags) |
80560f95 AM |
642 | { |
643 | gcc_checking_assert ((flags & SSA_OP_ALL_USES) == 0 | |
644 | && (flags & SSA_OP_DEF)); | |
645 | op_iter_init (ptr, stmt, flags); | |
646 | ptr->iter_type = ssa_op_iter_def; | |
647 | return op_iter_next_def (ptr); | |
648 | } | |
649 | ||
650 | /* Initialize iterator PTR to the operands in STMT based on FLAGS. Return | |
651 | the first operand as a tree. */ | |
652 | static inline tree | |
355fe088 | 653 | op_iter_init_tree (ssa_op_iter *ptr, gimple *stmt, int flags) |
80560f95 AM |
654 | { |
655 | op_iter_init (ptr, stmt, flags); | |
656 | ptr->iter_type = ssa_op_iter_tree; | |
657 | return op_iter_next_tree (ptr); | |
658 | } | |
659 | ||
660 | ||
661 | /* If there is a single operand in STMT matching FLAGS, return it. Otherwise | |
662 | return NULL. */ | |
663 | static inline tree | |
355fe088 | 664 | single_ssa_tree_operand (gimple *stmt, int flags) |
80560f95 AM |
665 | { |
666 | tree var; | |
667 | ssa_op_iter iter; | |
668 | ||
669 | var = op_iter_init_tree (&iter, stmt, flags); | |
670 | if (op_iter_done (&iter)) | |
671 | return NULL_TREE; | |
672 | op_iter_next_tree (&iter); | |
673 | if (op_iter_done (&iter)) | |
674 | return var; | |
675 | return NULL_TREE; | |
676 | } | |
677 | ||
678 | ||
679 | /* If there is a single operand in STMT matching FLAGS, return it. Otherwise | |
680 | return NULL. */ | |
681 | static inline use_operand_p | |
355fe088 | 682 | single_ssa_use_operand (gimple *stmt, int flags) |
80560f95 AM |
683 | { |
684 | use_operand_p var; | |
685 | ssa_op_iter iter; | |
686 | ||
687 | var = op_iter_init_use (&iter, stmt, flags); | |
688 | if (op_iter_done (&iter)) | |
689 | return NULL_USE_OPERAND_P; | |
690 | op_iter_next_use (&iter); | |
691 | if (op_iter_done (&iter)) | |
692 | return var; | |
693 | return NULL_USE_OPERAND_P; | |
694 | } | |
695 | ||
696 | ||
697 | ||
698 | /* If there is a single operand in STMT matching FLAGS, return it. Otherwise | |
699 | return NULL. */ | |
700 | static inline def_operand_p | |
355fe088 | 701 | single_ssa_def_operand (gimple *stmt, int flags) |
80560f95 AM |
702 | { |
703 | def_operand_p var; | |
704 | ssa_op_iter iter; | |
705 | ||
706 | var = op_iter_init_def (&iter, stmt, flags); | |
707 | if (op_iter_done (&iter)) | |
708 | return NULL_DEF_OPERAND_P; | |
709 | op_iter_next_def (&iter); | |
710 | if (op_iter_done (&iter)) | |
711 | return var; | |
712 | return NULL_DEF_OPERAND_P; | |
713 | } | |
714 | ||
715 | ||
716 | /* Return true if there are zero operands in STMT matching the type | |
717 | given in FLAGS. */ | |
718 | static inline bool | |
355fe088 | 719 | zero_ssa_operands (gimple *stmt, int flags) |
80560f95 AM |
720 | { |
721 | ssa_op_iter iter; | |
722 | ||
723 | op_iter_init_tree (&iter, stmt, flags); | |
724 | return op_iter_done (&iter); | |
725 | } | |
726 | ||
727 | ||
728 | /* Return the number of operands matching FLAGS in STMT. */ | |
729 | static inline int | |
355fe088 | 730 | num_ssa_operands (gimple *stmt, int flags) |
80560f95 AM |
731 | { |
732 | ssa_op_iter iter; | |
733 | tree t; | |
734 | int num = 0; | |
735 | ||
736 | gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI); | |
737 | FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags) | |
738 | num++; | |
739 | return num; | |
740 | } | |
741 | ||
742 | /* If there is a single DEF in the PHI node which matches FLAG, return it. | |
743 | Otherwise return NULL_DEF_OPERAND_P. */ | |
744 | static inline tree | |
538dd0b7 | 745 | single_phi_def (gphi *stmt, int flags) |
80560f95 AM |
746 | { |
747 | tree def = PHI_RESULT (stmt); | |
748 | if ((flags & SSA_OP_DEF) && is_gimple_reg (def)) | |
749 | return def; | |
750 | if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def)) | |
751 | return def; | |
752 | return NULL_TREE; | |
753 | } | |
754 | ||
755 | /* Initialize the iterator PTR for uses matching FLAGS in PHI. FLAGS should | |
756 | be either SSA_OP_USES or SSA_OP_VIRTUAL_USES. */ | |
757 | static inline use_operand_p | |
538dd0b7 | 758 | op_iter_init_phiuse (ssa_op_iter *ptr, gphi *phi, int flags) |
80560f95 AM |
759 | { |
760 | tree phi_def = gimple_phi_result (phi); | |
761 | int comp; | |
762 | ||
763 | clear_and_done_ssa_iter (ptr); | |
764 | ptr->done = false; | |
765 | ||
766 | gcc_checking_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0); | |
767 | ||
768 | comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES); | |
769 | ||
770 | /* If the PHI node doesn't the operand type we care about, we're done. */ | |
771 | if ((flags & comp) == 0) | |
772 | { | |
773 | ptr->done = true; | |
774 | return NULL_USE_OPERAND_P; | |
775 | } | |
776 | ||
777 | ptr->stmt = phi; | |
778 | ptr->numops = gimple_phi_num_args (phi); | |
779 | ptr->iter_type = ssa_op_iter_use; | |
780 | ptr->flags = flags; | |
781 | return op_iter_next_use (ptr); | |
782 | } | |
783 | ||
784 | ||
785 | /* Start an iterator for a PHI definition. */ | |
786 | ||
787 | static inline def_operand_p | |
538dd0b7 | 788 | op_iter_init_phidef (ssa_op_iter *ptr, gphi *phi, int flags) |
80560f95 AM |
789 | { |
790 | tree phi_def = PHI_RESULT (phi); | |
791 | int comp; | |
792 | ||
793 | clear_and_done_ssa_iter (ptr); | |
794 | ptr->done = false; | |
795 | ||
796 | gcc_checking_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0); | |
797 | ||
798 | comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS); | |
799 | ||
800 | /* If the PHI node doesn't have the operand type we care about, | |
801 | we're done. */ | |
802 | if ((flags & comp) == 0) | |
803 | { | |
804 | ptr->done = true; | |
805 | return NULL_DEF_OPERAND_P; | |
806 | } | |
807 | ||
808 | ptr->iter_type = ssa_op_iter_def; | |
809 | /* The first call to op_iter_next_def will terminate the iterator since | |
810 | all the fields are NULL. Simply return the result here as the first and | |
811 | therefore only result. */ | |
812 | return PHI_RESULT_PTR (phi); | |
813 | } | |
814 | ||
815 | /* Return true is IMM has reached the end of the immediate use stmt list. */ | |
816 | ||
817 | static inline bool | |
818 | end_imm_use_stmt_p (const imm_use_iterator *imm) | |
819 | { | |
820 | return (imm->imm_use == imm->end_p); | |
821 | } | |
822 | ||
823 | /* Finished the traverse of an immediate use stmt list IMM by removing the | |
824 | placeholder node from the list. */ | |
825 | ||
826 | static inline void | |
827 | end_imm_use_stmt_traverse (imm_use_iterator *imm) | |
828 | { | |
829 | delink_imm_use (&(imm->iter_node)); | |
830 | } | |
831 | ||
832 | /* Immediate use traversal of uses within a stmt require that all the | |
833 | uses on a stmt be sequentially listed. This routine is used to build up | |
834 | this sequential list by adding USE_P to the end of the current list | |
835 | currently delimited by HEAD and LAST_P. The new LAST_P value is | |
836 | returned. */ | |
837 | ||
838 | static inline use_operand_p | |
839 | move_use_after_head (use_operand_p use_p, use_operand_p head, | |
840 | use_operand_p last_p) | |
841 | { | |
842 | gcc_checking_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head)); | |
843 | /* Skip head when we find it. */ | |
844 | if (use_p != head) | |
845 | { | |
846 | /* If use_p is already linked in after last_p, continue. */ | |
847 | if (last_p->next == use_p) | |
848 | last_p = use_p; | |
849 | else | |
850 | { | |
851 | /* Delink from current location, and link in at last_p. */ | |
852 | delink_imm_use (use_p); | |
853 | link_imm_use_to_list (use_p, last_p); | |
854 | last_p = use_p; | |
855 | } | |
856 | } | |
857 | return last_p; | |
858 | } | |
859 | ||
860 | ||
861 | /* This routine will relink all uses with the same stmt as HEAD into the list | |
862 | immediately following HEAD for iterator IMM. */ | |
863 | ||
864 | static inline void | |
865 | link_use_stmts_after (use_operand_p head, imm_use_iterator *imm) | |
866 | { | |
867 | use_operand_p use_p; | |
868 | use_operand_p last_p = head; | |
355fe088 | 869 | gimple *head_stmt = USE_STMT (head); |
80560f95 AM |
870 | tree use = USE_FROM_PTR (head); |
871 | ssa_op_iter op_iter; | |
872 | int flag; | |
873 | ||
874 | /* Only look at virtual or real uses, depending on the type of HEAD. */ | |
875 | flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES); | |
876 | ||
538dd0b7 | 877 | if (gphi *phi = dyn_cast <gphi *> (head_stmt)) |
80560f95 | 878 | { |
538dd0b7 | 879 | FOR_EACH_PHI_ARG (use_p, phi, op_iter, flag) |
80560f95 AM |
880 | if (USE_FROM_PTR (use_p) == use) |
881 | last_p = move_use_after_head (use_p, head, last_p); | |
882 | } | |
883 | else | |
884 | { | |
885 | if (flag == SSA_OP_USE) | |
886 | { | |
887 | FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag) | |
888 | if (USE_FROM_PTR (use_p) == use) | |
889 | last_p = move_use_after_head (use_p, head, last_p); | |
890 | } | |
891 | else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P) | |
892 | { | |
893 | if (USE_FROM_PTR (use_p) == use) | |
894 | last_p = move_use_after_head (use_p, head, last_p); | |
895 | } | |
896 | } | |
897 | /* Link iter node in after last_p. */ | |
898 | if (imm->iter_node.prev != NULL) | |
899 | delink_imm_use (&imm->iter_node); | |
900 | link_imm_use_to_list (&(imm->iter_node), last_p); | |
901 | } | |
902 | ||
903 | /* Initialize IMM to traverse over uses of VAR. Return the first statement. */ | |
355fe088 | 904 | static inline gimple * |
80560f95 AM |
905 | first_imm_use_stmt (imm_use_iterator *imm, tree var) |
906 | { | |
907 | imm->end_p = &(SSA_NAME_IMM_USE_NODE (var)); | |
908 | imm->imm_use = imm->end_p->next; | |
909 | imm->next_imm_name = NULL_USE_OPERAND_P; | |
910 | ||
911 | /* iter_node is used as a marker within the immediate use list to indicate | |
912 | where the end of the current stmt's uses are. Initialize it to NULL | |
913 | stmt and use, which indicates a marker node. */ | |
914 | imm->iter_node.prev = NULL_USE_OPERAND_P; | |
915 | imm->iter_node.next = NULL_USE_OPERAND_P; | |
916 | imm->iter_node.loc.stmt = NULL; | |
917 | imm->iter_node.use = NULL; | |
918 | ||
919 | if (end_imm_use_stmt_p (imm)) | |
920 | return NULL; | |
921 | ||
922 | link_use_stmts_after (imm->imm_use, imm); | |
923 | ||
924 | return USE_STMT (imm->imm_use); | |
925 | } | |
926 | ||
927 | /* Bump IMM to the next stmt which has a use of var. */ | |
928 | ||
355fe088 | 929 | static inline gimple * |
80560f95 AM |
930 | next_imm_use_stmt (imm_use_iterator *imm) |
931 | { | |
932 | imm->imm_use = imm->iter_node.next; | |
933 | if (end_imm_use_stmt_p (imm)) | |
934 | { | |
935 | if (imm->iter_node.prev != NULL) | |
936 | delink_imm_use (&imm->iter_node); | |
937 | return NULL; | |
938 | } | |
939 | ||
940 | link_use_stmts_after (imm->imm_use, imm); | |
941 | return USE_STMT (imm->imm_use); | |
942 | } | |
943 | ||
944 | /* This routine will return the first use on the stmt IMM currently refers | |
945 | to. */ | |
946 | ||
947 | static inline use_operand_p | |
948 | first_imm_use_on_stmt (imm_use_iterator *imm) | |
949 | { | |
950 | imm->next_imm_name = imm->imm_use->next; | |
951 | return imm->imm_use; | |
952 | } | |
953 | ||
954 | /* Return TRUE if the last use on the stmt IMM refers to has been visited. */ | |
955 | ||
956 | static inline bool | |
957 | end_imm_use_on_stmt_p (const imm_use_iterator *imm) | |
958 | { | |
959 | return (imm->imm_use == &(imm->iter_node)); | |
960 | } | |
961 | ||
962 | /* Bump to the next use on the stmt IMM refers to, return NULL if done. */ | |
963 | ||
964 | static inline use_operand_p | |
965 | next_imm_use_on_stmt (imm_use_iterator *imm) | |
966 | { | |
967 | imm->imm_use = imm->next_imm_name; | |
968 | if (end_imm_use_on_stmt_p (imm)) | |
969 | return NULL_USE_OPERAND_P; | |
970 | else | |
971 | { | |
972 | imm->next_imm_name = imm->imm_use->next; | |
973 | return imm->imm_use; | |
974 | } | |
975 | } | |
976 | ||
977 | /* Delink all immediate_use information for STMT. */ | |
978 | static inline void | |
355fe088 | 979 | delink_stmt_imm_use (gimple *stmt) |
80560f95 AM |
980 | { |
981 | ssa_op_iter iter; | |
982 | use_operand_p use_p; | |
983 | ||
984 | if (ssa_operands_active (cfun)) | |
985 | FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_ALL_USES) | |
986 | delink_imm_use (use_p); | |
987 | } | |
988 | ||
989 | #endif /* GCC_TREE_SSA_ITERATORS_H */ |