gcc:
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobcee6502ffea40ff41ae1ee6c00504ee0799316af
1 /* Tree based points-to analysis
2 Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "obstack.h"
28 #include "bitmap.h"
29 #include "flags.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "output.h"
35 #include "errors.h"
36 #include "diagnostic.h"
37 #include "tree.h"
38 #include "c-common.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "varray.h"
42 #include "c-tree.h"
43 #include "tree-gimple.h"
44 #include "hashtab.h"
45 #include "function.h"
46 #include "cgraph.h"
47 #include "tree-pass.h"
48 #include "timevar.h"
49 #include "alloc-pool.h"
50 #include "splay-tree.h"
51 #include "tree-ssa-structalias.h"
53 /* The idea behind this analyzer is to generate set constraints from the
54 program, then solve the resulting constraints in order to generate the
55 points-to sets.
57 Set constraints are a way of modeling program analysis problems that
58 involve sets. They consist of an inclusion constraint language,
59 describing the variables (each variable is a set) and operations that
60 are involved on the variables, and a set of rules that derive facts
61 from these operations. To solve a system of set constraints, you derive
62 all possible facts under the rules, which gives you the correct sets
63 as a consequence.
65 See "Efficient Field-sensitive pointer analysis for C" by "David
66 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
67 http://citeseer.ist.psu.edu/pearce04efficient.html
69 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
70 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
71 http://citeseer.ist.psu.edu/heintze01ultrafast.html
73 There are three types of constraint expressions, DEREF, ADDRESSOF, and
74 SCALAR. Each constraint expression consists of a constraint type,
75 a variable, and an offset.
77 SCALAR is a constraint expression type used to represent x, whether
78 it appears on the LHS or the RHS of a statement.
79 DEREF is a constraint expression type used to represent *x, whether
80 it appears on the LHS or the RHS of a statement.
81 ADDRESSOF is a constraint expression used to represent &x, whether
82 it appears on the LHS or the RHS of a statement.
84 Each pointer variable in the program is assigned an integer id, and
85 each field of a structure variable is assigned an integer id as well.
87 Structure variables are linked to their list of fields through a "next
88 field" in each variable that points to the next field in offset
89 order.
90 Each variable for a structure field has
92 1. "size", that tells the size in bits of that field.
93 2. "fullsize, that tells the size in bits of the entire structure.
94 3. "offset", that tells the offset in bits from the beginning of the
95 structure to this field.
97 Thus,
98 struct f
100 int a;
101 int b;
102 } foo;
103 int *bar;
105 looks like
107 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
108 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
109 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
112 In order to solve the system of set constraints, the following is
113 done:
115 1. Each constraint variable x has a solution set associated with it,
116 Sol(x).
118 2. Constraints are separated into direct, copy, and complex.
119 Direct constraints are ADDRESSOF constraints that require no extra
120 processing, such as P = &Q
121 Copy constraints are those of the form P = Q.
122 Complex constraints are all the constraints involving dereferences.
124 3. All direct constraints of the form P = &Q are processed, such
125 that Q is added to Sol(P)
127 4. All complex constraints for a given constraint variable are stored in a
128 linked list attached to that variable's node.
130 5. A directed graph is built out of the copy constraints. Each
131 constraint variable is a node in the graph, and an edge from
132 Q to P is added for each copy constraint of the form P = Q
134 6. The graph is then walked, and solution sets are
135 propagated along the copy edges, such that an edge from Q to P
136 causes Sol(P) <- Sol(P) union Sol(Q).
138 7. As we visit each node, all complex constraints associated with
139 that node are processed by adding appropriate copy edges to the graph, or the
140 appropriate variables to the solution set.
142 8. The process of walking the graph is iterated until no solution
143 sets change.
145 Prior to walking the graph in steps 6 and 7, We perform static
146 cycle elimination on the constraint graph, as well
147 as off-line variable substitution.
149 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
150 on and turned into anything), but isn't. You can just see what offset
151 inside the pointed-to struct it's going to access.
153 TODO: Constant bounded arrays can be handled as if they were structs of the
154 same number of elements.
156 TODO: Modeling heap and incoming pointers becomes much better if we
157 add fields to them as we discover them, which we could do.
159 TODO: We could handle unions, but to be honest, it's probably not
160 worth the pain or slowdown. */
162 static bool use_field_sensitive = true;
163 static unsigned int create_variable_info_for (tree, const char *);
164 static struct constraint_expr get_constraint_for (tree, bool *);
165 static void build_constraint_graph (void);
167 static bitmap_obstack ptabitmap_obstack;
168 static bitmap_obstack iteration_obstack;
169 DEF_VEC_P(constraint_t);
170 DEF_VEC_ALLOC_P(constraint_t,heap);
172 static struct constraint_stats
174 unsigned int total_vars;
175 unsigned int collapsed_vars;
176 unsigned int unified_vars_static;
177 unsigned int unified_vars_dynamic;
178 unsigned int iterations;
179 } stats;
181 struct variable_info
183 /* ID of this variable */
184 unsigned int id;
186 /* Name of this variable */
187 const char *name;
189 /* Tree that this variable is associated with. */
190 tree decl;
192 /* Offset of this variable, in bits, from the base variable */
193 unsigned HOST_WIDE_INT offset;
195 /* Size of the variable, in bits. */
196 unsigned HOST_WIDE_INT size;
198 /* Full size of the base variable, in bits. */
199 unsigned HOST_WIDE_INT fullsize;
201 /* A link to the variable for the next field in this structure. */
202 struct variable_info *next;
204 /* Node in the graph that represents the constraints and points-to
205 solution for the variable. */
206 unsigned int node;
208 /* True if the address of this variable is taken. Needed for
209 variable substitution. */
210 unsigned int address_taken:1;
212 /* True if this variable is the target of a dereference. Needed for
213 variable substitution. */
214 unsigned int indirect_target:1;
216 /* True if this is a variable created by the constraint analysis, such as
217 heap variables and constraints we had to break up. */
218 unsigned int is_artificial_var:1;
220 /* True if this is a special variable whose solution set should not be
221 changed. */
222 unsigned int is_special_var:1;
224 /* True for variables whose size is not known or variable. */
225 unsigned int is_unknown_size_var:1;
227 /* True for variables that have unions somewhere in them. */
228 unsigned int has_union:1;
230 /* True if this is a heap variable. */
231 unsigned int is_heap_var:1;
233 /* Points-to set for this variable. */
234 bitmap solution;
236 /* Variable ids represented by this node. */
237 bitmap variables;
239 /* Vector of complex constraints for this node. Complex
240 constraints are those involving dereferences. */
241 VEC(constraint_t,heap) *complex;
243 /* Variable id this was collapsed to due to type unsafety.
244 This should be unused completely after build_constraint_graph, or
245 something is broken. */
246 struct variable_info *collapsed_to;
248 typedef struct variable_info *varinfo_t;
250 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
252 /* Pool of variable info structures. */
253 static alloc_pool variable_info_pool;
255 DEF_VEC_P(varinfo_t);
257 DEF_VEC_ALLOC_P(varinfo_t, heap);
259 /* Table of variable info structures for constraint variables. Indexed directly
260 by variable info id. */
261 static VEC(varinfo_t,heap) *varmap;
263 /* Return the varmap element N */
265 static inline varinfo_t
266 get_varinfo (unsigned int n)
268 return VEC_index(varinfo_t, varmap, n);
271 /* Return the varmap element N, following the collapsed_to link. */
273 static inline varinfo_t
274 get_varinfo_fc (unsigned int n)
276 varinfo_t v = VEC_index(varinfo_t, varmap, n);
278 if (v->collapsed_to)
279 return v->collapsed_to;
280 return v;
283 /* Variable that represents the unknown pointer. */
284 static varinfo_t var_anything;
285 static tree anything_tree;
286 static unsigned int anything_id;
288 /* Variable that represents the NULL pointer. */
289 static varinfo_t var_nothing;
290 static tree nothing_tree;
291 static unsigned int nothing_id;
293 /* Variable that represents read only memory. */
294 static varinfo_t var_readonly;
295 static tree readonly_tree;
296 static unsigned int readonly_id;
298 /* Variable that represents integers. This is used for when people do things
299 like &0->a.b. */
300 static varinfo_t var_integer;
301 static tree integer_tree;
302 static unsigned int integer_id;
304 /* Variable that represents arbitrary offsets into an object. Used to
305 represent pointer arithmetic, which may not legally escape the
306 bounds of an object. */
307 static varinfo_t var_anyoffset;
308 static tree anyoffset_tree;
309 static unsigned int anyoffset_id;
311 /* Return a new variable info structure consisting for a variable
312 named NAME, and using constraint graph node NODE. */
314 static varinfo_t
315 new_var_info (tree t, unsigned int id, const char *name, unsigned int node)
317 varinfo_t ret = pool_alloc (variable_info_pool);
319 ret->id = id;
320 ret->name = name;
321 ret->decl = t;
322 ret->node = node;
323 ret->address_taken = false;
324 ret->indirect_target = false;
325 ret->is_artificial_var = false;
326 ret->is_heap_var = false;
327 ret->is_special_var = false;
328 ret->is_unknown_size_var = false;
329 ret->has_union = false;
330 ret->solution = BITMAP_ALLOC (&ptabitmap_obstack);
331 bitmap_clear (ret->solution);
332 ret->variables = BITMAP_ALLOC (&ptabitmap_obstack);
333 bitmap_clear (ret->variables);
334 ret->complex = NULL;
335 ret->next = NULL;
336 ret->collapsed_to = NULL;
337 return ret;
340 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
342 /* An expression that appears in a constraint. */
344 struct constraint_expr
346 /* Constraint type. */
347 constraint_expr_type type;
349 /* Variable we are referring to in the constraint. */
350 unsigned int var;
352 /* Offset, in bits, of this constraint from the beginning of
353 variables it ends up referring to.
355 IOW, in a deref constraint, we would deref, get the result set,
356 then add OFFSET to each member. */
357 unsigned HOST_WIDE_INT offset;
360 static struct constraint_expr do_deref (struct constraint_expr);
362 /* Our set constraints are made up of two constraint expressions, one
363 LHS, and one RHS.
365 As described in the introduction, our set constraints each represent an
366 operation between set valued variables.
368 struct constraint
370 struct constraint_expr lhs;
371 struct constraint_expr rhs;
374 /* List of constraints that we use to build the constraint graph from. */
376 static VEC(constraint_t,heap) *constraints;
377 static alloc_pool constraint_pool;
379 /* An edge in the constraint graph. We technically have no use for
380 the src, since it will always be the same node that we are indexing
381 into the pred/succ arrays with, but it's nice for checking
382 purposes. The edges are weighted, with a bit set in weights for
383 each edge from src to dest with that weight. */
385 struct constraint_edge
387 unsigned int src;
388 unsigned int dest;
389 bitmap weights;
392 typedef struct constraint_edge *constraint_edge_t;
393 static alloc_pool constraint_edge_pool;
395 /* Return a new constraint edge from SRC to DEST. */
397 static constraint_edge_t
398 new_constraint_edge (unsigned int src, unsigned int dest)
400 constraint_edge_t ret = pool_alloc (constraint_edge_pool);
401 ret->src = src;
402 ret->dest = dest;
403 ret->weights = NULL;
404 return ret;
407 DEF_VEC_P(constraint_edge_t);
408 DEF_VEC_ALLOC_P(constraint_edge_t,heap);
411 /* The constraint graph is simply a set of adjacency vectors, one per
412 variable. succs[x] is the vector of successors for variable x, and preds[x]
413 is the vector of predecessors for variable x.
414 IOW, all edges are "forward" edges, which is not like our CFG.
415 So remember that
416 preds[x]->src == x, and
417 succs[x]->src == x. */
419 struct constraint_graph
421 VEC(constraint_edge_t,heap) **succs;
422 VEC(constraint_edge_t,heap) **preds;
425 typedef struct constraint_graph *constraint_graph_t;
427 static constraint_graph_t graph;
429 /* Create a new constraint consisting of LHS and RHS expressions. */
431 static constraint_t
432 new_constraint (const struct constraint_expr lhs,
433 const struct constraint_expr rhs)
435 constraint_t ret = pool_alloc (constraint_pool);
436 ret->lhs = lhs;
437 ret->rhs = rhs;
438 return ret;
441 /* Print out constraint C to FILE. */
443 void
444 dump_constraint (FILE *file, constraint_t c)
446 if (c->lhs.type == ADDRESSOF)
447 fprintf (file, "&");
448 else if (c->lhs.type == DEREF)
449 fprintf (file, "*");
450 fprintf (file, "%s", get_varinfo_fc (c->lhs.var)->name);
451 if (c->lhs.offset != 0)
452 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
453 fprintf (file, " = ");
454 if (c->rhs.type == ADDRESSOF)
455 fprintf (file, "&");
456 else if (c->rhs.type == DEREF)
457 fprintf (file, "*");
458 fprintf (file, "%s", get_varinfo_fc (c->rhs.var)->name);
459 if (c->rhs.offset != 0)
460 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
461 fprintf (file, "\n");
464 /* Print out constraint C to stderr. */
466 void
467 debug_constraint (constraint_t c)
469 dump_constraint (stderr, c);
472 /* Print out all constraints to FILE */
474 void
475 dump_constraints (FILE *file)
477 int i;
478 constraint_t c;
479 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
480 dump_constraint (file, c);
483 /* Print out all constraints to stderr. */
485 void
486 debug_constraints (void)
488 dump_constraints (stderr);
491 /* SOLVER FUNCTIONS
493 The solver is a simple worklist solver, that works on the following
494 algorithm:
496 sbitmap changed_nodes = all ones;
497 changed_count = number of nodes;
498 For each node that was already collapsed:
499 changed_count--;
502 while (changed_count > 0)
504 compute topological ordering for constraint graph
506 find and collapse cycles in the constraint graph (updating
507 changed if necessary)
509 for each node (n) in the graph in topological order:
510 changed_count--;
512 Process each complex constraint associated with the node,
513 updating changed if necessary.
515 For each outgoing edge from n, propagate the solution from n to
516 the destination of the edge, updating changed as necessary.
518 } */
520 /* Return true if two constraint expressions A and B are equal. */
522 static bool
523 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
525 return a.type == b.type
526 && a.var == b.var
527 && a.offset == b.offset;
530 /* Return true if constraint expression A is less than constraint expression
531 B. This is just arbitrary, but consistent, in order to give them an
532 ordering. */
534 static bool
535 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
537 if (a.type == b.type)
539 if (a.var == b.var)
540 return a.offset < b.offset;
541 else
542 return a.var < b.var;
544 else
545 return a.type < b.type;
548 /* Return true if constraint A is less than constraint B. This is just
549 arbitrary, but consistent, in order to give them an ordering. */
551 static bool
552 constraint_less (const constraint_t a, const constraint_t b)
554 if (constraint_expr_less (a->lhs, b->lhs))
555 return true;
556 else if (constraint_expr_less (b->lhs, a->lhs))
557 return false;
558 else
559 return constraint_expr_less (a->rhs, b->rhs);
562 /* Return true if two constraints A and B are equal. */
564 static bool
565 constraint_equal (struct constraint a, struct constraint b)
567 return constraint_expr_equal (a.lhs, b.lhs)
568 && constraint_expr_equal (a.rhs, b.rhs);
572 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
574 static constraint_t
575 constraint_vec_find (VEC(constraint_t,heap) *vec,
576 struct constraint lookfor)
578 unsigned int place;
579 constraint_t found;
581 if (vec == NULL)
582 return NULL;
584 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
585 if (place >= VEC_length (constraint_t, vec))
586 return NULL;
587 found = VEC_index (constraint_t, vec, place);
588 if (!constraint_equal (*found, lookfor))
589 return NULL;
590 return found;
593 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
595 static void
596 constraint_set_union (VEC(constraint_t,heap) **to,
597 VEC(constraint_t,heap) **from)
599 int i;
600 constraint_t c;
602 for (i = 0; VEC_iterate (constraint_t, *from, i, c); i++)
604 if (constraint_vec_find (*to, *c) == NULL)
606 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
607 constraint_less);
608 VEC_safe_insert (constraint_t, heap, *to, place, c);
613 /* Take a solution set SET, add OFFSET to each member of the set, and
614 overwrite SET with the result when done. */
616 static void
617 solution_set_add (bitmap set, unsigned HOST_WIDE_INT offset)
619 bitmap result = BITMAP_ALLOC (&iteration_obstack);
620 unsigned int i;
621 bitmap_iterator bi;
623 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
625 /* If this is a properly sized variable, only add offset if it's
626 less than end. Otherwise, it is globbed to a single
627 variable. */
629 if ((get_varinfo (i)->offset + offset) < get_varinfo (i)->fullsize)
631 unsigned HOST_WIDE_INT fieldoffset = get_varinfo (i)->offset + offset;
632 varinfo_t v = first_vi_for_offset (get_varinfo (i), fieldoffset);
633 if (!v)
634 continue;
635 bitmap_set_bit (result, v->id);
637 else if (get_varinfo (i)->is_artificial_var
638 || get_varinfo (i)->has_union
639 || get_varinfo (i)->is_unknown_size_var)
641 bitmap_set_bit (result, i);
645 bitmap_copy (set, result);
646 BITMAP_FREE (result);
649 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
650 process. */
652 static bool
653 set_union_with_increment (bitmap to, bitmap from, unsigned HOST_WIDE_INT inc)
655 if (inc == 0)
656 return bitmap_ior_into (to, from);
657 else
659 bitmap tmp;
660 bool res;
662 tmp = BITMAP_ALLOC (&iteration_obstack);
663 bitmap_copy (tmp, from);
664 solution_set_add (tmp, inc);
665 res = bitmap_ior_into (to, tmp);
666 BITMAP_FREE (tmp);
667 return res;
671 /* Insert constraint C into the list of complex constraints for VAR. */
673 static void
674 insert_into_complex (unsigned int var, constraint_t c)
676 varinfo_t vi = get_varinfo (var);
677 unsigned int place = VEC_lower_bound (constraint_t, vi->complex, c,
678 constraint_less);
679 VEC_safe_insert (constraint_t, heap, vi->complex, place, c);
683 /* Compare two constraint edges A and B, return true if they are equal. */
685 static bool
686 constraint_edge_equal (struct constraint_edge a, struct constraint_edge b)
688 return a.src == b.src && a.dest == b.dest;
691 /* Compare two constraint edges, return true if A is less than B */
693 static bool
694 constraint_edge_less (const constraint_edge_t a, const constraint_edge_t b)
696 if (a->dest < b->dest)
697 return true;
698 else if (a->dest == b->dest)
699 return a->src < b->src;
700 else
701 return false;
704 /* Find the constraint edge that matches LOOKFOR, in VEC.
705 Return the edge, if found, NULL otherwise. */
707 static constraint_edge_t
708 constraint_edge_vec_find (VEC(constraint_edge_t,heap) *vec,
709 struct constraint_edge lookfor)
711 unsigned int place;
712 constraint_edge_t edge;
714 place = VEC_lower_bound (constraint_edge_t, vec, &lookfor,
715 constraint_edge_less);
716 edge = VEC_index (constraint_edge_t, vec, place);
717 if (!constraint_edge_equal (*edge, lookfor))
718 return NULL;
719 return edge;
722 /* Condense two variable nodes into a single variable node, by moving
723 all associated info from SRC to TO. */
725 static void
726 condense_varmap_nodes (unsigned int to, unsigned int src)
728 varinfo_t tovi = get_varinfo (to);
729 varinfo_t srcvi = get_varinfo (src);
730 unsigned int i;
731 constraint_t c;
732 bitmap_iterator bi;
734 /* the src node, and all its variables, are now the to node. */
735 srcvi->node = to;
736 EXECUTE_IF_SET_IN_BITMAP (srcvi->variables, 0, i, bi)
737 get_varinfo (i)->node = to;
739 /* Merge the src node variables and the to node variables. */
740 bitmap_set_bit (tovi->variables, src);
741 bitmap_ior_into (tovi->variables, srcvi->variables);
742 bitmap_clear (srcvi->variables);
744 /* Move all complex constraints from src node into to node */
745 for (i = 0; VEC_iterate (constraint_t, srcvi->complex, i, c); i++)
747 /* In complex constraints for node src, we may have either
748 a = *src, and *src = a. */
750 if (c->rhs.type == DEREF)
751 c->rhs.var = to;
752 else
753 c->lhs.var = to;
755 constraint_set_union (&tovi->complex, &srcvi->complex);
756 VEC_free (constraint_t, heap, srcvi->complex);
757 srcvi->complex = NULL;
760 /* Erase EDGE from GRAPH. This routine only handles self-edges
761 (e.g. an edge from a to a). */
763 static void
764 erase_graph_self_edge (constraint_graph_t graph, struct constraint_edge edge)
766 VEC(constraint_edge_t,heap) *predvec = graph->preds[edge.src];
767 VEC(constraint_edge_t,heap) *succvec = graph->succs[edge.dest];
768 unsigned int place;
769 gcc_assert (edge.src == edge.dest);
771 /* Remove from the successors. */
772 place = VEC_lower_bound (constraint_edge_t, succvec, &edge,
773 constraint_edge_less);
775 /* Make sure we found the edge. */
776 #ifdef ENABLE_CHECKING
778 constraint_edge_t tmp = VEC_index (constraint_edge_t, succvec, place);
779 gcc_assert (constraint_edge_equal (*tmp, edge));
781 #endif
782 VEC_ordered_remove (constraint_edge_t, succvec, place);
784 /* Remove from the predecessors. */
785 place = VEC_lower_bound (constraint_edge_t, predvec, &edge,
786 constraint_edge_less);
788 /* Make sure we found the edge. */
789 #ifdef ENABLE_CHECKING
791 constraint_edge_t tmp = VEC_index (constraint_edge_t, predvec, place);
792 gcc_assert (constraint_edge_equal (*tmp, edge));
794 #endif
795 VEC_ordered_remove (constraint_edge_t, predvec, place);
798 /* Remove edges involving NODE from GRAPH. */
800 static void
801 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
803 VEC(constraint_edge_t,heap) *succvec = graph->succs[node];
804 VEC(constraint_edge_t,heap) *predvec = graph->preds[node];
805 constraint_edge_t c;
806 int i;
808 /* Walk the successors, erase the associated preds. */
809 for (i = 0; VEC_iterate (constraint_edge_t, succvec, i, c); i++)
810 if (c->dest != node)
812 unsigned int place;
813 struct constraint_edge lookfor;
814 lookfor.src = c->dest;
815 lookfor.dest = node;
816 place = VEC_lower_bound (constraint_edge_t, graph->preds[c->dest],
817 &lookfor, constraint_edge_less);
818 VEC_ordered_remove (constraint_edge_t, graph->preds[c->dest], place);
820 /* Walk the preds, erase the associated succs. */
821 for (i =0; VEC_iterate (constraint_edge_t, predvec, i, c); i++)
822 if (c->dest != node)
824 unsigned int place;
825 struct constraint_edge lookfor;
826 lookfor.src = c->dest;
827 lookfor.dest = node;
828 place = VEC_lower_bound (constraint_edge_t, graph->succs[c->dest],
829 &lookfor, constraint_edge_less);
830 VEC_ordered_remove (constraint_edge_t, graph->succs[c->dest], place);
833 VEC_free (constraint_edge_t, heap, graph->preds[node]);
834 VEC_free (constraint_edge_t, heap, graph->succs[node]);
835 graph->preds[node] = NULL;
836 graph->succs[node] = NULL;
839 static bool edge_added = false;
841 /* Add edge NEWE to the graph. */
843 static bool
844 add_graph_edge (constraint_graph_t graph, struct constraint_edge newe)
846 unsigned int place;
847 unsigned int src = newe.src;
848 unsigned int dest = newe.dest;
849 VEC(constraint_edge_t,heap) *vec;
851 vec = graph->preds[src];
852 place = VEC_lower_bound (constraint_edge_t, vec, &newe,
853 constraint_edge_less);
854 if (place == VEC_length (constraint_edge_t, vec)
855 || VEC_index (constraint_edge_t, vec, place)->dest != dest)
857 constraint_edge_t edge = new_constraint_edge (src, dest);
858 bitmap weightbitmap;
860 weightbitmap = BITMAP_ALLOC (&ptabitmap_obstack);
861 edge->weights = weightbitmap;
862 VEC_safe_insert (constraint_edge_t, heap, graph->preds[edge->src],
863 place, edge);
864 edge = new_constraint_edge (dest, src);
865 edge->weights = weightbitmap;
866 place = VEC_lower_bound (constraint_edge_t, graph->succs[edge->src],
867 edge, constraint_edge_less);
868 VEC_safe_insert (constraint_edge_t, heap, graph->succs[edge->src],
869 place, edge);
870 edge_added = true;
871 return true;
873 else
874 return false;
878 /* Return the bitmap representing the weights of edge LOOKFOR */
880 static bitmap
881 get_graph_weights (constraint_graph_t graph, struct constraint_edge lookfor)
883 constraint_edge_t edge;
884 unsigned int src = lookfor.src;
885 VEC(constraint_edge_t,heap) *vec;
886 vec = graph->preds[src];
887 edge = constraint_edge_vec_find (vec, lookfor);
888 gcc_assert (edge != NULL);
889 return edge->weights;
893 /* Merge GRAPH nodes FROM and TO into node TO. */
895 static void
896 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
897 unsigned int from)
899 VEC(constraint_edge_t,heap) *succvec = graph->succs[from];
900 VEC(constraint_edge_t,heap) *predvec = graph->preds[from];
901 int i;
902 constraint_edge_t c;
904 /* Merge all the predecessor edges. */
906 for (i = 0; VEC_iterate (constraint_edge_t, predvec, i, c); i++)
908 unsigned int d = c->dest;
909 struct constraint_edge olde;
910 struct constraint_edge newe;
911 bitmap temp;
912 bitmap weights;
913 if (c->dest == from)
914 d = to;
915 newe.src = to;
916 newe.dest = d;
917 add_graph_edge (graph, newe);
918 olde.src = from;
919 olde.dest = c->dest;
920 olde.weights = NULL;
921 temp = get_graph_weights (graph, olde);
922 weights = get_graph_weights (graph, newe);
923 bitmap_ior_into (weights, temp);
926 /* Merge all the successor edges. */
927 for (i = 0; VEC_iterate (constraint_edge_t, succvec, i, c); i++)
929 unsigned int d = c->dest;
930 struct constraint_edge olde;
931 struct constraint_edge newe;
932 bitmap temp;
933 bitmap weights;
934 if (c->dest == from)
935 d = to;
936 newe.src = d;
937 newe.dest = to;
938 add_graph_edge (graph, newe);
939 olde.src = c->dest;
940 olde.dest = from;
941 olde.weights = NULL;
942 temp = get_graph_weights (graph, olde);
943 weights = get_graph_weights (graph, newe);
944 bitmap_ior_into (weights, temp);
946 clear_edges_for_node (graph, from);
949 /* Add a graph edge to GRAPH, going from TO to FROM, with WEIGHT, if
950 it doesn't exist in the graph already.
951 Return false if the edge already existed, true otherwise. */
953 static bool
954 int_add_graph_edge (constraint_graph_t graph, unsigned int to,
955 unsigned int from, unsigned HOST_WIDE_INT weight)
957 if (to == from && weight == 0)
959 return false;
961 else
963 bool r;
964 struct constraint_edge edge;
965 edge.src = to;
966 edge.dest = from;
967 edge.weights = NULL;
968 r = add_graph_edge (graph, edge);
969 r |= !bitmap_bit_p (get_graph_weights (graph, edge), weight);
970 bitmap_set_bit (get_graph_weights (graph, edge), weight);
971 return r;
976 /* Return true if LOOKFOR is an existing graph edge. */
978 static bool
979 valid_graph_edge (constraint_graph_t graph, struct constraint_edge lookfor)
981 return constraint_edge_vec_find (graph->preds[lookfor.src], lookfor) != NULL;
985 /* Build the constraint graph. */
987 static void
988 build_constraint_graph (void)
990 int i = 0;
991 constraint_t c;
993 graph = xmalloc (sizeof (struct constraint_graph));
994 graph->succs = xcalloc (VEC_length (varinfo_t, varmap),
995 sizeof (*graph->succs));
996 graph->preds = xcalloc (VEC_length (varinfo_t, varmap),
997 sizeof (*graph->preds));
999 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1001 struct constraint_expr lhs = c->lhs;
1002 struct constraint_expr rhs = c->rhs;
1003 unsigned int lhsvar = get_varinfo_fc (lhs.var)->id;
1004 unsigned int rhsvar = get_varinfo_fc (rhs.var)->id;
1006 if (lhs.type == DEREF)
1008 /* *x = y or *x = &y (complex) */
1009 if (rhs.type == ADDRESSOF || rhsvar > anything_id)
1010 insert_into_complex (lhsvar, c);
1012 else if (rhs.type == DEREF)
1014 /* !special var= *y */
1015 if (!(get_varinfo (lhsvar)->is_special_var))
1016 insert_into_complex (rhsvar, c);
1018 else if (rhs.type == ADDRESSOF)
1020 /* x = &y */
1021 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1023 else if (lhsvar > anything_id)
1025 /* Ignore 0 weighted self edges, as they can't possibly contribute
1026 anything */
1027 if (lhsvar != rhsvar || rhs.offset != 0 || lhs.offset != 0)
1030 struct constraint_edge edge;
1031 edge.src = lhsvar;
1032 edge.dest = rhsvar;
1033 /* x = y (simple) */
1034 add_graph_edge (graph, edge);
1035 bitmap_set_bit (get_graph_weights (graph, edge),
1036 rhs.offset);
1044 /* Changed variables on the last iteration. */
1045 static unsigned int changed_count;
1046 static sbitmap changed;
1048 DEF_VEC_I(unsigned);
1049 DEF_VEC_ALLOC_I(unsigned,heap);
1052 /* Strongly Connected Component visitation info. */
1054 struct scc_info
1056 sbitmap visited;
1057 sbitmap in_component;
1058 int current_index;
1059 unsigned int *visited_index;
1060 VEC(unsigned,heap) *scc_stack;
1061 VEC(unsigned,heap) *unification_queue;
1065 /* Recursive routine to find strongly connected components in GRAPH.
1066 SI is the SCC info to store the information in, and N is the id of current
1067 graph node we are processing.
1069 This is Tarjan's strongly connected component finding algorithm, as
1070 modified by Nuutila to keep only non-root nodes on the stack.
1071 The algorithm can be found in "On finding the strongly connected
1072 connected components in a directed graph" by Esko Nuutila and Eljas
1073 Soisalon-Soininen, in Information Processing Letters volume 49,
1074 number 1, pages 9-14. */
1076 static void
1077 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1079 constraint_edge_t c;
1080 int i;
1082 gcc_assert (get_varinfo (n)->node == n);
1083 SET_BIT (si->visited, n);
1084 RESET_BIT (si->in_component, n);
1085 si->visited_index[n] = si->current_index ++;
1087 /* Visit all the successors. */
1088 for (i = 0; VEC_iterate (constraint_edge_t, graph->succs[n], i, c); i++)
1090 /* We only want to find and collapse the zero weight edges. */
1091 if (bitmap_bit_p (c->weights, 0))
1093 unsigned int w = c->dest;
1094 if (!TEST_BIT (si->visited, w))
1095 scc_visit (graph, si, w);
1096 if (!TEST_BIT (si->in_component, w))
1098 unsigned int t = get_varinfo (w)->node;
1099 unsigned int nnode = get_varinfo (n)->node;
1100 if (si->visited_index[t] < si->visited_index[nnode])
1101 get_varinfo (n)->node = t;
1106 /* See if any components have been identified. */
1107 if (get_varinfo (n)->node == n)
1109 unsigned int t = si->visited_index[n];
1110 SET_BIT (si->in_component, n);
1111 while (VEC_length (unsigned, si->scc_stack) != 0
1112 && t < si->visited_index[VEC_last (unsigned, si->scc_stack)])
1114 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1115 get_varinfo (w)->node = n;
1116 SET_BIT (si->in_component, w);
1117 /* Mark this node for collapsing. */
1118 VEC_safe_push (unsigned, heap, si->unification_queue, w);
1121 else
1122 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1126 /* Collapse two variables into one variable. */
1128 static void
1129 collapse_nodes (constraint_graph_t graph, unsigned int to, unsigned int from)
1131 bitmap tosol, fromsol;
1132 struct constraint_edge edge;
1135 condense_varmap_nodes (to, from);
1136 tosol = get_varinfo (to)->solution;
1137 fromsol = get_varinfo (from)->solution;
1138 bitmap_ior_into (tosol, fromsol);
1139 merge_graph_nodes (graph, to, from);
1140 edge.src = to;
1141 edge.dest = to;
1142 edge.weights = NULL;
1143 if (valid_graph_edge (graph, edge))
1145 bitmap weights = get_graph_weights (graph, edge);
1146 bitmap_clear_bit (weights, 0);
1147 if (bitmap_empty_p (weights))
1148 erase_graph_self_edge (graph, edge);
1150 bitmap_clear (fromsol);
1151 get_varinfo (to)->address_taken |= get_varinfo (from)->address_taken;
1152 get_varinfo (to)->indirect_target |= get_varinfo (from)->indirect_target;
1156 /* Unify nodes in GRAPH that we have found to be part of a cycle.
1157 SI is the Strongly Connected Components information structure that tells us
1158 what components to unify.
1159 UPDATE_CHANGED should be set to true if the changed sbitmap and changed
1160 count should be updated to reflect the unification. */
1162 static void
1163 process_unification_queue (constraint_graph_t graph, struct scc_info *si,
1164 bool update_changed)
1166 size_t i = 0;
1167 bitmap tmp = BITMAP_ALLOC (update_changed ? &iteration_obstack : NULL);
1168 bitmap_clear (tmp);
1170 /* We proceed as follows:
1172 For each component in the queue (components are delineated by
1173 when current_queue_element->node != next_queue_element->node):
1175 rep = representative node for component
1177 For each node (tounify) to be unified in the component,
1178 merge the solution for tounify into tmp bitmap
1180 clear solution for tounify
1182 merge edges from tounify into rep
1184 merge complex constraints from tounify into rep
1186 update changed count to note that tounify will never change
1187 again
1189 Merge tmp into solution for rep, marking rep changed if this
1190 changed rep's solution.
1192 Delete any 0 weighted self-edges we now have for rep. */
1193 while (i != VEC_length (unsigned, si->unification_queue))
1195 unsigned int tounify = VEC_index (unsigned, si->unification_queue, i);
1196 unsigned int n = get_varinfo (tounify)->node;
1198 if (dump_file && (dump_flags & TDF_DETAILS))
1199 fprintf (dump_file, "Unifying %s to %s\n",
1200 get_varinfo (tounify)->name,
1201 get_varinfo (n)->name);
1202 if (update_changed)
1203 stats.unified_vars_dynamic++;
1204 else
1205 stats.unified_vars_static++;
1206 bitmap_ior_into (tmp, get_varinfo (tounify)->solution);
1207 merge_graph_nodes (graph, n, tounify);
1208 condense_varmap_nodes (n, tounify);
1210 if (update_changed && TEST_BIT (changed, tounify))
1212 RESET_BIT (changed, tounify);
1213 if (!TEST_BIT (changed, n))
1214 SET_BIT (changed, n);
1215 else
1217 gcc_assert (changed_count > 0);
1218 changed_count--;
1222 bitmap_clear (get_varinfo (tounify)->solution);
1223 ++i;
1225 /* If we've either finished processing the entire queue, or
1226 finished processing all nodes for component n, update the solution for
1227 n. */
1228 if (i == VEC_length (unsigned, si->unification_queue)
1229 || get_varinfo (VEC_index (unsigned, si->unification_queue, i))->node != n)
1231 struct constraint_edge edge;
1233 /* If the solution changes because of the merging, we need to mark
1234 the variable as changed. */
1235 if (bitmap_ior_into (get_varinfo (n)->solution, tmp))
1237 if (update_changed && !TEST_BIT (changed, n))
1239 SET_BIT (changed, n);
1240 changed_count++;
1243 bitmap_clear (tmp);
1244 edge.src = n;
1245 edge.dest = n;
1246 edge.weights = NULL;
1247 if (valid_graph_edge (graph, edge))
1249 bitmap weights = get_graph_weights (graph, edge);
1250 bitmap_clear_bit (weights, 0);
1251 if (bitmap_empty_p (weights))
1252 erase_graph_self_edge (graph, edge);
1256 BITMAP_FREE (tmp);
1260 /* Information needed to compute the topological ordering of a graph. */
1262 struct topo_info
1264 /* sbitmap of visited nodes. */
1265 sbitmap visited;
1266 /* Array that stores the topological order of the graph, *in
1267 reverse*. */
1268 VEC(unsigned,heap) *topo_order;
1272 /* Initialize and return a topological info structure. */
1274 static struct topo_info *
1275 init_topo_info (void)
1277 size_t size = VEC_length (varinfo_t, varmap);
1278 struct topo_info *ti = xmalloc (sizeof (struct topo_info));
1279 ti->visited = sbitmap_alloc (size);
1280 sbitmap_zero (ti->visited);
1281 ti->topo_order = VEC_alloc (unsigned, heap, 1);
1282 return ti;
1286 /* Free the topological sort info pointed to by TI. */
1288 static void
1289 free_topo_info (struct topo_info *ti)
1291 sbitmap_free (ti->visited);
1292 VEC_free (unsigned, heap, ti->topo_order);
1293 free (ti);
1296 /* Visit the graph in topological order, and store the order in the
1297 topo_info structure. */
1299 static void
1300 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1301 unsigned int n)
1303 VEC(constraint_edge_t,heap) *succs = graph->succs[n];
1304 constraint_edge_t c;
1305 int i;
1306 SET_BIT (ti->visited, n);
1307 for (i = 0; VEC_iterate (constraint_edge_t, succs, i, c); i++)
1309 if (!TEST_BIT (ti->visited, c->dest))
1310 topo_visit (graph, ti, c->dest);
1312 VEC_safe_push (unsigned, heap, ti->topo_order, n);
1315 /* Return true if variable N + OFFSET is a legal field of N. */
1317 static bool
1318 type_safe (unsigned int n, unsigned HOST_WIDE_INT *offset)
1320 varinfo_t ninfo = get_varinfo (n);
1322 /* For things we've globbed to single variables, any offset into the
1323 variable acts like the entire variable, so that it becomes offset
1324 0. */
1325 if (ninfo->is_special_var
1326 || ninfo->is_artificial_var
1327 || ninfo->is_unknown_size_var)
1329 *offset = 0;
1330 return true;
1332 return (get_varinfo (n)->offset + *offset) < get_varinfo (n)->fullsize;
1335 /* Process a constraint C that represents *x = &y. */
1337 static void
1338 do_da_constraint (constraint_graph_t graph ATTRIBUTE_UNUSED,
1339 constraint_t c, bitmap delta)
1341 unsigned int rhs = c->rhs.var;
1342 unsigned int j;
1343 bitmap_iterator bi;
1345 /* For each member j of Delta (Sol(x)), add x to Sol(j) */
1346 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1348 unsigned HOST_WIDE_INT offset = c->lhs.offset;
1349 if (type_safe (j, &offset) && !(get_varinfo (j)->is_special_var))
1351 /* *x != NULL && *x != ANYTHING*/
1352 varinfo_t v;
1353 unsigned int t;
1354 bitmap sol;
1355 unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + offset;
1357 v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1358 if (!v)
1359 continue;
1360 t = v->node;
1361 sol = get_varinfo (t)->solution;
1362 if (!bitmap_bit_p (sol, rhs))
1364 bitmap_set_bit (sol, rhs);
1365 if (!TEST_BIT (changed, t))
1367 SET_BIT (changed, t);
1368 changed_count++;
1372 else if (dump_file && !(get_varinfo (j)->is_special_var))
1373 fprintf (dump_file, "Untypesafe usage in do_da_constraint.\n");
1378 /* Process a constraint C that represents x = *y, using DELTA as the
1379 starting solution. */
1381 static void
1382 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1383 bitmap delta)
1385 unsigned int lhs = get_varinfo (c->lhs.var)->node;
1386 bool flag = false;
1387 bitmap sol = get_varinfo (lhs)->solution;
1388 unsigned int j;
1389 bitmap_iterator bi;
1391 /* For each variable j in delta (Sol(y)), add
1392 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1393 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1395 unsigned HOST_WIDE_INT roffset = c->rhs.offset;
1396 if (type_safe (j, &roffset))
1398 varinfo_t v;
1399 unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + roffset;
1400 unsigned int t;
1402 v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1403 if (!v)
1404 continue;
1405 t = v->node;
1406 if (int_add_graph_edge (graph, lhs, t, 0))
1407 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1409 else if (dump_file && !(get_varinfo (j)->is_special_var))
1410 fprintf (dump_file, "Untypesafe usage in do_sd_constraint\n");
1414 /* If the LHS solution changed, mark the var as changed. */
1415 if (flag)
1417 get_varinfo (lhs)->solution = sol;
1418 if (!TEST_BIT (changed, lhs))
1420 SET_BIT (changed, lhs);
1421 changed_count++;
1426 /* Process a constraint C that represents *x = y. */
1428 static void
1429 do_ds_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1431 unsigned int rhs = get_varinfo (c->rhs.var)->node;
1432 unsigned HOST_WIDE_INT roff = c->rhs.offset;
1433 bitmap sol = get_varinfo (rhs)->solution;
1434 unsigned int j;
1435 bitmap_iterator bi;
1437 /* For each member j of delta (Sol(x)), add an edge from y to j and
1438 union Sol(y) into Sol(j) */
1439 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1441 unsigned HOST_WIDE_INT loff = c->lhs.offset;
1442 if (type_safe (j, &loff) && !(get_varinfo(j)->is_special_var))
1444 varinfo_t v;
1445 unsigned int t;
1446 unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + loff;
1448 v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1449 if (!v)
1450 continue;
1451 t = v->node;
1452 if (int_add_graph_edge (graph, t, rhs, roff))
1454 bitmap tmp = get_varinfo (t)->solution;
1455 if (set_union_with_increment (tmp, sol, roff))
1457 get_varinfo (t)->solution = tmp;
1458 if (t == rhs)
1460 sol = get_varinfo (rhs)->solution;
1462 if (!TEST_BIT (changed, t))
1464 SET_BIT (changed, t);
1465 changed_count++;
1470 else if (dump_file && !(get_varinfo (j)->is_special_var))
1471 fprintf (dump_file, "Untypesafe usage in do_ds_constraint\n");
1475 /* Handle a non-simple (simple meaning requires no iteration), non-copy
1476 constraint (IE *x = &y, x = *y, and *x = y). */
1478 static void
1479 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1481 if (c->lhs.type == DEREF)
1483 if (c->rhs.type == ADDRESSOF)
1485 /* *x = &y */
1486 do_da_constraint (graph, c, delta);
1488 else
1490 /* *x = y */
1491 do_ds_constraint (graph, c, delta);
1494 else
1496 /* x = *y */
1497 if (!(get_varinfo (c->lhs.var)->is_special_var))
1498 do_sd_constraint (graph, c, delta);
1502 /* Initialize and return a new SCC info structure. */
1504 static struct scc_info *
1505 init_scc_info (void)
1507 struct scc_info *si = xmalloc (sizeof (struct scc_info));
1508 size_t size = VEC_length (varinfo_t, varmap);
1510 si->current_index = 0;
1511 si->visited = sbitmap_alloc (size);
1512 sbitmap_zero (si->visited);
1513 si->in_component = sbitmap_alloc (size);
1514 sbitmap_ones (si->in_component);
1515 si->visited_index = xcalloc (sizeof (unsigned int), size + 1);
1516 si->scc_stack = VEC_alloc (unsigned, heap, 1);
1517 si->unification_queue = VEC_alloc (unsigned, heap, 1);
1518 return si;
1521 /* Free an SCC info structure pointed to by SI */
1523 static void
1524 free_scc_info (struct scc_info *si)
1526 sbitmap_free (si->visited);
1527 sbitmap_free (si->in_component);
1528 free (si->visited_index);
1529 VEC_free (unsigned, heap, si->scc_stack);
1530 VEC_free (unsigned, heap, si->unification_queue);
1531 free(si);
1535 /* Find cycles in GRAPH that occur, using strongly connected components, and
1536 collapse the cycles into a single representative node. if UPDATE_CHANGED
1537 is true, then update the changed sbitmap to note those nodes whose
1538 solutions have changed as a result of collapsing. */
1540 static void
1541 find_and_collapse_graph_cycles (constraint_graph_t graph, bool update_changed)
1543 unsigned int i;
1544 unsigned int size = VEC_length (varinfo_t, varmap);
1545 struct scc_info *si = init_scc_info ();
1547 for (i = 0; i != size; ++i)
1548 if (!TEST_BIT (si->visited, i) && get_varinfo (i)->node == i)
1549 scc_visit (graph, si, i);
1550 process_unification_queue (graph, si, update_changed);
1551 free_scc_info (si);
1554 /* Compute a topological ordering for GRAPH, and store the result in the
1555 topo_info structure TI. */
1557 static void
1558 compute_topo_order (constraint_graph_t graph,
1559 struct topo_info *ti)
1561 unsigned int i;
1562 unsigned int size = VEC_length (varinfo_t, varmap);
1564 for (i = 0; i != size; ++i)
1565 if (!TEST_BIT (ti->visited, i) && get_varinfo (i)->node == i)
1566 topo_visit (graph, ti, i);
1569 /* Return true if bitmap B is empty, or a bitmap other than bit 0 is set. */
1571 static bool
1572 bitmap_other_than_zero_bit_set (bitmap b)
1574 unsigned int i;
1575 bitmap_iterator bi;
1577 if (bitmap_empty_p (b))
1578 return false;
1579 EXECUTE_IF_SET_IN_BITMAP (b, 1, i, bi)
1580 return true;
1581 return false;
1584 /* Perform offline variable substitution.
1586 This is a linear time way of identifying variables that must have
1587 equivalent points-to sets, including those caused by static cycles,
1588 and single entry subgraphs, in the constraint graph.
1590 The technique is described in "Off-line variable substitution for
1591 scaling points-to analysis" by Atanas Rountev and Satish Chandra,
1592 in "ACM SIGPLAN Notices" volume 35, number 5, pages 47-56. */
1594 static void
1595 perform_var_substitution (constraint_graph_t graph)
1597 struct topo_info *ti = init_topo_info ();
1599 /* Compute the topological ordering of the graph, then visit each
1600 node in topological order. */
1601 compute_topo_order (graph, ti);
1603 while (VEC_length (unsigned, ti->topo_order) != 0)
1605 unsigned int i = VEC_pop (unsigned, ti->topo_order);
1606 unsigned int pred;
1607 varinfo_t vi = get_varinfo (i);
1608 bool okay_to_elim = false;
1609 unsigned int root = VEC_length (varinfo_t, varmap);
1610 VEC(constraint_edge_t,heap) *predvec = graph->preds[i];
1611 constraint_edge_t ce;
1612 bitmap tmp;
1614 /* We can't eliminate things whose address is taken, or which is
1615 the target of a dereference. */
1616 if (vi->address_taken || vi->indirect_target)
1617 continue;
1619 /* See if all predecessors of I are ripe for elimination */
1620 for (pred = 0; VEC_iterate (constraint_edge_t, predvec, pred, ce); pred++)
1622 bitmap weight;
1623 unsigned int w;
1624 weight = get_graph_weights (graph, *ce);
1626 /* We can't eliminate variables that have nonzero weighted
1627 edges between them. */
1628 if (bitmap_other_than_zero_bit_set (weight))
1630 okay_to_elim = false;
1631 break;
1633 w = get_varinfo (ce->dest)->node;
1635 /* We can't eliminate the node if one of the predecessors is
1636 part of a different strongly connected component. */
1637 if (!okay_to_elim)
1639 root = w;
1640 okay_to_elim = true;
1642 else if (w != root)
1644 okay_to_elim = false;
1645 break;
1648 /* Theorem 4 in Rountev and Chandra: If i is a direct node,
1649 then Solution(i) is a subset of Solution (w), where w is a
1650 predecessor in the graph.
1651 Corollary: If all predecessors of i have the same
1652 points-to set, then i has that same points-to set as
1653 those predecessors. */
1654 tmp = BITMAP_ALLOC (NULL);
1655 bitmap_and_compl (tmp, get_varinfo (i)->solution,
1656 get_varinfo (w)->solution);
1657 if (!bitmap_empty_p (tmp))
1659 okay_to_elim = false;
1660 BITMAP_FREE (tmp);
1661 break;
1663 BITMAP_FREE (tmp);
1666 /* See if the root is different than the original node.
1667 If so, we've found an equivalence. */
1668 if (root != get_varinfo (i)->node && okay_to_elim)
1670 /* Found an equivalence */
1671 get_varinfo (i)->node = root;
1672 collapse_nodes (graph, root, i);
1673 if (dump_file && (dump_flags & TDF_DETAILS))
1674 fprintf (dump_file, "Collapsing %s into %s\n",
1675 get_varinfo (i)->name,
1676 get_varinfo (root)->name);
1677 stats.collapsed_vars++;
1681 free_topo_info (ti);
1685 /* Solve the constraint graph GRAPH using our worklist solver.
1686 This is based on the PW* family of solvers from the "Efficient Field
1687 Sensitive Pointer Analysis for C" paper.
1688 It works by iterating over all the graph nodes, processing the complex
1689 constraints and propagating the copy constraints, until everything stops
1690 changed. This corresponds to steps 6-8 in the solving list given above. */
1692 static void
1693 solve_graph (constraint_graph_t graph)
1695 unsigned int size = VEC_length (varinfo_t, varmap);
1696 unsigned int i;
1698 changed_count = size;
1699 changed = sbitmap_alloc (size);
1700 sbitmap_ones (changed);
1702 /* The already collapsed/unreachable nodes will never change, so we
1703 need to account for them in changed_count. */
1704 for (i = 0; i < size; i++)
1705 if (get_varinfo (i)->node != i)
1706 changed_count--;
1708 while (changed_count > 0)
1710 unsigned int i;
1711 struct topo_info *ti = init_topo_info ();
1712 stats.iterations++;
1714 bitmap_obstack_initialize (&iteration_obstack);
1716 if (edge_added)
1718 /* We already did cycle elimination once, when we did
1719 variable substitution, so we don't need it again for the
1720 first iteration. */
1721 if (stats.iterations > 1)
1722 find_and_collapse_graph_cycles (graph, true);
1724 edge_added = false;
1727 compute_topo_order (graph, ti);
1729 while (VEC_length (unsigned, ti->topo_order) != 0)
1731 i = VEC_pop (unsigned, ti->topo_order);
1732 gcc_assert (get_varinfo (i)->node == i);
1734 /* If the node has changed, we need to process the
1735 complex constraints and outgoing edges again. */
1736 if (TEST_BIT (changed, i))
1738 unsigned int j;
1739 constraint_t c;
1740 constraint_edge_t e;
1741 bitmap solution;
1742 VEC(constraint_t,heap) *complex = get_varinfo (i)->complex;
1743 VEC(constraint_edge_t,heap) *succs;
1745 RESET_BIT (changed, i);
1746 changed_count--;
1748 /* Process the complex constraints */
1749 solution = get_varinfo (i)->solution;
1750 for (j = 0; VEC_iterate (constraint_t, complex, j, c); j++)
1751 do_complex_constraint (graph, c, solution);
1753 /* Propagate solution to all successors. */
1754 succs = graph->succs[i];
1755 for (j = 0; VEC_iterate (constraint_edge_t, succs, j, e); j++)
1757 bitmap tmp = get_varinfo (e->dest)->solution;
1758 bool flag = false;
1759 unsigned int k;
1760 bitmap weights = e->weights;
1761 bitmap_iterator bi;
1763 gcc_assert (!bitmap_empty_p (weights));
1764 EXECUTE_IF_SET_IN_BITMAP (weights, 0, k, bi)
1765 flag |= set_union_with_increment (tmp, solution, k);
1767 if (flag)
1769 get_varinfo (e->dest)->solution = tmp;
1770 if (!TEST_BIT (changed, e->dest))
1772 SET_BIT (changed, e->dest);
1773 changed_count++;
1779 free_topo_info (ti);
1780 bitmap_obstack_release (&iteration_obstack);
1783 sbitmap_free (changed);
1787 /* CONSTRAINT AND VARIABLE GENERATION FUNCTIONS */
1789 /* Map from trees to variable ids. */
1790 static htab_t id_for_tree;
1792 typedef struct tree_id
1794 tree t;
1795 unsigned int id;
1796 } *tree_id_t;
1798 /* Hash a tree id structure. */
1800 static hashval_t
1801 tree_id_hash (const void *p)
1803 const tree_id_t ta = (tree_id_t) p;
1804 return htab_hash_pointer (ta->t);
1807 /* Return true if the tree in P1 and the tree in P2 are the same. */
1809 static int
1810 tree_id_eq (const void *p1, const void *p2)
1812 const tree_id_t ta1 = (tree_id_t) p1;
1813 const tree_id_t ta2 = (tree_id_t) p2;
1814 return ta1->t == ta2->t;
1817 /* Insert ID as the variable id for tree T in the hashtable. */
1819 static void
1820 insert_id_for_tree (tree t, int id)
1822 void **slot;
1823 struct tree_id finder;
1824 tree_id_t new_pair;
1826 finder.t = t;
1827 slot = htab_find_slot (id_for_tree, &finder, INSERT);
1828 gcc_assert (*slot == NULL);
1829 new_pair = xmalloc (sizeof (struct tree_id));
1830 new_pair->t = t;
1831 new_pair->id = id;
1832 *slot = (void *)new_pair;
1835 /* Find the variable id for tree T in ID_FOR_TREE. If T does not
1836 exist in the hash table, return false, otherwise, return true and
1837 set *ID to the id we found. */
1839 static bool
1840 lookup_id_for_tree (tree t, unsigned int *id)
1842 tree_id_t pair;
1843 struct tree_id finder;
1845 finder.t = t;
1846 pair = htab_find (id_for_tree, &finder);
1847 if (pair == NULL)
1848 return false;
1849 *id = pair->id;
1850 return true;
1853 /* Return a printable name for DECL */
1855 static const char *
1856 alias_get_name (tree decl)
1858 const char *res = get_name (decl);
1859 char *temp;
1860 int num_printed = 0;
1862 if (res != NULL)
1863 return res;
1865 res = "NULL";
1866 if (TREE_CODE (decl) == SSA_NAME)
1868 num_printed = asprintf (&temp, "%s_%u",
1869 alias_get_name (SSA_NAME_VAR (decl)),
1870 SSA_NAME_VERSION (decl));
1872 else if (DECL_P (decl))
1874 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
1876 if (num_printed > 0)
1878 res = ggc_strdup (temp);
1879 free (temp);
1881 return res;
1884 /* Find the variable id for tree T in the hashtable.
1885 If T doesn't exist in the hash table, create an entry for it. */
1887 static unsigned int
1888 get_id_for_tree (tree t)
1890 tree_id_t pair;
1891 struct tree_id finder;
1893 finder.t = t;
1894 pair = htab_find (id_for_tree, &finder);
1895 if (pair == NULL)
1896 return create_variable_info_for (t, alias_get_name (t));
1898 return pair->id;
1901 /* Get a constraint expression from an SSA_VAR_P node. */
1903 static struct constraint_expr
1904 get_constraint_exp_from_ssa_var (tree t)
1906 struct constraint_expr cexpr;
1908 gcc_assert (SSA_VAR_P (t) || DECL_P (t));
1910 /* For parameters, get at the points-to set for the actual parm
1911 decl. */
1912 if (TREE_CODE (t) == SSA_NAME
1913 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
1914 && default_def (SSA_NAME_VAR (t)) == t)
1915 return get_constraint_exp_from_ssa_var (SSA_NAME_VAR (t));
1917 cexpr.type = SCALAR;
1919 cexpr.var = get_id_for_tree (t);
1920 /* If we determine the result is "anything", and we know this is readonly,
1921 say it points to readonly memory instead. */
1922 if (cexpr.var == anything_id && TREE_READONLY (t))
1924 cexpr.type = ADDRESSOF;
1925 cexpr.var = readonly_id;
1928 cexpr.offset = 0;
1929 return cexpr;
1932 /* Process a completed constraint T, and add it to the constraint
1933 list. */
1935 static void
1936 process_constraint (constraint_t t)
1938 struct constraint_expr rhs = t->rhs;
1939 struct constraint_expr lhs = t->lhs;
1941 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
1942 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
1944 /* ANYTHING == ANYTHING is pointless. */
1945 if (lhs.var == anything_id && rhs.var == anything_id)
1946 return;
1948 /* If we have &ANYTHING = something, convert to SOMETHING = &ANYTHING) */
1949 else if (lhs.var == anything_id && lhs.type == ADDRESSOF)
1951 rhs = t->lhs;
1952 t->lhs = t->rhs;
1953 t->rhs = rhs;
1954 process_constraint (t);
1956 /* This can happen in our IR with things like n->a = *p */
1957 else if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
1959 /* Split into tmp = *rhs, *lhs = tmp */
1960 tree rhsdecl = get_varinfo (rhs.var)->decl;
1961 tree pointertype = TREE_TYPE (rhsdecl);
1962 tree pointedtotype = TREE_TYPE (pointertype);
1963 tree tmpvar = create_tmp_var_raw (pointedtotype, "doubledereftmp");
1964 struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
1966 /* If this is an aggregate of known size, we should have passed
1967 this off to do_structure_copy, and it should have broken it
1968 up. */
1969 gcc_assert (!AGGREGATE_TYPE_P (pointedtotype)
1970 || get_varinfo (rhs.var)->is_unknown_size_var);
1972 process_constraint (new_constraint (tmplhs, rhs));
1973 process_constraint (new_constraint (lhs, tmplhs));
1975 else if (rhs.type == ADDRESSOF)
1977 varinfo_t vi;
1978 gcc_assert (rhs.offset == 0);
1980 for (vi = get_varinfo (rhs.var); vi != NULL; vi = vi->next)
1981 vi->address_taken = true;
1983 VEC_safe_push (constraint_t, heap, constraints, t);
1985 else
1987 if (lhs.type != DEREF && rhs.type == DEREF)
1988 get_varinfo (lhs.var)->indirect_target = true;
1989 VEC_safe_push (constraint_t, heap, constraints, t);
1994 /* Return the position, in bits, of FIELD_DECL from the beginning of its
1995 structure. */
1997 static unsigned HOST_WIDE_INT
1998 bitpos_of_field (const tree fdecl)
2001 if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2002 || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2003 return -1;
2005 return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2006 + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2010 /* Return true if an access to [ACCESSPOS, ACCESSSIZE]
2011 overlaps with a field at [FIELDPOS, FIELDSIZE] */
2013 static bool
2014 offset_overlaps_with_access (const unsigned HOST_WIDE_INT fieldpos,
2015 const unsigned HOST_WIDE_INT fieldsize,
2016 const unsigned HOST_WIDE_INT accesspos,
2017 const unsigned HOST_WIDE_INT accesssize)
2019 if (fieldpos == accesspos && fieldsize == accesssize)
2020 return true;
2021 if (accesspos >= fieldpos && accesspos < (fieldpos + fieldsize))
2022 return true;
2023 if (accesspos < fieldpos && (accesspos + accesssize > fieldpos))
2024 return true;
2026 return false;
2029 /* Given a COMPONENT_REF T, return the constraint_expr for it. */
2031 static struct constraint_expr
2032 get_constraint_for_component_ref (tree t, bool *need_anyoffset)
2034 struct constraint_expr result;
2035 HOST_WIDE_INT bitsize = -1;
2036 HOST_WIDE_INT bitpos;
2037 tree offset = NULL_TREE;
2038 enum machine_mode mode;
2039 int unsignedp;
2040 int volatilep;
2041 tree forzero;
2043 result.offset = 0;
2044 result.type = SCALAR;
2045 result.var = 0;
2047 /* Some people like to do cute things like take the address of
2048 &0->a.b */
2049 forzero = t;
2050 while (!SSA_VAR_P (forzero) && !CONSTANT_CLASS_P (forzero))
2051 forzero = TREE_OPERAND (forzero, 0);
2053 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
2055 result.offset = 0;
2056 result.var = integer_id;
2057 result.type = SCALAR;
2058 return result;
2061 t = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode,
2062 &unsignedp, &volatilep, false);
2063 result = get_constraint_for (t, need_anyoffset);
2065 /* This can also happen due to weird offsetof type macros. */
2066 if (TREE_CODE (t) != ADDR_EXPR && result.type == ADDRESSOF)
2067 result.type = SCALAR;
2069 /* If we know where this goes, then yay. Otherwise, booo. */
2071 if (offset == NULL && bitsize != -1)
2073 result.offset = bitpos;
2075 else if (need_anyoffset)
2077 result.offset = 0;
2078 *need_anyoffset = true;
2080 else
2082 result.var = anything_id;
2083 result.offset = 0;
2086 if (result.type == SCALAR)
2088 /* In languages like C, you can access one past the end of an
2089 array. You aren't allowed to dereference it, so we can
2090 ignore this constraint. When we handle pointer subtraction,
2091 we may have to do something cute here. */
2093 if (result.offset < get_varinfo (result.var)->fullsize)
2095 /* It's also not true that the constraint will actually start at the
2096 right offset, it may start in some padding. We only care about
2097 setting the constraint to the first actual field it touches, so
2098 walk to find it. */
2099 varinfo_t curr;
2100 for (curr = get_varinfo (result.var); curr; curr = curr->next)
2102 if (offset_overlaps_with_access (curr->offset, curr->size,
2103 result.offset, bitsize))
2105 result.var = curr->id;
2106 break;
2110 /* assert that we found *some* field there. The user couldn't be
2111 accessing *only* padding. */
2113 gcc_assert (curr);
2115 else
2116 if (dump_file && (dump_flags & TDF_DETAILS))
2117 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
2119 result.offset = 0;
2122 return result;
2126 /* Dereference the constraint expression CONS, and return the result.
2127 DEREF (ADDRESSOF) = SCALAR
2128 DEREF (SCALAR) = DEREF
2129 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
2130 This is needed so that we can handle dereferencing DEREF constraints. */
2132 static struct constraint_expr
2133 do_deref (struct constraint_expr cons)
2135 if (cons.type == SCALAR)
2137 cons.type = DEREF;
2138 return cons;
2140 else if (cons.type == ADDRESSOF)
2142 cons.type = SCALAR;
2143 return cons;
2145 else if (cons.type == DEREF)
2147 tree tmpvar = create_tmp_var_raw (ptr_type_node, "derefmp");
2148 struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2149 process_constraint (new_constraint (tmplhs, cons));
2150 cons.var = tmplhs.var;
2151 return cons;
2153 gcc_unreachable ();
2157 /* Given a tree T, return the constraint expression for it. */
2159 static struct constraint_expr
2160 get_constraint_for (tree t, bool *need_anyoffset)
2162 struct constraint_expr temp;
2164 /* x = integer is all glommed to a single variable, which doesn't
2165 point to anything by itself. That is, of course, unless it is an
2166 integer constant being treated as a pointer, in which case, we
2167 will return that this is really the addressof anything. This
2168 happens below, since it will fall into the default case. The only
2169 case we know something about an integer treated like a pointer is
2170 when it is the NULL pointer, and then we just say it points to
2171 NULL. */
2172 if (TREE_CODE (t) == INTEGER_CST
2173 && !POINTER_TYPE_P (TREE_TYPE (t)))
2175 temp.var = integer_id;
2176 temp.type = SCALAR;
2177 temp.offset = 0;
2178 return temp;
2180 else if (TREE_CODE (t) == INTEGER_CST
2181 && integer_zerop (t))
2183 temp.var = nothing_id;
2184 temp.type = ADDRESSOF;
2185 temp.offset = 0;
2186 return temp;
2189 switch (TREE_CODE_CLASS (TREE_CODE (t)))
2191 case tcc_expression:
2193 switch (TREE_CODE (t))
2195 case ADDR_EXPR:
2197 temp = get_constraint_for (TREE_OPERAND (t, 0), need_anyoffset);
2198 if (temp.type == DEREF)
2199 temp.type = SCALAR;
2200 else
2201 temp.type = ADDRESSOF;
2202 return temp;
2204 break;
2205 case CALL_EXPR:
2207 /* XXX: In interprocedural mode, if we didn't have the
2208 body, we would need to do *each pointer argument =
2209 &ANYTHING added. */
2210 if (call_expr_flags (t) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA))
2212 varinfo_t vi;
2213 tree heapvar;
2215 heapvar = create_tmp_var_raw (ptr_type_node, "HEAP");
2216 DECL_EXTERNAL (heapvar) = 1;
2217 add_referenced_tmp_var (heapvar);
2218 temp.var = create_variable_info_for (heapvar,
2219 alias_get_name (heapvar));
2221 vi = get_varinfo (temp.var);
2222 vi->is_artificial_var = 1;
2223 vi->is_heap_var = 1;
2224 temp.type = ADDRESSOF;
2225 temp.offset = 0;
2226 return temp;
2228 /* FALLTHRU */
2229 default:
2231 temp.type = ADDRESSOF;
2232 temp.var = anything_id;
2233 temp.offset = 0;
2234 return temp;
2238 case tcc_reference:
2240 switch (TREE_CODE (t))
2242 case INDIRECT_REF:
2244 temp = get_constraint_for (TREE_OPERAND (t, 0), need_anyoffset);
2245 temp = do_deref (temp);
2246 return temp;
2248 case ARRAY_REF:
2249 case COMPONENT_REF:
2250 temp = get_constraint_for_component_ref (t, need_anyoffset);
2251 return temp;
2252 default:
2254 temp.type = ADDRESSOF;
2255 temp.var = anything_id;
2256 temp.offset = 0;
2257 return temp;
2261 case tcc_unary:
2263 switch (TREE_CODE (t))
2265 case NOP_EXPR:
2266 case CONVERT_EXPR:
2267 case NON_LVALUE_EXPR:
2269 tree op = TREE_OPERAND (t, 0);
2271 /* Cast from non-pointer to pointers are bad news for us.
2272 Anything else, we see through */
2273 if (!(POINTER_TYPE_P (TREE_TYPE (t))
2274 && ! POINTER_TYPE_P (TREE_TYPE (op))))
2275 return get_constraint_for (op, need_anyoffset);
2277 /* FALLTHRU */
2279 default:
2281 temp.type = ADDRESSOF;
2282 temp.var = anything_id;
2283 temp.offset = 0;
2284 return temp;
2288 case tcc_exceptional:
2290 switch (TREE_CODE (t))
2292 case PHI_NODE:
2293 return get_constraint_for (PHI_RESULT (t), need_anyoffset);
2294 case SSA_NAME:
2295 return get_constraint_exp_from_ssa_var (t);
2296 default:
2298 temp.type = ADDRESSOF;
2299 temp.var = anything_id;
2300 temp.offset = 0;
2301 return temp;
2305 case tcc_declaration:
2306 return get_constraint_exp_from_ssa_var (t);
2307 default:
2309 temp.type = ADDRESSOF;
2310 temp.var = anything_id;
2311 temp.offset = 0;
2312 return temp;
2318 /* Handle the structure copy case where we have a simple structure copy
2319 between LHS and RHS that is of SIZE (in bits)
2321 For each field of the lhs variable (lhsfield)
2322 For each field of the rhs variable at lhsfield.offset (rhsfield)
2323 add the constraint lhsfield = rhsfield
2325 If we fail due to some kind of type unsafety or other thing we
2326 can't handle, return false. We expect the caller to collapse the
2327 variable in that case. */
2329 static bool
2330 do_simple_structure_copy (const struct constraint_expr lhs,
2331 const struct constraint_expr rhs,
2332 const unsigned HOST_WIDE_INT size)
2334 varinfo_t p = get_varinfo (lhs.var);
2335 unsigned HOST_WIDE_INT pstart, last;
2336 pstart = p->offset;
2337 last = p->offset + size;
2338 for (; p && p->offset < last; p = p->next)
2340 varinfo_t q;
2341 struct constraint_expr templhs = lhs;
2342 struct constraint_expr temprhs = rhs;
2343 unsigned HOST_WIDE_INT fieldoffset;
2345 templhs.var = p->id;
2346 q = get_varinfo (temprhs.var);
2347 fieldoffset = p->offset - pstart;
2348 q = first_vi_for_offset (q, q->offset + fieldoffset);
2349 if (!q)
2350 return false;
2351 temprhs.var = q->id;
2352 process_constraint (new_constraint (templhs, temprhs));
2354 return true;
2358 /* Handle the structure copy case where we have a structure copy between a
2359 aggregate on the LHS and a dereference of a pointer on the RHS
2360 that is of SIZE (in bits)
2362 For each field of the lhs variable (lhsfield)
2363 rhs.offset = lhsfield->offset
2364 add the constraint lhsfield = rhs
2367 static void
2368 do_rhs_deref_structure_copy (const struct constraint_expr lhs,
2369 const struct constraint_expr rhs,
2370 const unsigned HOST_WIDE_INT size)
2372 varinfo_t p = get_varinfo (lhs.var);
2373 unsigned HOST_WIDE_INT pstart,last;
2374 pstart = p->offset;
2375 last = p->offset + size;
2377 for (; p && p->offset < last; p = p->next)
2379 varinfo_t q;
2380 struct constraint_expr templhs = lhs;
2381 struct constraint_expr temprhs = rhs;
2382 unsigned HOST_WIDE_INT fieldoffset;
2385 if (templhs.type == SCALAR)
2386 templhs.var = p->id;
2387 else
2388 templhs.offset = p->offset;
2390 q = get_varinfo (temprhs.var);
2391 fieldoffset = p->offset - pstart;
2392 temprhs.offset += fieldoffset;
2393 process_constraint (new_constraint (templhs, temprhs));
2397 /* Handle the structure copy case where we have a structure copy
2398 between a aggregate on the RHS and a dereference of a pointer on
2399 the LHS that is of SIZE (in bits)
2401 For each field of the rhs variable (rhsfield)
2402 lhs.offset = rhsfield->offset
2403 add the constraint lhs = rhsfield
2406 static void
2407 do_lhs_deref_structure_copy (const struct constraint_expr lhs,
2408 const struct constraint_expr rhs,
2409 const unsigned HOST_WIDE_INT size)
2411 varinfo_t p = get_varinfo (rhs.var);
2412 unsigned HOST_WIDE_INT pstart,last;
2413 pstart = p->offset;
2414 last = p->offset + size;
2416 for (; p && p->offset < last; p = p->next)
2418 varinfo_t q;
2419 struct constraint_expr templhs = lhs;
2420 struct constraint_expr temprhs = rhs;
2421 unsigned HOST_WIDE_INT fieldoffset;
2424 if (temprhs.type == SCALAR)
2425 temprhs.var = p->id;
2426 else
2427 temprhs.offset = p->offset;
2429 q = get_varinfo (templhs.var);
2430 fieldoffset = p->offset - pstart;
2431 templhs.offset += fieldoffset;
2432 process_constraint (new_constraint (templhs, temprhs));
2436 /* Sometimes, frontends like to give us bad type information. This
2437 function will collapse all the fields from VAR to the end of VAR,
2438 into VAR, so that we treat those fields as a single variable.
2439 We return the variable they were collapsed into. */
2441 static unsigned int
2442 collapse_rest_of_var (unsigned int var)
2444 varinfo_t currvar = get_varinfo (var);
2445 varinfo_t field;
2447 for (field = currvar->next; field; field = field->next)
2449 if (dump_file)
2450 fprintf (dump_file, "Type safety: Collapsing var %s into %s\n",
2451 field->name, currvar->name);
2453 gcc_assert (!field->collapsed_to);
2454 field->collapsed_to = currvar;
2457 currvar->next = NULL;
2458 currvar->size = currvar->fullsize - currvar->offset;
2460 return currvar->id;
2463 /* Handle aggregate copies by expanding into copies of the respective
2464 fields of the structures. */
2466 static void
2467 do_structure_copy (tree lhsop, tree rhsop)
2469 struct constraint_expr lhs, rhs, tmp;
2470 varinfo_t p;
2471 unsigned HOST_WIDE_INT lhssize;
2472 unsigned HOST_WIDE_INT rhssize;
2474 lhs = get_constraint_for (lhsop, NULL);
2475 rhs = get_constraint_for (rhsop, NULL);
2477 /* If we have special var = x, swap it around. */
2478 if (lhs.var <= integer_id && !(get_varinfo (rhs.var)->is_special_var))
2480 tmp = lhs;
2481 lhs = rhs;
2482 rhs = tmp;
2485 /* This is fairly conservative for the RHS == ADDRESSOF case, in that it's
2486 possible it's something we could handle. However, most cases falling
2487 into this are dealing with transparent unions, which are slightly
2488 weird. */
2489 if (rhs.type == ADDRESSOF && !(get_varinfo (rhs.var)->is_special_var))
2491 rhs.type = ADDRESSOF;
2492 rhs.var = anything_id;
2495 /* If the RHS is a special var, or an addressof, set all the LHS fields to
2496 that special var. */
2497 if (rhs.var <= integer_id)
2499 for (p = get_varinfo (lhs.var); p; p = p->next)
2501 struct constraint_expr templhs = lhs;
2502 struct constraint_expr temprhs = rhs;
2503 if (templhs.type == SCALAR )
2504 templhs.var = p->id;
2505 else
2506 templhs.offset += p->offset;
2507 process_constraint (new_constraint (templhs, temprhs));
2510 else
2512 tree rhstype = TREE_TYPE (rhsop);
2513 tree lhstype = TREE_TYPE (lhsop);
2514 tree rhstypesize = TYPE_SIZE (rhstype);
2515 tree lhstypesize = TYPE_SIZE (lhstype);
2517 /* If we have a variably sized types on the rhs or lhs, and a deref
2518 constraint, add the constraint, lhsconstraint = &ANYTHING.
2519 This is conservatively correct because either the lhs is an unknown
2520 sized var (if the constraint is SCALAR), or the lhs is a DEREF
2521 constraint, and every variable it can point to must be unknown sized
2522 anyway, so we don't need to worry about fields at all. */
2523 if ((rhs.type == DEREF && TREE_CODE (rhstypesize) != INTEGER_CST)
2524 || (lhs.type == DEREF && TREE_CODE (lhstypesize) != INTEGER_CST))
2526 rhs.var = anything_id;
2527 rhs.type = ADDRESSOF;
2528 rhs.offset = 0;
2529 process_constraint (new_constraint (lhs, rhs));
2530 return;
2533 /* The size only really matters insofar as we don't set more or less of
2534 the variable. If we hit an unknown size var, the size should be the
2535 whole darn thing. */
2536 if (get_varinfo (rhs.var)->is_unknown_size_var)
2537 rhssize = ~0;
2538 else
2539 rhssize = TREE_INT_CST_LOW (rhstypesize);
2541 if (get_varinfo (lhs.var)->is_unknown_size_var)
2542 lhssize = ~0;
2543 else
2544 lhssize = TREE_INT_CST_LOW (lhstypesize);
2547 if (rhs.type == SCALAR && lhs.type == SCALAR)
2549 if (!do_simple_structure_copy (lhs, rhs, MIN (lhssize, rhssize)))
2551 lhs.var = collapse_rest_of_var (lhs.var);
2552 rhs.var = collapse_rest_of_var (rhs.var);
2553 lhs.offset = 0;
2554 rhs.offset = 0;
2555 lhs.type = SCALAR;
2556 rhs.type = SCALAR;
2557 process_constraint (new_constraint (lhs, rhs));
2560 else if (lhs.type != DEREF && rhs.type == DEREF)
2561 do_rhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
2562 else if (lhs.type == DEREF && rhs.type != DEREF)
2563 do_lhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
2564 else
2566 tree pointedtotype = lhstype;
2567 tree tmpvar;
2569 gcc_assert (rhs.type == DEREF && lhs.type == DEREF);
2570 tmpvar = create_tmp_var_raw (pointedtotype, "structcopydereftmp");
2571 do_structure_copy (tmpvar, rhsop);
2572 do_structure_copy (lhsop, tmpvar);
2577 /* Update related alias information kept in AI. This is used when
2578 building name tags, alias sets and deciding grouping heuristics.
2579 STMT is the statement to process. This function also updates
2580 ADDRESSABLE_VARS. */
2582 static void
2583 update_alias_info (tree stmt, struct alias_info *ai)
2585 bitmap addr_taken;
2586 use_operand_p use_p;
2587 ssa_op_iter iter;
2588 bool stmt_escapes_p = is_escape_site (stmt, ai);
2589 tree op;
2591 /* Mark all the variables whose address are taken by the statement. */
2592 addr_taken = addresses_taken (stmt);
2593 if (addr_taken)
2595 bitmap_ior_into (addressable_vars, addr_taken);
2597 /* If STMT is an escape point, all the addresses taken by it are
2598 call-clobbered. */
2599 if (stmt_escapes_p)
2601 bitmap_iterator bi;
2602 unsigned i;
2604 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
2605 mark_call_clobbered (referenced_var (i));
2609 /* Process each operand use. If an operand may be aliased, keep
2610 track of how many times it's being used. For pointers, determine
2611 whether they are dereferenced by the statement, or whether their
2612 value escapes, etc. */
2613 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
2615 tree op, var;
2616 var_ann_t v_ann;
2617 struct ptr_info_def *pi;
2618 bool is_store, is_potential_deref;
2619 unsigned num_uses, num_derefs;
2621 op = USE_FROM_PTR (use_p);
2623 /* If STMT is a PHI node, OP may be an ADDR_EXPR. If so, add it
2624 to the set of addressable variables. */
2625 if (TREE_CODE (op) == ADDR_EXPR)
2627 gcc_assert (TREE_CODE (stmt) == PHI_NODE);
2629 /* PHI nodes don't have annotations for pinning the set
2630 of addresses taken, so we collect them here.
2632 FIXME, should we allow PHI nodes to have annotations
2633 so that they can be treated like regular statements?
2634 Currently, they are treated as second-class
2635 statements. */
2636 add_to_addressable_set (TREE_OPERAND (op, 0), &addressable_vars);
2637 continue;
2640 /* Ignore constants. */
2641 if (TREE_CODE (op) != SSA_NAME)
2642 continue;
2644 var = SSA_NAME_VAR (op);
2645 v_ann = var_ann (var);
2647 /* If the operand's variable may be aliased, keep track of how
2648 many times we've referenced it. This is used for alias
2649 grouping in compute_flow_insensitive_aliasing. */
2650 if (may_be_aliased (var))
2651 NUM_REFERENCES_INC (v_ann);
2653 /* We are only interested in pointers. */
2654 if (!POINTER_TYPE_P (TREE_TYPE (op)))
2655 continue;
2657 pi = get_ptr_info (op);
2659 /* Add OP to AI->PROCESSED_PTRS, if it's not there already. */
2660 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op)))
2662 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op));
2663 VARRAY_PUSH_TREE (ai->processed_ptrs, op);
2666 /* If STMT is a PHI node, then it will not have pointer
2667 dereferences and it will not be an escape point. */
2668 if (TREE_CODE (stmt) == PHI_NODE)
2669 continue;
2671 /* Determine whether OP is a dereferenced pointer, and if STMT
2672 is an escape point, whether OP escapes. */
2673 count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
2675 /* Handle a corner case involving address expressions of the
2676 form '&PTR->FLD'. The problem with these expressions is that
2677 they do not represent a dereference of PTR. However, if some
2678 other transformation propagates them into an INDIRECT_REF
2679 expression, we end up with '*(&PTR->FLD)' which is folded
2680 into 'PTR->FLD'.
2682 So, if the original code had no other dereferences of PTR,
2683 the aliaser will not create memory tags for it, and when
2684 &PTR->FLD gets propagated to INDIRECT_REF expressions, the
2685 memory operations will receive no V_MAY_DEF/VUSE operands.
2687 One solution would be to have count_uses_and_derefs consider
2688 &PTR->FLD a dereference of PTR. But that is wrong, since it
2689 is not really a dereference but an offset calculation.
2691 What we do here is to recognize these special ADDR_EXPR
2692 nodes. Since these expressions are never GIMPLE values (they
2693 are not GIMPLE invariants), they can only appear on the RHS
2694 of an assignment and their base address is always an
2695 INDIRECT_REF expression. */
2696 is_potential_deref = false;
2697 if (TREE_CODE (stmt) == MODIFY_EXPR
2698 && TREE_CODE (TREE_OPERAND (stmt, 1)) == ADDR_EXPR
2699 && !is_gimple_val (TREE_OPERAND (stmt, 1)))
2701 /* If the RHS if of the form &PTR->FLD and PTR == OP, then
2702 this represents a potential dereference of PTR. */
2703 tree rhs = TREE_OPERAND (stmt, 1);
2704 tree base = get_base_address (TREE_OPERAND (rhs, 0));
2705 if (TREE_CODE (base) == INDIRECT_REF
2706 && TREE_OPERAND (base, 0) == op)
2707 is_potential_deref = true;
2710 if (num_derefs > 0 || is_potential_deref)
2712 /* Mark OP as dereferenced. In a subsequent pass,
2713 dereferenced pointers that point to a set of
2714 variables will be assigned a name tag to alias
2715 all the variables OP points to. */
2716 pi->is_dereferenced = 1;
2718 /* Keep track of how many time we've dereferenced each
2719 pointer. */
2720 NUM_REFERENCES_INC (v_ann);
2722 /* If this is a store operation, mark OP as being
2723 dereferenced to store, otherwise mark it as being
2724 dereferenced to load. */
2725 if (is_store)
2726 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
2727 else
2728 bitmap_set_bit (ai->dereferenced_ptrs_load, DECL_UID (var));
2731 if (stmt_escapes_p && num_derefs < num_uses)
2733 /* If STMT is an escape point and STMT contains at
2734 least one direct use of OP, then the value of OP
2735 escapes and so the pointed-to variables need to
2736 be marked call-clobbered. */
2737 pi->value_escapes_p = 1;
2739 /* If the statement makes a function call, assume
2740 that pointer OP will be dereferenced in a store
2741 operation inside the called function. */
2742 if (get_call_expr_in (stmt))
2744 bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
2745 pi->is_dereferenced = 1;
2750 if (TREE_CODE (stmt) == PHI_NODE)
2751 return;
2753 /* Update reference counter for definitions to any
2754 potentially aliased variable. This is used in the alias
2755 grouping heuristics. */
2756 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
2758 tree var = SSA_NAME_VAR (op);
2759 var_ann_t ann = var_ann (var);
2760 bitmap_set_bit (ai->written_vars, DECL_UID (var));
2761 if (may_be_aliased (var))
2762 NUM_REFERENCES_INC (ann);
2766 /* Mark variables in V_MAY_DEF operands as being written to. */
2767 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
2769 tree var = DECL_P (op) ? op : SSA_NAME_VAR (op);
2770 bitmap_set_bit (ai->written_vars, DECL_UID (var));
2775 /* Handle pointer arithmetic EXPR when creating aliasing constraints.
2776 Expressions of the type PTR + CST can be handled in two ways:
2778 1- If the constraint for PTR is ADDRESSOF for a non-structure
2779 variable, then we can use it directly because adding or
2780 subtracting a constant may not alter the original ADDRESSOF
2781 constraint (i.e., pointer arithmetic may not legally go outside
2782 an object's boundaries).
2784 2- If the constraint for PTR is ADDRESSOF for a structure variable,
2785 then if CST is a compile-time constant that can be used as an
2786 offset, we can determine which sub-variable will be pointed-to
2787 by the expression.
2789 Return true if the expression is handled. For any other kind of
2790 expression, return false so that each operand can be added as a
2791 separate constraint by the caller. */
2793 static bool
2794 handle_ptr_arith (struct constraint_expr lhs, tree expr)
2796 tree op0, op1;
2797 struct constraint_expr base, offset;
2799 if (TREE_CODE (expr) != PLUS_EXPR)
2800 return false;
2802 op0 = TREE_OPERAND (expr, 0);
2803 op1 = TREE_OPERAND (expr, 1);
2805 base = get_constraint_for (op0, NULL);
2807 offset.var = anyoffset_id;
2808 offset.type = ADDRESSOF;
2809 offset.offset = 0;
2811 process_constraint (new_constraint (lhs, base));
2812 process_constraint (new_constraint (lhs, offset));
2814 return true;
2818 /* Walk statement T setting up aliasing constraints according to the
2819 references found in T. This function is the main part of the
2820 constraint builder. AI points to auxiliary alias information used
2821 when building alias sets and computing alias grouping heuristics. */
2823 static void
2824 find_func_aliases (tree t, struct alias_info *ai)
2826 struct constraint_expr lhs, rhs;
2828 /* Update various related attributes like escaped addresses, pointer
2829 dereferences for loads and stores. This is used when creating
2830 name tags and alias sets. */
2831 update_alias_info (t, ai);
2833 /* Now build constraints expressions. */
2834 if (TREE_CODE (t) == PHI_NODE)
2836 /* Only care about pointers and structures containing
2837 pointers. */
2838 if (POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (t)))
2839 || AGGREGATE_TYPE_P (TREE_TYPE (PHI_RESULT (t))))
2841 int i;
2843 lhs = get_constraint_for (PHI_RESULT (t), NULL);
2844 for (i = 0; i < PHI_NUM_ARGS (t); i++)
2846 rhs = get_constraint_for (PHI_ARG_DEF (t, i), NULL);
2847 process_constraint (new_constraint (lhs, rhs));
2851 else if (TREE_CODE (t) == MODIFY_EXPR)
2853 tree lhsop = TREE_OPERAND (t, 0);
2854 tree rhsop = TREE_OPERAND (t, 1);
2855 int i;
2857 if (AGGREGATE_TYPE_P (TREE_TYPE (lhsop))
2858 && AGGREGATE_TYPE_P (TREE_TYPE (rhsop)))
2860 do_structure_copy (lhsop, rhsop);
2862 else
2864 /* Only care about operations with pointers, structures
2865 containing pointers, dereferences, and call expressions. */
2866 if (POINTER_TYPE_P (TREE_TYPE (lhsop))
2867 || AGGREGATE_TYPE_P (TREE_TYPE (lhsop))
2868 || ref_contains_indirect_ref (lhsop)
2869 || TREE_CODE (rhsop) == CALL_EXPR)
2871 lhs = get_constraint_for (lhsop, NULL);
2872 switch (TREE_CODE_CLASS (TREE_CODE (rhsop)))
2874 /* RHS that consist of unary operations,
2875 exceptional types, or bare decls/constants, get
2876 handled directly by get_constraint_for. */
2877 case tcc_reference:
2878 case tcc_declaration:
2879 case tcc_constant:
2880 case tcc_exceptional:
2881 case tcc_expression:
2882 case tcc_unary:
2884 tree anyoffsetrhs = rhsop;
2885 bool need_anyoffset = false;
2886 rhs = get_constraint_for (rhsop, &need_anyoffset);
2887 process_constraint (new_constraint (lhs, rhs));
2889 STRIP_NOPS (anyoffsetrhs);
2890 /* When taking the address of an aggregate
2891 type, from the LHS we can access any field
2892 of the RHS. */
2893 if (need_anyoffset || (rhs.type == ADDRESSOF
2894 && !(get_varinfo (rhs.var)->is_special_var)
2895 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (anyoffsetrhs)))))
2897 rhs.var = anyoffset_id;
2898 rhs.type = ADDRESSOF;
2899 rhs.offset = 0;
2900 process_constraint (new_constraint (lhs, rhs));
2903 break;
2905 case tcc_binary:
2907 /* For pointer arithmetic of the form
2908 PTR + CST, we can simply use PTR's
2909 constraint because pointer arithmetic is
2910 not allowed to go out of bounds. */
2911 if (handle_ptr_arith (lhs, rhsop))
2912 break;
2914 /* FALLTHRU */
2916 /* Otherwise, walk each operand. Notice that we
2917 can't use the operand interface because we need
2918 to process expressions other than simple operands
2919 (e.g. INDIRECT_REF, ADDR_EXPR, CALL_EXPR). */
2920 default:
2921 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (rhsop)); i++)
2923 tree op = TREE_OPERAND (rhsop, i);
2924 rhs = get_constraint_for (op, NULL);
2925 process_constraint (new_constraint (lhs, rhs));
2932 /* After promoting variables and computing aliasing we will
2933 need to re-scan most statements. FIXME: Try to minimize the
2934 number of statements re-scanned. It's not really necessary to
2935 re-scan *all* statements. */
2936 mark_stmt_modified (t);
2940 /* Find the first varinfo in the same variable as START that overlaps with
2941 OFFSET.
2942 Effectively, walk the chain of fields for the variable START to find the
2943 first field that overlaps with OFFSET.
2944 Return NULL if we can't find one. */
2946 static varinfo_t
2947 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
2949 varinfo_t curr = start;
2950 while (curr)
2952 /* We may not find a variable in the field list with the actual
2953 offset when when we have glommed a structure to a variable.
2954 In that case, however, offset should still be within the size
2955 of the variable. */
2956 if (offset >= curr->offset && offset < (curr->offset + curr->size))
2957 return curr;
2958 curr = curr->next;
2960 return NULL;
2964 /* Insert the varinfo FIELD into the field list for BASE, ordered by
2965 offset. */
2967 static void
2968 insert_into_field_list (varinfo_t base, varinfo_t field)
2970 varinfo_t prev = base;
2971 varinfo_t curr = base->next;
2973 if (curr == NULL)
2975 prev->next = field;
2976 field->next = NULL;
2978 else
2980 while (curr)
2982 if (field->offset <= curr->offset)
2983 break;
2984 prev = curr;
2985 curr = curr->next;
2987 field->next = prev->next;
2988 prev->next = field;
2992 /* qsort comparison function for two fieldoff's PA and PB */
2994 static int
2995 fieldoff_compare (const void *pa, const void *pb)
2997 const fieldoff_s *foa = (const fieldoff_s *)pa;
2998 const fieldoff_s *fob = (const fieldoff_s *)pb;
2999 HOST_WIDE_INT foasize, fobsize;
3001 if (foa->offset != fob->offset)
3002 return foa->offset - fob->offset;
3004 foasize = TREE_INT_CST_LOW (DECL_SIZE (foa->field));
3005 fobsize = TREE_INT_CST_LOW (DECL_SIZE (fob->field));
3006 return foasize - fobsize;
3009 /* Sort a fieldstack according to the field offset and sizes. */
3010 void sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
3012 qsort (VEC_address (fieldoff_s, fieldstack),
3013 VEC_length (fieldoff_s, fieldstack),
3014 sizeof (fieldoff_s),
3015 fieldoff_compare);
3018 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all the fields
3019 of TYPE onto fieldstack, recording their offsets along the way.
3020 OFFSET is used to keep track of the offset in this entire structure, rather
3021 than just the immediately containing structure. Returns the number
3022 of fields pushed.
3023 HAS_UNION is set to true if we find a union type as a field of
3024 TYPE. */
3027 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
3028 HOST_WIDE_INT offset, bool *has_union)
3030 tree field;
3031 int count = 0;
3033 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3034 if (TREE_CODE (field) == FIELD_DECL)
3036 bool push = false;
3037 int pushed = 0;
3039 if (has_union
3040 && (TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
3041 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
3042 *has_union = true;
3044 if (!var_can_have_subvars (field))
3045 push = true;
3046 else if (!(pushed = push_fields_onto_fieldstack
3047 (TREE_TYPE (field), fieldstack,
3048 offset + bitpos_of_field (field), has_union))
3049 && DECL_SIZE (field)
3050 && !integer_zerop (DECL_SIZE (field)))
3051 /* Empty structures may have actual size, like in C++. So
3052 see if we didn't push any subfields and the size is
3053 nonzero, push the field onto the stack */
3054 push = true;
3056 if (push)
3058 fieldoff_s *pair;
3060 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
3061 pair->field = field;
3062 pair->offset = offset + bitpos_of_field (field);
3063 count++;
3065 else
3066 count += pushed;
3069 return count;
3072 static void
3073 make_constraint_to_anything (varinfo_t vi)
3075 struct constraint_expr lhs, rhs;
3077 lhs.var = vi->id;
3078 lhs.offset = 0;
3079 lhs.type = SCALAR;
3081 rhs.var = anything_id;
3082 rhs.offset =0 ;
3083 rhs.type = ADDRESSOF;
3084 process_constraint (new_constraint (lhs, rhs));
3088 /* Return true if FIELDSTACK contains fields that overlap.
3089 FIELDSTACK is assumed to be sorted by offset. */
3091 static bool
3092 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
3094 fieldoff_s *fo = NULL;
3095 unsigned int i;
3096 HOST_WIDE_INT lastoffset = -1;
3098 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3100 if (fo->offset == lastoffset)
3101 return true;
3102 lastoffset = fo->offset;
3104 return false;
3107 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
3108 This will also create any varinfo structures necessary for fields
3109 of DECL. */
3111 static unsigned int
3112 create_variable_info_for (tree decl, const char *name)
3114 unsigned int index = VEC_length (varinfo_t, varmap);
3115 varinfo_t vi;
3116 tree decltype = TREE_TYPE (decl);
3117 bool notokay = false;
3118 bool hasunion;
3119 bool is_global = DECL_P (decl) ? is_global_var (decl) : false;
3120 VEC (fieldoff_s,heap) *fieldstack = NULL;
3123 hasunion = TREE_CODE (decltype) == UNION_TYPE
3124 || TREE_CODE (decltype) == QUAL_UNION_TYPE;
3125 if (var_can_have_subvars (decl) && use_field_sensitive && !hasunion)
3127 push_fields_onto_fieldstack (decltype, &fieldstack, 0, &hasunion);
3128 if (hasunion)
3130 VEC_free (fieldoff_s, heap, fieldstack);
3131 notokay = true;
3136 /* If the variable doesn't have subvars, we may end up needing to
3137 sort the field list and create fake variables for all the
3138 fields. */
3139 vi = new_var_info (decl, index, name, index);
3140 vi->decl = decl;
3141 vi->offset = 0;
3142 vi->has_union = hasunion;
3143 if (!TYPE_SIZE (decltype)
3144 || TREE_CODE (TYPE_SIZE (decltype)) != INTEGER_CST
3145 || TREE_CODE (decltype) == ARRAY_TYPE
3146 || TREE_CODE (decltype) == UNION_TYPE
3147 || TREE_CODE (decltype) == QUAL_UNION_TYPE)
3149 vi->is_unknown_size_var = true;
3150 vi->fullsize = ~0;
3151 vi->size = ~0;
3153 else
3155 vi->fullsize = TREE_INT_CST_LOW (TYPE_SIZE (decltype));
3156 vi->size = vi->fullsize;
3159 insert_id_for_tree (vi->decl, index);
3160 VEC_safe_push (varinfo_t, heap, varmap, vi);
3161 if (is_global)
3162 make_constraint_to_anything (vi);
3164 stats.total_vars++;
3165 if (use_field_sensitive
3166 && !notokay
3167 && !vi->is_unknown_size_var
3168 && var_can_have_subvars (decl))
3170 unsigned int newindex = VEC_length (varinfo_t, varmap);
3171 fieldoff_s *fo = NULL;
3172 unsigned int i;
3173 tree field;
3175 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3177 if (!DECL_SIZE (fo->field)
3178 || TREE_CODE (DECL_SIZE (fo->field)) != INTEGER_CST
3179 || TREE_CODE (TREE_TYPE (fo->field)) == ARRAY_TYPE
3180 || fo->offset < 0)
3182 notokay = true;
3183 break;
3187 /* We can't sort them if we have a field with a variable sized type,
3188 which will make notokay = true. In that case, we are going to return
3189 without creating varinfos for the fields anyway, so sorting them is a
3190 waste to boot. */
3191 if (!notokay)
3193 sort_fieldstack (fieldstack);
3194 /* Due to some C++ FE issues, like PR 22488, we might end up
3195 what appear to be overlapping fields even though they,
3196 in reality, do not overlap. Until the C++ FE is fixed,
3197 we will simply disable field-sensitivity for these cases. */
3198 notokay = check_for_overlaps (fieldstack);
3202 if (VEC_length (fieldoff_s, fieldstack) != 0)
3203 fo = VEC_index (fieldoff_s, fieldstack, 0);
3205 if (fo == NULL || notokay)
3207 vi->is_unknown_size_var = 1;
3208 vi->fullsize = ~0;
3209 vi->size = ~0;
3210 VEC_free (fieldoff_s, heap, fieldstack);
3211 return index;
3214 field = fo->field;
3215 vi->size = TREE_INT_CST_LOW (DECL_SIZE (field));
3216 vi->offset = fo->offset;
3217 for (i = 1; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3219 varinfo_t newvi;
3220 const char *newname;
3221 char *tempname;
3223 field = fo->field;
3224 newindex = VEC_length (varinfo_t, varmap);
3225 asprintf (&tempname, "%s.%s", vi->name, alias_get_name (field));
3226 newname = ggc_strdup (tempname);
3227 free (tempname);
3228 newvi = new_var_info (decl, newindex, newname, newindex);
3229 newvi->offset = fo->offset;
3230 newvi->size = TREE_INT_CST_LOW (DECL_SIZE (field));
3231 newvi->fullsize = vi->fullsize;
3232 insert_into_field_list (vi, newvi);
3233 VEC_safe_push (varinfo_t, heap, varmap, newvi);
3234 if (is_global)
3235 make_constraint_to_anything (newvi);
3237 stats.total_vars++;
3239 VEC_free (fieldoff_s, heap, fieldstack);
3241 return index;
3244 /* Print out the points-to solution for VAR to FILE. */
3246 void
3247 dump_solution_for_var (FILE *file, unsigned int var)
3249 varinfo_t vi = get_varinfo (var);
3250 unsigned int i;
3251 bitmap_iterator bi;
3253 fprintf (file, "%s = { ", vi->name);
3254 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (vi->node)->solution, 0, i, bi)
3256 fprintf (file, "%s ", get_varinfo (i)->name);
3258 fprintf (file, "}\n");
3261 /* Print the points-to solution for VAR to stdout. */
3263 void
3264 debug_solution_for_var (unsigned int var)
3266 dump_solution_for_var (stdout, var);
3270 /* Create varinfo structures for all of the variables in the
3271 function for intraprocedural mode. */
3273 static void
3274 intra_create_variable_infos (void)
3276 tree t;
3278 /* For each incoming argument arg, ARG = &ANYTHING */
3279 for (t = DECL_ARGUMENTS (current_function_decl); t; t = TREE_CHAIN (t))
3281 struct constraint_expr lhs;
3282 struct constraint_expr rhs;
3283 varinfo_t p;
3285 lhs.offset = 0;
3286 lhs.type = SCALAR;
3287 lhs.var = create_variable_info_for (t, alias_get_name (t));
3289 rhs.var = anything_id;
3290 rhs.type = ADDRESSOF;
3291 rhs.offset = 0;
3293 for (p = get_varinfo (lhs.var); p; p = p->next)
3295 struct constraint_expr temp = lhs;
3296 temp.var = p->id;
3297 process_constraint (new_constraint (temp, rhs));
3303 /* Set bits in INTO corresponding to the variable uids in solution set
3304 FROM */
3306 static void
3307 set_uids_in_ptset (bitmap into, bitmap from)
3309 unsigned int i;
3310 bitmap_iterator bi;
3311 bool found_anyoffset = false;
3312 subvar_t sv;
3314 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
3316 varinfo_t vi = get_varinfo (i);
3318 /* If we find ANYOFFSET in the solution and the solution
3319 includes SFTs for some structure, then all the SFTs in that
3320 structure will need to be added to the alias set. */
3321 if (vi->id == anyoffset_id)
3323 found_anyoffset = true;
3324 continue;
3327 /* The only artificial variables that are allowed in a may-alias
3328 set are heap variables. */
3329 if (vi->is_artificial_var && !vi->is_heap_var)
3330 continue;
3332 if (vi->has_union && get_subvars_for_var (vi->decl) != NULL)
3334 /* Variables containing unions may need to be converted to
3335 their SFT's, because SFT's can have unions and we cannot. */
3336 for (sv = get_subvars_for_var (vi->decl); sv; sv = sv->next)
3337 bitmap_set_bit (into, DECL_UID (sv->var));
3339 else if (TREE_CODE (vi->decl) == VAR_DECL
3340 || TREE_CODE (vi->decl) == PARM_DECL)
3342 if (found_anyoffset
3343 && var_can_have_subvars (vi->decl)
3344 && get_subvars_for_var (vi->decl))
3346 /* If ANYOFFSET is in the solution set and VI->DECL is
3347 an aggregate variable with sub-variables, then any of
3348 the SFTs inside VI->DECL may have been accessed. Add
3349 all the sub-vars for VI->DECL. */
3350 for (sv = get_subvars_for_var (vi->decl); sv; sv = sv->next)
3351 bitmap_set_bit (into, DECL_UID (sv->var));
3353 else if (var_can_have_subvars (vi->decl)
3354 && get_subvars_for_var (vi->decl))
3356 /* If VI->DECL is an aggregate for which we created
3357 SFTs, add the SFT corresponding to VI->OFFSET. */
3358 tree sft = get_subvar_at (vi->decl, vi->offset);
3359 bitmap_set_bit (into, DECL_UID (sft));
3361 else
3363 /* Otherwise, just add VI->DECL to the alias set. */
3364 bitmap_set_bit (into, DECL_UID (vi->decl));
3371 static bool have_alias_info = false;
3373 /* Given a pointer variable P, fill in its points-to set, or return
3374 false if we can't. */
3376 bool
3377 find_what_p_points_to (tree p)
3379 unsigned int id = 0;
3381 if (!have_alias_info)
3382 return false;
3384 if (lookup_id_for_tree (p, &id))
3386 varinfo_t vi = get_varinfo (id);
3388 if (vi->is_artificial_var)
3389 return false;
3391 /* See if this is a field or a structure. */
3392 if (vi->size != vi->fullsize)
3394 /* Nothing currently asks about structure fields directly,
3395 but when they do, we need code here to hand back the
3396 points-to set. */
3397 if (!var_can_have_subvars (vi->decl)
3398 || get_subvars_for_var (vi->decl) == NULL)
3399 return false;
3401 else
3403 struct ptr_info_def *pi = get_ptr_info (p);
3404 unsigned int i;
3405 bitmap_iterator bi;
3407 /* This variable may have been collapsed, let's get the real
3408 variable. */
3409 vi = get_varinfo (vi->node);
3411 /* Translate artificial variables into SSA_NAME_PTR_INFO
3412 attributes. */
3413 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
3415 varinfo_t vi = get_varinfo (i);
3417 if (vi->is_artificial_var)
3419 /* FIXME. READONLY should be handled better so that
3420 flow insensitive aliasing can disregard writable
3421 aliases. */
3422 if (vi->id == nothing_id)
3423 pi->pt_null = 1;
3424 else if (vi->id == anything_id)
3425 pi->pt_anything = 1;
3426 else if (vi->id == readonly_id)
3427 pi->pt_anything = 1;
3428 else if (vi->id == integer_id)
3429 pi->pt_anything = 1;
3430 else if (vi->is_heap_var)
3431 pi->pt_global_mem = 1;
3435 if (pi->pt_anything)
3436 return false;
3438 if (!pi->pt_vars)
3439 pi->pt_vars = BITMAP_GGC_ALLOC ();
3441 set_uids_in_ptset (pi->pt_vars, vi->solution);
3443 if (bitmap_empty_p (pi->pt_vars))
3444 pi->pt_vars = NULL;
3446 return true;
3450 return false;
3454 /* Initialize things necessary to perform PTA */
3456 static void
3457 init_alias_vars (void)
3459 bitmap_obstack_initialize (&ptabitmap_obstack);
3463 /* Dump points-to information to OUTFILE. */
3465 void
3466 dump_sa_points_to_info (FILE *outfile)
3468 unsigned int i;
3470 fprintf (outfile, "\nPoints-to sets\n\n");
3472 if (dump_flags & TDF_STATS)
3474 fprintf (outfile, "Stats:\n");
3475 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
3476 fprintf (outfile, "Statically unified vars: %d\n",
3477 stats.unified_vars_static);
3478 fprintf (outfile, "Collapsed vars: %d\n", stats.collapsed_vars);
3479 fprintf (outfile, "Dynamically unified vars: %d\n",
3480 stats.unified_vars_dynamic);
3481 fprintf (outfile, "Iterations: %d\n", stats.iterations);
3484 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
3485 dump_solution_for_var (outfile, i);
3489 /* Debug points-to information to stderr. */
3491 void
3492 debug_sa_points_to_info (void)
3494 dump_sa_points_to_info (stderr);
3498 /* Initialize the always-existing constraint variables for NULL
3499 ANYTHING, READONLY, and INTEGER */
3501 static void
3502 init_base_vars (void)
3504 struct constraint_expr lhs, rhs;
3506 /* Create the NULL variable, used to represent that a variable points
3507 to NULL. */
3508 nothing_tree = create_tmp_var_raw (void_type_node, "NULL");
3509 var_nothing = new_var_info (nothing_tree, 0, "NULL", 0);
3510 insert_id_for_tree (nothing_tree, 0);
3511 var_nothing->is_artificial_var = 1;
3512 var_nothing->offset = 0;
3513 var_nothing->size = ~0;
3514 var_nothing->fullsize = ~0;
3515 var_nothing->is_special_var = 1;
3516 nothing_id = 0;
3517 VEC_safe_push (varinfo_t, heap, varmap, var_nothing);
3519 /* Create the ANYTHING variable, used to represent that a variable
3520 points to some unknown piece of memory. */
3521 anything_tree = create_tmp_var_raw (void_type_node, "ANYTHING");
3522 var_anything = new_var_info (anything_tree, 1, "ANYTHING", 1);
3523 insert_id_for_tree (anything_tree, 1);
3524 var_anything->is_artificial_var = 1;
3525 var_anything->size = ~0;
3526 var_anything->offset = 0;
3527 var_anything->next = NULL;
3528 var_anything->fullsize = ~0;
3529 var_anything->is_special_var = 1;
3530 anything_id = 1;
3532 /* Anything points to anything. This makes deref constraints just
3533 work in the presence of linked list and other p = *p type loops,
3534 by saying that *ANYTHING = ANYTHING. */
3535 VEC_safe_push (varinfo_t, heap, varmap, var_anything);
3536 lhs.type = SCALAR;
3537 lhs.var = anything_id;
3538 lhs.offset = 0;
3539 rhs.type = ADDRESSOF;
3540 rhs.var = anything_id;
3541 rhs.offset = 0;
3542 var_anything->address_taken = true;
3544 /* This specifically does not use process_constraint because
3545 process_constraint ignores all anything = anything constraints, since all
3546 but this one are redundant. */
3547 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
3549 /* Create the READONLY variable, used to represent that a variable
3550 points to readonly memory. */
3551 readonly_tree = create_tmp_var_raw (void_type_node, "READONLY");
3552 var_readonly = new_var_info (readonly_tree, 2, "READONLY", 2);
3553 var_readonly->is_artificial_var = 1;
3554 var_readonly->offset = 0;
3555 var_readonly->size = ~0;
3556 var_readonly->fullsize = ~0;
3557 var_readonly->next = NULL;
3558 var_readonly->is_special_var = 1;
3559 insert_id_for_tree (readonly_tree, 2);
3560 readonly_id = 2;
3561 VEC_safe_push (varinfo_t, heap, varmap, var_readonly);
3563 /* readonly memory points to anything, in order to make deref
3564 easier. In reality, it points to anything the particular
3565 readonly variable can point to, but we don't track this
3566 separately. */
3567 lhs.type = SCALAR;
3568 lhs.var = readonly_id;
3569 lhs.offset = 0;
3570 rhs.type = ADDRESSOF;
3571 rhs.var = anything_id;
3572 rhs.offset = 0;
3574 process_constraint (new_constraint (lhs, rhs));
3576 /* Create the INTEGER variable, used to represent that a variable points
3577 to an INTEGER. */
3578 integer_tree = create_tmp_var_raw (void_type_node, "INTEGER");
3579 var_integer = new_var_info (integer_tree, 3, "INTEGER", 3);
3580 insert_id_for_tree (integer_tree, 3);
3581 var_integer->is_artificial_var = 1;
3582 var_integer->size = ~0;
3583 var_integer->fullsize = ~0;
3584 var_integer->offset = 0;
3585 var_integer->next = NULL;
3586 var_integer->is_special_var = 1;
3587 integer_id = 3;
3588 VEC_safe_push (varinfo_t, heap, varmap, var_integer);
3590 /* *INTEGER = ANYTHING, because we don't know where a dereference of a random
3591 integer will point to. */
3592 lhs.type = SCALAR;
3593 lhs.var = integer_id;
3594 lhs.offset = 0;
3595 rhs.type = ADDRESSOF;
3596 rhs.var = anything_id;
3597 rhs.offset = 0;
3598 process_constraint (new_constraint (lhs, rhs));
3600 /* Create the ANYOFFSET variable, used to represent an arbitrary offset
3601 inside an object. This is similar to ANYTHING, but less drastic.
3602 It means that the pointer can point anywhere inside an object,
3603 but not outside of it. */
3604 anyoffset_tree = create_tmp_var_raw (void_type_node, "ANYOFFSET");
3605 anyoffset_id = 4;
3606 var_anyoffset = new_var_info (anyoffset_tree, anyoffset_id, "ANYOFFSET",
3607 anyoffset_id);
3608 insert_id_for_tree (anyoffset_tree, anyoffset_id);
3609 var_anyoffset->is_artificial_var = 1;
3610 var_anyoffset->size = ~0;
3611 var_anyoffset->offset = 0;
3612 var_anyoffset->next = NULL;
3613 var_anyoffset->fullsize = ~0;
3614 var_anyoffset->is_special_var = 1;
3615 VEC_safe_push (varinfo_t, heap, varmap, var_anyoffset);
3617 /* ANYOFFSET points to ANYOFFSET. */
3618 lhs.type = SCALAR;
3619 lhs.var = anyoffset_id;
3620 lhs.offset = 0;
3621 rhs.type = ADDRESSOF;
3622 rhs.var = anyoffset_id;
3623 rhs.offset = 0;
3624 process_constraint (new_constraint (lhs, rhs));
3627 /* Return true if we actually need to solve the constraint graph in order to
3628 get our points-to sets. This is false when, for example, no addresses are
3629 taken other than special vars, or all points-to sets with members already
3630 contain the anything variable and there are no predecessors for other
3631 sets. */
3633 static bool
3634 need_to_solve (void)
3636 int i;
3637 varinfo_t v;
3638 bool found_address_taken = false;
3639 bool found_non_anything = false;
3641 for (i = 0; VEC_iterate (varinfo_t, varmap, i, v); i++)
3643 if (v->is_special_var)
3644 continue;
3646 if (v->address_taken)
3647 found_address_taken = true;
3649 if (v->solution
3650 && !bitmap_empty_p (v->solution)
3651 && !bitmap_bit_p (v->solution, anything_id))
3652 found_non_anything = true;
3653 else if (bitmap_empty_p (v->solution)
3654 && VEC_length (constraint_edge_t, graph->preds[v->id]) != 0)
3655 found_non_anything = true;
3657 if (found_address_taken && found_non_anything)
3658 return true;
3661 return false;
3664 /* Create points-to sets for the current function. See the comments
3665 at the start of the file for an algorithmic overview. */
3667 void
3668 compute_points_to_sets (struct alias_info *ai)
3670 basic_block bb;
3672 timevar_push (TV_TREE_PTA);
3674 init_alias_vars ();
3676 constraint_pool = create_alloc_pool ("Constraint pool",
3677 sizeof (struct constraint), 30);
3678 variable_info_pool = create_alloc_pool ("Variable info pool",
3679 sizeof (struct variable_info), 30);
3680 constraint_edge_pool = create_alloc_pool ("Constraint edges",
3681 sizeof (struct constraint_edge), 30);
3683 constraints = VEC_alloc (constraint_t, heap, 8);
3684 varmap = VEC_alloc (varinfo_t, heap, 8);
3685 id_for_tree = htab_create (10, tree_id_hash, tree_id_eq, free);
3686 memset (&stats, 0, sizeof (stats));
3688 init_base_vars ();
3690 intra_create_variable_infos ();
3692 /* Now walk all statements and derive aliases. */
3693 FOR_EACH_BB (bb)
3695 block_stmt_iterator bsi;
3696 tree phi;
3698 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
3699 if (is_gimple_reg (PHI_RESULT (phi)))
3700 find_func_aliases (phi, ai);
3702 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3703 find_func_aliases (bsi_stmt (bsi), ai);
3706 build_constraint_graph ();
3708 if (dump_file)
3710 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
3711 dump_constraints (dump_file);
3714 if (need_to_solve ())
3716 if (dump_file)
3717 fprintf (dump_file, "\nCollapsing static cycles and doing variable "
3718 "substitution:\n");
3720 find_and_collapse_graph_cycles (graph, false);
3721 perform_var_substitution (graph);
3723 if (dump_file)
3724 fprintf (dump_file, "\nSolving graph:\n");
3726 solve_graph (graph);
3729 if (dump_file)
3730 dump_sa_points_to_info (dump_file);
3732 have_alias_info = true;
3734 timevar_pop (TV_TREE_PTA);
3738 /* Delete created points-to sets. */
3740 void
3741 delete_points_to_sets (void)
3743 varinfo_t v;
3744 int i;
3746 htab_delete (id_for_tree);
3747 bitmap_obstack_release (&ptabitmap_obstack);
3748 VEC_free (constraint_t, heap, constraints);
3750 for (i = 0; VEC_iterate (varinfo_t, varmap, i, v); i++)
3752 VEC_free (constraint_edge_t, heap, graph->succs[i]);
3753 VEC_free (constraint_edge_t, heap, graph->preds[i]);
3754 VEC_free (constraint_t, heap, v->complex);
3756 free (graph->succs);
3757 free (graph->preds);
3758 free (graph);
3760 VEC_free (varinfo_t, heap, varmap);
3761 free_alloc_pool (variable_info_pool);
3762 free_alloc_pool (constraint_pool);
3763 free_alloc_pool (constraint_edge_pool);
3765 have_alias_info = false;