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