]>
Commit | Line | Data |
---|---|---|
910fdc79 | 1 | /* Tree based points-to analysis |
36dc1a88 | 2 | Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
c75c517d | 3 | Free Software Foundation, Inc. |
910fdc79 DB |
4 | Contributed by Daniel Berlin <dberlin@dberlin.org> |
5 | ||
9dcd6f09 | 6 | This file is part of GCC. |
910fdc79 | 7 | |
9dcd6f09 NC |
8 | GCC is free software; you can redistribute it and/or modify |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
910fdc79 | 12 | |
9dcd6f09 NC |
13 | GCC is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
910fdc79 | 17 | |
9dcd6f09 NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
910fdc79 DB |
21 | |
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" | |
910fdc79 | 29 | #include "flags.h" |
910fdc79 DB |
30 | #include "basic-block.h" |
31 | #include "output.h" | |
910fdc79 | 32 | #include "tree.h" |
910fdc79 DB |
33 | #include "tree-flow.h" |
34 | #include "tree-inline.h" | |
718f9c0f | 35 | #include "diagnostic-core.h" |
726a989a | 36 | #include "gimple.h" |
910fdc79 DB |
37 | #include "hashtab.h" |
38 | #include "function.h" | |
39 | #include "cgraph.h" | |
40 | #include "tree-pass.h" | |
41 | #include "timevar.h" | |
42 | #include "alloc-pool.h" | |
43 | #include "splay-tree.h" | |
a916f21d | 44 | #include "params.h" |
4ee00913 | 45 | #include "cgraph.h" |
c58936b6 | 46 | #include "alias.h" |
38635499 | 47 | #include "pointer-set.h" |
910fdc79 DB |
48 | |
49 | /* The idea behind this analyzer is to generate set constraints from the | |
50 | program, then solve the resulting constraints in order to generate the | |
c58936b6 | 51 | points-to sets. |
910fdc79 DB |
52 | |
53 | Set constraints are a way of modeling program analysis problems that | |
54 | involve sets. They consist of an inclusion constraint language, | |
55 | describing the variables (each variable is a set) and operations that | |
56 | are involved on the variables, and a set of rules that derive facts | |
57 | from these operations. To solve a system of set constraints, you derive | |
58 | all possible facts under the rules, which gives you the correct sets | |
59 | as a consequence. | |
60 | ||
61 | See "Efficient Field-sensitive pointer analysis for C" by "David | |
62 | J. Pearce and Paul H. J. Kelly and Chris Hankin, at | |
63 | http://citeseer.ist.psu.edu/pearce04efficient.html | |
64 | ||
65 | Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines | |
66 | of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at | |
c58936b6 DB |
67 | http://citeseer.ist.psu.edu/heintze01ultrafast.html |
68 | ||
69 | There are three types of real constraint expressions, DEREF, | |
3e5937d7 | 70 | ADDRESSOF, and SCALAR. Each constraint expression consists |
c58936b6 | 71 | of a constraint type, a variable, and an offset. |
910fdc79 | 72 | |
910fdc79 DB |
73 | SCALAR is a constraint expression type used to represent x, whether |
74 | it appears on the LHS or the RHS of a statement. | |
75 | DEREF is a constraint expression type used to represent *x, whether | |
c58936b6 | 76 | it appears on the LHS or the RHS of a statement. |
910fdc79 | 77 | ADDRESSOF is a constraint expression used to represent &x, whether |
607fb860 | 78 | it appears on the LHS or the RHS of a statement. |
c58936b6 | 79 | |
910fdc79 DB |
80 | Each pointer variable in the program is assigned an integer id, and |
81 | each field of a structure variable is assigned an integer id as well. | |
c58936b6 | 82 | |
910fdc79 DB |
83 | Structure variables are linked to their list of fields through a "next |
84 | field" in each variable that points to the next field in offset | |
c58936b6 DB |
85 | order. |
86 | Each variable for a structure field has | |
910fdc79 DB |
87 | |
88 | 1. "size", that tells the size in bits of that field. | |
89 | 2. "fullsize, that tells the size in bits of the entire structure. | |
90 | 3. "offset", that tells the offset in bits from the beginning of the | |
91 | structure to this field. | |
92 | ||
c58936b6 | 93 | Thus, |
910fdc79 DB |
94 | struct f |
95 | { | |
96 | int a; | |
97 | int b; | |
98 | } foo; | |
99 | int *bar; | |
100 | ||
101 | looks like | |
102 | ||
103 | foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b | |
104 | foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL | |
105 | bar -> id 3, size 32, offset 0, fullsize 32, next NULL | |
106 | ||
c58936b6 | 107 | |
910fdc79 DB |
108 | In order to solve the system of set constraints, the following is |
109 | done: | |
110 | ||
111 | 1. Each constraint variable x has a solution set associated with it, | |
112 | Sol(x). | |
c58936b6 | 113 | |
910fdc79 DB |
114 | 2. Constraints are separated into direct, copy, and complex. |
115 | Direct constraints are ADDRESSOF constraints that require no extra | |
116 | processing, such as P = &Q | |
117 | Copy constraints are those of the form P = Q. | |
2941f691 DB |
118 | Complex constraints are all the constraints involving dereferences |
119 | and offsets (including offsetted copies). | |
c58936b6 | 120 | |
910fdc79 | 121 | 3. All direct constraints of the form P = &Q are processed, such |
c58936b6 | 122 | that Q is added to Sol(P) |
910fdc79 DB |
123 | |
124 | 4. All complex constraints for a given constraint variable are stored in a | |
c58936b6 | 125 | linked list attached to that variable's node. |
910fdc79 DB |
126 | |
127 | 5. A directed graph is built out of the copy constraints. Each | |
c58936b6 | 128 | constraint variable is a node in the graph, and an edge from |
910fdc79 | 129 | Q to P is added for each copy constraint of the form P = Q |
c58936b6 | 130 | |
910fdc79 DB |
131 | 6. The graph is then walked, and solution sets are |
132 | propagated along the copy edges, such that an edge from Q to P | |
133 | causes Sol(P) <- Sol(P) union Sol(Q). | |
c58936b6 | 134 | |
910fdc79 | 135 | 7. As we visit each node, all complex constraints associated with |
607fb860 | 136 | that node are processed by adding appropriate copy edges to the graph, or the |
c58936b6 | 137 | appropriate variables to the solution set. |
910fdc79 DB |
138 | |
139 | 8. The process of walking the graph is iterated until no solution | |
140 | sets change. | |
141 | ||
142 | Prior to walking the graph in steps 6 and 7, We perform static | |
c58936b6 | 143 | cycle elimination on the constraint graph, as well |
910fdc79 | 144 | as off-line variable substitution. |
c58936b6 | 145 | |
910fdc79 DB |
146 | TODO: Adding offsets to pointer-to-structures can be handled (IE not punted |
147 | on and turned into anything), but isn't. You can just see what offset | |
148 | inside the pointed-to struct it's going to access. | |
c58936b6 | 149 | |
910fdc79 | 150 | TODO: Constant bounded arrays can be handled as if they were structs of the |
c58936b6 | 151 | same number of elements. |
910fdc79 DB |
152 | |
153 | TODO: Modeling heap and incoming pointers becomes much better if we | |
154 | add fields to them as we discover them, which we could do. | |
155 | ||
156 | TODO: We could handle unions, but to be honest, it's probably not | |
157 | worth the pain or slowdown. */ | |
158 | ||
25a6a873 RG |
159 | /* IPA-PTA optimizations possible. |
160 | ||
161 | When the indirect function called is ANYTHING we can add disambiguation | |
162 | based on the function signatures (or simply the parameter count which | |
163 | is the varinfo size). We also do not need to consider functions that | |
164 | do not have their address taken. | |
165 | ||
166 | The is_global_var bit which marks escape points is overly conservative | |
167 | in IPA mode. Split it to is_escape_point and is_global_var - only | |
168 | externally visible globals are escape points in IPA mode. This is | |
169 | also needed to fix the pt_solution_includes_global predicate | |
170 | (and thus ptr_deref_may_alias_global_p). | |
171 | ||
172 | The way we introduce DECL_PT_UID to avoid fixing up all points-to | |
173 | sets in the translation unit when we copy a DECL during inlining | |
174 | pessimizes precision. The advantage is that the DECL_PT_UID keeps | |
175 | compile-time and memory usage overhead low - the points-to sets | |
176 | do not grow or get unshared as they would during a fixup phase. | |
177 | An alternative solution is to delay IPA PTA until after all | |
178 | inlining transformations have been applied. | |
179 | ||
180 | The way we propagate clobber/use information isn't optimized. | |
181 | It should use a new complex constraint that properly filters | |
182 | out local variables of the callee (though that would make | |
183 | the sets invalid after inlining). OTOH we might as well | |
184 | admit defeat to WHOPR and simply do all the clobber/use analysis | |
185 | and propagation after PTA finished but before we threw away | |
186 | points-to information for memory variables. WHOPR and PTA | |
187 | do not play along well anyway - the whole constraint solving | |
188 | would need to be done in WPA phase and it will be very interesting | |
189 | to apply the results to local SSA names during LTRANS phase. | |
190 | ||
191 | We probably should compute a per-function unit-ESCAPE solution | |
192 | propagating it simply like the clobber / uses solutions. The | |
193 | solution can go alongside the non-IPA espaced solution and be | |
194 | used to query which vars escape the unit through a function. | |
195 | ||
196 | We never put function decls in points-to sets so we do not | |
197 | keep the set of called functions for indirect calls. | |
198 | ||
199 | And probably more. */ | |
21392f19 | 200 | |
910fdc79 | 201 | static bool use_field_sensitive = true; |
4ee00913 | 202 | static int in_ipa_mode = 0; |
3e5937d7 DB |
203 | |
204 | /* Used for predecessor bitmaps. */ | |
4ee00913 | 205 | static bitmap_obstack predbitmap_obstack; |
3e5937d7 DB |
206 | |
207 | /* Used for points-to sets. */ | |
208 | static bitmap_obstack pta_obstack; | |
209 | ||
210 | /* Used for oldsolution members of variables. */ | |
211 | static bitmap_obstack oldpta_obstack; | |
212 | ||
213 | /* Used for per-solver-iteration bitmaps. */ | |
4ee00913 DB |
214 | static bitmap_obstack iteration_obstack; |
215 | ||
910fdc79 | 216 | static unsigned int create_variable_info_for (tree, const char *); |
3e5937d7 DB |
217 | typedef struct constraint_graph *constraint_graph_t; |
218 | static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool); | |
910fdc79 | 219 | |
5006671f RG |
220 | struct constraint; |
221 | typedef struct constraint *constraint_t; | |
222 | ||
910fdc79 | 223 | DEF_VEC_P(constraint_t); |
b5efa470 | 224 | DEF_VEC_ALLOC_P(constraint_t,heap); |
910fdc79 | 225 | |
4ee00913 DB |
226 | #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \ |
227 | if (a) \ | |
228 | EXECUTE_IF_SET_IN_BITMAP (a, b, c, d) | |
229 | ||
910fdc79 DB |
230 | static struct constraint_stats |
231 | { | |
232 | unsigned int total_vars; | |
3e5937d7 | 233 | unsigned int nonpointer_vars; |
910fdc79 DB |
234 | unsigned int unified_vars_static; |
235 | unsigned int unified_vars_dynamic; | |
236 | unsigned int iterations; | |
4ee00913 | 237 | unsigned int num_edges; |
3e5937d7 DB |
238 | unsigned int num_implicit_edges; |
239 | unsigned int points_to_sets_created; | |
910fdc79 DB |
240 | } stats; |
241 | ||
242 | struct variable_info | |
243 | { | |
244 | /* ID of this variable */ | |
245 | unsigned int id; | |
246 | ||
910fdc79 DB |
247 | /* True if this is a variable created by the constraint analysis, such as |
248 | heap variables and constraints we had to break up. */ | |
74d27244 | 249 | unsigned int is_artificial_var : 1; |
c58936b6 | 250 | |
13c2c08b DB |
251 | /* True if this is a special variable whose solution set should not be |
252 | changed. */ | |
74d27244 | 253 | unsigned int is_special_var : 1; |
910fdc79 DB |
254 | |
255 | /* True for variables whose size is not known or variable. */ | |
74d27244 | 256 | unsigned int is_unknown_size_var : 1; |
910fdc79 | 257 | |
e5bae89b RG |
258 | /* True for (sub-)fields that represent a whole variable. */ |
259 | unsigned int is_full_var : 1; | |
260 | ||
e8ca4159 | 261 | /* True if this is a heap variable. */ |
74d27244 RG |
262 | unsigned int is_heap_var : 1; |
263 | ||
264 | /* True if this is a variable tracking a restrict pointer source. */ | |
265 | unsigned int is_restrict_var : 1; | |
e8ca4159 | 266 | |
9e39dba6 RG |
267 | /* True if this field may contain pointers. */ |
268 | unsigned int may_have_pointers : 1; | |
269 | ||
18abb35e RG |
270 | /* True if this field has only restrict qualified pointers. */ |
271 | unsigned int only_restrict_pointers : 1; | |
272 | ||
0bbf2ffa RG |
273 | /* True if this represents a global variable. */ |
274 | unsigned int is_global_var : 1; | |
275 | ||
25a6a873 RG |
276 | /* True if this represents a IPA function info. */ |
277 | unsigned int is_fn_info : 1; | |
278 | ||
795a337a RG |
279 | /* A link to the variable for the next field in this structure. */ |
280 | struct variable_info *next; | |
281 | ||
282 | /* Offset of this variable, in bits, from the base variable */ | |
283 | unsigned HOST_WIDE_INT offset; | |
284 | ||
285 | /* Size of the variable, in bits. */ | |
286 | unsigned HOST_WIDE_INT size; | |
287 | ||
288 | /* Full size of the base variable, in bits. */ | |
289 | unsigned HOST_WIDE_INT fullsize; | |
290 | ||
291 | /* Name of this variable */ | |
292 | const char *name; | |
293 | ||
294 | /* Tree that this variable is associated with. */ | |
295 | tree decl; | |
296 | ||
910fdc79 DB |
297 | /* Points-to set for this variable. */ |
298 | bitmap solution; | |
299 | ||
3e5937d7 DB |
300 | /* Old points-to set for this variable. */ |
301 | bitmap oldsolution; | |
910fdc79 DB |
302 | }; |
303 | typedef struct variable_info *varinfo_t; | |
304 | ||
305 | static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT); | |
5006671f RG |
306 | static varinfo_t first_or_preceding_vi_for_offset (varinfo_t, |
307 | unsigned HOST_WIDE_INT); | |
0e1f4c6b | 308 | static varinfo_t lookup_vi_for_tree (tree); |
910fdc79 DB |
309 | |
310 | /* Pool of variable info structures. */ | |
311 | static alloc_pool variable_info_pool; | |
312 | ||
313 | DEF_VEC_P(varinfo_t); | |
314 | ||
b5efa470 | 315 | DEF_VEC_ALLOC_P(varinfo_t, heap); |
910fdc79 | 316 | |
38635499 DN |
317 | /* Table of variable info structures for constraint variables. |
318 | Indexed directly by variable info id. */ | |
b5efa470 | 319 | static VEC(varinfo_t,heap) *varmap; |
13c2c08b DB |
320 | |
321 | /* Return the varmap element N */ | |
322 | ||
323 | static inline varinfo_t | |
03190594 | 324 | get_varinfo (unsigned int n) |
13c2c08b | 325 | { |
62e5bf5d | 326 | return VEC_index (varinfo_t, varmap, n); |
13c2c08b | 327 | } |
910fdc79 | 328 | |
b7091901 RG |
329 | /* Static IDs for the special variables. */ |
330 | enum { nothing_id = 0, anything_id = 1, readonly_id = 2, | |
3e8542ca RG |
331 | escaped_id = 3, nonlocal_id = 4, |
332 | storedanything_id = 5, integer_id = 6 }; | |
b7091901 | 333 | |
910fdc79 | 334 | /* Return a new variable info structure consisting for a variable |
0bbf2ffa RG |
335 | named NAME, and using constraint graph node NODE. Append it |
336 | to the vector of variable info structures. */ | |
910fdc79 DB |
337 | |
338 | static varinfo_t | |
0bbf2ffa | 339 | new_var_info (tree t, const char *name) |
910fdc79 | 340 | { |
0bbf2ffa | 341 | unsigned index = VEC_length (varinfo_t, varmap); |
c22940cd | 342 | varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool); |
910fdc79 | 343 | |
0bbf2ffa | 344 | ret->id = index; |
910fdc79 DB |
345 | ret->name = name; |
346 | ret->decl = t; | |
0bbf2ffa RG |
347 | /* Vars without decl are artificial and do not have sub-variables. */ |
348 | ret->is_artificial_var = (t == NULL_TREE); | |
13c2c08b | 349 | ret->is_special_var = false; |
910fdc79 | 350 | ret->is_unknown_size_var = false; |
02583d3c RG |
351 | ret->is_full_var = (t == NULL_TREE); |
352 | ret->is_heap_var = false; | |
353 | ret->is_restrict_var = false; | |
9e39dba6 | 354 | ret->may_have_pointers = true; |
18abb35e | 355 | ret->only_restrict_pointers = false; |
74d27244 | 356 | ret->is_global_var = (t == NULL_TREE); |
25a6a873 | 357 | ret->is_fn_info = false; |
0bbf2ffa | 358 | if (t && DECL_P (t)) |
6c0c92e6 RG |
359 | ret->is_global_var = (is_global_var (t) |
360 | /* We have to treat even local register variables | |
361 | as escape points. */ | |
9a6c9288 RG |
362 | || (TREE_CODE (t) == VAR_DECL |
363 | && DECL_HARD_REGISTER (t))); | |
3e5937d7 | 364 | ret->solution = BITMAP_ALLOC (&pta_obstack); |
74d8fa44 | 365 | ret->oldsolution = NULL; |
910fdc79 | 366 | ret->next = NULL; |
0bbf2ffa | 367 | |
18abb35e RG |
368 | stats.total_vars++; |
369 | ||
0bbf2ffa RG |
370 | VEC_safe_push (varinfo_t, heap, varmap, ret); |
371 | ||
910fdc79 DB |
372 | return ret; |
373 | } | |
374 | ||
3e8542ca RG |
375 | |
376 | /* A map mapping call statements to per-stmt variables for uses | |
377 | and clobbers specific to the call. */ | |
378 | struct pointer_map_t *call_stmt_vars; | |
379 | ||
380 | /* Lookup or create the variable for the call statement CALL. */ | |
381 | ||
382 | static varinfo_t | |
383 | get_call_vi (gimple call) | |
384 | { | |
385 | void **slot_p; | |
386 | varinfo_t vi, vi2; | |
387 | ||
388 | slot_p = pointer_map_insert (call_stmt_vars, call); | |
389 | if (*slot_p) | |
390 | return (varinfo_t) *slot_p; | |
391 | ||
392 | vi = new_var_info (NULL_TREE, "CALLUSED"); | |
393 | vi->offset = 0; | |
394 | vi->size = 1; | |
395 | vi->fullsize = 2; | |
396 | vi->is_full_var = true; | |
397 | ||
398 | vi->next = vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED"); | |
399 | vi2->offset = 1; | |
400 | vi2->size = 1; | |
401 | vi2->fullsize = 2; | |
402 | vi2->is_full_var = true; | |
403 | ||
404 | *slot_p = (void *) vi; | |
405 | return vi; | |
406 | } | |
407 | ||
408 | /* Lookup the variable for the call statement CALL representing | |
409 | the uses. Returns NULL if there is nothing special about this call. */ | |
410 | ||
411 | static varinfo_t | |
412 | lookup_call_use_vi (gimple call) | |
413 | { | |
414 | void **slot_p; | |
415 | ||
416 | slot_p = pointer_map_contains (call_stmt_vars, call); | |
417 | if (slot_p) | |
418 | return (varinfo_t) *slot_p; | |
419 | ||
420 | return NULL; | |
421 | } | |
422 | ||
423 | /* Lookup the variable for the call statement CALL representing | |
424 | the clobbers. Returns NULL if there is nothing special about this call. */ | |
425 | ||
426 | static varinfo_t | |
427 | lookup_call_clobber_vi (gimple call) | |
428 | { | |
429 | varinfo_t uses = lookup_call_use_vi (call); | |
430 | if (!uses) | |
431 | return NULL; | |
432 | ||
433 | return uses->next; | |
434 | } | |
435 | ||
436 | /* Lookup or create the variable for the call statement CALL representing | |
437 | the uses. */ | |
438 | ||
439 | static varinfo_t | |
440 | get_call_use_vi (gimple call) | |
441 | { | |
442 | return get_call_vi (call); | |
443 | } | |
444 | ||
445 | /* Lookup or create the variable for the call statement CALL representing | |
446 | the clobbers. */ | |
447 | ||
448 | static varinfo_t ATTRIBUTE_UNUSED | |
449 | get_call_clobber_vi (gimple call) | |
450 | { | |
451 | return get_call_vi (call)->next; | |
452 | } | |
453 | ||
454 | ||
3e5937d7 | 455 | typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type; |
910fdc79 DB |
456 | |
457 | /* An expression that appears in a constraint. */ | |
458 | ||
c58936b6 | 459 | struct constraint_expr |
910fdc79 DB |
460 | { |
461 | /* Constraint type. */ | |
462 | constraint_expr_type type; | |
463 | ||
464 | /* Variable we are referring to in the constraint. */ | |
465 | unsigned int var; | |
466 | ||
467 | /* Offset, in bits, of this constraint from the beginning of | |
468 | variables it ends up referring to. | |
469 | ||
470 | IOW, in a deref constraint, we would deref, get the result set, | |
471 | then add OFFSET to each member. */ | |
5006671f | 472 | HOST_WIDE_INT offset; |
910fdc79 DB |
473 | }; |
474 | ||
5006671f RG |
475 | /* Use 0x8000... as special unknown offset. */ |
476 | #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1)) | |
477 | ||
4ee00913 DB |
478 | typedef struct constraint_expr ce_s; |
479 | DEF_VEC_O(ce_s); | |
480 | DEF_VEC_ALLOC_O(ce_s, heap); | |
ed6c4831 | 481 | static void get_constraint_for_1 (tree, VEC(ce_s, heap) **, bool, bool); |
1d85354c | 482 | static void get_constraint_for (tree, VEC(ce_s, heap) **); |
ed6c4831 | 483 | static void get_constraint_for_rhs (tree, VEC(ce_s, heap) **); |
4ee00913 | 484 | static void do_deref (VEC (ce_s, heap) **); |
910fdc79 DB |
485 | |
486 | /* Our set constraints are made up of two constraint expressions, one | |
c58936b6 | 487 | LHS, and one RHS. |
910fdc79 DB |
488 | |
489 | As described in the introduction, our set constraints each represent an | |
490 | operation between set valued variables. | |
491 | */ | |
492 | struct constraint | |
493 | { | |
494 | struct constraint_expr lhs; | |
495 | struct constraint_expr rhs; | |
496 | }; | |
497 | ||
498 | /* List of constraints that we use to build the constraint graph from. */ | |
499 | ||
b5efa470 | 500 | static VEC(constraint_t,heap) *constraints; |
910fdc79 DB |
501 | static alloc_pool constraint_pool; |
502 | ||
57250223 DB |
503 | /* The constraint graph is represented as an array of bitmaps |
504 | containing successor nodes. */ | |
910fdc79 DB |
505 | |
506 | struct constraint_graph | |
507 | { | |
3e5937d7 DB |
508 | /* Size of this graph, which may be different than the number of |
509 | nodes in the variable map. */ | |
510 | unsigned int size; | |
511 | ||
512 | /* Explicit successors of each node. */ | |
57250223 | 513 | bitmap *succs; |
3e5937d7 DB |
514 | |
515 | /* Implicit predecessors of each node (Used for variable | |
516 | substitution). */ | |
517 | bitmap *implicit_preds; | |
518 | ||
519 | /* Explicit predecessors of each node (Used for variable substitution). */ | |
57250223 | 520 | bitmap *preds; |
910fdc79 | 521 | |
3e5937d7 DB |
522 | /* Indirect cycle representatives, or -1 if the node has no indirect |
523 | cycles. */ | |
524 | int *indirect_cycles; | |
525 | ||
526 | /* Representative node for a node. rep[a] == a unless the node has | |
527 | been unified. */ | |
528 | unsigned int *rep; | |
529 | ||
7b765bed | 530 | /* Equivalence class representative for a label. This is used for |
3e5937d7 DB |
531 | variable substitution. */ |
532 | int *eq_rep; | |
533 | ||
aa46c8a3 DB |
534 | /* Pointer equivalence label for a node. All nodes with the same |
535 | pointer equivalence label can be unified together at some point | |
536 | (either during constraint optimization or after the constraint | |
537 | graph is built). */ | |
7b765bed DB |
538 | unsigned int *pe; |
539 | ||
540 | /* Pointer equivalence representative for a label. This is used to | |
541 | handle nodes that are pointer equivalent but not location | |
542 | equivalent. We can unite these once the addressof constraints | |
543 | are transformed into initial points-to sets. */ | |
544 | int *pe_rep; | |
545 | ||
546 | /* Pointer equivalence label for each node, used during variable | |
547 | substitution. */ | |
548 | unsigned int *pointer_label; | |
549 | ||
550 | /* Location equivalence label for each node, used during location | |
551 | equivalence finding. */ | |
552 | unsigned int *loc_label; | |
553 | ||
554 | /* Pointed-by set for each node, used during location equivalence | |
555 | finding. This is pointed-by rather than pointed-to, because it | |
556 | is constructed using the predecessor graph. */ | |
557 | bitmap *pointed_by; | |
558 | ||
559 | /* Points to sets for pointer equivalence. This is *not* the actual | |
560 | points-to sets for nodes. */ | |
561 | bitmap *points_to; | |
3e5937d7 DB |
562 | |
563 | /* Bitmap of nodes where the bit is set if the node is a direct | |
564 | node. Used for variable substitution. */ | |
565 | sbitmap direct_nodes; | |
566 | ||
7b765bed DB |
567 | /* Bitmap of nodes where the bit is set if the node is address |
568 | taken. Used for variable substitution. */ | |
569 | bitmap address_taken; | |
570 | ||
3e5937d7 DB |
571 | /* Vector of complex constraints for each graph node. Complex |
572 | constraints are those involving dereferences or offsets that are | |
573 | not 0. */ | |
574 | VEC(constraint_t,heap) **complex; | |
575 | }; | |
910fdc79 DB |
576 | |
577 | static constraint_graph_t graph; | |
578 | ||
3e5937d7 DB |
579 | /* During variable substitution and the offline version of indirect |
580 | cycle finding, we create nodes to represent dereferences and | |
581 | address taken constraints. These represent where these start and | |
582 | end. */ | |
583 | #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap)) | |
584 | #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1)) | |
3e5937d7 DB |
585 | |
586 | /* Return the representative node for NODE, if NODE has been unioned | |
587 | with another NODE. | |
588 | This function performs path compression along the way to finding | |
589 | the representative. */ | |
590 | ||
591 | static unsigned int | |
592 | find (unsigned int node) | |
593 | { | |
594 | gcc_assert (node < graph->size); | |
595 | if (graph->rep[node] != node) | |
596 | return graph->rep[node] = find (graph->rep[node]); | |
597 | return node; | |
598 | } | |
599 | ||
600 | /* Union the TO and FROM nodes to the TO nodes. | |
601 | Note that at some point in the future, we may want to do | |
602 | union-by-rank, in which case we are going to have to return the | |
603 | node we unified to. */ | |
604 | ||
605 | static bool | |
606 | unite (unsigned int to, unsigned int from) | |
607 | { | |
608 | gcc_assert (to < graph->size && from < graph->size); | |
609 | if (to != from && graph->rep[from] != to) | |
610 | { | |
611 | graph->rep[from] = to; | |
612 | return true; | |
613 | } | |
614 | return false; | |
615 | } | |
616 | ||
910fdc79 DB |
617 | /* Create a new constraint consisting of LHS and RHS expressions. */ |
618 | ||
c58936b6 | 619 | static constraint_t |
910fdc79 DB |
620 | new_constraint (const struct constraint_expr lhs, |
621 | const struct constraint_expr rhs) | |
622 | { | |
c22940cd | 623 | constraint_t ret = (constraint_t) pool_alloc (constraint_pool); |
910fdc79 DB |
624 | ret->lhs = lhs; |
625 | ret->rhs = rhs; | |
626 | return ret; | |
627 | } | |
628 | ||
629 | /* Print out constraint C to FILE. */ | |
630 | ||
5006671f | 631 | static void |
910fdc79 DB |
632 | dump_constraint (FILE *file, constraint_t c) |
633 | { | |
634 | if (c->lhs.type == ADDRESSOF) | |
635 | fprintf (file, "&"); | |
636 | else if (c->lhs.type == DEREF) | |
c58936b6 | 637 | fprintf (file, "*"); |
5006671f RG |
638 | fprintf (file, "%s", get_varinfo (c->lhs.var)->name); |
639 | if (c->lhs.offset == UNKNOWN_OFFSET) | |
640 | fprintf (file, " + UNKNOWN"); | |
641 | else if (c->lhs.offset != 0) | |
910fdc79 DB |
642 | fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset); |
643 | fprintf (file, " = "); | |
644 | if (c->rhs.type == ADDRESSOF) | |
645 | fprintf (file, "&"); | |
646 | else if (c->rhs.type == DEREF) | |
647 | fprintf (file, "*"); | |
5006671f RG |
648 | fprintf (file, "%s", get_varinfo (c->rhs.var)->name); |
649 | if (c->rhs.offset == UNKNOWN_OFFSET) | |
650 | fprintf (file, " + UNKNOWN"); | |
651 | else if (c->rhs.offset != 0) | |
910fdc79 | 652 | fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset); |
910fdc79 DB |
653 | } |
654 | ||
5006671f RG |
655 | |
656 | void debug_constraint (constraint_t); | |
657 | void debug_constraints (void); | |
658 | void debug_constraint_graph (void); | |
659 | void debug_solution_for_var (unsigned int); | |
660 | void debug_sa_points_to_info (void); | |
661 | ||
910fdc79 DB |
662 | /* Print out constraint C to stderr. */ |
663 | ||
24e47c76 | 664 | DEBUG_FUNCTION void |
910fdc79 DB |
665 | debug_constraint (constraint_t c) |
666 | { | |
667 | dump_constraint (stderr, c); | |
8576f20a | 668 | fprintf (stderr, "\n"); |
910fdc79 DB |
669 | } |
670 | ||
671 | /* Print out all constraints to FILE */ | |
672 | ||
5006671f | 673 | static void |
25a6a873 | 674 | dump_constraints (FILE *file, int from) |
910fdc79 DB |
675 | { |
676 | int i; | |
677 | constraint_t c; | |
25a6a873 | 678 | for (i = from; VEC_iterate (constraint_t, constraints, i, c); i++) |
8576f20a RG |
679 | if (c) |
680 | { | |
681 | dump_constraint (file, c); | |
682 | fprintf (file, "\n"); | |
683 | } | |
910fdc79 DB |
684 | } |
685 | ||
686 | /* Print out all constraints to stderr. */ | |
687 | ||
24e47c76 | 688 | DEBUG_FUNCTION void |
910fdc79 DB |
689 | debug_constraints (void) |
690 | { | |
25a6a873 | 691 | dump_constraints (stderr, 0); |
910fdc79 DB |
692 | } |
693 | ||
fc93bcb6 FP |
694 | /* Print the constraint graph in dot format. */ |
695 | ||
5006671f | 696 | static void |
fc93bcb6 FP |
697 | dump_constraint_graph (FILE *file) |
698 | { | |
8576f20a | 699 | unsigned int i; |
fc93bcb6 FP |
700 | |
701 | /* Only print the graph if it has already been initialized: */ | |
702 | if (!graph) | |
703 | return; | |
704 | ||
fc93bcb6 | 705 | /* Prints the header of the dot file: */ |
fc93bcb6 FP |
706 | fprintf (file, "strict digraph {\n"); |
707 | fprintf (file, " node [\n shape = box\n ]\n"); | |
708 | fprintf (file, " edge [\n fontsize = \"12\"\n ]\n"); | |
8576f20a RG |
709 | fprintf (file, "\n // List of nodes and complex constraints in " |
710 | "the constraint graph:\n"); | |
711 | ||
712 | /* The next lines print the nodes in the graph together with the | |
713 | complex constraints attached to them. */ | |
714 | for (i = 0; i < graph->size; i++) | |
fc93bcb6 | 715 | { |
8576f20a RG |
716 | if (find (i) != i) |
717 | continue; | |
718 | if (i < FIRST_REF_NODE) | |
719 | fprintf (file, "\"%s\"", get_varinfo (i)->name); | |
720 | else | |
721 | fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name); | |
722 | if (graph->complex[i]) | |
723 | { | |
724 | unsigned j; | |
725 | constraint_t c; | |
726 | fprintf (file, " [label=\"\\N\\n"); | |
727 | for (j = 0; VEC_iterate (constraint_t, graph->complex[i], j, c); ++j) | |
728 | { | |
729 | dump_constraint (file, c); | |
730 | fprintf (file, "\\l"); | |
731 | } | |
732 | fprintf (file, "\"]"); | |
733 | } | |
734 | fprintf (file, ";\n"); | |
fc93bcb6 FP |
735 | } |
736 | ||
8576f20a RG |
737 | /* Go over the edges. */ |
738 | fprintf (file, "\n // Edges in the constraint graph:\n"); | |
739 | for (i = 0; i < graph->size; i++) | |
740 | { | |
741 | unsigned j; | |
742 | bitmap_iterator bi; | |
743 | if (find (i) != i) | |
744 | continue; | |
745 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi) | |
746 | { | |
747 | unsigned to = find (j); | |
748 | if (i == to) | |
749 | continue; | |
750 | if (i < FIRST_REF_NODE) | |
751 | fprintf (file, "\"%s\"", get_varinfo (i)->name); | |
752 | else | |
753 | fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name); | |
754 | fprintf (file, " -> "); | |
755 | if (to < FIRST_REF_NODE) | |
756 | fprintf (file, "\"%s\"", get_varinfo (to)->name); | |
757 | else | |
758 | fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name); | |
759 | fprintf (file, ";\n"); | |
760 | } | |
761 | } | |
fc93bcb6 | 762 | |
8576f20a RG |
763 | /* Prints the tail of the dot file. */ |
764 | fprintf (file, "}\n"); | |
fc93bcb6 FP |
765 | } |
766 | ||
767 | /* Print out the constraint graph to stderr. */ | |
768 | ||
24e47c76 | 769 | DEBUG_FUNCTION void |
fc93bcb6 FP |
770 | debug_constraint_graph (void) |
771 | { | |
772 | dump_constraint_graph (stderr); | |
773 | } | |
774 | ||
c58936b6 | 775 | /* SOLVER FUNCTIONS |
910fdc79 DB |
776 | |
777 | The solver is a simple worklist solver, that works on the following | |
778 | algorithm: | |
c58936b6 | 779 | |
3e5937d7 DB |
780 | sbitmap changed_nodes = all zeroes; |
781 | changed_count = 0; | |
782 | For each node that is not already collapsed: | |
783 | changed_count++; | |
784 | set bit in changed nodes | |
910fdc79 | 785 | |
910fdc79 DB |
786 | while (changed_count > 0) |
787 | { | |
788 | compute topological ordering for constraint graph | |
c58936b6 | 789 | |
910fdc79 DB |
790 | find and collapse cycles in the constraint graph (updating |
791 | changed if necessary) | |
c58936b6 | 792 | |
910fdc79 DB |
793 | for each node (n) in the graph in topological order: |
794 | changed_count--; | |
795 | ||
796 | Process each complex constraint associated with the node, | |
797 | updating changed if necessary. | |
798 | ||
799 | For each outgoing edge from n, propagate the solution from n to | |
800 | the destination of the edge, updating changed as necessary. | |
801 | ||
802 | } */ | |
803 | ||
804 | /* Return true if two constraint expressions A and B are equal. */ | |
805 | ||
806 | static bool | |
807 | constraint_expr_equal (struct constraint_expr a, struct constraint_expr b) | |
808 | { | |
4ee00913 | 809 | return a.type == b.type && a.var == b.var && a.offset == b.offset; |
910fdc79 DB |
810 | } |
811 | ||
812 | /* Return true if constraint expression A is less than constraint expression | |
813 | B. This is just arbitrary, but consistent, in order to give them an | |
814 | ordering. */ | |
815 | ||
816 | static bool | |
817 | constraint_expr_less (struct constraint_expr a, struct constraint_expr b) | |
818 | { | |
819 | if (a.type == b.type) | |
820 | { | |
821 | if (a.var == b.var) | |
822 | return a.offset < b.offset; | |
823 | else | |
824 | return a.var < b.var; | |
825 | } | |
826 | else | |
827 | return a.type < b.type; | |
828 | } | |
829 | ||
830 | /* Return true if constraint A is less than constraint B. This is just | |
831 | arbitrary, but consistent, in order to give them an ordering. */ | |
832 | ||
833 | static bool | |
834 | constraint_less (const constraint_t a, const constraint_t b) | |
835 | { | |
836 | if (constraint_expr_less (a->lhs, b->lhs)) | |
837 | return true; | |
838 | else if (constraint_expr_less (b->lhs, a->lhs)) | |
839 | return false; | |
840 | else | |
841 | return constraint_expr_less (a->rhs, b->rhs); | |
842 | } | |
843 | ||
844 | /* Return true if two constraints A and B are equal. */ | |
c58936b6 | 845 | |
910fdc79 DB |
846 | static bool |
847 | constraint_equal (struct constraint a, struct constraint b) | |
848 | { | |
c58936b6 | 849 | return constraint_expr_equal (a.lhs, b.lhs) |
910fdc79 DB |
850 | && constraint_expr_equal (a.rhs, b.rhs); |
851 | } | |
852 | ||
853 | ||
854 | /* Find a constraint LOOKFOR in the sorted constraint vector VEC */ | |
855 | ||
856 | static constraint_t | |
b5efa470 | 857 | constraint_vec_find (VEC(constraint_t,heap) *vec, |
910fdc79 DB |
858 | struct constraint lookfor) |
859 | { | |
c58936b6 | 860 | unsigned int place; |
910fdc79 DB |
861 | constraint_t found; |
862 | ||
863 | if (vec == NULL) | |
864 | return NULL; | |
865 | ||
866 | place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less); | |
867 | if (place >= VEC_length (constraint_t, vec)) | |
868 | return NULL; | |
869 | found = VEC_index (constraint_t, vec, place); | |
870 | if (!constraint_equal (*found, lookfor)) | |
871 | return NULL; | |
872 | return found; | |
873 | } | |
874 | ||
875 | /* Union two constraint vectors, TO and FROM. Put the result in TO. */ | |
876 | ||
877 | static void | |
b5efa470 DB |
878 | constraint_set_union (VEC(constraint_t,heap) **to, |
879 | VEC(constraint_t,heap) **from) | |
910fdc79 DB |
880 | { |
881 | int i; | |
882 | constraint_t c; | |
883 | ||
ac47786e | 884 | FOR_EACH_VEC_ELT (constraint_t, *from, i, c) |
910fdc79 DB |
885 | { |
886 | if (constraint_vec_find (*to, *c) == NULL) | |
887 | { | |
888 | unsigned int place = VEC_lower_bound (constraint_t, *to, c, | |
889 | constraint_less); | |
b5efa470 | 890 | VEC_safe_insert (constraint_t, heap, *to, place, c); |
910fdc79 DB |
891 | } |
892 | } | |
893 | } | |
894 | ||
5006671f RG |
895 | /* Expands the solution in SET to all sub-fields of variables included. |
896 | Union the expanded result into RESULT. */ | |
897 | ||
898 | static void | |
899 | solution_set_expand (bitmap result, bitmap set) | |
900 | { | |
901 | bitmap_iterator bi; | |
902 | bitmap vars = NULL; | |
903 | unsigned j; | |
904 | ||
905 | /* In a first pass record all variables we need to add all | |
906 | sub-fields off. This avoids quadratic behavior. */ | |
907 | EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi) | |
908 | { | |
909 | varinfo_t v = get_varinfo (j); | |
910 | if (v->is_artificial_var | |
911 | || v->is_full_var) | |
912 | continue; | |
913 | v = lookup_vi_for_tree (v->decl); | |
914 | if (vars == NULL) | |
915 | vars = BITMAP_ALLOC (NULL); | |
916 | bitmap_set_bit (vars, v->id); | |
917 | } | |
918 | ||
919 | /* In the second pass now do the addition to the solution and | |
920 | to speed up solving add it to the delta as well. */ | |
921 | if (vars != NULL) | |
922 | { | |
923 | EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi) | |
924 | { | |
925 | varinfo_t v = get_varinfo (j); | |
926 | for (; v != NULL; v = v->next) | |
927 | bitmap_set_bit (result, v->id); | |
928 | } | |
929 | BITMAP_FREE (vars); | |
930 | } | |
931 | } | |
932 | ||
910fdc79 DB |
933 | /* Take a solution set SET, add OFFSET to each member of the set, and |
934 | overwrite SET with the result when done. */ | |
935 | ||
936 | static void | |
5006671f | 937 | solution_set_add (bitmap set, HOST_WIDE_INT offset) |
910fdc79 DB |
938 | { |
939 | bitmap result = BITMAP_ALLOC (&iteration_obstack); | |
940 | unsigned int i; | |
941 | bitmap_iterator bi; | |
942 | ||
5006671f RG |
943 | /* If the offset is unknown we have to expand the solution to |
944 | all subfields. */ | |
945 | if (offset == UNKNOWN_OFFSET) | |
946 | { | |
947 | solution_set_expand (set, set); | |
948 | return; | |
949 | } | |
950 | ||
910fdc79 DB |
951 | EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi) |
952 | { | |
e5bae89b | 953 | varinfo_t vi = get_varinfo (i); |
c58936b6 | 954 | |
e5bae89b RG |
955 | /* If this is a variable with just one field just set its bit |
956 | in the result. */ | |
957 | if (vi->is_artificial_var | |
958 | || vi->is_unknown_size_var | |
959 | || vi->is_full_var) | |
960 | bitmap_set_bit (result, i); | |
961 | else | |
910fdc79 | 962 | { |
e5bae89b | 963 | unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset; |
5006671f RG |
964 | |
965 | /* If the offset makes the pointer point to before the | |
966 | variable use offset zero for the field lookup. */ | |
967 | if (offset < 0 | |
968 | && fieldoffset > vi->offset) | |
969 | fieldoffset = 0; | |
970 | ||
971 | if (offset != 0) | |
972 | vi = first_or_preceding_vi_for_offset (vi, fieldoffset); | |
973 | ||
974 | bitmap_set_bit (result, vi->id); | |
e5bae89b RG |
975 | /* If the result is not exactly at fieldoffset include the next |
976 | field as well. See get_constraint_for_ptr_offset for more | |
977 | rationale. */ | |
5006671f RG |
978 | if (vi->offset != fieldoffset |
979 | && vi->next != NULL) | |
980 | bitmap_set_bit (result, vi->next->id); | |
910fdc79 DB |
981 | } |
982 | } | |
c58936b6 DB |
983 | |
984 | bitmap_copy (set, result); | |
910fdc79 DB |
985 | BITMAP_FREE (result); |
986 | } | |
987 | ||
988 | /* Union solution sets TO and FROM, and add INC to each member of FROM in the | |
989 | process. */ | |
990 | ||
991 | static bool | |
5006671f | 992 | set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc) |
910fdc79 DB |
993 | { |
994 | if (inc == 0) | |
995 | return bitmap_ior_into (to, from); | |
996 | else | |
997 | { | |
998 | bitmap tmp; | |
999 | bool res; | |
1000 | ||
1001 | tmp = BITMAP_ALLOC (&iteration_obstack); | |
1002 | bitmap_copy (tmp, from); | |
1003 | solution_set_add (tmp, inc); | |
1004 | res = bitmap_ior_into (to, tmp); | |
1005 | BITMAP_FREE (tmp); | |
1006 | return res; | |
1007 | } | |
1008 | } | |
1009 | ||
3e5937d7 DB |
1010 | /* Insert constraint C into the list of complex constraints for graph |
1011 | node VAR. */ | |
910fdc79 DB |
1012 | |
1013 | static void | |
3e5937d7 DB |
1014 | insert_into_complex (constraint_graph_t graph, |
1015 | unsigned int var, constraint_t c) | |
910fdc79 | 1016 | { |
3e5937d7 DB |
1017 | VEC (constraint_t, heap) *complex = graph->complex[var]; |
1018 | unsigned int place = VEC_lower_bound (constraint_t, complex, c, | |
910fdc79 | 1019 | constraint_less); |
3e5937d7 DB |
1020 | |
1021 | /* Only insert constraints that do not already exist. */ | |
1022 | if (place >= VEC_length (constraint_t, complex) | |
1023 | || !constraint_equal (*c, *VEC_index (constraint_t, complex, place))) | |
1024 | VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c); | |
910fdc79 DB |
1025 | } |
1026 | ||
1027 | ||
910fdc79 DB |
1028 | /* Condense two variable nodes into a single variable node, by moving |
1029 | all associated info from SRC to TO. */ | |
1030 | ||
c58936b6 | 1031 | static void |
3e5937d7 DB |
1032 | merge_node_constraints (constraint_graph_t graph, unsigned int to, |
1033 | unsigned int from) | |
910fdc79 | 1034 | { |
910fdc79 DB |
1035 | unsigned int i; |
1036 | constraint_t c; | |
c58936b6 | 1037 | |
3e5937d7 | 1038 | gcc_assert (find (from) == to); |
c58936b6 | 1039 | |
910fdc79 | 1040 | /* Move all complex constraints from src node into to node */ |
ac47786e | 1041 | FOR_EACH_VEC_ELT (constraint_t, graph->complex[from], i, c) |
910fdc79 DB |
1042 | { |
1043 | /* In complex constraints for node src, we may have either | |
3e5937d7 DB |
1044 | a = *src, and *src = a, or an offseted constraint which are |
1045 | always added to the rhs node's constraints. */ | |
c58936b6 | 1046 | |
910fdc79 DB |
1047 | if (c->rhs.type == DEREF) |
1048 | c->rhs.var = to; | |
3e5937d7 | 1049 | else if (c->lhs.type == DEREF) |
910fdc79 | 1050 | c->lhs.var = to; |
3e5937d7 DB |
1051 | else |
1052 | c->rhs.var = to; | |
910fdc79 | 1053 | } |
3e5937d7 DB |
1054 | constraint_set_union (&graph->complex[to], &graph->complex[from]); |
1055 | VEC_free (constraint_t, heap, graph->complex[from]); | |
1056 | graph->complex[from] = NULL; | |
910fdc79 DB |
1057 | } |
1058 | ||
910fdc79 DB |
1059 | |
1060 | /* Remove edges involving NODE from GRAPH. */ | |
1061 | ||
1062 | static void | |
1063 | clear_edges_for_node (constraint_graph_t graph, unsigned int node) | |
1064 | { | |
57250223 | 1065 | if (graph->succs[node]) |
3e5937d7 | 1066 | BITMAP_FREE (graph->succs[node]); |
f71ef09d DB |
1067 | } |
1068 | ||
910fdc79 DB |
1069 | /* Merge GRAPH nodes FROM and TO into node TO. */ |
1070 | ||
1071 | static void | |
c58936b6 | 1072 | merge_graph_nodes (constraint_graph_t graph, unsigned int to, |
910fdc79 DB |
1073 | unsigned int from) |
1074 | { | |
3e5937d7 | 1075 | if (graph->indirect_cycles[from] != -1) |
4ee00913 | 1076 | { |
3e5937d7 DB |
1077 | /* If we have indirect cycles with the from node, and we have |
1078 | none on the to node, the to node has indirect cycles from the | |
1079 | from node now that they are unified. | |
1080 | If indirect cycles exist on both, unify the nodes that they | |
1081 | are in a cycle with, since we know they are in a cycle with | |
1082 | each other. */ | |
1083 | if (graph->indirect_cycles[to] == -1) | |
7b765bed | 1084 | graph->indirect_cycles[to] = graph->indirect_cycles[from]; |
4ee00913 | 1085 | } |
910fdc79 | 1086 | |
57250223 DB |
1087 | /* Merge all the successor edges. */ |
1088 | if (graph->succs[from]) | |
4ee00913 | 1089 | { |
57250223 | 1090 | if (!graph->succs[to]) |
3e5937d7 | 1091 | graph->succs[to] = BITMAP_ALLOC (&pta_obstack); |
c58936b6 | 1092 | bitmap_ior_into (graph->succs[to], |
57250223 | 1093 | graph->succs[from]); |
4ee00913 | 1094 | } |
4ee00913 | 1095 | |
910fdc79 DB |
1096 | clear_edges_for_node (graph, from); |
1097 | } | |
1098 | ||
3e5937d7 DB |
1099 | |
1100 | /* Add an indirect graph edge to GRAPH, going from TO to FROM if | |
1101 | it doesn't exist in the graph already. */ | |
1102 | ||
1103 | static void | |
1104 | add_implicit_graph_edge (constraint_graph_t graph, unsigned int to, | |
1105 | unsigned int from) | |
1106 | { | |
1107 | if (to == from) | |
1108 | return; | |
1109 | ||
1110 | if (!graph->implicit_preds[to]) | |
1111 | graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack); | |
1112 | ||
5f0d975b RG |
1113 | if (bitmap_set_bit (graph->implicit_preds[to], from)) |
1114 | stats.num_implicit_edges++; | |
3e5937d7 DB |
1115 | } |
1116 | ||
1117 | /* Add a predecessor graph edge to GRAPH, going from TO to FROM if | |
1118 | it doesn't exist in the graph already. | |
1119 | Return false if the edge already existed, true otherwise. */ | |
1120 | ||
1121 | static void | |
1122 | add_pred_graph_edge (constraint_graph_t graph, unsigned int to, | |
1123 | unsigned int from) | |
1124 | { | |
1125 | if (!graph->preds[to]) | |
1126 | graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack); | |
5f0d975b | 1127 | bitmap_set_bit (graph->preds[to], from); |
3e5937d7 DB |
1128 | } |
1129 | ||
1130 | /* Add a graph edge to GRAPH, going from FROM to TO if | |
910fdc79 DB |
1131 | it doesn't exist in the graph already. |
1132 | Return false if the edge already existed, true otherwise. */ | |
1133 | ||
1134 | static bool | |
57250223 DB |
1135 | add_graph_edge (constraint_graph_t graph, unsigned int to, |
1136 | unsigned int from) | |
910fdc79 | 1137 | { |
57250223 | 1138 | if (to == from) |
910fdc79 DB |
1139 | { |
1140 | return false; | |
1141 | } | |
1142 | else | |
1143 | { | |
4ee00913 | 1144 | bool r = false; |
c58936b6 | 1145 | |
57250223 | 1146 | if (!graph->succs[from]) |
3e5937d7 | 1147 | graph->succs[from] = BITMAP_ALLOC (&pta_obstack); |
5f0d975b | 1148 | if (bitmap_set_bit (graph->succs[from], to)) |
f71ef09d | 1149 | { |
57250223 | 1150 | r = true; |
3e5937d7 DB |
1151 | if (to < FIRST_REF_NODE && from < FIRST_REF_NODE) |
1152 | stats.num_edges++; | |
f71ef09d | 1153 | } |
910fdc79 DB |
1154 | return r; |
1155 | } | |
1156 | } | |
1157 | ||
1158 | ||
4ee00913 | 1159 | /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */ |
910fdc79 DB |
1160 | |
1161 | static bool | |
c58936b6 | 1162 | valid_graph_edge (constraint_graph_t graph, unsigned int src, |
4ee00913 | 1163 | unsigned int dest) |
910fdc79 | 1164 | { |
c58936b6 | 1165 | return (graph->succs[dest] |
57250223 | 1166 | && bitmap_bit_p (graph->succs[dest], src)); |
4ee00913 DB |
1167 | } |
1168 | ||
7b765bed DB |
1169 | /* Initialize the constraint graph structure to contain SIZE nodes. */ |
1170 | ||
1171 | static void | |
1172 | init_graph (unsigned int size) | |
1173 | { | |
1174 | unsigned int j; | |
1175 | ||
1176 | graph = XCNEW (struct constraint_graph); | |
1177 | graph->size = size; | |
1178 | graph->succs = XCNEWVEC (bitmap, graph->size); | |
1179 | graph->indirect_cycles = XNEWVEC (int, graph->size); | |
1180 | graph->rep = XNEWVEC (unsigned int, graph->size); | |
1181 | graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size); | |
aa46c8a3 | 1182 | graph->pe = XCNEWVEC (unsigned int, graph->size); |
7b765bed DB |
1183 | graph->pe_rep = XNEWVEC (int, graph->size); |
1184 | ||
1185 | for (j = 0; j < graph->size; j++) | |
1186 | { | |
1187 | graph->rep[j] = j; | |
7b765bed DB |
1188 | graph->pe_rep[j] = -1; |
1189 | graph->indirect_cycles[j] = -1; | |
1190 | } | |
1191 | } | |
1192 | ||
3e5937d7 | 1193 | /* Build the constraint graph, adding only predecessor edges right now. */ |
910fdc79 DB |
1194 | |
1195 | static void | |
3e5937d7 | 1196 | build_pred_graph (void) |
910fdc79 | 1197 | { |
3e5937d7 | 1198 | int i; |
910fdc79 | 1199 | constraint_t c; |
3e5937d7 | 1200 | unsigned int j; |
910fdc79 | 1201 | |
3e5937d7 DB |
1202 | graph->implicit_preds = XCNEWVEC (bitmap, graph->size); |
1203 | graph->preds = XCNEWVEC (bitmap, graph->size); | |
7b765bed DB |
1204 | graph->pointer_label = XCNEWVEC (unsigned int, graph->size); |
1205 | graph->loc_label = XCNEWVEC (unsigned int, graph->size); | |
1206 | graph->pointed_by = XCNEWVEC (bitmap, graph->size); | |
1207 | graph->points_to = XCNEWVEC (bitmap, graph->size); | |
3e5937d7 | 1208 | graph->eq_rep = XNEWVEC (int, graph->size); |
3e5937d7 | 1209 | graph->direct_nodes = sbitmap_alloc (graph->size); |
7b765bed | 1210 | graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack); |
3e5937d7 DB |
1211 | sbitmap_zero (graph->direct_nodes); |
1212 | ||
1213 | for (j = 0; j < FIRST_REF_NODE; j++) | |
1214 | { | |
1215 | if (!get_varinfo (j)->is_special_var) | |
1216 | SET_BIT (graph->direct_nodes, j); | |
1217 | } | |
1218 | ||
1219 | for (j = 0; j < graph->size; j++) | |
7b765bed | 1220 | graph->eq_rep[j] = -1; |
3e5937d7 DB |
1221 | |
1222 | for (j = 0; j < VEC_length (varinfo_t, varmap); j++) | |
1223 | graph->indirect_cycles[j] = -1; | |
e8ca4159 | 1224 | |
ac47786e | 1225 | FOR_EACH_VEC_ELT (constraint_t, constraints, i, c) |
910fdc79 DB |
1226 | { |
1227 | struct constraint_expr lhs = c->lhs; | |
1228 | struct constraint_expr rhs = c->rhs; | |
5006671f RG |
1229 | unsigned int lhsvar = lhs.var; |
1230 | unsigned int rhsvar = rhs.var; | |
03190594 | 1231 | |
910fdc79 DB |
1232 | if (lhs.type == DEREF) |
1233 | { | |
3e5937d7 DB |
1234 | /* *x = y. */ |
1235 | if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR) | |
1236 | add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar); | |
910fdc79 DB |
1237 | } |
1238 | else if (rhs.type == DEREF) | |
1239 | { | |
3e5937d7 DB |
1240 | /* x = *y */ |
1241 | if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR) | |
1242 | add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar); | |
1243 | else | |
1244 | RESET_BIT (graph->direct_nodes, lhsvar); | |
910fdc79 | 1245 | } |
3e5937d7 | 1246 | else if (rhs.type == ADDRESSOF) |
910fdc79 | 1247 | { |
10bd6c5c RG |
1248 | varinfo_t v; |
1249 | ||
910fdc79 | 1250 | /* x = &y */ |
7b765bed DB |
1251 | if (graph->points_to[lhsvar] == NULL) |
1252 | graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack); | |
1253 | bitmap_set_bit (graph->points_to[lhsvar], rhsvar); | |
1254 | ||
1255 | if (graph->pointed_by[rhsvar] == NULL) | |
1256 | graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack); | |
1257 | bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar); | |
1258 | ||
3e5937d7 DB |
1259 | /* Implicitly, *x = y */ |
1260 | add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar); | |
1261 | ||
10bd6c5c | 1262 | /* All related variables are no longer direct nodes. */ |
3e5937d7 | 1263 | RESET_BIT (graph->direct_nodes, rhsvar); |
5006671f RG |
1264 | v = get_varinfo (rhsvar); |
1265 | if (!v->is_full_var) | |
1266 | { | |
1267 | v = lookup_vi_for_tree (v->decl); | |
1268 | do | |
1269 | { | |
1270 | RESET_BIT (graph->direct_nodes, v->id); | |
1271 | v = v->next; | |
1272 | } | |
1273 | while (v != NULL); | |
1274 | } | |
7b765bed | 1275 | bitmap_set_bit (graph->address_taken, rhsvar); |
910fdc79 | 1276 | } |
3e5937d7 DB |
1277 | else if (lhsvar > anything_id |
1278 | && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0) | |
910fdc79 | 1279 | { |
3e5937d7 DB |
1280 | /* x = y */ |
1281 | add_pred_graph_edge (graph, lhsvar, rhsvar); | |
1282 | /* Implicitly, *x = *y */ | |
1283 | add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, | |
1284 | FIRST_REF_NODE + rhsvar); | |
1285 | } | |
1286 | else if (lhs.offset != 0 || rhs.offset != 0) | |
1287 | { | |
1288 | if (rhs.offset != 0) | |
1289 | RESET_BIT (graph->direct_nodes, lhs.var); | |
7b765bed | 1290 | else if (lhs.offset != 0) |
3e5937d7 DB |
1291 | RESET_BIT (graph->direct_nodes, rhs.var); |
1292 | } | |
1293 | } | |
1294 | } | |
1295 | ||
1296 | /* Build the constraint graph, adding successor edges. */ | |
1297 | ||
1298 | static void | |
1299 | build_succ_graph (void) | |
1300 | { | |
9e39dba6 | 1301 | unsigned i, t; |
3e5937d7 DB |
1302 | constraint_t c; |
1303 | ||
ac47786e | 1304 | FOR_EACH_VEC_ELT (constraint_t, constraints, i, c) |
3e5937d7 DB |
1305 | { |
1306 | struct constraint_expr lhs; | |
1307 | struct constraint_expr rhs; | |
1308 | unsigned int lhsvar; | |
1309 | unsigned int rhsvar; | |
1310 | ||
1311 | if (!c) | |
1312 | continue; | |
c58936b6 | 1313 | |
3e5937d7 DB |
1314 | lhs = c->lhs; |
1315 | rhs = c->rhs; | |
5006671f RG |
1316 | lhsvar = find (lhs.var); |
1317 | rhsvar = find (rhs.var); | |
3e5937d7 DB |
1318 | |
1319 | if (lhs.type == DEREF) | |
1320 | { | |
1321 | if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR) | |
1322 | add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar); | |
1323 | } | |
1324 | else if (rhs.type == DEREF) | |
1325 | { | |
1326 | if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR) | |
1327 | add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar); | |
1328 | } | |
1329 | else if (rhs.type == ADDRESSOF) | |
1330 | { | |
1331 | /* x = &y */ | |
5006671f | 1332 | gcc_assert (find (rhs.var) == rhs.var); |
3e5937d7 DB |
1333 | bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar); |
1334 | } | |
1335 | else if (lhsvar > anything_id | |
1336 | && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0) | |
1337 | { | |
1338 | add_graph_edge (graph, lhsvar, rhsvar); | |
910fdc79 DB |
1339 | } |
1340 | } | |
9e39dba6 | 1341 | |
de925a03 RG |
1342 | /* Add edges from STOREDANYTHING to all non-direct nodes that can |
1343 | receive pointers. */ | |
9e39dba6 RG |
1344 | t = find (storedanything_id); |
1345 | for (i = integer_id + 1; i < FIRST_REF_NODE; ++i) | |
1346 | { | |
de925a03 RG |
1347 | if (!TEST_BIT (graph->direct_nodes, i) |
1348 | && get_varinfo (i)->may_have_pointers) | |
9e39dba6 RG |
1349 | add_graph_edge (graph, find (i), t); |
1350 | } | |
379c6f48 RG |
1351 | |
1352 | /* Everything stored to ANYTHING also potentially escapes. */ | |
1353 | add_graph_edge (graph, find (escaped_id), t); | |
910fdc79 | 1354 | } |
e8ca4159 DN |
1355 | |
1356 | ||
910fdc79 | 1357 | /* Changed variables on the last iteration. */ |
648b5f85 | 1358 | static bitmap changed; |
910fdc79 | 1359 | |
910fdc79 DB |
1360 | /* Strongly Connected Component visitation info. */ |
1361 | ||
1362 | struct scc_info | |
1363 | { | |
1364 | sbitmap visited; | |
7b765bed | 1365 | sbitmap deleted; |
3e5937d7 DB |
1366 | unsigned int *dfs; |
1367 | unsigned int *node_mapping; | |
910fdc79 | 1368 | int current_index; |
740e80e8 | 1369 | VEC(unsigned,heap) *scc_stack; |
910fdc79 DB |
1370 | }; |
1371 | ||
1372 | ||
1373 | /* Recursive routine to find strongly connected components in GRAPH. | |
1374 | SI is the SCC info to store the information in, and N is the id of current | |
1375 | graph node we are processing. | |
c58936b6 | 1376 | |
910fdc79 | 1377 | This is Tarjan's strongly connected component finding algorithm, as |
c58936b6 | 1378 | modified by Nuutila to keep only non-root nodes on the stack. |
910fdc79 DB |
1379 | The algorithm can be found in "On finding the strongly connected |
1380 | connected components in a directed graph" by Esko Nuutila and Eljas | |
1381 | Soisalon-Soininen, in Information Processing Letters volume 49, | |
1382 | number 1, pages 9-14. */ | |
1383 | ||
1384 | static void | |
1385 | scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) | |
1386 | { | |
4ee00913 DB |
1387 | unsigned int i; |
1388 | bitmap_iterator bi; | |
3e5937d7 | 1389 | unsigned int my_dfs; |
910fdc79 | 1390 | |
910fdc79 | 1391 | SET_BIT (si->visited, n); |
3e5937d7 DB |
1392 | si->dfs[n] = si->current_index ++; |
1393 | my_dfs = si->dfs[n]; | |
c58936b6 | 1394 | |
910fdc79 | 1395 | /* Visit all the successors. */ |
57250223 | 1396 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi) |
910fdc79 | 1397 | { |
3e5937d7 DB |
1398 | unsigned int w; |
1399 | ||
1400 | if (i > LAST_REF_NODE) | |
1401 | break; | |
1402 | ||
1403 | w = find (i); | |
7b765bed | 1404 | if (TEST_BIT (si->deleted, w)) |
3e5937d7 DB |
1405 | continue; |
1406 | ||
4ee00913 DB |
1407 | if (!TEST_BIT (si->visited, w)) |
1408 | scc_visit (graph, si, w); | |
3e5937d7 DB |
1409 | { |
1410 | unsigned int t = find (w); | |
1411 | unsigned int nnode = find (n); | |
62e5bf5d | 1412 | gcc_assert (nnode == n); |
3e5937d7 DB |
1413 | |
1414 | if (si->dfs[t] < si->dfs[nnode]) | |
1415 | si->dfs[n] = si->dfs[t]; | |
1416 | } | |
910fdc79 | 1417 | } |
c58936b6 | 1418 | |
910fdc79 | 1419 | /* See if any components have been identified. */ |
3e5937d7 | 1420 | if (si->dfs[n] == my_dfs) |
910fdc79 | 1421 | { |
3e5937d7 DB |
1422 | if (VEC_length (unsigned, si->scc_stack) > 0 |
1423 | && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs) | |
910fdc79 | 1424 | { |
3e5937d7 | 1425 | bitmap scc = BITMAP_ALLOC (NULL); |
3e5937d7 DB |
1426 | unsigned int lowest_node; |
1427 | bitmap_iterator bi; | |
910fdc79 | 1428 | |
3e5937d7 | 1429 | bitmap_set_bit (scc, n); |
910fdc79 | 1430 | |
3e5937d7 DB |
1431 | while (VEC_length (unsigned, si->scc_stack) != 0 |
1432 | && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs) | |
1433 | { | |
1434 | unsigned int w = VEC_pop (unsigned, si->scc_stack); | |
910fdc79 | 1435 | |
3e5937d7 | 1436 | bitmap_set_bit (scc, w); |
3e5937d7 | 1437 | } |
4ee00913 | 1438 | |
3e5937d7 DB |
1439 | lowest_node = bitmap_first_set_bit (scc); |
1440 | gcc_assert (lowest_node < FIRST_REF_NODE); | |
7b765bed DB |
1441 | |
1442 | /* Collapse the SCC nodes into a single node, and mark the | |
1443 | indirect cycles. */ | |
3e5937d7 DB |
1444 | EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi) |
1445 | { | |
1446 | if (i < FIRST_REF_NODE) | |
1447 | { | |
3e5937d7 DB |
1448 | if (unite (lowest_node, i)) |
1449 | unify_nodes (graph, lowest_node, i, false); | |
1450 | } | |
1451 | else | |
1452 | { | |
1453 | unite (lowest_node, i); | |
1454 | graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node; | |
1455 | } | |
1456 | } | |
4ee00913 | 1457 | } |
7b765bed | 1458 | SET_BIT (si->deleted, n); |
910fdc79 | 1459 | } |
3e5937d7 DB |
1460 | else |
1461 | VEC_safe_push (unsigned, heap, si->scc_stack, n); | |
910fdc79 DB |
1462 | } |
1463 | ||
3e5937d7 DB |
1464 | /* Unify node FROM into node TO, updating the changed count if |
1465 | necessary when UPDATE_CHANGED is true. */ | |
910fdc79 DB |
1466 | |
1467 | static void | |
3e5937d7 DB |
1468 | unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from, |
1469 | bool update_changed) | |
910fdc79 | 1470 | { |
910fdc79 | 1471 | |
3e5937d7 DB |
1472 | gcc_assert (to != from && find (to) == to); |
1473 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1474 | fprintf (dump_file, "Unifying %s to %s\n", | |
1475 | get_varinfo (from)->name, | |
1476 | get_varinfo (to)->name); | |
910fdc79 | 1477 | |
3e5937d7 DB |
1478 | if (update_changed) |
1479 | stats.unified_vars_dynamic++; | |
1480 | else | |
1481 | stats.unified_vars_static++; | |
910fdc79 | 1482 | |
3e5937d7 DB |
1483 | merge_graph_nodes (graph, to, from); |
1484 | merge_node_constraints (graph, to, from); | |
c58936b6 | 1485 | |
7b765bed DB |
1486 | /* Mark TO as changed if FROM was changed. If TO was already marked |
1487 | as changed, decrease the changed count. */ | |
1488 | ||
648b5f85 RG |
1489 | if (update_changed |
1490 | && bitmap_bit_p (changed, from)) | |
910fdc79 | 1491 | { |
648b5f85 RG |
1492 | bitmap_clear_bit (changed, from); |
1493 | bitmap_set_bit (changed, to); | |
3e5937d7 | 1494 | } |
aa46c8a3 | 1495 | if (get_varinfo (from)->solution) |
3e5937d7 | 1496 | { |
aa46c8a3 DB |
1497 | /* If the solution changes because of the merging, we need to mark |
1498 | the variable as changed. */ | |
1499 | if (bitmap_ior_into (get_varinfo (to)->solution, | |
1500 | get_varinfo (from)->solution)) | |
910fdc79 | 1501 | { |
648b5f85 RG |
1502 | if (update_changed) |
1503 | bitmap_set_bit (changed, to); | |
aa46c8a3 | 1504 | } |
b8698a0f | 1505 | |
aa46c8a3 | 1506 | BITMAP_FREE (get_varinfo (from)->solution); |
74d8fa44 RG |
1507 | if (get_varinfo (from)->oldsolution) |
1508 | BITMAP_FREE (get_varinfo (from)->oldsolution); | |
b8698a0f | 1509 | |
74d8fa44 RG |
1510 | if (stats.iterations > 0 |
1511 | && get_varinfo (to)->oldsolution) | |
1512 | BITMAP_FREE (get_varinfo (to)->oldsolution); | |
3e5937d7 | 1513 | } |
3e5937d7 DB |
1514 | if (valid_graph_edge (graph, to, to)) |
1515 | { | |
1516 | if (graph->succs[to]) | |
1517 | bitmap_clear_bit (graph->succs[to], to); | |
910fdc79 | 1518 | } |
910fdc79 DB |
1519 | } |
1520 | ||
910fdc79 DB |
1521 | /* Information needed to compute the topological ordering of a graph. */ |
1522 | ||
1523 | struct topo_info | |
1524 | { | |
1525 | /* sbitmap of visited nodes. */ | |
1526 | sbitmap visited; | |
1527 | /* Array that stores the topological order of the graph, *in | |
1528 | reverse*. */ | |
740e80e8 | 1529 | VEC(unsigned,heap) *topo_order; |
910fdc79 DB |
1530 | }; |
1531 | ||
1532 | ||
1533 | /* Initialize and return a topological info structure. */ | |
1534 | ||
1535 | static struct topo_info * | |
1536 | init_topo_info (void) | |
1537 | { | |
7b765bed | 1538 | size_t size = graph->size; |
5ed6ace5 | 1539 | struct topo_info *ti = XNEW (struct topo_info); |
910fdc79 DB |
1540 | ti->visited = sbitmap_alloc (size); |
1541 | sbitmap_zero (ti->visited); | |
740e80e8 | 1542 | ti->topo_order = VEC_alloc (unsigned, heap, 1); |
910fdc79 DB |
1543 | return ti; |
1544 | } | |
1545 | ||
1546 | ||
1547 | /* Free the topological sort info pointed to by TI. */ | |
1548 | ||
1549 | static void | |
1550 | free_topo_info (struct topo_info *ti) | |
1551 | { | |
1552 | sbitmap_free (ti->visited); | |
740e80e8 | 1553 | VEC_free (unsigned, heap, ti->topo_order); |
910fdc79 DB |
1554 | free (ti); |
1555 | } | |
1556 | ||
1557 | /* Visit the graph in topological order, and store the order in the | |
1558 | topo_info structure. */ | |
1559 | ||
1560 | static void | |
1561 | topo_visit (constraint_graph_t graph, struct topo_info *ti, | |
1562 | unsigned int n) | |
1563 | { | |
4ee00913 | 1564 | bitmap_iterator bi; |
4ee00913 DB |
1565 | unsigned int j; |
1566 | ||
910fdc79 | 1567 | SET_BIT (ti->visited, n); |
4ee00913 | 1568 | |
3e5937d7 DB |
1569 | if (graph->succs[n]) |
1570 | EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi) | |
4ee00913 DB |
1571 | { |
1572 | if (!TEST_BIT (ti->visited, j)) | |
1573 | topo_visit (graph, ti, j); | |
1574 | } | |
3e5937d7 | 1575 | |
740e80e8 | 1576 | VEC_safe_push (unsigned, heap, ti->topo_order, n); |
910fdc79 DB |
1577 | } |
1578 | ||
5006671f RG |
1579 | /* Process a constraint C that represents x = *(y + off), using DELTA as the |
1580 | starting solution for y. */ | |
910fdc79 DB |
1581 | |
1582 | static void | |
1583 | do_sd_constraint (constraint_graph_t graph, constraint_t c, | |
1584 | bitmap delta) | |
1585 | { | |
7b765bed | 1586 | unsigned int lhs = c->lhs.var; |
910fdc79 DB |
1587 | bool flag = false; |
1588 | bitmap sol = get_varinfo (lhs)->solution; | |
1589 | unsigned int j; | |
1590 | bitmap_iterator bi; | |
5006671f | 1591 | HOST_WIDE_INT roffset = c->rhs.offset; |
4ee00913 | 1592 | |
5006671f RG |
1593 | /* Our IL does not allow this. */ |
1594 | gcc_assert (c->lhs.offset == 0); | |
0e1f4c6b | 1595 | |
5006671f RG |
1596 | /* If the solution of Y contains anything it is good enough to transfer |
1597 | this to the LHS. */ | |
14c28276 RG |
1598 | if (bitmap_bit_p (delta, anything_id)) |
1599 | { | |
1600 | flag |= bitmap_set_bit (sol, anything_id); | |
1601 | goto done; | |
1602 | } | |
1603 | ||
5006671f RG |
1604 | /* If we do not know at with offset the rhs is dereferenced compute |
1605 | the reachability set of DELTA, conservatively assuming it is | |
1606 | dereferenced at all valid offsets. */ | |
1607 | if (roffset == UNKNOWN_OFFSET) | |
1608 | { | |
1609 | solution_set_expand (delta, delta); | |
1610 | /* No further offset processing is necessary. */ | |
1611 | roffset = 0; | |
1612 | } | |
1613 | ||
c58936b6 | 1614 | /* For each variable j in delta (Sol(y)), add |
910fdc79 DB |
1615 | an edge in the graph from j to x, and union Sol(j) into Sol(x). */ |
1616 | EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi) | |
1617 | { | |
5006671f RG |
1618 | varinfo_t v = get_varinfo (j); |
1619 | HOST_WIDE_INT fieldoffset = v->offset + roffset; | |
1620 | unsigned int t; | |
1621 | ||
1622 | if (v->is_full_var) | |
1623 | fieldoffset = v->offset; | |
1624 | else if (roffset != 0) | |
1625 | v = first_vi_for_offset (v, fieldoffset); | |
1626 | /* If the access is outside of the variable we can ignore it. */ | |
1627 | if (!v) | |
1628 | continue; | |
910fdc79 | 1629 | |
5006671f RG |
1630 | do |
1631 | { | |
3e5937d7 | 1632 | t = find (v->id); |
4ee00913 DB |
1633 | |
1634 | /* Adding edges from the special vars is pointless. | |
1635 | They don't have sets that can change. */ | |
b7091901 | 1636 | if (get_varinfo (t)->is_special_var) |
4ee00913 | 1637 | flag |= bitmap_ior_into (sol, get_varinfo (t)->solution); |
b7091901 | 1638 | /* Merging the solution from ESCAPED needlessly increases |
472c7fbd | 1639 | the set. Use ESCAPED as representative instead. */ |
5006671f | 1640 | else if (v->id == escaped_id) |
6a66f28e | 1641 | flag |= bitmap_set_bit (sol, escaped_id); |
3c323b52 RG |
1642 | else if (v->may_have_pointers |
1643 | && add_graph_edge (graph, lhs, t)) | |
4ee00913 | 1644 | flag |= bitmap_ior_into (sol, get_varinfo (t)->solution); |
5006671f RG |
1645 | |
1646 | /* If the variable is not exactly at the requested offset | |
1647 | we have to include the next one. */ | |
1648 | if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset | |
1649 | || v->next == NULL) | |
1650 | break; | |
1651 | ||
1652 | v = v->next; | |
1653 | fieldoffset = v->offset; | |
910fdc79 | 1654 | } |
5006671f | 1655 | while (1); |
910fdc79 | 1656 | } |
4cf4d6a3 | 1657 | |
4ee00913 | 1658 | done: |
910fdc79 DB |
1659 | /* If the LHS solution changed, mark the var as changed. */ |
1660 | if (flag) | |
1661 | { | |
1662 | get_varinfo (lhs)->solution = sol; | |
648b5f85 | 1663 | bitmap_set_bit (changed, lhs); |
c58936b6 | 1664 | } |
910fdc79 DB |
1665 | } |
1666 | ||
5006671f RG |
1667 | /* Process a constraint C that represents *(x + off) = y using DELTA |
1668 | as the starting solution for x. */ | |
910fdc79 DB |
1669 | |
1670 | static void | |
57250223 | 1671 | do_ds_constraint (constraint_t c, bitmap delta) |
910fdc79 | 1672 | { |
7b765bed | 1673 | unsigned int rhs = c->rhs.var; |
910fdc79 DB |
1674 | bitmap sol = get_varinfo (rhs)->solution; |
1675 | unsigned int j; | |
1676 | bitmap_iterator bi; | |
5006671f | 1677 | HOST_WIDE_INT loff = c->lhs.offset; |
11152c95 | 1678 | bool escaped_p = false; |
910fdc79 | 1679 | |
9e39dba6 RG |
1680 | /* Our IL does not allow this. */ |
1681 | gcc_assert (c->rhs.offset == 0); | |
1682 | ||
1683 | /* If the solution of y contains ANYTHING simply use the ANYTHING | |
1684 | solution. This avoids needlessly increasing the points-to sets. */ | |
1685 | if (bitmap_bit_p (sol, anything_id)) | |
1686 | sol = get_varinfo (find (anything_id))->solution; | |
1687 | ||
1688 | /* If the solution for x contains ANYTHING we have to merge the | |
1689 | solution of y into all pointer variables which we do via | |
1690 | STOREDANYTHING. */ | |
1691 | if (bitmap_bit_p (delta, anything_id)) | |
1692 | { | |
1693 | unsigned t = find (storedanything_id); | |
1694 | if (add_graph_edge (graph, t, rhs)) | |
1695 | { | |
1696 | if (bitmap_ior_into (get_varinfo (t)->solution, sol)) | |
648b5f85 | 1697 | bitmap_set_bit (changed, t); |
9e39dba6 RG |
1698 | } |
1699 | return; | |
1700 | } | |
4ee00913 | 1701 | |
5006671f RG |
1702 | /* If we do not know at with offset the rhs is dereferenced compute |
1703 | the reachability set of DELTA, conservatively assuming it is | |
1704 | dereferenced at all valid offsets. */ | |
1705 | if (loff == UNKNOWN_OFFSET) | |
1706 | { | |
1707 | solution_set_expand (delta, delta); | |
1708 | loff = 0; | |
1709 | } | |
1710 | ||
910fdc79 DB |
1711 | /* For each member j of delta (Sol(x)), add an edge from y to j and |
1712 | union Sol(y) into Sol(j) */ | |
1713 | EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi) | |
1714 | { | |
5006671f RG |
1715 | varinfo_t v = get_varinfo (j); |
1716 | unsigned int t; | |
1717 | HOST_WIDE_INT fieldoffset = v->offset + loff; | |
c58936b6 | 1718 | |
5006671f RG |
1719 | if (v->is_full_var) |
1720 | fieldoffset = v->offset; | |
1721 | else if (loff != 0) | |
1722 | v = first_vi_for_offset (v, fieldoffset); | |
1723 | /* If the access is outside of the variable we can ignore it. */ | |
1724 | if (!v) | |
1725 | continue; | |
57250223 | 1726 | |
5006671f RG |
1727 | do |
1728 | { | |
9e39dba6 | 1729 | if (v->may_have_pointers) |
910fdc79 | 1730 | { |
11152c95 RG |
1731 | /* If v is a global variable then this is an escape point. */ |
1732 | if (v->is_global_var | |
1733 | && !escaped_p) | |
1734 | { | |
1735 | t = find (escaped_id); | |
1736 | if (add_graph_edge (graph, t, rhs) | |
648b5f85 RG |
1737 | && bitmap_ior_into (get_varinfo (t)->solution, sol)) |
1738 | bitmap_set_bit (changed, t); | |
11152c95 RG |
1739 | /* Enough to let rhs escape once. */ |
1740 | escaped_p = true; | |
1741 | } | |
1742 | ||
1743 | if (v->is_special_var) | |
1744 | break; | |
1745 | ||
9e39dba6 | 1746 | t = find (v->id); |
de70bb20 | 1747 | if (add_graph_edge (graph, t, rhs) |
648b5f85 RG |
1748 | && bitmap_ior_into (get_varinfo (t)->solution, sol)) |
1749 | bitmap_set_bit (changed, t); | |
de70bb20 | 1750 | } |
5006671f RG |
1751 | |
1752 | /* If the variable is not exactly at the requested offset | |
1753 | we have to include the next one. */ | |
1754 | if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset | |
1755 | || v->next == NULL) | |
1756 | break; | |
1757 | ||
1758 | v = v->next; | |
1759 | fieldoffset = v->offset; | |
57250223 | 1760 | } |
5006671f | 1761 | while (1); |
910fdc79 DB |
1762 | } |
1763 | } | |
1764 | ||
3e5937d7 DB |
1765 | /* Handle a non-simple (simple meaning requires no iteration), |
1766 | constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */ | |
c58936b6 | 1767 | |
910fdc79 DB |
1768 | static void |
1769 | do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta) | |
1770 | { | |
1771 | if (c->lhs.type == DEREF) | |
1772 | { | |
1773 | if (c->rhs.type == ADDRESSOF) | |
1774 | { | |
7b765bed | 1775 | gcc_unreachable(); |
910fdc79 DB |
1776 | } |
1777 | else | |
1778 | { | |
1779 | /* *x = y */ | |
57250223 | 1780 | do_ds_constraint (c, delta); |
910fdc79 DB |
1781 | } |
1782 | } | |
57250223 | 1783 | else if (c->rhs.type == DEREF) |
910fdc79 DB |
1784 | { |
1785 | /* x = *y */ | |
13c2c08b DB |
1786 | if (!(get_varinfo (c->lhs.var)->is_special_var)) |
1787 | do_sd_constraint (graph, c, delta); | |
910fdc79 | 1788 | } |
c58936b6 | 1789 | else |
57250223 | 1790 | { |
c58936b6 | 1791 | bitmap tmp; |
57250223 DB |
1792 | bitmap solution; |
1793 | bool flag = false; | |
57250223 | 1794 | |
62e5bf5d | 1795 | gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR); |
7b765bed DB |
1796 | solution = get_varinfo (c->rhs.var)->solution; |
1797 | tmp = get_varinfo (c->lhs.var)->solution; | |
57250223 DB |
1798 | |
1799 | flag = set_union_with_increment (tmp, solution, c->rhs.offset); | |
c58936b6 | 1800 | |
57250223 DB |
1801 | if (flag) |
1802 | { | |
7b765bed | 1803 | get_varinfo (c->lhs.var)->solution = tmp; |
648b5f85 | 1804 | bitmap_set_bit (changed, c->lhs.var); |
57250223 DB |
1805 | } |
1806 | } | |
910fdc79 DB |
1807 | } |
1808 | ||
1809 | /* Initialize and return a new SCC info structure. */ | |
1810 | ||
1811 | static struct scc_info * | |
3e5937d7 | 1812 | init_scc_info (size_t size) |
910fdc79 | 1813 | { |
5ed6ace5 | 1814 | struct scc_info *si = XNEW (struct scc_info); |
3e5937d7 | 1815 | size_t i; |
910fdc79 DB |
1816 | |
1817 | si->current_index = 0; | |
1818 | si->visited = sbitmap_alloc (size); | |
1819 | sbitmap_zero (si->visited); | |
7b765bed DB |
1820 | si->deleted = sbitmap_alloc (size); |
1821 | sbitmap_zero (si->deleted); | |
3e5937d7 DB |
1822 | si->node_mapping = XNEWVEC (unsigned int, size); |
1823 | si->dfs = XCNEWVEC (unsigned int, size); | |
1824 | ||
1825 | for (i = 0; i < size; i++) | |
1826 | si->node_mapping[i] = i; | |
1827 | ||
740e80e8 | 1828 | si->scc_stack = VEC_alloc (unsigned, heap, 1); |
910fdc79 DB |
1829 | return si; |
1830 | } | |
1831 | ||
1832 | /* Free an SCC info structure pointed to by SI */ | |
1833 | ||
1834 | static void | |
1835 | free_scc_info (struct scc_info *si) | |
c58936b6 | 1836 | { |
910fdc79 | 1837 | sbitmap_free (si->visited); |
7b765bed | 1838 | sbitmap_free (si->deleted); |
3e5937d7 DB |
1839 | free (si->node_mapping); |
1840 | free (si->dfs); | |
740e80e8 | 1841 | VEC_free (unsigned, heap, si->scc_stack); |
3e5937d7 | 1842 | free (si); |
910fdc79 DB |
1843 | } |
1844 | ||
1845 | ||
3e5937d7 DB |
1846 | /* Find indirect cycles in GRAPH that occur, using strongly connected |
1847 | components, and note them in the indirect cycles map. | |
1848 | ||
1849 | This technique comes from Ben Hardekopf and Calvin Lin, | |
1850 | "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of | |
1851 | Lines of Code", submitted to PLDI 2007. */ | |
910fdc79 DB |
1852 | |
1853 | static void | |
3e5937d7 | 1854 | find_indirect_cycles (constraint_graph_t graph) |
910fdc79 DB |
1855 | { |
1856 | unsigned int i; | |
3e5937d7 DB |
1857 | unsigned int size = graph->size; |
1858 | struct scc_info *si = init_scc_info (size); | |
910fdc79 | 1859 | |
3e5937d7 DB |
1860 | for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ ) |
1861 | if (!TEST_BIT (si->visited, i) && find (i) == i) | |
910fdc79 | 1862 | scc_visit (graph, si, i); |
c58936b6 | 1863 | |
910fdc79 DB |
1864 | free_scc_info (si); |
1865 | } | |
1866 | ||
1867 | /* Compute a topological ordering for GRAPH, and store the result in the | |
1868 | topo_info structure TI. */ | |
1869 | ||
c58936b6 | 1870 | static void |
910fdc79 DB |
1871 | compute_topo_order (constraint_graph_t graph, |
1872 | struct topo_info *ti) | |
1873 | { | |
1874 | unsigned int i; | |
7b765bed | 1875 | unsigned int size = graph->size; |
c58936b6 | 1876 | |
910fdc79 | 1877 | for (i = 0; i != size; ++i) |
3e5937d7 | 1878 | if (!TEST_BIT (ti->visited, i) && find (i) == i) |
910fdc79 DB |
1879 | topo_visit (graph, ti, i); |
1880 | } | |
1881 | ||
7b765bed DB |
1882 | /* Structure used to for hash value numbering of pointer equivalence |
1883 | classes. */ | |
1884 | ||
1885 | typedef struct equiv_class_label | |
1886 | { | |
3691626c | 1887 | hashval_t hashcode; |
7b765bed DB |
1888 | unsigned int equivalence_class; |
1889 | bitmap labels; | |
7b765bed | 1890 | } *equiv_class_label_t; |
586de218 | 1891 | typedef const struct equiv_class_label *const_equiv_class_label_t; |
7b765bed DB |
1892 | |
1893 | /* A hashtable for mapping a bitmap of labels->pointer equivalence | |
1894 | classes. */ | |
1895 | static htab_t pointer_equiv_class_table; | |
1896 | ||
1897 | /* A hashtable for mapping a bitmap of labels->location equivalence | |
1898 | classes. */ | |
1899 | static htab_t location_equiv_class_table; | |
1900 | ||
1901 | /* Hash function for a equiv_class_label_t */ | |
1902 | ||
1903 | static hashval_t | |
1904 | equiv_class_label_hash (const void *p) | |
1905 | { | |
586de218 | 1906 | const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p; |
7b765bed DB |
1907 | return ecl->hashcode; |
1908 | } | |
1909 | ||
1910 | /* Equality function for two equiv_class_label_t's. */ | |
1911 | ||
1912 | static int | |
1913 | equiv_class_label_eq (const void *p1, const void *p2) | |
1914 | { | |
586de218 KG |
1915 | const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1; |
1916 | const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2; | |
821bb7f8 RG |
1917 | return (eql1->hashcode == eql2->hashcode |
1918 | && bitmap_equal_p (eql1->labels, eql2->labels)); | |
7b765bed DB |
1919 | } |
1920 | ||
1921 | /* Lookup a equivalence class in TABLE by the bitmap of LABELS it | |
1922 | contains. */ | |
1923 | ||
1924 | static unsigned int | |
1925 | equiv_class_lookup (htab_t table, bitmap labels) | |
1926 | { | |
1927 | void **slot; | |
1928 | struct equiv_class_label ecl; | |
1929 | ||
1930 | ecl.labels = labels; | |
1931 | ecl.hashcode = bitmap_hash (labels); | |
c58936b6 | 1932 | |
7b765bed DB |
1933 | slot = htab_find_slot_with_hash (table, &ecl, |
1934 | ecl.hashcode, NO_INSERT); | |
1935 | if (!slot) | |
1936 | return 0; | |
1937 | else | |
1938 | return ((equiv_class_label_t) *slot)->equivalence_class; | |
1939 | } | |
1940 | ||
1941 | ||
1942 | /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS | |
1943 | to TABLE. */ | |
1944 | ||
1945 | static void | |
1946 | equiv_class_add (htab_t table, unsigned int equivalence_class, | |
1947 | bitmap labels) | |
1948 | { | |
1949 | void **slot; | |
1950 | equiv_class_label_t ecl = XNEW (struct equiv_class_label); | |
1951 | ||
1952 | ecl->labels = labels; | |
1953 | ecl->equivalence_class = equivalence_class; | |
1954 | ecl->hashcode = bitmap_hash (labels); | |
1955 | ||
1956 | slot = htab_find_slot_with_hash (table, ecl, | |
1957 | ecl->hashcode, INSERT); | |
1958 | gcc_assert (!*slot); | |
1959 | *slot = (void *) ecl; | |
1960 | } | |
1961 | ||
1962 | /* Perform offline variable substitution. | |
910fdc79 | 1963 | |
7b765bed DB |
1964 | This is a worst case quadratic time way of identifying variables |
1965 | that must have equivalent points-to sets, including those caused by | |
1966 | static cycles, and single entry subgraphs, in the constraint graph. | |
3e5937d7 | 1967 | |
7b765bed DB |
1968 | The technique is described in "Exploiting Pointer and Location |
1969 | Equivalence to Optimize Pointer Analysis. In the 14th International | |
1970 | Static Analysis Symposium (SAS), August 2007." It is known as the | |
1971 | "HU" algorithm, and is equivalent to value numbering the collapsed | |
1972 | constraint graph including evaluating unions. | |
3e5937d7 DB |
1973 | |
1974 | The general method of finding equivalence classes is as follows: | |
1975 | Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints. | |
7b765bed DB |
1976 | Initialize all non-REF nodes to be direct nodes. |
1977 | For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh | |
1978 | variable} | |
1979 | For each constraint containing the dereference, we also do the same | |
1980 | thing. | |
1981 | ||
1982 | We then compute SCC's in the graph and unify nodes in the same SCC, | |
1983 | including pts sets. | |
1984 | ||
1985 | For each non-collapsed node x: | |
1986 | Visit all unvisited explicit incoming edges. | |
1987 | Ignoring all non-pointers, set pts(x) = Union of pts(a) for y | |
1988 | where y->x. | |
1989 | Lookup the equivalence class for pts(x). | |
1990 | If we found one, equivalence_class(x) = found class. | |
1991 | Otherwise, equivalence_class(x) = new class, and new_class is | |
1992 | added to the lookup table. | |
3e5937d7 DB |
1993 | |
1994 | All direct nodes with the same equivalence class can be replaced | |
1995 | with a single representative node. | |
1996 | All unlabeled nodes (label == 0) are not pointers and all edges | |
1997 | involving them can be eliminated. | |
7b765bed DB |
1998 | We perform these optimizations during rewrite_constraints |
1999 | ||
2000 | In addition to pointer equivalence class finding, we also perform | |
2001 | location equivalence class finding. This is the set of variables | |
2002 | that always appear together in points-to sets. We use this to | |
2003 | compress the size of the points-to sets. */ | |
2004 | ||
2005 | /* Current maximum pointer equivalence class id. */ | |
2006 | static int pointer_equiv_class; | |
3e5937d7 | 2007 | |
7b765bed DB |
2008 | /* Current maximum location equivalence class id. */ |
2009 | static int location_equiv_class; | |
3e5937d7 DB |
2010 | |
2011 | /* Recursive routine to find strongly connected components in GRAPH, | |
7b765bed | 2012 | and label it's nodes with DFS numbers. */ |
910fdc79 DB |
2013 | |
2014 | static void | |
7b765bed | 2015 | condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) |
910fdc79 | 2016 | { |
3e5937d7 DB |
2017 | unsigned int i; |
2018 | bitmap_iterator bi; | |
2019 | unsigned int my_dfs; | |
c58936b6 | 2020 | |
3e5937d7 DB |
2021 | gcc_assert (si->node_mapping[n] == n); |
2022 | SET_BIT (si->visited, n); | |
2023 | si->dfs[n] = si->current_index ++; | |
2024 | my_dfs = si->dfs[n]; | |
c58936b6 | 2025 | |
3e5937d7 DB |
2026 | /* Visit all the successors. */ |
2027 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi) | |
910fdc79 | 2028 | { |
3e5937d7 | 2029 | unsigned int w = si->node_mapping[i]; |
910fdc79 | 2030 | |
7b765bed | 2031 | if (TEST_BIT (si->deleted, w)) |
910fdc79 DB |
2032 | continue; |
2033 | ||
3e5937d7 | 2034 | if (!TEST_BIT (si->visited, w)) |
7b765bed | 2035 | condense_visit (graph, si, w); |
3e5937d7 DB |
2036 | { |
2037 | unsigned int t = si->node_mapping[w]; | |
2038 | unsigned int nnode = si->node_mapping[n]; | |
62e5bf5d | 2039 | gcc_assert (nnode == n); |
910fdc79 | 2040 | |
3e5937d7 DB |
2041 | if (si->dfs[t] < si->dfs[nnode]) |
2042 | si->dfs[n] = si->dfs[t]; | |
2043 | } | |
2044 | } | |
910fdc79 | 2045 | |
3e5937d7 DB |
2046 | /* Visit all the implicit predecessors. */ |
2047 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi) | |
2048 | { | |
2049 | unsigned int w = si->node_mapping[i]; | |
2050 | ||
7b765bed | 2051 | if (TEST_BIT (si->deleted, w)) |
3e5937d7 DB |
2052 | continue; |
2053 | ||
2054 | if (!TEST_BIT (si->visited, w)) | |
7b765bed | 2055 | condense_visit (graph, si, w); |
3e5937d7 DB |
2056 | { |
2057 | unsigned int t = si->node_mapping[w]; | |
2058 | unsigned int nnode = si->node_mapping[n]; | |
2059 | gcc_assert (nnode == n); | |
2060 | ||
2061 | if (si->dfs[t] < si->dfs[nnode]) | |
2062 | si->dfs[n] = si->dfs[t]; | |
2063 | } | |
2064 | } | |
4ee00913 | 2065 | |
3e5937d7 DB |
2066 | /* See if any components have been identified. */ |
2067 | if (si->dfs[n] == my_dfs) | |
2068 | { | |
2069 | while (VEC_length (unsigned, si->scc_stack) != 0 | |
2070 | && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs) | |
910fdc79 | 2071 | { |
3e5937d7 DB |
2072 | unsigned int w = VEC_pop (unsigned, si->scc_stack); |
2073 | si->node_mapping[w] = n; | |
2074 | ||
2075 | if (!TEST_BIT (graph->direct_nodes, w)) | |
2076 | RESET_BIT (graph->direct_nodes, n); | |
3e5937d7 | 2077 | |
7b765bed DB |
2078 | /* Unify our nodes. */ |
2079 | if (graph->preds[w]) | |
2080 | { | |
2081 | if (!graph->preds[n]) | |
2082 | graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2083 | bitmap_ior_into (graph->preds[n], graph->preds[w]); | |
2084 | } | |
2085 | if (graph->implicit_preds[w]) | |
2086 | { | |
2087 | if (!graph->implicit_preds[n]) | |
2088 | graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2089 | bitmap_ior_into (graph->implicit_preds[n], | |
2090 | graph->implicit_preds[w]); | |
2091 | } | |
2092 | if (graph->points_to[w]) | |
2093 | { | |
2094 | if (!graph->points_to[n]) | |
2095 | graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2096 | bitmap_ior_into (graph->points_to[n], | |
2097 | graph->points_to[w]); | |
2098 | } | |
3e5937d7 | 2099 | } |
7b765bed | 2100 | SET_BIT (si->deleted, n); |
3e5937d7 DB |
2101 | } |
2102 | else | |
2103 | VEC_safe_push (unsigned, heap, si->scc_stack, n); | |
2104 | } | |
2105 | ||
7b765bed DB |
2106 | /* Label pointer equivalences. */ |
2107 | ||
2108 | static void | |
2109 | label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) | |
2110 | { | |
2111 | unsigned int i; | |
2112 | bitmap_iterator bi; | |
2113 | SET_BIT (si->visited, n); | |
2114 | ||
2115 | if (!graph->points_to[n]) | |
2116 | graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2117 | ||
2118 | /* Label and union our incoming edges's points to sets. */ | |
2119 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi) | |
2120 | { | |
2121 | unsigned int w = si->node_mapping[i]; | |
2122 | if (!TEST_BIT (si->visited, w)) | |
2123 | label_visit (graph, si, w); | |
2124 | ||
2125 | /* Skip unused edges */ | |
2126 | if (w == n || graph->pointer_label[w] == 0) | |
3dc21182 DB |
2127 | continue; |
2128 | ||
7b765bed DB |
2129 | if (graph->points_to[w]) |
2130 | bitmap_ior_into(graph->points_to[n], graph->points_to[w]); | |
7b765bed DB |
2131 | } |
2132 | /* Indirect nodes get fresh variables. */ | |
2133 | if (!TEST_BIT (graph->direct_nodes, n)) | |
2134 | bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n); | |
2135 | ||
2136 | if (!bitmap_empty_p (graph->points_to[n])) | |
2137 | { | |
2138 | unsigned int label = equiv_class_lookup (pointer_equiv_class_table, | |
2139 | graph->points_to[n]); | |
2140 | if (!label) | |
2141 | { | |
7b765bed DB |
2142 | label = pointer_equiv_class++; |
2143 | equiv_class_add (pointer_equiv_class_table, | |
2144 | label, graph->points_to[n]); | |
2145 | } | |
2146 | graph->pointer_label[n] = label; | |
2147 | } | |
2148 | } | |
2149 | ||
3e5937d7 DB |
2150 | /* Perform offline variable substitution, discovering equivalence |
2151 | classes, and eliminating non-pointer variables. */ | |
2152 | ||
2153 | static struct scc_info * | |
2154 | perform_var_substitution (constraint_graph_t graph) | |
2155 | { | |
2156 | unsigned int i; | |
2157 | unsigned int size = graph->size; | |
2158 | struct scc_info *si = init_scc_info (size); | |
2159 | ||
2160 | bitmap_obstack_initialize (&iteration_obstack); | |
7b765bed DB |
2161 | pointer_equiv_class_table = htab_create (511, equiv_class_label_hash, |
2162 | equiv_class_label_eq, free); | |
2163 | location_equiv_class_table = htab_create (511, equiv_class_label_hash, | |
2164 | equiv_class_label_eq, free); | |
2165 | pointer_equiv_class = 1; | |
2166 | location_equiv_class = 1; | |
2167 | ||
2168 | /* Condense the nodes, which means to find SCC's, count incoming | |
2169 | predecessors, and unite nodes in SCC's. */ | |
aa46c8a3 | 2170 | for (i = 0; i < FIRST_REF_NODE; i++) |
7b765bed DB |
2171 | if (!TEST_BIT (si->visited, si->node_mapping[i])) |
2172 | condense_visit (graph, si, si->node_mapping[i]); | |
3e5937d7 | 2173 | |
7b765bed DB |
2174 | sbitmap_zero (si->visited); |
2175 | /* Actually the label the nodes for pointer equivalences */ | |
aa46c8a3 | 2176 | for (i = 0; i < FIRST_REF_NODE; i++) |
3e5937d7 DB |
2177 | if (!TEST_BIT (si->visited, si->node_mapping[i])) |
2178 | label_visit (graph, si, si->node_mapping[i]); | |
2179 | ||
7b765bed DB |
2180 | /* Calculate location equivalence labels. */ |
2181 | for (i = 0; i < FIRST_REF_NODE; i++) | |
2182 | { | |
2183 | bitmap pointed_by; | |
2184 | bitmap_iterator bi; | |
2185 | unsigned int j; | |
2186 | unsigned int label; | |
2187 | ||
2188 | if (!graph->pointed_by[i]) | |
2189 | continue; | |
2190 | pointed_by = BITMAP_ALLOC (&iteration_obstack); | |
2191 | ||
2192 | /* Translate the pointed-by mapping for pointer equivalence | |
2193 | labels. */ | |
2194 | EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi) | |
2195 | { | |
2196 | bitmap_set_bit (pointed_by, | |
2197 | graph->pointer_label[si->node_mapping[j]]); | |
2198 | } | |
2199 | /* The original pointed_by is now dead. */ | |
2200 | BITMAP_FREE (graph->pointed_by[i]); | |
2201 | ||
2202 | /* Look up the location equivalence label if one exists, or make | |
2203 | one otherwise. */ | |
2204 | label = equiv_class_lookup (location_equiv_class_table, | |
2205 | pointed_by); | |
2206 | if (label == 0) | |
2207 | { | |
2208 | label = location_equiv_class++; | |
2209 | equiv_class_add (location_equiv_class_table, | |
2210 | label, pointed_by); | |
2211 | } | |
2212 | else | |
2213 | { | |
2214 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2215 | fprintf (dump_file, "Found location equivalence for node %s\n", | |
2216 | get_varinfo (i)->name); | |
2217 | BITMAP_FREE (pointed_by); | |
2218 | } | |
2219 | graph->loc_label[i] = label; | |
2220 | ||
2221 | } | |
2222 | ||
3e5937d7 DB |
2223 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2224 | for (i = 0; i < FIRST_REF_NODE; i++) | |
2225 | { | |
2226 | bool direct_node = TEST_BIT (graph->direct_nodes, i); | |
2227 | fprintf (dump_file, | |
7b765bed DB |
2228 | "Equivalence classes for %s node id %d:%s are pointer: %d" |
2229 | ", location:%d\n", | |
3e5937d7 | 2230 | direct_node ? "Direct node" : "Indirect node", i, |
62e5bf5d | 2231 | get_varinfo (i)->name, |
7b765bed DB |
2232 | graph->pointer_label[si->node_mapping[i]], |
2233 | graph->loc_label[si->node_mapping[i]]); | |
3e5937d7 DB |
2234 | } |
2235 | ||
2236 | /* Quickly eliminate our non-pointer variables. */ | |
2237 | ||
2238 | for (i = 0; i < FIRST_REF_NODE; i++) | |
2239 | { | |
2240 | unsigned int node = si->node_mapping[i]; | |
2241 | ||
aa46c8a3 | 2242 | if (graph->pointer_label[node] == 0) |
3e5937d7 | 2243 | { |
23e73993 | 2244 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3e5937d7 DB |
2245 | fprintf (dump_file, |
2246 | "%s is a non-pointer variable, eliminating edges.\n", | |
2247 | get_varinfo (node)->name); | |
2248 | stats.nonpointer_vars++; | |
2249 | clear_edges_for_node (graph, node); | |
910fdc79 DB |
2250 | } |
2251 | } | |
7b765bed | 2252 | |
3e5937d7 DB |
2253 | return si; |
2254 | } | |
2255 | ||
2256 | /* Free information that was only necessary for variable | |
2257 | substitution. */ | |
910fdc79 | 2258 | |
3e5937d7 DB |
2259 | static void |
2260 | free_var_substitution_info (struct scc_info *si) | |
2261 | { | |
2262 | free_scc_info (si); | |
7b765bed DB |
2263 | free (graph->pointer_label); |
2264 | free (graph->loc_label); | |
2265 | free (graph->pointed_by); | |
2266 | free (graph->points_to); | |
3e5937d7 DB |
2267 | free (graph->eq_rep); |
2268 | sbitmap_free (graph->direct_nodes); | |
7b765bed DB |
2269 | htab_delete (pointer_equiv_class_table); |
2270 | htab_delete (location_equiv_class_table); | |
4ee00913 | 2271 | bitmap_obstack_release (&iteration_obstack); |
3e5937d7 DB |
2272 | } |
2273 | ||
2274 | /* Return an existing node that is equivalent to NODE, which has | |
2275 | equivalence class LABEL, if one exists. Return NODE otherwise. */ | |
2276 | ||
2277 | static unsigned int | |
2278 | find_equivalent_node (constraint_graph_t graph, | |
2279 | unsigned int node, unsigned int label) | |
2280 | { | |
2281 | /* If the address version of this variable is unused, we can | |
2282 | substitute it for anything else with the same label. | |
2283 | Otherwise, we know the pointers are equivalent, but not the | |
7b765bed | 2284 | locations, and we can unite them later. */ |
3e5937d7 | 2285 | |
7b765bed | 2286 | if (!bitmap_bit_p (graph->address_taken, node)) |
3e5937d7 DB |
2287 | { |
2288 | gcc_assert (label < graph->size); | |
2289 | ||
2290 | if (graph->eq_rep[label] != -1) | |
2291 | { | |
2292 | /* Unify the two variables since we know they are equivalent. */ | |
2293 | if (unite (graph->eq_rep[label], node)) | |
2294 | unify_nodes (graph, graph->eq_rep[label], node, false); | |
2295 | return graph->eq_rep[label]; | |
2296 | } | |
2297 | else | |
2298 | { | |
2299 | graph->eq_rep[label] = node; | |
7b765bed | 2300 | graph->pe_rep[label] = node; |
3e5937d7 DB |
2301 | } |
2302 | } | |
7b765bed DB |
2303 | else |
2304 | { | |
2305 | gcc_assert (label < graph->size); | |
2306 | graph->pe[node] = label; | |
2307 | if (graph->pe_rep[label] == -1) | |
2308 | graph->pe_rep[label] = node; | |
2309 | } | |
2310 | ||
3e5937d7 DB |
2311 | return node; |
2312 | } | |
2313 | ||
7b765bed DB |
2314 | /* Unite pointer equivalent but not location equivalent nodes in |
2315 | GRAPH. This may only be performed once variable substitution is | |
2316 | finished. */ | |
2317 | ||
2318 | static void | |
2319 | unite_pointer_equivalences (constraint_graph_t graph) | |
2320 | { | |
2321 | unsigned int i; | |
2322 | ||
2323 | /* Go through the pointer equivalences and unite them to their | |
2324 | representative, if they aren't already. */ | |
aa46c8a3 | 2325 | for (i = 0; i < FIRST_REF_NODE; i++) |
7b765bed DB |
2326 | { |
2327 | unsigned int label = graph->pe[i]; | |
aa46c8a3 DB |
2328 | if (label) |
2329 | { | |
2330 | int label_rep = graph->pe_rep[label]; | |
b8698a0f | 2331 | |
aa46c8a3 DB |
2332 | if (label_rep == -1) |
2333 | continue; | |
b8698a0f | 2334 | |
aa46c8a3 DB |
2335 | label_rep = find (label_rep); |
2336 | if (label_rep >= 0 && unite (label_rep, find (i))) | |
2337 | unify_nodes (graph, label_rep, i, false); | |
2338 | } | |
7b765bed DB |
2339 | } |
2340 | } | |
2341 | ||
2342 | /* Move complex constraints to the GRAPH nodes they belong to. */ | |
3e5937d7 DB |
2343 | |
2344 | static void | |
7b765bed DB |
2345 | move_complex_constraints (constraint_graph_t graph) |
2346 | { | |
2347 | int i; | |
2348 | constraint_t c; | |
2349 | ||
ac47786e | 2350 | FOR_EACH_VEC_ELT (constraint_t, constraints, i, c) |
7b765bed DB |
2351 | { |
2352 | if (c) | |
2353 | { | |
2354 | struct constraint_expr lhs = c->lhs; | |
2355 | struct constraint_expr rhs = c->rhs; | |
2356 | ||
2357 | if (lhs.type == DEREF) | |
2358 | { | |
2359 | insert_into_complex (graph, lhs.var, c); | |
2360 | } | |
2361 | else if (rhs.type == DEREF) | |
2362 | { | |
2363 | if (!(get_varinfo (lhs.var)->is_special_var)) | |
2364 | insert_into_complex (graph, rhs.var, c); | |
2365 | } | |
2366 | else if (rhs.type != ADDRESSOF && lhs.var > anything_id | |
2367 | && (lhs.offset != 0 || rhs.offset != 0)) | |
2368 | { | |
2369 | insert_into_complex (graph, rhs.var, c); | |
2370 | } | |
2371 | } | |
2372 | } | |
2373 | } | |
2374 | ||
2375 | ||
2376 | /* Optimize and rewrite complex constraints while performing | |
2377 | collapsing of equivalent nodes. SI is the SCC_INFO that is the | |
2378 | result of perform_variable_substitution. */ | |
2379 | ||
2380 | static void | |
2381 | rewrite_constraints (constraint_graph_t graph, | |
2382 | struct scc_info *si) | |
3e5937d7 DB |
2383 | { |
2384 | int i; | |
2385 | unsigned int j; | |
2386 | constraint_t c; | |
2387 | ||
2388 | for (j = 0; j < graph->size; j++) | |
2389 | gcc_assert (find (j) == j); | |
2390 | ||
ac47786e | 2391 | FOR_EACH_VEC_ELT (constraint_t, constraints, i, c) |
3e5937d7 DB |
2392 | { |
2393 | struct constraint_expr lhs = c->lhs; | |
2394 | struct constraint_expr rhs = c->rhs; | |
5006671f RG |
2395 | unsigned int lhsvar = find (lhs.var); |
2396 | unsigned int rhsvar = find (rhs.var); | |
3e5937d7 DB |
2397 | unsigned int lhsnode, rhsnode; |
2398 | unsigned int lhslabel, rhslabel; | |
2399 | ||
2400 | lhsnode = si->node_mapping[lhsvar]; | |
2401 | rhsnode = si->node_mapping[rhsvar]; | |
7b765bed DB |
2402 | lhslabel = graph->pointer_label[lhsnode]; |
2403 | rhslabel = graph->pointer_label[rhsnode]; | |
3e5937d7 DB |
2404 | |
2405 | /* See if it is really a non-pointer variable, and if so, ignore | |
2406 | the constraint. */ | |
2407 | if (lhslabel == 0) | |
2408 | { | |
aa46c8a3 | 2409 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3e5937d7 | 2410 | { |
b8698a0f | 2411 | |
aa46c8a3 DB |
2412 | fprintf (dump_file, "%s is a non-pointer variable," |
2413 | "ignoring constraint:", | |
2414 | get_varinfo (lhs.var)->name); | |
2415 | dump_constraint (dump_file, c); | |
8576f20a | 2416 | fprintf (dump_file, "\n"); |
3e5937d7 | 2417 | } |
aa46c8a3 DB |
2418 | VEC_replace (constraint_t, constraints, i, NULL); |
2419 | continue; | |
3e5937d7 DB |
2420 | } |
2421 | ||
2422 | if (rhslabel == 0) | |
2423 | { | |
aa46c8a3 | 2424 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3e5937d7 | 2425 | { |
b8698a0f | 2426 | |
aa46c8a3 DB |
2427 | fprintf (dump_file, "%s is a non-pointer variable," |
2428 | "ignoring constraint:", | |
2429 | get_varinfo (rhs.var)->name); | |
2430 | dump_constraint (dump_file, c); | |
8576f20a | 2431 | fprintf (dump_file, "\n"); |
3e5937d7 | 2432 | } |
aa46c8a3 DB |
2433 | VEC_replace (constraint_t, constraints, i, NULL); |
2434 | continue; | |
3e5937d7 DB |
2435 | } |
2436 | ||
2437 | lhsvar = find_equivalent_node (graph, lhsvar, lhslabel); | |
2438 | rhsvar = find_equivalent_node (graph, rhsvar, rhslabel); | |
2439 | c->lhs.var = lhsvar; | |
2440 | c->rhs.var = rhsvar; | |
2441 | ||
3e5937d7 DB |
2442 | } |
2443 | } | |
2444 | ||
2445 | /* Eliminate indirect cycles involving NODE. Return true if NODE was | |
2446 | part of an SCC, false otherwise. */ | |
2447 | ||
2448 | static bool | |
2449 | eliminate_indirect_cycles (unsigned int node) | |
2450 | { | |
2451 | if (graph->indirect_cycles[node] != -1 | |
2452 | && !bitmap_empty_p (get_varinfo (node)->solution)) | |
2453 | { | |
2454 | unsigned int i; | |
2455 | VEC(unsigned,heap) *queue = NULL; | |
2456 | int queuepos; | |
2457 | unsigned int to = find (graph->indirect_cycles[node]); | |
2458 | bitmap_iterator bi; | |
2459 | ||
2460 | /* We can't touch the solution set and call unify_nodes | |
2461 | at the same time, because unify_nodes is going to do | |
2462 | bitmap unions into it. */ | |
2463 | ||
2464 | EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi) | |
2465 | { | |
2466 | if (find (i) == i && i != to) | |
2467 | { | |
2468 | if (unite (to, i)) | |
2469 | VEC_safe_push (unsigned, heap, queue, i); | |
2470 | } | |
2471 | } | |
2472 | ||
2473 | for (queuepos = 0; | |
2474 | VEC_iterate (unsigned, queue, queuepos, i); | |
2475 | queuepos++) | |
2476 | { | |
2477 | unify_nodes (graph, to, i, true); | |
2478 | } | |
2479 | VEC_free (unsigned, heap, queue); | |
2480 | return true; | |
2481 | } | |
2482 | return false; | |
910fdc79 DB |
2483 | } |
2484 | ||
910fdc79 DB |
2485 | /* Solve the constraint graph GRAPH using our worklist solver. |
2486 | This is based on the PW* family of solvers from the "Efficient Field | |
2487 | Sensitive Pointer Analysis for C" paper. | |
2488 | It works by iterating over all the graph nodes, processing the complex | |
2489 | constraints and propagating the copy constraints, until everything stops | |
2490 | changed. This corresponds to steps 6-8 in the solving list given above. */ | |
2491 | ||
2492 | static void | |
2493 | solve_graph (constraint_graph_t graph) | |
2494 | { | |
7b765bed | 2495 | unsigned int size = graph->size; |
910fdc79 | 2496 | unsigned int i; |
3e5937d7 | 2497 | bitmap pts; |
910fdc79 | 2498 | |
648b5f85 | 2499 | changed = BITMAP_ALLOC (NULL); |
c58936b6 | 2500 | |
3e5937d7 | 2501 | /* Mark all initial non-collapsed nodes as changed. */ |
910fdc79 | 2502 | for (i = 0; i < size; i++) |
3e5937d7 DB |
2503 | { |
2504 | varinfo_t ivi = get_varinfo (i); | |
2505 | if (find (i) == i && !bitmap_empty_p (ivi->solution) | |
2506 | && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i])) | |
2507 | || VEC_length (constraint_t, graph->complex[i]) > 0)) | |
648b5f85 | 2508 | bitmap_set_bit (changed, i); |
3e5937d7 DB |
2509 | } |
2510 | ||
2511 | /* Allocate a bitmap to be used to store the changed bits. */ | |
2512 | pts = BITMAP_ALLOC (&pta_obstack); | |
c58936b6 | 2513 | |
648b5f85 | 2514 | while (!bitmap_empty_p (changed)) |
910fdc79 DB |
2515 | { |
2516 | unsigned int i; | |
2517 | struct topo_info *ti = init_topo_info (); | |
2518 | stats.iterations++; | |
4ee00913 | 2519 | |
910fdc79 | 2520 | bitmap_obstack_initialize (&iteration_obstack); |
c58936b6 | 2521 | |
910fdc79 DB |
2522 | compute_topo_order (graph, ti); |
2523 | ||
740e80e8 | 2524 | while (VEC_length (unsigned, ti->topo_order) != 0) |
910fdc79 | 2525 | { |
3e5937d7 | 2526 | |
740e80e8 | 2527 | i = VEC_pop (unsigned, ti->topo_order); |
3e5937d7 DB |
2528 | |
2529 | /* If this variable is not a representative, skip it. */ | |
2530 | if (find (i) != i) | |
2531 | continue; | |
2532 | ||
d3c36974 DB |
2533 | /* In certain indirect cycle cases, we may merge this |
2534 | variable to another. */ | |
62e5bf5d | 2535 | if (eliminate_indirect_cycles (i) && find (i) != i) |
d3c36974 | 2536 | continue; |
910fdc79 DB |
2537 | |
2538 | /* If the node has changed, we need to process the | |
2539 | complex constraints and outgoing edges again. */ | |
648b5f85 | 2540 | if (bitmap_clear_bit (changed, i)) |
910fdc79 DB |
2541 | { |
2542 | unsigned int j; | |
2543 | constraint_t c; | |
910fdc79 | 2544 | bitmap solution; |
3e5937d7 | 2545 | VEC(constraint_t,heap) *complex = graph->complex[i]; |
74d8fa44 | 2546 | varinfo_t vi = get_varinfo (i); |
21392f19 | 2547 | bool solution_empty; |
48e540b0 | 2548 | |
3e5937d7 | 2549 | /* Compute the changed set of solution bits. */ |
74d8fa44 RG |
2550 | if (vi->oldsolution) |
2551 | bitmap_and_compl (pts, vi->solution, vi->oldsolution); | |
2552 | else | |
2553 | bitmap_copy (pts, vi->solution); | |
3e5937d7 DB |
2554 | |
2555 | if (bitmap_empty_p (pts)) | |
2556 | continue; | |
2557 | ||
74d8fa44 RG |
2558 | if (vi->oldsolution) |
2559 | bitmap_ior_into (vi->oldsolution, pts); | |
2560 | else | |
2561 | { | |
2562 | vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack); | |
2563 | bitmap_copy (vi->oldsolution, pts); | |
2564 | } | |
3e5937d7 | 2565 | |
74d8fa44 | 2566 | solution = vi->solution; |
21392f19 DB |
2567 | solution_empty = bitmap_empty_p (solution); |
2568 | ||
2569 | /* Process the complex constraints */ | |
ac47786e | 2570 | FOR_EACH_VEC_ELT (constraint_t, complex, j, c) |
21392f19 | 2571 | { |
7b765bed DB |
2572 | /* XXX: This is going to unsort the constraints in |
2573 | some cases, which will occasionally add duplicate | |
2574 | constraints during unification. This does not | |
2575 | affect correctness. */ | |
2576 | c->lhs.var = find (c->lhs.var); | |
2577 | c->rhs.var = find (c->rhs.var); | |
2578 | ||
21392f19 DB |
2579 | /* The only complex constraint that can change our |
2580 | solution to non-empty, given an empty solution, | |
2581 | is a constraint where the lhs side is receiving | |
2582 | some set from elsewhere. */ | |
2583 | if (!solution_empty || c->lhs.type != DEREF) | |
3e5937d7 | 2584 | do_complex_constraint (graph, c, pts); |
21392f19 | 2585 | } |
910fdc79 | 2586 | |
21392f19 DB |
2587 | solution_empty = bitmap_empty_p (solution); |
2588 | ||
5006671f | 2589 | if (!solution_empty) |
4ee00913 | 2590 | { |
3e5937d7 | 2591 | bitmap_iterator bi; |
5006671f | 2592 | unsigned eff_escaped_id = find (escaped_id); |
3e5937d7 | 2593 | |
21392f19 | 2594 | /* Propagate solution to all successors. */ |
c58936b6 | 2595 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], |
21392f19 | 2596 | 0, j, bi) |
4ee00913 | 2597 | { |
3e5937d7 DB |
2598 | bitmap tmp; |
2599 | bool flag; | |
2600 | ||
2601 | unsigned int to = find (j); | |
2602 | tmp = get_varinfo (to)->solution; | |
2603 | flag = false; | |
c58936b6 | 2604 | |
3e5937d7 DB |
2605 | /* Don't try to propagate to ourselves. */ |
2606 | if (to == i) | |
2607 | continue; | |
c58936b6 | 2608 | |
5006671f RG |
2609 | /* If we propagate from ESCAPED use ESCAPED as |
2610 | placeholder. */ | |
2611 | if (i == eff_escaped_id) | |
2612 | flag = bitmap_set_bit (tmp, escaped_id); | |
2613 | else | |
2614 | flag = set_union_with_increment (tmp, pts, 0); | |
c58936b6 | 2615 | |
21392f19 | 2616 | if (flag) |
4ee00913 | 2617 | { |
3e5937d7 | 2618 | get_varinfo (to)->solution = tmp; |
648b5f85 | 2619 | bitmap_set_bit (changed, to); |
4ee00913 DB |
2620 | } |
2621 | } | |
910fdc79 DB |
2622 | } |
2623 | } | |
2624 | } | |
2625 | free_topo_info (ti); | |
2626 | bitmap_obstack_release (&iteration_obstack); | |
2627 | } | |
c58936b6 | 2628 | |
3e5937d7 | 2629 | BITMAP_FREE (pts); |
648b5f85 | 2630 | BITMAP_FREE (changed); |
3e5937d7 | 2631 | bitmap_obstack_release (&oldpta_obstack); |
910fdc79 DB |
2632 | } |
2633 | ||
3e5937d7 | 2634 | /* Map from trees to variable infos. */ |
15814ba0 | 2635 | static struct pointer_map_t *vi_for_tree; |
910fdc79 | 2636 | |
910fdc79 | 2637 | |
15814ba0 | 2638 | /* Insert ID as the variable id for tree T in the vi_for_tree map. */ |
910fdc79 | 2639 | |
c58936b6 | 2640 | static void |
3e5937d7 | 2641 | insert_vi_for_tree (tree t, varinfo_t vi) |
910fdc79 | 2642 | { |
15814ba0 PB |
2643 | void **slot = pointer_map_insert (vi_for_tree, t); |
2644 | gcc_assert (vi); | |
910fdc79 | 2645 | gcc_assert (*slot == NULL); |
15814ba0 | 2646 | *slot = vi; |
910fdc79 DB |
2647 | } |
2648 | ||
3e5937d7 | 2649 | /* Find the variable info for tree T in VI_FOR_TREE. If T does not |
15814ba0 | 2650 | exist in the map, return NULL, otherwise, return the varinfo we found. */ |
910fdc79 | 2651 | |
15814ba0 PB |
2652 | static varinfo_t |
2653 | lookup_vi_for_tree (tree t) | |
910fdc79 | 2654 | { |
15814ba0 PB |
2655 | void **slot = pointer_map_contains (vi_for_tree, t); |
2656 | if (slot == NULL) | |
2657 | return NULL; | |
910fdc79 | 2658 | |
15814ba0 | 2659 | return (varinfo_t) *slot; |
910fdc79 DB |
2660 | } |
2661 | ||
2662 | /* Return a printable name for DECL */ | |
2663 | ||
2664 | static const char * | |
2665 | alias_get_name (tree decl) | |
2666 | { | |
27c2cfa6 | 2667 | const char *res; |
910fdc79 DB |
2668 | char *temp; |
2669 | int num_printed = 0; | |
2670 | ||
27c2cfa6 RG |
2671 | if (DECL_ASSEMBLER_NAME_SET_P (decl)) |
2672 | res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); | |
2673 | else | |
2674 | res= get_name (decl); | |
910fdc79 DB |
2675 | if (res != NULL) |
2676 | return res; | |
2677 | ||
2678 | res = "NULL"; | |
4f6c9110 RG |
2679 | if (!dump_file) |
2680 | return res; | |
2681 | ||
910fdc79 DB |
2682 | if (TREE_CODE (decl) == SSA_NAME) |
2683 | { | |
c58936b6 | 2684 | num_printed = asprintf (&temp, "%s_%u", |
910fdc79 DB |
2685 | alias_get_name (SSA_NAME_VAR (decl)), |
2686 | SSA_NAME_VERSION (decl)); | |
2687 | } | |
2688 | else if (DECL_P (decl)) | |
2689 | { | |
2690 | num_printed = asprintf (&temp, "D.%u", DECL_UID (decl)); | |
2691 | } | |
2692 | if (num_printed > 0) | |
2693 | { | |
2694 | res = ggc_strdup (temp); | |
2695 | free (temp); | |
2696 | } | |
2697 | return res; | |
2698 | } | |
2699 | ||
15814ba0 PB |
2700 | /* Find the variable id for tree T in the map. |
2701 | If T doesn't exist in the map, create an entry for it and return it. */ | |
910fdc79 | 2702 | |
3e5937d7 DB |
2703 | static varinfo_t |
2704 | get_vi_for_tree (tree t) | |
910fdc79 | 2705 | { |
15814ba0 PB |
2706 | void **slot = pointer_map_contains (vi_for_tree, t); |
2707 | if (slot == NULL) | |
3e5937d7 | 2708 | return get_varinfo (create_variable_info_for (t, alias_get_name (t))); |
c58936b6 | 2709 | |
15814ba0 | 2710 | return (varinfo_t) *slot; |
910fdc79 DB |
2711 | } |
2712 | ||
b14e9388 | 2713 | /* Get a scalar constraint expression for a new temporary variable. */ |
910fdc79 DB |
2714 | |
2715 | static struct constraint_expr | |
b14e9388 | 2716 | new_scalar_tmp_constraint_exp (const char *name) |
910fdc79 | 2717 | { |
b14e9388 | 2718 | struct constraint_expr tmp; |
b14e9388 | 2719 | varinfo_t vi; |
910fdc79 | 2720 | |
0bbf2ffa | 2721 | vi = new_var_info (NULL_TREE, name); |
b14e9388 RG |
2722 | vi->offset = 0; |
2723 | vi->size = -1; | |
2724 | vi->fullsize = -1; | |
2725 | vi->is_full_var = 1; | |
c0d459f0 | 2726 | |
b14e9388 RG |
2727 | tmp.var = vi->id; |
2728 | tmp.type = SCALAR; | |
2729 | tmp.offset = 0; | |
c0d459f0 | 2730 | |
b14e9388 | 2731 | return tmp; |
c0d459f0 RG |
2732 | } |
2733 | ||
2734 | /* Get a constraint expression vector from an SSA_VAR_P node. | |
2735 | If address_p is true, the result will be taken its address of. */ | |
2736 | ||
2737 | static void | |
2738 | get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p) | |
2739 | { | |
2740 | struct constraint_expr cexpr; | |
2741 | varinfo_t vi; | |
2742 | ||
2743 | /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */ | |
910fdc79 DB |
2744 | gcc_assert (SSA_VAR_P (t) || DECL_P (t)); |
2745 | ||
2746 | /* For parameters, get at the points-to set for the actual parm | |
2747 | decl. */ | |
c58936b6 | 2748 | if (TREE_CODE (t) == SSA_NAME |
6938f93f JH |
2749 | && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL |
2750 | || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL) | |
38635499 | 2751 | && SSA_NAME_IS_DEFAULT_DEF (t)) |
c0d459f0 RG |
2752 | { |
2753 | get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p); | |
2754 | return; | |
2755 | } | |
910fdc79 | 2756 | |
9c7c9f10 RG |
2757 | /* For global variables resort to the alias target. */ |
2758 | if (TREE_CODE (t) == VAR_DECL | |
2759 | && (TREE_STATIC (t) || DECL_EXTERNAL (t))) | |
2760 | { | |
2761 | struct varpool_node *node = varpool_get_node (t); | |
2762 | if (node && node->alias) | |
2763 | { | |
2764 | node = varpool_variable_node (node, NULL); | |
2765 | t = node->decl; | |
2766 | } | |
2767 | } | |
2768 | ||
c0d459f0 RG |
2769 | vi = get_vi_for_tree (t); |
2770 | cexpr.var = vi->id; | |
910fdc79 | 2771 | cexpr.type = SCALAR; |
c0d459f0 | 2772 | cexpr.offset = 0; |
47bcb538 DB |
2773 | /* If we determine the result is "anything", and we know this is readonly, |
2774 | say it points to readonly memory instead. */ | |
2775 | if (cexpr.var == anything_id && TREE_READONLY (t)) | |
910fdc79 | 2776 | { |
c0d459f0 | 2777 | gcc_unreachable (); |
3e5937d7 | 2778 | cexpr.type = ADDRESSOF; |
910fdc79 DB |
2779 | cexpr.var = readonly_id; |
2780 | } | |
c58936b6 | 2781 | |
c0d459f0 RG |
2782 | /* If we are not taking the address of the constraint expr, add all |
2783 | sub-fiels of the variable as well. */ | |
de925a03 RG |
2784 | if (!address_p |
2785 | && !vi->is_full_var) | |
c0d459f0 RG |
2786 | { |
2787 | for (; vi; vi = vi->next) | |
2788 | { | |
2789 | cexpr.var = vi->id; | |
2790 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
2791 | } | |
2792 | return; | |
2793 | } | |
2794 | ||
2795 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
910fdc79 DB |
2796 | } |
2797 | ||
faf2ecc5 RG |
2798 | /* Process constraint T, performing various simplifications and then |
2799 | adding it to our list of overall constraints. */ | |
910fdc79 DB |
2800 | |
2801 | static void | |
faf2ecc5 | 2802 | process_constraint (constraint_t t) |
910fdc79 DB |
2803 | { |
2804 | struct constraint_expr rhs = t->rhs; | |
2805 | struct constraint_expr lhs = t->lhs; | |
c58936b6 | 2806 | |
910fdc79 DB |
2807 | gcc_assert (rhs.var < VEC_length (varinfo_t, varmap)); |
2808 | gcc_assert (lhs.var < VEC_length (varinfo_t, varmap)); | |
2809 | ||
5006671f RG |
2810 | /* If we didn't get any useful constraint from the lhs we get |
2811 | &ANYTHING as fallback from get_constraint_for. Deal with | |
2812 | it here by turning it into *ANYTHING. */ | |
2813 | if (lhs.type == ADDRESSOF | |
2814 | && lhs.var == anything_id) | |
2815 | lhs.type = DEREF; | |
2816 | ||
2817 | /* ADDRESSOF on the lhs is invalid. */ | |
2818 | gcc_assert (lhs.type != ADDRESSOF); | |
910fdc79 | 2819 | |
3c323b52 RG |
2820 | /* We shouldn't add constraints from things that cannot have pointers. |
2821 | It's not completely trivial to avoid in the callers, so do it here. */ | |
2822 | if (rhs.type != ADDRESSOF | |
2823 | && !get_varinfo (rhs.var)->may_have_pointers) | |
2824 | return; | |
2825 | ||
2826 | /* Likewise adding to the solution of a non-pointer var isn't useful. */ | |
2827 | if (!get_varinfo (lhs.var)->may_have_pointers) | |
2828 | return; | |
2829 | ||
910fdc79 | 2830 | /* This can happen in our IR with things like n->a = *p */ |
5006671f | 2831 | if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id) |
910fdc79 DB |
2832 | { |
2833 | /* Split into tmp = *rhs, *lhs = tmp */ | |
b14e9388 RG |
2834 | struct constraint_expr tmplhs; |
2835 | tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp"); | |
faf2ecc5 RG |
2836 | process_constraint (new_constraint (tmplhs, rhs)); |
2837 | process_constraint (new_constraint (lhs, tmplhs)); | |
7b765bed DB |
2838 | } |
2839 | else if (rhs.type == ADDRESSOF && lhs.type == DEREF) | |
2840 | { | |
2841 | /* Split into tmp = &rhs, *lhs = tmp */ | |
b14e9388 RG |
2842 | struct constraint_expr tmplhs; |
2843 | tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp"); | |
faf2ecc5 RG |
2844 | process_constraint (new_constraint (tmplhs, rhs)); |
2845 | process_constraint (new_constraint (lhs, tmplhs)); | |
910fdc79 | 2846 | } |
910fdc79 DB |
2847 | else |
2848 | { | |
3e5937d7 | 2849 | gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0); |
b5efa470 | 2850 | VEC_safe_push (constraint_t, heap, constraints, t); |
910fdc79 DB |
2851 | } |
2852 | } | |
2853 | ||
2854 | ||
2855 | /* Return the position, in bits, of FIELD_DECL from the beginning of its | |
2856 | structure. */ | |
2857 | ||
ee7d4b57 | 2858 | static HOST_WIDE_INT |
910fdc79 DB |
2859 | bitpos_of_field (const tree fdecl) |
2860 | { | |
ee7d4b57 RG |
2861 | if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0) |
2862 | || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0)) | |
910fdc79 | 2863 | return -1; |
c58936b6 | 2864 | |
ea57f573 | 2865 | return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT |
ee7d4b57 | 2866 | + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl))); |
910fdc79 DB |
2867 | } |
2868 | ||
2869 | ||
e5bae89b RG |
2870 | /* Get constraint expressions for offsetting PTR by OFFSET. Stores the |
2871 | resulting constraint expressions in *RESULTS. */ | |
2872 | ||
2873 | static void | |
2874 | get_constraint_for_ptr_offset (tree ptr, tree offset, | |
2875 | VEC (ce_s, heap) **results) | |
2876 | { | |
bd02b3a0 | 2877 | struct constraint_expr c; |
e5bae89b | 2878 | unsigned int j, n; |
97919ae7 | 2879 | HOST_WIDE_INT rhsoffset; |
e5bae89b RG |
2880 | |
2881 | /* If we do not do field-sensitive PTA adding offsets to pointers | |
2882 | does not change the points-to solution. */ | |
2883 | if (!use_field_sensitive) | |
2884 | { | |
ed6c4831 | 2885 | get_constraint_for_rhs (ptr, results); |
e5bae89b RG |
2886 | return; |
2887 | } | |
2888 | ||
2889 | /* If the offset is not a non-negative integer constant that fits | |
2890 | in a HOST_WIDE_INT, we have to fall back to a conservative | |
2891 | solution which includes all sub-fields of all pointed-to | |
5006671f | 2892 | variables of ptr. */ |
779704e7 | 2893 | if (offset == NULL_TREE |
97919ae7 | 2894 | || TREE_CODE (offset) != INTEGER_CST) |
5006671f RG |
2895 | rhsoffset = UNKNOWN_OFFSET; |
2896 | else | |
e5bae89b | 2897 | { |
97919ae7 RG |
2898 | /* Sign-extend the offset. */ |
2899 | double_int soffset | |
2900 | = double_int_sext (tree_to_double_int (offset), | |
2901 | TYPE_PRECISION (TREE_TYPE (offset))); | |
2902 | if (!double_int_fits_in_shwi_p (soffset)) | |
5006671f | 2903 | rhsoffset = UNKNOWN_OFFSET; |
97919ae7 RG |
2904 | else |
2905 | { | |
2906 | /* Make sure the bit-offset also fits. */ | |
2907 | HOST_WIDE_INT rhsunitoffset = soffset.low; | |
2908 | rhsoffset = rhsunitoffset * BITS_PER_UNIT; | |
2909 | if (rhsunitoffset != rhsoffset / BITS_PER_UNIT) | |
2910 | rhsoffset = UNKNOWN_OFFSET; | |
2911 | } | |
e5bae89b RG |
2912 | } |
2913 | ||
ed6c4831 | 2914 | get_constraint_for_rhs (ptr, results); |
e5bae89b RG |
2915 | if (rhsoffset == 0) |
2916 | return; | |
2917 | ||
2918 | /* As we are eventually appending to the solution do not use | |
2919 | VEC_iterate here. */ | |
2920 | n = VEC_length (ce_s, *results); | |
2921 | for (j = 0; j < n; j++) | |
2922 | { | |
2923 | varinfo_t curr; | |
bd02b3a0 RG |
2924 | c = *VEC_index (ce_s, *results, j); |
2925 | curr = get_varinfo (c.var); | |
e5bae89b | 2926 | |
bd02b3a0 | 2927 | if (c.type == ADDRESSOF |
5006671f RG |
2928 | /* If this varinfo represents a full variable just use it. */ |
2929 | && curr->is_full_var) | |
bd02b3a0 RG |
2930 | c.offset = 0; |
2931 | else if (c.type == ADDRESSOF | |
5006671f RG |
2932 | /* If we do not know the offset add all subfields. */ |
2933 | && rhsoffset == UNKNOWN_OFFSET) | |
2934 | { | |
2935 | varinfo_t temp = lookup_vi_for_tree (curr->decl); | |
2936 | do | |
2937 | { | |
2938 | struct constraint_expr c2; | |
2939 | c2.var = temp->id; | |
2940 | c2.type = ADDRESSOF; | |
2941 | c2.offset = 0; | |
bd02b3a0 | 2942 | if (c2.var != c.var) |
779704e7 | 2943 | VEC_safe_push (ce_s, heap, *results, &c2); |
5006671f RG |
2944 | temp = temp->next; |
2945 | } | |
2946 | while (temp); | |
2947 | } | |
bd02b3a0 | 2948 | else if (c.type == ADDRESSOF) |
e5bae89b | 2949 | { |
5006671f RG |
2950 | varinfo_t temp; |
2951 | unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset; | |
e5bae89b RG |
2952 | |
2953 | /* Search the sub-field which overlaps with the | |
5006671f RG |
2954 | pointed-to offset. If the result is outside of the variable |
2955 | we have to provide a conservative result, as the variable is | |
2956 | still reachable from the resulting pointer (even though it | |
2957 | technically cannot point to anything). The last and first | |
2958 | sub-fields are such conservative results. | |
e5bae89b RG |
2959 | ??? If we always had a sub-field for &object + 1 then |
2960 | we could represent this in a more precise way. */ | |
5006671f RG |
2961 | if (rhsoffset < 0 |
2962 | && curr->offset < offset) | |
2963 | offset = 0; | |
2964 | temp = first_or_preceding_vi_for_offset (curr, offset); | |
e5bae89b RG |
2965 | |
2966 | /* If the found variable is not exactly at the pointed to | |
2967 | result, we have to include the next variable in the | |
2968 | solution as well. Otherwise two increments by offset / 2 | |
2969 | do not result in the same or a conservative superset | |
2970 | solution. */ | |
5006671f | 2971 | if (temp->offset != offset |
e5bae89b RG |
2972 | && temp->next != NULL) |
2973 | { | |
2974 | struct constraint_expr c2; | |
2975 | c2.var = temp->next->id; | |
2976 | c2.type = ADDRESSOF; | |
2977 | c2.offset = 0; | |
2978 | VEC_safe_push (ce_s, heap, *results, &c2); | |
2979 | } | |
bd02b3a0 RG |
2980 | c.var = temp->id; |
2981 | c.offset = 0; | |
e5bae89b | 2982 | } |
e5bae89b | 2983 | else |
bd02b3a0 RG |
2984 | c.offset = rhsoffset; |
2985 | ||
2986 | VEC_replace (ce_s, *results, j, &c); | |
e5bae89b RG |
2987 | } |
2988 | } | |
2989 | ||
2990 | ||
c0d459f0 | 2991 | /* Given a COMPONENT_REF T, return the constraint_expr vector for it. |
ed6c4831 RG |
2992 | If address_p is true the result will be taken its address of. |
2993 | If lhs_p is true then the constraint expression is assumed to be used | |
2994 | as the lhs. */ | |
910fdc79 | 2995 | |
4ee00913 | 2996 | static void |
c0d459f0 | 2997 | get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results, |
ed6c4831 | 2998 | bool address_p, bool lhs_p) |
910fdc79 | 2999 | { |
4ee00913 | 3000 | tree orig_t = t; |
b1347638 | 3001 | HOST_WIDE_INT bitsize = -1; |
6bec9271 | 3002 | HOST_WIDE_INT bitmaxsize = -1; |
910fdc79 | 3003 | HOST_WIDE_INT bitpos; |
910fdc79 | 3004 | tree forzero; |
4ee00913 | 3005 | struct constraint_expr *result; |
910fdc79 DB |
3006 | |
3007 | /* Some people like to do cute things like take the address of | |
3008 | &0->a.b */ | |
3009 | forzero = t; | |
2ea9dc64 | 3010 | while (handled_component_p (forzero) |
70f34814 RG |
3011 | || INDIRECT_REF_P (forzero) |
3012 | || TREE_CODE (forzero) == MEM_REF) | |
4ee00913 | 3013 | forzero = TREE_OPERAND (forzero, 0); |
910fdc79 | 3014 | |
c58936b6 | 3015 | if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero)) |
910fdc79 | 3016 | { |
4ee00913 | 3017 | struct constraint_expr temp; |
c58936b6 | 3018 | |
4ee00913 DB |
3019 | temp.offset = 0; |
3020 | temp.var = integer_id; | |
3021 | temp.type = SCALAR; | |
3022 | VEC_safe_push (ce_s, heap, *results, &temp); | |
3023 | return; | |
910fdc79 | 3024 | } |
c58936b6 | 3025 | |
ed6c4831 RG |
3026 | /* Handle type-punning through unions. If we are extracting a pointer |
3027 | from a union via a possibly type-punning access that pointer | |
3028 | points to anything, similar to a conversion of an integer to | |
3029 | a pointer. */ | |
3030 | if (!lhs_p) | |
3031 | { | |
3032 | tree u; | |
3033 | for (u = t; | |
3034 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
3035 | u = TREE_OPERAND (u, 0)) | |
3036 | if (TREE_CODE (u) == COMPONENT_REF | |
3037 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
3038 | { | |
3039 | struct constraint_expr temp; | |
3040 | ||
3041 | temp.offset = 0; | |
3042 | temp.var = anything_id; | |
3043 | temp.type = ADDRESSOF; | |
3044 | VEC_safe_push (ce_s, heap, *results, &temp); | |
3045 | return; | |
3046 | } | |
3047 | } | |
3048 | ||
6bec9271 | 3049 | t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize); |
21392f19 | 3050 | |
c0d459f0 RG |
3051 | /* Pretend to take the address of the base, we'll take care of |
3052 | adding the required subset of sub-fields below. */ | |
ed6c4831 | 3053 | get_constraint_for_1 (t, results, true, lhs_p); |
c0d459f0 | 3054 | gcc_assert (VEC_length (ce_s, *results) == 1); |
e5bae89b | 3055 | result = VEC_last (ce_s, *results); |
910fdc79 | 3056 | |
e5bae89b RG |
3057 | if (result->type == SCALAR |
3058 | && get_varinfo (result->var)->is_full_var) | |
3059 | /* For single-field vars do not bother about the offset. */ | |
3060 | result->offset = 0; | |
3061 | else if (result->type == SCALAR) | |
910fdc79 DB |
3062 | { |
3063 | /* In languages like C, you can access one past the end of an | |
3064 | array. You aren't allowed to dereference it, so we can | |
3065 | ignore this constraint. When we handle pointer subtraction, | |
3066 | we may have to do something cute here. */ | |
c58936b6 | 3067 | |
c0d459f0 | 3068 | if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize |
18455d17 | 3069 | && bitmaxsize != 0) |
dd68d988 DB |
3070 | { |
3071 | /* It's also not true that the constraint will actually start at the | |
3072 | right offset, it may start in some padding. We only care about | |
3073 | setting the constraint to the first actual field it touches, so | |
c58936b6 | 3074 | walk to find it. */ |
c0d459f0 | 3075 | struct constraint_expr cexpr = *result; |
dd68d988 | 3076 | varinfo_t curr; |
c0d459f0 RG |
3077 | VEC_pop (ce_s, *results); |
3078 | cexpr.offset = 0; | |
3079 | for (curr = get_varinfo (cexpr.var); curr; curr = curr->next) | |
dd68d988 | 3080 | { |
63d195d5 | 3081 | if (ranges_overlap_p (curr->offset, curr->size, |
c0d459f0 | 3082 | bitpos, bitmaxsize)) |
dd68d988 | 3083 | { |
c0d459f0 RG |
3084 | cexpr.var = curr->id; |
3085 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
3086 | if (address_p) | |
3087 | break; | |
dd68d988 DB |
3088 | } |
3089 | } | |
e5bae89b RG |
3090 | /* If we are going to take the address of this field then |
3091 | to be able to compute reachability correctly add at least | |
3092 | the last field of the variable. */ | |
3093 | if (address_p | |
3094 | && VEC_length (ce_s, *results) == 0) | |
3095 | { | |
3096 | curr = get_varinfo (cexpr.var); | |
3097 | while (curr->next != NULL) | |
3098 | curr = curr->next; | |
3099 | cexpr.var = curr->id; | |
3100 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
3101 | } | |
0ba0772b | 3102 | else if (VEC_length (ce_s, *results) == 0) |
e5bae89b RG |
3103 | /* Assert that we found *some* field there. The user couldn't be |
3104 | accessing *only* padding. */ | |
3105 | /* Still the user could access one past the end of an array | |
3106 | embedded in a struct resulting in accessing *only* padding. */ | |
0ba0772b RB |
3107 | /* Or accessing only padding via type-punning to a type |
3108 | that has a filed just in padding space. */ | |
3109 | { | |
3110 | cexpr.type = SCALAR; | |
3111 | cexpr.var = anything_id; | |
3112 | cexpr.offset = 0; | |
3113 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
3114 | } | |
dd68d988 | 3115 | } |
18455d17 RG |
3116 | else if (bitmaxsize == 0) |
3117 | { | |
3118 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3119 | fprintf (dump_file, "Access to zero-sized part of variable," | |
3120 | "ignoring\n"); | |
3121 | } | |
910fdc79 DB |
3122 | else |
3123 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3124 | fprintf (dump_file, "Access to past the end of variable, ignoring\n"); | |
910fdc79 | 3125 | } |
5006671f | 3126 | else if (result->type == DEREF) |
7b765bed | 3127 | { |
5006671f RG |
3128 | /* If we do not know exactly where the access goes say so. Note |
3129 | that only for non-structure accesses we know that we access | |
3130 | at most one subfiled of any variable. */ | |
3131 | if (bitpos == -1 | |
3132 | || bitsize != bitmaxsize | |
1c09321c RG |
3133 | || AGGREGATE_TYPE_P (TREE_TYPE (orig_t)) |
3134 | || result->offset == UNKNOWN_OFFSET) | |
5006671f RG |
3135 | result->offset = UNKNOWN_OFFSET; |
3136 | else | |
1c09321c | 3137 | result->offset += bitpos; |
7b765bed | 3138 | } |
b51605c4 RG |
3139 | else if (result->type == ADDRESSOF) |
3140 | { | |
3141 | /* We can end up here for component references on a | |
3142 | VIEW_CONVERT_EXPR <>(&foobar). */ | |
3143 | result->type = SCALAR; | |
3144 | result->var = anything_id; | |
3145 | result->offset = 0; | |
3146 | } | |
c0d459f0 | 3147 | else |
5006671f | 3148 | gcc_unreachable (); |
910fdc79 DB |
3149 | } |
3150 | ||
3151 | ||
3152 | /* Dereference the constraint expression CONS, and return the result. | |
3153 | DEREF (ADDRESSOF) = SCALAR | |
3154 | DEREF (SCALAR) = DEREF | |
3155 | DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp)) | |
3156 | This is needed so that we can handle dereferencing DEREF constraints. */ | |
3157 | ||
4ee00913 DB |
3158 | static void |
3159 | do_deref (VEC (ce_s, heap) **constraints) | |
910fdc79 | 3160 | { |
4ee00913 DB |
3161 | struct constraint_expr *c; |
3162 | unsigned int i = 0; | |
c58936b6 | 3163 | |
ac47786e | 3164 | FOR_EACH_VEC_ELT (ce_s, *constraints, i, c) |
910fdc79 | 3165 | { |
4ee00913 DB |
3166 | if (c->type == SCALAR) |
3167 | c->type = DEREF; | |
3168 | else if (c->type == ADDRESSOF) | |
3169 | c->type = SCALAR; | |
3170 | else if (c->type == DEREF) | |
3171 | { | |
b14e9388 RG |
3172 | struct constraint_expr tmplhs; |
3173 | tmplhs = new_scalar_tmp_constraint_exp ("dereftmp"); | |
4ee00913 DB |
3174 | process_constraint (new_constraint (tmplhs, *c)); |
3175 | c->var = tmplhs.var; | |
3176 | } | |
3177 | else | |
3178 | gcc_unreachable (); | |
910fdc79 | 3179 | } |
910fdc79 DB |
3180 | } |
3181 | ||
1d24fdd9 RG |
3182 | /* Given a tree T, return the constraint expression for taking the |
3183 | address of it. */ | |
3184 | ||
3185 | static void | |
3186 | get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results) | |
3187 | { | |
3188 | struct constraint_expr *c; | |
3189 | unsigned int i; | |
3190 | ||
ed6c4831 | 3191 | get_constraint_for_1 (t, results, true, true); |
1d24fdd9 | 3192 | |
ac47786e | 3193 | FOR_EACH_VEC_ELT (ce_s, *results, i, c) |
1d24fdd9 RG |
3194 | { |
3195 | if (c->type == DEREF) | |
3196 | c->type = SCALAR; | |
3197 | else | |
3198 | c->type = ADDRESSOF; | |
3199 | } | |
3200 | } | |
3201 | ||
910fdc79 DB |
3202 | /* Given a tree T, return the constraint expression for it. */ |
3203 | ||
4ee00913 | 3204 | static void |
ed6c4831 RG |
3205 | get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p, |
3206 | bool lhs_p) | |
910fdc79 DB |
3207 | { |
3208 | struct constraint_expr temp; | |
3209 | ||
3210 | /* x = integer is all glommed to a single variable, which doesn't | |
3211 | point to anything by itself. That is, of course, unless it is an | |
3212 | integer constant being treated as a pointer, in which case, we | |
3213 | will return that this is really the addressof anything. This | |
3214 | happens below, since it will fall into the default case. The only | |
3215 | case we know something about an integer treated like a pointer is | |
3216 | when it is the NULL pointer, and then we just say it points to | |
89ebafc6 PB |
3217 | NULL. |
3218 | ||
3219 | Do not do that if -fno-delete-null-pointer-checks though, because | |
3220 | in that case *NULL does not fail, so it _should_ alias *anything. | |
3221 | It is not worth adding a new option or renaming the existing one, | |
3222 | since this case is relatively obscure. */ | |
8eb7bc3c RG |
3223 | if ((TREE_CODE (t) == INTEGER_CST |
3224 | && integer_zerop (t)) | |
3225 | /* The only valid CONSTRUCTORs in gimple with pointer typed | |
3226 | elements are zero-initializer. But in IPA mode we also | |
3227 | process global initializers, so verify at least. */ | |
3228 | || (TREE_CODE (t) == CONSTRUCTOR | |
3229 | && CONSTRUCTOR_NELTS (t) == 0)) | |
3230 | { | |
3231 | if (flag_delete_null_pointer_checks) | |
3232 | temp.var = nothing_id; | |
3233 | else | |
1f181fde | 3234 | temp.var = nonlocal_id; |
910fdc79 DB |
3235 | temp.type = ADDRESSOF; |
3236 | temp.offset = 0; | |
4ee00913 DB |
3237 | VEC_safe_push (ce_s, heap, *results, &temp); |
3238 | return; | |
910fdc79 DB |
3239 | } |
3240 | ||
bd1f29d9 EB |
3241 | /* String constants are read-only. */ |
3242 | if (TREE_CODE (t) == STRING_CST) | |
3243 | { | |
3244 | temp.var = readonly_id; | |
3245 | temp.type = SCALAR; | |
3246 | temp.offset = 0; | |
3247 | VEC_safe_push (ce_s, heap, *results, &temp); | |
3248 | return; | |
3249 | } | |
3250 | ||
910fdc79 DB |
3251 | switch (TREE_CODE_CLASS (TREE_CODE (t))) |
3252 | { | |
3253 | case tcc_expression: | |
3254 | { | |
3255 | switch (TREE_CODE (t)) | |
3256 | { | |
3257 | case ADDR_EXPR: | |
1d24fdd9 RG |
3258 | get_constraint_for_address_of (TREE_OPERAND (t, 0), results); |
3259 | return; | |
e5bae89b | 3260 | default:; |
910fdc79 | 3261 | } |
e5bae89b | 3262 | break; |
910fdc79 DB |
3263 | } |
3264 | case tcc_reference: | |
3265 | { | |
3266 | switch (TREE_CODE (t)) | |
3267 | { | |
70f34814 | 3268 | case MEM_REF: |
910fdc79 | 3269 | { |
de2184c0 | 3270 | struct constraint_expr cs; |
343b2efc | 3271 | varinfo_t vi, curr; |
97919ae7 RG |
3272 | get_constraint_for_ptr_offset (TREE_OPERAND (t, 0), |
3273 | TREE_OPERAND (t, 1), results); | |
4ee00913 | 3274 | do_deref (results); |
343b2efc RG |
3275 | |
3276 | /* If we are not taking the address then make sure to process | |
3277 | all subvariables we might access. */ | |
1a5d20a4 RG |
3278 | if (address_p) |
3279 | return; | |
3280 | ||
de2184c0 | 3281 | cs = *VEC_last (ce_s, *results); |
1a5d20a4 RG |
3282 | if (cs.type == DEREF) |
3283 | { | |
3284 | /* For dereferences this means we have to defer it | |
3285 | to solving time. */ | |
3286 | VEC_last (ce_s, *results)->offset = UNKNOWN_OFFSET; | |
3287 | return; | |
3288 | } | |
3289 | if (cs.type != SCALAR) | |
343b2efc RG |
3290 | return; |
3291 | ||
de2184c0 | 3292 | vi = get_varinfo (cs.var); |
343b2efc RG |
3293 | curr = vi->next; |
3294 | if (!vi->is_full_var | |
3295 | && curr) | |
3296 | { | |
3297 | unsigned HOST_WIDE_INT size; | |
3298 | if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1)) | |
3299 | size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t))); | |
3300 | else | |
3301 | size = -1; | |
3302 | for (; curr; curr = curr->next) | |
3303 | { | |
3304 | if (curr->offset - vi->offset < size) | |
3305 | { | |
343b2efc RG |
3306 | cs.var = curr->id; |
3307 | VEC_safe_push (ce_s, heap, *results, &cs); | |
3308 | } | |
3309 | else | |
3310 | break; | |
3311 | } | |
3312 | } | |
4ee00913 | 3313 | return; |
910fdc79 DB |
3314 | } |
3315 | case ARRAY_REF: | |
32961db5 | 3316 | case ARRAY_RANGE_REF: |
910fdc79 | 3317 | case COMPONENT_REF: |
ed6c4831 | 3318 | get_constraint_for_component_ref (t, results, address_p, lhs_p); |
4ee00913 | 3319 | return; |
5006671f | 3320 | case VIEW_CONVERT_EXPR: |
ed6c4831 RG |
3321 | get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p, |
3322 | lhs_p); | |
5006671f RG |
3323 | return; |
3324 | /* We are missing handling for TARGET_MEM_REF here. */ | |
e5bae89b | 3325 | default:; |
910fdc79 | 3326 | } |
e5bae89b | 3327 | break; |
910fdc79 | 3328 | } |
910fdc79 DB |
3329 | case tcc_exceptional: |
3330 | { | |
3331 | switch (TREE_CODE (t)) | |
3332 | { | |
910fdc79 | 3333 | case SSA_NAME: |
4ee00913 | 3334 | { |
c0d459f0 | 3335 | get_constraint_for_ssa_var (t, results, address_p); |
4ee00913 DB |
3336 | return; |
3337 | } | |
47d8a903 RG |
3338 | case CONSTRUCTOR: |
3339 | { | |
3340 | unsigned int i; | |
3341 | tree val; | |
3342 | VEC (ce_s, heap) *tmp = NULL; | |
3343 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val) | |
3344 | { | |
3345 | struct constraint_expr *rhsp; | |
3346 | unsigned j; | |
ed6c4831 | 3347 | get_constraint_for_1 (val, &tmp, address_p, lhs_p); |
ac47786e | 3348 | FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp) |
47d8a903 RG |
3349 | VEC_safe_push (ce_s, heap, *results, rhsp); |
3350 | VEC_truncate (ce_s, tmp, 0); | |
3351 | } | |
3352 | VEC_free (ce_s, heap, tmp); | |
3353 | /* We do not know whether the constructor was complete, | |
3354 | so technically we have to add &NOTHING or &ANYTHING | |
3355 | like we do for an empty constructor as well. */ | |
3356 | return; | |
3357 | } | |
e5bae89b | 3358 | default:; |
910fdc79 | 3359 | } |
e5bae89b | 3360 | break; |
910fdc79 DB |
3361 | } |
3362 | case tcc_declaration: | |
4ee00913 | 3363 | { |
c0d459f0 | 3364 | get_constraint_for_ssa_var (t, results, address_p); |
4ee00913 DB |
3365 | return; |
3366 | } | |
1f181fde RG |
3367 | case tcc_constant: |
3368 | { | |
3369 | /* We cannot refer to automatic variables through constants. */ | |
3370 | temp.type = ADDRESSOF; | |
3371 | temp.var = nonlocal_id; | |
3372 | temp.offset = 0; | |
3373 | VEC_safe_push (ce_s, heap, *results, &temp); | |
3374 | return; | |
3375 | } | |
e5bae89b | 3376 | default:; |
910fdc79 | 3377 | } |
e5bae89b RG |
3378 | |
3379 | /* The default fallback is a constraint from anything. */ | |
3380 | temp.type = ADDRESSOF; | |
3381 | temp.var = anything_id; | |
3382 | temp.offset = 0; | |
3383 | VEC_safe_push (ce_s, heap, *results, &temp); | |
910fdc79 DB |
3384 | } |
3385 | ||
c0d459f0 RG |
3386 | /* Given a gimple tree T, return the constraint expression vector for it. */ |
3387 | ||
3388 | static void | |
3389 | get_constraint_for (tree t, VEC (ce_s, heap) **results) | |
3390 | { | |
3391 | gcc_assert (VEC_length (ce_s, *results) == 0); | |
3392 | ||
ed6c4831 RG |
3393 | get_constraint_for_1 (t, results, false, true); |
3394 | } | |
3395 | ||
3396 | /* Given a gimple tree T, return the constraint expression vector for it | |
3397 | to be used as the rhs of a constraint. */ | |
3398 | ||
3399 | static void | |
3400 | get_constraint_for_rhs (tree t, VEC (ce_s, heap) **results) | |
3401 | { | |
3402 | gcc_assert (VEC_length (ce_s, *results) == 0); | |
3403 | ||
3404 | get_constraint_for_1 (t, results, false, false); | |
c0d459f0 | 3405 | } |
910fdc79 | 3406 | |
779704e7 RG |
3407 | |
3408 | /* Efficiently generates constraints from all entries in *RHSC to all | |
3409 | entries in *LHSC. */ | |
3410 | ||
3411 | static void | |
3412 | process_all_all_constraints (VEC (ce_s, heap) *lhsc, VEC (ce_s, heap) *rhsc) | |
3413 | { | |
3414 | struct constraint_expr *lhsp, *rhsp; | |
3415 | unsigned i, j; | |
3416 | ||
3417 | if (VEC_length (ce_s, lhsc) <= 1 | |
3418 | || VEC_length (ce_s, rhsc) <= 1) | |
3419 | { | |
ac47786e NF |
3420 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
3421 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) | |
779704e7 RG |
3422 | process_constraint (new_constraint (*lhsp, *rhsp)); |
3423 | } | |
3424 | else | |
3425 | { | |
3426 | struct constraint_expr tmp; | |
b14e9388 | 3427 | tmp = new_scalar_tmp_constraint_exp ("allalltmp"); |
ac47786e | 3428 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
779704e7 | 3429 | process_constraint (new_constraint (tmp, *rhsp)); |
ac47786e | 3430 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
779704e7 RG |
3431 | process_constraint (new_constraint (*lhsp, tmp)); |
3432 | } | |
3433 | } | |
3434 | ||
910fdc79 DB |
3435 | /* Handle aggregate copies by expanding into copies of the respective |
3436 | fields of the structures. */ | |
3437 | ||
3438 | static void | |
3439 | do_structure_copy (tree lhsop, tree rhsop) | |
3440 | { | |
5006671f | 3441 | struct constraint_expr *lhsp, *rhsp; |
4ee00913 | 3442 | VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL; |
5006671f RG |
3443 | unsigned j; |
3444 | ||
3445 | get_constraint_for (lhsop, &lhsc); | |
ed6c4831 | 3446 | get_constraint_for_rhs (rhsop, &rhsc); |
5006671f RG |
3447 | lhsp = VEC_index (ce_s, lhsc, 0); |
3448 | rhsp = VEC_index (ce_s, rhsc, 0); | |
3449 | if (lhsp->type == DEREF | |
3450 | || (lhsp->type == ADDRESSOF && lhsp->var == anything_id) | |
3451 | || rhsp->type == DEREF) | |
b28ae58f RG |
3452 | { |
3453 | if (lhsp->type == DEREF) | |
3454 | { | |
3455 | gcc_assert (VEC_length (ce_s, lhsc) == 1); | |
3456 | lhsp->offset = UNKNOWN_OFFSET; | |
3457 | } | |
3458 | if (rhsp->type == DEREF) | |
3459 | { | |
3460 | gcc_assert (VEC_length (ce_s, rhsc) == 1); | |
3461 | rhsp->offset = UNKNOWN_OFFSET; | |
3462 | } | |
3463 | process_all_all_constraints (lhsc, rhsc); | |
3464 | } | |
5006671f RG |
3465 | else if (lhsp->type == SCALAR |
3466 | && (rhsp->type == SCALAR | |
3467 | || rhsp->type == ADDRESSOF)) | |
910fdc79 | 3468 | { |
5006671f RG |
3469 | HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset; |
3470 | HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset; | |
3471 | unsigned k = 0; | |
0f900dfa JJ |
3472 | get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize); |
3473 | get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize); | |
5006671f | 3474 | for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);) |
910fdc79 | 3475 | { |
5006671f RG |
3476 | varinfo_t lhsv, rhsv; |
3477 | rhsp = VEC_index (ce_s, rhsc, k); | |
3478 | lhsv = get_varinfo (lhsp->var); | |
3479 | rhsv = get_varinfo (rhsp->var); | |
3480 | if (lhsv->may_have_pointers | |
c636a4fb RG |
3481 | && (lhsv->is_full_var |
3482 | || rhsv->is_full_var | |
3483 | || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size, | |
3484 | rhsv->offset + lhsoffset, rhsv->size))) | |
5006671f | 3485 | process_constraint (new_constraint (*lhsp, *rhsp)); |
c636a4fb RG |
3486 | if (!rhsv->is_full_var |
3487 | && (lhsv->is_full_var | |
3488 | || (lhsv->offset + rhsoffset + lhsv->size | |
3489 | > rhsv->offset + lhsoffset + rhsv->size))) | |
5006671f RG |
3490 | { |
3491 | ++k; | |
3492 | if (k >= VEC_length (ce_s, rhsc)) | |
3493 | break; | |
3494 | } | |
910fdc79 | 3495 | else |
5006671f | 3496 | ++j; |
910fdc79 DB |
3497 | } |
3498 | } | |
3499 | else | |
5006671f | 3500 | gcc_unreachable (); |
a5eadacc | 3501 | |
5006671f RG |
3502 | VEC_free (ce_s, heap, lhsc); |
3503 | VEC_free (ce_s, heap, rhsc); | |
910fdc79 DB |
3504 | } |
3505 | ||
cb89b4b0 | 3506 | /* Create constraints ID = { rhsc }. */ |
b7091901 RG |
3507 | |
3508 | static void | |
cb89b4b0 | 3509 | make_constraints_to (unsigned id, VEC(ce_s, heap) *rhsc) |
b7091901 | 3510 | { |
b7091901 RG |
3511 | struct constraint_expr *c; |
3512 | struct constraint_expr includes; | |
3513 | unsigned int j; | |
3514 | ||
3515 | includes.var = id; | |
3516 | includes.offset = 0; | |
3517 | includes.type = SCALAR; | |
3518 | ||
ac47786e | 3519 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, c) |
faf2ecc5 | 3520 | process_constraint (new_constraint (includes, *c)); |
cb89b4b0 RG |
3521 | } |
3522 | ||
3523 | /* Create a constraint ID = OP. */ | |
3524 | ||
3525 | static void | |
3526 | make_constraint_to (unsigned id, tree op) | |
3527 | { | |
3528 | VEC(ce_s, heap) *rhsc = NULL; | |
3529 | get_constraint_for_rhs (op, &rhsc); | |
3530 | make_constraints_to (id, rhsc); | |
b7091901 RG |
3531 | VEC_free (ce_s, heap, rhsc); |
3532 | } | |
3533 | ||
74d27244 RG |
3534 | /* Create a constraint ID = &FROM. */ |
3535 | ||
3536 | static void | |
3537 | make_constraint_from (varinfo_t vi, int from) | |
3538 | { | |
3539 | struct constraint_expr lhs, rhs; | |
3540 | ||
3541 | lhs.var = vi->id; | |
3542 | lhs.offset = 0; | |
3543 | lhs.type = SCALAR; | |
3544 | ||
3545 | rhs.var = from; | |
3546 | rhs.offset = 0; | |
3547 | rhs.type = ADDRESSOF; | |
3548 | process_constraint (new_constraint (lhs, rhs)); | |
3549 | } | |
3550 | ||
3551 | /* Create a constraint ID = FROM. */ | |
3552 | ||
3553 | static void | |
3554 | make_copy_constraint (varinfo_t vi, int from) | |
3555 | { | |
3556 | struct constraint_expr lhs, rhs; | |
3557 | ||
3558 | lhs.var = vi->id; | |
3559 | lhs.offset = 0; | |
3560 | lhs.type = SCALAR; | |
3561 | ||
3562 | rhs.var = from; | |
3563 | rhs.offset = 0; | |
3564 | rhs.type = SCALAR; | |
3565 | process_constraint (new_constraint (lhs, rhs)); | |
3566 | } | |
3567 | ||
b7091901 RG |
3568 | /* Make constraints necessary to make OP escape. */ |
3569 | ||
3570 | static void | |
3571 | make_escape_constraint (tree op) | |
3572 | { | |
3573 | make_constraint_to (escaped_id, op); | |
3574 | } | |
3575 | ||
3e8542ca RG |
3576 | /* Add constraints to that the solution of VI is transitively closed. */ |
3577 | ||
3578 | static void | |
3579 | make_transitive_closure_constraints (varinfo_t vi) | |
3580 | { | |
3581 | struct constraint_expr lhs, rhs; | |
3582 | ||
3583 | /* VAR = *VAR; */ | |
3584 | lhs.type = SCALAR; | |
3585 | lhs.var = vi->id; | |
3586 | lhs.offset = 0; | |
3587 | rhs.type = DEREF; | |
3588 | rhs.var = vi->id; | |
3589 | rhs.offset = 0; | |
3590 | process_constraint (new_constraint (lhs, rhs)); | |
3591 | ||
3592 | /* VAR = VAR + UNKNOWN; */ | |
3593 | lhs.type = SCALAR; | |
3594 | lhs.var = vi->id; | |
3595 | lhs.offset = 0; | |
3596 | rhs.type = SCALAR; | |
3597 | rhs.var = vi->id; | |
3598 | rhs.offset = UNKNOWN_OFFSET; | |
3599 | process_constraint (new_constraint (lhs, rhs)); | |
3600 | } | |
3601 | ||
7d6e2521 RG |
3602 | /* Temporary storage for fake var decls. */ |
3603 | struct obstack fake_var_decl_obstack; | |
3604 | ||
3605 | /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */ | |
3606 | ||
3607 | static tree | |
3608 | build_fake_var_decl (tree type) | |
3609 | { | |
3610 | tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl); | |
3611 | memset (decl, 0, sizeof (struct tree_var_decl)); | |
3612 | TREE_SET_CODE (decl, VAR_DECL); | |
3613 | TREE_TYPE (decl) = type; | |
3614 | DECL_UID (decl) = allocate_decl_uid (); | |
3615 | SET_DECL_PT_UID (decl, -1); | |
3616 | layout_decl (decl, 0); | |
3617 | return decl; | |
3618 | } | |
3619 | ||
0b7b376d RG |
3620 | /* Create a new artificial heap variable with NAME. |
3621 | Return the created variable. */ | |
74d27244 RG |
3622 | |
3623 | static varinfo_t | |
7d6e2521 | 3624 | make_heapvar (const char *name) |
74d27244 RG |
3625 | { |
3626 | varinfo_t vi; | |
7d6e2521 RG |
3627 | tree heapvar; |
3628 | ||
3629 | heapvar = build_fake_var_decl (ptr_type_node); | |
3630 | DECL_EXTERNAL (heapvar) = 1; | |
74d27244 RG |
3631 | |
3632 | vi = new_var_info (heapvar, name); | |
3633 | vi->is_artificial_var = true; | |
3634 | vi->is_heap_var = true; | |
3635 | vi->is_unknown_size_var = true; | |
b41e33fe | 3636 | vi->offset = 0; |
74d27244 RG |
3637 | vi->fullsize = ~0; |
3638 | vi->size = ~0; | |
3639 | vi->is_full_var = true; | |
3640 | insert_vi_for_tree (heapvar, vi); | |
3641 | ||
0b7b376d RG |
3642 | return vi; |
3643 | } | |
3644 | ||
3645 | /* Create a new artificial heap variable with NAME and make a | |
3646 | constraint from it to LHS. Return the created variable. */ | |
3647 | ||
3648 | static varinfo_t | |
3649 | make_constraint_from_heapvar (varinfo_t lhs, const char *name) | |
3650 | { | |
7d6e2521 | 3651 | varinfo_t vi = make_heapvar (name); |
74d27244 RG |
3652 | make_constraint_from (lhs, vi->id); |
3653 | ||
3654 | return vi; | |
3655 | } | |
3656 | ||
3657 | /* Create a new artificial heap variable with NAME and make a | |
3658 | constraint from it to LHS. Set flags according to a tag used | |
3659 | for tracking restrict pointers. */ | |
3660 | ||
3661 | static void | |
3662 | make_constraint_from_restrict (varinfo_t lhs, const char *name) | |
3663 | { | |
3664 | varinfo_t vi; | |
3665 | vi = make_constraint_from_heapvar (lhs, name); | |
3666 | vi->is_restrict_var = 1; | |
3667 | vi->is_global_var = 0; | |
3668 | vi->is_special_var = 1; | |
3669 | vi->may_have_pointers = 0; | |
3670 | } | |
3671 | ||
25a6a873 RG |
3672 | /* In IPA mode there are varinfos for different aspects of reach |
3673 | function designator. One for the points-to set of the return | |
3674 | value, one for the variables that are clobbered by the function, | |
3675 | one for its uses and one for each parameter (including a single | |
3676 | glob for remaining variadic arguments). */ | |
3677 | ||
3678 | enum { fi_clobbers = 1, fi_uses = 2, | |
3679 | fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 }; | |
3680 | ||
3681 | /* Get a constraint for the requested part of a function designator FI | |
3682 | when operating in IPA mode. */ | |
3683 | ||
3684 | static struct constraint_expr | |
3685 | get_function_part_constraint (varinfo_t fi, unsigned part) | |
3686 | { | |
3687 | struct constraint_expr c; | |
3688 | ||
3689 | gcc_assert (in_ipa_mode); | |
3690 | ||
3691 | if (fi->id == anything_id) | |
3692 | { | |
3693 | /* ??? We probably should have a ANYFN special variable. */ | |
3694 | c.var = anything_id; | |
3695 | c.offset = 0; | |
3696 | c.type = SCALAR; | |
3697 | } | |
3698 | else if (TREE_CODE (fi->decl) == FUNCTION_DECL) | |
3699 | { | |
3700 | varinfo_t ai = first_vi_for_offset (fi, part); | |
18abb35e RG |
3701 | if (ai) |
3702 | c.var = ai->id; | |
3703 | else | |
3704 | c.var = anything_id; | |
25a6a873 RG |
3705 | c.offset = 0; |
3706 | c.type = SCALAR; | |
3707 | } | |
3708 | else | |
3709 | { | |
3710 | c.var = fi->id; | |
3711 | c.offset = part; | |
3712 | c.type = DEREF; | |
3713 | } | |
3714 | ||
3715 | return c; | |
3716 | } | |
3717 | ||
7b765bed DB |
3718 | /* For non-IPA mode, generate constraints necessary for a call on the |
3719 | RHS. */ | |
3720 | ||
3721 | static void | |
472c7fbd | 3722 | handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results) |
7b765bed | 3723 | { |
472c7fbd | 3724 | struct constraint_expr rhsc; |
726a989a | 3725 | unsigned i; |
0b7b376d | 3726 | bool returns_uses = false; |
7b765bed | 3727 | |
726a989a RB |
3728 | for (i = 0; i < gimple_call_num_args (stmt); ++i) |
3729 | { | |
3730 | tree arg = gimple_call_arg (stmt, i); | |
0b7b376d | 3731 | int flags = gimple_call_arg_flags (stmt, i); |
726a989a | 3732 | |
0f8d6231 RG |
3733 | /* If the argument is not used we can ignore it. */ |
3734 | if (flags & EAF_UNUSED) | |
0b7b376d RG |
3735 | continue; |
3736 | ||
3737 | /* As we compute ESCAPED context-insensitive we do not gain | |
3738 | any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE | |
3739 | set. The argument would still get clobbered through the | |
3740 | escape solution. | |
3741 | ??? We might get away with less (and more precise) constraints | |
3742 | if using a temporary for transitively closing things. */ | |
3743 | if ((flags & EAF_NOCLOBBER) | |
3744 | && (flags & EAF_NOESCAPE)) | |
3745 | { | |
3746 | varinfo_t uses = get_call_use_vi (stmt); | |
3747 | if (!(flags & EAF_DIRECT)) | |
3748 | make_transitive_closure_constraints (uses); | |
3749 | make_constraint_to (uses->id, arg); | |
3750 | returns_uses = true; | |
3751 | } | |
3752 | else if (flags & EAF_NOESCAPE) | |
3753 | { | |
3754 | varinfo_t uses = get_call_use_vi (stmt); | |
3755 | varinfo_t clobbers = get_call_clobber_vi (stmt); | |
3756 | if (!(flags & EAF_DIRECT)) | |
3757 | { | |
3758 | make_transitive_closure_constraints (uses); | |
3759 | make_transitive_closure_constraints (clobbers); | |
3760 | } | |
3761 | make_constraint_to (uses->id, arg); | |
3762 | make_constraint_to (clobbers->id, arg); | |
3763 | returns_uses = true; | |
3764 | } | |
3765 | else | |
726a989a RB |
3766 | make_escape_constraint (arg); |
3767 | } | |
b7091901 | 3768 | |
0b7b376d RG |
3769 | /* If we added to the calls uses solution make sure we account for |
3770 | pointers to it to be returned. */ | |
3771 | if (returns_uses) | |
3772 | { | |
3773 | rhsc.var = get_call_use_vi (stmt)->id; | |
3774 | rhsc.offset = 0; | |
3775 | rhsc.type = SCALAR; | |
3776 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
3777 | } | |
3778 | ||
b7091901 | 3779 | /* The static chain escapes as well. */ |
726a989a RB |
3780 | if (gimple_call_chain (stmt)) |
3781 | make_escape_constraint (gimple_call_chain (stmt)); | |
472c7fbd | 3782 | |
1d24fdd9 RG |
3783 | /* And if we applied NRV the address of the return slot escapes as well. */ |
3784 | if (gimple_call_return_slot_opt_p (stmt) | |
3785 | && gimple_call_lhs (stmt) != NULL_TREE | |
4d61856d | 3786 | && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt)))) |
1d24fdd9 RG |
3787 | { |
3788 | VEC(ce_s, heap) *tmpc = NULL; | |
3789 | struct constraint_expr lhsc, *c; | |
3790 | get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc); | |
3791 | lhsc.var = escaped_id; | |
3792 | lhsc.offset = 0; | |
3793 | lhsc.type = SCALAR; | |
ac47786e | 3794 | FOR_EACH_VEC_ELT (ce_s, tmpc, i, c) |
1d24fdd9 RG |
3795 | process_constraint (new_constraint (lhsc, *c)); |
3796 | VEC_free(ce_s, heap, tmpc); | |
3797 | } | |
3798 | ||
5006671f RG |
3799 | /* Regular functions return nonlocal memory. */ |
3800 | rhsc.var = nonlocal_id; | |
472c7fbd | 3801 | rhsc.offset = 0; |
5006671f | 3802 | rhsc.type = SCALAR; |
472c7fbd | 3803 | VEC_safe_push (ce_s, heap, *results, &rhsc); |
7b765bed | 3804 | } |
e8ca4159 | 3805 | |
af947da7 RG |
3806 | /* For non-IPA mode, generate constraints necessary for a call |
3807 | that returns a pointer and assigns it to LHS. This simply makes | |
b7091901 | 3808 | the LHS point to global and escaped variables. */ |
af947da7 RG |
3809 | |
3810 | static void | |
0b7b376d RG |
3811 | handle_lhs_call (gimple stmt, tree lhs, int flags, VEC(ce_s, heap) *rhsc, |
3812 | tree fndecl) | |
af947da7 RG |
3813 | { |
3814 | VEC(ce_s, heap) *lhsc = NULL; | |
af947da7 | 3815 | |
b7091901 | 3816 | get_constraint_for (lhs, &lhsc); |
0b7b376d RG |
3817 | /* If the store is to a global decl make sure to |
3818 | add proper escape constraints. */ | |
3819 | lhs = get_base_address (lhs); | |
3820 | if (lhs | |
3821 | && DECL_P (lhs) | |
3822 | && is_global_var (lhs)) | |
3823 | { | |
3824 | struct constraint_expr tmpc; | |
3825 | tmpc.var = escaped_id; | |
3826 | tmpc.offset = 0; | |
3827 | tmpc.type = SCALAR; | |
3828 | VEC_safe_push (ce_s, heap, lhsc, &tmpc); | |
3829 | } | |
183ae595 | 3830 | |
0b7b376d RG |
3831 | /* If the call returns an argument unmodified override the rhs |
3832 | constraints. */ | |
3833 | flags = gimple_call_return_flags (stmt); | |
3834 | if (flags & ERF_RETURNS_ARG | |
3835 | && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt)) | |
3836 | { | |
3837 | tree arg; | |
3838 | rhsc = NULL; | |
3839 | arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK); | |
3840 | get_constraint_for (arg, &rhsc); | |
3841 | process_all_all_constraints (lhsc, rhsc); | |
3842 | VEC_free (ce_s, heap, rhsc); | |
3843 | } | |
3844 | else if (flags & ERF_NOALIAS) | |
183ae595 | 3845 | { |
183ae595 | 3846 | varinfo_t vi; |
0b7b376d RG |
3847 | struct constraint_expr tmpc; |
3848 | rhsc = NULL; | |
7d6e2521 | 3849 | vi = make_heapvar ("HEAP"); |
14c41b9b RG |
3850 | /* We delay marking allocated storage global until we know if |
3851 | it escapes. */ | |
91deb937 | 3852 | DECL_EXTERNAL (vi->decl) = 0; |
14c41b9b | 3853 | vi->is_global_var = 0; |
72d182d3 | 3854 | /* If this is not a real malloc call assume the memory was |
0b7b376d | 3855 | initialized and thus may point to global memory. All |
72d182d3 RG |
3856 | builtin functions with the malloc attribute behave in a sane way. */ |
3857 | if (!fndecl | |
3858 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
3859 | make_constraint_from (vi, nonlocal_id); | |
0b7b376d RG |
3860 | tmpc.var = vi->id; |
3861 | tmpc.offset = 0; | |
3862 | tmpc.type = ADDRESSOF; | |
3863 | VEC_safe_push (ce_s, heap, rhsc, &tmpc); | |
183ae595 | 3864 | } |
0b7b376d RG |
3865 | |
3866 | process_all_all_constraints (lhsc, rhsc); | |
3867 | ||
b7091901 RG |
3868 | VEC_free (ce_s, heap, lhsc); |
3869 | } | |
3870 | ||
3871 | /* For non-IPA mode, generate constraints necessary for a call of a | |
3872 | const function that returns a pointer in the statement STMT. */ | |
3873 | ||
3874 | static void | |
472c7fbd | 3875 | handle_const_call (gimple stmt, VEC(ce_s, heap) **results) |
b7091901 | 3876 | { |
b14e9388 | 3877 | struct constraint_expr rhsc; |
472c7fbd | 3878 | unsigned int k; |
b7091901 | 3879 | |
472c7fbd RG |
3880 | /* Treat nested const functions the same as pure functions as far |
3881 | as the static chain is concerned. */ | |
726a989a | 3882 | if (gimple_call_chain (stmt)) |
b7091901 | 3883 | { |
3e8542ca RG |
3884 | varinfo_t uses = get_call_use_vi (stmt); |
3885 | make_transitive_closure_constraints (uses); | |
3886 | make_constraint_to (uses->id, gimple_call_chain (stmt)); | |
3887 | rhsc.var = uses->id; | |
b7091901 | 3888 | rhsc.offset = 0; |
472c7fbd RG |
3889 | rhsc.type = SCALAR; |
3890 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
b7091901 RG |
3891 | } |
3892 | ||
b7091901 | 3893 | /* May return arguments. */ |
726a989a RB |
3894 | for (k = 0; k < gimple_call_num_args (stmt); ++k) |
3895 | { | |
3896 | tree arg = gimple_call_arg (stmt, k); | |
0f8d6231 RG |
3897 | VEC(ce_s, heap) *argc = NULL; |
3898 | unsigned i; | |
3899 | struct constraint_expr *argp; | |
3900 | get_constraint_for_rhs (arg, &argc); | |
3901 | FOR_EACH_VEC_ELT (ce_s, argc, i, argp) | |
3902 | VEC_safe_push (ce_s, heap, *results, argp); | |
3903 | VEC_free(ce_s, heap, argc); | |
726a989a | 3904 | } |
b7091901 | 3905 | |
472c7fbd RG |
3906 | /* May return addresses of globals. */ |
3907 | rhsc.var = nonlocal_id; | |
3908 | rhsc.offset = 0; | |
3909 | rhsc.type = ADDRESSOF; | |
3910 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
af947da7 RG |
3911 | } |
3912 | ||
15c15196 RG |
3913 | /* For non-IPA mode, generate constraints necessary for a call to a |
3914 | pure function in statement STMT. */ | |
3915 | ||
3916 | static void | |
472c7fbd | 3917 | handle_pure_call (gimple stmt, VEC(ce_s, heap) **results) |
15c15196 | 3918 | { |
472c7fbd | 3919 | struct constraint_expr rhsc; |
726a989a | 3920 | unsigned i; |
3e8542ca | 3921 | varinfo_t uses = NULL; |
15c15196 RG |
3922 | |
3923 | /* Memory reached from pointer arguments is call-used. */ | |
726a989a RB |
3924 | for (i = 0; i < gimple_call_num_args (stmt); ++i) |
3925 | { | |
3926 | tree arg = gimple_call_arg (stmt, i); | |
0f8d6231 | 3927 | if (!uses) |
472c7fbd | 3928 | { |
0f8d6231 RG |
3929 | uses = get_call_use_vi (stmt); |
3930 | make_transitive_closure_constraints (uses); | |
472c7fbd | 3931 | } |
0f8d6231 | 3932 | make_constraint_to (uses->id, arg); |
726a989a | 3933 | } |
15c15196 RG |
3934 | |
3935 | /* The static chain is used as well. */ | |
726a989a | 3936 | if (gimple_call_chain (stmt)) |
15c15196 | 3937 | { |
3e8542ca RG |
3938 | if (!uses) |
3939 | { | |
3940 | uses = get_call_use_vi (stmt); | |
3941 | make_transitive_closure_constraints (uses); | |
3942 | } | |
3943 | make_constraint_to (uses->id, gimple_call_chain (stmt)); | |
472c7fbd | 3944 | } |
15c15196 | 3945 | |
3e8542ca RG |
3946 | /* Pure functions may return call-used and nonlocal memory. */ |
3947 | if (uses) | |
472c7fbd | 3948 | { |
3e8542ca | 3949 | rhsc.var = uses->id; |
15c15196 | 3950 | rhsc.offset = 0; |
472c7fbd RG |
3951 | rhsc.type = SCALAR; |
3952 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
15c15196 | 3953 | } |
5006671f | 3954 | rhsc.var = nonlocal_id; |
472c7fbd | 3955 | rhsc.offset = 0; |
5006671f | 3956 | rhsc.type = SCALAR; |
472c7fbd | 3957 | VEC_safe_push (ce_s, heap, *results, &rhsc); |
15c15196 RG |
3958 | } |
3959 | ||
25a6a873 RG |
3960 | |
3961 | /* Return the varinfo for the callee of CALL. */ | |
3962 | ||
3963 | static varinfo_t | |
3964 | get_fi_for_callee (gimple call) | |
3965 | { | |
5c04e9f4 | 3966 | tree decl, fn = gimple_call_fn (call); |
25a6a873 | 3967 | |
5c04e9f4 RG |
3968 | if (fn && TREE_CODE (fn) == OBJ_TYPE_REF) |
3969 | fn = OBJ_TYPE_REF_EXPR (fn); | |
25583c4f | 3970 | |
25a6a873 RG |
3971 | /* If we can directly resolve the function being called, do so. |
3972 | Otherwise, it must be some sort of indirect expression that | |
3973 | we should still be able to handle. */ | |
5c04e9f4 | 3974 | decl = gimple_call_addr_fndecl (fn); |
25a6a873 RG |
3975 | if (decl) |
3976 | return get_vi_for_tree (decl); | |
3977 | ||
5c04e9f4 | 3978 | /* If the function is anything other than a SSA name pointer we have no |
25a6a873 | 3979 | clue and should be getting ANYFN (well, ANYTHING for now). */ |
5c04e9f4 | 3980 | if (!fn || TREE_CODE (fn) != SSA_NAME) |
25a6a873 | 3981 | return get_varinfo (anything_id); |
5c04e9f4 RG |
3982 | |
3983 | if ((TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL | |
3984 | || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL) | |
3985 | && SSA_NAME_IS_DEFAULT_DEF (fn)) | |
3986 | fn = SSA_NAME_VAR (fn); | |
3987 | ||
3988 | return get_vi_for_tree (fn); | |
25a6a873 RG |
3989 | } |
3990 | ||
e38811ce RG |
3991 | /* Create constraints for the builtin call T. Return true if the call |
3992 | was handled, otherwise false. */ | |
910fdc79 | 3993 | |
e38811ce RG |
3994 | static bool |
3995 | find_func_aliases_for_builtin_call (gimple t) | |
910fdc79 | 3996 | { |
e38811ce | 3997 | tree fndecl = gimple_call_fndecl (t); |
4ee00913 DB |
3998 | VEC(ce_s, heap) *lhsc = NULL; |
3999 | VEC(ce_s, heap) *rhsc = NULL; | |
25a6a873 | 4000 | varinfo_t fi; |
910fdc79 | 4001 | |
e38811ce RG |
4002 | if (fndecl != NULL_TREE |
4003 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
4004 | /* ??? All builtins that are handled here need to be handled | |
4005 | in the alias-oracle query functions explicitly! */ | |
4006 | switch (DECL_FUNCTION_CODE (fndecl)) | |
4007 | { | |
4008 | /* All the following functions return a pointer to the same object | |
4009 | as their first argument points to. The functions do not add | |
4010 | to the ESCAPED solution. The functions make the first argument | |
4011 | pointed to memory point to what the second argument pointed to | |
4012 | memory points to. */ | |
4013 | case BUILT_IN_STRCPY: | |
4014 | case BUILT_IN_STRNCPY: | |
4015 | case BUILT_IN_BCOPY: | |
4016 | case BUILT_IN_MEMCPY: | |
4017 | case BUILT_IN_MEMMOVE: | |
4018 | case BUILT_IN_MEMPCPY: | |
4019 | case BUILT_IN_STPCPY: | |
4020 | case BUILT_IN_STPNCPY: | |
4021 | case BUILT_IN_STRCAT: | |
4022 | case BUILT_IN_STRNCAT: | |
36dc1a88 JJ |
4023 | case BUILT_IN_STRCPY_CHK: |
4024 | case BUILT_IN_STRNCPY_CHK: | |
4025 | case BUILT_IN_MEMCPY_CHK: | |
4026 | case BUILT_IN_MEMMOVE_CHK: | |
4027 | case BUILT_IN_MEMPCPY_CHK: | |
4028 | case BUILT_IN_STPCPY_CHK: | |
4029 | case BUILT_IN_STRCAT_CHK: | |
4030 | case BUILT_IN_STRNCAT_CHK: | |
e8ca4159 | 4031 | { |
e38811ce RG |
4032 | tree res = gimple_call_lhs (t); |
4033 | tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl) | |
4034 | == BUILT_IN_BCOPY ? 1 : 0)); | |
4035 | tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl) | |
4036 | == BUILT_IN_BCOPY ? 0 : 1)); | |
4037 | if (res != NULL_TREE) | |
0f8d6231 | 4038 | { |
e38811ce RG |
4039 | get_constraint_for (res, &lhsc); |
4040 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY | |
4041 | || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY | |
ce7e54ff JJ |
4042 | || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY |
4043 | || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK | |
4044 | || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK) | |
e38811ce RG |
4045 | get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc); |
4046 | else | |
4047 | get_constraint_for (dest, &rhsc); | |
4048 | process_all_all_constraints (lhsc, rhsc); | |
4049 | VEC_free (ce_s, heap, lhsc); | |
4050 | VEC_free (ce_s, heap, rhsc); | |
c58936b6 | 4051 | } |
e38811ce RG |
4052 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); |
4053 | get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc); | |
4054 | do_deref (&lhsc); | |
4055 | do_deref (&rhsc); | |
4056 | process_all_all_constraints (lhsc, rhsc); | |
4057 | VEC_free (ce_s, heap, lhsc); | |
4058 | VEC_free (ce_s, heap, rhsc); | |
4059 | return true; | |
4ee00913 | 4060 | } |
e38811ce | 4061 | case BUILT_IN_MEMSET: |
36dc1a88 | 4062 | case BUILT_IN_MEMSET_CHK: |
e38811ce RG |
4063 | { |
4064 | tree res = gimple_call_lhs (t); | |
4065 | tree dest = gimple_call_arg (t, 0); | |
4066 | unsigned i; | |
4067 | ce_s *lhsp; | |
4068 | struct constraint_expr ac; | |
4069 | if (res != NULL_TREE) | |
779704e7 | 4070 | { |
e38811ce RG |
4071 | get_constraint_for (res, &lhsc); |
4072 | get_constraint_for (dest, &rhsc); | |
779704e7 RG |
4073 | process_all_all_constraints (lhsc, rhsc); |
4074 | VEC_free (ce_s, heap, lhsc); | |
4075 | VEC_free (ce_s, heap, rhsc); | |
779704e7 | 4076 | } |
e38811ce RG |
4077 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); |
4078 | do_deref (&lhsc); | |
4079 | if (flag_delete_null_pointer_checks | |
4080 | && integer_zerop (gimple_call_arg (t, 1))) | |
779704e7 | 4081 | { |
e38811ce RG |
4082 | ac.type = ADDRESSOF; |
4083 | ac.var = nothing_id; | |
779704e7 | 4084 | } |
e38811ce | 4085 | else |
25a6a873 | 4086 | { |
e38811ce RG |
4087 | ac.type = SCALAR; |
4088 | ac.var = integer_id; | |
25a6a873 | 4089 | } |
e38811ce RG |
4090 | ac.offset = 0; |
4091 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) | |
4092 | process_constraint (new_constraint (*lhsp, ac)); | |
4093 | VEC_free (ce_s, heap, lhsc); | |
4094 | return true; | |
4095 | } | |
d9048d16 JJ |
4096 | case BUILT_IN_ASSUME_ALIGNED: |
4097 | { | |
4098 | tree res = gimple_call_lhs (t); | |
4099 | tree dest = gimple_call_arg (t, 0); | |
4100 | if (res != NULL_TREE) | |
4101 | { | |
4102 | get_constraint_for (res, &lhsc); | |
4103 | get_constraint_for (dest, &rhsc); | |
4104 | process_all_all_constraints (lhsc, rhsc); | |
4105 | VEC_free (ce_s, heap, lhsc); | |
4106 | VEC_free (ce_s, heap, rhsc); | |
4107 | } | |
4108 | return true; | |
4109 | } | |
e38811ce RG |
4110 | /* All the following functions do not return pointers, do not |
4111 | modify the points-to sets of memory reachable from their | |
4112 | arguments and do not add to the ESCAPED solution. */ | |
4113 | case BUILT_IN_SINCOS: | |
4114 | case BUILT_IN_SINCOSF: | |
4115 | case BUILT_IN_SINCOSL: | |
4116 | case BUILT_IN_FREXP: | |
4117 | case BUILT_IN_FREXPF: | |
4118 | case BUILT_IN_FREXPL: | |
4119 | case BUILT_IN_GAMMA_R: | |
4120 | case BUILT_IN_GAMMAF_R: | |
4121 | case BUILT_IN_GAMMAL_R: | |
4122 | case BUILT_IN_LGAMMA_R: | |
4123 | case BUILT_IN_LGAMMAF_R: | |
4124 | case BUILT_IN_LGAMMAL_R: | |
4125 | case BUILT_IN_MODF: | |
4126 | case BUILT_IN_MODFF: | |
4127 | case BUILT_IN_MODFL: | |
4128 | case BUILT_IN_REMQUO: | |
4129 | case BUILT_IN_REMQUOF: | |
4130 | case BUILT_IN_REMQUOL: | |
4131 | case BUILT_IN_FREE: | |
4132 | return true; | |
4133 | /* Trampolines are special - they set up passing the static | |
4134 | frame. */ | |
4135 | case BUILT_IN_INIT_TRAMPOLINE: | |
4136 | { | |
4137 | tree tramp = gimple_call_arg (t, 0); | |
4138 | tree nfunc = gimple_call_arg (t, 1); | |
4139 | tree frame = gimple_call_arg (t, 2); | |
4140 | unsigned i; | |
4141 | struct constraint_expr lhs, *rhsp; | |
4142 | if (in_ipa_mode) | |
25a6a873 | 4143 | { |
e38811ce RG |
4144 | varinfo_t nfi = NULL; |
4145 | gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR); | |
4146 | nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0)); | |
4147 | if (nfi) | |
25a6a873 | 4148 | { |
e38811ce RG |
4149 | lhs = get_function_part_constraint (nfi, fi_static_chain); |
4150 | get_constraint_for (frame, &rhsc); | |
4151 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) | |
4152 | process_constraint (new_constraint (lhs, *rhsp)); | |
4153 | VEC_free (ce_s, heap, rhsc); | |
4154 | ||
4155 | /* Make the frame point to the function for | |
4156 | the trampoline adjustment call. */ | |
4157 | get_constraint_for (tramp, &lhsc); | |
4158 | do_deref (&lhsc); | |
4159 | get_constraint_for (nfunc, &rhsc); | |
25a6a873 RG |
4160 | process_all_all_constraints (lhsc, rhsc); |
4161 | VEC_free (ce_s, heap, rhsc); | |
4162 | VEC_free (ce_s, heap, lhsc); | |
e38811ce RG |
4163 | |
4164 | return true; | |
25a6a873 | 4165 | } |
25a6a873 | 4166 | } |
e38811ce RG |
4167 | /* Else fallthru to generic handling which will let |
4168 | the frame escape. */ | |
4169 | break; | |
4170 | } | |
4171 | case BUILT_IN_ADJUST_TRAMPOLINE: | |
4172 | { | |
4173 | tree tramp = gimple_call_arg (t, 0); | |
4174 | tree res = gimple_call_lhs (t); | |
4175 | if (in_ipa_mode && res) | |
25a6a873 | 4176 | { |
e38811ce RG |
4177 | get_constraint_for (res, &lhsc); |
4178 | get_constraint_for (tramp, &rhsc); | |
4179 | do_deref (&rhsc); | |
4180 | process_all_all_constraints (lhsc, rhsc); | |
4181 | VEC_free (ce_s, heap, rhsc); | |
4182 | VEC_free (ce_s, heap, lhsc); | |
25a6a873 | 4183 | } |
e38811ce RG |
4184 | return true; |
4185 | } | |
4186 | /* Variadic argument handling needs to be handled in IPA | |
4187 | mode as well. */ | |
4188 | case BUILT_IN_VA_START: | |
4189 | { | |
df2f6100 RG |
4190 | tree valist = gimple_call_arg (t, 0); |
4191 | struct constraint_expr rhs, *lhsp; | |
4192 | unsigned i; | |
4193 | get_constraint_for (valist, &lhsc); | |
4194 | do_deref (&lhsc); | |
4195 | /* The va_list gets access to pointers in variadic | |
4196 | arguments. Which we know in the case of IPA analysis | |
4197 | and otherwise are just all nonlocal variables. */ | |
e38811ce | 4198 | if (in_ipa_mode) |
a4c9bc15 | 4199 | { |
e38811ce | 4200 | fi = lookup_vi_for_tree (cfun->decl); |
e38811ce RG |
4201 | rhs = get_function_part_constraint (fi, ~0); |
4202 | rhs.type = ADDRESSOF; | |
a4c9bc15 | 4203 | } |
df2f6100 RG |
4204 | else |
4205 | { | |
4206 | rhs.var = nonlocal_id; | |
4207 | rhs.type = ADDRESSOF; | |
4208 | rhs.offset = 0; | |
4209 | } | |
4210 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) | |
4211 | process_constraint (new_constraint (*lhsp, rhs)); | |
4212 | VEC_free (ce_s, heap, lhsc); | |
4213 | /* va_list is clobbered. */ | |
4214 | make_constraint_to (get_call_clobber_vi (t)->id, valist); | |
4215 | return true; | |
e38811ce RG |
4216 | } |
4217 | /* va_end doesn't have any effect that matters. */ | |
4218 | case BUILT_IN_VA_END: | |
4219 | return true; | |
4220 | /* Alternate return. Simply give up for now. */ | |
4221 | case BUILT_IN_RETURN: | |
4ee00913 | 4222 | { |
e38811ce RG |
4223 | fi = NULL; |
4224 | if (!in_ipa_mode | |
4225 | || !(fi = get_vi_for_tree (cfun->decl))) | |
4226 | make_constraint_from (get_varinfo (escaped_id), anything_id); | |
4227 | else if (in_ipa_mode | |
4228 | && fi != NULL) | |
b7091901 | 4229 | { |
e38811ce RG |
4230 | struct constraint_expr lhs, rhs; |
4231 | lhs = get_function_part_constraint (fi, fi_result); | |
4232 | rhs.var = anything_id; | |
4233 | rhs.offset = 0; | |
4234 | rhs.type = SCALAR; | |
4235 | process_constraint (new_constraint (lhs, rhs)); | |
b7091901 | 4236 | } |
e38811ce RG |
4237 | return true; |
4238 | } | |
4239 | /* printf-style functions may have hooks to set pointers to | |
4240 | point to somewhere into the generated string. Leave them | |
4241 | for a later excercise... */ | |
4242 | default: | |
4243 | /* Fallthru to general call handling. */; | |
4244 | } | |
4245 | ||
4246 | return false; | |
4247 | } | |
4248 | ||
4249 | /* Create constraints for the call T. */ | |
4250 | ||
4251 | static void | |
4252 | find_func_aliases_for_call (gimple t) | |
4253 | { | |
4254 | tree fndecl = gimple_call_fndecl (t); | |
4255 | VEC(ce_s, heap) *lhsc = NULL; | |
4256 | VEC(ce_s, heap) *rhsc = NULL; | |
4257 | varinfo_t fi; | |
4258 | ||
4259 | if (fndecl != NULL_TREE | |
4260 | && DECL_BUILT_IN (fndecl) | |
4261 | && find_func_aliases_for_builtin_call (t)) | |
4262 | return; | |
4263 | ||
5c04e9f4 | 4264 | fi = get_fi_for_callee (t); |
e38811ce | 4265 | if (!in_ipa_mode |
5c04e9f4 | 4266 | || (fndecl && !fi->is_fn_info)) |
e38811ce RG |
4267 | { |
4268 | VEC(ce_s, heap) *rhsc = NULL; | |
4269 | int flags = gimple_call_flags (t); | |
4270 | ||
4271 | /* Const functions can return their arguments and addresses | |
4272 | of global memory but not of escaped memory. */ | |
4273 | if (flags & (ECF_CONST|ECF_NOVOPS)) | |
4274 | { | |
cb89b4b0 | 4275 | if (gimple_call_lhs (t)) |
e38811ce | 4276 | handle_const_call (t, &rhsc); |
4ee00913 | 4277 | } |
e38811ce RG |
4278 | /* Pure functions can return addresses in and of memory |
4279 | reachable from their arguments, but they are not an escape | |
4280 | point for reachable memory of their arguments. */ | |
4281 | else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE)) | |
4282 | handle_pure_call (t, &rhsc); | |
4ee00913 | 4283 | else |
e38811ce RG |
4284 | handle_rhs_call (t, &rhsc); |
4285 | if (gimple_call_lhs (t)) | |
4286 | handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl); | |
4287 | VEC_free (ce_s, heap, rhsc); | |
4288 | } | |
4289 | else | |
4290 | { | |
4291 | tree lhsop; | |
4292 | unsigned j; | |
6e7e772d | 4293 | |
e38811ce RG |
4294 | /* Assign all the passed arguments to the appropriate incoming |
4295 | parameters of the function. */ | |
4296 | for (j = 0; j < gimple_call_num_args (t); j++) | |
4297 | { | |
4298 | struct constraint_expr lhs ; | |
4299 | struct constraint_expr *rhsp; | |
4300 | tree arg = gimple_call_arg (t, j); | |
7b765bed | 4301 | |
e38811ce RG |
4302 | get_constraint_for_rhs (arg, &rhsc); |
4303 | lhs = get_function_part_constraint (fi, fi_parm_base + j); | |
4304 | while (VEC_length (ce_s, rhsc) != 0) | |
4ee00913 | 4305 | { |
e38811ce RG |
4306 | rhsp = VEC_last (ce_s, rhsc); |
4307 | process_constraint (new_constraint (lhs, *rhsp)); | |
4308 | VEC_pop (ce_s, rhsc); | |
4ee00913 | 4309 | } |
e38811ce RG |
4310 | } |
4311 | ||
4312 | /* If we are returning a value, assign it to the result. */ | |
4313 | lhsop = gimple_call_lhs (t); | |
4314 | if (lhsop) | |
4315 | { | |
4316 | struct constraint_expr rhs; | |
4317 | struct constraint_expr *lhsp; | |
25a6a873 | 4318 | |
e38811ce RG |
4319 | get_constraint_for (lhsop, &lhsc); |
4320 | rhs = get_function_part_constraint (fi, fi_result); | |
4321 | if (fndecl | |
25a6a873 RG |
4322 | && DECL_RESULT (fndecl) |
4323 | && DECL_BY_REFERENCE (DECL_RESULT (fndecl))) | |
4324 | { | |
e38811ce RG |
4325 | VEC(ce_s, heap) *tem = NULL; |
4326 | VEC_safe_push (ce_s, heap, tem, &rhs); | |
4327 | do_deref (&tem); | |
4328 | rhs = *VEC_index (ce_s, tem, 0); | |
4329 | VEC_free(ce_s, heap, tem); | |
25a6a873 | 4330 | } |
e38811ce | 4331 | FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp) |
5c04e9f4 | 4332 | process_constraint (new_constraint (*lhsp, rhs)); |
e38811ce | 4333 | } |
25a6a873 | 4334 | |
e38811ce RG |
4335 | /* If we pass the result decl by reference, honor that. */ |
4336 | if (lhsop | |
4337 | && fndecl | |
4338 | && DECL_RESULT (fndecl) | |
4339 | && DECL_BY_REFERENCE (DECL_RESULT (fndecl))) | |
4340 | { | |
4341 | struct constraint_expr lhs; | |
4342 | struct constraint_expr *rhsp; | |
4343 | ||
4344 | get_constraint_for_address_of (lhsop, &rhsc); | |
4345 | lhs = get_function_part_constraint (fi, fi_result); | |
4346 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) | |
5c04e9f4 | 4347 | process_constraint (new_constraint (lhs, *rhsp)); |
e38811ce RG |
4348 | VEC_free (ce_s, heap, rhsc); |
4349 | } | |
25a6a873 | 4350 | |
e38811ce RG |
4351 | /* If we use a static chain, pass it along. */ |
4352 | if (gimple_call_chain (t)) | |
4353 | { | |
4354 | struct constraint_expr lhs; | |
4355 | struct constraint_expr *rhsp; | |
4356 | ||
4357 | get_constraint_for (gimple_call_chain (t), &rhsc); | |
4358 | lhs = get_function_part_constraint (fi, fi_static_chain); | |
4359 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) | |
5c04e9f4 | 4360 | process_constraint (new_constraint (lhs, *rhsp)); |
e38811ce RG |
4361 | } |
4362 | } | |
4363 | } | |
4364 | ||
4365 | /* Walk statement T setting up aliasing constraints according to the | |
4366 | references found in T. This function is the main part of the | |
4367 | constraint builder. AI points to auxiliary alias information used | |
4368 | when building alias sets and computing alias grouping heuristics. */ | |
4369 | ||
4370 | static void | |
4371 | find_func_aliases (gimple origt) | |
4372 | { | |
4373 | gimple t = origt; | |
4374 | VEC(ce_s, heap) *lhsc = NULL; | |
4375 | VEC(ce_s, heap) *rhsc = NULL; | |
4376 | struct constraint_expr *c; | |
4377 | varinfo_t fi; | |
4378 | ||
4379 | /* Now build constraints expressions. */ | |
4380 | if (gimple_code (t) == GIMPLE_PHI) | |
4381 | { | |
4382 | size_t i; | |
4383 | unsigned int j; | |
4384 | ||
4385 | /* For a phi node, assign all the arguments to | |
4386 | the result. */ | |
4387 | get_constraint_for (gimple_phi_result (t), &lhsc); | |
4388 | for (i = 0; i < gimple_phi_num_args (t); i++) | |
4389 | { | |
4390 | tree strippedrhs = PHI_ARG_DEF (t, i); | |
4391 | ||
4392 | STRIP_NOPS (strippedrhs); | |
4393 | get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc); | |
4394 | ||
4395 | FOR_EACH_VEC_ELT (ce_s, lhsc, j, c) | |
4396 | { | |
4397 | struct constraint_expr *c2; | |
4398 | while (VEC_length (ce_s, rhsc) > 0) | |
4399 | { | |
4400 | c2 = VEC_last (ce_s, rhsc); | |
4401 | process_constraint (new_constraint (*c, *c2)); | |
4402 | VEC_pop (ce_s, rhsc); | |
4403 | } | |
25a6a873 | 4404 | } |
c58936b6 | 4405 | } |
e8ca4159 | 4406 | } |
e38811ce RG |
4407 | /* In IPA mode, we need to generate constraints to pass call |
4408 | arguments through their calls. There are two cases, | |
4409 | either a GIMPLE_CALL returning a value, or just a plain | |
4410 | GIMPLE_CALL when we are not. | |
4411 | ||
4412 | In non-ipa mode, we need to generate constraints for each | |
4413 | pointer passed by address. */ | |
4414 | else if (is_gimple_call (t)) | |
4415 | find_func_aliases_for_call (t); | |
4416 | ||
e5bae89b RG |
4417 | /* Otherwise, just a regular assignment statement. Only care about |
4418 | operations with pointer result, others are dealt with as escape | |
4419 | points if they have pointer operands. */ | |
0f8d6231 | 4420 | else if (is_gimple_assign (t)) |
e8ca4159 | 4421 | { |
726a989a RB |
4422 | /* Otherwise, just a regular assignment statement. */ |
4423 | tree lhsop = gimple_assign_lhs (t); | |
4424 | tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL; | |
e8ca4159 | 4425 | |
726a989a | 4426 | if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop))) |
e5bae89b | 4427 | do_structure_copy (lhsop, rhsop); |
e8ca4159 DN |
4428 | else |
4429 | { | |
194313e2 RG |
4430 | enum tree_code code = gimple_assign_rhs_code (t); |
4431 | ||
e5bae89b | 4432 | get_constraint_for (lhsop, &lhsc); |
726a989a | 4433 | |
194313e2 | 4434 | if (code == POINTER_PLUS_EXPR) |
726a989a RB |
4435 | get_constraint_for_ptr_offset (gimple_assign_rhs1 (t), |
4436 | gimple_assign_rhs2 (t), &rhsc); | |
194313e2 | 4437 | else if (code == BIT_AND_EXPR |
fca821b5 RG |
4438 | && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST) |
4439 | { | |
4440 | /* Aligning a pointer via a BIT_AND_EXPR is offsetting | |
4441 | the pointer. Handle it by offsetting it by UNKNOWN. */ | |
4442 | get_constraint_for_ptr_offset (gimple_assign_rhs1 (t), | |
4443 | NULL_TREE, &rhsc); | |
4444 | } | |
194313e2 | 4445 | else if ((CONVERT_EXPR_CODE_P (code) |
1961418e RG |
4446 | && !(POINTER_TYPE_P (gimple_expr_type (t)) |
4447 | && !POINTER_TYPE_P (TREE_TYPE (rhsop)))) | |
4448 | || gimple_assign_single_p (t)) | |
ed6c4831 | 4449 | get_constraint_for_rhs (rhsop, &rhsc); |
194313e2 RG |
4450 | else if (truth_value_p (code)) |
4451 | /* Truth value results are not pointer (parts). Or at least | |
4452 | very very unreasonable obfuscation of a part. */ | |
4453 | ; | |
726a989a RB |
4454 | else |
4455 | { | |
0f8d6231 RG |
4456 | /* All other operations are merges. */ |
4457 | VEC (ce_s, heap) *tmp = NULL; | |
4458 | struct constraint_expr *rhsp; | |
4459 | unsigned i, j; | |
4460 | get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc); | |
4461 | for (i = 2; i < gimple_num_ops (t); ++i) | |
4462 | { | |
4463 | get_constraint_for_rhs (gimple_op (t, i), &tmp); | |
4464 | FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp) | |
4465 | VEC_safe_push (ce_s, heap, rhsc, rhsp); | |
4466 | VEC_truncate (ce_s, tmp, 0); | |
4467 | } | |
4468 | VEC_free (ce_s, heap, tmp); | |
726a989a | 4469 | } |
779704e7 | 4470 | process_all_all_constraints (lhsc, rhsc); |
e8ca4159 | 4471 | } |
de70bb20 RG |
4472 | /* If there is a store to a global variable the rhs escapes. */ |
4473 | if ((lhsop = get_base_address (lhsop)) != NULL_TREE | |
4474 | && DECL_P (lhsop) | |
25a6a873 RG |
4475 | && is_global_var (lhsop) |
4476 | && (!in_ipa_mode | |
4477 | || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop))) | |
de70bb20 | 4478 | make_escape_constraint (rhsop); |
74d27244 RG |
4479 | /* If this is a conversion of a non-restrict pointer to a |
4480 | restrict pointer track it with a new heapvar. */ | |
4481 | else if (gimple_assign_cast_p (t) | |
4482 | && POINTER_TYPE_P (TREE_TYPE (rhsop)) | |
4483 | && POINTER_TYPE_P (TREE_TYPE (lhsop)) | |
4484 | && !TYPE_RESTRICT (TREE_TYPE (rhsop)) | |
4485 | && TYPE_RESTRICT (TREE_TYPE (lhsop))) | |
4486 | make_constraint_from_restrict (get_vi_for_tree (lhsop), | |
4487 | "CAST_RESTRICT"); | |
910fdc79 | 4488 | } |
14c41b9b RG |
4489 | /* Handle escapes through return. */ |
4490 | else if (gimple_code (t) == GIMPLE_RETURN | |
0f8d6231 | 4491 | && gimple_return_retval (t) != NULL_TREE) |
14c41b9b | 4492 | { |
25a6a873 RG |
4493 | fi = NULL; |
4494 | if (!in_ipa_mode | |
4495 | || !(fi = get_vi_for_tree (cfun->decl))) | |
4496 | make_escape_constraint (gimple_return_retval (t)); | |
4497 | else if (in_ipa_mode | |
4498 | && fi != NULL) | |
4499 | { | |
4500 | struct constraint_expr lhs ; | |
4501 | struct constraint_expr *rhsp; | |
4502 | unsigned i; | |
4503 | ||
4504 | lhs = get_function_part_constraint (fi, fi_result); | |
ed6c4831 | 4505 | get_constraint_for_rhs (gimple_return_retval (t), &rhsc); |
ac47786e | 4506 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4507 | process_constraint (new_constraint (lhs, *rhsp)); |
4508 | } | |
14c41b9b | 4509 | } |
2e407842 RG |
4510 | /* Handle asms conservatively by adding escape constraints to everything. */ |
4511 | else if (gimple_code (t) == GIMPLE_ASM) | |
b7091901 | 4512 | { |
5006671f RG |
4513 | unsigned i, noutputs; |
4514 | const char **oconstraints; | |
4515 | const char *constraint; | |
4516 | bool allows_mem, allows_reg, is_inout; | |
4517 | ||
4518 | noutputs = gimple_asm_noutputs (t); | |
4519 | oconstraints = XALLOCAVEC (const char *, noutputs); | |
4520 | ||
4521 | for (i = 0; i < noutputs; ++i) | |
b7091901 | 4522 | { |
5006671f RG |
4523 | tree link = gimple_asm_output_op (t, i); |
4524 | tree op = TREE_VALUE (link); | |
4525 | ||
4526 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
4527 | oconstraints[i] = constraint; | |
4528 | parse_output_constraint (&constraint, i, 0, 0, &allows_mem, | |
4529 | &allows_reg, &is_inout); | |
4530 | ||
4531 | /* A memory constraint makes the address of the operand escape. */ | |
4532 | if (!allows_reg && allows_mem) | |
4533 | make_escape_constraint (build_fold_addr_expr (op)); | |
4534 | ||
4535 | /* The asm may read global memory, so outputs may point to | |
4536 | any global memory. */ | |
0f8d6231 | 4537 | if (op) |
5006671f RG |
4538 | { |
4539 | VEC(ce_s, heap) *lhsc = NULL; | |
4540 | struct constraint_expr rhsc, *lhsp; | |
4541 | unsigned j; | |
4542 | get_constraint_for (op, &lhsc); | |
4543 | rhsc.var = nonlocal_id; | |
4544 | rhsc.offset = 0; | |
4545 | rhsc.type = SCALAR; | |
ac47786e | 4546 | FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp) |
5006671f RG |
4547 | process_constraint (new_constraint (*lhsp, rhsc)); |
4548 | VEC_free (ce_s, heap, lhsc); | |
4549 | } | |
b7091901 | 4550 | } |
726a989a | 4551 | for (i = 0; i < gimple_asm_ninputs (t); ++i) |
b7091901 | 4552 | { |
5006671f RG |
4553 | tree link = gimple_asm_input_op (t, i); |
4554 | tree op = TREE_VALUE (link); | |
4555 | ||
4556 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
4557 | ||
4558 | parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints, | |
4559 | &allows_mem, &allows_reg); | |
4560 | ||
4561 | /* A memory constraint makes the address of the operand escape. */ | |
4562 | if (!allows_reg && allows_mem) | |
4563 | make_escape_constraint (build_fold_addr_expr (op)); | |
4564 | /* Strictly we'd only need the constraint to ESCAPED if | |
3e8542ca RG |
4565 | the asm clobbers memory, otherwise using something |
4566 | along the lines of per-call clobbers/uses would be enough. */ | |
0f8d6231 | 4567 | else if (op) |
b7091901 RG |
4568 | make_escape_constraint (op); |
4569 | } | |
4570 | } | |
4571 | ||
4ee00913 DB |
4572 | VEC_free (ce_s, heap, rhsc); |
4573 | VEC_free (ce_s, heap, lhsc); | |
910fdc79 DB |
4574 | } |
4575 | ||
4576 | ||
25a6a873 RG |
4577 | /* Create a constraint adding to the clobber set of FI the memory |
4578 | pointed to by PTR. */ | |
4579 | ||
4580 | static void | |
4581 | process_ipa_clobber (varinfo_t fi, tree ptr) | |
4582 | { | |
4583 | VEC(ce_s, heap) *ptrc = NULL; | |
4584 | struct constraint_expr *c, lhs; | |
4585 | unsigned i; | |
ed6c4831 | 4586 | get_constraint_for_rhs (ptr, &ptrc); |
25a6a873 | 4587 | lhs = get_function_part_constraint (fi, fi_clobbers); |
ac47786e | 4588 | FOR_EACH_VEC_ELT (ce_s, ptrc, i, c) |
25a6a873 RG |
4589 | process_constraint (new_constraint (lhs, *c)); |
4590 | VEC_free (ce_s, heap, ptrc); | |
4591 | } | |
4592 | ||
4593 | /* Walk statement T setting up clobber and use constraints according to the | |
4594 | references found in T. This function is a main part of the | |
4595 | IPA constraint builder. */ | |
4596 | ||
4597 | static void | |
4598 | find_func_clobbers (gimple origt) | |
4599 | { | |
4600 | gimple t = origt; | |
4601 | VEC(ce_s, heap) *lhsc = NULL; | |
4602 | VEC(ce_s, heap) *rhsc = NULL; | |
4603 | varinfo_t fi; | |
4604 | ||
4605 | /* Add constraints for clobbered/used in IPA mode. | |
4606 | We are not interested in what automatic variables are clobbered | |
4607 | or used as we only use the information in the caller to which | |
4608 | they do not escape. */ | |
4609 | gcc_assert (in_ipa_mode); | |
4610 | ||
4611 | /* If the stmt refers to memory in any way it better had a VUSE. */ | |
4612 | if (gimple_vuse (t) == NULL_TREE) | |
4613 | return; | |
4614 | ||
4615 | /* We'd better have function information for the current function. */ | |
4616 | fi = lookup_vi_for_tree (cfun->decl); | |
4617 | gcc_assert (fi != NULL); | |
4618 | ||
4619 | /* Account for stores in assignments and calls. */ | |
4620 | if (gimple_vdef (t) != NULL_TREE | |
4621 | && gimple_has_lhs (t)) | |
4622 | { | |
4623 | tree lhs = gimple_get_lhs (t); | |
4624 | tree tem = lhs; | |
4625 | while (handled_component_p (tem)) | |
4626 | tem = TREE_OPERAND (tem, 0); | |
4627 | if ((DECL_P (tem) | |
4628 | && !auto_var_in_fn_p (tem, cfun->decl)) | |
70f34814 RG |
4629 | || INDIRECT_REF_P (tem) |
4630 | || (TREE_CODE (tem) == MEM_REF | |
4631 | && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR | |
4632 | && auto_var_in_fn_p | |
4633 | (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl)))) | |
25a6a873 RG |
4634 | { |
4635 | struct constraint_expr lhsc, *rhsp; | |
4636 | unsigned i; | |
4637 | lhsc = get_function_part_constraint (fi, fi_clobbers); | |
4638 | get_constraint_for_address_of (lhs, &rhsc); | |
ac47786e | 4639 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4640 | process_constraint (new_constraint (lhsc, *rhsp)); |
4641 | VEC_free (ce_s, heap, rhsc); | |
4642 | } | |
4643 | } | |
4644 | ||
4645 | /* Account for uses in assigments and returns. */ | |
4646 | if (gimple_assign_single_p (t) | |
4647 | || (gimple_code (t) == GIMPLE_RETURN | |
4648 | && gimple_return_retval (t) != NULL_TREE)) | |
4649 | { | |
4650 | tree rhs = (gimple_assign_single_p (t) | |
4651 | ? gimple_assign_rhs1 (t) : gimple_return_retval (t)); | |
4652 | tree tem = rhs; | |
4653 | while (handled_component_p (tem)) | |
4654 | tem = TREE_OPERAND (tem, 0); | |
4655 | if ((DECL_P (tem) | |
4656 | && !auto_var_in_fn_p (tem, cfun->decl)) | |
70f34814 RG |
4657 | || INDIRECT_REF_P (tem) |
4658 | || (TREE_CODE (tem) == MEM_REF | |
4659 | && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR | |
4660 | && auto_var_in_fn_p | |
4661 | (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl)))) | |
25a6a873 RG |
4662 | { |
4663 | struct constraint_expr lhs, *rhsp; | |
4664 | unsigned i; | |
4665 | lhs = get_function_part_constraint (fi, fi_uses); | |
4666 | get_constraint_for_address_of (rhs, &rhsc); | |
ac47786e | 4667 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4668 | process_constraint (new_constraint (lhs, *rhsp)); |
4669 | VEC_free (ce_s, heap, rhsc); | |
4670 | } | |
4671 | } | |
4672 | ||
4673 | if (is_gimple_call (t)) | |
4674 | { | |
4675 | varinfo_t cfi = NULL; | |
4676 | tree decl = gimple_call_fndecl (t); | |
4677 | struct constraint_expr lhs, rhs; | |
4678 | unsigned i, j; | |
4679 | ||
4680 | /* For builtins we do not have separate function info. For those | |
4681 | we do not generate escapes for we have to generate clobbers/uses. */ | |
4682 | if (decl | |
4683 | && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) | |
4684 | switch (DECL_FUNCTION_CODE (decl)) | |
4685 | { | |
4686 | /* The following functions use and clobber memory pointed to | |
4687 | by their arguments. */ | |
4688 | case BUILT_IN_STRCPY: | |
4689 | case BUILT_IN_STRNCPY: | |
4690 | case BUILT_IN_BCOPY: | |
4691 | case BUILT_IN_MEMCPY: | |
4692 | case BUILT_IN_MEMMOVE: | |
4693 | case BUILT_IN_MEMPCPY: | |
4694 | case BUILT_IN_STPCPY: | |
4695 | case BUILT_IN_STPNCPY: | |
4696 | case BUILT_IN_STRCAT: | |
4697 | case BUILT_IN_STRNCAT: | |
36dc1a88 JJ |
4698 | case BUILT_IN_STRCPY_CHK: |
4699 | case BUILT_IN_STRNCPY_CHK: | |
4700 | case BUILT_IN_MEMCPY_CHK: | |
4701 | case BUILT_IN_MEMMOVE_CHK: | |
4702 | case BUILT_IN_MEMPCPY_CHK: | |
4703 | case BUILT_IN_STPCPY_CHK: | |
4704 | case BUILT_IN_STRCAT_CHK: | |
4705 | case BUILT_IN_STRNCAT_CHK: | |
25a6a873 RG |
4706 | { |
4707 | tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl) | |
4708 | == BUILT_IN_BCOPY ? 1 : 0)); | |
4709 | tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl) | |
4710 | == BUILT_IN_BCOPY ? 0 : 1)); | |
4711 | unsigned i; | |
4712 | struct constraint_expr *rhsp, *lhsp; | |
4713 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); | |
4714 | lhs = get_function_part_constraint (fi, fi_clobbers); | |
ac47786e | 4715 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
25a6a873 RG |
4716 | process_constraint (new_constraint (lhs, *lhsp)); |
4717 | VEC_free (ce_s, heap, lhsc); | |
4718 | get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc); | |
4719 | lhs = get_function_part_constraint (fi, fi_uses); | |
ac47786e | 4720 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4721 | process_constraint (new_constraint (lhs, *rhsp)); |
4722 | VEC_free (ce_s, heap, rhsc); | |
4723 | return; | |
4724 | } | |
4725 | /* The following function clobbers memory pointed to by | |
4726 | its argument. */ | |
4727 | case BUILT_IN_MEMSET: | |
36dc1a88 | 4728 | case BUILT_IN_MEMSET_CHK: |
25a6a873 RG |
4729 | { |
4730 | tree dest = gimple_call_arg (t, 0); | |
4731 | unsigned i; | |
4732 | ce_s *lhsp; | |
4733 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); | |
4734 | lhs = get_function_part_constraint (fi, fi_clobbers); | |
ac47786e | 4735 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
25a6a873 RG |
4736 | process_constraint (new_constraint (lhs, *lhsp)); |
4737 | VEC_free (ce_s, heap, lhsc); | |
4738 | return; | |
4739 | } | |
4740 | /* The following functions clobber their second and third | |
4741 | arguments. */ | |
4742 | case BUILT_IN_SINCOS: | |
4743 | case BUILT_IN_SINCOSF: | |
4744 | case BUILT_IN_SINCOSL: | |
4745 | { | |
4746 | process_ipa_clobber (fi, gimple_call_arg (t, 1)); | |
4747 | process_ipa_clobber (fi, gimple_call_arg (t, 2)); | |
4748 | return; | |
4749 | } | |
4750 | /* The following functions clobber their second argument. */ | |
4751 | case BUILT_IN_FREXP: | |
4752 | case BUILT_IN_FREXPF: | |
4753 | case BUILT_IN_FREXPL: | |
4754 | case BUILT_IN_LGAMMA_R: | |
4755 | case BUILT_IN_LGAMMAF_R: | |
4756 | case BUILT_IN_LGAMMAL_R: | |
4757 | case BUILT_IN_GAMMA_R: | |
4758 | case BUILT_IN_GAMMAF_R: | |
4759 | case BUILT_IN_GAMMAL_R: | |
4760 | case BUILT_IN_MODF: | |
4761 | case BUILT_IN_MODFF: | |
4762 | case BUILT_IN_MODFL: | |
4763 | { | |
4764 | process_ipa_clobber (fi, gimple_call_arg (t, 1)); | |
4765 | return; | |
4766 | } | |
4767 | /* The following functions clobber their third argument. */ | |
4768 | case BUILT_IN_REMQUO: | |
4769 | case BUILT_IN_REMQUOF: | |
4770 | case BUILT_IN_REMQUOL: | |
4771 | { | |
4772 | process_ipa_clobber (fi, gimple_call_arg (t, 2)); | |
4773 | return; | |
4774 | } | |
4775 | /* The following functions neither read nor clobber memory. */ | |
45d439ac | 4776 | case BUILT_IN_ASSUME_ALIGNED: |
25a6a873 RG |
4777 | case BUILT_IN_FREE: |
4778 | return; | |
4779 | /* Trampolines are of no interest to us. */ | |
4780 | case BUILT_IN_INIT_TRAMPOLINE: | |
4781 | case BUILT_IN_ADJUST_TRAMPOLINE: | |
4782 | return; | |
4783 | case BUILT_IN_VA_START: | |
4784 | case BUILT_IN_VA_END: | |
4785 | return; | |
4786 | /* printf-style functions may have hooks to set pointers to | |
4787 | point to somewhere into the generated string. Leave them | |
4788 | for a later excercise... */ | |
4789 | default: | |
4790 | /* Fallthru to general call handling. */; | |
4791 | } | |
4792 | ||
4793 | /* Parameters passed by value are used. */ | |
4794 | lhs = get_function_part_constraint (fi, fi_uses); | |
4795 | for (i = 0; i < gimple_call_num_args (t); i++) | |
4796 | { | |
4797 | struct constraint_expr *rhsp; | |
4798 | tree arg = gimple_call_arg (t, i); | |
4799 | ||
4800 | if (TREE_CODE (arg) == SSA_NAME | |
4801 | || is_gimple_min_invariant (arg)) | |
4802 | continue; | |
4803 | ||
4804 | get_constraint_for_address_of (arg, &rhsc); | |
ac47786e | 4805 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) |
25a6a873 RG |
4806 | process_constraint (new_constraint (lhs, *rhsp)); |
4807 | VEC_free (ce_s, heap, rhsc); | |
4808 | } | |
4809 | ||
4810 | /* Build constraints for propagating clobbers/uses along the | |
4811 | callgraph edges. */ | |
4812 | cfi = get_fi_for_callee (t); | |
4813 | if (cfi->id == anything_id) | |
4814 | { | |
4815 | if (gimple_vdef (t)) | |
4816 | make_constraint_from (first_vi_for_offset (fi, fi_clobbers), | |
4817 | anything_id); | |
4818 | make_constraint_from (first_vi_for_offset (fi, fi_uses), | |
4819 | anything_id); | |
4820 | return; | |
4821 | } | |
4822 | ||
4823 | /* For callees without function info (that's external functions), | |
4824 | ESCAPED is clobbered and used. */ | |
4825 | if (gimple_call_fndecl (t) | |
4826 | && !cfi->is_fn_info) | |
4827 | { | |
4828 | varinfo_t vi; | |
4829 | ||
4830 | if (gimple_vdef (t)) | |
4831 | make_copy_constraint (first_vi_for_offset (fi, fi_clobbers), | |
4832 | escaped_id); | |
4833 | make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id); | |
4834 | ||
4835 | /* Also honor the call statement use/clobber info. */ | |
4836 | if ((vi = lookup_call_clobber_vi (t)) != NULL) | |
4837 | make_copy_constraint (first_vi_for_offset (fi, fi_clobbers), | |
4838 | vi->id); | |
4839 | if ((vi = lookup_call_use_vi (t)) != NULL) | |
4840 | make_copy_constraint (first_vi_for_offset (fi, fi_uses), | |
4841 | vi->id); | |
4842 | return; | |
4843 | } | |
4844 | ||
4845 | /* Otherwise the caller clobbers and uses what the callee does. | |
4846 | ??? This should use a new complex constraint that filters | |
4847 | local variables of the callee. */ | |
4848 | if (gimple_vdef (t)) | |
4849 | { | |
4850 | lhs = get_function_part_constraint (fi, fi_clobbers); | |
4851 | rhs = get_function_part_constraint (cfi, fi_clobbers); | |
4852 | process_constraint (new_constraint (lhs, rhs)); | |
4853 | } | |
4854 | lhs = get_function_part_constraint (fi, fi_uses); | |
4855 | rhs = get_function_part_constraint (cfi, fi_uses); | |
4856 | process_constraint (new_constraint (lhs, rhs)); | |
4857 | } | |
4858 | else if (gimple_code (t) == GIMPLE_ASM) | |
4859 | { | |
4860 | /* ??? Ick. We can do better. */ | |
4861 | if (gimple_vdef (t)) | |
4862 | make_constraint_from (first_vi_for_offset (fi, fi_clobbers), | |
4863 | anything_id); | |
4864 | make_constraint_from (first_vi_for_offset (fi, fi_uses), | |
4865 | anything_id); | |
4866 | } | |
4867 | ||
4868 | VEC_free (ce_s, heap, rhsc); | |
4869 | } | |
4870 | ||
4871 | ||
910fdc79 | 4872 | /* Find the first varinfo in the same variable as START that overlaps with |
5006671f | 4873 | OFFSET. Return NULL if we can't find one. */ |
910fdc79 | 4874 | |
c58936b6 | 4875 | static varinfo_t |
910fdc79 DB |
4876 | first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset) |
4877 | { | |
5006671f RG |
4878 | /* If the offset is outside of the variable, bail out. */ |
4879 | if (offset >= start->fullsize) | |
4880 | return NULL; | |
4881 | ||
4882 | /* If we cannot reach offset from start, lookup the first field | |
4883 | and start from there. */ | |
4884 | if (start->offset > offset) | |
4885 | start = lookup_vi_for_tree (start->decl); | |
4886 | ||
4887 | while (start) | |
910fdc79 DB |
4888 | { |
4889 | /* We may not find a variable in the field list with the actual | |
4890 | offset when when we have glommed a structure to a variable. | |
4891 | In that case, however, offset should still be within the size | |
4892 | of the variable. */ | |
5006671f | 4893 | if (offset >= start->offset |
de925a03 | 4894 | && (offset - start->offset) < start->size) |
5006671f RG |
4895 | return start; |
4896 | ||
4897 | start= start->next; | |
910fdc79 | 4898 | } |
5006671f | 4899 | |
8971094d | 4900 | return NULL; |
910fdc79 DB |
4901 | } |
4902 | ||
5006671f RG |
4903 | /* Find the first varinfo in the same variable as START that overlaps with |
4904 | OFFSET. If there is no such varinfo the varinfo directly preceding | |
4905 | OFFSET is returned. */ | |
4906 | ||
4907 | static varinfo_t | |
4908 | first_or_preceding_vi_for_offset (varinfo_t start, | |
4909 | unsigned HOST_WIDE_INT offset) | |
4910 | { | |
4911 | /* If we cannot reach offset from start, lookup the first field | |
4912 | and start from there. */ | |
4913 | if (start->offset > offset) | |
4914 | start = lookup_vi_for_tree (start->decl); | |
4915 | ||
4916 | /* We may not find a variable in the field list with the actual | |
4917 | offset when when we have glommed a structure to a variable. | |
4918 | In that case, however, offset should still be within the size | |
4919 | of the variable. | |
4920 | If we got beyond the offset we look for return the field | |
4921 | directly preceding offset which may be the last field. */ | |
4922 | while (start->next | |
4923 | && offset >= start->offset | |
de925a03 | 4924 | && !((offset - start->offset) < start->size)) |
5006671f RG |
4925 | start = start->next; |
4926 | ||
4927 | return start; | |
4928 | } | |
4929 | ||
910fdc79 | 4930 | |
31de5b77 RG |
4931 | /* This structure is used during pushing fields onto the fieldstack |
4932 | to track the offset of the field, since bitpos_of_field gives it | |
4933 | relative to its immediate containing type, and we want it relative | |
4934 | to the ultimate containing object. */ | |
4935 | ||
4936 | struct fieldoff | |
4937 | { | |
ee7d4b57 RG |
4938 | /* Offset from the base of the base containing object to this field. */ |
4939 | HOST_WIDE_INT offset; | |
31de5b77 RG |
4940 | |
4941 | /* Size, in bits, of the field. */ | |
ee7d4b57 | 4942 | unsigned HOST_WIDE_INT size; |
31de5b77 | 4943 | |
ee7d4b57 | 4944 | unsigned has_unknown_size : 1; |
31de5b77 | 4945 | |
0f8d6231 RG |
4946 | unsigned must_have_pointers : 1; |
4947 | ||
ee7d4b57 | 4948 | unsigned may_have_pointers : 1; |
74d27244 RG |
4949 | |
4950 | unsigned only_restrict_pointers : 1; | |
31de5b77 RG |
4951 | }; |
4952 | typedef struct fieldoff fieldoff_s; | |
4953 | ||
4954 | DEF_VEC_O(fieldoff_s); | |
4955 | DEF_VEC_ALLOC_O(fieldoff_s,heap); | |
4956 | ||
910fdc79 DB |
4957 | /* qsort comparison function for two fieldoff's PA and PB */ |
4958 | ||
c58936b6 | 4959 | static int |
910fdc79 DB |
4960 | fieldoff_compare (const void *pa, const void *pb) |
4961 | { | |
4962 | const fieldoff_s *foa = (const fieldoff_s *)pa; | |
4963 | const fieldoff_s *fob = (const fieldoff_s *)pb; | |
185ab3b6 | 4964 | unsigned HOST_WIDE_INT foasize, fobsize; |
c58936b6 | 4965 | |
185ab3b6 RG |
4966 | if (foa->offset < fob->offset) |
4967 | return -1; | |
4968 | else if (foa->offset > fob->offset) | |
4969 | return 1; | |
910fdc79 | 4970 | |
ee7d4b57 RG |
4971 | foasize = foa->size; |
4972 | fobsize = fob->size; | |
185ab3b6 | 4973 | if (foasize < fobsize) |
ee7d4b57 | 4974 | return -1; |
185ab3b6 RG |
4975 | else if (foasize > fobsize) |
4976 | return 1; | |
4977 | return 0; | |
910fdc79 DB |
4978 | } |
4979 | ||
4980 | /* Sort a fieldstack according to the field offset and sizes. */ | |
31de5b77 | 4981 | static void |
83f676b3 | 4982 | sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack) |
910fdc79 | 4983 | { |
5095da95 | 4984 | VEC_qsort (fieldoff_s, fieldstack, fieldoff_compare); |
910fdc79 DB |
4985 | } |
4986 | ||
31de5b77 RG |
4987 | /* Return true if V is a tree that we can have subvars for. |
4988 | Normally, this is any aggregate type. Also complex | |
4989 | types which are not gimple registers can have subvars. */ | |
4990 | ||
4991 | static inline bool | |
4992 | var_can_have_subvars (const_tree v) | |
4993 | { | |
4994 | /* Volatile variables should never have subvars. */ | |
4995 | if (TREE_THIS_VOLATILE (v)) | |
4996 | return false; | |
4997 | ||
4998 | /* Non decls or memory tags can never have subvars. */ | |
5006671f | 4999 | if (!DECL_P (v)) |
31de5b77 RG |
5000 | return false; |
5001 | ||
5002 | /* Aggregates without overlapping fields can have subvars. */ | |
5003 | if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE) | |
5004 | return true; | |
5005 | ||
5006 | return false; | |
5007 | } | |
5008 | ||
0f8d6231 RG |
5009 | /* Return true if T is a type that does contain pointers. */ |
5010 | ||
5011 | static bool | |
5012 | type_must_have_pointers (tree type) | |
5013 | { | |
5014 | if (POINTER_TYPE_P (type)) | |
5015 | return true; | |
5016 | ||
5017 | if (TREE_CODE (type) == ARRAY_TYPE) | |
5018 | return type_must_have_pointers (TREE_TYPE (type)); | |
5019 | ||
5020 | /* A function or method can have pointers as arguments, so track | |
5021 | those separately. */ | |
5022 | if (TREE_CODE (type) == FUNCTION_TYPE | |
5023 | || TREE_CODE (type) == METHOD_TYPE) | |
5024 | return true; | |
5025 | ||
5026 | return false; | |
5027 | } | |
5028 | ||
5029 | static bool | |
5030 | field_must_have_pointers (tree t) | |
5031 | { | |
5032 | return type_must_have_pointers (TREE_TYPE (t)); | |
5033 | } | |
5034 | ||
d7705551 DN |
5035 | /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all |
5036 | the fields of TYPE onto fieldstack, recording their offsets along | |
5037 | the way. | |
5038 | ||
5039 | OFFSET is used to keep track of the offset in this entire | |
5040 | structure, rather than just the immediately containing structure. | |
18abb35e RG |
5041 | Returns false if the caller is supposed to handle the field we |
5042 | recursed for. */ | |
910fdc79 | 5043 | |
18abb35e | 5044 | static bool |
c58936b6 | 5045 | push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack, |
0f8d6231 | 5046 | HOST_WIDE_INT offset) |
910fdc79 DB |
5047 | { |
5048 | tree field; | |
18abb35e | 5049 | bool empty_p = true; |
31de5b77 RG |
5050 | |
5051 | if (TREE_CODE (type) != RECORD_TYPE) | |
18abb35e | 5052 | return false; |
3fe2f42a RG |
5053 | |
5054 | /* If the vector of fields is growing too big, bail out early. | |
5055 | Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make | |
5056 | sure this fails. */ | |
31de5b77 | 5057 | if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE) |
18abb35e | 5058 | return false; |
c58936b6 | 5059 | |
910ad8de | 5060 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
31de5b77 RG |
5061 | if (TREE_CODE (field) == FIELD_DECL) |
5062 | { | |
5063 | bool push = false; | |
ee7d4b57 | 5064 | HOST_WIDE_INT foff = bitpos_of_field (field); |
31de5b77 | 5065 | |
ee7d4b57 RG |
5066 | if (!var_can_have_subvars (field) |
5067 | || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE | |
5068 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
31de5b77 | 5069 | push = true; |
18abb35e | 5070 | else if (!push_fields_onto_fieldstack |
0f8d6231 | 5071 | (TREE_TYPE (field), fieldstack, offset + foff) |
31de5b77 RG |
5072 | && (DECL_SIZE (field) |
5073 | && !integer_zerop (DECL_SIZE (field)))) | |
5074 | /* Empty structures may have actual size, like in C++. So | |
5075 | see if we didn't push any subfields and the size is | |
5076 | nonzero, push the field onto the stack. */ | |
5077 | push = true; | |
5078 | ||
5079 | if (push) | |
910fdc79 | 5080 | { |
ee7d4b57 RG |
5081 | fieldoff_s *pair = NULL; |
5082 | bool has_unknown_size = false; | |
0f8d6231 | 5083 | bool must_have_pointers_p; |
ee7d4b57 RG |
5084 | |
5085 | if (!VEC_empty (fieldoff_s, *fieldstack)) | |
5086 | pair = VEC_last (fieldoff_s, *fieldstack); | |
5087 | ||
3fd3b156 RG |
5088 | /* If there isn't anything at offset zero, create sth. */ |
5089 | if (!pair | |
5090 | && offset + foff != 0) | |
5091 | { | |
5092 | pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL); | |
5093 | pair->offset = 0; | |
5094 | pair->size = offset + foff; | |
5095 | pair->has_unknown_size = false; | |
5096 | pair->must_have_pointers = false; | |
5097 | pair->may_have_pointers = false; | |
5098 | pair->only_restrict_pointers = false; | |
5099 | } | |
5100 | ||
ee7d4b57 RG |
5101 | if (!DECL_SIZE (field) |
5102 | || !host_integerp (DECL_SIZE (field), 1)) | |
5103 | has_unknown_size = true; | |
5104 | ||
5105 | /* If adjacent fields do not contain pointers merge them. */ | |
0f8d6231 | 5106 | must_have_pointers_p = field_must_have_pointers (field); |
ee7d4b57 | 5107 | if (pair |
ee7d4b57 | 5108 | && !has_unknown_size |
a81b065a | 5109 | && !must_have_pointers_p |
0f8d6231 RG |
5110 | && !pair->must_have_pointers |
5111 | && !pair->has_unknown_size | |
5112 | && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff) | |
ee7d4b57 | 5113 | { |
ee7d4b57 RG |
5114 | pair->size += TREE_INT_CST_LOW (DECL_SIZE (field)); |
5115 | } | |
5116 | else | |
5117 | { | |
5118 | pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL); | |
5119 | pair->offset = offset + foff; | |
5120 | pair->has_unknown_size = has_unknown_size; | |
5121 | if (!has_unknown_size) | |
5122 | pair->size = TREE_INT_CST_LOW (DECL_SIZE (field)); | |
5123 | else | |
5124 | pair->size = -1; | |
0f8d6231 RG |
5125 | pair->must_have_pointers = must_have_pointers_p; |
5126 | pair->may_have_pointers = true; | |
74d27244 RG |
5127 | pair->only_restrict_pointers |
5128 | = (!has_unknown_size | |
5129 | && POINTER_TYPE_P (TREE_TYPE (field)) | |
5130 | && TYPE_RESTRICT (TREE_TYPE (field))); | |
ee7d4b57 | 5131 | } |
31de5b77 | 5132 | } |
18abb35e RG |
5133 | |
5134 | empty_p = false; | |
31de5b77 | 5135 | } |
910fdc79 | 5136 | |
18abb35e | 5137 | return !empty_p; |
910fdc79 DB |
5138 | } |
5139 | ||
5006671f RG |
5140 | /* Count the number of arguments DECL has, and set IS_VARARGS to true |
5141 | if it is a varargs function. */ | |
5142 | ||
5143 | static unsigned int | |
5144 | count_num_arguments (tree decl, bool *is_varargs) | |
5145 | { | |
de925a03 | 5146 | unsigned int num = 0; |
5006671f RG |
5147 | tree t; |
5148 | ||
de925a03 RG |
5149 | /* Capture named arguments for K&R functions. They do not |
5150 | have a prototype and thus no TYPE_ARG_TYPES. */ | |
910ad8de | 5151 | for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t)) |
de925a03 | 5152 | ++num; |
c58936b6 | 5153 | |
de925a03 RG |
5154 | /* Check if the function has variadic arguments. */ |
5155 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) | |
5156 | if (TREE_VALUE (t) == void_type_node) | |
5157 | break; | |
4ee00913 DB |
5158 | if (!t) |
5159 | *is_varargs = true; | |
de925a03 RG |
5160 | |
5161 | return num; | |
4ee00913 DB |
5162 | } |
5163 | ||
5164 | /* Creation function node for DECL, using NAME, and return the index | |
5165 | of the variable we've created for the function. */ | |
5166 | ||
27c2cfa6 | 5167 | static varinfo_t |
4ee00913 DB |
5168 | create_function_info_for (tree decl, const char *name) |
5169 | { | |
25a6a873 RG |
5170 | struct function *fn = DECL_STRUCT_FUNCTION (decl); |
5171 | varinfo_t vi, prev_vi; | |
c58936b6 | 5172 | tree arg; |
4ee00913 DB |
5173 | unsigned int i; |
5174 | bool is_varargs = false; | |
25a6a873 | 5175 | unsigned int num_args = count_num_arguments (decl, &is_varargs); |
4ee00913 DB |
5176 | |
5177 | /* Create the variable info. */ | |
5178 | ||
0bbf2ffa | 5179 | vi = new_var_info (decl, name); |
4ee00913 | 5180 | vi->offset = 0; |
4ee00913 | 5181 | vi->size = 1; |
25a6a873 RG |
5182 | vi->fullsize = fi_parm_base + num_args; |
5183 | vi->is_fn_info = 1; | |
5184 | vi->may_have_pointers = false; | |
5185 | if (is_varargs) | |
5186 | vi->fullsize = ~0; | |
3e5937d7 | 5187 | insert_vi_for_tree (vi->decl, vi); |
4ee00913 | 5188 | |
25a6a873 RG |
5189 | prev_vi = vi; |
5190 | ||
5191 | /* Create a variable for things the function clobbers and one for | |
5192 | things the function uses. */ | |
4ee00913 | 5193 | { |
25a6a873 RG |
5194 | varinfo_t clobbervi, usevi; |
5195 | const char *newname; | |
5196 | char *tempname; | |
5197 | ||
5198 | asprintf (&tempname, "%s.clobber", name); | |
5199 | newname = ggc_strdup (tempname); | |
5200 | free (tempname); | |
5201 | ||
5202 | clobbervi = new_var_info (NULL, newname); | |
5203 | clobbervi->offset = fi_clobbers; | |
5204 | clobbervi->size = 1; | |
5205 | clobbervi->fullsize = vi->fullsize; | |
5206 | clobbervi->is_full_var = true; | |
5207 | clobbervi->is_global_var = false; | |
5208 | gcc_assert (prev_vi->offset < clobbervi->offset); | |
5209 | prev_vi->next = clobbervi; | |
5210 | prev_vi = clobbervi; | |
25a6a873 RG |
5211 | |
5212 | asprintf (&tempname, "%s.use", name); | |
5213 | newname = ggc_strdup (tempname); | |
5214 | free (tempname); | |
5215 | ||
5216 | usevi = new_var_info (NULL, newname); | |
5217 | usevi->offset = fi_uses; | |
5218 | usevi->size = 1; | |
5219 | usevi->fullsize = vi->fullsize; | |
5220 | usevi->is_full_var = true; | |
5221 | usevi->is_global_var = false; | |
5222 | gcc_assert (prev_vi->offset < usevi->offset); | |
5223 | prev_vi->next = usevi; | |
5224 | prev_vi = usevi; | |
4ee00913 DB |
5225 | } |
5226 | ||
25a6a873 RG |
5227 | /* And one for the static chain. */ |
5228 | if (fn->static_chain_decl != NULL_TREE) | |
5229 | { | |
5230 | varinfo_t chainvi; | |
5231 | const char *newname; | |
5232 | char *tempname; | |
5233 | ||
5234 | asprintf (&tempname, "%s.chain", name); | |
5235 | newname = ggc_strdup (tempname); | |
5236 | free (tempname); | |
5237 | ||
5238 | chainvi = new_var_info (fn->static_chain_decl, newname); | |
5239 | chainvi->offset = fi_static_chain; | |
5240 | chainvi->size = 1; | |
5241 | chainvi->fullsize = vi->fullsize; | |
5242 | chainvi->is_full_var = true; | |
5243 | chainvi->is_global_var = false; | |
5244 | gcc_assert (prev_vi->offset < chainvi->offset); | |
5245 | prev_vi->next = chainvi; | |
5246 | prev_vi = chainvi; | |
25a6a873 RG |
5247 | insert_vi_for_tree (fn->static_chain_decl, chainvi); |
5248 | } | |
5249 | ||
5250 | /* Create a variable for the return var. */ | |
5251 | if (DECL_RESULT (decl) != NULL | |
5252 | || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) | |
5253 | { | |
5254 | varinfo_t resultvi; | |
5255 | const char *newname; | |
5256 | char *tempname; | |
5257 | tree resultdecl = decl; | |
5258 | ||
5259 | if (DECL_RESULT (decl)) | |
5260 | resultdecl = DECL_RESULT (decl); | |
5261 | ||
5262 | asprintf (&tempname, "%s.result", name); | |
5263 | newname = ggc_strdup (tempname); | |
5264 | free (tempname); | |
5265 | ||
5266 | resultvi = new_var_info (resultdecl, newname); | |
5267 | resultvi->offset = fi_result; | |
5268 | resultvi->size = 1; | |
5269 | resultvi->fullsize = vi->fullsize; | |
5270 | resultvi->is_full_var = true; | |
5271 | if (DECL_RESULT (decl)) | |
0f8d6231 | 5272 | resultvi->may_have_pointers = true; |
25a6a873 RG |
5273 | gcc_assert (prev_vi->offset < resultvi->offset); |
5274 | prev_vi->next = resultvi; | |
5275 | prev_vi = resultvi; | |
25a6a873 RG |
5276 | if (DECL_RESULT (decl)) |
5277 | insert_vi_for_tree (DECL_RESULT (decl), resultvi); | |
5278 | } | |
4ee00913 | 5279 | |
6416ae7f | 5280 | /* Set up variables for each argument. */ |
25a6a873 RG |
5281 | arg = DECL_ARGUMENTS (decl); |
5282 | for (i = 0; i < num_args; i++) | |
c58936b6 | 5283 | { |
4ee00913 DB |
5284 | varinfo_t argvi; |
5285 | const char *newname; | |
5286 | char *tempname; | |
4ee00913 DB |
5287 | tree argdecl = decl; |
5288 | ||
5289 | if (arg) | |
5290 | argdecl = arg; | |
c58936b6 | 5291 | |
25a6a873 | 5292 | asprintf (&tempname, "%s.arg%d", name, i); |
4ee00913 DB |
5293 | newname = ggc_strdup (tempname); |
5294 | free (tempname); | |
5295 | ||
0bbf2ffa | 5296 | argvi = new_var_info (argdecl, newname); |
25a6a873 | 5297 | argvi->offset = fi_parm_base + i; |
4ee00913 | 5298 | argvi->size = 1; |
e5bae89b | 5299 | argvi->is_full_var = true; |
4ee00913 | 5300 | argvi->fullsize = vi->fullsize; |
25a6a873 | 5301 | if (arg) |
0f8d6231 | 5302 | argvi->may_have_pointers = true; |
25a6a873 RG |
5303 | gcc_assert (prev_vi->offset < argvi->offset); |
5304 | prev_vi->next = argvi; | |
5305 | prev_vi = argvi; | |
4ee00913 DB |
5306 | if (arg) |
5307 | { | |
3e5937d7 | 5308 | insert_vi_for_tree (arg, argvi); |
910ad8de | 5309 | arg = DECL_CHAIN (arg); |
4ee00913 DB |
5310 | } |
5311 | } | |
4cf4d6a3 | 5312 | |
25a6a873 RG |
5313 | /* Add one representative for all further args. */ |
5314 | if (is_varargs) | |
4ee00913 | 5315 | { |
25a6a873 | 5316 | varinfo_t argvi; |
4ee00913 DB |
5317 | const char *newname; |
5318 | char *tempname; | |
25a6a873 | 5319 | tree decl; |
c58936b6 | 5320 | |
25a6a873 | 5321 | asprintf (&tempname, "%s.varargs", name); |
4ee00913 DB |
5322 | newname = ggc_strdup (tempname); |
5323 | free (tempname); | |
5324 | ||
25a6a873 | 5325 | /* We need sth that can be pointed to for va_start. */ |
7d6e2521 | 5326 | decl = build_fake_var_decl (ptr_type_node); |
25a6a873 RG |
5327 | |
5328 | argvi = new_var_info (decl, newname); | |
5329 | argvi->offset = fi_parm_base + num_args; | |
5330 | argvi->size = ~0; | |
5331 | argvi->is_full_var = true; | |
5332 | argvi->is_heap_var = true; | |
5333 | argvi->fullsize = vi->fullsize; | |
5334 | gcc_assert (prev_vi->offset < argvi->offset); | |
5335 | prev_vi->next = argvi; | |
5336 | prev_vi = argvi; | |
4ee00913 | 5337 | } |
0bbf2ffa | 5338 | |
27c2cfa6 | 5339 | return vi; |
c58936b6 | 5340 | } |
4ee00913 | 5341 | |
6c11790d | 5342 | |
c58936b6 | 5343 | /* Return true if FIELDSTACK contains fields that overlap. |
6c11790d DB |
5344 | FIELDSTACK is assumed to be sorted by offset. */ |
5345 | ||
5346 | static bool | |
5347 | check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack) | |
5348 | { | |
5349 | fieldoff_s *fo = NULL; | |
5350 | unsigned int i; | |
30d2662c | 5351 | HOST_WIDE_INT lastoffset = -1; |
6c11790d | 5352 | |
ac47786e | 5353 | FOR_EACH_VEC_ELT (fieldoff_s, fieldstack, i, fo) |
6c11790d DB |
5354 | { |
5355 | if (fo->offset == lastoffset) | |
5356 | return true; | |
5357 | lastoffset = fo->offset; | |
5358 | } | |
5359 | return false; | |
5360 | } | |
21392f19 | 5361 | |
910fdc79 DB |
5362 | /* Create a varinfo structure for NAME and DECL, and add it to VARMAP. |
5363 | This will also create any varinfo structures necessary for fields | |
5364 | of DECL. */ | |
5365 | ||
18abb35e RG |
5366 | static varinfo_t |
5367 | create_variable_info_for_1 (tree decl, const char *name) | |
910fdc79 | 5368 | { |
18abb35e | 5369 | varinfo_t vi, newvi; |
82d6e6fc KG |
5370 | tree decl_type = TREE_TYPE (decl); |
5371 | tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type); | |
910fdc79 | 5372 | VEC (fieldoff_s,heap) *fieldstack = NULL; |
18abb35e RG |
5373 | fieldoff_s *fo; |
5374 | unsigned int i; | |
c58936b6 | 5375 | |
4ee00913 | 5376 | if (!declsize |
ee7d4b57 | 5377 | || !host_integerp (declsize, 1)) |
910fdc79 | 5378 | { |
18abb35e RG |
5379 | vi = new_var_info (decl, name); |
5380 | vi->offset = 0; | |
910fdc79 | 5381 | vi->size = ~0; |
18abb35e RG |
5382 | vi->fullsize = ~0; |
5383 | vi->is_unknown_size_var = true; | |
5384 | vi->is_full_var = true; | |
0f8d6231 | 5385 | vi->may_have_pointers = true; |
18abb35e | 5386 | return vi; |
910fdc79 | 5387 | } |
18abb35e RG |
5388 | |
5389 | /* Collect field information. */ | |
5390 | if (use_field_sensitive | |
5391 | && var_can_have_subvars (decl) | |
5392 | /* ??? Force us to not use subfields for global initializers | |
5393 | in IPA mode. Else we'd have to parse arbitrary initializers. */ | |
5394 | && !(in_ipa_mode | |
5395 | && is_global_var (decl) | |
5396 | && DECL_INITIAL (decl))) | |
910fdc79 | 5397 | { |
18abb35e RG |
5398 | fieldoff_s *fo = NULL; |
5399 | bool notokay = false; | |
5400 | unsigned int i; | |
5401 | ||
0f8d6231 | 5402 | push_fields_onto_fieldstack (decl_type, &fieldstack, 0); |
18abb35e RG |
5403 | |
5404 | for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++) | |
5405 | if (fo->has_unknown_size | |
5406 | || fo->offset < 0) | |
5407 | { | |
5408 | notokay = true; | |
5409 | break; | |
5410 | } | |
5411 | ||
5412 | /* We can't sort them if we have a field with a variable sized type, | |
5413 | which will make notokay = true. In that case, we are going to return | |
5414 | without creating varinfos for the fields anyway, so sorting them is a | |
5415 | waste to boot. */ | |
5416 | if (!notokay) | |
5417 | { | |
5418 | sort_fieldstack (fieldstack); | |
5419 | /* Due to some C++ FE issues, like PR 22488, we might end up | |
5420 | what appear to be overlapping fields even though they, | |
5421 | in reality, do not overlap. Until the C++ FE is fixed, | |
5422 | we will simply disable field-sensitivity for these cases. */ | |
5423 | notokay = check_for_overlaps (fieldstack); | |
5424 | } | |
5425 | ||
5426 | if (notokay) | |
5427 | VEC_free (fieldoff_s, heap, fieldstack); | |
5428 | } | |
5429 | ||
5430 | /* If we didn't end up collecting sub-variables create a full | |
5431 | variable for the decl. */ | |
5432 | if (VEC_length (fieldoff_s, fieldstack) <= 1 | |
5433 | || VEC_length (fieldoff_s, fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE) | |
5434 | { | |
5435 | vi = new_var_info (decl, name); | |
5436 | vi->offset = 0; | |
0f8d6231 | 5437 | vi->may_have_pointers = true; |
4ee00913 | 5438 | vi->fullsize = TREE_INT_CST_LOW (declsize); |
910fdc79 | 5439 | vi->size = vi->fullsize; |
18abb35e RG |
5440 | vi->is_full_var = true; |
5441 | VEC_free (fieldoff_s, heap, fieldstack); | |
5442 | return vi; | |
910fdc79 | 5443 | } |
c58936b6 | 5444 | |
18abb35e RG |
5445 | vi = new_var_info (decl, name); |
5446 | vi->fullsize = TREE_INT_CST_LOW (declsize); | |
5447 | for (i = 0, newvi = vi; | |
5448 | VEC_iterate (fieldoff_s, fieldstack, i, fo); | |
5449 | ++i, newvi = newvi->next) | |
5450 | { | |
5451 | const char *newname = "NULL"; | |
5452 | char *tempname; | |
5453 | ||
5454 | if (dump_file) | |
5455 | { | |
5456 | asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC | |
5457 | "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size); | |
5458 | newname = ggc_strdup (tempname); | |
5459 | free (tempname); | |
5460 | } | |
5461 | newvi->name = newname; | |
5462 | newvi->offset = fo->offset; | |
5463 | newvi->size = fo->size; | |
5464 | newvi->fullsize = vi->fullsize; | |
5465 | newvi->may_have_pointers = fo->may_have_pointers; | |
5466 | newvi->only_restrict_pointers = fo->only_restrict_pointers; | |
5467 | if (i + 1 < VEC_length (fieldoff_s, fieldstack)) | |
5468 | newvi->next = new_var_info (decl, name); | |
5469 | } | |
5470 | ||
5471 | VEC_free (fieldoff_s, heap, fieldstack); | |
25a6a873 | 5472 | |
18abb35e RG |
5473 | return vi; |
5474 | } | |
5475 | ||
5476 | static unsigned int | |
5477 | create_variable_info_for (tree decl, const char *name) | |
5478 | { | |
5479 | varinfo_t vi = create_variable_info_for_1 (decl, name); | |
5480 | unsigned int id = vi->id; | |
5481 | ||
5482 | insert_vi_for_tree (decl, vi); | |
5483 | ||
1565af08 RG |
5484 | if (TREE_CODE (decl) != VAR_DECL) |
5485 | return id; | |
5486 | ||
18abb35e RG |
5487 | /* Create initial constraints for globals. */ |
5488 | for (; vi; vi = vi->next) | |
13c6bff4 | 5489 | { |
18abb35e RG |
5490 | if (!vi->may_have_pointers |
5491 | || !vi->is_global_var) | |
5492 | continue; | |
5493 | ||
25a6a873 | 5494 | /* Mark global restrict qualified pointers. */ |
18abb35e RG |
5495 | if ((POINTER_TYPE_P (TREE_TYPE (decl)) |
5496 | && TYPE_RESTRICT (TREE_TYPE (decl))) | |
5497 | || vi->only_restrict_pointers) | |
74d27244 | 5498 | make_constraint_from_restrict (vi, "GLOBAL_RESTRICT"); |
25a6a873 | 5499 | |
1565af08 | 5500 | /* In non-IPA mode the initializer from nonlocal is all we need. */ |
25a6a873 | 5501 | if (!in_ipa_mode |
1565af08 | 5502 | || DECL_HARD_REGISTER (decl)) |
25a6a873 RG |
5503 | make_copy_constraint (vi, nonlocal_id); |
5504 | ||
1565af08 | 5505 | else |
25a6a873 | 5506 | { |
1565af08 RG |
5507 | struct varpool_node *vnode = varpool_get_node (decl); |
5508 | ||
5509 | /* For escaped variables initialize them from nonlocal. */ | |
5510 | if (!varpool_all_refs_explicit_p (vnode)) | |
5511 | make_copy_constraint (vi, nonlocal_id); | |
5512 | ||
5513 | /* If this is a global variable with an initializer and we are in | |
5514 | IPA mode generate constraints for it. */ | |
5515 | if (DECL_INITIAL (decl)) | |
25a6a873 | 5516 | { |
1565af08 RG |
5517 | VEC (ce_s, heap) *rhsc = NULL; |
5518 | struct constraint_expr lhs, *rhsp; | |
5519 | unsigned i; | |
5520 | get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc); | |
5521 | lhs.var = vi->id; | |
25a6a873 RG |
5522 | lhs.offset = 0; |
5523 | lhs.type = SCALAR; | |
ac47786e | 5524 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 | 5525 | process_constraint (new_constraint (lhs, *rhsp)); |
1565af08 RG |
5526 | /* If this is a variable that escapes from the unit |
5527 | the initializer escapes as well. */ | |
5528 | if (!varpool_all_refs_explicit_p (vnode)) | |
5529 | { | |
5530 | lhs.var = escaped_id; | |
5531 | lhs.offset = 0; | |
5532 | lhs.type = SCALAR; | |
5533 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) | |
5534 | process_constraint (new_constraint (lhs, *rhsp)); | |
5535 | } | |
5536 | VEC_free (ce_s, heap, rhsc); | |
25a6a873 | 5537 | } |
25a6a873 | 5538 | } |
13c6bff4 | 5539 | } |
910fdc79 | 5540 | |
18abb35e | 5541 | return id; |
910fdc79 DB |
5542 | } |
5543 | ||
5544 | /* Print out the points-to solution for VAR to FILE. */ | |
5545 | ||
5006671f | 5546 | static void |
910fdc79 DB |
5547 | dump_solution_for_var (FILE *file, unsigned int var) |
5548 | { | |
5549 | varinfo_t vi = get_varinfo (var); | |
5550 | unsigned int i; | |
c58936b6 DB |
5551 | bitmap_iterator bi; |
5552 | ||
25a6a873 RG |
5553 | /* Dump the solution for unified vars anyway, this avoids difficulties |
5554 | in scanning dumps in the testsuite. */ | |
5555 | fprintf (file, "%s = { ", vi->name); | |
5556 | vi = get_varinfo (find (var)); | |
5557 | EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi) | |
5558 | fprintf (file, "%s ", get_varinfo (i)->name); | |
5559 | fprintf (file, "}"); | |
5560 | ||
5561 | /* But note when the variable was unified. */ | |
5562 | if (vi->id != var) | |
5563 | fprintf (file, " same as %s", vi->name); | |
5564 | ||
5565 | fprintf (file, "\n"); | |
910fdc79 DB |
5566 | } |
5567 | ||
5568 | /* Print the points-to solution for VAR to stdout. */ | |
5569 | ||
24e47c76 | 5570 | DEBUG_FUNCTION void |
910fdc79 DB |
5571 | debug_solution_for_var (unsigned int var) |
5572 | { | |
5573 | dump_solution_for_var (stdout, var); | |
5574 | } | |
5575 | ||
910fdc79 DB |
5576 | /* Create varinfo structures for all of the variables in the |
5577 | function for intraprocedural mode. */ | |
5578 | ||
5579 | static void | |
5580 | intra_create_variable_infos (void) | |
5581 | { | |
5582 | tree t; | |
b23987ec | 5583 | |
6e7e772d | 5584 | /* For each incoming pointer argument arg, create the constraint ARG |
0d3c82d6 RG |
5585 | = NONLOCAL or a dummy variable if it is a restrict qualified |
5586 | passed-by-reference argument. */ | |
910ad8de | 5587 | for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t)) |
910fdc79 | 5588 | { |
910fdc79 | 5589 | varinfo_t p; |
c58936b6 | 5590 | |
bacd3fb6 | 5591 | /* For restrict qualified pointers to objects passed by |
960dcaf5 JJ |
5592 | reference build a real representative for the pointed-to object. |
5593 | Treat restrict qualified references the same. */ | |
5594 | if (TYPE_RESTRICT (TREE_TYPE (t)) | |
5595 | && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t))) | |
5596 | || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)) | |
bacd3fb6 RG |
5597 | { |
5598 | struct constraint_expr lhsc, rhsc; | |
5599 | varinfo_t vi; | |
7d6e2521 RG |
5600 | tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t))); |
5601 | DECL_EXTERNAL (heapvar) = 1; | |
1565af08 RG |
5602 | vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS"); |
5603 | insert_vi_for_tree (heapvar, vi); | |
bacd3fb6 RG |
5604 | lhsc.var = get_vi_for_tree (t)->id; |
5605 | lhsc.type = SCALAR; | |
5606 | lhsc.offset = 0; | |
7d6e2521 | 5607 | rhsc.var = vi->id; |
bacd3fb6 RG |
5608 | rhsc.type = ADDRESSOF; |
5609 | rhsc.offset = 0; | |
5610 | process_constraint (new_constraint (lhsc, rhsc)); | |
5611 | vi->is_restrict_var = 1; | |
1565af08 RG |
5612 | for (; vi; vi = vi->next) |
5613 | if (vi->may_have_pointers) | |
5614 | { | |
5615 | if (vi->only_restrict_pointers) | |
5616 | make_constraint_from_restrict (vi, "GLOBAL_RESTRICT"); | |
5617 | make_copy_constraint (vi, nonlocal_id); | |
5618 | } | |
bacd3fb6 RG |
5619 | continue; |
5620 | } | |
5621 | ||
10174ddf | 5622 | for (p = get_vi_for_tree (t); p; p = p->next) |
18abb35e RG |
5623 | { |
5624 | if (p->may_have_pointers) | |
5625 | make_constraint_from (p, nonlocal_id); | |
5626 | if (p->only_restrict_pointers) | |
5627 | make_constraint_from_restrict (p, "PARM_RESTRICT"); | |
5628 | } | |
74d27244 RG |
5629 | if (POINTER_TYPE_P (TREE_TYPE (t)) |
5630 | && TYPE_RESTRICT (TREE_TYPE (t))) | |
5631 | make_constraint_from_restrict (get_vi_for_tree (t), "PARM_RESTRICT"); | |
21392f19 | 5632 | } |
75af9746 | 5633 | |
10bd6c5c RG |
5634 | /* Add a constraint for a result decl that is passed by reference. */ |
5635 | if (DECL_RESULT (cfun->decl) | |
5636 | && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl))) | |
5637 | { | |
5638 | varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl)); | |
5639 | ||
5640 | for (p = result_vi; p; p = p->next) | |
5006671f | 5641 | make_constraint_from (p, nonlocal_id); |
10bd6c5c RG |
5642 | } |
5643 | ||
75af9746 RG |
5644 | /* Add a constraint for the incoming static chain parameter. */ |
5645 | if (cfun->static_chain_decl != NULL_TREE) | |
5646 | { | |
5647 | varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl); | |
5648 | ||
5649 | for (p = chain_vi; p; p = p->next) | |
5650 | make_constraint_from (p, nonlocal_id); | |
5651 | } | |
910fdc79 DB |
5652 | } |
5653 | ||
1296c31f DB |
5654 | /* Structure used to put solution bitmaps in a hashtable so they can |
5655 | be shared among variables with the same points-to set. */ | |
5656 | ||
5657 | typedef struct shared_bitmap_info | |
5658 | { | |
5659 | bitmap pt_vars; | |
5660 | hashval_t hashcode; | |
5661 | } *shared_bitmap_info_t; | |
e5cfc29f | 5662 | typedef const struct shared_bitmap_info *const_shared_bitmap_info_t; |
1296c31f DB |
5663 | |
5664 | static htab_t shared_bitmap_table; | |
5665 | ||
5666 | /* Hash function for a shared_bitmap_info_t */ | |
5667 | ||
5668 | static hashval_t | |
5669 | shared_bitmap_hash (const void *p) | |
5670 | { | |
e5cfc29f | 5671 | const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p; |
1296c31f DB |
5672 | return bi->hashcode; |
5673 | } | |
5674 | ||
5675 | /* Equality function for two shared_bitmap_info_t's. */ | |
5676 | ||
5677 | static int | |
5678 | shared_bitmap_eq (const void *p1, const void *p2) | |
5679 | { | |
e5cfc29f KG |
5680 | const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1; |
5681 | const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2; | |
1296c31f DB |
5682 | return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars); |
5683 | } | |
5684 | ||
5685 | /* Lookup a bitmap in the shared bitmap hashtable, and return an already | |
5686 | existing instance if there is one, NULL otherwise. */ | |
5687 | ||
5688 | static bitmap | |
5689 | shared_bitmap_lookup (bitmap pt_vars) | |
5690 | { | |
5691 | void **slot; | |
5692 | struct shared_bitmap_info sbi; | |
5693 | ||
5694 | sbi.pt_vars = pt_vars; | |
5695 | sbi.hashcode = bitmap_hash (pt_vars); | |
7b765bed | 5696 | |
1296c31f DB |
5697 | slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi, |
5698 | sbi.hashcode, NO_INSERT); | |
5699 | if (!slot) | |
5700 | return NULL; | |
5701 | else | |
5702 | return ((shared_bitmap_info_t) *slot)->pt_vars; | |
5703 | } | |
5704 | ||
5705 | ||
5706 | /* Add a bitmap to the shared bitmap hashtable. */ | |
5707 | ||
5708 | static void | |
5709 | shared_bitmap_add (bitmap pt_vars) | |
5710 | { | |
5711 | void **slot; | |
5712 | shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info); | |
7b765bed | 5713 | |
1296c31f DB |
5714 | sbi->pt_vars = pt_vars; |
5715 | sbi->hashcode = bitmap_hash (pt_vars); | |
7b765bed | 5716 | |
1296c31f DB |
5717 | slot = htab_find_slot_with_hash (shared_bitmap_table, sbi, |
5718 | sbi->hashcode, INSERT); | |
5719 | gcc_assert (!*slot); | |
5720 | *slot = (void *) sbi; | |
5721 | } | |
5722 | ||
5723 | ||
4d7a65ea | 5724 | /* Set bits in INTO corresponding to the variable uids in solution set FROM. */ |
910fdc79 | 5725 | |
b8698a0f | 5726 | static void |
4d7a65ea | 5727 | set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt) |
910fdc79 DB |
5728 | { |
5729 | unsigned int i; | |
5730 | bitmap_iterator bi; | |
f83ca251 | 5731 | |
910fdc79 DB |
5732 | EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi) |
5733 | { | |
5734 | varinfo_t vi = get_varinfo (i); | |
c58936b6 | 5735 | |
e8ca4159 DN |
5736 | /* The only artificial variables that are allowed in a may-alias |
5737 | set are heap variables. */ | |
5738 | if (vi->is_artificial_var && !vi->is_heap_var) | |
5739 | continue; | |
c58936b6 | 5740 | |
5611cf0b RG |
5741 | if (TREE_CODE (vi->decl) == VAR_DECL |
5742 | || TREE_CODE (vi->decl) == PARM_DECL | |
5743 | || TREE_CODE (vi->decl) == RESULT_DECL) | |
58b82d2b | 5744 | { |
25a6a873 RG |
5745 | /* If we are in IPA mode we will not recompute points-to |
5746 | sets after inlining so make sure they stay valid. */ | |
5747 | if (in_ipa_mode | |
5748 | && !DECL_PT_UID_SET_P (vi->decl)) | |
5749 | SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl)); | |
5750 | ||
5006671f RG |
5751 | /* Add the decl to the points-to set. Note that the points-to |
5752 | set contains global variables. */ | |
25a6a873 | 5753 | bitmap_set_bit (into, DECL_PT_UID (vi->decl)); |
74d27244 | 5754 | if (vi->is_global_var) |
5006671f | 5755 | pt->vars_contains_global = true; |
e8ca4159 | 5756 | } |
910fdc79 DB |
5757 | } |
5758 | } | |
e8ca4159 DN |
5759 | |
5760 | ||
4d7a65ea | 5761 | /* Compute the points-to solution *PT for the variable VI. */ |
ce1b6498 RG |
5762 | |
5763 | static void | |
1cfd38be | 5764 | find_what_var_points_to (varinfo_t orig_vi, struct pt_solution *pt) |
ce1b6498 | 5765 | { |
4d7a65ea | 5766 | unsigned int i; |
5006671f RG |
5767 | bitmap_iterator bi; |
5768 | bitmap finished_solution; | |
5769 | bitmap result; | |
1cfd38be | 5770 | varinfo_t vi; |
5006671f RG |
5771 | |
5772 | memset (pt, 0, sizeof (struct pt_solution)); | |
5773 | ||
5774 | /* This variable may have been collapsed, let's get the real | |
5775 | variable. */ | |
1cfd38be | 5776 | vi = get_varinfo (find (orig_vi->id)); |
5006671f RG |
5777 | |
5778 | /* Translate artificial variables into SSA_NAME_PTR_INFO | |
5779 | attributes. */ | |
5780 | EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi) | |
5781 | { | |
5782 | varinfo_t vi = get_varinfo (i); | |
5783 | ||
5784 | if (vi->is_artificial_var) | |
5785 | { | |
5786 | if (vi->id == nothing_id) | |
5787 | pt->null = 1; | |
5788 | else if (vi->id == escaped_id) | |
25a6a873 RG |
5789 | { |
5790 | if (in_ipa_mode) | |
5791 | pt->ipa_escaped = 1; | |
5792 | else | |
5793 | pt->escaped = 1; | |
5794 | } | |
5006671f RG |
5795 | else if (vi->id == nonlocal_id) |
5796 | pt->nonlocal = 1; | |
5797 | else if (vi->is_heap_var) | |
5798 | /* We represent heapvars in the points-to set properly. */ | |
5799 | ; | |
91deb937 RG |
5800 | else if (vi->id == readonly_id) |
5801 | /* Nobody cares. */ | |
5802 | ; | |
5006671f | 5803 | else if (vi->id == anything_id |
5006671f RG |
5804 | || vi->id == integer_id) |
5805 | pt->anything = 1; | |
5806 | } | |
74d27244 RG |
5807 | if (vi->is_restrict_var) |
5808 | pt->vars_contains_restrict = true; | |
5006671f RG |
5809 | } |
5810 | ||
5811 | /* Instead of doing extra work, simply do not create | |
5812 | elaborate points-to information for pt_anything pointers. */ | |
74d27244 | 5813 | if (pt->anything |
1cfd38be | 5814 | && (orig_vi->is_artificial_var |
74d27244 | 5815 | || !pt->vars_contains_restrict)) |
4d7a65ea | 5816 | return; |
5006671f RG |
5817 | |
5818 | /* Share the final set of variables when possible. */ | |
5819 | finished_solution = BITMAP_GGC_ALLOC (); | |
5820 | stats.points_to_sets_created++; | |
5821 | ||
4d7a65ea | 5822 | set_uids_in_ptset (finished_solution, vi->solution, pt); |
5006671f RG |
5823 | result = shared_bitmap_lookup (finished_solution); |
5824 | if (!result) | |
5825 | { | |
5826 | shared_bitmap_add (finished_solution); | |
5827 | pt->vars = finished_solution; | |
5828 | } | |
5829 | else | |
5830 | { | |
5831 | pt->vars = result; | |
5832 | bitmap_clear (finished_solution); | |
5833 | } | |
5006671f RG |
5834 | } |
5835 | ||
4d7a65ea | 5836 | /* Given a pointer variable P, fill in its points-to set. */ |
5006671f RG |
5837 | |
5838 | static void | |
4d7a65ea | 5839 | find_what_p_points_to (tree p) |
5006671f RG |
5840 | { |
5841 | struct ptr_info_def *pi; | |
7cc92f92 | 5842 | tree lookup_p = p; |
3e5937d7 | 5843 | varinfo_t vi; |
e8ca4159 | 5844 | |
7cc92f92 RG |
5845 | /* For parameters, get at the points-to set for the actual parm |
5846 | decl. */ | |
c58936b6 | 5847 | if (TREE_CODE (p) == SSA_NAME |
6938f93f JH |
5848 | && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL |
5849 | || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL) | |
38635499 | 5850 | && SSA_NAME_IS_DEFAULT_DEF (p)) |
7cc92f92 RG |
5851 | lookup_p = SSA_NAME_VAR (p); |
5852 | ||
15814ba0 | 5853 | vi = lookup_vi_for_tree (lookup_p); |
5006671f RG |
5854 | if (!vi) |
5855 | return; | |
5856 | ||
5857 | pi = get_ptr_info (p); | |
4d7a65ea | 5858 | find_what_var_points_to (vi, &pi->pt); |
5006671f | 5859 | } |
7b765bed | 5860 | |
910fdc79 | 5861 | |
5006671f | 5862 | /* Query statistics for points-to solutions. */ |
c58936b6 | 5863 | |
5006671f RG |
5864 | static struct { |
5865 | unsigned HOST_WIDE_INT pt_solution_includes_may_alias; | |
5866 | unsigned HOST_WIDE_INT pt_solution_includes_no_alias; | |
5867 | unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias; | |
5868 | unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias; | |
5869 | } pta_stats; | |
e8ca4159 | 5870 | |
5006671f RG |
5871 | void |
5872 | dump_pta_stats (FILE *s) | |
5873 | { | |
5874 | fprintf (s, "\nPTA query stats:\n"); | |
5875 | fprintf (s, " pt_solution_includes: " | |
5876 | HOST_WIDE_INT_PRINT_DEC" disambiguations, " | |
5877 | HOST_WIDE_INT_PRINT_DEC" queries\n", | |
5878 | pta_stats.pt_solution_includes_no_alias, | |
5879 | pta_stats.pt_solution_includes_no_alias | |
5880 | + pta_stats.pt_solution_includes_may_alias); | |
5881 | fprintf (s, " pt_solutions_intersect: " | |
5882 | HOST_WIDE_INT_PRINT_DEC" disambiguations, " | |
5883 | HOST_WIDE_INT_PRINT_DEC" queries\n", | |
5884 | pta_stats.pt_solutions_intersect_no_alias, | |
5885 | pta_stats.pt_solutions_intersect_no_alias | |
5886 | + pta_stats.pt_solutions_intersect_may_alias); | |
5887 | } | |
e8ca4159 | 5888 | |
9f09b13f | 5889 | |
5006671f RG |
5890 | /* Reset the points-to solution *PT to a conservative default |
5891 | (point to anything). */ | |
7b765bed | 5892 | |
5006671f RG |
5893 | void |
5894 | pt_solution_reset (struct pt_solution *pt) | |
5895 | { | |
5896 | memset (pt, 0, sizeof (struct pt_solution)); | |
5897 | pt->anything = true; | |
5898 | } | |
1296c31f | 5899 | |
55b34b5f | 5900 | /* Set the points-to solution *PT to point only to the variables |
25a6a873 RG |
5901 | in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains |
5902 | global variables and VARS_CONTAINS_RESTRICT specifies whether | |
5903 | it contains restrict tag variables. */ | |
55b34b5f RG |
5904 | |
5905 | void | |
25a6a873 RG |
5906 | pt_solution_set (struct pt_solution *pt, bitmap vars, |
5907 | bool vars_contains_global, bool vars_contains_restrict) | |
55b34b5f | 5908 | { |
55b34b5f RG |
5909 | memset (pt, 0, sizeof (struct pt_solution)); |
5910 | pt->vars = vars; | |
25a6a873 RG |
5911 | pt->vars_contains_global = vars_contains_global; |
5912 | pt->vars_contains_restrict = vars_contains_restrict; | |
5913 | } | |
5914 | ||
90fa9e17 RG |
5915 | /* Set the points-to solution *PT to point only to the variable VAR. */ |
5916 | ||
5917 | void | |
5918 | pt_solution_set_var (struct pt_solution *pt, tree var) | |
5919 | { | |
5920 | memset (pt, 0, sizeof (struct pt_solution)); | |
5921 | pt->vars = BITMAP_GGC_ALLOC (); | |
1b2bb171 | 5922 | bitmap_set_bit (pt->vars, DECL_PT_UID (var)); |
90fa9e17 RG |
5923 | pt->vars_contains_global = is_global_var (var); |
5924 | } | |
5925 | ||
25a6a873 RG |
5926 | /* Computes the union of the points-to solutions *DEST and *SRC and |
5927 | stores the result in *DEST. This changes the points-to bitmap | |
5928 | of *DEST and thus may not be used if that might be shared. | |
5929 | The points-to bitmap of *SRC and *DEST will not be shared after | |
5930 | this function if they were not before. */ | |
5931 | ||
5932 | static void | |
5933 | pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src) | |
5934 | { | |
5935 | dest->anything |= src->anything; | |
5936 | if (dest->anything) | |
55b34b5f | 5937 | { |
25a6a873 RG |
5938 | pt_solution_reset (dest); |
5939 | return; | |
55b34b5f | 5940 | } |
25a6a873 RG |
5941 | |
5942 | dest->nonlocal |= src->nonlocal; | |
5943 | dest->escaped |= src->escaped; | |
5944 | dest->ipa_escaped |= src->ipa_escaped; | |
5945 | dest->null |= src->null; | |
5946 | dest->vars_contains_global |= src->vars_contains_global; | |
5947 | dest->vars_contains_restrict |= src->vars_contains_restrict; | |
5948 | if (!src->vars) | |
5949 | return; | |
5950 | ||
5951 | if (!dest->vars) | |
5952 | dest->vars = BITMAP_GGC_ALLOC (); | |
5953 | bitmap_ior_into (dest->vars, src->vars); | |
55b34b5f RG |
5954 | } |
5955 | ||
5006671f | 5956 | /* Return true if the points-to solution *PT is empty. */ |
e8ca4159 | 5957 | |
25a6a873 | 5958 | bool |
5006671f RG |
5959 | pt_solution_empty_p (struct pt_solution *pt) |
5960 | { | |
5961 | if (pt->anything | |
5962 | || pt->nonlocal) | |
5963 | return false; | |
e8ca4159 | 5964 | |
5006671f RG |
5965 | if (pt->vars |
5966 | && !bitmap_empty_p (pt->vars)) | |
5967 | return false; | |
e8ca4159 | 5968 | |
5006671f RG |
5969 | /* If the solution includes ESCAPED, check if that is empty. */ |
5970 | if (pt->escaped | |
5971 | && !pt_solution_empty_p (&cfun->gimple_df->escaped)) | |
5972 | return false; | |
5973 | ||
25a6a873 RG |
5974 | /* If the solution includes ESCAPED, check if that is empty. */ |
5975 | if (pt->ipa_escaped | |
5976 | && !pt_solution_empty_p (&ipa_escaped_pt)) | |
5977 | return false; | |
5978 | ||
5006671f | 5979 | return true; |
910fdc79 DB |
5980 | } |
5981 | ||
5006671f | 5982 | /* Return true if the points-to solution *PT includes global memory. */ |
63a4ef6f | 5983 | |
2f571334 | 5984 | bool |
5006671f | 5985 | pt_solution_includes_global (struct pt_solution *pt) |
2f571334 | 5986 | { |
5006671f RG |
5987 | if (pt->anything |
5988 | || pt->nonlocal | |
5989 | || pt->vars_contains_global) | |
5990 | return true; | |
2f571334 | 5991 | |
5006671f RG |
5992 | if (pt->escaped) |
5993 | return pt_solution_includes_global (&cfun->gimple_df->escaped); | |
2f571334 | 5994 | |
25a6a873 RG |
5995 | if (pt->ipa_escaped) |
5996 | return pt_solution_includes_global (&ipa_escaped_pt); | |
5997 | ||
5998 | /* ??? This predicate is not correct for the IPA-PTA solution | |
5999 | as we do not properly distinguish between unit escape points | |
6000 | and global variables. */ | |
6001 | if (cfun->gimple_df->ipa_pta) | |
6002 | return true; | |
6003 | ||
5006671f RG |
6004 | return false; |
6005 | } | |
2f571334 | 6006 | |
5006671f RG |
6007 | /* Return true if the points-to solution *PT includes the variable |
6008 | declaration DECL. */ | |
15c15196 | 6009 | |
5006671f RG |
6010 | static bool |
6011 | pt_solution_includes_1 (struct pt_solution *pt, const_tree decl) | |
6012 | { | |
6013 | if (pt->anything) | |
6014 | return true; | |
2f571334 | 6015 | |
5006671f RG |
6016 | if (pt->nonlocal |
6017 | && is_global_var (decl)) | |
6018 | return true; | |
2f571334 | 6019 | |
5006671f | 6020 | if (pt->vars |
25a6a873 | 6021 | && bitmap_bit_p (pt->vars, DECL_PT_UID (decl))) |
5006671f | 6022 | return true; |
2f571334 | 6023 | |
5006671f RG |
6024 | /* If the solution includes ESCAPED, check it. */ |
6025 | if (pt->escaped | |
6026 | && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl)) | |
6027 | return true; | |
2f571334 | 6028 | |
25a6a873 RG |
6029 | /* If the solution includes ESCAPED, check it. */ |
6030 | if (pt->ipa_escaped | |
6031 | && pt_solution_includes_1 (&ipa_escaped_pt, decl)) | |
6032 | return true; | |
6033 | ||
5006671f | 6034 | return false; |
2f571334 | 6035 | } |
910fdc79 | 6036 | |
5006671f RG |
6037 | bool |
6038 | pt_solution_includes (struct pt_solution *pt, const_tree decl) | |
15c15196 | 6039 | { |
5006671f RG |
6040 | bool res = pt_solution_includes_1 (pt, decl); |
6041 | if (res) | |
6042 | ++pta_stats.pt_solution_includes_may_alias; | |
6043 | else | |
6044 | ++pta_stats.pt_solution_includes_no_alias; | |
6045 | return res; | |
6046 | } | |
15c15196 | 6047 | |
5006671f RG |
6048 | /* Return true if both points-to solutions PT1 and PT2 have a non-empty |
6049 | intersection. */ | |
15c15196 | 6050 | |
5006671f RG |
6051 | static bool |
6052 | pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2) | |
6053 | { | |
6054 | if (pt1->anything || pt2->anything) | |
6055 | return true; | |
15c15196 | 6056 | |
5006671f RG |
6057 | /* If either points to unknown global memory and the other points to |
6058 | any global memory they alias. */ | |
6059 | if ((pt1->nonlocal | |
6060 | && (pt2->nonlocal | |
6061 | || pt2->vars_contains_global)) | |
6062 | || (pt2->nonlocal | |
6063 | && pt1->vars_contains_global)) | |
6064 | return true; | |
15c15196 | 6065 | |
5006671f RG |
6066 | /* Check the escaped solution if required. */ |
6067 | if ((pt1->escaped || pt2->escaped) | |
6068 | && !pt_solution_empty_p (&cfun->gimple_df->escaped)) | |
6069 | { | |
6070 | /* If both point to escaped memory and that solution | |
6071 | is not empty they alias. */ | |
6072 | if (pt1->escaped && pt2->escaped) | |
6073 | return true; | |
15c15196 | 6074 | |
5006671f RG |
6075 | /* If either points to escaped memory see if the escaped solution |
6076 | intersects with the other. */ | |
6077 | if ((pt1->escaped | |
6078 | && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2)) | |
6079 | || (pt2->escaped | |
6080 | && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1))) | |
6081 | return true; | |
15c15196 RG |
6082 | } |
6083 | ||
25a6a873 RG |
6084 | /* Check the escaped solution if required. |
6085 | ??? Do we need to check the local against the IPA escaped sets? */ | |
6086 | if ((pt1->ipa_escaped || pt2->ipa_escaped) | |
6087 | && !pt_solution_empty_p (&ipa_escaped_pt)) | |
6088 | { | |
6089 | /* If both point to escaped memory and that solution | |
6090 | is not empty they alias. */ | |
6091 | if (pt1->ipa_escaped && pt2->ipa_escaped) | |
6092 | return true; | |
6093 | ||
6094 | /* If either points to escaped memory see if the escaped solution | |
6095 | intersects with the other. */ | |
6096 | if ((pt1->ipa_escaped | |
6097 | && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2)) | |
6098 | || (pt2->ipa_escaped | |
6099 | && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1))) | |
6100 | return true; | |
6101 | } | |
6102 | ||
5006671f RG |
6103 | /* Now both pointers alias if their points-to solution intersects. */ |
6104 | return (pt1->vars | |
6105 | && pt2->vars | |
6106 | && bitmap_intersect_p (pt1->vars, pt2->vars)); | |
6107 | } | |
6108 | ||
6109 | bool | |
6110 | pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2) | |
6111 | { | |
6112 | bool res = pt_solutions_intersect_1 (pt1, pt2); | |
6113 | if (res) | |
6114 | ++pta_stats.pt_solutions_intersect_may_alias; | |
6115 | else | |
6116 | ++pta_stats.pt_solutions_intersect_no_alias; | |
6117 | return res; | |
15c15196 RG |
6118 | } |
6119 | ||
74d27244 RG |
6120 | /* Return true if both points-to solutions PT1 and PT2 for two restrict |
6121 | qualified pointers are possibly based on the same pointer. */ | |
6122 | ||
6123 | bool | |
6124 | pt_solutions_same_restrict_base (struct pt_solution *pt1, | |
6125 | struct pt_solution *pt2) | |
6126 | { | |
6127 | /* If we deal with points-to solutions of two restrict qualified | |
6128 | pointers solely rely on the pointed-to variable bitmap intersection. | |
6129 | For two pointers that are based on each other the bitmaps will | |
6130 | intersect. */ | |
6131 | if (pt1->vars_contains_restrict | |
6132 | && pt2->vars_contains_restrict) | |
6133 | { | |
6134 | gcc_assert (pt1->vars && pt2->vars); | |
6135 | return bitmap_intersect_p (pt1->vars, pt2->vars); | |
6136 | } | |
6137 | ||
6138 | return true; | |
6139 | } | |
6140 | ||
b7091901 | 6141 | |
63a4ef6f DN |
6142 | /* Dump points-to information to OUTFILE. */ |
6143 | ||
5006671f | 6144 | static void |
910fdc79 DB |
6145 | dump_sa_points_to_info (FILE *outfile) |
6146 | { | |
910fdc79 | 6147 | unsigned int i; |
63a4ef6f | 6148 | |
e8ca4159 | 6149 | fprintf (outfile, "\nPoints-to sets\n\n"); |
63a4ef6f | 6150 | |
910fdc79 DB |
6151 | if (dump_flags & TDF_STATS) |
6152 | { | |
6153 | fprintf (outfile, "Stats:\n"); | |
63a4ef6f | 6154 | fprintf (outfile, "Total vars: %d\n", stats.total_vars); |
3e5937d7 DB |
6155 | fprintf (outfile, "Non-pointer vars: %d\n", |
6156 | stats.nonpointer_vars); | |
63a4ef6f DN |
6157 | fprintf (outfile, "Statically unified vars: %d\n", |
6158 | stats.unified_vars_static); | |
63a4ef6f DN |
6159 | fprintf (outfile, "Dynamically unified vars: %d\n", |
6160 | stats.unified_vars_dynamic); | |
6161 | fprintf (outfile, "Iterations: %d\n", stats.iterations); | |
4ee00913 | 6162 | fprintf (outfile, "Number of edges: %d\n", stats.num_edges); |
3e5937d7 DB |
6163 | fprintf (outfile, "Number of implicit edges: %d\n", |
6164 | stats.num_implicit_edges); | |
910fdc79 | 6165 | } |
63a4ef6f | 6166 | |
910fdc79 | 6167 | for (i = 0; i < VEC_length (varinfo_t, varmap); i++) |
25a6a873 RG |
6168 | { |
6169 | varinfo_t vi = get_varinfo (i); | |
6170 | if (!vi->may_have_pointers) | |
b28ae58f | 6171 | continue; |
25a6a873 RG |
6172 | dump_solution_for_var (outfile, i); |
6173 | } | |
910fdc79 DB |
6174 | } |
6175 | ||
6176 | ||
63a4ef6f DN |
6177 | /* Debug points-to information to stderr. */ |
6178 | ||
24e47c76 | 6179 | DEBUG_FUNCTION void |
63a4ef6f DN |
6180 | debug_sa_points_to_info (void) |
6181 | { | |
6182 | dump_sa_points_to_info (stderr); | |
6183 | } | |
6184 | ||
6185 | ||
910fdc79 DB |
6186 | /* Initialize the always-existing constraint variables for NULL |
6187 | ANYTHING, READONLY, and INTEGER */ | |
6188 | ||
6189 | static void | |
6190 | init_base_vars (void) | |
6191 | { | |
6192 | struct constraint_expr lhs, rhs; | |
0bbf2ffa RG |
6193 | varinfo_t var_anything; |
6194 | varinfo_t var_nothing; | |
6195 | varinfo_t var_readonly; | |
6196 | varinfo_t var_escaped; | |
6197 | varinfo_t var_nonlocal; | |
0bbf2ffa RG |
6198 | varinfo_t var_storedanything; |
6199 | varinfo_t var_integer; | |
910fdc79 DB |
6200 | |
6201 | /* Create the NULL variable, used to represent that a variable points | |
6202 | to NULL. */ | |
0bbf2ffa RG |
6203 | var_nothing = new_var_info (NULL_TREE, "NULL"); |
6204 | gcc_assert (var_nothing->id == nothing_id); | |
910fdc79 DB |
6205 | var_nothing->is_artificial_var = 1; |
6206 | var_nothing->offset = 0; | |
6207 | var_nothing->size = ~0; | |
6208 | var_nothing->fullsize = ~0; | |
13c2c08b | 6209 | var_nothing->is_special_var = 1; |
b28ae58f RG |
6210 | var_nothing->may_have_pointers = 0; |
6211 | var_nothing->is_global_var = 0; | |
910fdc79 DB |
6212 | |
6213 | /* Create the ANYTHING variable, used to represent that a variable | |
6214 | points to some unknown piece of memory. */ | |
0bbf2ffa RG |
6215 | var_anything = new_var_info (NULL_TREE, "ANYTHING"); |
6216 | gcc_assert (var_anything->id == anything_id); | |
910fdc79 DB |
6217 | var_anything->is_artificial_var = 1; |
6218 | var_anything->size = ~0; | |
6219 | var_anything->offset = 0; | |
6220 | var_anything->next = NULL; | |
6221 | var_anything->fullsize = ~0; | |
13c2c08b | 6222 | var_anything->is_special_var = 1; |
910fdc79 DB |
6223 | |
6224 | /* Anything points to anything. This makes deref constraints just | |
c58936b6 | 6225 | work in the presence of linked list and other p = *p type loops, |
910fdc79 | 6226 | by saying that *ANYTHING = ANYTHING. */ |
910fdc79 DB |
6227 | lhs.type = SCALAR; |
6228 | lhs.var = anything_id; | |
6229 | lhs.offset = 0; | |
3e5937d7 | 6230 | rhs.type = ADDRESSOF; |
910fdc79 DB |
6231 | rhs.var = anything_id; |
6232 | rhs.offset = 0; | |
e8ca4159 | 6233 | |
a5eadacc DB |
6234 | /* This specifically does not use process_constraint because |
6235 | process_constraint ignores all anything = anything constraints, since all | |
6236 | but this one are redundant. */ | |
b5efa470 | 6237 | VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs)); |
c58936b6 | 6238 | |
910fdc79 DB |
6239 | /* Create the READONLY variable, used to represent that a variable |
6240 | points to readonly memory. */ | |
0bbf2ffa RG |
6241 | var_readonly = new_var_info (NULL_TREE, "READONLY"); |
6242 | gcc_assert (var_readonly->id == readonly_id); | |
910fdc79 DB |
6243 | var_readonly->is_artificial_var = 1; |
6244 | var_readonly->offset = 0; | |
6245 | var_readonly->size = ~0; | |
6246 | var_readonly->fullsize = ~0; | |
6247 | var_readonly->next = NULL; | |
13c2c08b | 6248 | var_readonly->is_special_var = 1; |
910fdc79 DB |
6249 | |
6250 | /* readonly memory points to anything, in order to make deref | |
6251 | easier. In reality, it points to anything the particular | |
6252 | readonly variable can point to, but we don't track this | |
607fb860 | 6253 | separately. */ |
910fdc79 DB |
6254 | lhs.type = SCALAR; |
6255 | lhs.var = readonly_id; | |
6256 | lhs.offset = 0; | |
3e5937d7 | 6257 | rhs.type = ADDRESSOF; |
b7091901 | 6258 | rhs.var = readonly_id; /* FIXME */ |
910fdc79 | 6259 | rhs.offset = 0; |
b7091901 | 6260 | process_constraint (new_constraint (lhs, rhs)); |
c58936b6 | 6261 | |
b7091901 RG |
6262 | /* Create the ESCAPED variable, used to represent the set of escaped |
6263 | memory. */ | |
0bbf2ffa RG |
6264 | var_escaped = new_var_info (NULL_TREE, "ESCAPED"); |
6265 | gcc_assert (var_escaped->id == escaped_id); | |
b7091901 RG |
6266 | var_escaped->is_artificial_var = 1; |
6267 | var_escaped->offset = 0; | |
6268 | var_escaped->size = ~0; | |
6269 | var_escaped->fullsize = ~0; | |
6270 | var_escaped->is_special_var = 0; | |
b7091901 | 6271 | |
b7091901 RG |
6272 | /* Create the NONLOCAL variable, used to represent the set of nonlocal |
6273 | memory. */ | |
0bbf2ffa RG |
6274 | var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL"); |
6275 | gcc_assert (var_nonlocal->id == nonlocal_id); | |
b7091901 RG |
6276 | var_nonlocal->is_artificial_var = 1; |
6277 | var_nonlocal->offset = 0; | |
6278 | var_nonlocal->size = ~0; | |
6279 | var_nonlocal->fullsize = ~0; | |
6280 | var_nonlocal->is_special_var = 1; | |
b7091901 | 6281 | |
5006671f RG |
6282 | /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */ |
6283 | lhs.type = SCALAR; | |
6284 | lhs.var = escaped_id; | |
6285 | lhs.offset = 0; | |
6286 | rhs.type = DEREF; | |
6287 | rhs.var = escaped_id; | |
6288 | rhs.offset = 0; | |
6289 | process_constraint (new_constraint (lhs, rhs)); | |
6290 | ||
6291 | /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the | |
6292 | whole variable escapes. */ | |
6293 | lhs.type = SCALAR; | |
6294 | lhs.var = escaped_id; | |
6295 | lhs.offset = 0; | |
6296 | rhs.type = SCALAR; | |
6297 | rhs.var = escaped_id; | |
6298 | rhs.offset = UNKNOWN_OFFSET; | |
6299 | process_constraint (new_constraint (lhs, rhs)); | |
6300 | ||
6301 | /* *ESCAPED = NONLOCAL. This is true because we have to assume | |
6302 | everything pointed to by escaped points to what global memory can | |
6303 | point to. */ | |
6304 | lhs.type = DEREF; | |
6305 | lhs.var = escaped_id; | |
6306 | lhs.offset = 0; | |
6307 | rhs.type = SCALAR; | |
6308 | rhs.var = nonlocal_id; | |
6309 | rhs.offset = 0; | |
6310 | process_constraint (new_constraint (lhs, rhs)); | |
6311 | ||
6312 | /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because | |
6313 | global memory may point to global memory and escaped memory. */ | |
b7091901 RG |
6314 | lhs.type = SCALAR; |
6315 | lhs.var = nonlocal_id; | |
6316 | lhs.offset = 0; | |
6317 | rhs.type = ADDRESSOF; | |
5006671f RG |
6318 | rhs.var = nonlocal_id; |
6319 | rhs.offset = 0; | |
6320 | process_constraint (new_constraint (lhs, rhs)); | |
6321 | rhs.type = ADDRESSOF; | |
b7091901 RG |
6322 | rhs.var = escaped_id; |
6323 | rhs.offset = 0; | |
910fdc79 | 6324 | process_constraint (new_constraint (lhs, rhs)); |
c58936b6 | 6325 | |
9e39dba6 RG |
6326 | /* Create the STOREDANYTHING variable, used to represent the set of |
6327 | variables stored to *ANYTHING. */ | |
0bbf2ffa RG |
6328 | var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING"); |
6329 | gcc_assert (var_storedanything->id == storedanything_id); | |
9e39dba6 RG |
6330 | var_storedanything->is_artificial_var = 1; |
6331 | var_storedanything->offset = 0; | |
6332 | var_storedanything->size = ~0; | |
6333 | var_storedanything->fullsize = ~0; | |
6334 | var_storedanything->is_special_var = 0; | |
9e39dba6 | 6335 | |
910fdc79 | 6336 | /* Create the INTEGER variable, used to represent that a variable points |
5006671f | 6337 | to what an INTEGER "points to". */ |
0bbf2ffa RG |
6338 | var_integer = new_var_info (NULL_TREE, "INTEGER"); |
6339 | gcc_assert (var_integer->id == integer_id); | |
910fdc79 DB |
6340 | var_integer->is_artificial_var = 1; |
6341 | var_integer->size = ~0; | |
6342 | var_integer->fullsize = ~0; | |
6343 | var_integer->offset = 0; | |
6344 | var_integer->next = NULL; | |
13c2c08b | 6345 | var_integer->is_special_var = 1; |
a5eadacc | 6346 | |
21392f19 DB |
6347 | /* INTEGER = ANYTHING, because we don't know where a dereference of |
6348 | a random integer will point to. */ | |
a5eadacc DB |
6349 | lhs.type = SCALAR; |
6350 | lhs.var = integer_id; | |
6351 | lhs.offset = 0; | |
3e5937d7 | 6352 | rhs.type = ADDRESSOF; |
a5eadacc DB |
6353 | rhs.var = anything_id; |
6354 | rhs.offset = 0; | |
6355 | process_constraint (new_constraint (lhs, rhs)); | |
c58936b6 | 6356 | } |
910fdc79 | 6357 | |
4ee00913 | 6358 | /* Initialize things necessary to perform PTA */ |
910fdc79 | 6359 | |
4ee00913 DB |
6360 | static void |
6361 | init_alias_vars (void) | |
910fdc79 | 6362 | { |
e5bae89b RG |
6363 | use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1); |
6364 | ||
3e5937d7 DB |
6365 | bitmap_obstack_initialize (&pta_obstack); |
6366 | bitmap_obstack_initialize (&oldpta_obstack); | |
4ee00913 | 6367 | bitmap_obstack_initialize (&predbitmap_obstack); |
910fdc79 | 6368 | |
c58936b6 | 6369 | constraint_pool = create_alloc_pool ("Constraint pool", |
910fdc79 DB |
6370 | sizeof (struct constraint), 30); |
6371 | variable_info_pool = create_alloc_pool ("Variable info pool", | |
6372 | sizeof (struct variable_info), 30); | |
b5efa470 DB |
6373 | constraints = VEC_alloc (constraint_t, heap, 8); |
6374 | varmap = VEC_alloc (varinfo_t, heap, 8); | |
15814ba0 | 6375 | vi_for_tree = pointer_map_create (); |
3e8542ca | 6376 | call_stmt_vars = pointer_map_create (); |
3e5937d7 | 6377 | |
910fdc79 | 6378 | memset (&stats, 0, sizeof (stats)); |
1296c31f DB |
6379 | shared_bitmap_table = htab_create (511, shared_bitmap_hash, |
6380 | shared_bitmap_eq, free); | |
910fdc79 | 6381 | init_base_vars (); |
7d6e2521 RG |
6382 | |
6383 | gcc_obstack_init (&fake_var_decl_obstack); | |
4ee00913 DB |
6384 | } |
6385 | ||
3e5937d7 DB |
6386 | /* Remove the REF and ADDRESS edges from GRAPH, as well as all the |
6387 | predecessor edges. */ | |
6388 | ||
6389 | static void | |
6390 | remove_preds_and_fake_succs (constraint_graph_t graph) | |
6391 | { | |
6392 | unsigned int i; | |
6393 | ||
6394 | /* Clear the implicit ref and address nodes from the successor | |
6395 | lists. */ | |
6396 | for (i = 0; i < FIRST_REF_NODE; i++) | |
6397 | { | |
6398 | if (graph->succs[i]) | |
6399 | bitmap_clear_range (graph->succs[i], FIRST_REF_NODE, | |
6400 | FIRST_REF_NODE * 2); | |
6401 | } | |
6402 | ||
6403 | /* Free the successor list for the non-ref nodes. */ | |
6404 | for (i = FIRST_REF_NODE; i < graph->size; i++) | |
6405 | { | |
6406 | if (graph->succs[i]) | |
6407 | BITMAP_FREE (graph->succs[i]); | |
6408 | } | |
6409 | ||
6410 | /* Now reallocate the size of the successor list as, and blow away | |
6411 | the predecessor bitmaps. */ | |
6412 | graph->size = VEC_length (varinfo_t, varmap); | |
c22940cd | 6413 | graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size); |
3e5937d7 DB |
6414 | |
6415 | free (graph->implicit_preds); | |
6416 | graph->implicit_preds = NULL; | |
6417 | free (graph->preds); | |
6418 | graph->preds = NULL; | |
6419 | bitmap_obstack_release (&predbitmap_obstack); | |
6420 | } | |
6421 | ||
5c245b95 | 6422 | /* Solve the constraint set. */ |
4ee00913 | 6423 | |
5006671f | 6424 | static void |
5c245b95 | 6425 | solve_constraints (void) |
4ee00913 | 6426 | { |
3e5937d7 | 6427 | struct scc_info *si; |
910fdc79 | 6428 | |
21392f19 DB |
6429 | if (dump_file) |
6430 | fprintf (dump_file, | |
6431 | "\nCollapsing static cycles and doing variable " | |
7b765bed DB |
6432 | "substitution\n"); |
6433 | ||
6434 | init_graph (VEC_length (varinfo_t, varmap) * 2); | |
b8698a0f | 6435 | |
7b765bed DB |
6436 | if (dump_file) |
6437 | fprintf (dump_file, "Building predecessor graph\n"); | |
3e5937d7 | 6438 | build_pred_graph (); |
b8698a0f | 6439 | |
7b765bed DB |
6440 | if (dump_file) |
6441 | fprintf (dump_file, "Detecting pointer and location " | |
6442 | "equivalences\n"); | |
3e5937d7 | 6443 | si = perform_var_substitution (graph); |
b8698a0f | 6444 | |
7b765bed DB |
6445 | if (dump_file) |
6446 | fprintf (dump_file, "Rewriting constraints and unifying " | |
6447 | "variables\n"); | |
6448 | rewrite_constraints (graph, si); | |
fc93bcb6 | 6449 | |
8576f20a | 6450 | build_succ_graph (); |
fc93bcb6 | 6451 | |
8d3e3924 RG |
6452 | free_var_substitution_info (si); |
6453 | ||
8576f20a | 6454 | /* Attach complex constraints to graph nodes. */ |
7b765bed DB |
6455 | move_complex_constraints (graph); |
6456 | ||
6457 | if (dump_file) | |
6458 | fprintf (dump_file, "Uniting pointer but not location equivalent " | |
6459 | "variables\n"); | |
6460 | unite_pointer_equivalences (graph); | |
6461 | ||
6462 | if (dump_file) | |
6463 | fprintf (dump_file, "Finding indirect cycles\n"); | |
3e5937d7 | 6464 | find_indirect_cycles (graph); |
c58936b6 | 6465 | |
3e5937d7 DB |
6466 | /* Implicit nodes and predecessors are no longer necessary at this |
6467 | point. */ | |
6468 | remove_preds_and_fake_succs (graph); | |
c58936b6 | 6469 | |
8576f20a RG |
6470 | if (dump_file && (dump_flags & TDF_GRAPH)) |
6471 | { | |
6472 | fprintf (dump_file, "\n\n// The constraint graph before solve-graph " | |
6473 | "in dot format:\n"); | |
6474 | dump_constraint_graph (dump_file); | |
6475 | fprintf (dump_file, "\n\n"); | |
6476 | } | |
6477 | ||
21392f19 | 6478 | if (dump_file) |
7b765bed | 6479 | fprintf (dump_file, "Solving graph\n"); |
c58936b6 | 6480 | |
21392f19 | 6481 | solve_graph (graph); |
c58936b6 | 6482 | |
8576f20a RG |
6483 | if (dump_file && (dump_flags & TDF_GRAPH)) |
6484 | { | |
6485 | fprintf (dump_file, "\n\n// The constraint graph after solve-graph " | |
6486 | "in dot format:\n"); | |
6487 | dump_constraint_graph (dump_file); | |
6488 | fprintf (dump_file, "\n\n"); | |
6489 | } | |
6490 | ||
910fdc79 DB |
6491 | if (dump_file) |
6492 | dump_sa_points_to_info (dump_file); | |
5c245b95 RG |
6493 | } |
6494 | ||
6495 | /* Create points-to sets for the current function. See the comments | |
6496 | at the start of the file for an algorithmic overview. */ | |
6497 | ||
6498 | static void | |
6499 | compute_points_to_sets (void) | |
6500 | { | |
6501 | basic_block bb; | |
6502 | unsigned i; | |
6503 | varinfo_t vi; | |
6504 | ||
6505 | timevar_push (TV_TREE_PTA); | |
6506 | ||
6507 | init_alias_vars (); | |
5c245b95 RG |
6508 | |
6509 | intra_create_variable_infos (); | |
6510 | ||
25a6a873 | 6511 | /* Now walk all statements and build the constraint set. */ |
5c245b95 RG |
6512 | FOR_EACH_BB (bb) |
6513 | { | |
6514 | gimple_stmt_iterator gsi; | |
6515 | ||
6516 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6517 | { | |
6518 | gimple phi = gsi_stmt (gsi); | |
6519 | ||
6520 | if (is_gimple_reg (gimple_phi_result (phi))) | |
6521 | find_func_aliases (phi); | |
6522 | } | |
6523 | ||
6524 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6525 | { | |
6526 | gimple stmt = gsi_stmt (gsi); | |
6527 | ||
6528 | find_func_aliases (stmt); | |
6529 | } | |
6530 | } | |
6531 | ||
25a6a873 RG |
6532 | if (dump_file) |
6533 | { | |
6534 | fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n"); | |
6535 | dump_constraints (dump_file, 0); | |
6536 | } | |
6537 | ||
5c245b95 RG |
6538 | /* From the constraints compute the points-to sets. */ |
6539 | solve_constraints (); | |
c58936b6 | 6540 | |
3e8542ca | 6541 | /* Compute the points-to set for ESCAPED used for call-clobber analysis. */ |
0bbf2ffa RG |
6542 | find_what_var_points_to (get_varinfo (escaped_id), |
6543 | &cfun->gimple_df->escaped); | |
5006671f RG |
6544 | |
6545 | /* Make sure the ESCAPED solution (which is used as placeholder in | |
6546 | other solutions) does not reference itself. This simplifies | |
6547 | points-to solution queries. */ | |
6548 | cfun->gimple_df->escaped.escaped = 0; | |
6549 | ||
14c41b9b | 6550 | /* Mark escaped HEAP variables as global. */ |
ac47786e | 6551 | FOR_EACH_VEC_ELT (varinfo_t, varmap, i, vi) |
14c41b9b | 6552 | if (vi->is_heap_var |
91deb937 | 6553 | && !vi->is_restrict_var |
14c41b9b | 6554 | && !vi->is_global_var) |
91deb937 RG |
6555 | DECL_EXTERNAL (vi->decl) = vi->is_global_var |
6556 | = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl); | |
14c41b9b | 6557 | |
5006671f RG |
6558 | /* Compute the points-to sets for pointer SSA_NAMEs. */ |
6559 | for (i = 0; i < num_ssa_names; ++i) | |
6560 | { | |
6561 | tree ptr = ssa_name (i); | |
6562 | if (ptr | |
6563 | && POINTER_TYPE_P (TREE_TYPE (ptr))) | |
4d7a65ea | 6564 | find_what_p_points_to (ptr); |
5006671f | 6565 | } |
e8ca4159 | 6566 | |
d086d311 RG |
6567 | /* Compute the call-used/clobbered sets. */ |
6568 | FOR_EACH_BB (bb) | |
6569 | { | |
6570 | gimple_stmt_iterator gsi; | |
6571 | ||
6572 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6573 | { | |
6574 | gimple stmt = gsi_stmt (gsi); | |
6575 | struct pt_solution *pt; | |
6576 | if (!is_gimple_call (stmt)) | |
6577 | continue; | |
6578 | ||
6579 | pt = gimple_call_use_set (stmt); | |
6580 | if (gimple_call_flags (stmt) & ECF_CONST) | |
6581 | memset (pt, 0, sizeof (struct pt_solution)); | |
3e8542ca | 6582 | else if ((vi = lookup_call_use_vi (stmt)) != NULL) |
d086d311 | 6583 | { |
3e8542ca RG |
6584 | find_what_var_points_to (vi, pt); |
6585 | /* Escaped (and thus nonlocal) variables are always | |
6586 | implicitly used by calls. */ | |
d086d311 RG |
6587 | /* ??? ESCAPED can be empty even though NONLOCAL |
6588 | always escaped. */ | |
6589 | pt->nonlocal = 1; | |
6590 | pt->escaped = 1; | |
6591 | } | |
6592 | else | |
6593 | { | |
3e8542ca RG |
6594 | /* If there is nothing special about this call then |
6595 | we have made everything that is used also escape. */ | |
d086d311 RG |
6596 | *pt = cfun->gimple_df->escaped; |
6597 | pt->nonlocal = 1; | |
6598 | } | |
6599 | ||
6600 | pt = gimple_call_clobber_set (stmt); | |
6601 | if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS)) | |
6602 | memset (pt, 0, sizeof (struct pt_solution)); | |
3e8542ca RG |
6603 | else if ((vi = lookup_call_clobber_vi (stmt)) != NULL) |
6604 | { | |
6605 | find_what_var_points_to (vi, pt); | |
6606 | /* Escaped (and thus nonlocal) variables are always | |
6607 | implicitly clobbered by calls. */ | |
6608 | /* ??? ESCAPED can be empty even though NONLOCAL | |
6609 | always escaped. */ | |
6610 | pt->nonlocal = 1; | |
6611 | pt->escaped = 1; | |
6612 | } | |
d086d311 RG |
6613 | else |
6614 | { | |
3e8542ca RG |
6615 | /* If there is nothing special about this call then |
6616 | we have made everything that is used also escape. */ | |
d086d311 RG |
6617 | *pt = cfun->gimple_df->escaped; |
6618 | pt->nonlocal = 1; | |
6619 | } | |
6620 | } | |
6621 | } | |
6622 | ||
e8ca4159 | 6623 | timevar_pop (TV_TREE_PTA); |
910fdc79 DB |
6624 | } |
6625 | ||
910fdc79 DB |
6626 | |
6627 | /* Delete created points-to sets. */ | |
6628 | ||
5006671f | 6629 | static void |
e8ca4159 | 6630 | delete_points_to_sets (void) |
910fdc79 | 6631 | { |
7b765bed | 6632 | unsigned int i; |
c58936b6 | 6633 | |
1296c31f | 6634 | htab_delete (shared_bitmap_table); |
3e5937d7 DB |
6635 | if (dump_file && (dump_flags & TDF_STATS)) |
6636 | fprintf (dump_file, "Points to sets created:%d\n", | |
6637 | stats.points_to_sets_created); | |
6638 | ||
15814ba0 | 6639 | pointer_map_destroy (vi_for_tree); |
3e8542ca | 6640 | pointer_map_destroy (call_stmt_vars); |
3e5937d7 | 6641 | bitmap_obstack_release (&pta_obstack); |
b5efa470 | 6642 | VEC_free (constraint_t, heap, constraints); |
c58936b6 | 6643 | |
7b765bed | 6644 | for (i = 0; i < graph->size; i++) |
3e5937d7 | 6645 | VEC_free (constraint_t, heap, graph->complex[i]); |
285463b5 | 6646 | free (graph->complex); |
21392f19 | 6647 | |
3e5937d7 | 6648 | free (graph->rep); |
57250223 | 6649 | free (graph->succs); |
7b765bed DB |
6650 | free (graph->pe); |
6651 | free (graph->pe_rep); | |
3e5937d7 | 6652 | free (graph->indirect_cycles); |
b5efa470 DB |
6653 | free (graph); |
6654 | ||
6655 | VEC_free (varinfo_t, heap, varmap); | |
910fdc79 | 6656 | free_alloc_pool (variable_info_pool); |
c58936b6 | 6657 | free_alloc_pool (constraint_pool); |
7d6e2521 RG |
6658 | |
6659 | obstack_free (&fake_var_decl_obstack, NULL); | |
910fdc79 | 6660 | } |
973162ec | 6661 | |
5006671f RG |
6662 | |
6663 | /* Compute points-to information for every SSA_NAME pointer in the | |
6664 | current function and compute the transitive closure of escaped | |
6665 | variables to re-initialize the call-clobber states of local variables. */ | |
6666 | ||
6667 | unsigned int | |
6668 | compute_may_aliases (void) | |
6669 | { | |
25a6a873 RG |
6670 | if (cfun->gimple_df->ipa_pta) |
6671 | { | |
6672 | if (dump_file) | |
6673 | { | |
6674 | fprintf (dump_file, "\nNot re-computing points-to information " | |
6675 | "because IPA points-to information is available.\n\n"); | |
6676 | ||
6677 | /* But still dump what we have remaining it. */ | |
6678 | dump_alias_info (dump_file); | |
6679 | ||
6680 | if (dump_flags & TDF_DETAILS) | |
6681 | dump_referenced_vars (dump_file); | |
6682 | } | |
6683 | ||
6684 | return 0; | |
6685 | } | |
6686 | ||
5006671f RG |
6687 | /* For each pointer P_i, determine the sets of variables that P_i may |
6688 | point-to. Compute the reachability set of escaped and call-used | |
6689 | variables. */ | |
6690 | compute_points_to_sets (); | |
6691 | ||
6692 | /* Debugging dumps. */ | |
6693 | if (dump_file) | |
6694 | { | |
6695 | dump_alias_info (dump_file); | |
6696 | ||
6697 | if (dump_flags & TDF_DETAILS) | |
6698 | dump_referenced_vars (dump_file); | |
6699 | } | |
6700 | ||
6701 | /* Deallocate memory used by aliasing data structures and the internal | |
6702 | points-to solution. */ | |
6703 | delete_points_to_sets (); | |
6704 | ||
6705 | gcc_assert (!need_ssa_update_p (cfun)); | |
6706 | ||
6707 | return 0; | |
6708 | } | |
6709 | ||
248fc9f3 RG |
6710 | static bool |
6711 | gate_tree_pta (void) | |
6712 | { | |
6713 | return flag_tree_pta; | |
6714 | } | |
5006671f RG |
6715 | |
6716 | /* A dummy pass to cause points-to information to be computed via | |
6717 | TODO_rebuild_alias. */ | |
6718 | ||
6719 | struct gimple_opt_pass pass_build_alias = | |
6720 | { | |
6721 | { | |
6722 | GIMPLE_PASS, | |
6723 | "alias", /* name */ | |
248fc9f3 | 6724 | gate_tree_pta, /* gate */ |
5006671f RG |
6725 | NULL, /* execute */ |
6726 | NULL, /* sub */ | |
6727 | NULL, /* next */ | |
6728 | 0, /* static_pass_number */ | |
7072a650 | 6729 | TV_NONE, /* tv_id */ |
5006671f | 6730 | PROP_cfg | PROP_ssa, /* properties_required */ |
4effdf02 | 6731 | 0, /* properties_provided */ |
5006671f RG |
6732 | 0, /* properties_destroyed */ |
6733 | 0, /* todo_flags_start */ | |
22c5fa5f | 6734 | TODO_rebuild_alias /* todo_flags_finish */ |
5006671f RG |
6735 | } |
6736 | }; | |
6737 | ||
6b8ed145 RG |
6738 | /* A dummy pass to cause points-to information to be computed via |
6739 | TODO_rebuild_alias. */ | |
6740 | ||
6741 | struct gimple_opt_pass pass_build_ealias = | |
6742 | { | |
6743 | { | |
6744 | GIMPLE_PASS, | |
6745 | "ealias", /* name */ | |
6746 | gate_tree_pta, /* gate */ | |
6747 | NULL, /* execute */ | |
6748 | NULL, /* sub */ | |
6749 | NULL, /* next */ | |
6750 | 0, /* static_pass_number */ | |
6751 | TV_NONE, /* tv_id */ | |
6752 | PROP_cfg | PROP_ssa, /* properties_required */ | |
6753 | 0, /* properties_provided */ | |
6754 | 0, /* properties_destroyed */ | |
6755 | 0, /* todo_flags_start */ | |
22c5fa5f | 6756 | TODO_rebuild_alias /* todo_flags_finish */ |
6b8ed145 RG |
6757 | } |
6758 | }; | |
6759 | ||
5006671f | 6760 | |
4ee00913 DB |
6761 | /* Return true if we should execute IPA PTA. */ |
6762 | static bool | |
6763 | gate_ipa_pta (void) | |
6764 | { | |
de925a03 RG |
6765 | return (optimize |
6766 | && flag_ipa_pta | |
4ee00913 | 6767 | /* Don't bother doing anything if the program has errors. */ |
1da2ed5f | 6768 | && !seen_error ()); |
4ee00913 DB |
6769 | } |
6770 | ||
25a6a873 RG |
6771 | /* IPA PTA solutions for ESCAPED. */ |
6772 | struct pt_solution ipa_escaped_pt | |
6773 | = { true, false, false, false, false, false, false, NULL }; | |
6774 | ||
39e2db00 JH |
6775 | /* Associate node with varinfo DATA. Worker for |
6776 | cgraph_for_node_and_aliases. */ | |
6777 | static bool | |
6778 | associate_varinfo_to_alias (struct cgraph_node *node, void *data) | |
6779 | { | |
6780 | if (node->alias || node->thunk.thunk_p) | |
6781 | insert_vi_for_tree (node->decl, (varinfo_t)data); | |
6782 | return false; | |
6783 | } | |
6784 | ||
4ee00913 | 6785 | /* Execute the driver for IPA PTA. */ |
c2924966 | 6786 | static unsigned int |
4ee00913 DB |
6787 | ipa_pta_execute (void) |
6788 | { | |
6789 | struct cgraph_node *node; | |
25a6a873 RG |
6790 | struct varpool_node *var; |
6791 | int from; | |
3e5937d7 | 6792 | |
4ee00913 | 6793 | in_ipa_mode = 1; |
5c245b95 | 6794 | |
4ee00913 | 6795 | init_alias_vars (); |
c58936b6 | 6796 | |
1565af08 RG |
6797 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6798 | { | |
6799 | dump_cgraph (dump_file); | |
6800 | fprintf (dump_file, "\n"); | |
6801 | } | |
6802 | ||
5c245b95 | 6803 | /* Build the constraints. */ |
4ee00913 DB |
6804 | for (node = cgraph_nodes; node; node = node->next) |
6805 | { | |
27c2cfa6 | 6806 | varinfo_t vi; |
5c245b95 RG |
6807 | /* Nodes without a body are not interesting. Especially do not |
6808 | visit clones at this point for now - we get duplicate decls | |
6809 | there for inline clones at least. */ | |
1565af08 | 6810 | if (!cgraph_function_with_gimple_body_p (node)) |
5c245b95 RG |
6811 | continue; |
6812 | ||
1565af08 RG |
6813 | gcc_assert (!node->clone_of); |
6814 | ||
27c2cfa6 | 6815 | vi = create_function_info_for (node->decl, |
39e2db00 JH |
6816 | alias_get_name (node->decl)); |
6817 | cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true); | |
4ee00913 | 6818 | } |
5c245b95 | 6819 | |
25a6a873 RG |
6820 | /* Create constraints for global variables and their initializers. */ |
6821 | for (var = varpool_nodes; var; var = var->next) | |
27c2cfa6 | 6822 | { |
cd35bcf7 JH |
6823 | if (var->alias) |
6824 | continue; | |
27c2cfa6 | 6825 | |
9c7c9f10 | 6826 | get_vi_for_tree (var->decl); |
27c2cfa6 | 6827 | } |
25a6a873 RG |
6828 | |
6829 | if (dump_file) | |
6830 | { | |
6831 | fprintf (dump_file, | |
6832 | "Generating constraints for global initializers\n\n"); | |
6833 | dump_constraints (dump_file, 0); | |
6834 | fprintf (dump_file, "\n"); | |
6835 | } | |
6836 | from = VEC_length (constraint_t, constraints); | |
6837 | ||
4ee00913 DB |
6838 | for (node = cgraph_nodes; node; node = node->next) |
6839 | { | |
5c245b95 RG |
6840 | struct function *func; |
6841 | basic_block bb; | |
6842 | tree old_func_decl; | |
4ee00913 | 6843 | |
5c245b95 | 6844 | /* Nodes without a body are not interesting. */ |
1565af08 | 6845 | if (!cgraph_function_with_gimple_body_p (node)) |
5c245b95 | 6846 | continue; |
c58936b6 | 6847 | |
5c245b95 | 6848 | if (dump_file) |
27c2cfa6 RG |
6849 | { |
6850 | fprintf (dump_file, | |
6851 | "Generating constraints for %s", cgraph_node_name (node)); | |
6852 | if (DECL_ASSEMBLER_NAME_SET_P (node->decl)) | |
6853 | fprintf (dump_file, " (%s)", | |
6854 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl))); | |
6855 | fprintf (dump_file, "\n"); | |
6856 | } | |
c58936b6 | 6857 | |
5c245b95 RG |
6858 | func = DECL_STRUCT_FUNCTION (node->decl); |
6859 | old_func_decl = current_function_decl; | |
6860 | push_cfun (func); | |
6861 | current_function_decl = node->decl; | |
726a989a | 6862 | |
1565af08 RG |
6863 | /* For externally visible or attribute used annotated functions use |
6864 | local constraints for their arguments. | |
6865 | For local functions we see all callers and thus do not need initial | |
6866 | constraints for parameters. */ | |
6867 | if (node->reachable_from_other_partition | |
6868 | || node->local.externally_visible | |
6869 | || node->needed) | |
194313e2 | 6870 | { |
194313e2 RG |
6871 | intra_create_variable_infos (); |
6872 | ||
6873 | /* We also need to make function return values escape. Nothing | |
6874 | escapes by returning from main though. */ | |
6875 | if (!MAIN_NAME_P (DECL_NAME (node->decl))) | |
6876 | { | |
6877 | varinfo_t fi, rvi; | |
6878 | fi = lookup_vi_for_tree (node->decl); | |
6879 | rvi = first_vi_for_offset (fi, fi_result); | |
6880 | if (rvi && rvi->offset == fi_result) | |
6881 | { | |
6882 | struct constraint_expr includes; | |
6883 | struct constraint_expr var; | |
6884 | includes.var = escaped_id; | |
6885 | includes.offset = 0; | |
6886 | includes.type = SCALAR; | |
6887 | var.var = rvi->id; | |
6888 | var.offset = 0; | |
6889 | var.type = SCALAR; | |
6890 | process_constraint (new_constraint (includes, var)); | |
6891 | } | |
6892 | } | |
6893 | } | |
4ee00913 | 6894 | |
5c245b95 RG |
6895 | /* Build constriants for the function body. */ |
6896 | FOR_EACH_BB_FN (bb, func) | |
6897 | { | |
6898 | gimple_stmt_iterator gsi; | |
c58936b6 | 6899 | |
5c245b95 RG |
6900 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
6901 | gsi_next (&gsi)) | |
6902 | { | |
6903 | gimple phi = gsi_stmt (gsi); | |
c58936b6 | 6904 | |
5c245b95 RG |
6905 | if (is_gimple_reg (gimple_phi_result (phi))) |
6906 | find_func_aliases (phi); | |
6907 | } | |
3e5937d7 | 6908 | |
5c245b95 RG |
6909 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
6910 | { | |
6911 | gimple stmt = gsi_stmt (gsi); | |
3e5937d7 | 6912 | |
5c245b95 | 6913 | find_func_aliases (stmt); |
25a6a873 | 6914 | find_func_clobbers (stmt); |
5c245b95 RG |
6915 | } |
6916 | } | |
c58936b6 | 6917 | |
5c245b95 RG |
6918 | current_function_decl = old_func_decl; |
6919 | pop_cfun (); | |
25a6a873 RG |
6920 | |
6921 | if (dump_file) | |
6922 | { | |
6923 | fprintf (dump_file, "\n"); | |
6924 | dump_constraints (dump_file, from); | |
6925 | fprintf (dump_file, "\n"); | |
6926 | } | |
6927 | from = VEC_length (constraint_t, constraints); | |
5c245b95 | 6928 | } |
c58936b6 | 6929 | |
5c245b95 RG |
6930 | /* From the constraints compute the points-to sets. */ |
6931 | solve_constraints (); | |
c58936b6 | 6932 | |
25a6a873 RG |
6933 | /* Compute the global points-to sets for ESCAPED. |
6934 | ??? Note that the computed escape set is not correct | |
6935 | for the whole unit as we fail to consider graph edges to | |
6936 | externally visible functions. */ | |
6937 | find_what_var_points_to (get_varinfo (escaped_id), &ipa_escaped_pt); | |
6938 | ||
6939 | /* Make sure the ESCAPED solution (which is used as placeholder in | |
6940 | other solutions) does not reference itself. This simplifies | |
6941 | points-to solution queries. */ | |
6942 | ipa_escaped_pt.ipa_escaped = 0; | |
6943 | ||
6944 | /* Assign the points-to sets to the SSA names in the unit. */ | |
6945 | for (node = cgraph_nodes; node; node = node->next) | |
6946 | { | |
6947 | tree ptr; | |
6948 | struct function *fn; | |
6949 | unsigned i; | |
6950 | varinfo_t fi; | |
6951 | basic_block bb; | |
6952 | struct pt_solution uses, clobbers; | |
6953 | struct cgraph_edge *e; | |
6954 | ||
6955 | /* Nodes without a body are not interesting. */ | |
1565af08 | 6956 | if (!cgraph_function_with_gimple_body_p (node)) |
25a6a873 RG |
6957 | continue; |
6958 | ||
6959 | fn = DECL_STRUCT_FUNCTION (node->decl); | |
6960 | ||
6961 | /* Compute the points-to sets for pointer SSA_NAMEs. */ | |
ac47786e | 6962 | FOR_EACH_VEC_ELT (tree, fn->gimple_df->ssa_names, i, ptr) |
25a6a873 RG |
6963 | { |
6964 | if (ptr | |
6965 | && POINTER_TYPE_P (TREE_TYPE (ptr))) | |
6966 | find_what_p_points_to (ptr); | |
6967 | } | |
6968 | ||
6969 | /* Compute the call-use and call-clobber sets for all direct calls. */ | |
6970 | fi = lookup_vi_for_tree (node->decl); | |
6971 | gcc_assert (fi->is_fn_info); | |
6972 | find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers), | |
6973 | &clobbers); | |
6974 | find_what_var_points_to (first_vi_for_offset (fi, fi_uses), &uses); | |
6975 | for (e = node->callers; e; e = e->next_caller) | |
6976 | { | |
6977 | if (!e->call_stmt) | |
6978 | continue; | |
6979 | ||
6980 | *gimple_call_clobber_set (e->call_stmt) = clobbers; | |
6981 | *gimple_call_use_set (e->call_stmt) = uses; | |
6982 | } | |
6983 | ||
6984 | /* Compute the call-use and call-clobber sets for indirect calls | |
6985 | and calls to external functions. */ | |
6986 | FOR_EACH_BB_FN (bb, fn) | |
6987 | { | |
6988 | gimple_stmt_iterator gsi; | |
6989 | ||
6990 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6991 | { | |
6992 | gimple stmt = gsi_stmt (gsi); | |
6993 | struct pt_solution *pt; | |
6994 | varinfo_t vi; | |
6995 | tree decl; | |
6996 | ||
6997 | if (!is_gimple_call (stmt)) | |
6998 | continue; | |
6999 | ||
7000 | /* Handle direct calls to external functions. */ | |
7001 | decl = gimple_call_fndecl (stmt); | |
7002 | if (decl | |
7003 | && (!(fi = lookup_vi_for_tree (decl)) | |
7004 | || !fi->is_fn_info)) | |
7005 | { | |
7006 | pt = gimple_call_use_set (stmt); | |
7007 | if (gimple_call_flags (stmt) & ECF_CONST) | |
7008 | memset (pt, 0, sizeof (struct pt_solution)); | |
7009 | else if ((vi = lookup_call_use_vi (stmt)) != NULL) | |
7010 | { | |
7011 | find_what_var_points_to (vi, pt); | |
7012 | /* Escaped (and thus nonlocal) variables are always | |
7013 | implicitly used by calls. */ | |
7014 | /* ??? ESCAPED can be empty even though NONLOCAL | |
7015 | always escaped. */ | |
7016 | pt->nonlocal = 1; | |
7017 | pt->ipa_escaped = 1; | |
7018 | } | |
7019 | else | |
7020 | { | |
7021 | /* If there is nothing special about this call then | |
7022 | we have made everything that is used also escape. */ | |
7023 | *pt = ipa_escaped_pt; | |
7024 | pt->nonlocal = 1; | |
7025 | } | |
7026 | ||
7027 | pt = gimple_call_clobber_set (stmt); | |
7028 | if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS)) | |
7029 | memset (pt, 0, sizeof (struct pt_solution)); | |
7030 | else if ((vi = lookup_call_clobber_vi (stmt)) != NULL) | |
7031 | { | |
7032 | find_what_var_points_to (vi, pt); | |
7033 | /* Escaped (and thus nonlocal) variables are always | |
7034 | implicitly clobbered by calls. */ | |
7035 | /* ??? ESCAPED can be empty even though NONLOCAL | |
7036 | always escaped. */ | |
7037 | pt->nonlocal = 1; | |
7038 | pt->ipa_escaped = 1; | |
7039 | } | |
7040 | else | |
7041 | { | |
7042 | /* If there is nothing special about this call then | |
7043 | we have made everything that is used also escape. */ | |
7044 | *pt = ipa_escaped_pt; | |
7045 | pt->nonlocal = 1; | |
7046 | } | |
7047 | } | |
7048 | ||
7049 | /* Handle indirect calls. */ | |
7050 | if (!decl | |
7051 | && (fi = get_fi_for_callee (stmt))) | |
7052 | { | |
7053 | /* We need to accumulate all clobbers/uses of all possible | |
7054 | callees. */ | |
7055 | fi = get_varinfo (find (fi->id)); | |
7056 | /* If we cannot constrain the set of functions we'll end up | |
7057 | calling we end up using/clobbering everything. */ | |
7058 | if (bitmap_bit_p (fi->solution, anything_id) | |
7059 | || bitmap_bit_p (fi->solution, nonlocal_id) | |
7060 | || bitmap_bit_p (fi->solution, escaped_id)) | |
7061 | { | |
7062 | pt_solution_reset (gimple_call_clobber_set (stmt)); | |
7063 | pt_solution_reset (gimple_call_use_set (stmt)); | |
7064 | } | |
7065 | else | |
7066 | { | |
7067 | bitmap_iterator bi; | |
7068 | unsigned i; | |
7069 | struct pt_solution *uses, *clobbers; | |
7070 | ||
7071 | uses = gimple_call_use_set (stmt); | |
7072 | clobbers = gimple_call_clobber_set (stmt); | |
7073 | memset (uses, 0, sizeof (struct pt_solution)); | |
7074 | memset (clobbers, 0, sizeof (struct pt_solution)); | |
7075 | EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi) | |
7076 | { | |
7077 | struct pt_solution sol; | |
7078 | ||
7079 | vi = get_varinfo (i); | |
7080 | if (!vi->is_fn_info) | |
7081 | { | |
7082 | /* ??? We could be more precise here? */ | |
7083 | uses->nonlocal = 1; | |
7084 | uses->ipa_escaped = 1; | |
7085 | clobbers->nonlocal = 1; | |
7086 | clobbers->ipa_escaped = 1; | |
7087 | continue; | |
7088 | } | |
7089 | ||
7090 | if (!uses->anything) | |
7091 | { | |
7092 | find_what_var_points_to | |
7093 | (first_vi_for_offset (vi, fi_uses), &sol); | |
7094 | pt_solution_ior_into (uses, &sol); | |
7095 | } | |
7096 | if (!clobbers->anything) | |
7097 | { | |
7098 | find_what_var_points_to | |
7099 | (first_vi_for_offset (vi, fi_clobbers), &sol); | |
7100 | pt_solution_ior_into (clobbers, &sol); | |
7101 | } | |
7102 | } | |
7103 | } | |
7104 | } | |
7105 | } | |
7106 | } | |
7107 | ||
7108 | fn->gimple_df->ipa_pta = true; | |
7109 | } | |
7110 | ||
5c245b95 | 7111 | delete_points_to_sets (); |
c58936b6 | 7112 | |
4ee00913 | 7113 | in_ipa_mode = 0; |
5c245b95 | 7114 | |
c2924966 | 7115 | return 0; |
4ee00913 | 7116 | } |
c58936b6 | 7117 | |
8ddbbcae | 7118 | struct simple_ipa_opt_pass pass_ipa_pta = |
4ee00913 | 7119 | { |
8ddbbcae JH |
7120 | { |
7121 | SIMPLE_IPA_PASS, | |
4ee00913 DB |
7122 | "pta", /* name */ |
7123 | gate_ipa_pta, /* gate */ | |
7124 | ipa_pta_execute, /* execute */ | |
7125 | NULL, /* sub */ | |
7126 | NULL, /* next */ | |
7127 | 0, /* static_pass_number */ | |
7128 | TV_IPA_PTA, /* tv_id */ | |
7129 | 0, /* properties_required */ | |
7130 | 0, /* properties_provided */ | |
7131 | 0, /* properties_destroyed */ | |
7132 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
7133 | TODO_update_ssa /* todo_flags_finish */ |
7134 | } | |
4ee00913 | 7135 | }; |