1 /* Header file for SSA iterators.
2 Copyright (C) 2013-2015 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
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
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
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
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) \
92 end_imm_use_stmt_traverse (&(ITER)); \
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);
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,
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. */
136 enum ssa_op_iter_type iter_type
;
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), \
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), \
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. */
242 delink_imm_use (ssa_use_operand_t
*linknode
)
244 /* Return if this node is not in a list. */
245 if (linknode
->prev
== NULL
)
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. */
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. */
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
;
276 root
= &(SSA_NAME_IMM_USE_NODE (def
));
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. */
285 set_ssa_use_from_ptr (use_operand_p use
, tree val
)
287 delink_imm_use (use
);
289 link_imm_use (use
, val
);
292 /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
295 link_imm_use_stmt (ssa_use_operand_t
*linknode
, tree def
, gimple stmt
)
298 link_imm_use (linknode
, def
);
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. */
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
;
314 old
->prev
->next
= node
;
315 old
->next
->prev
= node
;
316 /* Remove the old node from the list. */
321 /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
324 relink_imm_use_stmt (ssa_use_operand_t
*linknode
, ssa_use_operand_t
*old
,
328 relink_imm_use (linknode
, old
);
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. */
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
;
351 if (end_readonly_imm_use_p (imm
))
352 return NULL_USE_OPERAND_P
;
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
;
371 imm
->imm_use
= old
->next
;
372 if (end_readonly_imm_use_p (imm
))
373 return NULL_USE_OPERAND_P
;
378 /* Return true if VAR has no nondebug uses. */
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
)
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
)
393 return has_zero_uses_1 (ptr
);
396 /* Return true if VAR has a single nondebug use. */
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
)
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
)
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. */
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
)
429 *use_p
= NULL_USE_OPERAND_P
;
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
)))
440 *stmt
= ptr
->next
->loc
.stmt
;
447 /* If there are debug stmts, we have to look at each of them. */
448 if (!MAY_HAVE_DEBUG_STMTS
)
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
)
466 for (ptr
= start
->next
; ptr
!= start
; ptr
= ptr
->next
)
467 if (!is_gimple_debug (USE_STMT (ptr
)))
473 /* ----------------------------------------------------------------------- */
475 /* The following set of routines are used to iterator over various type of
478 /* Return true if PTR is finished iterating. */
480 op_iter_done (const ssa_op_iter
*ptr
)
485 /* Get the next iterator use value for PTR. */
486 static inline use_operand_p
487 op_iter_next_use (ssa_op_iter
*ptr
)
490 gcc_checking_assert (ptr
->iter_type
== ssa_op_iter_use
);
493 use_p
= USE_OP_PTR (ptr
->uses
);
494 ptr
->uses
= ptr
->uses
->next
;
497 if (ptr
->i
< ptr
->numops
)
499 return PHI_ARG_DEF_PTR (ptr
->stmt
, (ptr
->i
)++);
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
)
513 ptr
->flags
&= ~SSA_OP_VDEF
;
514 p
= gimple_vdef_ptr (ptr
->stmt
);
518 if (ptr
->flags
& SSA_OP_DEF
)
520 while (ptr
->i
< ptr
->numops
)
522 tree
*val
= gimple_op_ptr (ptr
->stmt
, ptr
->i
);
526 if (TREE_CODE (*val
) == TREE_LIST
)
527 val
= &TREE_VALUE (*val
);
528 if (TREE_CODE (*val
) == SSA_NAME
529 || is_gimple_reg (*val
))
533 ptr
->flags
&= ~SSA_OP_DEF
;
537 return NULL_DEF_OPERAND_P
;
540 /* Get the next iterator tree value for PTR. */
542 op_iter_next_tree (ssa_op_iter
*ptr
)
545 gcc_checking_assert (ptr
->iter_type
== ssa_op_iter_tree
);
548 val
= USE_OP (ptr
->uses
);
549 ptr
->uses
= ptr
->uses
->next
;
552 if (ptr
->flags
& SSA_OP_VDEF
)
554 ptr
->flags
&= ~SSA_OP_VDEF
;
555 if ((val
= gimple_vdef (ptr
->stmt
)))
558 if (ptr
->flags
& SSA_OP_DEF
)
560 while (ptr
->i
< ptr
->numops
)
562 val
= gimple_op (ptr
->stmt
, ptr
->i
);
566 if (TREE_CODE (val
) == TREE_LIST
)
567 val
= TREE_VALUE (val
);
568 if (TREE_CODE (val
) == SSA_NAME
569 || is_gimple_reg (val
))
573 ptr
->flags
&= ~SSA_OP_DEF
;
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
586 clear_and_done_ssa_iter (ssa_op_iter
*ptr
)
591 ptr
->iter_type
= ssa_op_iter_none
;
597 /* Initialize the iterator PTR to the virtual defs in STMT. */
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
)));
608 if (flags
& (SSA_OP_DEF
| SSA_OP_VDEF
))
610 switch (gimple_code (stmt
))
617 ptr
->numops
= gimple_asm_noutputs (as_a
<gasm
*> (stmt
));
621 flags
&= ~(SSA_OP_DEF
| SSA_OP_VDEF
);
625 ptr
->uses
= (flags
& (SSA_OP_USE
|SSA_OP_VUSE
)) ? gimple_use_ops (stmt
) : NULL
;
626 if (!(flags
& SSA_OP_VUSE
)
628 && gimple_vuse (stmt
) != NULL_TREE
)
629 ptr
->uses
= ptr
->uses
->next
;
637 /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
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
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. */
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
675 single_ssa_tree_operand (gimple stmt
, int flags
)
680 var
= op_iter_init_tree (&iter
, stmt
, flags
);
681 if (op_iter_done (&iter
))
683 op_iter_next_tree (&iter
);
684 if (op_iter_done (&iter
))
690 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
692 static inline use_operand_p
693 single_ssa_use_operand (gimple stmt
, int flags
)
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
))
704 return NULL_USE_OPERAND_P
;
709 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
711 static inline def_operand_p
712 single_ssa_def_operand (gimple stmt
, int flags
)
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
))
723 return NULL_DEF_OPERAND_P
;
727 /* Return true if there are zero operands in STMT matching the type
730 zero_ssa_operands (gimple stmt
, int flags
)
734 op_iter_init_tree (&iter
, stmt
, flags
);
735 return op_iter_done (&iter
);
739 /* Return the number of operands matching FLAGS in STMT. */
741 num_ssa_operands (gimple stmt
, int flags
)
747 gcc_checking_assert (gimple_code (stmt
) != GIMPLE_PHI
);
748 FOR_EACH_SSA_TREE_OPERAND (t
, stmt
, iter
, flags
)
753 /* If there is a single DEF in the PHI node which matches FLAG, return it.
754 Otherwise return NULL_DEF_OPERAND_P. */
756 single_phi_def (gphi
*stmt
, int flags
)
758 tree def
= PHI_RESULT (stmt
);
759 if ((flags
& SSA_OP_DEF
) && is_gimple_reg (def
))
761 if ((flags
& SSA_OP_VIRTUAL_DEFS
) && !is_gimple_reg (def
))
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
);
774 clear_and_done_ssa_iter (ptr
);
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)
785 return NULL_USE_OPERAND_P
;
789 ptr
->numops
= gimple_phi_num_args (phi
);
790 ptr
->iter_type
= ssa_op_iter_use
;
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
);
804 clear_and_done_ssa_iter (ptr
);
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,
813 if ((flags
& comp
) == 0)
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. */
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. */
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
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. */
857 /* If use_p is already linked in after last_p, continue. */
858 if (last_p
->next
== use_p
)
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
);
872 /* This routine will relink all uses with the same stmt as HEAD into the list
873 immediately following HEAD for iterator IMM. */
876 link_use_stmts_after (use_operand_p head
, imm_use_iterator
*imm
)
879 use_operand_p last_p
= head
;
880 gimple head_stmt
= USE_STMT (head
);
881 tree use
= USE_FROM_PTR (head
);
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
);
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. */
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
))
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. */
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
);
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
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
;
965 /* Return TRUE if the last use on the stmt IMM refers to has been visited. */
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
;
983 imm
->next_imm_name
= imm
->imm_use
->next
;
988 /* Delink all immediate_use information for STMT. */
990 delink_stmt_imm_use (gimple stmt
)
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 */