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