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