1 /* Data flow functions for trees.
2 Copyright (C) 2001, 2002 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
39 #include "diagnostic.h"
40 #include "tree-simple.h"
41 #include "tree-flow.h"
42 #include "tree-inline.h"
43 #include "tree-alias-common.h"
46 /* Build and maintain data flow information for trees. */
48 /* Counters used to display DFA and SSA statistics. */
67 voperands_t prev_vops
;
70 /* Alias sets for type-based alias computation. Each entry in the array
71 ALIAS_SETS represents a unique tag that represents a group of variables
72 with conflicting alias types. This is only used when points-to analysis
73 is disabled (-ftree-points-to). */
79 HOST_WIDE_INT tag_set
;
80 HOST_WIDE_INT tag_sym_set
;
83 static varray_type alias_sets
;
86 /* Data and functions shared with tree-ssa.c. */
87 struct dfa_stats_d dfa_stats
;
88 extern FILE *tree_ssa_dump_file
;
89 extern int tree_ssa_dump_flags
;
92 /* Local functions. */
93 static void get_expr_operands
PARAMS ((tree
, tree
*, int,
95 static void collect_dfa_stats
PARAMS ((struct dfa_stats_d
*));
96 static tree collect_dfa_stats_r
PARAMS ((tree
*, int *, void *));
97 static tree clobber_vars_r
PARAMS ((tree
*, int *, void *));
98 static void compute_alias_sets
PARAMS ((void));
99 static void register_alias_set
PARAMS ((tree
, tree
));
100 static void find_alias_for
PARAMS ((tree
, tree
));
101 static bool may_alias_p
PARAMS ((tree
, tree
, HOST_WIDE_INT
,
102 tree
, tree
, HOST_WIDE_INT
));
103 static void find_may_aliases_for
PARAMS ((int));
104 static bool may_access_global_mem_p
PARAMS ((tree
, tree
));
105 static void set_def
PARAMS ((tree
*, tree
));
106 static void add_use
PARAMS ((tree
*, tree
));
107 static void add_vdef
PARAMS ((tree
, tree
, voperands_t
));
108 static void add_vuse
PARAMS ((tree
, tree
, voperands_t
));
109 static void add_stmt_operand
PARAMS ((tree
*, tree
, int, int,
111 static void add_immediate_use
PARAMS ((tree
, tree
));
112 static tree find_vars_r
PARAMS ((tree
*, int *, void *));
113 static void add_referenced_var
PARAMS ((tree
, tree
, void *));
114 static void add_indirect_ref_var
PARAMS ((tree
, void *));
115 static void compute_immediate_uses_for
PARAMS ((tree
, int));
116 static void add_may_alias
PARAMS ((tree
, tree
, tree
, tree
));
117 static bool call_may_clobber
PARAMS ((tree
));
118 static void find_vla_decls
PARAMS ((tree
));
119 static tree find_vla_decls_r
PARAMS ((tree
*, int *, void *));
122 /* Global declarations. */
124 /* The total number of referenced variables in the function. */
125 unsigned long num_referenced_vars
;
127 /* Array of all variables referenced in the function. */
128 varray_type referenced_vars
;
130 /* The total number of unique INDIRECT_REFs in the function. */
131 static unsigned long num_indirect_refs
;
133 /* Arrays for all the unique INDIRECT_REFs in the function.
135 INDIRECT_REFS contains the canonical INDIRECT_REFs
136 INDIRECT_REFS_BASE contains the base symbol for those refs
137 INDIRECT_REFS_ALIAS_SET contains the alias set for this INDIRECT_REF. */
139 static varray_type indirect_refs
;
140 static varray_type indirect_refs_base
;
141 static varray_type indirect_refs_alias_set
;
143 /* The total number of unique addressable vars in the function. */
144 static unsigned long num_addressable_vars
;
146 /* Arrays for all the unique addressable vars in the function.
148 ADDRESSABLE_VARS contains the canonical addressable variable
149 ADDRESSABLE_VARS_BASE contains the base symbol for those variables
150 ADDRESSABLE_VARS_ALIAS_SET contains the alias set for this addressable
152 static varray_type addressable_vars
;
153 static varray_type addressable_vars_base
;
154 static varray_type addressable_vars_alias_set
;
156 /* Artificial variable used to model the effects of function calls on every
157 variable that they may use and define. Calls to non-const and non-pure
158 functions are assumed to use and clobber this variable. The SSA builder
159 will then consider this variable to be an alias for every global
160 variable and every local that has had its address taken. */
163 /* Get the operands of statement STMT. Note that repeated calls to
164 get_stmt_operands for the same statement will do nothing until the
165 statement is marked modified by a call to modify_stmt(). */
168 get_stmt_operands (stmt
)
173 voperands_t prev_vops
= NULL
;
175 if (stmt
== error_mark_node
|| stmt
== empty_stmt_node
)
180 /* If the statement has not been modified, the operands are still valid. */
181 if (!stmt_modified_p (stmt
))
184 ann
= stmt_ann (stmt
);
186 ann
= create_stmt_ann (stmt
);
188 /* Remove any existing operands as they will be scanned again. */
191 /* We cannot remove existing virtual operands because we would lose their
192 SSA versions. Instead, we save them on PREV_VOPS. When add_vdef and
193 add_vuse are called, they will search for the operand in PREV_VOPS
196 prev_vops
= ann
->vops
;
199 code
= TREE_CODE (stmt
);
204 get_expr_operands (stmt
, &TREE_OPERAND (stmt
, 1), false, prev_vops
);
205 get_expr_operands (stmt
, &TREE_OPERAND (stmt
, 0), true, prev_vops
);
209 get_expr_operands (stmt
, &COND_EXPR_COND (stmt
), false, prev_vops
);
213 get_expr_operands (stmt
, &SWITCH_COND (stmt
), false, prev_vops
);
217 get_expr_operands (stmt
, &ASM_INPUTS (stmt
), false, prev_vops
);
218 get_expr_operands (stmt
, &ASM_OUTPUTS (stmt
), true, prev_vops
);
219 get_expr_operands (stmt
, &ASM_CLOBBERS (stmt
), true, prev_vops
);
223 get_expr_operands (stmt
, &TREE_OPERAND (stmt
, 0), false, prev_vops
);
227 get_expr_operands (stmt
, &GOTO_DESTINATION (stmt
), false, prev_vops
);
231 get_expr_operands (stmt
, &LABEL_EXPR_LABEL (stmt
), false, prev_vops
);
234 /* These nodes contain no variable references. */
237 case CASE_LABEL_EXPR
:
241 /* Notice that if get_expr_operands tries to use &STMT as the operand
242 pointer (which may only happen for USE operands), we will abort in
243 add_use. This default will handle statements like CALL_EXPRs or
244 VA_ARG_EXPRs that may appear on the RHS of a statement or as
245 statements themselves. */
246 get_expr_operands (stmt
, &stmt
, false, prev_vops
);
250 /* Resize the operand arrays. */
251 if (ann
->ops
&& ann
->ops
->use_ops
)
252 VARRAY_GROW (ann
->ops
->use_ops
, VARRAY_ACTIVE_SIZE (ann
->ops
->use_ops
));
256 if (ann
->vops
->vdef_ops
)
257 VARRAY_GROW (ann
->vops
->vdef_ops
,
258 VARRAY_ACTIVE_SIZE (ann
->vops
->vdef_ops
));
260 if (ann
->vops
->vuse_ops
)
261 VARRAY_GROW (ann
->vops
->vuse_ops
,
262 VARRAY_ACTIVE_SIZE (ann
->vops
->vuse_ops
));
265 /* Clear the modified bit for STMT. Subsequent calls to
266 get_stmt_operands for this statement will do nothing until the
267 statement is marked modified by a call to modify_stmt(). */
272 /* Recursively scan the expression pointed by EXPR_P in statement STMT.
273 IS_DEF is nonzero if the expression is expected to define the
274 referenced variables. PREV_VOPS is as in add_vdef and add_vuse. */
277 get_expr_operands (stmt
, expr_p
, is_def
, prev_vops
)
281 voperands_t prev_vops
;
287 if (expr
== NULL
|| expr
== error_mark_node
)
290 code
= TREE_CODE (expr
);
291 class = TREE_CODE_CLASS (code
);
293 /* Expressions that make no memory references. */
298 || code
== RESULT_DECL
299 || code
== FUNCTION_DECL
300 || code
== LABEL_DECL
)
303 /* If this reference is associated with a non SIMPLE expression, then we
304 mark the statement non GIMPLE and recursively clobber every
305 variable referenced by STMT. FIXME: TREE_NOT_GIMPLE must die. */
306 if (stmt
&& TREE_NOT_GIMPLE (expr
))
308 struct clobber_data_d cd
;
309 mark_not_simple (&stmt
);
311 cd
.prev_vops
= prev_vops
;
312 walk_tree (&stmt
, clobber_vars_r
, (void *) &cd
, NULL
);
316 /* If the parent statement is marked not-gimple, don't do anything. This
317 means that in a previous iteration we encountered a non-gimple
318 sub-expression which already clobbered all the variables in the
319 statement. FIXME: TREE_NOT_GIMPLE must die. */
320 if (stmt
&& TREE_NOT_GIMPLE (stmt
))
323 /* If we found a variable, add it to DEFS or USES depending on the
325 if (SSA_VAR_P (expr
))
327 add_stmt_operand (expr_p
, stmt
, is_def
, false, prev_vops
);
331 /* Treat array references as references to the virtual variable
332 representing the array. The virtual variable for an ARRAY_REF is the
333 VAR_DECL for the array. */
334 if (code
== ARRAY_REF
)
336 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
337 according to the value of IS_DEF. Recurse if the LHS of the
338 ARRAY_REF node is not a regular variable. */
339 if (SSA_VAR_P (TREE_OPERAND (expr
, 0)))
340 add_stmt_operand (expr_p
, stmt
, is_def
, false, prev_vops
);
342 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), is_def
, prev_vops
);
344 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), false, prev_vops
);
348 /* Similarly to arrays, references to compound variables (complex types
349 and structures/unions) are globbed.
351 FIXME: This means that
357 will not be constant propagated because the two partial
358 definitions to 'a' will kill each other. */
359 if (code
== IMAGPART_EXPR
|| code
== REALPART_EXPR
|| code
== COMPONENT_REF
)
361 /* If the LHS of the compound reference is not a regular variable,
362 recurse to keep looking for more operands in the subexpression. */
363 if (SSA_VAR_P (TREE_OPERAND (expr
, 0)))
364 add_stmt_operand (expr_p
, stmt
, is_def
, false, prev_vops
);
366 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), is_def
, prev_vops
);
371 /* Function calls. Add every argument to USES. If the callee is
372 neither pure nor const, create a use and clobbering definition of
373 *GLOBAL_VAR (See find_vars_r). */
374 if (code
== CALL_EXPR
)
377 bool may_clobber
= call_may_clobber (expr
);
379 /* Find uses in the called function. */
380 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), false, prev_vops
);
384 /* If the called function is neither pure nor const, we create a
385 clobbering definition of *GLOBAL_VAR. */
386 tree v
= indirect_ref (global_var
);
387 add_stmt_operand (&v
, stmt
, true, true, prev_vops
);
390 /* Add all the arguments to the function. If the function will not
391 clobber any local variable, check if it may dereference a local
392 pointer. If so, add a VUSE for the dereferenced pointer. This is
393 to address the following problem: Supose that function 'foo' is
394 constant but receives a pointer to a local variable:
408 Since p has never been dereferenced in function bar(), the alias
409 analyzer will never associate 'i' with '*p', which then causes DCE
410 to kill the assignment to 'i' because it's never used in bar().
411 To address this problem, we add a VUSE<*p> at the call site of
413 for (op
= TREE_OPERAND (expr
, 1); op
; op
= TREE_CHAIN (op
))
415 tree arg
= TREE_VALUE (op
);
417 add_stmt_operand (&TREE_VALUE (op
), stmt
, false, false, prev_vops
);
419 /* If the function may not clobber locals, add a VUSE<*p> for
420 every pointer p passed in the argument list (see note above). */
423 && POINTER_TYPE_P (TREE_TYPE (arg
)))
425 tree deref
= indirect_ref (arg
);
426 add_stmt_operand (&deref
, stmt
, false, true, prev_vops
);
434 if (code
== TREE_LIST
)
438 for (op
= expr
; op
; op
= TREE_CHAIN (op
))
439 add_stmt_operand (&TREE_VALUE (op
), stmt
, is_def
, false, prev_vops
);
445 if (code
== INIT_EXPR
|| code
== MODIFY_EXPR
)
447 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), false, prev_vops
);
448 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), true, prev_vops
);
452 /* VA_ARG_EXPR nodes read and modify the argument pointer. Add it to
453 VOPS to avoid optimizations messing it up. */
454 if (code
== VA_ARG_EXPR
)
456 add_stmt_operand (&TREE_OPERAND (expr
, 0), stmt
, true, true, prev_vops
);
460 /* Unary expressions. */
462 || code
== BIT_FIELD_REF
)
464 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), is_def
, prev_vops
);
468 /* Binary expressions. */
471 || code
== TRUTH_AND_EXPR
472 || code
== TRUTH_OR_EXPR
473 || code
== TRUTH_XOR_EXPR
474 || code
== COMPOUND_EXPR
475 || code
== CONSTRUCTOR
)
477 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 0), is_def
, prev_vops
);
478 get_expr_operands (stmt
, &TREE_OPERAND (expr
, 1), is_def
, prev_vops
);
482 /* If we get here, something has gone wrong. */
483 fprintf (stderr
, "unhandled expression in get_expr_operands():\n");
485 fputs ("\n", stderr
);
490 /* Add *VAR_P to the appropriate operand array of STMT. IS_DEF is nonzero
491 if *VAR_P is being defined. The following are the rules used to decide
492 whether an operand belongs in OPS or VOPS:
494 1- Non-aliased scalar and pointer variables are real operands.
496 2- If a variable is aliased, all its aliases are added to the virtual
499 3- For non-scalar variables (arrays, structures, unions and complex
500 types), their virtual variable (see get_virtual_var) is added to the
503 The caller may force a variable to be added as a virtual operand by
504 setting the FORCE_VOP flag.
506 PREV_VOPS is used when adding virtual operands to statements that
507 already had them (See add_vdef and add_vuse). */
510 add_stmt_operand (var_p
, stmt
, is_def
, force_vop
, prev_vops
)
515 voperands_t prev_vops
;
525 /* If the original variable is not a scalar, it will be added to the list
526 of virtual operands. In that case, use its base symbol as the virtual
527 variable representing it. */
528 is_scalar
= SSA_VAR_P (var
);
530 var
= get_virtual_var (var
);
532 /* If VAR is not a variable, do nothing. */
533 if (var
== NULL_TREE
|| !SSA_VAR_P (var
))
536 aliases
= may_aliases (var
);
539 /* The variable is not aliased. If it's a scalar, process it as a
540 real operand. Otherwise, add it to the virtual operands. Note
541 that we never consider ASM_EXPR operands as real. They are always
542 added to virtual operands so that optimizations don't try to
545 FIXME: This is true for CCP. It tries to propagate constants in
546 some __asm__ operands causing ICEs during RTL expansion
547 (execute/20020107-1.c). Do we really need to be this
548 drastic? Or should each optimization take care when
549 dealing with ASM_EXPRs? */
552 if (is_scalar
&& !force_vop
&& TREE_CODE (stmt
) == MODIFY_EXPR
)
553 set_def (var_p
, stmt
);
555 add_vdef (var
, stmt
, prev_vops
);
559 if (is_scalar
&& !force_vop
&& TREE_CODE (stmt
) != ASM_EXPR
)
560 add_use (var_p
, stmt
);
562 add_vuse (var
, stmt
, prev_vops
);
567 /* The variable is aliased. Add its aliases to the virtual operands. */
570 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (aliases
); i
++)
571 add_vdef (VARRAY_TREE (aliases
, i
), stmt
, prev_vops
);
575 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (aliases
); i
++)
576 add_vuse (VARRAY_TREE (aliases
, i
), stmt
, prev_vops
);
580 /* An assignment to a pointer variable 'p' may make 'p' point to memory
581 outside of the current scope. Therefore, dereferences of 'p' should be
582 treated as references to global variables. */
585 && POINTER_TYPE_P (TREE_TYPE (var
)))
589 if (TREE_CODE (stmt
) == MODIFY_EXPR
590 && may_access_global_mem_p (TREE_OPERAND (stmt
, 1),
591 get_base_symbol (TREE_OPERAND (stmt
, 1))))
592 set_may_point_to_global_mem (var
);
594 /* A definition of a pointer variable 'p' clobbers its associated
595 indirect variable '*p', because now 'p' is pointing to a different
597 deref
= indirect_ref (var
);
600 /* Add a VDEF for '*p' (or its aliases if it has any). */
601 aliases
= may_aliases (deref
);
603 add_vdef (deref
, stmt
, prev_vops
);
605 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (aliases
); i
++)
606 add_vdef (VARRAY_TREE (aliases
, i
), stmt
, prev_vops
);
608 /* If 'p' may point to global memory, then '*p' and its aliases are
609 an alias for global memory. */
610 if (may_point_to_global_mem_p (var
))
612 varray_type aliases
= may_aliases (deref
);
613 set_may_alias_global_mem (deref
);
614 for (i
= 0; aliases
&& i
< VARRAY_ACTIVE_SIZE (aliases
); i
++)
616 tree alias
= VARRAY_TREE (aliases
, i
);
617 set_may_alias_global_mem (alias
);
618 set_may_point_to_global_mem (TREE_OPERAND (alias
, 0));
625 /* If VAR is a pointer dereference, we need to add a VUSE for its
626 base pointer. If needed, strip its SSA version, to access the
627 base pointer. Otherwise we won't recognize use of pointers after
628 variables have been renamed. For instance, in (*p)_35, we need to
629 add an operand for 'p', and for that we need to remove the SSA
630 version number first. */
631 if (TREE_CODE (var
) == SSA_NAME
)
632 var
= SSA_NAME_VAR (var
);
634 if (TREE_CODE (var
) == INDIRECT_REF
)
635 add_stmt_operand (&TREE_OPERAND (var
, 0), stmt
, false, true, prev_vops
);
640 /* Set DEF_P to be the pointer to the operand defined by STMT. */
643 set_def (def_p
, stmt
)
647 stmt_ann_t ann
= stmt_ann (stmt
);
649 #if defined ENABLE_CHECKING
650 /* There should only be a single real definition per
652 if (ann
->ops
&& ann
->ops
->def_op
)
656 if (ann
->ops
== NULL
)
658 ann
->ops
= ggc_alloc (sizeof (struct operands_d
));
659 memset ((void *) ann
->ops
, 0, sizeof (*(ann
->ops
)));
662 ann
->ops
->def_op
= def_p
;
666 /* Add USE_P to the list of pointers to operands used by STMT. */
669 add_use (use_p
, stmt
)
673 stmt_ann_t ann
= stmt_ann (stmt
);
675 #if defined ENABLE_CHECKING
676 /* If the pointer to the operand is the statement itself, something is
677 wrong. It means that we are pointing to a local variable (the initial
678 call to get_stmt_operands does not pass a pointer to a statement). */
683 if (ann
->ops
== NULL
)
685 ann
->ops
= ggc_alloc (sizeof (struct operands_d
));
686 memset ((void *) ann
->ops
, 0, sizeof (*(ann
->ops
)));
689 if (ann
->ops
->use_ops
== NULL
)
690 VARRAY_GENERIC_PTR_INIT (ann
->ops
->use_ops
, 3, "use_ops");
692 VARRAY_PUSH_GENERIC_PTR (ann
->ops
->use_ops
, use_p
);
696 /* Add a new VDEF_EXPR for variable VAR to statement STMT. If PREV_VOPS
697 is not NULL, it will be searched for an existing VDEF_EXPR to VAR. If
698 found, the existing VDEF_EXPR will be added to STMT. This is done to
699 preserve the SSA numbering of virtual operands. */
702 add_vdef (var
, stmt
, prev_vops
)
705 voperands_t prev_vops
;
711 if (prev_vops
&& prev_vops
->vdef_ops
)
714 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (prev_vops
->vdef_ops
); i
++)
716 tree d
= VARRAY_TREE (prev_vops
->vdef_ops
, i
);
717 if (var
== SSA_NAME_VAR (VDEF_RESULT (d
)))
724 /* FIXME. If we didn't find an existing VDEF for VAR, it means that
725 we would have to rewrite this statement into SSA form again.
726 Which means that we would have to update other statements, insert
727 PHI nodes, etc. We don't handle this situation. */
732 vdef
= build_vdef_expr (var
);
734 ann
= stmt_ann (stmt
);
735 if (ann
->vops
== NULL
)
737 ann
->vops
= ggc_alloc (sizeof (struct voperands_d
));
738 memset ((void *) ann
->vops
, 0, sizeof (*(ann
->vops
)));
741 if (ann
->vops
->vdef_ops
== NULL
)
742 VARRAY_TREE_INIT (ann
->vops
->vdef_ops
, 5, "vdef_ops");
744 /* Don't allow duplicate entries. */
745 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ann
->vops
->vdef_ops
); i
++)
746 if (var
== VDEF_RESULT (VARRAY_TREE (ann
->vops
->vdef_ops
, i
)))
749 VARRAY_PUSH_TREE (ann
->vops
->vdef_ops
, vdef
);
753 /* Add VAR to the list of virtual uses for STMT. If PREV_VOPS is not NULL,
754 it will be searched for an existing VUSE of VAR. If found, the
755 existing VUSE will be added to STMT. This is done to preserve the
756 SSA numbering of virtual operands. */
759 add_vuse (var
, stmt
, prev_vops
)
762 voperands_t prev_vops
;
767 if (prev_vops
&& prev_vops
->vuse_ops
)
769 tree vuse
= NULL_TREE
;
770 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (prev_vops
->vuse_ops
); i
++)
772 tree u
= VARRAY_TREE (prev_vops
->vuse_ops
, i
);
773 if (var
== SSA_NAME_VAR (u
))
784 /* FIXME. If we didn't find an existing VUSE of VAR, it means
785 that we would have to rewrite this statement into SSA form
786 again. Which means that we would have to find its current
787 reaching definition. We don't handle this situation. */
792 ann
= stmt_ann (stmt
);
793 if (ann
->vops
== NULL
)
795 ann
->vops
= ggc_alloc (sizeof (struct voperands_d
));
796 memset ((void *) ann
->vops
, 0, sizeof (*(ann
->vops
)));
799 if (ann
->vops
->vuse_ops
== NULL
)
800 VARRAY_TREE_INIT (ann
->vops
->vuse_ops
, 5, "vuse_ops");
802 /* Don't allow duplicate entries. */
803 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ann
->vops
->vuse_ops
); i
++)
804 if (var
== VARRAY_TREE (ann
->vops
->vuse_ops
, i
))
807 VARRAY_PUSH_TREE (ann
->vops
->vuse_ops
, var
);
811 /* Create a new PHI node for variable VAR at basic block BB. */
814 create_phi_node (var
, bb
)
823 for (len
= 0, e
= bb
->pred
; e
; e
= e
->pred_next
)
826 phi
= make_phi_node (var
, len
);
828 /* Add the new PHI node to the list of PHI nodes for block BB. */
830 if (ann
->phi_nodes
== NULL
)
831 ann
->phi_nodes
= phi
;
833 chainon (ann
->phi_nodes
, phi
);
835 /* Associate BB to the PHI node. */
836 set_bb_for_stmt (phi
, bb
);
842 /* Add a new argument to PHI node PHI. DEF is the incoming reaching
843 definition and E is the edge through which DEF reaches PHI. */
846 add_phi_arg (phi
, def
, e
)
851 int i
= PHI_NUM_ARGS (phi
);
853 #if defined ENABLE_CHECKING
854 if (i
>= PHI_ARG_CAPACITY (phi
))
857 PHI_ARG_DEF (phi
, i
) = def
;
858 PHI_ARG_EDGE (phi
, i
) = e
;
859 PHI_NUM_ARGS (phi
)++;
863 /*---------------------------------------------------------------------------
865 ---------------------------------------------------------------------------*/
866 /* Return a copy of statement ORIG. Note that the original statement
867 annotations are not copied. */
877 walk_tree (©
, copy_tree_r
, NULL
, NULL
);
878 copy
->common
.ann
= NULL
;
884 /*---------------------------------------------------------------------------
885 Dataflow analysis (DFA) routines
886 ---------------------------------------------------------------------------*/
887 /* Compute immediate uses. */
890 compute_immediate_uses (flags
)
894 block_stmt_iterator si
;
900 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
901 compute_immediate_uses_for (phi
, flags
);
903 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
904 compute_immediate_uses_for (bsi_stmt (si
), flags
);
908 /* Helper for compute_immediate_uses. Check all the USE and/or VUSE
909 operands in STMT and add a def-use edge between their defining statement
913 compute_immediate_uses_for (stmt
, flags
)
920 /* PHI nodes are a special case. We only need to look at its arguments. */
921 if (TREE_CODE (stmt
) == PHI_NODE
)
925 for (i
= 0; i
< PHI_NUM_ARGS (stmt
); i
++)
927 tree imm_rdef_stmt
= SSA_NAME_DEF_STMT (PHI_ARG_DEF (stmt
, i
));
928 if (imm_rdef_stmt
!= empty_stmt_node
)
929 add_immediate_use (imm_rdef_stmt
, stmt
);
934 /* Otherwise, we look at USE_OPS or VUSE_OPS according to FLAGS. */
935 get_stmt_operands (stmt
);
937 if ((flags
& TDFA_USE_OPS
) && use_ops (stmt
))
939 ops
= use_ops (stmt
);
940 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ops
); i
++)
942 tree
*use_p
= VARRAY_GENERIC_PTR (ops
, i
);
943 tree imm_rdef_stmt
= SSA_NAME_DEF_STMT (*use_p
);
944 if (imm_rdef_stmt
!= empty_stmt_node
)
945 add_immediate_use (imm_rdef_stmt
, stmt
);
949 if ((flags
& TDFA_USE_VOPS
) && vuse_ops (stmt
))
951 ops
= vuse_ops (stmt
);
952 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ops
); i
++)
954 tree vuse
= VARRAY_TREE (ops
, i
);
955 tree imm_rdef_stmt
= SSA_NAME_DEF_STMT (vuse
);
956 if (imm_rdef_stmt
!= empty_stmt_node
)
957 add_immediate_use (imm_rdef_stmt
, stmt
);
963 /* Compute reached uses. */
966 compute_reached_uses (flags
)
967 int flags ATTRIBUTE_UNUSED
;
973 /* Compute reaching definitions. */
976 compute_reaching_defs (flags
)
977 int flags ATTRIBUTE_UNUSED
;
984 /* Add statement USE_STMT to the list of statements that use definitions
988 add_immediate_use (stmt
, use_stmt
)
992 stmt_ann_t ann
= stmt_ann (stmt
);
995 ann
= create_stmt_ann (stmt
);
999 ann
->df
= ggc_alloc (sizeof (struct dataflow_d
));
1000 memset ((void *) ann
->df
, 0, sizeof (*(ann
->df
)));
1003 if (ann
->df
->immediate_uses
== NULL
)
1004 VARRAY_GENERIC_PTR_INIT (ann
->df
->immediate_uses
, 10, "immediate_uses");
1006 VARRAY_PUSH_TREE (ann
->df
->immediate_uses
, use_stmt
);
1010 /*---------------------------------------------------------------------------
1012 ---------------------------------------------------------------------------*/
1013 /* Create a new annotation for a _DECL node T. */
1021 #if defined ENABLE_CHECKING
1022 if (t
== NULL_TREE
|| !SSA_VAR_P (t
))
1026 ann
= ggc_alloc (sizeof (*ann
));
1027 memset ((void *) ann
, 0, sizeof (*ann
));
1029 ann
->common
.type
= VAR_ANN
;
1031 t
->common
.ann
= (tree_ann
) ann
;
1037 /* Create a new annotation for a statement node T. */
1045 #if defined ENABLE_CHECKING
1046 if (t
== empty_stmt_node
1048 || TREE_CODE_CLASS (TREE_CODE (t
)) == 'c'
1049 || TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
1053 ann
= ggc_alloc (sizeof (*ann
));
1054 memset ((void *) ann
, 0, sizeof (*ann
));
1056 ann
->common
.type
= STMT_ANN
;
1058 /* Since we just created the annotation, mark the statement modified. */
1059 ann
->modified
= true;
1062 t
->common
.ann
= (tree_ann
) ann
;
1068 /*---------------------------------------------------------------------------
1070 ---------------------------------------------------------------------------*/
1071 /* Dump the list of all the referenced variables in the current function to
1075 dump_referenced_vars (file
)
1080 fprintf (file
, "\nReferenced variables in %s: %lu\n\n",
1081 get_name (current_function_decl
), num_referenced_vars
);
1083 for (i
= 0; i
< num_referenced_vars
; i
++)
1085 tree var
= referenced_var (i
);
1086 fprintf (file
, "Variable: ");
1087 dump_variable (file
, var
);
1088 fprintf (file
, "\n");
1093 /* Dump the list of all the referenced variables to stderr. */
1096 debug_referenced_vars ()
1098 dump_referenced_vars (stderr
);
1102 /* Dump variable VAR and its may-aliases to FILE. */
1105 dump_variable (file
, var
)
1109 varray_type aliases
;
1111 if (var
== NULL_TREE
)
1113 fprintf (file
, "<nil>");
1117 print_generic_expr (file
, var
, 0);
1119 aliases
= may_aliases (var
);
1122 size_t i
, num_aliases
= VARRAY_ACTIVE_SIZE (aliases
);
1123 fprintf (file
, ", may aliases: ");
1124 for (i
= 0; i
< num_aliases
; i
++)
1126 print_generic_expr (file
, VARRAY_TREE (aliases
, i
), 0);
1127 if (i
< num_aliases
- 1)
1128 fprintf (file
, ", ");
1132 if (may_alias_global_mem_p (var
))
1133 fprintf (file
, ", may alias global memory");
1135 if (is_vla_decl (var
))
1136 fprintf (file
, ", is used to declare a VLA");
1138 if (may_point_to_global_mem_p (var
))
1139 fprintf (file
, ", may point to global memory");
1141 fprintf (file
, "\n");
1145 /* Dump variable VAR and its may-aliases to stderr. */
1148 debug_variable (var
)
1151 dump_variable (stderr
, var
);
1155 /* Dump def-use edges on FILE. */
1158 dump_immediate_uses (file
)
1162 block_stmt_iterator si
;
1164 fprintf (file
, "\nDef-use edges for function %s\n", current_function_name
);
1170 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
1171 dump_immediate_uses_for (file
, phi
);
1173 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
1174 dump_immediate_uses_for (file
, bsi_stmt (si
));
1177 fprintf (file
, "\n");
1181 /* Dump def-use edges on stderr. */
1184 debug_immediate_uses ()
1186 dump_immediate_uses (stderr
);
1190 /* Dump all immediate uses for STMT on FILE. */
1193 dump_immediate_uses_for (file
, stmt
)
1197 varray_type imm_uses
= immediate_uses (stmt
);
1203 fprintf (file
, "-> ");
1204 print_generic_stmt (file
, stmt
, TDF_SLIM
);
1205 fprintf (file
, "\n");
1207 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (imm_uses
); i
++)
1209 fprintf (file
, "\t");
1210 print_generic_stmt (file
, VARRAY_TREE (imm_uses
, i
), TDF_SLIM
);
1211 fprintf (file
, "\n");
1214 fprintf (file
, "\n");
1219 /* Dump immediate uses for STMT on stderr. */
1222 debug_immediate_uses_for (stmt
)
1225 dump_immediate_uses_for (stderr
, stmt
);
1228 /* Dump various DFA statistics to FILE. */
1231 dump_dfa_stats (file
)
1234 struct dfa_stats_d dfa_stats
;
1236 unsigned long size
, total
= 0;
1237 const char * const fmt_str
= "%-30s%-13s%12s\n";
1238 const char * const fmt_str_1
= "%-30s%13lu%11lu%c\n";
1239 const char * const fmt_str_3
= "%-43s%11lu%c\n";
1241 collect_dfa_stats (&dfa_stats
);
1243 fprintf (file
, "\nDFA Statistics for %s\n\n", current_function_name
);
1245 fprintf (file
, "---------------------------------------------------------\n");
1246 fprintf (file
, fmt_str
, "", " Number of ", "Memory");
1247 fprintf (file
, fmt_str
, "", " instances ", "used ");
1248 fprintf (file
, "---------------------------------------------------------\n");
1250 size
= num_referenced_vars
* sizeof (tree
);
1252 fprintf (file
, fmt_str_1
, "Referenced variables", num_referenced_vars
,
1253 SCALE (size
), LABEL (size
));
1255 size
= num_indirect_refs
* sizeof (tree
);
1257 fprintf (file
, fmt_str_1
, "INDIRECT_REFs", num_indirect_refs
,
1258 SCALE (size
), LABEL (size
));
1260 size
= num_addressable_vars
* sizeof (tree
);
1262 fprintf (file
, fmt_str_1
, "Addressable variables", num_addressable_vars
,
1263 SCALE (size
), LABEL (size
));
1265 size
= dfa_stats
.num_stmt_anns
* sizeof (struct stmt_ann_d
);
1267 fprintf (file
, fmt_str_1
, "Statements annotated", dfa_stats
.num_stmt_anns
,
1268 SCALE (size
), LABEL (size
));
1270 size
= dfa_stats
.num_var_anns
* sizeof (struct var_ann_d
);
1272 fprintf (file
, fmt_str_1
, "Variables annotated", dfa_stats
.num_var_anns
,
1273 SCALE (size
), LABEL (size
));
1275 size
= dfa_stats
.num_uses
* sizeof (tree
*);
1277 fprintf (file
, fmt_str_1
, "USE operands", dfa_stats
.num_uses
,
1278 SCALE (size
), LABEL (size
));
1280 size
= dfa_stats
.num_defs
* sizeof (tree
*);
1282 fprintf (file
, fmt_str_1
, "DEF operands", dfa_stats
.num_defs
,
1283 SCALE (size
), LABEL (size
));
1285 size
= dfa_stats
.num_phis
* sizeof (struct tree_phi_node
);
1287 fprintf (file
, fmt_str_1
, "PHI nodes", dfa_stats
.num_phis
,
1288 SCALE (size
), LABEL (size
));
1290 size
= dfa_stats
.num_phi_args
* sizeof (struct phi_arg_d
);
1292 fprintf (file
, fmt_str_1
, "PHI arguments", dfa_stats
.num_phi_args
,
1293 SCALE (size
), LABEL (size
));
1295 size
= dfa_stats
.size_tree_refs
;
1297 fprintf (file
, fmt_str_1
, "Virtual references", dfa_stats
.num_tree_refs
,
1298 SCALE (size
), LABEL (size
));
1300 fprintf (file
, "---------------------------------------------------------\n");
1301 fprintf (file
, fmt_str_3
, "Total memory used by DFA/SSA data", SCALE (total
),
1303 fprintf (file
, "---------------------------------------------------------\n");
1304 fprintf (file
, "\n");
1306 if (dfa_stats
.num_phis
)
1307 fprintf (file
, "Average number of arguments per PHI node: %.1f (max: %d)\n",
1308 (float) dfa_stats
.num_phi_args
/ (float) dfa_stats
.num_phis
,
1309 dfa_stats
.max_num_phi_args
);
1311 fprintf (file
, "\n");
1315 /* Dump DFA statistics on stderr. */
1320 dump_dfa_stats (stderr
);
1324 /* Collect DFA statistics and store them in the structure pointed by
1328 collect_dfa_stats (dfa_stats_p
)
1329 struct dfa_stats_d
*dfa_stats_p
;
1333 block_stmt_iterator i
;
1335 if (dfa_stats_p
== NULL
)
1338 memset ((void *)dfa_stats_p
, 0, sizeof (struct dfa_stats_d
));
1340 /* Walk all the trees in the function counting references. Start at
1341 basic block 0, but don't stop at block boundaries. */
1342 htab
= htab_create (30, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1344 for (i
= bsi_start (BASIC_BLOCK (0)); !bsi_end_p (i
); bsi_next (&i
))
1345 walk_tree (bsi_stmt_ptr (i
), collect_dfa_stats_r
, (void *) dfa_stats_p
,
1353 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
1355 dfa_stats_p
->num_phis
++;
1356 dfa_stats_p
->num_phi_args
+= PHI_NUM_ARGS (phi
);
1357 if (PHI_NUM_ARGS (phi
) > dfa_stats_p
->max_num_phi_args
)
1358 dfa_stats_p
->max_num_phi_args
= PHI_NUM_ARGS (phi
);
1364 /* Callback for walk_tree to collect DFA statistics for a tree and its
1368 collect_dfa_stats_r (tp
, walk_subtrees
, data
)
1370 int *walk_subtrees ATTRIBUTE_UNUSED
;
1374 struct dfa_stats_d
*dfa_stats_p
= (struct dfa_stats_d
*)data
;
1378 switch (ann_type (t
->common
.ann
))
1382 stmt_ann_t ann
= (stmt_ann_t
) t
->common
.ann
;
1383 dfa_stats_p
->num_stmt_anns
++;
1386 if (ann
->ops
->def_op
)
1387 dfa_stats_p
->num_defs
++;
1388 if (ann
->ops
->use_ops
)
1389 dfa_stats_p
->num_uses
+= VARRAY_ACTIVE_SIZE (ann
->ops
->use_ops
);
1394 voperands_t vops
= ann
->vops
;
1397 dfa_stats_p
->num_vdefs
+= VARRAY_ACTIVE_SIZE (vops
->vdef_ops
);
1400 dfa_stats_p
->num_vuses
+= VARRAY_ACTIVE_SIZE (vops
->vuse_ops
);
1406 dfa_stats_p
->num_var_anns
++;
1418 /* Callback for walk_tree, create may-def/may-use references for every
1419 declaration node and compound reference found under a given tree node
1423 clobber_vars_r (tp
, walk_subtrees
, data
)
1425 int *walk_subtrees ATTRIBUTE_UNUSED
;
1428 enum tree_code code
= TREE_CODE (*tp
);
1430 /* Add every *_DECL node to VDEFS and VUSES. */
1431 if (code
== VAR_DECL
|| code
== PARM_DECL
)
1433 struct clobber_data_d
*cd
= (struct clobber_data_d
*) data
;
1434 add_vuse (*tp
, cd
->stmt
, cd
->prev_vops
);
1435 add_vdef (*tp
, cd
->stmt
, cd
->prev_vops
);
1442 /*---------------------------------------------------------------------------
1444 ---------------------------------------------------------------------------*/
1445 /* Compute may-alias information for every variable referenced in the
1446 program. Note that in the absence of points-to analysis
1447 (-ftree-points-to), this may compute a much bigger set than necessary. */
1450 compute_may_aliases ()
1453 static htab_t vars_found
;
1454 static htab_t indirect_refs_found
;
1455 static htab_t addressable_vars_found
;
1457 block_stmt_iterator si
;
1458 htab_t walk_state
[3];
1460 /* Walk the lexical blocks in the function looking for variables that may
1461 have been used to declare VLAs. Those variables will be considered
1462 implicitly live by passes like DCE. FIXME: This is a hack. GIMPLE
1463 should expose VLAs in the code. */
1464 find_vla_decls (DECL_INITIAL (current_function_decl
));
1466 timevar_push (TV_TREE_MAY_ALIAS
);
1468 num_indirect_refs
= 0;
1469 VARRAY_TREE_INIT (indirect_refs
, 20, "indirect_refs");
1470 VARRAY_TREE_INIT (indirect_refs_base
, 20, "indirect_refs_base");
1471 VARRAY_WIDE_INT_INIT (indirect_refs_alias_set
, 20, "indirect_refs_alias_set");
1473 num_addressable_vars
= 0;
1474 VARRAY_TREE_INIT (addressable_vars
, 20, "addressable_vars");
1475 VARRAY_TREE_INIT (addressable_vars_base
, 20, "addressable_vars_base");
1476 VARRAY_WIDE_INT_INIT (addressable_vars_alias_set
, 20,
1477 "addressable_vars_alias_set");
1479 /* Hash table of all the objects the SSA builder needs to be aware of. */
1480 vars_found
= htab_create (50, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1482 /* Hash table of all the unique INDIRECT_REFs found. */
1483 indirect_refs_found
= htab_create (50, htab_hash_pointer
, htab_eq_pointer
,
1486 /* Hash table of all the unique addressable variables found. */
1487 addressable_vars_found
= htab_create (50, htab_hash_pointer
, htab_eq_pointer
,
1490 walk_state
[0] = vars_found
;
1491 walk_state
[1] = indirect_refs_found
;
1492 walk_state
[2] = addressable_vars_found
;
1494 /* Find all the variables referenced in the function. */
1496 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
1497 walk_tree (bsi_stmt_ptr (si
), find_vars_r
, &walk_state
, NULL
);
1499 htab_delete (vars_found
);
1500 htab_delete (indirect_refs_found
);
1501 htab_delete (addressable_vars_found
);
1503 if (flag_tree_points_to
!= PTA_NONE
&& indirect_refs_found
)
1505 timevar_push (TV_TREE_PTA
);
1506 create_alias_vars (current_function_decl
);
1507 timevar_pop (TV_TREE_PTA
);
1510 if (flag_tree_points_to
== PTA_NONE
)
1511 compute_alias_sets ();
1514 for (i
= 0; i
< num_indirect_refs
; i
++)
1515 find_may_aliases_for (i
);
1518 num_indirect_refs
= 0;
1520 indirect_refs_base
= 0;
1521 indirect_refs_alias_set
= 0;
1522 num_addressable_vars
= 0;
1523 addressable_vars
= 0;
1524 addressable_vars_base
= 0;
1525 addressable_vars_alias_set
= 0;
1527 timevar_pop (TV_TREE_MAY_ALIAS
);
1531 /* Compute type-based alias sets. This is used in the absence of
1532 points-to analysis. We compute the alias sets for every addressable and
1533 pointer variable. Those sets that conflict are considered to alias each
1536 Each entry I in ALIAS_SETS represents a set of all the variables that
1537 are aliased by ALIAS_SETS[I]. The ALIAS_SETS array tends to have few
1538 entries, and each entry will likely alias many program variables.
1540 This will negatively impact optimizations because variable uses will be
1541 reached by many definitions that can't really reach them. See the
1542 documentation in tree-ssa.c. */
1545 compute_alias_sets ()
1548 tree var
, sym
, deref_gv
;
1550 VARRAY_GENERIC_PTR_INIT (alias_sets
, 20, "alias_sets");
1552 /* For each pointer P dereferenced in the program, compute its alias set
1553 and register it in ALIAS_SETS. If P's alias set does not conflict
1554 with any entry in ALIAS_SETS, or if it conflicts with more than one
1555 entry, create a new entry for P.
1557 We need to treat GLOBAL_VAR separately. Since GLOBAL_VAR aliases
1558 variables that might have been otherwise unaliased, we register it
1559 first so that we make sure that if GLOBAL_VAR is needed for this
1560 function, it is always an alias tag. Otherwise we will miss the
1561 following case: Suppose that *P and Q are two variables that don't
1570 If *P is added to ALIAS_SETS first, then we will build an alias set
1571 tagged with *P which contains *P and GLOBAL_VAR. However, since Q
1572 does not conflict with *P, it will never be added to the set. */
1573 deref_gv
= indirect_ref (global_var
);
1575 register_alias_set (deref_gv
, global_var
);
1577 for (i
= 0; i
< num_indirect_refs
; i
++)
1579 var
= VARRAY_TREE (indirect_refs
, i
);
1580 sym
= VARRAY_TREE (indirect_refs_base
, i
);
1581 if (var
!= deref_gv
)
1582 register_alias_set (var
, sym
);
1585 /* Compute alias sets for indirect_refs and addressables. For every
1586 variable V, find an entry P in ALIAS_SETS such that P and V have
1587 conflicting alias sets. If none is found, then V is not aliased to
1589 for (i
= 0; i
< num_indirect_refs
; i
++)
1591 var
= VARRAY_TREE (indirect_refs
, i
);
1592 sym
= VARRAY_TREE (indirect_refs_base
, i
);
1593 find_alias_for (var
, sym
);
1596 for (i
= 0; i
< num_addressable_vars
; i
++)
1598 var
= VARRAY_TREE (addressable_vars
, i
);
1599 sym
= VARRAY_TREE (addressable_vars_base
, i
);
1600 find_alias_for (var
, sym
);
1603 /* Debugging dumps. */
1604 if (tree_ssa_dump_file
&& tree_ssa_dump_flags
& TDF_ALIAS
)
1606 dump_alias_info (tree_ssa_dump_file
);
1607 dump_referenced_vars (tree_ssa_dump_file
);
1610 /* Deallocate memory used by ALIAS_SETS. */
1611 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (alias_sets
); i
++)
1613 struct alias_set_d
*as
;
1614 as
= (struct alias_set_d
*) VARRAY_GENERIC_PTR (alias_sets
, i
);
1622 /* Try to add DEREF as a new new alias tag in ALIAS_SETS. If a conflicting
1623 tag is already present, then instead of adding a new entry, DEREF is
1624 marked as an alias of the existing entry. DEREF_SYM is the base symbol
1628 register_alias_set (deref
, deref_sym
)
1633 struct alias_set_d
*curr
, *prev
;
1634 HOST_WIDE_INT deref_set
, deref_sym_set
;
1637 deref_set
= get_alias_set (deref
);
1638 deref_sym_set
= get_alias_set (deref_sym
);
1640 /* FIXME: ALIAS_SETS will usually be small (<< 10 entries). If it becomes
1641 a performance problem, try with a splay tree. */
1643 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (alias_sets
); i
++)
1645 curr
= (struct alias_set_d
*) VARRAY_GENERIC_PTR (alias_sets
, i
);
1647 if (may_alias_p (deref
, deref_sym
, deref_set
,
1648 curr
->tag
, curr
->tag_sym
, curr
->tag_set
))
1650 /* If this is the first time we find a conflict, mark the entry
1651 index where the conflict was found and continue. */
1652 if (last_found
== -1)
1658 /* We had found a conflict before, this means that DEREF may
1659 alias variables that are in different alias sets. If this
1660 happens, we need to aggregate the two alias sets into a new
1661 one with DEREF as its tag.
1663 Replace the tag for the previous entry with DEREF and remove
1664 the current entry by swapping it with the last element in the
1665 ALIAS_SETS array. */
1666 prev
= (struct alias_set_d
*) VARRAY_GENERIC_PTR (alias_sets
,
1669 /* We only need to update the previous tag once. */
1670 if (prev
->tag
!= deref
)
1673 prev
->tag_sym
= deref_sym
;
1674 prev
->tag_set
= deref_set
;
1675 prev
->tag_sym_set
= deref_sym_set
;
1678 /* Swap the current and the last element, and pop the array. */
1679 num_sets
= VARRAY_ACTIVE_SIZE (alias_sets
);
1680 if (i
< num_sets
- 1)
1682 VARRAY_GENERIC_PTR (alias_sets
, i
) =
1683 VARRAY_GENERIC_PTR (alias_sets
, num_sets
- 1);
1684 i
--; /* Reset the iterator to avoid missing the entry we
1688 VARRAY_POP (alias_sets
);
1692 /* If we didn't find an existing set that conflicts with SET. Add it. */
1693 if (last_found
== -1)
1695 curr
= xmalloc (sizeof (*curr
));
1697 curr
->tag_set
= deref_set
;
1698 curr
->tag_sym
= deref_sym
;
1699 curr
->tag_sym_set
= deref_sym_set
;
1700 VARRAY_PUSH_GENERIC_PTR (alias_sets
, (void *) curr
);
1705 /* Set an alias between VAR and the first entry in ALIAS_SETS that has a
1706 conflicting alias set with VAR. SYM is VAR's base symbol (needed when
1707 VAR is an INDIRECT_REF node). */
1710 find_alias_for (var
, sym
)
1715 HOST_WIDE_INT var_set
;
1717 var_set
= get_alias_set (var
);
1719 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (alias_sets
); i
++)
1721 struct alias_set_d
*as
;
1723 as
= (struct alias_set_d
*) VARRAY_GENERIC_PTR (alias_sets
, i
);
1725 /* If AS->TAG aliases VAR, add it to the set of may-aliases for VAR
1727 if (may_alias_p (var
, sym
, var_set
, as
->tag
, as
->tag_sym
, as
->tag_set
))
1729 /* Set the tag to be the alias for VAR. */
1730 add_may_alias (var
, sym
, as
->tag
, as
->tag_sym
);
1732 if (may_aliases (as
->tag
) == NULL
)
1734 /* Force the alias tag to alias itself. This avoids problems
1735 when the program is accessing the alias tag directly. For
1736 instance, suppose that '*p' is the alias tag for 'i' and
1743 If '*p' is not made an alias of itself, then the assignment
1744 at line 2 will be considered a killing definition of '*p'.
1745 This would make the assignment to 'i' at line 1 appear dead
1746 because the use of 'i' at line 3 is now reached by the
1747 assignment to '*p' at line 2.
1749 If '*p' does not alias 'i' at runtime, the compiler
1750 would've generated wrong code. This fixes the regression
1751 of gcc.c-torture/execute/950929-1.c. */
1752 add_may_alias (as
->tag
, as
->tag_sym
, as
->tag
, as
->tag_sym
);
1761 /* Return TRUE if variables V1 and V2 may alias.
1763 V1_BASE is the base symbol for V1
1764 V1_ALIAS_SET is the alias set for V1
1765 V2_BASE is the base symbol for V2
1766 V2_ALIAS_SET is the base symbol for V2. */
1769 may_alias_p (v1
, v1_base
, v1_alias_set
, v2
, v2_base
, v2_alias_set
)
1772 HOST_WIDE_INT v1_alias_set
;
1775 HOST_WIDE_INT v2_alias_set
;
1777 tree ptr
, var
, ptr_sym
, var_sym
;
1778 HOST_WIDE_INT ptr_alias_set
, var_alias_set
;
1783 /* One of the two variables needs to be an INDIRECT_REF or GLOBAL_VAR,
1784 otherwise they can't possibly alias each other. */
1785 if (TREE_CODE (v1
) == INDIRECT_REF
|| v1
== global_var
)
1789 ptr_alias_set
= v1_alias_set
;
1792 var_alias_set
= v2_alias_set
;
1794 else if (TREE_CODE (v2
) == INDIRECT_REF
|| v2
== global_var
)
1798 ptr_alias_set
= v2_alias_set
;
1801 var_alias_set
= v1_alias_set
;
1806 /* GLOBAL_VAR aliases every global variable, pointer dereference and
1807 locals that have had their address taken, unless points-to analysis is
1808 done. This is because points-to is supposed to handle this case, and
1809 thus, can give a more accurate answer. */
1810 if (ptr_sym
== global_var
1811 && (TREE_ADDRESSABLE (var_sym
)
1812 || TREE_CODE (var
) == INDIRECT_REF
1813 || decl_function_context (var_sym
) == NULL
))
1815 if (flag_tree_points_to
== PTA_NONE
)
1819 /* Right now, it's just not worth the time/space to make
1820 points-to handle the global variables seperately (in
1821 intraprocedural mode, anyway). */
1822 if (decl_function_context (var_sym
) == NULL
)
1825 /* For GLOBAL_VAR, we want to see if the variable aliases
1826 GLOBAL_VAR, not if GLOBAL_VAR aliases the variable (since
1827 the points-to sets are possibly directional, and
1828 GLOBAL_VAR never gets assigned to, only assigned from). */
1829 if (ptr_may_alias_var (var_sym
, ptr_sym
))
1834 /* If the alias sets don't conflict then PTR cannot alias VAR. */
1835 if (!alias_sets_conflict_p (ptr_alias_set
, var_alias_set
))
1837 /* Handle aliases to structure fields. If both VAR and PTR are
1838 aggregate types, they may not have conflicting types, but one of
1839 the structures could contain a pointer to the other one.
1846 It may happen that '*p' and '*q' can't alias because 'struct P'
1847 and 'struct Q' have non-conflicting alias sets. However, it could
1848 happen that one of the fields in 'struct P' is a 'struct Q *' and
1851 Therefore, we also need to check if 'struct P' aliases 'struct Q *'
1852 or 'struct Q' aliases 'struct P *'. Notice, that since GIMPLE
1853 does not have more than one-level pointers, we don't need to
1854 recurse into the structures. */
1855 if (TREE_CODE (var
) == INDIRECT_REF
1856 && AGGREGATE_TYPE_P (TREE_TYPE (ptr
))
1857 && AGGREGATE_TYPE_P (TREE_TYPE (var
))
1858 && (alias_sets_conflict_p (ptr_alias_set
, get_alias_set (var_sym
))
1859 || alias_sets_conflict_p (var_alias_set
, get_alias_set (ptr_sym
))))
1865 /* If -ftree-points-to is given, check if PTR may point to VAR. */
1866 if (flag_tree_points_to
)
1867 if (!ptr_may_alias_var (ptr_sym
, var_sym
))
1874 /* Find variables that may be aliased by the variable (V1) at
1875 index INDIRECT_REF_INDEX in the INDIRECT_REFS varray. */
1878 find_may_aliases_for (indirect_ref_index
)
1879 int indirect_ref_index
;
1882 tree v1
= VARRAY_TREE (indirect_refs
, indirect_ref_index
);
1883 tree v1_base
= VARRAY_TREE (indirect_refs_base
, indirect_ref_index
);
1884 HOST_WIDE_INT v1_alias_set
1885 = VARRAY_INT (indirect_refs_alias_set
, indirect_ref_index
);
1887 #if defined ENABLE_CHECKING
1888 if (TREE_CODE (v1
) != INDIRECT_REF
)
1892 /* Note that our aliasing properties are symmetric, so we can
1893 start this loop at INDIRECT_REF_INDEX + 1 to cut down on the
1894 runtime for this routine. */
1895 for (i
= indirect_ref_index
+ 1; i
< num_indirect_refs
; i
++)
1897 tree v2
= VARRAY_TREE (indirect_refs
, i
);
1898 tree v2_base
= VARRAY_TREE (indirect_refs_base
, i
);
1899 HOST_WIDE_INT v2_alias_set
= VARRAY_INT (indirect_refs_alias_set
, i
);
1901 if (may_alias_p (v1
, v1_base
, v1_alias_set
, v2
, v2_base
, v2_alias_set
))
1903 add_may_alias (v2
, v2_base
, v1
, v1_base
);
1904 add_may_alias (v1
, v1_base
, v2
, v2_base
);
1908 /* Now check if V1 may alias any of the addressable variables. */
1909 for (i
= 0; i
< num_addressable_vars
; i
++)
1911 tree v2
= VARRAY_TREE (addressable_vars
, i
);
1912 tree v2_base
= VARRAY_TREE (addressable_vars_base
, i
);
1913 HOST_WIDE_INT v2_alias_set
= VARRAY_INT (addressable_vars_alias_set
, i
);
1918 if (may_alias_p (v1
, v1_base
, v1_alias_set
, v2
, v2_base
, v2_alias_set
))
1920 add_may_alias (v2
, v2_base
, v1
, v1_base
);
1921 add_may_alias (v1
, v1_base
, v2
, v2_base
);
1927 /* Add ALIAS to the set of variables that may alias VAR. VAR_SYM and
1928 ALIAS_SYM are the base symbols for VAR and ALIAS respectively. */
1931 add_may_alias (var
, var_sym
, alias
, alias_sym
)
1938 var_ann_t v_ann
= var_ann (var
);
1939 var_ann_t a_ann
= var_ann (alias
);
1942 v_ann
= create_var_ann (var
);
1944 if (v_ann
->may_aliases
== NULL
)
1945 VARRAY_TREE_INIT (v_ann
->may_aliases
, 2, "aliases");
1947 /* Avoid adding duplicates. */
1948 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (v_ann
->may_aliases
); i
++)
1949 if (alias
== VARRAY_TREE (v_ann
->may_aliases
, i
))
1952 /* If either VAR or ALIAS may access global memory, then mark the other
1953 one as a global memory alias. */
1957 a_ann
= create_var_ann (alias
);
1959 if (may_access_global_mem_p (var
, var_sym
))
1960 a_ann
->may_alias_global_mem
= 1;
1962 if (may_access_global_mem_p (alias
, alias_sym
))
1963 v_ann
->may_alias_global_mem
= 1;
1966 VARRAY_PUSH_TREE (v_ann
->may_aliases
, alias
);
1970 /* Dump alias information on FILE. */
1973 dump_alias_info (file
)
1978 if (alias_sets
== NULL
)
1981 fprintf (file
, "\nAlias information for %s: %d sets\n\n",
1982 current_function_name
, VARRAY_ACTIVE_SIZE (alias_sets
));
1984 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (alias_sets
); i
++)
1986 struct alias_set_d
*as
;
1988 as
= (struct alias_set_d
*) VARRAY_GENERIC_PTR (alias_sets
, i
);
1990 fprintf (file
, "Alias set #%ld:\n", i
);
1991 fprintf (file
, " Tag: ");
1992 dump_variable (file
, as
->tag
);
1993 fprintf (file
, " Aliases: { ");
1995 for (j
= 0; j
< num_addressable_vars
; j
++)
1997 tree var
= VARRAY_TREE (addressable_vars
, j
);
1998 varray_type aliases
= may_aliases (var
);
1999 if (aliases
&& VARRAY_TREE (aliases
, 0) == as
->tag
)
2001 print_generic_expr (file
, var
, 0);
2002 fprintf (file
, " ");
2006 for (j
= 0; j
< num_indirect_refs
; j
++)
2008 tree var
= VARRAY_TREE (indirect_refs
, j
);
2009 varray_type aliases
= may_aliases (var
);
2010 if (aliases
&& VARRAY_TREE (aliases
, 0) == as
->tag
)
2012 print_generic_expr (file
, var
, 0);
2013 fprintf (file
, " ");
2016 fprintf (file
, "}\n\n");
2021 /* Dump alias information on stderr. */
2026 dump_alias_info (stderr
);
2031 /*---------------------------------------------------------------------------
2032 Miscellaneous helpers
2033 ---------------------------------------------------------------------------*/
2034 /* Return TRUE if expression EXPR may reference memory outside the current
2038 may_access_global_mem_p (expr
, expr_base
)
2044 if (expr
== NULL_TREE
)
2047 /* Function arguments and global variables may reference global memory. */
2048 if (expr_base
!= NULL_TREE
)
2050 if (TREE_CODE (expr_base
) == PARM_DECL
2051 || decl_function_context (expr_base
) == NULL_TREE
)
2055 /* If the expression is a variable that may point to or alias global memory,
2057 if (SSA_VAR_P (expr
))
2059 var_ann_t ann
= var_ann (expr
);
2060 if (ann
->may_point_to_global_mem
|| ann
->may_alias_global_mem
)
2064 /* Otherwise, the expression must be of pointer type. */
2065 if (TREE_TYPE (expr
) == NULL_TREE
2066 || !POINTER_TYPE_P (TREE_TYPE (expr
)))
2069 /* Call expressions that return pointers may point to global memory. */
2070 if (TREE_CODE (expr
) == CALL_EXPR
)
2073 /* Recursively check the expression's operands. */
2074 class = TREE_CODE_CLASS (TREE_CODE (expr
));
2075 if (IS_EXPR_CODE_CLASS (class) || class == 'r')
2079 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (expr
)); i
++)
2080 if (may_access_global_mem_p (TREE_OPERAND (expr
, i
),
2081 get_base_symbol (TREE_OPERAND (expr
, i
))))
2089 /* Remove variable DECL from the block that declares it. */
2097 loc
= find_decl_location (decl
, DECL_INITIAL (current_function_decl
));
2099 *loc
= TREE_CHAIN (decl
);
2103 /* Find the location for declaration DECL in lexical block BLOCK. All the
2104 subblocks of BLOCK are searched as well if BLOCK does not declare DECL.
2105 Return an address LOC such that *LOC == DECL or NULL if DECL couldn't be
2109 find_decl_location (decl
, block
)
2115 /* Special case. If DECL is the first symbol in the block, return its
2116 location directly. */
2117 if (BLOCK_VARS (block
) == decl
)
2118 return &(BLOCK_VARS (block
));
2120 for (d
= BLOCK_VARS (block
); d
; d
= TREE_CHAIN (d
))
2121 if (TREE_CHAIN (d
) == decl
)
2122 return &(TREE_CHAIN (d
));
2124 for (sub
= BLOCK_SUBBLOCKS (block
); sub
; sub
= TREE_CHAIN (sub
))
2126 tree
*loc
= find_decl_location (decl
, sub
);
2135 /* Callback for walk_tree. Used to collect variables referenced in
2139 find_vars_r (tp
, walk_subtrees
, data
)
2141 int *walk_subtrees ATTRIBUTE_UNUSED
;
2142 void *data ATTRIBUTE_UNUSED
;
2144 if (SSA_VAR_P (*tp
))
2146 tree sym
, var
, deref
;
2149 sym
= get_base_symbol (var
);
2151 /* If VAR is an INDIRECT_REF node rewrite *TP with the first
2152 INDIRECT_REF we found (if any). We need to share INDIRECT_REF
2153 nodes because we treat them as regular variables and several
2154 passes rely on pointer equality for testing if two variables are
2156 if (TREE_CODE (var
) == INDIRECT_REF
)
2158 deref
= indirect_ref (sym
);
2162 set_indirect_ref (sym
, var
);
2165 add_referenced_var (var
, sym
, data
);
2171 /* Function calls. Consider them a reference for an artificial variable
2172 called GLOBAL_VAR. This variable is a pointer that will alias every
2173 global variable and locals that have had their address taken. The
2174 exception to this rule are functions marked pure, const or if they are
2175 known to not return.
2177 Stores to *GLOBAL_VAR will reach uses of every call clobbered variable
2178 in the function. Uses of *GLOBAL_VAR will be reached by definitions
2179 of call clobbered variables.
2181 This is used to model the effects that the called function may have on
2182 local and global variables that might be visible to it. */
2183 if (TREE_CODE (*tp
) == CALL_EXPR
)
2185 if (call_may_clobber (*tp
))
2186 add_indirect_ref_var (global_var
, data
);
2191 /* If the function does not clobber locals, it still may
2192 dereference them. Scan its operands to see if it receives any
2193 pointers. For every pointer 'p' add '*p' to the list of
2194 referenced variables. */
2195 for (op
= TREE_OPERAND (*tp
, 1); op
; op
= TREE_CHAIN (op
))
2197 tree arg
= TREE_VALUE (op
);
2198 if (SSA_DECL_P (arg
) && POINTER_TYPE_P (TREE_TYPE (arg
)))
2199 add_indirect_ref_var (arg
, data
);
2208 /* Add VAR to the list of dereferenced variables. SYM is the base symbol
2209 when VAR is anything but a _DECL node. Otherwise, SYM and VAR are the
2212 Also add VAR to the sets ADDRESSABLE_VARS or INDIRECT_REFS needed for
2213 alias analysis. DATA is an array with three hash tables used to avoid
2214 adding the same variable more than once to its corresponding set. Note
2215 that this function assumes that VAR is a valid SSA variable. */
2218 add_referenced_var (var
, sym
, data
)
2224 htab_t vars_found
= ((htab_t
*) data
)[0];
2225 htab_t indirect_refs_found
= ((htab_t
*) data
)[1];
2226 htab_t addressable_vars_found
= ((htab_t
*) data
)[2];
2228 /* First handle an INDIRECT_REF. */
2229 if (TREE_CODE (var
) == INDIRECT_REF
)
2231 slot
= htab_find_slot (indirect_refs_found
, (void *) var
, INSERT
);
2234 *slot
= (void *) var
;
2235 VARRAY_PUSH_TREE (indirect_refs
, var
);
2236 VARRAY_PUSH_TREE (indirect_refs_base
, sym
);
2237 VARRAY_PUSH_INT (indirect_refs_alias_set
, get_alias_set (var
));
2238 num_indirect_refs
++;
2241 else if (TREE_ADDRESSABLE (sym
) || decl_function_context (sym
) == NULL
)
2243 slot
= htab_find_slot (addressable_vars_found
, (void *) var
, INSERT
);
2246 *slot
= (void *) var
;
2247 VARRAY_PUSH_TREE (addressable_vars
, var
);
2248 VARRAY_PUSH_TREE (addressable_vars_base
, sym
);
2249 VARRAY_PUSH_INT (addressable_vars_alias_set
, get_alias_set (var
));
2250 num_addressable_vars
++;
2254 slot
= htab_find_slot (vars_found
, (void *) var
, INSERT
);
2259 /* This is the first time we find this variable, add it to the
2260 REFERENCED_VARS array, also annotate it with its unique id. */
2261 *slot
= (void *) var
;
2262 VARRAY_PUSH_TREE (referenced_vars
, var
);
2263 ann
= var_ann (var
);
2265 ann
= create_var_ann (var
);
2266 ann
->uid
= num_referenced_vars
++;
2268 /* Arguments or global variable pointers may point to memory outside
2269 the current function. */
2270 if (POINTER_TYPE_P (TREE_TYPE (var
))
2272 && (TREE_CODE (var
) == PARM_DECL
2273 || decl_function_context (var
) == NULL_TREE
))
2274 set_may_point_to_global_mem (var
);
2276 /* Dereferences of pointers that may point to global memory must be
2277 marked as global memory aliases. Note that we still can't use the
2278 may_point_to_global_mem_p predicate here, because we may not have
2279 processed the base pointer yet. */
2280 if (TREE_CODE (var
) == INDIRECT_REF
2281 && (TREE_CODE (sym
) == PARM_DECL
2282 || decl_function_context (sym
) == NULL_TREE
))
2284 set_may_point_to_global_mem (sym
);
2285 set_may_alias_global_mem (var
);
2291 /* Add a reference to the INDIRECT_REF node of variable VAR. If VAR has
2292 not been dereferenced yet, create a new INDIRECT_REF node for it. DATA
2293 is as in add_referenced_var. Note that VAR is assumed to be a valid
2297 add_indirect_ref_var (ptr
, data
)
2301 tree deref
= indirect_ref (ptr
);
2302 if (deref
== NULL_TREE
)
2304 deref
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (ptr
)), ptr
);
2305 set_indirect_ref (ptr
, deref
);
2308 add_referenced_var (deref
, ptr
, data
);
2312 /* Return the virtual variable associated to the non-scalar variable VAR. */
2315 get_virtual_var (var
)
2318 enum tree_code code
;
2322 if (TREE_CODE (var
) == SSA_NAME
)
2323 var
= SSA_NAME_VAR (var
);
2325 code
= TREE_CODE (var
);
2327 while (code
== ARRAY_REF
2328 || code
== COMPONENT_REF
2329 || code
== REALPART_EXPR
2330 || code
== IMAGPART_EXPR
2331 || (code
== INDIRECT_REF
2332 && !DECL_P (TREE_OPERAND (var
, 0))))
2334 var
= TREE_OPERAND (var
, 0);
2335 code
= TREE_CODE (var
);
2342 /* Return true if EXPR is a CALL_EXPR tree to a function that may clobber
2343 globals and local addressable variables. */
2346 call_may_clobber (expr
)
2352 if (TREE_CODE (expr
) != CALL_EXPR
)
2355 callee
= get_callee_fndecl (expr
);
2356 flags
= (callee
) ? flags_from_decl_or_type (callee
) : 0;
2357 return (! (flags
& (ECF_CONST
| ECF_PURE
| ECF_NORETURN
)));
2361 /* Mark variables that are being used to declare VLAs (Variable Length
2362 Arrays). Those variables will be considered implicitly live by the
2363 dataflow routines. FIXME: This is a huge ugly hack. GIMPLE should
2364 expose VLAs more gracefully. */
2367 find_vla_decls (block
)
2372 /* Check all the arrays declared in the block. */
2373 for (decl
= BLOCK_VARS (block
); decl
; decl
= TREE_CHAIN (decl
))
2376 walk_tree (&decl
, find_vla_decls_r
, &inside_vla
, NULL
);
2379 /* Now repeat the search in any sub-blocks. */
2380 for (sub
= BLOCK_SUBBLOCKS (block
); sub
; sub
= TREE_CHAIN (sub
))
2381 find_vla_decls (sub
);
2385 /* Callback for walk_tree used by find_vla_decls to analyze each array in a
2386 lexical block. For each array type, it walks its TYPE_SIZE and
2387 TYPE_SIZE_UNIT trees looking for VAR_DECLs. Those VAR_DECLs will be
2388 marked as needed for VLA. */
2391 find_vla_decls_r (tp
, walk_subtrees
, data
)
2393 int *walk_subtrees ATTRIBUTE_UNUSED
;
2394 void *data ATTRIBUTE_UNUSED
;
2396 int *inside_vla
= (int *) data
;
2398 if (TREE_CODE (*tp
) == ARRAY_TYPE
)
2401 walk_tree (&TYPE_SIZE (*tp
), find_vla_decls_r
, inside_vla
, NULL
);
2402 walk_tree (&TYPE_SIZE_UNIT (*tp
), find_vla_decls_r
, inside_vla
, NULL
);
2404 else if (TREE_CODE (*tp
) == TYPE_DECL
)
2407 walk_tree (&TYPE_FIELDS (TREE_TYPE (*tp
)), find_vla_decls_r
,
2410 else if (*inside_vla
&& SSA_DECL_P (*tp
))