1/* Inline functions for tree-flow.h
2   Copyright (C) 2001, 2003, 2005, 2006, 2007, 2008, 2010
3   Free Software Foundation, Inc.
4   Contributed by Diego Novillo <dnovillo@redhat.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3.  If not see
20<http://www.gnu.org/licenses/>.  */
21
22#ifndef _TREE_FLOW_INLINE_H
23#define _TREE_FLOW_INLINE_H 1
24
25/* Inline functions for manipulating various data structures defined in
26   tree-flow.h.  See tree-flow.h for documentation.  */
27
28/* Return true when gimple SSA form was built.
29   gimple_in_ssa_p is queried by gimplifier in various early stages before SSA
30   infrastructure is initialized.  Check for presence of the datastructures
31   at first place.  */
32static inline bool
33gimple_in_ssa_p (const struct function *fun)
34{
35  return fun && fun->gimple_df && fun->gimple_df->in_ssa_p;
36}
37
38/* Array of all variables referenced in the function.  */
39static inline htab_t
40gimple_referenced_vars (const struct function *fun)
41{
42  if (!fun->gimple_df)
43    return NULL;
44  return fun->gimple_df->referenced_vars;
45}
46
47/* Artificial variable used for the virtual operand FUD chain.  */
48static inline tree
49gimple_vop (const struct function *fun)
50{
51  gcc_assert (fun && fun->gimple_df);
52  return fun->gimple_df->vop;
53}
54
55/* Initialize the hashtable iterator HTI to point to hashtable TABLE */
56
57static inline void *
58first_htab_element (htab_iterator *hti, htab_t table)
59{
60  hti->htab = table;
61  hti->slot = table->entries;
62  hti->limit = hti->slot + htab_size (table);
63  do
64    {
65      PTR x = *(hti->slot);
66      if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
67	break;
68    } while (++(hti->slot) < hti->limit);
69
70  if (hti->slot < hti->limit)
71    return *(hti->slot);
72  return NULL;
73}
74
75/* Return current non-empty/deleted slot of the hashtable pointed to by HTI,
76   or NULL if we have  reached the end.  */
77
78static inline bool
79end_htab_p (const htab_iterator *hti)
80{
81  if (hti->slot >= hti->limit)
82    return true;
83  return false;
84}
85
86/* Advance the hashtable iterator pointed to by HTI to the next element of the
87   hashtable.  */
88
89static inline void *
90next_htab_element (htab_iterator *hti)
91{
92  while (++(hti->slot) < hti->limit)
93    {
94      PTR x = *(hti->slot);
95      if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
96	return x;
97    };
98  return NULL;
99}
100
101/* Initialize ITER to point to the first referenced variable in the
102   referenced_vars hashtable, and return that variable.  */
103
104static inline tree
105first_referenced_var (referenced_var_iterator *iter)
106{
107  return (tree) first_htab_element (&iter->hti,
108				    gimple_referenced_vars (cfun));
109}
110
111/* Return true if we have hit the end of the referenced variables ITER is
112   iterating through.  */
113
114static inline bool
115end_referenced_vars_p (const referenced_var_iterator *iter)
116{
117  return end_htab_p (&iter->hti);
118}
119
120/* Make ITER point to the next referenced_var in the referenced_var hashtable,
121   and return that variable.  */
122
123static inline tree
124next_referenced_var (referenced_var_iterator *iter)
125{
126  return (tree) next_htab_element (&iter->hti);
127}
128
129/* Return the variable annotation for T, which must be a _DECL node.
130   Return NULL if the variable annotation doesn't already exist.  */
131static inline var_ann_t
132var_ann (const_tree t)
133{
134  const var_ann_t *p = DECL_VAR_ANN_PTR (t);
135  return p ? *p : NULL;
136}
137
138/* Return the variable annotation for T, which must be a _DECL node.
139   Create the variable annotation if it doesn't exist.  */
140static inline var_ann_t
141get_var_ann (tree var)
142{
143  var_ann_t *p = DECL_VAR_ANN_PTR (var);
144  gcc_assert (p);
145  return *p ? *p : create_var_ann (var);
146}
147
148/* Get the number of the next statement uid to be allocated.  */
149static inline unsigned int
150gimple_stmt_max_uid (struct function *fn)
151{
152  return fn->last_stmt_uid;
153}
154
155/* Set the number of the next statement uid to be allocated.  */
156static inline void
157set_gimple_stmt_max_uid (struct function *fn, unsigned int maxid)
158{
159  fn->last_stmt_uid = maxid;
160}
161
162/* Set the number of the next statement uid to be allocated.  */
163static inline unsigned int
164inc_gimple_stmt_max_uid (struct function *fn)
165{
166  return fn->last_stmt_uid++;
167}
168
169/* Return the line number for EXPR, or return -1 if we have no line
170   number information for it.  */
171static inline int
172get_lineno (const_gimple stmt)
173{
174  location_t loc;
175
176  if (!stmt)
177    return -1;
178
179  loc = gimple_location (stmt);
180  if (loc == UNKNOWN_LOCATION)
181    return -1;
182
183  return LOCATION_LINE (loc);
184}
185
186/* Delink an immediate_uses node from its chain.  */
187static inline void
188delink_imm_use (ssa_use_operand_t *linknode)
189{
190  /* Return if this node is not in a list.  */
191  if (linknode->prev == NULL)
192    return;
193
194  linknode->prev->next = linknode->next;
195  linknode->next->prev = linknode->prev;
196  linknode->prev = NULL;
197  linknode->next = NULL;
198}
199
200/* Link ssa_imm_use node LINKNODE into the chain for LIST.  */
201static inline void
202link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
203{
204  /* Link the new node at the head of the list.  If we are in the process of
205     traversing the list, we won't visit any new nodes added to it.  */
206  linknode->prev = list;
207  linknode->next = list->next;
208  list->next->prev = linknode;
209  list->next = linknode;
210}
211
212/* Link ssa_imm_use node LINKNODE into the chain for DEF.  */
213static inline void
214link_imm_use (ssa_use_operand_t *linknode, tree def)
215{
216  ssa_use_operand_t *root;
217
218  if (!def || TREE_CODE (def) != SSA_NAME)
219    linknode->prev = NULL;
220  else
221    {
222      root = &(SSA_NAME_IMM_USE_NODE (def));
223#ifdef ENABLE_CHECKING
224      if (linknode->use)
225        gcc_assert (*(linknode->use) == def);
226#endif
227      link_imm_use_to_list (linknode, root);
228    }
229}
230
231/* Set the value of a use pointed to by USE to VAL.  */
232static inline void
233set_ssa_use_from_ptr (use_operand_p use, tree val)
234{
235  delink_imm_use (use);
236  *(use->use) = val;
237  link_imm_use (use, val);
238}
239
240/* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
241   in STMT.  */
242static inline void
243link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple stmt)
244{
245  if (stmt)
246    link_imm_use (linknode, def);
247  else
248    link_imm_use (linknode, NULL);
249  linknode->loc.stmt = stmt;
250}
251
252/* Relink a new node in place of an old node in the list.  */
253static inline void
254relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
255{
256  /* The node one had better be in the same list.  */
257  gcc_assert (*(old->use) == *(node->use));
258  node->prev = old->prev;
259  node->next = old->next;
260  if (old->prev)
261    {
262      old->prev->next = node;
263      old->next->prev = node;
264      /* Remove the old node from the list.  */
265      old->prev = NULL;
266    }
267}
268
269/* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
270   in STMT.  */
271static inline void
272relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old,
273		     gimple stmt)
274{
275  if (stmt)
276    relink_imm_use (linknode, old);
277  else
278    link_imm_use (linknode, NULL);
279  linknode->loc.stmt = stmt;
280}
281
282
283/* Return true is IMM has reached the end of the immediate use list.  */
284static inline bool
285end_readonly_imm_use_p (const imm_use_iterator *imm)
286{
287  return (imm->imm_use == imm->end_p);
288}
289
290/* Initialize iterator IMM to process the list for VAR.  */
291static inline use_operand_p
292first_readonly_imm_use (imm_use_iterator *imm, tree var)
293{
294  imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
295  imm->imm_use = imm->end_p->next;
296#ifdef ENABLE_CHECKING
297  imm->iter_node.next = imm->imm_use->next;
298#endif
299  if (end_readonly_imm_use_p (imm))
300    return NULL_USE_OPERAND_P;
301  return imm->imm_use;
302}
303
304/* Bump IMM to the next use in the list.  */
305static inline use_operand_p
306next_readonly_imm_use (imm_use_iterator *imm)
307{
308  use_operand_p old = imm->imm_use;
309
310#ifdef ENABLE_CHECKING
311  /* If this assertion fails, it indicates the 'next' pointer has changed
312     since the last bump.  This indicates that the list is being modified
313     via stmt changes, or SET_USE, or somesuch thing, and you need to be
314     using the SAFE version of the iterator.  */
315  gcc_assert (imm->iter_node.next == old->next);
316  imm->iter_node.next = old->next->next;
317#endif
318
319  imm->imm_use = old->next;
320  if (end_readonly_imm_use_p (imm))
321    return NULL_USE_OPERAND_P;
322  return imm->imm_use;
323}
324
325/* tree-cfg.c */
326extern bool has_zero_uses_1 (const ssa_use_operand_t *head);
327extern bool single_imm_use_1 (const ssa_use_operand_t *head,
328			      use_operand_p *use_p, gimple *stmt);
329
330/* Return true if VAR has no nondebug uses.  */
331static inline bool
332has_zero_uses (const_tree var)
333{
334  const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
335
336  /* A single use_operand means there is no items in the list.  */
337  if (ptr == ptr->next)
338    return true;
339
340  /* If there are debug stmts, we have to look at each use and see
341     whether there are any nondebug uses.  */
342  if (!MAY_HAVE_DEBUG_STMTS)
343    return false;
344
345  return has_zero_uses_1 (ptr);
346}
347
348/* Return true if VAR has a single nondebug use.  */
349static inline bool
350has_single_use (const_tree var)
351{
352  const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
353
354  /* If there aren't any uses whatsoever, we're done.  */
355  if (ptr == ptr->next)
356    return false;
357
358  /* If there's a single use, check that it's not a debug stmt.  */
359  if (ptr == ptr->next->next)
360    return !is_gimple_debug (USE_STMT (ptr->next));
361
362  /* If there are debug stmts, we have to look at each of them.  */
363  if (!MAY_HAVE_DEBUG_STMTS)
364    return false;
365
366  return single_imm_use_1 (ptr, NULL, NULL);
367}
368
369
370/* If VAR has only a single immediate nondebug use, return true, and
371   set USE_P and STMT to the use pointer and stmt of occurrence.  */
372static inline bool
373single_imm_use (const_tree var, use_operand_p *use_p, gimple *stmt)
374{
375  const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
376
377  /* If there aren't any uses whatsoever, we're done.  */
378  if (ptr == ptr->next)
379    {
380    return_false:
381      *use_p = NULL_USE_OPERAND_P;
382      *stmt = NULL;
383      return false;
384    }
385
386  /* If there's a single use, check that it's not a debug stmt.  */
387  if (ptr == ptr->next->next)
388    {
389      if (!is_gimple_debug (USE_STMT (ptr->next)))
390	{
391	  *use_p = ptr->next;
392	  *stmt = ptr->next->loc.stmt;
393	  return true;
394	}
395      else
396	goto return_false;
397    }
398
399  /* If there are debug stmts, we have to look at each of them.  */
400  if (!MAY_HAVE_DEBUG_STMTS)
401    goto return_false;
402
403  return single_imm_use_1 (ptr, use_p, stmt);
404}
405
406/* Return the number of nondebug immediate uses of VAR.  */
407static inline unsigned int
408num_imm_uses (const_tree var)
409{
410  const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var));
411  const ssa_use_operand_t *ptr;
412  unsigned int num = 0;
413
414  if (!MAY_HAVE_DEBUG_STMTS)
415    for (ptr = start->next; ptr != start; ptr = ptr->next)
416      num++;
417  else
418    for (ptr = start->next; ptr != start; ptr = ptr->next)
419      if (!is_gimple_debug (USE_STMT (ptr)))
420	num++;
421
422  return num;
423}
424
425/* Return the tree pointed-to by USE.  */
426static inline tree
427get_use_from_ptr (use_operand_p use)
428{
429  return *(use->use);
430}
431
432/* Return the tree pointed-to by DEF.  */
433static inline tree
434get_def_from_ptr (def_operand_p def)
435{
436  return *def;
437}
438
439/* Return a use_operand_p pointer for argument I of PHI node GS.  */
440
441static inline use_operand_p
442gimple_phi_arg_imm_use_ptr (gimple gs, int i)
443{
444  return &gimple_phi_arg (gs, i)->imm_use;
445}
446
447/* Return the tree operand for argument I of PHI node GS.  */
448
449static inline tree
450gimple_phi_arg_def (gimple gs, size_t index)
451{
452  struct phi_arg_d *pd = gimple_phi_arg (gs, index);
453  return get_use_from_ptr (&pd->imm_use);
454}
455
456/* Return a pointer to the tree operand for argument I of PHI node GS.  */
457
458static inline tree *
459gimple_phi_arg_def_ptr (gimple gs, size_t index)
460{
461  return &gimple_phi_arg (gs, index)->def;
462}
463
464/* Return the edge associated with argument I of phi node GS.  */
465
466static inline edge
467gimple_phi_arg_edge (gimple gs, size_t i)
468{
469  return EDGE_PRED (gimple_bb (gs), i);
470}
471
472/* Return the source location of gimple argument I of phi node GS.  */
473
474static inline source_location
475gimple_phi_arg_location (gimple gs, size_t i)
476{
477  return gimple_phi_arg (gs, i)->locus;
478}
479
480/* Return the source location of the argument on edge E of phi node GS.  */
481
482static inline source_location
483gimple_phi_arg_location_from_edge (gimple gs, edge e)
484{
485  return gimple_phi_arg (gs, e->dest_idx)->locus;
486}
487
488/* Set the source location of gimple argument I of phi node GS to LOC.  */
489
490static inline void
491gimple_phi_arg_set_location (gimple gs, size_t i, source_location loc)
492{
493  gimple_phi_arg (gs, i)->locus = loc;
494}
495
496/* Return TRUE if argument I of phi node GS has a location record.  */
497
498static inline bool
499gimple_phi_arg_has_location (gimple gs, size_t i)
500{
501  return gimple_phi_arg_location (gs, i) != UNKNOWN_LOCATION;
502}
503
504
505/* Return the PHI nodes for basic block BB, or NULL if there are no
506   PHI nodes.  */
507static inline gimple_seq
508phi_nodes (const_basic_block bb)
509{
510  gcc_assert (!(bb->flags & BB_RTL));
511  if (!bb->il.gimple)
512    return NULL;
513  return bb->il.gimple->phi_nodes;
514}
515
516/* Set PHI nodes of a basic block BB to SEQ.  */
517
518static inline void
519set_phi_nodes (basic_block bb, gimple_seq seq)
520{
521  gimple_stmt_iterator i;
522
523  gcc_assert (!(bb->flags & BB_RTL));
524  bb->il.gimple->phi_nodes = seq;
525  if (seq)
526    for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
527      gimple_set_bb (gsi_stmt (i), bb);
528}
529
530/* Return the phi argument which contains the specified use.  */
531
532static inline int
533phi_arg_index_from_use (use_operand_p use)
534{
535  struct phi_arg_d *element, *root;
536  size_t index;
537  gimple phi;
538
539  /* Since the use is the first thing in a PHI argument element, we can
540     calculate its index based on casting it to an argument, and performing
541     pointer arithmetic.  */
542
543  phi = USE_STMT (use);
544  gcc_assert (gimple_code (phi) == GIMPLE_PHI);
545
546  element = (struct phi_arg_d *)use;
547  root = gimple_phi_arg (phi, 0);
548  index = element - root;
549
550#ifdef ENABLE_CHECKING
551  /* Make sure the calculation doesn't have any leftover bytes.  If it does,
552     then imm_use is likely not the first element in phi_arg_d.  */
553  gcc_assert ((((char *)element - (char *)root)
554	       % sizeof (struct phi_arg_d)) == 0
555	      && index < gimple_phi_capacity (phi));
556#endif
557
558 return index;
559}
560
561/* Mark VAR as used, so that it'll be preserved during rtl expansion.  */
562
563static inline void
564set_is_used (tree var)
565{
566  var_ann_t ann = get_var_ann (var);
567  ann->used = 1;
568}
569
570
571/* Return true if T (assumed to be a DECL) is a global variable.
572   A variable is considered global if its storage is not automatic.  */
573
574static inline bool
575is_global_var (const_tree t)
576{
577  return (TREE_STATIC (t) || DECL_EXTERNAL (t));
578}
579
580
581/* Return true if VAR may be aliased.  A variable is considered as
582   maybe aliased if it has its address taken by the local TU
583   or possibly by another TU and might be modified through a pointer.  */
584
585static inline bool
586may_be_aliased (const_tree var)
587{
588  return (TREE_CODE (var) != CONST_DECL
589	  && !((TREE_STATIC (var) || TREE_PUBLIC (var) || DECL_EXTERNAL (var))
590	       && TREE_READONLY (var)
591	       && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (var)))
592	  && (TREE_PUBLIC (var)
593	      || DECL_EXTERNAL (var)
594	      || TREE_ADDRESSABLE (var)));
595}
596
597
598/* PHI nodes should contain only ssa_names and invariants.  A test
599   for ssa_name is definitely simpler; don't let invalid contents
600   slip in in the meantime.  */
601
602static inline bool
603phi_ssa_name_p (const_tree t)
604{
605  if (TREE_CODE (t) == SSA_NAME)
606    return true;
607#ifdef ENABLE_CHECKING
608  gcc_assert (is_gimple_min_invariant (t));
609#endif
610  return false;
611}
612
613
614/* Returns the loop of the statement STMT.  */
615
616static inline struct loop *
617loop_containing_stmt (gimple stmt)
618{
619  basic_block bb = gimple_bb (stmt);
620  if (!bb)
621    return NULL;
622
623  return bb->loop_father;
624}
625
626
627/* Return true if VAR is clobbered by function calls.  */
628static inline bool
629is_call_clobbered (const_tree var)
630{
631  return (is_global_var (var)
632	  || (may_be_aliased (var)
633	      && pt_solution_includes (&cfun->gimple_df->escaped, var)));
634}
635
636/* Return true if VAR is used by function calls.  */
637static inline bool
638is_call_used (const_tree var)
639{
640  return (is_call_clobbered (var)
641	  || (may_be_aliased (var)
642	      && pt_solution_includes (&cfun->gimple_df->callused, var)));
643}
644
645/*  -----------------------------------------------------------------------  */
646
647/* The following set of routines are used to iterator over various type of
648   SSA operands.  */
649
650/* Return true if PTR is finished iterating.  */
651static inline bool
652op_iter_done (const ssa_op_iter *ptr)
653{
654  return ptr->done;
655}
656
657/* Get the next iterator use value for PTR.  */
658static inline use_operand_p
659op_iter_next_use (ssa_op_iter *ptr)
660{
661  use_operand_p use_p;
662#ifdef ENABLE_CHECKING
663  gcc_assert (ptr->iter_type == ssa_op_iter_use);
664#endif
665  if (ptr->uses)
666    {
667      use_p = USE_OP_PTR (ptr->uses);
668      ptr->uses = ptr->uses->next;
669      return use_p;
670    }
671  if (ptr->phi_i < ptr->num_phi)
672    {
673      return PHI_ARG_DEF_PTR (ptr->phi_stmt, (ptr->phi_i)++);
674    }
675  ptr->done = true;
676  return NULL_USE_OPERAND_P;
677}
678
679/* Get the next iterator def value for PTR.  */
680static inline def_operand_p
681op_iter_next_def (ssa_op_iter *ptr)
682{
683  def_operand_p def_p;
684#ifdef ENABLE_CHECKING
685  gcc_assert (ptr->iter_type == ssa_op_iter_def);
686#endif
687  if (ptr->defs)
688    {
689      def_p = DEF_OP_PTR (ptr->defs);
690      ptr->defs = ptr->defs->next;
691      return def_p;
692    }
693  ptr->done = true;
694  return NULL_DEF_OPERAND_P;
695}
696
697/* Get the next iterator tree value for PTR.  */
698static inline tree
699op_iter_next_tree (ssa_op_iter *ptr)
700{
701  tree val;
702#ifdef ENABLE_CHECKING
703  gcc_assert (ptr->iter_type == ssa_op_iter_tree);
704#endif
705  if (ptr->uses)
706    {
707      val = USE_OP (ptr->uses);
708      ptr->uses = ptr->uses->next;
709      return val;
710    }
711  if (ptr->defs)
712    {
713      val = DEF_OP (ptr->defs);
714      ptr->defs = ptr->defs->next;
715      return val;
716    }
717
718  ptr->done = true;
719  return NULL_TREE;
720
721}
722
723
724/* This functions clears the iterator PTR, and marks it done.  This is normally
725   used to prevent warnings in the compile about might be uninitialized
726   components.  */
727
728static inline void
729clear_and_done_ssa_iter (ssa_op_iter *ptr)
730{
731  ptr->defs = NULL;
732  ptr->uses = NULL;
733  ptr->iter_type = ssa_op_iter_none;
734  ptr->phi_i = 0;
735  ptr->num_phi = 0;
736  ptr->phi_stmt = NULL;
737  ptr->done = true;
738}
739
740/* Initialize the iterator PTR to the virtual defs in STMT.  */
741static inline void
742op_iter_init (ssa_op_iter *ptr, gimple stmt, int flags)
743{
744  /* We do not support iterating over virtual defs or uses without
745     iterating over defs or uses at the same time.  */
746  gcc_assert ((!(flags & SSA_OP_VDEF) || (flags & SSA_OP_DEF))
747	      && (!(flags & SSA_OP_VUSE) || (flags & SSA_OP_USE)));
748  ptr->defs = (flags & (SSA_OP_DEF|SSA_OP_VDEF)) ? gimple_def_ops (stmt) : NULL;
749  if (!(flags & SSA_OP_VDEF)
750      && ptr->defs
751      && gimple_vdef (stmt) != NULL_TREE)
752    ptr->defs = ptr->defs->next;
753  ptr->uses = (flags & (SSA_OP_USE|SSA_OP_VUSE)) ? gimple_use_ops (stmt) : NULL;
754  if (!(flags & SSA_OP_VUSE)
755      && ptr->uses
756      && gimple_vuse (stmt) != NULL_TREE)
757    ptr->uses = ptr->uses->next;
758  ptr->done = false;
759
760  ptr->phi_i = 0;
761  ptr->num_phi = 0;
762  ptr->phi_stmt = NULL;
763}
764
765/* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
766   the first use.  */
767static inline use_operand_p
768op_iter_init_use (ssa_op_iter *ptr, gimple stmt, int flags)
769{
770  gcc_assert ((flags & SSA_OP_ALL_DEFS) == 0
771	      && (flags & SSA_OP_USE));
772  op_iter_init (ptr, stmt, flags);
773  ptr->iter_type = ssa_op_iter_use;
774  return op_iter_next_use (ptr);
775}
776
777/* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
778   the first def.  */
779static inline def_operand_p
780op_iter_init_def (ssa_op_iter *ptr, gimple stmt, int flags)
781{
782  gcc_assert ((flags & SSA_OP_ALL_USES) == 0
783	      && (flags & SSA_OP_DEF));
784  op_iter_init (ptr, stmt, flags);
785  ptr->iter_type = ssa_op_iter_def;
786  return op_iter_next_def (ptr);
787}
788
789/* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
790   the first operand as a tree.  */
791static inline tree
792op_iter_init_tree (ssa_op_iter *ptr, gimple stmt, int flags)
793{
794  op_iter_init (ptr, stmt, flags);
795  ptr->iter_type = ssa_op_iter_tree;
796  return op_iter_next_tree (ptr);
797}
798
799
800/* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
801   return NULL.  */
802static inline tree
803single_ssa_tree_operand (gimple stmt, int flags)
804{
805  tree var;
806  ssa_op_iter iter;
807
808  var = op_iter_init_tree (&iter, stmt, flags);
809  if (op_iter_done (&iter))
810    return NULL_TREE;
811  op_iter_next_tree (&iter);
812  if (op_iter_done (&iter))
813    return var;
814  return NULL_TREE;
815}
816
817
818/* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
819   return NULL.  */
820static inline use_operand_p
821single_ssa_use_operand (gimple stmt, int flags)
822{
823  use_operand_p var;
824  ssa_op_iter iter;
825
826  var = op_iter_init_use (&iter, stmt, flags);
827  if (op_iter_done (&iter))
828    return NULL_USE_OPERAND_P;
829  op_iter_next_use (&iter);
830  if (op_iter_done (&iter))
831    return var;
832  return NULL_USE_OPERAND_P;
833}
834
835
836
837/* If there is a single operand in STMT matching FLAGS, return it.  Otherwise
838   return NULL.  */
839static inline def_operand_p
840single_ssa_def_operand (gimple stmt, int flags)
841{
842  def_operand_p var;
843  ssa_op_iter iter;
844
845  var = op_iter_init_def (&iter, stmt, flags);
846  if (op_iter_done (&iter))
847    return NULL_DEF_OPERAND_P;
848  op_iter_next_def (&iter);
849  if (op_iter_done (&iter))
850    return var;
851  return NULL_DEF_OPERAND_P;
852}
853
854
855/* Return true if there are zero operands in STMT matching the type
856   given in FLAGS.  */
857static inline bool
858zero_ssa_operands (gimple stmt, int flags)
859{
860  ssa_op_iter iter;
861
862  op_iter_init_tree (&iter, stmt, flags);
863  return op_iter_done (&iter);
864}
865
866
867/* Return the number of operands matching FLAGS in STMT.  */
868static inline int
869num_ssa_operands (gimple stmt, int flags)
870{
871  ssa_op_iter iter;
872  tree t;
873  int num = 0;
874
875  FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
876    num++;
877  return num;
878}
879
880
881/* Delink all immediate_use information for STMT.  */
882static inline void
883delink_stmt_imm_use (gimple stmt)
884{
885   ssa_op_iter iter;
886   use_operand_p use_p;
887
888   if (ssa_operands_active ())
889     FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
890       delink_imm_use (use_p);
891}
892
893
894/* If there is a single DEF in the PHI node which matches FLAG, return it.
895   Otherwise return NULL_DEF_OPERAND_P.  */
896static inline tree
897single_phi_def (gimple stmt, int flags)
898{
899  tree def = PHI_RESULT (stmt);
900  if ((flags & SSA_OP_DEF) && is_gimple_reg (def))
901    return def;
902  if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def))
903    return def;
904  return NULL_TREE;
905}
906
907/* Initialize the iterator PTR for uses matching FLAGS in PHI.  FLAGS should
908   be either SSA_OP_USES or SSA_OP_VIRTUAL_USES.  */
909static inline use_operand_p
910op_iter_init_phiuse (ssa_op_iter *ptr, gimple phi, int flags)
911{
912  tree phi_def = gimple_phi_result (phi);
913  int comp;
914
915  clear_and_done_ssa_iter (ptr);
916  ptr->done = false;
917
918  gcc_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
919
920  comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
921
922  /* If the PHI node doesn't the operand type we care about, we're done.  */
923  if ((flags & comp) == 0)
924    {
925      ptr->done = true;
926      return NULL_USE_OPERAND_P;
927    }
928
929  ptr->phi_stmt = phi;
930  ptr->num_phi = gimple_phi_num_args (phi);
931  ptr->iter_type = ssa_op_iter_use;
932  return op_iter_next_use (ptr);
933}
934
935
936/* Start an iterator for a PHI definition.  */
937
938static inline def_operand_p
939op_iter_init_phidef (ssa_op_iter *ptr, gimple phi, int flags)
940{
941  tree phi_def = PHI_RESULT (phi);
942  int comp;
943
944  clear_and_done_ssa_iter (ptr);
945  ptr->done = false;
946
947  gcc_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
948
949  comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
950
951  /* If the PHI node doesn't have the operand type we care about,
952     we're done.  */
953  if ((flags & comp) == 0)
954    {
955      ptr->done = true;
956      return NULL_DEF_OPERAND_P;
957    }
958
959  ptr->iter_type = ssa_op_iter_def;
960  /* The first call to op_iter_next_def will terminate the iterator since
961     all the fields are NULL.  Simply return the result here as the first and
962     therefore only result.  */
963  return PHI_RESULT_PTR (phi);
964}
965
966/* Return true is IMM has reached the end of the immediate use stmt list.  */
967
968static inline bool
969end_imm_use_stmt_p (const imm_use_iterator *imm)
970{
971  return (imm->imm_use == imm->end_p);
972}
973
974/* Finished the traverse of an immediate use stmt list IMM by removing the
975   placeholder node from the list.  */
976
977static inline void
978end_imm_use_stmt_traverse (imm_use_iterator *imm)
979{
980  delink_imm_use (&(imm->iter_node));
981}
982
983/* Immediate use traversal of uses within a stmt require that all the
984   uses on a stmt be sequentially listed.  This routine is used to build up
985   this sequential list by adding USE_P to the end of the current list
986   currently delimited by HEAD and LAST_P.  The new LAST_P value is
987   returned.  */
988
989static inline use_operand_p
990move_use_after_head (use_operand_p use_p, use_operand_p head,
991		      use_operand_p last_p)
992{
993#ifdef ENABLE_CHECKING
994  gcc_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head));
995#endif
996  /* Skip head when we find it.  */
997  if (use_p != head)
998    {
999      /* If use_p is already linked in after last_p, continue.  */
1000      if (last_p->next == use_p)
1001	last_p = use_p;
1002      else
1003	{
1004	  /* Delink from current location, and link in at last_p.  */
1005	  delink_imm_use (use_p);
1006	  link_imm_use_to_list (use_p, last_p);
1007	  last_p = use_p;
1008	}
1009    }
1010  return last_p;
1011}
1012
1013
1014/* This routine will relink all uses with the same stmt as HEAD into the list
1015   immediately following HEAD for iterator IMM.  */
1016
1017static inline void
1018link_use_stmts_after (use_operand_p head, imm_use_iterator *imm)
1019{
1020  use_operand_p use_p;
1021  use_operand_p last_p = head;
1022  gimple head_stmt = USE_STMT (head);
1023  tree use = USE_FROM_PTR (head);
1024  ssa_op_iter op_iter;
1025  int flag;
1026
1027  /* Only look at virtual or real uses, depending on the type of HEAD.  */
1028  flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
1029
1030  if (gimple_code (head_stmt) == GIMPLE_PHI)
1031    {
1032      FOR_EACH_PHI_ARG (use_p, head_stmt, op_iter, flag)
1033	if (USE_FROM_PTR (use_p) == use)
1034	  last_p = move_use_after_head (use_p, head, last_p);
1035    }
1036  else
1037    {
1038      if (flag == SSA_OP_USE)
1039	{
1040	  FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag)
1041	    if (USE_FROM_PTR (use_p) == use)
1042	      last_p = move_use_after_head (use_p, head, last_p);
1043	}
1044      else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P)
1045	{
1046	  if (USE_FROM_PTR (use_p) == use)
1047	    last_p = move_use_after_head (use_p, head, last_p);
1048	}
1049    }
1050  /* Link iter node in after last_p.  */
1051  if (imm->iter_node.prev != NULL)
1052    delink_imm_use (&imm->iter_node);
1053  link_imm_use_to_list (&(imm->iter_node), last_p);
1054}
1055
1056/* Initialize IMM to traverse over uses of VAR.  Return the first statement.  */
1057static inline gimple
1058first_imm_use_stmt (imm_use_iterator *imm, tree var)
1059{
1060  imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
1061  imm->imm_use = imm->end_p->next;
1062  imm->next_imm_name = NULL_USE_OPERAND_P;
1063
1064  /* iter_node is used as a marker within the immediate use list to indicate
1065     where the end of the current stmt's uses are.  Initialize it to NULL
1066     stmt and use, which indicates a marker node.  */
1067  imm->iter_node.prev = NULL_USE_OPERAND_P;
1068  imm->iter_node.next = NULL_USE_OPERAND_P;
1069  imm->iter_node.loc.stmt = NULL;
1070  imm->iter_node.use = NULL;
1071
1072  if (end_imm_use_stmt_p (imm))
1073    return NULL;
1074
1075  link_use_stmts_after (imm->imm_use, imm);
1076
1077  return USE_STMT (imm->imm_use);
1078}
1079
1080/* Bump IMM to the next stmt which has a use of var.  */
1081
1082static inline gimple
1083next_imm_use_stmt (imm_use_iterator *imm)
1084{
1085  imm->imm_use = imm->iter_node.next;
1086  if (end_imm_use_stmt_p (imm))
1087    {
1088      if (imm->iter_node.prev != NULL)
1089	delink_imm_use (&imm->iter_node);
1090      return NULL;
1091    }
1092
1093  link_use_stmts_after (imm->imm_use, imm);
1094  return USE_STMT (imm->imm_use);
1095}
1096
1097/* This routine will return the first use on the stmt IMM currently refers
1098   to.  */
1099
1100static inline use_operand_p
1101first_imm_use_on_stmt (imm_use_iterator *imm)
1102{
1103  imm->next_imm_name = imm->imm_use->next;
1104  return imm->imm_use;
1105}
1106
1107/*  Return TRUE if the last use on the stmt IMM refers to has been visited.  */
1108
1109static inline bool
1110end_imm_use_on_stmt_p (const imm_use_iterator *imm)
1111{
1112  return (imm->imm_use == &(imm->iter_node));
1113}
1114
1115/* Bump to the next use on the stmt IMM refers to, return NULL if done.  */
1116
1117static inline use_operand_p
1118next_imm_use_on_stmt (imm_use_iterator *imm)
1119{
1120  imm->imm_use = imm->next_imm_name;
1121  if (end_imm_use_on_stmt_p (imm))
1122    return NULL_USE_OPERAND_P;
1123  else
1124    {
1125      imm->next_imm_name = imm->imm_use->next;
1126      return imm->imm_use;
1127    }
1128}
1129
1130/* Return true if VAR cannot be modified by the program.  */
1131
1132static inline bool
1133unmodifiable_var_p (const_tree var)
1134{
1135  if (TREE_CODE (var) == SSA_NAME)
1136    var = SSA_NAME_VAR (var);
1137
1138  return TREE_READONLY (var) && (TREE_STATIC (var) || DECL_EXTERNAL (var));
1139}
1140
1141/* Return true if REF, an ARRAY_REF, has an INDIRECT_REF somewhere in it.  */
1142
1143static inline bool
1144array_ref_contains_indirect_ref (const_tree ref)
1145{
1146  gcc_assert (TREE_CODE (ref) == ARRAY_REF);
1147
1148  do {
1149    ref = TREE_OPERAND (ref, 0);
1150  } while (handled_component_p (ref));
1151
1152  return TREE_CODE (ref) == INDIRECT_REF;
1153}
1154
1155/* Return true if REF, a handled component reference, has an ARRAY_REF
1156   somewhere in it.  */
1157
1158static inline bool
1159ref_contains_array_ref (const_tree ref)
1160{
1161  gcc_assert (handled_component_p (ref));
1162
1163  do {
1164    if (TREE_CODE (ref) == ARRAY_REF)
1165      return true;
1166    ref = TREE_OPERAND (ref, 0);
1167  } while (handled_component_p (ref));
1168
1169  return false;
1170}
1171
1172/* Return true if REF has an VIEW_CONVERT_EXPR somewhere in it.  */
1173
1174static inline bool
1175contains_view_convert_expr_p (const_tree ref)
1176{
1177  while (handled_component_p (ref))
1178    {
1179      if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
1180	return true;
1181      ref = TREE_OPERAND (ref, 0);
1182    }
1183
1184  return false;
1185}
1186
1187/* Return true, if the two ranges [POS1, SIZE1] and [POS2, SIZE2]
1188   overlap.  SIZE1 and/or SIZE2 can be (unsigned)-1 in which case the
1189   range is open-ended.  Otherwise return false.  */
1190
1191static inline bool
1192ranges_overlap_p (unsigned HOST_WIDE_INT pos1,
1193		  unsigned HOST_WIDE_INT size1,
1194		  unsigned HOST_WIDE_INT pos2,
1195		  unsigned HOST_WIDE_INT size2)
1196{
1197  if (pos1 >= pos2
1198      && (size2 == (unsigned HOST_WIDE_INT)-1
1199	  || pos1 < (pos2 + size2)))
1200    return true;
1201  if (pos2 >= pos1
1202      && (size1 == (unsigned HOST_WIDE_INT)-1
1203	  || pos2 < (pos1 + size1)))
1204    return true;
1205
1206  return false;
1207}
1208
1209/* Accessor to tree-ssa-operands.c caches.  */
1210static inline struct ssa_operands *
1211gimple_ssa_operands (const struct function *fun)
1212{
1213  return &fun->gimple_df->ssa_operands;
1214}
1215
1216/* Given an edge_var_map V, return the PHI arg definition.  */
1217
1218static inline tree
1219redirect_edge_var_map_def (edge_var_map *v)
1220{
1221  return v->def;
1222}
1223
1224/* Given an edge_var_map V, return the PHI result.  */
1225
1226static inline tree
1227redirect_edge_var_map_result (edge_var_map *v)
1228{
1229  return v->result;
1230}
1231
1232/* Given an edge_var_map V, return the PHI arg location.  */
1233
1234static inline source_location
1235redirect_edge_var_map_location (edge_var_map *v)
1236{
1237  return v->locus;
1238}
1239
1240
1241/* Return an SSA_NAME node for variable VAR defined in statement STMT
1242   in function cfun.  */
1243
1244static inline tree
1245make_ssa_name (tree var, gimple stmt)
1246{
1247  return make_ssa_name_fn (cfun, var, stmt);
1248}
1249
1250#endif /* _TREE_FLOW_INLINE_H  */
1251