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