]>
Commit | Line | Data |
---|---|---|
910fdc79 | 1 | /* Tree based points-to analysis |
c75c517d SB |
2 | Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 |
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 DB |
364 | ret->solution = BITMAP_ALLOC (&pta_obstack); |
365 | ret->oldsolution = BITMAP_ALLOC (&oldpta_obstack); | |
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 DB |
1506 | BITMAP_FREE (get_varinfo (from)->solution); |
1507 | BITMAP_FREE (get_varinfo (from)->oldsolution); | |
b8698a0f | 1508 | |
aa46c8a3 DB |
1509 | if (stats.iterations > 0) |
1510 | { | |
1511 | BITMAP_FREE (get_varinfo (to)->oldsolution); | |
1512 | get_varinfo (to)->oldsolution = BITMAP_ALLOC (&oldpta_obstack); | |
910fdc79 | 1513 | } |
3e5937d7 | 1514 | } |
3e5937d7 DB |
1515 | if (valid_graph_edge (graph, to, to)) |
1516 | { | |
1517 | if (graph->succs[to]) | |
1518 | bitmap_clear_bit (graph->succs[to], to); | |
910fdc79 | 1519 | } |
910fdc79 DB |
1520 | } |
1521 | ||
910fdc79 DB |
1522 | /* Information needed to compute the topological ordering of a graph. */ |
1523 | ||
1524 | struct topo_info | |
1525 | { | |
1526 | /* sbitmap of visited nodes. */ | |
1527 | sbitmap visited; | |
1528 | /* Array that stores the topological order of the graph, *in | |
1529 | reverse*. */ | |
740e80e8 | 1530 | VEC(unsigned,heap) *topo_order; |
910fdc79 DB |
1531 | }; |
1532 | ||
1533 | ||
1534 | /* Initialize and return a topological info structure. */ | |
1535 | ||
1536 | static struct topo_info * | |
1537 | init_topo_info (void) | |
1538 | { | |
7b765bed | 1539 | size_t size = graph->size; |
5ed6ace5 | 1540 | struct topo_info *ti = XNEW (struct topo_info); |
910fdc79 DB |
1541 | ti->visited = sbitmap_alloc (size); |
1542 | sbitmap_zero (ti->visited); | |
740e80e8 | 1543 | ti->topo_order = VEC_alloc (unsigned, heap, 1); |
910fdc79 DB |
1544 | return ti; |
1545 | } | |
1546 | ||
1547 | ||
1548 | /* Free the topological sort info pointed to by TI. */ | |
1549 | ||
1550 | static void | |
1551 | free_topo_info (struct topo_info *ti) | |
1552 | { | |
1553 | sbitmap_free (ti->visited); | |
740e80e8 | 1554 | VEC_free (unsigned, heap, ti->topo_order); |
910fdc79 DB |
1555 | free (ti); |
1556 | } | |
1557 | ||
1558 | /* Visit the graph in topological order, and store the order in the | |
1559 | topo_info structure. */ | |
1560 | ||
1561 | static void | |
1562 | topo_visit (constraint_graph_t graph, struct topo_info *ti, | |
1563 | unsigned int n) | |
1564 | { | |
4ee00913 | 1565 | bitmap_iterator bi; |
4ee00913 DB |
1566 | unsigned int j; |
1567 | ||
910fdc79 | 1568 | SET_BIT (ti->visited, n); |
4ee00913 | 1569 | |
3e5937d7 DB |
1570 | if (graph->succs[n]) |
1571 | EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi) | |
4ee00913 DB |
1572 | { |
1573 | if (!TEST_BIT (ti->visited, j)) | |
1574 | topo_visit (graph, ti, j); | |
1575 | } | |
3e5937d7 | 1576 | |
740e80e8 | 1577 | VEC_safe_push (unsigned, heap, ti->topo_order, n); |
910fdc79 DB |
1578 | } |
1579 | ||
5006671f RG |
1580 | /* Process a constraint C that represents x = *(y + off), using DELTA as the |
1581 | starting solution for y. */ | |
910fdc79 DB |
1582 | |
1583 | static void | |
1584 | do_sd_constraint (constraint_graph_t graph, constraint_t c, | |
1585 | bitmap delta) | |
1586 | { | |
7b765bed | 1587 | unsigned int lhs = c->lhs.var; |
910fdc79 DB |
1588 | bool flag = false; |
1589 | bitmap sol = get_varinfo (lhs)->solution; | |
1590 | unsigned int j; | |
1591 | bitmap_iterator bi; | |
5006671f | 1592 | HOST_WIDE_INT roffset = c->rhs.offset; |
4ee00913 | 1593 | |
5006671f RG |
1594 | /* Our IL does not allow this. */ |
1595 | gcc_assert (c->lhs.offset == 0); | |
0e1f4c6b | 1596 | |
5006671f RG |
1597 | /* If the solution of Y contains anything it is good enough to transfer |
1598 | this to the LHS. */ | |
14c28276 RG |
1599 | if (bitmap_bit_p (delta, anything_id)) |
1600 | { | |
1601 | flag |= bitmap_set_bit (sol, anything_id); | |
1602 | goto done; | |
1603 | } | |
1604 | ||
5006671f RG |
1605 | /* If we do not know at with offset the rhs is dereferenced compute |
1606 | the reachability set of DELTA, conservatively assuming it is | |
1607 | dereferenced at all valid offsets. */ | |
1608 | if (roffset == UNKNOWN_OFFSET) | |
1609 | { | |
1610 | solution_set_expand (delta, delta); | |
1611 | /* No further offset processing is necessary. */ | |
1612 | roffset = 0; | |
1613 | } | |
1614 | ||
c58936b6 | 1615 | /* For each variable j in delta (Sol(y)), add |
910fdc79 DB |
1616 | an edge in the graph from j to x, and union Sol(j) into Sol(x). */ |
1617 | EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi) | |
1618 | { | |
5006671f RG |
1619 | varinfo_t v = get_varinfo (j); |
1620 | HOST_WIDE_INT fieldoffset = v->offset + roffset; | |
1621 | unsigned int t; | |
1622 | ||
1623 | if (v->is_full_var) | |
1624 | fieldoffset = v->offset; | |
1625 | else if (roffset != 0) | |
1626 | v = first_vi_for_offset (v, fieldoffset); | |
1627 | /* If the access is outside of the variable we can ignore it. */ | |
1628 | if (!v) | |
1629 | continue; | |
910fdc79 | 1630 | |
5006671f RG |
1631 | do |
1632 | { | |
3e5937d7 | 1633 | t = find (v->id); |
4ee00913 DB |
1634 | |
1635 | /* Adding edges from the special vars is pointless. | |
1636 | They don't have sets that can change. */ | |
b7091901 | 1637 | if (get_varinfo (t)->is_special_var) |
4ee00913 | 1638 | flag |= bitmap_ior_into (sol, get_varinfo (t)->solution); |
b7091901 | 1639 | /* Merging the solution from ESCAPED needlessly increases |
472c7fbd | 1640 | the set. Use ESCAPED as representative instead. */ |
5006671f | 1641 | else if (v->id == escaped_id) |
6a66f28e | 1642 | flag |= bitmap_set_bit (sol, escaped_id); |
3c323b52 RG |
1643 | else if (v->may_have_pointers |
1644 | && add_graph_edge (graph, lhs, t)) | |
4ee00913 | 1645 | flag |= bitmap_ior_into (sol, get_varinfo (t)->solution); |
5006671f RG |
1646 | |
1647 | /* If the variable is not exactly at the requested offset | |
1648 | we have to include the next one. */ | |
1649 | if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset | |
1650 | || v->next == NULL) | |
1651 | break; | |
1652 | ||
1653 | v = v->next; | |
1654 | fieldoffset = v->offset; | |
910fdc79 | 1655 | } |
5006671f | 1656 | while (1); |
910fdc79 | 1657 | } |
4cf4d6a3 | 1658 | |
4ee00913 | 1659 | done: |
910fdc79 DB |
1660 | /* If the LHS solution changed, mark the var as changed. */ |
1661 | if (flag) | |
1662 | { | |
1663 | get_varinfo (lhs)->solution = sol; | |
648b5f85 | 1664 | bitmap_set_bit (changed, lhs); |
c58936b6 | 1665 | } |
910fdc79 DB |
1666 | } |
1667 | ||
5006671f RG |
1668 | /* Process a constraint C that represents *(x + off) = y using DELTA |
1669 | as the starting solution for x. */ | |
910fdc79 DB |
1670 | |
1671 | static void | |
57250223 | 1672 | do_ds_constraint (constraint_t c, bitmap delta) |
910fdc79 | 1673 | { |
7b765bed | 1674 | unsigned int rhs = c->rhs.var; |
910fdc79 DB |
1675 | bitmap sol = get_varinfo (rhs)->solution; |
1676 | unsigned int j; | |
1677 | bitmap_iterator bi; | |
5006671f | 1678 | HOST_WIDE_INT loff = c->lhs.offset; |
11152c95 | 1679 | bool escaped_p = false; |
910fdc79 | 1680 | |
9e39dba6 RG |
1681 | /* Our IL does not allow this. */ |
1682 | gcc_assert (c->rhs.offset == 0); | |
1683 | ||
1684 | /* If the solution of y contains ANYTHING simply use the ANYTHING | |
1685 | solution. This avoids needlessly increasing the points-to sets. */ | |
1686 | if (bitmap_bit_p (sol, anything_id)) | |
1687 | sol = get_varinfo (find (anything_id))->solution; | |
1688 | ||
1689 | /* If the solution for x contains ANYTHING we have to merge the | |
1690 | solution of y into all pointer variables which we do via | |
1691 | STOREDANYTHING. */ | |
1692 | if (bitmap_bit_p (delta, anything_id)) | |
1693 | { | |
1694 | unsigned t = find (storedanything_id); | |
1695 | if (add_graph_edge (graph, t, rhs)) | |
1696 | { | |
1697 | if (bitmap_ior_into (get_varinfo (t)->solution, sol)) | |
648b5f85 | 1698 | bitmap_set_bit (changed, t); |
9e39dba6 RG |
1699 | } |
1700 | return; | |
1701 | } | |
4ee00913 | 1702 | |
5006671f RG |
1703 | /* If we do not know at with offset the rhs is dereferenced compute |
1704 | the reachability set of DELTA, conservatively assuming it is | |
1705 | dereferenced at all valid offsets. */ | |
1706 | if (loff == UNKNOWN_OFFSET) | |
1707 | { | |
1708 | solution_set_expand (delta, delta); | |
1709 | loff = 0; | |
1710 | } | |
1711 | ||
910fdc79 DB |
1712 | /* For each member j of delta (Sol(x)), add an edge from y to j and |
1713 | union Sol(y) into Sol(j) */ | |
1714 | EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi) | |
1715 | { | |
5006671f RG |
1716 | varinfo_t v = get_varinfo (j); |
1717 | unsigned int t; | |
1718 | HOST_WIDE_INT fieldoffset = v->offset + loff; | |
c58936b6 | 1719 | |
5006671f RG |
1720 | if (v->is_full_var) |
1721 | fieldoffset = v->offset; | |
1722 | else if (loff != 0) | |
1723 | v = first_vi_for_offset (v, fieldoffset); | |
1724 | /* If the access is outside of the variable we can ignore it. */ | |
1725 | if (!v) | |
1726 | continue; | |
57250223 | 1727 | |
5006671f RG |
1728 | do |
1729 | { | |
9e39dba6 | 1730 | if (v->may_have_pointers) |
910fdc79 | 1731 | { |
11152c95 RG |
1732 | /* If v is a global variable then this is an escape point. */ |
1733 | if (v->is_global_var | |
1734 | && !escaped_p) | |
1735 | { | |
1736 | t = find (escaped_id); | |
1737 | if (add_graph_edge (graph, t, rhs) | |
648b5f85 RG |
1738 | && bitmap_ior_into (get_varinfo (t)->solution, sol)) |
1739 | bitmap_set_bit (changed, t); | |
11152c95 RG |
1740 | /* Enough to let rhs escape once. */ |
1741 | escaped_p = true; | |
1742 | } | |
1743 | ||
1744 | if (v->is_special_var) | |
1745 | break; | |
1746 | ||
9e39dba6 | 1747 | t = find (v->id); |
de70bb20 | 1748 | if (add_graph_edge (graph, t, rhs) |
648b5f85 RG |
1749 | && bitmap_ior_into (get_varinfo (t)->solution, sol)) |
1750 | bitmap_set_bit (changed, t); | |
de70bb20 | 1751 | } |
5006671f RG |
1752 | |
1753 | /* If the variable is not exactly at the requested offset | |
1754 | we have to include the next one. */ | |
1755 | if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset | |
1756 | || v->next == NULL) | |
1757 | break; | |
1758 | ||
1759 | v = v->next; | |
1760 | fieldoffset = v->offset; | |
57250223 | 1761 | } |
5006671f | 1762 | while (1); |
910fdc79 DB |
1763 | } |
1764 | } | |
1765 | ||
3e5937d7 DB |
1766 | /* Handle a non-simple (simple meaning requires no iteration), |
1767 | constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */ | |
c58936b6 | 1768 | |
910fdc79 DB |
1769 | static void |
1770 | do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta) | |
1771 | { | |
1772 | if (c->lhs.type == DEREF) | |
1773 | { | |
1774 | if (c->rhs.type == ADDRESSOF) | |
1775 | { | |
7b765bed | 1776 | gcc_unreachable(); |
910fdc79 DB |
1777 | } |
1778 | else | |
1779 | { | |
1780 | /* *x = y */ | |
57250223 | 1781 | do_ds_constraint (c, delta); |
910fdc79 DB |
1782 | } |
1783 | } | |
57250223 | 1784 | else if (c->rhs.type == DEREF) |
910fdc79 DB |
1785 | { |
1786 | /* x = *y */ | |
13c2c08b DB |
1787 | if (!(get_varinfo (c->lhs.var)->is_special_var)) |
1788 | do_sd_constraint (graph, c, delta); | |
910fdc79 | 1789 | } |
c58936b6 | 1790 | else |
57250223 | 1791 | { |
c58936b6 | 1792 | bitmap tmp; |
57250223 DB |
1793 | bitmap solution; |
1794 | bool flag = false; | |
57250223 | 1795 | |
62e5bf5d | 1796 | gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR); |
7b765bed DB |
1797 | solution = get_varinfo (c->rhs.var)->solution; |
1798 | tmp = get_varinfo (c->lhs.var)->solution; | |
57250223 DB |
1799 | |
1800 | flag = set_union_with_increment (tmp, solution, c->rhs.offset); | |
c58936b6 | 1801 | |
57250223 DB |
1802 | if (flag) |
1803 | { | |
7b765bed | 1804 | get_varinfo (c->lhs.var)->solution = tmp; |
648b5f85 | 1805 | bitmap_set_bit (changed, c->lhs.var); |
57250223 DB |
1806 | } |
1807 | } | |
910fdc79 DB |
1808 | } |
1809 | ||
1810 | /* Initialize and return a new SCC info structure. */ | |
1811 | ||
1812 | static struct scc_info * | |
3e5937d7 | 1813 | init_scc_info (size_t size) |
910fdc79 | 1814 | { |
5ed6ace5 | 1815 | struct scc_info *si = XNEW (struct scc_info); |
3e5937d7 | 1816 | size_t i; |
910fdc79 DB |
1817 | |
1818 | si->current_index = 0; | |
1819 | si->visited = sbitmap_alloc (size); | |
1820 | sbitmap_zero (si->visited); | |
7b765bed DB |
1821 | si->deleted = sbitmap_alloc (size); |
1822 | sbitmap_zero (si->deleted); | |
3e5937d7 DB |
1823 | si->node_mapping = XNEWVEC (unsigned int, size); |
1824 | si->dfs = XCNEWVEC (unsigned int, size); | |
1825 | ||
1826 | for (i = 0; i < size; i++) | |
1827 | si->node_mapping[i] = i; | |
1828 | ||
740e80e8 | 1829 | si->scc_stack = VEC_alloc (unsigned, heap, 1); |
910fdc79 DB |
1830 | return si; |
1831 | } | |
1832 | ||
1833 | /* Free an SCC info structure pointed to by SI */ | |
1834 | ||
1835 | static void | |
1836 | free_scc_info (struct scc_info *si) | |
c58936b6 | 1837 | { |
910fdc79 | 1838 | sbitmap_free (si->visited); |
7b765bed | 1839 | sbitmap_free (si->deleted); |
3e5937d7 DB |
1840 | free (si->node_mapping); |
1841 | free (si->dfs); | |
740e80e8 | 1842 | VEC_free (unsigned, heap, si->scc_stack); |
3e5937d7 | 1843 | free (si); |
910fdc79 DB |
1844 | } |
1845 | ||
1846 | ||
3e5937d7 DB |
1847 | /* Find indirect cycles in GRAPH that occur, using strongly connected |
1848 | components, and note them in the indirect cycles map. | |
1849 | ||
1850 | This technique comes from Ben Hardekopf and Calvin Lin, | |
1851 | "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of | |
1852 | Lines of Code", submitted to PLDI 2007. */ | |
910fdc79 DB |
1853 | |
1854 | static void | |
3e5937d7 | 1855 | find_indirect_cycles (constraint_graph_t graph) |
910fdc79 DB |
1856 | { |
1857 | unsigned int i; | |
3e5937d7 DB |
1858 | unsigned int size = graph->size; |
1859 | struct scc_info *si = init_scc_info (size); | |
910fdc79 | 1860 | |
3e5937d7 DB |
1861 | for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ ) |
1862 | if (!TEST_BIT (si->visited, i) && find (i) == i) | |
910fdc79 | 1863 | scc_visit (graph, si, i); |
c58936b6 | 1864 | |
910fdc79 DB |
1865 | free_scc_info (si); |
1866 | } | |
1867 | ||
1868 | /* Compute a topological ordering for GRAPH, and store the result in the | |
1869 | topo_info structure TI. */ | |
1870 | ||
c58936b6 | 1871 | static void |
910fdc79 DB |
1872 | compute_topo_order (constraint_graph_t graph, |
1873 | struct topo_info *ti) | |
1874 | { | |
1875 | unsigned int i; | |
7b765bed | 1876 | unsigned int size = graph->size; |
c58936b6 | 1877 | |
910fdc79 | 1878 | for (i = 0; i != size; ++i) |
3e5937d7 | 1879 | if (!TEST_BIT (ti->visited, i) && find (i) == i) |
910fdc79 DB |
1880 | topo_visit (graph, ti, i); |
1881 | } | |
1882 | ||
7b765bed DB |
1883 | /* Structure used to for hash value numbering of pointer equivalence |
1884 | classes. */ | |
1885 | ||
1886 | typedef struct equiv_class_label | |
1887 | { | |
3691626c | 1888 | hashval_t hashcode; |
7b765bed DB |
1889 | unsigned int equivalence_class; |
1890 | bitmap labels; | |
7b765bed | 1891 | } *equiv_class_label_t; |
586de218 | 1892 | typedef const struct equiv_class_label *const_equiv_class_label_t; |
7b765bed DB |
1893 | |
1894 | /* A hashtable for mapping a bitmap of labels->pointer equivalence | |
1895 | classes. */ | |
1896 | static htab_t pointer_equiv_class_table; | |
1897 | ||
1898 | /* A hashtable for mapping a bitmap of labels->location equivalence | |
1899 | classes. */ | |
1900 | static htab_t location_equiv_class_table; | |
1901 | ||
1902 | /* Hash function for a equiv_class_label_t */ | |
1903 | ||
1904 | static hashval_t | |
1905 | equiv_class_label_hash (const void *p) | |
1906 | { | |
586de218 | 1907 | const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p; |
7b765bed DB |
1908 | return ecl->hashcode; |
1909 | } | |
1910 | ||
1911 | /* Equality function for two equiv_class_label_t's. */ | |
1912 | ||
1913 | static int | |
1914 | equiv_class_label_eq (const void *p1, const void *p2) | |
1915 | { | |
586de218 KG |
1916 | const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1; |
1917 | const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2; | |
821bb7f8 RG |
1918 | return (eql1->hashcode == eql2->hashcode |
1919 | && bitmap_equal_p (eql1->labels, eql2->labels)); | |
7b765bed DB |
1920 | } |
1921 | ||
1922 | /* Lookup a equivalence class in TABLE by the bitmap of LABELS it | |
1923 | contains. */ | |
1924 | ||
1925 | static unsigned int | |
1926 | equiv_class_lookup (htab_t table, bitmap labels) | |
1927 | { | |
1928 | void **slot; | |
1929 | struct equiv_class_label ecl; | |
1930 | ||
1931 | ecl.labels = labels; | |
1932 | ecl.hashcode = bitmap_hash (labels); | |
c58936b6 | 1933 | |
7b765bed DB |
1934 | slot = htab_find_slot_with_hash (table, &ecl, |
1935 | ecl.hashcode, NO_INSERT); | |
1936 | if (!slot) | |
1937 | return 0; | |
1938 | else | |
1939 | return ((equiv_class_label_t) *slot)->equivalence_class; | |
1940 | } | |
1941 | ||
1942 | ||
1943 | /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS | |
1944 | to TABLE. */ | |
1945 | ||
1946 | static void | |
1947 | equiv_class_add (htab_t table, unsigned int equivalence_class, | |
1948 | bitmap labels) | |
1949 | { | |
1950 | void **slot; | |
1951 | equiv_class_label_t ecl = XNEW (struct equiv_class_label); | |
1952 | ||
1953 | ecl->labels = labels; | |
1954 | ecl->equivalence_class = equivalence_class; | |
1955 | ecl->hashcode = bitmap_hash (labels); | |
1956 | ||
1957 | slot = htab_find_slot_with_hash (table, ecl, | |
1958 | ecl->hashcode, INSERT); | |
1959 | gcc_assert (!*slot); | |
1960 | *slot = (void *) ecl; | |
1961 | } | |
1962 | ||
1963 | /* Perform offline variable substitution. | |
910fdc79 | 1964 | |
7b765bed DB |
1965 | This is a worst case quadratic time way of identifying variables |
1966 | that must have equivalent points-to sets, including those caused by | |
1967 | static cycles, and single entry subgraphs, in the constraint graph. | |
3e5937d7 | 1968 | |
7b765bed DB |
1969 | The technique is described in "Exploiting Pointer and Location |
1970 | Equivalence to Optimize Pointer Analysis. In the 14th International | |
1971 | Static Analysis Symposium (SAS), August 2007." It is known as the | |
1972 | "HU" algorithm, and is equivalent to value numbering the collapsed | |
1973 | constraint graph including evaluating unions. | |
3e5937d7 DB |
1974 | |
1975 | The general method of finding equivalence classes is as follows: | |
1976 | Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints. | |
7b765bed DB |
1977 | Initialize all non-REF nodes to be direct nodes. |
1978 | For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh | |
1979 | variable} | |
1980 | For each constraint containing the dereference, we also do the same | |
1981 | thing. | |
1982 | ||
1983 | We then compute SCC's in the graph and unify nodes in the same SCC, | |
1984 | including pts sets. | |
1985 | ||
1986 | For each non-collapsed node x: | |
1987 | Visit all unvisited explicit incoming edges. | |
1988 | Ignoring all non-pointers, set pts(x) = Union of pts(a) for y | |
1989 | where y->x. | |
1990 | Lookup the equivalence class for pts(x). | |
1991 | If we found one, equivalence_class(x) = found class. | |
1992 | Otherwise, equivalence_class(x) = new class, and new_class is | |
1993 | added to the lookup table. | |
3e5937d7 DB |
1994 | |
1995 | All direct nodes with the same equivalence class can be replaced | |
1996 | with a single representative node. | |
1997 | All unlabeled nodes (label == 0) are not pointers and all edges | |
1998 | involving them can be eliminated. | |
7b765bed DB |
1999 | We perform these optimizations during rewrite_constraints |
2000 | ||
2001 | In addition to pointer equivalence class finding, we also perform | |
2002 | location equivalence class finding. This is the set of variables | |
2003 | that always appear together in points-to sets. We use this to | |
2004 | compress the size of the points-to sets. */ | |
2005 | ||
2006 | /* Current maximum pointer equivalence class id. */ | |
2007 | static int pointer_equiv_class; | |
3e5937d7 | 2008 | |
7b765bed DB |
2009 | /* Current maximum location equivalence class id. */ |
2010 | static int location_equiv_class; | |
3e5937d7 DB |
2011 | |
2012 | /* Recursive routine to find strongly connected components in GRAPH, | |
7b765bed | 2013 | and label it's nodes with DFS numbers. */ |
910fdc79 DB |
2014 | |
2015 | static void | |
7b765bed | 2016 | condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) |
910fdc79 | 2017 | { |
3e5937d7 DB |
2018 | unsigned int i; |
2019 | bitmap_iterator bi; | |
2020 | unsigned int my_dfs; | |
c58936b6 | 2021 | |
3e5937d7 DB |
2022 | gcc_assert (si->node_mapping[n] == n); |
2023 | SET_BIT (si->visited, n); | |
2024 | si->dfs[n] = si->current_index ++; | |
2025 | my_dfs = si->dfs[n]; | |
c58936b6 | 2026 | |
3e5937d7 DB |
2027 | /* Visit all the successors. */ |
2028 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi) | |
910fdc79 | 2029 | { |
3e5937d7 | 2030 | unsigned int w = si->node_mapping[i]; |
910fdc79 | 2031 | |
7b765bed | 2032 | if (TEST_BIT (si->deleted, w)) |
910fdc79 DB |
2033 | continue; |
2034 | ||
3e5937d7 | 2035 | if (!TEST_BIT (si->visited, w)) |
7b765bed | 2036 | condense_visit (graph, si, w); |
3e5937d7 DB |
2037 | { |
2038 | unsigned int t = si->node_mapping[w]; | |
2039 | unsigned int nnode = si->node_mapping[n]; | |
62e5bf5d | 2040 | gcc_assert (nnode == n); |
910fdc79 | 2041 | |
3e5937d7 DB |
2042 | if (si->dfs[t] < si->dfs[nnode]) |
2043 | si->dfs[n] = si->dfs[t]; | |
2044 | } | |
2045 | } | |
910fdc79 | 2046 | |
3e5937d7 DB |
2047 | /* Visit all the implicit predecessors. */ |
2048 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi) | |
2049 | { | |
2050 | unsigned int w = si->node_mapping[i]; | |
2051 | ||
7b765bed | 2052 | if (TEST_BIT (si->deleted, w)) |
3e5937d7 DB |
2053 | continue; |
2054 | ||
2055 | if (!TEST_BIT (si->visited, w)) | |
7b765bed | 2056 | condense_visit (graph, si, w); |
3e5937d7 DB |
2057 | { |
2058 | unsigned int t = si->node_mapping[w]; | |
2059 | unsigned int nnode = si->node_mapping[n]; | |
2060 | gcc_assert (nnode == n); | |
2061 | ||
2062 | if (si->dfs[t] < si->dfs[nnode]) | |
2063 | si->dfs[n] = si->dfs[t]; | |
2064 | } | |
2065 | } | |
4ee00913 | 2066 | |
3e5937d7 DB |
2067 | /* See if any components have been identified. */ |
2068 | if (si->dfs[n] == my_dfs) | |
2069 | { | |
2070 | while (VEC_length (unsigned, si->scc_stack) != 0 | |
2071 | && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs) | |
910fdc79 | 2072 | { |
3e5937d7 DB |
2073 | unsigned int w = VEC_pop (unsigned, si->scc_stack); |
2074 | si->node_mapping[w] = n; | |
2075 | ||
2076 | if (!TEST_BIT (graph->direct_nodes, w)) | |
2077 | RESET_BIT (graph->direct_nodes, n); | |
3e5937d7 | 2078 | |
7b765bed DB |
2079 | /* Unify our nodes. */ |
2080 | if (graph->preds[w]) | |
2081 | { | |
2082 | if (!graph->preds[n]) | |
2083 | graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2084 | bitmap_ior_into (graph->preds[n], graph->preds[w]); | |
2085 | } | |
2086 | if (graph->implicit_preds[w]) | |
2087 | { | |
2088 | if (!graph->implicit_preds[n]) | |
2089 | graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2090 | bitmap_ior_into (graph->implicit_preds[n], | |
2091 | graph->implicit_preds[w]); | |
2092 | } | |
2093 | if (graph->points_to[w]) | |
2094 | { | |
2095 | if (!graph->points_to[n]) | |
2096 | graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2097 | bitmap_ior_into (graph->points_to[n], | |
2098 | graph->points_to[w]); | |
2099 | } | |
3e5937d7 | 2100 | } |
7b765bed | 2101 | SET_BIT (si->deleted, n); |
3e5937d7 DB |
2102 | } |
2103 | else | |
2104 | VEC_safe_push (unsigned, heap, si->scc_stack, n); | |
2105 | } | |
2106 | ||
7b765bed DB |
2107 | /* Label pointer equivalences. */ |
2108 | ||
2109 | static void | |
2110 | label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n) | |
2111 | { | |
2112 | unsigned int i; | |
2113 | bitmap_iterator bi; | |
2114 | SET_BIT (si->visited, n); | |
2115 | ||
2116 | if (!graph->points_to[n]) | |
2117 | graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack); | |
2118 | ||
2119 | /* Label and union our incoming edges's points to sets. */ | |
2120 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi) | |
2121 | { | |
2122 | unsigned int w = si->node_mapping[i]; | |
2123 | if (!TEST_BIT (si->visited, w)) | |
2124 | label_visit (graph, si, w); | |
2125 | ||
2126 | /* Skip unused edges */ | |
2127 | if (w == n || graph->pointer_label[w] == 0) | |
3dc21182 DB |
2128 | continue; |
2129 | ||
7b765bed DB |
2130 | if (graph->points_to[w]) |
2131 | bitmap_ior_into(graph->points_to[n], graph->points_to[w]); | |
7b765bed DB |
2132 | } |
2133 | /* Indirect nodes get fresh variables. */ | |
2134 | if (!TEST_BIT (graph->direct_nodes, n)) | |
2135 | bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n); | |
2136 | ||
2137 | if (!bitmap_empty_p (graph->points_to[n])) | |
2138 | { | |
2139 | unsigned int label = equiv_class_lookup (pointer_equiv_class_table, | |
2140 | graph->points_to[n]); | |
2141 | if (!label) | |
2142 | { | |
7b765bed DB |
2143 | label = pointer_equiv_class++; |
2144 | equiv_class_add (pointer_equiv_class_table, | |
2145 | label, graph->points_to[n]); | |
2146 | } | |
2147 | graph->pointer_label[n] = label; | |
2148 | } | |
2149 | } | |
2150 | ||
3e5937d7 DB |
2151 | /* Perform offline variable substitution, discovering equivalence |
2152 | classes, and eliminating non-pointer variables. */ | |
2153 | ||
2154 | static struct scc_info * | |
2155 | perform_var_substitution (constraint_graph_t graph) | |
2156 | { | |
2157 | unsigned int i; | |
2158 | unsigned int size = graph->size; | |
2159 | struct scc_info *si = init_scc_info (size); | |
2160 | ||
2161 | bitmap_obstack_initialize (&iteration_obstack); | |
7b765bed DB |
2162 | pointer_equiv_class_table = htab_create (511, equiv_class_label_hash, |
2163 | equiv_class_label_eq, free); | |
2164 | location_equiv_class_table = htab_create (511, equiv_class_label_hash, | |
2165 | equiv_class_label_eq, free); | |
2166 | pointer_equiv_class = 1; | |
2167 | location_equiv_class = 1; | |
2168 | ||
2169 | /* Condense the nodes, which means to find SCC's, count incoming | |
2170 | predecessors, and unite nodes in SCC's. */ | |
aa46c8a3 | 2171 | for (i = 0; i < FIRST_REF_NODE; i++) |
7b765bed DB |
2172 | if (!TEST_BIT (si->visited, si->node_mapping[i])) |
2173 | condense_visit (graph, si, si->node_mapping[i]); | |
3e5937d7 | 2174 | |
7b765bed DB |
2175 | sbitmap_zero (si->visited); |
2176 | /* Actually the label the nodes for pointer equivalences */ | |
aa46c8a3 | 2177 | for (i = 0; i < FIRST_REF_NODE; i++) |
3e5937d7 DB |
2178 | if (!TEST_BIT (si->visited, si->node_mapping[i])) |
2179 | label_visit (graph, si, si->node_mapping[i]); | |
2180 | ||
7b765bed DB |
2181 | /* Calculate location equivalence labels. */ |
2182 | for (i = 0; i < FIRST_REF_NODE; i++) | |
2183 | { | |
2184 | bitmap pointed_by; | |
2185 | bitmap_iterator bi; | |
2186 | unsigned int j; | |
2187 | unsigned int label; | |
2188 | ||
2189 | if (!graph->pointed_by[i]) | |
2190 | continue; | |
2191 | pointed_by = BITMAP_ALLOC (&iteration_obstack); | |
2192 | ||
2193 | /* Translate the pointed-by mapping for pointer equivalence | |
2194 | labels. */ | |
2195 | EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi) | |
2196 | { | |
2197 | bitmap_set_bit (pointed_by, | |
2198 | graph->pointer_label[si->node_mapping[j]]); | |
2199 | } | |
2200 | /* The original pointed_by is now dead. */ | |
2201 | BITMAP_FREE (graph->pointed_by[i]); | |
2202 | ||
2203 | /* Look up the location equivalence label if one exists, or make | |
2204 | one otherwise. */ | |
2205 | label = equiv_class_lookup (location_equiv_class_table, | |
2206 | pointed_by); | |
2207 | if (label == 0) | |
2208 | { | |
2209 | label = location_equiv_class++; | |
2210 | equiv_class_add (location_equiv_class_table, | |
2211 | label, pointed_by); | |
2212 | } | |
2213 | else | |
2214 | { | |
2215 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2216 | fprintf (dump_file, "Found location equivalence for node %s\n", | |
2217 | get_varinfo (i)->name); | |
2218 | BITMAP_FREE (pointed_by); | |
2219 | } | |
2220 | graph->loc_label[i] = label; | |
2221 | ||
2222 | } | |
2223 | ||
3e5937d7 DB |
2224 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2225 | for (i = 0; i < FIRST_REF_NODE; i++) | |
2226 | { | |
2227 | bool direct_node = TEST_BIT (graph->direct_nodes, i); | |
2228 | fprintf (dump_file, | |
7b765bed DB |
2229 | "Equivalence classes for %s node id %d:%s are pointer: %d" |
2230 | ", location:%d\n", | |
3e5937d7 | 2231 | direct_node ? "Direct node" : "Indirect node", i, |
62e5bf5d | 2232 | get_varinfo (i)->name, |
7b765bed DB |
2233 | graph->pointer_label[si->node_mapping[i]], |
2234 | graph->loc_label[si->node_mapping[i]]); | |
3e5937d7 DB |
2235 | } |
2236 | ||
2237 | /* Quickly eliminate our non-pointer variables. */ | |
2238 | ||
2239 | for (i = 0; i < FIRST_REF_NODE; i++) | |
2240 | { | |
2241 | unsigned int node = si->node_mapping[i]; | |
2242 | ||
aa46c8a3 | 2243 | if (graph->pointer_label[node] == 0) |
3e5937d7 | 2244 | { |
23e73993 | 2245 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3e5937d7 DB |
2246 | fprintf (dump_file, |
2247 | "%s is a non-pointer variable, eliminating edges.\n", | |
2248 | get_varinfo (node)->name); | |
2249 | stats.nonpointer_vars++; | |
2250 | clear_edges_for_node (graph, node); | |
910fdc79 DB |
2251 | } |
2252 | } | |
7b765bed | 2253 | |
3e5937d7 DB |
2254 | return si; |
2255 | } | |
2256 | ||
2257 | /* Free information that was only necessary for variable | |
2258 | substitution. */ | |
910fdc79 | 2259 | |
3e5937d7 DB |
2260 | static void |
2261 | free_var_substitution_info (struct scc_info *si) | |
2262 | { | |
2263 | free_scc_info (si); | |
7b765bed DB |
2264 | free (graph->pointer_label); |
2265 | free (graph->loc_label); | |
2266 | free (graph->pointed_by); | |
2267 | free (graph->points_to); | |
3e5937d7 DB |
2268 | free (graph->eq_rep); |
2269 | sbitmap_free (graph->direct_nodes); | |
7b765bed DB |
2270 | htab_delete (pointer_equiv_class_table); |
2271 | htab_delete (location_equiv_class_table); | |
4ee00913 | 2272 | bitmap_obstack_release (&iteration_obstack); |
3e5937d7 DB |
2273 | } |
2274 | ||
2275 | /* Return an existing node that is equivalent to NODE, which has | |
2276 | equivalence class LABEL, if one exists. Return NODE otherwise. */ | |
2277 | ||
2278 | static unsigned int | |
2279 | find_equivalent_node (constraint_graph_t graph, | |
2280 | unsigned int node, unsigned int label) | |
2281 | { | |
2282 | /* If the address version of this variable is unused, we can | |
2283 | substitute it for anything else with the same label. | |
2284 | Otherwise, we know the pointers are equivalent, but not the | |
7b765bed | 2285 | locations, and we can unite them later. */ |
3e5937d7 | 2286 | |
7b765bed | 2287 | if (!bitmap_bit_p (graph->address_taken, node)) |
3e5937d7 DB |
2288 | { |
2289 | gcc_assert (label < graph->size); | |
2290 | ||
2291 | if (graph->eq_rep[label] != -1) | |
2292 | { | |
2293 | /* Unify the two variables since we know they are equivalent. */ | |
2294 | if (unite (graph->eq_rep[label], node)) | |
2295 | unify_nodes (graph, graph->eq_rep[label], node, false); | |
2296 | return graph->eq_rep[label]; | |
2297 | } | |
2298 | else | |
2299 | { | |
2300 | graph->eq_rep[label] = node; | |
7b765bed | 2301 | graph->pe_rep[label] = node; |
3e5937d7 DB |
2302 | } |
2303 | } | |
7b765bed DB |
2304 | else |
2305 | { | |
2306 | gcc_assert (label < graph->size); | |
2307 | graph->pe[node] = label; | |
2308 | if (graph->pe_rep[label] == -1) | |
2309 | graph->pe_rep[label] = node; | |
2310 | } | |
2311 | ||
3e5937d7 DB |
2312 | return node; |
2313 | } | |
2314 | ||
7b765bed DB |
2315 | /* Unite pointer equivalent but not location equivalent nodes in |
2316 | GRAPH. This may only be performed once variable substitution is | |
2317 | finished. */ | |
2318 | ||
2319 | static void | |
2320 | unite_pointer_equivalences (constraint_graph_t graph) | |
2321 | { | |
2322 | unsigned int i; | |
2323 | ||
2324 | /* Go through the pointer equivalences and unite them to their | |
2325 | representative, if they aren't already. */ | |
aa46c8a3 | 2326 | for (i = 0; i < FIRST_REF_NODE; i++) |
7b765bed DB |
2327 | { |
2328 | unsigned int label = graph->pe[i]; | |
aa46c8a3 DB |
2329 | if (label) |
2330 | { | |
2331 | int label_rep = graph->pe_rep[label]; | |
b8698a0f | 2332 | |
aa46c8a3 DB |
2333 | if (label_rep == -1) |
2334 | continue; | |
b8698a0f | 2335 | |
aa46c8a3 DB |
2336 | label_rep = find (label_rep); |
2337 | if (label_rep >= 0 && unite (label_rep, find (i))) | |
2338 | unify_nodes (graph, label_rep, i, false); | |
2339 | } | |
7b765bed DB |
2340 | } |
2341 | } | |
2342 | ||
2343 | /* Move complex constraints to the GRAPH nodes they belong to. */ | |
3e5937d7 DB |
2344 | |
2345 | static void | |
7b765bed DB |
2346 | move_complex_constraints (constraint_graph_t graph) |
2347 | { | |
2348 | int i; | |
2349 | constraint_t c; | |
2350 | ||
ac47786e | 2351 | FOR_EACH_VEC_ELT (constraint_t, constraints, i, c) |
7b765bed DB |
2352 | { |
2353 | if (c) | |
2354 | { | |
2355 | struct constraint_expr lhs = c->lhs; | |
2356 | struct constraint_expr rhs = c->rhs; | |
2357 | ||
2358 | if (lhs.type == DEREF) | |
2359 | { | |
2360 | insert_into_complex (graph, lhs.var, c); | |
2361 | } | |
2362 | else if (rhs.type == DEREF) | |
2363 | { | |
2364 | if (!(get_varinfo (lhs.var)->is_special_var)) | |
2365 | insert_into_complex (graph, rhs.var, c); | |
2366 | } | |
2367 | else if (rhs.type != ADDRESSOF && lhs.var > anything_id | |
2368 | && (lhs.offset != 0 || rhs.offset != 0)) | |
2369 | { | |
2370 | insert_into_complex (graph, rhs.var, c); | |
2371 | } | |
2372 | } | |
2373 | } | |
2374 | } | |
2375 | ||
2376 | ||
2377 | /* Optimize and rewrite complex constraints while performing | |
2378 | collapsing of equivalent nodes. SI is the SCC_INFO that is the | |
2379 | result of perform_variable_substitution. */ | |
2380 | ||
2381 | static void | |
2382 | rewrite_constraints (constraint_graph_t graph, | |
2383 | struct scc_info *si) | |
3e5937d7 DB |
2384 | { |
2385 | int i; | |
2386 | unsigned int j; | |
2387 | constraint_t c; | |
2388 | ||
2389 | for (j = 0; j < graph->size; j++) | |
2390 | gcc_assert (find (j) == j); | |
2391 | ||
ac47786e | 2392 | FOR_EACH_VEC_ELT (constraint_t, constraints, i, c) |
3e5937d7 DB |
2393 | { |
2394 | struct constraint_expr lhs = c->lhs; | |
2395 | struct constraint_expr rhs = c->rhs; | |
5006671f RG |
2396 | unsigned int lhsvar = find (lhs.var); |
2397 | unsigned int rhsvar = find (rhs.var); | |
3e5937d7 DB |
2398 | unsigned int lhsnode, rhsnode; |
2399 | unsigned int lhslabel, rhslabel; | |
2400 | ||
2401 | lhsnode = si->node_mapping[lhsvar]; | |
2402 | rhsnode = si->node_mapping[rhsvar]; | |
7b765bed DB |
2403 | lhslabel = graph->pointer_label[lhsnode]; |
2404 | rhslabel = graph->pointer_label[rhsnode]; | |
3e5937d7 DB |
2405 | |
2406 | /* See if it is really a non-pointer variable, and if so, ignore | |
2407 | the constraint. */ | |
2408 | if (lhslabel == 0) | |
2409 | { | |
aa46c8a3 | 2410 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3e5937d7 | 2411 | { |
b8698a0f | 2412 | |
aa46c8a3 DB |
2413 | fprintf (dump_file, "%s is a non-pointer variable," |
2414 | "ignoring constraint:", | |
2415 | get_varinfo (lhs.var)->name); | |
2416 | dump_constraint (dump_file, c); | |
8576f20a | 2417 | fprintf (dump_file, "\n"); |
3e5937d7 | 2418 | } |
aa46c8a3 DB |
2419 | VEC_replace (constraint_t, constraints, i, NULL); |
2420 | continue; | |
3e5937d7 DB |
2421 | } |
2422 | ||
2423 | if (rhslabel == 0) | |
2424 | { | |
aa46c8a3 | 2425 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3e5937d7 | 2426 | { |
b8698a0f | 2427 | |
aa46c8a3 DB |
2428 | fprintf (dump_file, "%s is a non-pointer variable," |
2429 | "ignoring constraint:", | |
2430 | get_varinfo (rhs.var)->name); | |
2431 | dump_constraint (dump_file, c); | |
8576f20a | 2432 | fprintf (dump_file, "\n"); |
3e5937d7 | 2433 | } |
aa46c8a3 DB |
2434 | VEC_replace (constraint_t, constraints, i, NULL); |
2435 | continue; | |
3e5937d7 DB |
2436 | } |
2437 | ||
2438 | lhsvar = find_equivalent_node (graph, lhsvar, lhslabel); | |
2439 | rhsvar = find_equivalent_node (graph, rhsvar, rhslabel); | |
2440 | c->lhs.var = lhsvar; | |
2441 | c->rhs.var = rhsvar; | |
2442 | ||
3e5937d7 DB |
2443 | } |
2444 | } | |
2445 | ||
2446 | /* Eliminate indirect cycles involving NODE. Return true if NODE was | |
2447 | part of an SCC, false otherwise. */ | |
2448 | ||
2449 | static bool | |
2450 | eliminate_indirect_cycles (unsigned int node) | |
2451 | { | |
2452 | if (graph->indirect_cycles[node] != -1 | |
2453 | && !bitmap_empty_p (get_varinfo (node)->solution)) | |
2454 | { | |
2455 | unsigned int i; | |
2456 | VEC(unsigned,heap) *queue = NULL; | |
2457 | int queuepos; | |
2458 | unsigned int to = find (graph->indirect_cycles[node]); | |
2459 | bitmap_iterator bi; | |
2460 | ||
2461 | /* We can't touch the solution set and call unify_nodes | |
2462 | at the same time, because unify_nodes is going to do | |
2463 | bitmap unions into it. */ | |
2464 | ||
2465 | EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi) | |
2466 | { | |
2467 | if (find (i) == i && i != to) | |
2468 | { | |
2469 | if (unite (to, i)) | |
2470 | VEC_safe_push (unsigned, heap, queue, i); | |
2471 | } | |
2472 | } | |
2473 | ||
2474 | for (queuepos = 0; | |
2475 | VEC_iterate (unsigned, queue, queuepos, i); | |
2476 | queuepos++) | |
2477 | { | |
2478 | unify_nodes (graph, to, i, true); | |
2479 | } | |
2480 | VEC_free (unsigned, heap, queue); | |
2481 | return true; | |
2482 | } | |
2483 | return false; | |
910fdc79 DB |
2484 | } |
2485 | ||
910fdc79 DB |
2486 | /* Solve the constraint graph GRAPH using our worklist solver. |
2487 | This is based on the PW* family of solvers from the "Efficient Field | |
2488 | Sensitive Pointer Analysis for C" paper. | |
2489 | It works by iterating over all the graph nodes, processing the complex | |
2490 | constraints and propagating the copy constraints, until everything stops | |
2491 | changed. This corresponds to steps 6-8 in the solving list given above. */ | |
2492 | ||
2493 | static void | |
2494 | solve_graph (constraint_graph_t graph) | |
2495 | { | |
7b765bed | 2496 | unsigned int size = graph->size; |
910fdc79 | 2497 | unsigned int i; |
3e5937d7 | 2498 | bitmap pts; |
910fdc79 | 2499 | |
648b5f85 | 2500 | changed = BITMAP_ALLOC (NULL); |
c58936b6 | 2501 | |
3e5937d7 | 2502 | /* Mark all initial non-collapsed nodes as changed. */ |
910fdc79 | 2503 | for (i = 0; i < size; i++) |
3e5937d7 DB |
2504 | { |
2505 | varinfo_t ivi = get_varinfo (i); | |
2506 | if (find (i) == i && !bitmap_empty_p (ivi->solution) | |
2507 | && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i])) | |
2508 | || VEC_length (constraint_t, graph->complex[i]) > 0)) | |
648b5f85 | 2509 | bitmap_set_bit (changed, i); |
3e5937d7 DB |
2510 | } |
2511 | ||
2512 | /* Allocate a bitmap to be used to store the changed bits. */ | |
2513 | pts = BITMAP_ALLOC (&pta_obstack); | |
c58936b6 | 2514 | |
648b5f85 | 2515 | while (!bitmap_empty_p (changed)) |
910fdc79 DB |
2516 | { |
2517 | unsigned int i; | |
2518 | struct topo_info *ti = init_topo_info (); | |
2519 | stats.iterations++; | |
4ee00913 | 2520 | |
910fdc79 | 2521 | bitmap_obstack_initialize (&iteration_obstack); |
c58936b6 | 2522 | |
910fdc79 DB |
2523 | compute_topo_order (graph, ti); |
2524 | ||
740e80e8 | 2525 | while (VEC_length (unsigned, ti->topo_order) != 0) |
910fdc79 | 2526 | { |
3e5937d7 | 2527 | |
740e80e8 | 2528 | i = VEC_pop (unsigned, ti->topo_order); |
3e5937d7 DB |
2529 | |
2530 | /* If this variable is not a representative, skip it. */ | |
2531 | if (find (i) != i) | |
2532 | continue; | |
2533 | ||
d3c36974 DB |
2534 | /* In certain indirect cycle cases, we may merge this |
2535 | variable to another. */ | |
62e5bf5d | 2536 | if (eliminate_indirect_cycles (i) && find (i) != i) |
d3c36974 | 2537 | continue; |
910fdc79 DB |
2538 | |
2539 | /* If the node has changed, we need to process the | |
2540 | complex constraints and outgoing edges again. */ | |
648b5f85 | 2541 | if (bitmap_clear_bit (changed, i)) |
910fdc79 DB |
2542 | { |
2543 | unsigned int j; | |
2544 | constraint_t c; | |
910fdc79 | 2545 | bitmap solution; |
3e5937d7 | 2546 | VEC(constraint_t,heap) *complex = graph->complex[i]; |
21392f19 | 2547 | bool solution_empty; |
48e540b0 | 2548 | |
3e5937d7 DB |
2549 | /* Compute the changed set of solution bits. */ |
2550 | bitmap_and_compl (pts, get_varinfo (i)->solution, | |
2551 | get_varinfo (i)->oldsolution); | |
2552 | ||
2553 | if (bitmap_empty_p (pts)) | |
2554 | continue; | |
2555 | ||
2556 | bitmap_ior_into (get_varinfo (i)->oldsolution, pts); | |
2557 | ||
910fdc79 | 2558 | solution = get_varinfo (i)->solution; |
21392f19 DB |
2559 | solution_empty = bitmap_empty_p (solution); |
2560 | ||
2561 | /* Process the complex constraints */ | |
ac47786e | 2562 | FOR_EACH_VEC_ELT (constraint_t, complex, j, c) |
21392f19 | 2563 | { |
7b765bed DB |
2564 | /* XXX: This is going to unsort the constraints in |
2565 | some cases, which will occasionally add duplicate | |
2566 | constraints during unification. This does not | |
2567 | affect correctness. */ | |
2568 | c->lhs.var = find (c->lhs.var); | |
2569 | c->rhs.var = find (c->rhs.var); | |
2570 | ||
21392f19 DB |
2571 | /* The only complex constraint that can change our |
2572 | solution to non-empty, given an empty solution, | |
2573 | is a constraint where the lhs side is receiving | |
2574 | some set from elsewhere. */ | |
2575 | if (!solution_empty || c->lhs.type != DEREF) | |
3e5937d7 | 2576 | do_complex_constraint (graph, c, pts); |
21392f19 | 2577 | } |
910fdc79 | 2578 | |
21392f19 DB |
2579 | solution_empty = bitmap_empty_p (solution); |
2580 | ||
5006671f | 2581 | if (!solution_empty) |
4ee00913 | 2582 | { |
3e5937d7 | 2583 | bitmap_iterator bi; |
5006671f | 2584 | unsigned eff_escaped_id = find (escaped_id); |
3e5937d7 | 2585 | |
21392f19 | 2586 | /* Propagate solution to all successors. */ |
c58936b6 | 2587 | EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], |
21392f19 | 2588 | 0, j, bi) |
4ee00913 | 2589 | { |
3e5937d7 DB |
2590 | bitmap tmp; |
2591 | bool flag; | |
2592 | ||
2593 | unsigned int to = find (j); | |
2594 | tmp = get_varinfo (to)->solution; | |
2595 | flag = false; | |
c58936b6 | 2596 | |
3e5937d7 DB |
2597 | /* Don't try to propagate to ourselves. */ |
2598 | if (to == i) | |
2599 | continue; | |
c58936b6 | 2600 | |
5006671f RG |
2601 | /* If we propagate from ESCAPED use ESCAPED as |
2602 | placeholder. */ | |
2603 | if (i == eff_escaped_id) | |
2604 | flag = bitmap_set_bit (tmp, escaped_id); | |
2605 | else | |
2606 | flag = set_union_with_increment (tmp, pts, 0); | |
c58936b6 | 2607 | |
21392f19 | 2608 | if (flag) |
4ee00913 | 2609 | { |
3e5937d7 | 2610 | get_varinfo (to)->solution = tmp; |
648b5f85 | 2611 | bitmap_set_bit (changed, to); |
4ee00913 DB |
2612 | } |
2613 | } | |
910fdc79 DB |
2614 | } |
2615 | } | |
2616 | } | |
2617 | free_topo_info (ti); | |
2618 | bitmap_obstack_release (&iteration_obstack); | |
2619 | } | |
c58936b6 | 2620 | |
3e5937d7 | 2621 | BITMAP_FREE (pts); |
648b5f85 | 2622 | BITMAP_FREE (changed); |
3e5937d7 | 2623 | bitmap_obstack_release (&oldpta_obstack); |
910fdc79 DB |
2624 | } |
2625 | ||
3e5937d7 | 2626 | /* Map from trees to variable infos. */ |
15814ba0 | 2627 | static struct pointer_map_t *vi_for_tree; |
910fdc79 | 2628 | |
910fdc79 | 2629 | |
15814ba0 | 2630 | /* Insert ID as the variable id for tree T in the vi_for_tree map. */ |
910fdc79 | 2631 | |
c58936b6 | 2632 | static void |
3e5937d7 | 2633 | insert_vi_for_tree (tree t, varinfo_t vi) |
910fdc79 | 2634 | { |
15814ba0 PB |
2635 | void **slot = pointer_map_insert (vi_for_tree, t); |
2636 | gcc_assert (vi); | |
910fdc79 | 2637 | gcc_assert (*slot == NULL); |
15814ba0 | 2638 | *slot = vi; |
910fdc79 DB |
2639 | } |
2640 | ||
3e5937d7 | 2641 | /* Find the variable info for tree T in VI_FOR_TREE. If T does not |
15814ba0 | 2642 | exist in the map, return NULL, otherwise, return the varinfo we found. */ |
910fdc79 | 2643 | |
15814ba0 PB |
2644 | static varinfo_t |
2645 | lookup_vi_for_tree (tree t) | |
910fdc79 | 2646 | { |
15814ba0 PB |
2647 | void **slot = pointer_map_contains (vi_for_tree, t); |
2648 | if (slot == NULL) | |
2649 | return NULL; | |
910fdc79 | 2650 | |
15814ba0 | 2651 | return (varinfo_t) *slot; |
910fdc79 DB |
2652 | } |
2653 | ||
2654 | /* Return a printable name for DECL */ | |
2655 | ||
2656 | static const char * | |
2657 | alias_get_name (tree decl) | |
2658 | { | |
27c2cfa6 | 2659 | const char *res; |
910fdc79 DB |
2660 | char *temp; |
2661 | int num_printed = 0; | |
2662 | ||
27c2cfa6 RG |
2663 | if (DECL_ASSEMBLER_NAME_SET_P (decl)) |
2664 | res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); | |
2665 | else | |
2666 | res= get_name (decl); | |
910fdc79 DB |
2667 | if (res != NULL) |
2668 | return res; | |
2669 | ||
2670 | res = "NULL"; | |
4f6c9110 RG |
2671 | if (!dump_file) |
2672 | return res; | |
2673 | ||
910fdc79 DB |
2674 | if (TREE_CODE (decl) == SSA_NAME) |
2675 | { | |
c58936b6 | 2676 | num_printed = asprintf (&temp, "%s_%u", |
910fdc79 DB |
2677 | alias_get_name (SSA_NAME_VAR (decl)), |
2678 | SSA_NAME_VERSION (decl)); | |
2679 | } | |
2680 | else if (DECL_P (decl)) | |
2681 | { | |
2682 | num_printed = asprintf (&temp, "D.%u", DECL_UID (decl)); | |
2683 | } | |
2684 | if (num_printed > 0) | |
2685 | { | |
2686 | res = ggc_strdup (temp); | |
2687 | free (temp); | |
2688 | } | |
2689 | return res; | |
2690 | } | |
2691 | ||
15814ba0 PB |
2692 | /* Find the variable id for tree T in the map. |
2693 | If T doesn't exist in the map, create an entry for it and return it. */ | |
910fdc79 | 2694 | |
3e5937d7 DB |
2695 | static varinfo_t |
2696 | get_vi_for_tree (tree t) | |
910fdc79 | 2697 | { |
15814ba0 PB |
2698 | void **slot = pointer_map_contains (vi_for_tree, t); |
2699 | if (slot == NULL) | |
3e5937d7 | 2700 | return get_varinfo (create_variable_info_for (t, alias_get_name (t))); |
c58936b6 | 2701 | |
15814ba0 | 2702 | return (varinfo_t) *slot; |
910fdc79 DB |
2703 | } |
2704 | ||
b14e9388 | 2705 | /* Get a scalar constraint expression for a new temporary variable. */ |
910fdc79 DB |
2706 | |
2707 | static struct constraint_expr | |
b14e9388 | 2708 | new_scalar_tmp_constraint_exp (const char *name) |
910fdc79 | 2709 | { |
b14e9388 | 2710 | struct constraint_expr tmp; |
b14e9388 | 2711 | varinfo_t vi; |
910fdc79 | 2712 | |
0bbf2ffa | 2713 | vi = new_var_info (NULL_TREE, name); |
b14e9388 RG |
2714 | vi->offset = 0; |
2715 | vi->size = -1; | |
2716 | vi->fullsize = -1; | |
2717 | vi->is_full_var = 1; | |
c0d459f0 | 2718 | |
b14e9388 RG |
2719 | tmp.var = vi->id; |
2720 | tmp.type = SCALAR; | |
2721 | tmp.offset = 0; | |
c0d459f0 | 2722 | |
b14e9388 | 2723 | return tmp; |
c0d459f0 RG |
2724 | } |
2725 | ||
2726 | /* Get a constraint expression vector from an SSA_VAR_P node. | |
2727 | If address_p is true, the result will be taken its address of. */ | |
2728 | ||
2729 | static void | |
2730 | get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p) | |
2731 | { | |
2732 | struct constraint_expr cexpr; | |
2733 | varinfo_t vi; | |
2734 | ||
2735 | /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */ | |
910fdc79 DB |
2736 | gcc_assert (SSA_VAR_P (t) || DECL_P (t)); |
2737 | ||
2738 | /* For parameters, get at the points-to set for the actual parm | |
2739 | decl. */ | |
c58936b6 | 2740 | if (TREE_CODE (t) == SSA_NAME |
6938f93f JH |
2741 | && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL |
2742 | || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL) | |
38635499 | 2743 | && SSA_NAME_IS_DEFAULT_DEF (t)) |
c0d459f0 RG |
2744 | { |
2745 | get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p); | |
2746 | return; | |
2747 | } | |
910fdc79 | 2748 | |
c0d459f0 RG |
2749 | vi = get_vi_for_tree (t); |
2750 | cexpr.var = vi->id; | |
910fdc79 | 2751 | cexpr.type = SCALAR; |
c0d459f0 | 2752 | cexpr.offset = 0; |
47bcb538 DB |
2753 | /* If we determine the result is "anything", and we know this is readonly, |
2754 | say it points to readonly memory instead. */ | |
2755 | if (cexpr.var == anything_id && TREE_READONLY (t)) | |
910fdc79 | 2756 | { |
c0d459f0 | 2757 | gcc_unreachable (); |
3e5937d7 | 2758 | cexpr.type = ADDRESSOF; |
910fdc79 DB |
2759 | cexpr.var = readonly_id; |
2760 | } | |
c58936b6 | 2761 | |
c0d459f0 RG |
2762 | /* If we are not taking the address of the constraint expr, add all |
2763 | sub-fiels of the variable as well. */ | |
de925a03 RG |
2764 | if (!address_p |
2765 | && !vi->is_full_var) | |
c0d459f0 RG |
2766 | { |
2767 | for (; vi; vi = vi->next) | |
2768 | { | |
2769 | cexpr.var = vi->id; | |
2770 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
2771 | } | |
2772 | return; | |
2773 | } | |
2774 | ||
2775 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
910fdc79 DB |
2776 | } |
2777 | ||
faf2ecc5 RG |
2778 | /* Process constraint T, performing various simplifications and then |
2779 | adding it to our list of overall constraints. */ | |
910fdc79 DB |
2780 | |
2781 | static void | |
faf2ecc5 | 2782 | process_constraint (constraint_t t) |
910fdc79 DB |
2783 | { |
2784 | struct constraint_expr rhs = t->rhs; | |
2785 | struct constraint_expr lhs = t->lhs; | |
c58936b6 | 2786 | |
910fdc79 DB |
2787 | gcc_assert (rhs.var < VEC_length (varinfo_t, varmap)); |
2788 | gcc_assert (lhs.var < VEC_length (varinfo_t, varmap)); | |
2789 | ||
5006671f RG |
2790 | /* If we didn't get any useful constraint from the lhs we get |
2791 | &ANYTHING as fallback from get_constraint_for. Deal with | |
2792 | it here by turning it into *ANYTHING. */ | |
2793 | if (lhs.type == ADDRESSOF | |
2794 | && lhs.var == anything_id) | |
2795 | lhs.type = DEREF; | |
2796 | ||
2797 | /* ADDRESSOF on the lhs is invalid. */ | |
2798 | gcc_assert (lhs.type != ADDRESSOF); | |
910fdc79 | 2799 | |
3c323b52 RG |
2800 | /* We shouldn't add constraints from things that cannot have pointers. |
2801 | It's not completely trivial to avoid in the callers, so do it here. */ | |
2802 | if (rhs.type != ADDRESSOF | |
2803 | && !get_varinfo (rhs.var)->may_have_pointers) | |
2804 | return; | |
2805 | ||
2806 | /* Likewise adding to the solution of a non-pointer var isn't useful. */ | |
2807 | if (!get_varinfo (lhs.var)->may_have_pointers) | |
2808 | return; | |
2809 | ||
910fdc79 | 2810 | /* This can happen in our IR with things like n->a = *p */ |
5006671f | 2811 | if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id) |
910fdc79 DB |
2812 | { |
2813 | /* Split into tmp = *rhs, *lhs = tmp */ | |
b14e9388 RG |
2814 | struct constraint_expr tmplhs; |
2815 | tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp"); | |
faf2ecc5 RG |
2816 | process_constraint (new_constraint (tmplhs, rhs)); |
2817 | process_constraint (new_constraint (lhs, tmplhs)); | |
7b765bed DB |
2818 | } |
2819 | else if (rhs.type == ADDRESSOF && lhs.type == DEREF) | |
2820 | { | |
2821 | /* Split into tmp = &rhs, *lhs = tmp */ | |
b14e9388 RG |
2822 | struct constraint_expr tmplhs; |
2823 | tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp"); | |
faf2ecc5 RG |
2824 | process_constraint (new_constraint (tmplhs, rhs)); |
2825 | process_constraint (new_constraint (lhs, tmplhs)); | |
910fdc79 | 2826 | } |
910fdc79 DB |
2827 | else |
2828 | { | |
3e5937d7 | 2829 | gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0); |
b5efa470 | 2830 | VEC_safe_push (constraint_t, heap, constraints, t); |
910fdc79 DB |
2831 | } |
2832 | } | |
2833 | ||
2834 | ||
2835 | /* Return the position, in bits, of FIELD_DECL from the beginning of its | |
2836 | structure. */ | |
2837 | ||
ee7d4b57 | 2838 | static HOST_WIDE_INT |
910fdc79 DB |
2839 | bitpos_of_field (const tree fdecl) |
2840 | { | |
ee7d4b57 RG |
2841 | if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0) |
2842 | || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0)) | |
910fdc79 | 2843 | return -1; |
c58936b6 | 2844 | |
ea57f573 | 2845 | return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT |
ee7d4b57 | 2846 | + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl))); |
910fdc79 DB |
2847 | } |
2848 | ||
2849 | ||
e5bae89b RG |
2850 | /* Get constraint expressions for offsetting PTR by OFFSET. Stores the |
2851 | resulting constraint expressions in *RESULTS. */ | |
2852 | ||
2853 | static void | |
2854 | get_constraint_for_ptr_offset (tree ptr, tree offset, | |
2855 | VEC (ce_s, heap) **results) | |
2856 | { | |
bd02b3a0 | 2857 | struct constraint_expr c; |
e5bae89b | 2858 | unsigned int j, n; |
5006671f | 2859 | HOST_WIDE_INT rhsunitoffset, rhsoffset; |
e5bae89b RG |
2860 | |
2861 | /* If we do not do field-sensitive PTA adding offsets to pointers | |
2862 | does not change the points-to solution. */ | |
2863 | if (!use_field_sensitive) | |
2864 | { | |
ed6c4831 | 2865 | get_constraint_for_rhs (ptr, results); |
e5bae89b RG |
2866 | return; |
2867 | } | |
2868 | ||
2869 | /* If the offset is not a non-negative integer constant that fits | |
2870 | in a HOST_WIDE_INT, we have to fall back to a conservative | |
2871 | solution which includes all sub-fields of all pointed-to | |
5006671f | 2872 | variables of ptr. */ |
779704e7 RG |
2873 | if (offset == NULL_TREE |
2874 | || !host_integerp (offset, 0)) | |
5006671f RG |
2875 | rhsoffset = UNKNOWN_OFFSET; |
2876 | else | |
e5bae89b | 2877 | { |
5006671f RG |
2878 | /* Make sure the bit-offset also fits. */ |
2879 | rhsunitoffset = TREE_INT_CST_LOW (offset); | |
2880 | rhsoffset = rhsunitoffset * BITS_PER_UNIT; | |
2881 | if (rhsunitoffset != rhsoffset / BITS_PER_UNIT) | |
2882 | rhsoffset = UNKNOWN_OFFSET; | |
e5bae89b RG |
2883 | } |
2884 | ||
ed6c4831 | 2885 | get_constraint_for_rhs (ptr, results); |
e5bae89b RG |
2886 | if (rhsoffset == 0) |
2887 | return; | |
2888 | ||
2889 | /* As we are eventually appending to the solution do not use | |
2890 | VEC_iterate here. */ | |
2891 | n = VEC_length (ce_s, *results); | |
2892 | for (j = 0; j < n; j++) | |
2893 | { | |
2894 | varinfo_t curr; | |
bd02b3a0 RG |
2895 | c = *VEC_index (ce_s, *results, j); |
2896 | curr = get_varinfo (c.var); | |
e5bae89b | 2897 | |
bd02b3a0 | 2898 | if (c.type == ADDRESSOF |
5006671f RG |
2899 | /* If this varinfo represents a full variable just use it. */ |
2900 | && curr->is_full_var) | |
bd02b3a0 RG |
2901 | c.offset = 0; |
2902 | else if (c.type == ADDRESSOF | |
5006671f RG |
2903 | /* If we do not know the offset add all subfields. */ |
2904 | && rhsoffset == UNKNOWN_OFFSET) | |
2905 | { | |
2906 | varinfo_t temp = lookup_vi_for_tree (curr->decl); | |
2907 | do | |
2908 | { | |
2909 | struct constraint_expr c2; | |
2910 | c2.var = temp->id; | |
2911 | c2.type = ADDRESSOF; | |
2912 | c2.offset = 0; | |
bd02b3a0 | 2913 | if (c2.var != c.var) |
779704e7 | 2914 | VEC_safe_push (ce_s, heap, *results, &c2); |
5006671f RG |
2915 | temp = temp->next; |
2916 | } | |
2917 | while (temp); | |
2918 | } | |
bd02b3a0 | 2919 | else if (c.type == ADDRESSOF) |
e5bae89b | 2920 | { |
5006671f RG |
2921 | varinfo_t temp; |
2922 | unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset; | |
e5bae89b RG |
2923 | |
2924 | /* Search the sub-field which overlaps with the | |
5006671f RG |
2925 | pointed-to offset. If the result is outside of the variable |
2926 | we have to provide a conservative result, as the variable is | |
2927 | still reachable from the resulting pointer (even though it | |
2928 | technically cannot point to anything). The last and first | |
2929 | sub-fields are such conservative results. | |
e5bae89b RG |
2930 | ??? If we always had a sub-field for &object + 1 then |
2931 | we could represent this in a more precise way. */ | |
5006671f RG |
2932 | if (rhsoffset < 0 |
2933 | && curr->offset < offset) | |
2934 | offset = 0; | |
2935 | temp = first_or_preceding_vi_for_offset (curr, offset); | |
e5bae89b RG |
2936 | |
2937 | /* If the found variable is not exactly at the pointed to | |
2938 | result, we have to include the next variable in the | |
2939 | solution as well. Otherwise two increments by offset / 2 | |
2940 | do not result in the same or a conservative superset | |
2941 | solution. */ | |
5006671f | 2942 | if (temp->offset != offset |
e5bae89b RG |
2943 | && temp->next != NULL) |
2944 | { | |
2945 | struct constraint_expr c2; | |
2946 | c2.var = temp->next->id; | |
2947 | c2.type = ADDRESSOF; | |
2948 | c2.offset = 0; | |
2949 | VEC_safe_push (ce_s, heap, *results, &c2); | |
2950 | } | |
bd02b3a0 RG |
2951 | c.var = temp->id; |
2952 | c.offset = 0; | |
e5bae89b | 2953 | } |
e5bae89b | 2954 | else |
bd02b3a0 RG |
2955 | c.offset = rhsoffset; |
2956 | ||
2957 | VEC_replace (ce_s, *results, j, &c); | |
e5bae89b RG |
2958 | } |
2959 | } | |
2960 | ||
2961 | ||
c0d459f0 | 2962 | /* Given a COMPONENT_REF T, return the constraint_expr vector for it. |
ed6c4831 RG |
2963 | If address_p is true the result will be taken its address of. |
2964 | If lhs_p is true then the constraint expression is assumed to be used | |
2965 | as the lhs. */ | |
910fdc79 | 2966 | |
4ee00913 | 2967 | static void |
c0d459f0 | 2968 | get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results, |
ed6c4831 | 2969 | bool address_p, bool lhs_p) |
910fdc79 | 2970 | { |
4ee00913 | 2971 | tree orig_t = t; |
b1347638 | 2972 | HOST_WIDE_INT bitsize = -1; |
6bec9271 | 2973 | HOST_WIDE_INT bitmaxsize = -1; |
910fdc79 | 2974 | HOST_WIDE_INT bitpos; |
910fdc79 | 2975 | tree forzero; |
4ee00913 | 2976 | struct constraint_expr *result; |
910fdc79 DB |
2977 | |
2978 | /* Some people like to do cute things like take the address of | |
2979 | &0->a.b */ | |
2980 | forzero = t; | |
2ea9dc64 | 2981 | while (handled_component_p (forzero) |
70f34814 RG |
2982 | || INDIRECT_REF_P (forzero) |
2983 | || TREE_CODE (forzero) == MEM_REF) | |
4ee00913 | 2984 | forzero = TREE_OPERAND (forzero, 0); |
910fdc79 | 2985 | |
c58936b6 | 2986 | if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero)) |
910fdc79 | 2987 | { |
4ee00913 | 2988 | struct constraint_expr temp; |
c58936b6 | 2989 | |
4ee00913 DB |
2990 | temp.offset = 0; |
2991 | temp.var = integer_id; | |
2992 | temp.type = SCALAR; | |
2993 | VEC_safe_push (ce_s, heap, *results, &temp); | |
2994 | return; | |
910fdc79 | 2995 | } |
c58936b6 | 2996 | |
ed6c4831 RG |
2997 | /* Handle type-punning through unions. If we are extracting a pointer |
2998 | from a union via a possibly type-punning access that pointer | |
2999 | points to anything, similar to a conversion of an integer to | |
3000 | a pointer. */ | |
3001 | if (!lhs_p) | |
3002 | { | |
3003 | tree u; | |
3004 | for (u = t; | |
3005 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
3006 | u = TREE_OPERAND (u, 0)) | |
3007 | if (TREE_CODE (u) == COMPONENT_REF | |
3008 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
3009 | { | |
3010 | struct constraint_expr temp; | |
3011 | ||
3012 | temp.offset = 0; | |
3013 | temp.var = anything_id; | |
3014 | temp.type = ADDRESSOF; | |
3015 | VEC_safe_push (ce_s, heap, *results, &temp); | |
3016 | return; | |
3017 | } | |
3018 | } | |
3019 | ||
6bec9271 | 3020 | t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize); |
21392f19 | 3021 | |
c0d459f0 RG |
3022 | /* Pretend to take the address of the base, we'll take care of |
3023 | adding the required subset of sub-fields below. */ | |
ed6c4831 | 3024 | get_constraint_for_1 (t, results, true, lhs_p); |
c0d459f0 | 3025 | gcc_assert (VEC_length (ce_s, *results) == 1); |
e5bae89b | 3026 | result = VEC_last (ce_s, *results); |
910fdc79 | 3027 | |
e5bae89b RG |
3028 | if (result->type == SCALAR |
3029 | && get_varinfo (result->var)->is_full_var) | |
3030 | /* For single-field vars do not bother about the offset. */ | |
3031 | result->offset = 0; | |
3032 | else if (result->type == SCALAR) | |
910fdc79 DB |
3033 | { |
3034 | /* In languages like C, you can access one past the end of an | |
3035 | array. You aren't allowed to dereference it, so we can | |
3036 | ignore this constraint. When we handle pointer subtraction, | |
3037 | we may have to do something cute here. */ | |
c58936b6 | 3038 | |
c0d459f0 | 3039 | if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize |
18455d17 | 3040 | && bitmaxsize != 0) |
dd68d988 DB |
3041 | { |
3042 | /* It's also not true that the constraint will actually start at the | |
3043 | right offset, it may start in some padding. We only care about | |
3044 | setting the constraint to the first actual field it touches, so | |
c58936b6 | 3045 | walk to find it. */ |
c0d459f0 | 3046 | struct constraint_expr cexpr = *result; |
dd68d988 | 3047 | varinfo_t curr; |
c0d459f0 RG |
3048 | VEC_pop (ce_s, *results); |
3049 | cexpr.offset = 0; | |
3050 | for (curr = get_varinfo (cexpr.var); curr; curr = curr->next) | |
dd68d988 | 3051 | { |
63d195d5 | 3052 | if (ranges_overlap_p (curr->offset, curr->size, |
c0d459f0 | 3053 | bitpos, bitmaxsize)) |
dd68d988 | 3054 | { |
c0d459f0 RG |
3055 | cexpr.var = curr->id; |
3056 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
3057 | if (address_p) | |
3058 | break; | |
dd68d988 DB |
3059 | } |
3060 | } | |
e5bae89b RG |
3061 | /* If we are going to take the address of this field then |
3062 | to be able to compute reachability correctly add at least | |
3063 | the last field of the variable. */ | |
3064 | if (address_p | |
3065 | && VEC_length (ce_s, *results) == 0) | |
3066 | { | |
3067 | curr = get_varinfo (cexpr.var); | |
3068 | while (curr->next != NULL) | |
3069 | curr = curr->next; | |
3070 | cexpr.var = curr->id; | |
3071 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
3072 | } | |
0ba0772b | 3073 | else if (VEC_length (ce_s, *results) == 0) |
e5bae89b RG |
3074 | /* Assert that we found *some* field there. The user couldn't be |
3075 | accessing *only* padding. */ | |
3076 | /* Still the user could access one past the end of an array | |
3077 | embedded in a struct resulting in accessing *only* padding. */ | |
0ba0772b RB |
3078 | /* Or accessing only padding via type-punning to a type |
3079 | that has a filed just in padding space. */ | |
3080 | { | |
3081 | cexpr.type = SCALAR; | |
3082 | cexpr.var = anything_id; | |
3083 | cexpr.offset = 0; | |
3084 | VEC_safe_push (ce_s, heap, *results, &cexpr); | |
3085 | } | |
dd68d988 | 3086 | } |
18455d17 RG |
3087 | else if (bitmaxsize == 0) |
3088 | { | |
3089 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3090 | fprintf (dump_file, "Access to zero-sized part of variable," | |
3091 | "ignoring\n"); | |
3092 | } | |
910fdc79 DB |
3093 | else |
3094 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3095 | fprintf (dump_file, "Access to past the end of variable, ignoring\n"); | |
910fdc79 | 3096 | } |
5006671f | 3097 | else if (result->type == DEREF) |
7b765bed | 3098 | { |
5006671f RG |
3099 | /* If we do not know exactly where the access goes say so. Note |
3100 | that only for non-structure accesses we know that we access | |
3101 | at most one subfiled of any variable. */ | |
3102 | if (bitpos == -1 | |
3103 | || bitsize != bitmaxsize | |
1c09321c RG |
3104 | || AGGREGATE_TYPE_P (TREE_TYPE (orig_t)) |
3105 | || result->offset == UNKNOWN_OFFSET) | |
5006671f RG |
3106 | result->offset = UNKNOWN_OFFSET; |
3107 | else | |
1c09321c | 3108 | result->offset += bitpos; |
7b765bed | 3109 | } |
b51605c4 RG |
3110 | else if (result->type == ADDRESSOF) |
3111 | { | |
3112 | /* We can end up here for component references on a | |
3113 | VIEW_CONVERT_EXPR <>(&foobar). */ | |
3114 | result->type = SCALAR; | |
3115 | result->var = anything_id; | |
3116 | result->offset = 0; | |
3117 | } | |
c0d459f0 | 3118 | else |
5006671f | 3119 | gcc_unreachable (); |
910fdc79 DB |
3120 | } |
3121 | ||
3122 | ||
3123 | /* Dereference the constraint expression CONS, and return the result. | |
3124 | DEREF (ADDRESSOF) = SCALAR | |
3125 | DEREF (SCALAR) = DEREF | |
3126 | DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp)) | |
3127 | This is needed so that we can handle dereferencing DEREF constraints. */ | |
3128 | ||
4ee00913 DB |
3129 | static void |
3130 | do_deref (VEC (ce_s, heap) **constraints) | |
910fdc79 | 3131 | { |
4ee00913 DB |
3132 | struct constraint_expr *c; |
3133 | unsigned int i = 0; | |
c58936b6 | 3134 | |
ac47786e | 3135 | FOR_EACH_VEC_ELT (ce_s, *constraints, i, c) |
910fdc79 | 3136 | { |
4ee00913 DB |
3137 | if (c->type == SCALAR) |
3138 | c->type = DEREF; | |
3139 | else if (c->type == ADDRESSOF) | |
3140 | c->type = SCALAR; | |
3141 | else if (c->type == DEREF) | |
3142 | { | |
b14e9388 RG |
3143 | struct constraint_expr tmplhs; |
3144 | tmplhs = new_scalar_tmp_constraint_exp ("dereftmp"); | |
4ee00913 DB |
3145 | process_constraint (new_constraint (tmplhs, *c)); |
3146 | c->var = tmplhs.var; | |
3147 | } | |
3148 | else | |
3149 | gcc_unreachable (); | |
910fdc79 | 3150 | } |
910fdc79 DB |
3151 | } |
3152 | ||
1d24fdd9 RG |
3153 | /* Given a tree T, return the constraint expression for taking the |
3154 | address of it. */ | |
3155 | ||
3156 | static void | |
3157 | get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results) | |
3158 | { | |
3159 | struct constraint_expr *c; | |
3160 | unsigned int i; | |
3161 | ||
ed6c4831 | 3162 | get_constraint_for_1 (t, results, true, true); |
1d24fdd9 | 3163 | |
ac47786e | 3164 | FOR_EACH_VEC_ELT (ce_s, *results, i, c) |
1d24fdd9 RG |
3165 | { |
3166 | if (c->type == DEREF) | |
3167 | c->type = SCALAR; | |
3168 | else | |
3169 | c->type = ADDRESSOF; | |
3170 | } | |
3171 | } | |
3172 | ||
910fdc79 DB |
3173 | /* Given a tree T, return the constraint expression for it. */ |
3174 | ||
4ee00913 | 3175 | static void |
ed6c4831 RG |
3176 | get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p, |
3177 | bool lhs_p) | |
910fdc79 DB |
3178 | { |
3179 | struct constraint_expr temp; | |
3180 | ||
3181 | /* x = integer is all glommed to a single variable, which doesn't | |
3182 | point to anything by itself. That is, of course, unless it is an | |
3183 | integer constant being treated as a pointer, in which case, we | |
3184 | will return that this is really the addressof anything. This | |
3185 | happens below, since it will fall into the default case. The only | |
3186 | case we know something about an integer treated like a pointer is | |
3187 | when it is the NULL pointer, and then we just say it points to | |
89ebafc6 PB |
3188 | NULL. |
3189 | ||
3190 | Do not do that if -fno-delete-null-pointer-checks though, because | |
3191 | in that case *NULL does not fail, so it _should_ alias *anything. | |
3192 | It is not worth adding a new option or renaming the existing one, | |
3193 | since this case is relatively obscure. */ | |
8eb7bc3c RG |
3194 | if ((TREE_CODE (t) == INTEGER_CST |
3195 | && integer_zerop (t)) | |
3196 | /* The only valid CONSTRUCTORs in gimple with pointer typed | |
3197 | elements are zero-initializer. But in IPA mode we also | |
3198 | process global initializers, so verify at least. */ | |
3199 | || (TREE_CODE (t) == CONSTRUCTOR | |
3200 | && CONSTRUCTOR_NELTS (t) == 0)) | |
3201 | { | |
3202 | if (flag_delete_null_pointer_checks) | |
3203 | temp.var = nothing_id; | |
3204 | else | |
1f181fde | 3205 | temp.var = nonlocal_id; |
910fdc79 DB |
3206 | temp.type = ADDRESSOF; |
3207 | temp.offset = 0; | |
4ee00913 DB |
3208 | VEC_safe_push (ce_s, heap, *results, &temp); |
3209 | return; | |
910fdc79 DB |
3210 | } |
3211 | ||
bd1f29d9 EB |
3212 | /* String constants are read-only. */ |
3213 | if (TREE_CODE (t) == STRING_CST) | |
3214 | { | |
3215 | temp.var = readonly_id; | |
3216 | temp.type = SCALAR; | |
3217 | temp.offset = 0; | |
3218 | VEC_safe_push (ce_s, heap, *results, &temp); | |
3219 | return; | |
3220 | } | |
3221 | ||
910fdc79 DB |
3222 | switch (TREE_CODE_CLASS (TREE_CODE (t))) |
3223 | { | |
3224 | case tcc_expression: | |
3225 | { | |
3226 | switch (TREE_CODE (t)) | |
3227 | { | |
3228 | case ADDR_EXPR: | |
1d24fdd9 RG |
3229 | get_constraint_for_address_of (TREE_OPERAND (t, 0), results); |
3230 | return; | |
e5bae89b | 3231 | default:; |
910fdc79 | 3232 | } |
e5bae89b | 3233 | break; |
910fdc79 DB |
3234 | } |
3235 | case tcc_reference: | |
3236 | { | |
3237 | switch (TREE_CODE (t)) | |
3238 | { | |
70f34814 | 3239 | case MEM_REF: |
910fdc79 | 3240 | { |
de2184c0 | 3241 | struct constraint_expr cs; |
343b2efc | 3242 | varinfo_t vi, curr; |
1c09321c RG |
3243 | tree off = double_int_to_tree (sizetype, mem_ref_offset (t)); |
3244 | get_constraint_for_ptr_offset (TREE_OPERAND (t, 0), off, results); | |
4ee00913 | 3245 | do_deref (results); |
343b2efc RG |
3246 | |
3247 | /* If we are not taking the address then make sure to process | |
3248 | all subvariables we might access. */ | |
de2184c0 | 3249 | cs = *VEC_last (ce_s, *results); |
343b2efc | 3250 | if (address_p |
de2184c0 | 3251 | || cs.type != SCALAR) |
343b2efc RG |
3252 | return; |
3253 | ||
de2184c0 | 3254 | vi = get_varinfo (cs.var); |
343b2efc RG |
3255 | curr = vi->next; |
3256 | if (!vi->is_full_var | |
3257 | && curr) | |
3258 | { | |
3259 | unsigned HOST_WIDE_INT size; | |
3260 | if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1)) | |
3261 | size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t))); | |
3262 | else | |
3263 | size = -1; | |
3264 | for (; curr; curr = curr->next) | |
3265 | { | |
3266 | if (curr->offset - vi->offset < size) | |
3267 | { | |
343b2efc RG |
3268 | cs.var = curr->id; |
3269 | VEC_safe_push (ce_s, heap, *results, &cs); | |
3270 | } | |
3271 | else | |
3272 | break; | |
3273 | } | |
3274 | } | |
4ee00913 | 3275 | return; |
910fdc79 DB |
3276 | } |
3277 | case ARRAY_REF: | |
32961db5 | 3278 | case ARRAY_RANGE_REF: |
910fdc79 | 3279 | case COMPONENT_REF: |
ed6c4831 | 3280 | get_constraint_for_component_ref (t, results, address_p, lhs_p); |
4ee00913 | 3281 | return; |
5006671f | 3282 | case VIEW_CONVERT_EXPR: |
ed6c4831 RG |
3283 | get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p, |
3284 | lhs_p); | |
5006671f RG |
3285 | return; |
3286 | /* We are missing handling for TARGET_MEM_REF here. */ | |
e5bae89b | 3287 | default:; |
910fdc79 | 3288 | } |
e5bae89b | 3289 | break; |
910fdc79 | 3290 | } |
910fdc79 DB |
3291 | case tcc_exceptional: |
3292 | { | |
3293 | switch (TREE_CODE (t)) | |
3294 | { | |
910fdc79 | 3295 | case SSA_NAME: |
4ee00913 | 3296 | { |
c0d459f0 | 3297 | get_constraint_for_ssa_var (t, results, address_p); |
4ee00913 DB |
3298 | return; |
3299 | } | |
47d8a903 RG |
3300 | case CONSTRUCTOR: |
3301 | { | |
3302 | unsigned int i; | |
3303 | tree val; | |
3304 | VEC (ce_s, heap) *tmp = NULL; | |
3305 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val) | |
3306 | { | |
3307 | struct constraint_expr *rhsp; | |
3308 | unsigned j; | |
ed6c4831 | 3309 | get_constraint_for_1 (val, &tmp, address_p, lhs_p); |
ac47786e | 3310 | FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp) |
47d8a903 RG |
3311 | VEC_safe_push (ce_s, heap, *results, rhsp); |
3312 | VEC_truncate (ce_s, tmp, 0); | |
3313 | } | |
3314 | VEC_free (ce_s, heap, tmp); | |
3315 | /* We do not know whether the constructor was complete, | |
3316 | so technically we have to add &NOTHING or &ANYTHING | |
3317 | like we do for an empty constructor as well. */ | |
3318 | return; | |
3319 | } | |
e5bae89b | 3320 | default:; |
910fdc79 | 3321 | } |
e5bae89b | 3322 | break; |
910fdc79 DB |
3323 | } |
3324 | case tcc_declaration: | |
4ee00913 | 3325 | { |
c0d459f0 | 3326 | get_constraint_for_ssa_var (t, results, address_p); |
4ee00913 DB |
3327 | return; |
3328 | } | |
1f181fde RG |
3329 | case tcc_constant: |
3330 | { | |
3331 | /* We cannot refer to automatic variables through constants. */ | |
3332 | temp.type = ADDRESSOF; | |
3333 | temp.var = nonlocal_id; | |
3334 | temp.offset = 0; | |
3335 | VEC_safe_push (ce_s, heap, *results, &temp); | |
3336 | return; | |
3337 | } | |
e5bae89b | 3338 | default:; |
910fdc79 | 3339 | } |
e5bae89b RG |
3340 | |
3341 | /* The default fallback is a constraint from anything. */ | |
3342 | temp.type = ADDRESSOF; | |
3343 | temp.var = anything_id; | |
3344 | temp.offset = 0; | |
3345 | VEC_safe_push (ce_s, heap, *results, &temp); | |
910fdc79 DB |
3346 | } |
3347 | ||
c0d459f0 RG |
3348 | /* Given a gimple tree T, return the constraint expression vector for it. */ |
3349 | ||
3350 | static void | |
3351 | get_constraint_for (tree t, VEC (ce_s, heap) **results) | |
3352 | { | |
3353 | gcc_assert (VEC_length (ce_s, *results) == 0); | |
3354 | ||
ed6c4831 RG |
3355 | get_constraint_for_1 (t, results, false, true); |
3356 | } | |
3357 | ||
3358 | /* Given a gimple tree T, return the constraint expression vector for it | |
3359 | to be used as the rhs of a constraint. */ | |
3360 | ||
3361 | static void | |
3362 | get_constraint_for_rhs (tree t, VEC (ce_s, heap) **results) | |
3363 | { | |
3364 | gcc_assert (VEC_length (ce_s, *results) == 0); | |
3365 | ||
3366 | get_constraint_for_1 (t, results, false, false); | |
c0d459f0 | 3367 | } |
910fdc79 | 3368 | |
779704e7 RG |
3369 | |
3370 | /* Efficiently generates constraints from all entries in *RHSC to all | |
3371 | entries in *LHSC. */ | |
3372 | ||
3373 | static void | |
3374 | process_all_all_constraints (VEC (ce_s, heap) *lhsc, VEC (ce_s, heap) *rhsc) | |
3375 | { | |
3376 | struct constraint_expr *lhsp, *rhsp; | |
3377 | unsigned i, j; | |
3378 | ||
3379 | if (VEC_length (ce_s, lhsc) <= 1 | |
3380 | || VEC_length (ce_s, rhsc) <= 1) | |
3381 | { | |
ac47786e NF |
3382 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
3383 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) | |
779704e7 RG |
3384 | process_constraint (new_constraint (*lhsp, *rhsp)); |
3385 | } | |
3386 | else | |
3387 | { | |
3388 | struct constraint_expr tmp; | |
b14e9388 | 3389 | tmp = new_scalar_tmp_constraint_exp ("allalltmp"); |
ac47786e | 3390 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
779704e7 | 3391 | process_constraint (new_constraint (tmp, *rhsp)); |
ac47786e | 3392 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
779704e7 RG |
3393 | process_constraint (new_constraint (*lhsp, tmp)); |
3394 | } | |
3395 | } | |
3396 | ||
910fdc79 DB |
3397 | /* Handle aggregate copies by expanding into copies of the respective |
3398 | fields of the structures. */ | |
3399 | ||
3400 | static void | |
3401 | do_structure_copy (tree lhsop, tree rhsop) | |
3402 | { | |
5006671f | 3403 | struct constraint_expr *lhsp, *rhsp; |
4ee00913 | 3404 | VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL; |
5006671f RG |
3405 | unsigned j; |
3406 | ||
3407 | get_constraint_for (lhsop, &lhsc); | |
ed6c4831 | 3408 | get_constraint_for_rhs (rhsop, &rhsc); |
5006671f RG |
3409 | lhsp = VEC_index (ce_s, lhsc, 0); |
3410 | rhsp = VEC_index (ce_s, rhsc, 0); | |
3411 | if (lhsp->type == DEREF | |
3412 | || (lhsp->type == ADDRESSOF && lhsp->var == anything_id) | |
3413 | || rhsp->type == DEREF) | |
b28ae58f RG |
3414 | { |
3415 | if (lhsp->type == DEREF) | |
3416 | { | |
3417 | gcc_assert (VEC_length (ce_s, lhsc) == 1); | |
3418 | lhsp->offset = UNKNOWN_OFFSET; | |
3419 | } | |
3420 | if (rhsp->type == DEREF) | |
3421 | { | |
3422 | gcc_assert (VEC_length (ce_s, rhsc) == 1); | |
3423 | rhsp->offset = UNKNOWN_OFFSET; | |
3424 | } | |
3425 | process_all_all_constraints (lhsc, rhsc); | |
3426 | } | |
5006671f RG |
3427 | else if (lhsp->type == SCALAR |
3428 | && (rhsp->type == SCALAR | |
3429 | || rhsp->type == ADDRESSOF)) | |
910fdc79 | 3430 | { |
5006671f RG |
3431 | HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset; |
3432 | HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset; | |
3433 | unsigned k = 0; | |
0f900dfa JJ |
3434 | get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize); |
3435 | get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize); | |
5006671f | 3436 | for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);) |
910fdc79 | 3437 | { |
5006671f RG |
3438 | varinfo_t lhsv, rhsv; |
3439 | rhsp = VEC_index (ce_s, rhsc, k); | |
3440 | lhsv = get_varinfo (lhsp->var); | |
3441 | rhsv = get_varinfo (rhsp->var); | |
3442 | if (lhsv->may_have_pointers | |
c636a4fb RG |
3443 | && (lhsv->is_full_var |
3444 | || rhsv->is_full_var | |
3445 | || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size, | |
3446 | rhsv->offset + lhsoffset, rhsv->size))) | |
5006671f | 3447 | process_constraint (new_constraint (*lhsp, *rhsp)); |
c636a4fb RG |
3448 | if (!rhsv->is_full_var |
3449 | && (lhsv->is_full_var | |
3450 | || (lhsv->offset + rhsoffset + lhsv->size | |
3451 | > rhsv->offset + lhsoffset + rhsv->size))) | |
5006671f RG |
3452 | { |
3453 | ++k; | |
3454 | if (k >= VEC_length (ce_s, rhsc)) | |
3455 | break; | |
3456 | } | |
910fdc79 | 3457 | else |
5006671f | 3458 | ++j; |
910fdc79 DB |
3459 | } |
3460 | } | |
3461 | else | |
5006671f | 3462 | gcc_unreachable (); |
a5eadacc | 3463 | |
5006671f RG |
3464 | VEC_free (ce_s, heap, lhsc); |
3465 | VEC_free (ce_s, heap, rhsc); | |
910fdc79 DB |
3466 | } |
3467 | ||
cb89b4b0 | 3468 | /* Create constraints ID = { rhsc }. */ |
b7091901 RG |
3469 | |
3470 | static void | |
cb89b4b0 | 3471 | make_constraints_to (unsigned id, VEC(ce_s, heap) *rhsc) |
b7091901 | 3472 | { |
b7091901 RG |
3473 | struct constraint_expr *c; |
3474 | struct constraint_expr includes; | |
3475 | unsigned int j; | |
3476 | ||
3477 | includes.var = id; | |
3478 | includes.offset = 0; | |
3479 | includes.type = SCALAR; | |
3480 | ||
ac47786e | 3481 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, c) |
faf2ecc5 | 3482 | process_constraint (new_constraint (includes, *c)); |
cb89b4b0 RG |
3483 | } |
3484 | ||
3485 | /* Create a constraint ID = OP. */ | |
3486 | ||
3487 | static void | |
3488 | make_constraint_to (unsigned id, tree op) | |
3489 | { | |
3490 | VEC(ce_s, heap) *rhsc = NULL; | |
3491 | get_constraint_for_rhs (op, &rhsc); | |
3492 | make_constraints_to (id, rhsc); | |
b7091901 RG |
3493 | VEC_free (ce_s, heap, rhsc); |
3494 | } | |
3495 | ||
74d27244 RG |
3496 | /* Create a constraint ID = &FROM. */ |
3497 | ||
3498 | static void | |
3499 | make_constraint_from (varinfo_t vi, int from) | |
3500 | { | |
3501 | struct constraint_expr lhs, rhs; | |
3502 | ||
3503 | lhs.var = vi->id; | |
3504 | lhs.offset = 0; | |
3505 | lhs.type = SCALAR; | |
3506 | ||
3507 | rhs.var = from; | |
3508 | rhs.offset = 0; | |
3509 | rhs.type = ADDRESSOF; | |
3510 | process_constraint (new_constraint (lhs, rhs)); | |
3511 | } | |
3512 | ||
3513 | /* Create a constraint ID = FROM. */ | |
3514 | ||
3515 | static void | |
3516 | make_copy_constraint (varinfo_t vi, int from) | |
3517 | { | |
3518 | struct constraint_expr lhs, rhs; | |
3519 | ||
3520 | lhs.var = vi->id; | |
3521 | lhs.offset = 0; | |
3522 | lhs.type = SCALAR; | |
3523 | ||
3524 | rhs.var = from; | |
3525 | rhs.offset = 0; | |
3526 | rhs.type = SCALAR; | |
3527 | process_constraint (new_constraint (lhs, rhs)); | |
3528 | } | |
3529 | ||
b7091901 RG |
3530 | /* Make constraints necessary to make OP escape. */ |
3531 | ||
3532 | static void | |
3533 | make_escape_constraint (tree op) | |
3534 | { | |
3535 | make_constraint_to (escaped_id, op); | |
3536 | } | |
3537 | ||
3e8542ca RG |
3538 | /* Add constraints to that the solution of VI is transitively closed. */ |
3539 | ||
3540 | static void | |
3541 | make_transitive_closure_constraints (varinfo_t vi) | |
3542 | { | |
3543 | struct constraint_expr lhs, rhs; | |
3544 | ||
3545 | /* VAR = *VAR; */ | |
3546 | lhs.type = SCALAR; | |
3547 | lhs.var = vi->id; | |
3548 | lhs.offset = 0; | |
3549 | rhs.type = DEREF; | |
3550 | rhs.var = vi->id; | |
3551 | rhs.offset = 0; | |
3552 | process_constraint (new_constraint (lhs, rhs)); | |
3553 | ||
3554 | /* VAR = VAR + UNKNOWN; */ | |
3555 | lhs.type = SCALAR; | |
3556 | lhs.var = vi->id; | |
3557 | lhs.offset = 0; | |
3558 | rhs.type = SCALAR; | |
3559 | rhs.var = vi->id; | |
3560 | rhs.offset = UNKNOWN_OFFSET; | |
3561 | process_constraint (new_constraint (lhs, rhs)); | |
3562 | } | |
3563 | ||
7d6e2521 RG |
3564 | /* Temporary storage for fake var decls. */ |
3565 | struct obstack fake_var_decl_obstack; | |
3566 | ||
3567 | /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */ | |
3568 | ||
3569 | static tree | |
3570 | build_fake_var_decl (tree type) | |
3571 | { | |
3572 | tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl); | |
3573 | memset (decl, 0, sizeof (struct tree_var_decl)); | |
3574 | TREE_SET_CODE (decl, VAR_DECL); | |
3575 | TREE_TYPE (decl) = type; | |
3576 | DECL_UID (decl) = allocate_decl_uid (); | |
3577 | SET_DECL_PT_UID (decl, -1); | |
3578 | layout_decl (decl, 0); | |
3579 | return decl; | |
3580 | } | |
3581 | ||
0b7b376d RG |
3582 | /* Create a new artificial heap variable with NAME. |
3583 | Return the created variable. */ | |
74d27244 RG |
3584 | |
3585 | static varinfo_t | |
7d6e2521 | 3586 | make_heapvar (const char *name) |
74d27244 RG |
3587 | { |
3588 | varinfo_t vi; | |
7d6e2521 RG |
3589 | tree heapvar; |
3590 | ||
3591 | heapvar = build_fake_var_decl (ptr_type_node); | |
3592 | DECL_EXTERNAL (heapvar) = 1; | |
74d27244 RG |
3593 | |
3594 | vi = new_var_info (heapvar, name); | |
3595 | vi->is_artificial_var = true; | |
3596 | vi->is_heap_var = true; | |
3597 | vi->is_unknown_size_var = true; | |
b41e33fe | 3598 | vi->offset = 0; |
74d27244 RG |
3599 | vi->fullsize = ~0; |
3600 | vi->size = ~0; | |
3601 | vi->is_full_var = true; | |
3602 | insert_vi_for_tree (heapvar, vi); | |
3603 | ||
0b7b376d RG |
3604 | return vi; |
3605 | } | |
3606 | ||
3607 | /* Create a new artificial heap variable with NAME and make a | |
3608 | constraint from it to LHS. Return the created variable. */ | |
3609 | ||
3610 | static varinfo_t | |
3611 | make_constraint_from_heapvar (varinfo_t lhs, const char *name) | |
3612 | { | |
7d6e2521 | 3613 | varinfo_t vi = make_heapvar (name); |
74d27244 RG |
3614 | make_constraint_from (lhs, vi->id); |
3615 | ||
3616 | return vi; | |
3617 | } | |
3618 | ||
3619 | /* Create a new artificial heap variable with NAME and make a | |
3620 | constraint from it to LHS. Set flags according to a tag used | |
3621 | for tracking restrict pointers. */ | |
3622 | ||
3623 | static void | |
3624 | make_constraint_from_restrict (varinfo_t lhs, const char *name) | |
3625 | { | |
3626 | varinfo_t vi; | |
3627 | vi = make_constraint_from_heapvar (lhs, name); | |
3628 | vi->is_restrict_var = 1; | |
3629 | vi->is_global_var = 0; | |
3630 | vi->is_special_var = 1; | |
3631 | vi->may_have_pointers = 0; | |
3632 | } | |
3633 | ||
25a6a873 RG |
3634 | /* In IPA mode there are varinfos for different aspects of reach |
3635 | function designator. One for the points-to set of the return | |
3636 | value, one for the variables that are clobbered by the function, | |
3637 | one for its uses and one for each parameter (including a single | |
3638 | glob for remaining variadic arguments). */ | |
3639 | ||
3640 | enum { fi_clobbers = 1, fi_uses = 2, | |
3641 | fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 }; | |
3642 | ||
3643 | /* Get a constraint for the requested part of a function designator FI | |
3644 | when operating in IPA mode. */ | |
3645 | ||
3646 | static struct constraint_expr | |
3647 | get_function_part_constraint (varinfo_t fi, unsigned part) | |
3648 | { | |
3649 | struct constraint_expr c; | |
3650 | ||
3651 | gcc_assert (in_ipa_mode); | |
3652 | ||
3653 | if (fi->id == anything_id) | |
3654 | { | |
3655 | /* ??? We probably should have a ANYFN special variable. */ | |
3656 | c.var = anything_id; | |
3657 | c.offset = 0; | |
3658 | c.type = SCALAR; | |
3659 | } | |
3660 | else if (TREE_CODE (fi->decl) == FUNCTION_DECL) | |
3661 | { | |
3662 | varinfo_t ai = first_vi_for_offset (fi, part); | |
18abb35e RG |
3663 | if (ai) |
3664 | c.var = ai->id; | |
3665 | else | |
3666 | c.var = anything_id; | |
25a6a873 RG |
3667 | c.offset = 0; |
3668 | c.type = SCALAR; | |
3669 | } | |
3670 | else | |
3671 | { | |
3672 | c.var = fi->id; | |
3673 | c.offset = part; | |
3674 | c.type = DEREF; | |
3675 | } | |
3676 | ||
3677 | return c; | |
3678 | } | |
3679 | ||
7b765bed DB |
3680 | /* For non-IPA mode, generate constraints necessary for a call on the |
3681 | RHS. */ | |
3682 | ||
3683 | static void | |
472c7fbd | 3684 | handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results) |
7b765bed | 3685 | { |
472c7fbd | 3686 | struct constraint_expr rhsc; |
726a989a | 3687 | unsigned i; |
0b7b376d | 3688 | bool returns_uses = false; |
7b765bed | 3689 | |
726a989a RB |
3690 | for (i = 0; i < gimple_call_num_args (stmt); ++i) |
3691 | { | |
3692 | tree arg = gimple_call_arg (stmt, i); | |
0b7b376d | 3693 | int flags = gimple_call_arg_flags (stmt, i); |
726a989a | 3694 | |
0f8d6231 RG |
3695 | /* If the argument is not used we can ignore it. */ |
3696 | if (flags & EAF_UNUSED) | |
0b7b376d RG |
3697 | continue; |
3698 | ||
3699 | /* As we compute ESCAPED context-insensitive we do not gain | |
3700 | any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE | |
3701 | set. The argument would still get clobbered through the | |
3702 | escape solution. | |
3703 | ??? We might get away with less (and more precise) constraints | |
3704 | if using a temporary for transitively closing things. */ | |
3705 | if ((flags & EAF_NOCLOBBER) | |
3706 | && (flags & EAF_NOESCAPE)) | |
3707 | { | |
3708 | varinfo_t uses = get_call_use_vi (stmt); | |
3709 | if (!(flags & EAF_DIRECT)) | |
3710 | make_transitive_closure_constraints (uses); | |
3711 | make_constraint_to (uses->id, arg); | |
3712 | returns_uses = true; | |
3713 | } | |
3714 | else if (flags & EAF_NOESCAPE) | |
3715 | { | |
3716 | varinfo_t uses = get_call_use_vi (stmt); | |
3717 | varinfo_t clobbers = get_call_clobber_vi (stmt); | |
3718 | if (!(flags & EAF_DIRECT)) | |
3719 | { | |
3720 | make_transitive_closure_constraints (uses); | |
3721 | make_transitive_closure_constraints (clobbers); | |
3722 | } | |
3723 | make_constraint_to (uses->id, arg); | |
3724 | make_constraint_to (clobbers->id, arg); | |
3725 | returns_uses = true; | |
3726 | } | |
3727 | else | |
726a989a RB |
3728 | make_escape_constraint (arg); |
3729 | } | |
b7091901 | 3730 | |
0b7b376d RG |
3731 | /* If we added to the calls uses solution make sure we account for |
3732 | pointers to it to be returned. */ | |
3733 | if (returns_uses) | |
3734 | { | |
3735 | rhsc.var = get_call_use_vi (stmt)->id; | |
3736 | rhsc.offset = 0; | |
3737 | rhsc.type = SCALAR; | |
3738 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
3739 | } | |
3740 | ||
b7091901 | 3741 | /* The static chain escapes as well. */ |
726a989a RB |
3742 | if (gimple_call_chain (stmt)) |
3743 | make_escape_constraint (gimple_call_chain (stmt)); | |
472c7fbd | 3744 | |
1d24fdd9 RG |
3745 | /* And if we applied NRV the address of the return slot escapes as well. */ |
3746 | if (gimple_call_return_slot_opt_p (stmt) | |
3747 | && gimple_call_lhs (stmt) != NULL_TREE | |
4d61856d | 3748 | && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt)))) |
1d24fdd9 RG |
3749 | { |
3750 | VEC(ce_s, heap) *tmpc = NULL; | |
3751 | struct constraint_expr lhsc, *c; | |
3752 | get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc); | |
3753 | lhsc.var = escaped_id; | |
3754 | lhsc.offset = 0; | |
3755 | lhsc.type = SCALAR; | |
ac47786e | 3756 | FOR_EACH_VEC_ELT (ce_s, tmpc, i, c) |
1d24fdd9 RG |
3757 | process_constraint (new_constraint (lhsc, *c)); |
3758 | VEC_free(ce_s, heap, tmpc); | |
3759 | } | |
3760 | ||
5006671f RG |
3761 | /* Regular functions return nonlocal memory. */ |
3762 | rhsc.var = nonlocal_id; | |
472c7fbd | 3763 | rhsc.offset = 0; |
5006671f | 3764 | rhsc.type = SCALAR; |
472c7fbd | 3765 | VEC_safe_push (ce_s, heap, *results, &rhsc); |
7b765bed | 3766 | } |
e8ca4159 | 3767 | |
af947da7 RG |
3768 | /* For non-IPA mode, generate constraints necessary for a call |
3769 | that returns a pointer and assigns it to LHS. This simply makes | |
b7091901 | 3770 | the LHS point to global and escaped variables. */ |
af947da7 RG |
3771 | |
3772 | static void | |
0b7b376d RG |
3773 | handle_lhs_call (gimple stmt, tree lhs, int flags, VEC(ce_s, heap) *rhsc, |
3774 | tree fndecl) | |
af947da7 RG |
3775 | { |
3776 | VEC(ce_s, heap) *lhsc = NULL; | |
af947da7 | 3777 | |
b7091901 | 3778 | get_constraint_for (lhs, &lhsc); |
0b7b376d RG |
3779 | /* If the store is to a global decl make sure to |
3780 | add proper escape constraints. */ | |
3781 | lhs = get_base_address (lhs); | |
3782 | if (lhs | |
3783 | && DECL_P (lhs) | |
3784 | && is_global_var (lhs)) | |
3785 | { | |
3786 | struct constraint_expr tmpc; | |
3787 | tmpc.var = escaped_id; | |
3788 | tmpc.offset = 0; | |
3789 | tmpc.type = SCALAR; | |
3790 | VEC_safe_push (ce_s, heap, lhsc, &tmpc); | |
3791 | } | |
183ae595 | 3792 | |
0b7b376d RG |
3793 | /* If the call returns an argument unmodified override the rhs |
3794 | constraints. */ | |
3795 | flags = gimple_call_return_flags (stmt); | |
3796 | if (flags & ERF_RETURNS_ARG | |
3797 | && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt)) | |
3798 | { | |
3799 | tree arg; | |
3800 | rhsc = NULL; | |
3801 | arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK); | |
3802 | get_constraint_for (arg, &rhsc); | |
3803 | process_all_all_constraints (lhsc, rhsc); | |
3804 | VEC_free (ce_s, heap, rhsc); | |
3805 | } | |
3806 | else if (flags & ERF_NOALIAS) | |
183ae595 | 3807 | { |
183ae595 | 3808 | varinfo_t vi; |
0b7b376d RG |
3809 | struct constraint_expr tmpc; |
3810 | rhsc = NULL; | |
7d6e2521 | 3811 | vi = make_heapvar ("HEAP"); |
14c41b9b RG |
3812 | /* We delay marking allocated storage global until we know if |
3813 | it escapes. */ | |
91deb937 | 3814 | DECL_EXTERNAL (vi->decl) = 0; |
14c41b9b | 3815 | vi->is_global_var = 0; |
72d182d3 | 3816 | /* If this is not a real malloc call assume the memory was |
0b7b376d | 3817 | initialized and thus may point to global memory. All |
72d182d3 RG |
3818 | builtin functions with the malloc attribute behave in a sane way. */ |
3819 | if (!fndecl | |
3820 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
3821 | make_constraint_from (vi, nonlocal_id); | |
0b7b376d RG |
3822 | tmpc.var = vi->id; |
3823 | tmpc.offset = 0; | |
3824 | tmpc.type = ADDRESSOF; | |
3825 | VEC_safe_push (ce_s, heap, rhsc, &tmpc); | |
183ae595 | 3826 | } |
0b7b376d RG |
3827 | |
3828 | process_all_all_constraints (lhsc, rhsc); | |
3829 | ||
b7091901 RG |
3830 | VEC_free (ce_s, heap, lhsc); |
3831 | } | |
3832 | ||
3833 | /* For non-IPA mode, generate constraints necessary for a call of a | |
3834 | const function that returns a pointer in the statement STMT. */ | |
3835 | ||
3836 | static void | |
472c7fbd | 3837 | handle_const_call (gimple stmt, VEC(ce_s, heap) **results) |
b7091901 | 3838 | { |
b14e9388 | 3839 | struct constraint_expr rhsc; |
472c7fbd | 3840 | unsigned int k; |
b7091901 | 3841 | |
472c7fbd RG |
3842 | /* Treat nested const functions the same as pure functions as far |
3843 | as the static chain is concerned. */ | |
726a989a | 3844 | if (gimple_call_chain (stmt)) |
b7091901 | 3845 | { |
3e8542ca RG |
3846 | varinfo_t uses = get_call_use_vi (stmt); |
3847 | make_transitive_closure_constraints (uses); | |
3848 | make_constraint_to (uses->id, gimple_call_chain (stmt)); | |
3849 | rhsc.var = uses->id; | |
b7091901 | 3850 | rhsc.offset = 0; |
472c7fbd RG |
3851 | rhsc.type = SCALAR; |
3852 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
b7091901 RG |
3853 | } |
3854 | ||
b7091901 | 3855 | /* May return arguments. */ |
726a989a RB |
3856 | for (k = 0; k < gimple_call_num_args (stmt); ++k) |
3857 | { | |
3858 | tree arg = gimple_call_arg (stmt, k); | |
0f8d6231 RG |
3859 | VEC(ce_s, heap) *argc = NULL; |
3860 | unsigned i; | |
3861 | struct constraint_expr *argp; | |
3862 | get_constraint_for_rhs (arg, &argc); | |
3863 | FOR_EACH_VEC_ELT (ce_s, argc, i, argp) | |
3864 | VEC_safe_push (ce_s, heap, *results, argp); | |
3865 | VEC_free(ce_s, heap, argc); | |
726a989a | 3866 | } |
b7091901 | 3867 | |
472c7fbd RG |
3868 | /* May return addresses of globals. */ |
3869 | rhsc.var = nonlocal_id; | |
3870 | rhsc.offset = 0; | |
3871 | rhsc.type = ADDRESSOF; | |
3872 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
af947da7 RG |
3873 | } |
3874 | ||
15c15196 RG |
3875 | /* For non-IPA mode, generate constraints necessary for a call to a |
3876 | pure function in statement STMT. */ | |
3877 | ||
3878 | static void | |
472c7fbd | 3879 | handle_pure_call (gimple stmt, VEC(ce_s, heap) **results) |
15c15196 | 3880 | { |
472c7fbd | 3881 | struct constraint_expr rhsc; |
726a989a | 3882 | unsigned i; |
3e8542ca | 3883 | varinfo_t uses = NULL; |
15c15196 RG |
3884 | |
3885 | /* Memory reached from pointer arguments is call-used. */ | |
726a989a RB |
3886 | for (i = 0; i < gimple_call_num_args (stmt); ++i) |
3887 | { | |
3888 | tree arg = gimple_call_arg (stmt, i); | |
0f8d6231 | 3889 | if (!uses) |
472c7fbd | 3890 | { |
0f8d6231 RG |
3891 | uses = get_call_use_vi (stmt); |
3892 | make_transitive_closure_constraints (uses); | |
472c7fbd | 3893 | } |
0f8d6231 | 3894 | make_constraint_to (uses->id, arg); |
726a989a | 3895 | } |
15c15196 RG |
3896 | |
3897 | /* The static chain is used as well. */ | |
726a989a | 3898 | if (gimple_call_chain (stmt)) |
15c15196 | 3899 | { |
3e8542ca RG |
3900 | if (!uses) |
3901 | { | |
3902 | uses = get_call_use_vi (stmt); | |
3903 | make_transitive_closure_constraints (uses); | |
3904 | } | |
3905 | make_constraint_to (uses->id, gimple_call_chain (stmt)); | |
472c7fbd | 3906 | } |
15c15196 | 3907 | |
3e8542ca RG |
3908 | /* Pure functions may return call-used and nonlocal memory. */ |
3909 | if (uses) | |
472c7fbd | 3910 | { |
3e8542ca | 3911 | rhsc.var = uses->id; |
15c15196 | 3912 | rhsc.offset = 0; |
472c7fbd RG |
3913 | rhsc.type = SCALAR; |
3914 | VEC_safe_push (ce_s, heap, *results, &rhsc); | |
15c15196 | 3915 | } |
5006671f | 3916 | rhsc.var = nonlocal_id; |
472c7fbd | 3917 | rhsc.offset = 0; |
5006671f | 3918 | rhsc.type = SCALAR; |
472c7fbd | 3919 | VEC_safe_push (ce_s, heap, *results, &rhsc); |
15c15196 RG |
3920 | } |
3921 | ||
25a6a873 RG |
3922 | |
3923 | /* Return the varinfo for the callee of CALL. */ | |
3924 | ||
3925 | static varinfo_t | |
3926 | get_fi_for_callee (gimple call) | |
3927 | { | |
5c04e9f4 | 3928 | tree decl, fn = gimple_call_fn (call); |
25a6a873 | 3929 | |
5c04e9f4 RG |
3930 | if (fn && TREE_CODE (fn) == OBJ_TYPE_REF) |
3931 | fn = OBJ_TYPE_REF_EXPR (fn); | |
25583c4f | 3932 | |
25a6a873 RG |
3933 | /* If we can directly resolve the function being called, do so. |
3934 | Otherwise, it must be some sort of indirect expression that | |
3935 | we should still be able to handle. */ | |
5c04e9f4 | 3936 | decl = gimple_call_addr_fndecl (fn); |
25a6a873 RG |
3937 | if (decl) |
3938 | return get_vi_for_tree (decl); | |
3939 | ||
5c04e9f4 | 3940 | /* If the function is anything other than a SSA name pointer we have no |
25a6a873 | 3941 | clue and should be getting ANYFN (well, ANYTHING for now). */ |
5c04e9f4 | 3942 | if (!fn || TREE_CODE (fn) != SSA_NAME) |
25a6a873 | 3943 | return get_varinfo (anything_id); |
5c04e9f4 RG |
3944 | |
3945 | if ((TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL | |
3946 | || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL) | |
3947 | && SSA_NAME_IS_DEFAULT_DEF (fn)) | |
3948 | fn = SSA_NAME_VAR (fn); | |
3949 | ||
3950 | return get_vi_for_tree (fn); | |
25a6a873 RG |
3951 | } |
3952 | ||
e38811ce RG |
3953 | /* Create constraints for the builtin call T. Return true if the call |
3954 | was handled, otherwise false. */ | |
910fdc79 | 3955 | |
e38811ce RG |
3956 | static bool |
3957 | find_func_aliases_for_builtin_call (gimple t) | |
910fdc79 | 3958 | { |
e38811ce | 3959 | tree fndecl = gimple_call_fndecl (t); |
4ee00913 DB |
3960 | VEC(ce_s, heap) *lhsc = NULL; |
3961 | VEC(ce_s, heap) *rhsc = NULL; | |
25a6a873 | 3962 | varinfo_t fi; |
910fdc79 | 3963 | |
e38811ce RG |
3964 | if (fndecl != NULL_TREE |
3965 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
3966 | /* ??? All builtins that are handled here need to be handled | |
3967 | in the alias-oracle query functions explicitly! */ | |
3968 | switch (DECL_FUNCTION_CODE (fndecl)) | |
3969 | { | |
3970 | /* All the following functions return a pointer to the same object | |
3971 | as their first argument points to. The functions do not add | |
3972 | to the ESCAPED solution. The functions make the first argument | |
3973 | pointed to memory point to what the second argument pointed to | |
3974 | memory points to. */ | |
3975 | case BUILT_IN_STRCPY: | |
3976 | case BUILT_IN_STRNCPY: | |
3977 | case BUILT_IN_BCOPY: | |
3978 | case BUILT_IN_MEMCPY: | |
3979 | case BUILT_IN_MEMMOVE: | |
3980 | case BUILT_IN_MEMPCPY: | |
3981 | case BUILT_IN_STPCPY: | |
3982 | case BUILT_IN_STPNCPY: | |
3983 | case BUILT_IN_STRCAT: | |
3984 | case BUILT_IN_STRNCAT: | |
e8ca4159 | 3985 | { |
e38811ce RG |
3986 | tree res = gimple_call_lhs (t); |
3987 | tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl) | |
3988 | == BUILT_IN_BCOPY ? 1 : 0)); | |
3989 | tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl) | |
3990 | == BUILT_IN_BCOPY ? 0 : 1)); | |
3991 | if (res != NULL_TREE) | |
0f8d6231 | 3992 | { |
e38811ce RG |
3993 | get_constraint_for (res, &lhsc); |
3994 | if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY | |
3995 | || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY | |
3996 | || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY) | |
3997 | get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc); | |
3998 | else | |
3999 | get_constraint_for (dest, &rhsc); | |
4000 | process_all_all_constraints (lhsc, rhsc); | |
4001 | VEC_free (ce_s, heap, lhsc); | |
4002 | VEC_free (ce_s, heap, rhsc); | |
c58936b6 | 4003 | } |
e38811ce RG |
4004 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); |
4005 | get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc); | |
4006 | do_deref (&lhsc); | |
4007 | do_deref (&rhsc); | |
4008 | process_all_all_constraints (lhsc, rhsc); | |
4009 | VEC_free (ce_s, heap, lhsc); | |
4010 | VEC_free (ce_s, heap, rhsc); | |
4011 | return true; | |
4ee00913 | 4012 | } |
e38811ce RG |
4013 | case BUILT_IN_MEMSET: |
4014 | { | |
4015 | tree res = gimple_call_lhs (t); | |
4016 | tree dest = gimple_call_arg (t, 0); | |
4017 | unsigned i; | |
4018 | ce_s *lhsp; | |
4019 | struct constraint_expr ac; | |
4020 | if (res != NULL_TREE) | |
779704e7 | 4021 | { |
e38811ce RG |
4022 | get_constraint_for (res, &lhsc); |
4023 | get_constraint_for (dest, &rhsc); | |
779704e7 RG |
4024 | process_all_all_constraints (lhsc, rhsc); |
4025 | VEC_free (ce_s, heap, lhsc); | |
4026 | VEC_free (ce_s, heap, rhsc); | |
779704e7 | 4027 | } |
e38811ce RG |
4028 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); |
4029 | do_deref (&lhsc); | |
4030 | if (flag_delete_null_pointer_checks | |
4031 | && integer_zerop (gimple_call_arg (t, 1))) | |
779704e7 | 4032 | { |
e38811ce RG |
4033 | ac.type = ADDRESSOF; |
4034 | ac.var = nothing_id; | |
779704e7 | 4035 | } |
e38811ce | 4036 | else |
25a6a873 | 4037 | { |
e38811ce RG |
4038 | ac.type = SCALAR; |
4039 | ac.var = integer_id; | |
25a6a873 | 4040 | } |
e38811ce RG |
4041 | ac.offset = 0; |
4042 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) | |
4043 | process_constraint (new_constraint (*lhsp, ac)); | |
4044 | VEC_free (ce_s, heap, lhsc); | |
4045 | return true; | |
4046 | } | |
4047 | /* All the following functions do not return pointers, do not | |
4048 | modify the points-to sets of memory reachable from their | |
4049 | arguments and do not add to the ESCAPED solution. */ | |
4050 | case BUILT_IN_SINCOS: | |
4051 | case BUILT_IN_SINCOSF: | |
4052 | case BUILT_IN_SINCOSL: | |
4053 | case BUILT_IN_FREXP: | |
4054 | case BUILT_IN_FREXPF: | |
4055 | case BUILT_IN_FREXPL: | |
4056 | case BUILT_IN_GAMMA_R: | |
4057 | case BUILT_IN_GAMMAF_R: | |
4058 | case BUILT_IN_GAMMAL_R: | |
4059 | case BUILT_IN_LGAMMA_R: | |
4060 | case BUILT_IN_LGAMMAF_R: | |
4061 | case BUILT_IN_LGAMMAL_R: | |
4062 | case BUILT_IN_MODF: | |
4063 | case BUILT_IN_MODFF: | |
4064 | case BUILT_IN_MODFL: | |
4065 | case BUILT_IN_REMQUO: | |
4066 | case BUILT_IN_REMQUOF: | |
4067 | case BUILT_IN_REMQUOL: | |
4068 | case BUILT_IN_FREE: | |
4069 | return true; | |
4070 | /* Trampolines are special - they set up passing the static | |
4071 | frame. */ | |
4072 | case BUILT_IN_INIT_TRAMPOLINE: | |
4073 | { | |
4074 | tree tramp = gimple_call_arg (t, 0); | |
4075 | tree nfunc = gimple_call_arg (t, 1); | |
4076 | tree frame = gimple_call_arg (t, 2); | |
4077 | unsigned i; | |
4078 | struct constraint_expr lhs, *rhsp; | |
4079 | if (in_ipa_mode) | |
25a6a873 | 4080 | { |
e38811ce RG |
4081 | varinfo_t nfi = NULL; |
4082 | gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR); | |
4083 | nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0)); | |
4084 | if (nfi) | |
25a6a873 | 4085 | { |
e38811ce RG |
4086 | lhs = get_function_part_constraint (nfi, fi_static_chain); |
4087 | get_constraint_for (frame, &rhsc); | |
4088 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) | |
4089 | process_constraint (new_constraint (lhs, *rhsp)); | |
4090 | VEC_free (ce_s, heap, rhsc); | |
4091 | ||
4092 | /* Make the frame point to the function for | |
4093 | the trampoline adjustment call. */ | |
4094 | get_constraint_for (tramp, &lhsc); | |
4095 | do_deref (&lhsc); | |
4096 | get_constraint_for (nfunc, &rhsc); | |
25a6a873 RG |
4097 | process_all_all_constraints (lhsc, rhsc); |
4098 | VEC_free (ce_s, heap, rhsc); | |
4099 | VEC_free (ce_s, heap, lhsc); | |
e38811ce RG |
4100 | |
4101 | return true; | |
25a6a873 | 4102 | } |
25a6a873 | 4103 | } |
e38811ce RG |
4104 | /* Else fallthru to generic handling which will let |
4105 | the frame escape. */ | |
4106 | break; | |
4107 | } | |
4108 | case BUILT_IN_ADJUST_TRAMPOLINE: | |
4109 | { | |
4110 | tree tramp = gimple_call_arg (t, 0); | |
4111 | tree res = gimple_call_lhs (t); | |
4112 | if (in_ipa_mode && res) | |
25a6a873 | 4113 | { |
e38811ce RG |
4114 | get_constraint_for (res, &lhsc); |
4115 | get_constraint_for (tramp, &rhsc); | |
4116 | do_deref (&rhsc); | |
4117 | process_all_all_constraints (lhsc, rhsc); | |
4118 | VEC_free (ce_s, heap, rhsc); | |
4119 | VEC_free (ce_s, heap, lhsc); | |
25a6a873 | 4120 | } |
e38811ce RG |
4121 | return true; |
4122 | } | |
4123 | /* Variadic argument handling needs to be handled in IPA | |
4124 | mode as well. */ | |
4125 | case BUILT_IN_VA_START: | |
4126 | { | |
4127 | if (in_ipa_mode) | |
a4c9bc15 | 4128 | { |
e38811ce RG |
4129 | tree valist = gimple_call_arg (t, 0); |
4130 | struct constraint_expr rhs, *lhsp; | |
4131 | unsigned i; | |
4132 | /* The va_list gets access to pointers in variadic | |
4133 | arguments. */ | |
4134 | fi = lookup_vi_for_tree (cfun->decl); | |
4135 | gcc_assert (fi != NULL); | |
4136 | get_constraint_for (valist, &lhsc); | |
4137 | do_deref (&lhsc); | |
4138 | rhs = get_function_part_constraint (fi, ~0); | |
4139 | rhs.type = ADDRESSOF; | |
4140 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) | |
4141 | process_constraint (new_constraint (*lhsp, rhs)); | |
4142 | VEC_free (ce_s, heap, lhsc); | |
4143 | /* va_list is clobbered. */ | |
4144 | make_constraint_to (get_call_clobber_vi (t)->id, valist); | |
4145 | return true; | |
a4c9bc15 | 4146 | } |
e38811ce RG |
4147 | break; |
4148 | } | |
4149 | /* va_end doesn't have any effect that matters. */ | |
4150 | case BUILT_IN_VA_END: | |
4151 | return true; | |
4152 | /* Alternate return. Simply give up for now. */ | |
4153 | case BUILT_IN_RETURN: | |
4ee00913 | 4154 | { |
e38811ce RG |
4155 | fi = NULL; |
4156 | if (!in_ipa_mode | |
4157 | || !(fi = get_vi_for_tree (cfun->decl))) | |
4158 | make_constraint_from (get_varinfo (escaped_id), anything_id); | |
4159 | else if (in_ipa_mode | |
4160 | && fi != NULL) | |
b7091901 | 4161 | { |
e38811ce RG |
4162 | struct constraint_expr lhs, rhs; |
4163 | lhs = get_function_part_constraint (fi, fi_result); | |
4164 | rhs.var = anything_id; | |
4165 | rhs.offset = 0; | |
4166 | rhs.type = SCALAR; | |
4167 | process_constraint (new_constraint (lhs, rhs)); | |
b7091901 | 4168 | } |
e38811ce RG |
4169 | return true; |
4170 | } | |
4171 | /* printf-style functions may have hooks to set pointers to | |
4172 | point to somewhere into the generated string. Leave them | |
4173 | for a later excercise... */ | |
4174 | default: | |
4175 | /* Fallthru to general call handling. */; | |
4176 | } | |
4177 | ||
4178 | return false; | |
4179 | } | |
4180 | ||
4181 | /* Create constraints for the call T. */ | |
4182 | ||
4183 | static void | |
4184 | find_func_aliases_for_call (gimple t) | |
4185 | { | |
4186 | tree fndecl = gimple_call_fndecl (t); | |
4187 | VEC(ce_s, heap) *lhsc = NULL; | |
4188 | VEC(ce_s, heap) *rhsc = NULL; | |
4189 | varinfo_t fi; | |
4190 | ||
4191 | if (fndecl != NULL_TREE | |
4192 | && DECL_BUILT_IN (fndecl) | |
4193 | && find_func_aliases_for_builtin_call (t)) | |
4194 | return; | |
4195 | ||
5c04e9f4 | 4196 | fi = get_fi_for_callee (t); |
e38811ce | 4197 | if (!in_ipa_mode |
5c04e9f4 | 4198 | || (fndecl && !fi->is_fn_info)) |
e38811ce RG |
4199 | { |
4200 | VEC(ce_s, heap) *rhsc = NULL; | |
4201 | int flags = gimple_call_flags (t); | |
4202 | ||
4203 | /* Const functions can return their arguments and addresses | |
4204 | of global memory but not of escaped memory. */ | |
4205 | if (flags & (ECF_CONST|ECF_NOVOPS)) | |
4206 | { | |
cb89b4b0 | 4207 | if (gimple_call_lhs (t)) |
e38811ce | 4208 | handle_const_call (t, &rhsc); |
4ee00913 | 4209 | } |
e38811ce RG |
4210 | /* Pure functions can return addresses in and of memory |
4211 | reachable from their arguments, but they are not an escape | |
4212 | point for reachable memory of their arguments. */ | |
4213 | else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE)) | |
4214 | handle_pure_call (t, &rhsc); | |
4ee00913 | 4215 | else |
e38811ce RG |
4216 | handle_rhs_call (t, &rhsc); |
4217 | if (gimple_call_lhs (t)) | |
4218 | handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl); | |
4219 | VEC_free (ce_s, heap, rhsc); | |
4220 | } | |
4221 | else | |
4222 | { | |
4223 | tree lhsop; | |
4224 | unsigned j; | |
6e7e772d | 4225 | |
e38811ce RG |
4226 | /* Assign all the passed arguments to the appropriate incoming |
4227 | parameters of the function. */ | |
4228 | for (j = 0; j < gimple_call_num_args (t); j++) | |
4229 | { | |
4230 | struct constraint_expr lhs ; | |
4231 | struct constraint_expr *rhsp; | |
4232 | tree arg = gimple_call_arg (t, j); | |
7b765bed | 4233 | |
e38811ce RG |
4234 | get_constraint_for_rhs (arg, &rhsc); |
4235 | lhs = get_function_part_constraint (fi, fi_parm_base + j); | |
4236 | while (VEC_length (ce_s, rhsc) != 0) | |
4ee00913 | 4237 | { |
e38811ce RG |
4238 | rhsp = VEC_last (ce_s, rhsc); |
4239 | process_constraint (new_constraint (lhs, *rhsp)); | |
4240 | VEC_pop (ce_s, rhsc); | |
4ee00913 | 4241 | } |
e38811ce RG |
4242 | } |
4243 | ||
4244 | /* If we are returning a value, assign it to the result. */ | |
4245 | lhsop = gimple_call_lhs (t); | |
4246 | if (lhsop) | |
4247 | { | |
4248 | struct constraint_expr rhs; | |
4249 | struct constraint_expr *lhsp; | |
25a6a873 | 4250 | |
e38811ce RG |
4251 | get_constraint_for (lhsop, &lhsc); |
4252 | rhs = get_function_part_constraint (fi, fi_result); | |
4253 | if (fndecl | |
25a6a873 RG |
4254 | && DECL_RESULT (fndecl) |
4255 | && DECL_BY_REFERENCE (DECL_RESULT (fndecl))) | |
4256 | { | |
e38811ce RG |
4257 | VEC(ce_s, heap) *tem = NULL; |
4258 | VEC_safe_push (ce_s, heap, tem, &rhs); | |
4259 | do_deref (&tem); | |
4260 | rhs = *VEC_index (ce_s, tem, 0); | |
4261 | VEC_free(ce_s, heap, tem); | |
25a6a873 | 4262 | } |
e38811ce | 4263 | FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp) |
5c04e9f4 | 4264 | process_constraint (new_constraint (*lhsp, rhs)); |
e38811ce | 4265 | } |
25a6a873 | 4266 | |
e38811ce RG |
4267 | /* If we pass the result decl by reference, honor that. */ |
4268 | if (lhsop | |
4269 | && fndecl | |
4270 | && DECL_RESULT (fndecl) | |
4271 | && DECL_BY_REFERENCE (DECL_RESULT (fndecl))) | |
4272 | { | |
4273 | struct constraint_expr lhs; | |
4274 | struct constraint_expr *rhsp; | |
4275 | ||
4276 | get_constraint_for_address_of (lhsop, &rhsc); | |
4277 | lhs = get_function_part_constraint (fi, fi_result); | |
4278 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) | |
5c04e9f4 | 4279 | process_constraint (new_constraint (lhs, *rhsp)); |
e38811ce RG |
4280 | VEC_free (ce_s, heap, rhsc); |
4281 | } | |
25a6a873 | 4282 | |
e38811ce RG |
4283 | /* If we use a static chain, pass it along. */ |
4284 | if (gimple_call_chain (t)) | |
4285 | { | |
4286 | struct constraint_expr lhs; | |
4287 | struct constraint_expr *rhsp; | |
4288 | ||
4289 | get_constraint_for (gimple_call_chain (t), &rhsc); | |
4290 | lhs = get_function_part_constraint (fi, fi_static_chain); | |
4291 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) | |
5c04e9f4 | 4292 | process_constraint (new_constraint (lhs, *rhsp)); |
e38811ce RG |
4293 | } |
4294 | } | |
4295 | } | |
4296 | ||
4297 | /* Walk statement T setting up aliasing constraints according to the | |
4298 | references found in T. This function is the main part of the | |
4299 | constraint builder. AI points to auxiliary alias information used | |
4300 | when building alias sets and computing alias grouping heuristics. */ | |
4301 | ||
4302 | static void | |
4303 | find_func_aliases (gimple origt) | |
4304 | { | |
4305 | gimple t = origt; | |
4306 | VEC(ce_s, heap) *lhsc = NULL; | |
4307 | VEC(ce_s, heap) *rhsc = NULL; | |
4308 | struct constraint_expr *c; | |
4309 | varinfo_t fi; | |
4310 | ||
4311 | /* Now build constraints expressions. */ | |
4312 | if (gimple_code (t) == GIMPLE_PHI) | |
4313 | { | |
4314 | size_t i; | |
4315 | unsigned int j; | |
4316 | ||
4317 | /* For a phi node, assign all the arguments to | |
4318 | the result. */ | |
4319 | get_constraint_for (gimple_phi_result (t), &lhsc); | |
4320 | for (i = 0; i < gimple_phi_num_args (t); i++) | |
4321 | { | |
4322 | tree strippedrhs = PHI_ARG_DEF (t, i); | |
4323 | ||
4324 | STRIP_NOPS (strippedrhs); | |
4325 | get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc); | |
4326 | ||
4327 | FOR_EACH_VEC_ELT (ce_s, lhsc, j, c) | |
4328 | { | |
4329 | struct constraint_expr *c2; | |
4330 | while (VEC_length (ce_s, rhsc) > 0) | |
4331 | { | |
4332 | c2 = VEC_last (ce_s, rhsc); | |
4333 | process_constraint (new_constraint (*c, *c2)); | |
4334 | VEC_pop (ce_s, rhsc); | |
4335 | } | |
25a6a873 | 4336 | } |
c58936b6 | 4337 | } |
e8ca4159 | 4338 | } |
e38811ce RG |
4339 | /* In IPA mode, we need to generate constraints to pass call |
4340 | arguments through their calls. There are two cases, | |
4341 | either a GIMPLE_CALL returning a value, or just a plain | |
4342 | GIMPLE_CALL when we are not. | |
4343 | ||
4344 | In non-ipa mode, we need to generate constraints for each | |
4345 | pointer passed by address. */ | |
4346 | else if (is_gimple_call (t)) | |
4347 | find_func_aliases_for_call (t); | |
4348 | ||
e5bae89b RG |
4349 | /* Otherwise, just a regular assignment statement. Only care about |
4350 | operations with pointer result, others are dealt with as escape | |
4351 | points if they have pointer operands. */ | |
0f8d6231 | 4352 | else if (is_gimple_assign (t)) |
e8ca4159 | 4353 | { |
726a989a RB |
4354 | /* Otherwise, just a regular assignment statement. */ |
4355 | tree lhsop = gimple_assign_lhs (t); | |
4356 | tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL; | |
e8ca4159 | 4357 | |
726a989a | 4358 | if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop))) |
e5bae89b | 4359 | do_structure_copy (lhsop, rhsop); |
e8ca4159 DN |
4360 | else |
4361 | { | |
194313e2 RG |
4362 | enum tree_code code = gimple_assign_rhs_code (t); |
4363 | ||
e5bae89b | 4364 | get_constraint_for (lhsop, &lhsc); |
726a989a | 4365 | |
194313e2 | 4366 | if (code == POINTER_PLUS_EXPR) |
726a989a RB |
4367 | get_constraint_for_ptr_offset (gimple_assign_rhs1 (t), |
4368 | gimple_assign_rhs2 (t), &rhsc); | |
194313e2 | 4369 | else if (code == BIT_AND_EXPR |
fca821b5 RG |
4370 | && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST) |
4371 | { | |
4372 | /* Aligning a pointer via a BIT_AND_EXPR is offsetting | |
4373 | the pointer. Handle it by offsetting it by UNKNOWN. */ | |
4374 | get_constraint_for_ptr_offset (gimple_assign_rhs1 (t), | |
4375 | NULL_TREE, &rhsc); | |
4376 | } | |
194313e2 | 4377 | else if ((CONVERT_EXPR_CODE_P (code) |
1961418e RG |
4378 | && !(POINTER_TYPE_P (gimple_expr_type (t)) |
4379 | && !POINTER_TYPE_P (TREE_TYPE (rhsop)))) | |
4380 | || gimple_assign_single_p (t)) | |
ed6c4831 | 4381 | get_constraint_for_rhs (rhsop, &rhsc); |
194313e2 RG |
4382 | else if (truth_value_p (code)) |
4383 | /* Truth value results are not pointer (parts). Or at least | |
4384 | very very unreasonable obfuscation of a part. */ | |
4385 | ; | |
726a989a RB |
4386 | else |
4387 | { | |
0f8d6231 RG |
4388 | /* All other operations are merges. */ |
4389 | VEC (ce_s, heap) *tmp = NULL; | |
4390 | struct constraint_expr *rhsp; | |
4391 | unsigned i, j; | |
4392 | get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc); | |
4393 | for (i = 2; i < gimple_num_ops (t); ++i) | |
4394 | { | |
4395 | get_constraint_for_rhs (gimple_op (t, i), &tmp); | |
4396 | FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp) | |
4397 | VEC_safe_push (ce_s, heap, rhsc, rhsp); | |
4398 | VEC_truncate (ce_s, tmp, 0); | |
4399 | } | |
4400 | VEC_free (ce_s, heap, tmp); | |
726a989a | 4401 | } |
779704e7 | 4402 | process_all_all_constraints (lhsc, rhsc); |
e8ca4159 | 4403 | } |
de70bb20 RG |
4404 | /* If there is a store to a global variable the rhs escapes. */ |
4405 | if ((lhsop = get_base_address (lhsop)) != NULL_TREE | |
4406 | && DECL_P (lhsop) | |
25a6a873 RG |
4407 | && is_global_var (lhsop) |
4408 | && (!in_ipa_mode | |
4409 | || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop))) | |
de70bb20 | 4410 | make_escape_constraint (rhsop); |
74d27244 RG |
4411 | /* If this is a conversion of a non-restrict pointer to a |
4412 | restrict pointer track it with a new heapvar. */ | |
4413 | else if (gimple_assign_cast_p (t) | |
4414 | && POINTER_TYPE_P (TREE_TYPE (rhsop)) | |
4415 | && POINTER_TYPE_P (TREE_TYPE (lhsop)) | |
4416 | && !TYPE_RESTRICT (TREE_TYPE (rhsop)) | |
4417 | && TYPE_RESTRICT (TREE_TYPE (lhsop))) | |
4418 | make_constraint_from_restrict (get_vi_for_tree (lhsop), | |
4419 | "CAST_RESTRICT"); | |
910fdc79 | 4420 | } |
14c41b9b RG |
4421 | /* Handle escapes through return. */ |
4422 | else if (gimple_code (t) == GIMPLE_RETURN | |
0f8d6231 | 4423 | && gimple_return_retval (t) != NULL_TREE) |
14c41b9b | 4424 | { |
25a6a873 RG |
4425 | fi = NULL; |
4426 | if (!in_ipa_mode | |
4427 | || !(fi = get_vi_for_tree (cfun->decl))) | |
4428 | make_escape_constraint (gimple_return_retval (t)); | |
4429 | else if (in_ipa_mode | |
4430 | && fi != NULL) | |
4431 | { | |
4432 | struct constraint_expr lhs ; | |
4433 | struct constraint_expr *rhsp; | |
4434 | unsigned i; | |
4435 | ||
4436 | lhs = get_function_part_constraint (fi, fi_result); | |
ed6c4831 | 4437 | get_constraint_for_rhs (gimple_return_retval (t), &rhsc); |
ac47786e | 4438 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4439 | process_constraint (new_constraint (lhs, *rhsp)); |
4440 | } | |
14c41b9b | 4441 | } |
2e407842 RG |
4442 | /* Handle asms conservatively by adding escape constraints to everything. */ |
4443 | else if (gimple_code (t) == GIMPLE_ASM) | |
b7091901 | 4444 | { |
5006671f RG |
4445 | unsigned i, noutputs; |
4446 | const char **oconstraints; | |
4447 | const char *constraint; | |
4448 | bool allows_mem, allows_reg, is_inout; | |
4449 | ||
4450 | noutputs = gimple_asm_noutputs (t); | |
4451 | oconstraints = XALLOCAVEC (const char *, noutputs); | |
4452 | ||
4453 | for (i = 0; i < noutputs; ++i) | |
b7091901 | 4454 | { |
5006671f RG |
4455 | tree link = gimple_asm_output_op (t, i); |
4456 | tree op = TREE_VALUE (link); | |
4457 | ||
4458 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
4459 | oconstraints[i] = constraint; | |
4460 | parse_output_constraint (&constraint, i, 0, 0, &allows_mem, | |
4461 | &allows_reg, &is_inout); | |
4462 | ||
4463 | /* A memory constraint makes the address of the operand escape. */ | |
4464 | if (!allows_reg && allows_mem) | |
4465 | make_escape_constraint (build_fold_addr_expr (op)); | |
4466 | ||
4467 | /* The asm may read global memory, so outputs may point to | |
4468 | any global memory. */ | |
0f8d6231 | 4469 | if (op) |
5006671f RG |
4470 | { |
4471 | VEC(ce_s, heap) *lhsc = NULL; | |
4472 | struct constraint_expr rhsc, *lhsp; | |
4473 | unsigned j; | |
4474 | get_constraint_for (op, &lhsc); | |
4475 | rhsc.var = nonlocal_id; | |
4476 | rhsc.offset = 0; | |
4477 | rhsc.type = SCALAR; | |
ac47786e | 4478 | FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp) |
5006671f RG |
4479 | process_constraint (new_constraint (*lhsp, rhsc)); |
4480 | VEC_free (ce_s, heap, lhsc); | |
4481 | } | |
b7091901 | 4482 | } |
726a989a | 4483 | for (i = 0; i < gimple_asm_ninputs (t); ++i) |
b7091901 | 4484 | { |
5006671f RG |
4485 | tree link = gimple_asm_input_op (t, i); |
4486 | tree op = TREE_VALUE (link); | |
4487 | ||
4488 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); | |
4489 | ||
4490 | parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints, | |
4491 | &allows_mem, &allows_reg); | |
4492 | ||
4493 | /* A memory constraint makes the address of the operand escape. */ | |
4494 | if (!allows_reg && allows_mem) | |
4495 | make_escape_constraint (build_fold_addr_expr (op)); | |
4496 | /* Strictly we'd only need the constraint to ESCAPED if | |
3e8542ca RG |
4497 | the asm clobbers memory, otherwise using something |
4498 | along the lines of per-call clobbers/uses would be enough. */ | |
0f8d6231 | 4499 | else if (op) |
b7091901 RG |
4500 | make_escape_constraint (op); |
4501 | } | |
4502 | } | |
4503 | ||
4ee00913 DB |
4504 | VEC_free (ce_s, heap, rhsc); |
4505 | VEC_free (ce_s, heap, lhsc); | |
910fdc79 DB |
4506 | } |
4507 | ||
4508 | ||
25a6a873 RG |
4509 | /* Create a constraint adding to the clobber set of FI the memory |
4510 | pointed to by PTR. */ | |
4511 | ||
4512 | static void | |
4513 | process_ipa_clobber (varinfo_t fi, tree ptr) | |
4514 | { | |
4515 | VEC(ce_s, heap) *ptrc = NULL; | |
4516 | struct constraint_expr *c, lhs; | |
4517 | unsigned i; | |
ed6c4831 | 4518 | get_constraint_for_rhs (ptr, &ptrc); |
25a6a873 | 4519 | lhs = get_function_part_constraint (fi, fi_clobbers); |
ac47786e | 4520 | FOR_EACH_VEC_ELT (ce_s, ptrc, i, c) |
25a6a873 RG |
4521 | process_constraint (new_constraint (lhs, *c)); |
4522 | VEC_free (ce_s, heap, ptrc); | |
4523 | } | |
4524 | ||
4525 | /* Walk statement T setting up clobber and use constraints according to the | |
4526 | references found in T. This function is a main part of the | |
4527 | IPA constraint builder. */ | |
4528 | ||
4529 | static void | |
4530 | find_func_clobbers (gimple origt) | |
4531 | { | |
4532 | gimple t = origt; | |
4533 | VEC(ce_s, heap) *lhsc = NULL; | |
4534 | VEC(ce_s, heap) *rhsc = NULL; | |
4535 | varinfo_t fi; | |
4536 | ||
4537 | /* Add constraints for clobbered/used in IPA mode. | |
4538 | We are not interested in what automatic variables are clobbered | |
4539 | or used as we only use the information in the caller to which | |
4540 | they do not escape. */ | |
4541 | gcc_assert (in_ipa_mode); | |
4542 | ||
4543 | /* If the stmt refers to memory in any way it better had a VUSE. */ | |
4544 | if (gimple_vuse (t) == NULL_TREE) | |
4545 | return; | |
4546 | ||
4547 | /* We'd better have function information for the current function. */ | |
4548 | fi = lookup_vi_for_tree (cfun->decl); | |
4549 | gcc_assert (fi != NULL); | |
4550 | ||
4551 | /* Account for stores in assignments and calls. */ | |
4552 | if (gimple_vdef (t) != NULL_TREE | |
4553 | && gimple_has_lhs (t)) | |
4554 | { | |
4555 | tree lhs = gimple_get_lhs (t); | |
4556 | tree tem = lhs; | |
4557 | while (handled_component_p (tem)) | |
4558 | tem = TREE_OPERAND (tem, 0); | |
4559 | if ((DECL_P (tem) | |
4560 | && !auto_var_in_fn_p (tem, cfun->decl)) | |
70f34814 RG |
4561 | || INDIRECT_REF_P (tem) |
4562 | || (TREE_CODE (tem) == MEM_REF | |
4563 | && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR | |
4564 | && auto_var_in_fn_p | |
4565 | (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl)))) | |
25a6a873 RG |
4566 | { |
4567 | struct constraint_expr lhsc, *rhsp; | |
4568 | unsigned i; | |
4569 | lhsc = get_function_part_constraint (fi, fi_clobbers); | |
4570 | get_constraint_for_address_of (lhs, &rhsc); | |
ac47786e | 4571 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4572 | process_constraint (new_constraint (lhsc, *rhsp)); |
4573 | VEC_free (ce_s, heap, rhsc); | |
4574 | } | |
4575 | } | |
4576 | ||
4577 | /* Account for uses in assigments and returns. */ | |
4578 | if (gimple_assign_single_p (t) | |
4579 | || (gimple_code (t) == GIMPLE_RETURN | |
4580 | && gimple_return_retval (t) != NULL_TREE)) | |
4581 | { | |
4582 | tree rhs = (gimple_assign_single_p (t) | |
4583 | ? gimple_assign_rhs1 (t) : gimple_return_retval (t)); | |
4584 | tree tem = rhs; | |
4585 | while (handled_component_p (tem)) | |
4586 | tem = TREE_OPERAND (tem, 0); | |
4587 | if ((DECL_P (tem) | |
4588 | && !auto_var_in_fn_p (tem, cfun->decl)) | |
70f34814 RG |
4589 | || INDIRECT_REF_P (tem) |
4590 | || (TREE_CODE (tem) == MEM_REF | |
4591 | && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR | |
4592 | && auto_var_in_fn_p | |
4593 | (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl)))) | |
25a6a873 RG |
4594 | { |
4595 | struct constraint_expr lhs, *rhsp; | |
4596 | unsigned i; | |
4597 | lhs = get_function_part_constraint (fi, fi_uses); | |
4598 | get_constraint_for_address_of (rhs, &rhsc); | |
ac47786e | 4599 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4600 | process_constraint (new_constraint (lhs, *rhsp)); |
4601 | VEC_free (ce_s, heap, rhsc); | |
4602 | } | |
4603 | } | |
4604 | ||
4605 | if (is_gimple_call (t)) | |
4606 | { | |
4607 | varinfo_t cfi = NULL; | |
4608 | tree decl = gimple_call_fndecl (t); | |
4609 | struct constraint_expr lhs, rhs; | |
4610 | unsigned i, j; | |
4611 | ||
4612 | /* For builtins we do not have separate function info. For those | |
4613 | we do not generate escapes for we have to generate clobbers/uses. */ | |
4614 | if (decl | |
4615 | && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) | |
4616 | switch (DECL_FUNCTION_CODE (decl)) | |
4617 | { | |
4618 | /* The following functions use and clobber memory pointed to | |
4619 | by their arguments. */ | |
4620 | case BUILT_IN_STRCPY: | |
4621 | case BUILT_IN_STRNCPY: | |
4622 | case BUILT_IN_BCOPY: | |
4623 | case BUILT_IN_MEMCPY: | |
4624 | case BUILT_IN_MEMMOVE: | |
4625 | case BUILT_IN_MEMPCPY: | |
4626 | case BUILT_IN_STPCPY: | |
4627 | case BUILT_IN_STPNCPY: | |
4628 | case BUILT_IN_STRCAT: | |
4629 | case BUILT_IN_STRNCAT: | |
4630 | { | |
4631 | tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl) | |
4632 | == BUILT_IN_BCOPY ? 1 : 0)); | |
4633 | tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl) | |
4634 | == BUILT_IN_BCOPY ? 0 : 1)); | |
4635 | unsigned i; | |
4636 | struct constraint_expr *rhsp, *lhsp; | |
4637 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); | |
4638 | lhs = get_function_part_constraint (fi, fi_clobbers); | |
ac47786e | 4639 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
25a6a873 RG |
4640 | process_constraint (new_constraint (lhs, *lhsp)); |
4641 | VEC_free (ce_s, heap, lhsc); | |
4642 | get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc); | |
4643 | lhs = get_function_part_constraint (fi, fi_uses); | |
ac47786e | 4644 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
4645 | process_constraint (new_constraint (lhs, *rhsp)); |
4646 | VEC_free (ce_s, heap, rhsc); | |
4647 | return; | |
4648 | } | |
4649 | /* The following function clobbers memory pointed to by | |
4650 | its argument. */ | |
4651 | case BUILT_IN_MEMSET: | |
4652 | { | |
4653 | tree dest = gimple_call_arg (t, 0); | |
4654 | unsigned i; | |
4655 | ce_s *lhsp; | |
4656 | get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc); | |
4657 | lhs = get_function_part_constraint (fi, fi_clobbers); | |
ac47786e | 4658 | FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp) |
25a6a873 RG |
4659 | process_constraint (new_constraint (lhs, *lhsp)); |
4660 | VEC_free (ce_s, heap, lhsc); | |
4661 | return; | |
4662 | } | |
4663 | /* The following functions clobber their second and third | |
4664 | arguments. */ | |
4665 | case BUILT_IN_SINCOS: | |
4666 | case BUILT_IN_SINCOSF: | |
4667 | case BUILT_IN_SINCOSL: | |
4668 | { | |
4669 | process_ipa_clobber (fi, gimple_call_arg (t, 1)); | |
4670 | process_ipa_clobber (fi, gimple_call_arg (t, 2)); | |
4671 | return; | |
4672 | } | |
4673 | /* The following functions clobber their second argument. */ | |
4674 | case BUILT_IN_FREXP: | |
4675 | case BUILT_IN_FREXPF: | |
4676 | case BUILT_IN_FREXPL: | |
4677 | case BUILT_IN_LGAMMA_R: | |
4678 | case BUILT_IN_LGAMMAF_R: | |
4679 | case BUILT_IN_LGAMMAL_R: | |
4680 | case BUILT_IN_GAMMA_R: | |
4681 | case BUILT_IN_GAMMAF_R: | |
4682 | case BUILT_IN_GAMMAL_R: | |
4683 | case BUILT_IN_MODF: | |
4684 | case BUILT_IN_MODFF: | |
4685 | case BUILT_IN_MODFL: | |
4686 | { | |
4687 | process_ipa_clobber (fi, gimple_call_arg (t, 1)); | |
4688 | return; | |
4689 | } | |
4690 | /* The following functions clobber their third argument. */ | |
4691 | case BUILT_IN_REMQUO: | |
4692 | case BUILT_IN_REMQUOF: | |
4693 | case BUILT_IN_REMQUOL: | |
4694 | { | |
4695 | process_ipa_clobber (fi, gimple_call_arg (t, 2)); | |
4696 | return; | |
4697 | } | |
4698 | /* The following functions neither read nor clobber memory. */ | |
4699 | case BUILT_IN_FREE: | |
4700 | return; | |
4701 | /* Trampolines are of no interest to us. */ | |
4702 | case BUILT_IN_INIT_TRAMPOLINE: | |
4703 | case BUILT_IN_ADJUST_TRAMPOLINE: | |
4704 | return; | |
4705 | case BUILT_IN_VA_START: | |
4706 | case BUILT_IN_VA_END: | |
4707 | return; | |
4708 | /* printf-style functions may have hooks to set pointers to | |
4709 | point to somewhere into the generated string. Leave them | |
4710 | for a later excercise... */ | |
4711 | default: | |
4712 | /* Fallthru to general call handling. */; | |
4713 | } | |
4714 | ||
4715 | /* Parameters passed by value are used. */ | |
4716 | lhs = get_function_part_constraint (fi, fi_uses); | |
4717 | for (i = 0; i < gimple_call_num_args (t); i++) | |
4718 | { | |
4719 | struct constraint_expr *rhsp; | |
4720 | tree arg = gimple_call_arg (t, i); | |
4721 | ||
4722 | if (TREE_CODE (arg) == SSA_NAME | |
4723 | || is_gimple_min_invariant (arg)) | |
4724 | continue; | |
4725 | ||
4726 | get_constraint_for_address_of (arg, &rhsc); | |
ac47786e | 4727 | FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp) |
25a6a873 RG |
4728 | process_constraint (new_constraint (lhs, *rhsp)); |
4729 | VEC_free (ce_s, heap, rhsc); | |
4730 | } | |
4731 | ||
4732 | /* Build constraints for propagating clobbers/uses along the | |
4733 | callgraph edges. */ | |
4734 | cfi = get_fi_for_callee (t); | |
4735 | if (cfi->id == anything_id) | |
4736 | { | |
4737 | if (gimple_vdef (t)) | |
4738 | make_constraint_from (first_vi_for_offset (fi, fi_clobbers), | |
4739 | anything_id); | |
4740 | make_constraint_from (first_vi_for_offset (fi, fi_uses), | |
4741 | anything_id); | |
4742 | return; | |
4743 | } | |
4744 | ||
4745 | /* For callees without function info (that's external functions), | |
4746 | ESCAPED is clobbered and used. */ | |
4747 | if (gimple_call_fndecl (t) | |
4748 | && !cfi->is_fn_info) | |
4749 | { | |
4750 | varinfo_t vi; | |
4751 | ||
4752 | if (gimple_vdef (t)) | |
4753 | make_copy_constraint (first_vi_for_offset (fi, fi_clobbers), | |
4754 | escaped_id); | |
4755 | make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id); | |
4756 | ||
4757 | /* Also honor the call statement use/clobber info. */ | |
4758 | if ((vi = lookup_call_clobber_vi (t)) != NULL) | |
4759 | make_copy_constraint (first_vi_for_offset (fi, fi_clobbers), | |
4760 | vi->id); | |
4761 | if ((vi = lookup_call_use_vi (t)) != NULL) | |
4762 | make_copy_constraint (first_vi_for_offset (fi, fi_uses), | |
4763 | vi->id); | |
4764 | return; | |
4765 | } | |
4766 | ||
4767 | /* Otherwise the caller clobbers and uses what the callee does. | |
4768 | ??? This should use a new complex constraint that filters | |
4769 | local variables of the callee. */ | |
4770 | if (gimple_vdef (t)) | |
4771 | { | |
4772 | lhs = get_function_part_constraint (fi, fi_clobbers); | |
4773 | rhs = get_function_part_constraint (cfi, fi_clobbers); | |
4774 | process_constraint (new_constraint (lhs, rhs)); | |
4775 | } | |
4776 | lhs = get_function_part_constraint (fi, fi_uses); | |
4777 | rhs = get_function_part_constraint (cfi, fi_uses); | |
4778 | process_constraint (new_constraint (lhs, rhs)); | |
4779 | } | |
4780 | else if (gimple_code (t) == GIMPLE_ASM) | |
4781 | { | |
4782 | /* ??? Ick. We can do better. */ | |
4783 | if (gimple_vdef (t)) | |
4784 | make_constraint_from (first_vi_for_offset (fi, fi_clobbers), | |
4785 | anything_id); | |
4786 | make_constraint_from (first_vi_for_offset (fi, fi_uses), | |
4787 | anything_id); | |
4788 | } | |
4789 | ||
4790 | VEC_free (ce_s, heap, rhsc); | |
4791 | } | |
4792 | ||
4793 | ||
910fdc79 | 4794 | /* Find the first varinfo in the same variable as START that overlaps with |
5006671f | 4795 | OFFSET. Return NULL if we can't find one. */ |
910fdc79 | 4796 | |
c58936b6 | 4797 | static varinfo_t |
910fdc79 DB |
4798 | first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset) |
4799 | { | |
5006671f RG |
4800 | /* If the offset is outside of the variable, bail out. */ |
4801 | if (offset >= start->fullsize) | |
4802 | return NULL; | |
4803 | ||
4804 | /* If we cannot reach offset from start, lookup the first field | |
4805 | and start from there. */ | |
4806 | if (start->offset > offset) | |
4807 | start = lookup_vi_for_tree (start->decl); | |
4808 | ||
4809 | while (start) | |
910fdc79 DB |
4810 | { |
4811 | /* We may not find a variable in the field list with the actual | |
4812 | offset when when we have glommed a structure to a variable. | |
4813 | In that case, however, offset should still be within the size | |
4814 | of the variable. */ | |
5006671f | 4815 | if (offset >= start->offset |
de925a03 | 4816 | && (offset - start->offset) < start->size) |
5006671f RG |
4817 | return start; |
4818 | ||
4819 | start= start->next; | |
910fdc79 | 4820 | } |
5006671f | 4821 | |
8971094d | 4822 | return NULL; |
910fdc79 DB |
4823 | } |
4824 | ||
5006671f RG |
4825 | /* Find the first varinfo in the same variable as START that overlaps with |
4826 | OFFSET. If there is no such varinfo the varinfo directly preceding | |
4827 | OFFSET is returned. */ | |
4828 | ||
4829 | static varinfo_t | |
4830 | first_or_preceding_vi_for_offset (varinfo_t start, | |
4831 | unsigned HOST_WIDE_INT offset) | |
4832 | { | |
4833 | /* If we cannot reach offset from start, lookup the first field | |
4834 | and start from there. */ | |
4835 | if (start->offset > offset) | |
4836 | start = lookup_vi_for_tree (start->decl); | |
4837 | ||
4838 | /* We may not find a variable in the field list with the actual | |
4839 | offset when when we have glommed a structure to a variable. | |
4840 | In that case, however, offset should still be within the size | |
4841 | of the variable. | |
4842 | If we got beyond the offset we look for return the field | |
4843 | directly preceding offset which may be the last field. */ | |
4844 | while (start->next | |
4845 | && offset >= start->offset | |
de925a03 | 4846 | && !((offset - start->offset) < start->size)) |
5006671f RG |
4847 | start = start->next; |
4848 | ||
4849 | return start; | |
4850 | } | |
4851 | ||
910fdc79 | 4852 | |
31de5b77 RG |
4853 | /* This structure is used during pushing fields onto the fieldstack |
4854 | to track the offset of the field, since bitpos_of_field gives it | |
4855 | relative to its immediate containing type, and we want it relative | |
4856 | to the ultimate containing object. */ | |
4857 | ||
4858 | struct fieldoff | |
4859 | { | |
ee7d4b57 RG |
4860 | /* Offset from the base of the base containing object to this field. */ |
4861 | HOST_WIDE_INT offset; | |
31de5b77 RG |
4862 | |
4863 | /* Size, in bits, of the field. */ | |
ee7d4b57 | 4864 | unsigned HOST_WIDE_INT size; |
31de5b77 | 4865 | |
ee7d4b57 | 4866 | unsigned has_unknown_size : 1; |
31de5b77 | 4867 | |
0f8d6231 RG |
4868 | unsigned must_have_pointers : 1; |
4869 | ||
ee7d4b57 | 4870 | unsigned may_have_pointers : 1; |
74d27244 RG |
4871 | |
4872 | unsigned only_restrict_pointers : 1; | |
31de5b77 RG |
4873 | }; |
4874 | typedef struct fieldoff fieldoff_s; | |
4875 | ||
4876 | DEF_VEC_O(fieldoff_s); | |
4877 | DEF_VEC_ALLOC_O(fieldoff_s,heap); | |
4878 | ||
910fdc79 DB |
4879 | /* qsort comparison function for two fieldoff's PA and PB */ |
4880 | ||
c58936b6 | 4881 | static int |
910fdc79 DB |
4882 | fieldoff_compare (const void *pa, const void *pb) |
4883 | { | |
4884 | const fieldoff_s *foa = (const fieldoff_s *)pa; | |
4885 | const fieldoff_s *fob = (const fieldoff_s *)pb; | |
185ab3b6 | 4886 | unsigned HOST_WIDE_INT foasize, fobsize; |
c58936b6 | 4887 | |
185ab3b6 RG |
4888 | if (foa->offset < fob->offset) |
4889 | return -1; | |
4890 | else if (foa->offset > fob->offset) | |
4891 | return 1; | |
910fdc79 | 4892 | |
ee7d4b57 RG |
4893 | foasize = foa->size; |
4894 | fobsize = fob->size; | |
185ab3b6 | 4895 | if (foasize < fobsize) |
ee7d4b57 | 4896 | return -1; |
185ab3b6 RG |
4897 | else if (foasize > fobsize) |
4898 | return 1; | |
4899 | return 0; | |
910fdc79 DB |
4900 | } |
4901 | ||
4902 | /* Sort a fieldstack according to the field offset and sizes. */ | |
31de5b77 | 4903 | static void |
83f676b3 | 4904 | sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack) |
910fdc79 | 4905 | { |
5095da95 | 4906 | VEC_qsort (fieldoff_s, fieldstack, fieldoff_compare); |
910fdc79 DB |
4907 | } |
4908 | ||
31de5b77 RG |
4909 | /* Return true if V is a tree that we can have subvars for. |
4910 | Normally, this is any aggregate type. Also complex | |
4911 | types which are not gimple registers can have subvars. */ | |
4912 | ||
4913 | static inline bool | |
4914 | var_can_have_subvars (const_tree v) | |
4915 | { | |
4916 | /* Volatile variables should never have subvars. */ | |
4917 | if (TREE_THIS_VOLATILE (v)) | |
4918 | return false; | |
4919 | ||
4920 | /* Non decls or memory tags can never have subvars. */ | |
5006671f | 4921 | if (!DECL_P (v)) |
31de5b77 RG |
4922 | return false; |
4923 | ||
4924 | /* Aggregates without overlapping fields can have subvars. */ | |
4925 | if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE) | |
4926 | return true; | |
4927 | ||
4928 | return false; | |
4929 | } | |
4930 | ||
0f8d6231 RG |
4931 | /* Return true if T is a type that does contain pointers. */ |
4932 | ||
4933 | static bool | |
4934 | type_must_have_pointers (tree type) | |
4935 | { | |
4936 | if (POINTER_TYPE_P (type)) | |
4937 | return true; | |
4938 | ||
4939 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4940 | return type_must_have_pointers (TREE_TYPE (type)); | |
4941 | ||
4942 | /* A function or method can have pointers as arguments, so track | |
4943 | those separately. */ | |
4944 | if (TREE_CODE (type) == FUNCTION_TYPE | |
4945 | || TREE_CODE (type) == METHOD_TYPE) | |
4946 | return true; | |
4947 | ||
4948 | return false; | |
4949 | } | |
4950 | ||
4951 | static bool | |
4952 | field_must_have_pointers (tree t) | |
4953 | { | |
4954 | return type_must_have_pointers (TREE_TYPE (t)); | |
4955 | } | |
4956 | ||
d7705551 DN |
4957 | /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all |
4958 | the fields of TYPE onto fieldstack, recording their offsets along | |
4959 | the way. | |
4960 | ||
4961 | OFFSET is used to keep track of the offset in this entire | |
4962 | structure, rather than just the immediately containing structure. | |
18abb35e RG |
4963 | Returns false if the caller is supposed to handle the field we |
4964 | recursed for. */ | |
910fdc79 | 4965 | |
18abb35e | 4966 | static bool |
c58936b6 | 4967 | push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack, |
0f8d6231 | 4968 | HOST_WIDE_INT offset) |
910fdc79 DB |
4969 | { |
4970 | tree field; | |
18abb35e | 4971 | bool empty_p = true; |
31de5b77 RG |
4972 | |
4973 | if (TREE_CODE (type) != RECORD_TYPE) | |
18abb35e | 4974 | return false; |
3fe2f42a RG |
4975 | |
4976 | /* If the vector of fields is growing too big, bail out early. | |
4977 | Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make | |
4978 | sure this fails. */ | |
31de5b77 | 4979 | if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE) |
18abb35e | 4980 | return false; |
c58936b6 | 4981 | |
910ad8de | 4982 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
31de5b77 RG |
4983 | if (TREE_CODE (field) == FIELD_DECL) |
4984 | { | |
4985 | bool push = false; | |
ee7d4b57 | 4986 | HOST_WIDE_INT foff = bitpos_of_field (field); |
31de5b77 | 4987 | |
ee7d4b57 RG |
4988 | if (!var_can_have_subvars (field) |
4989 | || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE | |
4990 | || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE) | |
31de5b77 | 4991 | push = true; |
18abb35e | 4992 | else if (!push_fields_onto_fieldstack |
0f8d6231 | 4993 | (TREE_TYPE (field), fieldstack, offset + foff) |
31de5b77 RG |
4994 | && (DECL_SIZE (field) |
4995 | && !integer_zerop (DECL_SIZE (field)))) | |
4996 | /* Empty structures may have actual size, like in C++. So | |
4997 | see if we didn't push any subfields and the size is | |
4998 | nonzero, push the field onto the stack. */ | |
4999 | push = true; | |
5000 | ||
5001 | if (push) | |
910fdc79 | 5002 | { |
ee7d4b57 RG |
5003 | fieldoff_s *pair = NULL; |
5004 | bool has_unknown_size = false; | |
0f8d6231 | 5005 | bool must_have_pointers_p; |
ee7d4b57 RG |
5006 | |
5007 | if (!VEC_empty (fieldoff_s, *fieldstack)) | |
5008 | pair = VEC_last (fieldoff_s, *fieldstack); | |
5009 | ||
3fd3b156 RG |
5010 | /* If there isn't anything at offset zero, create sth. */ |
5011 | if (!pair | |
5012 | && offset + foff != 0) | |
5013 | { | |
5014 | pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL); | |
5015 | pair->offset = 0; | |
5016 | pair->size = offset + foff; | |
5017 | pair->has_unknown_size = false; | |
5018 | pair->must_have_pointers = false; | |
5019 | pair->may_have_pointers = false; | |
5020 | pair->only_restrict_pointers = false; | |
5021 | } | |
5022 | ||
ee7d4b57 RG |
5023 | if (!DECL_SIZE (field) |
5024 | || !host_integerp (DECL_SIZE (field), 1)) | |
5025 | has_unknown_size = true; | |
5026 | ||
5027 | /* If adjacent fields do not contain pointers merge them. */ | |
0f8d6231 | 5028 | must_have_pointers_p = field_must_have_pointers (field); |
ee7d4b57 | 5029 | if (pair |
ee7d4b57 | 5030 | && !has_unknown_size |
a81b065a | 5031 | && !must_have_pointers_p |
0f8d6231 RG |
5032 | && !pair->must_have_pointers |
5033 | && !pair->has_unknown_size | |
5034 | && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff) | |
ee7d4b57 | 5035 | { |
ee7d4b57 RG |
5036 | pair->size += TREE_INT_CST_LOW (DECL_SIZE (field)); |
5037 | } | |
5038 | else | |
5039 | { | |
5040 | pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL); | |
5041 | pair->offset = offset + foff; | |
5042 | pair->has_unknown_size = has_unknown_size; | |
5043 | if (!has_unknown_size) | |
5044 | pair->size = TREE_INT_CST_LOW (DECL_SIZE (field)); | |
5045 | else | |
5046 | pair->size = -1; | |
0f8d6231 RG |
5047 | pair->must_have_pointers = must_have_pointers_p; |
5048 | pair->may_have_pointers = true; | |
74d27244 RG |
5049 | pair->only_restrict_pointers |
5050 | = (!has_unknown_size | |
5051 | && POINTER_TYPE_P (TREE_TYPE (field)) | |
5052 | && TYPE_RESTRICT (TREE_TYPE (field))); | |
ee7d4b57 | 5053 | } |
31de5b77 | 5054 | } |
18abb35e RG |
5055 | |
5056 | empty_p = false; | |
31de5b77 | 5057 | } |
910fdc79 | 5058 | |
18abb35e | 5059 | return !empty_p; |
910fdc79 DB |
5060 | } |
5061 | ||
5006671f RG |
5062 | /* Count the number of arguments DECL has, and set IS_VARARGS to true |
5063 | if it is a varargs function. */ | |
5064 | ||
5065 | static unsigned int | |
5066 | count_num_arguments (tree decl, bool *is_varargs) | |
5067 | { | |
de925a03 | 5068 | unsigned int num = 0; |
5006671f RG |
5069 | tree t; |
5070 | ||
de925a03 RG |
5071 | /* Capture named arguments for K&R functions. They do not |
5072 | have a prototype and thus no TYPE_ARG_TYPES. */ | |
910ad8de | 5073 | for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t)) |
de925a03 | 5074 | ++num; |
c58936b6 | 5075 | |
de925a03 RG |
5076 | /* Check if the function has variadic arguments. */ |
5077 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) | |
5078 | if (TREE_VALUE (t) == void_type_node) | |
5079 | break; | |
4ee00913 DB |
5080 | if (!t) |
5081 | *is_varargs = true; | |
de925a03 RG |
5082 | |
5083 | return num; | |
4ee00913 DB |
5084 | } |
5085 | ||
5086 | /* Creation function node for DECL, using NAME, and return the index | |
5087 | of the variable we've created for the function. */ | |
5088 | ||
27c2cfa6 | 5089 | static varinfo_t |
4ee00913 DB |
5090 | create_function_info_for (tree decl, const char *name) |
5091 | { | |
25a6a873 RG |
5092 | struct function *fn = DECL_STRUCT_FUNCTION (decl); |
5093 | varinfo_t vi, prev_vi; | |
c58936b6 | 5094 | tree arg; |
4ee00913 DB |
5095 | unsigned int i; |
5096 | bool is_varargs = false; | |
25a6a873 | 5097 | unsigned int num_args = count_num_arguments (decl, &is_varargs); |
4ee00913 DB |
5098 | |
5099 | /* Create the variable info. */ | |
5100 | ||
0bbf2ffa | 5101 | vi = new_var_info (decl, name); |
4ee00913 | 5102 | vi->offset = 0; |
4ee00913 | 5103 | vi->size = 1; |
25a6a873 RG |
5104 | vi->fullsize = fi_parm_base + num_args; |
5105 | vi->is_fn_info = 1; | |
5106 | vi->may_have_pointers = false; | |
5107 | if (is_varargs) | |
5108 | vi->fullsize = ~0; | |
3e5937d7 | 5109 | insert_vi_for_tree (vi->decl, vi); |
4ee00913 | 5110 | |
25a6a873 RG |
5111 | prev_vi = vi; |
5112 | ||
5113 | /* Create a variable for things the function clobbers and one for | |
5114 | things the function uses. */ | |
4ee00913 | 5115 | { |
25a6a873 RG |
5116 | varinfo_t clobbervi, usevi; |
5117 | const char *newname; | |
5118 | char *tempname; | |
5119 | ||
5120 | asprintf (&tempname, "%s.clobber", name); | |
5121 | newname = ggc_strdup (tempname); | |
5122 | free (tempname); | |
5123 | ||
5124 | clobbervi = new_var_info (NULL, newname); | |
5125 | clobbervi->offset = fi_clobbers; | |
5126 | clobbervi->size = 1; | |
5127 | clobbervi->fullsize = vi->fullsize; | |
5128 | clobbervi->is_full_var = true; | |
5129 | clobbervi->is_global_var = false; | |
5130 | gcc_assert (prev_vi->offset < clobbervi->offset); | |
5131 | prev_vi->next = clobbervi; | |
5132 | prev_vi = clobbervi; | |
25a6a873 RG |
5133 | |
5134 | asprintf (&tempname, "%s.use", name); | |
5135 | newname = ggc_strdup (tempname); | |
5136 | free (tempname); | |
5137 | ||
5138 | usevi = new_var_info (NULL, newname); | |
5139 | usevi->offset = fi_uses; | |
5140 | usevi->size = 1; | |
5141 | usevi->fullsize = vi->fullsize; | |
5142 | usevi->is_full_var = true; | |
5143 | usevi->is_global_var = false; | |
5144 | gcc_assert (prev_vi->offset < usevi->offset); | |
5145 | prev_vi->next = usevi; | |
5146 | prev_vi = usevi; | |
4ee00913 DB |
5147 | } |
5148 | ||
25a6a873 RG |
5149 | /* And one for the static chain. */ |
5150 | if (fn->static_chain_decl != NULL_TREE) | |
5151 | { | |
5152 | varinfo_t chainvi; | |
5153 | const char *newname; | |
5154 | char *tempname; | |
5155 | ||
5156 | asprintf (&tempname, "%s.chain", name); | |
5157 | newname = ggc_strdup (tempname); | |
5158 | free (tempname); | |
5159 | ||
5160 | chainvi = new_var_info (fn->static_chain_decl, newname); | |
5161 | chainvi->offset = fi_static_chain; | |
5162 | chainvi->size = 1; | |
5163 | chainvi->fullsize = vi->fullsize; | |
5164 | chainvi->is_full_var = true; | |
5165 | chainvi->is_global_var = false; | |
5166 | gcc_assert (prev_vi->offset < chainvi->offset); | |
5167 | prev_vi->next = chainvi; | |
5168 | prev_vi = chainvi; | |
25a6a873 RG |
5169 | insert_vi_for_tree (fn->static_chain_decl, chainvi); |
5170 | } | |
5171 | ||
5172 | /* Create a variable for the return var. */ | |
5173 | if (DECL_RESULT (decl) != NULL | |
5174 | || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) | |
5175 | { | |
5176 | varinfo_t resultvi; | |
5177 | const char *newname; | |
5178 | char *tempname; | |
5179 | tree resultdecl = decl; | |
5180 | ||
5181 | if (DECL_RESULT (decl)) | |
5182 | resultdecl = DECL_RESULT (decl); | |
5183 | ||
5184 | asprintf (&tempname, "%s.result", name); | |
5185 | newname = ggc_strdup (tempname); | |
5186 | free (tempname); | |
5187 | ||
5188 | resultvi = new_var_info (resultdecl, newname); | |
5189 | resultvi->offset = fi_result; | |
5190 | resultvi->size = 1; | |
5191 | resultvi->fullsize = vi->fullsize; | |
5192 | resultvi->is_full_var = true; | |
5193 | if (DECL_RESULT (decl)) | |
0f8d6231 | 5194 | resultvi->may_have_pointers = true; |
25a6a873 RG |
5195 | gcc_assert (prev_vi->offset < resultvi->offset); |
5196 | prev_vi->next = resultvi; | |
5197 | prev_vi = resultvi; | |
25a6a873 RG |
5198 | if (DECL_RESULT (decl)) |
5199 | insert_vi_for_tree (DECL_RESULT (decl), resultvi); | |
5200 | } | |
4ee00913 | 5201 | |
6416ae7f | 5202 | /* Set up variables for each argument. */ |
25a6a873 RG |
5203 | arg = DECL_ARGUMENTS (decl); |
5204 | for (i = 0; i < num_args; i++) | |
c58936b6 | 5205 | { |
4ee00913 DB |
5206 | varinfo_t argvi; |
5207 | const char *newname; | |
5208 | char *tempname; | |
4ee00913 DB |
5209 | tree argdecl = decl; |
5210 | ||
5211 | if (arg) | |
5212 | argdecl = arg; | |
c58936b6 | 5213 | |
25a6a873 | 5214 | asprintf (&tempname, "%s.arg%d", name, i); |
4ee00913 DB |
5215 | newname = ggc_strdup (tempname); |
5216 | free (tempname); | |
5217 | ||
0bbf2ffa | 5218 | argvi = new_var_info (argdecl, newname); |
25a6a873 | 5219 | argvi->offset = fi_parm_base + i; |
4ee00913 | 5220 | argvi->size = 1; |
e5bae89b | 5221 | argvi->is_full_var = true; |
4ee00913 | 5222 | argvi->fullsize = vi->fullsize; |
25a6a873 | 5223 | if (arg) |
0f8d6231 | 5224 | argvi->may_have_pointers = true; |
25a6a873 RG |
5225 | gcc_assert (prev_vi->offset < argvi->offset); |
5226 | prev_vi->next = argvi; | |
5227 | prev_vi = argvi; | |
4ee00913 DB |
5228 | if (arg) |
5229 | { | |
3e5937d7 | 5230 | insert_vi_for_tree (arg, argvi); |
910ad8de | 5231 | arg = DECL_CHAIN (arg); |
4ee00913 DB |
5232 | } |
5233 | } | |
4cf4d6a3 | 5234 | |
25a6a873 RG |
5235 | /* Add one representative for all further args. */ |
5236 | if (is_varargs) | |
4ee00913 | 5237 | { |
25a6a873 | 5238 | varinfo_t argvi; |
4ee00913 DB |
5239 | const char *newname; |
5240 | char *tempname; | |
25a6a873 | 5241 | tree decl; |
c58936b6 | 5242 | |
25a6a873 | 5243 | asprintf (&tempname, "%s.varargs", name); |
4ee00913 DB |
5244 | newname = ggc_strdup (tempname); |
5245 | free (tempname); | |
5246 | ||
25a6a873 | 5247 | /* We need sth that can be pointed to for va_start. */ |
7d6e2521 | 5248 | decl = build_fake_var_decl (ptr_type_node); |
25a6a873 RG |
5249 | |
5250 | argvi = new_var_info (decl, newname); | |
5251 | argvi->offset = fi_parm_base + num_args; | |
5252 | argvi->size = ~0; | |
5253 | argvi->is_full_var = true; | |
5254 | argvi->is_heap_var = true; | |
5255 | argvi->fullsize = vi->fullsize; | |
5256 | gcc_assert (prev_vi->offset < argvi->offset); | |
5257 | prev_vi->next = argvi; | |
5258 | prev_vi = argvi; | |
4ee00913 | 5259 | } |
0bbf2ffa | 5260 | |
27c2cfa6 | 5261 | return vi; |
c58936b6 | 5262 | } |
4ee00913 | 5263 | |
6c11790d | 5264 | |
c58936b6 | 5265 | /* Return true if FIELDSTACK contains fields that overlap. |
6c11790d DB |
5266 | FIELDSTACK is assumed to be sorted by offset. */ |
5267 | ||
5268 | static bool | |
5269 | check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack) | |
5270 | { | |
5271 | fieldoff_s *fo = NULL; | |
5272 | unsigned int i; | |
30d2662c | 5273 | HOST_WIDE_INT lastoffset = -1; |
6c11790d | 5274 | |
ac47786e | 5275 | FOR_EACH_VEC_ELT (fieldoff_s, fieldstack, i, fo) |
6c11790d DB |
5276 | { |
5277 | if (fo->offset == lastoffset) | |
5278 | return true; | |
5279 | lastoffset = fo->offset; | |
5280 | } | |
5281 | return false; | |
5282 | } | |
21392f19 | 5283 | |
910fdc79 DB |
5284 | /* Create a varinfo structure for NAME and DECL, and add it to VARMAP. |
5285 | This will also create any varinfo structures necessary for fields | |
5286 | of DECL. */ | |
5287 | ||
18abb35e RG |
5288 | static varinfo_t |
5289 | create_variable_info_for_1 (tree decl, const char *name) | |
910fdc79 | 5290 | { |
18abb35e | 5291 | varinfo_t vi, newvi; |
82d6e6fc KG |
5292 | tree decl_type = TREE_TYPE (decl); |
5293 | tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type); | |
910fdc79 | 5294 | VEC (fieldoff_s,heap) *fieldstack = NULL; |
18abb35e RG |
5295 | fieldoff_s *fo; |
5296 | unsigned int i; | |
c58936b6 | 5297 | |
4ee00913 | 5298 | if (!declsize |
ee7d4b57 | 5299 | || !host_integerp (declsize, 1)) |
910fdc79 | 5300 | { |
18abb35e RG |
5301 | vi = new_var_info (decl, name); |
5302 | vi->offset = 0; | |
910fdc79 | 5303 | vi->size = ~0; |
18abb35e RG |
5304 | vi->fullsize = ~0; |
5305 | vi->is_unknown_size_var = true; | |
5306 | vi->is_full_var = true; | |
0f8d6231 | 5307 | vi->may_have_pointers = true; |
18abb35e | 5308 | return vi; |
910fdc79 | 5309 | } |
18abb35e RG |
5310 | |
5311 | /* Collect field information. */ | |
5312 | if (use_field_sensitive | |
5313 | && var_can_have_subvars (decl) | |
5314 | /* ??? Force us to not use subfields for global initializers | |
5315 | in IPA mode. Else we'd have to parse arbitrary initializers. */ | |
5316 | && !(in_ipa_mode | |
5317 | && is_global_var (decl) | |
5318 | && DECL_INITIAL (decl))) | |
910fdc79 | 5319 | { |
18abb35e RG |
5320 | fieldoff_s *fo = NULL; |
5321 | bool notokay = false; | |
5322 | unsigned int i; | |
5323 | ||
0f8d6231 | 5324 | push_fields_onto_fieldstack (decl_type, &fieldstack, 0); |
18abb35e RG |
5325 | |
5326 | for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++) | |
5327 | if (fo->has_unknown_size | |
5328 | || fo->offset < 0) | |
5329 | { | |
5330 | notokay = true; | |
5331 | break; | |
5332 | } | |
5333 | ||
5334 | /* We can't sort them if we have a field with a variable sized type, | |
5335 | which will make notokay = true. In that case, we are going to return | |
5336 | without creating varinfos for the fields anyway, so sorting them is a | |
5337 | waste to boot. */ | |
5338 | if (!notokay) | |
5339 | { | |
5340 | sort_fieldstack (fieldstack); | |
5341 | /* Due to some C++ FE issues, like PR 22488, we might end up | |
5342 | what appear to be overlapping fields even though they, | |
5343 | in reality, do not overlap. Until the C++ FE is fixed, | |
5344 | we will simply disable field-sensitivity for these cases. */ | |
5345 | notokay = check_for_overlaps (fieldstack); | |
5346 | } | |
5347 | ||
5348 | if (notokay) | |
5349 | VEC_free (fieldoff_s, heap, fieldstack); | |
5350 | } | |
5351 | ||
5352 | /* If we didn't end up collecting sub-variables create a full | |
5353 | variable for the decl. */ | |
5354 | if (VEC_length (fieldoff_s, fieldstack) <= 1 | |
5355 | || VEC_length (fieldoff_s, fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE) | |
5356 | { | |
5357 | vi = new_var_info (decl, name); | |
5358 | vi->offset = 0; | |
0f8d6231 | 5359 | vi->may_have_pointers = true; |
4ee00913 | 5360 | vi->fullsize = TREE_INT_CST_LOW (declsize); |
910fdc79 | 5361 | vi->size = vi->fullsize; |
18abb35e RG |
5362 | vi->is_full_var = true; |
5363 | VEC_free (fieldoff_s, heap, fieldstack); | |
5364 | return vi; | |
910fdc79 | 5365 | } |
c58936b6 | 5366 | |
18abb35e RG |
5367 | vi = new_var_info (decl, name); |
5368 | vi->fullsize = TREE_INT_CST_LOW (declsize); | |
5369 | for (i = 0, newvi = vi; | |
5370 | VEC_iterate (fieldoff_s, fieldstack, i, fo); | |
5371 | ++i, newvi = newvi->next) | |
5372 | { | |
5373 | const char *newname = "NULL"; | |
5374 | char *tempname; | |
5375 | ||
5376 | if (dump_file) | |
5377 | { | |
5378 | asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC | |
5379 | "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size); | |
5380 | newname = ggc_strdup (tempname); | |
5381 | free (tempname); | |
5382 | } | |
5383 | newvi->name = newname; | |
5384 | newvi->offset = fo->offset; | |
5385 | newvi->size = fo->size; | |
5386 | newvi->fullsize = vi->fullsize; | |
5387 | newvi->may_have_pointers = fo->may_have_pointers; | |
5388 | newvi->only_restrict_pointers = fo->only_restrict_pointers; | |
5389 | if (i + 1 < VEC_length (fieldoff_s, fieldstack)) | |
5390 | newvi->next = new_var_info (decl, name); | |
5391 | } | |
5392 | ||
5393 | VEC_free (fieldoff_s, heap, fieldstack); | |
25a6a873 | 5394 | |
18abb35e RG |
5395 | return vi; |
5396 | } | |
5397 | ||
5398 | static unsigned int | |
5399 | create_variable_info_for (tree decl, const char *name) | |
5400 | { | |
5401 | varinfo_t vi = create_variable_info_for_1 (decl, name); | |
5402 | unsigned int id = vi->id; | |
5403 | ||
5404 | insert_vi_for_tree (decl, vi); | |
5405 | ||
5406 | /* Create initial constraints for globals. */ | |
5407 | for (; vi; vi = vi->next) | |
13c6bff4 | 5408 | { |
18abb35e RG |
5409 | if (!vi->may_have_pointers |
5410 | || !vi->is_global_var) | |
5411 | continue; | |
5412 | ||
25a6a873 | 5413 | /* Mark global restrict qualified pointers. */ |
18abb35e RG |
5414 | if ((POINTER_TYPE_P (TREE_TYPE (decl)) |
5415 | && TYPE_RESTRICT (TREE_TYPE (decl))) | |
5416 | || vi->only_restrict_pointers) | |
74d27244 | 5417 | make_constraint_from_restrict (vi, "GLOBAL_RESTRICT"); |
25a6a873 RG |
5418 | |
5419 | /* For escaped variables initialize them from nonlocal. */ | |
5420 | if (!in_ipa_mode | |
5421 | || DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
5422 | make_copy_constraint (vi, nonlocal_id); | |
5423 | ||
5424 | /* If this is a global variable with an initializer and we are in | |
5425 | IPA mode generate constraints for it. In non-IPA mode | |
5426 | the initializer from nonlocal is all we need. */ | |
5427 | if (in_ipa_mode | |
18abb35e | 5428 | && DECL_INITIAL (decl)) |
25a6a873 RG |
5429 | { |
5430 | VEC (ce_s, heap) *rhsc = NULL; | |
5431 | struct constraint_expr lhs, *rhsp; | |
5432 | unsigned i; | |
ed6c4831 | 5433 | get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc); |
25a6a873 RG |
5434 | lhs.var = vi->id; |
5435 | lhs.offset = 0; | |
5436 | lhs.type = SCALAR; | |
ac47786e | 5437 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
5438 | process_constraint (new_constraint (lhs, *rhsp)); |
5439 | /* If this is a variable that escapes from the unit | |
5440 | the initializer escapes as well. */ | |
5441 | if (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
5442 | { | |
5443 | lhs.var = escaped_id; | |
5444 | lhs.offset = 0; | |
5445 | lhs.type = SCALAR; | |
ac47786e | 5446 | FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp) |
25a6a873 RG |
5447 | process_constraint (new_constraint (lhs, *rhsp)); |
5448 | } | |
5449 | VEC_free (ce_s, heap, rhsc); | |
25a6a873 | 5450 | } |
13c6bff4 | 5451 | } |
910fdc79 | 5452 | |
18abb35e | 5453 | return id; |
910fdc79 DB |
5454 | } |
5455 | ||
5456 | /* Print out the points-to solution for VAR to FILE. */ | |
5457 | ||
5006671f | 5458 | static void |
910fdc79 DB |
5459 | dump_solution_for_var (FILE *file, unsigned int var) |
5460 | { | |
5461 | varinfo_t vi = get_varinfo (var); | |
5462 | unsigned int i; | |
c58936b6 DB |
5463 | bitmap_iterator bi; |
5464 | ||
25a6a873 RG |
5465 | /* Dump the solution for unified vars anyway, this avoids difficulties |
5466 | in scanning dumps in the testsuite. */ | |
5467 | fprintf (file, "%s = { ", vi->name); | |
5468 | vi = get_varinfo (find (var)); | |
5469 | EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi) | |
5470 | fprintf (file, "%s ", get_varinfo (i)->name); | |
5471 | fprintf (file, "}"); | |
5472 | ||
5473 | /* But note when the variable was unified. */ | |
5474 | if (vi->id != var) | |
5475 | fprintf (file, " same as %s", vi->name); | |
5476 | ||
5477 | fprintf (file, "\n"); | |
910fdc79 DB |
5478 | } |
5479 | ||
5480 | /* Print the points-to solution for VAR to stdout. */ | |
5481 | ||
24e47c76 | 5482 | DEBUG_FUNCTION void |
910fdc79 DB |
5483 | debug_solution_for_var (unsigned int var) |
5484 | { | |
5485 | dump_solution_for_var (stdout, var); | |
5486 | } | |
5487 | ||
910fdc79 DB |
5488 | /* Create varinfo structures for all of the variables in the |
5489 | function for intraprocedural mode. */ | |
5490 | ||
5491 | static void | |
5492 | intra_create_variable_infos (void) | |
5493 | { | |
5494 | tree t; | |
b23987ec | 5495 | |
6e7e772d | 5496 | /* For each incoming pointer argument arg, create the constraint ARG |
0d3c82d6 RG |
5497 | = NONLOCAL or a dummy variable if it is a restrict qualified |
5498 | passed-by-reference argument. */ | |
910ad8de | 5499 | for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t)) |
910fdc79 | 5500 | { |
910fdc79 | 5501 | varinfo_t p; |
c58936b6 | 5502 | |
bacd3fb6 RG |
5503 | /* For restrict qualified pointers to objects passed by |
5504 | reference build a real representative for the pointed-to object. */ | |
5505 | if (DECL_BY_REFERENCE (t) | |
5506 | && POINTER_TYPE_P (TREE_TYPE (t)) | |
5507 | && TYPE_RESTRICT (TREE_TYPE (t))) | |
5508 | { | |
5509 | struct constraint_expr lhsc, rhsc; | |
5510 | varinfo_t vi; | |
7d6e2521 RG |
5511 | tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t))); |
5512 | DECL_EXTERNAL (heapvar) = 1; | |
5513 | vi = get_varinfo (create_variable_info_for (heapvar, "PARM_NOALIAS")); | |
bacd3fb6 RG |
5514 | lhsc.var = get_vi_for_tree (t)->id; |
5515 | lhsc.type = SCALAR; | |
5516 | lhsc.offset = 0; | |
7d6e2521 | 5517 | rhsc.var = vi->id; |
bacd3fb6 RG |
5518 | rhsc.type = ADDRESSOF; |
5519 | rhsc.offset = 0; | |
5520 | process_constraint (new_constraint (lhsc, rhsc)); | |
5521 | vi->is_restrict_var = 1; | |
5522 | continue; | |
5523 | } | |
5524 | ||
10174ddf | 5525 | for (p = get_vi_for_tree (t); p; p = p->next) |
18abb35e RG |
5526 | { |
5527 | if (p->may_have_pointers) | |
5528 | make_constraint_from (p, nonlocal_id); | |
5529 | if (p->only_restrict_pointers) | |
5530 | make_constraint_from_restrict (p, "PARM_RESTRICT"); | |
5531 | } | |
74d27244 RG |
5532 | if (POINTER_TYPE_P (TREE_TYPE (t)) |
5533 | && TYPE_RESTRICT (TREE_TYPE (t))) | |
5534 | make_constraint_from_restrict (get_vi_for_tree (t), "PARM_RESTRICT"); | |
21392f19 | 5535 | } |
75af9746 | 5536 | |
10bd6c5c RG |
5537 | /* Add a constraint for a result decl that is passed by reference. */ |
5538 | if (DECL_RESULT (cfun->decl) | |
5539 | && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl))) | |
5540 | { | |
5541 | varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl)); | |
5542 | ||
5543 | for (p = result_vi; p; p = p->next) | |
5006671f | 5544 | make_constraint_from (p, nonlocal_id); |
10bd6c5c RG |
5545 | } |
5546 | ||
75af9746 RG |
5547 | /* Add a constraint for the incoming static chain parameter. */ |
5548 | if (cfun->static_chain_decl != NULL_TREE) | |
5549 | { | |
5550 | varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl); | |
5551 | ||
5552 | for (p = chain_vi; p; p = p->next) | |
5553 | make_constraint_from (p, nonlocal_id); | |
5554 | } | |
910fdc79 DB |
5555 | } |
5556 | ||
1296c31f DB |
5557 | /* Structure used to put solution bitmaps in a hashtable so they can |
5558 | be shared among variables with the same points-to set. */ | |
5559 | ||
5560 | typedef struct shared_bitmap_info | |
5561 | { | |
5562 | bitmap pt_vars; | |
5563 | hashval_t hashcode; | |
5564 | } *shared_bitmap_info_t; | |
e5cfc29f | 5565 | typedef const struct shared_bitmap_info *const_shared_bitmap_info_t; |
1296c31f DB |
5566 | |
5567 | static htab_t shared_bitmap_table; | |
5568 | ||
5569 | /* Hash function for a shared_bitmap_info_t */ | |
5570 | ||
5571 | static hashval_t | |
5572 | shared_bitmap_hash (const void *p) | |
5573 | { | |
e5cfc29f | 5574 | const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p; |
1296c31f DB |
5575 | return bi->hashcode; |
5576 | } | |
5577 | ||
5578 | /* Equality function for two shared_bitmap_info_t's. */ | |
5579 | ||
5580 | static int | |
5581 | shared_bitmap_eq (const void *p1, const void *p2) | |
5582 | { | |
e5cfc29f KG |
5583 | const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1; |
5584 | const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2; | |
1296c31f DB |
5585 | return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars); |
5586 | } | |
5587 | ||
5588 | /* Lookup a bitmap in the shared bitmap hashtable, and return an already | |
5589 | existing instance if there is one, NULL otherwise. */ | |
5590 | ||
5591 | static bitmap | |
5592 | shared_bitmap_lookup (bitmap pt_vars) | |
5593 | { | |
5594 | void **slot; | |
5595 | struct shared_bitmap_info sbi; | |
5596 | ||
5597 | sbi.pt_vars = pt_vars; | |
5598 | sbi.hashcode = bitmap_hash (pt_vars); | |
7b765bed | 5599 | |
1296c31f DB |
5600 | slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi, |
5601 | sbi.hashcode, NO_INSERT); | |
5602 | if (!slot) | |
5603 | return NULL; | |
5604 | else | |
5605 | return ((shared_bitmap_info_t) *slot)->pt_vars; | |
5606 | } | |
5607 | ||
5608 | ||
5609 | /* Add a bitmap to the shared bitmap hashtable. */ | |
5610 | ||
5611 | static void | |
5612 | shared_bitmap_add (bitmap pt_vars) | |
5613 | { | |
5614 | void **slot; | |
5615 | shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info); | |
7b765bed | 5616 | |
1296c31f DB |
5617 | sbi->pt_vars = pt_vars; |
5618 | sbi->hashcode = bitmap_hash (pt_vars); | |
7b765bed | 5619 | |
1296c31f DB |
5620 | slot = htab_find_slot_with_hash (shared_bitmap_table, sbi, |
5621 | sbi->hashcode, INSERT); | |
5622 | gcc_assert (!*slot); | |
5623 | *slot = (void *) sbi; | |
5624 | } | |
5625 | ||
5626 | ||
4d7a65ea | 5627 | /* Set bits in INTO corresponding to the variable uids in solution set FROM. */ |
910fdc79 | 5628 | |
b8698a0f | 5629 | static void |
4d7a65ea | 5630 | set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt) |
910fdc79 DB |
5631 | { |
5632 | unsigned int i; | |
5633 | bitmap_iterator bi; | |
f83ca251 | 5634 | |
910fdc79 DB |
5635 | EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi) |
5636 | { | |
5637 | varinfo_t vi = get_varinfo (i); | |
c58936b6 | 5638 | |
e8ca4159 DN |
5639 | /* The only artificial variables that are allowed in a may-alias |
5640 | set are heap variables. */ | |
5641 | if (vi->is_artificial_var && !vi->is_heap_var) | |
5642 | continue; | |
c58936b6 | 5643 | |
5611cf0b RG |
5644 | if (TREE_CODE (vi->decl) == VAR_DECL |
5645 | || TREE_CODE (vi->decl) == PARM_DECL | |
5646 | || TREE_CODE (vi->decl) == RESULT_DECL) | |
58b82d2b | 5647 | { |
25a6a873 RG |
5648 | /* If we are in IPA mode we will not recompute points-to |
5649 | sets after inlining so make sure they stay valid. */ | |
5650 | if (in_ipa_mode | |
5651 | && !DECL_PT_UID_SET_P (vi->decl)) | |
5652 | SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl)); | |
5653 | ||
5006671f RG |
5654 | /* Add the decl to the points-to set. Note that the points-to |
5655 | set contains global variables. */ | |
25a6a873 | 5656 | bitmap_set_bit (into, DECL_PT_UID (vi->decl)); |
74d27244 | 5657 | if (vi->is_global_var) |
5006671f | 5658 | pt->vars_contains_global = true; |
e8ca4159 | 5659 | } |
910fdc79 DB |
5660 | } |
5661 | } | |
e8ca4159 DN |
5662 | |
5663 | ||
4d7a65ea | 5664 | /* Compute the points-to solution *PT for the variable VI. */ |
ce1b6498 RG |
5665 | |
5666 | static void | |
1cfd38be | 5667 | find_what_var_points_to (varinfo_t orig_vi, struct pt_solution *pt) |
ce1b6498 | 5668 | { |
4d7a65ea | 5669 | unsigned int i; |
5006671f RG |
5670 | bitmap_iterator bi; |
5671 | bitmap finished_solution; | |
5672 | bitmap result; | |
1cfd38be | 5673 | varinfo_t vi; |
5006671f RG |
5674 | |
5675 | memset (pt, 0, sizeof (struct pt_solution)); | |
5676 | ||
5677 | /* This variable may have been collapsed, let's get the real | |
5678 | variable. */ | |
1cfd38be | 5679 | vi = get_varinfo (find (orig_vi->id)); |
5006671f RG |
5680 | |
5681 | /* Translate artificial variables into SSA_NAME_PTR_INFO | |
5682 | attributes. */ | |
5683 | EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi) | |
5684 | { | |
5685 | varinfo_t vi = get_varinfo (i); | |
5686 | ||
5687 | if (vi->is_artificial_var) | |
5688 | { | |
5689 | if (vi->id == nothing_id) | |
5690 | pt->null = 1; | |
5691 | else if (vi->id == escaped_id) | |
25a6a873 RG |
5692 | { |
5693 | if (in_ipa_mode) | |
5694 | pt->ipa_escaped = 1; | |
5695 | else | |
5696 | pt->escaped = 1; | |
5697 | } | |
5006671f RG |
5698 | else if (vi->id == nonlocal_id) |
5699 | pt->nonlocal = 1; | |
5700 | else if (vi->is_heap_var) | |
5701 | /* We represent heapvars in the points-to set properly. */ | |
5702 | ; | |
91deb937 RG |
5703 | else if (vi->id == readonly_id) |
5704 | /* Nobody cares. */ | |
5705 | ; | |
5006671f | 5706 | else if (vi->id == anything_id |
5006671f RG |
5707 | || vi->id == integer_id) |
5708 | pt->anything = 1; | |
5709 | } | |
74d27244 RG |
5710 | if (vi->is_restrict_var) |
5711 | pt->vars_contains_restrict = true; | |
5006671f RG |
5712 | } |
5713 | ||
5714 | /* Instead of doing extra work, simply do not create | |
5715 | elaborate points-to information for pt_anything pointers. */ | |
74d27244 | 5716 | if (pt->anything |
1cfd38be | 5717 | && (orig_vi->is_artificial_var |
74d27244 | 5718 | || !pt->vars_contains_restrict)) |
4d7a65ea | 5719 | return; |
5006671f RG |
5720 | |
5721 | /* Share the final set of variables when possible. */ | |
5722 | finished_solution = BITMAP_GGC_ALLOC (); | |
5723 | stats.points_to_sets_created++; | |
5724 | ||
4d7a65ea | 5725 | set_uids_in_ptset (finished_solution, vi->solution, pt); |
5006671f RG |
5726 | result = shared_bitmap_lookup (finished_solution); |
5727 | if (!result) | |
5728 | { | |
5729 | shared_bitmap_add (finished_solution); | |
5730 | pt->vars = finished_solution; | |
5731 | } | |
5732 | else | |
5733 | { | |
5734 | pt->vars = result; | |
5735 | bitmap_clear (finished_solution); | |
5736 | } | |
5006671f RG |
5737 | } |
5738 | ||
4d7a65ea | 5739 | /* Given a pointer variable P, fill in its points-to set. */ |
5006671f RG |
5740 | |
5741 | static void | |
4d7a65ea | 5742 | find_what_p_points_to (tree p) |
5006671f RG |
5743 | { |
5744 | struct ptr_info_def *pi; | |
7cc92f92 | 5745 | tree lookup_p = p; |
3e5937d7 | 5746 | varinfo_t vi; |
e8ca4159 | 5747 | |
7cc92f92 RG |
5748 | /* For parameters, get at the points-to set for the actual parm |
5749 | decl. */ | |
c58936b6 | 5750 | if (TREE_CODE (p) == SSA_NAME |
6938f93f JH |
5751 | && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL |
5752 | || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL) | |
38635499 | 5753 | && SSA_NAME_IS_DEFAULT_DEF (p)) |
7cc92f92 RG |
5754 | lookup_p = SSA_NAME_VAR (p); |
5755 | ||
15814ba0 | 5756 | vi = lookup_vi_for_tree (lookup_p); |
5006671f RG |
5757 | if (!vi) |
5758 | return; | |
5759 | ||
5760 | pi = get_ptr_info (p); | |
4d7a65ea | 5761 | find_what_var_points_to (vi, &pi->pt); |
5006671f | 5762 | } |
7b765bed | 5763 | |
910fdc79 | 5764 | |
5006671f | 5765 | /* Query statistics for points-to solutions. */ |
c58936b6 | 5766 | |
5006671f RG |
5767 | static struct { |
5768 | unsigned HOST_WIDE_INT pt_solution_includes_may_alias; | |
5769 | unsigned HOST_WIDE_INT pt_solution_includes_no_alias; | |
5770 | unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias; | |
5771 | unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias; | |
5772 | } pta_stats; | |
e8ca4159 | 5773 | |
5006671f RG |
5774 | void |
5775 | dump_pta_stats (FILE *s) | |
5776 | { | |
5777 | fprintf (s, "\nPTA query stats:\n"); | |
5778 | fprintf (s, " pt_solution_includes: " | |
5779 | HOST_WIDE_INT_PRINT_DEC" disambiguations, " | |
5780 | HOST_WIDE_INT_PRINT_DEC" queries\n", | |
5781 | pta_stats.pt_solution_includes_no_alias, | |
5782 | pta_stats.pt_solution_includes_no_alias | |
5783 | + pta_stats.pt_solution_includes_may_alias); | |
5784 | fprintf (s, " pt_solutions_intersect: " | |
5785 | HOST_WIDE_INT_PRINT_DEC" disambiguations, " | |
5786 | HOST_WIDE_INT_PRINT_DEC" queries\n", | |
5787 | pta_stats.pt_solutions_intersect_no_alias, | |
5788 | pta_stats.pt_solutions_intersect_no_alias | |
5789 | + pta_stats.pt_solutions_intersect_may_alias); | |
5790 | } | |
e8ca4159 | 5791 | |
9f09b13f | 5792 | |
5006671f RG |
5793 | /* Reset the points-to solution *PT to a conservative default |
5794 | (point to anything). */ | |
7b765bed | 5795 | |
5006671f RG |
5796 | void |
5797 | pt_solution_reset (struct pt_solution *pt) | |
5798 | { | |
5799 | memset (pt, 0, sizeof (struct pt_solution)); | |
5800 | pt->anything = true; | |
5801 | } | |
1296c31f | 5802 | |
55b34b5f | 5803 | /* Set the points-to solution *PT to point only to the variables |
25a6a873 RG |
5804 | in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains |
5805 | global variables and VARS_CONTAINS_RESTRICT specifies whether | |
5806 | it contains restrict tag variables. */ | |
55b34b5f RG |
5807 | |
5808 | void | |
25a6a873 RG |
5809 | pt_solution_set (struct pt_solution *pt, bitmap vars, |
5810 | bool vars_contains_global, bool vars_contains_restrict) | |
55b34b5f | 5811 | { |
55b34b5f RG |
5812 | memset (pt, 0, sizeof (struct pt_solution)); |
5813 | pt->vars = vars; | |
25a6a873 RG |
5814 | pt->vars_contains_global = vars_contains_global; |
5815 | pt->vars_contains_restrict = vars_contains_restrict; | |
5816 | } | |
5817 | ||
90fa9e17 RG |
5818 | /* Set the points-to solution *PT to point only to the variable VAR. */ |
5819 | ||
5820 | void | |
5821 | pt_solution_set_var (struct pt_solution *pt, tree var) | |
5822 | { | |
5823 | memset (pt, 0, sizeof (struct pt_solution)); | |
5824 | pt->vars = BITMAP_GGC_ALLOC (); | |
1b2bb171 | 5825 | bitmap_set_bit (pt->vars, DECL_PT_UID (var)); |
90fa9e17 RG |
5826 | pt->vars_contains_global = is_global_var (var); |
5827 | } | |
5828 | ||
25a6a873 RG |
5829 | /* Computes the union of the points-to solutions *DEST and *SRC and |
5830 | stores the result in *DEST. This changes the points-to bitmap | |
5831 | of *DEST and thus may not be used if that might be shared. | |
5832 | The points-to bitmap of *SRC and *DEST will not be shared after | |
5833 | this function if they were not before. */ | |
5834 | ||
5835 | static void | |
5836 | pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src) | |
5837 | { | |
5838 | dest->anything |= src->anything; | |
5839 | if (dest->anything) | |
55b34b5f | 5840 | { |
25a6a873 RG |
5841 | pt_solution_reset (dest); |
5842 | return; | |
55b34b5f | 5843 | } |
25a6a873 RG |
5844 | |
5845 | dest->nonlocal |= src->nonlocal; | |
5846 | dest->escaped |= src->escaped; | |
5847 | dest->ipa_escaped |= src->ipa_escaped; | |
5848 | dest->null |= src->null; | |
5849 | dest->vars_contains_global |= src->vars_contains_global; | |
5850 | dest->vars_contains_restrict |= src->vars_contains_restrict; | |
5851 | if (!src->vars) | |
5852 | return; | |
5853 | ||
5854 | if (!dest->vars) | |
5855 | dest->vars = BITMAP_GGC_ALLOC (); | |
5856 | bitmap_ior_into (dest->vars, src->vars); | |
55b34b5f RG |
5857 | } |
5858 | ||
5006671f | 5859 | /* Return true if the points-to solution *PT is empty. */ |
e8ca4159 | 5860 | |
25a6a873 | 5861 | bool |
5006671f RG |
5862 | pt_solution_empty_p (struct pt_solution *pt) |
5863 | { | |
5864 | if (pt->anything | |
5865 | || pt->nonlocal) | |
5866 | return false; | |
e8ca4159 | 5867 | |
5006671f RG |
5868 | if (pt->vars |
5869 | && !bitmap_empty_p (pt->vars)) | |
5870 | return false; | |
e8ca4159 | 5871 | |
5006671f RG |
5872 | /* If the solution includes ESCAPED, check if that is empty. */ |
5873 | if (pt->escaped | |
5874 | && !pt_solution_empty_p (&cfun->gimple_df->escaped)) | |
5875 | return false; | |
5876 | ||
25a6a873 RG |
5877 | /* If the solution includes ESCAPED, check if that is empty. */ |
5878 | if (pt->ipa_escaped | |
5879 | && !pt_solution_empty_p (&ipa_escaped_pt)) | |
5880 | return false; | |
5881 | ||
5006671f | 5882 | return true; |
910fdc79 DB |
5883 | } |
5884 | ||
5006671f | 5885 | /* Return true if the points-to solution *PT includes global memory. */ |
63a4ef6f | 5886 | |
2f571334 | 5887 | bool |
5006671f | 5888 | pt_solution_includes_global (struct pt_solution *pt) |
2f571334 | 5889 | { |
5006671f RG |
5890 | if (pt->anything |
5891 | || pt->nonlocal | |
5892 | || pt->vars_contains_global) | |
5893 | return true; | |
2f571334 | 5894 | |
5006671f RG |
5895 | if (pt->escaped) |
5896 | return pt_solution_includes_global (&cfun->gimple_df->escaped); | |
2f571334 | 5897 | |
25a6a873 RG |
5898 | if (pt->ipa_escaped) |
5899 | return pt_solution_includes_global (&ipa_escaped_pt); | |
5900 | ||
5901 | /* ??? This predicate is not correct for the IPA-PTA solution | |
5902 | as we do not properly distinguish between unit escape points | |
5903 | and global variables. */ | |
5904 | if (cfun->gimple_df->ipa_pta) | |
5905 | return true; | |
5906 | ||
5006671f RG |
5907 | return false; |
5908 | } | |
2f571334 | 5909 | |
5006671f RG |
5910 | /* Return true if the points-to solution *PT includes the variable |
5911 | declaration DECL. */ | |
15c15196 | 5912 | |
5006671f RG |
5913 | static bool |
5914 | pt_solution_includes_1 (struct pt_solution *pt, const_tree decl) | |
5915 | { | |
5916 | if (pt->anything) | |
5917 | return true; | |
2f571334 | 5918 | |
5006671f RG |
5919 | if (pt->nonlocal |
5920 | && is_global_var (decl)) | |
5921 | return true; | |
2f571334 | 5922 | |
5006671f | 5923 | if (pt->vars |
25a6a873 | 5924 | && bitmap_bit_p (pt->vars, DECL_PT_UID (decl))) |
5006671f | 5925 | return true; |
2f571334 | 5926 | |
5006671f RG |
5927 | /* If the solution includes ESCAPED, check it. */ |
5928 | if (pt->escaped | |
5929 | && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl)) | |
5930 | return true; | |
2f571334 | 5931 | |
25a6a873 RG |
5932 | /* If the solution includes ESCAPED, check it. */ |
5933 | if (pt->ipa_escaped | |
5934 | && pt_solution_includes_1 (&ipa_escaped_pt, decl)) | |
5935 | return true; | |
5936 | ||
5006671f | 5937 | return false; |
2f571334 | 5938 | } |
910fdc79 | 5939 | |
5006671f RG |
5940 | bool |
5941 | pt_solution_includes (struct pt_solution *pt, const_tree decl) | |
15c15196 | 5942 | { |
5006671f RG |
5943 | bool res = pt_solution_includes_1 (pt, decl); |
5944 | if (res) | |
5945 | ++pta_stats.pt_solution_includes_may_alias; | |
5946 | else | |
5947 | ++pta_stats.pt_solution_includes_no_alias; | |
5948 | return res; | |
5949 | } | |
15c15196 | 5950 | |
5006671f RG |
5951 | /* Return true if both points-to solutions PT1 and PT2 have a non-empty |
5952 | intersection. */ | |
15c15196 | 5953 | |
5006671f RG |
5954 | static bool |
5955 | pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2) | |
5956 | { | |
5957 | if (pt1->anything || pt2->anything) | |
5958 | return true; | |
15c15196 | 5959 | |
5006671f RG |
5960 | /* If either points to unknown global memory and the other points to |
5961 | any global memory they alias. */ | |
5962 | if ((pt1->nonlocal | |
5963 | && (pt2->nonlocal | |
5964 | || pt2->vars_contains_global)) | |
5965 | || (pt2->nonlocal | |
5966 | && pt1->vars_contains_global)) | |
5967 | return true; | |
15c15196 | 5968 | |
5006671f RG |
5969 | /* Check the escaped solution if required. */ |
5970 | if ((pt1->escaped || pt2->escaped) | |
5971 | && !pt_solution_empty_p (&cfun->gimple_df->escaped)) | |
5972 | { | |
5973 | /* If both point to escaped memory and that solution | |
5974 | is not empty they alias. */ | |
5975 | if (pt1->escaped && pt2->escaped) | |
5976 | return true; | |
15c15196 | 5977 | |
5006671f RG |
5978 | /* If either points to escaped memory see if the escaped solution |
5979 | intersects with the other. */ | |
5980 | if ((pt1->escaped | |
5981 | && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2)) | |
5982 | || (pt2->escaped | |
5983 | && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1))) | |
5984 | return true; | |
15c15196 RG |
5985 | } |
5986 | ||
25a6a873 RG |
5987 | /* Check the escaped solution if required. |
5988 | ??? Do we need to check the local against the IPA escaped sets? */ | |
5989 | if ((pt1->ipa_escaped || pt2->ipa_escaped) | |
5990 | && !pt_solution_empty_p (&ipa_escaped_pt)) | |
5991 | { | |
5992 | /* If both point to escaped memory and that solution | |
5993 | is not empty they alias. */ | |
5994 | if (pt1->ipa_escaped && pt2->ipa_escaped) | |
5995 | return true; | |
5996 | ||
5997 | /* If either points to escaped memory see if the escaped solution | |
5998 | intersects with the other. */ | |
5999 | if ((pt1->ipa_escaped | |
6000 | && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2)) | |
6001 | || (pt2->ipa_escaped | |
6002 | && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1))) | |
6003 | return true; | |
6004 | } | |
6005 | ||
5006671f RG |
6006 | /* Now both pointers alias if their points-to solution intersects. */ |
6007 | return (pt1->vars | |
6008 | && pt2->vars | |
6009 | && bitmap_intersect_p (pt1->vars, pt2->vars)); | |
6010 | } | |
6011 | ||
6012 | bool | |
6013 | pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2) | |
6014 | { | |
6015 | bool res = pt_solutions_intersect_1 (pt1, pt2); | |
6016 | if (res) | |
6017 | ++pta_stats.pt_solutions_intersect_may_alias; | |
6018 | else | |
6019 | ++pta_stats.pt_solutions_intersect_no_alias; | |
6020 | return res; | |
15c15196 RG |
6021 | } |
6022 | ||
74d27244 RG |
6023 | /* Return true if both points-to solutions PT1 and PT2 for two restrict |
6024 | qualified pointers are possibly based on the same pointer. */ | |
6025 | ||
6026 | bool | |
6027 | pt_solutions_same_restrict_base (struct pt_solution *pt1, | |
6028 | struct pt_solution *pt2) | |
6029 | { | |
6030 | /* If we deal with points-to solutions of two restrict qualified | |
6031 | pointers solely rely on the pointed-to variable bitmap intersection. | |
6032 | For two pointers that are based on each other the bitmaps will | |
6033 | intersect. */ | |
6034 | if (pt1->vars_contains_restrict | |
6035 | && pt2->vars_contains_restrict) | |
6036 | { | |
6037 | gcc_assert (pt1->vars && pt2->vars); | |
6038 | return bitmap_intersect_p (pt1->vars, pt2->vars); | |
6039 | } | |
6040 | ||
6041 | return true; | |
6042 | } | |
6043 | ||
b7091901 | 6044 | |
63a4ef6f DN |
6045 | /* Dump points-to information to OUTFILE. */ |
6046 | ||
5006671f | 6047 | static void |
910fdc79 DB |
6048 | dump_sa_points_to_info (FILE *outfile) |
6049 | { | |
910fdc79 | 6050 | unsigned int i; |
63a4ef6f | 6051 | |
e8ca4159 | 6052 | fprintf (outfile, "\nPoints-to sets\n\n"); |
63a4ef6f | 6053 | |
910fdc79 DB |
6054 | if (dump_flags & TDF_STATS) |
6055 | { | |
6056 | fprintf (outfile, "Stats:\n"); | |
63a4ef6f | 6057 | fprintf (outfile, "Total vars: %d\n", stats.total_vars); |
3e5937d7 DB |
6058 | fprintf (outfile, "Non-pointer vars: %d\n", |
6059 | stats.nonpointer_vars); | |
63a4ef6f DN |
6060 | fprintf (outfile, "Statically unified vars: %d\n", |
6061 | stats.unified_vars_static); | |
63a4ef6f DN |
6062 | fprintf (outfile, "Dynamically unified vars: %d\n", |
6063 | stats.unified_vars_dynamic); | |
6064 | fprintf (outfile, "Iterations: %d\n", stats.iterations); | |
4ee00913 | 6065 | fprintf (outfile, "Number of edges: %d\n", stats.num_edges); |
3e5937d7 DB |
6066 | fprintf (outfile, "Number of implicit edges: %d\n", |
6067 | stats.num_implicit_edges); | |
910fdc79 | 6068 | } |
63a4ef6f | 6069 | |
910fdc79 | 6070 | for (i = 0; i < VEC_length (varinfo_t, varmap); i++) |
25a6a873 RG |
6071 | { |
6072 | varinfo_t vi = get_varinfo (i); | |
6073 | if (!vi->may_have_pointers) | |
b28ae58f | 6074 | continue; |
25a6a873 RG |
6075 | dump_solution_for_var (outfile, i); |
6076 | } | |
910fdc79 DB |
6077 | } |
6078 | ||
6079 | ||
63a4ef6f DN |
6080 | /* Debug points-to information to stderr. */ |
6081 | ||
24e47c76 | 6082 | DEBUG_FUNCTION void |
63a4ef6f DN |
6083 | debug_sa_points_to_info (void) |
6084 | { | |
6085 | dump_sa_points_to_info (stderr); | |
6086 | } | |
6087 | ||
6088 | ||
910fdc79 DB |
6089 | /* Initialize the always-existing constraint variables for NULL |
6090 | ANYTHING, READONLY, and INTEGER */ | |
6091 | ||
6092 | static void | |
6093 | init_base_vars (void) | |
6094 | { | |
6095 | struct constraint_expr lhs, rhs; | |
0bbf2ffa RG |
6096 | varinfo_t var_anything; |
6097 | varinfo_t var_nothing; | |
6098 | varinfo_t var_readonly; | |
6099 | varinfo_t var_escaped; | |
6100 | varinfo_t var_nonlocal; | |
0bbf2ffa RG |
6101 | varinfo_t var_storedanything; |
6102 | varinfo_t var_integer; | |
910fdc79 DB |
6103 | |
6104 | /* Create the NULL variable, used to represent that a variable points | |
6105 | to NULL. */ | |
0bbf2ffa RG |
6106 | var_nothing = new_var_info (NULL_TREE, "NULL"); |
6107 | gcc_assert (var_nothing->id == nothing_id); | |
910fdc79 DB |
6108 | var_nothing->is_artificial_var = 1; |
6109 | var_nothing->offset = 0; | |
6110 | var_nothing->size = ~0; | |
6111 | var_nothing->fullsize = ~0; | |
13c2c08b | 6112 | var_nothing->is_special_var = 1; |
b28ae58f RG |
6113 | var_nothing->may_have_pointers = 0; |
6114 | var_nothing->is_global_var = 0; | |
910fdc79 DB |
6115 | |
6116 | /* Create the ANYTHING variable, used to represent that a variable | |
6117 | points to some unknown piece of memory. */ | |
0bbf2ffa RG |
6118 | var_anything = new_var_info (NULL_TREE, "ANYTHING"); |
6119 | gcc_assert (var_anything->id == anything_id); | |
910fdc79 DB |
6120 | var_anything->is_artificial_var = 1; |
6121 | var_anything->size = ~0; | |
6122 | var_anything->offset = 0; | |
6123 | var_anything->next = NULL; | |
6124 | var_anything->fullsize = ~0; | |
13c2c08b | 6125 | var_anything->is_special_var = 1; |
910fdc79 DB |
6126 | |
6127 | /* Anything points to anything. This makes deref constraints just | |
c58936b6 | 6128 | work in the presence of linked list and other p = *p type loops, |
910fdc79 | 6129 | by saying that *ANYTHING = ANYTHING. */ |
910fdc79 DB |
6130 | lhs.type = SCALAR; |
6131 | lhs.var = anything_id; | |
6132 | lhs.offset = 0; | |
3e5937d7 | 6133 | rhs.type = ADDRESSOF; |
910fdc79 DB |
6134 | rhs.var = anything_id; |
6135 | rhs.offset = 0; | |
e8ca4159 | 6136 | |
a5eadacc DB |
6137 | /* This specifically does not use process_constraint because |
6138 | process_constraint ignores all anything = anything constraints, since all | |
6139 | but this one are redundant. */ | |
b5efa470 | 6140 | VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs)); |
c58936b6 | 6141 | |
910fdc79 DB |
6142 | /* Create the READONLY variable, used to represent that a variable |
6143 | points to readonly memory. */ | |
0bbf2ffa RG |
6144 | var_readonly = new_var_info (NULL_TREE, "READONLY"); |
6145 | gcc_assert (var_readonly->id == readonly_id); | |
910fdc79 DB |
6146 | var_readonly->is_artificial_var = 1; |
6147 | var_readonly->offset = 0; | |
6148 | var_readonly->size = ~0; | |
6149 | var_readonly->fullsize = ~0; | |
6150 | var_readonly->next = NULL; | |
13c2c08b | 6151 | var_readonly->is_special_var = 1; |
910fdc79 DB |
6152 | |
6153 | /* readonly memory points to anything, in order to make deref | |
6154 | easier. In reality, it points to anything the particular | |
6155 | readonly variable can point to, but we don't track this | |
607fb860 | 6156 | separately. */ |
910fdc79 DB |
6157 | lhs.type = SCALAR; |
6158 | lhs.var = readonly_id; | |
6159 | lhs.offset = 0; | |
3e5937d7 | 6160 | rhs.type = ADDRESSOF; |
b7091901 | 6161 | rhs.var = readonly_id; /* FIXME */ |
910fdc79 | 6162 | rhs.offset = 0; |
b7091901 | 6163 | process_constraint (new_constraint (lhs, rhs)); |
c58936b6 | 6164 | |
b7091901 RG |
6165 | /* Create the ESCAPED variable, used to represent the set of escaped |
6166 | memory. */ | |
0bbf2ffa RG |
6167 | var_escaped = new_var_info (NULL_TREE, "ESCAPED"); |
6168 | gcc_assert (var_escaped->id == escaped_id); | |
b7091901 RG |
6169 | var_escaped->is_artificial_var = 1; |
6170 | var_escaped->offset = 0; | |
6171 | var_escaped->size = ~0; | |
6172 | var_escaped->fullsize = ~0; | |
6173 | var_escaped->is_special_var = 0; | |
b7091901 | 6174 | |
b7091901 RG |
6175 | /* Create the NONLOCAL variable, used to represent the set of nonlocal |
6176 | memory. */ | |
0bbf2ffa RG |
6177 | var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL"); |
6178 | gcc_assert (var_nonlocal->id == nonlocal_id); | |
b7091901 RG |
6179 | var_nonlocal->is_artificial_var = 1; |
6180 | var_nonlocal->offset = 0; | |
6181 | var_nonlocal->size = ~0; | |
6182 | var_nonlocal->fullsize = ~0; | |
6183 | var_nonlocal->is_special_var = 1; | |
b7091901 | 6184 | |
5006671f RG |
6185 | /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */ |
6186 | lhs.type = SCALAR; | |
6187 | lhs.var = escaped_id; | |
6188 | lhs.offset = 0; | |
6189 | rhs.type = DEREF; | |
6190 | rhs.var = escaped_id; | |
6191 | rhs.offset = 0; | |
6192 | process_constraint (new_constraint (lhs, rhs)); | |
6193 | ||
6194 | /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the | |
6195 | whole variable escapes. */ | |
6196 | lhs.type = SCALAR; | |
6197 | lhs.var = escaped_id; | |
6198 | lhs.offset = 0; | |
6199 | rhs.type = SCALAR; | |
6200 | rhs.var = escaped_id; | |
6201 | rhs.offset = UNKNOWN_OFFSET; | |
6202 | process_constraint (new_constraint (lhs, rhs)); | |
6203 | ||
6204 | /* *ESCAPED = NONLOCAL. This is true because we have to assume | |
6205 | everything pointed to by escaped points to what global memory can | |
6206 | point to. */ | |
6207 | lhs.type = DEREF; | |
6208 | lhs.var = escaped_id; | |
6209 | lhs.offset = 0; | |
6210 | rhs.type = SCALAR; | |
6211 | rhs.var = nonlocal_id; | |
6212 | rhs.offset = 0; | |
6213 | process_constraint (new_constraint (lhs, rhs)); | |
6214 | ||
6215 | /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because | |
6216 | global memory may point to global memory and escaped memory. */ | |
b7091901 RG |
6217 | lhs.type = SCALAR; |
6218 | lhs.var = nonlocal_id; | |
6219 | lhs.offset = 0; | |
6220 | rhs.type = ADDRESSOF; | |
5006671f RG |
6221 | rhs.var = nonlocal_id; |
6222 | rhs.offset = 0; | |
6223 | process_constraint (new_constraint (lhs, rhs)); | |
6224 | rhs.type = ADDRESSOF; | |
b7091901 RG |
6225 | rhs.var = escaped_id; |
6226 | rhs.offset = 0; | |
910fdc79 | 6227 | process_constraint (new_constraint (lhs, rhs)); |
c58936b6 | 6228 | |
9e39dba6 RG |
6229 | /* Create the STOREDANYTHING variable, used to represent the set of |
6230 | variables stored to *ANYTHING. */ | |
0bbf2ffa RG |
6231 | var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING"); |
6232 | gcc_assert (var_storedanything->id == storedanything_id); | |
9e39dba6 RG |
6233 | var_storedanything->is_artificial_var = 1; |
6234 | var_storedanything->offset = 0; | |
6235 | var_storedanything->size = ~0; | |
6236 | var_storedanything->fullsize = ~0; | |
6237 | var_storedanything->is_special_var = 0; | |
9e39dba6 | 6238 | |
910fdc79 | 6239 | /* Create the INTEGER variable, used to represent that a variable points |
5006671f | 6240 | to what an INTEGER "points to". */ |
0bbf2ffa RG |
6241 | var_integer = new_var_info (NULL_TREE, "INTEGER"); |
6242 | gcc_assert (var_integer->id == integer_id); | |
910fdc79 DB |
6243 | var_integer->is_artificial_var = 1; |
6244 | var_integer->size = ~0; | |
6245 | var_integer->fullsize = ~0; | |
6246 | var_integer->offset = 0; | |
6247 | var_integer->next = NULL; | |
13c2c08b | 6248 | var_integer->is_special_var = 1; |
a5eadacc | 6249 | |
21392f19 DB |
6250 | /* INTEGER = ANYTHING, because we don't know where a dereference of |
6251 | a random integer will point to. */ | |
a5eadacc DB |
6252 | lhs.type = SCALAR; |
6253 | lhs.var = integer_id; | |
6254 | lhs.offset = 0; | |
3e5937d7 | 6255 | rhs.type = ADDRESSOF; |
a5eadacc DB |
6256 | rhs.var = anything_id; |
6257 | rhs.offset = 0; | |
6258 | process_constraint (new_constraint (lhs, rhs)); | |
c58936b6 | 6259 | } |
910fdc79 | 6260 | |
4ee00913 | 6261 | /* Initialize things necessary to perform PTA */ |
910fdc79 | 6262 | |
4ee00913 DB |
6263 | static void |
6264 | init_alias_vars (void) | |
910fdc79 | 6265 | { |
e5bae89b RG |
6266 | use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1); |
6267 | ||
3e5937d7 DB |
6268 | bitmap_obstack_initialize (&pta_obstack); |
6269 | bitmap_obstack_initialize (&oldpta_obstack); | |
4ee00913 | 6270 | bitmap_obstack_initialize (&predbitmap_obstack); |
910fdc79 | 6271 | |
c58936b6 | 6272 | constraint_pool = create_alloc_pool ("Constraint pool", |
910fdc79 DB |
6273 | sizeof (struct constraint), 30); |
6274 | variable_info_pool = create_alloc_pool ("Variable info pool", | |
6275 | sizeof (struct variable_info), 30); | |
b5efa470 DB |
6276 | constraints = VEC_alloc (constraint_t, heap, 8); |
6277 | varmap = VEC_alloc (varinfo_t, heap, 8); | |
15814ba0 | 6278 | vi_for_tree = pointer_map_create (); |
3e8542ca | 6279 | call_stmt_vars = pointer_map_create (); |
3e5937d7 | 6280 | |
910fdc79 | 6281 | memset (&stats, 0, sizeof (stats)); |
1296c31f DB |
6282 | shared_bitmap_table = htab_create (511, shared_bitmap_hash, |
6283 | shared_bitmap_eq, free); | |
910fdc79 | 6284 | init_base_vars (); |
7d6e2521 RG |
6285 | |
6286 | gcc_obstack_init (&fake_var_decl_obstack); | |
4ee00913 DB |
6287 | } |
6288 | ||
3e5937d7 DB |
6289 | /* Remove the REF and ADDRESS edges from GRAPH, as well as all the |
6290 | predecessor edges. */ | |
6291 | ||
6292 | static void | |
6293 | remove_preds_and_fake_succs (constraint_graph_t graph) | |
6294 | { | |
6295 | unsigned int i; | |
6296 | ||
6297 | /* Clear the implicit ref and address nodes from the successor | |
6298 | lists. */ | |
6299 | for (i = 0; i < FIRST_REF_NODE; i++) | |
6300 | { | |
6301 | if (graph->succs[i]) | |
6302 | bitmap_clear_range (graph->succs[i], FIRST_REF_NODE, | |
6303 | FIRST_REF_NODE * 2); | |
6304 | } | |
6305 | ||
6306 | /* Free the successor list for the non-ref nodes. */ | |
6307 | for (i = FIRST_REF_NODE; i < graph->size; i++) | |
6308 | { | |
6309 | if (graph->succs[i]) | |
6310 | BITMAP_FREE (graph->succs[i]); | |
6311 | } | |
6312 | ||
6313 | /* Now reallocate the size of the successor list as, and blow away | |
6314 | the predecessor bitmaps. */ | |
6315 | graph->size = VEC_length (varinfo_t, varmap); | |
c22940cd | 6316 | graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size); |
3e5937d7 DB |
6317 | |
6318 | free (graph->implicit_preds); | |
6319 | graph->implicit_preds = NULL; | |
6320 | free (graph->preds); | |
6321 | graph->preds = NULL; | |
6322 | bitmap_obstack_release (&predbitmap_obstack); | |
6323 | } | |
6324 | ||
5c245b95 | 6325 | /* Solve the constraint set. */ |
4ee00913 | 6326 | |
5006671f | 6327 | static void |
5c245b95 | 6328 | solve_constraints (void) |
4ee00913 | 6329 | { |
3e5937d7 | 6330 | struct scc_info *si; |
910fdc79 | 6331 | |
21392f19 DB |
6332 | if (dump_file) |
6333 | fprintf (dump_file, | |
6334 | "\nCollapsing static cycles and doing variable " | |
7b765bed DB |
6335 | "substitution\n"); |
6336 | ||
6337 | init_graph (VEC_length (varinfo_t, varmap) * 2); | |
b8698a0f | 6338 | |
7b765bed DB |
6339 | if (dump_file) |
6340 | fprintf (dump_file, "Building predecessor graph\n"); | |
3e5937d7 | 6341 | build_pred_graph (); |
b8698a0f | 6342 | |
7b765bed DB |
6343 | if (dump_file) |
6344 | fprintf (dump_file, "Detecting pointer and location " | |
6345 | "equivalences\n"); | |
3e5937d7 | 6346 | si = perform_var_substitution (graph); |
b8698a0f | 6347 | |
7b765bed DB |
6348 | if (dump_file) |
6349 | fprintf (dump_file, "Rewriting constraints and unifying " | |
6350 | "variables\n"); | |
6351 | rewrite_constraints (graph, si); | |
fc93bcb6 | 6352 | |
8576f20a | 6353 | build_succ_graph (); |
fc93bcb6 | 6354 | |
8d3e3924 RG |
6355 | free_var_substitution_info (si); |
6356 | ||
8576f20a | 6357 | /* Attach complex constraints to graph nodes. */ |
7b765bed DB |
6358 | move_complex_constraints (graph); |
6359 | ||
6360 | if (dump_file) | |
6361 | fprintf (dump_file, "Uniting pointer but not location equivalent " | |
6362 | "variables\n"); | |
6363 | unite_pointer_equivalences (graph); | |
6364 | ||
6365 | if (dump_file) | |
6366 | fprintf (dump_file, "Finding indirect cycles\n"); | |
3e5937d7 | 6367 | find_indirect_cycles (graph); |
c58936b6 | 6368 | |
3e5937d7 DB |
6369 | /* Implicit nodes and predecessors are no longer necessary at this |
6370 | point. */ | |
6371 | remove_preds_and_fake_succs (graph); | |
c58936b6 | 6372 | |
8576f20a RG |
6373 | if (dump_file && (dump_flags & TDF_GRAPH)) |
6374 | { | |
6375 | fprintf (dump_file, "\n\n// The constraint graph before solve-graph " | |
6376 | "in dot format:\n"); | |
6377 | dump_constraint_graph (dump_file); | |
6378 | fprintf (dump_file, "\n\n"); | |
6379 | } | |
6380 | ||
21392f19 | 6381 | if (dump_file) |
7b765bed | 6382 | fprintf (dump_file, "Solving graph\n"); |
c58936b6 | 6383 | |
21392f19 | 6384 | solve_graph (graph); |
c58936b6 | 6385 | |
8576f20a RG |
6386 | if (dump_file && (dump_flags & TDF_GRAPH)) |
6387 | { | |
6388 | fprintf (dump_file, "\n\n// The constraint graph after solve-graph " | |
6389 | "in dot format:\n"); | |
6390 | dump_constraint_graph (dump_file); | |
6391 | fprintf (dump_file, "\n\n"); | |
6392 | } | |
6393 | ||
910fdc79 DB |
6394 | if (dump_file) |
6395 | dump_sa_points_to_info (dump_file); | |
5c245b95 RG |
6396 | } |
6397 | ||
6398 | /* Create points-to sets for the current function. See the comments | |
6399 | at the start of the file for an algorithmic overview. */ | |
6400 | ||
6401 | static void | |
6402 | compute_points_to_sets (void) | |
6403 | { | |
6404 | basic_block bb; | |
6405 | unsigned i; | |
6406 | varinfo_t vi; | |
6407 | ||
6408 | timevar_push (TV_TREE_PTA); | |
6409 | ||
6410 | init_alias_vars (); | |
5c245b95 RG |
6411 | |
6412 | intra_create_variable_infos (); | |
6413 | ||
25a6a873 | 6414 | /* Now walk all statements and build the constraint set. */ |
5c245b95 RG |
6415 | FOR_EACH_BB (bb) |
6416 | { | |
6417 | gimple_stmt_iterator gsi; | |
6418 | ||
6419 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6420 | { | |
6421 | gimple phi = gsi_stmt (gsi); | |
6422 | ||
6423 | if (is_gimple_reg (gimple_phi_result (phi))) | |
6424 | find_func_aliases (phi); | |
6425 | } | |
6426 | ||
6427 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6428 | { | |
6429 | gimple stmt = gsi_stmt (gsi); | |
6430 | ||
6431 | find_func_aliases (stmt); | |
6432 | } | |
6433 | } | |
6434 | ||
25a6a873 RG |
6435 | if (dump_file) |
6436 | { | |
6437 | fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n"); | |
6438 | dump_constraints (dump_file, 0); | |
6439 | } | |
6440 | ||
5c245b95 RG |
6441 | /* From the constraints compute the points-to sets. */ |
6442 | solve_constraints (); | |
c58936b6 | 6443 | |
3e8542ca | 6444 | /* Compute the points-to set for ESCAPED used for call-clobber analysis. */ |
0bbf2ffa RG |
6445 | find_what_var_points_to (get_varinfo (escaped_id), |
6446 | &cfun->gimple_df->escaped); | |
5006671f RG |
6447 | |
6448 | /* Make sure the ESCAPED solution (which is used as placeholder in | |
6449 | other solutions) does not reference itself. This simplifies | |
6450 | points-to solution queries. */ | |
6451 | cfun->gimple_df->escaped.escaped = 0; | |
6452 | ||
14c41b9b | 6453 | /* Mark escaped HEAP variables as global. */ |
ac47786e | 6454 | FOR_EACH_VEC_ELT (varinfo_t, varmap, i, vi) |
14c41b9b | 6455 | if (vi->is_heap_var |
91deb937 | 6456 | && !vi->is_restrict_var |
14c41b9b | 6457 | && !vi->is_global_var) |
91deb937 RG |
6458 | DECL_EXTERNAL (vi->decl) = vi->is_global_var |
6459 | = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl); | |
14c41b9b | 6460 | |
5006671f RG |
6461 | /* Compute the points-to sets for pointer SSA_NAMEs. */ |
6462 | for (i = 0; i < num_ssa_names; ++i) | |
6463 | { | |
6464 | tree ptr = ssa_name (i); | |
6465 | if (ptr | |
6466 | && POINTER_TYPE_P (TREE_TYPE (ptr))) | |
4d7a65ea | 6467 | find_what_p_points_to (ptr); |
5006671f | 6468 | } |
e8ca4159 | 6469 | |
d086d311 RG |
6470 | /* Compute the call-used/clobbered sets. */ |
6471 | FOR_EACH_BB (bb) | |
6472 | { | |
6473 | gimple_stmt_iterator gsi; | |
6474 | ||
6475 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6476 | { | |
6477 | gimple stmt = gsi_stmt (gsi); | |
6478 | struct pt_solution *pt; | |
6479 | if (!is_gimple_call (stmt)) | |
6480 | continue; | |
6481 | ||
6482 | pt = gimple_call_use_set (stmt); | |
6483 | if (gimple_call_flags (stmt) & ECF_CONST) | |
6484 | memset (pt, 0, sizeof (struct pt_solution)); | |
3e8542ca | 6485 | else if ((vi = lookup_call_use_vi (stmt)) != NULL) |
d086d311 | 6486 | { |
3e8542ca RG |
6487 | find_what_var_points_to (vi, pt); |
6488 | /* Escaped (and thus nonlocal) variables are always | |
6489 | implicitly used by calls. */ | |
d086d311 RG |
6490 | /* ??? ESCAPED can be empty even though NONLOCAL |
6491 | always escaped. */ | |
6492 | pt->nonlocal = 1; | |
6493 | pt->escaped = 1; | |
6494 | } | |
6495 | else | |
6496 | { | |
3e8542ca RG |
6497 | /* If there is nothing special about this call then |
6498 | we have made everything that is used also escape. */ | |
d086d311 RG |
6499 | *pt = cfun->gimple_df->escaped; |
6500 | pt->nonlocal = 1; | |
6501 | } | |
6502 | ||
6503 | pt = gimple_call_clobber_set (stmt); | |
6504 | if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS)) | |
6505 | memset (pt, 0, sizeof (struct pt_solution)); | |
3e8542ca RG |
6506 | else if ((vi = lookup_call_clobber_vi (stmt)) != NULL) |
6507 | { | |
6508 | find_what_var_points_to (vi, pt); | |
6509 | /* Escaped (and thus nonlocal) variables are always | |
6510 | implicitly clobbered by calls. */ | |
6511 | /* ??? ESCAPED can be empty even though NONLOCAL | |
6512 | always escaped. */ | |
6513 | pt->nonlocal = 1; | |
6514 | pt->escaped = 1; | |
6515 | } | |
d086d311 RG |
6516 | else |
6517 | { | |
3e8542ca RG |
6518 | /* If there is nothing special about this call then |
6519 | we have made everything that is used also escape. */ | |
d086d311 RG |
6520 | *pt = cfun->gimple_df->escaped; |
6521 | pt->nonlocal = 1; | |
6522 | } | |
6523 | } | |
6524 | } | |
6525 | ||
e8ca4159 | 6526 | timevar_pop (TV_TREE_PTA); |
910fdc79 DB |
6527 | } |
6528 | ||
910fdc79 DB |
6529 | |
6530 | /* Delete created points-to sets. */ | |
6531 | ||
5006671f | 6532 | static void |
e8ca4159 | 6533 | delete_points_to_sets (void) |
910fdc79 | 6534 | { |
7b765bed | 6535 | unsigned int i; |
c58936b6 | 6536 | |
1296c31f | 6537 | htab_delete (shared_bitmap_table); |
3e5937d7 DB |
6538 | if (dump_file && (dump_flags & TDF_STATS)) |
6539 | fprintf (dump_file, "Points to sets created:%d\n", | |
6540 | stats.points_to_sets_created); | |
6541 | ||
15814ba0 | 6542 | pointer_map_destroy (vi_for_tree); |
3e8542ca | 6543 | pointer_map_destroy (call_stmt_vars); |
3e5937d7 | 6544 | bitmap_obstack_release (&pta_obstack); |
b5efa470 | 6545 | VEC_free (constraint_t, heap, constraints); |
c58936b6 | 6546 | |
7b765bed | 6547 | for (i = 0; i < graph->size; i++) |
3e5937d7 | 6548 | VEC_free (constraint_t, heap, graph->complex[i]); |
285463b5 | 6549 | free (graph->complex); |
21392f19 | 6550 | |
3e5937d7 | 6551 | free (graph->rep); |
57250223 | 6552 | free (graph->succs); |
7b765bed DB |
6553 | free (graph->pe); |
6554 | free (graph->pe_rep); | |
3e5937d7 | 6555 | free (graph->indirect_cycles); |
b5efa470 DB |
6556 | free (graph); |
6557 | ||
6558 | VEC_free (varinfo_t, heap, varmap); | |
910fdc79 | 6559 | free_alloc_pool (variable_info_pool); |
c58936b6 | 6560 | free_alloc_pool (constraint_pool); |
7d6e2521 RG |
6561 | |
6562 | obstack_free (&fake_var_decl_obstack, NULL); | |
910fdc79 | 6563 | } |
973162ec | 6564 | |
5006671f RG |
6565 | |
6566 | /* Compute points-to information for every SSA_NAME pointer in the | |
6567 | current function and compute the transitive closure of escaped | |
6568 | variables to re-initialize the call-clobber states of local variables. */ | |
6569 | ||
6570 | unsigned int | |
6571 | compute_may_aliases (void) | |
6572 | { | |
25a6a873 RG |
6573 | if (cfun->gimple_df->ipa_pta) |
6574 | { | |
6575 | if (dump_file) | |
6576 | { | |
6577 | fprintf (dump_file, "\nNot re-computing points-to information " | |
6578 | "because IPA points-to information is available.\n\n"); | |
6579 | ||
6580 | /* But still dump what we have remaining it. */ | |
6581 | dump_alias_info (dump_file); | |
6582 | ||
6583 | if (dump_flags & TDF_DETAILS) | |
6584 | dump_referenced_vars (dump_file); | |
6585 | } | |
6586 | ||
6587 | return 0; | |
6588 | } | |
6589 | ||
5006671f RG |
6590 | /* For each pointer P_i, determine the sets of variables that P_i may |
6591 | point-to. Compute the reachability set of escaped and call-used | |
6592 | variables. */ | |
6593 | compute_points_to_sets (); | |
6594 | ||
6595 | /* Debugging dumps. */ | |
6596 | if (dump_file) | |
6597 | { | |
6598 | dump_alias_info (dump_file); | |
6599 | ||
6600 | if (dump_flags & TDF_DETAILS) | |
6601 | dump_referenced_vars (dump_file); | |
6602 | } | |
6603 | ||
6604 | /* Deallocate memory used by aliasing data structures and the internal | |
6605 | points-to solution. */ | |
6606 | delete_points_to_sets (); | |
6607 | ||
6608 | gcc_assert (!need_ssa_update_p (cfun)); | |
6609 | ||
6610 | return 0; | |
6611 | } | |
6612 | ||
248fc9f3 RG |
6613 | static bool |
6614 | gate_tree_pta (void) | |
6615 | { | |
6616 | return flag_tree_pta; | |
6617 | } | |
5006671f RG |
6618 | |
6619 | /* A dummy pass to cause points-to information to be computed via | |
6620 | TODO_rebuild_alias. */ | |
6621 | ||
6622 | struct gimple_opt_pass pass_build_alias = | |
6623 | { | |
6624 | { | |
6625 | GIMPLE_PASS, | |
6626 | "alias", /* name */ | |
248fc9f3 | 6627 | gate_tree_pta, /* gate */ |
5006671f RG |
6628 | NULL, /* execute */ |
6629 | NULL, /* sub */ | |
6630 | NULL, /* next */ | |
6631 | 0, /* static_pass_number */ | |
7072a650 | 6632 | TV_NONE, /* tv_id */ |
5006671f | 6633 | PROP_cfg | PROP_ssa, /* properties_required */ |
4effdf02 | 6634 | 0, /* properties_provided */ |
5006671f RG |
6635 | 0, /* properties_destroyed */ |
6636 | 0, /* todo_flags_start */ | |
22c5fa5f | 6637 | TODO_rebuild_alias /* todo_flags_finish */ |
5006671f RG |
6638 | } |
6639 | }; | |
6640 | ||
6b8ed145 RG |
6641 | /* A dummy pass to cause points-to information to be computed via |
6642 | TODO_rebuild_alias. */ | |
6643 | ||
6644 | struct gimple_opt_pass pass_build_ealias = | |
6645 | { | |
6646 | { | |
6647 | GIMPLE_PASS, | |
6648 | "ealias", /* name */ | |
6649 | gate_tree_pta, /* gate */ | |
6650 | NULL, /* execute */ | |
6651 | NULL, /* sub */ | |
6652 | NULL, /* next */ | |
6653 | 0, /* static_pass_number */ | |
6654 | TV_NONE, /* tv_id */ | |
6655 | PROP_cfg | PROP_ssa, /* properties_required */ | |
6656 | 0, /* properties_provided */ | |
6657 | 0, /* properties_destroyed */ | |
6658 | 0, /* todo_flags_start */ | |
22c5fa5f | 6659 | TODO_rebuild_alias /* todo_flags_finish */ |
6b8ed145 RG |
6660 | } |
6661 | }; | |
6662 | ||
5006671f | 6663 | |
4ee00913 DB |
6664 | /* Return true if we should execute IPA PTA. */ |
6665 | static bool | |
6666 | gate_ipa_pta (void) | |
6667 | { | |
de925a03 RG |
6668 | return (optimize |
6669 | && flag_ipa_pta | |
4ee00913 | 6670 | /* Don't bother doing anything if the program has errors. */ |
1da2ed5f | 6671 | && !seen_error ()); |
4ee00913 DB |
6672 | } |
6673 | ||
25a6a873 RG |
6674 | /* IPA PTA solutions for ESCAPED. */ |
6675 | struct pt_solution ipa_escaped_pt | |
6676 | = { true, false, false, false, false, false, false, NULL }; | |
6677 | ||
39e2db00 JH |
6678 | /* Associate node with varinfo DATA. Worker for |
6679 | cgraph_for_node_and_aliases. */ | |
6680 | static bool | |
6681 | associate_varinfo_to_alias (struct cgraph_node *node, void *data) | |
6682 | { | |
6683 | if (node->alias || node->thunk.thunk_p) | |
6684 | insert_vi_for_tree (node->decl, (varinfo_t)data); | |
6685 | return false; | |
6686 | } | |
6687 | ||
cd35bcf7 JH |
6688 | /* Associate node with varinfo DATA. Worker for |
6689 | varpool_for_node_and_aliases. */ | |
6690 | static bool | |
6691 | associate_varinfo_to_alias_1 (struct varpool_node *node, void *data) | |
6692 | { | |
6693 | if (node->alias) | |
6694 | insert_vi_for_tree (node->decl, (varinfo_t)data); | |
6695 | return false; | |
6696 | } | |
6697 | ||
4ee00913 | 6698 | /* Execute the driver for IPA PTA. */ |
c2924966 | 6699 | static unsigned int |
4ee00913 DB |
6700 | ipa_pta_execute (void) |
6701 | { | |
6702 | struct cgraph_node *node; | |
25a6a873 RG |
6703 | struct varpool_node *var; |
6704 | int from; | |
3e5937d7 | 6705 | |
4ee00913 | 6706 | in_ipa_mode = 1; |
5c245b95 | 6707 | |
4ee00913 | 6708 | init_alias_vars (); |
c58936b6 | 6709 | |
5c245b95 | 6710 | /* Build the constraints. */ |
4ee00913 DB |
6711 | for (node = cgraph_nodes; node; node = node->next) |
6712 | { | |
27c2cfa6 | 6713 | varinfo_t vi; |
5c245b95 RG |
6714 | /* Nodes without a body are not interesting. Especially do not |
6715 | visit clones at this point for now - we get duplicate decls | |
6716 | there for inline clones at least. */ | |
39e2db00 | 6717 | if (!cgraph_function_with_gimple_body_p (node) |
5c245b95 RG |
6718 | || node->clone_of) |
6719 | continue; | |
6720 | ||
27c2cfa6 | 6721 | vi = create_function_info_for (node->decl, |
39e2db00 JH |
6722 | alias_get_name (node->decl)); |
6723 | cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true); | |
4ee00913 | 6724 | } |
5c245b95 | 6725 | |
25a6a873 RG |
6726 | /* Create constraints for global variables and their initializers. */ |
6727 | for (var = varpool_nodes; var; var = var->next) | |
27c2cfa6 | 6728 | { |
27c2cfa6 | 6729 | varinfo_t vi; |
cd35bcf7 JH |
6730 | if (var->alias) |
6731 | continue; | |
27c2cfa6 RG |
6732 | |
6733 | vi = get_vi_for_tree (var->decl); | |
cd35bcf7 | 6734 | varpool_for_node_and_aliases (var, associate_varinfo_to_alias_1, vi, true); |
27c2cfa6 | 6735 | } |
25a6a873 RG |
6736 | |
6737 | if (dump_file) | |
6738 | { | |
6739 | fprintf (dump_file, | |
6740 | "Generating constraints for global initializers\n\n"); | |
6741 | dump_constraints (dump_file, 0); | |
6742 | fprintf (dump_file, "\n"); | |
6743 | } | |
6744 | from = VEC_length (constraint_t, constraints); | |
6745 | ||
4ee00913 DB |
6746 | for (node = cgraph_nodes; node; node = node->next) |
6747 | { | |
5c245b95 RG |
6748 | struct function *func; |
6749 | basic_block bb; | |
6750 | tree old_func_decl; | |
4ee00913 | 6751 | |
5c245b95 | 6752 | /* Nodes without a body are not interesting. */ |
39e2db00 | 6753 | if (!cgraph_function_with_gimple_body_p (node) |
5c245b95 RG |
6754 | || node->clone_of) |
6755 | continue; | |
c58936b6 | 6756 | |
5c245b95 | 6757 | if (dump_file) |
27c2cfa6 RG |
6758 | { |
6759 | fprintf (dump_file, | |
6760 | "Generating constraints for %s", cgraph_node_name (node)); | |
6761 | if (DECL_ASSEMBLER_NAME_SET_P (node->decl)) | |
6762 | fprintf (dump_file, " (%s)", | |
6763 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl))); | |
6764 | fprintf (dump_file, "\n"); | |
6765 | } | |
c58936b6 | 6766 | |
5c245b95 RG |
6767 | func = DECL_STRUCT_FUNCTION (node->decl); |
6768 | old_func_decl = current_function_decl; | |
6769 | push_cfun (func); | |
6770 | current_function_decl = node->decl; | |
726a989a | 6771 | |
5c245b95 | 6772 | if (node->local.externally_visible) |
194313e2 RG |
6773 | { |
6774 | /* For externally visible functions use local constraints for | |
6775 | their arguments. For local functions we see all callers | |
6776 | and thus do not need initial constraints for parameters. */ | |
6777 | intra_create_variable_infos (); | |
6778 | ||
6779 | /* We also need to make function return values escape. Nothing | |
6780 | escapes by returning from main though. */ | |
6781 | if (!MAIN_NAME_P (DECL_NAME (node->decl))) | |
6782 | { | |
6783 | varinfo_t fi, rvi; | |
6784 | fi = lookup_vi_for_tree (node->decl); | |
6785 | rvi = first_vi_for_offset (fi, fi_result); | |
6786 | if (rvi && rvi->offset == fi_result) | |
6787 | { | |
6788 | struct constraint_expr includes; | |
6789 | struct constraint_expr var; | |
6790 | includes.var = escaped_id; | |
6791 | includes.offset = 0; | |
6792 | includes.type = SCALAR; | |
6793 | var.var = rvi->id; | |
6794 | var.offset = 0; | |
6795 | var.type = SCALAR; | |
6796 | process_constraint (new_constraint (includes, var)); | |
6797 | } | |
6798 | } | |
6799 | } | |
4ee00913 | 6800 | |
5c245b95 RG |
6801 | /* Build constriants for the function body. */ |
6802 | FOR_EACH_BB_FN (bb, func) | |
6803 | { | |
6804 | gimple_stmt_iterator gsi; | |
c58936b6 | 6805 | |
5c245b95 RG |
6806 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
6807 | gsi_next (&gsi)) | |
6808 | { | |
6809 | gimple phi = gsi_stmt (gsi); | |
c58936b6 | 6810 | |
5c245b95 RG |
6811 | if (is_gimple_reg (gimple_phi_result (phi))) |
6812 | find_func_aliases (phi); | |
6813 | } | |
3e5937d7 | 6814 | |
5c245b95 RG |
6815 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
6816 | { | |
6817 | gimple stmt = gsi_stmt (gsi); | |
3e5937d7 | 6818 | |
5c245b95 | 6819 | find_func_aliases (stmt); |
25a6a873 | 6820 | find_func_clobbers (stmt); |
5c245b95 RG |
6821 | } |
6822 | } | |
c58936b6 | 6823 | |
5c245b95 RG |
6824 | current_function_decl = old_func_decl; |
6825 | pop_cfun (); | |
25a6a873 RG |
6826 | |
6827 | if (dump_file) | |
6828 | { | |
6829 | fprintf (dump_file, "\n"); | |
6830 | dump_constraints (dump_file, from); | |
6831 | fprintf (dump_file, "\n"); | |
6832 | } | |
6833 | from = VEC_length (constraint_t, constraints); | |
5c245b95 | 6834 | } |
c58936b6 | 6835 | |
5c245b95 RG |
6836 | /* From the constraints compute the points-to sets. */ |
6837 | solve_constraints (); | |
c58936b6 | 6838 | |
25a6a873 RG |
6839 | /* Compute the global points-to sets for ESCAPED. |
6840 | ??? Note that the computed escape set is not correct | |
6841 | for the whole unit as we fail to consider graph edges to | |
6842 | externally visible functions. */ | |
6843 | find_what_var_points_to (get_varinfo (escaped_id), &ipa_escaped_pt); | |
6844 | ||
6845 | /* Make sure the ESCAPED solution (which is used as placeholder in | |
6846 | other solutions) does not reference itself. This simplifies | |
6847 | points-to solution queries. */ | |
6848 | ipa_escaped_pt.ipa_escaped = 0; | |
6849 | ||
6850 | /* Assign the points-to sets to the SSA names in the unit. */ | |
6851 | for (node = cgraph_nodes; node; node = node->next) | |
6852 | { | |
6853 | tree ptr; | |
6854 | struct function *fn; | |
6855 | unsigned i; | |
6856 | varinfo_t fi; | |
6857 | basic_block bb; | |
6858 | struct pt_solution uses, clobbers; | |
6859 | struct cgraph_edge *e; | |
6860 | ||
6861 | /* Nodes without a body are not interesting. */ | |
39e2db00 | 6862 | if (!cgraph_function_with_gimple_body_p (node) |
25a6a873 RG |
6863 | || node->clone_of) |
6864 | continue; | |
6865 | ||
6866 | fn = DECL_STRUCT_FUNCTION (node->decl); | |
6867 | ||
6868 | /* Compute the points-to sets for pointer SSA_NAMEs. */ | |
ac47786e | 6869 | FOR_EACH_VEC_ELT (tree, fn->gimple_df->ssa_names, i, ptr) |
25a6a873 RG |
6870 | { |
6871 | if (ptr | |
6872 | && POINTER_TYPE_P (TREE_TYPE (ptr))) | |
6873 | find_what_p_points_to (ptr); | |
6874 | } | |
6875 | ||
6876 | /* Compute the call-use and call-clobber sets for all direct calls. */ | |
6877 | fi = lookup_vi_for_tree (node->decl); | |
6878 | gcc_assert (fi->is_fn_info); | |
6879 | find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers), | |
6880 | &clobbers); | |
6881 | find_what_var_points_to (first_vi_for_offset (fi, fi_uses), &uses); | |
6882 | for (e = node->callers; e; e = e->next_caller) | |
6883 | { | |
6884 | if (!e->call_stmt) | |
6885 | continue; | |
6886 | ||
6887 | *gimple_call_clobber_set (e->call_stmt) = clobbers; | |
6888 | *gimple_call_use_set (e->call_stmt) = uses; | |
6889 | } | |
6890 | ||
6891 | /* Compute the call-use and call-clobber sets for indirect calls | |
6892 | and calls to external functions. */ | |
6893 | FOR_EACH_BB_FN (bb, fn) | |
6894 | { | |
6895 | gimple_stmt_iterator gsi; | |
6896 | ||
6897 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
6898 | { | |
6899 | gimple stmt = gsi_stmt (gsi); | |
6900 | struct pt_solution *pt; | |
6901 | varinfo_t vi; | |
6902 | tree decl; | |
6903 | ||
6904 | if (!is_gimple_call (stmt)) | |
6905 | continue; | |
6906 | ||
6907 | /* Handle direct calls to external functions. */ | |
6908 | decl = gimple_call_fndecl (stmt); | |
6909 | if (decl | |
6910 | && (!(fi = lookup_vi_for_tree (decl)) | |
6911 | || !fi->is_fn_info)) | |
6912 | { | |
6913 | pt = gimple_call_use_set (stmt); | |
6914 | if (gimple_call_flags (stmt) & ECF_CONST) | |
6915 | memset (pt, 0, sizeof (struct pt_solution)); | |
6916 | else if ((vi = lookup_call_use_vi (stmt)) != NULL) | |
6917 | { | |
6918 | find_what_var_points_to (vi, pt); | |
6919 | /* Escaped (and thus nonlocal) variables are always | |
6920 | implicitly used by calls. */ | |
6921 | /* ??? ESCAPED can be empty even though NONLOCAL | |
6922 | always escaped. */ | |
6923 | pt->nonlocal = 1; | |
6924 | pt->ipa_escaped = 1; | |
6925 | } | |
6926 | else | |
6927 | { | |
6928 | /* If there is nothing special about this call then | |
6929 | we have made everything that is used also escape. */ | |
6930 | *pt = ipa_escaped_pt; | |
6931 | pt->nonlocal = 1; | |
6932 | } | |
6933 | ||
6934 | pt = gimple_call_clobber_set (stmt); | |
6935 | if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS)) | |
6936 | memset (pt, 0, sizeof (struct pt_solution)); | |
6937 | else if ((vi = lookup_call_clobber_vi (stmt)) != NULL) | |
6938 | { | |
6939 | find_what_var_points_to (vi, pt); | |
6940 | /* Escaped (and thus nonlocal) variables are always | |
6941 | implicitly clobbered by calls. */ | |
6942 | /* ??? ESCAPED can be empty even though NONLOCAL | |
6943 | always escaped. */ | |
6944 | pt->nonlocal = 1; | |
6945 | pt->ipa_escaped = 1; | |
6946 | } | |
6947 | else | |
6948 | { | |
6949 | /* If there is nothing special about this call then | |
6950 | we have made everything that is used also escape. */ | |
6951 | *pt = ipa_escaped_pt; | |
6952 | pt->nonlocal = 1; | |
6953 | } | |
6954 | } | |
6955 | ||
6956 | /* Handle indirect calls. */ | |
6957 | if (!decl | |
6958 | && (fi = get_fi_for_callee (stmt))) | |
6959 | { | |
6960 | /* We need to accumulate all clobbers/uses of all possible | |
6961 | callees. */ | |
6962 | fi = get_varinfo (find (fi->id)); | |
6963 | /* If we cannot constrain the set of functions we'll end up | |
6964 | calling we end up using/clobbering everything. */ | |
6965 | if (bitmap_bit_p (fi->solution, anything_id) | |
6966 | || bitmap_bit_p (fi->solution, nonlocal_id) | |
6967 | || bitmap_bit_p (fi->solution, escaped_id)) | |
6968 | { | |
6969 | pt_solution_reset (gimple_call_clobber_set (stmt)); | |
6970 | pt_solution_reset (gimple_call_use_set (stmt)); | |
6971 | } | |
6972 | else | |
6973 | { | |
6974 | bitmap_iterator bi; | |
6975 | unsigned i; | |
6976 | struct pt_solution *uses, *clobbers; | |
6977 | ||
6978 | uses = gimple_call_use_set (stmt); | |
6979 | clobbers = gimple_call_clobber_set (stmt); | |
6980 | memset (uses, 0, sizeof (struct pt_solution)); | |
6981 | memset (clobbers, 0, sizeof (struct pt_solution)); | |
6982 | EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi) | |
6983 | { | |
6984 | struct pt_solution sol; | |
6985 | ||
6986 | vi = get_varinfo (i); | |
6987 | if (!vi->is_fn_info) | |
6988 | { | |
6989 | /* ??? We could be more precise here? */ | |
6990 | uses->nonlocal = 1; | |
6991 | uses->ipa_escaped = 1; | |
6992 | clobbers->nonlocal = 1; | |
6993 | clobbers->ipa_escaped = 1; | |
6994 | continue; | |
6995 | } | |
6996 | ||
6997 | if (!uses->anything) | |
6998 | { | |
6999 | find_what_var_points_to | |
7000 | (first_vi_for_offset (vi, fi_uses), &sol); | |
7001 | pt_solution_ior_into (uses, &sol); | |
7002 | } | |
7003 | if (!clobbers->anything) | |
7004 | { | |
7005 | find_what_var_points_to | |
7006 | (first_vi_for_offset (vi, fi_clobbers), &sol); | |
7007 | pt_solution_ior_into (clobbers, &sol); | |
7008 | } | |
7009 | } | |
7010 | } | |
7011 | } | |
7012 | } | |
7013 | } | |
7014 | ||
7015 | fn->gimple_df->ipa_pta = true; | |
7016 | } | |
7017 | ||
5c245b95 | 7018 | delete_points_to_sets (); |
c58936b6 | 7019 | |
4ee00913 | 7020 | in_ipa_mode = 0; |
5c245b95 | 7021 | |
c2924966 | 7022 | return 0; |
4ee00913 | 7023 | } |
c58936b6 | 7024 | |
8ddbbcae | 7025 | struct simple_ipa_opt_pass pass_ipa_pta = |
4ee00913 | 7026 | { |
8ddbbcae JH |
7027 | { |
7028 | SIMPLE_IPA_PASS, | |
4ee00913 DB |
7029 | "pta", /* name */ |
7030 | gate_ipa_pta, /* gate */ | |
7031 | ipa_pta_execute, /* execute */ | |
7032 | NULL, /* sub */ | |
7033 | NULL, /* next */ | |
7034 | 0, /* static_pass_number */ | |
7035 | TV_IPA_PTA, /* tv_id */ | |
7036 | 0, /* properties_required */ | |
7037 | 0, /* properties_provided */ | |
7038 | 0, /* properties_destroyed */ | |
7039 | 0, /* todo_flags_start */ | |
8ddbbcae JH |
7040 | TODO_update_ssa /* todo_flags_finish */ |
7041 | } | |
4ee00913 | 7042 | }; |