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