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