PR c++/50852
[official-gcc.git] / gcc / tree-flow-inline.h
blob748a97c236e88f6a3122b2813e1f00e6577010a6
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>
6 This file is part of GCC.
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.
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.
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/>. */
22 #ifndef _TREE_FLOW_INLINE_H
23 #define _TREE_FLOW_INLINE_H 1
25 /* Inline functions for manipulating various data structures defined in
26 tree-flow.h. See tree-flow.h for documentation. */
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)
35 return fun && fun->gimple_df && fun->gimple_df->in_ssa_p;
38 /* Array of all variables referenced in the function. */
39 static inline htab_t
40 gimple_referenced_vars (const struct function *fun)
42 if (!fun || !fun->gimple_df)
43 return NULL;
44 return fun->gimple_df->referenced_vars;
47 /* Artificial variable used for the virtual operand FUD chain. */
48 static inline tree
49 gimple_vop (const struct function *fun)
51 gcc_checking_assert (fun && fun->gimple_df);
52 return fun->gimple_df->vop;
55 /* Initialize the hashtable iterator HTI to point to hashtable TABLE */
57 static inline void *
58 first_htab_element (htab_iterator *hti, htab_t table)
60 hti->htab = table;
61 hti->slot = table->entries;
62 hti->limit = hti->slot + htab_size (table);
65 PTR x = *(hti->slot);
66 if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
67 break;
68 } while (++(hti->slot) < hti->limit);
70 if (hti->slot < hti->limit)
71 return *(hti->slot);
72 return NULL;
75 /* Return current non-empty/deleted slot of the hashtable pointed to by HTI,
76 or NULL if we have reached the end. */
78 static inline bool
79 end_htab_p (const htab_iterator *hti)
81 if (hti->slot >= hti->limit)
82 return true;
83 return false;
86 /* Advance the hashtable iterator pointed to by HTI to the next element of the
87 hashtable. */
89 static inline void *
90 next_htab_element (htab_iterator *hti)
92 while (++(hti->slot) < hti->limit)
94 PTR x = *(hti->slot);
95 if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
96 return x;
98 return NULL;
101 /* Get the variable with uid UID from the list of referenced vars. */
103 static inline tree
104 referenced_var (unsigned int uid)
106 tree var = referenced_var_lookup (cfun, uid);
107 gcc_assert (var || uid == 0);
108 return var;
111 /* Initialize ITER to point to the first referenced variable in the
112 referenced_vars hashtable, and return that variable. */
114 static inline tree
115 first_referenced_var (struct function *fn, referenced_var_iterator *iter)
117 return (tree) first_htab_element (&iter->hti,
118 gimple_referenced_vars (fn));
121 /* Return true if we have hit the end of the referenced variables ITER is
122 iterating through. */
124 static inline bool
125 end_referenced_vars_p (const referenced_var_iterator *iter)
127 return end_htab_p (&iter->hti);
130 /* Make ITER point to the next referenced_var in the referenced_var hashtable,
131 and return that variable. */
133 static inline tree
134 next_referenced_var (referenced_var_iterator *iter)
136 return (tree) next_htab_element (&iter->hti);
139 /* Return the variable annotation for T, which must be a _DECL node.
140 Return NULL if the variable annotation doesn't already exist. */
141 static inline var_ann_t
142 var_ann (const_tree t)
144 const var_ann_t *p = DECL_VAR_ANN_PTR (t);
145 return p ? *p : NULL;
148 /* Get the number of the next statement uid to be allocated. */
149 static inline unsigned int
150 gimple_stmt_max_uid (struct function *fn)
152 return fn->last_stmt_uid;
155 /* Set the number of the next statement uid to be allocated. */
156 static inline void
157 set_gimple_stmt_max_uid (struct function *fn, unsigned int maxid)
159 fn->last_stmt_uid = maxid;
162 /* Set the number of the next statement uid to be allocated. */
163 static inline unsigned int
164 inc_gimple_stmt_max_uid (struct function *fn)
166 return fn->last_stmt_uid++;
169 /* Return the line number for EXPR, or return -1 if we have no line
170 number information for it. */
171 static inline int
172 get_lineno (const_gimple stmt)
174 location_t loc;
176 if (!stmt)
177 return -1;
179 loc = gimple_location (stmt);
180 if (loc == UNKNOWN_LOCATION)
181 return -1;
183 return LOCATION_LINE (loc);
186 /* Delink an immediate_uses node from its chain. */
187 static inline void
188 delink_imm_use (ssa_use_operand_t *linknode)
190 /* Return if this node is not in a list. */
191 if (linknode->prev == NULL)
192 return;
194 linknode->prev->next = linknode->next;
195 linknode->next->prev = linknode->prev;
196 linknode->prev = NULL;
197 linknode->next = NULL;
200 /* Link ssa_imm_use node LINKNODE into the chain for LIST. */
201 static inline void
202 link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
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;
212 /* Link ssa_imm_use node LINKNODE into the chain for DEF. */
213 static inline void
214 link_imm_use (ssa_use_operand_t *linknode, tree def)
216 ssa_use_operand_t *root;
218 if (!def || TREE_CODE (def) != SSA_NAME)
219 linknode->prev = NULL;
220 else
222 root = &(SSA_NAME_IMM_USE_NODE (def));
223 if (linknode->use)
224 gcc_checking_assert (*(linknode->use) == def);
225 link_imm_use_to_list (linknode, root);
229 /* Set the value of a use pointed to by USE to VAL. */
230 static inline void
231 set_ssa_use_from_ptr (use_operand_p use, tree val)
233 delink_imm_use (use);
234 *(use->use) = val;
235 link_imm_use (use, val);
238 /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
239 in STMT. */
240 static inline void
241 link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple stmt)
243 if (stmt)
244 link_imm_use (linknode, def);
245 else
246 link_imm_use (linknode, NULL);
247 linknode->loc.stmt = stmt;
250 /* Relink a new node in place of an old node in the list. */
251 static inline void
252 relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
254 /* The node one had better be in the same list. */
255 gcc_checking_assert (*(old->use) == *(node->use));
256 node->prev = old->prev;
257 node->next = old->next;
258 if (old->prev)
260 old->prev->next = node;
261 old->next->prev = node;
262 /* Remove the old node from the list. */
263 old->prev = NULL;
267 /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
268 in STMT. */
269 static inline void
270 relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old,
271 gimple stmt)
273 if (stmt)
274 relink_imm_use (linknode, old);
275 else
276 link_imm_use (linknode, NULL);
277 linknode->loc.stmt = stmt;
281 /* Return true is IMM has reached the end of the immediate use list. */
282 static inline bool
283 end_readonly_imm_use_p (const imm_use_iterator *imm)
285 return (imm->imm_use == imm->end_p);
288 /* Initialize iterator IMM to process the list for VAR. */
289 static inline use_operand_p
290 first_readonly_imm_use (imm_use_iterator *imm, tree var)
292 imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
293 imm->imm_use = imm->end_p->next;
294 #ifdef ENABLE_CHECKING
295 imm->iter_node.next = imm->imm_use->next;
296 #endif
297 if (end_readonly_imm_use_p (imm))
298 return NULL_USE_OPERAND_P;
299 return imm->imm_use;
302 /* Bump IMM to the next use in the list. */
303 static inline use_operand_p
304 next_readonly_imm_use (imm_use_iterator *imm)
306 use_operand_p old = imm->imm_use;
308 #ifdef ENABLE_CHECKING
309 /* If this assertion fails, it indicates the 'next' pointer has changed
310 since the last bump. This indicates that the list is being modified
311 via stmt changes, or SET_USE, or somesuch thing, and you need to be
312 using the SAFE version of the iterator. */
313 gcc_assert (imm->iter_node.next == old->next);
314 imm->iter_node.next = old->next->next;
315 #endif
317 imm->imm_use = old->next;
318 if (end_readonly_imm_use_p (imm))
319 return NULL_USE_OPERAND_P;
320 return imm->imm_use;
323 /* tree-cfg.c */
324 extern bool has_zero_uses_1 (const ssa_use_operand_t *head);
325 extern bool single_imm_use_1 (const ssa_use_operand_t *head,
326 use_operand_p *use_p, gimple *stmt);
328 /* Return true if VAR has no nondebug uses. */
329 static inline bool
330 has_zero_uses (const_tree var)
332 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
334 /* A single use_operand means there is no items in the list. */
335 if (ptr == ptr->next)
336 return true;
338 /* If there are debug stmts, we have to look at each use and see
339 whether there are any nondebug uses. */
340 if (!MAY_HAVE_DEBUG_STMTS)
341 return false;
343 return has_zero_uses_1 (ptr);
346 /* Return true if VAR has a single nondebug use. */
347 static inline bool
348 has_single_use (const_tree var)
350 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
352 /* If there aren't any uses whatsoever, we're done. */
353 if (ptr == ptr->next)
354 return false;
356 /* If there's a single use, check that it's not a debug stmt. */
357 if (ptr == ptr->next->next)
358 return !is_gimple_debug (USE_STMT (ptr->next));
360 /* If there are debug stmts, we have to look at each of them. */
361 if (!MAY_HAVE_DEBUG_STMTS)
362 return false;
364 return single_imm_use_1 (ptr, NULL, NULL);
368 /* If VAR has only a single immediate nondebug use, return true, and
369 set USE_P and STMT to the use pointer and stmt of occurrence. */
370 static inline bool
371 single_imm_use (const_tree var, use_operand_p *use_p, gimple *stmt)
373 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
375 /* If there aren't any uses whatsoever, we're done. */
376 if (ptr == ptr->next)
378 return_false:
379 *use_p = NULL_USE_OPERAND_P;
380 *stmt = NULL;
381 return false;
384 /* If there's a single use, check that it's not a debug stmt. */
385 if (ptr == ptr->next->next)
387 if (!is_gimple_debug (USE_STMT (ptr->next)))
389 *use_p = ptr->next;
390 *stmt = ptr->next->loc.stmt;
391 return true;
393 else
394 goto return_false;
397 /* If there are debug stmts, we have to look at each of them. */
398 if (!MAY_HAVE_DEBUG_STMTS)
399 goto return_false;
401 return single_imm_use_1 (ptr, use_p, stmt);
404 /* Return the number of nondebug immediate uses of VAR. */
405 static inline unsigned int
406 num_imm_uses (const_tree var)
408 const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var));
409 const ssa_use_operand_t *ptr;
410 unsigned int num = 0;
412 if (!MAY_HAVE_DEBUG_STMTS)
413 for (ptr = start->next; ptr != start; ptr = ptr->next)
414 num++;
415 else
416 for (ptr = start->next; ptr != start; ptr = ptr->next)
417 if (!is_gimple_debug (USE_STMT (ptr)))
418 num++;
420 return num;
423 /* Return the tree pointed-to by USE. */
424 static inline tree
425 get_use_from_ptr (use_operand_p use)
427 return *(use->use);
430 /* Return the tree pointed-to by DEF. */
431 static inline tree
432 get_def_from_ptr (def_operand_p def)
434 return *def;
437 /* Return a use_operand_p pointer for argument I of PHI node GS. */
439 static inline use_operand_p
440 gimple_phi_arg_imm_use_ptr (gimple gs, int i)
442 return &gimple_phi_arg (gs, i)->imm_use;
445 /* Return the tree operand for argument I of PHI node GS. */
447 static inline tree
448 gimple_phi_arg_def (gimple gs, size_t index)
450 struct phi_arg_d *pd = gimple_phi_arg (gs, index);
451 return get_use_from_ptr (&pd->imm_use);
454 /* Return a pointer to the tree operand for argument I of PHI node GS. */
456 static inline tree *
457 gimple_phi_arg_def_ptr (gimple gs, size_t index)
459 return &gimple_phi_arg (gs, index)->def;
462 /* Return the edge associated with argument I of phi node GS. */
464 static inline edge
465 gimple_phi_arg_edge (gimple gs, size_t i)
467 return EDGE_PRED (gimple_bb (gs), i);
470 /* Return the source location of gimple argument I of phi node GS. */
472 static inline source_location
473 gimple_phi_arg_location (gimple gs, size_t i)
475 return gimple_phi_arg (gs, i)->locus;
478 /* Return the source location of the argument on edge E of phi node GS. */
480 static inline source_location
481 gimple_phi_arg_location_from_edge (gimple gs, edge e)
483 return gimple_phi_arg (gs, e->dest_idx)->locus;
486 /* Set the source location of gimple argument I of phi node GS to LOC. */
488 static inline void
489 gimple_phi_arg_set_location (gimple gs, size_t i, source_location loc)
491 gimple_phi_arg (gs, i)->locus = loc;
494 /* Return TRUE if argument I of phi node GS has a location record. */
496 static inline bool
497 gimple_phi_arg_has_location (gimple gs, size_t i)
499 return gimple_phi_arg_location (gs, i) != UNKNOWN_LOCATION;
503 /* Return the PHI nodes for basic block BB, or NULL if there are no
504 PHI nodes. */
505 static inline gimple_seq
506 phi_nodes (const_basic_block bb)
508 gcc_checking_assert (!(bb->flags & BB_RTL));
509 return bb->il.gimple.phi_nodes;
512 static inline gimple_seq *
513 phi_nodes_ptr (basic_block bb)
515 gcc_checking_assert (!(bb->flags & BB_RTL));
516 return &bb->il.gimple.phi_nodes;
519 /* Set PHI nodes of a basic block BB to SEQ. */
521 static inline void
522 set_phi_nodes (basic_block bb, gimple_seq seq)
524 gimple_stmt_iterator i;
526 gcc_checking_assert (!(bb->flags & BB_RTL));
527 bb->il.gimple.phi_nodes = seq;
528 if (seq)
529 for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
530 gimple_set_bb (gsi_stmt (i), bb);
533 /* Return the phi argument which contains the specified use. */
535 static inline int
536 phi_arg_index_from_use (use_operand_p use)
538 struct phi_arg_d *element, *root;
539 size_t index;
540 gimple phi;
542 /* Since the use is the first thing in a PHI argument element, we can
543 calculate its index based on casting it to an argument, and performing
544 pointer arithmetic. */
546 phi = USE_STMT (use);
548 element = (struct phi_arg_d *)use;
549 root = gimple_phi_arg (phi, 0);
550 index = element - root;
552 /* Make sure the calculation doesn't have any leftover bytes. If it does,
553 then imm_use is likely not the first element in phi_arg_d. */
554 gcc_checking_assert ((((char *)element - (char *)root)
555 % sizeof (struct phi_arg_d)) == 0
556 && index < gimple_phi_capacity (phi));
558 return index;
561 /* Mark VAR as used, so that it'll be preserved during rtl expansion. */
563 static inline void
564 set_is_used (tree var)
566 var_ann_t ann = var_ann (var);
567 ann->used = true;
570 /* Clear VAR's used flag. */
572 static inline void
573 clear_is_used (tree var)
575 var_ann_t ann = var_ann (var);
576 ann->used = false;
579 /* Return true if VAR is marked as used. */
581 static inline bool
582 is_used_p (tree var)
584 var_ann_t ann = var_ann (var);
585 return ann->used;
588 /* Return true if T (assumed to be a DECL) is a global variable.
589 A variable is considered global if its storage is not automatic. */
591 static inline bool
592 is_global_var (const_tree t)
594 return (TREE_STATIC (t) || DECL_EXTERNAL (t));
598 /* Return true if VAR may be aliased. A variable is considered as
599 maybe aliased if it has its address taken by the local TU
600 or possibly by another TU and might be modified through a pointer. */
602 static inline bool
603 may_be_aliased (const_tree var)
605 return (TREE_CODE (var) != CONST_DECL
606 && !((TREE_STATIC (var) || TREE_PUBLIC (var) || DECL_EXTERNAL (var))
607 && TREE_READONLY (var)
608 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (var)))
609 && (TREE_PUBLIC (var)
610 || DECL_EXTERNAL (var)
611 || TREE_ADDRESSABLE (var)));
615 /* PHI nodes should contain only ssa_names and invariants. A test
616 for ssa_name is definitely simpler; don't let invalid contents
617 slip in in the meantime. */
619 static inline bool
620 phi_ssa_name_p (const_tree t)
622 if (TREE_CODE (t) == SSA_NAME)
623 return true;
624 gcc_checking_assert (is_gimple_min_invariant (t));
625 return false;
629 /* Returns the loop of the statement STMT. */
631 static inline struct loop *
632 loop_containing_stmt (gimple stmt)
634 basic_block bb = gimple_bb (stmt);
635 if (!bb)
636 return NULL;
638 return bb->loop_father;
642 /* ----------------------------------------------------------------------- */
644 /* The following set of routines are used to iterator over various type of
645 SSA operands. */
647 /* Return true if PTR is finished iterating. */
648 static inline bool
649 op_iter_done (const ssa_op_iter *ptr)
651 return ptr->done;
654 /* Get the next iterator use value for PTR. */
655 static inline use_operand_p
656 op_iter_next_use (ssa_op_iter *ptr)
658 use_operand_p use_p;
659 gcc_checking_assert (ptr->iter_type == ssa_op_iter_use);
660 if (ptr->uses)
662 use_p = USE_OP_PTR (ptr->uses);
663 ptr->uses = ptr->uses->next;
664 return use_p;
666 if (ptr->phi_i < ptr->num_phi)
668 return PHI_ARG_DEF_PTR (ptr->phi_stmt, (ptr->phi_i)++);
670 ptr->done = true;
671 return NULL_USE_OPERAND_P;
674 /* Get the next iterator def value for PTR. */
675 static inline def_operand_p
676 op_iter_next_def (ssa_op_iter *ptr)
678 def_operand_p def_p;
679 gcc_checking_assert (ptr->iter_type == ssa_op_iter_def);
680 if (ptr->defs)
682 def_p = DEF_OP_PTR (ptr->defs);
683 ptr->defs = ptr->defs->next;
684 return def_p;
686 ptr->done = true;
687 return NULL_DEF_OPERAND_P;
690 /* Get the next iterator tree value for PTR. */
691 static inline tree
692 op_iter_next_tree (ssa_op_iter *ptr)
694 tree val;
695 gcc_checking_assert (ptr->iter_type == ssa_op_iter_tree);
696 if (ptr->uses)
698 val = USE_OP (ptr->uses);
699 ptr->uses = ptr->uses->next;
700 return val;
702 if (ptr->defs)
704 val = DEF_OP (ptr->defs);
705 ptr->defs = ptr->defs->next;
706 return val;
709 ptr->done = true;
710 return NULL_TREE;
715 /* This functions clears the iterator PTR, and marks it done. This is normally
716 used to prevent warnings in the compile about might be uninitialized
717 components. */
719 static inline void
720 clear_and_done_ssa_iter (ssa_op_iter *ptr)
722 ptr->defs = NULL;
723 ptr->uses = NULL;
724 ptr->iter_type = ssa_op_iter_none;
725 ptr->phi_i = 0;
726 ptr->num_phi = 0;
727 ptr->phi_stmt = NULL;
728 ptr->done = true;
731 /* Initialize the iterator PTR to the virtual defs in STMT. */
732 static inline void
733 op_iter_init (ssa_op_iter *ptr, gimple stmt, int flags)
735 /* PHI nodes require a different iterator initialization path. We
736 do not support iterating over virtual defs or uses without
737 iterating over defs or uses at the same time. */
738 gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI
739 && (!(flags & SSA_OP_VDEF) || (flags & SSA_OP_DEF))
740 && (!(flags & SSA_OP_VUSE) || (flags & SSA_OP_USE)));
741 ptr->defs = (flags & (SSA_OP_DEF|SSA_OP_VDEF)) ? gimple_def_ops (stmt) : NULL;
742 if (!(flags & SSA_OP_VDEF)
743 && ptr->defs
744 && gimple_vdef (stmt) != NULL_TREE)
745 ptr->defs = ptr->defs->next;
746 ptr->uses = (flags & (SSA_OP_USE|SSA_OP_VUSE)) ? gimple_use_ops (stmt) : NULL;
747 if (!(flags & SSA_OP_VUSE)
748 && ptr->uses
749 && gimple_vuse (stmt) != NULL_TREE)
750 ptr->uses = ptr->uses->next;
751 ptr->done = false;
753 ptr->phi_i = 0;
754 ptr->num_phi = 0;
755 ptr->phi_stmt = NULL;
758 /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
759 the first use. */
760 static inline use_operand_p
761 op_iter_init_use (ssa_op_iter *ptr, gimple stmt, int flags)
763 gcc_checking_assert ((flags & SSA_OP_ALL_DEFS) == 0
764 && (flags & SSA_OP_USE));
765 op_iter_init (ptr, stmt, flags);
766 ptr->iter_type = ssa_op_iter_use;
767 return op_iter_next_use (ptr);
770 /* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
771 the first def. */
772 static inline def_operand_p
773 op_iter_init_def (ssa_op_iter *ptr, gimple stmt, int flags)
775 gcc_checking_assert ((flags & SSA_OP_ALL_USES) == 0
776 && (flags & SSA_OP_DEF));
777 op_iter_init (ptr, stmt, flags);
778 ptr->iter_type = ssa_op_iter_def;
779 return op_iter_next_def (ptr);
782 /* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
783 the first operand as a tree. */
784 static inline tree
785 op_iter_init_tree (ssa_op_iter *ptr, gimple stmt, int flags)
787 op_iter_init (ptr, stmt, flags);
788 ptr->iter_type = ssa_op_iter_tree;
789 return op_iter_next_tree (ptr);
793 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
794 return NULL. */
795 static inline tree
796 single_ssa_tree_operand (gimple stmt, int flags)
798 tree var;
799 ssa_op_iter iter;
801 var = op_iter_init_tree (&iter, stmt, flags);
802 if (op_iter_done (&iter))
803 return NULL_TREE;
804 op_iter_next_tree (&iter);
805 if (op_iter_done (&iter))
806 return var;
807 return NULL_TREE;
811 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
812 return NULL. */
813 static inline use_operand_p
814 single_ssa_use_operand (gimple stmt, int flags)
816 use_operand_p var;
817 ssa_op_iter iter;
819 var = op_iter_init_use (&iter, stmt, flags);
820 if (op_iter_done (&iter))
821 return NULL_USE_OPERAND_P;
822 op_iter_next_use (&iter);
823 if (op_iter_done (&iter))
824 return var;
825 return NULL_USE_OPERAND_P;
830 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
831 return NULL. */
832 static inline def_operand_p
833 single_ssa_def_operand (gimple stmt, int flags)
835 def_operand_p var;
836 ssa_op_iter iter;
838 var = op_iter_init_def (&iter, stmt, flags);
839 if (op_iter_done (&iter))
840 return NULL_DEF_OPERAND_P;
841 op_iter_next_def (&iter);
842 if (op_iter_done (&iter))
843 return var;
844 return NULL_DEF_OPERAND_P;
848 /* Return true if there are zero operands in STMT matching the type
849 given in FLAGS. */
850 static inline bool
851 zero_ssa_operands (gimple stmt, int flags)
853 ssa_op_iter iter;
855 op_iter_init_tree (&iter, stmt, flags);
856 return op_iter_done (&iter);
860 /* Return the number of operands matching FLAGS in STMT. */
861 static inline int
862 num_ssa_operands (gimple stmt, int flags)
864 ssa_op_iter iter;
865 tree t;
866 int num = 0;
868 gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI);
869 FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
870 num++;
871 return num;
874 static inline use_operand_p
875 op_iter_init_phiuse (ssa_op_iter *ptr, gimple phi, int flags);
877 /* Delink all immediate_use information for STMT. */
878 static inline void
879 delink_stmt_imm_use (gimple stmt)
881 ssa_op_iter iter;
882 use_operand_p use_p;
884 if (ssa_operands_active ())
885 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_ALL_USES)
886 delink_imm_use (use_p);
890 /* If there is a single DEF in the PHI node which matches FLAG, return it.
891 Otherwise return NULL_DEF_OPERAND_P. */
892 static inline tree
893 single_phi_def (gimple stmt, int flags)
895 tree def = PHI_RESULT (stmt);
896 if ((flags & SSA_OP_DEF) && is_gimple_reg (def))
897 return def;
898 if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def))
899 return def;
900 return NULL_TREE;
903 /* Initialize the iterator PTR for uses matching FLAGS in PHI. FLAGS should
904 be either SSA_OP_USES or SSA_OP_VIRTUAL_USES. */
905 static inline use_operand_p
906 op_iter_init_phiuse (ssa_op_iter *ptr, gimple phi, int flags)
908 tree phi_def = gimple_phi_result (phi);
909 int comp;
911 clear_and_done_ssa_iter (ptr);
912 ptr->done = false;
914 gcc_checking_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
916 comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
918 /* If the PHI node doesn't the operand type we care about, we're done. */
919 if ((flags & comp) == 0)
921 ptr->done = true;
922 return NULL_USE_OPERAND_P;
925 ptr->phi_stmt = phi;
926 ptr->num_phi = gimple_phi_num_args (phi);
927 ptr->iter_type = ssa_op_iter_use;
928 return op_iter_next_use (ptr);
932 /* Start an iterator for a PHI definition. */
934 static inline def_operand_p
935 op_iter_init_phidef (ssa_op_iter *ptr, gimple phi, int flags)
937 tree phi_def = PHI_RESULT (phi);
938 int comp;
940 clear_and_done_ssa_iter (ptr);
941 ptr->done = false;
943 gcc_checking_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
945 comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
947 /* If the PHI node doesn't have the operand type we care about,
948 we're done. */
949 if ((flags & comp) == 0)
951 ptr->done = true;
952 return NULL_DEF_OPERAND_P;
955 ptr->iter_type = ssa_op_iter_def;
956 /* The first call to op_iter_next_def will terminate the iterator since
957 all the fields are NULL. Simply return the result here as the first and
958 therefore only result. */
959 return PHI_RESULT_PTR (phi);
962 /* Return true is IMM has reached the end of the immediate use stmt list. */
964 static inline bool
965 end_imm_use_stmt_p (const imm_use_iterator *imm)
967 return (imm->imm_use == imm->end_p);
970 /* Finished the traverse of an immediate use stmt list IMM by removing the
971 placeholder node from the list. */
973 static inline void
974 end_imm_use_stmt_traverse (imm_use_iterator *imm)
976 delink_imm_use (&(imm->iter_node));
979 /* Immediate use traversal of uses within a stmt require that all the
980 uses on a stmt be sequentially listed. This routine is used to build up
981 this sequential list by adding USE_P to the end of the current list
982 currently delimited by HEAD and LAST_P. The new LAST_P value is
983 returned. */
985 static inline use_operand_p
986 move_use_after_head (use_operand_p use_p, use_operand_p head,
987 use_operand_p last_p)
989 gcc_checking_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head));
990 /* Skip head when we find it. */
991 if (use_p != head)
993 /* If use_p is already linked in after last_p, continue. */
994 if (last_p->next == use_p)
995 last_p = use_p;
996 else
998 /* Delink from current location, and link in at last_p. */
999 delink_imm_use (use_p);
1000 link_imm_use_to_list (use_p, last_p);
1001 last_p = use_p;
1004 return last_p;
1008 /* This routine will relink all uses with the same stmt as HEAD into the list
1009 immediately following HEAD for iterator IMM. */
1011 static inline void
1012 link_use_stmts_after (use_operand_p head, imm_use_iterator *imm)
1014 use_operand_p use_p;
1015 use_operand_p last_p = head;
1016 gimple head_stmt = USE_STMT (head);
1017 tree use = USE_FROM_PTR (head);
1018 ssa_op_iter op_iter;
1019 int flag;
1021 /* Only look at virtual or real uses, depending on the type of HEAD. */
1022 flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
1024 if (gimple_code (head_stmt) == GIMPLE_PHI)
1026 FOR_EACH_PHI_ARG (use_p, head_stmt, op_iter, flag)
1027 if (USE_FROM_PTR (use_p) == use)
1028 last_p = move_use_after_head (use_p, head, last_p);
1030 else
1032 if (flag == SSA_OP_USE)
1034 FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag)
1035 if (USE_FROM_PTR (use_p) == use)
1036 last_p = move_use_after_head (use_p, head, last_p);
1038 else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P)
1040 if (USE_FROM_PTR (use_p) == use)
1041 last_p = move_use_after_head (use_p, head, last_p);
1044 /* Link iter node in after last_p. */
1045 if (imm->iter_node.prev != NULL)
1046 delink_imm_use (&imm->iter_node);
1047 link_imm_use_to_list (&(imm->iter_node), last_p);
1050 /* Initialize IMM to traverse over uses of VAR. Return the first statement. */
1051 static inline gimple
1052 first_imm_use_stmt (imm_use_iterator *imm, tree var)
1054 imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
1055 imm->imm_use = imm->end_p->next;
1056 imm->next_imm_name = NULL_USE_OPERAND_P;
1058 /* iter_node is used as a marker within the immediate use list to indicate
1059 where the end of the current stmt's uses are. Initialize it to NULL
1060 stmt and use, which indicates a marker node. */
1061 imm->iter_node.prev = NULL_USE_OPERAND_P;
1062 imm->iter_node.next = NULL_USE_OPERAND_P;
1063 imm->iter_node.loc.stmt = NULL;
1064 imm->iter_node.use = NULL;
1066 if (end_imm_use_stmt_p (imm))
1067 return NULL;
1069 link_use_stmts_after (imm->imm_use, imm);
1071 return USE_STMT (imm->imm_use);
1074 /* Bump IMM to the next stmt which has a use of var. */
1076 static inline gimple
1077 next_imm_use_stmt (imm_use_iterator *imm)
1079 imm->imm_use = imm->iter_node.next;
1080 if (end_imm_use_stmt_p (imm))
1082 if (imm->iter_node.prev != NULL)
1083 delink_imm_use (&imm->iter_node);
1084 return NULL;
1087 link_use_stmts_after (imm->imm_use, imm);
1088 return USE_STMT (imm->imm_use);
1091 /* This routine will return the first use on the stmt IMM currently refers
1092 to. */
1094 static inline use_operand_p
1095 first_imm_use_on_stmt (imm_use_iterator *imm)
1097 imm->next_imm_name = imm->imm_use->next;
1098 return imm->imm_use;
1101 /* Return TRUE if the last use on the stmt IMM refers to has been visited. */
1103 static inline bool
1104 end_imm_use_on_stmt_p (const imm_use_iterator *imm)
1106 return (imm->imm_use == &(imm->iter_node));
1109 /* Bump to the next use on the stmt IMM refers to, return NULL if done. */
1111 static inline use_operand_p
1112 next_imm_use_on_stmt (imm_use_iterator *imm)
1114 imm->imm_use = imm->next_imm_name;
1115 if (end_imm_use_on_stmt_p (imm))
1116 return NULL_USE_OPERAND_P;
1117 else
1119 imm->next_imm_name = imm->imm_use->next;
1120 return imm->imm_use;
1124 /* Return true if VAR cannot be modified by the program. */
1126 static inline bool
1127 unmodifiable_var_p (const_tree var)
1129 if (TREE_CODE (var) == SSA_NAME)
1130 var = SSA_NAME_VAR (var);
1132 return TREE_READONLY (var) && (TREE_STATIC (var) || DECL_EXTERNAL (var));
1135 /* Return true if REF, a handled component reference, has an ARRAY_REF
1136 somewhere in it. */
1138 static inline bool
1139 ref_contains_array_ref (const_tree ref)
1141 gcc_checking_assert (handled_component_p (ref));
1143 do {
1144 if (TREE_CODE (ref) == ARRAY_REF)
1145 return true;
1146 ref = TREE_OPERAND (ref, 0);
1147 } while (handled_component_p (ref));
1149 return false;
1152 /* Return true if REF has an VIEW_CONVERT_EXPR somewhere in it. */
1154 static inline bool
1155 contains_view_convert_expr_p (const_tree ref)
1157 while (handled_component_p (ref))
1159 if (TREE_CODE (ref) == VIEW_CONVERT_EXPR)
1160 return true;
1161 ref = TREE_OPERAND (ref, 0);
1164 return false;
1167 /* Return true, if the two ranges [POS1, SIZE1] and [POS2, SIZE2]
1168 overlap. SIZE1 and/or SIZE2 can be (unsigned)-1 in which case the
1169 range is open-ended. Otherwise return false. */
1171 static inline bool
1172 ranges_overlap_p (unsigned HOST_WIDE_INT pos1,
1173 unsigned HOST_WIDE_INT size1,
1174 unsigned HOST_WIDE_INT pos2,
1175 unsigned HOST_WIDE_INT size2)
1177 if (pos1 >= pos2
1178 && (size2 == (unsigned HOST_WIDE_INT)-1
1179 || pos1 < (pos2 + size2)))
1180 return true;
1181 if (pos2 >= pos1
1182 && (size1 == (unsigned HOST_WIDE_INT)-1
1183 || pos2 < (pos1 + size1)))
1184 return true;
1186 return false;
1189 /* Accessor to tree-ssa-operands.c caches. */
1190 static inline struct ssa_operands *
1191 gimple_ssa_operands (const struct function *fun)
1193 return &fun->gimple_df->ssa_operands;
1196 /* Given an edge_var_map V, return the PHI arg definition. */
1198 static inline tree
1199 redirect_edge_var_map_def (edge_var_map *v)
1201 return v->def;
1204 /* Given an edge_var_map V, return the PHI result. */
1206 static inline tree
1207 redirect_edge_var_map_result (edge_var_map *v)
1209 return v->result;
1212 /* Given an edge_var_map V, return the PHI arg location. */
1214 static inline source_location
1215 redirect_edge_var_map_location (edge_var_map *v)
1217 return v->locus;
1221 /* Return an SSA_NAME node for variable VAR defined in statement STMT
1222 in function cfun. */
1224 static inline tree
1225 make_ssa_name (tree var, gimple stmt)
1227 return make_ssa_name_fn (cfun, var, stmt);
1230 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
1231 denotes the starting address of the memory access EXP.
1232 Returns NULL_TREE if the offset is not constant or any component
1233 is not BITS_PER_UNIT-aligned.
1234 VALUEIZE if non-NULL is used to valueize SSA names. It should return
1235 its argument or a constant if the argument is known to be constant. */
1237 static inline tree
1238 get_addr_base_and_unit_offset_1 (tree exp, HOST_WIDE_INT *poffset,
1239 tree (*valueize) (tree))
1241 HOST_WIDE_INT byte_offset = 0;
1243 /* Compute cumulative byte-offset for nested component-refs and array-refs,
1244 and find the ultimate containing object. */
1245 while (1)
1247 switch (TREE_CODE (exp))
1249 case BIT_FIELD_REF:
1250 return NULL_TREE;
1252 case COMPONENT_REF:
1254 tree field = TREE_OPERAND (exp, 1);
1255 tree this_offset = component_ref_field_offset (exp);
1256 HOST_WIDE_INT hthis_offset;
1258 if (!this_offset
1259 || TREE_CODE (this_offset) != INTEGER_CST
1260 || (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
1261 % BITS_PER_UNIT))
1262 return NULL_TREE;
1264 hthis_offset = TREE_INT_CST_LOW (this_offset);
1265 hthis_offset += (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
1266 / BITS_PER_UNIT);
1267 byte_offset += hthis_offset;
1269 break;
1271 case ARRAY_REF:
1272 case ARRAY_RANGE_REF:
1274 tree index = TREE_OPERAND (exp, 1);
1275 tree low_bound, unit_size;
1277 if (valueize
1278 && TREE_CODE (index) == SSA_NAME)
1279 index = (*valueize) (index);
1281 /* If the resulting bit-offset is constant, track it. */
1282 if (TREE_CODE (index) == INTEGER_CST
1283 && (low_bound = array_ref_low_bound (exp),
1284 TREE_CODE (low_bound) == INTEGER_CST)
1285 && (unit_size = array_ref_element_size (exp),
1286 TREE_CODE (unit_size) == INTEGER_CST))
1288 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (index);
1290 hindex -= TREE_INT_CST_LOW (low_bound);
1291 hindex *= TREE_INT_CST_LOW (unit_size);
1292 byte_offset += hindex;
1294 else
1295 return NULL_TREE;
1297 break;
1299 case REALPART_EXPR:
1300 break;
1302 case IMAGPART_EXPR:
1303 byte_offset += TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (exp)));
1304 break;
1306 case VIEW_CONVERT_EXPR:
1307 break;
1309 case MEM_REF:
1311 tree base = TREE_OPERAND (exp, 0);
1312 if (valueize
1313 && TREE_CODE (base) == SSA_NAME)
1314 base = (*valueize) (base);
1316 /* Hand back the decl for MEM[&decl, off]. */
1317 if (TREE_CODE (base) == ADDR_EXPR)
1319 if (!integer_zerop (TREE_OPERAND (exp, 1)))
1321 double_int off = mem_ref_offset (exp);
1322 gcc_assert (off.high == -1 || off.high == 0);
1323 byte_offset += double_int_to_shwi (off);
1325 exp = TREE_OPERAND (base, 0);
1327 goto done;
1330 case TARGET_MEM_REF:
1332 tree base = TREE_OPERAND (exp, 0);
1333 if (valueize
1334 && TREE_CODE (base) == SSA_NAME)
1335 base = (*valueize) (base);
1337 /* Hand back the decl for MEM[&decl, off]. */
1338 if (TREE_CODE (base) == ADDR_EXPR)
1340 if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
1341 return NULL_TREE;
1342 if (!integer_zerop (TMR_OFFSET (exp)))
1344 double_int off = mem_ref_offset (exp);
1345 gcc_assert (off.high == -1 || off.high == 0);
1346 byte_offset += double_int_to_shwi (off);
1348 exp = TREE_OPERAND (base, 0);
1350 goto done;
1353 default:
1354 goto done;
1357 exp = TREE_OPERAND (exp, 0);
1359 done:
1361 *poffset = byte_offset;
1362 return exp;
1365 #endif /* _TREE_FLOW_INLINE_H */