]>
Commit | Line | Data |
---|---|---|
ea900239 | 1 | /* Callgraph based analysis of static variables. |
5624e564 | 2 | Copyright (C) 2004-2015 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 | 20 | |
fa10beec RW |
21 | /* This file marks functions as being either const (TREE_READONLY) or |
22 | pure (DECL_PURE_P). It can also set a variant of these that | |
23 | are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P). | |
ea900239 DB |
24 | |
25 | This must be run after inlining decisions have been made since | |
26 | otherwise, the local sets will not contain information that is | |
27 | consistent with post inlined state. The global sets are not prone | |
28 | to this problem since they are by definition transitive. */ | |
29 | ||
30 | /* The code in this module is called by the ipa pass manager. It | |
31 | should be one of the later passes since it's information is used by | |
32 | the rest of the compilation. */ | |
33 | ||
34 | #include "config.h" | |
35 | #include "system.h" | |
36 | #include "coretypes.h" | |
c7131fb2 | 37 | #include "backend.h" |
ea900239 | 38 | #include "tree.h" |
c7131fb2 AM |
39 | #include "gimple.h" |
40 | #include "hard-reg-set.h" | |
41 | #include "alias.h" | |
40e23961 | 42 | #include "fold-const.h" |
d8a2d370 DN |
43 | #include "print-tree.h" |
44 | #include "calls.h" | |
60393bbc | 45 | #include "cfganal.h" |
2fb9a547 AM |
46 | #include "internal-fn.h" |
47 | #include "tree-eh.h" | |
5be5c238 AM |
48 | #include "gimple-iterator.h" |
49 | #include "gimple-walk.h" | |
442b4905 | 50 | #include "tree-cfg.h" |
e28030cf | 51 | #include "tree-ssa-loop-niter.h" |
ea900239 DB |
52 | #include "tree-inline.h" |
53 | #include "tree-pass.h" | |
54 | #include "langhooks.h" | |
c582198b | 55 | #include "cgraph.h" |
ea900239 | 56 | #include "ipa-utils.h" |
ea900239 | 57 | #include "flags.h" |
ea900239 | 58 | #include "diagnostic.h" |
cf835838 | 59 | #include "gimple-pretty-print.h" |
ea900239 | 60 | #include "langhooks.h" |
5d35c171 | 61 | #include "target.h" |
f0efc7aa | 62 | #include "tree-streamer.h" |
2de58650 JH |
63 | #include "cfgloop.h" |
64 | #include "tree-scalar-evolution.h" | |
5dc16b19 MLI |
65 | #include "intl.h" |
66 | #include "opts.h" | |
38147a2a | 67 | #include "varasm.h" |
ea900239 DB |
68 | |
69 | /* Lattice values for const and pure functions. Everything starts out | |
70 | being const, then may drop to pure and then neither depending on | |
71 | what is found. */ | |
72 | enum pure_const_state_e | |
73 | { | |
74 | IPA_CONST, | |
75 | IPA_PURE, | |
76 | IPA_NEITHER | |
77 | }; | |
78 | ||
d56026c2 JH |
79 | const char *pure_const_names[3] = {"const", "pure", "neither"}; |
80 | ||
812dbce5 JH |
81 | /* Holder for the const_state. There is one of these per function |
82 | decl. */ | |
b8698a0f | 83 | struct funct_state_d |
ea900239 | 84 | { |
812dbce5 | 85 | /* See above. */ |
ea900239 | 86 | enum pure_const_state_e pure_const_state; |
33977f81 | 87 | /* What user set here; we can be always sure about this. */ |
b8698a0f L |
88 | enum pure_const_state_e state_previously_known; |
89 | bool looping_previously_known; | |
812dbce5 JH |
90 | |
91 | /* True if the function could possibly infinite loop. There are a | |
92 | lot of ways that this could be determined. We are pretty | |
93 | conservative here. While it is possible to cse pure and const | |
94 | calls, it is not legal to have dce get rid of the call if there | |
95 | is a possibility that the call could infinite loop since this is | |
96 | a behavioral change. */ | |
becfd6e5 | 97 | bool looping; |
812dbce5 | 98 | |
33977f81 | 99 | bool can_throw; |
8413ca87 JJ |
100 | |
101 | /* If function can call free, munmap or otherwise make previously | |
102 | non-trapping memory accesses trapping. */ | |
103 | bool can_free; | |
ea900239 DB |
104 | }; |
105 | ||
37512c66 JH |
106 | /* State used when we know nothing about function. */ |
107 | static struct funct_state_d varying_state | |
8413ca87 | 108 | = { IPA_NEITHER, IPA_NEITHER, true, true, true, true }; |
37512c66 JH |
109 | |
110 | ||
ea900239 DB |
111 | typedef struct funct_state_d * funct_state; |
112 | ||
812dbce5 JH |
113 | /* The storage of the funct_state is abstracted because there is the |
114 | possibility that it may be desirable to move this to the cgraph | |
b8698a0f | 115 | local info. */ |
812dbce5 JH |
116 | |
117 | /* Array, indexed by cgraph node uid, of function states. */ | |
118 | ||
9771b263 | 119 | static vec<funct_state> funct_state_vec; |
812dbce5 | 120 | |
3edf64aa DM |
121 | static bool gate_pure_const (void); |
122 | ||
17795822 TS |
123 | namespace { |
124 | ||
125 | const pass_data pass_data_ipa_pure_const = | |
3edf64aa DM |
126 | { |
127 | IPA_PASS, /* type */ | |
128 | "pure-const", /* name */ | |
129 | OPTGROUP_NONE, /* optinfo_flags */ | |
130 | TV_IPA_PURE_CONST, /* tv_id */ | |
131 | 0, /* properties_required */ | |
132 | 0, /* properties_provided */ | |
133 | 0, /* properties_destroyed */ | |
134 | 0, /* todo_flags_start */ | |
135 | 0, /* todo_flags_finish */ | |
136 | }; | |
137 | ||
17795822 | 138 | class pass_ipa_pure_const : public ipa_opt_pass_d |
3edf64aa DM |
139 | { |
140 | public: | |
141 | pass_ipa_pure_const(gcc::context *ctxt); | |
142 | ||
143 | /* opt_pass methods: */ | |
144 | bool gate (function *) { return gate_pure_const (); } | |
145 | unsigned int execute (function *fun); | |
146 | ||
147 | void register_hooks (void); | |
148 | ||
149 | private: | |
150 | bool init_p; | |
151 | ||
152 | /* Holders of ipa cgraph hooks: */ | |
153 | struct cgraph_node_hook_list *function_insertion_hook_holder; | |
154 | struct cgraph_2node_hook_list *node_duplication_hook_holder; | |
155 | struct cgraph_node_hook_list *node_removal_hook_holder; | |
156 | ||
157 | }; // class pass_ipa_pure_const | |
158 | ||
17795822 TS |
159 | } // anon namespace |
160 | ||
5dc16b19 MLI |
161 | /* Try to guess if function body will always be visible to compiler |
162 | when compiling the call and whether compiler will be able | |
163 | to propagate the information by itself. */ | |
164 | ||
165 | static bool | |
166 | function_always_visible_to_compiler_p (tree decl) | |
167 | { | |
168 | return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl)); | |
169 | } | |
170 | ||
171 | /* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE | |
172 | is true if the function is known to be finite. The diagnostic is | |
6e2830c3 | 173 | controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for |
5dc16b19 MLI |
174 | OPTION, this function may initialize it and it is always returned |
175 | by the function. */ | |
176 | ||
6e2830c3 | 177 | static hash_set<tree> * |
5dc16b19 | 178 | suggest_attribute (int option, tree decl, bool known_finite, |
6e2830c3 | 179 | hash_set<tree> *warned_about, |
5dc16b19 MLI |
180 | const char * attrib_name) |
181 | { | |
46625112 | 182 | if (!option_enabled (option, &global_options)) |
5dc16b19 MLI |
183 | return warned_about; |
184 | if (TREE_THIS_VOLATILE (decl) | |
185 | || (known_finite && function_always_visible_to_compiler_p (decl))) | |
186 | return warned_about; | |
187 | ||
188 | if (!warned_about) | |
6e2830c3 TS |
189 | warned_about = new hash_set<tree>; |
190 | if (warned_about->contains (decl)) | |
5dc16b19 | 191 | return warned_about; |
6e2830c3 | 192 | warned_about->add (decl); |
5dc16b19 MLI |
193 | warning_at (DECL_SOURCE_LOCATION (decl), |
194 | option, | |
195 | known_finite | |
196 | ? _("function might be candidate for attribute %<%s%>") | |
197 | : _("function might be candidate for attribute %<%s%>" | |
198 | " if it is known to return normally"), attrib_name); | |
199 | return warned_about; | |
200 | } | |
201 | ||
202 | /* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE | |
203 | is true if the function is known to be finite. */ | |
204 | ||
205 | static void | |
206 | warn_function_pure (tree decl, bool known_finite) | |
207 | { | |
6e2830c3 | 208 | static hash_set<tree> *warned_about; |
5dc16b19 MLI |
209 | |
210 | warned_about | |
211 | = suggest_attribute (OPT_Wsuggest_attribute_pure, decl, | |
212 | known_finite, warned_about, "pure"); | |
213 | } | |
214 | ||
215 | /* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE | |
216 | is true if the function is known to be finite. */ | |
217 | ||
218 | static void | |
219 | warn_function_const (tree decl, bool known_finite) | |
220 | { | |
6e2830c3 | 221 | static hash_set<tree> *warned_about; |
5dc16b19 MLI |
222 | warned_about |
223 | = suggest_attribute (OPT_Wsuggest_attribute_const, decl, | |
224 | known_finite, warned_about, "const"); | |
225 | } | |
7ea6b6cf | 226 | |
c1bf2a39 | 227 | static void |
7ea6b6cf JH |
228 | warn_function_noreturn (tree decl) |
229 | { | |
6e2830c3 | 230 | static hash_set<tree> *warned_about; |
d45eae79 SL |
231 | if (!lang_hooks.missing_noreturn_ok_p (decl) |
232 | && targetm.warn_func_return (decl)) | |
7ea6b6cf JH |
233 | warned_about |
234 | = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl, | |
235 | true, warned_about, "noreturn"); | |
236 | } | |
df92c640 | 237 | |
97d45cef RG |
238 | /* Return true if we have a function state for NODE. */ |
239 | ||
240 | static inline bool | |
241 | has_function_state (struct cgraph_node *node) | |
242 | { | |
9771b263 DN |
243 | if (!funct_state_vec.exists () |
244 | || funct_state_vec.length () <= (unsigned int)node->uid) | |
97d45cef | 245 | return false; |
9771b263 | 246 | return funct_state_vec[node->uid] != NULL; |
97d45cef RG |
247 | } |
248 | ||
b8698a0f | 249 | /* Return the function state from NODE. */ |
ea900239 DB |
250 | |
251 | static inline funct_state | |
252 | get_function_state (struct cgraph_node *node) | |
253 | { | |
9771b263 DN |
254 | if (!funct_state_vec.exists () |
255 | || funct_state_vec.length () <= (unsigned int)node->uid | |
256 | || !funct_state_vec[node->uid]) | |
97d45cef | 257 | /* We might want to put correct previously_known state into varying. */ |
37512c66 | 258 | return &varying_state; |
9771b263 | 259 | return funct_state_vec[node->uid]; |
812dbce5 JH |
260 | } |
261 | ||
262 | /* Set the function state S for NODE. */ | |
263 | ||
264 | static inline void | |
265 | set_function_state (struct cgraph_node *node, funct_state s) | |
266 | { | |
9771b263 DN |
267 | if (!funct_state_vec.exists () |
268 | || funct_state_vec.length () <= (unsigned int)node->uid) | |
269 | funct_state_vec.safe_grow_cleared (node->uid + 1); | |
270 | funct_state_vec[node->uid] = s; | |
ea900239 DB |
271 | } |
272 | ||
fa10beec | 273 | /* Check to see if the use (or definition when CHECKING_WRITE is true) |
ea900239 DB |
274 | variable T is legal in a function that is either pure or const. */ |
275 | ||
b8698a0f L |
276 | static inline void |
277 | check_decl (funct_state local, | |
f10ea640 | 278 | tree t, bool checking_write, bool ipa) |
ea900239 | 279 | { |
ea900239 DB |
280 | /* Do not want to do anything with volatile except mark any |
281 | function that uses one to be not const or pure. */ | |
b8698a0f L |
282 | if (TREE_THIS_VOLATILE (t)) |
283 | { | |
ea900239 | 284 | local->pure_const_state = IPA_NEITHER; |
33977f81 JH |
285 | if (dump_file) |
286 | fprintf (dump_file, " Volatile operand is not const/pure"); | |
ea900239 DB |
287 | return; |
288 | } | |
289 | ||
290 | /* Do not care about a local automatic that is not static. */ | |
291 | if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) | |
292 | return; | |
293 | ||
33977f81 JH |
294 | /* If the variable has the "used" attribute, treat it as if it had a |
295 | been touched by the devil. */ | |
b42186f1 | 296 | if (DECL_PRESERVE_P (t)) |
33977f81 JH |
297 | { |
298 | local->pure_const_state = IPA_NEITHER; | |
299 | if (dump_file) | |
300 | fprintf (dump_file, " Used static/global variable is not const/pure\n"); | |
301 | return; | |
302 | } | |
303 | ||
f10ea640 JH |
304 | /* In IPA mode we are not interested in checking actual loads and stores; |
305 | they will be processed at propagation time using ipa_ref. */ | |
306 | if (ipa) | |
307 | return; | |
308 | ||
ea900239 DB |
309 | /* Since we have dealt with the locals and params cases above, if we |
310 | are CHECKING_WRITE, this cannot be a pure or constant | |
311 | function. */ | |
b8698a0f | 312 | if (checking_write) |
eb80272a ST |
313 | { |
314 | local->pure_const_state = IPA_NEITHER; | |
33977f81 JH |
315 | if (dump_file) |
316 | fprintf (dump_file, " static/global memory write is not const/pure\n"); | |
eb80272a ST |
317 | return; |
318 | } | |
ea900239 DB |
319 | |
320 | if (DECL_EXTERNAL (t) || TREE_PUBLIC (t)) | |
321 | { | |
33977f81 JH |
322 | /* Readonly reads are safe. */ |
323 | if (TREE_READONLY (t) && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (t))) | |
324 | return; /* Read of a constant, do not change the function state. */ | |
b8698a0f | 325 | else |
ea900239 | 326 | { |
33977f81 JH |
327 | if (dump_file) |
328 | fprintf (dump_file, " global memory read is not const\n"); | |
ea900239 DB |
329 | /* Just a regular read. */ |
330 | if (local->pure_const_state == IPA_CONST) | |
331 | local->pure_const_state = IPA_PURE; | |
332 | } | |
333 | } | |
33977f81 JH |
334 | else |
335 | { | |
336 | /* Compilation level statics can be read if they are readonly | |
337 | variables. */ | |
338 | if (TREE_READONLY (t)) | |
339 | return; | |
340 | ||
341 | if (dump_file) | |
342 | fprintf (dump_file, " static memory read is not const\n"); | |
343 | /* Just a regular read. */ | |
344 | if (local->pure_const_state == IPA_CONST) | |
345 | local->pure_const_state = IPA_PURE; | |
346 | } | |
ea900239 DB |
347 | } |
348 | ||
ea900239 | 349 | |
33977f81 JH |
350 | /* Check to see if the use (or definition when CHECKING_WRITE is true) |
351 | variable T is legal in a function that is either pure or const. */ | |
ea900239 | 352 | |
b8698a0f | 353 | static inline void |
346ef3fa | 354 | check_op (funct_state local, tree t, bool checking_write) |
ea900239 | 355 | { |
3c1832c3 JH |
356 | t = get_base_address (t); |
357 | if (t && TREE_THIS_VOLATILE (t)) | |
ea900239 | 358 | { |
346ef3fa RG |
359 | local->pure_const_state = IPA_NEITHER; |
360 | if (dump_file) | |
361 | fprintf (dump_file, " Volatile indirect ref is not const/pure\n"); | |
362 | return; | |
363 | } | |
3c1832c3 | 364 | else if (t |
70f34814 | 365 | && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF) |
3c1832c3 JH |
366 | && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME |
367 | && !ptr_deref_may_alias_global_p (TREE_OPERAND (t, 0))) | |
368 | { | |
369 | if (dump_file) | |
370 | fprintf (dump_file, " Indirect ref to local memory is OK\n"); | |
371 | return; | |
372 | } | |
346ef3fa RG |
373 | else if (checking_write) |
374 | { | |
375 | local->pure_const_state = IPA_NEITHER; | |
376 | if (dump_file) | |
377 | fprintf (dump_file, " Indirect ref write is not const/pure\n"); | |
378 | return; | |
379 | } | |
380 | else | |
381 | { | |
382 | if (dump_file) | |
383 | fprintf (dump_file, " Indirect ref read is not const\n"); | |
384 | if (local->pure_const_state == IPA_CONST) | |
385 | local->pure_const_state = IPA_PURE; | |
ea900239 DB |
386 | } |
387 | } | |
388 | ||
f10ea640 JH |
389 | /* compute state based on ECF FLAGS and store to STATE and LOOPING. */ |
390 | ||
391 | static void | |
392 | state_from_flags (enum pure_const_state_e *state, bool *looping, | |
393 | int flags, bool cannot_lead_to_return) | |
394 | { | |
395 | *looping = false; | |
396 | if (flags & ECF_LOOPING_CONST_OR_PURE) | |
397 | { | |
398 | *looping = true; | |
399 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
400 | fprintf (dump_file, " looping"); | |
401 | } | |
402 | if (flags & ECF_CONST) | |
403 | { | |
404 | *state = IPA_CONST; | |
405 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
406 | fprintf (dump_file, " const\n"); | |
407 | } | |
408 | else if (flags & ECF_PURE) | |
409 | { | |
410 | *state = IPA_PURE; | |
411 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
412 | fprintf (dump_file, " pure\n"); | |
413 | } | |
414 | else if (cannot_lead_to_return) | |
415 | { | |
416 | *state = IPA_PURE; | |
417 | *looping = true; | |
418 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
419 | fprintf (dump_file, " ignoring side effects->pure looping\n"); | |
420 | } | |
421 | else | |
422 | { | |
423 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
df92c640 | 424 | fprintf (dump_file, " neither\n"); |
f10ea640 JH |
425 | *state = IPA_NEITHER; |
426 | *looping = true; | |
427 | } | |
428 | } | |
429 | ||
430 | /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store | |
431 | into STATE and LOOPING better of the two variants. | |
432 | Be sure to merge looping correctly. IPA_NEITHER functions | |
433 | have looping 0 even if they don't have to return. */ | |
434 | ||
435 | static inline void | |
436 | better_state (enum pure_const_state_e *state, bool *looping, | |
437 | enum pure_const_state_e state2, bool looping2) | |
438 | { | |
439 | if (state2 < *state) | |
440 | { | |
441 | if (*state == IPA_NEITHER) | |
442 | *looping = looping2; | |
443 | else | |
444 | *looping = MIN (*looping, looping2); | |
b0d04a5f | 445 | *state = state2; |
f10ea640 JH |
446 | } |
447 | else if (state2 != IPA_NEITHER) | |
448 | *looping = MIN (*looping, looping2); | |
449 | } | |
450 | ||
451 | /* Merge STATE and STATE2 and LOOPING and LOOPING2 and store | |
452 | into STATE and LOOPING worse of the two variants. */ | |
453 | ||
454 | static inline void | |
455 | worse_state (enum pure_const_state_e *state, bool *looping, | |
456 | enum pure_const_state_e state2, bool looping2) | |
457 | { | |
458 | *state = MAX (*state, state2); | |
459 | *looping = MAX (*looping, looping2); | |
460 | } | |
461 | ||
61502ca8 | 462 | /* Recognize special cases of builtins that are by themselves not pure or const |
0a42aa4e JH |
463 | but function using them is. */ |
464 | static bool | |
9e97ff61 | 465 | special_builtin_state (enum pure_const_state_e *state, bool *looping, |
0a42aa4e JH |
466 | tree callee) |
467 | { | |
468 | if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL) | |
469 | switch (DECL_FUNCTION_CODE (callee)) | |
470 | { | |
471 | case BUILT_IN_RETURN: | |
472 | case BUILT_IN_UNREACHABLE: | |
473 | case BUILT_IN_ALLOCA: | |
13e49da9 | 474 | case BUILT_IN_ALLOCA_WITH_ALIGN: |
0a42aa4e JH |
475 | case BUILT_IN_STACK_SAVE: |
476 | case BUILT_IN_STACK_RESTORE: | |
477 | case BUILT_IN_EH_POINTER: | |
478 | case BUILT_IN_EH_FILTER: | |
479 | case BUILT_IN_UNWIND_RESUME: | |
480 | case BUILT_IN_CXA_END_CLEANUP: | |
481 | case BUILT_IN_EH_COPY_VALUES: | |
482 | case BUILT_IN_FRAME_ADDRESS: | |
483 | case BUILT_IN_APPLY: | |
484 | case BUILT_IN_APPLY_ARGS: | |
0a42aa4e JH |
485 | *looping = false; |
486 | *state = IPA_CONST; | |
487 | return true; | |
488 | case BUILT_IN_PREFETCH: | |
489 | *looping = true; | |
490 | *state = IPA_CONST; | |
491 | return true; | |
083e891e MP |
492 | default: |
493 | break; | |
0a42aa4e JH |
494 | } |
495 | return false; | |
496 | } | |
497 | ||
ea900239 DB |
498 | /* Check the parameters of a function call to CALL_EXPR to see if |
499 | there are any references in the parameters that are not allowed for | |
500 | pure or const functions. Also check to see if this is either an | |
501 | indirect call, a call outside the compilation unit, or has special | |
502 | attributes that may also effect the purity. The CALL_EXPR node for | |
503 | the entire call expression. */ | |
504 | ||
505 | static void | |
538dd0b7 | 506 | check_call (funct_state local, gcall *call, bool ipa) |
ea900239 | 507 | { |
726a989a | 508 | int flags = gimple_call_flags (call); |
33977f81 | 509 | tree callee_t = gimple_call_fndecl (call); |
33977f81 JH |
510 | bool possibly_throws = stmt_could_throw_p (call); |
511 | bool possibly_throws_externally = (possibly_throws | |
512 | && stmt_can_throw_external (call)); | |
ea900239 | 513 | |
33977f81 JH |
514 | if (possibly_throws) |
515 | { | |
516 | unsigned int i; | |
517 | for (i = 0; i < gimple_num_ops (call); i++) | |
518 | if (gimple_op (call, i) | |
519 | && tree_could_throw_p (gimple_op (call, i))) | |
520 | { | |
8f4f502f | 521 | if (possibly_throws && cfun->can_throw_non_call_exceptions) |
33977f81 JH |
522 | { |
523 | if (dump_file) | |
524 | fprintf (dump_file, " operand can throw; looping\n"); | |
525 | local->looping = true; | |
526 | } | |
527 | if (possibly_throws_externally) | |
528 | { | |
529 | if (dump_file) | |
530 | fprintf (dump_file, " operand can throw externally\n"); | |
531 | local->can_throw = true; | |
532 | } | |
533 | } | |
534 | } | |
b8698a0f | 535 | |
ea900239 DB |
536 | /* The const and pure flags are set by a variety of places in the |
537 | compiler (including here). If someone has already set the flags | |
538 | for the callee, (such as for some of the builtins) we will use | |
b8698a0f L |
539 | them, otherwise we will compute our own information. |
540 | ||
ea900239 DB |
541 | Const and pure functions have less clobber effects than other |
542 | functions so we process these first. Otherwise if it is a call | |
543 | outside the compilation unit or an indirect call we punt. This | |
544 | leaves local calls which will be processed by following the call | |
b8698a0f | 545 | graph. */ |
ea900239 DB |
546 | if (callee_t) |
547 | { | |
0a42aa4e JH |
548 | enum pure_const_state_e call_state; |
549 | bool call_looping; | |
550 | ||
8413ca87 JJ |
551 | if (gimple_call_builtin_p (call, BUILT_IN_NORMAL) |
552 | && !nonfreeing_call_p (call)) | |
553 | local->can_free = true; | |
554 | ||
9e97ff61 | 555 | if (special_builtin_state (&call_state, &call_looping, callee_t)) |
0a42aa4e JH |
556 | { |
557 | worse_state (&local->pure_const_state, &local->looping, | |
558 | call_state, call_looping); | |
559 | return; | |
560 | } | |
ea900239 DB |
561 | /* When bad things happen to bad functions, they cannot be const |
562 | or pure. */ | |
563 | if (setjmp_call_p (callee_t)) | |
becfd6e5 | 564 | { |
33977f81 JH |
565 | if (dump_file) |
566 | fprintf (dump_file, " setjmp is not const/pure\n"); | |
567 | local->looping = true; | |
becfd6e5 | 568 | local->pure_const_state = IPA_NEITHER; |
becfd6e5 | 569 | } |
ea900239 DB |
570 | |
571 | if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL) | |
572 | switch (DECL_FUNCTION_CODE (callee_t)) | |
573 | { | |
574 | case BUILT_IN_LONGJMP: | |
575 | case BUILT_IN_NONLOCAL_GOTO: | |
33977f81 JH |
576 | if (dump_file) |
577 | fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n"); | |
ea900239 | 578 | local->pure_const_state = IPA_NEITHER; |
33977f81 | 579 | local->looping = true; |
ea900239 DB |
580 | break; |
581 | default: | |
582 | break; | |
583 | } | |
584 | } | |
8413ca87 JJ |
585 | else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call)) |
586 | local->can_free = true; | |
ea900239 | 587 | |
33977f81 | 588 | /* When not in IPA mode, we can still handle self recursion. */ |
fc11f321 JH |
589 | if (!ipa && callee_t |
590 | && recursive_call_p (current_function_decl, callee_t)) | |
5dc16b19 MLI |
591 | { |
592 | if (dump_file) | |
593 | fprintf (dump_file, " Recursive call can loop.\n"); | |
594 | local->looping = true; | |
595 | } | |
61502ca8 | 596 | /* Either callee is unknown or we are doing local analysis. |
c59f5d1b JH |
597 | Look to see if there are any bits available for the callee (such as by |
598 | declaration or because it is builtin) and process solely on the basis of | |
599 | those bits. */ | |
f10ea640 | 600 | else if (!ipa) |
ea900239 | 601 | { |
f10ea640 JH |
602 | enum pure_const_state_e call_state; |
603 | bool call_looping; | |
8f4f502f | 604 | if (possibly_throws && cfun->can_throw_non_call_exceptions) |
33977f81 JH |
605 | { |
606 | if (dump_file) | |
607 | fprintf (dump_file, " can throw; looping\n"); | |
608 | local->looping = true; | |
609 | } | |
610 | if (possibly_throws_externally) | |
611 | { | |
612 | if (dump_file) | |
613 | { | |
1d65f45c RH |
614 | fprintf (dump_file, " can throw externally to lp %i\n", |
615 | lookup_stmt_eh_lp (call)); | |
33977f81 JH |
616 | if (callee_t) |
617 | fprintf (dump_file, " callee:%s\n", | |
618 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t))); | |
619 | } | |
620 | local->can_throw = true; | |
621 | } | |
f10ea640 JH |
622 | if (dump_file && (dump_flags & TDF_DETAILS)) |
623 | fprintf (dump_file, " checking flags for call:"); | |
624 | state_from_flags (&call_state, &call_looping, flags, | |
625 | ((flags & (ECF_NORETURN | ECF_NOTHROW)) | |
626 | == (ECF_NORETURN | ECF_NOTHROW)) | |
627 | || (!flag_exceptions && (flags & ECF_NORETURN))); | |
628 | worse_state (&local->pure_const_state, &local->looping, | |
629 | call_state, call_looping); | |
ea900239 | 630 | } |
33977f81 | 631 | /* Direct functions calls are handled by IPA propagation. */ |
ea900239 DB |
632 | } |
633 | ||
f10ea640 | 634 | /* Wrapper around check_decl for loads in local more. */ |
346ef3fa RG |
635 | |
636 | static bool | |
355fe088 | 637 | check_load (gimple *, tree op, tree, void *data) |
346ef3fa RG |
638 | { |
639 | if (DECL_P (op)) | |
f10ea640 | 640 | check_decl ((funct_state)data, op, false, false); |
346ef3fa RG |
641 | else |
642 | check_op ((funct_state)data, op, false); | |
643 | return false; | |
644 | } | |
645 | ||
f10ea640 | 646 | /* Wrapper around check_decl for stores in local more. */ |
346ef3fa RG |
647 | |
648 | static bool | |
355fe088 | 649 | check_store (gimple *, tree op, tree, void *data) |
346ef3fa RG |
650 | { |
651 | if (DECL_P (op)) | |
f10ea640 JH |
652 | check_decl ((funct_state)data, op, true, false); |
653 | else | |
654 | check_op ((funct_state)data, op, true); | |
655 | return false; | |
656 | } | |
657 | ||
658 | /* Wrapper around check_decl for loads in ipa mode. */ | |
659 | ||
660 | static bool | |
355fe088 | 661 | check_ipa_load (gimple *, tree op, tree, void *data) |
f10ea640 JH |
662 | { |
663 | if (DECL_P (op)) | |
664 | check_decl ((funct_state)data, op, false, true); | |
665 | else | |
666 | check_op ((funct_state)data, op, false); | |
667 | return false; | |
668 | } | |
669 | ||
670 | /* Wrapper around check_decl for stores in ipa mode. */ | |
671 | ||
672 | static bool | |
355fe088 | 673 | check_ipa_store (gimple *, tree op, tree, void *data) |
f10ea640 JH |
674 | { |
675 | if (DECL_P (op)) | |
676 | check_decl ((funct_state)data, op, true, true); | |
346ef3fa RG |
677 | else |
678 | check_op ((funct_state)data, op, true); | |
679 | return false; | |
680 | } | |
681 | ||
5006671f RG |
682 | /* Look into pointer pointed to by GSIP and figure out what interesting side |
683 | effects it has. */ | |
33977f81 JH |
684 | static void |
685 | check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa) | |
ea900239 | 686 | { |
355fe088 | 687 | gimple *stmt = gsi_stmt (*gsip); |
ea900239 | 688 | |
b5b8b0ac AO |
689 | if (is_gimple_debug (stmt)) |
690 | return; | |
691 | ||
38147a2a JH |
692 | /* Do consider clobber as side effects before IPA, so we rather inline |
693 | C++ destructors and keep clobber semantics than eliminate them. | |
694 | ||
695 | TODO: We may get smarter during early optimizations on these and let | |
696 | functions containing only clobbers to be optimized more. This is a common | |
697 | case of C++ destructors. */ | |
698 | ||
699 | if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt)) | |
700 | return; | |
701 | ||
33977f81 | 702 | if (dump_file) |
ea900239 | 703 | { |
33977f81 JH |
704 | fprintf (dump_file, " scanning: "); |
705 | print_gimple_stmt (dump_file, stmt, 0, 0); | |
706 | } | |
5006671f | 707 | |
ace018f9 RG |
708 | if (gimple_has_volatile_ops (stmt) |
709 | && !gimple_clobber_p (stmt)) | |
4e89a3fa RG |
710 | { |
711 | local->pure_const_state = IPA_NEITHER; | |
712 | if (dump_file) | |
713 | fprintf (dump_file, " Volatile stmt is not const/pure\n"); | |
714 | } | |
715 | ||
346ef3fa | 716 | /* Look for loads and stores. */ |
f10ea640 JH |
717 | walk_stmt_load_store_ops (stmt, local, |
718 | ipa ? check_ipa_load : check_load, | |
719 | ipa ? check_ipa_store : check_store); | |
33977f81 JH |
720 | |
721 | if (gimple_code (stmt) != GIMPLE_CALL | |
722 | && stmt_could_throw_p (stmt)) | |
723 | { | |
8f4f502f | 724 | if (cfun->can_throw_non_call_exceptions) |
33977f81 JH |
725 | { |
726 | if (dump_file) | |
425b784f | 727 | fprintf (dump_file, " can throw; looping\n"); |
33977f81 JH |
728 | local->looping = true; |
729 | } | |
730 | if (stmt_can_throw_external (stmt)) | |
731 | { | |
732 | if (dump_file) | |
425b784f | 733 | fprintf (dump_file, " can throw externally\n"); |
33977f81 JH |
734 | local->can_throw = true; |
735 | } | |
425b784f JH |
736 | else |
737 | if (dump_file) | |
738 | fprintf (dump_file, " can throw\n"); | |
726a989a | 739 | } |
726a989a RB |
740 | switch (gimple_code (stmt)) |
741 | { | |
33977f81 | 742 | case GIMPLE_CALL: |
538dd0b7 | 743 | check_call (local, as_a <gcall *> (stmt), ipa); |
ea900239 | 744 | break; |
726a989a | 745 | case GIMPLE_LABEL: |
538dd0b7 | 746 | if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt)))) |
ea900239 | 747 | /* Target of long jump. */ |
becfd6e5 | 748 | { |
33977f81 | 749 | if (dump_file) |
425b784f | 750 | fprintf (dump_file, " nonlocal label is not const/pure\n"); |
becfd6e5 | 751 | local->pure_const_state = IPA_NEITHER; |
becfd6e5 | 752 | } |
ea900239 | 753 | break; |
726a989a | 754 | case GIMPLE_ASM: |
538dd0b7 | 755 | if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt))) |
33977f81 | 756 | { |
edcdea5b | 757 | if (dump_file) |
425b784f | 758 | fprintf (dump_file, " memory asm clobber is not const/pure\n"); |
edcdea5b NF |
759 | /* Abandon all hope, ye who enter here. */ |
760 | local->pure_const_state = IPA_NEITHER; | |
8413ca87 | 761 | local->can_free = true; |
33977f81 | 762 | } |
538dd0b7 | 763 | if (gimple_asm_volatile_p (as_a <gasm *> (stmt))) |
33977f81 JH |
764 | { |
765 | if (dump_file) | |
425b784f | 766 | fprintf (dump_file, " volatile is not const/pure\n"); |
33977f81 JH |
767 | /* Abandon all hope, ye who enter here. */ |
768 | local->pure_const_state = IPA_NEITHER; | |
8413ca87 JJ |
769 | local->looping = true; |
770 | local->can_free = true; | |
33977f81 JH |
771 | } |
772 | return; | |
ea900239 DB |
773 | default: |
774 | break; | |
775 | } | |
ea900239 DB |
776 | } |
777 | ||
812dbce5 | 778 | |
ea900239 DB |
779 | /* This is the main routine for finding the reference patterns for |
780 | global variables within a function FN. */ | |
781 | ||
33977f81 JH |
782 | static funct_state |
783 | analyze_function (struct cgraph_node *fn, bool ipa) | |
ea900239 | 784 | { |
67348ccc | 785 | tree decl = fn->decl; |
33977f81 JH |
786 | funct_state l; |
787 | basic_block this_block; | |
ea900239 | 788 | |
33977f81 JH |
789 | l = XCNEW (struct funct_state_d); |
790 | l->pure_const_state = IPA_CONST; | |
f87c9042 JH |
791 | l->state_previously_known = IPA_NEITHER; |
792 | l->looping_previously_known = true; | |
33977f81 JH |
793 | l->looping = false; |
794 | l->can_throw = false; | |
8413ca87 | 795 | l->can_free = false; |
c47d0034 | 796 | state_from_flags (&l->state_previously_known, &l->looping_previously_known, |
67348ccc | 797 | flags_from_decl_or_type (fn->decl), |
d52f5295 | 798 | fn->cannot_return_p ()); |
c47d0034 | 799 | |
67348ccc | 800 | if (fn->thunk.thunk_p || fn->alias) |
c47d0034 JH |
801 | { |
802 | /* Thunk gets propagated through, so nothing interesting happens. */ | |
803 | gcc_assert (ipa); | |
6cbde2e3 BE |
804 | if (fn->thunk.thunk_p && fn->thunk.virtual_offset_p) |
805 | l->pure_const_state = IPA_NEITHER; | |
c47d0034 JH |
806 | return l; |
807 | } | |
ea900239 DB |
808 | |
809 | if (dump_file) | |
810 | { | |
b8698a0f | 811 | fprintf (dump_file, "\n\n local analysis of %s\n ", |
fec39fa6 | 812 | fn->name ()); |
ea900239 | 813 | } |
b8698a0f | 814 | |
33977f81 | 815 | push_cfun (DECL_STRUCT_FUNCTION (decl)); |
b8698a0f | 816 | |
11cd3bed | 817 | FOR_EACH_BB_FN (this_block, cfun) |
ea900239 | 818 | { |
33977f81 JH |
819 | gimple_stmt_iterator gsi; |
820 | struct walk_stmt_info wi; | |
ea900239 | 821 | |
c3284718 | 822 | memset (&wi, 0, sizeof (wi)); |
33977f81 JH |
823 | for (gsi = gsi_start_bb (this_block); |
824 | !gsi_end_p (gsi); | |
825 | gsi_next (&gsi)) | |
ea900239 | 826 | { |
33977f81 | 827 | check_stmt (&gsi, l, ipa); |
8413ca87 JJ |
828 | if (l->pure_const_state == IPA_NEITHER |
829 | && l->looping | |
830 | && l->can_throw | |
831 | && l->can_free) | |
33977f81 | 832 | goto end; |
ea900239 DB |
833 | } |
834 | } | |
13335ae6 AP |
835 | |
836 | end: | |
33977f81 JH |
837 | if (l->pure_const_state != IPA_NEITHER) |
838 | { | |
839 | /* Const functions cannot have back edges (an | |
840 | indication of possible infinite loop side | |
841 | effect. */ | |
842 | if (mark_dfs_back_edges ()) | |
2de58650 | 843 | { |
7351bcaa | 844 | /* Preheaders are needed for SCEV to work. |
61502ca8 | 845 | Simple latches and recorded exits improve chances that loop will |
0d5049b2 RB |
846 | proved to be finite in testcases such as in loop-15.c |
847 | and loop-24.c */ | |
848 | loop_optimizer_init (LOOPS_HAVE_PREHEADERS | |
849 | | LOOPS_HAVE_SIMPLE_LATCHES | |
7351bcaa | 850 | | LOOPS_HAVE_RECORDED_EXITS); |
2de58650 JH |
851 | if (dump_file && (dump_flags & TDF_DETAILS)) |
852 | flow_loops_dump (dump_file, NULL, 0); | |
853 | if (mark_irreducible_loops ()) | |
854 | { | |
855 | if (dump_file) | |
856 | fprintf (dump_file, " has irreducible loops\n"); | |
857 | l->looping = true; | |
858 | } | |
b8698a0f | 859 | else |
2de58650 | 860 | { |
2de58650 JH |
861 | struct loop *loop; |
862 | scev_initialize (); | |
f0bd40b1 | 863 | FOR_EACH_LOOP (loop, 0) |
2de58650 JH |
864 | if (!finite_loop_p (loop)) |
865 | { | |
866 | if (dump_file) | |
0d5049b2 RB |
867 | fprintf (dump_file, " can not prove finiteness of " |
868 | "loop %i\n", loop->num); | |
2de58650 | 869 | l->looping =true; |
f0bd40b1 | 870 | break; |
2de58650 JH |
871 | } |
872 | scev_finalize (); | |
873 | } | |
874 | loop_optimizer_finalize (); | |
875 | } | |
33977f81 JH |
876 | } |
877 | ||
f10ea640 JH |
878 | if (dump_file && (dump_flags & TDF_DETAILS)) |
879 | fprintf (dump_file, " checking previously known:"); | |
f10ea640 JH |
880 | |
881 | better_state (&l->pure_const_state, &l->looping, | |
882 | l->state_previously_known, | |
883 | l->looping_previously_known); | |
33977f81 JH |
884 | if (TREE_NOTHROW (decl)) |
885 | l->can_throw = false; | |
886 | ||
887 | pop_cfun (); | |
13335ae6 AP |
888 | if (dump_file) |
889 | { | |
33977f81 JH |
890 | if (l->looping) |
891 | fprintf (dump_file, "Function is locally looping.\n"); | |
892 | if (l->can_throw) | |
893 | fprintf (dump_file, "Function is locally throwing.\n"); | |
894 | if (l->pure_const_state == IPA_CONST) | |
895 | fprintf (dump_file, "Function is locally const.\n"); | |
896 | if (l->pure_const_state == IPA_PURE) | |
897 | fprintf (dump_file, "Function is locally pure.\n"); | |
8413ca87 JJ |
898 | if (l->can_free) |
899 | fprintf (dump_file, "Function can locally free.\n"); | |
13335ae6 | 900 | } |
33977f81 | 901 | return l; |
ea900239 DB |
902 | } |
903 | ||
129a37fc JH |
904 | /* Called when new function is inserted to callgraph late. */ |
905 | static void | |
906 | add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
907 | { | |
d52f5295 | 908 | if (node->get_availability () < AVAIL_INTERPOSABLE) |
e2c9111c | 909 | return; |
129a37fc JH |
910 | /* There are some shared nodes, in particular the initializers on |
911 | static declarations. We do not need to scan them more than once | |
912 | since all we would be interested in are the addressof | |
913 | operations. */ | |
2bf86c84 JH |
914 | if (node->get_availability () > AVAIL_INTERPOSABLE |
915 | && opt_for_fn (node->decl, flag_ipa_pure_const)) | |
5dc16b19 | 916 | set_function_state (node, analyze_function (node, true)); |
129a37fc JH |
917 | } |
918 | ||
e2c9111c JH |
919 | /* Called when new clone is inserted to callgraph late. */ |
920 | ||
921 | static void | |
922 | duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst, | |
923 | void *data ATTRIBUTE_UNUSED) | |
924 | { | |
97d45cef | 925 | if (has_function_state (src)) |
e2c9111c JH |
926 | { |
927 | funct_state l = XNEW (struct funct_state_d); | |
97d45cef | 928 | gcc_assert (!has_function_state (dst)); |
e2c9111c JH |
929 | memcpy (l, get_function_state (src), sizeof (*l)); |
930 | set_function_state (dst, l); | |
931 | } | |
932 | } | |
933 | ||
934 | /* Called when new clone is inserted to callgraph late. */ | |
935 | ||
936 | static void | |
937 | remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED) | |
938 | { | |
97d45cef | 939 | if (has_function_state (node)) |
e2c9111c | 940 | { |
37512c66 JH |
941 | funct_state l = get_function_state (node); |
942 | if (l != &varying_state) | |
943 | free (l); | |
e2c9111c JH |
944 | set_function_state (node, NULL); |
945 | } | |
946 | } | |
947 | ||
ea900239 | 948 | \f |
3edf64aa DM |
949 | void |
950 | pass_ipa_pure_const:: | |
d7f09764 | 951 | register_hooks (void) |
ea900239 | 952 | { |
d7f09764 DN |
953 | if (init_p) |
954 | return; | |
955 | ||
956 | init_p = true; | |
ea900239 | 957 | |
e2c9111c | 958 | node_removal_hook_holder = |
3dafb85c | 959 | symtab->add_cgraph_removal_hook (&remove_node_data, NULL); |
e2c9111c | 960 | node_duplication_hook_holder = |
3dafb85c | 961 | symtab->add_cgraph_duplication_hook (&duplicate_node_data, NULL); |
129a37fc | 962 | function_insertion_hook_holder = |
3dafb85c | 963 | symtab->add_cgraph_insertion_hook (&add_new_function, NULL); |
d7f09764 DN |
964 | } |
965 | ||
966 | ||
967 | /* Analyze each function in the cgraph to see if it is locally PURE or | |
968 | CONST. */ | |
969 | ||
b8698a0f | 970 | static void |
651df1b2 | 971 | pure_const_generate_summary (void) |
d7f09764 DN |
972 | { |
973 | struct cgraph_node *node; | |
974 | ||
3edf64aa DM |
975 | pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass); |
976 | pass->register_hooks (); | |
d7f09764 | 977 | |
b8698a0f | 978 | /* Process all of the functions. |
ea900239 | 979 | |
d52f5295 | 980 | We process AVAIL_INTERPOSABLE functions. We can not use the results |
c59f5d1b | 981 | by default, but the info can be used at LTO with -fwhole-program or |
61502ca8 | 982 | when function got cloned and the clone is AVAILABLE. */ |
c59f5d1b | 983 | |
65c70e6b | 984 | FOR_EACH_DEFINED_FUNCTION (node) |
2bf86c84 JH |
985 | if (node->get_availability () >= AVAIL_INTERPOSABLE |
986 | && opt_for_fn (node->decl, flag_ipa_pure_const)) | |
33977f81 | 987 | set_function_state (node, analyze_function (node, true)); |
812dbce5 JH |
988 | } |
989 | ||
d7f09764 DN |
990 | |
991 | /* Serialize the ipa info for lto. */ | |
992 | ||
993 | static void | |
f27c1867 | 994 | pure_const_write_summary (void) |
d7f09764 DN |
995 | { |
996 | struct cgraph_node *node; | |
997 | struct lto_simple_output_block *ob | |
998 | = lto_create_simple_output_block (LTO_section_ipa_pure_const); | |
999 | unsigned int count = 0; | |
f27c1867 JH |
1000 | lto_symtab_encoder_iterator lsei; |
1001 | lto_symtab_encoder_t encoder; | |
d7f09764 | 1002 | |
f27c1867 JH |
1003 | encoder = lto_get_out_decl_state ()->symtab_node_encoder; |
1004 | ||
1005 | for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); | |
1006 | lsei_next_function_in_partition (&lsei)) | |
d7f09764 | 1007 | { |
f27c1867 | 1008 | node = lsei_cgraph_node (lsei); |
67348ccc | 1009 | if (node->definition && has_function_state (node)) |
d7f09764 DN |
1010 | count++; |
1011 | } | |
b8698a0f | 1012 | |
412288f1 | 1013 | streamer_write_uhwi_stream (ob->main_stream, count); |
b8698a0f | 1014 | |
d7f09764 | 1015 | /* Process all of the functions. */ |
f27c1867 JH |
1016 | for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); |
1017 | lsei_next_function_in_partition (&lsei)) | |
d7f09764 | 1018 | { |
f27c1867 | 1019 | node = lsei_cgraph_node (lsei); |
67348ccc | 1020 | if (node->definition && has_function_state (node)) |
d7f09764 | 1021 | { |
2465dcc2 | 1022 | struct bitpack_d bp; |
d7f09764 DN |
1023 | funct_state fs; |
1024 | int node_ref; | |
7380e6ef | 1025 | lto_symtab_encoder_t encoder; |
b8698a0f | 1026 | |
d7f09764 DN |
1027 | fs = get_function_state (node); |
1028 | ||
7380e6ef | 1029 | encoder = ob->decl_state->symtab_node_encoder; |
67348ccc | 1030 | node_ref = lto_symtab_encoder_encode (encoder, node); |
412288f1 | 1031 | streamer_write_uhwi_stream (ob->main_stream, node_ref); |
b8698a0f | 1032 | |
d7f09764 DN |
1033 | /* Note that flags will need to be read in the opposite |
1034 | order as we are pushing the bitflags into FLAGS. */ | |
2465dcc2 RG |
1035 | bp = bitpack_create (ob->main_stream); |
1036 | bp_pack_value (&bp, fs->pure_const_state, 2); | |
1037 | bp_pack_value (&bp, fs->state_previously_known, 2); | |
1038 | bp_pack_value (&bp, fs->looping_previously_known, 1); | |
1039 | bp_pack_value (&bp, fs->looping, 1); | |
1040 | bp_pack_value (&bp, fs->can_throw, 1); | |
8413ca87 | 1041 | bp_pack_value (&bp, fs->can_free, 1); |
412288f1 | 1042 | streamer_write_bitpack (&bp); |
d7f09764 DN |
1043 | } |
1044 | } | |
1045 | ||
1046 | lto_destroy_simple_output_block (ob); | |
1047 | } | |
1048 | ||
1049 | ||
1050 | /* Deserialize the ipa info for lto. */ | |
1051 | ||
b8698a0f | 1052 | static void |
d7f09764 DN |
1053 | pure_const_read_summary (void) |
1054 | { | |
1055 | struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data (); | |
1056 | struct lto_file_decl_data *file_data; | |
1057 | unsigned int j = 0; | |
1058 | ||
3edf64aa DM |
1059 | pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass); |
1060 | pass->register_hooks (); | |
1061 | ||
d7f09764 DN |
1062 | while ((file_data = file_data_vec[j++])) |
1063 | { | |
1064 | const char *data; | |
1065 | size_t len; | |
1066 | struct lto_input_block *ib | |
b8698a0f L |
1067 | = lto_create_simple_input_block (file_data, |
1068 | LTO_section_ipa_pure_const, | |
d7f09764 DN |
1069 | &data, &len); |
1070 | if (ib) | |
1071 | { | |
1072 | unsigned int i; | |
412288f1 | 1073 | unsigned int count = streamer_read_uhwi (ib); |
d7f09764 DN |
1074 | |
1075 | for (i = 0; i < count; i++) | |
1076 | { | |
1077 | unsigned int index; | |
1078 | struct cgraph_node *node; | |
2465dcc2 | 1079 | struct bitpack_d bp; |
d7f09764 | 1080 | funct_state fs; |
7380e6ef | 1081 | lto_symtab_encoder_t encoder; |
d7f09764 DN |
1082 | |
1083 | fs = XCNEW (struct funct_state_d); | |
412288f1 | 1084 | index = streamer_read_uhwi (ib); |
7380e6ef | 1085 | encoder = file_data->symtab_node_encoder; |
d52f5295 ML |
1086 | node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder, |
1087 | index)); | |
d7f09764 DN |
1088 | set_function_state (node, fs); |
1089 | ||
1090 | /* Note that the flags must be read in the opposite | |
1091 | order in which they were written (the bitflags were | |
1092 | pushed into FLAGS). */ | |
412288f1 | 1093 | bp = streamer_read_bitpack (ib); |
d7f09764 | 1094 | fs->pure_const_state |
2465dcc2 | 1095 | = (enum pure_const_state_e) bp_unpack_value (&bp, 2); |
d7f09764 | 1096 | fs->state_previously_known |
2465dcc2 RG |
1097 | = (enum pure_const_state_e) bp_unpack_value (&bp, 2); |
1098 | fs->looping_previously_known = bp_unpack_value (&bp, 1); | |
1099 | fs->looping = bp_unpack_value (&bp, 1); | |
1100 | fs->can_throw = bp_unpack_value (&bp, 1); | |
8413ca87 | 1101 | fs->can_free = bp_unpack_value (&bp, 1); |
d56026c2 JH |
1102 | if (dump_file) |
1103 | { | |
67348ccc | 1104 | int flags = flags_from_decl_or_type (node->decl); |
d56026c2 | 1105 | fprintf (dump_file, "Read info for %s/%i ", |
fec39fa6 | 1106 | node->name (), |
67348ccc | 1107 | node->order); |
d56026c2 JH |
1108 | if (flags & ECF_CONST) |
1109 | fprintf (dump_file, " const"); | |
1110 | if (flags & ECF_PURE) | |
1111 | fprintf (dump_file, " pure"); | |
1112 | if (flags & ECF_NOTHROW) | |
1113 | fprintf (dump_file, " nothrow"); | |
1114 | fprintf (dump_file, "\n pure const state: %s\n", | |
1115 | pure_const_names[fs->pure_const_state]); | |
1116 | fprintf (dump_file, " previously known state: %s\n", | |
1117 | pure_const_names[fs->looping_previously_known]); | |
1118 | if (fs->looping) | |
1119 | fprintf (dump_file," function is locally looping\n"); | |
1120 | if (fs->looping_previously_known) | |
1121 | fprintf (dump_file," function is previously known looping\n"); | |
1122 | if (fs->can_throw) | |
1123 | fprintf (dump_file," function is locally throwing\n"); | |
8413ca87 JJ |
1124 | if (fs->can_free) |
1125 | fprintf (dump_file," function can locally free\n"); | |
d56026c2 | 1126 | } |
d7f09764 DN |
1127 | } |
1128 | ||
b8698a0f L |
1129 | lto_destroy_simple_input_block (file_data, |
1130 | LTO_section_ipa_pure_const, | |
d7f09764 DN |
1131 | ib, data, len); |
1132 | } | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | ||
2505c5ed JH |
1137 | static bool |
1138 | ignore_edge (struct cgraph_edge *e) | |
1139 | { | |
1140 | return (!e->can_throw_external); | |
1141 | } | |
1142 | ||
fede8efa | 1143 | /* Return true if NODE is self recursive function. |
fc11f321 JH |
1144 | Indirectly recursive functions appears as non-trivial strongly |
1145 | connected components, so we need to care about self recursion | |
1146 | only. */ | |
03ec7d01 JH |
1147 | |
1148 | static bool | |
1149 | self_recursive_p (struct cgraph_node *node) | |
1150 | { | |
1151 | struct cgraph_edge *e; | |
1152 | for (e = node->callees; e; e = e->next_callee) | |
d52f5295 | 1153 | if (e->callee->function_symbol () == node) |
03ec7d01 JH |
1154 | return true; |
1155 | return false; | |
1156 | } | |
1157 | ||
17e0fc92 JH |
1158 | /* Return true if N is cdtor that is not const or pure. In this case we may |
1159 | need to remove unreachable function if it is marked const/pure. */ | |
1160 | ||
1161 | static bool | |
1162 | cdtor_p (cgraph_node *n, void *) | |
1163 | { | |
1164 | if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl)) | |
1165 | return !TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl); | |
1166 | return false; | |
1167 | } | |
1168 | ||
15e80fc3 JH |
1169 | /* Produce transitive closure over the callgraph and compute pure/const |
1170 | attributes. */ | |
f10ea640 | 1171 | |
17e0fc92 | 1172 | static bool |
15e80fc3 | 1173 | propagate_pure_const (void) |
812dbce5 JH |
1174 | { |
1175 | struct cgraph_node *node; | |
1176 | struct cgraph_node *w; | |
1177 | struct cgraph_node **order = | |
3dafb85c | 1178 | XCNEWVEC (struct cgraph_node *, symtab->cgraph_count); |
812dbce5 JH |
1179 | int order_pos; |
1180 | int i; | |
1181 | struct ipa_dfs_info * w_info; | |
17e0fc92 | 1182 | bool remove_p = false; |
812dbce5 | 1183 | |
af8bca3c | 1184 | order_pos = ipa_reduced_postorder (order, true, false, NULL); |
ea900239 DB |
1185 | if (dump_file) |
1186 | { | |
d52f5295 | 1187 | cgraph_node::dump_cgraph (dump_file); |
40a7fe1e | 1188 | ipa_print_order (dump_file, "reduced", order, order_pos); |
ea900239 DB |
1189 | } |
1190 | ||
073a8998 | 1191 | /* Propagate the local information through the call graph to produce |
ea900239 DB |
1192 | the global information. All the nodes within a cycle will have |
1193 | the same info so we collapse cycles first. Then we can do the | |
1194 | propagation in one pass from the leaves to the roots. */ | |
1195 | for (i = 0; i < order_pos; i++ ) | |
1196 | { | |
1197 | enum pure_const_state_e pure_const_state = IPA_CONST; | |
becfd6e5 | 1198 | bool looping = false; |
17541d72 | 1199 | int count = 0; |
ea900239 DB |
1200 | node = order[i]; |
1201 | ||
67348ccc | 1202 | if (node->alias) |
71fb4f92 JH |
1203 | continue; |
1204 | ||
d56026c2 JH |
1205 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1206 | fprintf (dump_file, "Starting cycle\n"); | |
1207 | ||
ea900239 DB |
1208 | /* Find the worst state for any node in the cycle. */ |
1209 | w = node; | |
f10ea640 | 1210 | while (w && pure_const_state != IPA_NEITHER) |
ea900239 | 1211 | { |
33977f81 | 1212 | struct cgraph_edge *e; |
f10ea640 JH |
1213 | struct cgraph_edge *ie; |
1214 | int i; | |
d122681a | 1215 | struct ipa_ref *ref = NULL; |
f10ea640 | 1216 | |
ea900239 | 1217 | funct_state w_l = get_function_state (w); |
d56026c2 JH |
1218 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1219 | fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n", | |
fec39fa6 | 1220 | w->name (), |
67348ccc | 1221 | w->order, |
d56026c2 JH |
1222 | pure_const_names[w_l->pure_const_state], |
1223 | w_l->looping); | |
ea900239 | 1224 | |
f10ea640 JH |
1225 | /* First merge in function body properties. */ |
1226 | worse_state (&pure_const_state, &looping, | |
1227 | w_l->pure_const_state, w_l->looping); | |
1228 | if (pure_const_state == IPA_NEITHER) | |
1229 | break; | |
1230 | ||
1231 | /* For overwritable nodes we can not assume anything. */ | |
d52f5295 | 1232 | if (w->get_availability () == AVAIL_INTERPOSABLE) |
c59f5d1b | 1233 | { |
f10ea640 JH |
1234 | worse_state (&pure_const_state, &looping, |
1235 | w_l->state_previously_known, | |
1236 | w_l->looping_previously_known); | |
d56026c2 JH |
1237 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1238 | { | |
1239 | fprintf (dump_file, | |
1240 | " Overwritable. state %s looping %i\n", | |
1241 | pure_const_names[w_l->state_previously_known], | |
1242 | w_l->looping_previously_known); | |
1243 | } | |
f10ea640 | 1244 | break; |
c59f5d1b | 1245 | } |
becfd6e5 | 1246 | |
33977f81 JH |
1247 | count++; |
1248 | ||
f10ea640 JH |
1249 | /* We consider recursive cycles as possibly infinite. |
1250 | This might be relaxed since infinite recursion leads to stack | |
1251 | overflow. */ | |
33977f81 JH |
1252 | if (count > 1) |
1253 | looping = true; | |
b8698a0f | 1254 | |
f10ea640 | 1255 | /* Now walk the edges and merge in callee properties. */ |
b8698a0f | 1256 | for (e = w->callees; e; e = e->next_callee) |
ea900239 | 1257 | { |
fede8efa | 1258 | enum availability avail; |
6cbde2e3 BE |
1259 | struct cgraph_node *y = e->callee-> |
1260 | function_or_virtual_thunk_symbol (&avail); | |
d56026c2 JH |
1261 | enum pure_const_state_e edge_state = IPA_CONST; |
1262 | bool edge_looping = false; | |
17541d72 | 1263 | |
d56026c2 JH |
1264 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1265 | { | |
1266 | fprintf (dump_file, | |
1267 | " Call to %s/%i", | |
fec39fa6 | 1268 | e->callee->name (), |
67348ccc | 1269 | e->callee->order); |
d56026c2 | 1270 | } |
d52f5295 | 1271 | if (avail > AVAIL_INTERPOSABLE) |
ea900239 | 1272 | { |
33977f81 | 1273 | funct_state y_l = get_function_state (y); |
d56026c2 JH |
1274 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1275 | { | |
1276 | fprintf (dump_file, | |
1277 | " state:%s looping:%i\n", | |
1278 | pure_const_names[y_l->pure_const_state], | |
1279 | y_l->looping); | |
1280 | } | |
da8c7675 | 1281 | if (y_l->pure_const_state > IPA_PURE |
3dafb85c | 1282 | && e->cannot_lead_to_return_p ()) |
d56026c2 JH |
1283 | { |
1284 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
f10ea640 JH |
1285 | fprintf (dump_file, |
1286 | " Ignoring side effects" | |
1287 | " -> pure, looping\n"); | |
d56026c2 JH |
1288 | edge_state = IPA_PURE; |
1289 | edge_looping = true; | |
1290 | } | |
1291 | else | |
1292 | { | |
1293 | edge_state = y_l->pure_const_state; | |
1294 | edge_looping = y_l->looping; | |
1295 | } | |
ea900239 | 1296 | } |
9e97ff61 | 1297 | else if (special_builtin_state (&edge_state, &edge_looping, |
67348ccc | 1298 | y->decl)) |
0a42aa4e | 1299 | ; |
c59f5d1b | 1300 | else |
f10ea640 | 1301 | state_from_flags (&edge_state, &edge_looping, |
67348ccc | 1302 | flags_from_decl_or_type (y->decl), |
3dafb85c | 1303 | e->cannot_lead_to_return_p ()); |
f10ea640 JH |
1304 | |
1305 | /* Merge the results with what we already know. */ | |
1306 | better_state (&edge_state, &edge_looping, | |
1307 | w_l->state_previously_known, | |
1308 | w_l->looping_previously_known); | |
1309 | worse_state (&pure_const_state, &looping, | |
1310 | edge_state, edge_looping); | |
1311 | if (pure_const_state == IPA_NEITHER) | |
1312 | break; | |
1313 | } | |
1314 | if (pure_const_state == IPA_NEITHER) | |
1315 | break; | |
c59f5d1b | 1316 | |
f10ea640 | 1317 | /* Now process the indirect call. */ |
61e374ab | 1318 | for (ie = w->indirect_calls; ie; ie = ie->next_callee) |
f10ea640 JH |
1319 | { |
1320 | enum pure_const_state_e edge_state = IPA_CONST; | |
1321 | bool edge_looping = false; | |
da8c7675 | 1322 | |
f10ea640 JH |
1323 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1324 | fprintf (dump_file, " Indirect call"); | |
1325 | state_from_flags (&edge_state, &edge_looping, | |
1326 | ie->indirect_info->ecf_flags, | |
3dafb85c | 1327 | ie->cannot_lead_to_return_p ()); |
f10ea640 JH |
1328 | /* Merge the results with what we already know. */ |
1329 | better_state (&edge_state, &edge_looping, | |
1330 | w_l->state_previously_known, | |
1331 | w_l->looping_previously_known); | |
1332 | worse_state (&pure_const_state, &looping, | |
1333 | edge_state, edge_looping); | |
d56026c2 JH |
1334 | if (pure_const_state == IPA_NEITHER) |
1335 | break; | |
ea900239 | 1336 | } |
f10ea640 JH |
1337 | if (pure_const_state == IPA_NEITHER) |
1338 | break; | |
1339 | ||
1340 | /* And finally all loads and stores. */ | |
d122681a | 1341 | for (i = 0; w->iterate_reference (i, ref); i++) |
f10ea640 JH |
1342 | { |
1343 | enum pure_const_state_e ref_state = IPA_CONST; | |
1344 | bool ref_looping = false; | |
1345 | switch (ref->use) | |
1346 | { | |
1347 | case IPA_REF_LOAD: | |
1348 | /* readonly reads are safe. */ | |
d122681a | 1349 | if (TREE_READONLY (ref->referred->decl)) |
f10ea640 JH |
1350 | break; |
1351 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1352 | fprintf (dump_file, " nonreadonly global var read\n"); | |
1353 | ref_state = IPA_PURE; | |
1354 | break; | |
1355 | case IPA_REF_STORE: | |
d122681a | 1356 | if (ref->cannot_lead_to_return ()) |
f10ea640 JH |
1357 | break; |
1358 | ref_state = IPA_NEITHER; | |
1359 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1360 | fprintf (dump_file, " global var write\n"); | |
1361 | break; | |
1362 | case IPA_REF_ADDR: | |
d5e254e1 | 1363 | case IPA_REF_CHKP: |
f10ea640 | 1364 | break; |
7d2268ea MJ |
1365 | default: |
1366 | gcc_unreachable (); | |
f10ea640 JH |
1367 | } |
1368 | better_state (&ref_state, &ref_looping, | |
1369 | w_l->state_previously_known, | |
1370 | w_l->looping_previously_known); | |
1371 | worse_state (&pure_const_state, &looping, | |
1372 | ref_state, ref_looping); | |
1373 | if (pure_const_state == IPA_NEITHER) | |
1374 | break; | |
1375 | } | |
67348ccc | 1376 | w_info = (struct ipa_dfs_info *) w->aux; |
ea900239 DB |
1377 | w = w_info->next_cycle; |
1378 | } | |
d56026c2 JH |
1379 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1380 | fprintf (dump_file, "Result %s looping %i\n", | |
1381 | pure_const_names [pure_const_state], | |
1382 | looping); | |
ea900239 | 1383 | |
8413ca87 JJ |
1384 | /* Find the worst state of can_free for any node in the cycle. */ |
1385 | bool can_free = false; | |
1386 | w = node; | |
1387 | while (w && !can_free) | |
1388 | { | |
1389 | struct cgraph_edge *e; | |
1390 | funct_state w_l = get_function_state (w); | |
1391 | ||
1392 | if (w_l->can_free | |
1393 | || w->get_availability () == AVAIL_INTERPOSABLE | |
1394 | || w->indirect_calls) | |
1395 | can_free = true; | |
1396 | ||
1397 | for (e = w->callees; e && !can_free; e = e->next_callee) | |
1398 | { | |
1399 | enum availability avail; | |
6cbde2e3 BE |
1400 | struct cgraph_node *y = e->callee-> |
1401 | function_or_virtual_thunk_symbol (&avail); | |
8413ca87 JJ |
1402 | |
1403 | if (avail > AVAIL_INTERPOSABLE) | |
1404 | can_free = get_function_state (y)->can_free; | |
1405 | else | |
1406 | can_free = true; | |
1407 | } | |
1408 | w_info = (struct ipa_dfs_info *) w->aux; | |
1409 | w = w_info->next_cycle; | |
1410 | } | |
1411 | ||
ea900239 DB |
1412 | /* Copy back the region's pure_const_state which is shared by |
1413 | all nodes in the region. */ | |
1414 | w = node; | |
1415 | while (w) | |
1416 | { | |
1417 | funct_state w_l = get_function_state (w); | |
33977f81 JH |
1418 | enum pure_const_state_e this_state = pure_const_state; |
1419 | bool this_looping = looping; | |
ea900239 | 1420 | |
8413ca87 JJ |
1421 | w_l->can_free = can_free; |
1422 | w->nonfreeing_fn = !can_free; | |
1423 | if (!can_free && dump_file) | |
1424 | fprintf (dump_file, "Function found not to call free: %s\n", | |
1425 | w->name ()); | |
1426 | ||
f87c9042 JH |
1427 | if (w_l->state_previously_known != IPA_NEITHER |
1428 | && this_state > w_l->state_previously_known) | |
da8c7675 JH |
1429 | { |
1430 | this_state = w_l->state_previously_known; | |
1431 | this_looping |= w_l->looping_previously_known; | |
1432 | } | |
03ec7d01 JH |
1433 | if (!this_looping && self_recursive_p (w)) |
1434 | this_looping = true; | |
f87c9042 JH |
1435 | if (!w_l->looping_previously_known) |
1436 | this_looping = false; | |
812dbce5 | 1437 | |
33977f81 JH |
1438 | /* All nodes within a cycle share the same info. */ |
1439 | w_l->pure_const_state = this_state; | |
1440 | w_l->looping = this_looping; | |
1441 | ||
d7636f56 JH |
1442 | /* Inline clones share declaration with their offline copies; |
1443 | do not modify their declarations since the offline copy may | |
1444 | be different. */ | |
1445 | if (!w->global.inlined_to) | |
1446 | switch (this_state) | |
1447 | { | |
1448 | case IPA_CONST: | |
1449 | if (!TREE_READONLY (w->decl)) | |
1450 | { | |
1451 | warn_function_const (w->decl, !this_looping); | |
1452 | if (dump_file) | |
1453 | fprintf (dump_file, "Function found to be %sconst: %s\n", | |
1454 | this_looping ? "looping " : "", | |
1455 | w->name ()); | |
1456 | } | |
17e0fc92 JH |
1457 | remove_p |= w->call_for_symbol_and_aliases (cdtor_p, |
1458 | NULL, true); | |
d52f5295 | 1459 | w->set_const_flag (true, this_looping); |
d7636f56 | 1460 | break; |
b8698a0f | 1461 | |
d7636f56 JH |
1462 | case IPA_PURE: |
1463 | if (!DECL_PURE_P (w->decl)) | |
1464 | { | |
1465 | warn_function_pure (w->decl, !this_looping); | |
1466 | if (dump_file) | |
1467 | fprintf (dump_file, "Function found to be %spure: %s\n", | |
1468 | this_looping ? "looping " : "", | |
1469 | w->name ()); | |
1470 | } | |
17e0fc92 JH |
1471 | remove_p |= w->call_for_symbol_and_aliases (cdtor_p, |
1472 | NULL, true); | |
d52f5295 | 1473 | w->set_pure_flag (true, this_looping); |
d7636f56 | 1474 | break; |
b8698a0f | 1475 | |
d7636f56 JH |
1476 | default: |
1477 | break; | |
1478 | } | |
67348ccc | 1479 | w_info = (struct ipa_dfs_info *) w->aux; |
2505c5ed JH |
1480 | w = w_info->next_cycle; |
1481 | } | |
1482 | } | |
1483 | ||
af8bca3c | 1484 | ipa_free_postorder_info (); |
15e80fc3 | 1485 | free (order); |
17e0fc92 | 1486 | return remove_p; |
15e80fc3 JH |
1487 | } |
1488 | ||
1489 | /* Produce transitive closure over the callgraph and compute nothrow | |
1490 | attributes. */ | |
1491 | ||
1492 | static void | |
1493 | propagate_nothrow (void) | |
1494 | { | |
1495 | struct cgraph_node *node; | |
1496 | struct cgraph_node *w; | |
1497 | struct cgraph_node **order = | |
3dafb85c | 1498 | XCNEWVEC (struct cgraph_node *, symtab->cgraph_count); |
15e80fc3 JH |
1499 | int order_pos; |
1500 | int i; | |
1501 | struct ipa_dfs_info * w_info; | |
1502 | ||
af8bca3c | 1503 | order_pos = ipa_reduced_postorder (order, true, false, ignore_edge); |
2505c5ed JH |
1504 | if (dump_file) |
1505 | { | |
d52f5295 | 1506 | cgraph_node::dump_cgraph (dump_file); |
af8bca3c | 1507 | ipa_print_order (dump_file, "reduced for nothrow", order, order_pos); |
2505c5ed | 1508 | } |
15e80fc3 | 1509 | |
073a8998 | 1510 | /* Propagate the local information through the call graph to produce |
2505c5ed JH |
1511 | the global information. All the nodes within a cycle will have |
1512 | the same info so we collapse cycles first. Then we can do the | |
1513 | propagation in one pass from the leaves to the roots. */ | |
1514 | for (i = 0; i < order_pos; i++ ) | |
1515 | { | |
1516 | bool can_throw = false; | |
1517 | node = order[i]; | |
1518 | ||
67348ccc | 1519 | if (node->alias) |
71fb4f92 JH |
1520 | continue; |
1521 | ||
2505c5ed JH |
1522 | /* Find the worst state for any node in the cycle. */ |
1523 | w = node; | |
abb50207 | 1524 | while (w && !can_throw) |
2505c5ed | 1525 | { |
f10ea640 | 1526 | struct cgraph_edge *e, *ie; |
2505c5ed JH |
1527 | funct_state w_l = get_function_state (w); |
1528 | ||
c59f5d1b | 1529 | if (w_l->can_throw |
d52f5295 | 1530 | || w->get_availability () == AVAIL_INTERPOSABLE) |
2505c5ed JH |
1531 | can_throw = true; |
1532 | ||
abb50207 | 1533 | for (e = w->callees; e && !can_throw; e = e->next_callee) |
2505c5ed | 1534 | { |
fede8efa | 1535 | enum availability avail; |
6cbde2e3 BE |
1536 | struct cgraph_node *y = e->callee-> |
1537 | function_or_virtual_thunk_symbol (&avail); | |
2505c5ed | 1538 | |
d52f5295 | 1539 | if (avail > AVAIL_INTERPOSABLE) |
2505c5ed JH |
1540 | { |
1541 | funct_state y_l = get_function_state (y); | |
1542 | ||
67348ccc | 1543 | if (y_l->can_throw && !TREE_NOTHROW (w->decl) |
2505c5ed JH |
1544 | && e->can_throw_external) |
1545 | can_throw = true; | |
1546 | } | |
67348ccc | 1547 | else if (e->can_throw_external && !TREE_NOTHROW (y->decl)) |
c59f5d1b | 1548 | can_throw = true; |
2505c5ed | 1549 | } |
abb50207 | 1550 | for (ie = w->indirect_calls; ie && !can_throw; ie = ie->next_callee) |
f10ea640 | 1551 | if (ie->can_throw_external) |
abb50207 | 1552 | can_throw = true; |
67348ccc | 1553 | w_info = (struct ipa_dfs_info *) w->aux; |
2505c5ed JH |
1554 | w = w_info->next_cycle; |
1555 | } | |
1556 | ||
1557 | /* Copy back the region's pure_const_state which is shared by | |
1558 | all nodes in the region. */ | |
1559 | w = node; | |
1560 | while (w) | |
1561 | { | |
b6fa5b01 | 1562 | funct_state w_l = get_function_state (w); |
67348ccc | 1563 | if (!can_throw && !TREE_NOTHROW (w->decl)) |
33977f81 | 1564 | { |
d7636f56 JH |
1565 | /* Inline clones share declaration with their offline copies; |
1566 | do not modify their declarations since the offline copy may | |
1567 | be different. */ | |
1568 | if (!w->global.inlined_to) | |
1569 | { | |
d52f5295 | 1570 | w->set_nothrow_flag (true); |
d7636f56 JH |
1571 | if (dump_file) |
1572 | fprintf (dump_file, "Function found to be nothrow: %s\n", | |
1573 | w->name ()); | |
1574 | } | |
ea900239 | 1575 | } |
67348ccc | 1576 | else if (can_throw && !TREE_NOTHROW (w->decl)) |
b6fa5b01 | 1577 | w_l->can_throw = true; |
67348ccc | 1578 | w_info = (struct ipa_dfs_info *) w->aux; |
ea900239 DB |
1579 | w = w_info->next_cycle; |
1580 | } | |
1581 | } | |
1582 | ||
af8bca3c | 1583 | ipa_free_postorder_info (); |
ea900239 | 1584 | free (order); |
15e80fc3 JH |
1585 | } |
1586 | ||
1587 | ||
1588 | /* Produce the global information by preforming a transitive closure | |
1589 | on the local information that was produced by generate_summary. */ | |
1590 | ||
3edf64aa DM |
1591 | unsigned int |
1592 | pass_ipa_pure_const:: | |
1593 | execute (function *) | |
15e80fc3 JH |
1594 | { |
1595 | struct cgraph_node *node; | |
17e0fc92 | 1596 | bool remove_p; |
15e80fc3 | 1597 | |
3dafb85c ML |
1598 | symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder); |
1599 | symtab->remove_cgraph_duplication_hook (node_duplication_hook_holder); | |
1600 | symtab->remove_cgraph_removal_hook (node_removal_hook_holder); | |
15e80fc3 JH |
1601 | |
1602 | /* Nothrow makes more function to not lead to return and improve | |
1603 | later analysis. */ | |
1604 | propagate_nothrow (); | |
17e0fc92 | 1605 | remove_p = propagate_pure_const (); |
15e80fc3 JH |
1606 | |
1607 | /* Cleanup. */ | |
307f83a3 | 1608 | FOR_EACH_FUNCTION (node) |
15e80fc3 JH |
1609 | if (has_function_state (node)) |
1610 | free (get_function_state (node)); | |
9771b263 | 1611 | funct_state_vec.release (); |
17e0fc92 | 1612 | return remove_p ? TODO_remove_functions : 0; |
ea900239 DB |
1613 | } |
1614 | ||
1615 | static bool | |
1616 | gate_pure_const (void) | |
1617 | { | |
2bf86c84 | 1618 | return flag_ipa_pure_const || in_lto_p; |
ea900239 DB |
1619 | } |
1620 | ||
3edf64aa DM |
1621 | pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt) |
1622 | : ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt, | |
1623 | pure_const_generate_summary, /* generate_summary */ | |
1624 | pure_const_write_summary, /* write_summary */ | |
1625 | pure_const_read_summary, /* read_summary */ | |
1626 | NULL, /* write_optimization_summary */ | |
1627 | NULL, /* read_optimization_summary */ | |
1628 | NULL, /* stmt_fixup */ | |
1629 | 0, /* function_transform_todo_flags_start */ | |
1630 | NULL, /* function_transform */ | |
1631 | NULL), /* variable_transform */ | |
1632 | init_p(false), | |
1633 | function_insertion_hook_holder(NULL), | |
1634 | node_duplication_hook_holder(NULL), | |
1635 | node_removal_hook_holder(NULL) | |
ea900239 | 1636 | { |
3edf64aa | 1637 | } |
27a4cd48 DM |
1638 | |
1639 | ipa_opt_pass_d * | |
1640 | make_pass_ipa_pure_const (gcc::context *ctxt) | |
1641 | { | |
1642 | return new pass_ipa_pure_const (ctxt); | |
1643 | } | |
1644 | ||
5dc16b19 | 1645 | /* Return true if function should be skipped for local pure const analysis. */ |
33977f81 | 1646 | |
5dc16b19 MLI |
1647 | static bool |
1648 | skip_function_for_local_pure_const (struct cgraph_node *node) | |
33977f81 | 1649 | { |
33977f81 JH |
1650 | /* Because we do not schedule pass_fixup_cfg over whole program after early optimizations |
1651 | we must not promote functions that are called by already processed functions. */ | |
1652 | ||
1653 | if (function_called_by_processed_nodes_p ()) | |
1654 | { | |
1655 | if (dump_file) | |
1656 | fprintf (dump_file, "Function called in recursive cycle; ignoring\n"); | |
5dc16b19 | 1657 | return true; |
33977f81 | 1658 | } |
d52f5295 | 1659 | if (node->get_availability () <= AVAIL_INTERPOSABLE) |
33977f81 JH |
1660 | { |
1661 | if (dump_file) | |
61502ca8 | 1662 | fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n"); |
5dc16b19 | 1663 | return true; |
33977f81 | 1664 | } |
5dc16b19 MLI |
1665 | return false; |
1666 | } | |
1667 | ||
1668 | /* Simple local pass for pure const discovery reusing the analysis from | |
1669 | ipa_pure_const. This pass is effective when executed together with | |
1670 | other optimization passes in early optimization pass queue. */ | |
33977f81 | 1671 | |
17795822 TS |
1672 | namespace { |
1673 | ||
1674 | const pass_data pass_data_local_pure_const = | |
be55bfe6 TS |
1675 | { |
1676 | GIMPLE_PASS, /* type */ | |
1677 | "local-pure-const", /* name */ | |
1678 | OPTGROUP_NONE, /* optinfo_flags */ | |
be55bfe6 TS |
1679 | TV_IPA_PURE_CONST, /* tv_id */ |
1680 | 0, /* properties_required */ | |
1681 | 0, /* properties_provided */ | |
1682 | 0, /* properties_destroyed */ | |
1683 | 0, /* todo_flags_start */ | |
1684 | 0, /* todo_flags_finish */ | |
1685 | }; | |
1686 | ||
17795822 | 1687 | class pass_local_pure_const : public gimple_opt_pass |
be55bfe6 TS |
1688 | { |
1689 | public: | |
1690 | pass_local_pure_const (gcc::context *ctxt) | |
1691 | : gimple_opt_pass (pass_data_local_pure_const, ctxt) | |
1692 | {} | |
1693 | ||
1694 | /* opt_pass methods: */ | |
1695 | opt_pass * clone () { return new pass_local_pure_const (m_ctxt); } | |
1696 | virtual bool gate (function *) { return gate_pure_const (); } | |
1697 | virtual unsigned int execute (function *); | |
1698 | ||
1699 | }; // class pass_local_pure_const | |
1700 | ||
1701 | unsigned int | |
1702 | pass_local_pure_const::execute (function *fun) | |
5dc16b19 MLI |
1703 | { |
1704 | bool changed = false; | |
1705 | funct_state l; | |
1706 | bool skip; | |
1707 | struct cgraph_node *node; | |
1708 | ||
d52f5295 | 1709 | node = cgraph_node::get (current_function_decl); |
5dc16b19 MLI |
1710 | skip = skip_function_for_local_pure_const (node); |
1711 | if (!warn_suggest_attribute_const | |
1712 | && !warn_suggest_attribute_pure | |
1713 | && skip) | |
1714 | return 0; | |
73add7fe | 1715 | |
269c80f2 JJ |
1716 | l = analyze_function (node, false); |
1717 | ||
1718 | /* Do NORETURN discovery. */ | |
73add7fe | 1719 | if (!skip && !TREE_THIS_VOLATILE (current_function_decl) |
be55bfe6 | 1720 | && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0) |
73add7fe | 1721 | { |
be55bfe6 | 1722 | warn_function_noreturn (fun->decl); |
73add7fe | 1723 | if (dump_file) |
be55bfe6 TS |
1724 | fprintf (dump_file, "Function found to be noreturn: %s\n", |
1725 | current_function_name ()); | |
73add7fe JH |
1726 | |
1727 | /* Update declaration and reduce profile to executed once. */ | |
1728 | TREE_THIS_VOLATILE (current_function_decl) = 1; | |
1729 | if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE) | |
be55bfe6 | 1730 | node->frequency = NODE_FREQUENCY_EXECUTED_ONCE; |
73add7fe JH |
1731 | |
1732 | changed = true; | |
1733 | } | |
c59f5d1b | 1734 | |
33977f81 JH |
1735 | switch (l->pure_const_state) |
1736 | { | |
1737 | case IPA_CONST: | |
1738 | if (!TREE_READONLY (current_function_decl)) | |
1739 | { | |
5dc16b19 MLI |
1740 | warn_function_const (current_function_decl, !l->looping); |
1741 | if (!skip) | |
1742 | { | |
d52f5295 | 1743 | node->set_const_flag (true, l->looping); |
5dc16b19 MLI |
1744 | changed = true; |
1745 | } | |
33977f81 JH |
1746 | if (dump_file) |
1747 | fprintf (dump_file, "Function found to be %sconst: %s\n", | |
1748 | l->looping ? "looping " : "", | |
df92c640 | 1749 | current_function_name ()); |
33977f81 JH |
1750 | } |
1751 | else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl) | |
1752 | && !l->looping) | |
1753 | { | |
5dc16b19 MLI |
1754 | if (!skip) |
1755 | { | |
d52f5295 | 1756 | node->set_const_flag (true, false); |
5dc16b19 MLI |
1757 | changed = true; |
1758 | } | |
33977f81 JH |
1759 | if (dump_file) |
1760 | fprintf (dump_file, "Function found to be non-looping: %s\n", | |
df92c640 | 1761 | current_function_name ()); |
33977f81 JH |
1762 | } |
1763 | break; | |
1764 | ||
1765 | case IPA_PURE: | |
5dc16b19 | 1766 | if (!DECL_PURE_P (current_function_decl)) |
33977f81 | 1767 | { |
5dc16b19 MLI |
1768 | if (!skip) |
1769 | { | |
d52f5295 | 1770 | node->set_pure_flag (true, l->looping); |
5dc16b19 MLI |
1771 | changed = true; |
1772 | } | |
1773 | warn_function_pure (current_function_decl, !l->looping); | |
33977f81 JH |
1774 | if (dump_file) |
1775 | fprintf (dump_file, "Function found to be %spure: %s\n", | |
1776 | l->looping ? "looping " : "", | |
df92c640 | 1777 | current_function_name ()); |
33977f81 JH |
1778 | } |
1779 | else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl) | |
1780 | && !l->looping) | |
1781 | { | |
5dc16b19 MLI |
1782 | if (!skip) |
1783 | { | |
d52f5295 | 1784 | node->set_pure_flag (true, false); |
5dc16b19 MLI |
1785 | changed = true; |
1786 | } | |
33977f81 JH |
1787 | if (dump_file) |
1788 | fprintf (dump_file, "Function found to be non-looping: %s\n", | |
df92c640 | 1789 | current_function_name ()); |
33977f81 JH |
1790 | } |
1791 | break; | |
1792 | ||
1793 | default: | |
1794 | break; | |
1795 | } | |
1796 | if (!l->can_throw && !TREE_NOTHROW (current_function_decl)) | |
1797 | { | |
d52f5295 | 1798 | node->set_nothrow_flag (true); |
33977f81 JH |
1799 | changed = true; |
1800 | if (dump_file) | |
1801 | fprintf (dump_file, "Function found to be nothrow: %s\n", | |
df92c640 | 1802 | current_function_name ()); |
33977f81 | 1803 | } |
04695783 | 1804 | free (l); |
33977f81 JH |
1805 | if (changed) |
1806 | return execute_fixup_cfg (); | |
1807 | else | |
1808 | return 0; | |
1809 | } | |
1810 | ||
17795822 TS |
1811 | } // anon namespace |
1812 | ||
27a4cd48 DM |
1813 | gimple_opt_pass * |
1814 | make_pass_local_pure_const (gcc::context *ctxt) | |
1815 | { | |
1816 | return new pass_local_pure_const (ctxt); | |
1817 | } | |
c1bf2a39 AM |
1818 | |
1819 | /* Emit noreturn warnings. */ | |
1820 | ||
17795822 TS |
1821 | namespace { |
1822 | ||
1823 | const pass_data pass_data_warn_function_noreturn = | |
c1bf2a39 AM |
1824 | { |
1825 | GIMPLE_PASS, /* type */ | |
1826 | "*warn_function_noreturn", /* name */ | |
1827 | OPTGROUP_NONE, /* optinfo_flags */ | |
c1bf2a39 AM |
1828 | TV_NONE, /* tv_id */ |
1829 | PROP_cfg, /* properties_required */ | |
1830 | 0, /* properties_provided */ | |
1831 | 0, /* properties_destroyed */ | |
1832 | 0, /* todo_flags_start */ | |
1833 | 0, /* todo_flags_finish */ | |
1834 | }; | |
1835 | ||
17795822 | 1836 | class pass_warn_function_noreturn : public gimple_opt_pass |
c1bf2a39 AM |
1837 | { |
1838 | public: | |
1839 | pass_warn_function_noreturn (gcc::context *ctxt) | |
1840 | : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt) | |
1841 | {} | |
1842 | ||
1843 | /* opt_pass methods: */ | |
1a3d085c | 1844 | virtual bool gate (function *) { return warn_suggest_attribute_noreturn; } |
be55bfe6 TS |
1845 | virtual unsigned int execute (function *fun) |
1846 | { | |
1847 | if (!TREE_THIS_VOLATILE (current_function_decl) | |
1848 | && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0) | |
1849 | warn_function_noreturn (current_function_decl); | |
1850 | return 0; | |
1851 | } | |
c1bf2a39 AM |
1852 | |
1853 | }; // class pass_warn_function_noreturn | |
1854 | ||
17795822 TS |
1855 | } // anon namespace |
1856 | ||
c1bf2a39 AM |
1857 | gimple_opt_pass * |
1858 | make_pass_warn_function_noreturn (gcc::context *ctxt) | |
1859 | { | |
1860 | return new pass_warn_function_noreturn (ctxt); | |
1861 | } | |
38147a2a JH |
1862 | |
1863 | /* Simple local pass for pure const discovery reusing the analysis from | |
1864 | ipa_pure_const. This pass is effective when executed together with | |
1865 | other optimization passes in early optimization pass queue. */ | |
1866 | ||
17795822 TS |
1867 | namespace { |
1868 | ||
1869 | const pass_data pass_data_nothrow = | |
38147a2a JH |
1870 | { |
1871 | GIMPLE_PASS, /* type */ | |
1872 | "nothrow", /* name */ | |
1873 | OPTGROUP_NONE, /* optinfo_flags */ | |
1874 | TV_IPA_PURE_CONST, /* tv_id */ | |
1875 | 0, /* properties_required */ | |
1876 | 0, /* properties_provided */ | |
1877 | 0, /* properties_destroyed */ | |
1878 | 0, /* todo_flags_start */ | |
1879 | 0, /* todo_flags_finish */ | |
1880 | }; | |
1881 | ||
17795822 | 1882 | class pass_nothrow : public gimple_opt_pass |
38147a2a JH |
1883 | { |
1884 | public: | |
1885 | pass_nothrow (gcc::context *ctxt) | |
1886 | : gimple_opt_pass (pass_data_nothrow, ctxt) | |
1887 | {} | |
1888 | ||
1889 | /* opt_pass methods: */ | |
1890 | opt_pass * clone () { return new pass_nothrow (m_ctxt); } | |
1891 | virtual bool gate (function *) { return optimize; } | |
1892 | virtual unsigned int execute (function *); | |
1893 | ||
1894 | }; // class pass_nothrow | |
1895 | ||
1896 | unsigned int | |
1897 | pass_nothrow::execute (function *) | |
1898 | { | |
1899 | struct cgraph_node *node; | |
1900 | basic_block this_block; | |
1901 | ||
1902 | if (TREE_NOTHROW (current_function_decl)) | |
1903 | return 0; | |
1904 | ||
1905 | node = cgraph_node::get (current_function_decl); | |
1906 | ||
1907 | /* We run during lowering, we can not really use availability yet. */ | |
1908 | if (cgraph_node::get (current_function_decl)->get_availability () | |
1909 | <= AVAIL_INTERPOSABLE) | |
1910 | { | |
1911 | if (dump_file) | |
1912 | fprintf (dump_file, "Function is interposable;" | |
1913 | " not analyzing.\n"); | |
1914 | return true; | |
1915 | } | |
1916 | ||
1917 | FOR_EACH_BB_FN (this_block, cfun) | |
1918 | { | |
1919 | for (gimple_stmt_iterator gsi = gsi_start_bb (this_block); | |
1920 | !gsi_end_p (gsi); | |
1921 | gsi_next (&gsi)) | |
1922 | if (stmt_can_throw_external (gsi_stmt (gsi))) | |
1923 | { | |
1924 | if (is_gimple_call (gsi_stmt (gsi))) | |
1925 | { | |
1926 | tree callee_t = gimple_call_fndecl (gsi_stmt (gsi)); | |
1927 | if (callee_t && recursive_call_p (current_function_decl, | |
1928 | callee_t)) | |
1929 | continue; | |
1930 | } | |
1931 | ||
1932 | if (dump_file) | |
1933 | { | |
1934 | fprintf (dump_file, "Statement can throw: "); | |
1935 | print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0); | |
1936 | } | |
1937 | return 0; | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | node->set_nothrow_flag (true); | |
1942 | if (dump_file) | |
1943 | fprintf (dump_file, "Function found to be nothrow: %s\n", | |
1944 | current_function_name ()); | |
1945 | return 0; | |
1946 | } | |
1947 | ||
17795822 TS |
1948 | } // anon namespace |
1949 | ||
38147a2a JH |
1950 | gimple_opt_pass * |
1951 | make_pass_nothrow (gcc::context *ctxt) | |
1952 | { | |
1953 | return new pass_nothrow (ctxt); | |
1954 | } |