2014-12-18 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ssa-iterators.h
blobf178e77c9f6985d89b99d8a1165005f873748b64
1 /* Header file for SSA iterators.
2 Copyright (C) 2013-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifndef GCC_SSA_ITERATORS_H
21 #define GCC_SSA_ITERATORS_H
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.
26 The structure ssa_use_operand_t consists of PREV and NEXT pointers
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.
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.
39 Fast iteration allows each use to be examined, but does not allow
40 any modifications to the uses or stmts.
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.
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.
56 If iteration is halted early, the marker node must be removed from
57 the list before continuing. */
58 struct imm_use_iterator
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;
72 /* Use this iterator when simply looking at stmts. Adding, deleting or
73 modifying stmts will cause this iterator to malfunction. */
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))))
80 /* Use this iterator to visit each stmt which has a use of SSAVAR. */
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))))
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; \
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:
101 FOR_EACH_IMM_USE_STMT (stmt, iter, ssavar)
103 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
105 SET_USE (use_p, blah);
107 update_stmt (stmt);
108 } */
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))))
117 extern bool has_zero_uses_1 (const ssa_use_operand_t *head);
118 extern bool single_imm_use_1 (const ssa_use_operand_t *head,
119 use_operand_p *use_p, gimple *stmt);
122 enum ssa_op_iter_type {
123 ssa_op_iter_none = 0,
124 ssa_op_iter_tree,
125 ssa_op_iter_use,
126 ssa_op_iter_def
129 /* This structure is used in the operand iterator loops. It contains the
130 items required to determine which operand is retrieved next. During
131 optimization, this structure is scalarized, and any unused fields are
132 optimized away, resulting in little overhead. */
134 struct ssa_op_iter
136 enum ssa_op_iter_type iter_type;
137 bool done;
138 int flags;
139 unsigned i;
140 unsigned numops;
141 use_optype_p uses;
142 gimple stmt;
145 /* NOTE: Keep these in sync with doc/tree-ssa.texi. */
146 /* These flags are used to determine which operands are returned during
147 execution of the loop. */
148 #define SSA_OP_USE 0x01 /* Real USE operands. */
149 #define SSA_OP_DEF 0x02 /* Real DEF operands. */
150 #define SSA_OP_VUSE 0x04 /* VUSE operands. */
151 #define SSA_OP_VDEF 0x08 /* VDEF operands. */
152 /* These are commonly grouped operand flags. */
153 #define SSA_OP_VIRTUAL_USES (SSA_OP_VUSE)
154 #define SSA_OP_VIRTUAL_DEFS (SSA_OP_VDEF)
155 #define SSA_OP_ALL_VIRTUALS (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_DEFS)
156 #define SSA_OP_ALL_USES (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
157 #define SSA_OP_ALL_DEFS (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
158 #define SSA_OP_ALL_OPERANDS (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
160 /* This macro executes a loop over the operands of STMT specified in FLAG,
161 returning each operand as a 'tree' in the variable TREEVAR. ITER is an
162 ssa_op_iter structure used to control the loop. */
163 #define FOR_EACH_SSA_TREE_OPERAND(TREEVAR, STMT, ITER, FLAGS) \
164 for (TREEVAR = op_iter_init_tree (&(ITER), STMT, FLAGS); \
165 !op_iter_done (&(ITER)); \
166 (void) (TREEVAR = op_iter_next_tree (&(ITER))))
168 /* This macro executes a loop over the operands of STMT specified in FLAG,
169 returning each operand as a 'use_operand_p' in the variable USEVAR.
170 ITER is an ssa_op_iter structure used to control the loop. */
171 #define FOR_EACH_SSA_USE_OPERAND(USEVAR, STMT, ITER, FLAGS) \
172 for (USEVAR = op_iter_init_use (&(ITER), STMT, FLAGS); \
173 !op_iter_done (&(ITER)); \
174 USEVAR = op_iter_next_use (&(ITER)))
176 /* This macro executes a loop over the operands of STMT specified in FLAG,
177 returning each operand as a 'def_operand_p' in the variable DEFVAR.
178 ITER is an ssa_op_iter structure used to control the loop. */
179 #define FOR_EACH_SSA_DEF_OPERAND(DEFVAR, STMT, ITER, FLAGS) \
180 for (DEFVAR = op_iter_init_def (&(ITER), STMT, FLAGS); \
181 !op_iter_done (&(ITER)); \
182 DEFVAR = op_iter_next_def (&(ITER)))
184 /* This macro will execute a loop over all the arguments of a PHI which
185 match FLAGS. A use_operand_p is always returned via USEVAR. FLAGS
186 can be either SSA_OP_USE or SSA_OP_VIRTUAL_USES or SSA_OP_ALL_USES. */
187 #define FOR_EACH_PHI_ARG(USEVAR, STMT, ITER, FLAGS) \
188 for ((USEVAR) = op_iter_init_phiuse (&(ITER), STMT, FLAGS); \
189 !op_iter_done (&(ITER)); \
190 (USEVAR) = op_iter_next_use (&(ITER)))
193 /* This macro will execute a loop over a stmt, regardless of whether it is
194 a real stmt or a PHI node, looking at the USE nodes matching FLAGS. */
195 #define FOR_EACH_PHI_OR_STMT_USE(USEVAR, STMT, ITER, FLAGS) \
196 for ((USEVAR) = (gimple_code (STMT) == GIMPLE_PHI \
197 ? op_iter_init_phiuse (&(ITER), \
198 as_a <gphi *> (STMT), \
199 FLAGS) \
200 : op_iter_init_use (&(ITER), STMT, FLAGS)); \
201 !op_iter_done (&(ITER)); \
202 (USEVAR) = op_iter_next_use (&(ITER)))
204 /* This macro will execute a loop over a stmt, regardless of whether it is
205 a real stmt or a PHI node, looking at the DEF nodes matching FLAGS. */
206 #define FOR_EACH_PHI_OR_STMT_DEF(DEFVAR, STMT, ITER, FLAGS) \
207 for ((DEFVAR) = (gimple_code (STMT) == GIMPLE_PHI \
208 ? op_iter_init_phidef (&(ITER), \
209 as_a <gphi *> (STMT), \
210 FLAGS) \
211 : op_iter_init_def (&(ITER), STMT, FLAGS)); \
212 !op_iter_done (&(ITER)); \
213 (DEFVAR) = op_iter_next_def (&(ITER)))
215 /* This macro returns an operand in STMT as a tree if it is the ONLY
216 operand matching FLAGS. If there are 0 or more than 1 operand matching
217 FLAGS, then NULL_TREE is returned. */
218 #define SINGLE_SSA_TREE_OPERAND(STMT, FLAGS) \
219 single_ssa_tree_operand (STMT, FLAGS)
221 /* This macro returns an operand in STMT as a use_operand_p if it is the ONLY
222 operand matching FLAGS. If there are 0 or more than 1 operand matching
223 FLAGS, then NULL_USE_OPERAND_P is returned. */
224 #define SINGLE_SSA_USE_OPERAND(STMT, FLAGS) \
225 single_ssa_use_operand (STMT, FLAGS)
227 /* This macro returns an operand in STMT as a def_operand_p if it is the ONLY
228 operand matching FLAGS. If there are 0 or more than 1 operand matching
229 FLAGS, then NULL_DEF_OPERAND_P is returned. */
230 #define SINGLE_SSA_DEF_OPERAND(STMT, FLAGS) \
231 single_ssa_def_operand (STMT, FLAGS)
233 /* This macro returns TRUE if there are no operands matching FLAGS in STMT. */
234 #define ZERO_SSA_OPERANDS(STMT, FLAGS) zero_ssa_operands (STMT, FLAGS)
236 /* This macro counts the number of operands in STMT matching FLAGS. */
237 #define NUM_SSA_OPERANDS(STMT, FLAGS) num_ssa_operands (STMT, FLAGS)
240 /* Delink an immediate_uses node from its chain. */
241 static inline void
242 delink_imm_use (ssa_use_operand_t *linknode)
244 /* Return if this node is not in a list. */
245 if (linknode->prev == NULL)
246 return;
248 linknode->prev->next = linknode->next;
249 linknode->next->prev = linknode->prev;
250 linknode->prev = NULL;
251 linknode->next = NULL;
254 /* Link ssa_imm_use node LINKNODE into the chain for LIST. */
255 static inline void
256 link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
258 /* Link the new node at the head of the list. If we are in the process of
259 traversing the list, we won't visit any new nodes added to it. */
260 linknode->prev = list;
261 linknode->next = list->next;
262 list->next->prev = linknode;
263 list->next = linknode;
266 /* Link ssa_imm_use node LINKNODE into the chain for DEF. */
267 static inline void
268 link_imm_use (ssa_use_operand_t *linknode, tree def)
270 ssa_use_operand_t *root;
272 if (!def || TREE_CODE (def) != SSA_NAME)
273 linknode->prev = NULL;
274 else
276 root = &(SSA_NAME_IMM_USE_NODE (def));
277 if (linknode->use)
278 gcc_checking_assert (*(linknode->use) == def);
279 link_imm_use_to_list (linknode, root);
283 /* Set the value of a use pointed to by USE to VAL. */
284 static inline void
285 set_ssa_use_from_ptr (use_operand_p use, tree val)
287 delink_imm_use (use);
288 *(use->use) = val;
289 link_imm_use (use, val);
292 /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
293 in STMT. */
294 static inline void
295 link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple stmt)
297 if (stmt)
298 link_imm_use (linknode, def);
299 else
300 link_imm_use (linknode, NULL);
301 linknode->loc.stmt = stmt;
304 /* Relink a new node in place of an old node in the list. */
305 static inline void
306 relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
308 /* The node one had better be in the same list. */
309 gcc_checking_assert (*(old->use) == *(node->use));
310 node->prev = old->prev;
311 node->next = old->next;
312 if (old->prev)
314 old->prev->next = node;
315 old->next->prev = node;
316 /* Remove the old node from the list. */
317 old->prev = NULL;
321 /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
322 in STMT. */
323 static inline void
324 relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old,
325 gimple stmt)
327 if (stmt)
328 relink_imm_use (linknode, old);
329 else
330 link_imm_use (linknode, NULL);
331 linknode->loc.stmt = stmt;
335 /* Return true is IMM has reached the end of the immediate use list. */
336 static inline bool
337 end_readonly_imm_use_p (const imm_use_iterator *imm)
339 return (imm->imm_use == imm->end_p);
342 /* Initialize iterator IMM to process the list for VAR. */
343 static inline use_operand_p
344 first_readonly_imm_use (imm_use_iterator *imm, tree var)
346 imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
347 imm->imm_use = imm->end_p->next;
348 #ifdef ENABLE_CHECKING
349 imm->iter_node.next = imm->imm_use->next;
350 #endif
351 if (end_readonly_imm_use_p (imm))
352 return NULL_USE_OPERAND_P;
353 return imm->imm_use;
356 /* Bump IMM to the next use in the list. */
357 static inline use_operand_p
358 next_readonly_imm_use (imm_use_iterator *imm)
360 use_operand_p old = imm->imm_use;
362 #ifdef ENABLE_CHECKING
363 /* If this assertion fails, it indicates the 'next' pointer has changed
364 since the last bump. This indicates that the list is being modified
365 via stmt changes, or SET_USE, or somesuch thing, and you need to be
366 using the SAFE version of the iterator. */
367 gcc_assert (imm->iter_node.next == old->next);
368 imm->iter_node.next = old->next->next;
369 #endif
371 imm->imm_use = old->next;
372 if (end_readonly_imm_use_p (imm))
373 return NULL_USE_OPERAND_P;
374 return imm->imm_use;
378 /* Return true if VAR has no nondebug uses. */
379 static inline bool
380 has_zero_uses (const_tree var)
382 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
384 /* A single use_operand means there is no items in the list. */
385 if (ptr == ptr->next)
386 return true;
388 /* If there are debug stmts, we have to look at each use and see
389 whether there are any nondebug uses. */
390 if (!MAY_HAVE_DEBUG_STMTS)
391 return false;
393 return has_zero_uses_1 (ptr);
396 /* Return true if VAR has a single nondebug use. */
397 static inline bool
398 has_single_use (const_tree var)
400 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
402 /* If there aren't any uses whatsoever, we're done. */
403 if (ptr == ptr->next)
404 return false;
406 /* If there's a single use, check that it's not a debug stmt. */
407 if (ptr == ptr->next->next)
408 return !is_gimple_debug (USE_STMT (ptr->next));
410 /* If there are debug stmts, we have to look at each of them. */
411 if (!MAY_HAVE_DEBUG_STMTS)
412 return false;
414 return single_imm_use_1 (ptr, NULL, NULL);
418 /* If VAR has only a single immediate nondebug use, return true, and
419 set USE_P and STMT to the use pointer and stmt of occurrence. */
420 static inline bool
421 single_imm_use (const_tree var, use_operand_p *use_p, gimple *stmt)
423 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
425 /* If there aren't any uses whatsoever, we're done. */
426 if (ptr == ptr->next)
428 return_false:
429 *use_p = NULL_USE_OPERAND_P;
430 *stmt = NULL;
431 return false;
434 /* If there's a single use, check that it's not a debug stmt. */
435 if (ptr == ptr->next->next)
437 if (!is_gimple_debug (USE_STMT (ptr->next)))
439 *use_p = ptr->next;
440 *stmt = ptr->next->loc.stmt;
441 return true;
443 else
444 goto return_false;
447 /* If there are debug stmts, we have to look at each of them. */
448 if (!MAY_HAVE_DEBUG_STMTS)
449 goto return_false;
451 return single_imm_use_1 (ptr, use_p, stmt);
454 /* Return the number of nondebug immediate uses of VAR. */
455 static inline unsigned int
456 num_imm_uses (const_tree var)
458 const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var));
459 const ssa_use_operand_t *ptr;
460 unsigned int num = 0;
462 if (!MAY_HAVE_DEBUG_STMTS)
463 for (ptr = start->next; ptr != start; ptr = ptr->next)
464 num++;
465 else
466 for (ptr = start->next; ptr != start; ptr = ptr->next)
467 if (!is_gimple_debug (USE_STMT (ptr)))
468 num++;
470 return num;
473 /* ----------------------------------------------------------------------- */
475 /* The following set of routines are used to iterator over various type of
476 SSA operands. */
478 /* Return true if PTR is finished iterating. */
479 static inline bool
480 op_iter_done (const ssa_op_iter *ptr)
482 return ptr->done;
485 /* Get the next iterator use value for PTR. */
486 static inline use_operand_p
487 op_iter_next_use (ssa_op_iter *ptr)
489 use_operand_p use_p;
490 gcc_checking_assert (ptr->iter_type == ssa_op_iter_use);
491 if (ptr->uses)
493 use_p = USE_OP_PTR (ptr->uses);
494 ptr->uses = ptr->uses->next;
495 return use_p;
497 if (ptr->i < ptr->numops)
499 return PHI_ARG_DEF_PTR (ptr->stmt, (ptr->i)++);
501 ptr->done = true;
502 return NULL_USE_OPERAND_P;
505 /* Get the next iterator def value for PTR. */
506 static inline def_operand_p
507 op_iter_next_def (ssa_op_iter *ptr)
509 gcc_checking_assert (ptr->iter_type == ssa_op_iter_def);
510 if (ptr->flags & SSA_OP_VDEF)
512 tree *p;
513 ptr->flags &= ~SSA_OP_VDEF;
514 p = gimple_vdef_ptr (ptr->stmt);
515 if (p && *p)
516 return p;
518 if (ptr->flags & SSA_OP_DEF)
520 while (ptr->i < ptr->numops)
522 tree *val = gimple_op_ptr (ptr->stmt, ptr->i);
523 ptr->i++;
524 if (*val)
526 if (TREE_CODE (*val) == TREE_LIST)
527 val = &TREE_VALUE (*val);
528 if (TREE_CODE (*val) == SSA_NAME
529 || is_gimple_reg (*val))
530 return val;
533 ptr->flags &= ~SSA_OP_DEF;
536 ptr->done = true;
537 return NULL_DEF_OPERAND_P;
540 /* Get the next iterator tree value for PTR. */
541 static inline tree
542 op_iter_next_tree (ssa_op_iter *ptr)
544 tree val;
545 gcc_checking_assert (ptr->iter_type == ssa_op_iter_tree);
546 if (ptr->uses)
548 val = USE_OP (ptr->uses);
549 ptr->uses = ptr->uses->next;
550 return val;
552 if (ptr->flags & SSA_OP_VDEF)
554 ptr->flags &= ~SSA_OP_VDEF;
555 if ((val = gimple_vdef (ptr->stmt)))
556 return val;
558 if (ptr->flags & SSA_OP_DEF)
560 while (ptr->i < ptr->numops)
562 val = gimple_op (ptr->stmt, ptr->i);
563 ptr->i++;
564 if (val)
566 if (TREE_CODE (val) == TREE_LIST)
567 val = TREE_VALUE (val);
568 if (TREE_CODE (val) == SSA_NAME
569 || is_gimple_reg (val))
570 return val;
573 ptr->flags &= ~SSA_OP_DEF;
576 ptr->done = true;
577 return NULL_TREE;
581 /* This functions clears the iterator PTR, and marks it done. This is normally
582 used to prevent warnings in the compile about might be uninitialized
583 components. */
585 static inline void
586 clear_and_done_ssa_iter (ssa_op_iter *ptr)
588 ptr->i = 0;
589 ptr->numops = 0;
590 ptr->uses = NULL;
591 ptr->iter_type = ssa_op_iter_none;
592 ptr->stmt = NULL;
593 ptr->done = true;
594 ptr->flags = 0;
597 /* Initialize the iterator PTR to the virtual defs in STMT. */
598 static inline void
599 op_iter_init (ssa_op_iter *ptr, gimple stmt, int flags)
601 /* PHI nodes require a different iterator initialization path. We
602 do not support iterating over virtual defs or uses without
603 iterating over defs or uses at the same time. */
604 gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI
605 && (!(flags & SSA_OP_VDEF) || (flags & SSA_OP_DEF))
606 && (!(flags & SSA_OP_VUSE) || (flags & SSA_OP_USE)));
607 ptr->numops = 0;
608 if (flags & (SSA_OP_DEF | SSA_OP_VDEF))
610 switch (gimple_code (stmt))
612 case GIMPLE_ASSIGN:
613 case GIMPLE_CALL:
614 ptr->numops = 1;
615 break;
616 case GIMPLE_ASM:
617 ptr->numops = gimple_asm_noutputs (as_a <gasm *> (stmt));
618 break;
619 default:
620 ptr->numops = 0;
621 flags &= ~(SSA_OP_DEF | SSA_OP_VDEF);
622 break;
625 ptr->uses = (flags & (SSA_OP_USE|SSA_OP_VUSE)) ? gimple_use_ops (stmt) : NULL;
626 if (!(flags & SSA_OP_VUSE)
627 && ptr->uses
628 && gimple_vuse (stmt) != NULL_TREE)
629 ptr->uses = ptr->uses->next;
630 ptr->done = false;
631 ptr->i = 0;
633 ptr->stmt = stmt;
634 ptr->flags = flags;
637 /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
638 the first use. */
639 static inline use_operand_p
640 op_iter_init_use (ssa_op_iter *ptr, gimple stmt, int flags)
642 gcc_checking_assert ((flags & SSA_OP_ALL_DEFS) == 0
643 && (flags & SSA_OP_USE));
644 op_iter_init (ptr, stmt, flags);
645 ptr->iter_type = ssa_op_iter_use;
646 return op_iter_next_use (ptr);
649 /* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
650 the first def. */
651 static inline def_operand_p
652 op_iter_init_def (ssa_op_iter *ptr, gimple stmt, int flags)
654 gcc_checking_assert ((flags & SSA_OP_ALL_USES) == 0
655 && (flags & SSA_OP_DEF));
656 op_iter_init (ptr, stmt, flags);
657 ptr->iter_type = ssa_op_iter_def;
658 return op_iter_next_def (ptr);
661 /* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
662 the first operand as a tree. */
663 static inline tree
664 op_iter_init_tree (ssa_op_iter *ptr, gimple stmt, int flags)
666 op_iter_init (ptr, stmt, flags);
667 ptr->iter_type = ssa_op_iter_tree;
668 return op_iter_next_tree (ptr);
672 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
673 return NULL. */
674 static inline tree
675 single_ssa_tree_operand (gimple stmt, int flags)
677 tree var;
678 ssa_op_iter iter;
680 var = op_iter_init_tree (&iter, stmt, flags);
681 if (op_iter_done (&iter))
682 return NULL_TREE;
683 op_iter_next_tree (&iter);
684 if (op_iter_done (&iter))
685 return var;
686 return NULL_TREE;
690 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
691 return NULL. */
692 static inline use_operand_p
693 single_ssa_use_operand (gimple stmt, int flags)
695 use_operand_p var;
696 ssa_op_iter iter;
698 var = op_iter_init_use (&iter, stmt, flags);
699 if (op_iter_done (&iter))
700 return NULL_USE_OPERAND_P;
701 op_iter_next_use (&iter);
702 if (op_iter_done (&iter))
703 return var;
704 return NULL_USE_OPERAND_P;
709 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
710 return NULL. */
711 static inline def_operand_p
712 single_ssa_def_operand (gimple stmt, int flags)
714 def_operand_p var;
715 ssa_op_iter iter;
717 var = op_iter_init_def (&iter, stmt, flags);
718 if (op_iter_done (&iter))
719 return NULL_DEF_OPERAND_P;
720 op_iter_next_def (&iter);
721 if (op_iter_done (&iter))
722 return var;
723 return NULL_DEF_OPERAND_P;
727 /* Return true if there are zero operands in STMT matching the type
728 given in FLAGS. */
729 static inline bool
730 zero_ssa_operands (gimple stmt, int flags)
732 ssa_op_iter iter;
734 op_iter_init_tree (&iter, stmt, flags);
735 return op_iter_done (&iter);
739 /* Return the number of operands matching FLAGS in STMT. */
740 static inline int
741 num_ssa_operands (gimple stmt, int flags)
743 ssa_op_iter iter;
744 tree t;
745 int num = 0;
747 gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI);
748 FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
749 num++;
750 return num;
753 /* If there is a single DEF in the PHI node which matches FLAG, return it.
754 Otherwise return NULL_DEF_OPERAND_P. */
755 static inline tree
756 single_phi_def (gphi *stmt, int flags)
758 tree def = PHI_RESULT (stmt);
759 if ((flags & SSA_OP_DEF) && is_gimple_reg (def))
760 return def;
761 if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def))
762 return def;
763 return NULL_TREE;
766 /* Initialize the iterator PTR for uses matching FLAGS in PHI. FLAGS should
767 be either SSA_OP_USES or SSA_OP_VIRTUAL_USES. */
768 static inline use_operand_p
769 op_iter_init_phiuse (ssa_op_iter *ptr, gphi *phi, int flags)
771 tree phi_def = gimple_phi_result (phi);
772 int comp;
774 clear_and_done_ssa_iter (ptr);
775 ptr->done = false;
777 gcc_checking_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
779 comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
781 /* If the PHI node doesn't the operand type we care about, we're done. */
782 if ((flags & comp) == 0)
784 ptr->done = true;
785 return NULL_USE_OPERAND_P;
788 ptr->stmt = phi;
789 ptr->numops = gimple_phi_num_args (phi);
790 ptr->iter_type = ssa_op_iter_use;
791 ptr->flags = flags;
792 return op_iter_next_use (ptr);
796 /* Start an iterator for a PHI definition. */
798 static inline def_operand_p
799 op_iter_init_phidef (ssa_op_iter *ptr, gphi *phi, int flags)
801 tree phi_def = PHI_RESULT (phi);
802 int comp;
804 clear_and_done_ssa_iter (ptr);
805 ptr->done = false;
807 gcc_checking_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
809 comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
811 /* If the PHI node doesn't have the operand type we care about,
812 we're done. */
813 if ((flags & comp) == 0)
815 ptr->done = true;
816 return NULL_DEF_OPERAND_P;
819 ptr->iter_type = ssa_op_iter_def;
820 /* The first call to op_iter_next_def will terminate the iterator since
821 all the fields are NULL. Simply return the result here as the first and
822 therefore only result. */
823 return PHI_RESULT_PTR (phi);
826 /* Return true is IMM has reached the end of the immediate use stmt list. */
828 static inline bool
829 end_imm_use_stmt_p (const imm_use_iterator *imm)
831 return (imm->imm_use == imm->end_p);
834 /* Finished the traverse of an immediate use stmt list IMM by removing the
835 placeholder node from the list. */
837 static inline void
838 end_imm_use_stmt_traverse (imm_use_iterator *imm)
840 delink_imm_use (&(imm->iter_node));
843 /* Immediate use traversal of uses within a stmt require that all the
844 uses on a stmt be sequentially listed. This routine is used to build up
845 this sequential list by adding USE_P to the end of the current list
846 currently delimited by HEAD and LAST_P. The new LAST_P value is
847 returned. */
849 static inline use_operand_p
850 move_use_after_head (use_operand_p use_p, use_operand_p head,
851 use_operand_p last_p)
853 gcc_checking_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head));
854 /* Skip head when we find it. */
855 if (use_p != head)
857 /* If use_p is already linked in after last_p, continue. */
858 if (last_p->next == use_p)
859 last_p = use_p;
860 else
862 /* Delink from current location, and link in at last_p. */
863 delink_imm_use (use_p);
864 link_imm_use_to_list (use_p, last_p);
865 last_p = use_p;
868 return last_p;
872 /* This routine will relink all uses with the same stmt as HEAD into the list
873 immediately following HEAD for iterator IMM. */
875 static inline void
876 link_use_stmts_after (use_operand_p head, imm_use_iterator *imm)
878 use_operand_p use_p;
879 use_operand_p last_p = head;
880 gimple head_stmt = USE_STMT (head);
881 tree use = USE_FROM_PTR (head);
882 ssa_op_iter op_iter;
883 int flag;
885 /* Only look at virtual or real uses, depending on the type of HEAD. */
886 flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
888 if (gphi *phi = dyn_cast <gphi *> (head_stmt))
890 FOR_EACH_PHI_ARG (use_p, phi, op_iter, flag)
891 if (USE_FROM_PTR (use_p) == use)
892 last_p = move_use_after_head (use_p, head, last_p);
894 else
896 if (flag == SSA_OP_USE)
898 FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag)
899 if (USE_FROM_PTR (use_p) == use)
900 last_p = move_use_after_head (use_p, head, last_p);
902 else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P)
904 if (USE_FROM_PTR (use_p) == use)
905 last_p = move_use_after_head (use_p, head, last_p);
908 /* Link iter node in after last_p. */
909 if (imm->iter_node.prev != NULL)
910 delink_imm_use (&imm->iter_node);
911 link_imm_use_to_list (&(imm->iter_node), last_p);
914 /* Initialize IMM to traverse over uses of VAR. Return the first statement. */
915 static inline gimple
916 first_imm_use_stmt (imm_use_iterator *imm, tree var)
918 imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
919 imm->imm_use = imm->end_p->next;
920 imm->next_imm_name = NULL_USE_OPERAND_P;
922 /* iter_node is used as a marker within the immediate use list to indicate
923 where the end of the current stmt's uses are. Initialize it to NULL
924 stmt and use, which indicates a marker node. */
925 imm->iter_node.prev = NULL_USE_OPERAND_P;
926 imm->iter_node.next = NULL_USE_OPERAND_P;
927 imm->iter_node.loc.stmt = NULL;
928 imm->iter_node.use = NULL;
930 if (end_imm_use_stmt_p (imm))
931 return NULL;
933 link_use_stmts_after (imm->imm_use, imm);
935 return USE_STMT (imm->imm_use);
938 /* Bump IMM to the next stmt which has a use of var. */
940 static inline gimple
941 next_imm_use_stmt (imm_use_iterator *imm)
943 imm->imm_use = imm->iter_node.next;
944 if (end_imm_use_stmt_p (imm))
946 if (imm->iter_node.prev != NULL)
947 delink_imm_use (&imm->iter_node);
948 return NULL;
951 link_use_stmts_after (imm->imm_use, imm);
952 return USE_STMT (imm->imm_use);
955 /* This routine will return the first use on the stmt IMM currently refers
956 to. */
958 static inline use_operand_p
959 first_imm_use_on_stmt (imm_use_iterator *imm)
961 imm->next_imm_name = imm->imm_use->next;
962 return imm->imm_use;
965 /* Return TRUE if the last use on the stmt IMM refers to has been visited. */
967 static inline bool
968 end_imm_use_on_stmt_p (const imm_use_iterator *imm)
970 return (imm->imm_use == &(imm->iter_node));
973 /* Bump to the next use on the stmt IMM refers to, return NULL if done. */
975 static inline use_operand_p
976 next_imm_use_on_stmt (imm_use_iterator *imm)
978 imm->imm_use = imm->next_imm_name;
979 if (end_imm_use_on_stmt_p (imm))
980 return NULL_USE_OPERAND_P;
981 else
983 imm->next_imm_name = imm->imm_use->next;
984 return imm->imm_use;
988 /* Delink all immediate_use information for STMT. */
989 static inline void
990 delink_stmt_imm_use (gimple stmt)
992 ssa_op_iter iter;
993 use_operand_p use_p;
995 if (ssa_operands_active (cfun))
996 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_ALL_USES)
997 delink_imm_use (use_p);
1000 #endif /* GCC_TREE_SSA_ITERATORS_H */