]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-pure-const.c
IPA REF refactoring
[thirdparty/gcc.git] / gcc / ipa-pure-const.c
CommitLineData
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
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
ea900239
DB
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along 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"
2fb9a547
AM
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"
442b4905 47#include "gimple.h"
5be5c238
AM
48#include "gimple-iterator.h"
49#include "gimple-walk.h"
442b4905 50#include "tree-cfg.h"
e28030cf 51#include "tree-ssa-loop-niter.h"
ea900239
DB
52#include "tree-inline.h"
53#include "tree-pass.h"
54#include "langhooks.h"
ea900239 55#include "ipa-utils.h"
ea900239 56#include "flags.h"
ea900239 57#include "diagnostic.h"
cf835838 58#include "gimple-pretty-print.h"
ea900239 59#include "langhooks.h"
5d35c171 60#include "target.h"
d7f09764 61#include "lto-streamer.h"
f0efc7aa
DN
62#include "data-streamer.h"
63#include "tree-streamer.h"
2de58650
JH
64#include "cfgloop.h"
65#include "tree-scalar-evolution.h"
5dc16b19
MLI
66#include "intl.h"
67#include "opts.h"
ea900239
DB
68
69static 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. */
74enum pure_const_state_e
75{
76 IPA_CONST,
77 IPA_PURE,
78 IPA_NEITHER
79};
80
d56026c2
JH
81const char *pure_const_names[3] = {"const", "pure", "neither"};
82
812dbce5
JH
83/* Holder for the const_state. There is one of these per function
84 decl. */
b8698a0f 85struct funct_state_d
ea900239 86{
812dbce5 87 /* See above. */
ea900239 88 enum pure_const_state_e pure_const_state;
33977f81 89 /* What user set here; we can be always sure about this. */
b8698a0f
L
90 enum pure_const_state_e state_previously_known;
91 bool looping_previously_known;
812dbce5
JH
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. */
becfd6e5 99 bool looping;
812dbce5 100
33977f81 101 bool can_throw;
ea900239
DB
102};
103
37512c66
JH
104/* State used when we know nothing about function. */
105static struct funct_state_d varying_state
106 = { IPA_NEITHER, IPA_NEITHER, true, true, true };
107
108
ea900239
DB
109typedef struct funct_state_d * funct_state;
110
812dbce5
JH
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
b8698a0f 113 local info. */
812dbce5
JH
114
115/* Array, indexed by cgraph node uid, of function states. */
116
9771b263 117static vec<funct_state> funct_state_vec;
812dbce5 118
129a37fc
JH
119/* Holders of ipa cgraph hooks: */
120static struct cgraph_node_hook_list *function_insertion_hook_holder;
e2c9111c
JH
121static struct cgraph_2node_hook_list *node_duplication_hook_holder;
122static struct cgraph_node_hook_list *node_removal_hook_holder;
812dbce5 123
5dc16b19
MLI
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
128static bool
129function_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
140static struct pointer_set_t *
141suggest_attribute (int option, tree decl, bool known_finite,
142 struct pointer_set_t *warned_about,
143 const char * attrib_name)
144{
46625112 145 if (!option_enabled (option, &global_options))
5dc16b19
MLI
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
168static void
169warn_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
181static void
182warn_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}
7ea6b6cf 189
c1bf2a39 190static void
7ea6b6cf
JH
191warn_function_noreturn (tree decl)
192{
193 static struct pointer_set_t *warned_about;
d45eae79
SL
194 if (!lang_hooks.missing_noreturn_ok_p (decl)
195 && targetm.warn_func_return (decl))
7ea6b6cf
JH
196 warned_about
197 = suggest_attribute (OPT_Wsuggest_attribute_noreturn, decl,
198 true, warned_about, "noreturn");
199}
df92c640 200
97d45cef
RG
201/* Return true if we have a function state for NODE. */
202
203static inline bool
204has_function_state (struct cgraph_node *node)
205{
9771b263
DN
206 if (!funct_state_vec.exists ()
207 || funct_state_vec.length () <= (unsigned int)node->uid)
97d45cef 208 return false;
9771b263 209 return funct_state_vec[node->uid] != NULL;
97d45cef
RG
210}
211
b8698a0f 212/* Return the function state from NODE. */
ea900239
DB
213
214static inline funct_state
215get_function_state (struct cgraph_node *node)
216{
9771b263
DN
217 if (!funct_state_vec.exists ()
218 || funct_state_vec.length () <= (unsigned int)node->uid
219 || !funct_state_vec[node->uid])
97d45cef 220 /* We might want to put correct previously_known state into varying. */
37512c66 221 return &varying_state;
9771b263 222 return funct_state_vec[node->uid];
812dbce5
JH
223}
224
225/* Set the function state S for NODE. */
226
227static inline void
228set_function_state (struct cgraph_node *node, funct_state s)
229{
9771b263
DN
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;
ea900239
DB
234}
235
fa10beec 236/* Check to see if the use (or definition when CHECKING_WRITE is true)
ea900239
DB
237 variable T is legal in a function that is either pure or const. */
238
b8698a0f
L
239static inline void
240check_decl (funct_state local,
f10ea640 241 tree t, bool checking_write, bool ipa)
ea900239 242{
ea900239
DB
243 /* Do not want to do anything with volatile except mark any
244 function that uses one to be not const or pure. */
b8698a0f
L
245 if (TREE_THIS_VOLATILE (t))
246 {
ea900239 247 local->pure_const_state = IPA_NEITHER;
33977f81
JH
248 if (dump_file)
249 fprintf (dump_file, " Volatile operand is not const/pure");
ea900239
DB
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
33977f81
JH
257 /* If the variable has the "used" attribute, treat it as if it had a
258 been touched by the devil. */
b42186f1 259 if (DECL_PRESERVE_P (t))
33977f81
JH
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
f10ea640
JH
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
ea900239
DB
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. */
b8698a0f 275 if (checking_write)
eb80272a
ST
276 {
277 local->pure_const_state = IPA_NEITHER;
33977f81
JH
278 if (dump_file)
279 fprintf (dump_file, " static/global memory write is not const/pure\n");
eb80272a
ST
280 return;
281 }
ea900239
DB
282
283 if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
284 {
33977f81
JH
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. */
b8698a0f 288 else
ea900239 289 {
33977f81
JH
290 if (dump_file)
291 fprintf (dump_file, " global memory read is not const\n");
ea900239
DB
292 /* Just a regular read. */
293 if (local->pure_const_state == IPA_CONST)
294 local->pure_const_state = IPA_PURE;
295 }
296 }
33977f81
JH
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 }
ea900239
DB
310}
311
ea900239 312
33977f81
JH
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. */
ea900239 315
b8698a0f 316static inline void
346ef3fa 317check_op (funct_state local, tree t, bool checking_write)
ea900239 318{
3c1832c3
JH
319 t = get_base_address (t);
320 if (t && TREE_THIS_VOLATILE (t))
ea900239 321 {
346ef3fa
RG
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 }
3c1832c3 327 else if (t
70f34814 328 && (INDIRECT_REF_P (t) || TREE_CODE (t) == MEM_REF)
3c1832c3
JH
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 }
346ef3fa
RG
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;
ea900239
DB
349 }
350}
351
f10ea640
JH
352/* compute state based on ECF FLAGS and store to STATE and LOOPING. */
353
354static void
355state_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))
df92c640 387 fprintf (dump_file, " neither\n");
f10ea640
JH
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
398static inline void
399better_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);
b0d04a5f 408 *state = state2;
f10ea640
JH
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
417static inline void
418worse_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
61502ca8 425/* Recognize special cases of builtins that are by themselves not pure or const
0a42aa4e
JH
426 but function using them is. */
427static bool
9e97ff61 428special_builtin_state (enum pure_const_state_e *state, bool *looping,
0a42aa4e
JH
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:
13e49da9 437 case BUILT_IN_ALLOCA_WITH_ALIGN:
0a42aa4e
JH
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:
0a42aa4e
JH
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
ea900239
DB
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
466static void
33977f81 467check_call (funct_state local, gimple call, bool ipa)
ea900239 468{
726a989a 469 int flags = gimple_call_flags (call);
33977f81 470 tree callee_t = gimple_call_fndecl (call);
33977f81
JH
471 bool possibly_throws = stmt_could_throw_p (call);
472 bool possibly_throws_externally = (possibly_throws
473 && stmt_can_throw_external (call));
ea900239 474
33977f81
JH
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 {
8f4f502f 482 if (possibly_throws && cfun->can_throw_non_call_exceptions)
33977f81
JH
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 }
b8698a0f 496
ea900239
DB
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
b8698a0f
L
500 them, otherwise we will compute our own information.
501
ea900239
DB
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
b8698a0f 506 graph. */
ea900239
DB
507 if (callee_t)
508 {
0a42aa4e
JH
509 enum pure_const_state_e call_state;
510 bool call_looping;
511
9e97ff61 512 if (special_builtin_state (&call_state, &call_looping, callee_t))
0a42aa4e
JH
513 {
514 worse_state (&local->pure_const_state, &local->looping,
515 call_state, call_looping);
516 return;
517 }
ea900239
DB
518 /* When bad things happen to bad functions, they cannot be const
519 or pure. */
520 if (setjmp_call_p (callee_t))
becfd6e5 521 {
33977f81
JH
522 if (dump_file)
523 fprintf (dump_file, " setjmp is not const/pure\n");
524 local->looping = true;
becfd6e5 525 local->pure_const_state = IPA_NEITHER;
becfd6e5 526 }
ea900239
DB
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:
33977f81
JH
533 if (dump_file)
534 fprintf (dump_file, " longjmp and nonlocal goto is not const/pure\n");
ea900239 535 local->pure_const_state = IPA_NEITHER;
33977f81 536 local->looping = true;
ea900239
DB
537 break;
538 default:
539 break;
540 }
541 }
542
33977f81 543 /* When not in IPA mode, we can still handle self recursion. */
fc11f321
JH
544 if (!ipa && callee_t
545 && recursive_call_p (current_function_decl, callee_t))
5dc16b19
MLI
546 {
547 if (dump_file)
548 fprintf (dump_file, " Recursive call can loop.\n");
549 local->looping = true;
550 }
61502ca8 551 /* Either callee is unknown or we are doing local analysis.
c59f5d1b
JH
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. */
f10ea640 555 else if (!ipa)
ea900239 556 {
f10ea640
JH
557 enum pure_const_state_e call_state;
558 bool call_looping;
8f4f502f 559 if (possibly_throws && cfun->can_throw_non_call_exceptions)
33977f81
JH
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 {
1d65f45c
RH
569 fprintf (dump_file, " can throw externally to lp %i\n",
570 lookup_stmt_eh_lp (call));
33977f81
JH
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 }
f10ea640
JH
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);
ea900239 585 }
33977f81 586 /* Direct functions calls are handled by IPA propagation. */
ea900239
DB
587}
588
f10ea640 589/* Wrapper around check_decl for loads in local more. */
346ef3fa
RG
590
591static bool
9f1363cd 592check_load (gimple, tree op, tree, void *data)
346ef3fa
RG
593{
594 if (DECL_P (op))
f10ea640 595 check_decl ((funct_state)data, op, false, false);
346ef3fa
RG
596 else
597 check_op ((funct_state)data, op, false);
598 return false;
599}
600
f10ea640 601/* Wrapper around check_decl for stores in local more. */
346ef3fa
RG
602
603static bool
9f1363cd 604check_store (gimple, tree op, tree, void *data)
346ef3fa
RG
605{
606 if (DECL_P (op))
f10ea640
JH
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
615static bool
9f1363cd 616check_ipa_load (gimple, tree op, tree, void *data)
f10ea640
JH
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
627static bool
9f1363cd 628check_ipa_store (gimple, tree op, tree, void *data)
f10ea640
JH
629{
630 if (DECL_P (op))
631 check_decl ((funct_state)data, op, true, true);
346ef3fa
RG
632 else
633 check_op ((funct_state)data, op, true);
634 return false;
635}
636
5006671f
RG
637/* Look into pointer pointed to by GSIP and figure out what interesting side
638 effects it has. */
33977f81
JH
639static void
640check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa)
ea900239 641{
33977f81 642 gimple stmt = gsi_stmt (*gsip);
ea900239 643
b5b8b0ac
AO
644 if (is_gimple_debug (stmt))
645 return;
646
33977f81 647 if (dump_file)
ea900239 648 {
33977f81
JH
649 fprintf (dump_file, " scanning: ");
650 print_gimple_stmt (dump_file, stmt, 0, 0);
651 }
5006671f 652
ace018f9
RG
653 if (gimple_has_volatile_ops (stmt)
654 && !gimple_clobber_p (stmt))
4e89a3fa
RG
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
346ef3fa 661 /* Look for loads and stores. */
f10ea640
JH
662 walk_stmt_load_store_ops (stmt, local,
663 ipa ? check_ipa_load : check_load,
664 ipa ? check_ipa_store : check_store);
33977f81
JH
665
666 if (gimple_code (stmt) != GIMPLE_CALL
667 && stmt_could_throw_p (stmt))
668 {
8f4f502f 669 if (cfun->can_throw_non_call_exceptions)
33977f81
JH
670 {
671 if (dump_file)
425b784f 672 fprintf (dump_file, " can throw; looping\n");
33977f81
JH
673 local->looping = true;
674 }
675 if (stmt_can_throw_external (stmt))
676 {
677 if (dump_file)
425b784f 678 fprintf (dump_file, " can throw externally\n");
33977f81
JH
679 local->can_throw = true;
680 }
425b784f
JH
681 else
682 if (dump_file)
683 fprintf (dump_file, " can throw\n");
726a989a 684 }
726a989a
RB
685 switch (gimple_code (stmt))
686 {
33977f81 687 case GIMPLE_CALL:
33977f81 688 check_call (local, stmt, ipa);
ea900239 689 break;
726a989a
RB
690 case GIMPLE_LABEL:
691 if (DECL_NONLOCAL (gimple_label_label (stmt)))
ea900239 692 /* Target of long jump. */
becfd6e5 693 {
33977f81 694 if (dump_file)
425b784f 695 fprintf (dump_file, " nonlocal label is not const/pure\n");
becfd6e5 696 local->pure_const_state = IPA_NEITHER;
becfd6e5 697 }
ea900239 698 break;
726a989a 699 case GIMPLE_ASM:
edcdea5b 700 if (gimple_asm_clobbers_memory_p (stmt))
33977f81 701 {
edcdea5b 702 if (dump_file)
425b784f 703 fprintf (dump_file, " memory asm clobber is not const/pure\n");
edcdea5b
NF
704 /* Abandon all hope, ye who enter here. */
705 local->pure_const_state = IPA_NEITHER;
33977f81
JH
706 }
707 if (gimple_asm_volatile_p (stmt))
708 {
709 if (dump_file)
425b784f 710 fprintf (dump_file, " volatile is not const/pure\n");
33977f81
JH
711 /* Abandon all hope, ye who enter here. */
712 local->pure_const_state = IPA_NEITHER;
713 local->looping = true;
714 }
715 return;
ea900239
DB
716 default:
717 break;
718 }
ea900239
DB
719}
720
812dbce5 721
ea900239
DB
722/* This is the main routine for finding the reference patterns for
723 global variables within a function FN. */
724
33977f81
JH
725static funct_state
726analyze_function (struct cgraph_node *fn, bool ipa)
ea900239 727{
67348ccc 728 tree decl = fn->decl;
33977f81
JH
729 funct_state l;
730 basic_block this_block;
ea900239 731
33977f81
JH
732 l = XCNEW (struct funct_state_d);
733 l->pure_const_state = IPA_CONST;
f87c9042
JH
734 l->state_previously_known = IPA_NEITHER;
735 l->looping_previously_known = true;
33977f81
JH
736 l->looping = false;
737 l->can_throw = false;
c47d0034 738 state_from_flags (&l->state_previously_known, &l->looping_previously_known,
67348ccc 739 flags_from_decl_or_type (fn->decl),
c47d0034
JH
740 cgraph_node_cannot_return (fn));
741
67348ccc 742 if (fn->thunk.thunk_p || fn->alias)
c47d0034
JH
743 {
744 /* Thunk gets propagated through, so nothing interesting happens. */
745 gcc_assert (ipa);
746 return l;
747 }
ea900239
DB
748
749 if (dump_file)
750 {
b8698a0f 751 fprintf (dump_file, "\n\n local analysis of %s\n ",
fec39fa6 752 fn->name ());
ea900239 753 }
b8698a0f 754
33977f81 755 push_cfun (DECL_STRUCT_FUNCTION (decl));
b8698a0f 756
11cd3bed 757 FOR_EACH_BB_FN (this_block, cfun)
ea900239 758 {
33977f81
JH
759 gimple_stmt_iterator gsi;
760 struct walk_stmt_info wi;
ea900239 761
c3284718 762 memset (&wi, 0, sizeof (wi));
33977f81
JH
763 for (gsi = gsi_start_bb (this_block);
764 !gsi_end_p (gsi);
765 gsi_next (&gsi))
ea900239 766 {
33977f81
JH
767 check_stmt (&gsi, l, ipa);
768 if (l->pure_const_state == IPA_NEITHER && l->looping && l->can_throw)
769 goto end;
ea900239
DB
770 }
771 }
13335ae6
AP
772
773end:
33977f81
JH
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 ())
2de58650 780 {
7351bcaa 781 /* Preheaders are needed for SCEV to work.
61502ca8 782 Simple latches and recorded exits improve chances that loop will
0d5049b2
RB
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
7351bcaa 787 | LOOPS_HAVE_RECORDED_EXITS);
2de58650
JH
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 }
b8698a0f 796 else
2de58650 797 {
2de58650
JH
798 struct loop *loop;
799 scev_initialize ();
f0bd40b1 800 FOR_EACH_LOOP (loop, 0)
2de58650
JH
801 if (!finite_loop_p (loop))
802 {
803 if (dump_file)
0d5049b2
RB
804 fprintf (dump_file, " can not prove finiteness of "
805 "loop %i\n", loop->num);
2de58650 806 l->looping =true;
f0bd40b1 807 break;
2de58650
JH
808 }
809 scev_finalize ();
810 }
811 loop_optimizer_finalize ();
812 }
33977f81
JH
813 }
814
f10ea640
JH
815 if (dump_file && (dump_flags & TDF_DETAILS))
816 fprintf (dump_file, " checking previously known:");
f10ea640
JH
817
818 better_state (&l->pure_const_state, &l->looping,
819 l->state_previously_known,
820 l->looping_previously_known);
33977f81
JH
821 if (TREE_NOTHROW (decl))
822 l->can_throw = false;
823
824 pop_cfun ();
13335ae6
AP
825 if (dump_file)
826 {
33977f81
JH
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");
13335ae6 835 }
33977f81 836 return l;
ea900239
DB
837}
838
129a37fc
JH
839/* Called when new function is inserted to callgraph late. */
840static void
841add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
842{
c59f5d1b 843 if (cgraph_function_body_availability (node) < AVAIL_OVERWRITABLE)
e2c9111c 844 return;
129a37fc
JH
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 ();
5dc16b19
MLI
850 if (cgraph_function_body_availability (node) > AVAIL_OVERWRITABLE)
851 set_function_state (node, analyze_function (node, true));
129a37fc
JH
852 pointer_set_destroy (visited_nodes);
853 visited_nodes = NULL;
854}
855
e2c9111c
JH
856/* Called when new clone is inserted to callgraph late. */
857
858static void
859duplicate_node_data (struct cgraph_node *src, struct cgraph_node *dst,
860 void *data ATTRIBUTE_UNUSED)
861{
97d45cef 862 if (has_function_state (src))
e2c9111c
JH
863 {
864 funct_state l = XNEW (struct funct_state_d);
97d45cef 865 gcc_assert (!has_function_state (dst));
e2c9111c
JH
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
873static void
874remove_node_data (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
875{
97d45cef 876 if (has_function_state (node))
e2c9111c 877 {
37512c66
JH
878 funct_state l = get_function_state (node);
879 if (l != &varying_state)
880 free (l);
e2c9111c
JH
881 set_function_state (node, NULL);
882 }
883}
884
ea900239 885\f
d7f09764
DN
886static void
887register_hooks (void)
ea900239 888{
d7f09764
DN
889 static bool init_p = false;
890
891 if (init_p)
892 return;
893
894 init_p = true;
ea900239 895
e2c9111c
JH
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);
129a37fc
JH
900 function_insertion_hook_holder =
901 cgraph_add_function_insertion_hook (&add_new_function, NULL);
d7f09764
DN
902}
903
904
905/* Analyze each function in the cgraph to see if it is locally PURE or
906 CONST. */
907
b8698a0f 908static void
651df1b2 909pure_const_generate_summary (void)
d7f09764
DN
910{
911 struct cgraph_node *node;
912
913 register_hooks ();
914
ea900239
DB
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
b8698a0f 921 /* Process all of the functions.
ea900239 922
c59f5d1b
JH
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
61502ca8 925 when function got cloned and the clone is AVAILABLE. */
c59f5d1b 926
65c70e6b 927 FOR_EACH_DEFINED_FUNCTION (node)
c59f5d1b 928 if (cgraph_function_body_availability (node) >= AVAIL_OVERWRITABLE)
33977f81 929 set_function_state (node, analyze_function (node, true));
ea900239
DB
930
931 pointer_set_destroy (visited_nodes);
932 visited_nodes = NULL;
812dbce5
JH
933}
934
d7f09764
DN
935
936/* Serialize the ipa info for lto. */
937
938static void
f27c1867 939pure_const_write_summary (void)
d7f09764
DN
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;
f27c1867
JH
945 lto_symtab_encoder_iterator lsei;
946 lto_symtab_encoder_t encoder;
d7f09764 947
f27c1867
JH
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))
d7f09764 952 {
f27c1867 953 node = lsei_cgraph_node (lsei);
67348ccc 954 if (node->definition && has_function_state (node))
d7f09764
DN
955 count++;
956 }
b8698a0f 957
412288f1 958 streamer_write_uhwi_stream (ob->main_stream, count);
b8698a0f 959
d7f09764 960 /* Process all of the functions. */
f27c1867
JH
961 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
962 lsei_next_function_in_partition (&lsei))
d7f09764 963 {
f27c1867 964 node = lsei_cgraph_node (lsei);
67348ccc 965 if (node->definition && has_function_state (node))
d7f09764 966 {
2465dcc2 967 struct bitpack_d bp;
d7f09764
DN
968 funct_state fs;
969 int node_ref;
7380e6ef 970 lto_symtab_encoder_t encoder;
b8698a0f 971
d7f09764
DN
972 fs = get_function_state (node);
973
7380e6ef 974 encoder = ob->decl_state->symtab_node_encoder;
67348ccc 975 node_ref = lto_symtab_encoder_encode (encoder, node);
412288f1 976 streamer_write_uhwi_stream (ob->main_stream, node_ref);
b8698a0f 977
d7f09764
DN
978 /* Note that flags will need to be read in the opposite
979 order as we are pushing the bitflags into FLAGS. */
2465dcc2
RG
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);
412288f1 986 streamer_write_bitpack (&bp);
d7f09764
DN
987 }
988 }
989
990 lto_destroy_simple_output_block (ob);
991}
992
993
994/* Deserialize the ipa info for lto. */
995
b8698a0f 996static void
d7f09764
DN
997pure_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
b8698a0f
L
1009 = lto_create_simple_input_block (file_data,
1010 LTO_section_ipa_pure_const,
d7f09764
DN
1011 &data, &len);
1012 if (ib)
1013 {
1014 unsigned int i;
412288f1 1015 unsigned int count = streamer_read_uhwi (ib);
d7f09764
DN
1016
1017 for (i = 0; i < count; i++)
1018 {
1019 unsigned int index;
1020 struct cgraph_node *node;
2465dcc2 1021 struct bitpack_d bp;
d7f09764 1022 funct_state fs;
7380e6ef 1023 lto_symtab_encoder_t encoder;
d7f09764
DN
1024
1025 fs = XCNEW (struct funct_state_d);
412288f1 1026 index = streamer_read_uhwi (ib);
7380e6ef
JH
1027 encoder = file_data->symtab_node_encoder;
1028 node = cgraph (lto_symtab_encoder_deref (encoder, index));
d7f09764
DN
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). */
412288f1 1034 bp = streamer_read_bitpack (ib);
d7f09764 1035 fs->pure_const_state
2465dcc2 1036 = (enum pure_const_state_e) bp_unpack_value (&bp, 2);
d7f09764 1037 fs->state_previously_known
2465dcc2
RG
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);
d56026c2
JH
1042 if (dump_file)
1043 {
67348ccc 1044 int flags = flags_from_decl_or_type (node->decl);
d56026c2 1045 fprintf (dump_file, "Read info for %s/%i ",
fec39fa6 1046 node->name (),
67348ccc 1047 node->order);
d56026c2
JH
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 }
d7f09764
DN
1065 }
1066
b8698a0f
L
1067 lto_destroy_simple_input_block (file_data,
1068 LTO_section_ipa_pure_const,
d7f09764
DN
1069 ib, data, len);
1070 }
1071 }
1072}
1073
1074
2505c5ed
JH
1075static bool
1076ignore_edge (struct cgraph_edge *e)
1077{
1078 return (!e->can_throw_external);
1079}
1080
fede8efa 1081/* Return true if NODE is self recursive function.
fc11f321
JH
1082 Indirectly recursive functions appears as non-trivial strongly
1083 connected components, so we need to care about self recursion
1084 only. */
03ec7d01
JH
1085
1086static bool
1087self_recursive_p (struct cgraph_node *node)
1088{
1089 struct cgraph_edge *e;
1090 for (e = node->callees; e; e = e->next_callee)
fede8efa 1091 if (cgraph_function_node (e->callee, NULL) == node)
03ec7d01
JH
1092 return true;
1093 return false;
1094}
1095
15e80fc3
JH
1096/* Produce transitive closure over the callgraph and compute pure/const
1097 attributes. */
f10ea640 1098
15e80fc3
JH
1099static void
1100propagate_pure_const (void)
812dbce5
JH
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
af8bca3c 1110 order_pos = ipa_reduced_postorder (order, true, false, NULL);
ea900239
DB
1111 if (dump_file)
1112 {
1113 dump_cgraph (dump_file);
40a7fe1e 1114 ipa_print_order (dump_file, "reduced", order, order_pos);
ea900239
DB
1115 }
1116
073a8998 1117 /* Propagate the local information through the call graph to produce
ea900239
DB
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;
becfd6e5 1124 bool looping = false;
17541d72 1125 int count = 0;
ea900239
DB
1126 node = order[i];
1127
67348ccc 1128 if (node->alias)
71fb4f92
JH
1129 continue;
1130
d56026c2
JH
1131 if (dump_file && (dump_flags & TDF_DETAILS))
1132 fprintf (dump_file, "Starting cycle\n");
1133
ea900239
DB
1134 /* Find the worst state for any node in the cycle. */
1135 w = node;
f10ea640 1136 while (w && pure_const_state != IPA_NEITHER)
ea900239 1137 {
33977f81 1138 struct cgraph_edge *e;
f10ea640
JH
1139 struct cgraph_edge *ie;
1140 int i;
d122681a 1141 struct ipa_ref *ref = NULL;
f10ea640 1142
ea900239 1143 funct_state w_l = get_function_state (w);
d56026c2
JH
1144 if (dump_file && (dump_flags & TDF_DETAILS))
1145 fprintf (dump_file, " Visiting %s/%i state:%s looping %i\n",
fec39fa6 1146 w->name (),
67348ccc 1147 w->order,
d56026c2
JH
1148 pure_const_names[w_l->pure_const_state],
1149 w_l->looping);
ea900239 1150
f10ea640
JH
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. */
c59f5d1b
JH
1158 if (cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
1159 {
f10ea640
JH
1160 worse_state (&pure_const_state, &looping,
1161 w_l->state_previously_known,
1162 w_l->looping_previously_known);
d56026c2
JH
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 }
f10ea640 1170 break;
c59f5d1b 1171 }
becfd6e5 1172
33977f81
JH
1173 count++;
1174
f10ea640
JH
1175 /* We consider recursive cycles as possibly infinite.
1176 This might be relaxed since infinite recursion leads to stack
1177 overflow. */
33977f81
JH
1178 if (count > 1)
1179 looping = true;
b8698a0f 1180
f10ea640 1181 /* Now walk the edges and merge in callee properties. */
b8698a0f 1182 for (e = w->callees; e; e = e->next_callee)
ea900239 1183 {
fede8efa
JH
1184 enum availability avail;
1185 struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
d56026c2
JH
1186 enum pure_const_state_e edge_state = IPA_CONST;
1187 bool edge_looping = false;
17541d72 1188
d56026c2
JH
1189 if (dump_file && (dump_flags & TDF_DETAILS))
1190 {
1191 fprintf (dump_file,
1192 " Call to %s/%i",
fec39fa6 1193 e->callee->name (),
67348ccc 1194 e->callee->order);
d56026c2 1195 }
fede8efa 1196 if (avail > AVAIL_OVERWRITABLE)
ea900239 1197 {
33977f81 1198 funct_state y_l = get_function_state (y);
d56026c2
JH
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 }
da8c7675 1206 if (y_l->pure_const_state > IPA_PURE
97d45cef 1207 && cgraph_edge_cannot_lead_to_return (e))
d56026c2
JH
1208 {
1209 if (dump_file && (dump_flags & TDF_DETAILS))
f10ea640
JH
1210 fprintf (dump_file,
1211 " Ignoring side effects"
1212 " -> pure, looping\n");
d56026c2
JH
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 }
ea900239 1221 }
9e97ff61 1222 else if (special_builtin_state (&edge_state, &edge_looping,
67348ccc 1223 y->decl))
0a42aa4e 1224 ;
c59f5d1b 1225 else
f10ea640 1226 state_from_flags (&edge_state, &edge_looping,
67348ccc 1227 flags_from_decl_or_type (y->decl),
0a42aa4e 1228 cgraph_edge_cannot_lead_to_return (e));
f10ea640
JH
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;
c59f5d1b 1241
f10ea640 1242 /* Now process the indirect call. */
61e374ab 1243 for (ie = w->indirect_calls; ie; ie = ie->next_callee)
f10ea640
JH
1244 {
1245 enum pure_const_state_e edge_state = IPA_CONST;
1246 bool edge_looping = false;
da8c7675 1247
f10ea640
JH
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);
d56026c2
JH
1259 if (pure_const_state == IPA_NEITHER)
1260 break;
ea900239 1261 }
f10ea640
JH
1262 if (pure_const_state == IPA_NEITHER)
1263 break;
1264
1265 /* And finally all loads and stores. */
d122681a 1266 for (i = 0; w->iterate_reference (i, ref); i++)
f10ea640
JH
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. */
d122681a 1274 if (TREE_READONLY (ref->referred->decl))
f10ea640
JH
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:
d122681a 1281 if (ref->cannot_lead_to_return ())
f10ea640
JH
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;
7d2268ea
MJ
1289 default:
1290 gcc_unreachable ();
f10ea640
JH
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 }
67348ccc 1300 w_info = (struct ipa_dfs_info *) w->aux;
ea900239
DB
1301 w = w_info->next_cycle;
1302 }
d56026c2
JH
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);
ea900239
DB
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);
33977f81
JH
1314 enum pure_const_state_e this_state = pure_const_state;
1315 bool this_looping = looping;
ea900239 1316
f87c9042
JH
1317 if (w_l->state_previously_known != IPA_NEITHER
1318 && this_state > w_l->state_previously_known)
da8c7675
JH
1319 {
1320 this_state = w_l->state_previously_known;
1321 this_looping |= w_l->looping_previously_known;
1322 }
03ec7d01
JH
1323 if (!this_looping && self_recursive_p (w))
1324 this_looping = true;
f87c9042
JH
1325 if (!w_l->looping_previously_known)
1326 this_looping = false;
812dbce5 1327
33977f81
JH
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
d7636f56
JH
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;
b8698a0f 1349
d7636f56
JH
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;
b8698a0f 1361
d7636f56
JH
1362 default:
1363 break;
1364 }
67348ccc 1365 w_info = (struct ipa_dfs_info *) w->aux;
2505c5ed
JH
1366 w = w_info->next_cycle;
1367 }
1368 }
1369
af8bca3c 1370 ipa_free_postorder_info ();
15e80fc3
JH
1371 free (order);
1372}
1373
1374/* Produce transitive closure over the callgraph and compute nothrow
1375 attributes. */
1376
1377static void
1378propagate_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
af8bca3c 1388 order_pos = ipa_reduced_postorder (order, true, false, ignore_edge);
2505c5ed
JH
1389 if (dump_file)
1390 {
1391 dump_cgraph (dump_file);
af8bca3c 1392 ipa_print_order (dump_file, "reduced for nothrow", order, order_pos);
2505c5ed 1393 }
15e80fc3 1394
073a8998 1395 /* Propagate the local information through the call graph to produce
2505c5ed
JH
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
67348ccc 1404 if (node->alias)
71fb4f92
JH
1405 continue;
1406
2505c5ed
JH
1407 /* Find the worst state for any node in the cycle. */
1408 w = node;
1409 while (w)
1410 {
f10ea640 1411 struct cgraph_edge *e, *ie;
2505c5ed
JH
1412 funct_state w_l = get_function_state (w);
1413
c59f5d1b
JH
1414 if (w_l->can_throw
1415 || cgraph_function_body_availability (w) == AVAIL_OVERWRITABLE)
2505c5ed
JH
1416 can_throw = true;
1417
1418 if (can_throw)
1419 break;
b8698a0f
L
1420
1421 for (e = w->callees; e; e = e->next_callee)
2505c5ed 1422 {
fede8efa
JH
1423 enum availability avail;
1424 struct cgraph_node *y = cgraph_function_node (e->callee, &avail);
2505c5ed 1425
fede8efa 1426 if (avail > AVAIL_OVERWRITABLE)
2505c5ed
JH
1427 {
1428 funct_state y_l = get_function_state (y);
1429
b8698a0f 1430 if (can_throw)
2505c5ed 1431 break;
67348ccc 1432 if (y_l->can_throw && !TREE_NOTHROW (w->decl)
2505c5ed
JH
1433 && e->can_throw_external)
1434 can_throw = true;
1435 }
67348ccc 1436 else if (e->can_throw_external && !TREE_NOTHROW (y->decl))
c59f5d1b 1437 can_throw = true;
2505c5ed 1438 }
f10ea640
JH
1439 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1440 if (ie->can_throw_external)
ae382ebd
PCC
1441 {
1442 can_throw = true;
1443 break;
1444 }
67348ccc 1445 w_info = (struct ipa_dfs_info *) w->aux;
2505c5ed
JH
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 {
b6fa5b01 1454 funct_state w_l = get_function_state (w);
67348ccc 1455 if (!can_throw && !TREE_NOTHROW (w->decl))
33977f81 1456 {
d7636f56
JH
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 }
ea900239 1467 }
67348ccc 1468 else if (can_throw && !TREE_NOTHROW (w->decl))
b6fa5b01 1469 w_l->can_throw = true;
67348ccc 1470 w_info = (struct ipa_dfs_info *) w->aux;
ea900239
DB
1471 w = w_info->next_cycle;
1472 }
1473 }
1474
af8bca3c 1475 ipa_free_postorder_info ();
ea900239 1476 free (order);
15e80fc3
JH
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
1483static unsigned int
1484propagate (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. */
307f83a3 1498 FOR_EACH_FUNCTION (node)
15e80fc3
JH
1499 if (has_function_state (node))
1500 free (get_function_state (node));
9771b263 1501 funct_state_vec.release ();
c2924966 1502 return 0;
ea900239
DB
1503}
1504
1505static bool
1506gate_pure_const (void)
1507{
e2c9111c 1508 return (flag_ipa_pure_const
ea900239 1509 /* Don't bother doing anything if the program has errors. */
1da2ed5f 1510 && !seen_error ());
ea900239
DB
1511}
1512
27a4cd48
DM
1513namespace {
1514
1515const pass_data pass_data_ipa_pure_const =
ea900239 1516{
27a4cd48
DM
1517 IPA_PASS, /* type */
1518 "pure-const", /* name */
1519 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
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 */
ea900239 1527};
33977f81 1528
27a4cd48
DM
1529class pass_ipa_pure_const : public ipa_opt_pass_d
1530{
1531public:
c3284718
RS
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 */
27a4cd48
DM
1543 {}
1544
1545 /* opt_pass methods: */
1a3d085c 1546 virtual bool gate (function *) { return gate_pure_const (); }
be55bfe6 1547 virtual unsigned int execute (function *) { return propagate (); }
27a4cd48
DM
1548
1549}; // class pass_ipa_pure_const
1550
1551} // anon namespace
1552
1553ipa_opt_pass_d *
1554make_pass_ipa_pure_const (gcc::context *ctxt)
1555{
1556 return new pass_ipa_pure_const (ctxt);
1557}
1558
5dc16b19 1559/* Return true if function should be skipped for local pure const analysis. */
33977f81 1560
5dc16b19
MLI
1561static bool
1562skip_function_for_local_pure_const (struct cgraph_node *node)
33977f81 1563{
33977f81
JH
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");
5dc16b19 1571 return true;
33977f81 1572 }
20cdc2be 1573 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
33977f81
JH
1574 {
1575 if (dump_file)
61502ca8 1576 fprintf (dump_file, "Function is not available or overwritable; not analyzing.\n");
5dc16b19 1577 return true;
33977f81 1578 }
5dc16b19
MLI
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. */
33977f81 1585
be55bfe6
TS
1586namespace {
1587
1588const 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
1602class pass_local_pure_const : public gimple_opt_pass
1603{
1604public:
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
1616unsigned int
1617pass_local_pure_const::execute (function *fun)
5dc16b19
MLI
1618{
1619 bool changed = false;
1620 funct_state l;
1621 bool skip;
1622 struct cgraph_node *node;
1623
581985d7 1624 node = cgraph_get_node (current_function_decl);
5dc16b19
MLI
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;
73add7fe 1630
269c80f2
JJ
1631 l = analyze_function (node, false);
1632
1633 /* Do NORETURN discovery. */
73add7fe 1634 if (!skip && !TREE_THIS_VOLATILE (current_function_decl)
be55bfe6 1635 && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0)
73add7fe 1636 {
be55bfe6 1637 warn_function_noreturn (fun->decl);
73add7fe 1638 if (dump_file)
be55bfe6
TS
1639 fprintf (dump_file, "Function found to be noreturn: %s\n",
1640 current_function_name ());
73add7fe
JH
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)
be55bfe6 1645 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
73add7fe
JH
1646
1647 changed = true;
1648 }
c59f5d1b 1649
33977f81
JH
1650 switch (l->pure_const_state)
1651 {
1652 case IPA_CONST:
1653 if (!TREE_READONLY (current_function_decl))
1654 {
5dc16b19
MLI
1655 warn_function_const (current_function_decl, !l->looping);
1656 if (!skip)
1657 {
530f3a1b 1658 cgraph_set_const_flag (node, true, l->looping);
5dc16b19
MLI
1659 changed = true;
1660 }
33977f81
JH
1661 if (dump_file)
1662 fprintf (dump_file, "Function found to be %sconst: %s\n",
1663 l->looping ? "looping " : "",
df92c640 1664 current_function_name ());
33977f81
JH
1665 }
1666 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1667 && !l->looping)
1668 {
5dc16b19
MLI
1669 if (!skip)
1670 {
530f3a1b 1671 cgraph_set_const_flag (node, true, false);
5dc16b19
MLI
1672 changed = true;
1673 }
33977f81
JH
1674 if (dump_file)
1675 fprintf (dump_file, "Function found to be non-looping: %s\n",
df92c640 1676 current_function_name ());
33977f81
JH
1677 }
1678 break;
1679
1680 case IPA_PURE:
5dc16b19 1681 if (!DECL_PURE_P (current_function_decl))
33977f81 1682 {
5dc16b19
MLI
1683 if (!skip)
1684 {
530f3a1b 1685 cgraph_set_pure_flag (node, true, l->looping);
5dc16b19
MLI
1686 changed = true;
1687 }
1688 warn_function_pure (current_function_decl, !l->looping);
33977f81
JH
1689 if (dump_file)
1690 fprintf (dump_file, "Function found to be %spure: %s\n",
1691 l->looping ? "looping " : "",
df92c640 1692 current_function_name ());
33977f81
JH
1693 }
1694 else if (DECL_LOOPING_CONST_OR_PURE_P (current_function_decl)
1695 && !l->looping)
1696 {
5dc16b19
MLI
1697 if (!skip)
1698 {
530f3a1b 1699 cgraph_set_pure_flag (node, true, false);
5dc16b19
MLI
1700 changed = true;
1701 }
33977f81
JH
1702 if (dump_file)
1703 fprintf (dump_file, "Function found to be non-looping: %s\n",
df92c640 1704 current_function_name ());
33977f81
JH
1705 }
1706 break;
1707
1708 default:
1709 break;
1710 }
1711 if (!l->can_throw && !TREE_NOTHROW (current_function_decl))
1712 {
20cdc2be 1713 cgraph_set_nothrow_flag (node, true);
33977f81
JH
1714 changed = true;
1715 if (dump_file)
1716 fprintf (dump_file, "Function found to be nothrow: %s\n",
df92c640 1717 current_function_name ());
33977f81 1718 }
04695783 1719 free (l);
33977f81
JH
1720 if (changed)
1721 return execute_fixup_cfg ();
1722 else
1723 return 0;
1724}
1725
27a4cd48
DM
1726} // anon namespace
1727
1728gimple_opt_pass *
1729make_pass_local_pure_const (gcc::context *ctxt)
1730{
1731 return new pass_local_pure_const (ctxt);
1732}
c1bf2a39
AM
1733
1734/* Emit noreturn warnings. */
1735
c1bf2a39
AM
1736namespace {
1737
1738const pass_data pass_data_warn_function_noreturn =
1739{
1740 GIMPLE_PASS, /* type */
1741 "*warn_function_noreturn", /* name */
1742 OPTGROUP_NONE, /* optinfo_flags */
c1bf2a39
AM
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
1752class pass_warn_function_noreturn : public gimple_opt_pass
1753{
1754public:
1755 pass_warn_function_noreturn (gcc::context *ctxt)
1756 : gimple_opt_pass (pass_data_warn_function_noreturn, ctxt)
1757 {}
1758
1759 /* opt_pass methods: */
1a3d085c 1760 virtual bool gate (function *) { return warn_suggest_attribute_noreturn; }
be55bfe6
TS
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 }
c1bf2a39
AM
1768
1769}; // class pass_warn_function_noreturn
1770
1771} // anon namespace
1772
1773gimple_opt_pass *
1774make_pass_warn_function_noreturn (gcc::context *ctxt)
1775{
1776 return new pass_warn_function_noreturn (ctxt);
1777}
1778
1779