OSDN Git Service

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