]>
Commit | Line | Data |
---|---|---|
ea900239 | 1 | /* Callgraph based analysis of static variables. |
a5544970 | 2 | Copyright (C) 2004-2019 Free Software Foundation, Inc. |
ea900239 DB |
3 | Contributed by Kenneth Zadeck <zadeck@naturalbridge.com> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
ea900239 DB |
10 | version. |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
ea900239 DB |
20 | |
21 | /* This file gathers information about how variables whose scope is | |
b8698a0f | 22 | confined to the compilation unit are used. |
ea900239 | 23 | |
4a444e58 | 24 | The transitive call site specific clobber effects are computed |
ea900239 DB |
25 | for the variables whose scope is contained within this compilation |
26 | unit. | |
27 | ||
28 | First each function and static variable initialization is analyzed | |
29 | to determine which local static variables are either read, written, | |
30 | or have their address taken. Any local static that has its address | |
31 | taken is removed from consideration. Once the local read and | |
32 | writes are determined, a transitive closure of this information is | |
33 | performed over the call graph to determine the worst case set of | |
34 | side effects of each call. In later parts of the compiler, these | |
35 | local and global sets are examined to make the call clobbering less | |
36 | traumatic, promote some statics to registers, and improve aliasing | |
4a444e58 | 37 | information. */ |
ea900239 DB |
38 | |
39 | #include "config.h" | |
40 | #include "system.h" | |
41 | #include "coretypes.h" | |
c7131fb2 AM |
42 | #include "backend.h" |
43 | #include "tree.h" | |
44 | #include "gimple.h" | |
957060b5 AM |
45 | #include "tree-pass.h" |
46 | #include "cgraph.h" | |
47 | #include "data-streamer.h" | |
d8a2d370 | 48 | #include "calls.h" |
ea264ca5 | 49 | #include "splay-tree.h" |
ea900239 DB |
50 | #include "ipa-utils.h" |
51 | #include "ipa-reference.h" | |
6adcb793 | 52 | #include "symbol-summary.h" |
ea900239 | 53 | |
e2c9111c | 54 | /* The static variables defined within the compilation unit that are |
b8698a0f | 55 | loaded or stored directly by function that owns this structure. */ |
e2c9111c | 56 | |
b8698a0f | 57 | struct ipa_reference_local_vars_info_d |
e2c9111c JH |
58 | { |
59 | bitmap statics_read; | |
60 | bitmap statics_written; | |
e2c9111c JH |
61 | }; |
62 | ||
63 | /* Statics that are read and written by some set of functions. The | |
64 | local ones are based on the loads and stores local to the function. | |
65 | The global ones are based on the local info as well as the | |
46c30019 | 66 | transitive closure of the functions that are called. */ |
e2c9111c JH |
67 | |
68 | struct ipa_reference_global_vars_info_d | |
69 | { | |
70 | bitmap statics_read; | |
71 | bitmap statics_written; | |
46c30019 JH |
72 | }; |
73 | ||
61502ca8 | 74 | /* Information we save about every function after ipa-reference is completed. */ |
46c30019 JH |
75 | |
76 | struct ipa_reference_optimization_summary_d | |
77 | { | |
e2c9111c JH |
78 | bitmap statics_not_read; |
79 | bitmap statics_not_written; | |
80 | }; | |
81 | ||
6adcb793 ML |
82 | typedef ipa_reference_local_vars_info_d *ipa_reference_local_vars_info_t; |
83 | typedef ipa_reference_global_vars_info_d *ipa_reference_global_vars_info_t; | |
84 | typedef ipa_reference_optimization_summary_d * | |
85 | ipa_reference_optimization_summary_t; | |
46c30019 | 86 | |
b8698a0f | 87 | struct ipa_reference_vars_info_d |
e2c9111c | 88 | { |
46c30019 JH |
89 | struct ipa_reference_local_vars_info_d local; |
90 | struct ipa_reference_global_vars_info_d global; | |
e2c9111c JH |
91 | }; |
92 | ||
93 | typedef struct ipa_reference_vars_info_d *ipa_reference_vars_info_t; | |
94 | ||
ea900239 | 95 | /* This splay tree contains all of the static variables that are |
46c30019 JH |
96 | being considered by the compilation level alias analysis. */ |
97 | static splay_tree reference_vars_to_consider; | |
ea900239 | 98 | |
df92c640 SB |
99 | /* Set of all interesting module statics. A bit is set for every module |
100 | static we are considering. This is added to the local info when asm | |
101 | code is found that clobbers all memory. */ | |
ea900239 | 102 | static bitmap all_module_statics; |
56aae4b7 | 103 | /* Set of all statics that should be ignored because they are touched by |
b16650ac JH |
104 | -fno-ipa-reference code. */ |
105 | static bitmap ignore_module_statics; | |
ea900239 | 106 | |
ebcf9dc8 JH |
107 | /* Obstack holding bitmaps of local analysis (live from analysis to |
108 | propagation) */ | |
109 | static bitmap_obstack local_info_obstack; | |
110 | /* Obstack holding global analysis live forever. */ | |
46c30019 | 111 | static bitmap_obstack optimization_summary_obstack; |
ea900239 | 112 | |
db30281f ML |
113 | class ipa_ref_var_info_summary_t: public fast_function_summary |
114 | <ipa_reference_vars_info_d *, va_heap> | |
6adcb793 ML |
115 | { |
116 | public: | |
117 | ipa_ref_var_info_summary_t (symbol_table *symtab): | |
db30281f | 118 | fast_function_summary <ipa_reference_vars_info_d *, va_heap> (symtab) {} |
6adcb793 | 119 | }; |
129a37fc | 120 | |
6adcb793 | 121 | static ipa_ref_var_info_summary_t *ipa_ref_var_info_summaries = NULL; |
df92c640 | 122 | |
db30281f ML |
123 | class ipa_ref_opt_summary_t: public fast_function_summary |
124 | <ipa_reference_optimization_summary_d *, va_heap> | |
6adcb793 ML |
125 | { |
126 | public: | |
127 | ipa_ref_opt_summary_t (symbol_table *symtab): | |
db30281f | 128 | fast_function_summary <ipa_reference_optimization_summary_d *, va_heap> (symtab) {} |
6adcb793 ML |
129 | |
130 | virtual void remove (cgraph_node *src_node, | |
131 | ipa_reference_optimization_summary_d *data); | |
132 | virtual void duplicate (cgraph_node *src_node, cgraph_node *dst_node, | |
133 | ipa_reference_optimization_summary_d *src_data, | |
134 | ipa_reference_optimization_summary_d *dst_data); | |
135 | }; | |
136 | ||
137 | static ipa_ref_opt_summary_t *ipa_ref_opt_sum_summaries = NULL; | |
e2c9111c | 138 | |
ea900239 DB |
139 | /* Return the ipa_reference_vars structure starting from the cgraph NODE. */ |
140 | static inline ipa_reference_vars_info_t | |
e2c9111c JH |
141 | get_reference_vars_info (struct cgraph_node *node) |
142 | { | |
6adcb793 | 143 | if (ipa_ref_var_info_summaries == NULL) |
e2c9111c | 144 | return NULL; |
6adcb793 ML |
145 | |
146 | ipa_reference_vars_info_t v = ipa_ref_var_info_summaries->get (node); | |
147 | return v == NULL ? NULL : v; | |
e2c9111c JH |
148 | } |
149 | ||
150 | /* Return the ipa_reference_vars structure starting from the cgraph NODE. */ | |
46c30019 JH |
151 | static inline ipa_reference_optimization_summary_t |
152 | get_reference_optimization_summary (struct cgraph_node *node) | |
ea900239 | 153 | { |
6adcb793 | 154 | if (ipa_ref_opt_sum_summaries == NULL) |
ea900239 | 155 | return NULL; |
ea900239 | 156 | |
6adcb793 ML |
157 | ipa_reference_optimization_summary_t v |
158 | = ipa_ref_opt_sum_summaries->get (node); | |
ea900239 | 159 | |
6adcb793 | 160 | return v == NULL ? NULL : v; |
ea900239 DB |
161 | } |
162 | ||
37074a02 JH |
163 | /* Return a bitmap indexed by ipa_reference_var_uid for the static variables |
164 | that are *not* read during the execution of the function FN. Returns | |
ea900239 DB |
165 | NULL if no data is available. */ |
166 | ||
b8698a0f L |
167 | bitmap |
168 | ipa_reference_get_not_read_global (struct cgraph_node *fn) | |
ea900239 | 169 | { |
13261557 | 170 | if (!opt_for_fn (current_function_decl, flag_ipa_reference)) |
b16650ac | 171 | return NULL; |
13261557 JH |
172 | |
173 | enum availability avail; | |
174 | struct cgraph_node *fn2 = fn->function_symbol (&avail); | |
df92c640 | 175 | ipa_reference_optimization_summary_t info = |
13261557 JH |
176 | get_reference_optimization_summary (fn2); |
177 | ||
178 | if (info | |
179 | && (avail >= AVAIL_AVAILABLE | |
180 | || (avail == AVAIL_INTERPOSABLE | |
181 | && flags_from_decl_or_type (fn->decl) & ECF_LEAF)) | |
182 | && opt_for_fn (fn2->decl, flag_ipa_reference)) | |
46c30019 | 183 | return info->statics_not_read; |
13261557 JH |
184 | else if (avail == AVAIL_NOT_AVAILABLE |
185 | && flags_from_decl_or_type (fn->decl) & ECF_LEAF) | |
46a4da10 | 186 | return all_module_statics; |
ea900239 DB |
187 | else |
188 | return NULL; | |
189 | } | |
190 | ||
37074a02 JH |
191 | /* Return a bitmap indexed by ipa_reference_var_uid for the static variables |
192 | that are *not* written during the execution of the function FN. Note | |
ea900239 DB |
193 | that variables written may or may not be read during the function |
194 | call. Returns NULL if no data is available. */ | |
195 | ||
b8698a0f L |
196 | bitmap |
197 | ipa_reference_get_not_written_global (struct cgraph_node *fn) | |
ea900239 | 198 | { |
13261557 | 199 | if (!opt_for_fn (current_function_decl, flag_ipa_reference)) |
b16650ac | 200 | return NULL; |
13261557 JH |
201 | |
202 | enum availability avail; | |
203 | struct cgraph_node *fn2 = fn->function_symbol (&avail); | |
df92c640 | 204 | ipa_reference_optimization_summary_t info = |
13261557 JH |
205 | get_reference_optimization_summary (fn2); |
206 | ||
207 | if (info | |
208 | && (avail >= AVAIL_AVAILABLE | |
209 | || (avail == AVAIL_INTERPOSABLE | |
210 | && flags_from_decl_or_type (fn->decl) & ECF_LEAF)) | |
211 | && opt_for_fn (fn2->decl, flag_ipa_reference)) | |
46c30019 | 212 | return info->statics_not_written; |
13261557 JH |
213 | else if (avail == AVAIL_NOT_AVAILABLE |
214 | && flags_from_decl_or_type (fn->decl) & ECF_LEAF) | |
46a4da10 | 215 | return all_module_statics; |
ea900239 DB |
216 | else |
217 | return NULL; | |
218 | } | |
ea900239 | 219 | \f |
ea900239 | 220 | |
37074a02 JH |
221 | /* Hepler for is_proper_for_analysis. */ |
222 | static bool | |
223 | is_improper (symtab_node *n, void *v ATTRIBUTE_UNUSED) | |
ea900239 | 224 | { |
37074a02 | 225 | tree t = n->decl; |
ea900239 DB |
226 | /* If the variable has the "used" attribute, treat it as if it had a |
227 | been touched by the devil. */ | |
b42186f1 | 228 | if (DECL_PRESERVE_P (t)) |
37074a02 | 229 | return true; |
ea900239 DB |
230 | |
231 | /* Do not want to do anything with volatile except mark any | |
232 | function that uses one to be not const or pure. */ | |
b8698a0f | 233 | if (TREE_THIS_VOLATILE (t)) |
37074a02 | 234 | return true; |
ea900239 | 235 | |
22a8d1e6 JH |
236 | /* We do not need to analyze readonly vars, we already know they do not |
237 | alias. */ | |
238 | if (TREE_READONLY (t)) | |
37074a02 | 239 | return true; |
22a8d1e6 | 240 | |
67914693 | 241 | /* We cannot track variables with address taken. */ |
1685ecf3 | 242 | if (TREE_ADDRESSABLE (t)) |
37074a02 | 243 | return true; |
1685ecf3 | 244 | |
37074a02 JH |
245 | /* TODO: We could track public variables that are not addressable, but |
246 | currently frontends don't give us those. */ | |
1685ecf3 | 247 | if (TREE_PUBLIC (t)) |
37074a02 JH |
248 | return true; |
249 | ||
250 | return false; | |
251 | } | |
252 | ||
253 | /* Return true if the variable T is the right kind of static variable to | |
254 | perform compilation unit scope escape analysis. */ | |
255 | ||
256 | static inline bool | |
257 | is_proper_for_analysis (tree t) | |
258 | { | |
259 | if (bitmap_bit_p (ignore_module_statics, ipa_reference_var_uid (t))) | |
1685ecf3 JH |
260 | return false; |
261 | ||
37074a02 JH |
262 | if (symtab_node::get (t) |
263 | ->call_for_symbol_and_aliases (is_improper, NULL, true)) | |
b16650ac | 264 | return false; |
1685ecf3 | 265 | |
ea900239 DB |
266 | return true; |
267 | } | |
268 | ||
ea900239 | 269 | /* Lookup the tree node for the static variable that has UID and |
a4174ebf | 270 | convert the name to a string for debugging. */ |
ea900239 DB |
271 | |
272 | static const char * | |
273 | get_static_name (int index) | |
274 | { | |
b8698a0f | 275 | splay_tree_node stn = |
ea900239 | 276 | splay_tree_lookup (reference_vars_to_consider, index); |
df92c640 SB |
277 | return fndecl_name ((tree)(stn->value)); |
278 | } | |
279 | ||
280 | /* Dump a set of static vars to FILE. */ | |
281 | static void | |
282 | dump_static_vars_set_to_file (FILE *f, bitmap set) | |
283 | { | |
284 | unsigned int index; | |
285 | bitmap_iterator bi; | |
286 | if (set == NULL) | |
287 | return; | |
288 | else if (set == all_module_statics) | |
289 | fprintf (f, "ALL"); | |
290 | else | |
291 | EXECUTE_IF_SET_IN_BITMAP (set, 0, index, bi) | |
292 | { | |
293 | fprintf (f, "%s ", get_static_name (index)); | |
294 | } | |
295 | } | |
296 | ||
297 | /* Compute X |= Y, taking into account the possibility that | |
298 | either X or Y is already the maximum set. | |
299 | Return true if X is the maximum set after taking the union with Y. */ | |
300 | ||
301 | static bool | |
302 | union_static_var_sets (bitmap &x, bitmap y) | |
303 | { | |
304 | if (x != all_module_statics) | |
305 | { | |
306 | if (y == all_module_statics) | |
307 | { | |
308 | BITMAP_FREE (x); | |
309 | x = all_module_statics; | |
310 | } | |
311 | else if (bitmap_ior_into (x, y)) | |
312 | { | |
313 | /* The union may have reduced X to the maximum set. | |
314 | In that case, we want to make that visible explicitly. | |
315 | Even though bitmap_equal_p can be very expensive, it | |
316 | turns out to be an overall win to check this here for | |
317 | an LTO bootstrap of GCC itself. Liberally extrapoliate | |
318 | that result to be applicable to all cases. */ | |
319 | if (bitmap_equal_p (x, all_module_statics)) | |
320 | { | |
321 | BITMAP_FREE (x); | |
322 | x = all_module_statics; | |
323 | } | |
324 | } | |
325 | } | |
326 | return x == all_module_statics; | |
327 | } | |
328 | ||
df92c640 SB |
329 | /* Return a copy of SET on the bitmap obstack containing SET. |
330 | But if SET is NULL or the maximum set, return that instead. */ | |
331 | ||
332 | static bitmap | |
333 | copy_static_var_set (bitmap set) | |
334 | { | |
335 | if (set == NULL || set == all_module_statics) | |
336 | return set; | |
337 | bitmap_obstack *o = set->obstack; | |
338 | gcc_checking_assert (o); | |
339 | bitmap copy = BITMAP_ALLOC (o); | |
340 | bitmap_copy (copy, set); | |
341 | return copy; | |
342 | } | |
343 | ||
344 | /* Compute the union all of the statics read and written by every callee of X | |
345 | into X_GLOBAL->statics_read and X_GLOBAL->statics_written. X_GLOBAL is | |
346 | actually the set representing the cycle containing X. If the read and | |
347 | written sets of X_GLOBAL has been reduced to the maximum set, we don't | |
348 | have to look at the remaining callees. */ | |
ea900239 DB |
349 | |
350 | static void | |
e2c9111c | 351 | propagate_bits (ipa_reference_global_vars_info_t x_global, struct cgraph_node *x) |
ea900239 | 352 | { |
ea900239 | 353 | struct cgraph_edge *e; |
df92c640 SB |
354 | bool read_all = x_global->statics_read == all_module_statics; |
355 | bool write_all = x_global->statics_written == all_module_statics; | |
356 | for (e = x->callees; | |
357 | e && !(read_all && write_all); | |
358 | e = e->next_callee) | |
ea900239 | 359 | { |
46a4da10 | 360 | enum availability avail; |
d52f5295 | 361 | struct cgraph_node *y = e->callee->function_symbol (&avail); |
fede8efa JH |
362 | if (!y) |
363 | continue; | |
df92c640 | 364 | |
c59f5d1b | 365 | /* Only look into nodes we can propagate something. */ |
67348ccc | 366 | int flags = flags_from_decl_or_type (y->decl); |
b16650ac JH |
367 | if (opt_for_fn (y->decl, flag_ipa_reference) |
368 | && (avail > AVAIL_INTERPOSABLE | |
369 | || (avail == AVAIL_INTERPOSABLE && (flags & ECF_LEAF)))) | |
ea900239 | 370 | { |
e2c9111c | 371 | if (get_reference_vars_info (y)) |
ea900239 | 372 | { |
df92c640 | 373 | ipa_reference_vars_info_t y_info = get_reference_vars_info (y); |
46c30019 | 374 | ipa_reference_global_vars_info_t y_global = &y_info->global; |
e2c9111c | 375 | |
df92c640 SB |
376 | /* Calls in the current cycle do not have their global set |
377 | computed yet (but everything else does because we're | |
378 | visiting nodes in topological order). */ | |
46c30019 | 379 | if (!y_global->statics_read) |
e2c9111c | 380 | continue; |
b8698a0f | 381 | |
df92c640 | 382 | /* If the function is const, it reads no memory even if it |
22a8d1e6 JH |
383 | seems so to local analysis. */ |
384 | if (flags & ECF_CONST) | |
385 | continue; | |
386 | ||
df92c640 | 387 | union_static_var_sets (x_global->statics_read, |
ea900239 | 388 | y_global->statics_read); |
b8698a0f | 389 | |
df92c640 SB |
390 | /* If the function is pure, it has no stores even if it |
391 | seems so to local analysis. If we cannot return from | |
392 | the function, we can safely ignore the call. */ | |
22a8d1e6 | 393 | if ((flags & ECF_PURE) |
3dafb85c | 394 | || e->cannot_lead_to_return_p ()) |
22a8d1e6 JH |
395 | continue; |
396 | ||
df92c640 | 397 | union_static_var_sets (x_global->statics_written, |
ea900239 | 398 | y_global->statics_written); |
ea900239 | 399 | } |
b8698a0f | 400 | else |
ebcf9dc8 | 401 | gcc_unreachable (); |
ea900239 DB |
402 | } |
403 | } | |
404 | } | |
405 | ||
3edf64aa DM |
406 | static bool ipa_init_p = false; |
407 | ||
ea900239 DB |
408 | /* The init routine for analyzing global static variable usage. See |
409 | comments at top for description. */ | |
b8698a0f L |
410 | static void |
411 | ipa_init (void) | |
ea900239 | 412 | { |
3edf64aa | 413 | if (ipa_init_p) |
d7f09764 DN |
414 | return; |
415 | ||
3edf64aa | 416 | ipa_init_p = true; |
d7f09764 | 417 | |
46c30019 JH |
418 | if (dump_file) |
419 | reference_vars_to_consider = splay_tree_new (splay_tree_compare_ints, 0, 0); | |
ea900239 | 420 | |
ebcf9dc8 | 421 | bitmap_obstack_initialize (&local_info_obstack); |
46c30019 | 422 | bitmap_obstack_initialize (&optimization_summary_obstack); |
22a8d1e6 | 423 | all_module_statics = BITMAP_ALLOC (&optimization_summary_obstack); |
b16650ac | 424 | ignore_module_statics = BITMAP_ALLOC (&optimization_summary_obstack); |
7dbca013 | 425 | |
6adcb793 ML |
426 | if (ipa_ref_var_info_summaries == NULL) |
427 | ipa_ref_var_info_summaries = new ipa_ref_var_info_summary_t (symtab); | |
428 | ||
429 | if (ipa_ref_opt_sum_summaries != NULL) | |
430 | { | |
431 | delete ipa_ref_opt_sum_summaries; | |
432 | ipa_ref_opt_sum_summaries = NULL; | |
433 | } | |
ea900239 DB |
434 | } |
435 | ||
d7f09764 | 436 | |
812dbce5 | 437 | /* Set up the persistent info for FN. */ |
ea900239 | 438 | |
812dbce5 JH |
439 | static ipa_reference_local_vars_info_t |
440 | init_function_info (struct cgraph_node *fn) | |
ea900239 | 441 | { |
b8698a0f | 442 | ipa_reference_vars_info_t info |
6adcb793 | 443 | = ipa_ref_var_info_summaries->get_create (fn); |
ea900239 | 444 | |
46c30019 JH |
445 | info->local.statics_read = BITMAP_ALLOC (&local_info_obstack); |
446 | info->local.statics_written = BITMAP_ALLOC (&local_info_obstack); | |
ea900239 | 447 | |
46c30019 | 448 | return &info->local; |
812dbce5 JH |
449 | } |
450 | ||
d7f09764 | 451 | |
812dbce5 JH |
452 | /* This is the main routine for finding the reference patterns for |
453 | global variables within a function FN. */ | |
d7f09764 | 454 | |
812dbce5 JH |
455 | static void |
456 | analyze_function (struct cgraph_node *fn) | |
457 | { | |
8b583a06 | 458 | ipa_reference_local_vars_info_t local; |
d122681a | 459 | struct ipa_ref *ref = NULL; |
5f902d76 JH |
460 | int i; |
461 | tree var; | |
812dbce5 | 462 | |
b16650ac JH |
463 | if (!opt_for_fn (fn->decl, flag_ipa_reference)) |
464 | return; | |
5f902d76 | 465 | local = init_function_info (fn); |
d122681a | 466 | for (i = 0; fn->iterate_reference (i, ref); i++) |
812dbce5 | 467 | { |
7de90a6c | 468 | if (!is_a <varpool_node *> (ref->referred)) |
5f902d76 | 469 | continue; |
d122681a | 470 | var = ref->referred->decl; |
5264f487 | 471 | if (!is_proper_for_analysis (var)) |
5f902d76 | 472 | continue; |
2fd2ae34 RB |
473 | /* This is a variable we care about. Check if we have seen it |
474 | before, and if not add it the set of variables we care about. */ | |
475 | if (all_module_statics | |
37074a02 | 476 | && bitmap_set_bit (all_module_statics, ipa_reference_var_uid (var))) |
2fd2ae34 RB |
477 | { |
478 | if (dump_file) | |
479 | splay_tree_insert (reference_vars_to_consider, | |
37074a02 JH |
480 | ipa_reference_var_uid (var), |
481 | (splay_tree_value)var); | |
2fd2ae34 | 482 | } |
5f902d76 | 483 | switch (ref->use) |
812dbce5 | 484 | { |
5f902d76 | 485 | case IPA_REF_LOAD: |
37074a02 | 486 | bitmap_set_bit (local->statics_read, ipa_reference_var_uid (var)); |
5f902d76 JH |
487 | break; |
488 | case IPA_REF_STORE: | |
d122681a | 489 | if (ref->cannot_lead_to_return ()) |
f10ea640 | 490 | break; |
37074a02 | 491 | bitmap_set_bit (local->statics_written, ipa_reference_var_uid (var)); |
5f902d76 JH |
492 | break; |
493 | case IPA_REF_ADDR: | |
5f902d76 | 494 | break; |
7d2268ea MJ |
495 | default: |
496 | gcc_unreachable (); | |
812dbce5 | 497 | } |
812dbce5 | 498 | } |
ea900239 | 499 | |
d52f5295 | 500 | if (fn->cannot_return_p ()) |
22a8d1e6 | 501 | bitmap_clear (local->statics_written); |
ea900239 DB |
502 | } |
503 | ||
5f902d76 | 504 | |
e2c9111c JH |
505 | /* Called when new clone is inserted to callgraph late. */ |
506 | ||
6adcb793 ML |
507 | void |
508 | ipa_ref_opt_summary_t::duplicate (cgraph_node *, cgraph_node *, | |
509 | ipa_reference_optimization_summary_d *ginfo, | |
510 | ipa_reference_optimization_summary_d | |
511 | *dst_ginfo) | |
e2c9111c | 512 | { |
df92c640 SB |
513 | dst_ginfo->statics_not_read = |
514 | copy_static_var_set (ginfo->statics_not_read); | |
515 | dst_ginfo->statics_not_written = | |
516 | copy_static_var_set (ginfo->statics_not_written); | |
e2c9111c JH |
517 | } |
518 | ||
519 | /* Called when node is removed. */ | |
520 | ||
6adcb793 ML |
521 | void |
522 | ipa_ref_opt_summary_t::remove (cgraph_node *, | |
523 | ipa_reference_optimization_summary_d *ginfo) | |
e2c9111c | 524 | { |
6adcb793 ML |
525 | if (ginfo->statics_not_read |
526 | && ginfo->statics_not_read != all_module_statics) | |
527 | BITMAP_FREE (ginfo->statics_not_read); | |
528 | ||
529 | if (ginfo->statics_not_written | |
530 | && ginfo->statics_not_written != all_module_statics) | |
531 | BITMAP_FREE (ginfo->statics_not_written); | |
e2c9111c JH |
532 | } |
533 | ||
812dbce5 JH |
534 | /* Analyze each function in the cgraph to see which global or statics |
535 | are read or written. */ | |
536 | ||
b8698a0f | 537 | static void |
812dbce5 | 538 | generate_summary (void) |
ea900239 DB |
539 | { |
540 | struct cgraph_node *node; | |
812dbce5 JH |
541 | unsigned int index; |
542 | bitmap_iterator bi; | |
b8698a0f | 543 | |
ea900239 DB |
544 | ipa_init (); |
545 | ||
5f902d76 | 546 | /* Process all of the functions next. */ |
b16650ac JH |
547 | FOR_EACH_DEFINED_FUNCTION (node) |
548 | if (!node->alias && !opt_for_fn (node->decl, flag_ipa_reference)) | |
549 | { | |
550 | struct ipa_ref *ref = NULL; | |
551 | int i; | |
552 | tree var; | |
553 | for (i = 0; node->iterate_reference (i, ref); i++) | |
554 | { | |
555 | if (!is_a <varpool_node *> (ref->referred)) | |
556 | continue; | |
557 | var = ref->referred->decl; | |
558 | if (!is_proper_for_analysis (var)) | |
559 | continue; | |
37074a02 | 560 | bitmap_set_bit (ignore_module_statics, ipa_reference_var_uid (var)); |
b16650ac JH |
561 | } |
562 | } | |
65c70e6b JH |
563 | FOR_EACH_DEFINED_FUNCTION (node) |
564 | analyze_function (node); | |
ea900239 | 565 | |
812dbce5 | 566 | if (dump_file) |
ea900239 DB |
567 | EXECUTE_IF_SET_IN_BITMAP (all_module_statics, 0, index, bi) |
568 | { | |
df92c640 SB |
569 | fprintf (dump_file, "\nPromotable global:%s (uid=%u)\n", |
570 | get_static_name (index), index); | |
ea900239 | 571 | } |
b8698a0f | 572 | |
ea900239 | 573 | if (dump_file) |
65c70e6b | 574 | FOR_EACH_DEFINED_FUNCTION (node) |
b16650ac JH |
575 | if (node->get_availability () >= AVAIL_INTERPOSABLE |
576 | && opt_for_fn (node->decl, flag_ipa_reference)) | |
ea900239 | 577 | { |
ea900239 | 578 | ipa_reference_local_vars_info_t l; |
812dbce5 | 579 | unsigned int index; |
ea900239 | 580 | bitmap_iterator bi; |
b8698a0f | 581 | |
46c30019 | 582 | l = &get_reference_vars_info (node)->local; |
b8698a0f | 583 | fprintf (dump_file, |
464d0118 | 584 | "\nFunction name:%s:", node->dump_name ()); |
ea900239 | 585 | fprintf (dump_file, "\n locals read: "); |
8b583a06 JH |
586 | if (l->statics_read) |
587 | EXECUTE_IF_SET_IN_BITMAP (l->statics_read, | |
588 | 0, index, bi) | |
589 | { | |
590 | fprintf (dump_file, "%s ", | |
591 | get_static_name (index)); | |
592 | } | |
ea900239 | 593 | fprintf (dump_file, "\n locals written: "); |
8b583a06 JH |
594 | if (l->statics_written) |
595 | EXECUTE_IF_SET_IN_BITMAP (l->statics_written, | |
596 | 0, index, bi) | |
597 | { | |
c3284718 | 598 | fprintf (dump_file, "%s ", get_static_name (index)); |
8b583a06 | 599 | } |
ea900239 | 600 | } |
812dbce5 JH |
601 | } |
602 | \f | |
22a8d1e6 | 603 | /* Set READ_ALL/WRITE_ALL based on decl flags of NODE. */ |
46c30019 | 604 | |
c59f5d1b | 605 | static void |
df92c640 SB |
606 | read_write_all_from_decl (struct cgraph_node *node, |
607 | bool &read_all, bool &write_all) | |
c59f5d1b | 608 | { |
67348ccc | 609 | tree decl = node->decl; |
c59f5d1b | 610 | int flags = flags_from_decl_or_type (decl); |
46a4da10 | 611 | if ((flags & ECF_LEAF) |
b16650ac | 612 | && node->get_availability () < AVAIL_INTERPOSABLE) |
46a4da10 JH |
613 | ; |
614 | else if (flags & ECF_CONST) | |
c59f5d1b | 615 | ; |
d52f5295 | 616 | else if ((flags & ECF_PURE) || node->cannot_return_p ()) |
46a4da10 | 617 | { |
df92c640 | 618 | read_all = true; |
46a4da10 | 619 | if (dump_file && (dump_flags & TDF_DETAILS)) |
464d0118 | 620 | fprintf (dump_file, " %s -> read all\n", node->dump_name ()); |
46a4da10 | 621 | } |
c59f5d1b JH |
622 | else |
623 | { | |
624 | /* TODO: To be able to produce sane results, we should also handle | |
46c30019 | 625 | common builtins, in particular throw. */ |
df92c640 SB |
626 | read_all = true; |
627 | write_all = true; | |
46a4da10 | 628 | if (dump_file && (dump_flags & TDF_DETAILS)) |
464d0118 ML |
629 | fprintf (dump_file, " %s -> read all, write all\n", |
630 | node->dump_name ()); | |
c59f5d1b JH |
631 | } |
632 | } | |
633 | ||
df92c640 SB |
634 | /* Set READ_ALL/WRITE_ALL based on decl flags of NODE or any member |
635 | in the cycle of NODE. */ | |
636 | ||
637 | static void | |
638 | get_read_write_all_from_node (struct cgraph_node *node, | |
639 | bool &read_all, bool &write_all) | |
640 | { | |
641 | struct cgraph_edge *e, *ie; | |
642 | ||
67914693 | 643 | /* When function is overwritable, we cannot assume anything. */ |
b16650ac JH |
644 | if (node->get_availability () <= AVAIL_INTERPOSABLE |
645 | || (node->analyzed && !opt_for_fn (node->decl, flag_ipa_reference))) | |
df92c640 SB |
646 | read_write_all_from_decl (node, read_all, write_all); |
647 | ||
648 | for (e = node->callees; | |
649 | e && !(read_all && write_all); | |
650 | e = e->next_callee) | |
651 | { | |
652 | enum availability avail; | |
d52f5295 | 653 | struct cgraph_node *callee = e->callee->function_symbol (&avail); |
df92c640 | 654 | gcc_checking_assert (callee); |
b16650ac | 655 | if (avail <= AVAIL_INTERPOSABLE |
6adcb793 ML |
656 | || (callee->analyzed && !opt_for_fn (callee->decl, |
657 | flag_ipa_reference))) | |
df92c640 SB |
658 | read_write_all_from_decl (callee, read_all, write_all); |
659 | } | |
660 | ||
661 | for (ie = node->indirect_calls; | |
662 | ie && !(read_all && write_all); | |
663 | ie = ie->next_callee) | |
664 | if (!(ie->indirect_info->ecf_flags & ECF_CONST)) | |
665 | { | |
666 | read_all = true; | |
667 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
668 | fprintf (dump_file, " indirect call -> read all\n"); | |
3dafb85c | 669 | if (!ie->cannot_lead_to_return_p () |
df92c640 SB |
670 | && !(ie->indirect_info->ecf_flags & ECF_PURE)) |
671 | { | |
672 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
673 | fprintf (dump_file, " indirect call -> write all\n"); | |
674 | write_all = true; | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
b16650ac JH |
679 | /* Skip edges from and to nodes without ipa_reference enables. This leave |
680 | them out of strongy connected coponents and makes them easyto skip in the | |
681 | propagation loop bellow. */ | |
682 | ||
683 | static bool | |
684 | ignore_edge_p (cgraph_edge *e) | |
685 | { | |
686 | return (!opt_for_fn (e->caller->decl, flag_ipa_reference) | |
687 | || !opt_for_fn (e->callee->function_symbol ()->decl, | |
688 | flag_ipa_reference)); | |
689 | } | |
690 | ||
812dbce5 | 691 | /* Produce the global information by preforming a transitive closure |
df92c640 | 692 | on the local information that was produced by ipa_analyze_function. */ |
812dbce5 JH |
693 | |
694 | static unsigned int | |
695 | propagate (void) | |
696 | { | |
697 | struct cgraph_node *node; | |
812dbce5 | 698 | struct cgraph_node **order = |
3dafb85c | 699 | XCNEWVEC (struct cgraph_node *, symtab->cgraph_count); |
af8bca3c | 700 | int order_pos; |
812dbce5 | 701 | int i; |
dea91a66 | 702 | bool remove_p; |
812dbce5 | 703 | |
b8698a0f | 704 | if (dump_file) |
d52f5295 | 705 | cgraph_node::dump_cgraph (dump_file); |
ea900239 | 706 | |
2e14744f | 707 | remove_p = ipa_discover_variable_flags (); |
5f902d76 JH |
708 | generate_summary (); |
709 | ||
073a8998 | 710 | /* Propagate the local information through the call graph to produce |
ea900239 DB |
711 | the global information. All the nodes within a cycle will have |
712 | the same info so we collapse cycles first. Then we can do the | |
713 | propagation in one pass from the leaves to the roots. */ | |
45272fd2 | 714 | order_pos = ipa_reduced_postorder (order, true, ignore_edge_p); |
ea900239 | 715 | if (dump_file) |
af8bca3c | 716 | ipa_print_order (dump_file, "reduced", order, order_pos); |
ea900239 DB |
717 | |
718 | for (i = 0; i < order_pos; i++ ) | |
719 | { | |
df92c640 SB |
720 | unsigned x; |
721 | struct cgraph_node *w; | |
ea900239 | 722 | ipa_reference_vars_info_t node_info; |
46c30019 | 723 | ipa_reference_global_vars_info_t node_g; |
ea900239 | 724 | ipa_reference_local_vars_info_t node_l; |
df92c640 SB |
725 | bool read_all = false; |
726 | bool write_all = false; | |
ea900239 DB |
727 | |
728 | node = order[i]; | |
b16650ac | 729 | if (node->alias || !opt_for_fn (node->decl, flag_ipa_reference)) |
71fb4f92 | 730 | continue; |
df92c640 | 731 | |
e2c9111c | 732 | node_info = get_reference_vars_info (node); |
22a8d1e6 | 733 | gcc_assert (node_info); |
df92c640 SB |
734 | node_l = &node_info->local; |
735 | node_g = &node_info->global; | |
46a4da10 JH |
736 | |
737 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
464d0118 | 738 | fprintf (dump_file, "Starting cycle with %s\n", node->dump_name ()); |
46a4da10 | 739 | |
d52f5295 | 740 | vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node); |
c59f5d1b | 741 | |
df92c640 | 742 | /* If any node in a cycle is read_all or write_all, they all are. */ |
9771b263 | 743 | FOR_EACH_VEC_ELT (cycle_nodes, x, w) |
ea900239 | 744 | { |
46a4da10 | 745 | if (dump_file && (dump_flags & TDF_DETAILS)) |
464d0118 | 746 | fprintf (dump_file, " Visiting %s\n", w->dump_asm_name ()); |
df92c640 SB |
747 | get_read_write_all_from_node (w, read_all, write_all); |
748 | if (read_all && write_all) | |
749 | break; | |
ea900239 DB |
750 | } |
751 | ||
df92c640 | 752 | /* Initialized the bitmaps global sets for the reduced node. */ |
b8698a0f | 753 | if (read_all) |
ea900239 | 754 | node_g->statics_read = all_module_statics; |
b8698a0f | 755 | else |
df92c640 | 756 | node_g->statics_read = copy_static_var_set (node_l->statics_read); |
b8698a0f | 757 | if (write_all) |
ea900239 DB |
758 | node_g->statics_written = all_module_statics; |
759 | else | |
df92c640 | 760 | node_g->statics_written = copy_static_var_set (node_l->statics_written); |
ea900239 | 761 | |
df92c640 SB |
762 | /* Merge the sets of this cycle with all sets of callees reached |
763 | from this cycle. */ | |
9771b263 | 764 | FOR_EACH_VEC_ELT (cycle_nodes, x, w) |
ea900239 | 765 | { |
df92c640 SB |
766 | if (read_all && write_all) |
767 | break; | |
768 | ||
769 | if (w != node) | |
770 | { | |
771 | ipa_reference_vars_info_t w_ri = get_reference_vars_info (w); | |
772 | ipa_reference_local_vars_info_t w_l = &w_ri->local; | |
67348ccc | 773 | int flags = flags_from_decl_or_type (w->decl); |
df92c640 SB |
774 | |
775 | if (!(flags & ECF_CONST)) | |
776 | read_all = union_static_var_sets (node_g->statics_read, | |
777 | w_l->statics_read); | |
778 | if (!(flags & ECF_PURE) | |
d52f5295 | 779 | && !w->cannot_return_p ()) |
df92c640 SB |
780 | write_all = union_static_var_sets (node_g->statics_written, |
781 | w_l->statics_written); | |
782 | } | |
783 | ||
e2c9111c | 784 | propagate_bits (node_g, w); |
ea900239 DB |
785 | } |
786 | ||
e2c9111c | 787 | /* All nodes within a cycle have the same global info bitmaps. */ |
9771b263 | 788 | FOR_EACH_VEC_ELT (cycle_nodes, x, w) |
ea900239 | 789 | { |
df92c640 | 790 | ipa_reference_vars_info_t w_ri = get_reference_vars_info (w); |
46c30019 | 791 | w_ri->global = *node_g; |
ea900239 | 792 | } |
df92c640 | 793 | |
9771b263 | 794 | cycle_nodes.release (); |
ea900239 DB |
795 | } |
796 | ||
797 | if (dump_file) | |
798 | { | |
df92c640 | 799 | for (i = 0; i < order_pos; i++) |
ea900239 | 800 | { |
df92c640 SB |
801 | unsigned x; |
802 | struct cgraph_node *w; | |
ea900239 DB |
803 | |
804 | node = order[i]; | |
b16650ac | 805 | if (node->alias || !opt_for_fn (node->decl, flag_ipa_reference)) |
71fb4f92 | 806 | continue; |
df92c640 | 807 | |
464d0118 | 808 | fprintf (dump_file, "\nFunction name:%s:", node->dump_asm_name ()); |
ea900239 | 809 | |
df92c640 SB |
810 | ipa_reference_vars_info_t node_info = get_reference_vars_info (node); |
811 | ipa_reference_global_vars_info_t node_g = &node_info->global; | |
812 | ||
d52f5295 | 813 | vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node); |
9771b263 | 814 | FOR_EACH_VEC_ELT (cycle_nodes, x, w) |
ea900239 | 815 | { |
df92c640 | 816 | ipa_reference_vars_info_t w_ri = get_reference_vars_info (w); |
46c30019 | 817 | ipa_reference_local_vars_info_t w_l = &w_ri->local; |
df92c640 | 818 | if (w != node) |
464d0118 | 819 | fprintf (dump_file, "\n next cycle: %s ", w->dump_asm_name ()); |
40513dd3 | 820 | fprintf (dump_file, "\n locals read: "); |
df92c640 | 821 | dump_static_vars_set_to_file (dump_file, w_l->statics_read); |
ea900239 | 822 | fprintf (dump_file, "\n locals written: "); |
df92c640 | 823 | dump_static_vars_set_to_file (dump_file, w_l->statics_written); |
ea900239 | 824 | } |
9771b263 | 825 | cycle_nodes.release (); |
df92c640 | 826 | |
ea900239 | 827 | fprintf (dump_file, "\n globals read: "); |
df92c640 | 828 | dump_static_vars_set_to_file (dump_file, node_g->statics_read); |
ea900239 | 829 | fprintf (dump_file, "\n globals written: "); |
df92c640 SB |
830 | dump_static_vars_set_to_file (dump_file, node_g->statics_written); |
831 | fprintf (dump_file, "\n"); | |
ea900239 DB |
832 | } |
833 | } | |
834 | ||
6adcb793 ML |
835 | if (ipa_ref_opt_sum_summaries == NULL) |
836 | ipa_ref_opt_sum_summaries = new ipa_ref_opt_summary_t (symtab); | |
837 | ||
ea900239 | 838 | /* Cleanup. */ |
65c70e6b | 839 | FOR_EACH_DEFINED_FUNCTION (node) |
ea900239 DB |
840 | { |
841 | ipa_reference_vars_info_t node_info; | |
842 | ipa_reference_global_vars_info_t node_g; | |
46c30019 | 843 | |
e2c9111c | 844 | node_info = get_reference_vars_info (node); |
b16650ac | 845 | if (!node->alias && opt_for_fn (node->decl, flag_ipa_reference) |
d52f5295 | 846 | && (node->get_availability () > AVAIL_INTERPOSABLE |
67348ccc | 847 | || (flags_from_decl_or_type (node->decl) & ECF_LEAF))) |
46c30019 JH |
848 | { |
849 | node_g = &node_info->global; | |
ea900239 | 850 | |
6adcb793 ML |
851 | ipa_reference_optimization_summary_d *opt |
852 | = ipa_ref_opt_sum_summaries->get_create (node); | |
ea900239 | 853 | |
46c30019 | 854 | /* Create the complimentary sets. */ |
22a8d1e6 JH |
855 | |
856 | if (bitmap_empty_p (node_g->statics_read)) | |
857 | opt->statics_not_read = all_module_statics; | |
858 | else | |
859 | { | |
860 | opt->statics_not_read | |
861 | = BITMAP_ALLOC (&optimization_summary_obstack); | |
862 | if (node_g->statics_read != all_module_statics) | |
863 | bitmap_and_compl (opt->statics_not_read, | |
864 | all_module_statics, | |
865 | node_g->statics_read); | |
866 | } | |
867 | ||
868 | if (bitmap_empty_p (node_g->statics_written)) | |
869 | opt->statics_not_written = all_module_statics; | |
870 | else | |
871 | { | |
872 | opt->statics_not_written | |
873 | = BITMAP_ALLOC (&optimization_summary_obstack); | |
874 | if (node_g->statics_written != all_module_statics) | |
875 | bitmap_and_compl (opt->statics_not_written, | |
876 | all_module_statics, | |
877 | node_g->statics_written); | |
878 | } | |
46c30019 | 879 | } |
46c30019 JH |
880 | } |
881 | ||
af8bca3c | 882 | ipa_free_postorder_info (); |
46c30019 | 883 | free (order); |
b8698a0f | 884 | |
ebcf9dc8 | 885 | bitmap_obstack_release (&local_info_obstack); |
6adcb793 ML |
886 | |
887 | if (ipa_ref_var_info_summaries == NULL) | |
888 | { | |
889 | delete ipa_ref_var_info_summaries; | |
890 | ipa_ref_var_info_summaries = NULL; | |
891 | } | |
892 | ||
893 | ipa_ref_var_info_summaries = NULL; | |
46c30019 JH |
894 | if (dump_file) |
895 | splay_tree_delete (reference_vars_to_consider); | |
896 | reference_vars_to_consider = NULL; | |
dea91a66 | 897 | return remove_p ? TODO_remove_functions : 0; |
ea900239 DB |
898 | } |
899 | ||
f3380641 JH |
900 | /* Return true if we need to write summary of NODE. */ |
901 | ||
902 | static bool | |
903 | write_node_summary_p (struct cgraph_node *node, | |
f27c1867 | 904 | lto_symtab_encoder_t encoder, |
f3380641 JH |
905 | bitmap ltrans_statics) |
906 | { | |
907 | ipa_reference_optimization_summary_t info; | |
908 | ||
909 | /* See if we have (non-empty) info. */ | |
67348ccc | 910 | if (!node->definition || node->global.inlined_to) |
f3380641 JH |
911 | return false; |
912 | info = get_reference_optimization_summary (node); | |
6adcb793 ML |
913 | if (!info |
914 | || (bitmap_empty_p (info->statics_not_read) | |
915 | && bitmap_empty_p (info->statics_not_written))) | |
f3380641 JH |
916 | return false; |
917 | ||
918 | /* See if we want to encode it. | |
919 | Encode also referenced functions since constant folding might turn it into | |
920 | a direct call. | |
921 | ||
922 | In future we might also want to include summaries of functions references | |
923 | by initializers of constant variables references in current unit. */ | |
f27c1867 | 924 | if (!reachable_from_this_partition_p (node, encoder) |
d122681a | 925 | && !referenced_from_this_partition_p (node, encoder)) |
f3380641 JH |
926 | return false; |
927 | ||
928 | /* See if the info has non-empty intersections with vars we want to encode. */ | |
929 | if (!bitmap_intersect_p (info->statics_not_read, ltrans_statics) | |
930 | && !bitmap_intersect_p (info->statics_not_written, ltrans_statics)) | |
931 | return false; | |
932 | return true; | |
933 | } | |
934 | ||
22a8d1e6 JH |
935 | /* Stream out BITS<RANS_STATICS as list of decls to OB. |
936 | LTRANS_STATICS_BITCOUNT specify number of bits in LTRANS_STATICS | |
937 | or -1. When it is positive, just output -1 when | |
938 | BITS<RANS_STATICS == BITS<RANS_STATICS. */ | |
f3380641 JH |
939 | |
940 | static void | |
941 | stream_out_bitmap (struct lto_simple_output_block *ob, | |
22a8d1e6 JH |
942 | bitmap bits, bitmap ltrans_statics, |
943 | int ltrans_statics_bitcount) | |
f3380641 | 944 | { |
22a8d1e6 | 945 | int count = 0; |
f3380641 JH |
946 | unsigned int index; |
947 | bitmap_iterator bi; | |
22a8d1e6 JH |
948 | if (bits == all_module_statics) |
949 | { | |
412288f1 | 950 | streamer_write_hwi_stream (ob->main_stream, -1); |
22a8d1e6 JH |
951 | return; |
952 | } | |
f3380641 JH |
953 | EXECUTE_IF_AND_IN_BITMAP (bits, ltrans_statics, 0, index, bi) |
954 | count ++; | |
22a8d1e6 JH |
955 | if (count == ltrans_statics_bitcount) |
956 | { | |
412288f1 | 957 | streamer_write_hwi_stream (ob->main_stream, -1); |
22a8d1e6 JH |
958 | return; |
959 | } | |
412288f1 | 960 | streamer_write_hwi_stream (ob->main_stream, count); |
f3380641 JH |
961 | if (!count) |
962 | return; | |
963 | EXECUTE_IF_AND_IN_BITMAP (bits, ltrans_statics, 0, index, bi) | |
964 | { | |
6adcb793 ML |
965 | tree decl = (tree)splay_tree_lookup (reference_vars_to_consider, |
966 | index)->value; | |
c3284718 | 967 | lto_output_var_decl_index (ob->decl_state, ob->main_stream, decl); |
f3380641 JH |
968 | } |
969 | } | |
970 | ||
971 | /* Serialize the ipa info for lto. */ | |
972 | ||
973 | static void | |
f27c1867 | 974 | ipa_reference_write_optimization_summary (void) |
f3380641 | 975 | { |
f3380641 JH |
976 | struct lto_simple_output_block *ob |
977 | = lto_create_simple_output_block (LTO_section_ipa_reference); | |
978 | unsigned int count = 0; | |
22a8d1e6 | 979 | int ltrans_statics_bitcount = 0; |
7380e6ef | 980 | lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder; |
0e3de1d4 | 981 | auto_bitmap ltrans_statics; |
22a8d1e6 | 982 | int i; |
f3380641 JH |
983 | |
984 | reference_vars_to_consider = splay_tree_new (splay_tree_compare_ints, 0, 0); | |
985 | ||
986 | /* See what variables we are interested in. */ | |
7380e6ef | 987 | for (i = 0; i < lto_symtab_encoder_size (encoder); i++) |
22a8d1e6 | 988 | { |
5e20cdc9 | 989 | symtab_node *snode = lto_symtab_encoder_deref (encoder, i); |
7de90a6c | 990 | varpool_node *vnode = dyn_cast <varpool_node *> (snode); |
5d59b5e1 | 991 | if (vnode |
37074a02 JH |
992 | && bitmap_bit_p (all_module_statics, |
993 | ipa_reference_var_uid (vnode->decl)) | |
d122681a | 994 | && referenced_from_this_partition_p (vnode, encoder)) |
22a8d1e6 | 995 | { |
67348ccc | 996 | tree decl = vnode->decl; |
37074a02 | 997 | bitmap_set_bit (ltrans_statics, ipa_reference_var_uid (decl)); |
22a8d1e6 | 998 | splay_tree_insert (reference_vars_to_consider, |
37074a02 JH |
999 | ipa_reference_var_uid (decl), |
1000 | (splay_tree_value)decl); | |
22a8d1e6 JH |
1001 | ltrans_statics_bitcount ++; |
1002 | } | |
1003 | } | |
f3380641 | 1004 | |
22a8d1e6 JH |
1005 | |
1006 | if (ltrans_statics_bitcount) | |
7380e6ef | 1007 | for (i = 0; i < lto_symtab_encoder_size (encoder); i++) |
5d59b5e1 | 1008 | { |
5e20cdc9 | 1009 | symtab_node *snode = lto_symtab_encoder_deref (encoder, i); |
7de90a6c | 1010 | cgraph_node *cnode = dyn_cast <cgraph_node *> (snode); |
5d59b5e1 | 1011 | if (cnode && write_node_summary_p (cnode, encoder, ltrans_statics)) |
22a8d1e6 | 1012 | count++; |
5d59b5e1 | 1013 | } |
f3380641 | 1014 | |
412288f1 | 1015 | streamer_write_uhwi_stream (ob->main_stream, count); |
22a8d1e6 JH |
1016 | if (count) |
1017 | stream_out_bitmap (ob, ltrans_statics, ltrans_statics, | |
1018 | -1); | |
f3380641 JH |
1019 | |
1020 | /* Process all of the functions. */ | |
22a8d1e6 | 1021 | if (ltrans_statics_bitcount) |
7380e6ef | 1022 | for (i = 0; i < lto_symtab_encoder_size (encoder); i++) |
f3380641 | 1023 | { |
5e20cdc9 | 1024 | symtab_node *snode = lto_symtab_encoder_deref (encoder, i); |
7de90a6c | 1025 | cgraph_node *cnode = dyn_cast <cgraph_node *> (snode); |
5d59b5e1 | 1026 | if (cnode && write_node_summary_p (cnode, encoder, ltrans_statics)) |
22a8d1e6 JH |
1027 | { |
1028 | ipa_reference_optimization_summary_t info; | |
1029 | int node_ref; | |
1030 | ||
5d59b5e1 LC |
1031 | info = get_reference_optimization_summary (cnode); |
1032 | node_ref = lto_symtab_encoder_encode (encoder, snode); | |
412288f1 | 1033 | streamer_write_uhwi_stream (ob->main_stream, node_ref); |
22a8d1e6 JH |
1034 | |
1035 | stream_out_bitmap (ob, info->statics_not_read, ltrans_statics, | |
1036 | ltrans_statics_bitcount); | |
1037 | stream_out_bitmap (ob, info->statics_not_written, ltrans_statics, | |
1038 | ltrans_statics_bitcount); | |
1039 | } | |
f3380641 | 1040 | } |
f3380641 JH |
1041 | lto_destroy_simple_output_block (ob); |
1042 | splay_tree_delete (reference_vars_to_consider); | |
1043 | } | |
1044 | ||
1045 | /* Deserialize the ipa info for lto. */ | |
1046 | ||
1047 | static void | |
1048 | ipa_reference_read_optimization_summary (void) | |
1049 | { | |
1050 | struct lto_file_decl_data ** file_data_vec | |
1051 | = lto_get_file_decl_data (); | |
1052 | struct lto_file_decl_data * file_data; | |
1053 | unsigned int j = 0; | |
1054 | bitmap_obstack_initialize (&optimization_summary_obstack); | |
1055 | ||
6adcb793 ML |
1056 | if (ipa_ref_opt_sum_summaries == NULL) |
1057 | ipa_ref_opt_sum_summaries = new ipa_ref_opt_summary_t (symtab); | |
1058 | ||
22a8d1e6 | 1059 | all_module_statics = BITMAP_ALLOC (&optimization_summary_obstack); |
f3380641 JH |
1060 | |
1061 | while ((file_data = file_data_vec[j++])) | |
1062 | { | |
1063 | const char *data; | |
1064 | size_t len; | |
1065 | struct lto_input_block *ib | |
1066 | = lto_create_simple_input_block (file_data, | |
1067 | LTO_section_ipa_reference, | |
1068 | &data, &len); | |
1069 | if (ib) | |
1070 | { | |
1071 | unsigned int i; | |
412288f1 | 1072 | unsigned int f_count = streamer_read_uhwi (ib); |
22a8d1e6 JH |
1073 | int b_count; |
1074 | if (!f_count) | |
1075 | continue; | |
412288f1 | 1076 | b_count = streamer_read_hwi (ib); |
22a8d1e6 JH |
1077 | if (dump_file) |
1078 | fprintf (dump_file, "all module statics:"); | |
1079 | for (i = 0; i < (unsigned int)b_count; i++) | |
1080 | { | |
412288f1 | 1081 | unsigned int var_index = streamer_read_uhwi (ib); |
22a8d1e6 JH |
1082 | tree v_decl = lto_file_decl_data_get_var_decl (file_data, |
1083 | var_index); | |
37074a02 JH |
1084 | bitmap_set_bit (all_module_statics, |
1085 | ipa_reference_var_uid (v_decl)); | |
22a8d1e6 | 1086 | if (dump_file) |
df92c640 | 1087 | fprintf (dump_file, " %s", fndecl_name (v_decl)); |
22a8d1e6 | 1088 | } |
f3380641 JH |
1089 | |
1090 | for (i = 0; i < f_count; i++) | |
1091 | { | |
1092 | unsigned int j, index; | |
1093 | struct cgraph_node *node; | |
f3380641 | 1094 | int v_count; |
7380e6ef | 1095 | lto_symtab_encoder_t encoder; |
f3380641 | 1096 | |
412288f1 | 1097 | index = streamer_read_uhwi (ib); |
7380e6ef | 1098 | encoder = file_data->symtab_node_encoder; |
d52f5295 ML |
1099 | node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref |
1100 | (encoder, index)); | |
6adcb793 ML |
1101 | |
1102 | ipa_reference_optimization_summary_d *info | |
1103 | = ipa_ref_opt_sum_summaries->get_create (node); | |
1104 | ||
1105 | info->statics_not_read = BITMAP_ALLOC | |
1106 | (&optimization_summary_obstack); | |
1107 | info->statics_not_written = BITMAP_ALLOC | |
1108 | (&optimization_summary_obstack); | |
f3380641 JH |
1109 | if (dump_file) |
1110 | fprintf (dump_file, | |
464d0118 ML |
1111 | "\nFunction name:%s:\n static not read:", |
1112 | node->dump_asm_name ()); | |
f3380641 JH |
1113 | |
1114 | /* Set the statics not read. */ | |
412288f1 | 1115 | v_count = streamer_read_hwi (ib); |
22a8d1e6 | 1116 | if (v_count == -1) |
f3380641 | 1117 | { |
22a8d1e6 | 1118 | info->statics_not_read = all_module_statics; |
f3380641 | 1119 | if (dump_file) |
22a8d1e6 | 1120 | fprintf (dump_file, " all module statics"); |
f3380641 | 1121 | } |
22a8d1e6 JH |
1122 | else |
1123 | for (j = 0; j < (unsigned int)v_count; j++) | |
1124 | { | |
412288f1 | 1125 | unsigned int var_index = streamer_read_uhwi (ib); |
22a8d1e6 JH |
1126 | tree v_decl = lto_file_decl_data_get_var_decl (file_data, |
1127 | var_index); | |
37074a02 JH |
1128 | bitmap_set_bit (info->statics_not_read, |
1129 | ipa_reference_var_uid (v_decl)); | |
22a8d1e6 | 1130 | if (dump_file) |
df92c640 | 1131 | fprintf (dump_file, " %s", fndecl_name (v_decl)); |
22a8d1e6 | 1132 | } |
f3380641 JH |
1133 | |
1134 | if (dump_file) | |
1135 | fprintf (dump_file, | |
1136 | "\n static not written:"); | |
1137 | /* Set the statics not written. */ | |
412288f1 | 1138 | v_count = streamer_read_hwi (ib); |
22a8d1e6 | 1139 | if (v_count == -1) |
f3380641 | 1140 | { |
22a8d1e6 | 1141 | info->statics_not_written = all_module_statics; |
f3380641 | 1142 | if (dump_file) |
22a8d1e6 | 1143 | fprintf (dump_file, " all module statics"); |
f3380641 | 1144 | } |
22a8d1e6 JH |
1145 | else |
1146 | for (j = 0; j < (unsigned int)v_count; j++) | |
1147 | { | |
412288f1 | 1148 | unsigned int var_index = streamer_read_uhwi (ib); |
22a8d1e6 JH |
1149 | tree v_decl = lto_file_decl_data_get_var_decl (file_data, |
1150 | var_index); | |
37074a02 JH |
1151 | bitmap_set_bit (info->statics_not_written, |
1152 | ipa_reference_var_uid (v_decl)); | |
22a8d1e6 | 1153 | if (dump_file) |
df92c640 | 1154 | fprintf (dump_file, " %s", fndecl_name (v_decl)); |
22a8d1e6 | 1155 | } |
f3380641 JH |
1156 | if (dump_file) |
1157 | fprintf (dump_file, "\n"); | |
1158 | } | |
1159 | ||
1160 | lto_destroy_simple_input_block (file_data, | |
1161 | LTO_section_ipa_reference, | |
1162 | ib, data, len); | |
1163 | } | |
1164 | else | |
6adcb793 ML |
1165 | /* Fatal error here. We do not want to support compiling ltrans units |
1166 | with different version of compiler or different flags than | |
1167 | the WPA unit, so this should never happen. */ | |
40fecdd6 JM |
1168 | fatal_error (input_location, |
1169 | "ipa reference summary is missing in ltrans unit"); | |
f3380641 JH |
1170 | } |
1171 | } | |
ea900239 | 1172 | |
27a4cd48 DM |
1173 | namespace { |
1174 | ||
1175 | const pass_data pass_data_ipa_reference = | |
ea900239 | 1176 | { |
27a4cd48 DM |
1177 | IPA_PASS, /* type */ |
1178 | "static-var", /* name */ | |
1179 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
1180 | TV_IPA_REFERENCE, /* tv_id */ |
1181 | 0, /* properties_required */ | |
1182 | 0, /* properties_provided */ | |
1183 | 0, /* properties_destroyed */ | |
1184 | 0, /* todo_flags_start */ | |
1185 | 0, /* todo_flags_finish */ | |
ea900239 | 1186 | }; |
27a4cd48 DM |
1187 | |
1188 | class pass_ipa_reference : public ipa_opt_pass_d | |
1189 | { | |
1190 | public: | |
c3284718 RS |
1191 | pass_ipa_reference (gcc::context *ctxt) |
1192 | : ipa_opt_pass_d (pass_data_ipa_reference, ctxt, | |
1193 | NULL, /* generate_summary */ | |
1194 | NULL, /* write_summary */ | |
1195 | NULL, /* read_summary */ | |
1196 | ipa_reference_write_optimization_summary, /* | |
1197 | write_optimization_summary */ | |
1198 | ipa_reference_read_optimization_summary, /* | |
1199 | read_optimization_summary */ | |
1200 | NULL, /* stmt_fixup */ | |
1201 | 0, /* function_transform_todo_flags_start */ | |
1202 | NULL, /* function_transform */ | |
1203 | NULL) /* variable_transform */ | |
1204 | {} | |
27a4cd48 DM |
1205 | |
1206 | /* opt_pass methods: */ | |
1a3d085c TS |
1207 | virtual bool gate (function *) |
1208 | { | |
b16650ac | 1209 | return ((in_lto_p || flag_ipa_reference) |
1a3d085c TS |
1210 | /* Don't bother doing anything if the program has errors. */ |
1211 | && !seen_error ()); | |
1212 | } | |
1213 | ||
be55bfe6 | 1214 | virtual unsigned int execute (function *) { return propagate (); } |
27a4cd48 DM |
1215 | |
1216 | }; // class pass_ipa_reference | |
1217 | ||
1218 | } // anon namespace | |
1219 | ||
1220 | ipa_opt_pass_d * | |
1221 | make_pass_ipa_reference (gcc::context *ctxt) | |
1222 | { | |
1223 | return new pass_ipa_reference (ctxt); | |
1224 | } | |
3edf64aa DM |
1225 | |
1226 | /* Reset all state within ipa-reference.c so that we can rerun the compiler | |
1227 | within the same process. For use by toplev::finalize. */ | |
1228 | ||
1229 | void | |
1230 | ipa_reference_c_finalize (void) | |
1231 | { | |
e67343d7 DM |
1232 | if (ipa_ref_opt_sum_summaries != NULL) |
1233 | { | |
1234 | delete ipa_ref_opt_sum_summaries; | |
1235 | ipa_ref_opt_sum_summaries = NULL; | |
1236 | } | |
1237 | ||
4c4d052c DM |
1238 | if (ipa_init_p) |
1239 | { | |
1240 | bitmap_obstack_release (&optimization_summary_obstack); | |
1241 | ipa_init_p = false; | |
1242 | } | |
3edf64aa | 1243 | } |