1 /* Header file for SSA iterators.
2    Copyright (C) 2013-2014 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef GCC_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.  */
58 struct 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 
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);
120 
121 
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
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 
134 struct 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), STMT, FLAGS)	\
198 		   : op_iter_init_use (&(ITER), STMT, FLAGS));	\
199        !op_iter_done (&(ITER));					\
200        (USEVAR) = op_iter_next_use (&(ITER)))
201 
202 /* This macro will execute a loop over a stmt, regardless of whether it is
203    a real stmt or a PHI node, looking at the DEF nodes matching FLAGS.  */
204 #define FOR_EACH_PHI_OR_STMT_DEF(DEFVAR, STMT, ITER, FLAGS)	\
205   for ((DEFVAR) = (gimple_code (STMT) == GIMPLE_PHI 		\
206 		   ? op_iter_init_phidef (&(ITER), STMT, FLAGS)	\
207 		   : op_iter_init_def (&(ITER), STMT, FLAGS));	\
208        !op_iter_done (&(ITER));					\
209        (DEFVAR) = op_iter_next_def (&(ITER)))
210 
211 /* This macro returns an operand in STMT as a tree if it is the ONLY
212    operand matching FLAGS.  If there are 0 or more than 1 operand matching
213    FLAGS, then NULL_TREE is returned.  */
214 #define SINGLE_SSA_TREE_OPERAND(STMT, FLAGS)			\
215   single_ssa_tree_operand (STMT, FLAGS)
216 
217 /* This macro returns an operand in STMT as a use_operand_p if it is the ONLY
218    operand matching FLAGS.  If there are 0 or more than 1 operand matching
219    FLAGS, then NULL_USE_OPERAND_P is returned.  */
220 #define SINGLE_SSA_USE_OPERAND(STMT, FLAGS)			\
221   single_ssa_use_operand (STMT, FLAGS)
222 
223 /* This macro returns an operand in STMT as a def_operand_p if it is the ONLY
224    operand matching FLAGS.  If there are 0 or more than 1 operand matching
225    FLAGS, then NULL_DEF_OPERAND_P is returned.  */
226 #define SINGLE_SSA_DEF_OPERAND(STMT, FLAGS)			\
227   single_ssa_def_operand (STMT, FLAGS)
228 
229 /* This macro returns TRUE if there are no operands matching FLAGS in STMT.  */
230 #define ZERO_SSA_OPERANDS(STMT, FLAGS) 	zero_ssa_operands (STMT, FLAGS)
231 
232 /* This macro counts the number of operands in STMT matching FLAGS.  */
233 #define NUM_SSA_OPERANDS(STMT, FLAGS)	num_ssa_operands (STMT, FLAGS)
234 
235 
236 /* Delink an immediate_uses node from its chain.  */
237 static inline void
delink_imm_use(ssa_use_operand_t * linknode)238 delink_imm_use (ssa_use_operand_t *linknode)
239 {
240   /* Return if this node is not in a list.  */
241   if (linknode->prev == NULL)
242     return;
243 
244   linknode->prev->next = linknode->next;
245   linknode->next->prev = linknode->prev;
246   linknode->prev = NULL;
247   linknode->next = NULL;
248 }
249 
250 /* Link ssa_imm_use node LINKNODE into the chain for LIST.  */
251 static inline void
link_imm_use_to_list(ssa_use_operand_t * linknode,ssa_use_operand_t * list)252 link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
253 {
254   /* Link the new node at the head of the list.  If we are in the process of
255      traversing the list, we won't visit any new nodes added to it.  */
256   linknode->prev = list;
257   linknode->next = list->next;
258   list->next->prev = linknode;
259   list->next = linknode;
260 }
261 
262 /* Link ssa_imm_use node LINKNODE into the chain for DEF.  */
263 static inline void
link_imm_use(ssa_use_operand_t * linknode,tree def)264 link_imm_use (ssa_use_operand_t *linknode, tree def)
265 {
266   ssa_use_operand_t *root;
267 
268   if (!def || TREE_CODE (def) != SSA_NAME)
269     linknode->prev = NULL;
270   else
271     {
272       root = &(SSA_NAME_IMM_USE_NODE (def));
273       if (linknode->use)
274         gcc_checking_assert (*(linknode->use) == def);
275       link_imm_use_to_list (linknode, root);
276     }
277 }
278 
279 /* Set the value of a use pointed to by USE to VAL.  */
280 static inline void
set_ssa_use_from_ptr(use_operand_p use,tree val)281 set_ssa_use_from_ptr (use_operand_p use, tree val)
282 {
283   delink_imm_use (use);
284   *(use->use) = val;
285   link_imm_use (use, val);
286 }
287 
288 /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
289    in STMT.  */
290 static inline void
link_imm_use_stmt(ssa_use_operand_t * linknode,tree def,gimple stmt)291 link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple stmt)
292 {
293   if (stmt)
294     link_imm_use (linknode, def);
295   else
296     link_imm_use (linknode, NULL);
297   linknode->loc.stmt = stmt;
298 }
299 
300 /* Relink a new node in place of an old node in the list.  */
301 static inline void
relink_imm_use(ssa_use_operand_t * node,ssa_use_operand_t * old)302 relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
303 {
304   /* The node one had better be in the same list.  */
305   gcc_checking_assert (*(old->use) == *(node->use));
306   node->prev = old->prev;
307   node->next = old->next;
308   if (old->prev)
309     {
310       old->prev->next = node;
311       old->next->prev = node;
312       /* Remove the old node from the list.  */
313       old->prev = NULL;
314     }
315 }
316 
317 /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
318    in STMT.  */
319 static inline void
relink_imm_use_stmt(ssa_use_operand_t * linknode,ssa_use_operand_t * old,gimple stmt)320 relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old,
321 		     gimple stmt)
322 {
323   if (stmt)
324     relink_imm_use (linknode, old);
325   else
326     link_imm_use (linknode, NULL);
327   linknode->loc.stmt = stmt;
328 }
329 
330 
331 /* Return true is IMM has reached the end of the immediate use list.  */
332 static inline bool
end_readonly_imm_use_p(const imm_use_iterator * imm)333 end_readonly_imm_use_p (const imm_use_iterator *imm)
334 {
335   return (imm->imm_use == imm->end_p);
336 }
337 
338 /* Initialize iterator IMM to process the list for VAR.  */
339 static inline use_operand_p
first_readonly_imm_use(imm_use_iterator * imm,tree var)340 first_readonly_imm_use (imm_use_iterator *imm, tree var)
341 {
342   imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
343   imm->imm_use = imm->end_p->next;
344 #ifdef ENABLE_CHECKING
345   imm->iter_node.next = imm->imm_use->next;
346 #endif
347   if (end_readonly_imm_use_p (imm))
348     return NULL_USE_OPERAND_P;
349   return imm->imm_use;
350 }
351 
352 /* Bump IMM to the next use in the list.  */
353 static inline use_operand_p
next_readonly_imm_use(imm_use_iterator * imm)354 next_readonly_imm_use (imm_use_iterator *imm)
355 {
356   use_operand_p old = imm->imm_use;
357 
358 #ifdef ENABLE_CHECKING
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.  */
363   gcc_assert (imm->iter_node.next == old->next);
364   imm->iter_node.next = old->next->next;
365 #endif
366 
367   imm->imm_use = old->next;
368   if (end_readonly_imm_use_p (imm))
369     return NULL_USE_OPERAND_P;
370   return imm->imm_use;
371 }
372 
373 
374 /* Return true if VAR has no nondebug uses.  */
375 static inline bool
has_zero_uses(const_tree var)376 has_zero_uses (const_tree var)
377 {
378   const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
379 
380   /* A single use_operand means there is no items in the list.  */
381   if (ptr == ptr->next)
382     return true;
383 
384   /* If there are debug stmts, we have to look at each use and see
385      whether there are any nondebug uses.  */
386   if (!MAY_HAVE_DEBUG_STMTS)
387     return false;
388 
389   return has_zero_uses_1 (ptr);
390 }
391 
392 /* Return true if VAR has a single nondebug use.  */
393 static inline bool
has_single_use(const_tree var)394 has_single_use (const_tree var)
395 {
396   const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
397 
398   /* If there aren't any uses whatsoever, we're done.  */
399   if (ptr == ptr->next)
400     return false;
401 
402   /* If there's a single use, check that it's not a debug stmt.  */
403   if (ptr == ptr->next->next)
404     return !is_gimple_debug (USE_STMT (ptr->next));
405 
406   /* If there are debug stmts, we have to look at each of them.  */
407   if (!MAY_HAVE_DEBUG_STMTS)
408     return false;
409 
410   return single_imm_use_1 (ptr, NULL, NULL);
411 }
412 
413 
414 /* If VAR has only a single immediate nondebug use, return true, and
415    set USE_P and STMT to the use pointer and stmt of occurrence.  */
416 static inline bool
single_imm_use(const_tree var,use_operand_p * use_p,gimple * stmt)417 single_imm_use (const_tree var, use_operand_p *use_p, gimple *stmt)
418 {
419   const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
420 
421   /* If there aren't any uses whatsoever, we're done.  */
422   if (ptr == ptr->next)
423     {
424     return_false:
425       *use_p = NULL_USE_OPERAND_P;
426       *stmt = NULL;
427       return false;
428     }
429 
430   /* If there's a single use, check that it's not a debug stmt.  */
431   if (ptr == ptr->next->next)
432     {
433       if (!is_gimple_debug (USE_STMT (ptr->next)))
434 	{
435 	  *use_p = ptr->next;
436 	  *stmt = ptr->next->loc.stmt;
437 	  return true;
438 	}
439       else
440 	goto return_false;
441     }
442 
443   /* If there are debug stmts, we have to look at each of them.  */
444   if (!MAY_HAVE_DEBUG_STMTS)
445     goto return_false;
446 
447   return single_imm_use_1 (ptr, use_p, stmt);
448 }
449 
450 /* Return the number of nondebug immediate uses of VAR.  */
451 static inline unsigned int
num_imm_uses(const_tree var)452 num_imm_uses (const_tree var)
453 {
454   const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var));
455   const ssa_use_operand_t *ptr;
456   unsigned int num = 0;
457 
458   if (!MAY_HAVE_DEBUG_STMTS)
459     for (ptr = start->next; ptr != start; ptr = ptr->next)
460       num++;
461   else
462     for (ptr = start->next; ptr != start; ptr = ptr->next)
463       if (!is_gimple_debug (USE_STMT (ptr)))
464 	num++;
465 
466   return num;
467 }
468 
469 /*  -----------------------------------------------------------------------  */
470 
471 /* The following set of routines are used to iterator over various type of
472    SSA operands.  */
473 
474 /* Return true if PTR is finished iterating.  */
475 static inline bool
op_iter_done(const ssa_op_iter * ptr)476 op_iter_done (const ssa_op_iter *ptr)
477 {
478   return ptr->done;
479 }
480 
481 /* Get the next iterator use value for PTR.  */
482 static inline use_operand_p
op_iter_next_use(ssa_op_iter * ptr)483 op_iter_next_use (ssa_op_iter *ptr)
484 {
485   use_operand_p use_p;
486   gcc_checking_assert (ptr->iter_type == ssa_op_iter_use);
487   if (ptr->uses)
488     {
489       use_p = USE_OP_PTR (ptr->uses);
490       ptr->uses = ptr->uses->next;
491       return use_p;
492     }
493   if (ptr->i < ptr->numops)
494     {
495       return PHI_ARG_DEF_PTR (ptr->stmt, (ptr->i)++);
496     }
497   ptr->done = true;
498   return NULL_USE_OPERAND_P;
499 }
500 
501 /* Get the next iterator def value for PTR.  */
502 static inline def_operand_p
op_iter_next_def(ssa_op_iter * ptr)503 op_iter_next_def (ssa_op_iter *ptr)
504 {
505   gcc_checking_assert (ptr->iter_type == ssa_op_iter_def);
506   if (ptr->flags & SSA_OP_VDEF)
507     {
508       tree *p;
509       ptr->flags &= ~SSA_OP_VDEF;
510       p = gimple_vdef_ptr (ptr->stmt);
511       if (p && *p)
512 	return p;
513     }
514   if (ptr->flags & SSA_OP_DEF)
515     {
516       while (ptr->i < ptr->numops)
517 	{
518 	  tree *val = gimple_op_ptr (ptr->stmt, ptr->i);
519 	  ptr->i++;
520 	  if (*val)
521 	    {
522 	      if (TREE_CODE (*val) == TREE_LIST)
523 		val = &TREE_VALUE (*val);
524 	      if (TREE_CODE (*val) == SSA_NAME
525 		  || is_gimple_reg (*val))
526 		return val;
527 	    }
528 	}
529       ptr->flags &= ~SSA_OP_DEF;
530     }
531 
532   ptr->done = true;
533   return NULL_DEF_OPERAND_P;
534 }
535 
536 /* Get the next iterator tree value for PTR.  */
537 static inline tree
op_iter_next_tree(ssa_op_iter * ptr)538 op_iter_next_tree (ssa_op_iter *ptr)
539 {
540   tree val;
541   gcc_checking_assert (ptr->iter_type == ssa_op_iter_tree);
542   if (ptr->uses)
543     {
544       val = USE_OP (ptr->uses);
545       ptr->uses = ptr->uses->next;
546       return val;
547     }
548   if (ptr->flags & SSA_OP_VDEF)
549     {
550       ptr->flags &= ~SSA_OP_VDEF;
551       if ((val = gimple_vdef (ptr->stmt)))
552 	return val;
553     }
554   if (ptr->flags & SSA_OP_DEF)
555     {
556       while (ptr->i < ptr->numops)
557 	{
558 	  val = gimple_op (ptr->stmt, ptr->i);
559 	  ptr->i++;
560 	  if (val)
561 	    {
562 	      if (TREE_CODE (val) == TREE_LIST)
563 		val = TREE_VALUE (val);
564 	      if (TREE_CODE (val) == SSA_NAME
565 		  || is_gimple_reg (val))
566 		return val;
567 	    }
568 	}
569       ptr->flags &= ~SSA_OP_DEF;
570     }
571 
572   ptr->done = true;
573   return NULL_TREE;
574 }
575 
576 
577 /* This functions clears the iterator PTR, and marks it done.  This is normally
578    used to prevent warnings in the compile about might be uninitialized
579    components.  */
580 
581 static inline void
clear_and_done_ssa_iter(ssa_op_iter * ptr)582 clear_and_done_ssa_iter (ssa_op_iter *ptr)
583 {
584   ptr->i = 0;
585   ptr->numops = 0;
586   ptr->uses = NULL;
587   ptr->iter_type = ssa_op_iter_none;
588   ptr->stmt = NULL;
589   ptr->done = true;
590   ptr->flags = 0;
591 }
592 
593 /* Initialize the iterator PTR to the virtual defs in STMT.  */
594 static inline void
op_iter_init(ssa_op_iter * ptr,gimple stmt,int flags)595 op_iter_init (ssa_op_iter *ptr, gimple stmt, int flags)
596 {
597   /* PHI nodes require a different iterator initialization path.  We
598      do not support iterating over virtual defs or uses without
599      iterating over defs or uses at the same time.  */
600   gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI
601 		       && (!(flags & SSA_OP_VDEF) || (flags & SSA_OP_DEF))
602 		       && (!(flags & SSA_OP_VUSE) || (flags & SSA_OP_USE)));
603   ptr->numops = 0;
604   if (flags & (SSA_OP_DEF | SSA_OP_VDEF))
605     {
606       switch (gimple_code (stmt))
607 	{
608 	  case GIMPLE_ASSIGN:
609 	  case GIMPLE_CALL:
610 	    ptr->numops = 1;
611 	    break;
612 	  case GIMPLE_ASM:
613 	    ptr->numops = gimple_asm_noutputs (stmt);
614 	    break;
615 	  default:
616 	    ptr->numops = 0;
617 	    flags &= ~(SSA_OP_DEF | SSA_OP_VDEF);
618 	    break;
619 	}
620     }
621   ptr->uses = (flags & (SSA_OP_USE|SSA_OP_VUSE)) ? gimple_use_ops (stmt) : NULL;
622   if (!(flags & SSA_OP_VUSE)
623       && ptr->uses
624       && gimple_vuse (stmt) != NULL_TREE)
625     ptr->uses = ptr->uses->next;
626   ptr->done = false;
627   ptr->i = 0;
628 
629   ptr->stmt = stmt;
630   ptr->flags = flags;
631 }
632 
633 /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
634    the first use.  */
635 static inline use_operand_p
op_iter_init_use(ssa_op_iter * ptr,gimple stmt,int flags)636 op_iter_init_use (ssa_op_iter *ptr, gimple stmt, int flags)
637 {
638   gcc_checking_assert ((flags & SSA_OP_ALL_DEFS) == 0
639 		       && (flags & SSA_OP_USE));
640   op_iter_init (ptr, stmt, flags);
641   ptr->iter_type = ssa_op_iter_use;
642   return op_iter_next_use (ptr);
643 }
644 
645 /* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
646    the first def.  */
647 static inline def_operand_p
op_iter_init_def(ssa_op_iter * ptr,gimple stmt,int flags)648 op_iter_init_def (ssa_op_iter *ptr, gimple stmt, int flags)
649 {
650   gcc_checking_assert ((flags & SSA_OP_ALL_USES) == 0
651 		       && (flags & SSA_OP_DEF));
652   op_iter_init (ptr, stmt, flags);
653   ptr->iter_type = ssa_op_iter_def;
654   return op_iter_next_def (ptr);
655 }
656 
657 /* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
658    the first operand as a tree.  */
659 static inline tree
op_iter_init_tree(ssa_op_iter * ptr,gimple stmt,int flags)660 op_iter_init_tree (ssa_op_iter *ptr, gimple stmt, int flags)
661 {
662   op_iter_init (ptr, stmt, flags);
663   ptr->iter_type = ssa_op_iter_tree;
664   return op_iter_next_tree (ptr);
665 }
666 
667 
668 /* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
669    return NULL.  */
670 static inline tree
single_ssa_tree_operand(gimple stmt,int flags)671 single_ssa_tree_operand (gimple stmt, int flags)
672 {
673   tree var;
674   ssa_op_iter iter;
675 
676   var = op_iter_init_tree (&iter, stmt, flags);
677   if (op_iter_done (&iter))
678     return NULL_TREE;
679   op_iter_next_tree (&iter);
680   if (op_iter_done (&iter))
681     return var;
682   return NULL_TREE;
683 }
684 
685 
686 /* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
687    return NULL.  */
688 static inline use_operand_p
single_ssa_use_operand(gimple stmt,int flags)689 single_ssa_use_operand (gimple stmt, int flags)
690 {
691   use_operand_p var;
692   ssa_op_iter iter;
693 
694   var = op_iter_init_use (&iter, stmt, flags);
695   if (op_iter_done (&iter))
696     return NULL_USE_OPERAND_P;
697   op_iter_next_use (&iter);
698   if (op_iter_done (&iter))
699     return var;
700   return NULL_USE_OPERAND_P;
701 }
702 
703 
704 
705 /* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
706    return NULL.  */
707 static inline def_operand_p
single_ssa_def_operand(gimple stmt,int flags)708 single_ssa_def_operand (gimple stmt, int flags)
709 {
710   def_operand_p var;
711   ssa_op_iter iter;
712 
713   var = op_iter_init_def (&iter, stmt, flags);
714   if (op_iter_done (&iter))
715     return NULL_DEF_OPERAND_P;
716   op_iter_next_def (&iter);
717   if (op_iter_done (&iter))
718     return var;
719   return NULL_DEF_OPERAND_P;
720 }
721 
722 
723 /* Return true if there are zero operands in STMT matching the type
724    given in FLAGS.  */
725 static inline bool
zero_ssa_operands(gimple stmt,int flags)726 zero_ssa_operands (gimple stmt, int flags)
727 {
728   ssa_op_iter iter;
729 
730   op_iter_init_tree (&iter, stmt, flags);
731   return op_iter_done (&iter);
732 }
733 
734 
735 /* Return the number of operands matching FLAGS in STMT.  */
736 static inline int
num_ssa_operands(gimple stmt,int flags)737 num_ssa_operands (gimple stmt, int flags)
738 {
739   ssa_op_iter iter;
740   tree t;
741   int num = 0;
742 
743   gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI);
744   FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
745     num++;
746   return num;
747 }
748 
749 /* If there is a single DEF in the PHI node which matches FLAG, return it.
750    Otherwise return NULL_DEF_OPERAND_P.  */
751 static inline tree
single_phi_def(gimple stmt,int flags)752 single_phi_def (gimple stmt, int flags)
753 {
754   tree def = PHI_RESULT (stmt);
755   if ((flags & SSA_OP_DEF) && is_gimple_reg (def))
756     return def;
757   if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def))
758     return def;
759   return NULL_TREE;
760 }
761 
762 /* Initialize the iterator PTR for uses matching FLAGS in PHI.  FLAGS should
763    be either SSA_OP_USES or SSA_OP_VIRTUAL_USES.  */
764 static inline use_operand_p
op_iter_init_phiuse(ssa_op_iter * ptr,gimple phi,int flags)765 op_iter_init_phiuse (ssa_op_iter *ptr, gimple phi, int flags)
766 {
767   tree phi_def = gimple_phi_result (phi);
768   int comp;
769 
770   clear_and_done_ssa_iter (ptr);
771   ptr->done = false;
772 
773   gcc_checking_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
774 
775   comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
776 
777   /* If the PHI node doesn't the operand type we care about, we're done.  */
778   if ((flags & comp) == 0)
779     {
780       ptr->done = true;
781       return NULL_USE_OPERAND_P;
782     }
783 
784   ptr->stmt = phi;
785   ptr->numops = gimple_phi_num_args (phi);
786   ptr->iter_type = ssa_op_iter_use;
787   ptr->flags = flags;
788   return op_iter_next_use (ptr);
789 }
790 
791 
792 /* Start an iterator for a PHI definition.  */
793 
794 static inline def_operand_p
op_iter_init_phidef(ssa_op_iter * ptr,gimple phi,int flags)795 op_iter_init_phidef (ssa_op_iter *ptr, gimple phi, int flags)
796 {
797   tree phi_def = PHI_RESULT (phi);
798   int comp;
799 
800   clear_and_done_ssa_iter (ptr);
801   ptr->done = false;
802 
803   gcc_checking_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
804 
805   comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
806 
807   /* If the PHI node doesn't have the operand type we care about,
808      we're done.  */
809   if ((flags & comp) == 0)
810     {
811       ptr->done = true;
812       return NULL_DEF_OPERAND_P;
813     }
814 
815   ptr->iter_type = ssa_op_iter_def;
816   /* The first call to op_iter_next_def will terminate the iterator since
817      all the fields are NULL.  Simply return the result here as the first and
818      therefore only result.  */
819   return PHI_RESULT_PTR (phi);
820 }
821 
822 /* Return true is IMM has reached the end of the immediate use stmt list.  */
823 
824 static inline bool
end_imm_use_stmt_p(const imm_use_iterator * imm)825 end_imm_use_stmt_p (const imm_use_iterator *imm)
826 {
827   return (imm->imm_use == imm->end_p);
828 }
829 
830 /* Finished the traverse of an immediate use stmt list IMM by removing the
831    placeholder node from the list.  */
832 
833 static inline void
end_imm_use_stmt_traverse(imm_use_iterator * imm)834 end_imm_use_stmt_traverse (imm_use_iterator *imm)
835 {
836   delink_imm_use (&(imm->iter_node));
837 }
838 
839 /* Immediate use traversal of uses within a stmt require that all the
840    uses on a stmt be sequentially listed.  This routine is used to build up
841    this sequential list by adding USE_P to the end of the current list
842    currently delimited by HEAD and LAST_P.  The new LAST_P value is
843    returned.  */
844 
845 static inline use_operand_p
move_use_after_head(use_operand_p use_p,use_operand_p head,use_operand_p last_p)846 move_use_after_head (use_operand_p use_p, use_operand_p head,
847 		      use_operand_p last_p)
848 {
849   gcc_checking_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head));
850   /* Skip head when we find it.  */
851   if (use_p != head)
852     {
853       /* If use_p is already linked in after last_p, continue.  */
854       if (last_p->next == use_p)
855 	last_p = use_p;
856       else
857 	{
858 	  /* Delink from current location, and link in at last_p.  */
859 	  delink_imm_use (use_p);
860 	  link_imm_use_to_list (use_p, last_p);
861 	  last_p = use_p;
862 	}
863     }
864   return last_p;
865 }
866 
867 
868 /* This routine will relink all uses with the same stmt as HEAD into the list
869    immediately following HEAD for iterator IMM.  */
870 
871 static inline void
link_use_stmts_after(use_operand_p head,imm_use_iterator * imm)872 link_use_stmts_after (use_operand_p head, imm_use_iterator *imm)
873 {
874   use_operand_p use_p;
875   use_operand_p last_p = head;
876   gimple head_stmt = USE_STMT (head);
877   tree use = USE_FROM_PTR (head);
878   ssa_op_iter op_iter;
879   int flag;
880 
881   /* Only look at virtual or real uses, depending on the type of HEAD.  */
882   flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
883 
884   if (gimple_code (head_stmt) == GIMPLE_PHI)
885     {
886       FOR_EACH_PHI_ARG (use_p, head_stmt, op_iter, flag)
887 	if (USE_FROM_PTR (use_p) == use)
888 	  last_p = move_use_after_head (use_p, head, last_p);
889     }
890   else
891     {
892       if (flag == SSA_OP_USE)
893 	{
894 	  FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag)
895 	    if (USE_FROM_PTR (use_p) == use)
896 	      last_p = move_use_after_head (use_p, head, last_p);
897 	}
898       else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P)
899 	{
900 	  if (USE_FROM_PTR (use_p) == use)
901 	    last_p = move_use_after_head (use_p, head, last_p);
902 	}
903     }
904   /* Link iter node in after last_p.  */
905   if (imm->iter_node.prev != NULL)
906     delink_imm_use (&imm->iter_node);
907   link_imm_use_to_list (&(imm->iter_node), last_p);
908 }
909 
910 /* Initialize IMM to traverse over uses of VAR.  Return the first statement.  */
911 static inline gimple
first_imm_use_stmt(imm_use_iterator * imm,tree var)912 first_imm_use_stmt (imm_use_iterator *imm, tree var)
913 {
914   imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
915   imm->imm_use = imm->end_p->next;
916   imm->next_imm_name = NULL_USE_OPERAND_P;
917 
918   /* iter_node is used as a marker within the immediate use list to indicate
919      where the end of the current stmt's uses are.  Initialize it to NULL
920      stmt and use, which indicates a marker node.  */
921   imm->iter_node.prev = NULL_USE_OPERAND_P;
922   imm->iter_node.next = NULL_USE_OPERAND_P;
923   imm->iter_node.loc.stmt = NULL;
924   imm->iter_node.use = NULL;
925 
926   if (end_imm_use_stmt_p (imm))
927     return NULL;
928 
929   link_use_stmts_after (imm->imm_use, imm);
930 
931   return USE_STMT (imm->imm_use);
932 }
933 
934 /* Bump IMM to the next stmt which has a use of var.  */
935 
936 static inline gimple
next_imm_use_stmt(imm_use_iterator * imm)937 next_imm_use_stmt (imm_use_iterator *imm)
938 {
939   imm->imm_use = imm->iter_node.next;
940   if (end_imm_use_stmt_p (imm))
941     {
942       if (imm->iter_node.prev != NULL)
943 	delink_imm_use (&imm->iter_node);
944       return NULL;
945     }
946 
947   link_use_stmts_after (imm->imm_use, imm);
948   return USE_STMT (imm->imm_use);
949 }
950 
951 /* This routine will return the first use on the stmt IMM currently refers
952    to.  */
953 
954 static inline use_operand_p
first_imm_use_on_stmt(imm_use_iterator * imm)955 first_imm_use_on_stmt (imm_use_iterator *imm)
956 {
957   imm->next_imm_name = imm->imm_use->next;
958   return imm->imm_use;
959 }
960 
961 /*  Return TRUE if the last use on the stmt IMM refers to has been visited.  */
962 
963 static inline bool
end_imm_use_on_stmt_p(const imm_use_iterator * imm)964 end_imm_use_on_stmt_p (const imm_use_iterator *imm)
965 {
966   return (imm->imm_use == &(imm->iter_node));
967 }
968 
969 /* Bump to the next use on the stmt IMM refers to, return NULL if done.  */
970 
971 static inline use_operand_p
next_imm_use_on_stmt(imm_use_iterator * imm)972 next_imm_use_on_stmt (imm_use_iterator *imm)
973 {
974   imm->imm_use = imm->next_imm_name;
975   if (end_imm_use_on_stmt_p (imm))
976     return NULL_USE_OPERAND_P;
977   else
978     {
979       imm->next_imm_name = imm->imm_use->next;
980       return imm->imm_use;
981     }
982 }
983 
984 /* Delink all immediate_use information for STMT.  */
985 static inline void
delink_stmt_imm_use(gimple stmt)986 delink_stmt_imm_use (gimple stmt)
987 {
988    ssa_op_iter iter;
989    use_operand_p use_p;
990 
991    if (ssa_operands_active (cfun))
992      FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_ALL_USES)
993        delink_imm_use (use_p);
994 }
995 
996 #endif /* GCC_TREE_SSA_ITERATORS_H */
997