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