1 /* Tree based points-to analysis
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "coretypes.h"
27 #include "tree-alias-type.h"
29 #include "tree-alias-common.h"
31 /* If we have andersen's points-to analysis, include it. */
33 #include "tree-alias-ander.h"
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
44 #include "diagnostic.h"
47 #include "tree-flow.h"
48 #include "tree-inline.h"
51 #include "tree-gimple.h"
55 #include "tree-pass.h"
58 /* Reduce ifdefery later. */
60 #define HAVE_BANSHEE 0
63 /* This file contains the implementation of the common parts of the
64 tree points-to analysis infrastructure.
68 This file contains the points-to analysis driver. It does two main things:
69 1. Keeps track of the PTA data for each variable (IE the data each
70 specific PTA implementation wants to keep associated with a
72 2. Walks the function trees, calling the appropriate functions that
73 each PTA implementation has implemented.
75 In order to speed up PTA queries, the PTA specific data is stored
76 in the tree for *_DECL's, in DECL_PTA_ALIASVAR. This way, we only
77 need to use the hash table for non-DECL's. */
80 /* Array of all created alias_vars.
81 Note that this should contain all the alias_vars we wanted
83 static GTY((param_is (union alias_var_def
))) varray_type alias_vars
= NULL
;
84 struct tree_alias_ops
*current_alias_ops
;
86 /* Array of local (to a function) alias_vars.
87 Note that this should contain all the alias_vars that are
88 local to this function. We delete these from alias_vars before
90 static GTY(()) varray_type local_alias_vars
;
91 static GTY(()) varray_type local_alias_varnums
;
93 static bitmap addrargs
;
94 static alias_var
get_alias_var_decl (tree
);
95 static alias_var
get_alias_var (tree
);
96 static void find_func_aliases (tree
);
97 static void deal_with_call_aliasing (tree
, alias_var
);
98 static alias_var
create_fun_alias_var_ptf (tree
, tree
);
99 static alias_var
create_fun_alias_var (tree
, int);
100 static alias_var
create_alias_var (tree
);
101 static void intra_function_call (varray_type
);
102 static void get_values_from_constructor (tree
, varray_type
*, bitmap
, int *);
103 static bool call_may_clobber (tree
);
104 static bool call_may_return (tree
);
106 /* Return true if a EXPR, which is a CALL_EXPR, may clobber variables. */
109 call_may_clobber (tree expr
)
113 if (TREE_CODE (expr
) != CALL_EXPR
)
116 flags
= call_expr_flags (expr
);
117 return (! (flags
& (ECF_CONST
| ECF_PURE
| ECF_NORETURN
)));
120 /* Return true if a EXPR, which is a CALL_EXPR, may return. */
123 call_may_return (tree expr
)
127 if (TREE_CODE (expr
) != CALL_EXPR
)
130 flags
= call_expr_flags (expr
);
131 return ! (flags
& ECF_NORETURN
);
134 /* Get the alias_var for DECL.
135 Creates the alias_var if it does not exist already. Also
136 handles FUNCTION_DECL properly. */
139 get_alias_var_decl (tree decl
)
142 if (TREE_CODE (decl
) == FIELD_DECL
)
146 if (DECL_PTA_ALIASVAR (decl
))
147 return DECL_PTA_ALIASVAR (decl
);
150 if (TREE_CODE (decl
) == FUNCTION_DECL
)
151 newvar
= create_fun_alias_var (decl
, 0);
154 newvar
= create_alias_var (decl
);
155 /* Assign globals to global var for purposes of intraprocedural
157 if (TREE_STATIC (decl
) && decl
!= pta_global_var
)
159 current_alias_ops
->addr_assign (current_alias_ops
,
160 get_alias_var (pta_global_var
),
162 /* If the global has some DECL_INITIAL, we need to process
164 if (DECL_INITIAL (decl
))
165 find_func_aliases (decl
);
169 if (!current_alias_ops
->ip
)
171 if (!current_alias_ops
->ip_partial
172 || (TREE_CODE (decl
) != FUNCTION_DECL
173 && TREE_CODE (decl
) != PARM_DECL
))
175 VARRAY_PUSH_INT (local_alias_varnums
, ALIAS_VAR_VARNUM (newvar
));
176 VARRAY_PUSH_TREE (local_alias_vars
, decl
);
182 /* Get the alias_var for an expression EXPR.
183 Note that this function expects to only be handed a RHS or LHS, not
187 get_alias_var (tree expr
)
189 /* If it's a decl, get the alias var of the decl. We farm this off
190 to get_alias_var_decl so it can abort if the alias var doesn't
191 exist, and in case something else *knows* it has a decl, and
192 wants the alias var. */
195 return get_alias_var_decl (expr
);
197 /* True constants have no aliases (unless modifiable strings are on,
198 in which case i don't think we'll end up with a STRING_CST anyway) */
199 if (TREE_CODE_CLASS (TREE_CODE (expr
)) == 'c')
203 switch (TREE_CODE (expr
))
206 case ARRAY_RANGE_REF
:
208 /* Find the first non-array ref, and return its alias variable. */
212 TREE_CODE (p
) == ARRAY_REF
|| TREE_CODE (p
) == ARRAY_RANGE_REF
;
213 p
= TREE_OPERAND (p
, 0))
215 return get_alias_var (p
);
224 TREE_CODE (p
) == COMPONENT_REF
|| TREE_CODE (p
) == INDIRECT_REF
;
225 p
= TREE_OPERAND (p
, 0))
227 if (TREE_CODE (TREE_TYPE (p
)) == UNION_TYPE
228 || TREE_CODE (TREE_TYPE (p
)) == QUAL_UNION_TYPE
)
236 for (p
= expr
; TREE_CODE (p
) == COMPONENT_REF
;
237 p
= TREE_OPERAND (p
, 0));
238 return get_alias_var (p
);
242 return get_alias_var (TREE_OPERAND (expr
, 1));
245 /* Find the first non-component ref, and return its alias variable. */
247 for (p
= expr
; TREE_CODE (p
) == COMPONENT_REF
;
248 p
= TREE_OPERAND (p
, 0));
249 return get_alias_var (p
);
264 /* If it's a ref or cast or conversion of something, get the
265 alias var of the something. */
266 return get_alias_var (TREE_OPERAND (expr
, 0));
274 /* Perform conservative aliasing for an intraprocedural mode function
275 call. ARGS are the arguments that were passed to that function
279 intra_function_call (varray_type args
)
281 size_t l
= VARRAY_ACTIVE_SIZE (args
);
283 alias_var av
= get_alias_var (pta_global_var
);
285 /* We assume assignments among the actual parameters. */
286 for (i
= 0; i
< l
; i
++)
288 alias_var argi
= VARRAY_GENERIC_PTR (args
, i
);
290 for (j
= 0; j
< l
; j
++)
295 argj
= VARRAY_GENERIC_PTR (args
, j
);
296 /* Restricted pointers can't be aliased with other
297 restricted pointers. */
298 if (!TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (argi
)))
299 || !TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (argj
))))
300 /* Do a bit of TBAA to avoid pointless assignments. */
301 if (alias_sets_conflict_p (get_alias_set (ALIAS_VAR_DECL (argi
)),
302 get_alias_set (ALIAS_VAR_DECL (argj
))))
303 current_alias_ops
->simple_assign (current_alias_ops
, argi
, argj
);
306 /* We assume that an actual parameter can point to any global. */
307 for (i
= 0; i
< l
; i
++)
309 alias_var argav
= VARRAY_GENERIC_PTR (args
, i
);
310 /* Restricted pointers can't be aliased with other
311 restricted pointers. */
312 if (!TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (argav
)))
313 || !TYPE_RESTRICT (TREE_TYPE (ALIAS_VAR_DECL (av
))))
315 /* Arguments can alias globals, and whatever they point to
316 can point to a global as well. */
317 current_alias_ops
->simple_assign (current_alias_ops
, argav
, av
);
322 /* Put all pointers in a constructor in an array. */
325 get_values_from_constructor (tree constructor
, varray_type
*vals
,
326 bitmap addrargs
, int *i
)
329 switch (TREE_CODE (constructor
))
333 for (elt_list
= CONSTRUCTOR_ELTS (constructor
);
335 elt_list
= TREE_CHAIN (elt_list
))
337 tree value
= TREE_VALUE (elt_list
);
338 if (TREE_CODE (value
) == TREE_LIST
339 || TREE_CODE (value
) == CONSTRUCTOR
)
341 get_values_from_constructor (value
, vals
, addrargs
, i
); }
345 aav
= get_alias_var (value
);
347 VARRAY_PUSH_GENERIC_PTR (*vals
, aav
);
348 if (TREE_CODE (value
) == ADDR_EXPR
)
349 bitmap_set_bit (addrargs
, *i
);
356 for (elt_list
= constructor
;
358 elt_list
= TREE_CHAIN (elt_list
))
360 get_values_from_constructor (TREE_VALUE (elt_list
), vals
, addrargs
, i
);
368 /* Deal with the possible return values of a call that we don't have
369 actual PTA info about. */
372 deal_with_call_aliasing (tree callargs
, alias_var lhsAV
)
376 for (argp
= callargs
;
378 argp
= TREE_CHAIN (argp
))
380 arg
= TREE_VALUE (argp
);
381 /* If we take the address of a variable directly in the
382 argument, the return value could be the address of that
384 if (TREE_CODE (arg
) == ADDR_EXPR
)
385 current_alias_ops
->addr_assign (current_alias_ops
, lhsAV
,
386 get_alias_var (arg
));
387 /* If we pass in a pointer, we could return that pointer. */
388 else if (POINTER_TYPE_P (TREE_TYPE (arg
)))
390 alias_var argtv
= get_alias_var (arg
);
392 current_alias_ops
->simple_assign (current_alias_ops
, lhsAV
,
398 /* Find the operand of the component ref that actually is doing
399 something to the DECL */
401 find_op_of_decl (tree cref
)
403 while (!DECL_P (TREE_OPERAND (cref
, 0)))
405 cref
= TREE_OPERAND (cref
, 0);
411 /* Tree walker that is the heart of the aliasing infrastructure.
412 TP is a pointer to the current tree.
413 WALK_SUBTREES specifies whether to continue traversing subtrees or
415 Returns NULL_TREE when we should stop.
417 This function is the main part of the aliasing infrastructure. It
418 walks the trees, calling the appropriate alias analyzer functions to process
419 various statements. */
422 find_func_aliases (tree stp
)
424 if (TREE_CODE (stp
) == RETURN_EXPR
)
426 stp
= TREE_OPERAND (stp
, 0);
431 if (TREE_CODE (stp
) == MODIFY_EXPR
432 || (DECL_P (stp
) && DECL_INITIAL (stp
) != NULL_TREE
))
435 alias_var lhsAV
= NULL
;
436 alias_var rhsAV
= NULL
;
441 op1
= DECL_INITIAL (stp
);
445 op0
= TREE_OPERAND (stp
, 0);
446 op1
= TREE_OPERAND (stp
, 1);
447 if (TREE_CODE (op1
) == WITH_SIZE_EXPR
)
448 op1
= TREE_OPERAND (op1
, 0);
451 /* lhsAV should always have an alias variable */
452 lhsAV
= get_alias_var (op0
);
455 /* rhsAV might not have one, c.f. c = 5 */
456 rhsAV
= get_alias_var (op1
);
458 while (TREE_CODE (op1
) == COMPONENT_REF
459 && TREE_CODE (TREE_OPERAND (op1
, 0)) == COMPONENT_REF
)
461 op1
= TREE_OPERAND (op1
, 0);
463 while (TREE_CODE (op1
) == BIT_FIELD_REF
)
465 op1
= TREE_OPERAND (op1
, 0);
467 /* Take care of fact that we may have multi-level component
469 if (TREE_CODE (op1
) == COMPONENT_REF
)
470 op1
= find_op_of_decl (op1
);
473 /* You would think we could test rhsAV at the top, rather than
474 50 separate times, but we can't, because it can be NULL for
475 operator assignments, where we'd still collect the individual
476 alias vars for the operator. */
478 /* Note that structures are treated as a single alias
479 variable, since we can disambiguate based on TBAA first,
480 and fall back on points-to. */
481 /* x = <something> */
482 if (is_gimple_variable (op0
))
485 if (is_gimple_variable (op1
))
488 current_alias_ops
->simple_assign (current_alias_ops
, lhsAV
,
492 else if (TREE_CODE (op1
) == COMPONENT_REF
493 && DECL_P (TREE_OPERAND (op1
, 0)))
496 current_alias_ops
->simple_assign (current_alias_ops
, lhsAV
,
499 /* x = (cast) [maybe-addr-expr] y */
500 else if (is_gimple_cast (op1
))
502 tree stripped_op1
= op1
;
503 STRIP_NOPS (stripped_op1
);
506 if (TREE_CODE (stripped_op1
) == ADDR_EXPR
)
507 current_alias_ops
->addr_assign (current_alias_ops
, lhsAV
,
510 current_alias_ops
->simple_assign (current_alias_ops
, lhsAV
,
514 /* x = *y or x = foo->y */
515 else if (TREE_CODE (op1
) == INDIRECT_REF
516 || TREE_CODE (op1
) == ARRAY_REF
517 || (TREE_CODE (op1
) == COMPONENT_REF
518 && TREE_CODE (TREE_OPERAND (op1
, 0)) == INDIRECT_REF
))
521 current_alias_ops
->ptr_assign (current_alias_ops
, lhsAV
,
524 /* x = &y = x = &foo.y */
525 else if (TREE_CODE (op1
) == ADDR_EXPR
)
528 current_alias_ops
->addr_assign (current_alias_ops
, lhsAV
,
532 else if (TREE_CODE (op1
) == CALL_EXPR
)
534 /* Heap assignment. These are __attribute__ malloc or
535 something, I'll deal with it later. */
541 /* NORETURN functions have no effect on aliasing. */
542 if (call_may_return (op1
))
546 tree callop0
, callop1
;
549 /* Collect the arguments */
550 VARRAY_GENERIC_PTR_INIT (args
, 1, "Arguments");
551 bitmap_clear (addrargs
);
552 callop1
= TREE_OPERAND (op1
, 1);
553 callop0
= TREE_OPERAND (op1
, 0);
554 for (arg
= callop1
, argnum
= 0;
556 arg
= TREE_CHAIN (arg
), argnum
++)
558 alias_var aav
= get_alias_var (TREE_VALUE (arg
));
561 VARRAY_PUSH_GENERIC_PTR (args
, aav
);
562 if (TREE_CODE (TREE_VALUE (arg
)) == ADDR_EXPR
)
563 bitmap_set_bit (addrargs
, argnum
);
566 /* Simulate the call */
567 if (current_alias_ops
->function_call (current_alias_ops
, lhsAV
,
568 get_alias_var (callop0
),
571 if (call_may_clobber (op1
)
572 && !current_alias_ops
->ip
573 && flag_argument_noalias
!= 2)
575 intra_function_call (args
);
577 if (POINTER_TYPE_P (TREE_TYPE (op0
)))
578 deal_with_call_aliasing (callop1
, lhsAV
);
586 bitmap_clear (addrargs
);
587 if (TREE_CODE (op1
) == CONSTRUCTOR
)
591 VARRAY_GENERIC_PTR_INIT (ops
, 1, "Operands");
592 get_values_from_constructor (op1
, &ops
, addrargs
, &i
);
593 current_alias_ops
->op_assign (current_alias_ops
, lhsAV
,
597 switch (TREE_CODE_CLASS (TREE_CODE (op1
)))
599 case 'e': /* an expression */
600 case 's': /* an expression with side effects */
601 case '<': /* a comparison expression */
602 case '1': /* a unary arithmetic expression */
603 case 'r': /* a reference */
604 case '2': /* a binary arithmetic expression */
609 VARRAY_GENERIC_PTR_INIT (ops
, 1, "Operands");
610 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (op1
)); i
++)
613 op
= TREE_OPERAND (op1
, i
);
614 aav
= get_alias_var (op
);
616 VARRAY_PUSH_GENERIC_PTR (ops
, aav
);
617 if (TREE_CODE (op
) == ADDR_EXPR
)
618 bitmap_set_bit (addrargs
, i
);
620 current_alias_ops
->op_assign (current_alias_ops
, lhsAV
,
629 /* *x = <something> */
632 /* x.f = y or x->f = y */
633 if ((TREE_CODE (op0
) == COMPONENT_REF
634 || TREE_CODE (op0
) == BIT_FIELD_REF
)
635 && is_gimple_variable (op1
))
638 current_alias_ops
->simple_assign (current_alias_ops
, lhsAV
,
641 /* x.f = &y or x->f = &y */
642 else if (TREE_CODE (op0
) == COMPONENT_REF
643 && TREE_CODE (op1
) == ADDR_EXPR
)
646 current_alias_ops
->addr_assign (current_alias_ops
, lhsAV
,
649 /* *x.f = y or *x->f = y */
650 else if ((TREE_CODE (op0
) == INDIRECT_REF
651 || TREE_CODE (op0
) == ARRAY_REF
)
652 && TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
653 && is_gimple_variable (op1
))
656 current_alias_ops
->assign_ptr (current_alias_ops
, lhsAV
,
660 else if ((TREE_CODE (op0
) == INDIRECT_REF
661 || TREE_CODE (op0
) == ARRAY_REF
)
662 && TREE_CODE (op1
) == ADDR_EXPR
)
664 /* This becomes temp = &y and *x = temp . */
666 tree temp
= create_tmp_var_raw (void_type_node
, "aliastmp");
667 tempvar
= current_alias_ops
->add_var (current_alias_ops
, temp
);
668 current_alias_ops
->addr_assign (current_alias_ops
, tempvar
,
670 current_alias_ops
->assign_ptr (current_alias_ops
, lhsAV
,
675 else if ((TREE_CODE (op0
) == INDIRECT_REF
676 || TREE_CODE (op0
) == ARRAY_REF
)
677 && (TREE_CODE (op1
) == INDIRECT_REF
678 || TREE_CODE (op1
) == ARRAY_REF
))
680 /* This becomes temp = *y and *x = temp . */
683 temp
= create_tmp_var_raw (void_type_node
, "aliastmp");
684 tempvar
= current_alias_ops
->add_var (current_alias_ops
, temp
);
685 current_alias_ops
->ptr_assign (current_alias_ops
, tempvar
,
687 current_alias_ops
->assign_ptr (current_alias_ops
, lhsAV
,
692 else if ((TREE_CODE (op0
) == INDIRECT_REF
693 || TREE_CODE (op0
) == ARRAY_REF
)
694 && is_gimple_cast (op1
))
698 /* This becomes temp = (cast) y and *x = temp. */
701 temp
= create_tmp_var_raw (void_type_node
, "aliastmp");
702 tempvar
= current_alias_ops
->add_var (current_alias_ops
,
704 current_alias_ops
->simple_assign (current_alias_ops
,
706 current_alias_ops
->assign_ptr (current_alias_ops
, lhsAV
,
710 /* *x = <something else> */
714 current_alias_ops
->assign_ptr (current_alias_ops
, lhsAV
,
719 /* Calls without return values. */
720 else if (TREE_CODE (stp
) == CALL_EXPR
)
725 callvar
= get_alias_var (TREE_OPERAND (stp
, 0));
729 /* NORETURN and CONST functions with no return value
730 have no effect on aliasing (as may be seen above,
731 const functions that return a value might have an
732 effect on aliasing, since the return value can point
733 to one of the arguments. */
734 if (call_may_clobber (stp
))
737 VARRAY_GENERIC_PTR_INIT (args
, 1, "Arguments");
738 bitmap_clear (addrargs
);
739 for (arg
= TREE_OPERAND (stp
, 1), argnum
=0;
741 arg
= TREE_CHAIN (arg
), argnum
++)
743 alias_var aav
= get_alias_var (TREE_VALUE (arg
));
746 VARRAY_PUSH_GENERIC_PTR (args
, aav
);
747 if (TREE_CODE (TREE_VALUE (arg
)) == ADDR_EXPR
)
748 bitmap_set_bit (addrargs
, argnum
);
753 if (current_alias_ops
->function_call (current_alias_ops
, NULL
,
754 callvar
, args
, addrargs
))
755 if (!current_alias_ops
->ip
&& flag_argument_noalias
!= 2)
756 intra_function_call (args
);
762 /* Create the alias_var for a function definition DECL, it's
763 arguments, and it's return value. If FORCE is true, we force
764 creation of the alias_var, regardless of whether one exists already.
766 This includes creation of alias_var's for
767 - The function itself.
769 - The return value. */
772 create_fun_alias_var (tree decl
, int force
)
774 alias_var avar
, retvar
;
776 varray_type params
= NULL
;
780 if (DECL_PTA_ALIASVAR (decl
))
781 return DECL_PTA_ALIASVAR (decl
);
784 VARRAY_GENERIC_PTR_INIT (params
, 1, "Arguments");
785 if (DECL_ARGUMENTS (decl
) != NULL
)
788 for (arg
= DECL_ARGUMENTS (decl
); arg
; arg
= TREE_CHAIN (arg
))
790 alias_var var
= get_alias_var (arg
);
791 VARRAY_PUSH_GENERIC_PTR (params
, var
);
792 /* Incoming pointers can point to pta_global_var, unless
793 either we are interprocedural, or we can do ip on all
794 statics + this function has been defined + it's not an
795 external function. */
796 if (POINTER_TYPE_P (TREE_TYPE (arg
))
797 && !current_alias_ops
->ip
798 /* FIXME: Need to let analyzer decide in partial case. */
799 && (!current_alias_ops
->ip_partial
800 || !cgraph_local_info (decl
)->local
))
801 current_alias_ops
->simple_assign (current_alias_ops
, var
,
802 get_alias_var (pta_global_var
));
805 else if (TYPE_ARG_TYPES (TREE_TYPE (decl
)) != NULL
)
808 /* FIXME: Handle varargs */
809 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
810 arg
&& TREE_VALUE (arg
) != void_type_node
;
811 arg
= TREE_CHAIN (arg
))
813 tree fakedecl
= create_tmp_var_raw (TREE_VALUE (arg
), "normarg");
815 DECL_CONTEXT (fakedecl
) = current_function_decl
;
816 var
= get_alias_var (fakedecl
);
817 VARRAY_PUSH_GENERIC_PTR (params
, var
);
819 /* Incoming pointers can point to pta_global_var, unless
820 either we are interprocedural, or we can do ip on all
821 statics + this function has been defined + it's not an
822 external function. */
823 if (POINTER_TYPE_P (TREE_TYPE (fakedecl
))
824 && !current_alias_ops
->ip
825 /* FIXME: need to let analyzer decide in partial case. */
826 && (!current_alias_ops
->ip_partial
827 || !TREE_STATIC (decl
)
828 || TREE_PUBLIC (decl
)))
829 current_alias_ops
->simple_assign (current_alias_ops
, var
,
830 get_alias_var (pta_global_var
));
833 /* Functions declared like void f() are *not* equivalent to void
834 f(void). You can pass an argument to them. Thus, we need to
835 create some fake argument that would alias any actuals that get
836 passed to our function. */
839 tree fakedecl
= create_tmp_var_raw (void_type_node
, "fakearg");
841 DECL_CONTEXT (fakedecl
) = current_function_decl
;
842 fakevar
= get_alias_var (fakedecl
);
843 VARRAY_PUSH_GENERIC_PTR (params
, fakevar
);
846 if (!DECL_RESULT (decl
))
848 rdecl
= create_tmp_var_raw (TREE_TYPE (TREE_TYPE (decl
)), "_rv_");
849 retvar
= current_alias_ops
->add_var (current_alias_ops
, rdecl
);
850 DECL_PTA_ALIASVAR (rdecl
) = retvar
;
854 retvar
= current_alias_ops
->add_var (current_alias_ops
,
856 DECL_PTA_ALIASVAR (DECL_RESULT (decl
)) = retvar
;
858 VARRAY_PUSH_GENERIC_PTR (alias_vars
, retvar
);
859 ALIAS_VAR_VARNUM (retvar
) = VARRAY_ACTIVE_SIZE (alias_vars
) - 1;
860 avar
= current_alias_ops
->add_var (current_alias_ops
, decl
);
861 VARRAY_PUSH_GENERIC_PTR (alias_vars
, avar
);
862 ALIAS_VAR_VARNUM (avar
) = VARRAY_ACTIVE_SIZE (alias_vars
) - 1;
864 current_alias_ops
->function_def (current_alias_ops
, avar
, params
, retvar
);
865 DECL_PTA_ALIASVAR (decl
) = avar
;
867 /* FIXME: Also, if this is a defining declaration then add the annotation
868 to all extern definitions of the function. */
872 /* Create an alias variable for a pointer-to-member function DECL of
873 type TYPE, it's arguments, and it's return value.
874 Returns the alias_var for the PTF.
876 This includes creating alias_var's for
877 - The function itself.
879 - The return value. */
882 create_fun_alias_var_ptf (tree decl
, tree type
)
884 alias_var avar
, retvar
;
886 varray_type params
= NULL
;
888 if (DECL_PTA_ALIASVAR (decl
))
889 return DECL_PTA_ALIASVAR (decl
);
891 VARRAY_GENERIC_PTR_INIT (params
, 1, "Arguments");
893 if (TYPE_ARG_TYPES (type
) != NULL
)
896 /* FIXME: Handle varargs */
897 for (arg
= TYPE_ARG_TYPES (type
);
898 arg
&& TREE_VALUE (arg
) != void_type_node
;
899 arg
= TREE_CHAIN (arg
))
901 tree fakedecl
= create_tmp_var_raw (TREE_VALUE (arg
), "ptfarg");
903 DECL_CONTEXT (fakedecl
) = DECL_CONTEXT (decl
);
904 var
= get_alias_var (fakedecl
);
905 VARRAY_PUSH_GENERIC_PTR (params
, var
);
908 /* Functions declared like void f() are *not* equivalent to void
909 f(void). You can pass an argument to them. Thus, we need to
910 create some fake argument that would alias any actuals that get
911 passed to our function. */
914 tree fakedecl
= create_tmp_var_raw (void_type_node
, "fakearg");
916 DECL_CONTEXT (fakedecl
) = DECL_CONTEXT (decl
);
917 fakevar
= get_alias_var (fakedecl
);
918 VARRAY_PUSH_GENERIC_PTR (params
, fakevar
);
921 rdecl
= create_tmp_var_raw (TREE_TYPE (type
), "_rv_");
922 retvar
= current_alias_ops
->add_var (current_alias_ops
, rdecl
);
923 VARRAY_PUSH_GENERIC_PTR (alias_vars
, retvar
);
924 ALIAS_VAR_VARNUM (retvar
) = VARRAY_ACTIVE_SIZE (alias_vars
) - 1;
926 avar
= current_alias_ops
->add_var (current_alias_ops
, decl
);
927 VARRAY_PUSH_GENERIC_PTR (alias_vars
, avar
);
928 ALIAS_VAR_VARNUM (avar
) = VARRAY_ACTIVE_SIZE (alias_vars
) - 1;
930 current_alias_ops
->function_def (current_alias_ops
, avar
, params
, retvar
);
931 DECL_PTA_ALIASVAR (decl
) = avar
;
936 /* Create the alias_var for a *_DECL node DECL.
937 Returns the alias_var for DECL.
939 This function also handles creation of alias_var's for PTF
943 create_alias_var (tree decl
)
952 if (DECL_PTA_ALIASVAR (decl
))
953 return DECL_PTA_ALIASVAR (decl
);
956 if (POINTER_TYPE_P (TREE_TYPE (decl
))
957 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) == FUNCTION_TYPE
)
959 avar
= create_fun_alias_var_ptf (decl
, TREE_TYPE (TREE_TYPE (decl
)));
962 avar
= current_alias_ops
->add_var (current_alias_ops
, decl
);
966 DECL_PTA_ALIASVAR (decl
) = avar
;
969 VARRAY_PUSH_GENERIC_PTR (alias_vars
, avar
);
970 ALIAS_VAR_VARNUM (avar
) = VARRAY_ACTIVE_SIZE (alias_vars
) - 1;
974 /* Create points-to sets for the current function. */
977 create_alias_vars (void)
981 if (flag_tree_points_to
== PTA_ANDERSEN
)
982 current_alias_ops
= andersen_alias_ops
;
986 current_alias_ops
= NULL
;
987 flag_tree_points_to
= PTA_NONE
;
991 pta_global_var
= build_decl (VAR_DECL
, get_identifier (".pta_global_var"),
993 DECL_ARTIFICIAL (pta_global_var
) = 1;
994 TREE_READONLY (pta_global_var
) = 1;
995 DECL_EXTERNAL (pta_global_var
) = 0;
996 TREE_STATIC (pta_global_var
) = 1;
997 TREE_USED (pta_global_var
) = 1;
998 DECL_CONTEXT (pta_global_var
) = current_function_decl
;
999 TREE_THIS_VOLATILE (pta_global_var
) = 1;
1000 TREE_ADDRESSABLE (pta_global_var
) = 0;
1004 DECL_PTA_ALIASVAR (current_function_decl
) = NULL
;
1005 get_alias_var (current_function_decl
);
1007 /* First, walk the variables and their DECL_INITIAL's */
1008 if (cfun
->unexpanded_var_list
)
1011 for (vars
= cfun
->unexpanded_var_list
; vars
; vars
= TREE_CHAIN (vars
))
1013 var
= TREE_VALUE (vars
);
1014 if (TREE_CODE (var
) != LABEL_DECL
1015 && TREE_STATIC (var
)
1016 && DECL_INITIAL (var
))
1017 find_func_aliases (var
);
1021 /* Now walk all statements and derive aliases. */
1024 block_stmt_iterator bsi
;
1025 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1026 find_func_aliases (bsi_stmt (bsi
));
1029 pta_global_var
= NULL_TREE
;
1032 struct tree_opt_pass pass_build_pta
=
1036 create_alias_vars
, /* execute */
1039 0, /* static_pass_number */
1040 TV_TREE_PTA
, /* tv_id */
1041 PROP_cfg
, /* properties_required */
1042 PROP_pta
, /* properties_provided */
1043 0, /* properties_destroyed */
1044 0, /* todo_flags_start */
1045 0 /* todo_flags_finish */
1049 /* Delete created points-to sets. */
1052 delete_alias_vars (void)
1056 if (flag_tree_points_to
!= PTA_ANDERSEN
)
1059 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (local_alias_vars
); i
++)
1061 tree key
= VARRAY_TREE (local_alias_vars
, i
);
1063 DECL_PTA_ALIASVAR (key
) = NULL
;
1068 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (local_alias_varnums
); i
++)
1069 VARRAY_GENERIC_PTR (alias_vars
, VARRAY_INT (local_alias_varnums
, i
)) = NULL
;
1070 if (!current_alias_ops
->ip
&& !current_alias_ops
->ip_partial
)
1072 /* VARRAY_CLEAR (alias_vars); */
1073 VARRAY_CLEAR (local_alias_vars
);
1074 VARRAY_CLEAR (local_alias_varnums
);
1076 BITMAP_XFREE (addrargs
);
1077 current_alias_ops
->cleanup (current_alias_ops
);
1080 struct tree_opt_pass pass_del_pta
=
1084 delete_alias_vars
, /* execute */
1087 0, /* static_pass_number */
1088 TV_TREE_PTA
, /* tv_id */
1089 PROP_pta
, /* properties_required */
1090 0, /* properties_provided */
1091 PROP_pta
, /* properties_destroyed */
1092 0, /* todo_flags_start */
1093 0 /* todo_flags_finish */
1097 /* Initialize points-to analysis machinery. */
1100 init_alias_vars (void)
1102 current_alias_ops
->init (current_alias_ops
);
1103 addrargs
= BITMAP_XMALLOC ();
1104 VARRAY_TREE_INIT (local_alias_vars
, 10, "Local alias vars");
1105 VARRAY_INT_INIT (local_alias_varnums
, 10, "Local alias varnums");
1106 if ((!current_alias_ops
->ip
&& !current_alias_ops
->ip_partial
)
1107 || alias_vars
== NULL
)
1108 VARRAY_GENERIC_PTR_INIT (alias_vars
, 10, "Alias vars");
1111 /* Return true if PTR can't point to anything (i.e. it has an empty
1114 empty_points_to_set (tree ptr
)
1120 if (TREE_CODE (ptr
) == COMPONENT_REF
)
1121 ptr
= TREE_OPERAND (ptr
, 1);
1126 ptrtv
= DECL_PTA_ALIASVAR (ptr
);
1133 return current_alias_ops
->empty_points_to_set (current_alias_ops
, ptrtv
);
1136 /* Return true if PTR and VAR have the same points-to set. */
1139 same_points_to_set (tree ptr
, tree var
)
1141 alias_var ptrtv
, vartv
;
1145 if (TREE_CODE (ptr
) == COMPONENT_REF
)
1146 ptr
= TREE_OPERAND (ptr
, 1);
1147 if (TREE_CODE (var
) == COMPONENT_REF
)
1148 var
= TREE_OPERAND (var
, 1);
1156 ptrtv
= DECL_PTA_ALIASVAR (ptr
);
1165 vartv
= DECL_PTA_ALIASVAR (var
);
1172 return current_alias_ops
->same_points_to_set (current_alias_ops
, vartv
, ptrtv
);
1175 /* Determine whether two variables (PTR and VAR) may-alias.
1176 Returns TRUE if PTR may-alias VAR. */
1179 ptr_may_alias_var (tree ptr
, tree var
)
1181 alias_var ptrtv
, vartv
;
1185 if (TREE_CODE (ptr
) == COMPONENT_REF
)
1186 ptr
= TREE_OPERAND (ptr
, 1);
1187 if (TREE_CODE (var
) == COMPONENT_REF
)
1188 var
= TREE_OPERAND (var
, 1);
1196 ptrtv
= DECL_PTA_ALIASVAR (ptr
);
1205 vartv
= DECL_PTA_ALIASVAR (var
);
1212 return current_alias_ops
->may_alias (current_alias_ops
, ptrtv
, vartv
);
1215 #define MASK_POINTER(P) ((unsigned)((unsigned long)(P) & 0xffff))
1218 alias_get_name (tree t
)
1223 if (TREE_CODE (t
) == FIELD_DECL
)
1225 /* First get the name of the field, then the prefix, then smash them
1227 const char *fieldname
= IDENTIFIER_POINTER (DECL_NAME (t
));
1228 const char *prefix
= alias_get_name (DECL_CONTEXT (t
));
1230 size_t neededlen
= strlen (fieldname
) + strlen (prefix
) + 2;
1231 smashed
= ggc_alloc (neededlen
);
1232 sprintf (smashed
, "%s.%s", prefix
, fieldname
);
1236 else if (TYPE_P (t
))
1238 if (TYPE_NAME (t
) && IDENTIFIER_POINTER (TYPE_NAME (t
)))
1239 name
= IDENTIFIER_POINTER (TYPE_NAME (t
));
1241 name
= "<unnamed type>";
1246 if (TREE_CODE (t
) == FUNCTION_DECL
)
1247 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
1248 else if (TREE_CODE (t
) == RESULT_DECL
)
1249 name
= "<return value>";
1251 name
= get_name (t
);
1258 4 = the masked pointer
1259 2 = the <> around it
1260 1 = the terminator. */
1261 namep
= ggc_alloc (2 + 4 + 2 + 1);
1262 sprintf (namep
, "<UV%x>", MASK_POINTER (t
));
1269 #include "gt-tree-alias-common.h"