1 /* Inline functions for tree-flow.h
2 Copyright (C) 2001, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef _TREE_FLOW_INLINE_H
22 #define _TREE_FLOW_INLINE_H 1
24 /* Inline functions for manipulating various data structures defined in
25 tree-flow.h. See tree-flow.h for documentation. */
27 /* Return true when gimple SSA form was built.
28 gimple_in_ssa_p is queried by gimplifier in various early stages before SSA
29 infrastructure is initialized. Check for presence of the datastructures
32 gimple_in_ssa_p (const struct function *fun)
34 return fun && fun->gimple_df && fun->gimple_df->in_ssa_p;
37 /* 'true' after aliases have been computed (see compute_may_aliases). */
39 gimple_aliases_computed_p (const struct function *fun)
41 gcc_assert (fun && fun->gimple_df);
42 return fun->gimple_df->aliases_computed_p;
45 /* Addressable variables in the function. If bit I is set, then
46 REFERENCED_VARS (I) has had its address taken. Note that
47 CALL_CLOBBERED_VARS and ADDRESSABLE_VARS are not related. An
48 addressable variable is not necessarily call-clobbered (e.g., a
49 local addressable whose address does not escape) and not all
50 call-clobbered variables are addressable (e.g., a local static
53 gimple_addressable_vars (const struct function *fun)
55 gcc_assert (fun && fun->gimple_df);
56 return fun->gimple_df->addressable_vars;
59 /* Call clobbered variables in the function. If bit I is set, then
60 REFERENCED_VARS (I) is call-clobbered. */
62 gimple_call_clobbered_vars (const struct function *fun)
64 gcc_assert (fun && fun->gimple_df);
65 return fun->gimple_df->call_clobbered_vars;
68 /* Array of all variables referenced in the function. */
70 gimple_referenced_vars (const struct function *fun)
74 return fun->gimple_df->referenced_vars;
77 /* Artificial variable used to model the effects of function calls. */
79 gimple_global_var (const struct function *fun)
81 gcc_assert (fun && fun->gimple_df);
82 return fun->gimple_df->global_var;
85 /* Artificial variable used to model the effects of nonlocal
88 gimple_nonlocal_all (const struct function *fun)
90 gcc_assert (fun && fun->gimple_df);
91 return fun->gimple_df->nonlocal_all;
94 /* Hashtable of variables annotations. Used for static variables only;
95 local variables have direct pointer in the tree node. */
97 gimple_var_anns (const struct function *fun)
99 return fun->gimple_df->var_anns;
102 /* Initialize the hashtable iterator HTI to point to hashtable TABLE */
105 first_htab_element (htab_iterator *hti, htab_t table)
108 hti->slot = table->entries;
109 hti->limit = hti->slot + htab_size (table);
112 PTR x = *(hti->slot);
113 if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
115 } while (++(hti->slot) < hti->limit);
117 if (hti->slot < hti->limit)
122 /* Return current non-empty/deleted slot of the hashtable pointed to by HTI,
123 or NULL if we have reached the end. */
126 end_htab_p (const htab_iterator *hti)
128 if (hti->slot >= hti->limit)
133 /* Advance the hashtable iterator pointed to by HTI to the next element of the
137 next_htab_element (htab_iterator *hti)
139 while (++(hti->slot) < hti->limit)
141 PTR x = *(hti->slot);
142 if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
148 /* Helper for FOR_EACH_REFERENCED_VAR. Needed unless iterator
149 bodies deal with NULL elements. */
152 referenced_var_iterator_set (referenced_var_iterator *ri, tree *var)
156 if (!bmp_iter_set (&ri->bi, &ri->i))
158 *var = lookup_decl_from_uid (ri->i);
161 bmp_iter_next (&ri->bi, &ri->i);
165 /* Fill up VEC with the variables in the referenced vars hashtable. */
168 fill_referenced_var_vec (VEC (tree, heap) **vec)
170 referenced_var_iterator rvi;
173 FOR_EACH_REFERENCED_VAR (var, rvi)
174 VEC_safe_push (tree, heap, *vec, var);
177 /* Return the variable annotation for T, which must be a _DECL node.
178 Return NULL if the variable annotation doesn't already exist. */
179 static inline var_ann_t
180 var_ann (const_tree t)
185 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
187 struct static_var_ann_d *sann
188 = ((struct static_var_ann_d *)
189 htab_find_with_hash (gimple_var_anns (cfun), t, DECL_UID (t)));
198 ann = (var_ann_t) t->base.ann;
201 gcc_assert (ann->common.type == VAR_ANN);
206 /* Return the variable annotation for T, which must be a _DECL node.
207 Create the variable annotation if it doesn't exist. */
208 static inline var_ann_t
209 get_var_ann (tree var)
211 var_ann_t ann = var_ann (var);
212 return (ann) ? ann : create_var_ann (var);
215 /* Return the function annotation for T, which must be a FUNCTION_DECL node.
216 Return NULL if the function annotation doesn't already exist. */
217 static inline function_ann_t
218 function_ann (const_tree t)
221 gcc_assert (TREE_CODE (t) == FUNCTION_DECL);
222 gcc_assert (!t->base.ann
223 || t->base.ann->common.type == FUNCTION_ANN);
225 return (function_ann_t) t->base.ann;
228 /* Return the function annotation for T, which must be a FUNCTION_DECL node.
229 Create the function annotation if it doesn't exist. */
230 static inline function_ann_t
231 get_function_ann (tree var)
233 function_ann_t ann = function_ann (var);
234 gcc_assert (!var->base.ann || var->base.ann->common.type == FUNCTION_ANN);
235 return (ann) ? ann : create_function_ann (var);
238 /* Return true if T has a statement annotation attached to it. */
241 has_stmt_ann (tree t)
243 #ifdef ENABLE_CHECKING
244 gcc_assert (is_gimple_stmt (t));
246 return t->base.ann && t->base.ann->common.type == STMT_ANN;
249 /* Return the statement annotation for T, which must be a statement
250 node. Return NULL if the statement annotation doesn't exist. */
251 static inline stmt_ann_t
254 #ifdef ENABLE_CHECKING
255 gcc_assert (is_gimple_stmt (t));
257 gcc_assert (!t->base.ann || t->base.ann->common.type == STMT_ANN);
258 return (stmt_ann_t) t->base.ann;
261 /* Return the statement annotation for T, which must be a statement
262 node. Create the statement annotation if it doesn't exist. */
263 static inline stmt_ann_t
264 get_stmt_ann (tree stmt)
266 stmt_ann_t ann = stmt_ann (stmt);
267 return (ann) ? ann : create_stmt_ann (stmt);
270 /* Return the annotation type for annotation ANN. */
271 static inline enum tree_ann_type
272 ann_type (tree_ann_t ann)
274 return ann->common.type;
277 /* Return the basic block for statement T. */
278 static inline basic_block
283 if (TREE_CODE (t) == PHI_NODE)
287 return ann ? ann->bb : NULL;
290 /* Return the may_aliases bitmap for variable VAR, or NULL if it has
293 may_aliases (const_tree var)
295 return MTAG_ALIASES (var);
298 /* Return the line number for EXPR, or return -1 if we have no line
299 number information for it. */
301 get_lineno (const_tree expr)
303 if (expr == NULL_TREE)
306 if (TREE_CODE (expr) == COMPOUND_EXPR)
307 expr = TREE_OPERAND (expr, 0);
309 if (! EXPR_HAS_LOCATION (expr))
312 return EXPR_LINENO (expr);
315 /* Return true if T is a noreturn call. */
317 noreturn_call_p (tree t)
319 tree call = get_call_expr_in (t);
320 return call != 0 && (call_expr_flags (call) & ECF_NORETURN) != 0;
323 /* Mark statement T as modified. */
325 mark_stmt_modified (tree t)
328 if (TREE_CODE (t) == PHI_NODE)
333 ann = create_stmt_ann (t);
334 else if (noreturn_call_p (t) && cfun->gimple_df)
335 VEC_safe_push (tree, gc, MODIFIED_NORETURN_CALLS (cfun), t);
339 /* Mark statement T as modified, and update it. */
343 if (TREE_CODE (t) == PHI_NODE)
345 mark_stmt_modified (t);
346 update_stmt_operands (t);
350 update_stmt_if_modified (tree t)
352 if (stmt_modified_p (t))
353 update_stmt_operands (t);
356 /* Return true if T is marked as modified, false otherwise. */
358 stmt_modified_p (tree t)
360 stmt_ann_t ann = stmt_ann (t);
362 /* Note that if the statement doesn't yet have an annotation, we consider it
363 modified. This will force the next call to update_stmt_operands to scan
365 return ann ? ann->modified : true;
368 /* Delink an immediate_uses node from its chain. */
370 delink_imm_use (ssa_use_operand_t *linknode)
372 /* Return if this node is not in a list. */
373 if (linknode->prev == NULL)
376 linknode->prev->next = linknode->next;
377 linknode->next->prev = linknode->prev;
378 linknode->prev = NULL;
379 linknode->next = NULL;
382 /* Link ssa_imm_use node LINKNODE into the chain for LIST. */
384 link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
386 /* Link the new node at the head of the list. If we are in the process of
387 traversing the list, we won't visit any new nodes added to it. */
388 linknode->prev = list;
389 linknode->next = list->next;
390 list->next->prev = linknode;
391 list->next = linknode;
394 /* Link ssa_imm_use node LINKNODE into the chain for DEF. */
396 link_imm_use (ssa_use_operand_t *linknode, tree def)
398 ssa_use_operand_t *root;
400 if (!def || TREE_CODE (def) != SSA_NAME)
401 linknode->prev = NULL;
404 root = &(SSA_NAME_IMM_USE_NODE (def));
405 #ifdef ENABLE_CHECKING
407 gcc_assert (*(linknode->use) == def);
409 link_imm_use_to_list (linknode, root);
413 /* Set the value of a use pointed to by USE to VAL. */
415 set_ssa_use_from_ptr (use_operand_p use, tree val)
417 delink_imm_use (use);
419 link_imm_use (use, val);
422 /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
425 link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, tree stmt)
428 link_imm_use (linknode, def);
430 link_imm_use (linknode, NULL);
431 linknode->stmt = stmt;
434 /* Relink a new node in place of an old node in the list. */
436 relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
438 /* The node one had better be in the same list. */
439 gcc_assert (*(old->use) == *(node->use));
440 node->prev = old->prev;
441 node->next = old->next;
444 old->prev->next = node;
445 old->next->prev = node;
446 /* Remove the old node from the list. */
451 /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
454 relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old, tree stmt)
457 relink_imm_use (linknode, old);
459 link_imm_use (linknode, NULL);
460 linknode->stmt = stmt;
464 /* Return true is IMM has reached the end of the immediate use list. */
466 end_readonly_imm_use_p (const imm_use_iterator *imm)
468 return (imm->imm_use == imm->end_p);
471 /* Initialize iterator IMM to process the list for VAR. */
472 static inline use_operand_p
473 first_readonly_imm_use (imm_use_iterator *imm, tree var)
475 gcc_assert (TREE_CODE (var) == SSA_NAME);
477 imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
478 imm->imm_use = imm->end_p->next;
479 #ifdef ENABLE_CHECKING
480 imm->iter_node.next = imm->imm_use->next;
482 if (end_readonly_imm_use_p (imm))
483 return NULL_USE_OPERAND_P;
487 /* Bump IMM to the next use in the list. */
488 static inline use_operand_p
489 next_readonly_imm_use (imm_use_iterator *imm)
491 use_operand_p old = imm->imm_use;
493 #ifdef ENABLE_CHECKING
494 /* If this assertion fails, it indicates the 'next' pointer has changed
495 since the last bump. This indicates that the list is being modified
496 via stmt changes, or SET_USE, or somesuch thing, and you need to be
497 using the SAFE version of the iterator. */
498 gcc_assert (imm->iter_node.next == old->next);
499 imm->iter_node.next = old->next->next;
502 imm->imm_use = old->next;
503 if (end_readonly_imm_use_p (imm))
508 /* Return true if VAR has no uses. */
510 has_zero_uses (const_tree var)
512 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
513 /* A single use means there is no items in the list. */
514 return (ptr == ptr->next);
517 /* Return true if VAR has a single use. */
519 has_single_use (const_tree var)
521 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
522 /* A single use means there is one item in the list. */
523 return (ptr != ptr->next && ptr == ptr->next->next);
527 /* If VAR has only a single immediate use, return true, and set USE_P and STMT
528 to the use pointer and stmt of occurrence. */
530 single_imm_use (const_tree var, use_operand_p *use_p, tree *stmt)
532 const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
533 if (ptr != ptr->next && ptr == ptr->next->next)
536 *stmt = ptr->next->stmt;
539 *use_p = NULL_USE_OPERAND_P;
544 /* Return the number of immediate uses of VAR. */
545 static inline unsigned int
546 num_imm_uses (const_tree var)
548 const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var));
549 const ssa_use_operand_t *ptr;
550 unsigned int num = 0;
552 for (ptr = start->next; ptr != start; ptr = ptr->next)
558 /* Return the tree pointer to by USE. */
560 get_use_from_ptr (use_operand_p use)
565 /* Return the tree pointer to by DEF. */
567 get_def_from_ptr (def_operand_p def)
572 /* Return a def_operand_p pointer for the result of PHI. */
573 static inline def_operand_p
574 get_phi_result_ptr (tree phi)
576 return &(PHI_RESULT_TREE (phi));
579 /* Return a use_operand_p pointer for argument I of phinode PHI. */
580 static inline use_operand_p
581 get_phi_arg_def_ptr (tree phi, int i)
583 return &(PHI_ARG_IMM_USE_NODE (phi,i));
587 /* Return the bitmap of addresses taken by STMT, or NULL if it takes
590 addresses_taken (tree stmt)
592 stmt_ann_t ann = stmt_ann (stmt);
593 return ann ? ann->addresses_taken : NULL;
596 /* Return the PHI nodes for basic block BB, or NULL if there are no
599 phi_nodes (const_basic_block bb)
601 gcc_assert (!(bb->flags & BB_RTL));
604 return bb->il.tree->phi_nodes;
607 /* Return pointer to the list of PHI nodes for basic block BB. */
610 phi_nodes_ptr (basic_block bb)
612 gcc_assert (!(bb->flags & BB_RTL));
613 return &bb->il.tree->phi_nodes;
616 /* Set list of phi nodes of a basic block BB to L. */
619 set_phi_nodes (basic_block bb, tree l)
623 gcc_assert (!(bb->flags & BB_RTL));
624 bb->il.tree->phi_nodes = l;
625 for (phi = l; phi; phi = PHI_CHAIN (phi))
626 set_bb_for_stmt (phi, bb);
629 /* Return the phi argument which contains the specified use. */
632 phi_arg_index_from_use (use_operand_p use)
634 struct phi_arg_d *element, *root;
638 /* Since the use is the first thing in a PHI argument element, we can
639 calculate its index based on casting it to an argument, and performing
640 pointer arithmetic. */
642 phi = USE_STMT (use);
643 gcc_assert (TREE_CODE (phi) == PHI_NODE);
645 element = (struct phi_arg_d *)use;
646 root = &(PHI_ARG_ELT (phi, 0));
647 index = element - root;
649 #ifdef ENABLE_CHECKING
650 /* Make sure the calculation doesn't have any leftover bytes. If it does,
651 then imm_use is likely not the first element in phi_arg_d. */
653 (((char *)element - (char *)root) % sizeof (struct phi_arg_d)) == 0);
654 gcc_assert (index >= 0 && index < PHI_ARG_CAPACITY (phi));
660 /* Mark VAR as used, so that it'll be preserved during rtl expansion. */
663 set_is_used (tree var)
665 var_ann_t ann = get_var_ann (var);
670 /* Return true if T (assumed to be a DECL) is a global variable. */
673 is_global_var (const_tree t)
676 return (TREE_STATIC (t) || MTAG_GLOBAL (t));
678 return (TREE_STATIC (t) || DECL_EXTERNAL (t));
681 /* PHI nodes should contain only ssa_names and invariants. A test
682 for ssa_name is definitely simpler; don't let invalid contents
683 slip in in the meantime. */
686 phi_ssa_name_p (const_tree t)
688 if (TREE_CODE (t) == SSA_NAME)
690 #ifdef ENABLE_CHECKING
691 gcc_assert (is_gimple_min_invariant (t));
696 /* ----------------------------------------------------------------------- */
698 /* Returns the list of statements in BB. */
701 bb_stmt_list (const_basic_block bb)
703 gcc_assert (!(bb->flags & BB_RTL));
704 return bb->il.tree->stmt_list;
707 /* Sets the list of statements in BB to LIST. */
710 set_bb_stmt_list (basic_block bb, tree list)
712 gcc_assert (!(bb->flags & BB_RTL));
713 bb->il.tree->stmt_list = list;
716 /* Return a block_stmt_iterator that points to beginning of basic
718 static inline block_stmt_iterator
719 bsi_start (basic_block bb)
721 block_stmt_iterator bsi;
722 if (bb->index < NUM_FIXED_BLOCKS)
725 bsi.tsi.container = NULL;
728 bsi.tsi = tsi_start (bb_stmt_list (bb));
733 /* Return a block statement iterator that points to the first non-label
734 statement in block BB. */
736 static inline block_stmt_iterator
737 bsi_after_labels (basic_block bb)
739 block_stmt_iterator bsi = bsi_start (bb);
741 while (!bsi_end_p (bsi) && TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
747 /* Return a block statement iterator that points to the end of basic
749 static inline block_stmt_iterator
750 bsi_last (basic_block bb)
752 block_stmt_iterator bsi;
754 if (bb->index < NUM_FIXED_BLOCKS)
757 bsi.tsi.container = NULL;
760 bsi.tsi = tsi_last (bb_stmt_list (bb));
765 /* Return true if block statement iterator I has reached the end of
768 bsi_end_p (block_stmt_iterator i)
770 return tsi_end_p (i.tsi);
773 /* Modify block statement iterator I so that it is at the next
774 statement in the basic block. */
776 bsi_next (block_stmt_iterator *i)
781 /* Modify block statement iterator I so that it is at the previous
782 statement in the basic block. */
784 bsi_prev (block_stmt_iterator *i)
789 /* Return the statement that block statement iterator I is currently
792 bsi_stmt (block_stmt_iterator i)
794 return tsi_stmt (i.tsi);
797 /* Return a pointer to the statement that block statement iterator I
800 bsi_stmt_ptr (block_stmt_iterator i)
802 return tsi_stmt_ptr (i.tsi);
805 /* Returns the loop of the statement STMT. */
807 static inline struct loop *
808 loop_containing_stmt (tree stmt)
810 basic_block bb = bb_for_stmt (stmt);
814 return bb->loop_father;
818 /* Return the memory partition tag associated with symbol SYM. */
821 memory_partition (tree sym)
825 /* MPTs belong to their own partition. */
826 if (TREE_CODE (sym) == MEMORY_PARTITION_TAG)
829 gcc_assert (!is_gimple_reg (sym));
830 tag = get_var_ann (sym)->mpt;
832 #if defined ENABLE_CHECKING
834 gcc_assert (TREE_CODE (tag) == MEMORY_PARTITION_TAG);
840 /* Return true if NAME is a memory factoring SSA name (i.e., an SSA
841 name for a memory partition. */
844 factoring_name_p (const_tree name)
846 return TREE_CODE (SSA_NAME_VAR (name)) == MEMORY_PARTITION_TAG;
849 /* Return true if VAR is a clobbered by function calls. */
851 is_call_clobbered (const_tree var)
854 return var_ann (var)->call_clobbered;
856 return bitmap_bit_p (gimple_call_clobbered_vars (cfun), DECL_UID (var));
859 /* Mark variable VAR as being clobbered by function calls. */
861 mark_call_clobbered (tree var, unsigned int escape_type)
863 var_ann (var)->escape_mask |= escape_type;
865 var_ann (var)->call_clobbered = true;
866 bitmap_set_bit (gimple_call_clobbered_vars (cfun), DECL_UID (var));
869 /* Clear the call-clobbered attribute from variable VAR. */
871 clear_call_clobbered (tree var)
873 var_ann_t ann = var_ann (var);
874 ann->escape_mask = 0;
875 if (MTAG_P (var) && TREE_CODE (var) != STRUCT_FIELD_TAG)
876 MTAG_GLOBAL (var) = 0;
878 var_ann (var)->call_clobbered = false;
879 bitmap_clear_bit (gimple_call_clobbered_vars (cfun), DECL_UID (var));
882 /* Return the common annotation for T. Return NULL if the annotation
883 doesn't already exist. */
884 static inline tree_ann_common_t
885 tree_common_ann (const_tree t)
887 /* Watch out static variables with unshared annotations. */
888 if (DECL_P (t) && TREE_CODE (t) == VAR_DECL)
889 return &var_ann (t)->common;
890 return &t->base.ann->common;
893 /* Return a common annotation for T. Create the constant annotation if it
895 static inline tree_ann_common_t
896 get_tree_common_ann (tree t)
898 tree_ann_common_t ann = tree_common_ann (t);
899 return (ann) ? ann : create_tree_common_ann (t);
902 /* ----------------------------------------------------------------------- */
904 /* The following set of routines are used to iterator over various type of
907 /* Return true if PTR is finished iterating. */
909 op_iter_done (const ssa_op_iter *ptr)
914 /* Get the next iterator use value for PTR. */
915 static inline use_operand_p
916 op_iter_next_use (ssa_op_iter *ptr)
919 #ifdef ENABLE_CHECKING
920 gcc_assert (ptr->iter_type == ssa_op_iter_use);
924 use_p = USE_OP_PTR (ptr->uses);
925 ptr->uses = ptr->uses->next;
930 use_p = VUSE_OP_PTR (ptr->vuses, ptr->vuse_index);
931 if (++(ptr->vuse_index) >= VUSE_NUM (ptr->vuses))
934 ptr->vuses = ptr->vuses->next;
940 use_p = VDEF_OP_PTR (ptr->mayuses, ptr->mayuse_index);
941 if (++(ptr->mayuse_index) >= VDEF_NUM (ptr->mayuses))
943 ptr->mayuse_index = 0;
944 ptr->mayuses = ptr->mayuses->next;
948 if (ptr->phi_i < ptr->num_phi)
950 return PHI_ARG_DEF_PTR (ptr->phi_stmt, (ptr->phi_i)++);
953 return NULL_USE_OPERAND_P;
956 /* Get the next iterator def value for PTR. */
957 static inline def_operand_p
958 op_iter_next_def (ssa_op_iter *ptr)
961 #ifdef ENABLE_CHECKING
962 gcc_assert (ptr->iter_type == ssa_op_iter_def);
966 def_p = DEF_OP_PTR (ptr->defs);
967 ptr->defs = ptr->defs->next;
972 def_p = VDEF_RESULT_PTR (ptr->vdefs);
973 ptr->vdefs = ptr->vdefs->next;
977 return NULL_DEF_OPERAND_P;
980 /* Get the next iterator tree value for PTR. */
982 op_iter_next_tree (ssa_op_iter *ptr)
985 #ifdef ENABLE_CHECKING
986 gcc_assert (ptr->iter_type == ssa_op_iter_tree);
990 val = USE_OP (ptr->uses);
991 ptr->uses = ptr->uses->next;
996 val = VUSE_OP (ptr->vuses, ptr->vuse_index);
997 if (++(ptr->vuse_index) >= VUSE_NUM (ptr->vuses))
1000 ptr->vuses = ptr->vuses->next;
1006 val = VDEF_OP (ptr->mayuses, ptr->mayuse_index);
1007 if (++(ptr->mayuse_index) >= VDEF_NUM (ptr->mayuses))
1009 ptr->mayuse_index = 0;
1010 ptr->mayuses = ptr->mayuses->next;
1016 val = DEF_OP (ptr->defs);
1017 ptr->defs = ptr->defs->next;
1022 val = VDEF_RESULT (ptr->vdefs);
1023 ptr->vdefs = ptr->vdefs->next;
1033 /* This functions clears the iterator PTR, and marks it done. This is normally
1034 used to prevent warnings in the compile about might be uninitialized
1038 clear_and_done_ssa_iter (ssa_op_iter *ptr)
1044 ptr->mayuses = NULL;
1045 ptr->iter_type = ssa_op_iter_none;
1048 ptr->phi_stmt = NULL_TREE;
1050 ptr->vuse_index = 0;
1051 ptr->mayuse_index = 0;
1054 /* Initialize the iterator PTR to the virtual defs in STMT. */
1056 op_iter_init (ssa_op_iter *ptr, tree stmt, int flags)
1058 #ifdef ENABLE_CHECKING
1059 gcc_assert (stmt_ann (stmt));
1062 ptr->defs = (flags & SSA_OP_DEF) ? DEF_OPS (stmt) : NULL;
1063 ptr->uses = (flags & SSA_OP_USE) ? USE_OPS (stmt) : NULL;
1064 ptr->vuses = (flags & SSA_OP_VUSE) ? VUSE_OPS (stmt) : NULL;
1065 ptr->vdefs = (flags & SSA_OP_VDEF) ? VDEF_OPS (stmt) : NULL;
1066 ptr->mayuses = (flags & SSA_OP_VMAYUSE) ? VDEF_OPS (stmt) : NULL;
1071 ptr->phi_stmt = NULL_TREE;
1072 ptr->vuse_index = 0;
1073 ptr->mayuse_index = 0;
1076 /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
1078 static inline use_operand_p
1079 op_iter_init_use (ssa_op_iter *ptr, tree stmt, int flags)
1081 gcc_assert ((flags & SSA_OP_ALL_DEFS) == 0);
1082 op_iter_init (ptr, stmt, flags);
1083 ptr->iter_type = ssa_op_iter_use;
1084 return op_iter_next_use (ptr);
1087 /* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
1089 static inline def_operand_p
1090 op_iter_init_def (ssa_op_iter *ptr, tree stmt, int flags)
1092 gcc_assert ((flags & SSA_OP_ALL_USES) == 0);
1093 op_iter_init (ptr, stmt, flags);
1094 ptr->iter_type = ssa_op_iter_def;
1095 return op_iter_next_def (ptr);
1098 /* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
1099 the first operand as a tree. */
1101 op_iter_init_tree (ssa_op_iter *ptr, tree stmt, int flags)
1103 op_iter_init (ptr, stmt, flags);
1104 ptr->iter_type = ssa_op_iter_tree;
1105 return op_iter_next_tree (ptr);
1108 /* Get the next iterator mustdef value for PTR, returning the mustdef values in
1111 op_iter_next_vdef (vuse_vec_p *use, def_operand_p *def,
1114 #ifdef ENABLE_CHECKING
1115 gcc_assert (ptr->iter_type == ssa_op_iter_vdef);
1119 *def = VDEF_RESULT_PTR (ptr->mayuses);
1120 *use = VDEF_VECT (ptr->mayuses);
1121 ptr->mayuses = ptr->mayuses->next;
1125 *def = NULL_DEF_OPERAND_P;
1133 op_iter_next_mustdef (use_operand_p *use, def_operand_p *def,
1137 op_iter_next_vdef (&vp, def, ptr);
1140 gcc_assert (VUSE_VECT_NUM_ELEM (*vp) == 1);
1141 *use = VUSE_ELEMENT_PTR (*vp, 0);
1144 *use = NULL_USE_OPERAND_P;
1147 /* Initialize iterator PTR to the operands in STMT. Return the first operands
1150 op_iter_init_vdef (ssa_op_iter *ptr, tree stmt, vuse_vec_p *use,
1153 gcc_assert (TREE_CODE (stmt) != PHI_NODE);
1155 op_iter_init (ptr, stmt, SSA_OP_VMAYUSE);
1156 ptr->iter_type = ssa_op_iter_vdef;
1157 op_iter_next_vdef (use, def, ptr);
1161 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
1164 single_ssa_tree_operand (tree stmt, int flags)
1169 var = op_iter_init_tree (&iter, stmt, flags);
1170 if (op_iter_done (&iter))
1172 op_iter_next_tree (&iter);
1173 if (op_iter_done (&iter))
1179 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
1181 static inline use_operand_p
1182 single_ssa_use_operand (tree stmt, int flags)
1187 var = op_iter_init_use (&iter, stmt, flags);
1188 if (op_iter_done (&iter))
1189 return NULL_USE_OPERAND_P;
1190 op_iter_next_use (&iter);
1191 if (op_iter_done (&iter))
1193 return NULL_USE_OPERAND_P;
1198 /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
1200 static inline def_operand_p
1201 single_ssa_def_operand (tree stmt, int flags)
1206 var = op_iter_init_def (&iter, stmt, flags);
1207 if (op_iter_done (&iter))
1208 return NULL_DEF_OPERAND_P;
1209 op_iter_next_def (&iter);
1210 if (op_iter_done (&iter))
1212 return NULL_DEF_OPERAND_P;
1216 /* Return true if there are zero operands in STMT matching the type
1219 zero_ssa_operands (tree stmt, int flags)
1223 op_iter_init_tree (&iter, stmt, flags);
1224 return op_iter_done (&iter);
1228 /* Return the number of operands matching FLAGS in STMT. */
1230 num_ssa_operands (tree stmt, int flags)
1236 FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
1242 /* Delink all immediate_use information for STMT. */
1244 delink_stmt_imm_use (tree stmt)
1247 use_operand_p use_p;
1249 if (ssa_operands_active ())
1250 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1251 delink_imm_use (use_p);
1255 /* This routine will compare all the operands matching FLAGS in STMT1 to those
1256 in STMT2. TRUE is returned if they are the same. STMTs can be NULL. */
1258 compare_ssa_operands_equal (tree stmt1, tree stmt2, int flags)
1260 ssa_op_iter iter1, iter2;
1261 tree op1 = NULL_TREE;
1262 tree op2 = NULL_TREE;
1268 look1 = stmt1 && stmt_ann (stmt1);
1269 look2 = stmt2 && stmt_ann (stmt2);
1273 op1 = op_iter_init_tree (&iter1, stmt1, flags);
1275 return op_iter_done (&iter1);
1278 clear_and_done_ssa_iter (&iter1);
1282 op2 = op_iter_init_tree (&iter2, stmt2, flags);
1284 return op_iter_done (&iter2);
1287 clear_and_done_ssa_iter (&iter2);
1289 while (!op_iter_done (&iter1) && !op_iter_done (&iter2))
1293 op1 = op_iter_next_tree (&iter1);
1294 op2 = op_iter_next_tree (&iter2);
1297 return (op_iter_done (&iter1) && op_iter_done (&iter2));
1301 /* If there is a single DEF in the PHI node which matches FLAG, return it.
1302 Otherwise return NULL_DEF_OPERAND_P. */
1304 single_phi_def (tree stmt, int flags)
1306 tree def = PHI_RESULT (stmt);
1307 if ((flags & SSA_OP_DEF) && is_gimple_reg (def))
1309 if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def))
1314 /* Initialize the iterator PTR for uses matching FLAGS in PHI. FLAGS should
1315 be either SSA_OP_USES or SSA_OP_VIRTUAL_USES. */
1316 static inline use_operand_p
1317 op_iter_init_phiuse (ssa_op_iter *ptr, tree phi, int flags)
1319 tree phi_def = PHI_RESULT (phi);
1322 clear_and_done_ssa_iter (ptr);
1325 gcc_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
1327 comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
1329 /* If the PHI node doesn't the operand type we care about, we're done. */
1330 if ((flags & comp) == 0)
1333 return NULL_USE_OPERAND_P;
1336 ptr->phi_stmt = phi;
1337 ptr->num_phi = PHI_NUM_ARGS (phi);
1338 ptr->iter_type = ssa_op_iter_use;
1339 return op_iter_next_use (ptr);
1343 /* Start an iterator for a PHI definition. */
1345 static inline def_operand_p
1346 op_iter_init_phidef (ssa_op_iter *ptr, tree phi, int flags)
1348 tree phi_def = PHI_RESULT (phi);
1351 clear_and_done_ssa_iter (ptr);
1354 gcc_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
1356 comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
1358 /* If the PHI node doesn't the operand type we care about, we're done. */
1359 if ((flags & comp) == 0)
1362 return NULL_USE_OPERAND_P;
1365 ptr->iter_type = ssa_op_iter_def;
1366 /* The first call to op_iter_next_def will terminate the iterator since
1367 all the fields are NULL. Simply return the result here as the first and
1368 therefore only result. */
1369 return PHI_RESULT_PTR (phi);
1372 /* Return true is IMM has reached the end of the immediate use stmt list. */
1375 end_imm_use_stmt_p (const imm_use_iterator *imm)
1377 return (imm->imm_use == imm->end_p);
1380 /* Finished the traverse of an immediate use stmt list IMM by removing the
1381 placeholder node from the list. */
1384 end_imm_use_stmt_traverse (imm_use_iterator *imm)
1386 delink_imm_use (&(imm->iter_node));
1389 /* Immediate use traversal of uses within a stmt require that all the
1390 uses on a stmt be sequentially listed. This routine is used to build up
1391 this sequential list by adding USE_P to the end of the current list
1392 currently delimited by HEAD and LAST_P. The new LAST_P value is
1395 static inline use_operand_p
1396 move_use_after_head (use_operand_p use_p, use_operand_p head,
1397 use_operand_p last_p)
1399 gcc_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head));
1400 /* Skip head when we find it. */
1403 /* If use_p is already linked in after last_p, continue. */
1404 if (last_p->next == use_p)
1408 /* Delink from current location, and link in at last_p. */
1409 delink_imm_use (use_p);
1410 link_imm_use_to_list (use_p, last_p);
1418 /* This routine will relink all uses with the same stmt as HEAD into the list
1419 immediately following HEAD for iterator IMM. */
1422 link_use_stmts_after (use_operand_p head, imm_use_iterator *imm)
1424 use_operand_p use_p;
1425 use_operand_p last_p = head;
1426 tree head_stmt = USE_STMT (head);
1427 tree use = USE_FROM_PTR (head);
1428 ssa_op_iter op_iter;
1431 /* Only look at virtual or real uses, depending on the type of HEAD. */
1432 flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
1434 if (TREE_CODE (head_stmt) == PHI_NODE)
1436 FOR_EACH_PHI_ARG (use_p, head_stmt, op_iter, flag)
1437 if (USE_FROM_PTR (use_p) == use)
1438 last_p = move_use_after_head (use_p, head, last_p);
1442 FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag)
1443 if (USE_FROM_PTR (use_p) == use)
1444 last_p = move_use_after_head (use_p, head, last_p);
1446 /* LInk iter node in after last_p. */
1447 if (imm->iter_node.prev != NULL)
1448 delink_imm_use (&imm->iter_node);
1449 link_imm_use_to_list (&(imm->iter_node), last_p);
1452 /* Initialize IMM to traverse over uses of VAR. Return the first statement. */
1454 first_imm_use_stmt (imm_use_iterator *imm, tree var)
1456 gcc_assert (TREE_CODE (var) == SSA_NAME);
1458 imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
1459 imm->imm_use = imm->end_p->next;
1460 imm->next_imm_name = NULL_USE_OPERAND_P;
1462 /* iter_node is used as a marker within the immediate use list to indicate
1463 where the end of the current stmt's uses are. Initialize it to NULL
1464 stmt and use, which indicates a marker node. */
1465 imm->iter_node.prev = NULL_USE_OPERAND_P;
1466 imm->iter_node.next = NULL_USE_OPERAND_P;
1467 imm->iter_node.stmt = NULL_TREE;
1468 imm->iter_node.use = NULL_USE_OPERAND_P;
1470 if (end_imm_use_stmt_p (imm))
1473 link_use_stmts_after (imm->imm_use, imm);
1475 return USE_STMT (imm->imm_use);
1478 /* Bump IMM to the next stmt which has a use of var. */
1481 next_imm_use_stmt (imm_use_iterator *imm)
1483 imm->imm_use = imm->iter_node.next;
1484 if (end_imm_use_stmt_p (imm))
1486 if (imm->iter_node.prev != NULL)
1487 delink_imm_use (&imm->iter_node);
1491 link_use_stmts_after (imm->imm_use, imm);
1492 return USE_STMT (imm->imm_use);
1495 /* This routine will return the first use on the stmt IMM currently refers
1498 static inline use_operand_p
1499 first_imm_use_on_stmt (imm_use_iterator *imm)
1501 imm->next_imm_name = imm->imm_use->next;
1502 return imm->imm_use;
1505 /* Return TRUE if the last use on the stmt IMM refers to has been visited. */
1508 end_imm_use_on_stmt_p (const imm_use_iterator *imm)
1510 return (imm->imm_use == &(imm->iter_node));
1513 /* Bump to the next use on the stmt IMM refers to, return NULL if done. */
1515 static inline use_operand_p
1516 next_imm_use_on_stmt (imm_use_iterator *imm)
1518 imm->imm_use = imm->next_imm_name;
1519 if (end_imm_use_on_stmt_p (imm))
1520 return NULL_USE_OPERAND_P;
1523 imm->next_imm_name = imm->imm_use->next;
1524 return imm->imm_use;
1528 /* Return true if VAR cannot be modified by the program. */
1531 unmodifiable_var_p (const_tree var)
1533 if (TREE_CODE (var) == SSA_NAME)
1534 var = SSA_NAME_VAR (var);
1537 return TREE_READONLY (var) && (TREE_STATIC (var) || MTAG_GLOBAL (var));
1539 return TREE_READONLY (var) && (TREE_STATIC (var) || DECL_EXTERNAL (var));
1542 /* Return true if REF, an ARRAY_REF, has an INDIRECT_REF somewhere in it. */
1545 array_ref_contains_indirect_ref (const_tree ref)
1547 gcc_assert (TREE_CODE (ref) == ARRAY_REF);
1550 ref = TREE_OPERAND (ref, 0);
1551 } while (handled_component_p (ref));
1553 return TREE_CODE (ref) == INDIRECT_REF;
1556 /* Return true if REF, a handled component reference, has an ARRAY_REF
1560 ref_contains_array_ref (const_tree ref)
1562 gcc_assert (handled_component_p (ref));
1565 if (TREE_CODE (ref) == ARRAY_REF)
1567 ref = TREE_OPERAND (ref, 0);
1568 } while (handled_component_p (ref));
1573 /* Given a variable VAR, lookup and return a pointer to the list of
1574 subvariables for it. */
1576 static inline subvar_t *
1577 lookup_subvars_for_var (const_tree var)
1579 var_ann_t ann = var_ann (var);
1581 return &ann->subvars;
1584 /* Given a variable VAR, return a linked list of subvariables for VAR, or
1585 NULL, if there are no subvariables. */
1587 static inline subvar_t
1588 get_subvars_for_var (tree var)
1592 gcc_assert (SSA_VAR_P (var));
1594 if (TREE_CODE (var) == SSA_NAME)
1595 subvars = *(lookup_subvars_for_var (SSA_NAME_VAR (var)));
1597 subvars = *(lookup_subvars_for_var (var));
1601 /* Return the subvariable of VAR at offset OFFSET. */
1604 get_subvar_at (tree var, unsigned HOST_WIDE_INT offset)
1606 subvar_t sv = get_subvars_for_var (var);
1610 high = VEC_length (tree, sv) - 1;
1613 int mid = (low + high) / 2;
1614 tree subvar = VEC_index (tree, sv, mid);
1615 if (SFT_OFFSET (subvar) == offset)
1617 else if (SFT_OFFSET (subvar) < offset)
1627 /* Return the first subvariable in SV that overlaps [offset, offset + size[.
1628 NULL_TREE is returned, if there is no overlapping subvariable, else *I
1629 is set to the index in the SV vector of the first overlap. */
1632 get_first_overlapping_subvar (subvar_t sv, unsigned HOST_WIDE_INT offset,
1633 unsigned HOST_WIDE_INT size, unsigned int *i)
1636 int high = VEC_length (tree, sv) - 1;
1643 /* Binary search for offset. */
1646 mid = (low + high) / 2;
1647 subvar = VEC_index (tree, sv, mid);
1648 if (SFT_OFFSET (subvar) == offset)
1653 else if (SFT_OFFSET (subvar) < offset)
1658 while (low <= high);
1660 /* As we didn't find a subvar with offset, adjust to return the
1661 first overlapping one. */
1662 if (SFT_OFFSET (subvar) < offset
1663 && SFT_OFFSET (subvar) + SFT_SIZE (subvar) <= offset)
1666 if ((unsigned)mid >= VEC_length (tree, sv))
1668 subvar = VEC_index (tree, sv, mid);
1670 else if (SFT_OFFSET (subvar) > offset
1671 && size <= SFT_OFFSET (subvar) - offset)
1676 subvar = VEC_index (tree, sv, mid);
1679 if (overlap_subvar (offset, size, subvar, NULL))
1689 /* Return true if V is a tree that we can have subvars for.
1690 Normally, this is any aggregate type. Also complex
1691 types which are not gimple registers can have subvars. */
1694 var_can_have_subvars (const_tree v)
1696 /* Volatile variables should never have subvars. */
1697 if (TREE_THIS_VOLATILE (v))
1700 /* Non decls or memory tags can never have subvars. */
1701 if (!DECL_P (v) || MTAG_P (v))
1704 /* Aggregates can have subvars. */
1705 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
1708 /* Complex types variables which are not also a gimple register can
1710 if (TREE_CODE (TREE_TYPE (v)) == COMPLEX_TYPE
1711 && !DECL_GIMPLE_REG_P (v))
1718 /* Return true if OFFSET and SIZE define a range that overlaps with some
1719 portion of the range of SV, a subvar. If there was an exact overlap,
1720 *EXACT will be set to true upon return. */
1723 overlap_subvar (unsigned HOST_WIDE_INT offset, unsigned HOST_WIDE_INT size,
1724 const_tree sv, bool *exact)
1726 /* There are three possible cases of overlap.
1727 1. We can have an exact overlap, like so:
1728 |offset, offset + size |
1729 |sv->offset, sv->offset + sv->size |
1731 2. We can have offset starting after sv->offset, like so:
1733 |offset, offset + size |
1734 |sv->offset, sv->offset + sv->size |
1736 3. We can have offset starting before sv->offset, like so:
1738 |offset, offset + size |
1739 |sv->offset, sv->offset + sv->size|
1744 if (offset == SFT_OFFSET (sv) && size == SFT_SIZE (sv))
1750 else if (offset >= SFT_OFFSET (sv)
1751 && offset < (SFT_OFFSET (sv) + SFT_SIZE (sv)))
1755 else if (offset < SFT_OFFSET (sv)
1756 && (size > SFT_OFFSET (sv) - offset))
1764 /* Return the memory tag associated with symbol SYM. */
1767 symbol_mem_tag (tree sym)
1769 tree tag = get_var_ann (sym)->symbol_mem_tag;
1771 #if defined ENABLE_CHECKING
1773 gcc_assert (TREE_CODE (tag) == SYMBOL_MEMORY_TAG);
1780 /* Set the memory tag associated with symbol SYM. */
1783 set_symbol_mem_tag (tree sym, tree tag)
1785 #if defined ENABLE_CHECKING
1787 gcc_assert (TREE_CODE (tag) == SYMBOL_MEMORY_TAG);
1790 get_var_ann (sym)->symbol_mem_tag = tag;
1793 /* Get the value handle of EXPR. This is the only correct way to get
1794 the value handle for a "thing". If EXPR does not have a value
1795 handle associated, it returns NULL_TREE.
1796 NB: If EXPR is min_invariant, this function is *required* to return
1800 get_value_handle (tree expr)
1802 if (TREE_CODE (expr) == SSA_NAME)
1803 return SSA_NAME_VALUE (expr);
1804 else if (DECL_P (expr) || TREE_CODE (expr) == TREE_LIST
1805 || TREE_CODE (expr) == CONSTRUCTOR)
1807 tree_ann_common_t ann = tree_common_ann (expr);
1808 return ((ann) ? ann->value_handle : NULL_TREE);
1810 else if (is_gimple_min_invariant (expr))
1812 else if (EXPR_P (expr))
1814 tree_ann_common_t ann = tree_common_ann (expr);
1815 return ((ann) ? ann->value_handle : NULL_TREE);
1821 /* Accessor to tree-ssa-operands.c caches. */
1822 static inline struct ssa_operands *
1823 gimple_ssa_operands (const struct function *fun)
1825 return &fun->gimple_df->ssa_operands;
1828 /* Map describing reference statistics for function FN. */
1829 static inline struct mem_ref_stats_d *
1830 gimple_mem_ref_stats (const struct function *fn)
1832 return &fn->gimple_df->mem_ref_stats;
1834 #endif /* _TREE_FLOW_INLINE_H */