1/* Header file for SSA iterators.
2   Copyright (C) 2013-2015 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
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.
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.  */
58struct imm_use_iterator
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;
69};
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
101     FOR_EACH_IMM_USE_STMT (stmt, iter, ssavar)
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
117extern bool has_zero_uses_1 (const ssa_use_operand_t *head);
118extern bool single_imm_use_1 (const ssa_use_operand_t *head,
119			      use_operand_p *use_p, gimple *stmt);
120
121
122enum 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
127};
128
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.  */
133
134struct ssa_op_iter
135{
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;
143};
144
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)
159
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))))
167
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)))
175
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)))
183
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)))
191
192
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)))
203
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)))
214
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)
220
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)
226
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)
232
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)
235
236/* This macro counts the number of operands in STMT matching FLAGS.  */
237#define NUM_SSA_OPERANDS(STMT, FLAGS)	num_ssa_operands (STMT, FLAGS)
238
239
240/* Delink an immediate_uses node from its chain.  */
241static inline void
242delink_imm_use (ssa_use_operand_t *linknode)
243{
244  /* Return if this node is not in a list.  */
245  if (linknode->prev == NULL)
246    return;
247
248  linknode->prev->next = linknode->next;
249  linknode->next->prev = linknode->prev;
250  linknode->prev = NULL;
251  linknode->next = NULL;
252}
253
254/* Link ssa_imm_use node LINKNODE into the chain for LIST.  */
255static inline void
256link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
257{
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;
264}
265
266/* Link ssa_imm_use node LINKNODE into the chain for DEF.  */
267static inline void
268link_imm_use (ssa_use_operand_t *linknode, tree def)
269{
270  ssa_use_operand_t *root;
271
272  if (!def || TREE_CODE (def) != SSA_NAME)
273    linknode->prev = NULL;
274  else
275    {
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);
280    }
281}
282
283/* Set the value of a use pointed to by USE to VAL.  */
284static inline void
285set_ssa_use_from_ptr (use_operand_p use, tree val)
286{
287  delink_imm_use (use);
288  *(use->use) = val;
289  link_imm_use (use, val);
290}
291
292/* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
293   in STMT.  */
294static inline void
295link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple stmt)
296{
297  if (stmt)
298    link_imm_use (linknode, def);
299  else
300    link_imm_use (linknode, NULL);
301  linknode->loc.stmt = stmt;
302}
303
304/* Relink a new node in place of an old node in the list.  */
305static inline void
306relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
307{
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)
313    {
314      old->prev->next = node;
315      old->next->prev = node;
316      /* Remove the old node from the list.  */
317      old->prev = NULL;
318    }
319}
320
321/* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
322   in STMT.  */
323static inline void
324relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old,
325		     gimple stmt)
326{
327  if (stmt)
328    relink_imm_use (linknode, old);
329  else
330    link_imm_use (linknode, NULL);
331  linknode->loc.stmt = stmt;
332}
333
334
335/* Return true is IMM has reached the end of the immediate use list.  */
336static inline bool
337end_readonly_imm_use_p (const imm_use_iterator *imm)
338{
339  return (imm->imm_use == imm->end_p);
340}
341
342/* Initialize iterator IMM to process the list for VAR.  */
343static inline use_operand_p
344first_readonly_imm_use (imm_use_iterator *imm, tree var)
345{
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;
354}
355
356/* Bump IMM to the next use in the list.  */
357static inline use_operand_p
358next_readonly_imm_use (imm_use_iterator *imm)
359{
360  use_operand_p old = imm->imm_use;
361
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
370
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;
375}
376
377
378/* Return true if VAR has no nondebug uses.  */
379static inline bool
380has_zero_uses (const_tree var)
381{
382  const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
383
384  /* A single use_operand means there is no items in the list.  */
385  if (ptr == ptr->next)
386    return true;
387
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;
392
393  return has_zero_uses_1 (ptr);
394}
395
396/* Return true if VAR has a single nondebug use.  */
397static inline bool
398has_single_use (const_tree var)
399{
400  const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
401
402  /* If there aren't any uses whatsoever, we're done.  */
403  if (ptr == ptr->next)
404    return false;
405
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));
409
410  /* If there are debug stmts, we have to look at each of them.  */
411  if (!MAY_HAVE_DEBUG_STMTS)
412    return false;
413
414  return single_imm_use_1 (ptr, NULL, NULL);
415}
416
417
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.  */
420static inline bool
421single_imm_use (const_tree var, use_operand_p *use_p, gimple *stmt)
422{
423  const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
424
425  /* If there aren't any uses whatsoever, we're done.  */
426  if (ptr == ptr->next)
427    {
428    return_false:
429      *use_p = NULL_USE_OPERAND_P;
430      *stmt = NULL;
431      return false;
432    }
433
434  /* If there's a single use, check that it's not a debug stmt.  */
435  if (ptr == ptr->next->next)
436    {
437      if (!is_gimple_debug (USE_STMT (ptr->next)))
438	{
439	  *use_p = ptr->next;
440	  *stmt = ptr->next->loc.stmt;
441	  return true;
442	}
443      else
444	goto return_false;
445    }
446
447  /* If there are debug stmts, we have to look at each of them.  */
448  if (!MAY_HAVE_DEBUG_STMTS)
449    goto return_false;
450
451  return single_imm_use_1 (ptr, use_p, stmt);
452}
453
454/* Return the number of nondebug immediate uses of VAR.  */
455static inline unsigned int
456num_imm_uses (const_tree var)
457{
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;
461
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++;
469
470  return num;
471}
472
473/*  -----------------------------------------------------------------------  */
474
475/* The following set of routines are used to iterator over various type of
476   SSA operands.  */
477
478/* Return true if PTR is finished iterating.  */
479static inline bool
480op_iter_done (const ssa_op_iter *ptr)
481{
482  return ptr->done;
483}
484
485/* Get the next iterator use value for PTR.  */
486static inline use_operand_p
487op_iter_next_use (ssa_op_iter *ptr)
488{
489  use_operand_p use_p;
490  gcc_checking_assert (ptr->iter_type == ssa_op_iter_use);
491  if (ptr->uses)
492    {
493      use_p = USE_OP_PTR (ptr->uses);
494      ptr->uses = ptr->uses->next;
495      return use_p;
496    }
497  if (ptr->i < ptr->numops)
498    {
499      return PHI_ARG_DEF_PTR (ptr->stmt, (ptr->i)++);
500    }
501  ptr->done = true;
502  return NULL_USE_OPERAND_P;
503}
504
505/* Get the next iterator def value for PTR.  */
506static inline def_operand_p
507op_iter_next_def (ssa_op_iter *ptr)
508{
509  gcc_checking_assert (ptr->iter_type == ssa_op_iter_def);
510  if (ptr->flags & SSA_OP_VDEF)
511    {
512      tree *p;
513      ptr->flags &= ~SSA_OP_VDEF;
514      p = gimple_vdef_ptr (ptr->stmt);
515      if (p && *p)
516	return p;
517    }
518  if (ptr->flags & SSA_OP_DEF)
519    {
520      while (ptr->i < ptr->numops)
521	{
522	  tree *val = gimple_op_ptr (ptr->stmt, ptr->i);
523	  ptr->i++;
524	  if (*val)
525	    {
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;
531	    }
532	}
533      ptr->flags &= ~SSA_OP_DEF;
534    }
535
536  ptr->done = true;
537  return NULL_DEF_OPERAND_P;
538}
539
540/* Get the next iterator tree value for PTR.  */
541static inline tree
542op_iter_next_tree (ssa_op_iter *ptr)
543{
544  tree val;
545  gcc_checking_assert (ptr->iter_type == ssa_op_iter_tree);
546  if (ptr->uses)
547    {
548      val = USE_OP (ptr->uses);
549      ptr->uses = ptr->uses->next;
550      return val;
551    }
552  if (ptr->flags & SSA_OP_VDEF)
553    {
554      ptr->flags &= ~SSA_OP_VDEF;
555      if ((val = gimple_vdef (ptr->stmt)))
556	return val;
557    }
558  if (ptr->flags & SSA_OP_DEF)
559    {
560      while (ptr->i < ptr->numops)
561	{
562	  val = gimple_op (ptr->stmt, ptr->i);
563	  ptr->i++;
564	  if (val)
565	    {
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;
571	    }
572	}
573      ptr->flags &= ~SSA_OP_DEF;
574    }
575
576  ptr->done = true;
577  return NULL_TREE;
578}
579
580
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.  */
584
585static inline void
586clear_and_done_ssa_iter (ssa_op_iter *ptr)
587{
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;
595}
596
597/* Initialize the iterator PTR to the virtual defs in STMT.  */
598static inline void
599op_iter_init (ssa_op_iter *ptr, gimple stmt, int flags)
600{
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))
609    {
610      switch (gimple_code (stmt))
611	{
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;
623	}
624    }
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;
632
633  ptr->stmt = stmt;
634  ptr->flags = flags;
635}
636
637/* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
638   the first use.  */
639static inline use_operand_p
640op_iter_init_use (ssa_op_iter *ptr, gimple stmt, int flags)
641{
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);
647}
648
649/* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
650   the first def.  */
651static inline def_operand_p
652op_iter_init_def (ssa_op_iter *ptr, gimple stmt, int flags)
653{
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);
659}
660
661/* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
662   the first operand as a tree.  */
663static inline tree
664op_iter_init_tree (ssa_op_iter *ptr, gimple stmt, int flags)
665{
666  op_iter_init (ptr, stmt, flags);
667  ptr->iter_type = ssa_op_iter_tree;
668  return op_iter_next_tree (ptr);
669}
670
671
672/* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
673   return NULL.  */
674static inline tree
675single_ssa_tree_operand (gimple stmt, int flags)
676{
677  tree var;
678  ssa_op_iter iter;
679
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;
687}
688
689
690/* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
691   return NULL.  */
692static inline use_operand_p
693single_ssa_use_operand (gimple stmt, int flags)
694{
695  use_operand_p var;
696  ssa_op_iter iter;
697
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;
705}
706
707
708
709/* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
710   return NULL.  */
711static inline def_operand_p
712single_ssa_def_operand (gimple stmt, int flags)
713{
714  def_operand_p var;
715  ssa_op_iter iter;
716
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;
724}
725
726
727/* Return true if there are zero operands in STMT matching the type
728   given in FLAGS.  */
729static inline bool
730zero_ssa_operands (gimple stmt, int flags)
731{
732  ssa_op_iter iter;
733
734  op_iter_init_tree (&iter, stmt, flags);
735  return op_iter_done (&iter);
736}
737
738
739/* Return the number of operands matching FLAGS in STMT.  */
740static inline int
741num_ssa_operands (gimple stmt, int flags)
742{
743  ssa_op_iter iter;
744  tree t;
745  int num = 0;
746
747  gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI);
748  FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
749    num++;
750  return num;
751}
752
753/* If there is a single DEF in the PHI node which matches FLAG, return it.
754   Otherwise return NULL_DEF_OPERAND_P.  */
755static inline tree
756single_phi_def (gphi *stmt, int flags)
757{
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;
764}
765
766/* Initialize the iterator PTR for uses matching FLAGS in PHI.  FLAGS should
767   be either SSA_OP_USES or SSA_OP_VIRTUAL_USES.  */
768static inline use_operand_p
769op_iter_init_phiuse (ssa_op_iter *ptr, gphi *phi, int flags)
770{
771  tree phi_def = gimple_phi_result (phi);
772  int comp;
773
774  clear_and_done_ssa_iter (ptr);
775  ptr->done = false;
776
777  gcc_checking_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
778
779  comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
780
781  /* If the PHI node doesn't the operand type we care about, we're done.  */
782  if ((flags & comp) == 0)
783    {
784      ptr->done = true;
785      return NULL_USE_OPERAND_P;
786    }
787
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);
793}
794
795
796/* Start an iterator for a PHI definition.  */
797
798static inline def_operand_p
799op_iter_init_phidef (ssa_op_iter *ptr, gphi *phi, int flags)
800{
801  tree phi_def = PHI_RESULT (phi);
802  int comp;
803
804  clear_and_done_ssa_iter (ptr);
805  ptr->done = false;
806
807  gcc_checking_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
808
809  comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
810
811  /* If the PHI node doesn't have the operand type we care about,
812     we're done.  */
813  if ((flags & comp) == 0)
814    {
815      ptr->done = true;
816      return NULL_DEF_OPERAND_P;
817    }
818
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);
824}
825
826/* Return true is IMM has reached the end of the immediate use stmt list.  */
827
828static inline bool
829end_imm_use_stmt_p (const imm_use_iterator *imm)
830{
831  return (imm->imm_use == imm->end_p);
832}
833
834/* Finished the traverse of an immediate use stmt list IMM by removing the
835   placeholder node from the list.  */
836
837static inline void
838end_imm_use_stmt_traverse (imm_use_iterator *imm)
839{
840  delink_imm_use (&(imm->iter_node));
841}
842
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.  */
848
849static inline use_operand_p
850move_use_after_head (use_operand_p use_p, use_operand_p head,
851		      use_operand_p last_p)
852{
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)
856    {
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
861	{
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;
866	}
867    }
868  return last_p;
869}
870
871
872/* This routine will relink all uses with the same stmt as HEAD into the list
873   immediately following HEAD for iterator IMM.  */
874
875static inline void
876link_use_stmts_after (use_operand_p head, imm_use_iterator *imm)
877{
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;
884
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);
887
888  if (gphi *phi = dyn_cast <gphi *> (head_stmt))
889    {
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);
893    }
894  else
895    {
896      if (flag == SSA_OP_USE)
897	{
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);
901	}
902      else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P)
903	{
904	  if (USE_FROM_PTR (use_p) == use)
905	    last_p = move_use_after_head (use_p, head, last_p);
906	}
907    }
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);
912}
913
914/* Initialize IMM to traverse over uses of VAR.  Return the first statement.  */
915static inline gimple
916first_imm_use_stmt (imm_use_iterator *imm, tree var)
917{
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;
921
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;
929
930  if (end_imm_use_stmt_p (imm))
931    return NULL;
932
933  link_use_stmts_after (imm->imm_use, imm);
934
935  return USE_STMT (imm->imm_use);
936}
937
938/* Bump IMM to the next stmt which has a use of var.  */
939
940static inline gimple
941next_imm_use_stmt (imm_use_iterator *imm)
942{
943  imm->imm_use = imm->iter_node.next;
944  if (end_imm_use_stmt_p (imm))
945    {
946      if (imm->iter_node.prev != NULL)
947	delink_imm_use (&imm->iter_node);
948      return NULL;
949    }
950
951  link_use_stmts_after (imm->imm_use, imm);
952  return USE_STMT (imm->imm_use);
953}
954
955/* This routine will return the first use on the stmt IMM currently refers
956   to.  */
957
958static inline use_operand_p
959first_imm_use_on_stmt (imm_use_iterator *imm)
960{
961  imm->next_imm_name = imm->imm_use->next;
962  return imm->imm_use;
963}
964
965/*  Return TRUE if the last use on the stmt IMM refers to has been visited.  */
966
967static inline bool
968end_imm_use_on_stmt_p (const imm_use_iterator *imm)
969{
970  return (imm->imm_use == &(imm->iter_node));
971}
972
973/* Bump to the next use on the stmt IMM refers to, return NULL if done.  */
974
975static inline use_operand_p
976next_imm_use_on_stmt (imm_use_iterator *imm)
977{
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
982    {
983      imm->next_imm_name = imm->imm_use->next;
984      return imm->imm_use;
985    }
986}
987
988/* Delink all immediate_use information for STMT.  */
989static inline void
990delink_stmt_imm_use (gimple stmt)
991{
992   ssa_op_iter iter;
993   use_operand_p use_p;
994
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);
998}
999
1000#endif /* GCC_TREE_SSA_ITERATORS_H */
1001